From 5d7a935784a6c0d126a0365e01a4098074470753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 16 Jul 2018 15:58:38 +0200 Subject: [core] make the offline DB schema a regular include file --- cmake/filesource.cmake | 1 + platform/default/mbgl/storage/offline_database.cpp | 6 +-- .../mbgl/storage/offline_schema.cpp.include | 55 ------------------- platform/default/mbgl/storage/offline_schema.hpp | 63 ++++++++++++++++++++++ platform/default/mbgl/storage/offline_schema.js | 37 ++++++------- 5 files changed, 84 insertions(+), 78 deletions(-) delete mode 100644 platform/default/mbgl/storage/offline_schema.cpp.include create mode 100644 platform/default/mbgl/storage/offline_schema.hpp diff --git a/cmake/filesource.cmake b/cmake/filesource.cmake index 861a845d6e..5dc706340f 100644 --- a/cmake/filesource.cmake +++ b/cmake/filesource.cmake @@ -19,6 +19,7 @@ add_library(mbgl-filesource STATIC platform/default/mbgl/storage/offline_database.cpp platform/default/mbgl/storage/offline_download.hpp platform/default/mbgl/storage/offline_download.cpp + platform/default/mbgl/storage/offline_schema.hpp # Database platform/default/sqlite3.hpp diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp index 92e534634d..8f7f0965f4 100644 --- a/platform/default/mbgl/storage/offline_database.cpp +++ b/platform/default/mbgl/storage/offline_database.cpp @@ -6,6 +6,8 @@ #include #include +#include "offline_schema.hpp" + #include "sqlite3.hpp" namespace mbgl { @@ -82,8 +84,6 @@ void OfflineDatabase::ensureSchema() { } try { - #include "offline_schema.cpp.include" - // When downgrading the database, or when the database is corrupt, we've deleted the old database handle, // so we need to reopen it. if (!db) { @@ -95,7 +95,7 @@ void OfflineDatabase::ensureSchema() { db->exec("PRAGMA auto_vacuum = INCREMENTAL"); db->exec("PRAGMA journal_mode = DELETE"); db->exec("PRAGMA synchronous = FULL"); - db->exec(schema); + db->exec(offlineDatabaseSchema); db->exec("PRAGMA user_version = 6"); } catch (...) { Log::Error(Event::Database, "Unexpected error creating database schema: %s", util::toString(std::current_exception()).c_str()); diff --git a/platform/default/mbgl/storage/offline_schema.cpp.include b/platform/default/mbgl/storage/offline_schema.cpp.include deleted file mode 100644 index 41af81e55b..0000000000 --- a/platform/default/mbgl/storage/offline_schema.cpp.include +++ /dev/null @@ -1,55 +0,0 @@ -/* THIS IS A GENERATED FILE; EDIT offline_schema.sql INSTEAD */ -static const char * schema = -"CREATE TABLE resources (\n" -" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" -" url TEXT NOT NULL,\n" -" kind INTEGER NOT NULL,\n" -" expires INTEGER,\n" -" modified INTEGER,\n" -" etag TEXT,\n" -" data BLOB,\n" -" compressed INTEGER NOT NULL DEFAULT 0,\n" -" accessed INTEGER NOT NULL,\n" -" must_revalidate INTEGER NOT NULL DEFAULT 0,\n" -" UNIQUE (url)\n" -");\n" -"CREATE TABLE tiles (\n" -" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" -" url_template TEXT NOT NULL,\n" -" pixel_ratio INTEGER NOT NULL,\n" -" z INTEGER NOT NULL,\n" -" x INTEGER NOT NULL,\n" -" y INTEGER NOT NULL,\n" -" expires INTEGER,\n" -" modified INTEGER,\n" -" etag TEXT,\n" -" data BLOB,\n" -" compressed INTEGER NOT NULL DEFAULT 0,\n" -" accessed INTEGER NOT NULL,\n" -" must_revalidate INTEGER NOT NULL DEFAULT 0,\n" -" UNIQUE (url_template, pixel_ratio, z, x, y)\n" -");\n" -"CREATE TABLE regions (\n" -" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" -" definition TEXT NOT NULL,\n" -" description BLOB\n" -");\n" -"CREATE TABLE region_resources (\n" -" region_id INTEGER NOT NULL REFERENCES regions(id) ON DELETE CASCADE,\n" -" resource_id INTEGER NOT NULL REFERENCES resources(id),\n" -" UNIQUE (region_id, resource_id)\n" -");\n" -"CREATE TABLE region_tiles (\n" -" region_id INTEGER NOT NULL REFERENCES regions(id) ON DELETE CASCADE,\n" -" tile_id INTEGER NOT NULL REFERENCES tiles(id),\n" -" UNIQUE (region_id, tile_id)\n" -");\n" -"CREATE INDEX resources_accessed\n" -"ON resources (accessed);\n" -"CREATE INDEX tiles_accessed\n" -"ON tiles (accessed);\n" -"CREATE INDEX region_resources_resource_id\n" -"ON region_resources (resource_id);\n" -"CREATE INDEX region_tiles_tile_id\n" -"ON region_tiles (tile_id);\n" -; diff --git a/platform/default/mbgl/storage/offline_schema.hpp b/platform/default/mbgl/storage/offline_schema.hpp new file mode 100644 index 0000000000..e177d0dbd3 --- /dev/null +++ b/platform/default/mbgl/storage/offline_schema.hpp @@ -0,0 +1,63 @@ +#pragma once + +// THIS IS A GENERATED FILE; EDIT offline_schema.sql INSTEAD +// To regenerate, run `node platform/default/mbgl/storage/offline_schema.js` + +namespace mbgl { + +static constexpr const char* offlineDatabaseSchema = +"CREATE TABLE resources (\n" +" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +" url TEXT NOT NULL,\n" +" kind INTEGER NOT NULL,\n" +" expires INTEGER,\n" +" modified INTEGER,\n" +" etag TEXT,\n" +" data BLOB,\n" +" compressed INTEGER NOT NULL DEFAULT 0,\n" +" accessed INTEGER NOT NULL,\n" +" must_revalidate INTEGER NOT NULL DEFAULT 0,\n" +" UNIQUE (url)\n" +");\n" +"CREATE TABLE tiles (\n" +" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +" url_template TEXT NOT NULL,\n" +" pixel_ratio INTEGER NOT NULL,\n" +" z INTEGER NOT NULL,\n" +" x INTEGER NOT NULL,\n" +" y INTEGER NOT NULL,\n" +" expires INTEGER,\n" +" modified INTEGER,\n" +" etag TEXT,\n" +" data BLOB,\n" +" compressed INTEGER NOT NULL DEFAULT 0,\n" +" accessed INTEGER NOT NULL,\n" +" must_revalidate INTEGER NOT NULL DEFAULT 0,\n" +" UNIQUE (url_template, pixel_ratio, z, x, y)\n" +");\n" +"CREATE TABLE regions (\n" +" id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +" definition TEXT NOT NULL,\n" +" description BLOB\n" +");\n" +"CREATE TABLE region_resources (\n" +" region_id INTEGER NOT NULL REFERENCES regions(id) ON DELETE CASCADE,\n" +" resource_id INTEGER NOT NULL REFERENCES resources(id),\n" +" UNIQUE (region_id, resource_id)\n" +");\n" +"CREATE TABLE region_tiles (\n" +" region_id INTEGER NOT NULL REFERENCES regions(id) ON DELETE CASCADE,\n" +" tile_id INTEGER NOT NULL REFERENCES tiles(id),\n" +" UNIQUE (region_id, tile_id)\n" +");\n" +"CREATE INDEX resources_accessed\n" +"ON resources (accessed);\n" +"CREATE INDEX tiles_accessed\n" +"ON tiles (accessed);\n" +"CREATE INDEX region_resources_resource_id\n" +"ON region_resources (resource_id);\n" +"CREATE INDEX region_tiles_tile_id\n" +"ON region_tiles (tile_id);\n" +; + +} // namespace mbgl diff --git a/platform/default/mbgl/storage/offline_schema.js b/platform/default/mbgl/storage/offline_schema.js index 153ba34e38..fdb7dc6405 100644 --- a/platform/default/mbgl/storage/offline_schema.js +++ b/platform/default/mbgl/storage/offline_schema.js @@ -1,24 +1,21 @@ -// To regenerate: -// (cd platform/default/mbgl/storage && node offline_schema.js) - var fs = require('fs'); -var readline = require('readline'); +fs.writeFileSync('platform/default/mbgl/storage/offline_schema.hpp', `#pragma once + +// THIS IS A GENERATED FILE; EDIT offline_schema.sql INSTEAD +// To regenerate, run \`node platform/default/mbgl/storage/offline_schema.js\` + +namespace mbgl { -var lineReader = readline.createInterface({ - input: fs.createReadStream('offline_schema.sql') -}); +static constexpr const char* offlineDatabaseSchema = +${fs.readFileSync('platform/default/mbgl/storage/offline_schema.sql', 'utf8') + .replace(/ *--.*/g, '') + .split('\n') + .filter(a => a) + .map(line => '"' + line + '\\n"') + .join('\n') +} +; -var lines = [ - "/* THIS IS A GENERATED FILE; EDIT offline_schema.sql INSTEAD */", - "static const char * schema = ", -]; +} // namespace mbgl +`); -lineReader - .on('line', function (line) { - line = line.replace(/ *--.*/, ''); - if (line) lines.push('"' + line + '\\n"'); - }) - .on('close', function () { - lines.push(';\n'); - fs.writeFileSync('offline_schema.cpp.include', lines.join('\n')); - }); -- cgit v1.2.1