99 lines
2.7 KiB
C
99 lines
2.7 KiB
C
|
|
/**
|
|||
|
|
* @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 <SQLAPI.h>
|
|||
|
|
#include <db2_linux/sql.h>
|
|||
|
|
#include <db2_linux/sqlsystm.h>
|
|||
|
|
#include <boost/type_traits.hpp>
|
|||
|
|
#include <boost/type_index.hpp>
|
|||
|
|
#include <boost/algorithm/string.hpp>
|
|||
|
|
#include "mix_cc/exception.h"
|
|||
|
|
#include "mix_cc/type/mix_time.h"
|
|||
|
|
#include <memory>
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
namespace mix_cc {
|
|||
|
|
namespace sql {
|
|||
|
|
struct sqlapi_warpper {
|
|||
|
|
public:
|
|||
|
|
struct cmd_t {
|
|||
|
|
protected:
|
|||
|
|
std::unique_ptr<SACommand> cmd_ptr;
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
cmd_t() { cmd_ptr = std::make_unique<SACommand>(); }
|
|||
|
|
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 <typename Col>
|
|||
|
|
constexpr auto field(Col) {
|
|||
|
|
auto field_name =
|
|||
|
|
boost::to_upper_copy(std::string(Col::name_type::c_str()));
|
|||
|
|
if constexpr (boost::is_integral<typename Col::value_type>::value) {
|
|||
|
|
return cmd_ptr->Field(field_name.c_str()).asLong();
|
|||
|
|
} else if constexpr (boost::is_float<typename Col::value_type>::value) {
|
|||
|
|
return cmd_ptr->Field(field_name.c_str()).asDouble();
|
|||
|
|
} else if constexpr (std::is_same_v<typename Col::value_type,
|
|||
|
|
std::string>) {
|
|||
|
|
return cmd_ptr->Field(field_name.c_str()).asString();
|
|||
|
|
} else if constexpr (std::is_same_v<typename Col::value_type,
|
|||
|
|
mix_cc::mix_time_t>) {
|
|||
|
|
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
|