|
| 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