fdb2 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +21 -21
- package/.editorconfig +11 -11
- package/.eslintrc.cjs +14 -14
- package/.eslintrc.json +7 -7
- package/.prettierrc.js +3 -3
- package/.tpl.env +21 -21
- package/.vscodeignore +45 -45
- package/README.md +312 -312
- package/bin/build.sh +28 -28
- package/bin/deploy.sh +8 -8
- package/bin/dev.sh +10 -10
- package/bin/docker/dev-docker-compose.yml +43 -43
- package/bin/docker/dev.Dockerfile +24 -24
- package/bin/docker/prod-docker-compose.yml +17 -17
- package/bin/docker/prod.Dockerfile +29 -29
- package/bin/fdb2.js +220 -220
- package/dist/package.json +29 -29
- package/dist/pnpm-lock.yaml +1042 -354
- package/dist/public/explorer.css +1464 -1437
- package/dist/public/explorer.js +759 -223
- package/dist/public/index.css +1026 -1026
- package/dist/public/index.js +15 -9
- package/dist/public/layout.css +221 -221
- package/dist/public/layout.js +1 -1
- package/dist/public/vue.js +8 -2
- package/dist/scripts/preinstall.js +112 -112
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +8 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.ts +680 -671
- package/dist/server/model/connection.entity.ts +65 -65
- package/dist/server/model/database.entity.ts +245 -245
- package/dist/server/service/connection.service.d.ts +6 -1
- package/dist/server/service/connection.service.d.ts.map +1 -1
- package/dist/server/service/connection.service.js +15 -0
- package/dist/server/service/connection.service.js.map +1 -1
- package/dist/server/service/connection.service.ts +356 -341
- package/dist/server/service/database/base.service.d.ts +27 -0
- package/dist/server/service/database/base.service.d.ts.map +1 -1
- package/dist/server/service/database/base.service.js +17 -0
- package/dist/server/service/database/base.service.js.map +1 -1
- package/dist/server/service/database/base.service.ts +406 -367
- package/dist/server/service/database/cockroachdb.service.d.ts +16 -0
- package/dist/server/service/database/cockroachdb.service.d.ts.map +1 -1
- package/dist/server/service/database/cockroachdb.service.js +220 -154
- package/dist/server/service/database/cockroachdb.service.js.map +1 -1
- package/dist/server/service/database/cockroachdb.service.ts +871 -782
- package/dist/server/service/database/database.service.d.ts +4 -0
- package/dist/server/service/database/database.service.d.ts.map +1 -1
- package/dist/server/service/database/database.service.js +123 -0
- package/dist/server/service/database/database.service.js.map +1 -1
- package/dist/server/service/database/database.service.ts +775 -638
- package/dist/server/service/database/index.ts +6 -6
- package/dist/server/service/database/mongodb.service.d.ts +16 -0
- package/dist/server/service/database/mongodb.service.d.ts.map +1 -1
- package/dist/server/service/database/mongodb.service.js +35 -0
- package/dist/server/service/database/mongodb.service.js.map +1 -1
- package/dist/server/service/database/mongodb.service.ts +39 -1
- package/dist/server/service/database/mssql.service.d.ts +16 -0
- package/dist/server/service/database/mssql.service.d.ts.map +1 -1
- package/dist/server/service/database/mssql.service.js +168 -96
- package/dist/server/service/database/mssql.service.js.map +1 -1
- package/dist/server/service/database/mssql.service.ts +931 -840
- package/dist/server/service/database/mysql.service.d.ts +16 -0
- package/dist/server/service/database/mysql.service.d.ts.map +1 -1
- package/dist/server/service/database/mysql.service.js +189 -80
- package/dist/server/service/database/mysql.service.js.map +1 -1
- package/dist/server/service/database/mysql.service.ts +1025 -890
- package/dist/server/service/database/oracle.service.d.ts +16 -0
- package/dist/server/service/database/oracle.service.d.ts.map +1 -1
- package/dist/server/service/database/oracle.service.js +182 -120
- package/dist/server/service/database/oracle.service.js.map +1 -1
- package/dist/server/service/database/oracle.service.ts +1035 -959
- package/dist/server/service/database/postgres.service.d.ts +16 -0
- package/dist/server/service/database/postgres.service.d.ts.map +1 -1
- package/dist/server/service/database/postgres.service.js +154 -88
- package/dist/server/service/database/postgres.service.js.map +1 -1
- package/dist/server/service/database/postgres.service.ts +960 -871
- package/dist/server/service/database/sap.service.d.ts +16 -0
- package/dist/server/service/database/sap.service.d.ts.map +1 -1
- package/dist/server/service/database/sap.service.js +66 -0
- package/dist/server/service/database/sap.service.js.map +1 -1
- package/dist/server/service/database/sap.service.ts +89 -0
- package/dist/server/service/database/sqlite.service.d.ts +16 -0
- package/dist/server/service/database/sqlite.service.d.ts.map +1 -1
- package/dist/server/service/database/sqlite.service.js +77 -18
- package/dist/server/service/database/sqlite.service.js.map +1 -1
- package/dist/server/service/database/sqlite.service.ts +787 -708
- package/dist/server/service/session.service.ts +158 -158
- package/dist/view/index.html +38 -38
- package/env.d.ts +1 -1
- package/package.json +1 -1
- package/packages/vscode/.vscodeignore +44 -44
- package/packages/vscode/README.md +62 -62
- package/packages/vscode/out/database-services/cockroachdb.service.js +154 -154
- package/packages/vscode/out/database-services/mssql.service.js +96 -96
- package/packages/vscode/out/database-services/mysql.service.js +80 -80
- package/packages/vscode/out/database-services/oracle.service.js +120 -120
- package/packages/vscode/out/database-services/postgres.service.js +88 -88
- package/packages/vscode/out/database-services/sqlite.service.js +18 -18
- package/packages/vscode/out/provider/WebViewProvider.js +32 -32
- package/packages/vscode/package.json +142 -142
- package/packages/vscode/resources/icon.svg +5 -5
- package/packages/vscode/resources/webview/connection.css +41 -41
- package/packages/vscode/resources/webview/database.css +163 -163
- package/packages/vscode/resources/webview/index.html +9 -9
- package/packages/vscode/resources/webview/modules/header.tpl +13 -13
- package/packages/vscode/resources/webview/modules/initial_state.tpl +54 -54
- package/packages/vscode/resources/webview/query.css +104 -104
- package/packages/vscode/src/database-services/base.service.ts +362 -362
- package/packages/vscode/src/database-services/cockroachdb.service.ts +659 -659
- package/packages/vscode/src/database-services/connection.service.ts +340 -340
- package/packages/vscode/src/database-services/database.service.ts +629 -629
- package/packages/vscode/src/database-services/index.ts +6 -6
- package/packages/vscode/src/database-services/model/connection.entity.ts +65 -65
- package/packages/vscode/src/database-services/model/database.entity.ts +245 -245
- package/packages/vscode/src/database-services/mssql.service.ts +722 -722
- package/packages/vscode/src/database-services/mysql.service.ts +760 -760
- package/packages/vscode/src/database-services/oracle.service.ts +831 -831
- package/packages/vscode/src/database-services/postgres.service.ts +740 -740
- package/packages/vscode/src/database-services/sqlite.service.ts +558 -558
- package/packages/vscode/src/extension.ts +76 -76
- package/packages/vscode/src/provider/DatabaseTreeProvider.ts +167 -167
- package/packages/vscode/src/provider/WebViewProvider.ts +277 -277
- package/packages/vscode/src/service/DatabaseServiceBridge.ts +414 -414
- package/packages/vscode/src/typings/connection.ts +90 -90
- package/packages/vscode/tsconfig.json +21 -21
- package/public/index.html +9 -9
- package/public/modules/header.tpl +13 -13
- package/public/modules/initial_state.tpl +54 -54
- package/scripts/preinstall.js +112 -112
- package/server/index.ts +680 -671
- package/server/model/connection.entity.ts +65 -65
- package/server/model/database.entity.ts +245 -245
- package/server/service/connection.service.ts +356 -341
- package/server/service/database/base.service.ts +406 -367
- package/server/service/database/cockroachdb.service.ts +871 -782
- package/server/service/database/database.service.ts +775 -638
- package/server/service/database/index.ts +6 -6
- package/server/service/database/mongodb.service.ts +39 -1
- package/server/service/database/mssql.service.ts +931 -840
- package/server/service/database/mysql.service.ts +1025 -890
- package/server/service/database/oracle.service.ts +1035 -959
- package/server/service/database/postgres.service.ts +960 -871
- package/server/service/database/sap.service.ts +89 -0
- package/server/service/database/sqlite.service.ts +787 -708
- package/server/service/session.service.ts +158 -158
- package/server/tsconfig.json +20 -20
- package/server.js +149 -149
- package/server.pid +1 -0
- package/src/adapter/ajax.ts +135 -135
- package/src/assets/base.css +1 -1
- package/src/assets/database.css +949 -949
- package/src/assets/images/svg/illustrations/illustration-1.svg +1 -1
- package/src/assets/images/svg/illustrations/illustration-2.svg +2 -2
- package/src/assets/images/svg/illustrations/illustration-3.svg +50 -50
- package/src/assets/images/svg/illustrations/illustration-4.svg +1 -1
- package/src/assets/images/svg/illustrations/illustration-5.svg +73 -73
- package/src/assets/images/svg/illustrations/illustration-6.svg +89 -89
- package/src/assets/images/svg/illustrations/illustration-7.svg +39 -39
- package/src/assets/images/svg/separators/curve-2.svg +3 -3
- package/src/assets/images/svg/separators/curve.svg +3 -3
- package/src/assets/images/svg/separators/line.svg +3 -3
- package/src/assets/logo.svg +73 -73
- package/src/assets/main.css +1 -1
- package/src/base/config.ts +20 -20
- package/src/base/detect.ts +134 -134
- package/src/base/entity.ts +92 -92
- package/src/base/eventBus.ts +36 -36
- package/src/components/connection-editor/index.vue +588 -588
- package/src/components/dataGrid/index.vue +104 -104
- package/src/components/dataGrid/pagination.vue +105 -105
- package/src/components/loading/index.vue +42 -42
- package/src/components/modal/index.ts +180 -180
- package/src/components/modal/index.vue +560 -560
- package/src/components/toast/index.ts +43 -43
- package/src/components/toast/toast.vue +57 -57
- package/src/components/user/name.vue +103 -103
- package/src/components/user/selector.vue +416 -416
- package/src/domain/SysConfig.ts +74 -74
- package/src/platform/App.vue +7 -7
- package/src/platform/database/components/connection-detail.vue +1153 -1154
- package/src/platform/database/components/data-editor.vue +477 -477
- package/src/platform/database/components/database-detail.vue +1173 -1172
- package/src/platform/database/components/database-monitor.vue +1085 -1085
- package/src/platform/database/components/db-tools.vue +1264 -816
- package/src/platform/database/components/query-history.vue +1348 -1348
- package/src/platform/database/components/sql-executor.vue +737 -737
- package/src/platform/database/components/sql-query-editor.vue +1045 -1045
- package/src/platform/database/components/table-detail.vue +1375 -1376
- package/src/platform/database/components/table-editor.vue +916 -916
- package/src/platform/database/explorer.vue +1839 -1839
- package/src/platform/database/index.vue +1192 -1192
- package/src/platform/database/layout.vue +366 -366
- package/src/platform/database/router.ts +36 -36
- package/src/platform/database/styles/common.scss +601 -601
- package/src/platform/database/types/common.ts +444 -444
- package/src/platform/database/utils/export.ts +231 -231
- package/src/platform/database/utils/helpers.ts +436 -436
- package/src/platform/index.ts +32 -32
- package/src/platform/router.ts +40 -40
- package/src/platform/vscode/bridge.ts +121 -121
- package/src/platform/vscode/components/ConnectionPanel.vue +272 -272
- package/src/platform/vscode/components/DatabasePanel.vue +532 -532
- package/src/platform/vscode/components/QueryPanel.vue +371 -371
- package/src/platform/vscode/entry/connection.ts +13 -13
- package/src/platform/vscode/entry/database.ts +13 -13
- package/src/platform/vscode/entry/query.ts +13 -13
- package/src/platform/vscode/index.ts +5 -5
- package/src/service/base.ts +133 -127
- package/src/service/database.ts +505 -495
- package/src/service/login.ts +120 -120
- package/src/shims-vue.d.ts +6 -6
- package/src/stores/connection.ts +266 -266
- package/src/stores/session.ts +87 -87
- package/src/typings/database-types.ts +412 -412
- package/src/typings/database.ts +363 -363
- package/src/typings/global.d.ts +58 -58
- package/src/typings/pinia.d.ts +7 -7
- package/src/utils/clipboard.ts +29 -29
- package/src/utils/database-types.ts +242 -242
- package/src/utils/modal.ts +123 -123
- package/src/utils/request.ts +55 -55
- package/src/utils/sleep.ts +3 -3
- package/src/utils/toast.ts +73 -73
- package/src/utils/util.ts +171 -171
- package/src/utils/xlsx.ts +228 -228
- package/tsconfig.json +33 -33
- package/view/index.html +9 -9
- package/view/modules/header.tpl +13 -13
- package/view/modules/initial_state.tpl +19 -19
- package/vite.config.ts +424 -424
- package/vite.config.vscode.ts +47 -47
- package/fdb2.server.pid +0 -1
- package/server/backups/db_ai_breakout_2026-03-11T08-38-48-677Z.sql +0 -0
|
@@ -1,414 +1,414 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 数据库服务桥接层
|
|
3
|
-
* 用于 VSCode 扩展调用 server 目录下的现有数据库服务
|
|
4
|
-
* 为了简化部署,这里直接实现必要的功能,复用 server 目录的逻辑
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as fs from 'fs';
|
|
8
|
-
import * as path from 'path';
|
|
9
|
-
import * as os from 'os';
|
|
10
|
-
import { DataSource, type DataSourceOptions } from 'typeorm';
|
|
11
|
-
|
|
12
|
-
// 引入数据库服务接口(从本地复制的服务)
|
|
13
|
-
import { MySQLService } from '../database-services/mysql.service';
|
|
14
|
-
import { PostgreSQLService } from '../database-services/postgres.service';
|
|
15
|
-
import { SQLiteService } from '../database-services/sqlite.service';
|
|
16
|
-
import { OracleService } from '../database-services/oracle.service';
|
|
17
|
-
import { SQLServerService } from '../database-services/mssql.service';
|
|
18
|
-
import { CockroachDBService } from '../database-services/cockroachdb.service';
|
|
19
|
-
import { MongoDBService } from '../database-services/mongodb.service';
|
|
20
|
-
import { SAPHANADatabaseService } from '../database-services/sap.service';
|
|
21
|
-
import { BaseDatabaseService } from '../database-services/base.service';
|
|
22
|
-
import { ConnectionEntity } from '../database-services/model/connection.entity';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 数据库服务桥接类
|
|
26
|
-
* 直接复用 server 目录下的服务
|
|
27
|
-
*/
|
|
28
|
-
export class DatabaseServiceBridge {
|
|
29
|
-
/**
|
|
30
|
-
* 连接配置文件路径
|
|
31
|
-
*/
|
|
32
|
-
private readonly configPath: string;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 活跃的数据库连接实例
|
|
36
|
-
*/
|
|
37
|
-
private activeConnections: Map<string, DataSource> = new Map();
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 数据库服务实例
|
|
41
|
-
*/
|
|
42
|
-
private mysqlService: MySQLService;
|
|
43
|
-
private postgreSQLService: PostgreSQLService;
|
|
44
|
-
private sqliteService: SQLiteService;
|
|
45
|
-
private oracleService: OracleService;
|
|
46
|
-
private sqlServerService: SQLServerService;
|
|
47
|
-
private cockroachDBService: CockroachDBService;
|
|
48
|
-
private mongoDBService: MongoDBService;
|
|
49
|
-
private sapHANADatabaseService: SAPHANADatabaseService;
|
|
50
|
-
|
|
51
|
-
constructor() {
|
|
52
|
-
const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
|
|
53
|
-
this.configPath = path.join(dataDir, 'connections.json');
|
|
54
|
-
|
|
55
|
-
this.mysqlService = new MySQLService();
|
|
56
|
-
this.postgreSQLService = new PostgreSQLService();
|
|
57
|
-
this.sqliteService = new SQLiteService();
|
|
58
|
-
this.oracleService = new OracleService();
|
|
59
|
-
this.sqlServerService = new SQLServerService();
|
|
60
|
-
this.cockroachDBService = new CockroachDBService();
|
|
61
|
-
this.mongoDBService = new MongoDBService();
|
|
62
|
-
this.sapHANADatabaseService = new SAPHANADatabaseService();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 初始化服务
|
|
67
|
-
*/
|
|
68
|
-
async init() {
|
|
69
|
-
const configDir = path.dirname(this.configPath);
|
|
70
|
-
if (!fs.existsSync(configDir)) {
|
|
71
|
-
fs.mkdirSync(configDir, { recursive: true });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* 连接管理接口
|
|
77
|
-
*/
|
|
78
|
-
get connection() {
|
|
79
|
-
return {
|
|
80
|
-
getAllConnections: this.getAllConnections.bind(this),
|
|
81
|
-
getConnectionById: this.getConnectionById.bind(this),
|
|
82
|
-
addConnection: this.addConnection.bind(this),
|
|
83
|
-
updateConnection: this.updateConnection.bind(this),
|
|
84
|
-
deleteConnection: this.deleteConnection.bind(this),
|
|
85
|
-
testConnection: this.testConnection.bind(this)
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* 数据库管理接口
|
|
91
|
-
*/
|
|
92
|
-
get database() {
|
|
93
|
-
return {
|
|
94
|
-
getDatabases: this.getDatabases.bind(this),
|
|
95
|
-
getTables: this.getTables.bind(this),
|
|
96
|
-
executeQuery: this.executeQuery.bind(this),
|
|
97
|
-
getViews: this.getViews.bind(this),
|
|
98
|
-
getProcedures: this.getProcedures.bind(this)
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 连接管理实现
|
|
104
|
-
*/
|
|
105
|
-
private async getAllConnections(): Promise<ConnectionEntity[]> {
|
|
106
|
-
try {
|
|
107
|
-
if (!fs.existsSync(this.configPath)) {
|
|
108
|
-
return [];
|
|
109
|
-
}
|
|
110
|
-
const data = fs.readFileSync(this.configPath, 'utf8');
|
|
111
|
-
return JSON.parse(data) as ConnectionEntity[];
|
|
112
|
-
} catch (error) {
|
|
113
|
-
console.error('读取连接配置失败:', error);
|
|
114
|
-
return [];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
private async getConnectionById(id: string): Promise<ConnectionEntity | null> {
|
|
119
|
-
const connections = await this.getAllConnections();
|
|
120
|
-
return connections.find(conn => conn.id === id) || null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private async addConnection(connection: ConnectionEntity): Promise<ConnectionEntity> {
|
|
124
|
-
const connections = await this.getAllConnections();
|
|
125
|
-
|
|
126
|
-
if (connections.find(conn => conn.name === connection.name)) {
|
|
127
|
-
throw new Error('连接名称已存在');
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
connection.id = this.generateId();
|
|
131
|
-
connection.createdAt = new Date();
|
|
132
|
-
connection.updatedAt = new Date();
|
|
133
|
-
connection.enabled = connection.enabled !== undefined ? connection.enabled : true;
|
|
134
|
-
|
|
135
|
-
connections.push(connection);
|
|
136
|
-
await this.saveConnections(connections);
|
|
137
|
-
|
|
138
|
-
return connection;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private async updateConnection(id: string, updates: Partial<ConnectionEntity>): Promise<ConnectionEntity> {
|
|
142
|
-
const connections = await this.getAllConnections();
|
|
143
|
-
const index = connections.findIndex(conn => conn.id === id);
|
|
144
|
-
|
|
145
|
-
if (index === -1) {
|
|
146
|
-
throw new Error('连接配置不存在');
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (updates.name && connections.find((conn, idx) => conn.name === updates.name && idx !== index)) {
|
|
150
|
-
throw new Error('连接名称已存在');
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
connections[index] = { ...connections[index], ...updates, updatedAt: new Date() } as ConnectionEntity;
|
|
154
|
-
await this.saveConnections(connections);
|
|
155
|
-
|
|
156
|
-
return connections[index];
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private async deleteConnection(id: string): Promise<void> {
|
|
160
|
-
const connections = await this.getAllConnections();
|
|
161
|
-
const filteredConnections = connections.filter(conn => conn.id !== id);
|
|
162
|
-
|
|
163
|
-
if (filteredConnections.length === connections.length) {
|
|
164
|
-
throw new Error('连接配置不存在');
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (this.activeConnections.has(id)) {
|
|
168
|
-
await this.activeConnections.get(id)?.destroy();
|
|
169
|
-
this.activeConnections.delete(id);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
await this.saveConnections(filteredConnections);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private async testConnection(connection: ConnectionEntity): Promise<boolean> {
|
|
176
|
-
try {
|
|
177
|
-
const tempDataSource = await this.createTypeORMDataSource(connection);
|
|
178
|
-
await tempDataSource.query('SELECT 1');
|
|
179
|
-
await tempDataSource.destroy();
|
|
180
|
-
return true;
|
|
181
|
-
} catch (error) {
|
|
182
|
-
console.error(error);
|
|
183
|
-
return false;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* 数据库管理实现
|
|
189
|
-
*/
|
|
190
|
-
private async getDatabases(connectionId: string): Promise<string[]> {
|
|
191
|
-
const dataSource = await this.getActiveConnection(connectionId);
|
|
192
|
-
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
193
|
-
return databaseService.getDatabases(dataSource);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
private async getTables(connectionId: string, databaseName: string): Promise<any[]> {
|
|
197
|
-
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
198
|
-
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
199
|
-
return databaseService.getTables(dataSource, databaseName);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
private async executeQuery(connectionId: string, sql: string, databaseName?: string): Promise<any> {
|
|
203
|
-
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
204
|
-
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
205
|
-
return databaseService.executeQuery(dataSource, sql);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
private async getViews(connectionId: string, databaseName: string): Promise<any[]> {
|
|
209
|
-
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
210
|
-
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
211
|
-
return databaseService.getViews(dataSource, databaseName);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
private async getProcedures(connectionId: string, databaseName: string): Promise<any[]> {
|
|
215
|
-
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
216
|
-
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
217
|
-
return databaseService.getProcedures(dataSource, databaseName);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 获取活跃的数据库连接
|
|
222
|
-
*/
|
|
223
|
-
private async getActiveConnection(id: string, database?: string): Promise<DataSource> {
|
|
224
|
-
const key = database ? `${id}_${database}` : id;
|
|
225
|
-
if (this.activeConnections.has(key)) {
|
|
226
|
-
const db = this.activeConnections.get(key);
|
|
227
|
-
if (db?.isInitialized) {
|
|
228
|
-
return db;
|
|
229
|
-
} else {
|
|
230
|
-
this.activeConnections.delete(key);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (this.activeConnections.size >= 10) {
|
|
235
|
-
const oldestKey = this.activeConnections.keys().next().value || '';
|
|
236
|
-
const oldestConnection = this.activeConnections.get(oldestKey);
|
|
237
|
-
try {
|
|
238
|
-
await oldestConnection?.destroy();
|
|
239
|
-
} catch (error) {
|
|
240
|
-
console.error(`关闭旧连接 ${oldestKey} 失败:`, error);
|
|
241
|
-
}
|
|
242
|
-
this.activeConnections.delete(oldestKey);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const connectionConfig = await this.getConnectionById(id);
|
|
246
|
-
if (!connectionConfig) {
|
|
247
|
-
throw new Error('连接配置不存在');
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const updatedConnectionConfig: ConnectionEntity = {
|
|
251
|
-
...connectionConfig,
|
|
252
|
-
database: database || connectionConfig.database
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
const dataSource = await this.createTypeORMDataSource(updatedConnectionConfig);
|
|
256
|
-
this.activeConnections.set(key, dataSource);
|
|
257
|
-
|
|
258
|
-
return dataSource;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* 创建TypeORM数据源
|
|
263
|
-
*/
|
|
264
|
-
private async createTypeORMDataSource(connectionConfig: ConnectionEntity): Promise<DataSource> {
|
|
265
|
-
const connectionOptions = this.getTypeORMOptions(connectionConfig);
|
|
266
|
-
return new DataSource(connectionOptions).initialize();
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* 获取TypeORM连接配置
|
|
271
|
-
*/
|
|
272
|
-
private getTypeORMOptions(connectionConfig: ConnectionEntity): DataSourceOptions {
|
|
273
|
-
const baseOptions: DataSourceOptions = {
|
|
274
|
-
type: connectionConfig.type as any,
|
|
275
|
-
host: connectionConfig.host,
|
|
276
|
-
port: connectionConfig.port,
|
|
277
|
-
username: connectionConfig.username,
|
|
278
|
-
password: connectionConfig.password,
|
|
279
|
-
database: connectionConfig.database,
|
|
280
|
-
synchronize: false,
|
|
281
|
-
logging: false,
|
|
282
|
-
extra: {
|
|
283
|
-
multipleStatements: true
|
|
284
|
-
},
|
|
285
|
-
...connectionConfig.options
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
switch (connectionConfig.type.toLowerCase()) {
|
|
289
|
-
case 'sqlite':
|
|
290
|
-
return {
|
|
291
|
-
...baseOptions,
|
|
292
|
-
type: 'sqlite' as any,
|
|
293
|
-
database: connectionConfig.database,
|
|
294
|
-
host: undefined,
|
|
295
|
-
port: undefined,
|
|
296
|
-
username: undefined,
|
|
297
|
-
password: undefined
|
|
298
|
-
};
|
|
299
|
-
case 'postgres':
|
|
300
|
-
case 'postgresql':
|
|
301
|
-
return {
|
|
302
|
-
...baseOptions,
|
|
303
|
-
type: 'postgres' as any,
|
|
304
|
-
ssl: connectionConfig.options?.ssl || false
|
|
305
|
-
};
|
|
306
|
-
case 'oracle':
|
|
307
|
-
return {
|
|
308
|
-
...baseOptions,
|
|
309
|
-
type: 'oracle' as any,
|
|
310
|
-
connectString: `${connectionConfig.host}:${connectionConfig.port}/${connectionConfig.database}`,
|
|
311
|
-
host: undefined,
|
|
312
|
-
port: undefined,
|
|
313
|
-
database: undefined,
|
|
314
|
-
extra: {
|
|
315
|
-
connectionTimeout: 60000,
|
|
316
|
-
poolMax: 10,
|
|
317
|
-
poolMin: 1,
|
|
318
|
-
poolIncrement: 1
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
case 'mssql':
|
|
322
|
-
case 'sqlserver':
|
|
323
|
-
return {
|
|
324
|
-
...baseOptions,
|
|
325
|
-
type: 'mssql' as any,
|
|
326
|
-
options: {
|
|
327
|
-
encrypt: connectionConfig.options?.encrypt || false,
|
|
328
|
-
trustServerCertificate: true
|
|
329
|
-
},
|
|
330
|
-
extra: {
|
|
331
|
-
connectionTimeout: 60000,
|
|
332
|
-
requestTimeout: 15000
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
default:
|
|
336
|
-
return baseOptions;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* 获取数据库服务实例
|
|
342
|
-
*/
|
|
343
|
-
private getDatabaseService(type: string): BaseDatabaseService {
|
|
344
|
-
switch (type.toLowerCase()) {
|
|
345
|
-
case 'mysql':
|
|
346
|
-
case 'aurora-mysql':
|
|
347
|
-
case 'auroramysql':
|
|
348
|
-
return this.mysqlService;
|
|
349
|
-
case 'postgres':
|
|
350
|
-
case 'postgresql':
|
|
351
|
-
case 'aurora-postgres':
|
|
352
|
-
case 'aurorapostgres':
|
|
353
|
-
case 'aurora-postgresql':
|
|
354
|
-
return this.postgreSQLService;
|
|
355
|
-
case 'sqlite':
|
|
356
|
-
case 'better-sqlite3':
|
|
357
|
-
case 'bettersqlite3':
|
|
358
|
-
return this.sqliteService;
|
|
359
|
-
case 'oracle':
|
|
360
|
-
return this.oracleService;
|
|
361
|
-
case 'mssql':
|
|
362
|
-
case 'sqlserver':
|
|
363
|
-
return this.sqlServerService;
|
|
364
|
-
case 'cockroachdb':
|
|
365
|
-
case 'cockroach':
|
|
366
|
-
return this.cockroachDBService;
|
|
367
|
-
case 'mongodb':
|
|
368
|
-
case 'mongo':
|
|
369
|
-
return this.mongoDBService;
|
|
370
|
-
case 'sap':
|
|
371
|
-
case 'sap-hana':
|
|
372
|
-
case 'saphana':
|
|
373
|
-
return this.sapHANADatabaseService;
|
|
374
|
-
default:
|
|
375
|
-
throw new Error(`不支持的数据库类型: ${type}`);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* 保存连接配置到文件
|
|
381
|
-
*/
|
|
382
|
-
private async saveConnections(connections: ConnectionEntity[]): Promise<void> {
|
|
383
|
-
try {
|
|
384
|
-
const dir = path.dirname(this.configPath);
|
|
385
|
-
if (!fs.existsSync(dir)) {
|
|
386
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
387
|
-
}
|
|
388
|
-
fs.writeFileSync(this.configPath, JSON.stringify(connections, null, 2), 'utf8');
|
|
389
|
-
} catch (error) {
|
|
390
|
-
console.error('保存连接配置失败:', error);
|
|
391
|
-
throw error;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* 生成唯一ID
|
|
397
|
-
*/
|
|
398
|
-
private generateId(): string {
|
|
399
|
-
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// 创建全局单例
|
|
404
|
-
let bridgeInstance: DatabaseServiceBridge | null = null;
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* 获取数据库服务桥接实例
|
|
408
|
-
*/
|
|
409
|
-
export function getDatabaseServiceBridge(): DatabaseServiceBridge {
|
|
410
|
-
if (!bridgeInstance) {
|
|
411
|
-
bridgeInstance = new DatabaseServiceBridge();
|
|
412
|
-
}
|
|
413
|
-
return bridgeInstance;
|
|
414
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 数据库服务桥接层
|
|
3
|
+
* 用于 VSCode 扩展调用 server 目录下的现有数据库服务
|
|
4
|
+
* 为了简化部署,这里直接实现必要的功能,复用 server 目录的逻辑
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import * as os from 'os';
|
|
10
|
+
import { DataSource, type DataSourceOptions } from 'typeorm';
|
|
11
|
+
|
|
12
|
+
// 引入数据库服务接口(从本地复制的服务)
|
|
13
|
+
import { MySQLService } from '../database-services/mysql.service';
|
|
14
|
+
import { PostgreSQLService } from '../database-services/postgres.service';
|
|
15
|
+
import { SQLiteService } from '../database-services/sqlite.service';
|
|
16
|
+
import { OracleService } from '../database-services/oracle.service';
|
|
17
|
+
import { SQLServerService } from '../database-services/mssql.service';
|
|
18
|
+
import { CockroachDBService } from '../database-services/cockroachdb.service';
|
|
19
|
+
import { MongoDBService } from '../database-services/mongodb.service';
|
|
20
|
+
import { SAPHANADatabaseService } from '../database-services/sap.service';
|
|
21
|
+
import { BaseDatabaseService } from '../database-services/base.service';
|
|
22
|
+
import { ConnectionEntity } from '../database-services/model/connection.entity';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 数据库服务桥接类
|
|
26
|
+
* 直接复用 server 目录下的服务
|
|
27
|
+
*/
|
|
28
|
+
export class DatabaseServiceBridge {
|
|
29
|
+
/**
|
|
30
|
+
* 连接配置文件路径
|
|
31
|
+
*/
|
|
32
|
+
private readonly configPath: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 活跃的数据库连接实例
|
|
36
|
+
*/
|
|
37
|
+
private activeConnections: Map<string, DataSource> = new Map();
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 数据库服务实例
|
|
41
|
+
*/
|
|
42
|
+
private mysqlService: MySQLService;
|
|
43
|
+
private postgreSQLService: PostgreSQLService;
|
|
44
|
+
private sqliteService: SQLiteService;
|
|
45
|
+
private oracleService: OracleService;
|
|
46
|
+
private sqlServerService: SQLServerService;
|
|
47
|
+
private cockroachDBService: CockroachDBService;
|
|
48
|
+
private mongoDBService: MongoDBService;
|
|
49
|
+
private sapHANADatabaseService: SAPHANADatabaseService;
|
|
50
|
+
|
|
51
|
+
constructor() {
|
|
52
|
+
const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
|
|
53
|
+
this.configPath = path.join(dataDir, 'connections.json');
|
|
54
|
+
|
|
55
|
+
this.mysqlService = new MySQLService();
|
|
56
|
+
this.postgreSQLService = new PostgreSQLService();
|
|
57
|
+
this.sqliteService = new SQLiteService();
|
|
58
|
+
this.oracleService = new OracleService();
|
|
59
|
+
this.sqlServerService = new SQLServerService();
|
|
60
|
+
this.cockroachDBService = new CockroachDBService();
|
|
61
|
+
this.mongoDBService = new MongoDBService();
|
|
62
|
+
this.sapHANADatabaseService = new SAPHANADatabaseService();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 初始化服务
|
|
67
|
+
*/
|
|
68
|
+
async init() {
|
|
69
|
+
const configDir = path.dirname(this.configPath);
|
|
70
|
+
if (!fs.existsSync(configDir)) {
|
|
71
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 连接管理接口
|
|
77
|
+
*/
|
|
78
|
+
get connection() {
|
|
79
|
+
return {
|
|
80
|
+
getAllConnections: this.getAllConnections.bind(this),
|
|
81
|
+
getConnectionById: this.getConnectionById.bind(this),
|
|
82
|
+
addConnection: this.addConnection.bind(this),
|
|
83
|
+
updateConnection: this.updateConnection.bind(this),
|
|
84
|
+
deleteConnection: this.deleteConnection.bind(this),
|
|
85
|
+
testConnection: this.testConnection.bind(this)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 数据库管理接口
|
|
91
|
+
*/
|
|
92
|
+
get database() {
|
|
93
|
+
return {
|
|
94
|
+
getDatabases: this.getDatabases.bind(this),
|
|
95
|
+
getTables: this.getTables.bind(this),
|
|
96
|
+
executeQuery: this.executeQuery.bind(this),
|
|
97
|
+
getViews: this.getViews.bind(this),
|
|
98
|
+
getProcedures: this.getProcedures.bind(this)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 连接管理实现
|
|
104
|
+
*/
|
|
105
|
+
private async getAllConnections(): Promise<ConnectionEntity[]> {
|
|
106
|
+
try {
|
|
107
|
+
if (!fs.existsSync(this.configPath)) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
const data = fs.readFileSync(this.configPath, 'utf8');
|
|
111
|
+
return JSON.parse(data) as ConnectionEntity[];
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error('读取连接配置失败:', error);
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private async getConnectionById(id: string): Promise<ConnectionEntity | null> {
|
|
119
|
+
const connections = await this.getAllConnections();
|
|
120
|
+
return connections.find(conn => conn.id === id) || null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private async addConnection(connection: ConnectionEntity): Promise<ConnectionEntity> {
|
|
124
|
+
const connections = await this.getAllConnections();
|
|
125
|
+
|
|
126
|
+
if (connections.find(conn => conn.name === connection.name)) {
|
|
127
|
+
throw new Error('连接名称已存在');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
connection.id = this.generateId();
|
|
131
|
+
connection.createdAt = new Date();
|
|
132
|
+
connection.updatedAt = new Date();
|
|
133
|
+
connection.enabled = connection.enabled !== undefined ? connection.enabled : true;
|
|
134
|
+
|
|
135
|
+
connections.push(connection);
|
|
136
|
+
await this.saveConnections(connections);
|
|
137
|
+
|
|
138
|
+
return connection;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async updateConnection(id: string, updates: Partial<ConnectionEntity>): Promise<ConnectionEntity> {
|
|
142
|
+
const connections = await this.getAllConnections();
|
|
143
|
+
const index = connections.findIndex(conn => conn.id === id);
|
|
144
|
+
|
|
145
|
+
if (index === -1) {
|
|
146
|
+
throw new Error('连接配置不存在');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (updates.name && connections.find((conn, idx) => conn.name === updates.name && idx !== index)) {
|
|
150
|
+
throw new Error('连接名称已存在');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
connections[index] = { ...connections[index], ...updates, updatedAt: new Date() } as ConnectionEntity;
|
|
154
|
+
await this.saveConnections(connections);
|
|
155
|
+
|
|
156
|
+
return connections[index];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private async deleteConnection(id: string): Promise<void> {
|
|
160
|
+
const connections = await this.getAllConnections();
|
|
161
|
+
const filteredConnections = connections.filter(conn => conn.id !== id);
|
|
162
|
+
|
|
163
|
+
if (filteredConnections.length === connections.length) {
|
|
164
|
+
throw new Error('连接配置不存在');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (this.activeConnections.has(id)) {
|
|
168
|
+
await this.activeConnections.get(id)?.destroy();
|
|
169
|
+
this.activeConnections.delete(id);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
await this.saveConnections(filteredConnections);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async testConnection(connection: ConnectionEntity): Promise<boolean> {
|
|
176
|
+
try {
|
|
177
|
+
const tempDataSource = await this.createTypeORMDataSource(connection);
|
|
178
|
+
await tempDataSource.query('SELECT 1');
|
|
179
|
+
await tempDataSource.destroy();
|
|
180
|
+
return true;
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error(error);
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 数据库管理实现
|
|
189
|
+
*/
|
|
190
|
+
private async getDatabases(connectionId: string): Promise<string[]> {
|
|
191
|
+
const dataSource = await this.getActiveConnection(connectionId);
|
|
192
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
193
|
+
return databaseService.getDatabases(dataSource);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private async getTables(connectionId: string, databaseName: string): Promise<any[]> {
|
|
197
|
+
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
198
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
199
|
+
return databaseService.getTables(dataSource, databaseName);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private async executeQuery(connectionId: string, sql: string, databaseName?: string): Promise<any> {
|
|
203
|
+
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
204
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
205
|
+
return databaseService.executeQuery(dataSource, sql);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private async getViews(connectionId: string, databaseName: string): Promise<any[]> {
|
|
209
|
+
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
210
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
211
|
+
return databaseService.getViews(dataSource, databaseName);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private async getProcedures(connectionId: string, databaseName: string): Promise<any[]> {
|
|
215
|
+
const dataSource = await this.getActiveConnection(connectionId, databaseName);
|
|
216
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
217
|
+
return databaseService.getProcedures(dataSource, databaseName);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 获取活跃的数据库连接
|
|
222
|
+
*/
|
|
223
|
+
private async getActiveConnection(id: string, database?: string): Promise<DataSource> {
|
|
224
|
+
const key = database ? `${id}_${database}` : id;
|
|
225
|
+
if (this.activeConnections.has(key)) {
|
|
226
|
+
const db = this.activeConnections.get(key);
|
|
227
|
+
if (db?.isInitialized) {
|
|
228
|
+
return db;
|
|
229
|
+
} else {
|
|
230
|
+
this.activeConnections.delete(key);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (this.activeConnections.size >= 10) {
|
|
235
|
+
const oldestKey = this.activeConnections.keys().next().value || '';
|
|
236
|
+
const oldestConnection = this.activeConnections.get(oldestKey);
|
|
237
|
+
try {
|
|
238
|
+
await oldestConnection?.destroy();
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.error(`关闭旧连接 ${oldestKey} 失败:`, error);
|
|
241
|
+
}
|
|
242
|
+
this.activeConnections.delete(oldestKey);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const connectionConfig = await this.getConnectionById(id);
|
|
246
|
+
if (!connectionConfig) {
|
|
247
|
+
throw new Error('连接配置不存在');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const updatedConnectionConfig: ConnectionEntity = {
|
|
251
|
+
...connectionConfig,
|
|
252
|
+
database: database || connectionConfig.database
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const dataSource = await this.createTypeORMDataSource(updatedConnectionConfig);
|
|
256
|
+
this.activeConnections.set(key, dataSource);
|
|
257
|
+
|
|
258
|
+
return dataSource;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 创建TypeORM数据源
|
|
263
|
+
*/
|
|
264
|
+
private async createTypeORMDataSource(connectionConfig: ConnectionEntity): Promise<DataSource> {
|
|
265
|
+
const connectionOptions = this.getTypeORMOptions(connectionConfig);
|
|
266
|
+
return new DataSource(connectionOptions).initialize();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 获取TypeORM连接配置
|
|
271
|
+
*/
|
|
272
|
+
private getTypeORMOptions(connectionConfig: ConnectionEntity): DataSourceOptions {
|
|
273
|
+
const baseOptions: DataSourceOptions = {
|
|
274
|
+
type: connectionConfig.type as any,
|
|
275
|
+
host: connectionConfig.host,
|
|
276
|
+
port: connectionConfig.port,
|
|
277
|
+
username: connectionConfig.username,
|
|
278
|
+
password: connectionConfig.password,
|
|
279
|
+
database: connectionConfig.database,
|
|
280
|
+
synchronize: false,
|
|
281
|
+
logging: false,
|
|
282
|
+
extra: {
|
|
283
|
+
multipleStatements: true
|
|
284
|
+
},
|
|
285
|
+
...connectionConfig.options
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
switch (connectionConfig.type.toLowerCase()) {
|
|
289
|
+
case 'sqlite':
|
|
290
|
+
return {
|
|
291
|
+
...baseOptions,
|
|
292
|
+
type: 'sqlite' as any,
|
|
293
|
+
database: connectionConfig.database,
|
|
294
|
+
host: undefined,
|
|
295
|
+
port: undefined,
|
|
296
|
+
username: undefined,
|
|
297
|
+
password: undefined
|
|
298
|
+
};
|
|
299
|
+
case 'postgres':
|
|
300
|
+
case 'postgresql':
|
|
301
|
+
return {
|
|
302
|
+
...baseOptions,
|
|
303
|
+
type: 'postgres' as any,
|
|
304
|
+
ssl: connectionConfig.options?.ssl || false
|
|
305
|
+
};
|
|
306
|
+
case 'oracle':
|
|
307
|
+
return {
|
|
308
|
+
...baseOptions,
|
|
309
|
+
type: 'oracle' as any,
|
|
310
|
+
connectString: `${connectionConfig.host}:${connectionConfig.port}/${connectionConfig.database}`,
|
|
311
|
+
host: undefined,
|
|
312
|
+
port: undefined,
|
|
313
|
+
database: undefined,
|
|
314
|
+
extra: {
|
|
315
|
+
connectionTimeout: 60000,
|
|
316
|
+
poolMax: 10,
|
|
317
|
+
poolMin: 1,
|
|
318
|
+
poolIncrement: 1
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
case 'mssql':
|
|
322
|
+
case 'sqlserver':
|
|
323
|
+
return {
|
|
324
|
+
...baseOptions,
|
|
325
|
+
type: 'mssql' as any,
|
|
326
|
+
options: {
|
|
327
|
+
encrypt: connectionConfig.options?.encrypt || false,
|
|
328
|
+
trustServerCertificate: true
|
|
329
|
+
},
|
|
330
|
+
extra: {
|
|
331
|
+
connectionTimeout: 60000,
|
|
332
|
+
requestTimeout: 15000
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
default:
|
|
336
|
+
return baseOptions;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* 获取数据库服务实例
|
|
342
|
+
*/
|
|
343
|
+
private getDatabaseService(type: string): BaseDatabaseService {
|
|
344
|
+
switch (type.toLowerCase()) {
|
|
345
|
+
case 'mysql':
|
|
346
|
+
case 'aurora-mysql':
|
|
347
|
+
case 'auroramysql':
|
|
348
|
+
return this.mysqlService;
|
|
349
|
+
case 'postgres':
|
|
350
|
+
case 'postgresql':
|
|
351
|
+
case 'aurora-postgres':
|
|
352
|
+
case 'aurorapostgres':
|
|
353
|
+
case 'aurora-postgresql':
|
|
354
|
+
return this.postgreSQLService;
|
|
355
|
+
case 'sqlite':
|
|
356
|
+
case 'better-sqlite3':
|
|
357
|
+
case 'bettersqlite3':
|
|
358
|
+
return this.sqliteService;
|
|
359
|
+
case 'oracle':
|
|
360
|
+
return this.oracleService;
|
|
361
|
+
case 'mssql':
|
|
362
|
+
case 'sqlserver':
|
|
363
|
+
return this.sqlServerService;
|
|
364
|
+
case 'cockroachdb':
|
|
365
|
+
case 'cockroach':
|
|
366
|
+
return this.cockroachDBService;
|
|
367
|
+
case 'mongodb':
|
|
368
|
+
case 'mongo':
|
|
369
|
+
return this.mongoDBService;
|
|
370
|
+
case 'sap':
|
|
371
|
+
case 'sap-hana':
|
|
372
|
+
case 'saphana':
|
|
373
|
+
return this.sapHANADatabaseService;
|
|
374
|
+
default:
|
|
375
|
+
throw new Error(`不支持的数据库类型: ${type}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 保存连接配置到文件
|
|
381
|
+
*/
|
|
382
|
+
private async saveConnections(connections: ConnectionEntity[]): Promise<void> {
|
|
383
|
+
try {
|
|
384
|
+
const dir = path.dirname(this.configPath);
|
|
385
|
+
if (!fs.existsSync(dir)) {
|
|
386
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
387
|
+
}
|
|
388
|
+
fs.writeFileSync(this.configPath, JSON.stringify(connections, null, 2), 'utf8');
|
|
389
|
+
} catch (error) {
|
|
390
|
+
console.error('保存连接配置失败:', error);
|
|
391
|
+
throw error;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 生成唯一ID
|
|
397
|
+
*/
|
|
398
|
+
private generateId(): string {
|
|
399
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 创建全局单例
|
|
404
|
+
let bridgeInstance: DatabaseServiceBridge | null = null;
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 获取数据库服务桥接实例
|
|
408
|
+
*/
|
|
409
|
+
export function getDatabaseServiceBridge(): DatabaseServiceBridge {
|
|
410
|
+
if (!bridgeInstance) {
|
|
411
|
+
bridgeInstance = new DatabaseServiceBridge();
|
|
412
|
+
}
|
|
413
|
+
return bridgeInstance;
|
|
414
|
+
}
|