fix #1
This commit is contained in:
parent
f55b00dbf5
commit
2b495367a2
@ -12,9 +12,9 @@ __webpack_require__.r(__webpack_exports__);
|
||||
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
||||
/* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper)
|
||||
/* 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 _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs");
|
||||
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
||||
/* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3");
|
||||
/* 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 _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");
|
||||
|
||||
|
||||
@ -22,16 +22,34 @@ __webpack_require__.r(__webpack_exports__);
|
||||
|
||||
|
||||
|
||||
|
||||
class DatabaseWrapper {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
constructor(dbName) {
|
||||
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() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.close((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;
|
||||
}
|
||||
resolve();
|
||||
@ -40,51 +58,10 @@ class DatabaseWrapper {
|
||||
}
|
||||
|
||||
sql(sql, ...args) {
|
||||
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.QueryWrapper(sql);
|
||||
|
||||
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
||||
return this.executeRun(query, ...args);
|
||||
} else {
|
||||
return this.executeAll(query, ...args);
|
||||
}
|
||||
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.QueryWrapper(sql, ...args);
|
||||
return query.execute(this.db);
|
||||
}
|
||||
|
||||
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 */ "QueryWrapper": () => (/* binding */ QueryWrapper)
|
||||
/* 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 {
|
||||
constructor(sql) {
|
||||
constructor(sql, ...args) {
|
||||
this.sql = sql.trim();
|
||||
this.args = args;
|
||||
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
||||
}
|
||||
|
||||
@ -163,6 +149,51 @@ class QueryWrapper {
|
||||
}
|
||||
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 */ "default": () => (__WEBPACK_DEFAULT_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_1__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.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_3__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
|
||||
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
||||
|
||||
|
||||
/* 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 _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __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 _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
|
||||
|
||||
|
||||
|
||||
@ -279,45 +307,44 @@ const databases = new Map();
|
||||
name: "PawSQLiteNodeAdapter",
|
||||
open: async (dbName) => {
|
||||
if (!databases.has(dbName)) {
|
||||
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.DatabaseWrapper(
|
||||
new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(dbName)
|
||||
);
|
||||
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName);
|
||||
await db.open();
|
||||
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) => {
|
||||
const db = databases.get(dbName);
|
||||
if (!db) {
|
||||
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
|
||||
}
|
||||
|
||||
const db = getDatabase(dbName);
|
||||
await db.close();
|
||||
|
||||
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) => {
|
||||
const db = databases.get(dbName);
|
||||
if (!db) {
|
||||
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
|
||||
}
|
||||
|
||||
const db = getDatabase(dbName);
|
||||
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) => {
|
||||
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;
|
||||
|
File diff suppressed because one or more lines are too long
@ -2,8 +2,7 @@ import * as sqlite3 from "sqlite3";
|
||||
|
||||
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
||||
import { QueryWrapper } from "./query_wrapper.mjs";
|
||||
import { ResponseWrapper } from "./response_wrapper.mjs";
|
||||
import { enableDebug, log } from "./log.mjs";
|
||||
import { log } from "./log.mjs";
|
||||
|
||||
|
||||
export class DatabaseWrapper {
|
||||
@ -41,49 +40,8 @@ export class DatabaseWrapper {
|
||||
}
|
||||
|
||||
sql(sql, ...args) {
|
||||
const query = new QueryWrapper(sql);
|
||||
|
||||
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
|
||||
return this.executeRun(query, ...args);
|
||||
} else {
|
||||
return this.executeAll(query, ...args);
|
||||
}
|
||||
const query = new QueryWrapper(sql, ...args);
|
||||
return query.execute(this.db);
|
||||
}
|
||||
|
||||
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}));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
@ -18,23 +18,15 @@ export default {
|
||||
},
|
||||
|
||||
close: async (dbName) => {
|
||||
const db = databases.get(dbName);
|
||||
if (!db) {
|
||||
throw new NodeAdapterError("Database not open");
|
||||
}
|
||||
|
||||
const db = getDatabase(dbName);
|
||||
await db.close();
|
||||
|
||||
databases.delete(dbName);
|
||||
|
||||
return ResponseWrapper.success();
|
||||
},
|
||||
|
||||
sql: async (dbName, sql, ...args) => {
|
||||
const db = databases.get(dbName);
|
||||
if (!db) {
|
||||
throw new NodeAdapterError("Database not open");
|
||||
}
|
||||
|
||||
const db = getDatabase(dbName);
|
||||
const result = await db.sql(sql, ...args);
|
||||
|
||||
return ResponseWrapper.success(result);
|
||||
@ -46,3 +38,12 @@ export default {
|
||||
|
||||
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 {
|
||||
constructor(sql) {
|
||||
constructor(sql, ...args) {
|
||||
this.sql = sql.trim();
|
||||
this.args = args;
|
||||
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
|
||||
}
|
||||
|
||||
@ -12,4 +18,49 @@ export class QueryWrapper {
|
||||
}
|
||||
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