fdb2 1.0.0
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 -0
- package/.editorconfig +11 -0
- package/.eslintrc.cjs +14 -0
- package/.eslintrc.json +7 -0
- package/.prettierrc.js +3 -0
- package/.tpl.env +22 -0
- package/README.md +260 -0
- package/bin/build.sh +28 -0
- package/bin/deploy.sh +8 -0
- package/bin/dev.sh +10 -0
- package/bin/docker/.env +4 -0
- package/bin/docker/dev-docker-compose.yml +43 -0
- package/bin/docker/dev.Dockerfile +24 -0
- package/bin/docker/prod-docker-compose.yml +17 -0
- package/bin/docker/prod.Dockerfile +29 -0
- package/bin/fdb2.js +142 -0
- package/data/connections.demo.json +32 -0
- package/env.d.ts +1 -0
- package/nw-build.js +120 -0
- package/nw-dev.js +65 -0
- package/package.json +114 -0
- package/public/favicon.ico +0 -0
- package/public/index.html +9 -0
- package/public/modules/header.tpl +14 -0
- package/public/modules/initial_state.tpl +55 -0
- package/server/index.ts +677 -0
- package/server/model/connection.entity.ts +66 -0
- package/server/model/database.entity.ts +246 -0
- package/server/service/connection.service.ts +334 -0
- package/server/service/database/base.service.ts +363 -0
- package/server/service/database/database.service.ts +510 -0
- package/server/service/database/index.ts +7 -0
- package/server/service/database/mssql.service.ts +723 -0
- package/server/service/database/mysql.service.ts +761 -0
- package/server/service/database/oracle.service.ts +839 -0
- package/server/service/database/postgres.service.ts +744 -0
- package/server/service/database/sqlite.service.ts +559 -0
- package/server/service/session.service.ts +158 -0
- package/server.js +128 -0
- package/src/adapter/ajax.ts +135 -0
- package/src/assets/base.css +1 -0
- package/src/assets/database.css +950 -0
- package/src/assets/images/collapse.png +0 -0
- package/src/assets/images/no-login.png +0 -0
- package/src/assets/images/svg/illustrations/illustration-1.svg +1 -0
- package/src/assets/images/svg/illustrations/illustration-2.svg +2 -0
- package/src/assets/images/svg/illustrations/illustration-3.svg +50 -0
- package/src/assets/images/svg/illustrations/illustration-4.svg +1 -0
- package/src/assets/images/svg/illustrations/illustration-5.svg +73 -0
- package/src/assets/images/svg/illustrations/illustration-6.svg +89 -0
- package/src/assets/images/svg/illustrations/illustration-7.svg +39 -0
- package/src/assets/images/svg/illustrations/illustration-8.svg +1 -0
- package/src/assets/images/svg/separators/curve-2.svg +3 -0
- package/src/assets/images/svg/separators/curve.svg +3 -0
- package/src/assets/images/svg/separators/line.svg +3 -0
- package/src/assets/images/theme/light/screen-1-1000x800.jpg +0 -0
- package/src/assets/images/theme/light/screen-2-1000x800.jpg +0 -0
- package/src/assets/login/bg.jpg +0 -0
- package/src/assets/login/bg.png +0 -0
- package/src/assets/login/left.jpg +0 -0
- package/src/assets/logo.svg +73 -0
- package/src/assets/logo.webp +0 -0
- package/src/assets/main.css +1 -0
- package/src/base/config.ts +20 -0
- package/src/base/detect.ts +134 -0
- package/src/base/entity.ts +92 -0
- package/src/base/eventBus.ts +37 -0
- package/src/base//345/237/272/347/241/200/345/261/202.md +7 -0
- package/src/components/connection-editor/index.vue +590 -0
- package/src/components/dataGrid/index.vue +105 -0
- package/src/components/dataGrid/pagination.vue +106 -0
- package/src/components/loading/index.vue +43 -0
- package/src/components/modal/index.ts +181 -0
- package/src/components/modal/index.vue +560 -0
- package/src/components/toast/index.ts +44 -0
- package/src/components/toast/toast.vue +58 -0
- package/src/components/user/name.vue +104 -0
- package/src/components/user/selector.vue +416 -0
- package/src/domain/SysConfig.ts +74 -0
- package/src/platform/App.vue +8 -0
- package/src/platform/database/components/connection-detail.vue +1154 -0
- package/src/platform/database/components/data-editor.vue +478 -0
- package/src/platform/database/components/data-import-export.vue +1602 -0
- package/src/platform/database/components/database-detail.vue +1173 -0
- package/src/platform/database/components/database-monitor.vue +1086 -0
- package/src/platform/database/components/db-tools.vue +577 -0
- package/src/platform/database/components/query-history.vue +1349 -0
- package/src/platform/database/components/sql-executor.vue +738 -0
- package/src/platform/database/components/sql-query-editor.vue +1046 -0
- package/src/platform/database/components/table-detail.vue +1376 -0
- package/src/platform/database/components/table-editor.vue +690 -0
- package/src/platform/database/explorer.vue +1840 -0
- package/src/platform/database/index.vue +1193 -0
- package/src/platform/database/layout.vue +367 -0
- package/src/platform/database/router.ts +37 -0
- package/src/platform/database/styles/common.scss +602 -0
- package/src/platform/database/types/common.ts +445 -0
- package/src/platform/database/utils/export.ts +232 -0
- package/src/platform/database/utils/helpers.ts +437 -0
- package/src/platform/index.ts +33 -0
- package/src/platform/router.ts +41 -0
- package/src/service/base.ts +128 -0
- package/src/service/database.ts +500 -0
- package/src/service/login.ts +121 -0
- package/src/shims-vue.d.ts +7 -0
- package/src/stores/connection.ts +266 -0
- package/src/stores/session.ts +87 -0
- package/src/typings/database-types.ts +413 -0
- package/src/typings/database.ts +364 -0
- package/src/typings/global.d.ts +58 -0
- package/src/typings/pinia.d.ts +8 -0
- package/src/utils/clipboard.ts +30 -0
- package/src/utils/database-types.ts +243 -0
- package/src/utils/modal.ts +124 -0
- package/src/utils/request.ts +55 -0
- package/src/utils/sleep.ts +4 -0
- package/src/utils/toast.ts +73 -0
- package/src/utils/util.ts +171 -0
- package/src/utils/xlsx.ts +228 -0
- package/tsconfig.json +33 -0
- package/tsconfig.server.json +19 -0
- package/view/index.html +9 -0
- package/view/modules/header.tpl +14 -0
- package/view/modules/initial_state.tpl +20 -0
- package/vite.config.ts +384 -0
|
@@ -0,0 +1,723 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { BaseDatabaseService } from './base.service';
|
|
5
|
+
import {
|
|
6
|
+
TableEntity,
|
|
7
|
+
ColumnEntity,
|
|
8
|
+
IndexEntity,
|
|
9
|
+
ForeignKeyEntity
|
|
10
|
+
} from '../../model/database.entity';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* SQL Server数据库服务实现
|
|
14
|
+
*/
|
|
15
|
+
export class SQLServerService extends BaseDatabaseService {
|
|
16
|
+
|
|
17
|
+
getDatabaseType(): string {
|
|
18
|
+
return 'mssql';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 获取SQL Server数据库列表
|
|
23
|
+
*/
|
|
24
|
+
async getDatabases(dataSource: DataSource): Promise<string[]> {
|
|
25
|
+
const result = await dataSource.query(`
|
|
26
|
+
SELECT name
|
|
27
|
+
FROM sys.databases
|
|
28
|
+
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')
|
|
29
|
+
AND state = 0
|
|
30
|
+
ORDER BY name
|
|
31
|
+
`);
|
|
32
|
+
return result.map((row: any) => row.name);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 获取SQL Server表列表
|
|
37
|
+
*/
|
|
38
|
+
async getTables(dataSource: DataSource, database: string): Promise<TableEntity[]> {
|
|
39
|
+
const result = await dataSource.query(`
|
|
40
|
+
SELECT
|
|
41
|
+
t.name,
|
|
42
|
+
'BASE TABLE' as type,
|
|
43
|
+
p.rows as rowCount,
|
|
44
|
+
SUM(a.total_pages) * 8 * 1024 as dataSize
|
|
45
|
+
FROM ${this.quoteIdentifier(database)}.sys.tables t
|
|
46
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.partitions p ON t.object_id = p.object_id
|
|
47
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.allocation_units a ON p.partition_id = a.container_id
|
|
48
|
+
WHERE t.is_ms_shipped = 0
|
|
49
|
+
AND p.index_id IN (0, 1)
|
|
50
|
+
GROUP BY t.name, p.rows
|
|
51
|
+
ORDER BY t.name
|
|
52
|
+
`);
|
|
53
|
+
|
|
54
|
+
return result.map((row: any) => ({
|
|
55
|
+
name: row.name,
|
|
56
|
+
type: row.type,
|
|
57
|
+
rowCount: row.rowCount || 0,
|
|
58
|
+
dataSize: row.dataSize || 0,
|
|
59
|
+
indexSize: 0 // SQL Server索引大小计算较复杂,这里简化处理
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 获取SQL Server列信息
|
|
65
|
+
*/
|
|
66
|
+
async getColumns(dataSource: DataSource, database: string, table: string): Promise<ColumnEntity[]> {
|
|
67
|
+
// 使用兼容的SQL查询,避免使用可能不兼容的函数
|
|
68
|
+
const result = await dataSource.query(`
|
|
69
|
+
SELECT
|
|
70
|
+
c.name,
|
|
71
|
+
t.name as type,
|
|
72
|
+
c.max_length as length,
|
|
73
|
+
c.precision,
|
|
74
|
+
c.scale,
|
|
75
|
+
c.is_nullable as nullable,
|
|
76
|
+
c.default_object_id,
|
|
77
|
+
COLUMNPROPERTY(c.object_id, c.name, 'IsIdentity') as isIdentity
|
|
78
|
+
FROM ${this.quoteIdentifier(database)}.sys.columns c
|
|
79
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.types t ON c.user_type_id = t.user_type_id
|
|
80
|
+
WHERE c.object_id = OBJECT_ID(?)
|
|
81
|
+
ORDER BY c.column_id
|
|
82
|
+
`, [`${database}.${table}`]);
|
|
83
|
+
|
|
84
|
+
// 获取主键信息
|
|
85
|
+
const primaryKeys = await this.getPrimaryKeys(dataSource, database, table);
|
|
86
|
+
|
|
87
|
+
return result.map((row: any) => ({
|
|
88
|
+
name: row.name,
|
|
89
|
+
type: row.type,
|
|
90
|
+
nullable: row.nullable,
|
|
91
|
+
defaultValue: row.default_object_id ? 'DEFAULT' : null,
|
|
92
|
+
isPrimary: primaryKeys.includes(row.name),
|
|
93
|
+
isAutoIncrement: row.isIdentity === 1,
|
|
94
|
+
length: row.length,
|
|
95
|
+
precision: row.precision,
|
|
96
|
+
scale: row.scale
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 获取SQL Server索引信息
|
|
102
|
+
*/
|
|
103
|
+
async getIndexes(dataSource: DataSource, database: string, table: string): Promise<IndexEntity[]> {
|
|
104
|
+
const result = await dataSource.query(`
|
|
105
|
+
SELECT
|
|
106
|
+
i.name,
|
|
107
|
+
i.type_desc as type,
|
|
108
|
+
c.name as column,
|
|
109
|
+
i.is_unique as isUnique
|
|
110
|
+
FROM ${this.quoteIdentifier(database)}.sys.indexes i
|
|
111
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
|
|
112
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
|
|
113
|
+
WHERE i.object_id = OBJECT_ID(?)
|
|
114
|
+
AND i.is_primary_key = 0
|
|
115
|
+
ORDER BY i.name, ic.key_ordinal
|
|
116
|
+
`, [`${database}.${table}`]);
|
|
117
|
+
|
|
118
|
+
// 按索引名分组
|
|
119
|
+
const indexMap = new Map<string, IndexEntity>();
|
|
120
|
+
result.forEach((row: any) => {
|
|
121
|
+
if (!indexMap.has(row.name)) {
|
|
122
|
+
indexMap.set(row.name, {
|
|
123
|
+
name: row.name,
|
|
124
|
+
type: row.type,
|
|
125
|
+
columns: [],
|
|
126
|
+
unique: row.isUnique
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
indexMap.get(row.name)!.columns.push(row.column);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
return Array.from(indexMap.values());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 获取SQL Server外键信息
|
|
137
|
+
*/
|
|
138
|
+
async getForeignKeys(dataSource: DataSource, database: string, table: string): Promise<ForeignKeyEntity[]> {
|
|
139
|
+
const result = await dataSource.query(`
|
|
140
|
+
SELECT
|
|
141
|
+
fk.name as name,
|
|
142
|
+
c.name as column,
|
|
143
|
+
rt.name as referencedTable,
|
|
144
|
+
rc.name as referencedColumn,
|
|
145
|
+
fk.delete_action_desc as onDelete,
|
|
146
|
+
fk.update_action_desc as onUpdate
|
|
147
|
+
FROM ${this.quoteIdentifier(database)}.sys.foreign_keys fk
|
|
148
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
|
|
149
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.columns c ON fkc.parent_object_id = c.object_id AND fkc.parent_column_id = c.column_id
|
|
150
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.columns rc ON fkc.referenced_object_id = rc.object_id AND fkc.referenced_column_id = rc.column_id
|
|
151
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.tables rt ON fkc.referenced_object_id = rt.object_id
|
|
152
|
+
WHERE fkc.parent_object_id = OBJECT_ID(?)
|
|
153
|
+
`, [`${database}.${table}`]);
|
|
154
|
+
|
|
155
|
+
return result.map((row: any) => ({
|
|
156
|
+
name: row.name,
|
|
157
|
+
column: row.column,
|
|
158
|
+
referencedTable: row.referencedTable,
|
|
159
|
+
referencedColumn: row.referencedColumn,
|
|
160
|
+
onDelete: row.onDelete,
|
|
161
|
+
onUpdate: row.onUpdate
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 获取SQL Server数据库大小
|
|
167
|
+
*/
|
|
168
|
+
async getDatabaseSize(dataSource: DataSource, database: string): Promise<number> {
|
|
169
|
+
const result = await dataSource.query(`
|
|
170
|
+
SELECT SUM(size * 8 * 1024) as size
|
|
171
|
+
FROM sys.master_files
|
|
172
|
+
WHERE database_id = DB_ID(?)
|
|
173
|
+
`, [database]);
|
|
174
|
+
return result[0]?.size || 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 获取主键信息
|
|
179
|
+
*/
|
|
180
|
+
private async getPrimaryKeys(dataSource: DataSource, database: string, table: string): Promise<string[]> {
|
|
181
|
+
const result = await dataSource.query(`
|
|
182
|
+
SELECT c.name
|
|
183
|
+
FROM ${this.quoteIdentifier(database)}.sys.key_constraints k
|
|
184
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.index_columns ic ON k.parent_object_id = ic.object_id AND k.unique_index_id = ic.index_id
|
|
185
|
+
INNER JOIN ${this.quoteIdentifier(database)}.sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
|
|
186
|
+
WHERE k.type = 'PK'
|
|
187
|
+
AND k.parent_object_id = OBJECT_ID(?)
|
|
188
|
+
ORDER BY ic.key_ordinal
|
|
189
|
+
`, [`${database}.${table}`]);
|
|
190
|
+
return result.map((row: any) => row.name);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* SQL Server的标识符引用方式
|
|
195
|
+
*/
|
|
196
|
+
public quoteIdentifier(identifier: string): string {
|
|
197
|
+
return `[${identifier}]`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 获取SQL Server视图列表
|
|
202
|
+
*/
|
|
203
|
+
async getViews(dataSource: DataSource, database: string): Promise<any[]> {
|
|
204
|
+
const result = await dataSource.query(`
|
|
205
|
+
SELECT
|
|
206
|
+
TABLE_NAME as name,
|
|
207
|
+
'' as comment,
|
|
208
|
+
TABLE_SCHEMA as schemaName
|
|
209
|
+
FROM ${this.quoteIdentifier(database)}.INFORMATION_SCHEMA.VIEWS
|
|
210
|
+
ORDER BY TABLE_NAME
|
|
211
|
+
`);
|
|
212
|
+
|
|
213
|
+
return result.map((row: any) => ({
|
|
214
|
+
name: row.name,
|
|
215
|
+
comment: row.comment || '',
|
|
216
|
+
schemaName: row.schemaname
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 获取SQL Server视图定义
|
|
222
|
+
*/
|
|
223
|
+
async getViewDefinition(dataSource: DataSource, database: string, viewName: string): Promise<string> {
|
|
224
|
+
const result = await dataSource.query(`
|
|
225
|
+
SELECT VIEW_DEFINITION as definition
|
|
226
|
+
FROM ${this.quoteIdentifier(database)}.INFORMATION_SCHEMA.VIEWS
|
|
227
|
+
WHERE TABLE_NAME = ?
|
|
228
|
+
`, [viewName]);
|
|
229
|
+
|
|
230
|
+
return result[0]?.definition || '';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 获取SQL Server存储过程列表
|
|
235
|
+
*/
|
|
236
|
+
async getProcedures(dataSource: DataSource, database: string): Promise<any[]> {
|
|
237
|
+
const result = await dataSource.query(`
|
|
238
|
+
SELECT
|
|
239
|
+
ROUTINE_NAME as name,
|
|
240
|
+
'' as comment,
|
|
241
|
+
ROUTINE_TYPE as type,
|
|
242
|
+
'' as returnType,
|
|
243
|
+
'SQL' as language
|
|
244
|
+
FROM ${this.quoteIdentifier(database)}.INFORMATION_SCHEMA.ROUTINES
|
|
245
|
+
WHERE ROUTINE_SCHEMA NOT IN ('sys', 'INFORMATION_SCHEMA')
|
|
246
|
+
ORDER BY ROUTINE_NAME
|
|
247
|
+
`);
|
|
248
|
+
|
|
249
|
+
return result.map((row: any) => ({
|
|
250
|
+
name: row.name,
|
|
251
|
+
comment: row.comment || '',
|
|
252
|
+
type: row.type,
|
|
253
|
+
returnType: row.returnType || '',
|
|
254
|
+
language: row.language || 'SQL'
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 获取SQL Server存储过程定义
|
|
260
|
+
*/
|
|
261
|
+
async getProcedureDefinition(dataSource: DataSource, database: string, procedureName: string): Promise<string> {
|
|
262
|
+
const result = await dataSource.query(`
|
|
263
|
+
SELECT ROUTINE_DEFINITION as definition
|
|
264
|
+
FROM ${this.quoteIdentifier(database)}.INFORMATION_SCHEMA.ROUTINES
|
|
265
|
+
WHERE ROUTINE_NAME = ?
|
|
266
|
+
`, [procedureName]);
|
|
267
|
+
|
|
268
|
+
return result[0]?.definition || '';
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 创建SQL Server数据库
|
|
273
|
+
*/
|
|
274
|
+
async createDatabase(dataSource: DataSource, databaseName: string, options?: any): Promise<void> {
|
|
275
|
+
let sql = `CREATE DATABASE ${this.quoteIdentifier(databaseName)}`;
|
|
276
|
+
|
|
277
|
+
if (options) {
|
|
278
|
+
const clauses = [] as Array<string>;
|
|
279
|
+
|
|
280
|
+
if (options.collation) {
|
|
281
|
+
clauses.push(`COLLATE ${options.collation}`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (options.containment) {
|
|
285
|
+
clauses.push(`CONTAINMENT = ${options.containment}`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (options.compatibilityLevel) {
|
|
289
|
+
clauses.push(`COMPATIBILITY_LEVEL = ${options.compatibilityLevel}`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 添加数据文件配置
|
|
293
|
+
if (options.dataFiles) {
|
|
294
|
+
const fileClauses = options.dataFiles.map((file: any) => {
|
|
295
|
+
let fileClause = `(NAME = '${file.name}', FILENAME = '${file.filename}'`;
|
|
296
|
+
if (file.size) fileClause += `, SIZE = ${file.size}`;
|
|
297
|
+
if (file.maxSize) fileClause += `, MAXSIZE = ${file.maxSize}`;
|
|
298
|
+
if (file.growth) fileClause += `, FILEGROWTH = ${file.growth}`;
|
|
299
|
+
fileClause += ')';
|
|
300
|
+
return fileClause;
|
|
301
|
+
});
|
|
302
|
+
clauses.push(`ON ${fileClauses.join(', ')}`);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// 添加日志文件配置
|
|
306
|
+
if (options.logFiles) {
|
|
307
|
+
const logClauses = options.logFiles.map((log: any) => {
|
|
308
|
+
let logClause = `(NAME = '${log.name}', FILENAME = '${log.filename}'`;
|
|
309
|
+
if (log.size) logClause += `, SIZE = ${log.size}`;
|
|
310
|
+
if (log.maxSize) logClause += `, MAXSIZE = ${log.maxSize}`;
|
|
311
|
+
if (log.growth) logClause += `, FILEGROWTH = ${log.growth}`;
|
|
312
|
+
logClause += ')';
|
|
313
|
+
return logClause;
|
|
314
|
+
});
|
|
315
|
+
clauses.push(`LOG ON ${logClauses.join(', ')}`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (clauses.length > 0) {
|
|
319
|
+
sql += ' ' + clauses.join(' ');
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
await dataSource.query(sql);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 删除SQL Server数据库
|
|
328
|
+
*/
|
|
329
|
+
async dropDatabase(dataSource: DataSource, databaseName: string): Promise<void> {
|
|
330
|
+
const sql = `DROP DATABASE ${this.quoteIdentifier(databaseName)}`;
|
|
331
|
+
await dataSource.query(sql);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* 导出数据库架构
|
|
336
|
+
*/
|
|
337
|
+
async exportSchema(dataSource: DataSource, databaseName: string): Promise<string> {
|
|
338
|
+
// 获取所有表
|
|
339
|
+
const tables = await this.getTables(dataSource, databaseName);
|
|
340
|
+
let schemaSql = `-- 数据库架构导出 - ${databaseName}\n`;
|
|
341
|
+
schemaSql += `-- 导出时间: ${new Date().toISOString()}\n\n`;
|
|
342
|
+
|
|
343
|
+
// 为每个表生成CREATE TABLE语句
|
|
344
|
+
for (const table of tables) {
|
|
345
|
+
// 获取表结构
|
|
346
|
+
const columns = await this.getColumns(dataSource, databaseName, table.name);
|
|
347
|
+
const indexes = await this.getIndexes(dataSource, databaseName, table.name);
|
|
348
|
+
const foreignKeys = await this.getForeignKeys(dataSource, databaseName, table.name);
|
|
349
|
+
|
|
350
|
+
// 生成CREATE TABLE语句
|
|
351
|
+
schemaSql += `-- 表结构: ${table.name}\n`;
|
|
352
|
+
schemaSql += `CREATE TABLE IF NOT EXISTS ${this.quoteIdentifier(table.name)} (\n`;
|
|
353
|
+
|
|
354
|
+
// 添加列定义
|
|
355
|
+
const columnDefinitions = columns.map(column => {
|
|
356
|
+
let definition = ` ${this.quoteIdentifier(column.name)} ${column.type}`;
|
|
357
|
+
if (!column.nullable) definition += ' NOT NULL';
|
|
358
|
+
if (column.defaultValue !== undefined) {
|
|
359
|
+
// 特殊关键字处理
|
|
360
|
+
const upperDefault = column.defaultValue.toString().toUpperCase();
|
|
361
|
+
if (upperDefault === 'CURRENT_TIMESTAMP' || upperDefault === 'NOW()' || upperDefault === 'CURRENT_DATE') {
|
|
362
|
+
definition += ` DEFAULT ${upperDefault}`;
|
|
363
|
+
} else {
|
|
364
|
+
definition += ` DEFAULT ${column.defaultValue === null ? 'NULL' : `'${column.defaultValue}'`}`;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (column.isAutoIncrement) definition += ' IDENTITY(1,1)';
|
|
368
|
+
return definition;
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// 添加主键
|
|
372
|
+
const primaryKeyColumns = columns.filter(column => column.isPrimary);
|
|
373
|
+
if (primaryKeyColumns.length > 0) {
|
|
374
|
+
const primaryKeyNames = primaryKeyColumns.map(column => this.quoteIdentifier(column.name)).join(', ');
|
|
375
|
+
columnDefinitions.push(` PRIMARY KEY (${primaryKeyNames})`);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
schemaSql += columnDefinitions.join(',\n');
|
|
379
|
+
schemaSql += '\n);\n\n';
|
|
380
|
+
|
|
381
|
+
// 添加索引
|
|
382
|
+
for (const index of indexes) {
|
|
383
|
+
if (index.type === 'PRIMARY' || index.name.toUpperCase() === 'PRIMARY') continue; // 跳过主键索引
|
|
384
|
+
schemaSql += `-- 索引: ${index.name} on ${table.name}\n`;
|
|
385
|
+
schemaSql += `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${this.quoteIdentifier(index.name)} ON ${this.quoteIdentifier(table.name)} (${index.columns.map(col => this.quoteIdentifier(col)).join(', ')});\n`;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (indexes.length > 0) schemaSql += '\n';
|
|
389
|
+
|
|
390
|
+
// 添加外键
|
|
391
|
+
for (const foreignKey of foreignKeys) {
|
|
392
|
+
schemaSql += `-- 外键: ${foreignKey.name} on ${table.name}\n`;
|
|
393
|
+
schemaSql += `ALTER TABLE ${this.quoteIdentifier(table.name)} ADD CONSTRAINT ${this.quoteIdentifier(foreignKey.name)} FOREIGN KEY (${this.quoteIdentifier(foreignKey.column)}) REFERENCES ${this.quoteIdentifier(foreignKey.referencedTable)} (${this.quoteIdentifier(foreignKey.referencedColumn)})${foreignKey.onDelete ? ` ON DELETE ${foreignKey.onDelete}` : ''}${foreignKey.onUpdate ? ` ON UPDATE ${foreignKey.onUpdate}` : ''};\n`;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (foreignKeys.length > 0) schemaSql += '\n';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return schemaSql;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* 查看数据库日志
|
|
404
|
+
*/
|
|
405
|
+
async viewLogs(dataSource: DataSource, database?: string, limit: number = 100): Promise<any[]> {
|
|
406
|
+
// SQL Server查看日志
|
|
407
|
+
try {
|
|
408
|
+
// 尝试查看SQL Server错误日志
|
|
409
|
+
const logs = await dataSource.query(`EXEC xp_readerrorlog 0, 1, NULL, NULL, NULL, NULL, 'DESC'`);
|
|
410
|
+
return logs.slice(0, limit);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
try {
|
|
413
|
+
// 尝试查看SQL Server事务日志
|
|
414
|
+
const logs = await dataSource.query(`SELECT TOP ${limit} * FROM fn_dblog(NULL, NULL)`);
|
|
415
|
+
return logs;
|
|
416
|
+
} catch (e) {
|
|
417
|
+
return [{ message: '无法获取SQL Server日志,请确保具有适当的权限' }];
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* 备份数据库
|
|
424
|
+
*/
|
|
425
|
+
async backupDatabase(dataSource: DataSource, databaseName: string, options?: any): Promise<string> {
|
|
426
|
+
// SQL Server备份数据库
|
|
427
|
+
try {
|
|
428
|
+
// 使用BACKUP DATABASE命令备份
|
|
429
|
+
const backupPath = options?.path || path.join(__dirname, '..', '..', 'backups');
|
|
430
|
+
|
|
431
|
+
// 确保备份目录存在
|
|
432
|
+
if (!fs.existsSync(backupPath)) {
|
|
433
|
+
fs.mkdirSync(backupPath, { recursive: true });
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
437
|
+
const backupFile = path.join(backupPath, `${databaseName}_${timestamp}.bak`);
|
|
438
|
+
|
|
439
|
+
// 执行备份命令
|
|
440
|
+
const backupSql = `BACKUP DATABASE ${this.quoteIdentifier(databaseName)} TO DISK = '${backupFile}' WITH INIT`;
|
|
441
|
+
await dataSource.query(backupSql);
|
|
442
|
+
|
|
443
|
+
return `备份成功:${backupFile}`;
|
|
444
|
+
} catch (error) {
|
|
445
|
+
console.error('SQL Server备份失败:', error);
|
|
446
|
+
// @ts-ignore
|
|
447
|
+
throw new Error(`备份失败: ${error.message}`);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* 恢复数据库
|
|
453
|
+
*/
|
|
454
|
+
async restoreDatabase(dataSource: DataSource, databaseName: string, filePath: string, options?: any): Promise<void> {
|
|
455
|
+
// SQL Server恢复数据库
|
|
456
|
+
try {
|
|
457
|
+
// 断开所有连接
|
|
458
|
+
await dataSource.query(`ALTER DATABASE ${this.quoteIdentifier(databaseName)} SET SINGLE_USER WITH ROLLBACK IMMEDIATE`);
|
|
459
|
+
|
|
460
|
+
// 执行恢复命令
|
|
461
|
+
const restoreSql = `RESTORE DATABASE ${this.quoteIdentifier(databaseName)} FROM DISK = '${filePath}' WITH REPLACE`;
|
|
462
|
+
await dataSource.query(restoreSql);
|
|
463
|
+
|
|
464
|
+
// 恢复多用户模式
|
|
465
|
+
await dataSource.query(`ALTER DATABASE ${this.quoteIdentifier(databaseName)} SET MULTI_USER`);
|
|
466
|
+
} catch (error) {
|
|
467
|
+
console.error('SQL Server恢复失败:', error);
|
|
468
|
+
// @ts-ignore
|
|
469
|
+
throw new Error(`恢复失败: ${error.message}`);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* 导出表数据到 SQL 文件
|
|
475
|
+
*/
|
|
476
|
+
async exportTableDataToSQL(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
477
|
+
try {
|
|
478
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
479
|
+
if (!fs.existsSync(exportPath)) fs.mkdirSync(exportPath, { recursive: true });
|
|
480
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
481
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.sql`);
|
|
482
|
+
|
|
483
|
+
const columns = await this.getColumns(dataSource, databaseName, tableName);
|
|
484
|
+
const columnNames = columns.map(column => column.name);
|
|
485
|
+
|
|
486
|
+
// 生成文件头部
|
|
487
|
+
const header = `-- 表数据导出 - ${tableName}\n` +
|
|
488
|
+
`-- 导出时间: ${new Date().toISOString()}\n\n`;
|
|
489
|
+
fs.writeFileSync(exportFile, header, 'utf8');
|
|
490
|
+
|
|
491
|
+
// 分批处理数据,避免一次性加载大量数据到内存
|
|
492
|
+
const batchSize = options?.batchSize || 10000; // 每批处理10000行
|
|
493
|
+
let offset = 0;
|
|
494
|
+
let hasMoreData = true;
|
|
495
|
+
|
|
496
|
+
while (hasMoreData) {
|
|
497
|
+
// 分批查询数据(SQL Server 使用 OFFSET FETCH 语法)
|
|
498
|
+
const query = `SELECT * FROM ${this.quoteIdentifier(databaseName)}.${this.quoteIdentifier(tableName)} ORDER BY (SELECT NULL) OFFSET ${offset} ROWS FETCH NEXT ${batchSize} ROWS ONLY`;
|
|
499
|
+
const data = await dataSource.query(query);
|
|
500
|
+
|
|
501
|
+
if (data.length === 0) {
|
|
502
|
+
hasMoreData = false;
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// 生成当前批次的 INSERT 语句
|
|
507
|
+
let batchSql = '';
|
|
508
|
+
data.forEach((row: any) => {
|
|
509
|
+
const values = columnNames.map(column => {
|
|
510
|
+
const value = row[column];
|
|
511
|
+
if (value === null || value === undefined) return 'NULL';
|
|
512
|
+
if (typeof value === 'string') {
|
|
513
|
+
// 处理字符串,转义单引号
|
|
514
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
515
|
+
}
|
|
516
|
+
if (typeof value === 'boolean') {
|
|
517
|
+
// SQL Server 使用 BIT 类型,1 表示 true,0 表示 false
|
|
518
|
+
return value ? '1' : '0';
|
|
519
|
+
}
|
|
520
|
+
if (value instanceof Date) {
|
|
521
|
+
// 格式化日期为 SQL Server 兼容格式
|
|
522
|
+
const year = value.getFullYear();
|
|
523
|
+
const month = String(value.getMonth() + 1).padStart(2, '0');
|
|
524
|
+
const day = String(value.getDate()).padStart(2, '0');
|
|
525
|
+
const hours = String(value.getHours()).padStart(2, '0');
|
|
526
|
+
const minutes = String(value.getMinutes()).padStart(2, '0');
|
|
527
|
+
const seconds = String(value.getSeconds()).padStart(2, '0');
|
|
528
|
+
return `'${year}-${month}-${day} ${hours}:${minutes}:${seconds}'`;
|
|
529
|
+
}
|
|
530
|
+
if (typeof value === 'object') {
|
|
531
|
+
// 处理对象类型
|
|
532
|
+
try {
|
|
533
|
+
const stringValue = JSON.stringify(value);
|
|
534
|
+
return `'${stringValue.replace(/'/g, "''")}'`;
|
|
535
|
+
} catch {
|
|
536
|
+
return `'${String(value).replace(/'/g, "''")}'`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
// 其他类型直接转换为字符串
|
|
540
|
+
return String(value);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
batchSql += `INSERT INTO ${this.quoteIdentifier(databaseName)}.${this.quoteIdentifier(tableName)} (${columnNames.map(col => this.quoteIdentifier(col)).join(', ')}) VALUES (${values.join(', ')});\n`;
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
// 追加写入文件
|
|
547
|
+
fs.appendFileSync(exportFile, batchSql, 'utf8');
|
|
548
|
+
|
|
549
|
+
// 增加偏移量
|
|
550
|
+
offset += batchSize;
|
|
551
|
+
|
|
552
|
+
// 打印进度信息
|
|
553
|
+
console.log(`SQL Server导出表数据进度: ${tableName} - 已处理 ${offset} 行`);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return exportFile;
|
|
557
|
+
} catch (error) {
|
|
558
|
+
console.error('SQL Server导出表数据失败:', error);
|
|
559
|
+
// @ts-ignore
|
|
560
|
+
throw new Error(`导出表数据失败: ${error.message}`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* 导出表数据到 CSV 文件
|
|
566
|
+
*/
|
|
567
|
+
async exportTableDataToCSV(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
568
|
+
try {
|
|
569
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
570
|
+
if (!fs.existsSync(exportPath)) fs.mkdirSync(exportPath, { recursive: true });
|
|
571
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
572
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.csv`);
|
|
573
|
+
|
|
574
|
+
const columns = await this.getColumns(dataSource, databaseName, tableName);
|
|
575
|
+
const columnNames = columns.map(column => column.name);
|
|
576
|
+
|
|
577
|
+
// 写入 CSV 头部(包含 UTF-8 BOM)
|
|
578
|
+
const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
|
|
579
|
+
fs.writeFileSync(exportFile, bom);
|
|
580
|
+
fs.appendFileSync(exportFile, columnNames.map(name => `"${name}"`).join(',') + '\n', 'utf8');
|
|
581
|
+
|
|
582
|
+
// 分批处理数据,避免一次性加载大量数据到内存
|
|
583
|
+
const batchSize = options?.batchSize || 10000; // 每批处理10000行
|
|
584
|
+
let offset = 0;
|
|
585
|
+
let hasMoreData = true;
|
|
586
|
+
|
|
587
|
+
while (hasMoreData) {
|
|
588
|
+
// 分批查询数据(SQL Server 使用 OFFSET FETCH 语法)
|
|
589
|
+
const query = `SELECT * FROM ${this.quoteIdentifier(databaseName)}.${this.quoteIdentifier(tableName)} ORDER BY (SELECT NULL) OFFSET ${offset} ROWS FETCH NEXT ${batchSize} ROWS ONLY`;
|
|
590
|
+
const data = await dataSource.query(query);
|
|
591
|
+
|
|
592
|
+
if (data.length === 0) {
|
|
593
|
+
hasMoreData = false;
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
// 生成当前批次的 CSV 行
|
|
598
|
+
let batchCsv = '';
|
|
599
|
+
data.forEach((row: any) => {
|
|
600
|
+
const values = columnNames.map(column => {
|
|
601
|
+
const value = row[column];
|
|
602
|
+
if (value === null || value === undefined) {
|
|
603
|
+
return '';
|
|
604
|
+
} else if (typeof value === 'string') {
|
|
605
|
+
// 转义双引号并包裹在双引号中
|
|
606
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
607
|
+
} else if (value instanceof Date) {
|
|
608
|
+
// 格式化日期为 SQL Server 兼容格式
|
|
609
|
+
const year = value.getFullYear();
|
|
610
|
+
const month = String(value.getMonth() + 1).padStart(2, '0');
|
|
611
|
+
const day = String(value.getDate()).padStart(2, '0');
|
|
612
|
+
const hours = String(value.getHours()).padStart(2, '0');
|
|
613
|
+
const minutes = String(value.getMinutes()).padStart(2, '0');
|
|
614
|
+
const seconds = String(value.getSeconds()).padStart(2, '0');
|
|
615
|
+
return `"${year}-${month}-${day} ${hours}:${minutes}:${seconds}"`;
|
|
616
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
617
|
+
// 处理对象类型
|
|
618
|
+
try {
|
|
619
|
+
return `"${JSON.stringify(value).replace(/"/g, '""')}"`;
|
|
620
|
+
} catch {
|
|
621
|
+
return `"${String(value).replace(/"/g, '""')}"`;
|
|
622
|
+
}
|
|
623
|
+
} else {
|
|
624
|
+
return String(value);
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
batchCsv += values.join(',') + '\n';
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
// 追加写入文件
|
|
632
|
+
fs.appendFileSync(exportFile, batchCsv, 'utf8');
|
|
633
|
+
|
|
634
|
+
// 增加偏移量
|
|
635
|
+
offset += batchSize;
|
|
636
|
+
|
|
637
|
+
// 打印进度信息
|
|
638
|
+
console.log(`SQL Server导出表数据到CSV进度: ${tableName} - 已处理 ${offset} 行`);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return exportFile;
|
|
642
|
+
} catch (error) {
|
|
643
|
+
console.error('SQL Server导出表数据到CSV失败:', error);
|
|
644
|
+
// @ts-ignore
|
|
645
|
+
throw new Error(`导出表数据到CSV失败: ${error.message}`);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* 导出表数据到 JSON 文件
|
|
651
|
+
*/
|
|
652
|
+
async exportTableDataToJSON(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
653
|
+
try {
|
|
654
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
655
|
+
if (!fs.existsSync(exportPath)) fs.mkdirSync(exportPath, { recursive: true });
|
|
656
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
657
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.json`);
|
|
658
|
+
|
|
659
|
+
// 写入 JSON 头部
|
|
660
|
+
fs.writeFileSync(exportFile, '[\n', 'utf8');
|
|
661
|
+
|
|
662
|
+
// 分批处理数据,避免一次性加载大量数据到内存
|
|
663
|
+
const batchSize = options?.batchSize || 10000; // 每批处理10000行
|
|
664
|
+
let offset = 0;
|
|
665
|
+
let hasMoreData = true;
|
|
666
|
+
let isFirstBatch = true;
|
|
667
|
+
|
|
668
|
+
while (hasMoreData) {
|
|
669
|
+
// 分批查询数据(SQL Server 使用 OFFSET FETCH 语法)
|
|
670
|
+
const query = `SELECT * FROM ${this.quoteIdentifier(databaseName)}.${this.quoteIdentifier(tableName)} ORDER BY (SELECT NULL) OFFSET ${offset} ROWS FETCH NEXT ${batchSize} ROWS ONLY`;
|
|
671
|
+
const data = await dataSource.query(query);
|
|
672
|
+
|
|
673
|
+
if (data.length === 0) {
|
|
674
|
+
hasMoreData = false;
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// 生成当前批次的 JSON 数据
|
|
679
|
+
let batchJson = '';
|
|
680
|
+
data.forEach((row: any, index: number) => {
|
|
681
|
+
if (!isFirstBatch || index > 0) {
|
|
682
|
+
batchJson += ',\n';
|
|
683
|
+
}
|
|
684
|
+
batchJson += JSON.stringify(row);
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
// 追加写入文件
|
|
688
|
+
fs.appendFileSync(exportFile, batchJson, 'utf8');
|
|
689
|
+
|
|
690
|
+
// 增加偏移量
|
|
691
|
+
offset += batchSize;
|
|
692
|
+
isFirstBatch = false;
|
|
693
|
+
|
|
694
|
+
// 打印进度信息
|
|
695
|
+
console.log(`SQL Server导出表数据到JSON进度: ${tableName} - 已处理 ${offset} 行`);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// 写入 JSON 尾部
|
|
699
|
+
fs.appendFileSync(exportFile, '\n]', 'utf8');
|
|
700
|
+
|
|
701
|
+
return exportFile;
|
|
702
|
+
} catch (error) {
|
|
703
|
+
console.error('SQL Server导出表数据到JSON失败:', error);
|
|
704
|
+
// @ts-ignore
|
|
705
|
+
throw new Error(`导出表数据到JSON失败: ${error.message}`);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* 导出表数据到 Excel 文件
|
|
711
|
+
*/
|
|
712
|
+
async exportTableDataToExcel(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
713
|
+
try {
|
|
714
|
+
// 由于 Excel 文件格式复杂,这里我们先导出为 CSV,然后可以考虑使用库来转换
|
|
715
|
+
// 或者直接调用其他服务来处理 Excel 导出
|
|
716
|
+
return this.exportTableDataToCSV(dataSource, databaseName, tableName, options);
|
|
717
|
+
} catch (error) {
|
|
718
|
+
console.error('SQL Server导出表数据到Excel失败:', error);
|
|
719
|
+
// @ts-ignore
|
|
720
|
+
throw new Error(`导出表数据到Excel失败: ${error.message}`);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|