模块外置
This commit is contained in:
@@ -15,11 +15,6 @@
|
||||
#include "module/http/session.h"
|
||||
#include "module/http/httpclient.h"
|
||||
#include "module/http/interceptor.h"
|
||||
#include "module/mysql.h"
|
||||
#ifdef _WIN32
|
||||
#include "module/mssql.h"
|
||||
#endif
|
||||
#include "module/localstorage.h"
|
||||
#include "module/globalfuns.h"
|
||||
#include "module/mutex.h"
|
||||
#include "module/codec.h"
|
||||
@@ -44,6 +39,7 @@ void fastweb::module_manager::start()
|
||||
module_info mi;
|
||||
std::string mod_filepath = app()->config->scripts.module_dir + "/" + ms[i];
|
||||
#ifdef _WIN32
|
||||
SetDllDirectoryA(ylib::file::parent_dir(mod_filepath).c_str());
|
||||
mi.dll = LoadLibrary(mod_filepath.c_str());
|
||||
if (mi.dll == nullptr)
|
||||
{
|
||||
@@ -123,12 +119,7 @@ void fastweb::module_manager::load_core(sol::state* lua)
|
||||
module::interceptor::regist(lua);
|
||||
module::session::regist(lua);
|
||||
module::httpclient::regist(lua);
|
||||
module::mysql_regist(lua);
|
||||
#ifdef _WIN32
|
||||
module::mssql::regist(lua);
|
||||
#endif
|
||||
module::globalfuncs::regist(lua);
|
||||
module::local_storage::regist(lua);
|
||||
module::mutex::regist(lua);
|
||||
module::auto_lock::regist(lua);
|
||||
module::codec::regist(lua);
|
||||
@@ -173,6 +164,13 @@ std::vector<std::string> fastweb::module_manager::modules()
|
||||
if (iter->second == IS_DIRECTORY)
|
||||
continue;
|
||||
std::string path = strutils::replace(iter->first, '\\', '/');
|
||||
if (path.find("/") != -1)
|
||||
{
|
||||
if (strutils::split(path, '/').size() != 2)
|
||||
continue; //多级不支持
|
||||
if (ylib::file::parent_dir(path) != ylib::file::filename(path, false))
|
||||
continue;
|
||||
}
|
||||
results.push_back(path);
|
||||
}
|
||||
return results;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include "sol/sol.hpp"
|
||||
#include "core/define.h"
|
||||
|
||||
namespace module
|
||||
{
|
||||
|
||||
@@ -12,22 +12,77 @@ namespace module
|
||||
public:
|
||||
request(network::http::request* request);
|
||||
~request();
|
||||
|
||||
/// <summary>
|
||||
/// 取协议头
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
std::string header(const std::string& name);
|
||||
/// <summary>
|
||||
/// 取请求类型
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
network::http::method method();
|
||||
/// <summary>
|
||||
/// 取请求路径
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
std::string filepath();
|
||||
/// <summary>
|
||||
/// 取Host主机
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
std::string host();
|
||||
/// <summary>
|
||||
/// 取请求参数
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="throw_"></param>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
sol::object param(const std::string& name,bool throw_,sol::this_state s);
|
||||
/// <summary>
|
||||
/// 取远程地址
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
std::string remote_ipaddress();
|
||||
/// <summary>
|
||||
/// 取远程端口
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
ushort remote_port();
|
||||
|
||||
/// <summary>
|
||||
/// 取token
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
std::string token();
|
||||
/// <summary>
|
||||
/// 取session
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
module::session* session(const std::string& token);
|
||||
|
||||
/// <summary>
|
||||
/// 取body参数
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
sol::table body_param(sol::this_state s);
|
||||
/// <summary>
|
||||
/// 取URL参数
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
sol::table url_param(sol::this_state s);
|
||||
|
||||
/// <summary>
|
||||
/// 取请求体
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
std::string body();
|
||||
/// <summary>
|
||||
/// 取网站指针
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
void* website();
|
||||
|
||||
static void regist(sol::state* lua);
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
#include "localstorage.h"
|
||||
#include "util/localstorage.h"
|
||||
module::local_storage::local_storage()
|
||||
{
|
||||
}
|
||||
|
||||
module::local_storage::~local_storage()
|
||||
{
|
||||
::ylib::local_storage::close();
|
||||
}
|
||||
|
||||
sol::optional<std::string> module::local_storage::readex(const std::string& name)
|
||||
{
|
||||
std::string value;
|
||||
if (this->read(name, value) == false)
|
||||
return sol::nullopt;
|
||||
return value;
|
||||
}
|
||||
|
||||
void module::local_storage::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::local_storage>("local_storage",
|
||||
"new", sol::constructors<module::local_storage()>(),
|
||||
"clear", &module::local_storage::clear,
|
||||
"close", &module::local_storage::close,
|
||||
"del", &module::local_storage::del,
|
||||
"exist", &module::local_storage::exist,
|
||||
"open", &module::local_storage::open,
|
||||
"read", &module::local_storage::readex,
|
||||
"write", &module::local_storage::write,
|
||||
"self", &module::local_storage::self,
|
||||
"last_error", &module::local_storage::last_error
|
||||
);
|
||||
}
|
||||
|
||||
void module::local_storage::regist_global(const std::string& name, sol::state* lua)
|
||||
{
|
||||
lua->registry()[name] = this;
|
||||
(*lua)[name] = this;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
#include "util/localstorage.h"
|
||||
#include "basemodule.h"
|
||||
namespace module
|
||||
{
|
||||
/// <summary>
|
||||
/// 本地缓存(leveldb)
|
||||
/// </summary>
|
||||
class local_storage : public ylib::local_storage,public module::base {
|
||||
public:
|
||||
local_storage();
|
||||
~local_storage() override;
|
||||
/// <summary>
|
||||
/// 取数据
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
sol::optional<std::string> readex(const std::string& name);
|
||||
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
// 通过 imodule 继承
|
||||
virtual void regist_global(const std::string& name, sol::state* lua);
|
||||
virtual void delete_global() { delete this; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
#ifdef _WIN32
|
||||
#include "mssql.h"
|
||||
#include "soci/odbc/soci-odbc.h"
|
||||
#include "core/define.h"
|
||||
module::mssql::mssql(const std::string& connstring)
|
||||
{
|
||||
m_session = std::make_shared<soci::session>(soci::odbc, connstring);
|
||||
}
|
||||
|
||||
module::mssql::~mssql()
|
||||
{
|
||||
|
||||
}
|
||||
std::string module::mssql::get_str(const std::string& name)
|
||||
{
|
||||
if (m_iter == m_rows.end())
|
||||
throw ylib::exception("no recorded data");
|
||||
std::string result;
|
||||
result = m_iter->get<std::string>(name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string module::mssql::get_datetime(const std::string& name)
|
||||
{
|
||||
if (m_iter == m_rows.end())
|
||||
throw ylib::exception("no recorded data");
|
||||
std::tm result;
|
||||
memset(&result, 0, sizeof(result));
|
||||
result = m_iter->get<std::tm>(name, result);
|
||||
if (result.tm_year != 0)
|
||||
{
|
||||
char buffer[80];
|
||||
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &result);
|
||||
std::string dateTimeStr(buffer);
|
||||
return dateTimeStr;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
int32 module::mssql::get_i32(const std::string& name)
|
||||
{
|
||||
if (m_iter == m_rows.end())
|
||||
throw ylib::exception("no recorded data");
|
||||
int32 result = 0;
|
||||
result = m_iter->get<int32>(name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
int64 module::mssql::get_i64(const std::string& name)
|
||||
{
|
||||
if (m_iter == m_rows.end())
|
||||
throw ylib::exception("no recorded data");
|
||||
int64 result = 0;
|
||||
result = m_iter->get<int64>(name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
double module::mssql::get_dob(const std::string& name)
|
||||
{
|
||||
if (m_iter == m_rows.end())
|
||||
throw ylib::exception("no recorded data");
|
||||
double result = 0.0f;
|
||||
result = m_iter->get<double>(name, result);
|
||||
return result;
|
||||
}
|
||||
void module::mssql::query(const std::string& sql)
|
||||
{
|
||||
//std::cout << "[mssql][query]: " << sql << std::endl;
|
||||
m_read = false;
|
||||
m_rows = (m_session->prepare << sql);
|
||||
//m_iter = m_rows.begin();
|
||||
}
|
||||
int64 module::mssql::update(const std::string& sql)
|
||||
{
|
||||
//std::cout << "[mssql][update]: " << sql << std::endl;
|
||||
soci::statement st = m_session->prepare << sql;
|
||||
st.execute();
|
||||
return st.get_affected_rows();
|
||||
}
|
||||
bool module::mssql::next()
|
||||
{
|
||||
if (m_read == false)
|
||||
{
|
||||
m_iter = m_rows.begin();
|
||||
|
||||
return m_iter != m_rows.end();
|
||||
}
|
||||
if (m_iter == m_rows.end())
|
||||
return false;
|
||||
m_iter++;
|
||||
return m_iter != m_rows.end();
|
||||
}
|
||||
|
||||
|
||||
void module::mssql::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::mssql>("mssql",
|
||||
"new", sol::constructors<module::mssql(const std::string&)>(),
|
||||
"get_dob", &module::mssql::get_dob,
|
||||
"get_i32", &module::mssql::get_i32,
|
||||
"get_i64", &module::mssql::get_i64,
|
||||
"get_str", &module::mssql::get_str,
|
||||
"next", &module::mssql::next,
|
||||
"query", &module::mssql::query,
|
||||
"update", &module::mssql::update,
|
||||
"get_datetime", &module::mssql::get_datetime
|
||||
);
|
||||
}
|
||||
#endif
|
||||
@@ -1,49 +0,0 @@
|
||||
#pragma once
|
||||
#ifdef _WIN32
|
||||
#include "sol/sol.hpp"
|
||||
#include "soci/soci.h"
|
||||
#include "core/define.h"
|
||||
namespace module
|
||||
{
|
||||
/// <summary>
|
||||
/// 结果集
|
||||
/// </summary>
|
||||
class mssql
|
||||
{
|
||||
public:
|
||||
mssql(const std::string& connstring);
|
||||
~mssql();
|
||||
|
||||
/// <summary>
|
||||
/// 取数据
|
||||
/// </summary>
|
||||
std::string get_str(const std::string& name);
|
||||
std::string get_datetime(const std::string& name);
|
||||
int32 get_i32(const std::string& name);
|
||||
int64 get_i64(const std::string& name);
|
||||
double get_dob(const std::string& name);
|
||||
void query(const std::string& sql);
|
||||
/// <summary>
|
||||
/// 下一行
|
||||
/// </summary>
|
||||
bool next();
|
||||
|
||||
|
||||
int64 update(const std::string& sql);
|
||||
/// <summary>
|
||||
/// 注册
|
||||
/// </summary>
|
||||
/// <param name="lua"></param>
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
std::shared_ptr<soci::session> m_session;
|
||||
//std::shared_ptr<soci::statement> m_st;
|
||||
soci::rowset_iterator<soci::row> m_iter;
|
||||
soci::rowset<soci::row> m_rows;
|
||||
bool m_read = false;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include "sol/sol.hpp"
|
||||
#include "module/basemodule.h"
|
||||
|
||||
#include <mutex>
|
||||
namespace module
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -1,604 +0,0 @@
|
||||
#include "mysql.h"
|
||||
#include "util/time.h"
|
||||
module::select::select(ylib::mysql::conn *conn)
|
||||
{
|
||||
m_select = std::make_shared<ylib::select>(conn);
|
||||
}
|
||||
|
||||
module::select::~select()
|
||||
{
|
||||
}
|
||||
|
||||
module::select& module::select::where_i32(const std::string& name, const std::string& expression, int32 value)
|
||||
{
|
||||
m_select->where(name,expression,value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::where_i64(const std::string& name, const std::string& expression, int64 value)
|
||||
{
|
||||
m_select->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::where_dob(const std::string& name, const std::string& expression, double value)
|
||||
{
|
||||
m_select->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::where_str(const std::string& name, const std::string& expression, const std::string& value)
|
||||
{
|
||||
m_select->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::where_expression(const std::string& expression)
|
||||
{
|
||||
m_select->where(expression);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::where_like(const std::string& name, const std::string& value)
|
||||
{
|
||||
m_select->where_like(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::table(const std::string& table_name)
|
||||
{
|
||||
m_select->table(table_name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::field(sol::table table)
|
||||
{
|
||||
std::vector<std::string> strings;
|
||||
for (auto& pair : table) {
|
||||
if (pair.second.get_type() != sol::type::string)
|
||||
{
|
||||
throw ylib::exception("module::select::field: attempting to obtain field name that is not a string");
|
||||
}
|
||||
strings.push_back(pair.second.as<std::string>());
|
||||
}
|
||||
m_select->field(strings);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::page(uint32 page, uint32 count)
|
||||
{
|
||||
m_select->page(page,count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::limit(uint32 start, uint32 count)
|
||||
{
|
||||
m_select->limit(start,count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::select& module::select::orderby(const std::string& field, int sort)
|
||||
{
|
||||
m_select->orderby(field,(ylib::sort)sort);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void module::select::clear()
|
||||
{
|
||||
m_select->clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<module::mysql_result> module::select::query()
|
||||
{
|
||||
return std::make_shared<module::mysql_result>(m_select->query());
|
||||
}
|
||||
|
||||
uint64 module::select::count()
|
||||
{
|
||||
return m_select->count();
|
||||
}
|
||||
|
||||
void module::select::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::select>("mysql_builder_select",
|
||||
"new", sol::constructors<module::select(ylib::mysql::conn*)>(),
|
||||
"count", &module::select::count,
|
||||
"field", &module::select::field,
|
||||
"limit", &module::select::limit,
|
||||
"orderby", &module::select::orderby,
|
||||
"page", &module::select::page,
|
||||
"table", &module::select::table,
|
||||
"where_dob", &module::select::where_dob,
|
||||
"where_expression", &module::select::where_expression,
|
||||
"where_i32", &module::select::where_i32,
|
||||
"where_i64", &module::select::where_i64,
|
||||
"where_str", &module::select::where_str,
|
||||
"query", &module::select::query,
|
||||
"clear", &module::select::clear,
|
||||
"where_like", &module::select::where_like,
|
||||
"clear", &module::select::clear
|
||||
);
|
||||
}
|
||||
|
||||
module::update::update(ylib::mysql::conn* conn)
|
||||
{
|
||||
m_update = std::make_shared<ylib::update>(conn);
|
||||
}
|
||||
|
||||
module::update::~update()
|
||||
{
|
||||
}
|
||||
|
||||
module::update& module::update::table(const std::string& table_name)
|
||||
{
|
||||
m_update->table(table_name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::set_i32(const std::string& name, int32 value)
|
||||
{
|
||||
m_update->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::set_i64(const std::string& name, int64 value)
|
||||
{
|
||||
m_update->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::set_dob(const std::string& name, double value)
|
||||
{
|
||||
m_update->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::set_str(const std::string& name, const std::string& value)
|
||||
{
|
||||
m_update->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::set(const std::string& expression)
|
||||
{
|
||||
m_update->set(expression);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::where_i32(const std::string& name, const std::string& expression, int32 value)
|
||||
{
|
||||
m_update->where(name,expression,value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::where_i64(const std::string& name, const std::string& expression, int64 value)
|
||||
{
|
||||
m_update->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::where_dob(const std::string& name, const std::string& expression, double value)
|
||||
{
|
||||
m_update->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::where_str(const std::string& name, const std::string& expression, const std::string& value)
|
||||
{
|
||||
m_update->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::where_expression(const std::string& expression)
|
||||
{
|
||||
m_update->where(expression);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::page(uint32 page, uint32 count)
|
||||
{
|
||||
m_update->page(page,count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::limit(uint32 start, uint32 count)
|
||||
{
|
||||
m_update->limit(start, count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::update& module::update::orderby(const std::string& field, int sort)
|
||||
{
|
||||
m_update->orderby(field,(ylib::sort)sort);
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint64 module::update::exec()
|
||||
{
|
||||
return m_update->exec();
|
||||
}
|
||||
|
||||
void module::update::clear()
|
||||
{
|
||||
m_update->clear();
|
||||
}
|
||||
|
||||
void module::update::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::update>("mysql_builder_update",
|
||||
"new", sol::constructors<module::update(ylib::mysql::conn*)>(),
|
||||
"exec", &module::update::exec,
|
||||
"limit", &module::update::limit,
|
||||
"orderby", &module::update::orderby,
|
||||
"page", &module::update::page,
|
||||
"table", &module::update::table,
|
||||
"where_dob", &module::update::where_dob,
|
||||
"where_expression", &module::update::where_expression,
|
||||
"where_i32", &module::update::where_i32,
|
||||
"where_i64", &module::update::where_i64,
|
||||
"where_str", &module::update::where_str,
|
||||
"set", &module::update::set,
|
||||
"set_dob", &module::update::set_dob,
|
||||
"set_i32", &module::update::set_i32,
|
||||
"set_i64", &module::update::set_i64,
|
||||
"set_str", &module::update::set_str,
|
||||
"clear", &module::update::clear
|
||||
);
|
||||
}
|
||||
|
||||
module::insert::insert(ylib::mysql::conn* conn)
|
||||
{
|
||||
m_insert = std::make_shared<ylib::insert>(conn);
|
||||
}
|
||||
|
||||
module::insert::~insert()
|
||||
{
|
||||
}
|
||||
|
||||
module::insert& module::insert::table(const std::string& table_name)
|
||||
{
|
||||
m_insert->table(table_name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::insert& module::insert::set_i32(const std::string& name, int32 value)
|
||||
{
|
||||
m_insert->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::insert& module::insert::set_i64(const std::string& name, int64 value)
|
||||
{
|
||||
m_insert->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::insert& module::insert::set_dob(const std::string& name, double value)
|
||||
{
|
||||
m_insert->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::insert& module::insert::set_str(const std::string& name, const std::string& value)
|
||||
{
|
||||
m_insert->set(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::insert& module::insert::set_not_ppst(const std::string& name, const std::string& value)
|
||||
{
|
||||
m_insert->set_not_pret(name, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint64 module::insert::exec()
|
||||
{
|
||||
return m_insert->exec();
|
||||
}
|
||||
|
||||
void module::insert::clear()
|
||||
{
|
||||
m_insert->clear();
|
||||
}
|
||||
|
||||
void module::insert::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::insert>("mysql_builder_insert",
|
||||
"new", sol::constructors<module::insert(ylib::mysql::conn*)>(),
|
||||
"exec", &module::insert::exec,
|
||||
"table", &module::insert::table,
|
||||
"set_not_ppst", &module::insert::set_not_ppst,
|
||||
"set_dob", &module::insert::set_dob,
|
||||
"set_i32", &module::insert::set_i32,
|
||||
"set_i64", &module::insert::set_i64,
|
||||
"set_str", &module::insert::set_str,
|
||||
"clear", &module::insert::clear
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module::delete_::delete_(ylib::mysql::conn* conn)
|
||||
{
|
||||
m_delete = std::make_shared<ylib::delete_>(conn);
|
||||
}
|
||||
|
||||
module::delete_::~delete_()
|
||||
{
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::table(const std::string& table_name)
|
||||
{
|
||||
m_delete->table(table_name);
|
||||
return *this;
|
||||
}
|
||||
module::delete_& module::delete_::where_i32(const std::string& name, const std::string& expression, int32 value)
|
||||
{
|
||||
m_delete->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::where_i64(const std::string& name, const std::string& expression, int64 value)
|
||||
{
|
||||
m_delete->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::where_dob(const std::string& name, const std::string& expression, double value)
|
||||
{
|
||||
m_delete->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::where_str(const std::string& name, const std::string& expression, const std::string& value)
|
||||
{
|
||||
m_delete->where(name, expression, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::where_expression(const std::string& expression)
|
||||
{
|
||||
m_delete->where(expression);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::page(uint32 page, uint32 count)
|
||||
{
|
||||
m_delete->page(page, count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::limit(uint32 start, uint32 count)
|
||||
{
|
||||
m_delete->limit(start, count);
|
||||
return *this;
|
||||
}
|
||||
|
||||
module::delete_& module::delete_::orderby(const std::string& field, int sort)
|
||||
{
|
||||
m_delete->orderby(field, (ylib::sort)sort);
|
||||
return *this;
|
||||
}
|
||||
|
||||
uint64 module::delete_::exec()
|
||||
{
|
||||
return m_delete->exec();
|
||||
}
|
||||
|
||||
void module::delete_::clear()
|
||||
{
|
||||
m_delete->clear();
|
||||
}
|
||||
|
||||
void module::delete_::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::delete_>("mysql_builder_delete",
|
||||
"new", sol::constructors<module::delete_(ylib::mysql::conn*)>(),
|
||||
"exec", &module::delete_::exec,
|
||||
"limit", &module::delete_::limit,
|
||||
"orderby", &module::delete_::orderby,
|
||||
"page", &module::delete_::page,
|
||||
"table", &module::delete_::table,
|
||||
"where_dob", &module::delete_::where_dob,
|
||||
"where_expression", &module::delete_::where_expression,
|
||||
"where_i32", &module::delete_::where_i32,
|
||||
"where_i64", &module::delete_::where_i64,
|
||||
"where_str", &module::delete_::where_str,
|
||||
"clear", &module::delete_::clear
|
||||
);
|
||||
}
|
||||
|
||||
void module::mysql_regist(sol::state* lua)
|
||||
{
|
||||
(*lua)["DESC"] = ylib::sort::DESC;
|
||||
(*lua)["ASC"] = ylib::sort::ASC;
|
||||
|
||||
lua->new_usertype<ylib::mysql::conn>("mysql_conn",
|
||||
"clear", &ylib::mysql::conn::clear,
|
||||
"close", &ylib::mysql::conn::close,
|
||||
"commit", &ylib::mysql::conn::commit,
|
||||
"insert_id", &ylib::mysql::conn::insert_id,
|
||||
"last_error", &ylib::mysql::conn::last_error,
|
||||
"recover", &ylib::mysql::conn::recover,
|
||||
"rollback", &ylib::mysql::conn::rollback,
|
||||
"setsql", &ylib::mysql::conn::setsql
|
||||
);
|
||||
lua->new_usertype<module::mysql>("mysql_pool",
|
||||
"new", sol::constructors<module::mysql()>(),
|
||||
"start", &module::mysql::start,
|
||||
"close", &module::mysql::close,
|
||||
"self", &module::mysql::self,
|
||||
"select", &module::mysql::select,
|
||||
"update", &module::mysql::update,
|
||||
"insert", &module::mysql::insert,
|
||||
"delete", &module::mysql::delete_
|
||||
);
|
||||
module::mysql_result::regist(lua);
|
||||
|
||||
module::select::regist(lua);
|
||||
module::update::regist(lua);
|
||||
module::delete_::regist(lua);
|
||||
module::insert::regist(lua);
|
||||
|
||||
}
|
||||
|
||||
module::mysql::mysql()
|
||||
{
|
||||
}
|
||||
|
||||
module::mysql::~mysql()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
bool module::mysql::start(const std::string& ipaddress, const std::string& username, const std::string& password, const std::string& database, const std::string& charset, ushort port, int32 size)
|
||||
{
|
||||
close();
|
||||
m_pool = std::make_shared<ylib::mysql::pool>();
|
||||
ylib::mysql::mysql_conn_info info;
|
||||
info.ipaddress = ipaddress;
|
||||
info.username = username;
|
||||
info.password = password;
|
||||
info.charset = charset;
|
||||
info.port = port;
|
||||
info.database = database;
|
||||
return m_pool->start(info, size);
|
||||
}
|
||||
|
||||
void module::mysql::close()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<module::select> module::mysql::select()
|
||||
{
|
||||
return std::make_shared<module::select>(m_pool->get());
|
||||
}
|
||||
|
||||
std::shared_ptr<module::insert> module::mysql::insert()
|
||||
{
|
||||
return std::make_shared<module::insert>(m_pool->get());
|
||||
}
|
||||
|
||||
std::shared_ptr<module::update> module::mysql::update()
|
||||
{
|
||||
return std::make_shared<module::update>(m_pool->get());
|
||||
}
|
||||
|
||||
std::shared_ptr<module::delete_> module::mysql::delete_()
|
||||
{
|
||||
return std::make_shared<module::delete_>(m_pool->get());
|
||||
}
|
||||
|
||||
void module::mysql::regist_global(const std::string& name, sol::state* lua)
|
||||
{
|
||||
lua->registry()[name] = this;
|
||||
(*lua)[name] = this;
|
||||
}
|
||||
module::mysql_result::mysql_result(ylib::mysql::result* result):m_result(result)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
module::mysql_result::~mysql_result()
|
||||
{
|
||||
|
||||
}
|
||||
uint32 module::mysql_result::field_count()
|
||||
{
|
||||
return m_result->field_count();
|
||||
}
|
||||
|
||||
std::string module::mysql_result::field_type(sol::object obj)
|
||||
{
|
||||
if (obj.is<int>() || obj.is<double>()) {
|
||||
m_result->field_type((uint32)obj.as<int>());
|
||||
}
|
||||
else if (obj.is<std::string>()) {
|
||||
m_result->field_type((std::string)obj.as<std::string>());
|
||||
}
|
||||
return "";
|
||||
}
|
||||
std::string module::mysql_result::field_name(uint32 index)
|
||||
{
|
||||
return m_result->field_name(index);
|
||||
}
|
||||
|
||||
size_t module::mysql_result::row_count()
|
||||
{
|
||||
return m_result->row_count();
|
||||
}
|
||||
|
||||
bool module::mysql_result::next()
|
||||
{
|
||||
return m_result->next();
|
||||
}
|
||||
|
||||
sol::object module::mysql_result::get(sol::object obj, sol::this_state s)
|
||||
{
|
||||
std::string type;
|
||||
if (obj.get_type() == sol::type::string)
|
||||
type = m_result->field_type(obj.as<std::string>());
|
||||
else if (obj.get_type() == sol::type::number)
|
||||
type = m_result->field_type(obj.as<int>());
|
||||
|
||||
#define GET_VALUE(FUNCTION) \
|
||||
if (obj.get_type() == sol::type::string) \
|
||||
return sol::make_object(s, m_result->FUNCTION(obj.as<std::string>())); \
|
||||
else if (obj.get_type() == sol::type::number) \
|
||||
return sol::make_object(s, m_result->FUNCTION(obj.as<uint32>()))
|
||||
|
||||
if (type == "int" || type == "tinyint")
|
||||
{
|
||||
GET_VALUE(get_int32);
|
||||
}
|
||||
|
||||
else if (type == "varchar" || type == "char" || type == "text" || type == "datetime")
|
||||
{
|
||||
GET_VALUE(get_string);
|
||||
}
|
||||
else if (type == "int unsigned")
|
||||
{
|
||||
GET_VALUE(get_uint32);
|
||||
}
|
||||
else if (type == "bigint")
|
||||
{
|
||||
GET_VALUE(get_int64);
|
||||
}
|
||||
else if (type == "decimal")
|
||||
{
|
||||
GET_VALUE(get_double);
|
||||
}
|
||||
return sol::make_object(s, sol::nil);
|
||||
|
||||
}
|
||||
|
||||
sol::table module::mysql_result::table(sol::this_state s)
|
||||
{
|
||||
sol::state_view lua(s);
|
||||
sol::table result_table = lua.create_table();
|
||||
int row_num = 1;
|
||||
|
||||
while (next()) {
|
||||
sol::table row = lua.create_table();
|
||||
for (uint32_t i = 0; i < field_count(); ++i) {
|
||||
row[field_name(i+1)] = get(sol::make_object(s, i+1), s);
|
||||
}
|
||||
result_table[row_num++] = row;
|
||||
}
|
||||
return result_table;
|
||||
}
|
||||
|
||||
void module::mysql_result::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::mysql_result>("mysql_result",
|
||||
"field_name", &module::mysql_result::field_name,
|
||||
"get", &module::mysql_result::get,
|
||||
"next", &module::mysql_result::next,
|
||||
"row_count", &module::mysql_result::row_count,
|
||||
"table", &module::mysql_result::table,
|
||||
"field_count", &module::mysql_result::field_count,
|
||||
"field_type", &module::mysql_result::field_type
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,188 +0,0 @@
|
||||
#pragma once
|
||||
#include "db/mysql.h"
|
||||
#include "db/sqler.h"
|
||||
#include "sol/sol.hpp"
|
||||
#include "module/basemodule.h"
|
||||
namespace module
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册
|
||||
/// </summary>
|
||||
/// <param name="lua"></param>
|
||||
void mysql_regist(sol::state* lua);
|
||||
/// <summary>
|
||||
/// 结果集
|
||||
/// </summary>
|
||||
class mysql_result
|
||||
{
|
||||
public:
|
||||
mysql_result(ylib::mysql::result* result);
|
||||
~mysql_result();
|
||||
/// <summary>
|
||||
/// 字段数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
uint32 field_count();
|
||||
/// <summary>
|
||||
/// 字段类型
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
std::string field_type(sol::object obj);
|
||||
/// <summary>
|
||||
/// 字段名称
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
std::string field_name(uint32 index);
|
||||
/// <summary>
|
||||
/// 结果行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
size_t row_count();
|
||||
/// <summary>
|
||||
/// 下一行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
bool next();
|
||||
/// <summary>
|
||||
/// 取数据
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
sol::object get(sol::object obj, sol::this_state s);
|
||||
/// <summary>
|
||||
/// 取结果集到table
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
sol::table table(sol::this_state s);
|
||||
/// <summary>
|
||||
/// 注册
|
||||
/// </summary>
|
||||
/// <param name="lua"></param>
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
ylib::mysql::result* m_result = nullptr;
|
||||
};
|
||||
|
||||
|
||||
class select {
|
||||
public:
|
||||
select(ylib::mysql::conn* conn);
|
||||
~select();
|
||||
module::select& where_i32(const std::string& name, const std::string& expression, int32 value);
|
||||
module::select& where_i64(const std::string& name, const std::string& expression, int64 value);
|
||||
module::select& where_dob(const std::string& name, const std::string& expression, double value);
|
||||
module::select& where_str(const std::string& name, const std::string& expression, const std::string& value);
|
||||
module::select& where_expression(const std::string& expression);
|
||||
module::select& where_like(const std::string& name,const std::string& value);
|
||||
module::select& table(const std::string& table_name);
|
||||
module::select& field(sol::table table);
|
||||
module::select& page(uint32 page, uint32 count);
|
||||
module::select& limit(uint32 start, uint32 count);
|
||||
module::select& orderby(const std::string& field, int sort);
|
||||
void clear();
|
||||
std::shared_ptr<module::mysql_result> query();
|
||||
uint64 count();
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
std::shared_ptr<ylib::select> m_select;
|
||||
};
|
||||
class update {
|
||||
public:
|
||||
update(ylib::mysql::conn* conn);
|
||||
~update();
|
||||
module::update& table(const std::string& table_name);
|
||||
module::update& set_i32(const std::string& name, int32 value);
|
||||
module::update& set_i64(const std::string& name, int64 value);
|
||||
module::update& set_dob(const std::string& name, double value);
|
||||
module::update& set_str(const std::string& name, const std::string& value);
|
||||
module::update& set(const std::string& expression);
|
||||
module::update& where_i32(const std::string& name, const std::string& expression, int32 value);
|
||||
module::update& where_i64(const std::string& name, const std::string& expression, int64 value);
|
||||
module::update& where_dob(const std::string& name, const std::string& expression, double value);
|
||||
module::update& where_str(const std::string& name, const std::string& expression, const std::string& value);
|
||||
module::update& where_expression(const std::string& expression);
|
||||
module::update& page(uint32 page, uint32 count);
|
||||
module::update& limit(uint32 start, uint32 count);
|
||||
module::update& orderby(const std::string& field, int sort);
|
||||
uint64 exec();
|
||||
void clear();
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
std::shared_ptr<ylib::update> m_update;
|
||||
};
|
||||
class insert {
|
||||
public:
|
||||
insert(ylib::mysql::conn* conn);
|
||||
~insert();
|
||||
module::insert& table(const std::string& table_name);
|
||||
module::insert& set_i32(const std::string& name, int32 value);
|
||||
module::insert& set_i64(const std::string& name, int64 value);
|
||||
module::insert& set_dob(const std::string& name, double value);
|
||||
module::insert& set_str(const std::string& name, const std::string& value);
|
||||
module::insert& set_not_ppst(const std::string& name, const std::string& value);
|
||||
uint64 exec();
|
||||
void clear();
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
std::shared_ptr<ylib::insert> m_insert;
|
||||
};
|
||||
class delete_ {
|
||||
public:
|
||||
delete_(ylib::mysql::conn* conn);
|
||||
~delete_();
|
||||
module::delete_& table(const std::string& table_name);
|
||||
module::delete_& where_i32(const std::string& name, const std::string& expression, int32 value);
|
||||
module::delete_& where_i64(const std::string& name, const std::string& expression, int64 value);
|
||||
module::delete_& where_dob(const std::string& name, const std::string& expression, double value);
|
||||
module::delete_& where_str(const std::string& name, const std::string& expression, const std::string& value);
|
||||
module::delete_& where_expression(const std::string& expression);
|
||||
module::delete_& page(uint32 page, uint32 count);
|
||||
module::delete_& limit(uint32 start, uint32 count);
|
||||
module::delete_& orderby(const std::string& field, int sort);
|
||||
uint64 exec();
|
||||
void clear();
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
std::shared_ptr <ylib::delete_> m_delete;
|
||||
};
|
||||
/// <summary>
|
||||
/// MYSQL连接池
|
||||
/// </summary>
|
||||
class mysql :public module::base {
|
||||
public:
|
||||
mysql();
|
||||
~mysql() override;
|
||||
/// <summary>
|
||||
/// 启动
|
||||
/// </summary>
|
||||
/// <param name="ipaddress"></param>
|
||||
/// <param name="username"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="database"></param>
|
||||
/// <param name="charset"></param>
|
||||
/// <param name="port"></param>
|
||||
/// <param name="size"></param>
|
||||
/// <returns></returns>
|
||||
bool start(const std::string& ipaddress, const std::string& username, const std::string& password, const std::string& database, const std::string& charset, ushort port, int32 size);
|
||||
/// <summary>
|
||||
/// 关闭
|
||||
/// </summary>
|
||||
void close();
|
||||
|
||||
std::shared_ptr<module::select> select();
|
||||
std::shared_ptr<module::insert> insert();
|
||||
std::shared_ptr<module::update> update();
|
||||
std::shared_ptr<module::delete_> delete_();
|
||||
private:
|
||||
std::shared_ptr<ylib::mysql::pool> m_pool;
|
||||
|
||||
// 通过 imodule 继承
|
||||
virtual void regist_global(const std::string& name, sol::state* lua);
|
||||
virtual void delete_global() { delete this; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user