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>
28 //Before calling this method, application is required to call readConfigValues
29 DbRetVal
SessionImpl::initSystemDatabase()
33 rv
= readConfigFile();
36 printError(ErrSysInit
, "Configuration file read failed\n");
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());
60 Database
*db
= dbMgr
->sysDb();
62 rv
= db
->getDatabaseMutex();
65 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
70 db
->createAllCatalogTables();
72 //create the default dba user
73 CatalogTableUSER
cUser(db
);
74 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
77 db
->releaseDatabaseMutex();
81 //Allocate space for the lock hash bucket
82 ret
= db
->allocLockHashBuckets();
85 db
->releaseDatabaseMutex();
86 printError(ErrSysInit
, "Allocation of Lock buckets failed");
90 db
->releaseDatabaseMutex();
92 //create user database
93 rv
= dbMgr
->createDatabase("praba", Conf::config
.getMaxDbSize());
94 if (OK
!= rv
) return rv
;
98 DbRetVal
SessionImpl::destroySystemDatabase()
101 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
102 if (OK
!= rv
) return rv
;
103 rv
= dbMgr
->deleteDatabase("praba");
104 if (OK
!= rv
) return rv
;
110 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
113 rv
= readConfigFile();
116 printError(ErrSysFatal
, "Configuration file read failed\n");
122 dbMgr
= new DatabaseManagerImpl();
123 rv
= dbMgr
->openSystemDatabase();
127 printError(rv
,"Unable to open the system database");
131 rv
= dbMgr
->sysDb()->getDatabaseMutex();
134 printError(rv
,"Unable to get database mutex");
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();
148 printError(ErrNoPrivilege
,"User Authentication failed");
149 return ErrNoPrivilege
;
152 dbMgr
->createTransactionManager();
153 dbMgr
->createLockManager();
154 rv
= dbMgr
->openDatabase("praba");
159 rv
= dbMgr
->registerThread();
162 printError(rv
,"Unable to register to csql server");
168 DbRetVal
SessionImpl::close()
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
; }
190 DatabaseManager
* SessionImpl::getDatabaseManager()
192 if (isAuthenticated
) return dbMgr
;
193 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
197 UserManager
* SessionImpl::getUserManager()
199 if (!isAuthenticated
)
201 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
204 if (uMgr
!= NULL
) return uMgr
;
205 UserManagerImpl
*userMgr
= new UserManagerImpl();
206 if(0 == strcmp(userName
, DBAUSER
))
207 userMgr
->setDba(true);
209 userMgr
->setDba(false);
211 userMgr
->setSysDb(dbMgr
->sysDb());
213 userMgr
->setUserName(userName
);
218 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
220 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
222 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
226 rv
= dbMgr
->sysDb()->getTransTableMutex();
229 printError(rv
,"Unable to get TransTable mutex\n");
232 rv
= dbMgr
->txnMgr()->startTransaction(level
);
233 dbMgr
->sysDb()->releaseTransTableMutex();
238 DbRetVal
SessionImpl::commit()
241 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
243 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
246 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
249 printError(rv
,"Unable to get TransTable mutex\n");
256 DbRetVal
SessionImpl::rollback()
259 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
261 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
264 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
267 printError(rv
,"Unable to get TransTable mutex\n");
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.");
283 int rv
= Conf::config
.readAllValues(confFilename
);
284 if (rv
!= 0) return ErrSysInit
;