|
| 1 | +// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*- |
| 2 | +// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi |
| 3 | +// |
| 4 | +// Copyright 2024 Mozilla Foundation |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include "db.h" |
| 19 | +#include <stdio.h> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +__static_yoink("llamafile/schema.sql"); |
| 23 | + |
| 24 | +#define SCHEMA_VERSION 1 |
| 25 | + |
| 26 | +namespace llamafile { |
| 27 | +namespace db { |
| 28 | + |
| 29 | +static bool table_exists(sqlite3* db, const char* table_name) { |
| 30 | + const char* query = "SELECT name FROM sqlite_master WHERE type='table' AND name=?;"; |
| 31 | + sqlite3_stmt* stmt; |
| 32 | + if (sqlite3_prepare_v2(db, query, -1, &stmt, nullptr) != SQLITE_OK) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + if (sqlite3_bind_text(stmt, 1, table_name, -1, SQLITE_STATIC) != SQLITE_OK) { |
| 36 | + sqlite3_finalize(stmt); |
| 37 | + return false; |
| 38 | + } |
| 39 | + bool exists = sqlite3_step(stmt) == SQLITE_ROW; |
| 40 | + sqlite3_finalize(stmt); |
| 41 | + return exists; |
| 42 | +} |
| 43 | + |
| 44 | +static bool init_schema(sqlite3* db) { |
| 45 | + FILE* f = fopen("/zip/llamafile/schema.sql", "r"); |
| 46 | + if (!f) |
| 47 | + return false; |
| 48 | + std::string schema; |
| 49 | + int c; |
| 50 | + while ((c = fgetc(f)) != EOF) |
| 51 | + schema += c; |
| 52 | + fclose(f); |
| 53 | + char* errmsg = nullptr; |
| 54 | + int rc = sqlite3_exec(db, schema.c_str(), nullptr, nullptr, &errmsg); |
| 55 | + if (rc != SQLITE_OK) { |
| 56 | + if (errmsg) { |
| 57 | + fprintf(stderr, "SQL error: %s\n", errmsg); |
| 58 | + sqlite3_free(errmsg); |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | +} |
| 64 | + |
| 65 | +sqlite3* open(const char* path) { |
| 66 | + sqlite3* db; |
| 67 | + int rc = sqlite3_open(path, &db); |
| 68 | + if (rc) { |
| 69 | + fprintf(stderr, "%s: can't open database: %s\n", path, sqlite3_errmsg(db)); |
| 70 | + return nullptr; |
| 71 | + } |
| 72 | + char* errmsg = nullptr; |
| 73 | + if (sqlite3_exec(db, "PRAGMA journal_mode=WAL;", nullptr, nullptr, &errmsg) != SQLITE_OK) { |
| 74 | + fprintf(stderr, "Failed to set journal mode to WAL: %s\n", errmsg); |
| 75 | + sqlite3_free(errmsg); |
| 76 | + sqlite3_close(db); |
| 77 | + return nullptr; |
| 78 | + } |
| 79 | + if (sqlite3_exec(db, "PRAGMA synchronous=NORMAL;", nullptr, nullptr, &errmsg) != SQLITE_OK) { |
| 80 | + fprintf(stderr, "Failed to set synchronous to NORMAL: %s\n", errmsg); |
| 81 | + sqlite3_free(errmsg); |
| 82 | + sqlite3_close(db); |
| 83 | + return nullptr; |
| 84 | + } |
| 85 | + if (!table_exists(db, "metadata") && !init_schema(db)) { |
| 86 | + fprintf(stderr, "%s: failed to initialize database schema\n", path); |
| 87 | + sqlite3_close(db); |
| 88 | + return nullptr; |
| 89 | + } |
| 90 | + return db; |
| 91 | +} |
| 92 | + |
| 93 | +void close(sqlite3* db) { |
| 94 | + sqlite3_close(db); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace db |
| 98 | +} // namespace llamafile |
0 commit comments