|
| 1 | +#include <iostream> |
| 2 | +#include <cstdlib> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sqlite_modern_cpp.h> |
| 5 | +#include <endian.h> |
| 6 | +using namespace sqlite; |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +struct TmpFile |
| 10 | +{ |
| 11 | + string fname; |
| 12 | + |
| 13 | + TmpFile() |
| 14 | + { |
| 15 | + char f[]="/tmp/sqlite_modern_cpp_test_XXXXXX"; |
| 16 | + int fid = mkstemp(f); |
| 17 | + close(fid); |
| 18 | + |
| 19 | + fname = f; |
| 20 | + } |
| 21 | + |
| 22 | + ~TmpFile() |
| 23 | + { |
| 24 | + unlink(fname.c_str()); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +#if __BYTE_ORDER == __BIG_ENDIAN |
| 29 | +#define OUR_UTF16 "UTF-16be" |
| 30 | +#else |
| 31 | +#define OUR_UTF16 "UTF-16le" |
| 32 | +#endif |
| 33 | + |
| 34 | +int main() |
| 35 | +{ |
| 36 | + try |
| 37 | + { |
| 38 | + TmpFile file; |
| 39 | + sqlite::sqlite_config cfg; |
| 40 | + std::string enc; |
| 41 | + { |
| 42 | + database db(":memory:", cfg); |
| 43 | + db << "PRAGMA encoding;" >> enc; |
| 44 | + if(enc != "UTF-8") { |
| 45 | + cout << "Unexpected encoding on line " << __LINE__ << '\n'; |
| 46 | + exit(EXIT_FAILURE); |
| 47 | + } |
| 48 | + } |
| 49 | + { |
| 50 | + database db(u":memory:", cfg); |
| 51 | + db << "PRAGMA encoding;" >> enc; |
| 52 | + if(enc != OUR_UTF16) { |
| 53 | + cout << "Unexpected encoding on line " << __LINE__ << '\n'; |
| 54 | + exit(EXIT_FAILURE); |
| 55 | + } |
| 56 | + } |
| 57 | + { |
| 58 | + cfg.encoding = Encoding::UTF8; |
| 59 | + database db(u":memory:", cfg); |
| 60 | + db << "PRAGMA encoding;" >> enc; |
| 61 | + if(enc != "UTF-8") { |
| 62 | + cout << "Unexpected encoding on line " << __LINE__ << '\n'; |
| 63 | + exit(EXIT_FAILURE); |
| 64 | + } |
| 65 | + } |
| 66 | + { |
| 67 | + cfg.encoding = Encoding::UTF16; |
| 68 | + database db(u":memory:", cfg); |
| 69 | + db << "PRAGMA encoding;" >> enc; |
| 70 | + if(enc != OUR_UTF16) { |
| 71 | + cout << "Unexpected encoding on line " << __LINE__ << '\n'; |
| 72 | + exit(EXIT_FAILURE); |
| 73 | + } |
| 74 | + } |
| 75 | + { |
| 76 | + database db(file.fname, cfg); |
| 77 | + db << "PRAGMA encoding;" >> enc; |
| 78 | + if(enc != OUR_UTF16) { |
| 79 | + cout << "Unexpected encoding on line " << __LINE__ << '\n'; |
| 80 | + exit(EXIT_FAILURE); |
| 81 | + } |
| 82 | + |
| 83 | + db << "CREATE TABLE foo (a string);"; |
| 84 | + db << "INSERT INTO foo VALUES (?)" << "hello"; |
| 85 | + } |
| 86 | + { |
| 87 | + cfg.flags = sqlite::Flags::OPEN_READONLY; |
| 88 | + database db(file.fname, cfg); |
| 89 | + |
| 90 | + string str; |
| 91 | + db << "SELECT a FROM foo;" >> str; |
| 92 | + |
| 93 | + if(str != "hello") |
| 94 | + { |
| 95 | + cout << "Bad result on line " << __LINE__ << endl; |
| 96 | + exit(EXIT_FAILURE); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + db << "INSERT INTO foo VALUES (?)" << "invalid"; |
| 101 | + cout << "Unexpected success on line " << __LINE__ << endl; |
| 102 | + exit(EXIT_FAILURE); |
| 103 | + } catch(exceptions::readonly&) {} |
| 104 | + } |
| 105 | + } |
| 106 | + catch(sqlite_exception e) |
| 107 | + { |
| 108 | + cout << "Unexpected error " << e.what() << endl; |
| 109 | + exit(EXIT_FAILURE); |
| 110 | + } |
| 111 | + catch(...) |
| 112 | + { |
| 113 | + cout << "Unknown error\n"; |
| 114 | + exit(EXIT_FAILURE); |
| 115 | + } |
| 116 | + |
| 117 | + cout << "OK\n"; |
| 118 | + exit(EXIT_SUCCESS); |
| 119 | +} |
0 commit comments