Skip to content

Commit 163021c

Browse files
authored
Merge pull request #46 from iwongu/extension
Extension
2 parents 9b8543f + e9c8440 commit 163021c

File tree

8 files changed

+79
-13
lines changed

8 files changed

+79
-13
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,22 @@ sqlite3pp::query qry(
314314
"FROM foods");
315315
```
316316
317+
## loadable extension
318+
319+
```cpp
320+
#define SQLITE3PP_LOADABLE_EXTENSION
321+
#include <sqlite3ppext.h>
322+
323+
int sqlite3_extension_init(
324+
sqlite3 *pdb,
325+
char **pzErrMsg,
326+
const sqlite3_api_routines *pApi) {
327+
SQLITE_EXTENSION_INIT2(pApi);
328+
sqlite3pp:database db(sqlite3pp::ext::borrow(pdb));
329+
// pdb is not closed since db just borrows it.
330+
}
331+
332+
```
317333

318334

319335
# See also

headeronly_src/sqlite3pp.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,33 @@
2525
#ifndef SQLITE3PP_H
2626
#define SQLITE3PP_H
2727

28-
#define SQLITE3PP_VERSION "1.0.6"
28+
#define SQLITE3PP_VERSION "1.0.7"
2929
#define SQLITE3PP_VERSION_MAJOR 1
3030
#define SQLITE3PP_VERSION_MINOR 0
31-
#define SQLITE3PP_VERSION_PATCH 6
31+
#define SQLITE3PP_VERSION_PATCH 7
3232

3333
#include <functional>
3434
#include <iterator>
35-
#include <sqlite3.h>
3635
#include <stdexcept>
3736
#include <string>
3837
#include <tuple>
3938

39+
#ifdef SQLITE3PP_LOADABLE_EXTENSION
40+
#include <sqlite3ext.h>
41+
SQLITE_EXTENSION_INIT1
42+
#else
43+
# include <sqlite3.h>
44+
#endif
45+
4046
namespace sqlite3pp
4147
{
48+
class database;
49+
4250
namespace ext
4351
{
4452
class function;
4553
class aggregate;
54+
database borrow(sqlite3* pdb);
4655
}
4756

4857
template <class T>
@@ -71,6 +80,7 @@ namespace sqlite3pp
7180
friend class database_error;
7281
friend class ext::function;
7382
friend class ext::aggregate;
83+
friend database ext::borrow(sqlite3* pdb);
7484

7585
public:
7686
using busy_handler = std::function<int (int)>;
@@ -119,8 +129,12 @@ namespace sqlite3pp
119129
void set_update_handler(update_handler h);
120130
void set_authorize_handler(authorize_handler h);
121131

132+
private:
133+
database(sqlite3* pdb) : db_(pdb), borrowing_(true) {}
134+
122135
private:
123136
sqlite3* db_;
137+
bool borrowing_;
124138

125139
busy_handler bh_;
126140
commit_handler ch_;

headeronly_src/sqlite3pp.ipp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace sqlite3pp
6464

6565
} // namespace
6666

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

7676
inline database::database(database&& db) : db_(std::move(db.db_)),
77+
borrowing_(std::move(db.borrowing_)),
7778
bh_(std::move(db.bh_)),
7879
ch_(std::move(db.ch_)),
7980
rh_(std::move(db.rh_)),
@@ -87,6 +88,7 @@ namespace sqlite3pp
8788
{
8889
db_ = std::move(db.db_);
8990
db.db_ = nullptr;
91+
borrowing_ = std::move(db.borrowing_);
9092

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

100102
inline database::~database()
101103
{
102-
disconnect();
104+
if (!borrowing_) {
105+
disconnect();
106+
}
103107
}
104108

105109
inline int database::connect(char const* dbname, int flags, char const* vfs)
106110
{
107-
disconnect();
111+
if (!borrowing_) {
112+
disconnect();
113+
}
108114

109115
return sqlite3_open_v2(dbname, &db_, flags, vfs);
110116
}

headeronly_src/sqlite3ppext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ namespace sqlite3pp
7676

7777
namespace ext
7878
{
79+
database borrow(sqlite3* pdb) {
80+
return database(pdb);
81+
}
7982

8083
class context : noncopyable
8184
{

src/sqlite3pp.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ namespace sqlite3pp
6666

6767
} // namespace
6868

69-
database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr)
69+
database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr), borrowing_(false)
7070
{
7171
if (dbname) {
7272
auto rc = connect(dbname, flags, vfs);
@@ -75,7 +75,12 @@ namespace sqlite3pp
7575
}
7676
}
7777

78+
database::database(sqlite3* pdb) : db_(pdb), borrowing_(true)
79+
{
80+
}
81+
7882
database::database(database&& db) : db_(std::move(db.db_)),
83+
borrowing_(std::move(db.borrowing_)),
7984
bh_(std::move(db.bh_)),
8085
ch_(std::move(db.ch_)),
8186
rh_(std::move(db.rh_)),
@@ -89,7 +94,7 @@ namespace sqlite3pp
8994
{
9095
db_ = std::move(db.db_);
9196
db.db_ = nullptr;
92-
97+
borrowing_ = std::move(db.borrowing_);
9398
bh_ = std::move(db.bh_);
9499
ch_ = std::move(db.ch_);
95100
rh_ = std::move(db.rh_);
@@ -101,12 +106,16 @@ namespace sqlite3pp
101106

102107
database::~database()
103108
{
104-
disconnect();
109+
if (!borrowing_) {
110+
disconnect();
111+
}
105112
}
106113

107114
int database::connect(char const* dbname, int flags, char const* vfs)
108115
{
109-
disconnect();
116+
if (!borrowing_) {
117+
disconnect();
118+
}
110119

111120
return sqlite3_open_v2(dbname, &db_, flags, vfs);
112121
}

src/sqlite3pp.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,33 @@
2525
#ifndef SQLITE3PP_H
2626
#define SQLITE3PP_H
2727

28-
#define SQLITE3PP_VERSION "1.0.0"
28+
#define SQLITE3PP_VERSION "1.0.7"
2929
#define SQLITE3PP_VERSION_MAJOR 1
3030
#define SQLITE3PP_VERSION_MINOR 0
31-
#define SQLITE3PP_VERSION_PATCH 6
31+
#define SQLITE3PP_VERSION_PATCH 7
3232

3333
#include <functional>
3434
#include <iterator>
35-
#include <sqlite3.h>
3635
#include <stdexcept>
3736
#include <string>
3837
#include <tuple>
3938

39+
#ifdef SQLITE3PP_LOADABLE_EXTENSION
40+
#include <sqlite3ext.h>
41+
SQLITE_EXTENSION_INIT1
42+
#else
43+
# include <sqlite3.h>
44+
#endif
45+
4046
namespace sqlite3pp
4147
{
48+
class database;
49+
4250
namespace ext
4351
{
4452
class function;
4553
class aggregate;
54+
database borrow(sqlite3* pdb);
4655
}
4756

4857
template <class T>
@@ -72,6 +81,7 @@ namespace sqlite3pp
7281
friend class database_error;
7382
friend class ext::function;
7483
friend class ext::aggregate;
84+
friend database ext::borrow(sqlite3* pdb);
7585

7686
public:
7787
using busy_handler = std::function<int (int)>;
@@ -120,8 +130,12 @@ namespace sqlite3pp
120130
void set_update_handler(update_handler h);
121131
void set_authorize_handler(authorize_handler h);
122132

133+
private:
134+
database(sqlite3* pdb);
135+
123136
private:
124137
sqlite3* db_;
138+
bool borrowing_;
125139

126140
busy_handler bh_;
127141
commit_handler ch_;

src/sqlite3ppext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ namespace sqlite3pp
5959

6060
} // namespace
6161

62+
database borrow(sqlite3* pdb) {
63+
return database(pdb);
64+
}
6265

6366
context::context(sqlite3_context* ctx, int nargs, sqlite3_value** values)
6467
: ctx_(ctx), nargs_(nargs), values_(values)

src/sqlite3ppext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace sqlite3pp
7676

7777
namespace ext
7878
{
79+
database borrow(sqlite3* pdb);
7980

8081
class context : noncopyable
8182
{

0 commit comments

Comments
 (0)