From fa286f585eb10a0d59fa9a304e5432a1497d5ab9 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Fri, 9 Dec 2022 09:16:54 -0500 Subject: sqlite, check db exists, writable etc. --- org/out_sqlite.org | 179 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 133 insertions(+), 46 deletions(-) (limited to 'org') diff --git a/org/out_sqlite.org b/org/out_sqlite.org index a90e6fd..c01db16 100644 --- a/org/out_sqlite.org +++ b/org/out_sqlite.org @@ -86,8 +86,12 @@ 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, @@ -100,7 +104,33 @@ template SQLiteHubBuildTablesAndPopulate() { } } } - 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(); + } } } #+END_SRC @@ -117,7 +147,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)( @@ -125,7 +160,31 @@ template SQLiteHubDiscreteBuildTablesAndPopulate() { const D doc_abstraction, M doc_matters, ) { - <> + try { + <> + } + 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)); } @@ -349,7 +408,31 @@ template SQLiteTablesCreate() { return _sql_instruct; } } - <> + try { + <> + } + 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(); + } } } #+END_SRC @@ -381,7 +464,12 @@ if (opt_action.sqlite_db_create) { writeln("attempting to create db: ", db_path, "/", db_filename); } auto pth_sqlite = spinePathsSQLite!()(db_filename, 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); { _db_statement ~= SQLiteTablesReCreate!()(); @@ -436,39 +524,40 @@ template SQLiteDbDrop() { #+NAME: sqlite_db_statement_composite_collection #+BEGIN_SRC d -{ - { - 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; #+END_SRC *** discrete @@ -476,16 +565,14 @@ template SQLiteDbDrop() { #+NAME: sqlite_db_statement_composite_discrete #+BEGIN_SRC d { - { - string _db_statement; - _db_statement ~= SQLiteTablesReCreate!()(); - _db_statement ~= SQLiteInsertMetadata!()(doc_matters); - _db_statement ~= SQLiteInsertMetadataTopics!()(doc_matters); - _db_statement ~= doc_abstraction.SQLiteInsertDocObjectsLoop!()(doc_matters); - SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "table CREATE Tables, INSERT DocObjects"); - } - db.close; + string _db_statement; + _db_statement ~= SQLiteTablesReCreate!()(); + _db_statement ~= SQLiteInsertMetadata!()(doc_matters); + _db_statement ~= SQLiteInsertMetadataTopics!()(doc_matters); + _db_statement ~= doc_abstraction.SQLiteInsertDocObjectsLoop!()(doc_matters); + SQLiteDbRun!()(db, _db_statement, doc_matters.opt.action, "table CREATE Tables, INSERT DocObjects"); } +db.close; #+END_SRC ** 2. imports -- cgit v1.2.3