SQLite数据的封装体
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
var sqliteDbPath = path.resolve(__dirname, './sqlite3.db');
var sqlite3Connection = new sqlite3.Database(sqliteDbPath, (err: any) => {
if (err) throw err;
});
type Istrings = string | string[] | undefined;
interface Idb {
type: string;
table: string;
field?: Istrings;
value?: Istrings;
set?: Istrings;
where?: Istrings;
order?: Istrings;
limit?: number;
}
class DbSQLite3 {
pool: any;
constructor(pool: any) {
this.pool = pool;
}
joinSql(sql: Idb, type: 'where' | 'order' | 'value' | 'set' | 'field') {
var val = sql[type];
if (Array.isArray(val)) {
if (val.length) {
if (type === 'where') {
return ' where ' + val.join(' and ');
} else if (type === 'order') {
return ' order by ' + val.join(',');
} else if (type === 'value') {
return ' values (' + val.join('),(') + ')';
} else if (type === 'set') {
return ' set ' + val.join(',');
} else if (type === 'field') {
return ' (' + val.join(',') + ')';
}
} else {
if (['value', 'set'].includes(type)) {
}
}
} else if (val) {
if (type === 'where') {
return ' where ' + val;
} else if (type === 'order') {
return ' order by ' + val;
} else if (type === 'value') {
return ' values (' + val + ')';
} else if (type === 'set') {
return ' set ' + val;
} else if (type === 'field') {
return ' (' + val + ')';
}
}
return '';
}
makeSql(sql: Idb) {
var sqls = sql.type;
if (sqls === 'insert') {
sqls += ' into';
sqls += ' ' + sql.table;
sqls += this.joinSql(sql, 'field');
sqls += this.joinSql(sql, 'value');
} else if (sqls === 'delete') {
sqls += ' from ' + sql.table;
sqls += this.joinSql(sql, 'where');
} else if (sqls === 'update') {
sqls += ' ' + sql.table;
sqls += this.joinSql(sql, 'set');
sqls += this.joinSql(sql, 'where');
} else if (sqls === 'select') {
sqls += sql.field ? ' ' + sql.field : ' *';
sqls += ' from ' + sql.table;
sqls += this.joinSql(sql, 'where');
sqls += this.joinSql(sql, 'order');
sqls += sql.limit ? ' limit ' + sql.limit : '';
}
console.log(666.111, sqls);
return sqls;
}
getErr(err: any) {
var errs = err?.message || null;
return errs;
}
getResult(rows: any) {
var result = rows ?? 'OK';
return result;
}
query(sql: Idb, callback: any) {
console.log(666.101, sql, callback);
if (sql.type === 'select') {
if (sql.where && (!sql.limit || sql.limit === 1) && false) {
this.pool.each(this.makeSql(sql), (err: any, rows: any) => {
console.log(666.103, err, rows);
callback(this.getErr(err), this.getResult(rows));
});
} else {
this.pool.all(this.makeSql(sql), (err: any, rows: any) => {
console.log(666.104, err, rows);
callback(this.getErr(err), this.getResult(rows));
});
}
} else {
this.pool.run(this.makeSql(sql), (err: any, rows: any) => {
console.log(666.105, err, rows);
callback(this.getErr(err), this.getResult(rows));
});
}
}
}
export default new DbSQLite3(sqlite3Connection);
读取示例,与上一篇的mysql保持一致
import db from './db/sqlite/db';
var a = 'select';
if (a === 'delete') {
db.query(
{
type: 'delete',
table: 'user',
where: 'userName="SSSS"',
limit: 2,
order: 'id desc',
},
(err: any, result: any) => {
if (err) {
console.log(666.201, err);
} else {
console.log(666.202, result);
if (result) {
console.log(666.2022, '删除结果:' + result);
}
}
}
);
} else if (a === 'update') {
db.query(
{
type: 'update',
table: 'user',
set: ['userName="SSSS"', 'userPassword="FFFF"'],
where: 'id=10',
limit: 3,
order: 'id desc',
},
(err: any, result: any) => {
if (err) {
console.log(666.201, err);
} else {
console.log(666.202, result);
if (result) {
console.log(666.2022, '修改结果:' + result);
}
}
}
);
} else if (a === 'insert') {
db.query(
{
type: 'insert',
table: 'user',
field: 'userName,userPassword,userType',
value: ['"asai","123456",8', '"阿萨","分sss",88', '"阿萨","分sss",88'],
},
(err: any, result: any) => {
if (err) {
console.log(666.201, err);
} else {
console.log(666.202, result);
if (result) {
console.log(666.2022, '增加结果:' + result);
}
}
}
);
} else if (a === 'select') {
db.query(
{
type: 'select',
table: 'user',
field: '*',
where: 'id>5',
limit: 1,
order: 'id desc',
},
(err: any, result: any) => {
if (err) {
console.log(666.201, err);
} else {
console.log(666.202, result?.length, result);
if (result && result.length) {
result.forEach((element: any) => {
console.log(666.2022, element?.id, element);
});
}
}
}
);
}