feat: refactor database
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
4ee8fc9a20
commit
47413c6766
@ -1,5 +1,45 @@
|
|||||||
|
import { injectable } from "@ccms/container"
|
||||||
|
|
||||||
export namespace database {
|
export namespace database {
|
||||||
export const DataBaseManager = Symbol("DataBaseManager");
|
export const DataSource = Symbol("DataSource")
|
||||||
export const DataSource = Symbol("DataSource");
|
/**
|
||||||
export const DataBase = Symbol("DataBase");
|
* 数据库配置
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ export function loadMavenDepend(groupId: string, artifactId: string, version: st
|
|||||||
let dependencies = doc.getElementsByTagName("dependency")
|
let dependencies = doc.getElementsByTagName("dependency")
|
||||||
let size = dependencies.length
|
let size = dependencies.length
|
||||||
if (!size) { return }
|
if (!size) { return }
|
||||||
console.info(key, 'found', size, 'dependencies loading...')
|
console.debug(key, 'found', size, 'dependencies loading...')
|
||||||
for (let i = 0; i < size; i++) {
|
for (let i = 0; i < size; i++) {
|
||||||
const dependency = dependencies.item(i)
|
const dependency = dependencies.item(i)
|
||||||
const gav = dependency.getChildNodes()
|
const gav = dependency.getChildNodes()
|
||||||
|
@ -115,13 +115,11 @@ function loadMiaoScriptConfig() {
|
|||||||
if (!fs.exists(configFile)) {
|
if (!fs.exists(configFile)) {
|
||||||
global.ScriptEngineConfig = base.save(configFile, yaml.dump({
|
global.ScriptEngineConfig = base.save(configFile, yaml.dump({
|
||||||
uuid: UUID.randomUUID().toString(),
|
uuid: UUID.randomUUID().toString(),
|
||||||
channel: 'latest',
|
|
||||||
slow_execute: 50
|
slow_execute: 50
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
global.ScriptEngineConfig = yaml.load(base.read(configFile))
|
global.ScriptEngineConfig = yaml.load(base.read(configFile))
|
||||||
}
|
}
|
||||||
global.ScriptEngineChannel = global.ScriptEngineConfig.channel || 'latest'
|
|
||||||
global.ScriptSlowExecuteTime = global.ScriptEngineConfig.slow_execute || 50
|
global.ScriptSlowExecuteTime = global.ScriptEngineConfig.slow_execute || 50
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,32 +1,12 @@
|
|||||||
import { JSClass } from '@ccms/container'
|
import { database } from '@ccms/api'
|
||||||
import { Model } from './model'
|
import { JSClass, postConstruct } from '@ccms/container'
|
||||||
|
|
||||||
/**
|
const Properties = Java.type('java.util.Properties')
|
||||||
* 数据库配置
|
|
||||||
*/
|
|
||||||
export interface DataBaseConfig {
|
|
||||||
/**
|
|
||||||
* 数据库连接串
|
|
||||||
*/
|
|
||||||
url: string | javax.sql.DataSource
|
|
||||||
/**
|
|
||||||
* 数据库驱动
|
|
||||||
*/
|
|
||||||
driverClassName?: string
|
|
||||||
/**
|
|
||||||
* 用户名
|
|
||||||
*/
|
|
||||||
username?: string
|
|
||||||
/**
|
|
||||||
* 密码
|
|
||||||
*/
|
|
||||||
password?: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库封装类
|
* 数据库封装类
|
||||||
*/
|
*/
|
||||||
export class DataBase {
|
export class DataBase extends database.DataBase {
|
||||||
private dataSource: javax.sql.DataSource
|
private dataSource: javax.sql.DataSource
|
||||||
private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
|
private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
|
||||||
|
|
||||||
@ -37,13 +17,13 @@ export class DataBase {
|
|||||||
@JSClass('org.springframework.jdbc.core.JdbcTemplate')
|
@JSClass('org.springframework.jdbc.core.JdbcTemplate')
|
||||||
private JdbcTemplate: typeof 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!') }
|
if (!dbConfig.url) { throw new Error('DataBase url can\'t be null!') }
|
||||||
this.createDataSource(dbConfig)
|
this.createDataSource(dbConfig)
|
||||||
this.initialize()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private createDataSource(dbConfig: DataBaseConfig) {
|
private createDataSource(dbConfig: database.DataBaseConfig) {
|
||||||
if (typeof dbConfig.url === "string") {
|
if (typeof dbConfig.url === "string") {
|
||||||
let config = new this.HikariConfig()
|
let config = new this.HikariConfig()
|
||||||
if (dbConfig.driverClassName) {
|
if (dbConfig.driverClassName) {
|
||||||
@ -56,12 +36,20 @@ export class DataBase {
|
|||||||
config.setPassword(dbConfig.password)
|
config.setPassword(dbConfig.password)
|
||||||
}
|
}
|
||||||
config.setJdbcUrl(dbConfig.url)
|
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)
|
this.dataSource = new this.HikariDataSource(config)
|
||||||
} else {
|
} else {
|
||||||
this.dataSource = dbConfig.url
|
this.dataSource = dbConfig.url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@postConstruct()
|
||||||
private initialize() {
|
private initialize() {
|
||||||
this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
|
this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
|
||||||
}
|
}
|
||||||
@ -69,6 +57,7 @@ export class DataBase {
|
|||||||
/**
|
/**
|
||||||
* 执行SQL查询
|
* 执行SQL查询
|
||||||
* @param sql SQL语句
|
* @param sql SQL语句
|
||||||
|
* @param args 参数
|
||||||
*/
|
*/
|
||||||
query<T>(sql: string, ...args: any[]): Array<T> {
|
query<T>(sql: string, ...args: any[]): Array<T> {
|
||||||
let startTime = Date.now()
|
let startTime = Date.now()
|
||||||
@ -80,6 +69,7 @@ export class DataBase {
|
|||||||
/**
|
/**
|
||||||
* 执行SQL更新
|
* 执行SQL更新
|
||||||
* @param sql SQL语句
|
* @param sql SQL语句
|
||||||
|
* @param args 参数
|
||||||
*/
|
*/
|
||||||
update(sql: string, ...args: any[]): number {
|
update(sql: string, ...args: any[]): number {
|
||||||
let startTime = Date.now()
|
let startTime = Date.now()
|
||||||
@ -88,6 +78,16 @@ export class DataBase {
|
|||||||
return result
|
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() {
|
close() {
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
this.dataSource.close()
|
this.dataSource.close()
|
||||||
|
@ -1,12 +1,17 @@
|
|||||||
import { database } from '@ccms/api'
|
import { database } from '@ccms/api'
|
||||||
import { provideSingleton } from '@ccms/container'
|
import { provideSingleton } from '@ccms/container'
|
||||||
import { DataBase, DataBaseConfig } from './database'
|
import { DataBase } from './database'
|
||||||
|
|
||||||
@provideSingleton(database.DataBaseManager)
|
@provideSingleton(database.DataBaseManager)
|
||||||
export class DataBaseManager {
|
export class DataBaseManager extends database.DataBaseManager {
|
||||||
private mainDatabase: DataBase
|
private mainDatabase: DataBase
|
||||||
private databases = new Map<string, DataBase>()
|
private databases = new Map<string, DataBase>()
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
process.on('exit', () => this.disable())
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置主数据库
|
* 设置主数据库
|
||||||
* @param mainDatabase 主数据库
|
* @param mainDatabase 主数据库
|
||||||
@ -29,8 +34,8 @@ export class DataBaseManager {
|
|||||||
* @param name 数据库名称 用于代码 database Name use at code
|
* @param name 数据库名称 用于代码 database Name use at code
|
||||||
* @param config 数据库配置
|
* @param config 数据库配置
|
||||||
*/
|
*/
|
||||||
createDatabase(name: string, config: DataBaseConfig) {
|
createDatabase(name: string, config: database.DataBaseConfig) {
|
||||||
Java.synchronized(() => {
|
return Java.synchronized(() => {
|
||||||
if (!this.databases.has(name)) {
|
if (!this.databases.has(name)) {
|
||||||
this.databases.set(name, new DataBase(config))
|
this.databases.set(name, new DataBase(config))
|
||||||
}
|
}
|
||||||
@ -38,6 +43,13 @@ export class DataBaseManager {
|
|||||||
}, this.databases)()
|
}, this.databases)()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeDatabase(name: string) {
|
||||||
|
if (this.databases.has(name)) {
|
||||||
|
this.databases.get(name).close()
|
||||||
|
}
|
||||||
|
return this.databases.delete(name)
|
||||||
|
}
|
||||||
|
|
||||||
getDatabase(name: string) {
|
getDatabase(name: string) {
|
||||||
return this.databases.get(name)
|
return this.databases.get(name)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user