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