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");
42 dbMgr
= new DatabaseManagerImpl();
43 rv
= dbMgr
->createDatabase(SYSTEMDB
, Conf::config
.getMaxSysDbSize());
44 if (OK
!= rv
) return rv
;
45 dbMgr
->setSysDb(dbMgr
->db());
48 Database
*db
= dbMgr
->sysDb();
50 rv
= db
->getDatabaseMutex();
53 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
58 db
->createAllCatalogTables();
60 //create the default dba user
61 CatalogTableUSER
cUser(db
);
62 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
65 db
->releaseDatabaseMutex();
69 //Allocate space for the lock hash bucket
70 ret
= db
->allocLockHashBuckets();
73 db
->releaseDatabaseMutex();
74 printError(ErrSysInit
, "Allocation of Lock buckets failed");
78 db
->releaseDatabaseMutex();
79 printf("sysdb size %ld dbsize %ld\n", Conf::config
.getMaxSysDbSize(), Conf::config
.getMaxDbSize());
80 //create user database
81 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
82 if (OK
!= rv
) return rv
;
86 DbRetVal
SessionImpl::destroySystemDatabase()
89 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
90 if (OK
!= rv
) return rv
;
91 rv
= dbMgr
->deleteDatabase("userdb");
92 if (OK
!= rv
) return rv
;
98 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
101 rv
= readConfigFile();
104 printError(ErrSysFatal
, "Configuration file read failed\n");
110 dbMgr
= new DatabaseManagerImpl();
111 rv
= dbMgr
->openSystemDatabase();
115 printError(rv
,"Unable to open the system database");
119 rv
= authenticate(username
, password
);
122 dbMgr
->closeSystemDatabase();
123 delete dbMgr
; dbMgr
= NULL
;
127 dbMgr
->createTransactionManager();
128 dbMgr
->createLockManager();
129 rv
= dbMgr
->registerThread();
132 printError(rv
,"Unable to register to csql server");
133 dbMgr
->closeSystemDatabase();
134 delete dbMgr
; dbMgr
= NULL
;
137 rv
= dbMgr
->openDatabase("userdb");
139 dbMgr
->closeSystemDatabase();
140 delete dbMgr
; dbMgr
= NULL
;
143 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
144 //ProcessManager::systemDatabase = dbMgr->sysDb();
147 DbRetVal
SessionImpl::authenticate(const char *username
, const char *password
)
149 DbRetVal rv
= dbMgr
->sysDb()->getDatabaseMutex(false);
152 printError(rv
,"Unable to get database mutex");
155 CatalogTableUSER
cUser(dbMgr
->sysDb());
156 cUser
.authenticate(username
, password
, isAuthenticated
, isDba
);
157 strcpy(userName
, username
);
158 dbMgr
->sysDb()->releaseDatabaseMutex(false);
159 if (!isAuthenticated
)
161 printError(ErrNoPrivilege
,"User Authentication failed");
162 return ErrNoPrivilege
;
166 DbRetVal
SessionImpl::close()
171 rv
= dbMgr
->closeDatabase();
172 if (rv
!= OK
) { return ErrBadCall
; }
173 rv
= dbMgr
->deregisterThread();
174 if (rv
!= OK
) { return ErrBadCall
; }
175 rv
= dbMgr
->closeSystemDatabase();
176 if (rv
!= OK
) { return ErrBadCall
; }
188 DatabaseManager
* SessionImpl::getDatabaseManager()
190 if (isAuthenticated
) return dbMgr
;
191 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
195 UserManager
* SessionImpl::getUserManager()
197 if (!isAuthenticated
)
199 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
202 if (uMgr
!= NULL
) return uMgr
;
203 UserManagerImpl
*userMgr
= new UserManagerImpl();
204 if(0 == strcmp(userName
, DBAUSER
))
205 userMgr
->setDba(true);
207 userMgr
->setDba(false);
209 userMgr
->setSysDb(dbMgr
->sysDb());
211 userMgr
->setUserName(userName
);
216 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
218 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
220 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
225 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
230 DbRetVal
SessionImpl::commit()
233 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
235 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
238 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
241 printError(rv
,"Transaction commit failed\n");
248 DbRetVal
SessionImpl::rollback()
251 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
253 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
256 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
259 printError(rv
, "Transaction rollback failed\n");
265 DbRetVal
SessionImpl::readConfigFile()
267 // Check if env variable is set or not
268 char *confFilename
= os::getenv("CSQL_CONFIG_FILE");
269 if (confFilename
== NULL
)
271 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable should be set.");
275 int rv
= Conf::config
.readAllValues(confFilename
);
276 if (rv
!= 0) return ErrSysInit
;
279 Database
* SessionImpl::getSystemDatabase()
281 return dbMgr
->sysDb();