Feature Request ID: 1669025
[csql.git] / src / server / SessionImpl.c
blob1224f3d64b4c9165e0861f3417df27ed33d9a6fb
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 db->getDatabaseMutex();
67 db->createAllCatalogTables();
69 //create the default dba user
70 CatalogTableUSER cUser(db);
71 rv = cUser.insert(DBAUSER, DBAPASS);
72 if (OK != rv)
74 db->releaseDatabaseMutex();
75 return rv;
77 void *ret = NULL;
78 //Allocate space for the lock hash bucket
79 ret = db->allocLockHashBuckets();
80 if (NULL == ret)
82 db->releaseDatabaseMutex();
83 printError(ErrSysInit, "Allocation of Lock buckets failed");
84 return ErrSysInit;
87 db->releaseDatabaseMutex();
89 //create user database
90 rv = dbMgr->createDatabase("praba", config.getMaxDbSize());
91 if (OK != rv) return rv;
92 return OK;
95 DbRetVal SessionImpl::destroySystemDatabase()
97 DbRetVal rv = OK;
98 rv = dbMgr->deleteDatabase(SYSTEMDB);
99 if (OK != rv) return rv;
100 rv = dbMgr->deleteDatabase("praba");
101 if (OK != rv) return rv;
102 return OK;
105 DbRetVal SessionImpl::open(const char *username, const char *password)
107 DbRetVal rv = OK;
108 rv = readConfigFile();
109 if (rv != OK)
111 printError(ErrSysFatal, "Configuration file read failed\n");
112 return ErrSysFatal;
114 if ( NULL == dbMgr)
116 dbMgr = new DatabaseManagerImpl();
117 rv = dbMgr->openSystemDatabase();
119 if (OK != rv)
121 printError(rv,"Unable to open the system database");
122 return rv;
125 //TODO::process registration
126 //pMgr = new ProcessManager();
127 //pMgr->registerProcess();
129 rv = dbMgr->sysDb()->getDatabaseMutex();
130 if (OK != rv)
132 printError(rv,"Unable to get database mutex");
133 return rv;
136 CatalogTableUSER cUser(dbMgr->sysDb());
137 //cUser.authenticate(username, password, isAuthenticated, isDba );
138 isAuthenticated=true;
139 dbMgr->sysDb()->releaseDatabaseMutex();
140 if (!isAuthenticated)
142 dbMgr->closeSystemDatabase();
143 delete dbMgr;
144 //delete pMgr;
145 printError(ErrNoPrivilege,"User Authentication failed");
146 return ErrNoPrivilege;
149 dbMgr->createTransactionManager();
150 dbMgr->createLockManager();
151 rv = dbMgr->openDatabase("praba");
152 if (OK != rv) return rv;
154 return OK;
157 DbRetVal SessionImpl::close()
159 if (dbMgr)
161 dbMgr->closeDatabase();
162 dbMgr->closeSystemDatabase();
163 delete dbMgr;
164 dbMgr = NULL;
166 //pMgr->deregisterProcess();
167 //delete pMgr;
168 //pMgr = NULL;
169 if (uMgr)
171 delete uMgr;
172 uMgr = NULL;
174 return OK;
177 DatabaseManager* SessionImpl::getDatabaseManager()
179 if (isAuthenticated) return dbMgr;
180 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
181 return NULL;
184 UserManager* SessionImpl::getUserManager()
186 if (!isAuthenticated)
188 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
189 return NULL;
191 if (uMgr != NULL) return uMgr;
192 UserManagerImpl *userMgr = new UserManagerImpl();
193 if(0 == strcmp(userName, DBAUSER))
194 userMgr->setDba(true);
195 else
196 userMgr->setDba(false);
198 userMgr->setSysDb(dbMgr->sysDb());
200 userMgr->setUserName(userName);
201 uMgr = userMgr;
202 return userMgr;
205 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
207 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
209 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
210 return ErrSysFatal;
212 DbRetVal rv = OK;
213 rv = dbMgr->sysDb()->getTransTableMutex();
214 if (OK != rv)
216 printError(rv,"Unable to get TransTable mutex\n");
217 return rv;
219 rv = dbMgr->txnMgr()->startTransaction(level);
220 dbMgr->sysDb()->releaseTransTableMutex();
221 return rv;
225 DbRetVal SessionImpl::commit()
227 DbRetVal rv = OK;
228 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
230 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
231 return ErrSysFatal;
233 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
234 if (OK != rv)
236 printError(rv,"Unable to get TransTable mutex\n");
237 return rv;
239 return OK;
243 DbRetVal SessionImpl::rollback()
245 DbRetVal rv = OK;
246 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
248 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
249 return ErrSysFatal;
251 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
252 if (OK != rv)
254 printError(rv,"Unable to get TransTable mutex\n");
255 return rv;
257 return OK;
260 DbRetVal SessionImpl::readConfigFile()
262 char *confFilename = os::getenv("CSQL_CONFIG_FILE");
263 if (confFilename == NULL)
265 printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
266 return ErrSysInit;
268 int rv = config.readAllValues(confFilename);
269 if (rv == OK) return OK; else return ErrSysInit;