增强mysql
This commit is contained in:
@@ -59,7 +59,7 @@ if(MSVC)
|
||||
$<$<CONFIG:Debug>:${YLIB}/lib/ylib_d.lib>
|
||||
|
||||
$<$<CONFIG:Release>:${FASTWEB}/bin/release/3rdparty/lib/lua.lib>
|
||||
$<$<CONFIG:Release>:D:/3rdparty/mysql-connector-c++-8.2.0-winx64-release/lib64/vs14/mysqlcppconn.lib>
|
||||
$<$<CONFIG:Release>:D:/mysql-connector-c++-8.2.0-winx64-release/lib64/vs14/mysqlcppconn.lib>
|
||||
$<$<CONFIG:Release>:${YLIB}/lib/ylib.lib>
|
||||
)
|
||||
else()
|
||||
|
||||
177
src/mysql.cpp
177
src/mysql.cpp
@@ -426,16 +426,16 @@ 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<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,
|
||||
@@ -452,6 +452,8 @@ void module::mysql_regist(sol::state* lua)
|
||||
module::update::regist(lua);
|
||||
module::delete_::regist(lua);
|
||||
module::insert::regist(lua);
|
||||
module::mysql_conn::regist(lua);
|
||||
module::mysql_prepare_statement::regist(lua);
|
||||
|
||||
}
|
||||
|
||||
@@ -503,7 +505,12 @@ 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)
|
||||
std::shared_ptr<module::mysql_conn> module::mysql::get()
|
||||
{
|
||||
return std::make_shared<module::mysql_conn>(m_pool->get());
|
||||
}
|
||||
|
||||
void module::mysql::regist_global(const char* name, sol::state* lua)
|
||||
{
|
||||
lua->registry()[name] = this;
|
||||
(*lua)[name] = this;
|
||||
@@ -566,7 +573,7 @@ sol::object module::mysql_result::get(sol::object obj, sol::this_state s)
|
||||
GET_VALUE(get_int32);
|
||||
}
|
||||
|
||||
else if (type == "varchar" || type == "char" || type == "text" || type == "datetime")
|
||||
else if (type == "varchar" || type == "char" || type == "text" || type == "datetime" || type == "binary")
|
||||
{
|
||||
GET_VALUE(get_string);
|
||||
}
|
||||
@@ -615,3 +622,149 @@ void module::mysql_result::regist(sol::state* lua)
|
||||
);
|
||||
}
|
||||
|
||||
module::mysql_conn::mysql_conn(ylib::mysql::conn* conn)
|
||||
{
|
||||
m_conn = conn;
|
||||
}
|
||||
|
||||
module::mysql_conn::~mysql_conn()
|
||||
{
|
||||
if (m_conn != nullptr)
|
||||
{
|
||||
m_conn->pool()->recover(m_conn);
|
||||
m_conn = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void module::mysql_conn::clear()
|
||||
{
|
||||
m_conn->clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<module::mysql_prepare_statement> module::mysql_conn::setsql(const std::string& sql)
|
||||
{
|
||||
return std::make_shared<module::mysql_prepare_statement>(m_conn->setsql(sql));
|
||||
}
|
||||
|
||||
uint64 module::mysql_conn::insert_id()
|
||||
{
|
||||
return m_conn->insert_id();
|
||||
}
|
||||
|
||||
void module::mysql_conn::begin(bool autocommit)
|
||||
{
|
||||
m_conn->begin(autocommit);
|
||||
}
|
||||
|
||||
void module::mysql_conn::commit()
|
||||
{
|
||||
m_conn->commit();
|
||||
}
|
||||
|
||||
void module::mysql_conn::rollback()
|
||||
{
|
||||
m_conn->rollback();
|
||||
}
|
||||
|
||||
void module::mysql_conn::setDatabase(const std::string& name)
|
||||
{
|
||||
m_conn->setDatabase(name);
|
||||
}
|
||||
|
||||
void module::mysql_conn::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::mysql_conn>("mysql_conn",
|
||||
"begin", &module::mysql_conn::begin,
|
||||
"clear", &module::mysql_conn::clear,
|
||||
"commit", &module::mysql_conn::commit,
|
||||
"insert_id", &module::mysql_conn::insert_id,
|
||||
"rollback", &module::mysql_conn::rollback,
|
||||
"setDatabase", &module::mysql_conn::setDatabase,
|
||||
"setsql", &module::mysql_conn::setsql
|
||||
);
|
||||
}
|
||||
|
||||
module::mysql_prepare_statement::mysql_prepare_statement(ylib::mysql::prepare_statement* pstt):m_pstt(pstt)
|
||||
{
|
||||
}
|
||||
|
||||
module::mysql_prepare_statement::~mysql_prepare_statement()
|
||||
{
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_bigint(uint32 index, const std::string& value)
|
||||
{
|
||||
m_pstt->set_bigint(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_boolean(uint32 index, bool value)
|
||||
{
|
||||
m_pstt->set_boolean(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_datetime(uint32 index, const std::string& value)
|
||||
{
|
||||
m_pstt->set_datetime(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_dob(uint32 index, double value)
|
||||
{
|
||||
m_pstt->set_double(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_i32(uint32 index, int32 value)
|
||||
{
|
||||
m_pstt->set_int32(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_i64(uint32 index, int64 value)
|
||||
{
|
||||
m_pstt->set_int64(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_null(uint32 index)
|
||||
{
|
||||
m_pstt->set_null(index);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_str(uint32 index, const std::string& value)
|
||||
{
|
||||
m_pstt->set_string(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::set_blob(uint32 index, const ylib::buffer& value)
|
||||
{
|
||||
m_pstt->set_blob(index, value);
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::clear()
|
||||
{
|
||||
m_pstt->clear();
|
||||
}
|
||||
|
||||
uint64 module::mysql_prepare_statement::update()
|
||||
{
|
||||
return m_pstt->update();
|
||||
}
|
||||
|
||||
std::shared_ptr<module::mysql_result> module::mysql_prepare_statement::query()
|
||||
{
|
||||
return std::make_shared<module::mysql_result>(m_pstt->query());
|
||||
}
|
||||
|
||||
void module::mysql_prepare_statement::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::mysql_prepare_statement>("mysql_prepare_statement",
|
||||
"begin", &module::mysql_prepare_statement::clear,
|
||||
"clear", &module::mysql_prepare_statement::query,
|
||||
"set_bigint", &module::mysql_prepare_statement::set_bigint,
|
||||
"set_blob", &module::mysql_prepare_statement::set_blob,
|
||||
"set_boolean", &module::mysql_prepare_statement::set_boolean,
|
||||
"set_datetime", &module::mysql_prepare_statement::set_datetime,
|
||||
"set_dob", &module::mysql_prepare_statement::set_dob,
|
||||
"set_i32", &module::mysql_prepare_statement::set_i32,
|
||||
"set_i64", &module::mysql_prepare_statement::set_i64,
|
||||
"set_str", &module::mysql_prepare_statement::set_str
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
45
src/mysql.h
45
src/mysql.h
@@ -148,6 +148,47 @@ namespace module
|
||||
private:
|
||||
std::shared_ptr <ylib::delete_> m_delete;
|
||||
};
|
||||
|
||||
class mysql_prepare_statement{
|
||||
public:
|
||||
mysql_prepare_statement(ylib::mysql::prepare_statement* pstt);
|
||||
~mysql_prepare_statement();
|
||||
void set_bigint(uint32 index, const std::string& value);
|
||||
void set_boolean(uint32 index, bool value);
|
||||
void set_datetime(uint32 index, const std::string& value);
|
||||
void set_dob(uint32 index, double value);
|
||||
void set_i32(uint32 index, int32 value);
|
||||
void set_i64(uint32 index, int64 value);
|
||||
void set_null(uint32 index);
|
||||
void set_str(uint32 index, const std::string& value);
|
||||
void set_blob(uint32 index, const ylib::buffer& value);
|
||||
void clear();
|
||||
uint64 update();
|
||||
std::shared_ptr<module::mysql_result> query();
|
||||
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
ylib::mysql::prepare_statement* m_pstt = nullptr;
|
||||
};
|
||||
|
||||
class mysql_conn
|
||||
{
|
||||
public:
|
||||
mysql_conn(ylib::mysql::conn* conn);
|
||||
~mysql_conn();
|
||||
|
||||
void clear();
|
||||
std::shared_ptr<module::mysql_prepare_statement> setsql(const std::string& sql);
|
||||
uint64 insert_id();
|
||||
void begin(bool autocommit = false);
|
||||
void commit();
|
||||
void rollback();
|
||||
void setDatabase(const std::string& name);
|
||||
|
||||
static void regist(sol::state* lua);
|
||||
private:
|
||||
ylib::mysql::conn* m_conn = nullptr;
|
||||
};
|
||||
/// <summary>
|
||||
/// MYSQL连接池
|
||||
/// </summary>
|
||||
@@ -176,11 +217,13 @@ namespace module
|
||||
std::shared_ptr<module::insert> insert();
|
||||
std::shared_ptr<module::update> update();
|
||||
std::shared_ptr<module::delete_> delete_();
|
||||
|
||||
std::shared_ptr<module::mysql_conn> get();
|
||||
private:
|
||||
std::shared_ptr<ylib::mysql::pool> m_pool;
|
||||
|
||||
// 通过 imodule 继承
|
||||
virtual void regist_global(const std::string& name, sol::state* lua);
|
||||
virtual void regist_global(const char* name, sol::state* lua);
|
||||
virtual void delete_global() { delete this; }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user