src/server/SessionImpl.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include<DatabaseManagerImpl.h>
00017 #include<DatabaseManager.h>
00018 #include<CatalogTables.h>
00019 #include<Database.h>
00020 #include<SessionImpl.h>
00021 #include<UserManager.h>
00022 #include<UserManagerImpl.h>
00023 #include<Transaction.h>
00024 #include<Debug.h>
00025 #include<Config.h>
00026 #include<Process.h>
00027 
00028 //Before calling this method, application is required to call readConfigValues
00029 DbRetVal SessionImpl::initSystemDatabase()
00030 
00031 {
00032     DbRetVal rv = OK;
00033     rv = readConfigFile();
00034     if (rv != OK) 
00035     {
00036        printError(ErrSysInit, "Configuration file read failed\n");
00037        return ErrSysInit;
00038     }
00039 
00040     Conf::config.print();
00041 
00042     dbMgr = new DatabaseManagerImpl();
00043     rv = dbMgr->createDatabase(SYSTEMDB, Conf::config.getMaxSysDbSize());
00044     if (OK != rv) return rv;
00045     dbMgr->setSysDb(dbMgr->db());
00046     dbMgr->setDb(NULL);
00047 
00048     Database *db = dbMgr->sysDb();
00049 
00050     rv = db->getDatabaseMutex();
00051     if (OK != rv)
00052     {
00053         printError(ErrLockTimeOut, "Unable to get Database Mutex");
00054         return rv;
00055     }
00056 
00057 
00058     db->createAllCatalogTables();
00059 
00060     //create the default dba user
00061     CatalogTableUSER cUser(db);
00062     rv = cUser.insert(DBAUSER, DBAPASS);
00063     if (OK != rv)
00064     {
00065         db->releaseDatabaseMutex();
00066         return rv;
00067     }
00068     void *ret = NULL;
00069     //Allocate space for the lock hash bucket
00070     ret = db->allocLockHashBuckets();
00071     if (NULL == ret)
00072     {
00073         db->releaseDatabaseMutex();
00074         printError(ErrSysInit, "Allocation of Lock buckets failed");
00075         return ErrSysInit;
00076     }
00077 
00078     db->releaseDatabaseMutex();
00079     printf("sysdb size %ld dbsize %ld\n", Conf::config.getMaxSysDbSize(), Conf::config.getMaxDbSize());
00080     //create user database
00081     rv = dbMgr->createDatabase("userdb", Conf::config.getMaxDbSize());
00082     if (OK != rv) return rv;
00083     return OK;
00084 }
00085 
00086 DbRetVal SessionImpl::destroySystemDatabase()
00087 {
00088     DbRetVal rv = OK;
00089     rv = dbMgr->deleteDatabase(SYSTEMDB);
00090     if (OK != rv) return rv;
00091     rv = dbMgr->deleteDatabase("userdb");
00092     if (OK != rv) return rv;
00093     delete dbMgr;
00094     dbMgr = NULL;
00095     return OK;
00096 }
00097 
00098 DbRetVal SessionImpl::open(const char *username, const char *password)
00099 {
00100     DbRetVal rv = OK;
00101     rv = readConfigFile();
00102     if (rv != OK)
00103     {
00104        printError(ErrSysFatal, "Configuration file read failed\n");
00105        return ErrSysFatal;
00106     }
00107 
00108     if ( NULL == dbMgr)
00109     {
00110         dbMgr = new DatabaseManagerImpl();
00111         rv = dbMgr->openSystemDatabase();
00112     }
00113     if (OK != rv)
00114     {
00115         printError(rv,"Unable to open the system database");
00116         return rv;
00117     }
00118 
00119     rv = authenticate(username, password);
00120     if (OK != rv)
00121     {
00122         dbMgr->closeSystemDatabase();
00123         delete dbMgr; dbMgr = NULL;
00124         return rv;
00125     }
00126 
00127     dbMgr->createTransactionManager();
00128     dbMgr->createLockManager();
00129     rv = dbMgr->registerThread();
00130     if (OK != rv)
00131     {
00132         printError(rv,"Unable to register to csql server");
00133         dbMgr->closeSystemDatabase();
00134         delete dbMgr; dbMgr = NULL;
00135         return rv;
00136     }
00137     rv = dbMgr->openDatabase("userdb");
00138     if (OK != rv) {
00139         dbMgr->closeSystemDatabase();
00140         delete dbMgr; dbMgr = NULL;
00141         return rv;
00142     }
00143     ((DatabaseManagerImpl*)dbMgr)->setProcSlot();
00144     //ProcessManager::systemDatabase = dbMgr->sysDb();
00145     return OK;
00146 }
00147 DbRetVal SessionImpl::authenticate(const char *username, const char *password)
00148 {
00149     DbRetVal rv = dbMgr->sysDb()->getDatabaseMutex(false);
00150     if (OK != rv)
00151     {
00152         printError(rv,"Unable to get database mutex");
00153         return rv;
00154     }
00155     CatalogTableUSER cUser(dbMgr->sysDb());
00156     cUser.authenticate(username, password, isAuthenticated, isDba);
00157     strcpy(userName, username);
00158     dbMgr->sysDb()->releaseDatabaseMutex(false);
00159     if (!isAuthenticated)
00160     {
00161         printError(ErrNoPrivilege,"User Authentication failed");
00162         return ErrNoPrivilege;
00163     }
00164     return OK;
00165 }
00166 DbRetVal SessionImpl::close()
00167 {
00168     DbRetVal rv = OK;
00169     if (dbMgr)
00170     {
00171         rv = dbMgr->closeDatabase();
00172         if (rv != OK) { return ErrBadCall;  }
00173         rv = dbMgr->deregisterThread();
00174         if (rv != OK) {  return ErrBadCall;  }
00175         rv = dbMgr->closeSystemDatabase();
00176         if (rv != OK) { return ErrBadCall;  }
00177         delete dbMgr;
00178         dbMgr = NULL;
00179     }
00180     if (uMgr)
00181     {
00182         delete uMgr;
00183         uMgr = NULL;
00184     }
00185     return OK;
00186 }
00187 
00188 DatabaseManager* SessionImpl::getDatabaseManager()
00189 {
00190     if (isAuthenticated) return dbMgr;
00191     printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
00192     return NULL;
00193 }
00194 
00195 UserManager* SessionImpl::getUserManager()
00196 {
00197     if (!isAuthenticated)
00198     {
00199         printError(ErrNoPrivilege, "Not Authenticated: Returning NULL");
00200         return NULL;
00201     }
00202     if (uMgr != NULL) return uMgr;
00203     UserManagerImpl *userMgr =  new UserManagerImpl();
00204     if(0 == strcmp(userName, DBAUSER))
00205         userMgr->setDba(true);
00206     else
00207         userMgr->setDba(false);
00208 
00209     userMgr->setSysDb(dbMgr->sysDb());
00210 
00211     userMgr->setUserName(userName);
00212     uMgr = userMgr;
00213     return userMgr;
00214 }
00215 
00216 DbRetVal SessionImpl::startTransaction(IsolationLevel level)
00217 {
00218     if (NULL == dbMgr || NULL == dbMgr->txnMgr())
00219     {
00220         printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
00221         return ErrSysFatal;
00222     }
00223     DbRetVal rv = OK;
00224 
00225     rv = dbMgr->txnMgr()->startTransaction(dbMgr->lockMgr(), level);
00226     return rv;
00227 }
00228 
00229 
00230 DbRetVal SessionImpl::commit()
00231 {
00232     DbRetVal rv = OK;
00233     if (NULL == dbMgr || NULL == dbMgr->txnMgr())
00234     {
00235         printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
00236         return ErrSysFatal;
00237     }
00238     rv = dbMgr->txnMgr()->commit(dbMgr->lockMgr());
00239     if (OK != rv)
00240     {
00241         printError(rv,"Transaction commit failed\n");
00242         return rv;
00243     }
00244     return OK;
00245 }
00246 
00247 
00248 DbRetVal SessionImpl::rollback()
00249 {
00250     DbRetVal rv = OK;
00251     if (NULL == dbMgr || NULL == dbMgr->txnMgr())
00252     {
00253         printError(ErrSysFatal, "Database Manager or Txn Manager object is NULL");
00254         return ErrSysFatal;
00255     }
00256     rv = dbMgr->txnMgr()->rollback(dbMgr->lockMgr());
00257     if (OK != rv)
00258     {
00259         printError(rv, "Transaction rollback failed\n");
00260         return rv;
00261     }
00262     return OK;
00263 }
00264 
00265 DbRetVal SessionImpl::readConfigFile()
00266 {
00267     // Check if env variable is set or not
00268     char *confFilename = os::getenv("CSQL_CONFIG_FILE");
00269     if (confFilename == NULL)
00270     {
00271         printError(ErrSysInit, "CSQL_CONFIG_FILE environment variable should be set.");
00272         return ErrSysInit;
00273     }
00274 
00275     int  rv = Conf::config.readAllValues(confFilename);
00276     if (rv != 0) return ErrSysInit;
00277     return OK;
00278 }
00279 Database* SessionImpl::getSystemDatabase()
00280 {
00281     return dbMgr->sysDb();
00282 }
00283 

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7