19 #define access(f,a) _access(f,a)
31 static bool sql_enact(sqlite3 *db,
const char *cmd) {
33 int result = sqlite3_exec(db, cmd,
nullptr,
nullptr,
nullptr);
34 if (result!=SQLITE_OK) {
35 const char *msg = sqlite3_errmsg(db);
37 fprintf(stderr,
"Database error: %s\n", msg);
40 fprintf(stderr,
"Failed to set up database tables\n");
50 sqlite3 *db =
nullptr;
52 int result = access(filename,
F_OK);
54 fprintf(stderr,
"Database needs to be recreated.\n");
55 fprintf(stderr,
"Please move %s out of the way.\n", filename);
60 int result = sqlite3_open_v2(filename,
62 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX,
64 if (result!=SQLITE_OK) {
65 fprintf(stderr,
"Failed to open database %s\n", filename);
73 string create_main_table =
"CREATE TABLE IF NOT EXISTS tags (\n\
74 id INTEGER PRIMARY KEY,\n\
80 result = sqlite3_exec(db, create_main_table.c_str(),
nullptr,
nullptr,
nullptr);
81 if (result!=SQLITE_OK) {
82 const char *msg = sqlite3_errmsg(db);
84 fprintf(stderr,
"Error in %s: %s\n", filename, msg);
87 fprintf(stderr,
"Failed to set up database tables\n");
91 string cmd_synch = string(
"PRAGMA synchronous=") + (cautious?
"FULL":
"OFF") +
";";
94 sql_enact(db,
"CREATE INDEX IF NOT EXISTS tagsRidNameValue on tags(rid,name,value);");
103 if (accessor !=
nullptr) {