Initial revision
[csql.git] / src / server / SessionImpl.c
blobe17dbae755258a7d51ae859f7ae68d7f71cc7044
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<DatabaseManagerImpl.h>
17 #include<DatabaseManager.h>
18 #include<CatalogTables.h>
19 #include<Database.h>
20 #include<SessionImpl.h>
21 #include<UserManager.h>
22 #include<UserManagerImpl.h>
23 #include<Transaction.h>
24 #include<Debug.h>
26 DbRetVal SessionImpl::initSystemDatabase()
29 DbRetVal rv = OK;
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());
38 dbMgr->setDb(NULL);
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);
49 if (OK != rv)
51 db->releaseDatabaseMutex();
52 return rv;
54 void *ret = NULL;
55 //Allocate space for the lock hash bucket
56 ret = db->allocLockHashBuckets();
57 if (NULL == ret)
59 db->releaseDatabaseMutex();
60 printError(ErrSysInit, "Allocation of Lock buckets failed");
61 return ErrSysInit;
64 db->releaseDatabaseMutex();
66 //create user database
67 rv = dbMgr->createDatabase("praba", SYSTEM_DB_SIZE);
68 if (OK != rv) return rv;
69 return OK;
72 DbRetVal SessionImpl::destroySystemDatabase()
74 DbRetVal rv = OK;
75 rv = dbMgr->deleteDatabase(SYSTEMDB);
76 if (OK != rv) return rv;
77 rv = dbMgr->deleteDatabase("praba");
78 if (OK != rv) return rv;
79 return OK;
82 DbRetVal SessionImpl::open(const char *username, const char *password)
84 DbRetVal rv = OK;
85 if ( NULL == dbMgr)
87 dbMgr = new DatabaseManagerImpl();
88 rv = dbMgr->openSystemDatabase();
90 if (OK != rv)
92 printError(rv,"Unable to open the system database");
93 return rv;
96 //TODO::process registration
97 //pMgr = new ProcessManager();
98 //pMgr->registerProcess();
100 rv = dbMgr->sysDb()->getDatabaseMutex();
101 if (OK != rv)
103 printError(rv,"Unable to get database mutex");
104 return rv;
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();
114 delete dbMgr;
115 //delete pMgr;
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;
125 return OK;
128 DbRetVal SessionImpl::close()
130 if (dbMgr)
132 dbMgr->closeDatabase();
133 dbMgr->closeSystemDatabase();
134 delete dbMgr;
135 dbMgr = NULL;
137 //pMgr->deregisterProcess();
138 //delete pMgr;
139 //pMgr = NULL;
140 if (uMgr)
142 delete uMgr;
143 uMgr = NULL;
145 return OK;
148 DatabaseManager* SessionImpl::getDatabaseManager()
150 if (isAuthenticated) return dbMgr;
151 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
152 return NULL;
155 UserManager* SessionImpl::getUserManager()
157 if (!isAuthenticated)
159 printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
160 return NULL;
162 if (uMgr != NULL) return uMgr;
163 UserManagerImpl *userMgr = new UserManagerImpl();
164 if(0 == strcmp(userName, DBAUSER))
165 userMgr->setDba(true);
166 else
167 userMgr->setDba(false);
169 userMgr->setSysDb(dbMgr->sysDb());
171 userMgr->setUserName(userName);
172 uMgr = userMgr;
173 return userMgr;
176 DbRetVal SessionImpl::startTransaction()
178 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
180 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
181 return ErrSysFatal;
183 DbRetVal rv = OK;
184 rv = dbMgr->sysDb()->getTransTableMutex();
185 if (OK != rv)
187 printError(rv,"Unable to get TransTable mutex\n");
188 return rv;
190 rv = dbMgr->txnMgr()->startTransaction();
191 dbMgr->sysDb()->releaseTransTableMutex();
192 return rv;
196 DbRetVal SessionImpl::commit()
198 DbRetVal rv = OK;
199 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
201 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
202 return ErrSysFatal;
204 rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
205 if (OK != rv)
207 printError(rv,"Unable to get TransTable mutex\n");
208 return rv;
210 return OK;
214 DbRetVal SessionImpl::rollback()
216 DbRetVal rv = OK;
217 if (NULL == dbMgr || NULL == dbMgr->txnMgr())
219 printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
220 return ErrSysFatal;
222 rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
223 if (OK != rv)
225 printError(rv,"Unable to get TransTable mutex\n");
226 return rv;
228 return OK;