feat: refactor database

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-05-11 10:54:44 +08:00
parent 4ee8fc9a20
commit 47413c6766
5 changed files with 87 additions and 37 deletions

View File

@ -1,5 +1,45 @@
import { injectable } from "@ccms/container"
export namespace database {
export const DataBaseManager = Symbol("DataBaseManager");
export const DataSource = Symbol("DataSource");
export const DataBase = Symbol("DataBase");
export const DataSource = Symbol("DataSource")
/**
*
*/
export interface DataBaseConfig {
/**
*
*/
url: string | javax.sql.DataSource
/**
*
*/
driverClassName?: string
/**
*
*/
username?: string
/**
*
*/
password?: string
/**
*
*/
properties?: { [key: string]: any }
}
@injectable()
export abstract class DataBaseManager {
abstract setMainDatabase(mainDatabase: DataBase): void
abstract getMainDatabase(): DataBase
abstract createDatabase(name: string, config: DataBaseConfig): DataBase
abstract removeDatabase(name: string): boolean
abstract getDatabase(name: string): DataBase
}
@injectable()
export abstract class DataBase {
abstract query<T>(sql: string, ...args: any[]): Array<T>
abstract update(sql: string, ...args: any[]): number
abstract execute(sql: string): void
}
}

View File

@ -113,7 +113,7 @@ export function loadMavenDepend(groupId: string, artifactId: string, version: st
let dependencies = doc.getElementsByTagName("dependency")
let size = dependencies.length
if (!size) { return }
console.info(key, 'found', size, 'dependencies loading...')
console.debug(key, 'found', size, 'dependencies loading...')
for (let i = 0; i < size; i++) {
const dependency = dependencies.item(i)
const gav = dependency.getChildNodes()

View File

@ -115,13 +115,11 @@ function loadMiaoScriptConfig() {
if (!fs.exists(configFile)) {
global.ScriptEngineConfig = base.save(configFile, yaml.dump({
uuid: UUID.randomUUID().toString(),
channel: 'latest',
slow_execute: 50
}))
} else {
global.ScriptEngineConfig = yaml.load(base.read(configFile))
}
global.ScriptEngineChannel = global.ScriptEngineConfig.channel || 'latest'
global.ScriptSlowExecuteTime = global.ScriptEngineConfig.slow_execute || 50
}

View File

@ -1,32 +1,12 @@
import { JSClass } from '@ccms/container'
import { Model } from './model'
import { database } from '@ccms/api'
import { JSClass, postConstruct } from '@ccms/container'
/**
*
*/
export interface DataBaseConfig {
/**
*
*/
url: string | javax.sql.DataSource
/**
*
*/
driverClassName?: string
/**
*
*/
username?: string
/**
*
*/
password?: string
}
const Properties = Java.type('java.util.Properties')
/**
*
*/
export class DataBase {
export class DataBase extends database.DataBase {
private dataSource: javax.sql.DataSource
private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
@ -37,13 +17,13 @@ export class DataBase {
@JSClass('org.springframework.jdbc.core.JdbcTemplate')
private JdbcTemplate: typeof org.springframework.jdbc.core.JdbcTemplate
constructor(dbConfig: DataBaseConfig) {
constructor(dbConfig: database.DataBaseConfig) {
super()
if (!dbConfig.url) { throw new Error('DataBase url can\'t be null!') }
this.createDataSource(dbConfig)
this.initialize()
}
private createDataSource(dbConfig: DataBaseConfig) {
private createDataSource(dbConfig: database.DataBaseConfig) {
if (typeof dbConfig.url === "string") {
let config = new this.HikariConfig()
if (dbConfig.driverClassName) {
@ -56,12 +36,20 @@ export class DataBase {
config.setPassword(dbConfig.password)
}
config.setJdbcUrl(dbConfig.url)
if (dbConfig.properties) {
let properties = new Properties()
for (const key in dbConfig.properties) {
properties.setProperty(key, dbConfig.properties[key])
}
config.setDataSourceProperties(properties)
}
this.dataSource = new this.HikariDataSource(config)
} else {
this.dataSource = dbConfig.url
}
}
@postConstruct()
private initialize() {
this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
}
@ -69,6 +57,7 @@ export class DataBase {
/**
* SQL查询
* @param sql SQL语句
* @param args
*/
query<T>(sql: string, ...args: any[]): Array<T> {
let startTime = Date.now()
@ -80,6 +69,7 @@ export class DataBase {
/**
* SQL更新
* @param sql SQL语句
* @param args
*/
update(sql: string, ...args: any[]): number {
let startTime = Date.now()
@ -88,6 +78,16 @@ export class DataBase {
return result
}
/**
* SQL语句
* @param sql SQL语句
*/
execute(sql: string): void {
let startTime = Date.now()
this.jdbcTemplate.execute(sql)
console.debug(java.lang.String.format(`\n[DB] execute \nSQL : sql} \nCOST : ${Date.now() - startTime}ms`))
}
close() {
//@ts-ignore
this.dataSource.close()

View File

@ -1,12 +1,17 @@
import { database } from '@ccms/api'
import { provideSingleton } from '@ccms/container'
import { DataBase, DataBaseConfig } from './database'
import { DataBase } from './database'
@provideSingleton(database.DataBaseManager)
export class DataBaseManager {
export class DataBaseManager extends database.DataBaseManager {
private mainDatabase: DataBase
private databases = new Map<string, DataBase>()
constructor() {
super()
process.on('exit', () => this.disable())
}
/**
*
* @param mainDatabase
@ -29,8 +34,8 @@ export class DataBaseManager {
* @param name database Name use at code
* @param config
*/
createDatabase(name: string, config: DataBaseConfig) {
Java.synchronized(() => {
createDatabase(name: string, config: database.DataBaseConfig) {
return Java.synchronized(() => {
if (!this.databases.has(name)) {
this.databases.set(name, new DataBase(config))
}
@ -38,6 +43,13 @@ export class DataBaseManager {
}, this.databases)()
}
removeDatabase(name: string) {
if (this.databases.has(name)) {
this.databases.get(name).close()
}
return this.databases.delete(name)
}
getDatabase(name: string) {
return this.databases.get(name)
}