From d92ce4cf9fd2a949cdb6179f7dcf2b8abbbab95b Mon Sep 17 00:00:00 2001 From: antao Date: Wed, 28 Nov 2018 10:48:03 +0800 Subject: [PATCH] Send query --- orm_lib/src/mysql_impl/MysqlConnection.cc | 84 +++++++++++++++++++++++ orm_lib/src/mysql_impl/MysqlConnection.h | 1 + orm_lib/src/mysql_impl/MysqlResultImpl.h | 48 +++++++++++++ 3 files changed, 133 insertions(+) diff --git a/orm_lib/src/mysql_impl/MysqlConnection.cc b/orm_lib/src/mysql_impl/MysqlConnection.cc index fe6cf691..2466dc3f 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.cc +++ b/orm_lib/src/mysql_impl/MysqlConnection.cc @@ -18,6 +18,18 @@ #include using namespace drogon::orm; +namespace drogon +{ +namespace orm +{ + +Result makeResult(const std::shared_ptr &r = std::shared_ptr(nullptr), const std::string &query = "") +{ + return Result(std::shared_ptr(new MysqlResultImpl(r, query))); +} + +} // namespace orm +} // namespace drogon MysqlConnection::MysqlConnection(trantor::EventLoop *loop, const std::string &connInfo) : DbConnection(loop) @@ -210,7 +222,60 @@ void MysqlConnection::handleEvent() setChannel(); break; } + case ExecStatus_SendQuery: + { + int err; + _waitStatus = mysql_send_query_cont(&err, _stmtPtr.get(), status); + if (_waitStatus == 0) + { + if (err) + { + _execStatus = ExecStatus_None; + outputError(); + //FIXME exception callback; + return; + } + _execStatus = ExecStatus_StoreResult; + MYSQL_RES *ret; + _waitStatus = mysql_store_result_start(&ret, MYSQL * mysql); + LOG_TRACE + << "send_query completely!"; + } + setChannel(); + break; + } + case ExecStatus_StoreResult: + { + MYSQL_RES *ret; + _waitStatus = mysql_store_result_cont(&ret, MYSQL * mysql); + if (_waitStatus == 0) + { + if(!ret) + { + _execStatus = ExecStatus_None; + outputError(); + //FIXME exception callback; + return; + } + //make result! + auto resultPtr = std::shared_ptr(ret, [](MYSQL_RES *r) { + mysql_free_result(r); + }); + auto Result = makeResult(r, _sql); + if(_isWorking) + { + _cb(Result); + _cb = decltype(_cb)(); + _exceptCb = decltype(_exceptCallback)(); + _isWorking = false; + _idleCb(); + _idleCb = decltype(_idleCb)(); + } + } + setChannel(); + break; + } default: return; } @@ -240,7 +305,26 @@ void MysqlConnection::execSql(const std::string &sql, _exceptCb = exceptCallback; //_channel.enableWriting(); LOG_TRACE << sql; + if (paraNum == 0) + { + _loop->runInLoop([=]() { + int err; + //int mysql_send_query_start(int *ret, MYSQL *mysql, const char *q, unsigned long length) + _waitStatus = mysql_send_query_start(&err, _stmtPtr.get(), sql.c_str(), sql.length()); + if (_waitStatus == 0) + { + if (err) + { + outputError(); + return; + } + } + _execStatus = ExecStatus_SendQuery; + setChannel(); + }); + return; + } _stmtPtr = std::shared_ptr(mysql_stmt_init(_mysqlPtr.get()), [](MYSQL_STMT *stmt) { //blocking method; mysql_stmt_close(stmt); diff --git a/orm_lib/src/mysql_impl/MysqlConnection.h b/orm_lib/src/mysql_impl/MysqlConnection.h index 16e38e4b..c38f4dae 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.h +++ b/orm_lib/src/mysql_impl/MysqlConnection.h @@ -58,6 +58,7 @@ class MysqlConnection : public DbConnection, public std::enable_shared_from_this enum ExecStatus { ExecStatus_None = 0, + ExecStatus_SendQuery, ExecStatus_StmtPrepare }; ExecStatus _execStatus = ExecStatus_None; diff --git a/orm_lib/src/mysql_impl/MysqlResultImpl.h b/orm_lib/src/mysql_impl/MysqlResultImpl.h index e69de29b..f53bcfaa 100644 --- a/orm_lib/src/mysql_impl/MysqlResultImpl.h +++ b/orm_lib/src/mysql_impl/MysqlResultImpl.h @@ -0,0 +1,48 @@ +/** + * + * MysqlResultImpl.h + * An Tao + * + * Copyright 2018, An Tao. All rights reserved. + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * + */ +#pragma once + +#include "../ResultImpl.h" + +#include +#include +#include + +namespace drogon +{ +namespace orm +{ + +class MysqlResultImpl : public ResultImpl +{ + public: + MysqlResultImpl(const std::shared_ptr &r, const std::string &query) noexcept + : _result(r), + _query(query) + { + } + virtual size_type size() const noexcept override; + virtual row_size_type columns() const noexcept override; + virtual const char *columnName(row_size_type Number) const override; + virtual size_type affectedRows() const noexcept override; + virtual row_size_type columnNumber(const char colName[]) const override; + virtual const char *getValue(size_type row, row_size_type column) const override; + virtual bool isNull(size_type row, row_size_type column) const override; + virtual field_size_type getLength(size_type row, row_size_type column) const override; + + private: + std::shared_ptr _result; + std::string _query; +}; + +} // namespace orm +} // namespace drogon