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

📄 msqlquery.cpp

📁 C++连接一写常用数据库的接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * MsqlQuery object defines the needed query functions for the dbConnect mSQL driver * Copyright (C) 2003 Johnathan Ingram, jingram@rogue-order.net * * 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 "msqlQuery.h"#include <time.h>#include <string.h>#include "dbconnectTypes.h"// -----------------------------------------------------------------------------// PRIVATE:// -----------------------------------------------------------------------------// Thread mutexes. mSQL is not thread safe at all and requires to be locked by// all objectsstatic SimpleThread_Mutex queryAndStoreResultMutex;//------------------------------------------------------------------------------// MsqlQuery::_msqGetFieldsInformation//------------------------------------------------------------------------------void MsqlQuery::_msqlGetFieldsInformation(){   // Clear any previouse field information just incase.   _freeCollection(FIELD_INFORMATION);   // Internal method used to build the field information.	m_field *field_prop= NULL;      // Allocate the information pointers   _numFieldInformation = _fieldCount;   _fieldInformation = (BaseFieldDescription**)malloc(_numFieldInformation * sizeof(BaseFieldDescription*));      // Assign the data to the corresponding fields.   for (int i=0; i<_numFieldInformation; i++)   {      field_prop = msqlFetchField(_result);       if (field_prop != NULL)      {         // Field Name.         string fName = field_prop->name;               // Work out the field type.         FieldType _type = _msqlResolveFieldType(field_prop->type);         // Field Properties.      	long int  _precision = 0;        // Not supported in mSQL         long int  _scale = 0;            // Not supported in mSQL         bool     _isIncrement = false;   // Not supported in mSQL         bool     _isPriKey    = false;   // Not supported in mSQL         bool     _isUnique    = IS_UNIQUE(field_prop->flags);         bool     _isNotNull   = IS_NOT_NULL(field_prop->flags);         // Add the field properties to the vector.         _fieldInformation[i] = new BaseFieldDescription(fName, i, _type, _isIncrement, _isPriKey, _isUnique, _isNotNull);      }   }}  // MsqlQuery::_msqGetFieldsInformation//------------------------------------------------------------------------------// MsqlQuery::_msqlGetResultSetRow//------------------------------------------------------------------------------voidMsqlQuery::_msqlGetResultSetRow(){   // Free the current field values just incase   _freeCollection(FIELD_VALUES);      // Retrieve the data from the current row.   m_row     rowData;   unsigned long *row_lengths;    // Unsigned long as this is how the function is defined   try   {      // Fetch the row and advance the cursor with msqlFetchRow.      rowData = msqlFetchRow(_result);      // Allocate the value pointers      _numRecordValues = _fieldCount;      _recordValues = (BaseValue**)malloc(_numRecordValues * sizeof(BaseValue*));                  // Assign the data to the corresponding fields.      for (int i=0; i<_numRecordValues; i++)      {         _recordValues[i] = new BaseValue(_fieldInformation[i]->name().c_str());			// Check if the field is NULL         if (rowData[i] == NULL)         {            _recordValues[i]->setNULL();         }         else         {            // mSQL result is a NULL terminated string            _recordValues[i]->setString((const char*)rowData[i]);         }         		}   }   catch(...)   { }   }  // MsqlQuery::_msqlGetResultSetRow//------------------------------------------------------------------------------// MsqlQuery::_msqlResolveFieldType//------------------------------------------------------------------------------FieldTypeMsqlQuery::_msqlResolveFieldType(   int type){   FieldType res;      switch(type)   {      case INT_TYPE:         res = FT_LONG;         break;            case CHAR_TYPE:         res = FT_STRING;         break;            case REAL_TYPE:         res = FT_DOUBLE;         break;            case TEXT_TYPE:       // Keep an eye on this mapping         res = FT_BLOB;         break;            case DATE_TYPE:         res = FT_DATETIME;         break;      case UINT_TYPE:         res = FT_UNSIGNED_LONG;         break;      case MONEY_TYPE:         res = FT_DOUBLE;         break;      case TIME_TYPE:         res = FT_DATETIME;         break;      case IPV4_TYPE:         res = FT_STRING;         break;      case INT64_TYPE:         res = FT_LONG;         break;      case UINT64_TYPE:         res = FT_UNSIGNED_LONG;         break;      case INT8_TYPE:         res = FT_SHORT;         break;      case INT16_TYPE:         res = FT_SHORT;         break;      case UINT8_TYPE:         res = FT_UNSIGNED_SHORT;         break;      case UINT16_TYPE:         res = FT_UNSIGNED_SHORT;         break;      default:         res = FT_UNKNOWN;         break;         }      return res;}  // MsqlQuery::_msqlResolveFieldType//------------------------------------------------------------------------------// MsqlQuery::_msqlParseBindParameters//------------------------------------------------------------------------------stringMsqlQuery::_msqlParseBindParameters(      const string& originalSqlStatement){   // Try and substitute the parameters checking to make sure they exist.   string res = originalSqlStatement;    int pos;      for (int i =0; i<_numParameters; i++)   {      string paramName = ":" + _parameters[i]->name();      if ((pos = res.find(paramName)) == string::npos)      {         // We have a parameter that is not present in our sql statement         string err = "_msqlParseBindParameters(): The specified bind parameter, ";         err += paramName;         err += ", is not present in the SQL statement: ";         err += originalSqlStatement;         throw BindParameterNotPresent(err);      }            // Substitute the parameter with the correct value according to the parameter type.      res.replace(pos, paramName.length(), _parameters[i]->paramToMSQLString());   }      // Make sure there are no parameters left   if (((pos = res.find(" :")) != string::npos) ||        ((pos = res.find("(:")) != string::npos) ||        ((pos = res.find(",:")) != string::npos) )   {      // We have a parameter that has not been set.      pos += 1;      int pos2 = res.find(" ", pos);      int pos3 = res.find(")", pos);      int pos4 = res.find(",", pos);      if (pos2 == string::npos) pos2 = 65535;      if (pos3 == string::npos) pos3 = 65535;      if (pos4 == string::npos) pos4 = 65535;                     int endPos;            if (pos2 < pos3 && pos2 < pos4)         endPos = pos2;      if (pos3 < pos2 && pos3 < pos4)         endPos = pos3;      if (pos4 < pos2 && pos4 < pos3)         endPos = pos4;                 string unknownParam = res.substr(pos, endPos-pos);            string err = "_msqlParseBindParameters(): The bind parameter, ";      err += unknownParam;      err += ", in the SQL statement: ";      err += originalSqlStatement;      err += " has not been set.";      throw BindParameterNotSet(err);   }      return res;}  // MsqlQuery::_msqlParseBindParameters//------------------------------------------------------------------------------// MsqlQuery::_freeCollection//------------------------------------------------------------------------------void MsqlQuery::_freeCollection(   CollectionType type){   int i;   switch (type)   {      case FIELD_INFORMATION:         if (_fieldInformation)         {            for (i=0; i<_numFieldInformation; i++)            {               if (_fieldInformation[i])               {                  delete _fieldInformation[i];                  _fieldInformation[i] = NULL;                        }            }            free(_fieldInformation);            _fieldInformation = NULL;            _numFieldInformation = 0;            _fieldCount = 0;         }               break;                 case FIELD_VALUES:         if (_recordValues)         {            for (i=0; i<_numRecordValues; i++)            {               if (_recordValues[i])               {                  delete _recordValues[i];                  _recordValues[i] = NULL;                        }            }            free(_recordValues);            _recordValues = NULL;            _numRecordValues = 0;         }                break;               case BIND_PARAMETERS:         if (_parameters)         {            for (i=0; i<_numParameters; i++)            {

⌨️ 快捷键说明

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