⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnect.cpp

📁 C++连接一写常用数据库的接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            ConfigFile configSystemFile(SYSTEMCONFIG);            configSystemFile.read();                        if (configSystemFile.hasKeyName("dbconnect", "driverPath"))            {               driverPath = configSystemFile["dbconnect"]["driverPath"];               // Make sure we have a proper path               if (driverPath[driverPath.length()-1] != DIRCHAR)                  driverPath += DIRCHAR;            }                  // Check if we have a driver file name or need to use the default.            if (configSystemFile.hasKeyName(driverNames[driver], "driverFile"))               driverPath += configSystemFile[driverNames[driver]]["driverFile"];            else               driverPath += driverPaths[driver];            // Check if we have a driver section and try and obtain needed values.            if (configSystemFile.hasSection(driverNames[driver]))            {                          //Get all the driver arguments and values               vector<string> keyNames = configSystemFile.getSectionKeyNames(driverNames[driver]);               for (i=0; i<keyNames.size(); i++)               {                  args.push_back(keyNames[i].c_str());                                         // Name                  args.push_back(configSystemFile[driverNames[driver]][keyNames[i].c_str()]);  // Value               }            }         }         catch(...)         {}      }   }   // 4. If still no library path, then use default driver name and hope its   //    in the systems library path.   if (driverPath.length() == 0)      driverPath = driverPaths[driver];     // Load the driver and resolve the symbols to pointers.   try   {      dlLoader.loadLibrary(driverPath);            //Resolve the symbols required from the library      ptr_createDriverInstance  = (void* (*)(int , const char**))dlLoader.resolveSymbol("createDriverInstance");      ptr_destroyDriverInstance = (void* (*)(void*)) dlLoader.resolveSymbol("destroyDriverInstance");      ptr_getAuthor             = (const char* (*)())dlLoader.resolveSymbol("getAuthor");      ptr_getVendor             = (const char* (*)())dlLoader.resolveSymbol("getVendor");      ptr_getCopyright          = (const char* (*)())dlLoader.resolveSymbol("getCopyright");      ptr_getDriverType         = (const char* (*)())dlLoader.resolveSymbol("getDriverType");      ptr_getDriverName         = (const char* (*)())dlLoader.resolveSymbol("getDriverName");      ptr_getDriverDesc         = (const char* (*)())dlLoader.resolveSymbol("getDriverDesc");      ptr_getDbConnectVersion   = (const char* (*)())dlLoader.resolveSymbol("getDbConnectVersion");    }   catch(BaseException &ex)   {      throw DriverError(ex.description);    }   // Get all the driver information.   driverInfo.author = ptr_getAuthor();   driverInfo.vendor = ptr_getVendor();   driverInfo.copyright = ptr_getCopyright();   driverInfo.driverType = ptr_getDriverType();   driverInfo.driverName = ptr_getDriverName();   driverInfo.driverDescription = ptr_getDriverDesc();   driverInfo.dbConnectVersion = ptr_getDbConnectVersion();   // Get the version from the driver and make sure it is compatible.   if (!checkCompatibility(driverInfo.dbConnectVersion.c_str()) )   {      string err = "The so library ";      err += driverPath;      err += " with version ";      err += driverInfo.dbConnectVersion.c_str();      err += " is not compatibible with version ";      err += DBCONNECTVER;      err += " of the DBConnect API";      throw DriverError(err);   }   // Obtain a pointer reference to a driver instance passing the arguments.   const char** argsref = NULL;   argsref = (const char**)malloc(args.size()*sizeof(const char*));   for (i=0; i<args.size(); i++)      argsref[i] = args[i].c_str();
   try   {      drvRef = (BaseConnection*)ptr_createDriverInstance(args.size(), argsref);   }   catch(...)   {      drvRef = NULL;   }   free(argsref);

   // If we do not have a driver reference, throw an error
   if (!drvRef)
      throw DriverError("Unable to obtain a reference to the driver object");
}  // DbConnection::DbConnection//------------------------------------------------------------------------------// DbConnection::~DbConnection//------------------------------------------------------------------------------DbConnection::~DbConnection(){   // Free the driver instance. (Returns NULL if freed, or the reference if not freed)   drvRef = (BaseConnection*)ptr_destroyDriverInstance((void*)drvRef);   // Unload the driver.   try   {      ptr_createDriverInstance = NULL;      ptr_destroyDriverInstance = NULL;      ptr_getAuthor = NULL;      ptr_getVendor = NULL;      ptr_getDriverName = NULL;      ptr_getDriverDesc = NULL;      ptr_getDbConnectVersion = NULL;           dlLoader.unloadLibrary();   }   catch(BaseException &ex)   {      throw Error(ex.description);        }     if (drvRef)      throw Error("The memory for the driver was not released.");
}  // DbConnection::~DbConnection//------------------------------------------------------------------------------// DbConnection::connect//------------------------------------------------------------------------------void DbConnection::connect(   const string &username,    const string &password,    const string &databaseName,    const string &host,    int   maxConnections,   int   minConnections,   const string &optParam1,   const string &optParam2){   // The driver will propogate any exceptions.
   drvRef->connect(username, password, databaseName, host,                   maxConnections, minConnections,                   optParam1,                   optParam2);} // DbConnection::connect//------------------------------------------------------------------------------// DbConnection::disconnect//------------------------------------------------------------------------------void DbConnection::disconnect(   time_t timeout){   // The driver will propogate any exceptions.   drvRef->disconnect(timeout);   }  // DbConnection::disconnect      //------------------------------------------------------------------------------// DbConnection::setPingInterval//------------------------------------------------------------------------------void DbConnection::setPingInterval(   time_t pingInterval){   // The driver will propogate any exceptions.   drvRef->setPingInterval(pingInterval);}  // DbConnection::setPingInterval//------------------------------------------------------------------------------// DbConnection::requestQueryConnection//------------------------------------------------------------------------------BaseQuery*DbConnection::requestQueryConnection(){   // The driver will propogate any exceptions.   return (BaseQuery*)drvRef->requestQueryConnection();}  // DbConnection::requestQueryConnection//------------------------------------------------------------------------------// DbConnection::checkCompatibility//------------------------------------------------------------------------------boolDbConnection::checkCompatibility(   const string &ver){   for (int i=0; i<compatibilityTable[DBCONNECTVER].size(); i++)      if (strcmp(compatibilityTable[DBCONNECTVER][i].c_str(), ver.c_str()) == 0)         return true;            return false;  }  // DbConnection::checkCompatibility//------------------------------------------------------------------------------// DbConnection::checkCompatibility//------------------------------------------------------------------------------DbConnectionDriverInfo* DbConnection::getDriverInformation(){   return &driverInfo;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -