Skip to content

Commit bff4fff

Browse files
committed
New features added to README
1 parent 3fb513c commit bff4fff

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,64 @@ int main() {
7777
}
7878
```
7979

80+
Prepared Statements
81+
=====
82+
It is possible to retain and reuse statments this will keep the query plan and in case of an complex query or many uses might increase the performance significantly.
83+
84+
```c++
85+
86+
database db(":memory:");
87+
88+
// if you use << on a sqlite::database you get a prepared statment back
89+
// this will not be executed till it gets destroyed or you execute it explicitly
90+
auto ps = db << "select ?,? "; // get a prepared parsed and ready statment
91+
92+
// first if needed bind values to it
93+
ps << 5;
94+
int tmp = 8;
95+
ps << tmp;
96+
97+
// now you can execute it, if the statment was executed once it will not be executed again when it goes out of scope.
98+
// But beware that it will if it wasn't executed!
99+
ps >> [&](int a,int b){ ... };
100+
101+
// after a successfull execution the statment needs to be reset to be execute again. This will reset the bound values too!
102+
ps.reset();
103+
104+
// If you dont need the returned values you can execute it like this
105+
ps.execute(); // the statment will not be reset!
106+
107+
// there is a convinience operator to execute and reset in one go
108+
ps++;
109+
110+
// To disable the execution of a statment when it goes out of scope and wasn't used
111+
ps.set_used(true); // or false if you want it to execute even if it was used
112+
113+
// Usage Example:
114+
115+
auto ps = db << "insert into complex_table_with_lots_of_indices values (?,?,?)";
116+
int i = 0;
117+
while( i < 100000 ){
118+
ps << long_list[i++] << long_list[i++] << long_list[i++];
119+
ps++;
120+
}
121+
```
122+
123+
Shared Connections
124+
=====
125+
If you need the handle to the database connection to execute sqlite3 commands directly you can get a managed shared_ptr to it, so it will not close as long as you have a referenc to it.
126+
127+
```c++
128+
sqlite::connection_type con;
129+
130+
{ // scope of our db object
131+
database db(":memory:"); // Make a connection and create in memory db
132+
con = db.get_sqlite3_connection();
133+
} // here the temporary db would be lost but we still have the connection for our direct API calls
134+
135+
database db2(con); // or we can even make a new database object from it
136+
```
137+
80138
Transactions
81139
=====
82140
You can use transactions with `begin;`, `commit;` and `rollback;` commands.

hdr/sqlite_modern_cpp.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ namespace sqlite {
9999

100100
template<std::size_t> class binder;
101101

102-
//template<typename T> database_binder&& operator <<(database_binder&& db,T const&& val);
103-
//template<typename T> void get_col_from_db(database_binder& db, int inx, T& val);
102+
typedef std::shared_ptr<sqlite3> connection_type;
104103

105104
template<typename Tuple, int Element = 0, bool Last = (std::tuple_size<Tuple>::value == Element)> struct tuple_iterate {
106105
static void iterate(Tuple& t, database_binder& db) {
@@ -134,6 +133,9 @@ namespace sqlite {
134133
}
135134
}
136135

136+
void set_used(bool state) { execution_started = state; }
137+
bool get_used() const { return execution_started; }
138+
137139
private:
138140
std::shared_ptr<sqlite3> _db;
139141
std::u16string _sql;
@@ -286,7 +288,7 @@ namespace sqlite {
286288
return database_binder::chain_type(new database_binder(_db, std::string(sql)));
287289
}
288290

289-
std::shared_ptr<sqlite3> get_sqlite3_connection() const { return _db; }
291+
connection_type get_sqlite3_connection() const { return _db; }
290292

291293
sqlite3_int64 last_insert_rowid() const {
292294
return sqlite3_last_insert_rowid(_db.get());

0 commit comments

Comments
 (0)