1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
16 #include<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
27 //Before calling this method, application is required to call readConfigValues
28 DbRetVal
SessionImpl::initSystemDatabase()
32 rv
= readConfigFile();
35 printError(ErrSysInit
, "Configuration file read failed\n");
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());
63 Database
*db
= dbMgr
->sysDb();
65 rv
= db
->getDatabaseMutex();
68 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
73 db
->createAllCatalogTables();
75 //create the default dba user
76 CatalogTableUSER
cUser(db
);
77 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
80 db
->releaseDatabaseMutex();
84 //Allocate space for the lock hash bucket
85 ret
= db
->allocLockHashBuckets();
88 db
->releaseDatabaseMutex();
89 printError(ErrSysInit
, "Allocation of Lock buckets failed");
93 db
->releaseDatabaseMutex();
95 //create user database
96 rv
= dbMgr
->createDatabase("praba", config
.getMaxDbSize());
97 if (OK
!= rv
) return rv
;
101 DbRetVal
SessionImpl::destroySystemDatabase()
104 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
105 if (OK
!= rv
) return rv
;
106 rv
= dbMgr
->deleteDatabase("praba");
107 if (OK
!= rv
) return rv
;
111 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
114 rv
= readConfigFile();
117 printError(ErrSysFatal
, "Configuration file read failed\n");
122 dbMgr
= new DatabaseManagerImpl();
123 rv
= dbMgr
->openSystemDatabase();
127 printError(rv
,"Unable to open the system database");
132 rv
= dbMgr
->sysDb()->getDatabaseMutex();
135 printError(rv
,"Unable to get database mutex");
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();
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();
161 printError(rv
,"Unable to register to csql server");
167 DbRetVal
SessionImpl::close()
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
;
189 DatabaseManager
* SessionImpl::getDatabaseManager()
191 if (isAuthenticated
) return dbMgr
;
192 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
196 UserManager
* SessionImpl::getUserManager()
198 if (!isAuthenticated
)
200 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
203 if (uMgr
!= NULL
) return uMgr
;
204 UserManagerImpl
*userMgr
= new UserManagerImpl();
205 if(0 == strcmp(userName
, DBAUSER
))
206 userMgr
->setDba(true);
208 userMgr
->setDba(false);
210 userMgr
->setSysDb(dbMgr
->sysDb());
212 userMgr
->setUserName(userName
);
217 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
219 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
221 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
225 rv
= dbMgr
->sysDb()->getTransTableMutex();
228 printError(rv
,"Unable to get TransTable mutex\n");
231 rv
= dbMgr
->txnMgr()->startTransaction(level
);
232 dbMgr
->sysDb()->releaseTransTableMutex();
237 DbRetVal
SessionImpl::commit()
240 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
242 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
245 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
248 printError(rv
,"Unable to get TransTable mutex\n");
255 DbRetVal
SessionImpl::rollback()
258 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
260 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
263 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
266 printError(rv
,"Unable to get TransTable mutex\n");
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.");
282 int rv
= config
.readAllValues(confFilename
);
283 if (rv
!= 0) return ErrSysInit
;