Skip to content

Commit bbfff7b

Browse files
committed
Added test and documentation
1 parent 3a14459 commit bbfff7b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,25 @@ sqlite::sqlite_exception has a get_code() member function to get the SQLITE3 err
304304
catch(sqlite::exceptions::constraint e) { } */
305305
```
306306

307+
Custom SQL functions
308+
----
309+
310+
To extend SQLite with custom functions, you just implement them in C++:
311+
312+
```c++
313+
database db(":memory:");
314+
db.define("tgamma", [](double i) {return std::tgamma(i);});
315+
db << "CREATE TABLE numbers (number INTEGER);";
316+
317+
for(auto i=0; i!=10; ++i)
318+
db << "INSERT INTO numbers VALUES (?);" << i;
319+
320+
db << "SELECT number, tgamma(number+1) FROM numbers;" >> [](double number, double factorial) {
321+
cout << number << "! = " << factorial << '\n';
322+
};
323+
```
324+
325+
307326
NDK support
308327
----
309328
Just Make sure you are using the full path of your database file :

tests/functions.cc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <cmath>
4+
#include <sqlite_modern_cpp.h>
5+
using namespace sqlite;
6+
using namespace std;
7+
8+
int main()
9+
{
10+
try
11+
{
12+
database db(":memory:");
13+
14+
db.define("my_new_concat", [](std::string i, std::string j) {return i+j;});
15+
db.define("my_new_concat", [](std::string i, std::string j, std::string k) {return i+j+k;});
16+
db.define("add_integers", [](int i, int j) {return i+j;});
17+
std::string test1, test3;
18+
int test2 = 0;
19+
db << "select my_new_concat('Hello ','world!')" >> test1;
20+
db << "select add_integers(1,1)" >> test2;
21+
db << "select my_new_concat('a','b','c')" >> test3;
22+
23+
if(test1 != "Hello world!" || test2 != 2 || test3 != "abc") {
24+
cout << "Wrong result\n";
25+
exit(EXIT_FAILURE);
26+
}
27+
28+
db.define("my_count", [](int &i, int) {++i;}, [](int &i) {return i;});
29+
db.define("my_concat_aggregate", [](std::string &stored, std::string current) {stored += current;}, [](std::string &stored) {return stored;});
30+
db << "create table countable(i, s)";
31+
db << "insert into countable values(1, 'a')";
32+
db << "insert into countable values(2, 'b')";
33+
db << "insert into countable values(3, 'c')";
34+
db << "select my_count(i) from countable" >> test2;
35+
db << "select my_concat_aggregate(s) from countable order by i" >> test3;
36+
37+
if(test2 != 3 || test3 != "abc") {
38+
cout << "Wrong result\n";
39+
exit(EXIT_FAILURE);
40+
}
41+
42+
db.define("tgamma", [](double i) {return std::tgamma(i);});
43+
db << "CREATE TABLE numbers (number INTEGER);";
44+
45+
for(auto i=0; i!=10; ++i)
46+
db << "INSERT INTO numbers VALUES (?);" << i;
47+
48+
db << "SELECT number, tgamma(number+1) FROM numbers;" >> [](double number, double factorial) {
49+
cout << number << "! = " << factorial << '\n';
50+
};
51+
}
52+
catch(sqlite_exception e)
53+
{
54+
cout << "Unexpected error " << e.what() << endl;
55+
exit(EXIT_FAILURE);
56+
}
57+
catch(...)
58+
{
59+
cout << "Unknown error\n";
60+
exit(EXIT_FAILURE);
61+
}
62+
63+
cout << "OK\n";
64+
exit(EXIT_SUCCESS);
65+
}

0 commit comments

Comments
 (0)