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 {
32     import std.conv : to;
33     return sqlite3_libversion().to!string;
34 }
35 
36 /// Ditto
37 int versionNumber() nothrow
38 {
39     return sqlite3_libversion_number();
40 }
41 
42 /// Ditto
43 string sourceID()
44 {
45     import std.conv : to;
46     return sqlite3_sourceid().to!string;
47 }
48 
49 /++
50 Tells whether SQLite was compiled with the thread-safe options.
51 
52 See_also: $(LINK http://www.sqlite.org/c3ref/threadsafe.html).
53 +/
54 bool threadSafe() nothrow
55 {
56     return cast(bool) sqlite3_threadsafe();
57 }
58 
59 /++
60 Manually initializes (or shuts down) SQLite.
61 
62 SQLite initializes itself automatically on the first request execution, so this
63 usually wouldn't be called. Use for instance before a call to config().
64 +/
65 void initialize()
66 {
67     immutable result = sqlite3_initialize();
68     enforce(result == SQLITE_OK, new SqliteException("Initialization: error %s".format(result), result));
69 }
70 /// Ditto
71 void shutdown()
72 {
73     immutable result = sqlite3_shutdown();
74     enforce(result == SQLITE_OK, new SqliteException("Shutdown: error %s".format(result), result));
75 }
76 
77 /++
78 Sets a configuration option.
79 
80 Use before initialization, e.g. before the first
81 call to initialize and before execution of the first statement.
82 
83 See_Also: $(LINK http://www.sqlite.org/c3ref/config.html).
84 +/
85 void config(Args...)(int code, Args args)
86 {
87     immutable result = sqlite3_config(code, args);
88     enforce(result == SQLITE_OK, new SqliteException("Configuration: error %s".format(result), result));
89 }
90 
91 /++
92 Tests if an SQLite compile option is set
93 
94 See_Also: $(LINK http://sqlite.org/c3ref/compileoption_get.html).
95 +/
96 bool isCompiledWith(string option)
97 {
98     import std.string : toStringz;
99     return cast(bool) sqlite3_compileoption_used(option.toStringz);
100 }
101 ///
102 version (SqliteEnableUnlockNotify)
103 unittest
104 {
105     assert(isCompiledWith("SQLITE_ENABLE_UNLOCK_NOTIFY"));
106     assert(!isCompiledWith("SQLITE_UNKNOWN_COMPILE_OPTION"));
107 }