/** * @file mix_cc/sql/sqlapi_warpper.h * @brief 对SQL API++的包装,作为执行SQL语句的核心 * @author Cat (null.null.null@qq.com) * @version 0.1 * @date 2021-09-17 * * Copyright: Baosight Co. Ltd. * DO NOT COPY/USE WITHOUT PERMISSION * */ #pragma once #include #include #include #include #include #include #include "mix_cc/exception.h" #include "mix_cc/type/mix_time.h" #include #include namespace mix_cc { namespace sql { struct sqlapi_warpper { public: struct cmd_t { protected: std::unique_ptr cmd_ptr; public: cmd_t() { cmd_ptr = std::make_unique(); } bool connect(SAConnection* conn_ptr) { cmd_ptr->setConnection(conn_ptr); return true; } ~cmd_t() {} bool execute(std::string command) { try { cmd_ptr->setCommandText(_TSA(command.c_str())); cmd_ptr->Execute(); } catch (const SAException& e) { throw Exception(-1, e.ErrText().GetMultiByteChars(), BOOST_CURRENT_LOCATION, {"sql", command}); } return true; } auto is_result_set() { return cmd_ptr->isResultSet(); } auto rows_affacted() { return cmd_ptr->RowsAffected(); } auto cancel() { return cmd_ptr->Cancel(); } auto close() { return cmd_ptr->Close(); } auto destroy() { return cmd_ptr->Destroy(); } auto fetch_next() { return cmd_ptr->FetchNext(); } auto command_text() { return cmd_ptr->CommandText().GetMultiByteChars(); } template constexpr auto field(Col) { auto field_name = boost::to_upper_copy(std::string(Col::name_type::c_str())); if constexpr (boost::is_integral::value) { return cmd_ptr->Field(field_name.c_str()).asLong(); } else if constexpr (boost::is_float::value) { return cmd_ptr->Field(field_name.c_str()).asDouble(); } else if constexpr (std::is_same_v) { return cmd_ptr->Field(field_name.c_str()).asString(); } else if constexpr (std::is_same_v) { std::string tmp_time_str = cmd_ptr->Field(field_name.c_str()).asString().GetMultiByteChars(); if (!tmp_time_str.empty()) { tmp_time_str.replace(tmp_time_str.find("T"), 1, " "); } return mix_cc::mix_time_t{tmp_time_str}; } } }; public: sqlapi_warpper() : cmd_() {} ~sqlapi_warpper() {} public: cmd_t cmd_; }; } // namespace sql } // namespace mix_cc