@fxjs/db-driver
Version:
[](https://www.npmjs.org/package/@fxjs/db-driver) [](https://travis-ci.org/fxjs-modules/db-driver) [ • 1.4 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");
class MySQLDriver extends base_class_1.SQLDriver {
constructor(conn) {
super(conn);
this.connection = null;
}
switchDb(targetDb) {
this.execute(`use \`${targetDb}\``);
this.currentDb = targetDb;
}
open() { return super.open(); }
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() { return db.openMySQL(this.uri); }
dbExists(dbname) {
return this.execute(`SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbname}'`).length > 0;
}
execute(sql) {
if (this.extend_config.debug_sql) {
(0, utils_1.logDebugSQL)('mysql', sql);
}
if (this.isPool)
return this.pool(conn => conn.execute(sql));
if (!this.connection)
this.open();
return this.connection.execute(sql);
}
}
exports.default = MySQLDriver;