From 387a1da4e83d24e82cfba5bebc3c98c85e824820 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 8 May 2020 12:28:28 +0200
Subject: [PATCH] Do things in a better way to init the database

---
 meson.build                               |  6 +++-
 scripts/init.sql => src/database/disk.sql |  0
 src/database/memory.sql                   | 36 ++++++++++++++++++++
 src/database/open.c                       | 40 +++--------------------
 4 files changed, 45 insertions(+), 37 deletions(-)
 rename scripts/init.sql => src/database/disk.sql (100%)
 create mode 100644 src/database/memory.sql

diff --git a/meson.build b/meson.build
index a86a9e9c..1c1a4b81 100644
--- a/meson.build
+++ b/meson.build
@@ -85,7 +85,11 @@ mthread_deps = [ declare_dependency( link_with: static_library( 'mthread'
 generated_deps = [ declare_dependency( link_with: static_library( 'generated'
                                                                 , [ custom_target( 'sqlinit'
                                                                                  , output: 'sqlinit.c'
-                                                                                 , input: 'scripts/init.sql'
+                                                                                 , input: 'src/database/disk.sql'
+                                                                                 , command: [ find_program('xxd'), '-i', '@INPUT@', '@OUTPUT@' ] ) ]
+                                                                , [ custom_target( 'sqlmemory'
+                                                                                 , output: 'sqlmemory.c'
+                                                                                 , input: 'src/database/memory.sql'
                                                                                  , command: [ find_program('xxd'), '-i', '@INPUT@', '@OUTPUT@' ] ) ]
                                                                 , [ custom_target( 'manpath'
                                                                                  , output: 'manpath.c'
diff --git a/scripts/init.sql b/src/database/disk.sql
similarity index 100%
rename from scripts/init.sql
rename to src/database/disk.sql
diff --git a/src/database/memory.sql b/src/database/memory.sql
new file mode 100644
index 00000000..5a36f3a9
--- /dev/null
+++ b/src/database/memory.sql
@@ -0,0 +1,36 @@
+--   This schema is used to initialize the in-memory database.
+
+-- Some notes about the queue_state table
+-- - There's one and only row.
+-- - `paused` 0 = play, 1 = paused
+-- - `random` whether the queue is played randomly
+-- - `repeat` whether the queue loops
+-- - `single` whether only one kara loops
+-- - `current` the position in the queue of the kara being played
+-- - `elapsed` the number of seconds from the beginning of the current kara
+-- - `duration` the total duration of the playing kara
+
+CREATE TABLE IF NOT EXISTS queue_state
+  ( id         INTEGER PRIMARY KEY DEFAULT 42 CHECK(id = 42)
+  , volume     INTEGER NOT NULL DEFAULT 100 CHECK(0 <= volume AND volume <= 100)
+  , paused     INTEGER NOT NULL DEFAULT 1
+  , random     INTEGER NOT NULL DEFAULT 0
+  , repeat     INTEGER NOT NULL DEFAULT 0
+  , single     INTEGER NOT NULL DEFAULT 0
+  , consume    INTEGER NOT NULL DEFAULT 0
+  , current    INTEGER CHECK(current > 0)
+  , duration   INTEGER CHECK(duration >= 0)
+  , elapsed    INTEGER CHECK(elapsed >= 0)
+  );
+
+INSERT INTO queue_state (id) VALUES (42);
+
+
+-- Used to store the content of the ini configuration file.
+
+CREATE TABLE IF NOT EXISTS config
+  ( section    TEXT NOT NULL
+  , key        TEXT NOT NULL
+  , value      TEXT
+  , PRIMARY KEY (section, key)
+  ) WITHOUT ROWID;
diff --git a/src/database/open.c b/src/database/open.c
index 9c288868..3021a6e1 100644
--- a/src/database/open.c
+++ b/src/database/open.c
@@ -7,40 +7,8 @@
 #include <string.h>
 #include <strings.h>
 
-/* Some notes:
-   - There's one and only row.
-   - `paused` 0 = play, 1 = paused
-   - `random` whether the queue is played randomly
-   - `repeat` whether the queue loops
-   - `single` whether only one kara loops
-   - `current` the position in the queue of the kara being played
-   - `elapsed` the number of seconds from the beginning of the current kara
-   - `duration` the total duration of the playing kara
-   This schema is used to initialize the in-memory database. */
-static const char *const SQL_MEM_SCHEM =
-    "CREATE TABLE IF NOT EXISTS queue_state"
-    "  ( id         INTEGER PRIMARY KEY DEFAULT 42 CHECK(id = 42)"
-    "  , volume     INTEGER NOT NULL DEFAULT 100 CHECK(0 <= volume AND volume <= 100)"
-    "  , paused     INTEGER NOT NULL DEFAULT 1"
-    "  , random     INTEGER NOT NULL DEFAULT 0"
-    "  , repeat     INTEGER NOT NULL DEFAULT 0"
-    "  , single     INTEGER NOT NULL DEFAULT 0"
-    "  , consume    INTEGER NOT NULL DEFAULT 0"
-    "  , current    INTEGER CHECK(current > 0)"
-    "  , duration   INTEGER CHECK(duration >= 0)"
-    "  , elapsed    INTEGER CHECK(elapsed >= 0)"
-    "  );\n"
-    "INSERT INTO queue_state (id) VALUES (42);\n"
-    "CREATE TABLE IF NOT EXISTS config"
-    "  ( section    TEXT NOT NULL"
-    "  , key        TEXT NOT NULL"
-    "  , value      TEXT"
-    "  , PRIMARY KEY (section, key)"
-    "  ) WITHOUT ROWID;\n";
-
-/* Should be, defined scripts_init_sql and its length scripts_init_sql_len */
-extern unsigned char ___scripts_init_sql[];
-extern int ___scripts_init_sql_len;
+extern unsigned char ___src_database_disk_sql[];
+extern unsigned char ___src_database_memory_sql[];
 
 #define INVALID_CHARS_DBPATH    ":?!'\""
 #define HEAP_LIMIT_SOFT         100 * 1024 * 1024
@@ -64,7 +32,7 @@ database_new(volatile sqlite3 **db)
     RETURN_IF(sqlite3_soft_heap_limit64(HEAP_LIMIT_SOFT) < 0, "Failed to set soft heap limit", false);
     RETURN_IF(sqlite3_hard_heap_limit64(HEAP_LIMIT_HARD) < 0, "Failed to set soft heap limit", false);
     RETURN_IF(SQLITE_OK != sqlite3_open_v2(":memory:", (sqlite3 **) db, flags, NULL), "Failed to open :memory:", false);
-    SQLITE_EXEC(*db, SQL_MEM_SCHEM, err_not_init);
+    SQLITE_EXEC(*db, (const char *) ___src_database_memory_sql, err_not_init);
     return true;
 err_not_init:
     *db = NULL;
@@ -182,7 +150,7 @@ database_init(const char *dbpath)
 {
     sqlite3 *db;
     GOTO_IF(SQLITE_OK != sqlite3_open(dbpath, &db), "Failed to open the database", error);
-    SQLITE_EXEC(db, (const char *) ___scripts_init_sql, error);
+    SQLITE_EXEC(db, (const char *) ___src_database_disk_sql, error);
     LOG_INFO_SCT("DB", "Initialized the 'disk' database successfully, path was '%s'", dbpath);
     return true;
 error:
-- 
GitLab