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

📄 msqlquery.cpp

📁 C++连接一写常用数据库的接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
               if (_parameters[i])               {                  delete _parameters[i];                  _parameters[i] = NULL;                        }            }            free(_parameters);            _parameters = NULL;            _numParameters = 0;         }                        break;         }}  // MsqlQuery::_freeCollection// -----------------------------------------------------------------------------// PUBLIC:// -----------------------------------------------------------------------------//------------------------------------------------------------------------------// MsqlQuery::MsqlQuery//------------------------------------------------------------------------------MsqlQuery::MsqlQuery(   MsqlConnection* parentConnection,   int             index):    _parentConnection(parentConnection),   _index(index),   _result(NULL),   _msqlNumRows(0),   _msqlCurrentRow(0),   _recordValues(NULL),   _numRecordValues(0),   _fieldInformation(NULL),   _numFieldInformation(0),   _parameters(NULL),   _numParameters(0){}  // MsqlQuery::MsqlQuery//------------------------------------------------------------------------------// MsqlQuery::~MsqlQuery//------------------------------------------------------------------------------MsqlQuery::~MsqlQuery(){      // Would need to rollback if transactions where supported      //Make sure any stored query results are freed.   if (_result != NULL)   {      msqlFreeResult(_result);      _result = NULL;   }      // Free any parameters   _freeCollection(BIND_PARAMETERS);      // Free the current field values   _freeCollection(FIELD_VALUES);     // Clear any previouse field information.   _freeCollection(FIELD_INFORMATION);   // Release this connection from the parent connection object.   _parentConnection->releaseQueryConnection(this);}  // MsqlQuery::~MsqlQuery//------------------------------------------------------------------------------// MsqlQuery::clearBindParams//------------------------------------------------------------------------------void MsqlQuery::clearBindParams(){   // Free all the bind parameters.   _freeCollection(BIND_PARAMETERS);   }  // MsqlQuery::clearBindParams//------------------------------------------------------------------------------// MsqlQuery::command//------------------------------------------------------------------------------voidMsqlQuery::command(   const string& sqlStatement){   // Call the base query to store a copy of the query.   BaseQuery::command(sqlStatement);      // Clear any bind parameters as we now have a new query   clearBindParams();} // MsqlQuery::command//------------------------------------------------------------------------------// MsqlQuery::bindParam//------------------------------------------------------------------------------BaseValue*MsqlQuery::bindParam(   const string& paramName){   // Make sure the name has not already been added. If it has, return the instance to it   for (int i=0; i<_numParameters; i++)      if (strcasecmp(_parameters[i]->bindName.c_str(), paramName.c_str()) == 0)         return _parameters[i];   // Make sure the parameter is present in the quey   if (!_isBindParameterPresent(paramName))   {      string err = "bindParam(): The specified bind parameter, ";      err += paramName;      err += ", is not present in the SQL statement: ";      err += _sqlStatement;            throw BindParameterNotPresent(err);   }         // Add the value to the parameters array.   _numParameters++;   _parameters = (MsqlBindParam**)realloc((void*)_parameters, _numParameters * sizeof(MsqlBindParam*));   _parameters[_numParameters-1] = new MsqlBindParam(paramName);           return _parameters[_numParameters-1];   }  // MsqlQuery::bindParam//------------------------------------------------------------------------------// MsqlQuery::execute//------------------------------------------------------------------------------voidMsqlQuery::execute(){   //Make sure any stored query results are freed.   if (_result != NULL)   {      msqlFreeResult(_result);      _result = NULL;   }   // Number of rows in the result set is now 0   _msqlNumRows = 0;   _msqlCurrentRow = 0;      // Clear any previouse field information.   _freeCollection(FIELD_INFORMATION);       // Free the current field values   _freeCollection(FIELD_VALUES);   // Resolve the bind parameters   string resolvedSqlStatement = _msqlParseBindParameters(_sqlStatement);        // Ping the connection to make sure it is still valid.   _parentConnection->_msqlPing(_index);   // Execute the query   // Lock the static mutex. Need to query and store results in a single thread at a time   // Damn the mSQL API is really really not thread safe at all.   SimpleThread_Synchronize sync(queryAndStoreResultMutex);   _parentConnection->_msqlQuery(_index, resolvedSqlStatement);   // Determine if we have any data or not and get the field structures if we do.   // Using store result. This should change to be configurable from the config file.   _result = msqlStoreResult();   if (( _msqlNumRows = msqlNumRows(_result)) == 0)   {      // Nope, its a DML statement.      _fieldCount = 0;      _eof = true;   }   else   {      // We have data      _fieldCount = msqlNumFields(_result);          // Get the field information for this query.      _msqlGetFieldsInformation();                 _eof = false;   }   }  // MsqlQuery::execute//------------------------------------------------------------------------------// MsqlQuery::next//------------------------------------------------------------------------------voidMsqlQuery::fetchNext(){   // Do nothing if the we have reached the end of file.   if (_eof)      return;      _msqlGetResultSetRow();   _msqlCurrentRow++;      // Check if we are at the end of the result set.   if (_msqlCurrentRow >= _msqlNumRows)      _eof = true;   }  // MsqlQuery::next//------------------------------------------------------------------------------// MsqlQuery::transBegin//------------------------------------------------------------------------------voidMsqlQuery::transBegin(){   throw NotImplemented("transBegin(): The transBegin function is not implemented in the mSQL driver.");}  // MsqlQuery::transBegin      //------------------------------------------------------------------------------// MsqlQuery::commit//------------------------------------------------------------------------------voidMsqlQuery::commit(){   throw NotImplemented("commit(): The commit function is not implemented in the mSQL driver.");   }  // MsqlQuery::commit      //------------------------------------------------------------------------------// MsqlQuery::rollback//------------------------------------------------------------------------------voidMsqlQuery::rollback(){   throw NotImplemented("rollback(): The rollback function is not implemented in the mSQL driver.");   }  // MsqlQuery::rollback//------------------------------------------------------------------------------// MsqlQuery::getFieldInfoByColumn//------------------------------------------------------------------------------BaseFieldDescription*MsqlQuery::getFieldInfoByColumn(   int index){   // Make sure the index is in range.   if (index < 0 || index >= _numFieldInformation)      throw IndexOutOfRange("getFieldInfoByColumn(): The field index is out of range for the current result set");   return _fieldInformation[index];}  // MsqlQuery::getFieldInfoByColumn//------------------------------------------------------------------------------// MsqlQuery::getFieldInfoByName//------------------------------------------------------------------------------BaseFieldDescription*MsqlQuery::getFieldInfoByName(   const string& fieldName){   // Try and find the field name   for (int i=0; i<_numFieldInformation; i++)      if (strcasecmp(_fieldInformation[i]->name().c_str(), fieldName.c_str()) == 0)         return _fieldInformation[i];   throw NameNotFound("getFieldInfoByName(): The field name was not found for the current result set.");}  // MsqlQuery::getFieldInfoByName//------------------------------------------------------------------------------// MsqlQuery::getFieldByColumn//------------------------------------------------------------------------------BaseValue*MsqlQuery::getFieldByColumn(   int index){   // Make sure the index is in range.   if (index < 0 || index >= _numRecordValues)      throw IndexOutOfRange("getFieldByColumn(): The field index is out of range for the current result set");   return _recordValues[index];   }  // MsqlQuery::getFieldByColumn   //------------------------------------------------------------------------------// MsqlQuery::getFieldByName//------------------------------------------------------------------------------BaseValue*MsqlQuery::getFieldByName(   const string& fieldName){   // Try and find the field name   for (int i=0; i<_numRecordValues; i++)      if (strcasecmp(_recordValues[i]->name().c_str(), fieldName.c_str()) == 0)         return _recordValues[i];   throw NameNotFound("getFieldByName(): The field name was not found for the current result set.");   }  // MsqlQuery::getFieldByName

⌨️ 快捷键说明

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