@fxjs/db-driver
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -122,3 +122,5 @@ /// <reference types="fib-pool" /> | ||
*/ | ||
switchDb(targetDb) { } | ||
switchDb(targetDb) { | ||
this.currentDb = targetDb; | ||
} | ||
; | ||
@@ -181,5 +183,7 @@ /** | ||
class SQLDriver extends Driver { | ||
constructor() { | ||
super(...arguments); | ||
constructor(opts) { | ||
super(opts); | ||
this.currentDb = null; | ||
const options = Utils.parseConnectionString(opts); | ||
this.extend_config.debug_sql = Utils.castQueryStringToBoolean(options.query.debug_sql); | ||
} | ||
@@ -189,2 +193,7 @@ /** | ||
*/ | ||
dbExists(dbname) { return false; } | ||
; | ||
/** | ||
* @override | ||
*/ | ||
begin() { } | ||
@@ -191,0 +200,0 @@ /** |
@@ -0,0 +0,0 @@ /// <reference types="fib-pool" /> |
@@ -5,2 +5,3 @@ /// <reference types="fib-pool" /> | ||
const base_class_1 = require("./base.class"); | ||
const utils_1 = require("../utils"); | ||
class MySQLDriver extends base_class_1.SQLDriver { | ||
@@ -13,2 +14,3 @@ constructor(conn) { | ||
this.execute(`use \`${targetDb}\``); | ||
this.currentDb = targetDb; | ||
} | ||
@@ -26,3 +28,9 @@ open() { return super.open(); } | ||
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) { | ||
utils_1.logDebugSQL('mysql', sql); | ||
} | ||
if (this.isPool) | ||
@@ -29,0 +37,0 @@ return this.pool(conn => conn.execute(sql)); |
@@ -5,2 +5,3 @@ /// <reference types="fib-pool" /> | ||
const base_class_1 = require("./base.class"); | ||
const utils_1 = require("../utils"); | ||
class PostgreSQLDriver extends base_class_1.SQLDriver { | ||
@@ -11,4 +12,12 @@ constructor(conn) { | ||
} | ||
/** | ||
* @description unsafe for parallel execution, make sure call it in serial | ||
* @param targetDb | ||
*/ | ||
switchDb(targetDb) { | ||
this.execute(`\\c ${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; | ||
} | ||
@@ -26,3 +35,9 @@ open() { return super.open(); } | ||
getConnection() { return db.openPSQL(this.uri); } | ||
dbExists(dbname) { | ||
return this.execute(`SELECT datname FROM pg_database WHERE datname = '${dbname}'`).length > 0; | ||
} | ||
execute(sql) { | ||
if (this.extend_config.debug_sql) { | ||
utils_1.logDebugSQL('postgresql', sql); | ||
} | ||
if (this.isPool) | ||
@@ -29,0 +44,0 @@ return this.pool(conn => conn.execute(sql)); |
@@ -0,0 +0,0 @@ /// <reference types="fib-pool" /> |
@@ -5,2 +5,3 @@ /// <reference types="fib-pool" /> | ||
const base_class_1 = require("./base.class"); | ||
const utils_1 = require("../utils"); | ||
class SQLiteDriver extends base_class_1.SQLDriver { | ||
@@ -22,3 +23,10 @@ constructor(conn) { | ||
getConnection() { return db.openSQLite(this.uri); } | ||
dbExists(dbname) { | ||
// return this.execute(`SELECT name FROM sqlite_master WHERE type='table' AND name='${dbname}'`).length > 0; | ||
return true; | ||
} | ||
execute(sql) { | ||
if (this.extend_config.debug_sql) { | ||
utils_1.logDebugSQL('sqlite', sql); | ||
} | ||
if (this.isPool) | ||
@@ -25,0 +33,0 @@ return this.pool(conn => conn.execute(sql)); |
@@ -0,0 +0,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
Object.defineProperty(exports, "__esModule", { value: true }); |
/// <reference types="@fibjs/types" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.arraify = exports.mountPoolToDriver = exports.parsePoolConfig = exports.parseConnectionString = exports.ensureSuffix = exports.castQueryStringToBoolean = exports.forceInteger = exports.filterDriverType = exports.driverUUid = void 0; | ||
exports.logDebugSQL = exports.arraify = exports.mountPoolToDriver = exports.parsePoolConfig = exports.parseConnectionString = exports.ensureSuffix = exports.castQueryStringToBoolean = exports.forceInteger = exports.filterDriverType = exports.driverUUid = void 0; | ||
const url = require("url"); | ||
const util = require("util"); | ||
const tty = require("tty"); | ||
const net = require("net"); | ||
@@ -121,2 +122,10 @@ const uuid = require("uuid"); | ||
]); | ||
Object.defineProperty(input, 'database', { | ||
set(v) { | ||
this.pathname = '/' + v; | ||
}, | ||
get() { | ||
return unPrefix(this.pathname, '/'); | ||
}, | ||
}); | ||
input.slashes = !!input.slashes; | ||
@@ -153,1 +162,20 @@ input.port = forceInteger(input.port, null); | ||
exports.arraify = arraify; | ||
function logDebugSQL(dbtype, sql, is_sync = true) { | ||
let fmt; | ||
if (tty.isatty(process.stdout.fd)) { | ||
fmt = "\033[32;1m(orm/%s) \033[34m%s\033[0m\n"; | ||
sql = sql.replace(/`(.+?)`/g, function (m) { return "\033[31m" + m + "\033[34m"; }); | ||
} | ||
else { | ||
fmt = "[SQL/%s] %s\n"; | ||
} | ||
const text = util.format(fmt, dbtype, sql); | ||
if (is_sync) { | ||
console.log(text); | ||
} | ||
else { | ||
process.stdout.write(text); | ||
} | ||
} | ||
exports.logDebugSQL = logDebugSQL; | ||
; |
@@ -0,0 +0,0 @@ MIT License |
{ | ||
"name": "@fxjs/db-driver", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -60,3 +60,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "f784b7a1f8a08c0d81e9028f79d05772c8981caf" | ||
"gitHead": "e3b9577fa0fd3e9b4cea52620e31ccdbc9fde40f" | ||
} |
@@ -0,0 +0,0 @@ ## Fibjs Orm Drivers |
@@ -68,2 +68,3 @@ /// <reference types="@fibjs/types" /> | ||
export declare class SQLDriver<CONN_TYPE extends Driver.IConnTypeEnum> extends Driver<CONN_TYPE> implements FxDbDriverNS.SQLDriver { | ||
constructor(opts: FxDbDriverNS.ConnectionInputArgs | string); | ||
currentDb: FxDbDriverNS.SQLDriver['currentDb']; | ||
@@ -73,2 +74,6 @@ /** | ||
*/ | ||
dbExists(dbname: string): boolean; | ||
/** | ||
* @override | ||
*/ | ||
begin(): void; | ||
@@ -75,0 +80,0 @@ /** |
@@ -0,0 +0,0 @@ /// <reference types="@fibjs/types" /> |
@@ -16,3 +16,4 @@ /// <reference types="@fibjs/types" /> | ||
getConnection(): Class_MySQL; | ||
dbExists(dbname: string): boolean; | ||
execute<T = any>(sql: string): T; | ||
} |
@@ -7,2 +7,6 @@ /// <reference types="@fibjs/types" /> | ||
constructor(conn: FxDbDriverNS.ConnectionInputArgs | string); | ||
/** | ||
* @description unsafe for parallel execution, make sure call it in serial | ||
* @param targetDb | ||
*/ | ||
switchDb(targetDb: string): void; | ||
@@ -17,3 +21,4 @@ open(): Class_DbConnection; | ||
getConnection(): Class_DbConnection; | ||
dbExists(dbname: string): boolean; | ||
execute<T = any>(sql: string): T; | ||
} |
@@ -0,0 +0,0 @@ /// <reference types="@fibjs/types" /> |
@@ -15,3 +15,4 @@ /// <reference types="@fibjs/types" /> | ||
getConnection(): Class_SQLite; | ||
dbExists(dbname: string): boolean; | ||
execute<T = any>(sql: string): T; | ||
} |
export { Driver } from './built-ins/base.class'; | ||
export type { Driver as IDbDriver, } from './built-ins/base.class'; | ||
export type { FxDbDriverNS } from './Typo'; |
@@ -93,2 +93,7 @@ /// <reference types="@fibjs/types" /> | ||
currentDb: string; | ||
/** | ||
* | ||
* @param dbname dbname, expected to be escaped | ||
*/ | ||
dbExists(dbname: string): boolean; | ||
execute: { | ||
@@ -95,0 +100,0 @@ <T = any>(sql: string): T; |
@@ -11,1 +11,2 @@ import { FxDbDriverNS } from './Typo'; | ||
export declare function arraify<T = any>(item: T | T[]): T[]; | ||
export declare function logDebugSQL(dbtype: string, sql: string, is_sync?: boolean): void; |
35657
925