Removing dependency for Cache module in MMDB build
[csql.git] / src / storage / SessionImpl.cxx
blob25d7549bde9600f00193e08e4c4843149f59763c
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 if (dbMgr->isAnyOneRegistered()) {
181 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
182 return ErrLockTimeOut;
184 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
185 if (OK == rv) isXTaken = true;
186 return rv;
188 DbRetVal SessionImpl::close()
190 DbRetVal rv = OK;
191 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
192 if (dbMgr)
194 rv = dbMgr->closeDatabase();
195 if (rv != OK) { return ErrBadCall; }
196 rv = dbMgr->deregisterThread();
197 if (rv != OK) { return ErrBadCall; }
198 rv = dbMgr->closeSystemDatabase();
199 if (rv != OK) { return ErrBadCall; }
200 delete dbMgr;
201 dbMgr = NULL;
203 if (uMgr)
205 delete uMgr;
206 uMgr = NULL;
208 return OK;
211 DatabaseManager* SessionImpl::getDatabaseManager()
213 if (isAuthenticated) return dbMgr;
214 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
215 return NULL;
218 UserManager* SessionImpl::getUserManager()
220 if (!isAuthenticated)
222 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
223 return NULL;
225 if (uMgr != NULL) return uMgr;
226 UserManagerImpl *userMgr = new UserManagerImpl();
227 if(0 == strcmp(userName, DBAUSER))
228 userMgr->setDba(true);
229 else
230 userMgr->setDba(false);
232 userMgr->setSysDb(dbMgr->sysDb());
234 userMgr->setUserName(userName);
235 uMgr = userMgr;
236 return userMgr;
239 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
241 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
243 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
244 return ErrSysFatal;
246 DbRetVal rv = OK;
248 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
249 return rv;
253 DbRetVal SessionImpl::commit()
255 DbRetVal rv = OK;
256 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
258 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
259 return ErrSysFatal;
261 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
262 if (OK != rv)
264 printError(rv,"Transaction commit failed\n");
265 return rv;
267 return OK;
271 DbRetVal SessionImpl::rollback()
273 DbRetVal rv = OK;
274 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
276 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
277 return ErrSysFatal;
279 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
280 if (OK != rv)
282 printError(rv, "Transaction rollback failed\n");
283 return rv;
285 return OK;
288 DbRetVal SessionImpl::readConfigFile()
290 // Check if env variable is set or not
291 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
292 if (confFilename == NULL)
294 if (os::fileExists(DEFAULT_CONFIG_FILE)) {
295 confFilename = DEFAULT_CONFIG_FILE;
297 else {
298 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable "
299 "should be set.");
300 return ErrSysInit;
304 int rv = Conf::config.readAllValues(confFilename);
305 if (rv != 0) return ErrSysInit;
306 return OK;
308 Database* SessionImpl::getSystemDatabase()
310 return dbMgr->sysDb();