Skip to content

Commit

Permalink
Support attaching to existing sqlite3* db instance.
Browse files Browse the repository at this point in the history
Add intro in README.
  • Loading branch information
yangacer committed Jun 22, 2019
1 parent f504087 commit 76a7ce1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@
<a target="_blank" href="https://ci.appveyor.com/project/yangacer/sqlite3cpp">![Appveyor][badge.Appveyor]</a>
<a target="_blank" href="https://coveralls.io/github/yangacer/sqlite3cpp?branch=master">![Coveralls][badge.Coveralls]</a>

A C++17 wrapper library for the awsome sqlite3.
Having a cool C/C++ function and want to use it with SQL of sqlite3?

```cpp
sqlite3 *db;
sqlite3cpp::database mydb(db);
mydb.create_scalar("coolFunc", [](std::string_view input) {
// do cool stuff
return "done!";
});
```
That's it! Now you can use the `coolFunc` with the same database connection. e.g.
```cpp
char const *query = "select coolFunc(colA) from Table";
sqlite3_exec(db, query, 0, 0, 0);
// or
mydb.execute(query);
```


## Source

Expand Down
6 changes: 6 additions & 0 deletions sqlite3cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ database::database(std::string const &urn) {
m_db.reset(i);
}

database::database(sqlite3 *db) : m_owned(false), m_db(db) {}

database::~database() {
if (!m_owned) m_db.release();
}

cursor database::make_cursor() const noexcept { return cursor(*this); }

std::string database::version() const { return SQLITE3CPP_VERSION_STRING; }
Expand Down
8 changes: 8 additions & 0 deletions sqlite3cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ struct SQLITE3CPP_EXPORT database {
// filename. |urn| should be encoded in UTF-8.
database(std::string const &urn);

// Attach to an opened sqlite3 database. Call site is responsible to mangage
// life time of the passed pointer |db|. sqlite3cpp does not release the
// pointer.
database(sqlite3* db);

~database();

// Create a cursor per current database for executing SQL statements.
cursor make_cursor() const noexcept;

Expand Down Expand Up @@ -279,6 +286,7 @@ struct SQLITE3CPP_EXPORT database {
static void final_ag(sqlite3_context *ctx);
static void dispose_ag(void *user_data);

bool m_owned = true;
std::unique_ptr<sqlite3, sqlite3_deleter> m_db;
};

Expand Down

0 comments on commit 76a7ce1

Please sign in to comment.