From db6a7bd73b3ad0d1dcf2a92d5c5fc054b02e42cd Mon Sep 17 00:00:00 2001 From: MrFry Date: Tue, 7 Apr 2020 15:09:54 +0200 Subject: [PATCH] dbtools logging --- utils/dbtools.js | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/utils/dbtools.js b/utils/dbtools.js index fcabba6..917952e 100644 --- a/utils/dbtools.js +++ b/utils/dbtools.js @@ -59,8 +59,7 @@ function AddColumn (db, table, col) { const colType = col.type const s = `ALTER TABLE ${table} ADD COLUMN ${colName} ${colType}` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.run() } catch (e) { @@ -75,14 +74,12 @@ function TableInfo (db, table) { console.error(e) } const s = `PRAGMA table_info(${table})` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) const infoRes = stmt.all() const s2 = `SELECT COUNT(*) FROM ${table}` - DebugLog(s2) - const stmt2 = db.prepare(s2) + const stmt2 = PrepareStatement(db, s2) const countRes = stmt2.get() @@ -95,8 +92,7 @@ function TableInfo (db, table) { function Update (db, table, newData, conditions) { try { const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.run() } catch (e) { @@ -107,8 +103,7 @@ function Update (db, table, newData, conditions) { function Delete (db, table, conditions) { try { const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.run() } catch (e) { @@ -154,9 +149,7 @@ function CreateTable (db, name, columns, foreignKeys) { // IF NOT EXISTS const s = `CREATE TABLE ${name}(${cols}${fKeys.join(', ')})` - DebugLog(s) - - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.run() } catch (e) { console.error(e) @@ -166,9 +159,8 @@ function CreateTable (db, name, columns, foreignKeys) { function SelectAll (db, from) { try { const s = `SELECT * from ${from}` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.all() } catch (e) { console.error(e) @@ -178,9 +170,8 @@ function SelectAll (db, from) { function Select (db, from, conditions) { try { const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.all() } catch (e) { console.error(e) @@ -205,8 +196,7 @@ function Insert (db, table, data) { }, []).join(', ') const s = `INSERT INTO ${table} (${cols}) VALUES (${values})` - DebugLog(s) - const stmt = db.prepare(s) + const stmt = PrepareStatement(db, s) return stmt.run() } catch (e) { @@ -222,3 +212,10 @@ function CloseDB (db) { DebugLog('Close the database connection.') }) } + +// ------------------------------------------------------------------------- + +function PrepareStatement (db, s) { + DebugLog(s) + return db.prepare(s) +}