From fa286f585eb10a0d59fa9a304e5432a1497d5ab9 Mon Sep 17 00:00:00 2001
From: Ralph Amissah <ralph.amissah@gmail.com>
Date: Fri, 9 Dec 2022 09:16:54 -0500
Subject: sqlite, check db exists, writable etc.

---
 src/doc_reform/io_out/sqlite.d | 214 +++++++++++++++++++++++++++++------------
 1 file changed, 153 insertions(+), 61 deletions(-)

(limited to 'src')

diff --git a/src/doc_reform/io_out/sqlite.d b/src/doc_reform/io_out/sqlite.d
index 33c25fe..7560bb1 100644
--- a/src/doc_reform/io_out/sqlite.d
+++ b/src/doc_reform/io_out/sqlite.d
@@ -72,53 +72,84 @@ template SQLiteHubBuildTablesAndPopulate() {
           M  doc_matters,
   ) {
     auto pth_sqlite = spinePathsSQLite!()(doc_matters.sqlite.filename, doc_matters.sqlite.path);
-    pth_sqlite.base.mkdirRecurse;
-    auto db = Database(pth_sqlite.sqlite_file);
+    if ((isValidPath(pth_sqlite.base) && exists(pth_sqlite.base) != 0 && pth_sqlite.base.isDir)) {
+    } else {
+      try {
+        pth_sqlite.base.mkdirRecurse;
+      } catch (FileException ex) { }
+    }
     template SQLiteDbStatementComposite() {
       void SQLiteDbStatementComposite(Db,D,M)(
               Db   db,
         const D    doc_abstraction,
               M    doc_matters,
       ) {
-        {
-          {
-            string _db_statement;
-            if ((doc_matters.opt.action.sqlite_db_create)) {
-              auto pth_sqlite = spinePathsSQLite!()(doc_matters.sqlite.filename, doc_matters.sqlite.path);
+        string _db_statement;
+        if ((doc_matters.opt.action.sqlite_db_create)) {
+          auto pth_sqlite = spinePathsSQLite!()(doc_matters.sqlite.filename, doc_matters.sqlite.path);
+          if ((isValidPath(pth_sqlite.base) && exists(pth_sqlite.base) != 0 && pth_sqlite.base.isDir)) {
+          } else {
+            try {
               pth_sqlite.base.mkdirRecurse;
-              _db_statement ~= SQLiteTablesReCreate!()();
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "TABLE RE-CREATE");
-              _db_statement = [];
-            }
-            if (doc_matters.opt.action.sqlite_delete) {
-              _db_statement ~= SQLiteDeleteDocument!()(doc_matters);
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "DELETE Document");
-              _db_statement = [];
-            }
-            if (doc_matters.opt.action.sqlite_update) {
-              _db_statement ~= SQLiteDeleteDocument!()(doc_matters);
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "DELETE Document");
-              _db_statement = [];
-              _db_statement ~= SQLiteInsertMetadata!()(doc_matters);
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT MetaData");
-              _db_statement = [];
-              /+ get tid (lastrowid or max) for use in doc_objects table +/
-              _db_statement ~= doc_abstraction.SQLiteInsertDocObjectsLoop!()(doc_matters);
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT DocObjects");
-              _db_statement = [];
-             _db_statement ~= SQLiteInsertMetadataTopics!()(doc_matters);
-              SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT MetaDataTopics");
-              _db_statement = [];
-            }
+            } catch (FileException ex) { }
           }
-          db.close;
+          _db_statement ~= SQLiteTablesReCreate!()();
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "TABLE RE-CREATE");
+          _db_statement = [];
+        }
+        if (doc_matters.opt.action.sqlite_delete) {
+          _db_statement ~= SQLiteDeleteDocument!()(doc_matters);
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "DELETE Document");
+          _db_statement = [];
         }
