@@ -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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user