code reorg
[csql.git] / src / server / SessionImpl.cxx
blobfe3d8d0ddc6b01aae113e4db5babb74dc64c37e6
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 return OK;
147 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
149 DbRetVal rv = dbMgr->sysDb()->getDatabaseMutex(false);
150 if (OK != rv)
152 printError(rv,"Unable to get database mutex");
153 return rv;
155 CatalogTableUSER cUser(dbMgr->sysDb());
156 cUser.authenticate(username, password, isAuthenticated, isDba);
157 strcpy(userName, username);
158 dbMgr->sysDb()->releaseDatabaseMutex(false);
159 if (!isAuthenticated)
161 printError(ErrNoPrivilege,"User Authentication failed");
162 return ErrNoPrivilege;
164 return OK;
166 DbRetVal SessionImpl::close()
168 DbRetVal rv = OK;
169 if (dbMgr)
171 rv = dbMgr->closeDatabase();
172 if (rv != OK) { return ErrBadCall; }
173 rv = dbMgr->deregisterThread();
174 if (rv != OK) { return ErrBadCall; }
175 rv = dbMgr->closeSystemDatabase();
176 if (rv != OK) { return ErrBadCall; }
177 delete dbMgr;
178 dbMgr = NULL;
180 if (uMgr)
182 delete uMgr;
183 uMgr = NULL;
185 return OK;
188 DatabaseManager* SessionImpl::getDatabaseManager()
190 if (isAuthenticated) return dbMgr;
191 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
192 return NULL;
195 UserManager* SessionImpl::getUserManager()
197 if (!isAuthenticated)
199 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
200 return NULL;
202 if (uMgr != NULL) return uMgr;
203 UserManagerImpl *userMgr = new UserManagerImpl();
204 if(0 == strcmp(userName, DBAUSER))
205 userMgr->setDba(true);
206 else
207 userMgr->setDba(false);
209 userMgr->setSysDb(dbMgr->sysDb());
211 userMgr->setUserName(userName);
212 uMgr = userMgr;
213 return userMgr;
216 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
218 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
220 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
221 return ErrSysFatal;
223 DbRetVal rv = OK;
225 rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
226 return rv;
230 DbRetVal SessionImpl::commit()
232 DbRetVal rv = OK;
233 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
235 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
236 return ErrSysFatal;
238 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
239 if (OK != rv)
241 printError(rv,"Transaction commit failed\n");
242 return rv;
244 return OK;
248 DbRetVal SessionImpl::rollback()
250 DbRetVal rv = OK;
251 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
253 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
254 return ErrSysFatal;
256 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
257 if (OK != rv)
259 printError(rv, "Transaction rollback failed\n");
260 return rv;
262 return OK;
265 DbRetVal SessionImpl::readConfigFile()
267 // Check if env variable is set or not
268 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
269 if (confFilename == NULL)
271 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
272 return ErrSysInit;
275 int rv = Conf::config.readAllValues(confFilename);
276 if (rv != 0) return ErrSysInit;
277 return OK;
279 Database* SessionImpl::getSystemDatabase()
281 return dbMgr->sysDb();