+        if (doc_matters.opt.action.sqlite_update) {
+          _db_statement ~= SQLiteDeleteDocument!()(doc_matters);
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "DELETE Document");
+          _db_statement = [];
+          _db_statement ~= SQLiteInsertMetadata!()(doc_matters);
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT MetaData");
+          _db_statement = [];
+          /+ get tid (lastrowid or max) for use in doc_objects table +/
+          _db_statement ~= doc_abstraction.SQLiteInsertDocObjectsLoop!()(doc_matters);
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT DocObjects");
+          _db_statement = [];
+         _db_statement ~= SQLiteInsertMetadataTopics!()(doc_matters);
+          SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "INSERT MetaDataTopics");
+          _db_statement = [];
+        }
+        db.close;
         if (doc_matters.opt.action.vox_gt0) {
           writeln(" ", pth_sqlite.sqlite_file);
         }
       }
     }
-    SQLiteDbStatementComposite!()(db, doc_abstraction, doc_matters);
+    try {
+      auto db = Database(pth_sqlite.sqlite_file);
+      SQLiteDbStatementComposite!()(db, doc_abstraction, doc_matters);
+    }
+    catch (FileException e) {
+      writeln("Failed (FileException): ", e.msg, " ", pth_sqlite.sqlite_file);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (ErrnoException e) {
+      writeln("Failed (ErrnoException): ", e.msg, " ", pth_sqlite.sqlite_file);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (Exception e) {
+      writeln("Failed (Exception): ", e.msg, " ", pth_sqlite.sqlite_file);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (Throwable) {
+      writeln("Failed (Trowable): ", pth_sqlite.sqlite_file);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
   }
 }
 template SQLiteHubDiscreteBuildTablesAndPopulate() {
@@ -128,7 +159,12 @@ template SQLiteHubDiscreteBuildTablesAndPopulate() {
   ) {
     auto url_html = spineUrlsHTML!()(doc_matters.conf_make_meta.conf.w_srv_data_root_url_html, doc_matters.src.language);
     auto pth_sqlite = spinePathsSQLiteDiscrete!()(doc_matters.output_path, doc_matters.src.language); // doc_matters.db_path
-    pth_sqlite.base.mkdirRecurse;
+    if ((isValidPath(pth_sqlite.base) && exists(pth_sqlite.base) != 0 && pth_sqlite.base.isDir)) {
+    } else {
+      try {
+        pth_sqlite.base.mkdirRecurse;
+      } catch (FileException ex) { }
+    }
     auto db = Database(pth_sqlite.sqlite_file(doc_matters.src.filename));
     template SQLiteDiscreteDbStatementComposite() {
       void SQLiteDiscreteDbStatementComposite(Db,D,M)(
@@ -136,7 +172,7 @@ template SQLiteHubDiscreteBuildTablesAndPopulate() {
         const D    doc_abstraction,
               M    doc_matters,
       ) {
-        {
+        try {
           {
             string _db_statement;
             _db_statement ~= SQLiteTablesReCreate!()();
@@ -147,6 +183,28 @@ template SQLiteHubDiscreteBuildTablesAndPopulate() {
           }
           db.close;
         }
+        catch (FileException e) {
+          writeln("Failed (FileException): ", e.msg);
+          writeln(e.file, " line: ", e.line);
+          import core.runtime;
+          core.runtime.Runtime.terminate();
+        }
+        catch (ErrnoException e) {
+          writeln("Failed (ErrnoException): ", e.msg);
+          writeln(e.file, " line: ", e.line);
+          import core.runtime;
+          core.runtime.Runtime.terminate();
+        }
+        catch (Exception e) {
+          writeln("Failed (Exception): ", e.msg);
+          writeln(e.file, " line: ", e.line);
+          import core.runtime;
+          core.runtime.Runtime.terminate();
+        }
+        catch (Throwable) {
+          import core.runtime;
+          core.runtime.Runtime.terminate();
+        }
         if (doc_matters.opt.action.vox_gt0) {
           writeln(" ", pth_sqlite.sqlite_file(doc_matters.src.filename));
         }
@@ -1607,35 +1665,69 @@ template SQLiteTablesCreate() {
         return _sql_instruct;
       }
     }
-    if (opt_action.sqlite_db_create) {
-      string _db_statement;
-      string db_filename = (opt_action.sqliteDB_filename.length > 0)
-      ? opt_action.sqliteDB_filename
-      : (config.conf.w_srv_db_sqlite_filename.length > 0)
-        ? config.conf.w_srv_db_sqlite_filename
-        : "";
-      string db_path = (opt_action.sqliteDB_path.length > 0)
-      ? opt_action.sqliteDB_path
-      : (config.conf.w_srv_db_sqlite_path.length > 0)
-        ? config.conf.w_srv_db_sqlite_path
-        : "";
-      if (db_filename.length > 0 && db_path.length > 0) {
-        if ((opt_action.vox_gt1)) {
-          writeln("db name & path: ", db_path, "/", db_filename);
-        }
-        auto pth_sqlite = spinePathsSQLite!()(db_filename, db_path);
-        pth_sqlite.base.mkdirRecurse;
-        auto db = Database(pth_sqlite.sqlite_file);
-        {
-          _db_statement ~= SQLiteTablesReCreate!()();
+    try {
+      if (opt_action.sqlite_db_create) {
+        string _db_statement;
+        string db_filename = (opt_action.sqliteDB_filename.length > 0)
+        ? opt_action.sqliteDB_filename
+        : (config.conf.w_srv_db_sqlite_filename.length > 0)
+          ? config.conf.w_srv_db_sqlite_filename
+          : "";
+        string db_path = (opt_action.sqliteDB_path.length > 0)
+        ? opt_action.sqliteDB_path
+        : (config.conf.w_srv_db_sqlite_path.length > 0)
+          ? config.conf.w_srv_db_sqlite_path
+          : "";
+        if (db_filename.length > 0 && db_path.length > 0) {
+          if (opt_action.vox_gt2) {
+            writeln("db name: ", db_filename);
+            writeln("db path: ", db_path);
+            writeln("db name & path: ", db_path, "/", db_filename);
+          }
+          if (opt_action.vox_gt1) {
+            writeln("attempting to create db: ", db_path, "/", db_filename);
+          }
+          auto pth_sqlite = spinePathsSQLite!()(db_filename, db_path);
+          if ((isValidPath(pth_sqlite.base) && exists(pth_sqlite.base) != 0 && pth_sqlite.base.isDir)) {
+          } else {
+            try {
+              pth_sqlite.base.mkdirRecurse;
+            } catch (FileException ex) { }
+          }
+          auto db = Database(pth_sqlite.sqlite_file);
+          {
+            _db_statement ~= SQLiteTablesReCreate!()();
+          }
+          SQLiteDbRun!()(db, _db_statement, opt_action, "TABLE RE-CREATE");
+        } else {
+          writeln("must provide db name & output root path either on the command line or in configuration file");
+          writeln("db name: ", db_filename);
+          writeln("db path: ", db_path);
         }
-        SQLiteDbRun!()(db, _db_statement, opt_action, "TABLE RE-CREATE");
-      } else {
-        writeln("must provide db name & output root path either on the command line or in configuration file");
-        writeln("db name: ", db_filename);
-        writeln("db path: ", db_path);
       }
     }
+    catch (FileException e) {
+      writeln("Failed (FileException): ", e.msg);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (ErrnoException e) {
+      writeln("Failed (ErrnoException):  ", e.msg);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (Exception e) {
+      writeln("Failed (Exception): ", e.msg);
+      writeln(e.file, " line: ", e.line);
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
+    catch (Throwable) {
+      import core.runtime;
+      core.runtime.Runtime.terminate();
+    }
   }
 }
 template SQLiteDbDrop() {
-- 
cgit v1.2.3