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 db
->getDatabaseMutex();
67 db
->createAllCatalogTables();
69 //create the default dba user
70 CatalogTableUSER
cUser(db
);
71 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
74 db
->releaseDatabaseMutex();
78 //Allocate space for the lock hash bucket
79 ret
= db
->allocLockHashBuckets();
82 db
->releaseDatabaseMutex();
83 printError(ErrSysInit
, "Allocation of Lock buckets failed");
87 db
->releaseDatabaseMutex();
89 //create user database
90 rv
= dbMgr
->createDatabase("praba", config
.getMaxDbSize());
91 if (OK
!= rv
) return rv
;
95 DbRetVal
SessionImpl::destroySystemDatabase()
98 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
99 if (OK
!= rv
) return rv
;
100 rv
= dbMgr
->deleteDatabase("praba");
101 if (OK
!= rv
) return rv
;
105 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
108 rv
= readConfigFile();
111 printError(ErrSysFatal
, "Configuration file read failed\n");
116 dbMgr
= new DatabaseManagerImpl();
117 rv
= dbMgr
->openSystemDatabase();
121 printError(rv
,"Unable to open the system database");
125 //TODO::process registration
126 //pMgr = new ProcessManager();
127 //pMgr->registerProcess();
129 rv
= dbMgr
->sysDb()->getDatabaseMutex();
132 printError(rv
,"Unable to get database mutex");
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();
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
;
157 DbRetVal
SessionImpl::close()
161 dbMgr
->closeDatabase();
162 dbMgr
->closeSystemDatabase();
166 //pMgr->deregisterProcess();
177 DatabaseManager
* SessionImpl::getDatabaseManager()
179 if (isAuthenticated
) return dbMgr
;
180 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
184 UserManager
* SessionImpl::getUserManager()
186 if (!isAuthenticated
)
188 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
191 if (uMgr
!= NULL
) return uMgr
;
192 UserManagerImpl
*userMgr
= new UserManagerImpl();
193 if(0 == strcmp(userName
, DBAUSER
))
194 userMgr
->setDba(true);
196 userMgr
->setDba(false);
198 userMgr
->setSysDb(dbMgr
->sysDb());
200 userMgr
->setUserName(userName
);
205 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
207 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
209 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
213 rv
= dbMgr
->sysDb()->getTransTableMutex();
216 printError(rv
,"Unable to get TransTable mutex\n");
219 rv
= dbMgr
->txnMgr()->startTransaction(level
);
220 dbMgr
->sysDb()->releaseTransTableMutex();
225 DbRetVal
SessionImpl::commit()
228 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
230 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
233 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
236 printError(rv
,"Unable to get TransTable mutex\n");
243 DbRetVal
SessionImpl::rollback()
246 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
248 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
251 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
254 printError(rv
,"Unable to get TransTable mutex\n");
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.");
268 int rv
= config
.readAllValues(confFilename
);
269 if (rv
== OK
) return OK
; else return ErrSysInit
;