@fxjs/db-driver
Version:
[](https://www.npmjs.org/package/@fxjs/db-driver) [](https://travis-ci.org/fxjs-modules/db-driver) [ • 3.2 kB
JavaScript
/// <reference types="fib-pool" />
Object.defineProperty(exports, "__esModule", { value: true });
const db = require("db");
const base_class_1 = require("./base.class");
const utils_1 = require("../utils");
const CodepointsList = require('../../codepoints-map.json');
const CodepointsMap = CodepointsList.reduce((accu, item) => {
accu[item.codepoint] = item;
return accu;
}, {});
const win32CpInfo = (0, utils_1.detectWindowsCodePoints)();
class PostgreSQLDriver extends base_class_1.SQLDriver {
constructor(conn) {
super(conn);
this.connection = null;
}
/**
* @description unsafe for parallel execution, make sure call it in serial
* @param targetDb
*/
switchDb(targetDb) {
// // will throw out error now, postgresql does not support run commmand as sql
// this.execute(`\\c ${targetDb};`);
this.config.database = targetDb;
this.reopen();
this.currentDb = targetDb;
}
open() {
if (!win32CpInfo.codepoints)
return super.open();
const conn = super.open();
const matchedCodepoint = CodepointsMap[win32CpInfo.codepoints];
// TODO: support common codepoints -> postgresql-encoding
if (conn.codec && win32CpInfo.codepoints === '936') {
conn.codec = 'GBK';
}
else if (conn.codec && matchedCodepoint) {
conn.codec = matchedCodepoint.dotNetName;
}
else if (conn.codec === 'utf8' && win32CpInfo.codepoints !== '65001') {
console.error(`system default code points is '${win32CpInfo.codepoints}', but odbc try to use codec UTF8 with codepoints 65001, refer to https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers to set connection.codec as UTF8`);
}
return conn;
}
close() {
if (this.connection)
this.connection.close();
}
ping() { return; }
begin() { return this.connection.begin(); }
commit() { return this.connection.commit(); }
trans(cb) { return this.connection.trans(cb); }
rollback() { return this.connection.rollback(); }
getConnection() {
var _a, _b;
const conn = db.openPSQL(this.uri);
let searchPath = ((_a = this.config.query) === null || _a === void 0 ? void 0 : _a.search_path) || ((_b = this.config.query) === null || _b === void 0 ? void 0 : _b.searchPath) || '';
if (searchPath) {
searchPath = (0, utils_1.filterPSQLSearchPath)(searchPath);
searchPath && conn.execute(`SET search_path TO ${searchPath}`);
}
;
return conn;
}
dbExists(dbname) {
return this.execute(`SELECT datname FROM pg_database WHERE datname = '${dbname}'`).length > 0;
}
execute(sql) {
if (this.extend_config.debug_sql) {
(0, utils_1.logDebugSQL)('postgresql', sql);
}
if (this.isPool)
return this.pool(conn => conn.execute(sql));
if (!this.connection)
this.open();
return this.connection.execute(sql);
}
}
exports.default = PostgreSQLDriver;