Feature: 1501518
[csql.git] / src / server / SessionImpl.cxx
blob424427047a77bf21f24aa97c342a1de020813a7d
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>
27 //Before calling this method, application is required to call readConfigValues
28 DbRetVal SessionImpl::initSystemDatabase()
31 DbRetVal rv = OK;
32 rv = readConfigFile();
33 if (rv != OK)
35 printError(ErrSysInit, "Configuration file read failed\n");
36 return ErrSysInit;
38 printf("ConfigValues\n");
39 printf(" getPageSize %d\n", config.getPageSize());
40 printf(" getMaxTrans %d\n", config.getMaxTrans());
41 printf(" getMaxProcs %d\n", config.getMaxProcs());
42 printf(" getMaxSysDbSize %ld\n", config.getMaxSysDbSize());
43 printf(" getMaxDbSize %ld\n", config.getMaxDbSize());
44 printf(" getSysDbKey %d\n", config.getSysDbKey());
45 printf(" getUserDbKey %d\n", config.getUserDbKey());
46 printf(" getLogFile %s\n", config.getLogFile());
47 printf(" getMapAddress %ld\n", config.getMapAddress());
48 printf(" getMutexSecs %d\n", config.getMutexSecs());
49 printf(" getMutexUSecs %d\n", config.getMutexUSecs());
52 dbMgr = new DatabaseManagerImpl();
54 //TODO:Size of system database 100 MB ->config parameter
56 //TODO:No of chunks of system database->config parameter
57 //This limits the total number of catalog tables system shall support.
58 rv = dbMgr->createDatabase(SYSTEMDB, config.getMaxSysDbSize());
59 if (OK != rv) return rv;
60 dbMgr->setSysDb(dbMgr->db());
61 dbMgr->setDb(NULL);
63 Database *db = dbMgr->sysDb();
65 rv = db->getDatabaseMutex();
66 if (OK != rv)
68 printError(ErrLockTimeOut, "Unable to get Database Mutex");
69 return rv;
73 db->createAllCatalogTables();
75 //create the default dba user
76 CatalogTableUSER cUser(db);
77 rv = cUser.insert(DBAUSER, DBAPASS);
78 if (OK != rv)
80 db->releaseDatabaseMutex();
81 return rv;
83 void *ret = NULL;
84 //Allocate space for the lock hash bucket
85 ret = db->allocLockHashBuckets();
86 if (NULL == ret)
88 db->releaseDatabaseMutex();
89 printError(ErrSysInit, "Allocation of Lock buckets failed");
90 return ErrSysInit;
93 db->releaseDatabaseMutex();
95 //create user database
96 rv = dbMgr->createDatabase("praba", config.getMaxDbSize());
97 if (OK != rv) return rv;
98 return OK;
101 DbRetVal SessionImpl::destroySystemDatabase()
103 DbRetVal rv = OK;
104 rv = dbMgr->deleteDatabase(SYSTEMDB);
105 if (OK != rv) return rv;
106 rv = dbMgr->deleteDatabase("praba");
107 if (OK != rv) return rv;
108 return OK;
111 DbRetVal SessionImpl::open(const char *username, const char *password)
113 DbRetVal rv = OK;
114 rv = readConfigFile();
115 if (rv != OK)
117 printError(ErrSysFatal, "Configuration file read failed\n");
118 return ErrSysFatal;
120 if ( NULL == dbMgr)
122 dbMgr = new DatabaseManagerImpl();
123 rv = dbMgr->openSystemDatabase();
125 if (OK != rv)
127 printError(rv,"Unable to open the system database");
128 return rv;
132 rv = dbMgr->sysDb()->getDatabaseMutex();
133 if (OK != rv)
135 printError(rv,"Unable to get database mutex");
136 return rv;
139 CatalogTableUSER cUser(dbMgr->sysDb());
140 cUser.authenticate(username, password, isAuthenticated, isDba);
141 //isAuthenticated=true;
142 dbMgr->sysDb()->releaseDatabaseMutex();
143 if (!isAuthenticated)
145 dbMgr->deregisterThread();
146 dbMgr->closeSystemDatabase();
147 delete dbMgr;
148 dbMgr = NULL;
149 printError(ErrNoPrivilege,"User Authentication failed");
150 return ErrNoPrivilege;
153 dbMgr->createTransactionManager();
154 dbMgr->createLockManager();
155 rv = dbMgr->openDatabase("praba");
156 if (OK != rv) return rv;
158 rv = dbMgr->registerThread();
159 if (OK != rv)
161 printError(rv,"Unable to register to csql server");
162 return rv;
164 return OK;
167 DbRetVal SessionImpl::close()
169 DbRetVal rv = OK;
170 if (dbMgr)
172 rv = dbMgr->closeDatabase();
173 if (rv != OK) return ErrBadCall;
174 rv = dbMgr->closeSystemDatabase();
175 if (rv != OK) return ErrBadCall;
176 rv = dbMgr->deregisterThread();
177 if (rv != OK) return ErrBadCall;
178 delete dbMgr;
179 dbMgr = NULL;
181 if (uMgr)
183 delete uMgr;
184 uMgr = NULL;
186 return OK;
189 DatabaseManager* SessionImpl::getDatabaseManager()
191 if (isAuthenticated) return dbMgr;
192 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
193 return NULL;
196 UserManager* SessionImpl::getUserManager()
198 if (!isAuthenticated)
200 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
201 return NULL;
203 if (uMgr != NULL) return uMgr;
204 UserManagerImpl *userMgr = new UserManagerImpl();
205 if(0 == strcmp(userName, DBAUSER))
206 userMgr->setDba(true);
207 else
208 userMgr->setDba(false);
210 userMgr->setSysDb(dbMgr->sysDb());
212 userMgr->setUserName(userName);
213 uMgr = userMgr;
214 return userMgr;
217 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
219 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
221 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
222 return ErrSysFatal;
224 DbRetVal rv = OK;
225 rv = dbMgr->sysDb()->getTransTableMutex();
226 if (OK != rv)
228 printError(rv,"Unable to get TransTable mutex\n");
229 return rv;
231 rv = dbMgr->txnMgr()->startTransaction(level);
232 dbMgr->sysDb()->releaseTransTableMutex();
233 return rv;
237 DbRetVal SessionImpl::commit()
239 DbRetVal rv = OK;
240 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
242 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
243 return ErrSysFatal;
245 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
246 if (OK != rv)
248 printError(rv,"Unable to get TransTable mutex\n");
249 return rv;
251 return OK;
255 DbRetVal SessionImpl::rollback()
257 DbRetVal rv = OK;
258 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
260 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
261 return ErrSysFatal;
263 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
264 if (OK != rv)
266 printError(rv,"Unable to get TransTable mutex\n");
267 return rv;
269 return OK;
272 DbRetVal SessionImpl::readConfigFile()
274 // Check if env variable is set or not
275 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
276 if (confFilename == NULL)
278 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
279 return ErrSysInit;
282 int rv = config.readAllValues(confFilename);
283 if (rv != 0) return ErrSysInit;
284 return OK;