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

📄 dbconnect.cpp

📁 C++连接一写常用数据库的接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * DBConnection Object defining the wrapper class for all the database drivers. * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org * * This library is free software; you can redistribute it and/or *   modify it under the terms of the GNU Lesser General Public *   License as published by the Free Software Foundation; either *   version 2.1 of the License, or (at your option) any later version. * *   This library is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *   Lesser General Public License for more details. * *   You should have received a copy of the GNU Lesser General Public *   License along with this library; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US * */#include "dbconnect.h"
#include "configFile.h"
#ifdef WIN32
   #define DIRCHAR '\\'   #define LOCALCONFIG "dbconnect.ini"   #define SYSTEMCONFIG "\\win32\\system\\dbconnect.ini"#else   #define DIRCHAR '/'   #define LOCALCONFIG "./dbconnect.cfg"   #define SYSTEMCONFIG "/etc/dbconn/dbconnect.cfg"#endif

static char _copyright[] = {"/*"" * DBConnect API"" * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org"" *"" * This library is free software; you can redistribute it and/or"" *   modify it under the terms of the GNU Lesser General Public"" *   License as published by the Free Software Foundation; either"" *   version 2.1 of the License, or (at your option) any later version."" *"" *   This library is distributed in the hope that it will be useful,"" *   but WITHOUT ANY WARRANTY; without even the implied warranty of"" *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU"" *   Lesser General Public License for more details."" *"" *   You should have received a copy of the GNU Lesser General Public"" *   License along with this library; if not, write to the Free Software"" *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US"" *"" */" };
// Driver file name defaults
#ifdef WIN32
// Windowsconst char* DbConnection::driverPaths[] = {        "",      "dbconndrv-mysql.dll",      "dbconndrv-msql.dll",      "dbconndrv-postgresql.dll",      "dbconndrv-ibmDB2.dll",      "dbconndrv-oracle8.dll",      "dbconndrv-odbc.dll"   };
#else
// Unices
const char* DbConnection::driverPaths[] = {  
      "",
      "dbconndrv-mysql.so",
      "dbconndrv-msql.so",
      "dbconndrv-postgresql.so",      "dbconndrv-ibmDB2.so",
      "dbconndrv-oracle8.so",
      "dbconndrv-odbc.so"
   };
#endifconst char* DbConnection::driverNames[] = {        "NONE",      "MYSQL",      "MSQL",      "POSTGRESQL",      "DB2",      "ORACLE8",      "ODBC"   };//------------------------------------------------------------------------------// PRIVATE//------------------------------------------------------------------------------//------------------------------------------------------------------------------// DbConnection::_buildCompatibilityTable//------------------------------------------------------------------------------void DbConnection::_buildCompatibilityTable(){   // Hard coded lookup table.   vector<string> versions;        // Populate version 0.3.5   versions.push_back("0.3.4");   versions.push_back("0.3.5");   compatibilityTable["0.3.5"] = versions;}  // DbConnection::_buildCompatibilityTable      //------------------------------------------------------------------------------// PUBLIC//------------------------------------------------------------------------------//------------------------------------------------------------------------------// DbConnection::DbConnection//------------------------------------------------------------------------------DbConnection::DbConnection(         Driver driver,          const string &configFile):   ptr_getAuthor(NULL),   ptr_getVendor(NULL),   ptr_getCopyright(NULL),   ptr_getDriverType(NULL),   ptr_getDriverName(NULL),   ptr_getDriverDesc(NULL),   ptr_getDbConnectVersion(NULL),   ptr_createDriverInstance(NULL),   ptr_destroyDriverInstance(NULL),   drvRef(NULL){   // Build the compatibility table.   _buildCompatibilityTable();   /* Steps to finding the config file in order to determine the path and config    *  parameters for the drivers.    * 1. Use the file iif given in configFile param.    * 2. Look in the current directory for the config file. (./dbconnect.cfg on unix).    * 3. Look in the systems config dir for the file.       (eg /etc/dbconnect.cfg on unix).    * 4. If still no library path, then use defaults.    */   long i;   string driverPath = "";   vector<string> args;       // 1. Use the file iif given in configFile param.   if (configFile.length() > 0)   {      // Try and read the information from the config file.      ConfigFile configFile(configFile.c_str());      configFile.read();      if (configFile.hasKeyName("dbconnect", "driverPath"))      {         driverPath = configFile["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 (configFile.hasKeyName(driverNames[driver], "driverFile"))         driverPath += configFile[driverNames[driver]]["driverFile"];      else         driverPath += driverPaths[driver];      // Check if we have a driver section and try and obtain needed values.      if (configFile.hasSection(driverNames[driver]))      {                    //Get all the driver arguments and values         vector<string> keyNames = configFile.getSectionKeyNames(driverNames[driver]);         for (i=0; i<keyNames.size(); i++)         {            args.push_back(keyNames[i].c_str());                                   // Name            args.push_back(configFile[driverNames[driver]][keyNames[i].c_str()]);  // Value         }      }   }   else  // Try and find a config file.   {      try      {         // 2. Look in the current directory for the config file. (./dbconnect.cfg on unix).         ConfigFile configLocalFile(LOCALCONFIG);         configLocalFile.read();         if (configLocalFile.hasKeyName("dbconnect", "driverPath"))         {            driverPath = configLocalFile["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 (configLocalFile.hasKeyName(driverNames[driver], "driverFile"))            driverPath += configLocalFile[driverNames[driver]]["driverFile"];         else            driverPath += driverPaths[driver];         // Check if we have a driver section and try and obtain needed values.         if (configLocalFile.hasSection(driverNames[driver]))         {                       //Get all the driver arguments and values            vector<string> keyNames = configLocalFile.getSectionKeyNames(driverNames[driver]);            for (i=0; i<keyNames.size(); i++)            {               args.push_back(keyNames[i].c_str());                                        // Name               args.push_back(configLocalFile[driverNames[driver]][keyNames[i].c_str()]);  // Value            }         }
      }      catch(...)      {         try         {            // 3. Look in the systems config dir for the file. (eg /etc/dbconnect.cfg on unix).

⌨️ 快捷键说明

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