code reorganisation phase-I
[csql.git] / src / storage / SessionImpl.cxx
blob6de2740412eecbe69fb7551008c82956752ff014
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
19 #include<Database.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
24 #include<Debug.h>
25 #include<Config.h>
26 #include<Process.h>
28 //Before calling this method, application is required to call readConfigValues
29 DbRetVal SessionImpl::initSystemDatabase()
32 long value=0;
34 DbRetVal rv = OK;
35 rv = readConfigFile();
36 if (rv != OK)
38 printError(ErrSysInit, "Configuration file read failed\n");
39 return ErrSysInit;
42 // Conf::config.print();
44 dbMgr = new DatabaseManagerImpl();
45 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
46 if (OK != rv) return rv;
48 dbMgr->setSysDb(dbMgr->db());
49 dbMgr->setDb(NULL);
51 Database *db = dbMgr->sysDb();
53 rv = db->getDatabaseMutex();
54 if (OK != rv)
56 printError(ErrLockTimeOut, "Unable to get Database Mutex");
57 return rv;
61 db->createAllCatalogTables();
63 //create the default dba user
64 CatalogTableUSER cUser(db);
65 rv = cUser.insert(I_USER, I_PASS);
66 if (OK != rv)
68 db->releaseDatabaseMutex();
69 return rv;
72 rv = cUser.insert(DBAUSER, DBAPASS);
73 if (OK != rv)
75 db->releaseDatabaseMutex();
76 return rv;
78 void *ret = NULL;
79 //Allocate space for the lock hash bucket
80 ret = db->allocLockHashBuckets();
81 if (NULL == ret)
83 db->releaseDatabaseMutex();
84 printError(ErrSysInit, "Allocation of Lock buckets failed");
85 return ErrSysInit;
87 db->releaseDatabaseMutex();
89 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config.getMaxSysDbSize()/1048576, Conf::config.getMaxDbSize()/1048576);
90 //create user database
91 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
92 if (OK != rv) return rv;
93 return OK;
96 DbRetVal SessionImpl::destroySystemDatabase()
98 DbRetVal rv = OK;
99 rv = dbMgr->deleteDatabase(SYSTEMDB);
100 if (OK != rv) return rv;
101 rv = dbMgr->deleteDatabase("userdb");
102 if (OK != rv) return rv;
103 delete dbMgr;
104 dbMgr = NULL;
105 return OK;
108 DbRetVal SessionImpl::open(const char *username, const char *password)
110 DbRetVal rv = OK;
111 rv = readConfigFile();
112 if (rv != OK)
114 printError(ErrSysFatal, "Configuration file read failed\n");
115 return ErrSysFatal;
118 if ( NULL == dbMgr)
120 dbMgr = new DatabaseManagerImpl();
121 rv = dbMgr->openSystemDatabase();
123 if (OK != rv)
125 printError(rv,"Unable to open the system database");
126 return rv;
129 rv = authenticate(username, password);
130 if (OK != rv)
132 dbMgr->closeSystemDatabase();
133 delete dbMgr; dbMgr = NULL;
134 return rv;
137 dbMgr->createTransactionManager();
138 dbMgr->createLockManager();
139 rv = dbMgr->registerThread();
140 if (OK != rv)
142 printError(rv,"Unable to register to csql server");
143 dbMgr->closeSystemDatabase();
144 delete dbMgr; dbMgr = NULL;
145 return rv;
147 rv = dbMgr->openDatabase("userdb");
148 if (OK != rv) {
149 dbMgr->closeSystemDatabase();
150 delete dbMgr; dbMgr = NULL;
151 return rv;
153 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
154 //ProcessManager::systemDatabase = dbMgr->sysDb();
155 isXTaken = false;
156 return OK;
158 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
160 DbRetVal rv = dbMgr->sysDb()->getDatabaseMutex(false);
161 if (OK != rv)
163 printError(ErrLockTimeOut,"System under recovery or DDL operation going on.");
164 printError(ErrLockTimeOut,"Unable to get the database mutex.Retry...");
165 return ErrLockTimeOut;
167 CatalogTableUSER cUser(dbMgr->sysDb());
168 cUser.authenticate(username, password, isAuthenticated, isDba);
169 strcpy(userName, username);
170 dbMgr->sysDb()->releaseDatabaseMutex(false);
171 if (!isAuthenticated)
173 printError(ErrNoPrivilege,"User Authentication failed");
174 return ErrNoPrivilege;
176 return OK;
178 DbRetVal SessionImpl::getExclusiveLock()
180 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
181 if (OK != rv) {
182 printError(ErrLockTimeOut, "Unable to acquire proc table mutex");
183 return rv;
185 if (dbMgr->isAnyOneRegistered()) {
186 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
187 dbMgr->sysDb()->releaseProcessTableMutex(true);
188 return ErrLockTimeOut;
190 isXTaken = true;
191 return rv;
193 DbRetVal SessionImpl::close()
195 DbRetVal rv = OK;
196 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
197 if (dbMgr)
199 rv = dbMgr->closeDatabase();
200 if (rv != OK) { return ErrBadCall; }
201 rv = dbMgr->deregisterThread();
202 if (rv != OK) { return ErrBadCall; }
203 rv = dbMgr->closeSystemDatabase();
204 if (rv != OK) { return ErrBadCall; }
205 delete dbMgr;
206 dbMgr = NULL;
208 if (uMgr)
210 delete uMgr;
211 uMgr = NULL;
213 isXTaken = false;
214 return OK;
217 DatabaseManager* SessionImpl::getDatabaseManager()
219 if (isAuthenticated) return dbMgr;
220 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
221 return NULL;
224 UserManager* SessionImpl::getUserManager()
226 if (!isAuthenticated)
228 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
229 return NULL;
231 if (uMgr != NULL) return uMgr;
232 UserManagerImpl *userMgr = new UserManagerImpl();
233 if(0 == strcmp(userName, DBAUSER))
234 userMgr->setDba(true);
235 else
236 userMgr->setDba(false);
238 userMgr->setSysDb(dbMgr->sysDb());
240 userMgr->setUserName(userName);
241 uMgr = userMgr;
242 return userMgr;
245 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
247 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
249 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
250 return ErrSysFatal;
252 DbRetVal rv = OK;
254 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
255 return rv;
259 DbRetVal SessionImpl::commit()
261 DbRetVal rv = OK;
262 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
264 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
265 return ErrSysFatal;
267 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
268 if (OK != rv)
270 printError(rv,"Transaction commit failed\n");
271 return rv;
273 return OK;
277 DbRetVal SessionImpl::rollback()
279 DbRetVal rv = OK;
280 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
282 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
283 return ErrSysFatal;
285 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
286 if (OK != rv)
288 printError(rv, "Transaction rollback failed\n");
289 return rv;
291 return OK;
294 DbRetVal SessionImpl::readConfigFile()
296 // Check if env variable is set or not
297 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
298 if (confFilename == NULL)
300 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
301 confFilename = DEFAULT_CONFIG_FILE;
303 else {
304 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
305 "should be set.");
306 return ErrSysInit;
310 int rv = Conf::config.readAllValues(confFilename);
311 if (rv != 0) return ErrSysInit;
312 return OK;
314 Database* SessionImpl::getSystemDatabase()
316 return dbMgr->sysDb();