Procmgmt second patch:
[csql.git] / src / server / SessionImpl.cxx
blob4718cdcc7b896e279dacf273ea612ea8c2a59f53
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;
39 printf("ConfigValues\n");
40 printf(" getPageSize %d\n", Conf::config.getPageSize());
41 printf(" getMaxTrans %d\n", Conf::config.getMaxTrans());
42 printf(" getMaxProcs %d\n", Conf::config.getMaxProcs());
43 printf(" getMaxSysDbSize %ld\n", Conf::config.getMaxSysDbSize());
44 printf(" getMaxDbSize %ld\n", Conf::config.getMaxDbSize());
45 printf(" getSysDbKey %d\n", Conf::config.getSysDbKey());
46 printf(" getUserDbKey %d\n", Conf::config.getUserDbKey());
47 printf(" getLogFile %s\n", Conf::config.getLogFile());
48 printf(" getMapAddress %ld\n", Conf::config.getMapAddress());
49 printf(" getMutexSecs %d\n", Conf::config.getMutexSecs());
50 printf(" getMutexUSecs %d\n", Conf::config.getMutexUSecs());
53 dbMgr = new DatabaseManagerImpl();
55 rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
56 if (OK != rv) return rv;
57 dbMgr->setSysDb(dbMgr->db());
58 dbMgr->setDb(NULL);
60 Database *db = dbMgr->sysDb();
62 rv = db->getDatabaseMutex();
63 if (OK != rv)
65 printError(ErrLockTimeOut, "Unable to get Database Mutex");
66 return rv;
70 db->createAllCatalogTables();
72 //create the default dba user
73 CatalogTableUSER cUser(db);
74 rv = cUser.insert(DBAUSER, DBAPASS);
75 if (OK != rv)
77 db->releaseDatabaseMutex();
78 return rv;
80 void *ret = NULL;
81 //Allocate space for the lock hash bucket
82 ret = db->allocLockHashBuckets();
83 if (NULL == ret)
85 db->releaseDatabaseMutex();
86 printError(ErrSysInit, "Allocation of Lock buckets failed");
87 return ErrSysInit;
90 db->releaseDatabaseMutex();
92 //create user database
93 rv = dbMgr->createDatabase("praba", Conf::config.getMaxDbSize());
94 if (OK != rv) return rv;
95 return OK;
98 DbRetVal SessionImpl::destroySystemDatabase()
100 DbRetVal rv = OK;
101 rv = dbMgr->deleteDatabase(SYSTEMDB);
102 if (OK != rv) return rv;
103 rv = dbMgr->deleteDatabase("praba");
104 if (OK != rv) return rv;
105 delete dbMgr;
106 dbMgr = NULL;
107 return OK;
110 DbRetVal SessionImpl::open(const char *username, const char *password)
112 DbRetVal rv = OK;
113 rv = readConfigFile();
114 if (rv != OK)
116 printError(ErrSysFatal, "Configuration file read failed\n");
117 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;
131 rv = dbMgr->sysDb()->getDatabaseMutex();
132 if (OK != rv)
134 printError(rv,"Unable to get database mutex");
135 return rv;
138 CatalogTableUSER cUser(dbMgr->sysDb());
139 cUser.authenticate(username, password, isAuthenticated, isDba);
140 strcpy(userName, username);
141 //isAuthenticated=true;
142 dbMgr->sysDb()->releaseDatabaseMutex();
143 if (!isAuthenticated)
145 dbMgr->closeSystemDatabase();
146 delete dbMgr;
147 dbMgr = NULL;
148 printError(ErrNoPrivilege,"User Authentication failed");
149 return ErrNoPrivilege;
152 dbMgr->createTransactionManager();
153 dbMgr->createLockManager();
154 rv = dbMgr->openDatabase("praba");
155 if (OK != rv) {
156 return rv;
159 rv = dbMgr->registerThread();
160 if (OK != rv)
162 printError(rv,"Unable to register to csql server");
163 return rv;
165 return OK;
168 DbRetVal SessionImpl::close()
170 DbRetVal rv = OK;
171 if (dbMgr)
173 rv = dbMgr->deregisterThread();
174 if (rv != OK) { return ErrBadCall; }
175 rv = dbMgr->closeDatabase();
176 if (rv != OK) { return ErrBadCall; }
177 rv = dbMgr->closeSystemDatabase();
178 if (rv != OK) { return ErrBadCall; }
179 delete dbMgr;
180 dbMgr = NULL;
182 if (uMgr)
184 delete uMgr;
185 uMgr = NULL;
187 return OK;
190 DatabaseManager* SessionImpl::getDatabaseManager()
192 if (isAuthenticated) return dbMgr;
193 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
194 return NULL;
197 UserManager* SessionImpl::getUserManager()
199 if (!isAuthenticated)
201 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
202 return NULL;
204 if (uMgr != NULL) return uMgr;
205 UserManagerImpl *userMgr = new UserManagerImpl();
206 if(0 == strcmp(userName, DBAUSER))
207 userMgr->setDba(true);
208 else
209 userMgr->setDba(false);
211 userMgr->setSysDb(dbMgr->sysDb());
213 userMgr->setUserName(userName);
214 uMgr = userMgr;
215 return userMgr;
218 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
220 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
222 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
223 return ErrSysFatal;
225 DbRetVal rv = OK;
226 rv = dbMgr->sysDb()->getTransTableMutex();
227 if (OK != rv)
229 printError(rv,"Unable to get TransTable mutex\n");
230 return rv;
232 rv = dbMgr->txnMgr()->startTransaction(level);
233 dbMgr->sysDb()->releaseTransTableMutex();
234 return rv;
238 DbRetVal SessionImpl::commit()
240 DbRetVal rv = OK;
241 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
243 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
244 return ErrSysFatal;
246 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
247 if (OK != rv)
249 printError(rv,"Unable to get TransTable mutex\n");
250 return rv;
252 return OK;
256 DbRetVal SessionImpl::rollback()
258 DbRetVal rv = OK;
259 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
261 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
262 return ErrSysFatal;
264 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
265 if (OK != rv)
267 printError(rv,"Unable to get TransTable mutex\n");
268 return rv;
270 return OK;
273 DbRetVal SessionImpl::readConfigFile()
275 // Check if env variable is set or not
276 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
277 if (confFilename == NULL)
279 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
280 return ErrSysInit;
283 int rv = Conf::config.readAllValues(confFilename);
284 if (rv != 0) return ErrSysInit;
285 return OK;