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()
35 rv
= readConfigFile();
38 printError(ErrSysInit
, "Configuration file read failed\n");
42 // Conf::config.print();
44 dbMgr
= new DatabaseManagerImpl();
45 rv
= dbMgr
->createDatabase(SYSTEMDB
, Conf::config
.getMaxSysDbSize());
46 if (OK
!= rv
) return rv
;
48 dbMgr
->setSysDb(dbMgr
->db());
51 Database
*db
= dbMgr
->sysDb();
53 rv
= db
->getDatabaseMutex();
56 printError(ErrLockTimeOut
, "Unable to get Database Mutex");
61 db
->createAllCatalogTables();
63 //create the default dba user
64 CatalogTableUSER
cUser(db
);
65 rv
= cUser
.insert(I_USER
, I_PASS
);
68 db
->releaseDatabaseMutex();
72 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
75 db
->releaseDatabaseMutex();
79 //Allocate space for the lock hash bucket
80 ret
= db
->allocLockHashBuckets();
83 db
->releaseDatabaseMutex();
84 printError(ErrSysInit
, "Allocation of Lock buckets failed");
87 db
->releaseDatabaseMutex();
89 printf("Sys_DB [Size=%4.4ldMB] \nUser_DB [Size=%4.4ldMB]\n", Conf::config
.getMaxSysDbSize()/1048576, Conf::config
.getMaxDbSize()/1048576);
90 //create user database
91 rv
= dbMgr
->createDatabase("userdb", Conf::config
.getMaxDbSize());
92 if (OK
!= rv
) return rv
;
96 DbRetVal
SessionImpl::destroySystemDatabase()
99 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
100 if (OK
!= rv
) return rv
;
101 rv
= dbMgr
->deleteDatabase("userdb");
102 if (OK
!= rv
) return rv
;
108 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
111 rv
= readConfigFile();
114 printError(ErrSysFatal
, "Configuration file read failed\n");
120 dbMgr
= new DatabaseManagerImpl();
121 rv
= dbMgr
->openSystemDatabase();
125 printError(rv
,"Unable to open the system database");
129 rv
= authenticate(username
, password
);
132 dbMgr
->closeSystemDatabase();
133 delete dbMgr
; dbMgr
= NULL
;
137 dbMgr
->createTransactionManager();
138 dbMgr
->createLockManager();
139 rv
= dbMgr
->registerThread();
142 printError(rv
,"Unable to register to csql server");
143 dbMgr
->closeSystemDatabase();
144 delete dbMgr
; dbMgr
= NULL
;
147 rv
= dbMgr
->openDatabase("userdb");
149 dbMgr
->closeSystemDatabase();
150 delete dbMgr
; dbMgr
= NULL
;
153 ((DatabaseManagerImpl
*)dbMgr
)->setProcSlot();
154 //ProcessManager::systemDatabase = dbMgr->sysDb();
158 DbRetVal
SessionImpl::authenticate(const char *username
, const char *password
)
160 DbRetVal rv
= dbMgr
->sysDb()->getDatabaseMutex(false);
163 printError(ErrLockTimeOut
,"System under recovery or DDL operation going on.");
164 printError(ErrLockTimeOut
,"Unable to get the database mutex.Retry...");
165 return ErrLockTimeOut
;
167 CatalogTableUSER
cUser(dbMgr
->sysDb());
168 cUser
.authenticate(username
, password
, isAuthenticated
, isDba
);
169 strcpy(userName
, username
);
170 dbMgr
->sysDb()->releaseDatabaseMutex(false);
171 if (!isAuthenticated
)
173 printError(ErrNoPrivilege
,"User Authentication failed");
174 return ErrNoPrivilege
;
178 DbRetVal
SessionImpl::getExclusiveLock()
180 DbRetVal rv
= dbMgr
->sysDb()->getProcessTableMutex(true);
182 printError(ErrLockTimeOut
, "Unable to acquire proc table mutex");
185 if (dbMgr
->isAnyOneRegistered()) {
186 printError(ErrLockTimeOut
, "Unable to acquire exclusive lock. somebody is connected");
187 dbMgr
->sysDb()->releaseProcessTableMutex(true);
188 return ErrLockTimeOut
;
193 DbRetVal
SessionImpl::close()
196 if (isXTaken
&& dbMgr
) dbMgr
->sysDb()->releaseProcessTableMutex(true);
199 rv
= dbMgr
->closeDatabase();
200 if (rv
!= OK
) { return ErrBadCall
; }
201 rv
= dbMgr
->deregisterThread();
202 if (rv
!= OK
) { return ErrBadCall
; }
203 rv
= dbMgr
->closeSystemDatabase();
204 if (rv
!= OK
) { return ErrBadCall
; }
217 DatabaseManager
* SessionImpl::getDatabaseManager()
219 if (isAuthenticated
) return dbMgr
;
220 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
224 UserManager
* SessionImpl::getUserManager()
226 if (!isAuthenticated
)
228 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
231 if (uMgr
!= NULL
) return uMgr
;
232 UserManagerImpl
*userMgr
= new UserManagerImpl();
233 if(0 == strcmp(userName
, DBAUSER
))
234 userMgr
->setDba(true);
236 userMgr
->setDba(false);
238 userMgr
->setSysDb(dbMgr
->sysDb());
240 userMgr
->setUserName(userName
);
245 DbRetVal
SessionImpl::startTransaction(IsolationLevel level
)
247 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
249 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
254 rv
= dbMgr
->txnMgr()->startTransaction(dbMgr
->lockMgr(), level
);
259 DbRetVal
SessionImpl::commit()
262 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
264 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
267 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
270 printError(rv
,"Transaction commit failed\n");
277 DbRetVal
SessionImpl::rollback()
280 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
282 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
285 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
288 printError(rv
, "Transaction rollback failed\n");
294 DbRetVal
SessionImpl::readConfigFile()
296 // Check if env variable is set or not
297 char *confFilename
= os::getenv("CSQL_CONFIG_FILE");
298 if (confFilename
== NULL
)
300 if (os::fileExists(DEFAULT_CONFIG_FILE
)) {
301 confFilename
= DEFAULT_CONFIG_FILE
;
304 printError(ErrSysInit
, "CSQL_CONFIG_FILE environment variable "
310 int rv
= Conf::config
.readAllValues(confFilename
);
311 if (rv
!= 0) return ErrSysInit
;
314 Database
* SessionImpl::getSystemDatabase()
316 return dbMgr
->sysDb();