Enterprise to opensource. 40 files including Makefil.am and .in from storage, sqllog...
[csql.git] / src / storage / SessionImpl.cxx
blob9c95867bbae4a8f9f2034f0b158c3db5b8b99b4b
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 DbRetVal rv = OK;
33 rv = readConfigFile();
34 if (rv != OK)
36 printError(ErrSysInit, "Configuration file read failed\n");
37 return ErrSysInit;
40 Conf::config.print();
42 dbMgr = new DatabaseManagerImpl();
43 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
44 if (OK != rv) return rv;
45 dbMgr->setSysDb(dbMgr->db());
46 dbMgr->setDb(NULL);
48 Database *db = dbMgr->sysDb();
50 rv = db->getDatabaseMutex();
51 if (OK != rv)
53 printError(ErrLockTimeOut, "Unable to get Database Mutex");
54 return rv;
58 db->createAllCatalogTables();
60 //create the default dba user
61 CatalogTableUSER cUser(db);
62 rv = cUser.insert(DBAUSER, DBAPASS);
63 if (OK != rv)
65 db->releaseDatabaseMutex();
66 return rv;
68 void *ret = NULL;
69 //Allocate space for the lock hash bucket
70 ret = db->allocLockHashBuckets();
71 if (NULL == ret)
73 db->releaseDatabaseMutex();
74 printError(ErrSysInit, "Allocation of Lock buckets failed");
75 return ErrSysInit;
78 db->releaseDatabaseMutex();
79 printf("sysdb size %ld dbsize %ld\n", Conf::config.getMaxSysDbSize(), Conf::config.getMaxDbSize());
80 //create user database
81 rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
82 if (OK != rv) return rv;
83 return OK;
86 DbRetVal SessionImpl::destroySystemDatabase()
88 DbRetVal rv = OK;
89 rv = dbMgr->deleteDatabase(SYSTEMDB);
90 if (OK != rv) return rv;
91 rv = dbMgr->deleteDatabase("userdb");
92 if (OK != rv) return rv;
93 delete dbMgr;
94 dbMgr = NULL;
95 return OK;
98 DbRetVal SessionImpl::open(const char *username, const char *password)
100 DbRetVal rv = OK;
101 rv = readConfigFile();
102 if (rv != OK)
104 printError(ErrSysFatal, "Configuration file read failed\n");
105 return ErrSysFatal;
108 if ( NULL == dbMgr)
110 dbMgr = new DatabaseManagerImpl();
111 rv = dbMgr->openSystemDatabase();
113 if (OK != rv)
115 printError(rv,"Unable to open the system database");
116 return rv;
119 rv = authenticate(username, password);
120 if (OK != rv)
122 dbMgr->closeSystemDatabase();
123 delete dbMgr; dbMgr = NULL;
124 return rv;
127 dbMgr->createTransactionManager();
128 dbMgr->createLockManager();
129 rv = dbMgr->registerThread();
130 if (OK != rv)
132 printError(rv,"Unable to register to csql server");
133 dbMgr->closeSystemDatabase();
134 delete dbMgr; dbMgr = NULL;
135 return rv;
137 rv = dbMgr->openDatabase("userdb");
138 if (OK != rv) {
139 dbMgr->closeSystemDatabase();
140 delete dbMgr; dbMgr = NULL;
141 return rv;
143 ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
144 //ProcessManager::systemDatabase = dbMgr->sysDb();
145 isXTaken = false;
146 return OK;
148 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
150 DbRetVal rv = dbMgr->sysDb()->getDatabaseMutex(false);
151 if (OK != rv)
153 printError(rv,"Unable to get database mutex");
154 return rv;
156 CatalogTableUSER cUser(dbMgr->sysDb());
157 cUser.authenticate(username, password, isAuthenticated, isDba);
158 strcpy(userName, username);
159 dbMgr->sysDb()->releaseDatabaseMutex(false);
160 if (!isAuthenticated)
162 printError(ErrNoPrivilege,"User Authentication failed");
163 return ErrNoPrivilege;
165 return OK;
167 DbRetVal SessionImpl::getExclusiveLock()
169 if (dbMgr->isAnyOneRegistered()) {
170 printError(ErrLockTimeOut, "Unable to acquire exclusive lock. somebody is connected");
171 return ErrLockTimeOut;
173 DbRetVal rv = dbMgr->sysDb()->getProcessTableMutex(true);
174 if (OK == rv) isXTaken = true;
175 return rv;
177 DbRetVal SessionImpl::close()
179 DbRetVal rv = OK;
180 if (isXTaken && dbMgr ) dbMgr->sysDb()->releaseProcessTableMutex(true);
181 if (dbMgr)
183 rv = dbMgr->closeDatabase();
184 if (rv != OK) { return ErrBadCall; }
185 rv = dbMgr->deregisterThread();
186 if (rv != OK) { return ErrBadCall; }
187 rv = dbMgr->closeSystemDatabase();
188 if (rv != OK) { return ErrBadCall; }
189 delete dbMgr;
190 dbMgr = NULL;
192 if (uMgr)
194 delete uMgr;
195 uMgr = NULL;
197 return OK;
200 DatabaseManager* SessionImpl::getDatabaseManager()
202 if (isAuthenticated) return dbMgr;
203 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
204 return NULL;
207 UserManager* SessionImpl::getUserManager()
209 if (!isAuthenticated)
211 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
212 return NULL;
214 if (uMgr != NULL) return uMgr;
215 UserManagerImpl *userMgr = new UserManagerImpl();
216 if(0 == strcmp(userName, DBAUSER))
217 userMgr->setDba(true);
218 else
219 userMgr->setDba(false);
221 userMgr->setSysDb(dbMgr->sysDb());
223 userMgr->setUserName(userName);
224 uMgr = userMgr;
225 return userMgr;
228 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
230 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
232 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
233 return ErrSysFatal;
235 DbRetVal rv = OK;
237 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
238 return rv;
242 DbRetVal SessionImpl::commit()
244 DbRetVal rv = OK;
245 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
247 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
248 return ErrSysFatal;
250 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
251 if (OK != rv)
253 printError(rv,"Transaction commit failed\n");
254 return rv;
256 return OK;
260 DbRetVal SessionImpl::rollback()
262 DbRetVal rv = OK;
263 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
265 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
266 return ErrSysFatal;
268 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
269 if (OK != rv)
271 printError(rv, "Transaction rollback failed\n");
272 return rv;
274 return OK;
277 DbRetVal SessionImpl::readConfigFile()
279 // Check if env variable is set or not
280 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
281 if (confFilename == NULL)
283 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
284 return ErrSysInit;
287 int rv = Conf::config.readAllValues(confFilename);
288 if (rv != 0) return ErrSysInit;
289 return OK;
291 Database* SessionImpl::getSystemDatabase()
293 return dbMgr->sysDb();