mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
dbtools logging
This commit is contained in:
parent
dbb23a6724
commit
db6a7bd73b
1 changed files with 16 additions and 19 deletions
|
@ -59,8 +59,7 @@ function AddColumn (db, table, col) {
|
||||||
const colType = col.type
|
const colType = col.type
|
||||||
|
|
||||||
const s = `ALTER TABLE ${table} ADD COLUMN ${colName} ${colType}`
|
const s = `ALTER TABLE ${table} ADD COLUMN ${colName} ${colType}`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
const stmt = db.prepare(s)
|
|
||||||
|
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -75,14 +74,12 @@ function TableInfo (db, table) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
}
|
}
|
||||||
const s = `PRAGMA table_info(${table})`
|
const s = `PRAGMA table_info(${table})`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
const stmt = db.prepare(s)
|
|
||||||
|
|
||||||
const infoRes = stmt.all()
|
const infoRes = stmt.all()
|
||||||
|
|
||||||
const s2 = `SELECT COUNT(*) FROM ${table}`
|
const s2 = `SELECT COUNT(*) FROM ${table}`
|
||||||
DebugLog(s2)
|
const stmt2 = PrepareStatement(db, s2)
|
||||||
const stmt2 = db.prepare(s2)
|
|
||||||
|
|
||||||
const countRes = stmt2.get()
|
const countRes = stmt2.get()
|
||||||
|
|
||||||
|
@ -95,8 +92,7 @@ function TableInfo (db, table) {
|
||||||
function Update (db, table, newData, conditions) {
|
function Update (db, table, newData, conditions) {
|
||||||
try {
|
try {
|
||||||
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
const stmt = db.prepare(s)
|
|
||||||
|
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -107,8 +103,7 @@ function Update (db, table, newData, conditions) {
|
||||||
function Delete (db, table, conditions) {
|
function Delete (db, table, conditions) {
|
||||||
try {
|
try {
|
||||||
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
const stmt = db.prepare(s)
|
|
||||||
|
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -154,9 +149,7 @@ function CreateTable (db, name, columns, foreignKeys) {
|
||||||
|
|
||||||
// IF NOT EXISTS
|
// IF NOT EXISTS
|
||||||
const s = `CREATE TABLE ${name}(${cols}${fKeys.join(', ')})`
|
const s = `CREATE TABLE ${name}(${cols}${fKeys.join(', ')})`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
|
|
||||||
const stmt = db.prepare(s)
|
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
@ -166,9 +159,8 @@ function CreateTable (db, name, columns, foreignKeys) {
|
||||||
function SelectAll (db, from) {
|
function SelectAll (db, from) {
|
||||||
try {
|
try {
|
||||||
const s = `SELECT * from ${from}`
|
const s = `SELECT * from ${from}`
|
||||||
DebugLog(s)
|
|
||||||
|
|
||||||
const stmt = db.prepare(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
return stmt.all()
|
return stmt.all()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
@ -178,9 +170,8 @@ function SelectAll (db, from) {
|
||||||
function Select (db, from, conditions) {
|
function Select (db, from, conditions) {
|
||||||
try {
|
try {
|
||||||
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}`
|
||||||
DebugLog(s)
|
|
||||||
|
|
||||||
const stmt = db.prepare(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
return stmt.all()
|
return stmt.all()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
|
@ -205,8 +196,7 @@ function Insert (db, table, data) {
|
||||||
}, []).join(', ')
|
}, []).join(', ')
|
||||||
|
|
||||||
const s = `INSERT INTO ${table} (${cols}) VALUES (${values})`
|
const s = `INSERT INTO ${table} (${cols}) VALUES (${values})`
|
||||||
DebugLog(s)
|
const stmt = PrepareStatement(db, s)
|
||||||
const stmt = db.prepare(s)
|
|
||||||
|
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -222,3 +212,10 @@ function CloseDB (db) {
|
||||||
DebugLog('Close the database connection.')
|
DebugLog('Close the database connection.')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function PrepareStatement (db, s) {
|
||||||
|
DebugLog(s)
|
||||||
|
return db.prepare(s)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue