diff --git a/packages/api/package.json b/packages/api/package.json index 90826d16..401737df 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -21,6 +21,7 @@ "dependencies": { "@ccms/container": "^0.7.0", "@ccms/ployfill": "^0.7.0", + "@ccms/common": "^0.7.0", "base64-js": "^1.3.1", "source-map-builder": "^0.0.7" }, @@ -30,4 +31,4 @@ "rimraf": "^3.0.2", "typescript": "^3.9.2" } -} +} \ No newline at end of file diff --git a/packages/api/src/interfaces/database.ts b/packages/api/src/interfaces/database.ts new file mode 100644 index 00000000..cca6850f --- /dev/null +++ b/packages/api/src/interfaces/database.ts @@ -0,0 +1,5 @@ +export namespace database { + export const DataBaseManager = Symbol("DataBaseManager"); + export const DataSource = Symbol("DataSource"); + export const DataBase = Symbol("DataBase"); +} diff --git a/packages/api/src/interfaces/index.ts b/packages/api/src/interfaces/index.ts index 95f34fe3..437c0999 100644 --- a/packages/api/src/interfaces/index.ts +++ b/packages/api/src/interfaces/index.ts @@ -1,2 +1,3 @@ export * from './plugin' export * from './server' +export * from './database' \ No newline at end of file diff --git a/packages/database/.npmignore b/packages/database/.npmignore new file mode 120000 index 00000000..b4359f69 --- /dev/null +++ b/packages/database/.npmignore @@ -0,0 +1 @@ +../../.npmignore \ No newline at end of file diff --git a/packages/database/package.json b/packages/database/package.json new file mode 100644 index 00000000..a0e707c4 --- /dev/null +++ b/packages/database/package.json @@ -0,0 +1,30 @@ +{ + "name": "@ccms/database", + "version": "0.6.7", + "description": "MiaoScript database package", + "keywords": [ + "miaoscript", + "minecraft", + "bukkit", + "sponge" + ], + "author": "MiaoWoo ", + "homepage": "https://github.com/circlecloud/ms.git", + "license": "ISC", + "main": "dist/index.js", + "scripts": { + "clean": "rimraf dist", + "watch": "tsc --watch", + "build": "yarn clean && tsc", + "test": "echo \"Error: run tests from root\" && exit 1" + }, + "devDependencies": { + "reflect-metadata": "^0.1.13", + "rimraf": "^3.0.2", + "typescript": "^3.9.2" + }, + "dependencies": { + "@ccms/api": "^0.7.0", + "@ccms/container": "^0.7.0" + } +} \ No newline at end of file diff --git a/packages/database/src/database.ts b/packages/database/src/database.ts new file mode 100644 index 00000000..48032d51 --- /dev/null +++ b/packages/database/src/database.ts @@ -0,0 +1,51 @@ +/// + +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') + +/** + * 数据库封装类 + */ +export class DataBase { + private dataSource: javax.sql.DataSource + private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate + + constructor(url: string | javax.sql.DataSource, username?: string, password?: string) { + if (!url) { throw new Error('DataBase url can\'t be null!') } + this.createDataSource(url, username, password) + this.initialize() + } + + private createDataSource(url: string | javax.sql.DataSource, username?: string, password?: string) { + if (typeof url === "string") { + if (!username || !password) { + throw new Error('DataBase username or password can\'t be null!') + } + let config = new HikariConfig() + config.setUsername(username) + config.setPassword(password) + config.setJdbcUrl(url) + this.dataSource = new HikariDataSource(config) + } else { + this.dataSource = url + } + } + + private initialize() { + this.jdbcTemplate = new JdbcTemplate(this.dataSource) + } + + /** + * 执行SQL查询 + * @param sql SQL语句 + */ + query(sql: string, ...args: any[]): Array { + return this.jdbcTemplate.queryForList(sql, args) + } + + close() { + //@ts-ignore + this.dataSource.close() + } +} diff --git a/packages/database/src/index.ts b/packages/database/src/index.ts new file mode 100644 index 00000000..86de9412 --- /dev/null +++ b/packages/database/src/index.ts @@ -0,0 +1,5 @@ +/// +/// + +export * from './database' +export * from './manager' \ No newline at end of file diff --git a/packages/database/src/manager.ts b/packages/database/src/manager.ts new file mode 100644 index 00000000..84cfb07c --- /dev/null +++ b/packages/database/src/manager.ts @@ -0,0 +1,45 @@ +import { plugin, database } from '@ccms/api' +import { provideSingleton, inject, postConstruct, Container, ContainerInstance } from '@ccms/container' +import { DataBase } from './database' + +@provideSingleton(database.DataBaseManager) +export class DataBaseManager { + @inject(ContainerInstance) + private container: Container + @inject(plugin.PluginInstance) + private instance: any + + private beanFactory: any + private mainDatabase: DataBase + private databases: { [key: string]: DataBase } = {} + + @postConstruct() + initialize() { + try { + this.beanFactory = this.instance.getAutowireCapableBeanFactory() + let mainDatasource = this.beanFactory.getBean(Packages.javax.sql.DataSource.class) + this.mainDatabase = new DataBase(mainDatasource) + } catch (error) { + console.ex(error) + } + } + + getMainDatabase() { + return this.mainDatabase + } + + createDatabase(name: string, url: string, username: string, password: string) { + Java.synchronized(() => { + if (this.databases[name]) return this.databases[name] + return this.databases[name] = new DataBase(url, username, password) + }, this.databases)() + } + + getDatabase(name: string) { + return this.databases[name] + } + + disable() { + Object.values(this.databases).forEach((ds) => ds?.close()) + } +} diff --git a/packages/database/tsconfig.json b/packages/database/tsconfig.json new file mode 100644 index 00000000..7aae5d2b --- /dev/null +++ b/packages/database/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file