📄 mysqlquery.cpp
字号:
/* * MysqQuery object defines the needed query functions for the dbConnect MySQL driver * Copyright (C) 2002 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 "mysqlQuery.h"#include <time.h>#include <string.h>#include "mysqlConnection.h"#include "dbconnectTypes.h"// -----------------------------------------------------------------------------// PRIVATE:// -----------------------------------------------------------------------------//------------------------------------------------------------------------------// MysqlQuery::_mysqGetFieldsInformation//------------------------------------------------------------------------------void MysqlQuery::_mysqlGetFieldsInformation(){ // Clear any previouse field information just incase. _freeCollection(FIELD_INFORMATION); // Internal method used to build the field information. MYSQL_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 = mysql_fetch_field(__mysql_res__); if (field_prop != NULL) { // Field Name. string fName = field_prop->name; // Work out the field type. FieldType _type = _mysqlResolveFieldType(field_prop->type); // Field Properties. long int _precision = 0; long int _scale = 0; bool _isIncrement = field_prop->flags & AUTO_INCREMENT_FLAG; bool _isPriKey = field_prop->flags & PRI_KEY_FLAG; bool _isUnique = field_prop->flags & UNIQUE_KEY_FLAG; bool _isNotNull = field_prop->flags & NOT_NULL_FLAG; if (_type == FT_DOUBLE) { _precision = field_prop->max_length; //Seems to work for the precision. _scale = field_prop->decimals; } // Add the field properties to the vector. _fieldInformation[i] = new BaseFieldDescription(fName, i, _type, _isIncrement, _isPriKey, _isUnique, _isNotNull); } }} // MysqlQuery::_mysqGetFieldsInformation//------------------------------------------------------------------------------// MysqlQuery::_mysqlGetResultSetRow//------------------------------------------------------------------------------voidMysqlQuery::_mysqlGetResultSetRow(){ // Free the current field values just incase _freeCollection(FIELD_VALUES); // Retrieve the data from the current row. MYSQL_ROW row_data; unsigned long *row_lengths; // Unsigned long as this is how the function is defined try { // Fetch the row and data sizes and advance the cursor with mysql_fetch_row. row_data = mysql_fetch_row(__mysql_res__); row_lengths = mysql_fetch_lengths(__mysql_res__); // Allocate the value pointers _numRecordValues = _fieldCount; _recordValues = (MysqlValue**)malloc(_numRecordValues * sizeof(MysqlValue*)); // Assign the data to the corresponding fields. for (int i=0; i<_numRecordValues; i++) { // Allocate the field _recordValues[i] = new MysqlValue(_fieldInformation[i]->name().c_str()); // Check if the field is NULL if (row_data[i] == NULL) { _recordValues[i]->setNULL(); } else { char* data = (char*)malloc(row_lengths[i] + 1); strncpy(data, row_data[i], row_lengths[i]); if (_fieldInformation[i]->type() == FT_BLOB) { // BLOB type, set it as a Binary field and do not free the memory as the MysqlValue Object will _recordValues[i]->setBinary((void*)data, row_lengths[i]); } else { // Need to first convert the MySQL result to a NULL terminating string data[row_lengths[i]] = '\0'; _recordValues[i]->setString((const char*)data);
free((void*)data); } } } } catch(...) { }} // MysqlQuery::_mysqlGetResultSetRow//------------------------------------------------------------------------------// MysqlQuery::_mysqlResolveFieldType//------------------------------------------------------------------------------FieldTypeMysqlQuery::_mysqlResolveFieldType( enum_field_types type){ FieldType res; switch(type) { case FIELD_TYPE_TINY: res = FT_SHORT; break; case FIELD_TYPE_SHORT: res = FT_SHORT; break; case FIELD_TYPE_LONG: res = FT_LONG; break; case FIELD_TYPE_INT24: res = FT_LONG; break; case FIELD_TYPE_LONGLONG: res = FT_LONG; break; case FIELD_TYPE_DECIMAL: res = FT_DOUBLE; break; case FIELD_TYPE_FLOAT: res = FT_DOUBLE; break; case FIELD_TYPE_DOUBLE: res = FT_DOUBLE; break; case FIELD_TYPE_TIMESTAMP: res = FT_DATETIME; break; case FIELD_TYPE_DATE: res = FT_DATETIME; break; case FIELD_TYPE_TIME: res = FT_DATETIME; break; case FIELD_TYPE_DATETIME: res = FT_DATETIME; break; case FIELD_TYPE_YEAR: res = FT_DATETIME; break; case FIELD_TYPE_STRING: res = FT_STRING; break; case FIELD_TYPE_BLOB: res = FT_BLOB; break; case FIELD_TYPE_TINY_BLOB: res = FT_BLOB; break; case FIELD_TYPE_MEDIUM_BLOB: res = FT_BLOB; break; case FIELD_TYPE_LONG_BLOB: res = FT_BLOB; break; case FIELD_TYPE_SET: res = FT_STRING; break; case FIELD_TYPE_ENUM: res = FT_STRING; break; case FIELD_TYPE_NULL: res = FT_NULL; break; case FIELD_TYPE_VAR_STRING: res = FT_STRING; break; default: res = FT_UNKNOWN; break; } return res;} // MysqlQuery::_mysqlResolveFieldType //------------------------------------------------------------------------------// MysqlQuery::_mysqlParseBindParameters//------------------------------------------------------------------------------stringMysqlQuery::_mysqlParseBindParameters( 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 = "_mysqlParseBindParameters(): 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]->paramToMySQLString()); } // 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 = "_mysqlParseBindParameters(): The bind parameter, "; err += unknownParam; err += ", in the SQL statement: "; err += originalSqlStatement; err += " has not been set."; throw BindParameterNotSet(err); } return res;} // MysqlQuery::_mysqlParseBindParameters//------------------------------------------------------------------------------// MysqlQuery::_freeCollection//------------------------------------------------------------------------------void MysqlQuery::_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++) { if (_parameters[i]) { delete _parameters[i]; _parameters[i] = NULL; } } free(_parameters); _parameters = NULL; _numParameters = 0; } break; }} // MysqlQuery::_freeCollection// -----------------------------------------------------------------------------// PUBLIC:// -----------------------------------------------------------------------------//------------------------------------------------------------------------------// MysqlQuery::MysqlQuery//------------------------------------------------------------------------------MysqlQuery::MysqlQuery( MysqlConnection* parentConnection, int index): _parentConnection(parentConnection), _index(index), _isTransaction(false), __mysql_res__(NULL), _mysqlNumRows(0),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -