Compare commits
2 Commits
75f0921cce
...
2b495367a2
Author | SHA1 | Date | |
---|---|---|---|
2b495367a2 | |||
f55b00dbf5 |
@ -12,9 +12,9 @@ __webpack_require__.r(__webpack_exports__);
|
|||||||
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
||||||
/* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper)
|
/* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper)
|
||||||
/* harmony export */ });
|
/* harmony export */ });
|
||||||
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
|
/* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3");
|
||||||
/* harmony import */ var _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs");
|
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
|
||||||
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
/* harmony import */ var _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs");
|
||||||
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
||||||
|
|
||||||
|
|
||||||
@ -22,16 +22,34 @@ __webpack_require__.r(__webpack_exports__);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DatabaseWrapper {
|
class DatabaseWrapper {
|
||||||
constructor(db) {
|
constructor(dbName) {
|
||||||
this.db = db;
|
this.dbName = dbName;
|
||||||
|
}
|
||||||
|
|
||||||
|
open() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const db = new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(
|
||||||
|
this.dbName,
|
||||||
|
sqlite3__WEBPACK_IMPORTED_MODULE_0__.OPEN_READWRITE | sqlite3__WEBPACK_IMPORTED_MODULE_0__.OPEN_CREATE,
|
||||||
|
(err) => {
|
||||||
|
if (err) {
|
||||||
|
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.db = db;
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.db.close((err) => {
|
this.db.close((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
|
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve();
|
resolve();
|
||||||
@ -40,51 +58,10 @@ class DatabaseWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql(sql, ...args) {
|
sql(sql, ...args) {
|
||||||
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.QueryWrapper(sql);
|
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.QueryWrapper(sql, ...args);
|
||||||
|
return query.execute(this.db);
|
||||||
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
|
||||||
return this.executeRun(query, ...args);
|
|
||||||
} else {
|
|
||||||
return this.executeAll(query, ...args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
executeRun(query, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_3__.log)(query.sql);
|
|
||||||
this.db.run(query.sql, ...args, function (err) {
|
|
||||||
if (err) {
|
|
||||||
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {};
|
|
||||||
|
|
||||||
if (query.isAnyOf("INSERT")) {
|
|
||||||
result.insertId = this.lastID;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query.isAnyOf("UPDATE", "DELETE")) {
|
|
||||||
result.rowsAffected = this.changes;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
executeAll(query, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_3__.log)(query.sql);
|
|
||||||
this.db.all(query.sql, ...args, function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success({rows}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
@ -149,9 +126,18 @@ __webpack_require__.r(__webpack_exports__);
|
|||||||
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
||||||
/* harmony export */ "QueryWrapper": () => (/* binding */ QueryWrapper)
|
/* harmony export */ "QueryWrapper": () => (/* binding */ QueryWrapper)
|
||||||
/* harmony export */ });
|
/* harmony export */ });
|
||||||
|
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
|
||||||
|
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
||||||
|
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class QueryWrapper {
|
class QueryWrapper {
|
||||||
constructor(sql) {
|
constructor(sql, ...args) {
|
||||||
this.sql = sql.trim();
|
this.sql = sql.trim();
|
||||||
|
this.args = args;
|
||||||
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,6 +149,51 @@ class QueryWrapper {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execute(db) {
|
||||||
|
if (this.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
||||||
|
return this.executeRun(db);
|
||||||
|
} else {
|
||||||
|
return this.executeAll(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
executeRun(db) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
|
||||||
|
db.run(this.sql, ...this.args, function (err) {
|
||||||
|
if (err) {
|
||||||
|
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {};
|
||||||
|
|
||||||
|
if (this.isAnyOf("INSERT")) {
|
||||||
|
result.insertId = this.lastID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isAnyOf("UPDATE", "DELETE")) {
|
||||||
|
result.rowsAffected = this.changes;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.ResponseWrapper.success(result));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executeAll(db) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
|
||||||
|
db.all(this.sql, ...this.args, function (err, rows) {
|
||||||
|
if (err) {
|
||||||
|
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.ResponseWrapper.success({rows}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/***/ }),
|
/***/ }),
|
||||||
@ -261,13 +292,10 @@ __webpack_require__.r(__webpack_exports__);
|
|||||||
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
||||||
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
|
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
|
||||||
/* harmony export */ });
|
/* harmony export */ });
|
||||||
/* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3");
|
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
|
||||||
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
|
/* harmony import */ var _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_wrapper.mjs");
|
||||||
/* harmony import */ var _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_wrapper.mjs");
|
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
||||||
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
||||||
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -279,45 +307,44 @@ const databases = new Map();
|
|||||||
name: "PawSQLiteNodeAdapter",
|
name: "PawSQLiteNodeAdapter",
|
||||||
open: async (dbName) => {
|
open: async (dbName) => {
|
||||||
if (!databases.has(dbName)) {
|
if (!databases.has(dbName)) {
|
||||||
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.DatabaseWrapper(
|
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName);
|
||||||
new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(dbName)
|
await db.open();
|
||||||
);
|
|
||||||
databases.set(dbName, db);
|
databases.set(dbName, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
|
||||||
},
|
},
|
||||||
|
|
||||||
close: async (dbName) => {
|
close: async (dbName) => {
|
||||||
const db = databases.get(dbName);
|
const db = getDatabase(dbName);
|
||||||
if (!db) {
|
|
||||||
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
|
|
||||||
}
|
|
||||||
|
|
||||||
await db.close();
|
await db.close();
|
||||||
|
|
||||||
databases.delete(dbName);
|
databases.delete(dbName);
|
||||||
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
|
|
||||||
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
|
||||||
},
|
},
|
||||||
|
|
||||||
sql: async (dbName, sql, ...args) => {
|
sql: async (dbName, sql, ...args) => {
|
||||||
const db = databases.get(dbName);
|
const db = getDatabase(dbName);
|
||||||
if (!db) {
|
|
||||||
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await db.sql(sql, ...args);
|
const result = await db.sql(sql, ...args);
|
||||||
|
|
||||||
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success(result);
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result);
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: async (dbName) => {
|
delete: async (dbName) => {
|
||||||
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Delete not implemented");
|
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Delete not implemented");
|
||||||
},
|
},
|
||||||
|
|
||||||
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_4__.enableDebug
|
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_3__.enableDebug
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function getDatabase(dbName) {
|
||||||
|
const db = databases.get(dbName);
|
||||||
|
if (!db) {
|
||||||
|
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Database not open");
|
||||||
|
}
|
||||||
|
return db;
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
module.exports = __webpack_exports__.default;
|
module.exports = __webpack_exports__.default;
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1,12 +1,30 @@
|
|||||||
|
import * as sqlite3 from "sqlite3";
|
||||||
|
|
||||||
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
||||||
import { QueryWrapper } from "./query_wrapper.mjs";
|
import { QueryWrapper } from "./query_wrapper.mjs";
|
||||||
import { ResponseWrapper } from "./response_wrapper.mjs";
|
import { log } from "./log.mjs";
|
||||||
import { enableDebug, log } from "./log.mjs";
|
|
||||||
|
|
||||||
|
|
||||||
export class DatabaseWrapper {
|
export class DatabaseWrapper {
|
||||||
constructor(db) {
|
constructor(dbName) {
|
||||||
this.db = db;
|
this.dbName = dbName;
|
||||||
|
}
|
||||||
|
|
||||||
|
open() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const db = new sqlite3.Database(
|
||||||
|
this.dbName,
|
||||||
|
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
|
||||||
|
(err) => {
|
||||||
|
if (err) {
|
||||||
|
reject(NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.db = db;
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
@ -22,49 +40,8 @@ export class DatabaseWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sql(sql, ...args) {
|
sql(sql, ...args) {
|
||||||
const query = new QueryWrapper(sql);
|
const query = new QueryWrapper(sql, ...args);
|
||||||
|
return query.execute(this.db);
|
||||||
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
|
||||||
return this.executeRun(query, ...args);
|
|
||||||
} else {
|
|
||||||
return this.executeAll(query, ...args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
executeRun(query, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
log(query.sql);
|
|
||||||
this.db.run(query.sql, ...args, function (err) {
|
|
||||||
if (err) {
|
|
||||||
reject(NodeAdapterError.from(err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {};
|
|
||||||
|
|
||||||
if (query.isAnyOf("INSERT")) {
|
|
||||||
result.insertId = this.lastID;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (query.isAnyOf("UPDATE", "DELETE")) {
|
|
||||||
result.rowsAffected = this.changes;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(ResponseWrapper.success(result));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
executeAll(query, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
log(query.sql);
|
|
||||||
this.db.all(query.sql, ...args, function (err, rows) {
|
|
||||||
if (err) {
|
|
||||||
reject(NodeAdapterError.from(err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
resolve(ResponseWrapper.success({rows}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,5 +1,3 @@
|
|||||||
import * as sqlite3 from "sqlite3";
|
|
||||||
|
|
||||||
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
||||||
import { DatabaseWrapper } from "./database_wrapper.mjs";
|
import { DatabaseWrapper } from "./database_wrapper.mjs";
|
||||||
import { ResponseWrapper } from "./response_wrapper.mjs";
|
import { ResponseWrapper } from "./response_wrapper.mjs";
|
||||||
@ -11,9 +9,8 @@ export default {
|
|||||||
name: "PawSQLiteNodeAdapter",
|
name: "PawSQLiteNodeAdapter",
|
||||||
open: async (dbName) => {
|
open: async (dbName) => {
|
||||||
if (!databases.has(dbName)) {
|
if (!databases.has(dbName)) {
|
||||||
const db = new DatabaseWrapper(
|
const db = new DatabaseWrapper(dbName);
|
||||||
new sqlite3.Database(dbName)
|
await db.open();
|
||||||
);
|
|
||||||
databases.set(dbName, db);
|
databases.set(dbName, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,23 +18,15 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
close: async (dbName) => {
|
close: async (dbName) => {
|
||||||
const db = databases.get(dbName);
|
const db = getDatabase(dbName);
|
||||||
if (!db) {
|
|
||||||
throw new NodeAdapterError("Database not open");
|
|
||||||
}
|
|
||||||
|
|
||||||
await db.close();
|
await db.close();
|
||||||
|
|
||||||
databases.delete(dbName);
|
databases.delete(dbName);
|
||||||
|
|
||||||
return ResponseWrapper.success();
|
return ResponseWrapper.success();
|
||||||
},
|
},
|
||||||
|
|
||||||
sql: async (dbName, sql, ...args) => {
|
sql: async (dbName, sql, ...args) => {
|
||||||
const db = databases.get(dbName);
|
const db = getDatabase(dbName);
|
||||||
if (!db) {
|
|
||||||
throw new NodeAdapterError("Database not open");
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await db.sql(sql, ...args);
|
const result = await db.sql(sql, ...args);
|
||||||
|
|
||||||
return ResponseWrapper.success(result);
|
return ResponseWrapper.success(result);
|
||||||
@ -49,3 +38,12 @@ export default {
|
|||||||
|
|
||||||
debug: enableDebug
|
debug: enableDebug
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function getDatabase(dbName) {
|
||||||
|
const db = databases.get(dbName);
|
||||||
|
if (!db) {
|
||||||
|
throw new NodeAdapterError("Database not open");
|
||||||
|
}
|
||||||
|
return db;
|
||||||
|
}
|
@ -1,6 +1,12 @@
|
|||||||
|
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
||||||
|
import { ResponseWrapper } from "./response_wrapper.mjs";
|
||||||
|
import { log } from "./log.mjs";
|
||||||
|
|
||||||
|
|
||||||
export class QueryWrapper {
|
export class QueryWrapper {
|
||||||
constructor(sql) {
|
constructor(sql, ...args) {
|
||||||
this.sql = sql.trim();
|
this.sql = sql.trim();
|
||||||
|
this.args = args;
|
||||||
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12,4 +18,49 @@ export class QueryWrapper {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
execute(db) {
|
||||||
|
if (this.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
||||||
|
return this.executeRun(db);
|
||||||
|
} else {
|
||||||
|
return this.executeAll(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
executeRun(db) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
log(this.sql);
|
||||||
|
db.run(this.sql, ...this.args, function (err) {
|
||||||
|
if (err) {
|
||||||
|
reject(NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {};
|
||||||
|
|
||||||
|
if (this.isAnyOf("INSERT")) {
|
||||||
|
result.insertId = this.lastID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isAnyOf("UPDATE", "DELETE")) {
|
||||||
|
result.rowsAffected = this.changes;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(ResponseWrapper.success(result));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
executeAll(db) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
log(this.sql);
|
||||||
|
db.all(this.sql, ...this.args, function (err, rows) {
|
||||||
|
if (err) {
|
||||||
|
reject(NodeAdapterError.from(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
resolve(ResponseWrapper.success({rows}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user