Change pointers to smart pointer (shared_ptr)

This commit is contained in:
Kirigaya Kazuto 2017-04-16 17:32:39 +08:00
parent 95dbdbd277
commit 92f2d9b4af
2 changed files with 36 additions and 20 deletions

View File

@ -2,47 +2,57 @@
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
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) void SQLStatement::_setStmt(sqlite3_stmt* p)
{ {
_st=p; _set(p);
} }
sqlite3_stmt* SQLStatement::_getStmt() const 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) 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 SQLDB::prepare(const std::string& SQLCommand)
{ {
SQLStatement stmt; SQLStatement stmt;
sqlite3_stmt* pstmt=nullptr; 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; if(ret<0) return stmt;
stmt._setStmt(pstmt); stmt._setStmt(pstmt);
return stmt; return stmt;
@ -56,7 +66,7 @@ int SQLDB::step(const SQLStatement& Statement)
int SQLDB::exec(const std::string& SQLCommand,SQLCallback callback,void* param) int SQLDB::exec(const std::string& SQLCommand,SQLCallback callback,void* param)
{ {
char* errmsg=nullptr; 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); printf("ErrMsg: %s\n",errmsg);
return ret; return ret;
} }

View File

@ -1,18 +1,21 @@
#pragma once #pragma once
#include "sqlite/sqlite3.h" #include "sqlite/sqlite3.h"
#include <string> #include <string>
#include <memory>
class SQLStatement class SQLStatement
{ {
public: public:
bool isReady(); bool isReady() const;
SQLStatement(); SQLStatement()=default;
~SQLStatement(); ~SQLStatement()=default;
void _setStmt(sqlite3_stmt*); void _setStmt(sqlite3_stmt*);
sqlite3_stmt* _getStmt() const; sqlite3_stmt* _getStmt() const;
private: private:
sqlite3_stmt* _st; sqlite3_stmt* _get() const;
void _set(sqlite3_stmt*);
std::shared_ptr<sqlite3_stmt> _st;
}; };
@ -21,15 +24,18 @@ using SQLCallback = int (*)(void* ExParam,int colNum,char** colVal,char** colNam
class SQLDB class SQLDB
{ {
public: public:
SQLDB(); SQLDB()=default;
~SQLDB(); ~SQLDB()=default;
bool isOpened(); bool isOpened();
int open(const std::string& filename); int open(const std::string& filename);
SQLStatement prepare(const std::string& SQLCommand); SQLStatement prepare(const std::string& SQLCommand);
int step(const SQLStatement& Statement); int step(const SQLStatement& Statement);
int exec(const std::string& SQLCommand,SQLCallback callback,void* param); int exec(const std::string& SQLCommand,SQLCallback callback,void* param);
private: private:
sqlite3* _db; sqlite3* _get();
void _set(sqlite3*);
std::shared_ptr<sqlite3> _db;
}; };