dbtools logging

This commit is contained in:
MrFry 2020-04-07 15:09:54 +02:00
parent dbb23a6724
commit db6a7bd73b

View file

@ -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)
}