2020-06-02 10:00:26 +00:00
|
|
|
/// <reference types="@ccms/types/dist/typings/spring" />
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
import { Model } from './model'
|
|
|
|
|
2020-06-02 10:00:26 +00:00
|
|
|
const HikariDataSource = Java.type('com.zaxxer.hikari.HikariDataSource')
|
|
|
|
const HikariConfig = Java.type('com.zaxxer.hikari.HikariConfig')
|
|
|
|
const JdbcTemplate = Java.type('org.springframework.jdbc.core.JdbcTemplate')
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
/**
|
|
|
|
* 数据库配置
|
|
|
|
*/
|
|
|
|
export interface DataBaseConfig {
|
|
|
|
/**
|
|
|
|
* 数据库连接串
|
|
|
|
*/
|
|
|
|
url: string | javax.sql.DataSource
|
|
|
|
/**
|
|
|
|
* 数据库驱动
|
|
|
|
*/
|
|
|
|
driverClassName?: string
|
|
|
|
/**
|
|
|
|
* 用户名
|
|
|
|
*/
|
|
|
|
username?: string
|
|
|
|
/**
|
|
|
|
* 密码
|
|
|
|
*/
|
|
|
|
password?: string
|
|
|
|
}
|
|
|
|
|
2020-06-02 10:00:26 +00:00
|
|
|
/**
|
|
|
|
* 数据库封装类
|
|
|
|
*/
|
|
|
|
export class DataBase {
|
|
|
|
private dataSource: javax.sql.DataSource
|
|
|
|
private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
constructor(dbConfig: DataBaseConfig) {
|
|
|
|
if (!dbConfig.url) { throw new Error('DataBase url can\'t be null!') }
|
|
|
|
this.createDataSource(dbConfig)
|
2020-06-02 10:00:26 +00:00
|
|
|
this.initialize()
|
|
|
|
}
|
|
|
|
|
2020-06-20 08:39:03 +00:00
|
|
|
private createDataSource(dbConfig: DataBaseConfig) {
|
|
|
|
if (typeof dbConfig.url === "string") {
|
|
|
|
if (!dbConfig.username || !dbConfig.password) {
|
2020-06-02 10:00:26 +00:00
|
|
|
throw new Error('DataBase username or password can\'t be null!')
|
|
|
|
}
|
|
|
|
let config = new HikariConfig()
|
2020-06-20 08:39:03 +00:00
|
|
|
if (dbConfig.driverClassName) {
|
|
|
|
config.setDriverClassName(dbConfig.driverClassName)
|
|
|
|
}
|
|
|
|
config.setUsername(dbConfig.username)
|
|
|
|
config.setPassword(dbConfig.password)
|
|
|
|
config.setJdbcUrl(dbConfig.url)
|
2020-06-02 10:00:26 +00:00
|
|
|
this.dataSource = new HikariDataSource(config)
|
|
|
|
} else {
|
2020-06-20 08:39:03 +00:00
|
|
|
this.dataSource = dbConfig.url
|
2020-06-02 10:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private initialize() {
|
|
|
|
this.jdbcTemplate = new JdbcTemplate(this.dataSource)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 执行SQL查询
|
|
|
|
* @param sql SQL语句
|
|
|
|
*/
|
|
|
|
query<T>(sql: string, ...args: any[]): Array<T> {
|
2020-06-20 08:39:03 +00:00
|
|
|
let startTime = Date.now()
|
|
|
|
let result = Java.from(this.jdbcTemplate.queryForList(sql, args))
|
|
|
|
console.debug(java.lang.String.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 执行SQL更新
|
|
|
|
* @param sql SQL语句
|
|
|
|
*/
|
|
|
|
update(sql: string, ...args: any[]): number {
|
|
|
|
let startTime = Date.now()
|
|
|
|
let result = this.jdbcTemplate.update(sql, args)
|
|
|
|
console.debug(java.lang.String.format(`\n[DB] update \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
|
|
|
|
return result
|
2020-06-02 10:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
//@ts-ignore
|
|
|
|
this.dataSource.close()
|
|
|
|
}
|
|
|
|
}
|