fdb2 1.0.5 → 1.0.7
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/.vscodeignore +45 -0
- package/README.md +24 -85
- package/bin/fdb2.js +3 -3
- package/dist/package.json +115 -0
- package/dist/pnpm-lock.yaml +7447 -0
- package/dist/public/explorer.css +244 -111
- package/dist/public/explorer.js +518 -275
- package/dist/scripts/preinstall.js +112 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -9
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.ts +5 -10
- package/dist/server/service/connection.service.d.ts.map +1 -1
- package/dist/server/service/connection.service.js +2 -1
- package/dist/server/service/connection.service.js.map +1 -1
- package/dist/server/service/connection.service.ts +2 -1
- package/dist/server/service/database/base.service.d.ts +4 -0
- package/dist/server/service/database/base.service.d.ts.map +1 -1
- package/dist/server/service/database/base.service.js +3 -3
- package/dist/server/service/database/base.service.js.map +1 -1
- package/dist/server/service/database/base.service.ts +8 -3
- package/dist/server/service/database/cockroachdb.service.d.ts +5 -0
- package/dist/server/service/database/cockroachdb.service.d.ts.map +1 -1
- package/dist/server/service/database/cockroachdb.service.js +112 -0
- package/dist/server/service/database/cockroachdb.service.js.map +1 -1
- package/dist/server/service/database/cockroachdb.service.ts +123 -0
- 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 +8 -0
- package/dist/server/service/database/database.service.js.map +1 -1
- package/dist/server/service/database/database.service.ts +9 -0
- package/dist/server/service/database/mongodb.service.d.ts +6 -0
- package/dist/server/service/database/mongodb.service.d.ts.map +1 -1
- package/dist/server/service/database/mongodb.service.js +8 -0
- package/dist/server/service/database/mongodb.service.js.map +1 -1
- package/dist/server/service/database/mongodb.service.ts +9 -0
- package/dist/server/service/database/mssql.service.d.ts +4 -0
- package/dist/server/service/database/mssql.service.d.ts.map +1 -1
- package/dist/server/service/database/mssql.service.js +105 -0
- package/dist/server/service/database/mssql.service.js.map +1 -1
- package/dist/server/service/database/mssql.service.ts +118 -0
- package/dist/server/service/database/mysql.service.d.ts +4 -0
- package/dist/server/service/database/mysql.service.d.ts.map +1 -1
- package/dist/server/service/database/mysql.service.js +116 -0
- package/dist/server/service/database/mysql.service.js.map +1 -1
- package/dist/server/service/database/mysql.service.ts +130 -0
- package/dist/server/service/database/oracle.service.d.ts +4 -0
- package/dist/server/service/database/oracle.service.d.ts.map +1 -1
- package/dist/server/service/database/oracle.service.js +114 -0
- package/dist/server/service/database/oracle.service.js.map +1 -1
- package/dist/server/service/database/oracle.service.ts +128 -0
- package/dist/server/service/database/postgres.service.d.ts +4 -0
- package/dist/server/service/database/postgres.service.d.ts.map +1 -1
- package/dist/server/service/database/postgres.service.js +120 -0
- package/dist/server/service/database/postgres.service.js.map +1 -1
- package/dist/server/service/database/postgres.service.ts +131 -0
- package/dist/server/service/database/sap.service.d.ts +4 -0
- package/dist/server/service/database/sap.service.d.ts.map +1 -1
- package/dist/server/service/database/sap.service.js +107 -0
- package/dist/server/service/database/sap.service.js.map +1 -1
- package/dist/server/service/database/sap.service.ts +120 -0
- package/dist/server/service/database/sqlite.service.d.ts +5 -0
- package/dist/server/service/database/sqlite.service.d.ts.map +1 -1
- package/dist/server/service/database/sqlite.service.js +133 -0
- package/dist/server/service/database/sqlite.service.js.map +1 -1
- package/dist/server/service/database/sqlite.service.ts +150 -0
- package/package.json +18 -9
- package/packages/vscode/.vscodeignore +44 -0
- package/packages/vscode/README.md +62 -0
- package/packages/vscode/out/database-services/base.service.js +236 -0
- package/packages/vscode/out/database-services/base.service.js.map +1 -0
- package/packages/vscode/out/database-services/cockroachdb.service.js +634 -0
- package/packages/vscode/out/database-services/cockroachdb.service.js.map +1 -0
- package/packages/vscode/out/database-services/connection.service.js +346 -0
- package/packages/vscode/out/database-services/connection.service.js.map +1 -0
- package/packages/vscode/out/database-services/database.service.js +571 -0
- package/packages/vscode/out/database-services/database.service.js.map +1 -0
- package/packages/vscode/out/database-services/index.js +18 -0
- package/packages/vscode/out/database-services/index.js.map +1 -0
- package/packages/vscode/out/database-services/model/connection.entity.js +11 -0
- package/packages/vscode/out/database-services/model/connection.entity.js.map +1 -0
- package/packages/vscode/out/database-services/model/database.entity.js +35 -0
- package/packages/vscode/out/database-services/model/database.entity.js.map +1 -0
- package/packages/vscode/out/database-services/mongodb.service.js +458 -0
- package/packages/vscode/out/database-services/mongodb.service.js.map +1 -0
- package/packages/vscode/out/database-services/mssql.service.js +694 -0
- package/packages/vscode/out/database-services/mssql.service.js.map +1 -0
- package/packages/vscode/out/database-services/mysql.service.js +735 -0
- package/packages/vscode/out/database-services/mysql.service.js.map +1 -0
- package/packages/vscode/out/database-services/oracle.service.js +787 -0
- package/packages/vscode/out/database-services/oracle.service.js.map +1 -0
- package/packages/vscode/out/database-services/postgres.service.js +696 -0
- package/packages/vscode/out/database-services/postgres.service.js.map +1 -0
- package/packages/vscode/out/database-services/sap.service.js +695 -0
- package/packages/vscode/out/database-services/sap.service.js.map +1 -0
- package/packages/vscode/out/database-services/sqlite.service.js +532 -0
- package/packages/vscode/out/database-services/sqlite.service.js.map +1 -0
- package/packages/vscode/out/extension.js +93 -0
- package/packages/vscode/out/extension.js.map +1 -0
- package/packages/vscode/out/provider/DatabaseTreeProvider.js +159 -0
- package/packages/vscode/out/provider/DatabaseTreeProvider.js.map +1 -0
- package/packages/vscode/out/provider/WebViewProvider.js +259 -0
- package/packages/vscode/out/provider/WebViewProvider.js.map +1 -0
- package/packages/vscode/out/service/ConnectionManager.js +105 -0
- package/packages/vscode/out/service/ConnectionManager.js.map +1 -0
- package/packages/vscode/out/service/DatabaseServiceBridge.js +395 -0
- package/packages/vscode/out/service/DatabaseServiceBridge.js.map +1 -0
- package/packages/vscode/out/typings/connection.js +3 -0
- package/packages/vscode/out/typings/connection.js.map +1 -0
- package/packages/vscode/package.json +142 -0
- package/packages/vscode/resources/icon.svg +5 -0
- package/packages/vscode/resources/webview/_plugin-vue_export-helper.js +6529 -0
- package/packages/vscode/resources/webview/_plugin-vue_export-helper.js.map +1 -0
- package/packages/vscode/resources/webview/connection.css +69 -0
- package/packages/vscode/resources/webview/connection.js +228 -0
- package/packages/vscode/resources/webview/connection.js.map +1 -0
- package/packages/vscode/resources/webview/database.css +259 -0
- package/packages/vscode/resources/webview/database.js +275 -0
- package/packages/vscode/resources/webview/database.js.map +1 -0
- package/packages/vscode/resources/webview/favicon.ico +0 -0
- package/packages/vscode/resources/webview/index.html +9 -0
- package/packages/vscode/resources/webview/modules/header.tpl +14 -0
- package/packages/vscode/resources/webview/modules/initial_state.tpl +55 -0
- package/packages/vscode/resources/webview/query.css +162 -0
- package/packages/vscode/resources/webview/query.js +198 -0
- package/packages/vscode/resources/webview/query.js.map +1 -0
- package/packages/vscode/src/database-services/base.service.js.map +1 -0
- package/packages/vscode/src/database-services/base.service.ts +363 -0
- package/packages/vscode/src/database-services/cockroachdb.service.js.map +1 -0
- package/packages/vscode/src/database-services/cockroachdb.service.ts +659 -0
- package/packages/vscode/src/database-services/connection.service.ts +341 -0
- package/packages/vscode/src/database-services/database.service.ts +630 -0
- package/packages/vscode/src/database-services/index.ts +7 -0
- package/packages/vscode/src/database-services/model/connection.entity.js +11 -0
- package/packages/vscode/src/database-services/model/connection.entity.js.map +1 -0
- package/packages/vscode/src/database-services/model/connection.entity.ts +66 -0
- package/packages/vscode/src/database-services/model/database.entity.js +35 -0
- package/packages/vscode/src/database-services/model/database.entity.js.map +1 -0
- package/packages/vscode/src/database-services/model/database.entity.ts +246 -0
- package/packages/vscode/src/database-services/mongodb.service.js.map +1 -0
- package/packages/vscode/src/database-services/mongodb.service.ts +454 -0
- package/packages/vscode/src/database-services/mssql.service.js.map +1 -0
- package/packages/vscode/src/database-services/mssql.service.ts +723 -0
- package/packages/vscode/src/database-services/mysql.service.js.map +1 -0
- package/packages/vscode/src/database-services/mysql.service.ts +761 -0
- package/packages/vscode/src/database-services/oracle.service.js.map +1 -0
- package/packages/vscode/src/database-services/oracle.service.ts +832 -0
- package/packages/vscode/src/database-services/postgres.service.js.map +1 -0
- package/packages/vscode/src/database-services/postgres.service.ts +741 -0
- package/packages/vscode/src/database-services/sap.service.js.map +1 -0
- package/packages/vscode/src/database-services/sap.service.ts +713 -0
- package/packages/vscode/src/database-services/sqlite.service.js.map +1 -0
- package/packages/vscode/src/database-services/sqlite.service.ts +559 -0
- package/packages/vscode/src/extension.ts +76 -0
- package/packages/vscode/src/provider/DatabaseTreeProvider.ts +167 -0
- package/packages/vscode/src/provider/WebViewProvider.ts +277 -0
- package/packages/vscode/src/service/DatabaseServiceBridge.ts +414 -0
- package/packages/vscode/src/typings/connection.ts +90 -0
- package/packages/vscode/tsconfig.json +21 -0
- package/public/fdb2.png +0 -0
- package/scripts/preinstall.js +90 -16
- package/server/backups/db_ai_breakout_2026-03-11T08-38-48-677Z.sql +0 -0
- package/server/index.ts +5 -10
- package/server/model/connection.entity.js +11 -0
- package/server/model/connection.entity.js.map +1 -0
- package/server/model/database.entity.js +35 -0
- package/server/model/database.entity.js.map +1 -0
- package/server/service/connection.service.ts +1 -0
- package/server/service/database/base.service.ts +8 -3
- package/server/service/database/cockroachdb.service.ts +123 -0
- package/server/service/database/database.service.ts +9 -0
- package/server/service/database/mongodb.service.ts +9 -0
- package/server/service/database/mssql.service.ts +118 -0
- package/server/service/database/mysql.service.ts +130 -0
- package/server/service/database/oracle.service.ts +128 -0
- package/server/service/database/postgres.service.ts +131 -0
- package/server/service/database/sap.service.ts +120 -0
- package/server/service/database/sqlite.service.ts +150 -0
- package/server/tsconfig.json +20 -0
- package/server.js +1 -1
- package/src/components/connection-editor/index.vue +0 -1
- package/src/platform/database/components/db-tools.vue +414 -174
- package/src/platform/database/components/table-detail.vue +1 -0
- package/src/platform/database/components/table-editor.vue +245 -18
- package/src/platform/vscode/bridge.ts +121 -0
- package/src/platform/vscode/components/ConnectionPanel.vue +272 -0
- package/src/platform/vscode/components/DatabasePanel.vue +532 -0
- package/src/platform/vscode/components/QueryPanel.vue +371 -0
- package/src/platform/vscode/entry/connection.ts +13 -0
- package/src/platform/vscode/entry/database.ts +13 -0
- package/src/platform/vscode/entry/query.ts +13 -0
- package/src/platform/vscode/index.ts +5 -0
- package/src/service/database.ts +2 -6
- package/vite.config.ts +46 -6
- package/vite.config.vscode.ts +47 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 数据库信息实体
|
|
4
|
+
* 用于存储数据库的基本信息和结构
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.ForeignKeyEntity = exports.IndexEntity = exports.ColumnEntity = exports.TableEntity = exports.DatabaseEntity = void 0;
|
|
8
|
+
class DatabaseEntity {
|
|
9
|
+
}
|
|
10
|
+
exports.DatabaseEntity = DatabaseEntity;
|
|
11
|
+
/**
|
|
12
|
+
* 数据库表实体
|
|
13
|
+
*/
|
|
14
|
+
class TableEntity {
|
|
15
|
+
}
|
|
16
|
+
exports.TableEntity = TableEntity;
|
|
17
|
+
/**
|
|
18
|
+
* 数据库列实体
|
|
19
|
+
*/
|
|
20
|
+
class ColumnEntity {
|
|
21
|
+
}
|
|
22
|
+
exports.ColumnEntity = ColumnEntity;
|
|
23
|
+
/**
|
|
24
|
+
* 数据库索引实体
|
|
25
|
+
*/
|
|
26
|
+
class IndexEntity {
|
|
27
|
+
}
|
|
28
|
+
exports.IndexEntity = IndexEntity;
|
|
29
|
+
/**
|
|
30
|
+
* 外键实体
|
|
31
|
+
*/
|
|
32
|
+
class ForeignKeyEntity {
|
|
33
|
+
}
|
|
34
|
+
exports.ForeignKeyEntity = ForeignKeyEntity;
|
|
35
|
+
//# sourceMappingURL=database.entity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.entity.js","sourceRoot":"","sources":["database.entity.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,MAAa,cAAc;CAmC1B;AAnCD,wCAmCC;AAED;;GAEG;AACH,MAAa,WAAW;CAsEvB;AAtED,kCAsEC;AAED;;GAEG;AACH,MAAa,YAAY;CA4DxB;AA5DD,oCA4DC;AAED;;GAEG;AACH,MAAa,WAAW;CAyBvB;AAzBD,kCAyBC;AAED;;GAEG;AACH,MAAa,gBAAgB;CA8B5B;AA9BD,4CA8BC"}
|
|
@@ -174,7 +174,7 @@ export abstract class BaseDatabaseService {
|
|
|
174
174
|
success++;
|
|
175
175
|
} catch (error) {
|
|
176
176
|
failed++;
|
|
177
|
-
errors.push({ statement, error: error.message });
|
|
177
|
+
errors.push({ statement, error: error instanceof Error ? error.message : String(error) });
|
|
178
178
|
if (!continueOnError) {
|
|
179
179
|
throw error;
|
|
180
180
|
}
|
|
@@ -189,7 +189,7 @@ export abstract class BaseDatabaseService {
|
|
|
189
189
|
success++;
|
|
190
190
|
} catch (error) {
|
|
191
191
|
failed++;
|
|
192
|
-
errors.push({ statement, error: error.message });
|
|
192
|
+
errors.push({ statement, error: error instanceof Error ? error.message : String(error) });
|
|
193
193
|
if (!continueOnError) {
|
|
194
194
|
throw error;
|
|
195
195
|
}
|
|
@@ -199,7 +199,7 @@ export abstract class BaseDatabaseService {
|
|
|
199
199
|
} catch (batchError) {
|
|
200
200
|
console.error(`批次执行失败 (${i}-${i + batchSize}):`, batchError);
|
|
201
201
|
if (!continueOnError) {
|
|
202
|
-
throw new Error(`批次执行失败: ${batchError.message}`);
|
|
202
|
+
throw new Error(`批次执行失败: ${batchError instanceof Error ? batchError.message : String(batchError)}`);
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
}
|
|
@@ -360,4 +360,9 @@ export abstract class BaseDatabaseService {
|
|
|
360
360
|
* 导出表数据到 Excel 文件 - 子类实现
|
|
361
361
|
*/
|
|
362
362
|
abstract exportTableDataToExcel(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string>;
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* 修改表结构 - 子类实现
|
|
366
|
+
*/
|
|
367
|
+
abstract alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any>;
|
|
363
368
|
}
|
|
@@ -656,4 +656,127 @@ export class CockroachDBService extends BaseDatabaseService {
|
|
|
656
656
|
public quoteIdentifier(identifier: string): string {
|
|
657
657
|
return `"${identifier}"`;
|
|
658
658
|
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* 修改表结构
|
|
662
|
+
* CockroachDB与PostgreSQL兼容,使用类似的语法
|
|
663
|
+
*/
|
|
664
|
+
async alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any> {
|
|
665
|
+
try {
|
|
666
|
+
const tableName = tableDiff.tableName;
|
|
667
|
+
const sqlStatements: string[] = [];
|
|
668
|
+
|
|
669
|
+
// 修改表注释
|
|
670
|
+
if (tableDiff.tableCommentChanged) {
|
|
671
|
+
sqlStatements.push(`COMMENT ON TABLE "${tableName}" IS '${tableDiff.tableComment}';`);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// 添加新列
|
|
675
|
+
tableDiff.addedColumns.forEach((column: any) => {
|
|
676
|
+
// 检查type是否已经包含长度信息(括号)
|
|
677
|
+
const typeHasLength = /\(\d+\)/.test(column.type);
|
|
678
|
+
|
|
679
|
+
let columnSQL = `ALTER TABLE "${tableName}" ADD COLUMN "${column.name}" ${column.type}`;
|
|
680
|
+
|
|
681
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
682
|
+
if (!typeHasLength) {
|
|
683
|
+
if (column.length) {
|
|
684
|
+
columnSQL += `(${column.length})`;
|
|
685
|
+
} else if (column.precision) {
|
|
686
|
+
if (column.scale) {
|
|
687
|
+
columnSQL += `(${column.precision},${column.scale})`;
|
|
688
|
+
} else {
|
|
689
|
+
columnSQL += `(${column.precision})`;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// 处理NULL约束
|
|
695
|
+
if (!column.nullable) {
|
|
696
|
+
columnSQL += ' NOT NULL';
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// 处理默认值
|
|
700
|
+
if (column.defaultValue) {
|
|
701
|
+
const upperDefault = column.defaultValue.toString().toUpperCase();
|
|
702
|
+
if (['CURRENT_TIMESTAMP', 'NOW()', 'CURRENT_DATE', 'CURRENT_TIME'].includes(upperDefault)) {
|
|
703
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
704
|
+
} else {
|
|
705
|
+
columnSQL += ` DEFAULT '${column.defaultValue}'`;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
sqlStatements.push(columnSQL + ';');
|
|
710
|
+
|
|
711
|
+
// 添加列注释
|
|
712
|
+
if (column.comment) {
|
|
713
|
+
sqlStatements.push(`COMMENT ON COLUMN "${tableName}"."${column.name}" IS '${column.comment}';`);
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
// 修改列
|
|
718
|
+
tableDiff.modifiedColumns.forEach((modification: any) => {
|
|
719
|
+
const { oldColumn, newColumn } = modification;
|
|
720
|
+
|
|
721
|
+
// 检查type是否已经包含长度信息(括号)
|
|
722
|
+
const typeHasLength = /\(\d+\)/.test(newColumn.type);
|
|
723
|
+
|
|
724
|
+
let columnSQL = `ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" SET DATA TYPE ${newColumn.type}`;
|
|
725
|
+
|
|
726
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
727
|
+
if (!typeHasLength) {
|
|
728
|
+
if (newColumn.length) {
|
|
729
|
+
columnSQL += `(${newColumn.length})`;
|
|
730
|
+
} else if (newColumn.precision) {
|
|
731
|
+
if (newColumn.scale) {
|
|
732
|
+
columnSQL += `(${newColumn.precision},${newColumn.scale})`;
|
|
733
|
+
} else {
|
|
734
|
+
columnSQL += `(${newColumn.precision})`;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
sqlStatements.push(columnSQL + ';');
|
|
740
|
+
|
|
741
|
+
// 修改NULL约束
|
|
742
|
+
if (!newColumn.nullable) {
|
|
743
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" SET NOT NULL;`);
|
|
744
|
+
} else {
|
|
745
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" DROP NOT NULL;`);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// 修改默认值
|
|
749
|
+
if (newColumn.defaultValue) {
|
|
750
|
+
const upperDefault = newColumn.defaultValue.toString().toUpperCase();
|
|
751
|
+
if (['CURRENT_TIMESTAMP', 'NOW()', 'CURRENT_DATE', 'CURRENT_TIME'].includes(upperDefault)) {
|
|
752
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" SET DEFAULT ${upperDefault};`);
|
|
753
|
+
} else {
|
|
754
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" SET DEFAULT '${newColumn.defaultValue}';`);
|
|
755
|
+
}
|
|
756
|
+
} else {
|
|
757
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" ALTER COLUMN "${newColumn.name}" DROP DEFAULT;`);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// 修改列注释
|
|
761
|
+
if (newColumn.comment) {
|
|
762
|
+
sqlStatements.push(`COMMENT ON COLUMN "${tableName}"."${newColumn.name}" IS '${newColumn.comment}';`);
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
// 删除列
|
|
767
|
+
tableDiff.deletedColumns.forEach((column: any) => {
|
|
768
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" DROP COLUMN "${column.name}";`);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// 执行SQL语句
|
|
772
|
+
if (sqlStatements.length > 0) {
|
|
773
|
+
await this.executeBatchQuery(dataSource, sqlStatements, { useTransaction: true });
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
return { ret: 0, message: '表结构修改成功' };
|
|
777
|
+
} catch (error) {
|
|
778
|
+
console.error('CockroachDB修改表结构失败:', error);
|
|
779
|
+
return { ret: 1, message: `修改表结构失败: ${error instanceof Error ? error.message : String(error)}` };
|
|
780
|
+
}
|
|
781
|
+
}
|
|
659
782
|
}
|
|
@@ -144,6 +144,15 @@ export class DatabaseService {
|
|
|
144
144
|
return databaseService.executeQuery(dataSource, sql);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 修改表结构
|
|
149
|
+
*/
|
|
150
|
+
async alterTable(connectionId: string, databaseName: string, tableDiff: any): Promise<any> {
|
|
151
|
+
const dataSource = await this.connectionService.getActiveConnection(connectionId, databaseName);
|
|
152
|
+
const databaseService = this.getDatabaseService(dataSource.options.type as string);
|
|
153
|
+
return databaseService.alterTable(dataSource, databaseName, tableDiff);
|
|
154
|
+
}
|
|
155
|
+
|
|
147
156
|
/**
|
|
148
157
|
* 获取视图列表
|
|
149
158
|
*/
|
|
@@ -436,6 +436,15 @@ export class MongoDBService extends BaseDatabaseService {
|
|
|
436
436
|
public quoteIdentifier(identifier: string): string {
|
|
437
437
|
return `\`${identifier}\``;
|
|
438
438
|
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 修改表结构
|
|
442
|
+
* MongoDB是文档数据库,没有传统的表结构概念
|
|
443
|
+
* 集合中的文档可以有不同的结构,因此不需要ALTER TABLE
|
|
444
|
+
*/
|
|
445
|
+
async alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any> {
|
|
446
|
+
return { ret: 0, message: 'MongoDB是文档数据库,不需要修改表结构' };
|
|
447
|
+
}
|
|
439
448
|
}
|
|
440
449
|
|
|
441
450
|
/**
|
|
@@ -720,4 +720,122 @@ export class SQLServerService extends BaseDatabaseService {
|
|
|
720
720
|
throw new Error(`导出表数据到Excel失败: ${error.message}`);
|
|
721
721
|
}
|
|
722
722
|
}
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* 修改表结构
|
|
726
|
+
*/
|
|
727
|
+
async alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any> {
|
|
728
|
+
try {
|
|
729
|
+
const tableName = tableDiff.tableName;
|
|
730
|
+
const sqlStatements: string[] = [];
|
|
731
|
+
|
|
732
|
+
// 修改表注释(使用扩展属性)
|
|
733
|
+
if (tableDiff.tableCommentChanged) {
|
|
734
|
+
sqlStatements.push(`EXEC sp_addextendedproperty @name = N'MS_Description', @value = '${tableDiff.tableComment}', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'${tableName}';`);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// 添加新列
|
|
738
|
+
tableDiff.addedColumns.forEach((column: any) => {
|
|
739
|
+
// 检查type是否已经包含长度信息(括号)
|
|
740
|
+
const typeHasLength = /\(\d+\)/.test(column.type);
|
|
741
|
+
|
|
742
|
+
let columnSQL = `ALTER TABLE [${databaseName}].[dbo].[${tableName}] ADD [${column.name}] ${column.type}`;
|
|
743
|
+
|
|
744
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
745
|
+
if (!typeHasLength) {
|
|
746
|
+
if (column.length) {
|
|
747
|
+
columnSQL += `(${column.length})`;
|
|
748
|
+
} else if (column.precision) {
|
|
749
|
+
if (column.scale) {
|
|
750
|
+
columnSQL += `(${column.precision},${column.scale})`;
|
|
751
|
+
} else {
|
|
752
|
+
columnSQL += `(${column.precision})`;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// 处理NULL约束
|
|
758
|
+
if (!column.nullable) {
|
|
759
|
+
columnSQL += ' NOT NULL';
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// 处理默认值
|
|
763
|
+
if (column.defaultValue) {
|
|
764
|
+
const upperDefault = column.defaultValue.toString().toUpperCase();
|
|
765
|
+
if (['GETDATE()', 'GETUTCDATE()', 'CURRENT_TIMESTAMP', 'SYSDATETIME()'].includes(upperDefault)) {
|
|
766
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
767
|
+
} else {
|
|
768
|
+
columnSQL += ` DEFAULT '${column.defaultValue}'`;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// 处理自增(MSSQL使用IDENTITY)
|
|
773
|
+
if (column.isAutoIncrement) {
|
|
774
|
+
if (column.type.toLowerCase().includes('int') || column.type.toLowerCase().includes('bigint')) {
|
|
775
|
+
columnSQL += ' IDENTITY(1,1)';
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
sqlStatements.push(columnSQL + ';');
|
|
780
|
+
|
|
781
|
+
// 添加列注释
|
|
782
|
+
if (column.comment) {
|
|
783
|
+
sqlStatements.push(`EXEC sp_addextendedproperty @name = N'MS_Description', @value = '${column.comment}', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'${tableName}', @level2type = N'COLUMN', @level2name = N'${column.name}';`);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
// 修改列
|
|
788
|
+
tableDiff.modifiedColumns.forEach((modification: any) => {
|
|
789
|
+
const { oldColumn, newColumn } = modification;
|
|
790
|
+
|
|
791
|
+
// 检查type是否已经包含长度信息(括号)
|
|
792
|
+
const typeHasLength = /\(\d+\)/.test(newColumn.type);
|
|
793
|
+
|
|
794
|
+
// 修改列类型和长度
|
|
795
|
+
let columnSQL = `ALTER TABLE [${databaseName}].[dbo].[${tableName}] ALTER COLUMN [${newColumn.name}] ${newColumn.type}`;
|
|
796
|
+
|
|
797
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
798
|
+
if (!typeHasLength) {
|
|
799
|
+
if (newColumn.length) {
|
|
800
|
+
columnSQL += `(${newColumn.length})`;
|
|
801
|
+
} else if (newColumn.precision) {
|
|
802
|
+
if (newColumn.scale) {
|
|
803
|
+
columnSQL += `(${newColumn.precision},${newColumn.scale})`;
|
|
804
|
+
} else {
|
|
805
|
+
columnSQL += `(${newColumn.precision})`;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// 处理NULL约束
|
|
811
|
+
if (!newColumn.nullable) {
|
|
812
|
+
columnSQL += ' NOT NULL';
|
|
813
|
+
} else {
|
|
814
|
+
columnSQL += ' NULL';
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
sqlStatements.push(columnSQL + ';');
|
|
818
|
+
|
|
819
|
+
// 修改列注释
|
|
820
|
+
if (newColumn.comment) {
|
|
821
|
+
sqlStatements.push(`EXEC sp_addextendedproperty @name = N'MS_Description', @value = '${newColumn.comment}', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'${tableName}', @level2type = N'COLUMN', @level2name = N'${newColumn.name}';`);
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
// 删除列
|
|
826
|
+
tableDiff.deletedColumns.forEach((column: any) => {
|
|
827
|
+
sqlStatements.push(`ALTER TABLE [${databaseName}].[dbo].[${tableName}] DROP COLUMN [${column.name}];`);
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
// 执行SQL语句
|
|
831
|
+
if (sqlStatements.length > 0) {
|
|
832
|
+
await this.executeBatchQuery(dataSource, sqlStatements, { useTransaction: true });
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
return { ret: 0, message: '表结构修改成功' };
|
|
836
|
+
} catch (error) {
|
|
837
|
+
console.error('SQL Server修改表结构失败:', error);
|
|
838
|
+
return { ret: 1, message: `修改表结构失败: ${error instanceof Error ? error.message : String(error)}` };
|
|
839
|
+
}
|
|
840
|
+
}
|
|
723
841
|
}
|
|
@@ -758,4 +758,134 @@ export class MySQLService extends BaseDatabaseService {
|
|
|
758
758
|
throw new Error(`导出表数据失败: ${error.message}`);
|
|
759
759
|
}
|
|
760
760
|
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* 修改表结构
|
|
764
|
+
*/
|
|
765
|
+
async alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any> {
|
|
766
|
+
try {
|
|
767
|
+
const tableName = tableDiff.tableName;
|
|
768
|
+
const sqlStatements: string[] = [];
|
|
769
|
+
|
|
770
|
+
// 修改表注释
|
|
771
|
+
if (tableDiff.tableCommentChanged) {
|
|
772
|
+
sqlStatements.push(`ALTER TABLE \`${tableName}\` COMMENT='${tableDiff.tableComment}';`);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// 添加新列
|
|
776
|
+
tableDiff.addedColumns.forEach((column: any) => {
|
|
777
|
+
// 检查type是否已经包含长度信息(括号)
|
|
778
|
+
const typeHasLength = /\(\d+\)/.test(column.type);
|
|
779
|
+
|
|
780
|
+
let columnSQL = `ALTER TABLE \`${tableName}\` ADD COLUMN \`${column.name}\` ${column.type}`;
|
|
781
|
+
|
|
782
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
783
|
+
if (!typeHasLength) {
|
|
784
|
+
if (column.length) {
|
|
785
|
+
columnSQL += `(${column.length})`;
|
|
786
|
+
} else if (column.precision) {
|
|
787
|
+
if (column.scale) {
|
|
788
|
+
columnSQL += `(${column.precision},${column.scale})`;
|
|
789
|
+
} else {
|
|
790
|
+
columnSQL += `(${column.precision})`;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// 处理NULL约束
|
|
796
|
+
if (!column.nullable) {
|
|
797
|
+
columnSQL += ' NOT NULL';
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// 处理默认值
|
|
801
|
+
if (column.defaultValue) {
|
|
802
|
+
const upperDefault = column.defaultValue.toString().toUpperCase();
|
|
803
|
+
if (['CURRENT_TIMESTAMP', 'NOW()', 'CURRENT_DATE', 'CURRENT_TIME'].includes(upperDefault)) {
|
|
804
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
805
|
+
} else {
|
|
806
|
+
columnSQL += ` DEFAULT '${column.defaultValue}'`;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// 处理自增
|
|
811
|
+
if (column.isAutoIncrement) {
|
|
812
|
+
columnSQL += ' AUTO_INCREMENT';
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// 处理注释
|
|
816
|
+
if (column.comment) {
|
|
817
|
+
columnSQL += ` COMMENT '${column.comment}'`;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
sqlStatements.push(columnSQL + ';');
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
// 修改列
|
|
824
|
+
tableDiff.modifiedColumns.forEach((modification: any) => {
|
|
825
|
+
const { oldColumn, newColumn } = modification;
|
|
826
|
+
|
|
827
|
+
// 检查type是否已经包含长度信息(括号)
|
|
828
|
+
const typeHasLength = /\(\d+\)/.test(newColumn.type);
|
|
829
|
+
|
|
830
|
+
let columnSQL = `ALTER TABLE \`${tableName}\` MODIFY COLUMN \`${newColumn.name}\` ${newColumn.type}`;
|
|
831
|
+
|
|
832
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
833
|
+
if (!typeHasLength) {
|
|
834
|
+
if (newColumn.length) {
|
|
835
|
+
columnSQL += `(${newColumn.length})`;
|
|
836
|
+
} else if (newColumn.precision) {
|
|
837
|
+
if (newColumn.scale) {
|
|
838
|
+
columnSQL += `(${newColumn.precision},${newColumn.scale})`;
|
|
839
|
+
} else {
|
|
840
|
+
columnSQL += `(${newColumn.precision})`;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// 处理NULL约束
|
|
846
|
+
if (!newColumn.nullable) {
|
|
847
|
+
columnSQL += ' NOT NULL';
|
|
848
|
+
} else {
|
|
849
|
+
columnSQL += ' NULL';
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// 处理默认值
|
|
853
|
+
if (newColumn.defaultValue) {
|
|
854
|
+
const upperDefault = newColumn.defaultValue.toString().toUpperCase();
|
|
855
|
+
if (['CURRENT_TIMESTAMP', 'NOW()', 'CURRENT_DATE', 'CURRENT_TIME'].includes(upperDefault)) {
|
|
856
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
857
|
+
} else {
|
|
858
|
+
columnSQL += ` DEFAULT '${newColumn.defaultValue}'`;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// 处理自增
|
|
863
|
+
if (newColumn.isAutoIncrement) {
|
|
864
|
+
columnSQL += ' AUTO_INCREMENT';
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// 处理注释
|
|
868
|
+
if (newColumn.comment) {
|
|
869
|
+
columnSQL += ` COMMENT '${newColumn.comment}'`;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
sqlStatements.push(columnSQL + ';');
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
// 删除列
|
|
876
|
+
tableDiff.deletedColumns.forEach((column: any) => {
|
|
877
|
+
sqlStatements.push(`ALTER TABLE \`${tableName}\` DROP COLUMN \`${column.name}\`;`);
|
|
878
|
+
});
|
|
879
|
+
|
|
880
|
+
// 执行SQL语句
|
|
881
|
+
if (sqlStatements.length > 0) {
|
|
882
|
+
await this.executeBatchQuery(dataSource, sqlStatements, { useTransaction: true });
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
return { ret: 0, message: '表结构修改成功' };
|
|
886
|
+
} catch (error) {
|
|
887
|
+
console.error('MySQL修改表结构失败:', error);
|
|
888
|
+
return { ret: 1, message: `修改表结构失败: ${error instanceof Error ? error.message : String(error)}` };
|
|
889
|
+
}
|
|
890
|
+
}
|
|
761
891
|
}
|
|
@@ -829,4 +829,132 @@ ALTER DATABASE OPEN;
|
|
|
829
829
|
throw new Error(`导出表数据到Excel失败: ${error.message}`);
|
|
830
830
|
}
|
|
831
831
|
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* 修改表结构
|
|
835
|
+
*/
|
|
836
|
+
async alterTable(dataSource: DataSource, databaseName: string, tableDiff: any): Promise<any> {
|
|
837
|
+
try {
|
|
838
|
+
const tableName = tableDiff.tableName;
|
|
839
|
+
const sqlStatements: string[] = [];
|
|
840
|
+
|
|
841
|
+
// 修改表注释
|
|
842
|
+
if (tableDiff.tableCommentChanged) {
|
|
843
|
+
sqlStatements.push(`COMMENT ON TABLE "${tableName}" IS '${tableDiff.tableComment}';`);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// 添加新列
|
|
847
|
+
tableDiff.addedColumns.forEach((column: any) => {
|
|
848
|
+
// 检查type是否已经包含长度信息(括号)
|
|
849
|
+
const typeHasLength = /\(\d+\)/.test(column.type);
|
|
850
|
+
|
|
851
|
+
let columnSQL = `ALTER TABLE "${tableName}" ADD ("${column.name}" ${column.type}`;
|
|
852
|
+
|
|
853
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
854
|
+
if (!typeHasLength) {
|
|
855
|
+
if (column.length) {
|
|
856
|
+
columnSQL += `(${column.length})`;
|
|
857
|
+
} else if (column.precision) {
|
|
858
|
+
if (column.scale) {
|
|
859
|
+
columnSQL += `(${column.precision},${column.scale})`;
|
|
860
|
+
} else {
|
|
861
|
+
columnSQL += `(${column.precision})`;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// 处理NULL约束
|
|
867
|
+
if (!column.nullable) {
|
|
868
|
+
columnSQL += ' NOT NULL';
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// 处理默认值
|
|
872
|
+
if (column.defaultValue) {
|
|
873
|
+
const upperDefault = column.defaultValue.toString().toUpperCase();
|
|
874
|
+
if (['SYSDATE', 'SYSTIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIMESTAMP'].includes(upperDefault)) {
|
|
875
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
876
|
+
} else {
|
|
877
|
+
columnSQL += ` DEFAULT '${column.defaultValue}'`;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
// 处理自增(Oracle使用GENERATED ALWAYS AS IDENTITY或SEQUENCE)
|
|
882
|
+
if (column.isAutoIncrement) {
|
|
883
|
+
if (column.type.toLowerCase().includes('number')) {
|
|
884
|
+
columnSQL += ' GENERATED ALWAYS AS IDENTITY';
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
columnSQL += ')';
|
|
889
|
+
sqlStatements.push(columnSQL + ';');
|
|
890
|
+
|
|
891
|
+
// 添加列注释
|
|
892
|
+
if (column.comment) {
|
|
893
|
+
sqlStatements.push(`COMMENT ON COLUMN "${tableName}"."${column.name}" IS '${column.comment}';`);
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
|
|
897
|
+
// 修改列
|
|
898
|
+
tableDiff.modifiedColumns.forEach((modification: any) => {
|
|
899
|
+
const { oldColumn, newColumn } = modification;
|
|
900
|
+
|
|
901
|
+
// 检查type是否已经包含长度信息(括号)
|
|
902
|
+
const typeHasLength = /\(\d+\)/.test(newColumn.type);
|
|
903
|
+
|
|
904
|
+
// 修改列类型和长度
|
|
905
|
+
let columnSQL = `ALTER TABLE "${tableName}" MODIFY ("${newColumn.name}" ${newColumn.type}`;
|
|
906
|
+
|
|
907
|
+
// 处理长度和精度(仅当type中没有指定长度时才添加)
|
|
908
|
+
if (!typeHasLength) {
|
|
909
|
+
if (newColumn.length) {
|
|
910
|
+
columnSQL += `(${newColumn.length})`;
|
|
911
|
+
} else if (newColumn.precision) {
|
|
912
|
+
if (newColumn.scale) {
|
|
913
|
+
columnSQL += `(${newColumn.precision},${newColumn.scale})`;
|
|
914
|
+
} else {
|
|
915
|
+
columnSQL += `(${newColumn.precision})`;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// 处理NULL约束
|
|
921
|
+
if (!newColumn.nullable) {
|
|
922
|
+
columnSQL += ' NOT NULL';
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// 处理默认值
|
|
926
|
+
if (newColumn.defaultValue) {
|
|
927
|
+
const upperDefault = newColumn.defaultValue.toString().toUpperCase();
|
|
928
|
+
if (['SYSDATE', 'SYSTIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIMESTAMP'].includes(upperDefault)) {
|
|
929
|
+
columnSQL += ` DEFAULT ${upperDefault}`;
|
|
930
|
+
} else {
|
|
931
|
+
columnSQL += ` DEFAULT '${newColumn.defaultValue}'`;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
columnSQL += ')';
|
|
936
|
+
sqlStatements.push(columnSQL + ';');
|
|
937
|
+
|
|
938
|
+
// 修改列注释
|
|
939
|
+
if (newColumn.comment) {
|
|
940
|
+
sqlStatements.push(`COMMENT ON COLUMN "${tableName}"."${newColumn.name}" IS '${newColumn.comment}';`);
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
// 删除列
|
|
945
|
+
tableDiff.deletedColumns.forEach((column: any) => {
|
|
946
|
+
sqlStatements.push(`ALTER TABLE "${tableName}" DROP COLUMN "${column.name}";`);
|
|
947
|
+
});
|
|
948
|
+
|
|
949
|
+
// 执行SQL语句
|
|
950
|
+
if (sqlStatements.length > 0) {
|
|
951
|
+
await this.executeBatchQuery(dataSource, sqlStatements, { useTransaction: true });
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
return { ret: 0, message: '表结构修改成功' };
|
|
955
|
+
} catch (error) {
|
|
956
|
+
console.error('Oracle修改表结构失败:', error);
|
|
957
|
+
return { ret: 1, message: `修改表结构失败: ${error instanceof Error ? error.message : String(error)}` };
|
|
958
|
+
}
|
|
959
|
+
}
|
|
832
960
|
}
|