-dbopen(int real)
-{
- const char *sql;
- int rc, ofl;
-
- if (nodb)
- return(1);
-
- *tempfilename = '\0';
- ofl = SQLITE_OPEN_READWRITE;
-
- if (real) {
- rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
- if (SQLITE_OK != rc) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- if (SQLITE_CANTOPEN != rc)
- say(MANDOC_DB, "%s", sqlite3_errstr(rc));
- return(0);
- }
- goto prepare_statements;
- }
-
- ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
-
- remove(MANDOC_DB "~");
- rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
- if (SQLITE_OK == rc)
- goto create_tables;
- if (MPARSE_QUICK & mparse_options) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB "~", "%s", sqlite3_errstr(rc));
- return(0);
- }
-
- (void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
- sizeof(tempfilename));
- if (NULL == mkdtemp(tempfilename)) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say("", "&%s", tempfilename);
- return(0);
- }
- (void)strlcat(tempfilename, "/" MANDOC_DB,
- sizeof(tempfilename));
- rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
- if (SQLITE_OK != rc) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say("", "%s: %s", tempfilename, sqlite3_errstr(rc));
- return(0);
- }
-
-create_tables:
- sql = "CREATE TABLE \"mpages\" (\n"
- " \"desc\" TEXT NOT NULL,\n"
- " \"form\" INTEGER NOT NULL,\n"
- " \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
- ");\n"
- "\n"
- "CREATE TABLE \"mlinks\" (\n"
- " \"sec\" TEXT NOT NULL,\n"
- " \"arch\" TEXT NOT NULL,\n"
- " \"name\" TEXT NOT NULL,\n"
- " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
- "ON DELETE CASCADE\n"
- ");\n"
- "CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n"
- "\n"
- "CREATE TABLE \"names\" (\n"
- " \"bits\" INTEGER NOT NULL,\n"
- " \"name\" TEXT NOT NULL,\n"
- " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
- "ON DELETE CASCADE\n"
- ");\n"
- "\n"
- "CREATE TABLE \"keys\" (\n"
- " \"bits\" INTEGER NOT NULL,\n"
- " \"key\" TEXT NOT NULL,\n"
- " \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) "
- "ON DELETE CASCADE\n"
- ");\n"
- "CREATE INDEX keys_pageid_idx ON keys (pageid);\n";
-
- if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB, "%s", sqlite3_errmsg(db));
- sqlite3_close(db);
- return(0);
- }
-
-prepare_statements:
- if (SQLITE_OK != sqlite3_exec(db,
- "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB, "PRAGMA foreign_keys: %s",
- sqlite3_errmsg(db));
- sqlite3_close(db);
- return(0);
- }
-
- sql = "DELETE FROM mpages WHERE pageid IN "
- "(SELECT pageid FROM mlinks WHERE "
- "sec=? AND arch=? AND name=?)";
- sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
- sql = "INSERT INTO mpages "
- "(desc,form) VALUES (?,?)";
- sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
- sql = "INSERT INTO mlinks "
- "(sec,arch,name,pageid) VALUES (?,?,?,?)";
- sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
- sql = "INSERT INTO names "
- "(bits,name,pageid) VALUES (?,?,?)";
- sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL);
- sql = "INSERT INTO keys "
- "(bits,key,pageid) VALUES (?,?,?)";
- sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
-
-#ifndef __APPLE__
- /*
- * When opening a new database, we can turn off
- * synchronous mode for much better performance.
- */
-
- if (real && SQLITE_OK != sqlite3_exec(db,
- "PRAGMA synchronous = OFF", NULL, NULL, NULL)) {
- exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB, "PRAGMA synchronous: %s",
- sqlite3_errmsg(db));
- sqlite3_close(db);
- return(0);
- }
-#endif
-
- return(1);
-}
-
-static void *
-hash_calloc(size_t n, size_t sz, void *arg)
-{
-
- return(mandoc_calloc(n, sz));
-}
-
-static void *
-hash_alloc(size_t sz, void *arg)
-{
-
- return(mandoc_malloc(sz));
-}
-
-static void
-hash_free(void *p, void *arg)
-{
-
- free(p);
-}
-
-static int
-set_basedir(const char *targetdir)