Skip to content

Commit

Permalink
Merge pull request #46 from iwongu/extension
Browse files Browse the repository at this point in the history
Extension
  • Loading branch information
iwongu committed Aug 1, 2017
2 parents 9b8543f + e9c8440 commit 163021c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 13 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,22 @@ sqlite3pp::query qry(
"FROM foods");
```
## loadable extension
```cpp
#define SQLITE3PP_LOADABLE_EXTENSION
#include <sqlite3ppext.h>
int sqlite3_extension_init(
sqlite3 *pdb,
char **pzErrMsg,
const sqlite3_api_routines *pApi) {
SQLITE_EXTENSION_INIT2(pApi);
sqlite3pp:database db(sqlite3pp::ext::borrow(pdb));
// pdb is not closed since db just borrows it.
}
```


# See also
Expand Down
20 changes: 17 additions & 3 deletions headeronly_src/sqlite3pp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,33 @@
#ifndef SQLITE3PP_H
#define SQLITE3PP_H

#define SQLITE3PP_VERSION "1.0.6"
#define SQLITE3PP_VERSION "1.0.7"
#define SQLITE3PP_VERSION_MAJOR 1
#define SQLITE3PP_VERSION_MINOR 0
#define SQLITE3PP_VERSION_PATCH 6
#define SQLITE3PP_VERSION_PATCH 7

#include <functional>
#include <iterator>
#include <sqlite3.h>
#include <stdexcept>
#include <string>
#include <tuple>

#ifdef SQLITE3PP_LOADABLE_EXTENSION
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#else
# include <sqlite3.h>
#endif

namespace sqlite3pp
{
class database;

namespace ext
{
class function;
class aggregate;
database borrow(sqlite3* pdb);
}

template <class T>
Expand Down Expand Up @@ -71,6 +80,7 @@ namespace sqlite3pp
friend class database_error;
friend class ext::function;
friend class ext::aggregate;
friend database ext::borrow(sqlite3* pdb);

public:
using busy_handler = std::function<int (int)>;
Expand Down Expand Up @@ -119,8 +129,12 @@ namespace sqlite3pp
void set_update_handler(update_handler h);
void set_authorize_handler(authorize_handler h);

private:
database(sqlite3* pdb) : db_(pdb), borrowing_(true) {}

private:
sqlite3* db_;
bool borrowing_;

busy_handler bh_;
commit_handler ch_;
Expand Down
12 changes: 9 additions & 3 deletions headeronly_src/sqlite3pp.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace sqlite3pp

} // namespace

inline database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr)
inline database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr), borrowing_(false)
{
if (dbname) {
auto rc = connect(dbname, flags, vfs);
Expand All @@ -74,6 +74,7 @@ namespace sqlite3pp
}

inline database::database(database&& db) : db_(std::move(db.db_)),
borrowing_(std::move(db.borrowing_)),
bh_(std::move(db.bh_)),
ch_(std::move(db.ch_)),
rh_(std::move(db.rh_)),
Expand All @@ -87,6 +88,7 @@ namespace sqlite3pp
{
db_ = std::move(db.db_);
db.db_ = nullptr;
borrowing_ = std::move(db.borrowing_);

bh_ = std::move(db.bh_);
ch_ = std::move(db.ch_);
Expand All @@ -99,12 +101,16 @@ namespace sqlite3pp

inline database::~database()
{
disconnect();
if (!borrowing_) {
disconnect();
}
}

inline int database::connect(char const* dbname, int flags, char const* vfs)
{
disconnect();
if (!borrowing_) {
disconnect();
}

return sqlite3_open_v2(dbname, &db_, flags, vfs);
}
Expand Down
3 changes: 3 additions & 0 deletions headeronly_src/sqlite3ppext.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ namespace sqlite3pp

namespace ext
{
database borrow(sqlite3* pdb) {
return database(pdb);
}

class context : noncopyable
{
Expand Down
17 changes: 13 additions & 4 deletions src/sqlite3pp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace sqlite3pp

} // namespace

database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr)
database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr), borrowing_(false)
{
if (dbname) {
auto rc = connect(dbname, flags, vfs);
Expand All @@ -75,7 +75,12 @@ namespace sqlite3pp
}
}

database::database(sqlite3* pdb) : db_(pdb), borrowing_(true)
{
}

database::database(database&& db) : db_(std::move(db.db_)),
borrowing_(std::move(db.borrowing_)),
bh_(std::move(db.bh_)),
ch_(std::move(db.ch_)),
rh_(std::move(db.rh_)),
Expand All @@ -89,7 +94,7 @@ namespace sqlite3pp
{
db_ = std::move(db.db_);
db.db_ = nullptr;

borrowing_ = std::move(db.borrowing_);
bh_ = std::move(db.bh_);
ch_ = std::move(db.ch_);
rh_ = std::move(db.rh_);
Expand All @@ -101,12 +106,16 @@ namespace sqlite3pp

database::~database()
{
disconnect();
if (!borrowing_) {
disconnect();
}
}

int database::connect(char const* dbname, int flags, char const* vfs)
{
disconnect();
if (!borrowing_) {
disconnect();
}

return sqlite3_open_v2(dbname, &db_, flags, vfs);
}
Expand Down
20 changes: 17 additions & 3 deletions src/sqlite3pp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,33 @@
#ifndef SQLITE3PP_H
#define SQLITE3PP_H

#define SQLITE3PP_VERSION "1.0.0"
#define SQLITE3PP_VERSION "1.0.7"
#define SQLITE3PP_VERSION_MAJOR 1
#define SQLITE3PP_VERSION_MINOR 0
#define SQLITE3PP_VERSION_PATCH 6
#define SQLITE3PP_VERSION_PATCH 7

#include <functional>
#include <iterator>
#include <sqlite3.h>
#include <stdexcept>
#include <string>
#include <tuple>

#ifdef SQLITE3PP_LOADABLE_EXTENSION
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#else
# include <sqlite3.h>
#endif

namespace sqlite3pp
{
class database;

namespace ext
{
class function;
class aggregate;
database borrow(sqlite3* pdb);
}

template <class T>
Expand Down Expand Up @@ -72,6 +81,7 @@ namespace sqlite3pp
friend class database_error;
friend class ext::function;
friend class ext::aggregate;
friend database ext::borrow(sqlite3* pdb);

public:
using busy_handler = std::function<int (int)>;
Expand Down Expand Up @@ -120,8 +130,12 @@ namespace sqlite3pp
void set_update_handler(update_handler h);
void set_authorize_handler(authorize_handler h);

private:
database(sqlite3* pdb);

private:
sqlite3* db_;
bool borrowing_;

busy_handler bh_;
commit_handler ch_;
Expand Down
3 changes: 3 additions & 0 deletions src/sqlite3ppext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace sqlite3pp

} // namespace

database borrow(sqlite3* pdb) {
return database(pdb);
}

context::context(sqlite3_context* ctx, int nargs, sqlite3_value** values)
: ctx_(ctx), nargs_(nargs), values_(values)
Expand Down
1 change: 1 addition & 0 deletions src/sqlite3ppext.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace sqlite3pp

namespace ext
{
database borrow(sqlite3* pdb);

class context : noncopyable
{
Expand Down

0 comments on commit 163021c

Please sign in to comment.