fixed MySQL (continue)

This commit is contained in:
fasiondog 2020-09-24 00:43:24 +08:00
parent e0f491a0f5
commit 0eb73bd8e7
7 changed files with 55 additions and 45 deletions

View File

@ -31,18 +31,18 @@ public:
explicit DBConnectBase(const Parameter& param) noexcept;
virtual ~DBConnectBase() = default;
/** 开始事务 */
void transaction();
/** 提交事务 */
void commit();
/** 回滚事务 */
void rollback();
//-------------------------------------------------------------------------
// 子类接口
//-------------------------------------------------------------------------
/** 开始事务 */
virtual void transaction() = 0;
/** 提交事务 */
virtual void commit() = 0;
/** 回滚事务 */
virtual void rollback() = 0;
/** 检测连接是否可用 */
virtual bool ping() = 0;
@ -178,18 +178,6 @@ typedef shared_ptr<DBConnectBase> DBConnectPtr;
inline DBConnectBase::DBConnectBase(const Parameter& param) noexcept : m_params(param) {}
inline void DBConnectBase::transaction() {
exec("BEGIN;");
}
inline void DBConnectBase::commit() {
exec("COMMIT;");
}
inline void DBConnectBase::rollback() {
exec("ROLLBACK;");
}
inline int DBConnectBase::queryInt(const string& query) {
SQLStatementPtr st = getStatement(query);
st->exec();

View File

@ -111,4 +111,16 @@ bool MySQLConnect::tableExist(const string& tablename) {
return result;
}
void MySQLConnect::transaction() {
exec("BEGIN");
}
void MySQLConnect::commit() {
exec("COMMIT");
}
void MySQLConnect::rollback() {
exec("ROLLBACK");
}
} // namespace hku

View File

@ -33,6 +33,10 @@ public:
virtual SQLStatementPtr getStatement(const string& sql_statement) override;
virtual bool tableExist(const string& tablename) override;
virtual void transaction() override;
virtual void commit() override;
virtual void rollback() override;
private:
void close();

View File

@ -63,7 +63,7 @@ void MySQLStatement::_reset() {
HKU_CHECK(ret == 0, "Failed reset statement! {}", mysql_stmt_error(m_stmt));
// m_param_bind.clear();
// m_result_bind.clear();
m_param_buffer.clear();
// m_param_buffer.clear();
m_result_buffer.clear();
m_needs_reset = false;
m_has_bind_result = false;
@ -89,7 +89,7 @@ void MySQLStatement::_bindResult() {
MYSQL_FIELD* field;
int idx = 0;
while ((field = mysql_fetch_field(m_meta_result))) {
HKU_INFO("field {} len: {}", field->name, field->length);
// HKU_INFO("field {} len: {}", field->name, field->length);
m_result_bind[idx].buffer_type = field->type;
#if MYSQL_VERSION_ID >= 80000
m_result_bind[idx].is_null = (bool*)&m_result_is_null[idx];
@ -201,12 +201,6 @@ void MySQLStatement::sub_bindText(int idx, const string& item) {
m_param_bind[idx].buffer = (void*)p->data();
m_param_bind[idx].buffer_length = item.size();
m_param_bind[idx].is_null = 0;
unsigned long str_len = item.size();
m_param_buffer.push_back(str_len);
auto& ref = m_param_buffer.back();
m_param_bind[idx].length = boost::any_cast<unsigned long>(&ref);
;
}
void MySQLStatement::sub_bindBlob(int idx, const string& item) {
@ -219,12 +213,6 @@ void MySQLStatement::sub_bindBlob(int idx, const string& item) {
m_param_bind[idx].buffer = (void*)p->data();
m_param_bind[idx].buffer_length = item.size();
m_param_bind[idx].is_null = 0;
unsigned long str_len = item.size();
m_param_buffer.push_back(str_len);
auto& ref = m_param_buffer.back();
m_param_bind[idx].length = boost::any_cast<unsigned long>(&ref);
;
}
int MySQLStatement::sub_getNumColumns() const {
@ -287,10 +275,10 @@ void MySQLStatement::sub_getColumnAsText(int idx, string& item) {
}
try {
vector<char> p = boost::any_cast<vector<char>>(m_result_buffer[idx]);
vector<char>* p = boost::any_cast<vector<char>>(&(m_result_buffer[idx]));
std::ostringstream buf;
for (unsigned long i = 0; i < m_result_length[idx]; i++) {
buf << p[i];
buf << (*p)[i];
}
item = buf.str();
} catch (...) {
@ -310,10 +298,10 @@ void MySQLStatement::sub_getColumnAsBlob(int idx, string& item) {
}
try {
vector<char> p = boost::any_cast<vector<char>>(m_result_buffer[idx]);
vector<char>* p = boost::any_cast<vector<char>>(&m_result_buffer[idx]);
std::ostringstream buf;
for (unsigned long i = 0; i < m_result_length[idx]; i++) {
buf << p[i];
buf << (*p)[i];
}
item = buf.str();
} catch (...) {

View File

@ -91,4 +91,16 @@ bool SQLiteConnect::tableExist(const string& tablename) {
return result;
}
void SQLiteConnect::transaction() {
exec("BEGIN TRANSACTION");
}
void SQLiteConnect::commit() {
exec("COMMIT TRANSACTION");
}
void SQLiteConnect::rollback() {
exec("ROLLBACK TRANSACTION");
}
} /* namespace hku */

View File

@ -46,6 +46,10 @@ public:
virtual SQLStatementPtr getStatement(const string& sql_statement) override;
virtual bool tableExist(const string& tablename) override;
virtual void transaction() override;
virtual void commit() override;
virtual void rollback() override;
private:
void close();

View File

@ -106,12 +106,15 @@ TEST_CASE("test_mysql") {
t_list.push_back(TTT("bbb", 30, "bbb@x.com"));
t_list.push_back(TTT("ccc", 15, "ccc@x.com"));
con->batchSave(t_list.begin(), t_list.end());
/*for (auto& r : t_list) {
HKU_INFO("{}", r.toString());
}*/
vector<TTT> r_list;
con->batchLoad(r_list, "1=1 order by name DESC");
for (auto& r : r_list) {
/*for (auto& r : r_list) {
HKU_INFO("{}", r.toString());
}
}*/
CHECK(r_list.size() == 3);
CHECK(r_list[0].name == "ccc");
@ -124,17 +127,16 @@ TEST_CASE("test_mysql") {
CHECK(r_list[2].age == 20);
CHECK(r_list[2].email == "aaa@x.com");
/*TTT x;
TTT x;
con->load(x, "name='bbb'");
HKU_INFO("id: {}, name: {}, age: {}", x.id(), x.name, x.age);
x.age = 100;
con->save(x);
TTT y;
con->load(y, "name='bbb'");
CHECK(y.age == 100);*/
CHECK(y.age == 100);
// con->exec("drop table ttt");
con->exec("drop table ttt");
}
}
#endif