1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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>
26 DbRetVal
SessionImpl::initSystemDatabase()
30 dbMgr
= new DatabaseManagerImpl();
31 //TODO:Size of system database 100 MB ->config parameter
33 //TODO:No of chunks of system database->config parameter
34 //This limits the total number of catalog tables system shall support.
35 rv
= dbMgr
->createDatabase(SYSTEMDB
, SYSTEM_DB_SIZE
);
36 if (OK
!= rv
) return rv
;
37 dbMgr
->setSysDb(dbMgr
->db());
40 Database
*db
= dbMgr
->sysDb();
42 db
->getDatabaseMutex();
44 db
->createAllCatalogTables();
46 //create the default dba user
47 CatalogTableUSER
cUser(db
);
48 rv
= cUser
.insert(DBAUSER
, DBAPASS
);
51 db
->releaseDatabaseMutex();
55 //Allocate space for the lock hash bucket
56 ret
= db
->allocLockHashBuckets();
59 db
->releaseDatabaseMutex();
60 printError(ErrSysInit
, "Allocation of Lock buckets failed");
64 db
->releaseDatabaseMutex();
66 //create user database
67 rv
= dbMgr
->createDatabase("praba", SYSTEM_DB_SIZE
);
68 if (OK
!= rv
) return rv
;
72 DbRetVal
SessionImpl::destroySystemDatabase()
75 rv
= dbMgr
->deleteDatabase(SYSTEMDB
);
76 if (OK
!= rv
) return rv
;
77 rv
= dbMgr
->deleteDatabase("praba");
78 if (OK
!= rv
) return rv
;
82 DbRetVal
SessionImpl::open(const char *username
, const char *password
)
87 dbMgr
= new DatabaseManagerImpl();
88 rv
= dbMgr
->openSystemDatabase();
92 printError(rv
,"Unable to open the system database");
96 //TODO::process registration
97 //pMgr = new ProcessManager();
98 //pMgr->registerProcess();
100 rv
= dbMgr
->sysDb()->getDatabaseMutex();
103 printError(rv
,"Unable to get database mutex");
107 CatalogTableUSER
cUser(dbMgr
->sysDb());
108 //cUser.authenticate(username, password, isAuthenticated, isDba );
109 isAuthenticated
=true;
110 dbMgr
->sysDb()->releaseDatabaseMutex();
111 if (!isAuthenticated
)
113 dbMgr
->closeSystemDatabase();
116 printError(ErrNoPrivilege
,"User Authentication failed");
117 return ErrNoPrivilege
;
120 dbMgr
->createTransactionManager();
121 dbMgr
->createLockManager();
122 rv
= dbMgr
->openDatabase("praba");
123 if (OK
!= rv
) return rv
;
128 DbRetVal
SessionImpl::close()
132 dbMgr
->closeDatabase();
133 dbMgr
->closeSystemDatabase();
137 //pMgr->deregisterProcess();
148 DatabaseManager
* SessionImpl::getDatabaseManager()
150 if (isAuthenticated
) return dbMgr
;
151 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
155 UserManager
* SessionImpl::getUserManager()
157 if (!isAuthenticated
)
159 printError(ErrNoPrivilege
, "Not Authenticated: Returning NULL");
162 if (uMgr
!= NULL
) return uMgr
;
163 UserManagerImpl
*userMgr
= new UserManagerImpl();
164 if(0 == strcmp(userName
, DBAUSER
))
165 userMgr
->setDba(true);
167 userMgr
->setDba(false);
169 userMgr
->setSysDb(dbMgr
->sysDb());
171 userMgr
->setUserName(userName
);
176 DbRetVal
SessionImpl::startTransaction()
178 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
180 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
184 rv
= dbMgr
->sysDb()->getTransTableMutex();
187 printError(rv
,"Unable to get TransTable mutex\n");
190 rv
= dbMgr
->txnMgr()->startTransaction();
191 dbMgr
->sysDb()->releaseTransTableMutex();
196 DbRetVal
SessionImpl::commit()
199 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
201 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
204 rv
= dbMgr
->txnMgr()->commit(dbMgr
->lockMgr());
207 printError(rv
,"Unable to get TransTable mutex\n");
214 DbRetVal
SessionImpl::rollback()
217 if (NULL
== dbMgr
|| NULL
== dbMgr
->txnMgr())
219 printError(ErrSysFatal
, "Database Manager or Txn Manager object is NULL");
222 rv
= dbMgr
->txnMgr()->rollback(dbMgr
->lockMgr());
225 printError(rv
,"Unable to get TransTable mutex\n");