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     immutable result = sqlite3_initialize();
65     enforce(result == SQLITE_OK,
66             new SqliteException("Initialization: error %s".format(result), result));
67 }
68 /// Ditto
69 void shutdown() {
70     immutable result = sqlite3_shutdown();
71     enforce(result == SQLITE_OK, new SqliteException("Shutdown: error %s".format(result), result));
72 }
73 
74 /++
75 Sets a configuration option.
76 
77 Use before initialization, e.g. before the first
78 call to initialize and before execution of the first statement.
79 
80 See_Also: $(LINK http://www.sqlite.org/c3ref/config.html).
81 +/
82 void config(Args...)(int code, Args args) {
83     immutable result = sqlite3_config(code, args);
84     enforce(result == SQLITE_OK,
85             new SqliteException("Configuration: error %s".format(result), result));
86 }
87 
88 /++
89 Tests if an SQLite compile option is set
90 
91 See_Also: $(LINK http://sqlite.org/c3ref/compileoption_get.html).
92 +/
93 bool isCompiledWith(string option) {
94     import std.string : toStringz;
95 
96     return cast(bool) sqlite3_compileoption_used(option.toStringz);
97 }
98 ///
99 version (SqliteEnableUnlockNotify) unittest {
100     assert(isCompiledWith("SQLITE_ENABLE_UNLOCK_NOTIFY"));
101     assert(!isCompiledWith("SQLITE_UNKNOWN_COMPILE_OPTION"));
102 }