123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #if TARS_MYSQL
- #include "util/tc_mysql.h"
- #include "util/tc_common.h"
- #include "util/tc_des.h"
- #include "util/tc_base64.h"
- #include "errmsg.h"
- #include <sstream>
- #include <string.h>
- namespace tars
- {
- TC_Mysql::TC_Mysql()
- :_bConnected(false)
- {
- _pstMql = mysql_init(NULL);
- }
- TC_Mysql::TC_Mysql(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag)
- :_bConnected(false)
- {
- init(sHost, sUser, sPasswd, sDatabase, sCharSet, port, iFlag);
-
- _pstMql = mysql_init(NULL);
- }
- TC_Mysql::TC_Mysql(const TC_DBConf& tcDBConf)
- :_bConnected(false)
- {
- _dbConf = tcDBConf;
-
- _pstMql = mysql_init(NULL);
- }
- TC_Mysql::~TC_Mysql()
- {
- if (_pstMql != NULL)
- {
- mysql_close(_pstMql);
- _pstMql = NULL;
- }
- }
- void TC_Mysql::init(const string& sHost, const string& sUser, const string& sPasswd, const string& sDatabase, const string &sCharSet, int port, int iFlag)
- {
- _dbConf._host = sHost;
- _dbConf._user = sUser;
- _dbConf._password = sPasswd;
- _dbConf._database = sDatabase;
- _dbConf._charset = sCharSet;
- _dbConf._port = port;
- _dbConf._flag = iFlag;
- }
- void TC_Mysql::init(const TC_DBConf& tcDBConf)
- {
- _dbConf = tcDBConf;
- }
- TC_DBConf TC_Mysql::getDBConf()
- {
- return _dbConf;
- }
- void TC_Mysql::connect()
- {
- disconnect();
- if( _pstMql == NULL)
- {
- _pstMql = mysql_init(NULL);
- }
- //建立连接后, 自动调用设置字符集语句
- if(!_dbConf._charset.empty()) {
- if (mysql_options(_pstMql, MYSQL_SET_CHARSET_NAME, _dbConf._charset.c_str())) {
- throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_SET_CHARSET_NAME ") + _dbConf._charset + ":" + string(mysql_error(_pstMql)));
- }
- }
-
- //设置连接超时
- if(_dbConf._connectTimeout > 0) {
- if (mysql_options(_pstMql, MYSQL_OPT_CONNECT_TIMEOUT, &_dbConf._connectTimeout)) {
- throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_CONNECT_TIMEOUT ") + TC_Common::tostr(_dbConf._connectTimeout) + ":" + string(mysql_error(_pstMql)));
- }
- }
-
- if(_dbConf._writeReadTimeout > 0) {
- //设置读超时
- if (mysql_options(_pstMql, MYSQL_OPT_READ_TIMEOUT, &_dbConf._writeReadTimeout)) {
- throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_READ_TIMEOUT ") + TC_Common::tostr(_dbConf._writeReadTimeout) + ":" + string(mysql_error(_pstMql)));
- }
- //设置写超时
- if (mysql_options(_pstMql, MYSQL_OPT_WRITE_TIMEOUT, &_dbConf._writeReadTimeout)) {
- throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_WRITE_TIMEOUT ") + TC_Common::tostr(_dbConf._writeReadTimeout) + ":" + string(mysql_error(_pstMql)));
- }
- }
- if (mysql_real_connect(_pstMql, _dbConf._host.c_str(), _dbConf._user.c_str(), _dbConf._password.c_str(), _dbConf._database.c_str(), _dbConf._port, NULL, _dbConf._flag) == NULL)
- {
- throw TC_Mysql_Exception("[TC_Mysql::connect]: mysql_real_connect: " + string(mysql_error(_pstMql)));
- }
-
- _bConnected = true;
- }
- void TC_Mysql::disconnect()
- {
- if (_pstMql != NULL)
- {
- mysql_close(_pstMql);
- _pstMql = mysql_init(NULL);
- }
-
- _bConnected = false;
- }
- string TC_Mysql::escapeString(const string& sFrom)
- {
- string sTo;
- string::size_type iLen = sFrom.length() * 2 + 1;
- char *pTo = (char *)malloc(iLen);
- memset(pTo, 0x00, iLen);
-
- mysql_escape_string(pTo, sFrom.c_str(), sFrom.length());
- sTo = pTo;
- free(pTo);
- return sTo;
- }
- string TC_Mysql::buildInsertSQLNoSafe(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- return buildSQLNoSafe(sTableName, "insert", mpColumns);
- }
- string TC_Mysql::buildInsertSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns)
- {
- return buildBatchSQLNoSafe(sTableName, "insert", mpColumns);
- }
- string TC_Mysql::buildReplaceSQLNoSafe(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- return buildSQLNoSafe(sTableName, "replace", mpColumns);
- }
- string TC_Mysql::buildReplaceSQLNoSafe(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns)
- {
- return buildBatchSQLNoSafe(sTableName, "replace", mpColumns);
- }
- string TC_Mysql::buildSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns)
- {
- ostringstream sColumnNames;
- ostringstream sColumnValues;
-
- map<string, pair<FT, string> >::const_iterator itEnd = mpColumns.end();
- for(map<string, pair<FT, string> >::const_iterator it = mpColumns.begin(); it != itEnd; ++it)
- {
- if (it == mpColumns.begin())
- {
- sColumnNames << "`" << it->first << "`";
- if(it->second.first == DB_INT)
- {
- sColumnValues << it->second.second;
- }
- else
- {
- sColumnValues << "'" << escapeString(it->second.second) << "'";
- }
- }
- else
- {
- sColumnNames << ",`" << it->first << "`";
- if(it->second.first == DB_INT)
- {
- sColumnValues << "," + it->second.second;
- }
- else
- {
- sColumnValues << ",'" + escapeString(it->second.second) << "'";
- }
- }
- }
- ostringstream os;
- os << command << " into " << sTableName << " (" << sColumnNames.str() << ") values (" << sColumnValues.str() << ")";
- return os.str();
- }
- string TC_Mysql::buildBatchSQLNoSafe(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns)
- {
- if(mpColumns.empty())
- return "";
- ostringstream sColumnNames;
- ostringstream sColumnValues;
- size_t count = mpColumns.begin()->second.second.size();
- auto itEnd = mpColumns.end();
- for(auto it = mpColumns.begin(); it != itEnd; ++it)
- {
- if(it == mpColumns.begin())
- {
- sColumnNames << "`" << it->first << "`";
- }
- else
- {
- sColumnNames << ",`" << it->first << "`";
- }
- if(count != it->second.second.size())
- {
- throw TC_Mysql_Exception("[TC_Mysql::buildBatchSQLNoSafe]: column count not same!");
- }
- }
- for(size_t i = 0; i < count; i++)
- {
- sColumnValues << "(";
- auto itEnd = mpColumns.end();
- for(auto it = mpColumns.begin(); it != itEnd; ++it)
- {
- if(it != mpColumns.begin())
- sColumnValues << ",";
- if(it->second.first == DB_INT)
- {
- sColumnValues << it->second.second[i];
- }
- else
- {
- sColumnValues << "'" << escapeString(it->second.second[i]) << "'";
- }
- }
- sColumnValues << ")";
- if(i != count - 1)
- sColumnValues << ",";
- }
- ostringstream os;
- os << command << " into " << sTableName << " (" << sColumnNames.str() << ") values " << sColumnValues.str();
- return os.str();
- }
- string TC_Mysql::buildUpdateSQLNoSafe(const string &sTableName,const RECORD_DATA &mpColumns, const string &sWhereFilter)
- {
- ostringstream sColumnNameValueSet;
- map<string, pair<FT, string> >::const_iterator itEnd = mpColumns.end();
- for(map<string, pair<FT, string> >::const_iterator it = mpColumns.begin(); it != itEnd; ++it)
- {
- if (it == mpColumns.begin())
- {
- sColumnNameValueSet << "`" << it->first << "`";
- }
- else
- {
- sColumnNameValueSet << ",`" << it->first << "`";
- }
- if(it->second.first == DB_INT)
- {
- sColumnNameValueSet << "= " << it->second.second;
- }
- else
- {
- sColumnNameValueSet << "= '" << escapeString(it->second.second) << "'";
- }
- }
- ostringstream os;
- os << "update " << sTableName << " set " << sColumnNameValueSet.str() << " " << sWhereFilter;
- return os.str();
- }
- string TC_Mysql::realEscapeString(const string& sFrom)
- {
- if(!_bConnected)
- {
- connect();
- }
- string sTo;
- string::size_type iLen = sFrom.length() * 2 + 1;
- char *pTo = (char *)malloc(iLen);
- memset(pTo, 0x00, iLen);
- mysql_real_escape_string(_pstMql, pTo, sFrom.c_str(), sFrom.length());
- sTo = pTo;
- free(pTo);
- return sTo;
- }
- MYSQL *TC_Mysql::getMysql(void)
- {
- return _pstMql;
- }
- string TC_Mysql::buildInsertSQL(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- return buildSQL(sTableName, "insert", mpColumns);
- }
- string TC_Mysql::buildInsertSQL(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns)
- {
- return buildBatchSQL(sTableName, "insert", mpColumns);
- }
- string TC_Mysql::buildReplaceSQL(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- return buildSQL(sTableName, "replace", mpColumns);
- }
- string TC_Mysql::buildReplaceSQL(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns)
- {
- return buildBatchSQL(sTableName, "replace", mpColumns);
- }
- string TC_Mysql::buildSQL(const string &sTableName, const string &command, const map<string, pair<FT, string> > &mpColumns)
- {
- ostringstream sColumnNames;
- ostringstream sColumnValues;
- map<string, pair<FT, string> >::const_iterator itEnd = mpColumns.end();
- for(map<string, pair<FT, string> >::const_iterator it = mpColumns.begin(); it != itEnd; ++it)
- {
- if (it == mpColumns.begin())
- {
- sColumnNames << "`" << it->first << "`";
- if(it->second.first == DB_INT)
- {
- sColumnValues << it->second.second;
- }
- else
- {
- sColumnValues << "'" << realEscapeString(it->second.second) << "'";
- }
- }
- else
- {
- sColumnNames << ",`" << it->first << "`";
- if(it->second.first == DB_INT)
- {
- sColumnValues << "," + it->second.second;
- }
- else
- {
- sColumnValues << ",'" << realEscapeString(it->second.second) << "'";
- }
- }
- }
- ostringstream os;
- os << command << " into " << sTableName << " (" << sColumnNames.str() << ") values (" << sColumnValues.str() << ")";
- return os.str();
- }
- string TC_Mysql::buildBatchSQL(const string &sTableName, const string &command, const map<string, pair<FT, vector<string> >> &mpColumns)
- {
- if(mpColumns.empty())
- return "";
- ostringstream sColumnNames;
- ostringstream sColumnValues;
- size_t count = mpColumns.begin()->second.second.size();
- auto itEnd = mpColumns.end();
- for(auto it = mpColumns.begin(); it != itEnd; ++it)
- {
- if(it == mpColumns.begin())
- {
- sColumnNames << "`" << it->first << "`";
- }
- else
- {
- sColumnNames << ",`" << it->first << "`";
- }
- if(count != it->second.second.size())
- {
- throw TC_Mysql_Exception("[TC_Mysql::buildBatchSQL]: column count not same!" + TC_Common::tostr(count) + " !=" + TC_Common::tostr(it->second.second.size()));
- }
- }
- for(size_t i = 0; i < count; i++)
- {
- sColumnValues << "(";
- auto itEnd = mpColumns.end();
- for(auto it = mpColumns.begin(); it != itEnd; ++it)
- {
- if(it != mpColumns.begin())
- sColumnValues << ",";
- if(it->second.first == DB_INT)
- {
- sColumnValues << it->second.second[i];
- }
- else
- {
- sColumnValues << "'" << realEscapeString(it->second.second[i]) << "'";
- }
- }
- sColumnValues << ")";
- if(i != count - 1)
- sColumnValues << ",";
- }
- ostringstream os;
- os << command << " into " << sTableName << " (" << sColumnNames.str() << ") values " << sColumnValues.str();
- return os.str();
- }
- string TC_Mysql::buildUpdateSQL(const string &sTableName,const RECORD_DATA &mpColumns, const string &sWhereFilter)
- {
- ostringstream sColumnNameValueSet;
- map<string, pair<FT, string> >::const_iterator itEnd = mpColumns.end();
- for(map<string, pair<FT, string> >::const_iterator it = mpColumns.begin(); it != itEnd; ++it)
- {
- if (it == mpColumns.begin())
- {
- sColumnNameValueSet << "`" << it->first << "`";
- }
- else
- {
- sColumnNameValueSet << ",`" << it->first << "`";
- }
- if(it->second.first == DB_INT)
- {
- sColumnNameValueSet << "= " << it->second.second;
- }
- else
- {
- sColumnNameValueSet << "= '" << realEscapeString(it->second.second) << "'";
- }
- }
- ostringstream os;
- os << "update " << sTableName << " set " << sColumnNameValueSet.str() << " " << sWhereFilter;
- return os.str();
- }
- string TC_Mysql::getVariables(const string &sName)
- {
- string sql = "SHOW VARIABLES LIKE '" + sName + "'";
- MysqlData data = queryRecord(sql);
- if(data.size() == 0)
- {
- return "";
- }
- if(sName == data[0]["Variable_name"])
- {
- return data[0]["Value"];
- }
- return "";
- }
- void TC_Mysql::execute(const string& sSql)
- {
- /**
- 没有连上, 连接数据库
- */
- if(!_bConnected)
- {
- connect();
- }
- _sLastSql = sSql;
- int iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- if(iRet != 0)
- {
- /**
- 自动重新连接
- */
- int iErrno = mysql_errno(_pstMql);
- if (iErrno == 2013 || iErrno == 2006)
- {
- connect();
- iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- }
- }
- if (iRet != 0)
- {
- throw TC_Mysql_Exception("[TC_Mysql::execute]: mysql_query: [ " + sSql+" ] :" + string(mysql_error(_pstMql)));
- }
- }
- TC_Mysql::MysqlData TC_Mysql::queryRecord(const string& sSql)
- {
- MysqlData data;
- /**
- 没有连上, 连接数据库
- */
- if(!_bConnected)
- {
- connect();
- }
- _sLastSql = sSql;
- int iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- if(iRet != 0)
- {
- /**
- 自动重新连接
- */
- int iErrno = mysql_errno(_pstMql);
- if (iErrno == 2013 || iErrno == 2006)
- {
- connect();
- iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- }
- }
- if (iRet != 0)
- {
- throw TC_Mysql_Exception("[TC_Mysql::execute]: mysql_query: [ " + sSql+" ] :" + string(mysql_error(_pstMql)));
- }
-
- MYSQL_RES *pstRes = mysql_store_result(_pstMql);
- if(pstRes == NULL)
- {
- throw TC_Mysql_Exception("[TC_Mysql::queryRecord]: mysql_store_result: " + sSql + " : " + string(mysql_error(_pstMql)));
- }
-
- vector<string> vtFields;
- MYSQL_FIELD *field;
- while((field = mysql_fetch_field(pstRes)))
- {
- vtFields.push_back(field->name);
- }
- map<string, string> mpRow;
- MYSQL_ROW stRow;
-
- while((stRow = mysql_fetch_row(pstRes)) != (MYSQL_ROW)NULL)
- {
- mpRow.clear();
- unsigned long * lengths = mysql_fetch_lengths(pstRes);
- for(size_t i = 0; i < vtFields.size(); i++)
- {
- if(stRow[i])
- {
- mpRow[vtFields[i]] = string(stRow[i], lengths[i]);
- }
- else
- {
- mpRow[vtFields[i]] = "";
- }
- }
- data.data().push_back(mpRow);
- }
- mysql_free_result(pstRes);
- return data;
- }
- size_t TC_Mysql::travelRecord(const string& sSql, const std::function<void(const map<string, string> &)> & func)
- {
- size_t count = 0;
- /**
- 没有连上, 连接数据库
- */
- if (!_bConnected)
- {
- connect();
- }
- _sLastSql = sSql;
- int iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- if (iRet != 0)
- {
- /**
- 自动重新连接
- */
- int iErrno = mysql_errno(_pstMql);
- if (iErrno == 2013 || iErrno == 2006)
- {
- connect();
- iRet = mysql_real_query(_pstMql, sSql.c_str(), sSql.length());
- }
- }
- if (iRet != 0)
- {
- throw TC_Mysql_Exception("[TC_Mysql::execute]: mysql_query: [ " + sSql + " ] :" + string(mysql_error(_pstMql)));
- }
- MYSQL_RES *pstRes = mysql_store_result(_pstMql);
- if (pstRes == NULL)
- {
- throw TC_Mysql_Exception("[TC_Mysql::queryRecord]: mysql_store_result: " + sSql + " : " + string(mysql_error(_pstMql)));
- }
- vector<string> vtFields;
- MYSQL_FIELD *field;
- while ((field = mysql_fetch_field(pstRes)))
- {
- vtFields.push_back(field->name);
- }
- MYSQL_ROW stRow;
- while ((stRow = mysql_fetch_row(pstRes)) != (MYSQL_ROW)NULL)
- {
- map<string, string> mpRow;
- unsigned long * lengths = mysql_fetch_lengths(pstRes);
- for (size_t i = 0; i < vtFields.size(); i++)
- {
- if (stRow[i])
- {
- mpRow[vtFields[i]] = string(stRow[i], lengths[i]);
- }
- else
- {
- mpRow[vtFields[i]] = "";
- }
- }
- func(mpRow);
- count++;
- }
- mysql_free_result(pstRes);
- return count;
- }
- size_t TC_Mysql::updateRecord(const string &sTableName, const RECORD_DATA &mpColumns, const string &sCondition)
- {
- string sSql = buildUpdateSQL(sTableName, mpColumns, sCondition);
- execute(sSql);
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::insertRecord(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- string sSql = buildInsertSQL(sTableName, mpColumns);
- execute(sSql);
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::insertRecord(const string &sTableName, const map<string, pair<FT, vector<string> >> &mpColumns)
- {
- string sSql = buildInsertSQL(sTableName, mpColumns);
- execute(sSql);
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::replaceRecord(const string &sTableName, const RECORD_DATA &mpColumns)
- {
- string sSql = buildReplaceSQL(sTableName, mpColumns);
- execute(sSql);
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::replaceRecord(const string &sTableName, const map<string, pair<FT, vector<string>>> &mpColumns)
- {
- string sSql = buildReplaceSQL(sTableName, mpColumns);
- execute(sSql);
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::deleteRecord(const string &sTableName, const string &sCondition)
- {
- ostringstream sSql;
- sSql << "delete from " << sTableName << " " << sCondition;
- execute(sSql.str());
- return mysql_affected_rows(_pstMql);
- }
- size_t TC_Mysql::getRecordCount(const string& sTableName, const string &sCondition)
- {
- ostringstream sSql;
- sSql << "select count(*) as num from " << sTableName << " " << sCondition;
- MysqlData data = queryRecord(sSql.str());
- long n = atol(data[0]["num"].c_str());
- return n;
- }
- size_t TC_Mysql::getSqlCount(const string &sCondition)
- {
- ostringstream sSql;
- sSql << "select count(*) as num " << sCondition;
- MysqlData data = queryRecord(sSql.str());
- long n = atol(data[0]["num"].c_str());
- return n;
- }
- int TC_Mysql::getMaxValue(const string& sTableName, const string& sFieldName,const string &sCondition)
- {
- ostringstream sSql;
- sSql << "select " << sFieldName << " as f from " << sTableName << " " << sCondition << " order by f desc limit 1";
- MysqlData data = queryRecord(sSql.str());
- int n = 0;
-
- if(data.size() == 0)
- {
- n = 0;
- }
- else
- {
- n = atol(data[0]["f"].c_str());
- }
- return n;
- }
- bool TC_Mysql::existRecord(const string& sql)
- {
- return queryRecord(sql).size() > 0;
- }
- long TC_Mysql::lastInsertID()
- {
- return mysql_insert_id(_pstMql);
- }
- size_t TC_Mysql::getAffectedRows()
- {
- return mysql_affected_rows(_pstMql);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- TC_Mysql::MysqlRecord::MysqlRecord(const map<string, string> &record)
- : _record(record)
- {
- }
- const string& TC_Mysql::MysqlRecord::operator[](const string &s)
- {
- map<string, string>::const_iterator it = _record.find(s);
- if(it == _record.end())
- {
- throw TC_Mysql_Exception("field '" + s + "' not exists.");
- }
- return it->second;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- vector<map<string, string> >& TC_Mysql::MysqlData::data()
- {
- return _data;
- }
- size_t TC_Mysql::MysqlData::size()
- {
- return _data.size();
- }
- TC_Mysql::MysqlRecord TC_Mysql::MysqlData::operator[](size_t i)
- {
- return MysqlRecord(_data[i]);
- }
- }
- #endif
|