summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-07-16 15:58:38 +0200
committerKonstantin Käfer <mail@kkaefer.com>2018-07-16 15:58:38 +0200
commit5d7a935784a6c0d126a0365e01a4098074470753 (patch)
treef1527e5df8cee6d0764723783d7d52553c9c3996
parenta6727921fc1608d341c086cd70341e6df5e79a2b (diff)
downloadqtlocation-mapboxgl-upstream/make-offline-schema-a-regular-include-header.tar.gz
[core] make the offline DB schema a regular include fileupstream/make-offline-schema-a-regular-include-header
-rw-r--r--cmake/filesource.cmake1
-rw-r--r--platform/default/mbgl/storage/offline_database.cpp6
-rw-r--r--platform/default/mbgl/storage/offline_schema.hpp (renamed from platform/default/mbgl/storage/offline_schema.cpp.include)12
-rw-r--r--platform/default/mbgl/storage/offline_schema.js37
4 files changed, 31 insertions, 25 deletions
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 <mbgl/util/chrono.hpp>
#include <mbgl/util/logging.hpp>
+#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.hpp
index 41af81e55b..e177d0dbd3 100644
--- a/platform/default/mbgl/storage/offline_schema.cpp.include
+++ b/platform/default/mbgl/storage/offline_schema.hpp
@@ -1,5 +1,11 @@
-/* THIS IS A GENERATED FILE; EDIT offline_schema.sql INSTEAD */
-static const char * schema =
+#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"
@@ -53,3 +59,5 @@ static const char * schema =
"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'));
- });