1 /++
2 Miscellaneous SQLite3 library functions.
3 
4 Authors:
5     Nicolas Sicard (biozic) and other contributors at $(LINK https://github.com/biozic/d2sqlite3)
6 
7 Copyright:
8     Copyright 2011-18 Nicolas Sicard.
9 
10 License:
11     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
12 +/
13 module d2sqlite3.library;
14 
15 import d2sqlite3.sqlite3;
16 import d2sqlite3.database : SqliteException;
17 import std.exception : enforce;
18 import std.string : format;
19 
20 /++
21 Gets the library's version string (e.g. "3.8.7"), version number (e.g. 3_008_007)
22 or source ID.
23 
24 These values are returned by the linked SQLite C library. They can be checked against
25 the values of the enums defined by the `d2sqlite3` package (`SQLITE_VERSION`,
26 `SQLITE_VERSION_NUMBER` and `SQLITE_SOURCE_ID`).
27 
28 See_Also: $(LINK http://www.sqlite.org/c3ref/libversion.html).
29 +/
30 string versionString() {
31     import std.conv : to;
32 
33     return sqlite3_libversion().to!string;
34 }
35 
36 /// Ditto
37 int versionNumber() nothrow {
38     return sqlite3_libversion_number();
39 }
40 
41 /// Ditto
42 string sourceID() {
43     import std.conv : to;
44 
45     return sqlite3_sourceid().to!string;
46 }
47 
48 /++
49 Tells whether SQLite was compiled with the thread-safe options.
50 
51 See_also: $(LINK http://www.sqlite.org/c3ref/threadsafe.html).
52 +/
53 bool threadSafe() nothrow {
54     return cast(bool) sqlite3_threadsafe();
55 }
56 
57 /++
58 Manually initializes (or shuts down) SQLite.
59 
60 SQLite initializes itself automatically on the first request execution, so this
61 usually wouldn't be called. Use for instance before a call to config().
62 +/
63 void initialize() {
64     auto result = sqlite3_initialize();
65     enforce(result == SQLITE_OK, new SqliteException("Initialization: error %s".format(result)));
66 }
67 /// Ditto
68 void shutdown() {
69     auto result = sqlite3_shutdown();
70     enforce(result == SQLITE_OK, new SqliteException("Shutdown: error %s".format(result)));
71 }
72 
73 /++
74 Sets a configuration option.
75 
76 Use before initialization, e.g. before the first
77 call to initialize and before execution of the first statement.
78 
79 See_Also: $(LINK http://www.sqlite.org/c3ref/config.html).
80 +/
81 void config(Args...)(int code, Args args) {
82     auto result = sqlite3_config(code, args);
83     enforce(result == SQLITE_OK, new SqliteException("Configuration: error %s".format(result)));
84 }
85 
86 /++
87 Tests if an SQLite compile option is set
88 
89 See_Also: $(LINK http://sqlite.org/c3ref/compileoption_get.html).
90 +/
91 bool isCompiledWith(string option) {
92     import std.string : toStringz;
93 
94     return cast(bool) sqlite3_compileoption_used(option.toStringz);
95 }
96 ///
97 version (SqliteEnableUnlockNotify) unittest {
98     assert(isCompiledWith("SQLITE_ENABLE_UNLOCK_NOTIFY"));
99     assert(!isCompiledWith("SQLITE_UNKNOWN_COMPILE_OPTION"));
100 }