From 92f2d9b4af1fcba453126ccef37eae337e76ccd1 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sun, 16 Apr 2017 17:32:39 +0800 Subject: [PATCH] Change pointers to smart pointer (shared_ptr) --- sqlite_wrapper.cpp | 36 +++++++++++++++++++++++------------- sqlite_wrapper.h | 20 +++++++++++++------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/sqlite_wrapper.cpp b/sqlite_wrapper.cpp index 37cf6b8..e8822ad 100644 --- a/sqlite_wrapper.cpp +++ b/sqlite_wrapper.cpp @@ -2,47 +2,57 @@ #include #include -SQLStatement::SQLStatement() +// private +void SQLStatement::_set(sqlite3_stmt* p) { - _st=nullptr; + _st.reset(p,sqlite3_finalize); } -SQLStatement::~SQLStatement() +// private +sqlite3_stmt* SQLStatement::_get() const { - sqlite3_finalize(_st); + return _st.get(); } void SQLStatement::_setStmt(sqlite3_stmt* p) { - _st=p; + _set(p); } sqlite3_stmt* SQLStatement::_getStmt() const { - return _st; + return _get(); +} + +bool SQLStatement::isReady() const +{ + return _get()!=nullptr; } -SQLDB::SQLDB() +void SQLDB::_set(sqlite3* p) { - _db=nullptr; + _db.reset(p,sqlite3_close); } -SQLDB::~SQLDB() +sqlite3* SQLDB::_get() { - sqlite3_close(_db); + return _db.get(); } int SQLDB::open(const std::string& filename) { - return sqlite3_open(filename.c_str(),&_db); + sqlite3* _temp=nullptr; + int ret=sqlite3_open(filename.c_str(),&_temp); + _set(_temp); + return ret; } SQLStatement SQLDB::prepare(const std::string& SQLCommand) { SQLStatement stmt; sqlite3_stmt* pstmt=nullptr; - int ret=sqlite3_prepare(_db,SQLCommand.c_str(),SQLCommand.size(),&pstmt,NULL); + int ret=sqlite3_prepare(_get(),SQLCommand.c_str(),SQLCommand.size(),&pstmt,NULL); if(ret<0) return stmt; stmt._setStmt(pstmt); return stmt; @@ -56,7 +66,7 @@ int SQLDB::step(const SQLStatement& Statement) int SQLDB::exec(const std::string& SQLCommand,SQLCallback callback,void* param) { char* errmsg=nullptr; - int ret=sqlite3_exec(_db,SQLCommand.c_str(),callback,param,&errmsg); + int ret=sqlite3_exec(_get(),SQLCommand.c_str(),callback,param,&errmsg); printf("ErrMsg: %s\n",errmsg); return ret; } diff --git a/sqlite_wrapper.h b/sqlite_wrapper.h index 65bbdc9..9364192 100644 --- a/sqlite_wrapper.h +++ b/sqlite_wrapper.h @@ -1,18 +1,21 @@ #pragma once #include "sqlite/sqlite3.h" #include +#include class SQLStatement { public: - bool isReady(); - SQLStatement(); - ~SQLStatement(); + bool isReady() const; + SQLStatement()=default; + ~SQLStatement()=default; void _setStmt(sqlite3_stmt*); sqlite3_stmt* _getStmt() const; private: - sqlite3_stmt* _st; + sqlite3_stmt* _get() const; + void _set(sqlite3_stmt*); + std::shared_ptr _st; }; @@ -21,15 +24,18 @@ using SQLCallback = int (*)(void* ExParam,int colNum,char** colVal,char** colNam class SQLDB { public: - SQLDB(); - ~SQLDB(); + SQLDB()=default; + ~SQLDB()=default; + bool isOpened(); int open(const std::string& filename); SQLStatement prepare(const std::string& SQLCommand); int step(const SQLStatement& Statement); int exec(const std::string& SQLCommand,SQLCallback callback,void* param); private: - sqlite3* _db; + sqlite3* _get(); + void _set(sqlite3*); + std::shared_ptr _db; };