16#define access(f,a) _access(f,a)
27static bool sql_enact(sqlite3 *db,
const char *cmd) {
29 int result = sqlite3_exec(db, cmd,
nullptr,
nullptr,
nullptr);
30 if (result!=SQLITE_OK) {
31 const char *msg = sqlite3_errmsg(db);
33 fprintf(stderr,
"Database error: %s\n", msg);
36 fprintf(stderr,
"Failed to set up database tables\n");
46 sqlite3 *db =
nullptr;
48 int result = access(filename,
F_OK);
50 fprintf(stderr,
"Database needs to be recreated.\n");
51 fprintf(stderr,
"Please move %s out of the way.\n", filename);
56 int result = sqlite3_open_v2(filename,
58 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX,
60 if (result!=SQLITE_OK) {
61 fprintf(stderr,
"Failed to open database %s\n", filename);
69 std::string create_main_table =
"CREATE TABLE IF NOT EXISTS tags (\n\
70 id INTEGER PRIMARY KEY,\n\
76 result = sqlite3_exec(db, create_main_table.c_str(),
nullptr,
nullptr,
nullptr);
77 if (result!=SQLITE_OK) {
78 const char *msg = sqlite3_errmsg(db);
80 fprintf(stderr,
"Error in %s: %s\n", filename, msg);
83 fprintf(stderr,
"Failed to set up database tables\n");
87 std::string cmd_synch = std::string(
"PRAGMA synchronous=") + (cautious?
"FULL":
"OFF") +
";";
90 sql_enact(db,
"CREATE INDEX IF NOT EXISTS tagsRidNameValue on tags(rid,name,value);");
99 if (accessor !=
nullptr) {
103 if (implementation !=
nullptr) {
104 auto* db = (sqlite3 *)implementation;
106 implementation =
nullptr;
static bool sql_enact(sqlite3 *db, const char *cmd)
Sqlite database, viewed as a collection of triples.
Abstract view of a database as a collection of triples.