Skip to content

Commit b958f9b

Browse files
committed
Merge pull request #31 from aminroosta/frontier
Changed the way arguments are chained #29
2 parents bfb1f3c + ec34b05 commit b958f9b

File tree

4 files changed

+615
-431
lines changed

4 files changed

+615
-431
lines changed

Diff for: README.md

+73
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,79 @@ 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+
database db(":memory:");
86+
87+
// if you use << on a sqlite::database you get a prepared statment back
88+
// this will not be executed till it gets destroyed or you execute it explicitly
89+
auto ps = db << "select a,b from table where something = ? and anotherthing = ?"; // get a prepared parsed and ready statment
90+
91+
// first if needed bind values to it
92+
ps << 5;
93+
int tmp = 8;
94+
ps << tmp;
95+
96+
// now you can execute it with `operator>>` or `execute()`.
97+
// If the statment was executed once it will not be executed again when it goes out of scope.
98+
// But beware that it will execute on destruction 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.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+
Take this example on how to deal with a database backup using SQLITEs own functions in a save and modern way.
128+
```c++
129+
try {
130+
database backup("backup"); //Open the database file we want to backup to
131+
132+
auto con = db.connection(); // get a handle to the DB we want to backup in our scope
133+
// this way we are sure the DB is open and ok while we backup
134+
135+
// Init Backup and make sure its freed on exit or exceptions!
136+
auto state =
137+
std::unique_ptr<sqlite3_backup,decltype(&sqlite3_backup_finish)>(
138+
sqlite3_backup_init(backup.connection().get(), "main", con.get(), "main"),
139+
sqlite3_backup_finish
140+
);
141+
142+
if(state) {
143+
int rc;
144+
// Each iteration of this loop copies 500 database pages from database db to the backup database.
145+
do {
146+
rc = sqlite3_backup_step(state.get(), 500);
147+
std::cout << "Remaining " << sqlite3_backup_remaining(state.get()) << "/" << sqlite3_backup_pagecount(state.get()) << "\n";
148+
} while(rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
149+
}
150+
} // Release allocated resources.
151+
```
152+
80153
Transactions
81154
=====
82155
You can use transactions with `begin;`, `commit;` and `rollback;` commands.

0 commit comments

Comments
 (0)