fdb2 1.0.8 → 1.0.10

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.
Files changed (235) hide show
  1. package/.dockerignore +21 -21
  2. package/.editorconfig +11 -11
  3. package/.eslintrc.cjs +14 -14
  4. package/.eslintrc.json +7 -7
  5. package/.prettierrc.js +3 -3
  6. package/.tpl.env +21 -21
  7. package/.vscodeignore +45 -45
  8. package/README.md +312 -312
  9. package/bin/build.sh +28 -28
  10. package/bin/deploy.sh +8 -8
  11. package/bin/dev.sh +10 -10
  12. package/bin/docker/dev-docker-compose.yml +43 -43
  13. package/bin/docker/dev.Dockerfile +24 -24
  14. package/bin/docker/prod-docker-compose.yml +17 -17
  15. package/bin/docker/prod.Dockerfile +29 -29
  16. package/bin/fdb2.js +220 -220
  17. package/dist/package.json +29 -29
  18. package/dist/pnpm-lock.yaml +1042 -354
  19. package/dist/public/explorer.css +1464 -1437
  20. package/dist/public/explorer.js +759 -223
  21. package/dist/public/index.css +1026 -1026
  22. package/dist/public/index.js +15 -9
  23. package/dist/public/layout.css +221 -221
  24. package/dist/public/layout.js +1 -1
  25. package/dist/public/vue.js +8 -2
  26. package/dist/scripts/preinstall.js +112 -112
  27. package/dist/server/index.d.ts.map +1 -1
  28. package/dist/server/index.js +8 -0
  29. package/dist/server/index.js.map +1 -1
  30. package/dist/server/index.ts +680 -671
  31. package/dist/server/model/connection.entity.ts +65 -65
  32. package/dist/server/model/database.entity.ts +245 -245
  33. package/dist/server/service/connection.service.d.ts +6 -1
  34. package/dist/server/service/connection.service.d.ts.map +1 -1
  35. package/dist/server/service/connection.service.js +18 -1
  36. package/dist/server/service/connection.service.js.map +1 -1
  37. package/dist/server/service/connection.service.ts +358 -341
  38. package/dist/server/service/database/base.service.d.ts +27 -0
  39. package/dist/server/service/database/base.service.d.ts.map +1 -1
  40. package/dist/server/service/database/base.service.js +17 -0
  41. package/dist/server/service/database/base.service.js.map +1 -1
  42. package/dist/server/service/database/base.service.ts +406 -367
  43. package/dist/server/service/database/cockroachdb.service.d.ts +16 -0
  44. package/dist/server/service/database/cockroachdb.service.d.ts.map +1 -1
  45. package/dist/server/service/database/cockroachdb.service.js +220 -154
  46. package/dist/server/service/database/cockroachdb.service.js.map +1 -1
  47. package/dist/server/service/database/cockroachdb.service.ts +871 -782
  48. package/dist/server/service/database/database.service.d.ts +4 -0
  49. package/dist/server/service/database/database.service.d.ts.map +1 -1
  50. package/dist/server/service/database/database.service.js +123 -0
  51. package/dist/server/service/database/database.service.js.map +1 -1
  52. package/dist/server/service/database/database.service.ts +775 -638
  53. package/dist/server/service/database/index.ts +6 -6
  54. package/dist/server/service/database/mongodb.service.d.ts +16 -0
  55. package/dist/server/service/database/mongodb.service.d.ts.map +1 -1
  56. package/dist/server/service/database/mongodb.service.js +35 -0
  57. package/dist/server/service/database/mongodb.service.js.map +1 -1
  58. package/dist/server/service/database/mongodb.service.ts +39 -1
  59. package/dist/server/service/database/mssql.service.d.ts +16 -0
  60. package/dist/server/service/database/mssql.service.d.ts.map +1 -1
  61. package/dist/server/service/database/mssql.service.js +168 -96
  62. package/dist/server/service/database/mssql.service.js.map +1 -1
  63. package/dist/server/service/database/mssql.service.ts +931 -840
  64. package/dist/server/service/database/mysql.service.d.ts +16 -0
  65. package/dist/server/service/database/mysql.service.d.ts.map +1 -1
  66. package/dist/server/service/database/mysql.service.js +189 -80
  67. package/dist/server/service/database/mysql.service.js.map +1 -1
  68. package/dist/server/service/database/mysql.service.ts +1025 -890
  69. package/dist/server/service/database/oracle.service.d.ts +16 -0
  70. package/dist/server/service/database/oracle.service.d.ts.map +1 -1
  71. package/dist/server/service/database/oracle.service.js +182 -120
  72. package/dist/server/service/database/oracle.service.js.map +1 -1
  73. package/dist/server/service/database/oracle.service.ts +1035 -959
  74. package/dist/server/service/database/postgres.service.d.ts +16 -0
  75. package/dist/server/service/database/postgres.service.d.ts.map +1 -1
  76. package/dist/server/service/database/postgres.service.js +154 -88
  77. package/dist/server/service/database/postgres.service.js.map +1 -1
  78. package/dist/server/service/database/postgres.service.ts +960 -871
  79. package/dist/server/service/database/sap.service.d.ts +16 -0
  80. package/dist/server/service/database/sap.service.d.ts.map +1 -1
  81. package/dist/server/service/database/sap.service.js +66 -0
  82. package/dist/server/service/database/sap.service.js.map +1 -1
  83. package/dist/server/service/database/sap.service.ts +89 -0
  84. package/dist/server/service/database/sqlite.service.d.ts +17 -1
  85. package/dist/server/service/database/sqlite.service.d.ts.map +1 -1
  86. package/dist/server/service/database/sqlite.service.js +78 -19
  87. package/dist/server/service/database/sqlite.service.js.map +1 -1
  88. package/dist/server/service/database/sqlite.service.ts +787 -708
  89. package/dist/server/service/session.service.ts +158 -158
  90. package/dist/view/index.html +38 -38
  91. package/env.d.ts +1 -1
  92. package/package.json +7 -2
  93. package/packages/vscode/.vscodeignore +44 -44
  94. package/packages/vscode/README.md +62 -62
  95. package/packages/vscode/out/database-services/cockroachdb.service.js +154 -154
  96. package/packages/vscode/out/database-services/mssql.service.js +96 -96
  97. package/packages/vscode/out/database-services/mysql.service.js +80 -80
  98. package/packages/vscode/out/database-services/oracle.service.js +120 -120
  99. package/packages/vscode/out/database-services/postgres.service.js +88 -88
  100. package/packages/vscode/out/database-services/sqlite.service.js +18 -18
  101. package/packages/vscode/out/provider/WebViewProvider.js +32 -32
  102. package/packages/vscode/package.json +142 -142
  103. package/packages/vscode/resources/icon.svg +5 -5
  104. package/packages/vscode/resources/webview/connection.css +41 -41
  105. package/packages/vscode/resources/webview/database.css +163 -163
  106. package/packages/vscode/resources/webview/index.html +9 -9
  107. package/packages/vscode/resources/webview/modules/header.tpl +13 -13
  108. package/packages/vscode/resources/webview/modules/initial_state.tpl +54 -54
  109. package/packages/vscode/resources/webview/query.css +104 -104
  110. package/packages/vscode/src/database-services/base.service.ts +362 -362
  111. package/packages/vscode/src/database-services/cockroachdb.service.ts +659 -659
  112. package/packages/vscode/src/database-services/connection.service.ts +340 -340
  113. package/packages/vscode/src/database-services/database.service.ts +629 -629
  114. package/packages/vscode/src/database-services/index.ts +6 -6
  115. package/packages/vscode/src/database-services/model/connection.entity.ts +65 -65
  116. package/packages/vscode/src/database-services/model/database.entity.ts +245 -245
  117. package/packages/vscode/src/database-services/mssql.service.ts +722 -722
  118. package/packages/vscode/src/database-services/mysql.service.ts +760 -760
  119. package/packages/vscode/src/database-services/oracle.service.ts +831 -831
  120. package/packages/vscode/src/database-services/postgres.service.ts +740 -740
  121. package/packages/vscode/src/database-services/sqlite.service.ts +558 -558
  122. package/packages/vscode/src/extension.ts +76 -76
  123. package/packages/vscode/src/provider/DatabaseTreeProvider.ts +167 -167
  124. package/packages/vscode/src/provider/WebViewProvider.ts +277 -277
  125. package/packages/vscode/src/service/DatabaseServiceBridge.ts +414 -414
  126. package/packages/vscode/src/typings/connection.ts +90 -90
  127. package/packages/vscode/tsconfig.json +21 -21
  128. package/public/index.html +9 -9
  129. package/public/modules/header.tpl +13 -13
  130. package/public/modules/initial_state.tpl +54 -54
  131. package/scripts/preinstall.js +112 -112
  132. package/server/index.ts +680 -671
  133. package/server/model/connection.entity.ts +65 -65
  134. package/server/model/database.entity.ts +245 -245
  135. package/server/service/connection.service.ts +358 -341
  136. package/server/service/database/base.service.ts +406 -367
  137. package/server/service/database/cockroachdb.service.ts +871 -782
  138. package/server/service/database/database.service.ts +775 -638
  139. package/server/service/database/index.ts +6 -6
  140. package/server/service/database/mongodb.service.ts +39 -1
  141. package/server/service/database/mssql.service.ts +931 -840
  142. package/server/service/database/mysql.service.ts +1025 -890
  143. package/server/service/database/oracle.service.ts +1035 -959
  144. package/server/service/database/postgres.service.ts +960 -871
  145. package/server/service/database/sap.service.ts +89 -0
  146. package/server/service/database/sqlite.service.ts +787 -708
  147. package/server/service/session.service.ts +158 -158
  148. package/server/tsconfig.json +20 -20
  149. package/server.js +149 -149
  150. package/server.pid +1 -0
  151. package/src/adapter/ajax.ts +135 -135
  152. package/src/assets/base.css +1 -1
  153. package/src/assets/database.css +949 -949
  154. package/src/assets/images/svg/illustrations/illustration-1.svg +1 -1
  155. package/src/assets/images/svg/illustrations/illustration-2.svg +2 -2
  156. package/src/assets/images/svg/illustrations/illustration-3.svg +50 -50
  157. package/src/assets/images/svg/illustrations/illustration-4.svg +1 -1
  158. package/src/assets/images/svg/illustrations/illustration-5.svg +73 -73
  159. package/src/assets/images/svg/illustrations/illustration-6.svg +89 -89
  160. package/src/assets/images/svg/illustrations/illustration-7.svg +39 -39
  161. package/src/assets/images/svg/separators/curve-2.svg +3 -3
  162. package/src/assets/images/svg/separators/curve.svg +3 -3
  163. package/src/assets/images/svg/separators/line.svg +3 -3
  164. package/src/assets/logo.svg +73 -73
  165. package/src/assets/main.css +1 -1
  166. package/src/base/config.ts +20 -20
  167. package/src/base/detect.ts +134 -134
  168. package/src/base/entity.ts +92 -92
  169. package/src/base/eventBus.ts +36 -36
  170. package/src/components/connection-editor/index.vue +588 -588
  171. package/src/components/dataGrid/index.vue +104 -104
  172. package/src/components/dataGrid/pagination.vue +105 -105
  173. package/src/components/loading/index.vue +42 -42
  174. package/src/components/modal/index.ts +180 -180
  175. package/src/components/modal/index.vue +560 -560
  176. package/src/components/toast/index.ts +43 -43
  177. package/src/components/toast/toast.vue +57 -57
  178. package/src/components/user/name.vue +103 -103
  179. package/src/components/user/selector.vue +416 -416
  180. package/src/domain/SysConfig.ts +74 -74
  181. package/src/platform/App.vue +7 -7
  182. package/src/platform/database/components/connection-detail.vue +1153 -1154
  183. package/src/platform/database/components/data-editor.vue +477 -477
  184. package/src/platform/database/components/database-detail.vue +1173 -1172
  185. package/src/platform/database/components/database-monitor.vue +1085 -1085
  186. package/src/platform/database/components/db-tools.vue +1264 -816
  187. package/src/platform/database/components/query-history.vue +1348 -1348
  188. package/src/platform/database/components/sql-executor.vue +737 -737
  189. package/src/platform/database/components/sql-query-editor.vue +1045 -1045
  190. package/src/platform/database/components/table-detail.vue +1375 -1376
  191. package/src/platform/database/components/table-editor.vue +916 -916
  192. package/src/platform/database/explorer.vue +1839 -1839
  193. package/src/platform/database/index.vue +1192 -1192
  194. package/src/platform/database/layout.vue +366 -366
  195. package/src/platform/database/router.ts +36 -36
  196. package/src/platform/database/styles/common.scss +601 -601
  197. package/src/platform/database/types/common.ts +444 -444
  198. package/src/platform/database/utils/export.ts +231 -231
  199. package/src/platform/database/utils/helpers.ts +436 -436
  200. package/src/platform/index.ts +32 -32
  201. package/src/platform/router.ts +40 -40
  202. package/src/platform/vscode/bridge.ts +121 -121
  203. package/src/platform/vscode/components/ConnectionPanel.vue +272 -272
  204. package/src/platform/vscode/components/DatabasePanel.vue +532 -532
  205. package/src/platform/vscode/components/QueryPanel.vue +371 -371
  206. package/src/platform/vscode/entry/connection.ts +13 -13
  207. package/src/platform/vscode/entry/database.ts +13 -13
  208. package/src/platform/vscode/entry/query.ts +13 -13
  209. package/src/platform/vscode/index.ts +5 -5
  210. package/src/service/base.ts +133 -127
  211. package/src/service/database.ts +505 -495
  212. package/src/service/login.ts +120 -120
  213. package/src/shims-vue.d.ts +6 -6
  214. package/src/stores/connection.ts +266 -266
  215. package/src/stores/session.ts +87 -87
  216. package/src/typings/database-types.ts +412 -412
  217. package/src/typings/database.ts +363 -363
  218. package/src/typings/global.d.ts +58 -58
  219. package/src/typings/pinia.d.ts +7 -7
  220. package/src/utils/clipboard.ts +29 -29
  221. package/src/utils/database-types.ts +242 -242
  222. package/src/utils/modal.ts +123 -123
  223. package/src/utils/request.ts +55 -55
  224. package/src/utils/sleep.ts +3 -3
  225. package/src/utils/toast.ts +73 -73
  226. package/src/utils/util.ts +171 -171
  227. package/src/utils/xlsx.ts +228 -228
  228. package/tsconfig.json +33 -33
  229. package/view/index.html +9 -9
  230. package/view/modules/header.tpl +13 -13
  231. package/view/modules/initial_state.tpl +19 -19
  232. package/vite.config.ts +424 -424
  233. package/vite.config.vscode.ts +47 -47
  234. package/fdb2.server.pid +0 -1
  235. package/server/backups/db_ai_breakout_2026-03-11T08-38-48-677Z.sql +0 -0
@@ -1,917 +1,917 @@
1
- <template>
2
- <div v-if="visible" class="modal fade show" style="display: block; background-color: rgba(0,0,0,0.5);" @click="close">
3
- <div class="modal-dialog modal-lg" style="width: 80%; max-width: none;" @click.stop>
4
- <div class="modal-content">
5
- <div class="modal-header">
6
- <h5 class="modal-title">
7
- <i class="bi bi-pencil-square me-2"></i>
8
- {{ mode === 'create' ? '创建新表' : '修改表结构' }}
9
- </h5>
10
- <button type="button" class="btn-close" @click="close"></button>
11
- </div>
12
-
13
- <div class="modal-body">
14
- <!-- 表基本信息 -->
15
- <div class="table-info-section mb-4">
16
- <h6 class="section-title">表信息</h6>
17
- <div class="row">
18
- <div class="col-md-6">
19
- <div class="mb-3">
20
- <label class="form-label">表名</label>
21
- <input
22
- v-model="formData.tableName"
23
- type="text"
24
- class="form-control"
25
- :disabled="mode === 'edit'"
26
- placeholder="请输入表名"
27
- >
28
- </div>
29
- </div>
30
- <div class="col-md-6">
31
- <div class="mb-3">
32
- <label class="form-label">表注释</label>
33
- <input
34
- v-model="formData.tableComment"
35
- type="text"
36
- class="form-control"
37
- placeholder="请输入表注释"
38
- >
39
- </div>
40
- </div>
41
- </div>
42
- </div>
43
-
44
- <!-- 字段管理 -->
45
- <div class="columns-section">
46
- <div class="section-header d-flex justify-content-between align-items-center mb-3">
47
- <h6 class="section-title mb-0">字段管理</h6>
48
- <button class="btn btn-success btn-sm" @click="addColumn">
49
- <i class="bi bi-plus-lg"></i> 添加字段
50
- </button>
51
- </div>
52
-
53
- <div class="table-responsive">
54
- <table class="table table-sm table-bordered">
55
- <thead class="table-light">
56
- <tr>
57
- <th width="120">字段名</th>
58
- <th width="140">数据类型</th>
59
- <th width="120">长度/精度</th>
60
- <th width="80">可空</th>
61
- <th width="100">默认值</th>
62
- <th width="80">主键</th>
63
- <th width="80">自增</th>
64
- <th>注释</th>
65
- <th width="100">操作</th>
66
- </tr>
67
- </thead>
68
- <tbody>
69
- <tr v-for="(column, index) in formData.columns" :key="index">
70
- <td>
71
- <input
72
- v-model="column.name"
73
- type="text"
74
- class="form-control form-control-sm"
75
- placeholder="字段名"
76
- >
77
- </td>
78
- <td>
79
- <select v-model="column.type" class="form-select form-select-sm">
80
- <option value="">选择类型</option>
81
- <optgroup v-for="(types, category) in columnTypesByCategory" :key="category" :label="getCategoryLabel(category)">
82
- <option v-for="type in types" :key="type.name" :value="type.name">
83
- {{ type.label }}
84
- </option>
85
- </optgroup>
86
- </select>
87
- </td>
88
- <td>
89
- <div class="input-group input-group-sm" v-if="needsLength(column) || needsPrecision(column)">
90
- <input
91
- v-if="needsLength(column)"
92
- v-model="column.length"
93
- type="number"
94
- class="form-control form-control-sm"
95
- placeholder="长度"
96
- >
97
- <input
98
- v-if="needsPrecision(column)"
99
- v-model="column.precision"
100
- type="number"
101
- class="form-control form-control-sm"
102
- placeholder="精度"
103
- >
104
- <input
105
- v-if="needsScale(column)"
106
- v-model="column.scale"
107
- type="number"
108
- class="form-control form-control-sm"
109
- placeholder="小数"
110
- >
111
- </div>
112
- <span v-else class="text-muted">-</span>
113
- </td>
114
- <td>
115
- <div class="form-check">
116
- <input
117
- v-model="column.nullable"
118
- type="checkbox"
119
- class="form-check-input"
120
- >
121
- </div>
122
- </td>
123
- <td>
124
- <input
125
- v-model="column.defaultValue"
126
- type="text"
127
- class="form-control form-control-sm"
128
- placeholder="默认值"
129
- >
130
- </td>
131
- <td>
132
- <div class="form-check">
133
- <input
134
- v-model="column.isPrimary"
135
- type="checkbox"
136
- class="form-check-input"
137
- @change="onPrimaryKeyChange(column, index)"
138
- >
139
- </div>
140
- </td>
141
- <td>
142
- <div class="form-check">
143
- <input
144
- v-model="column.isAutoIncrement"
145
- type="checkbox"
146
- class="form-check-input"
147
- :disabled="!column.isPrimary || !supportsAutoIncrement(column)"
148
- >
149
- </div>
150
- </td>
151
- <td>
152
- <input
153
- v-model="column.comment"
154
- type="text"
155
- class="form-control form-control-sm"
156
- placeholder="字段注释"
157
- >
158
- </td>
159
- <td>
160
- <button
161
- class="btn btn-outline-danger btn-sm"
162
- @click="removeColumn(index)"
163
- :disabled="formData.columns.length <= 1"
164
- >
165
- <i class="bi bi-trash"></i>
166
- </button>
167
- </td>
168
- </tr>
169
- </tbody>
170
- </table>
171
- </div>
172
- </div>
173
-
174
- <!-- SQL预览 -->
175
- <div class="sql-preview-section mt-4">
176
- <h6 class="section-title">SQL预览</h6>
177
- <pre class="sql-preview bg-light p-3 rounded">{{ generateSQL() }}</pre>
178
- </div>
179
- </div>
180
-
181
- <div class="modal-footer">
182
- <button type="button" class="btn btn-secondary" @click="close">取消</button>
183
- <button type="button" class="btn btn-primary" @click="submit">保存</button>
184
- </div>
185
- </div>
186
- </div>
187
- </div>
188
- </template>
189
-
190
- <script lang="ts" setup>
191
- import { ref, computed, watch, onMounted } from 'vue';
192
- import type { ConnectionEntity, TableEntity } from '@/typings/database';
193
- import { DatabaseService } from '@/service/database';
194
- import { modal } from '@/utils/modal';
195
- import { getColumnTypesByName, ColumnCategory } from '@/typings/database-types';
196
- import { isNumericType, isBooleanType } from '@/utils/database-types';
197
-
198
- // Props
199
- const props = defineProps<{
200
- visible: boolean;
201
- connection: ConnectionEntity | null;
202
- database: string;
203
- table?: TableEntity | null;
204
- columns?: any[];
205
- mode: 'create' | 'edit';
206
- }>();
207
-
208
- // Emits
209
- const emit = defineEmits<{
210
- close: [];
211
- submit: [result: any];
212
- }>();
213
-
214
- const databaseService = new DatabaseService();
215
-
216
- // 获取当前数据库支持的列类型
217
- const columnTypes = computed(() => {
218
- if (!props.connection) return [];
219
- return getColumnTypesByName(props.connection.type);
220
- });
221
-
222
- // 按类别分组的列类型
223
- const columnTypesByCategory = computed(() => {
224
- const categories: Record<string, any[]> = {};
225
- columnTypes.value.forEach(type => {
226
- if (!categories[type.category]) {
227
- categories[type.category] = [];
228
- }
229
- categories[type.category].push(type);
230
- });
231
- return categories;
232
- });
233
-
234
- // 表单数据
235
- const formData = ref({
236
- tableName: '',
237
- tableComment: '',
238
- columns: [{
239
- name: '',
240
- type: '',
241
- length: '',
242
- nullable: false,
243
- defaultValue: '',
244
- isPrimary: false,
245
- isAutoIncrement: false,
246
- comment: ''
247
- }]
248
- });
249
-
250
- // 原始表结构(用于对比差异)
251
- const originalTableData = ref({
252
- tableName: '',
253
- tableComment: '',
254
- columns: []
255
- });
256
-
257
- // 初始化表单数据
258
- function initFormData() {
259
- if (props.mode === 'edit' && props.table) {
260
- const columns = props.columns || props.table.columns || [];
261
- const tableData = {
262
- tableName: props.table.name || '',
263
- tableComment: props.table.comment || '',
264
- columns: columns.map(col => ({
265
- name: col.name || '',
266
- type: col.type || '',
267
- length: col.length || '',
268
- precision: col.precision || null,
269
- scale: col.scale || null,
270
- nullable: col.nullable || false,
271
- defaultValue: col.defaultValue || '',
272
- isPrimary: col.isPrimary || false,
273
- isAutoIncrement: col.isAutoIncrement || false,
274
- comment: col.comment || ''
275
- })) || []
276
- };
277
- formData.value = { ...tableData };
278
- originalTableData.value = JSON.parse(JSON.stringify(tableData));
279
- } else {
280
- formData.value = {
281
- tableName: '',
282
- tableComment: '',
283
- columns: [{
284
- name: '',
285
- type: '',
286
- length: '',
287
- precision: null,
288
- scale: null,
289
- nullable: false,
290
- defaultValue: '',
291
- isPrimary: false,
292
- isAutoIncrement: false,
293
- comment: ''
294
- }]
295
- };
296
- originalTableData.value = {
297
- tableName: '',
298
- tableComment: '',
299
- columns: []
300
- };
301
- }
302
- }
303
-
304
- // 计算表结构差异
305
- function calculateTableDiff() {
306
- const diff = {
307
- tableName: formData.value.tableName,
308
- tableCommentChanged: formData.value.tableComment !== originalTableData.value.tableComment,
309
- tableComment: formData.value.tableComment,
310
- addedColumns: [],
311
- modifiedColumns: [],
312
- deletedColumns: []
313
- };
314
-
315
- // 创建原始列的映射(按列名)
316
- const originalColumnsMap = new Map();
317
- originalTableData.value.columns.forEach(col => {
318
- originalColumnsMap.set(col.name, col);
319
- });
320
-
321
- // 检查新增和修改的列
322
- formData.value.columns.forEach(newCol => {
323
- const originalCol = originalColumnsMap.get(newCol.name);
324
-
325
- if (!originalCol) {
326
- // 新增列
327
- diff.addedColumns.push(newCol);
328
- } else {
329
- // 检查列是否被修改
330
- const isModified =
331
- newCol.type !== originalCol.type ||
332
- newCol.length !== originalCol.length ||
333
- newCol.precision !== originalCol.precision ||
334
- newCol.scale !== originalCol.scale ||
335
- newCol.nullable !== originalCol.nullable ||
336
- newCol.defaultValue !== originalCol.defaultValue ||
337
- newCol.isPrimary !== originalCol.isPrimary ||
338
- newCol.isAutoIncrement !== originalCol.isAutoIncrement ||
339
- newCol.comment !== originalCol.comment;
340
-
341
- if (isModified) {
342
- diff.modifiedColumns.push({
343
- oldColumn: originalCol,
344
- newColumn: newCol
345
- });
346
- }
347
- }
348
- });
349
-
350
- // 检查删除的列
351
- originalTableData.value.columns.forEach(originalCol => {
352
- const existsInNew = formData.value.columns.some(newCol => newCol.name === originalCol.name);
353
- if (!existsInNew) {
354
- diff.deletedColumns.push(originalCol);
355
- }
356
- });
357
-
358
- return diff;
359
- }
360
- // 添加字段
361
- function addColumn() {
362
- formData.value.columns.push({
363
- name: '',
364
- type: '',
365
- length: '',
366
- precision: null,
367
- scale: null,
368
- nullable: false,
369
- defaultValue: '',
370
- isPrimary: false,
371
- isAutoIncrement: false,
372
- comment: ''
373
- });
374
- }
375
-
376
- // 删除字段
377
- function removeColumn(index: number) {
378
- if (formData.value.columns.length > 1) {
379
- formData.value.columns.splice(index, 1);
380
- }
381
- }
382
-
383
- // 主键变更处理
384
- function onPrimaryKeyChange(column: any, index: number) {
385
- if (column.isPrimary) {
386
- // 取消其他字段的主键标记
387
- formData.value.columns.forEach((col, idx) => {
388
- if (idx !== index) {
389
- col.isPrimary = false;
390
- }
391
- });
392
- }
393
- // 如果取消主键,也取消自增
394
- if (!column.isPrimary) {
395
- column.isAutoIncrement = false;
396
- }
397
- }
398
-
399
- // 生成SQL语句
400
- function generateSQL(): string {
401
- if (!formData.value.tableName) return '请输入表名';
402
-
403
- if (props.mode === 'create') {
404
- // 获取当前数据库的标识符引用方式
405
- const quoteIdentifier = (name: string) => {
406
- if (!props.connection) return `"${name}"`;
407
- switch (props.connection.type.toLowerCase()) {
408
- case 'mysql': return `\`${name}\``;
409
- case 'postgres': return `"${name}"`;
410
- case 'sqlite': return `"${name}"`;
411
- case 'oracle': return `"${name.toUpperCase()}"`;
412
- case 'mssql': return `[${name}]`;
413
- default: return `"${name}"`;
414
- }
415
- };
416
-
417
- // 创建表SQL
418
- const columnsSQL = formData.value.columns.map(col => {
419
- if (!col.name || !col.type) return '';
420
-
421
- let sql = `${quoteIdentifier(col.name)} ${col.type}`;
422
-
423
- // 处理长度和精度参数(仅当类型中不包含括号时)
424
- if (!col.type.includes('(') && col.length && (needsLength(col) || col.type.includes('CHAR'))) {
425
- sql += `(${col.length})`;
426
- } else if (!col.type.includes('(') && col.precision) {
427
- if (col.scale) {
428
- sql += `(${col.precision},${col.scale})`;
429
- } else {
430
- sql += `(${col.precision})`;
431
- }
432
- }
433
-
434
- // SQLite 特殊处理:自增主键必须在列定义中包含 PRIMARY KEY
435
- const isSqliteAutoIncrementPrimary =
436
- props.connection?.type.toLowerCase() === 'sqlite' &&
437
- col.isAutoIncrement &&
438
- col.isPrimary;
439
-
440
- // 处理NULL约束(SQLite 自增主键不需要 NOT NULL)
441
- if (!col.nullable && !isSqliteAutoIncrementPrimary) {
442
- sql += ' NOT NULL';
443
- }
444
-
445
- // 处理默认值
446
- if (col.defaultValue) {
447
- sql += ` DEFAULT ${formatDefaultValue(col.defaultValue, col.type)}`;
448
- }
449
-
450
- // 处理自增(数据库特定语法)
451
- if (col.isAutoIncrement) {
452
- switch (props.connection?.type.toLowerCase()) {
453
- case 'mysql':
454
- sql += ' AUTO_INCREMENT';
455
- break;
456
- case 'postgres':
457
- // PostgreSQL 使用 SERIAL 类型或 GENERATED AS IDENTITY
458
- if (col.type.toLowerCase().includes('serial') || col.type.toLowerCase().includes('bigserial')) {
459
- // 已包含自增
460
- } else {
461
- sql += ' GENERATED ALWAYS AS IDENTITY';
462
- }
463
- break;
464
- case 'sqlite':
465
- // SQLite 中 AUTOINCREMENT 只能用于 INTEGER 类型
466
- // 如果类型是 INT,需要改为 INTEGER
467
- if (col.type.toUpperCase() === 'INT') {
468
- sql = sql.replace(/\bINT\b/, 'INTEGER');
469
- }
470
- // SQLite 自增主键必须在列定义中包含 PRIMARY KEY
471
- if (col.isPrimary) {
472
- sql += ' PRIMARY KEY AUTOINCREMENT';
473
- } else {
474
- sql += ' AUTOINCREMENT';
475
- }
476
- break;
477
- case 'oracle':
478
- // Oracle 使用序列和触发器,这里简化处理
479
- break;
480
- case 'mssql':
481
- sql += ' IDENTITY(1,1)';
482
- break;
483
- }
484
- }
485
-
486
- // 处理注释(数据库特定语法)
487
- if (col.comment) {
488
- switch (props.connection?.type.toLowerCase()) {
489
- case 'mysql':
490
- sql += ` COMMENT '${col.comment}'`;
491
- break;
492
- case 'postgres':
493
- // PostgreSQL 注释需要单独的 COMMENT 语句
494
- break;
495
- case 'sqlite':
496
- // SQLite 不支持列注释
497
- break;
498
- case 'oracle':
499
- sql += ` COMMENT '${col.comment}'`;
500
- break;
501
- case 'mssql':
502
- // SQL Server 使用扩展属性
503
- break;
504
- }
505
- }
506
-
507
- return sql;
508
- }).filter(sql => sql).join(',\n ');
509
-
510
- // 处理主键
511
- const primaryKeys = formData.value.columns
512
- .filter(col => col.isPrimary)
513
- .map(col => quoteIdentifier(col.name));
514
-
515
- let sql = `CREATE TABLE ${quoteIdentifier(formData.value.tableName)} (\n ${columnsSQL}`;
516
-
517
- // SQLite 自增主键已经在列定义中包含 PRIMARY KEY,不需要再添加
518
- const hasSqliteAutoIncrementPrimary = props.connection?.type.toLowerCase() === 'sqlite' &&
519
- formData.value.columns.some(col => col.isAutoIncrement && col.isPrimary);
520
-
521
- if (primaryKeys.length > 0 && !hasSqliteAutoIncrementPrimary) {
522
- sql += `,\n PRIMARY KEY (${primaryKeys.join(', ')})`;
523
- }
524
-
525
- sql += '\n)';
526
-
527
- // 处理表注释和引擎(数据库特定)
528
- switch (props.connection?.type.toLowerCase()) {
529
- case 'mysql':
530
- if (formData.value.tableComment) {
531
- sql += ` COMMENT='${formData.value.tableComment}'`;
532
- }
533
- sql += ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4';
534
- break;
535
- case 'postgres':
536
- // PostgreSQL 表注释需要单独的 COMMENT 语句
537
- break;
538
- case 'oracle':
539
- if (formData.value.tableComment) {
540
- sql += ` COMMENT '${formData.value.tableComment}'`;
541
- }
542
- break;
543
- case 'mssql':
544
- // SQL Server 使用扩展属性
545
- break;
546
- }
547
-
548
- // 生成额外的注释语句(如果需要)
549
- if (props.connection?.type.toLowerCase() === 'postgres') {
550
- const commentStatements = [];
551
- if (formData.value.tableComment) {
552
- commentStatements.push(`COMMENT ON TABLE ${quoteIdentifier(formData.value.tableName)} IS '${formData.value.tableComment}'`);
553
- }
554
- formData.value.columns.forEach(col => {
555
- if (col.comment) {
556
- commentStatements.push(`COMMENT ON COLUMN ${quoteIdentifier(formData.value.tableName)}.${quoteIdentifier(col.name)} IS '${col.comment}'`);
557
- }
558
- });
559
- if (commentStatements.length > 0) {
560
- sql += ';\n' + commentStatements.join(';\n');
561
- }
562
- }
563
-
564
- return sql;
565
- } else {
566
- // 修改表SQL
567
- const quoteIdentifier = (name: string) => {
568
- if (!props.connection) return `"${name}"`;
569
- switch (props.connection.type.toLowerCase()) {
570
- case 'mysql': return `\`${name}\``;
571
- case 'postgres': return `"${name}"`;
572
- case 'sqlite': return `"${name}"`;
573
- case 'oracle': return `"${name.toUpperCase()}"`;
574
- case 'mssql': return `[${name}]`;
575
- default: return `"${name}"`;
576
- }
577
- };
578
-
579
- const sqlStatements: string[] = [];
580
- const tableName = formData.value.tableName;
581
-
582
- // 修改表注释
583
- if (formData.value.tableComment) {
584
- switch (props.connection?.type.toLowerCase()) {
585
- case 'mysql':
586
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} COMMENT='${formData.value.tableComment}'`);
587
- break;
588
- case 'postgres':
589
- sqlStatements.push(`COMMENT ON TABLE ${quoteIdentifier(tableName)} IS '${formData.value.tableComment}'`);
590
- break;
591
- case 'oracle':
592
- sqlStatements.push(`COMMENT ON TABLE ${quoteIdentifier(tableName)} IS '${formData.value.tableComment}'`);
593
- break;
594
- case 'mssql':
595
- sqlStatements.push(`EXEC sp_addextendedproperty 'MS_Description', '${formData.value.tableComment}', 'SCHEMA', 'dbo', 'TABLE', '${tableName}'`);
596
- break;
597
- }
598
- }
599
-
600
- // 生成列修改语句
601
- formData.value.columns.forEach(col => {
602
- if (!col.name || !col.type) return;
603
-
604
- let columnSQL = `${quoteIdentifier(col.name)} ${col.type}`;
605
-
606
- // 处理长度和精度参数(仅当类型中不包含括号时)
607
- if (!col.type.includes('(') && col.length && (needsLength(col) || col.type.includes('CHAR'))) {
608
- columnSQL += `(${col.length})`;
609
- } else if (!col.type.includes('(') && col.precision) {
610
- if (col.scale) {
611
- columnSQL += `(${col.precision},${col.scale})`;
612
- } else {
613
- columnSQL += `(${col.precision})`;
614
- }
615
- }
616
-
617
- // 处理NULL约束
618
- if (!col.nullable) {
619
- columnSQL += ' NOT NULL';
620
- } else {
621
- columnSQL += ' NULL';
622
- }
623
-
624
- // 处理默认值
625
- if (col.defaultValue) {
626
- columnSQL += ` DEFAULT ${formatDefaultValue(col.defaultValue, col.type)}`;
627
- }
628
-
629
- // 处理注释
630
- let commentStatement = '';
631
- if (col.comment) {
632
- switch (props.connection?.type.toLowerCase()) {
633
- case 'mysql':
634
- columnSQL += ` COMMENT '${col.comment}'`;
635
- break;
636
- case 'postgres':
637
- commentStatement = `COMMENT ON COLUMN ${quoteIdentifier(tableName)}.${quoteIdentifier(col.name)} IS '${col.comment}'`;
638
- break;
639
- case 'oracle':
640
- columnSQL += ` COMMENT '${col.comment}'`;
641
- break;
642
- case 'mssql':
643
- commentStatement = `EXEC sp_addextendedproperty 'MS_Description', '${col.comment}', 'SCHEMA', 'dbo', 'TABLE', '${tableName}', 'COLUMN', '${col.name}'`;
644
- break;
645
- }
646
- }
647
-
648
- // 根据数据库类型生成 ALTER COLUMN 或 MODIFY COLUMN 语句
649
- switch (props.connection?.type.toLowerCase()) {
650
- case 'mysql':
651
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} MODIFY COLUMN ${columnSQL}`);
652
- break;
653
- case 'postgres':
654
- case 'mssql':
655
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} ALTER COLUMN ${columnSQL}`);
656
- break;
657
- case 'oracle':
658
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} MODIFY ${columnSQL}`);
659
- break;
660
- case 'sqlite':
661
- // SQLite 不支持直接修改列,需要重建表
662
- sqlStatements.push(`-- SQLite 不支持直接修改列,需要重建表`);
663
- break;
664
- }
665
-
666
- // 添加注释语句
667
- if (commentStatement) {
668
- sqlStatements.push(commentStatement);
669
- }
670
- });
671
-
672
- // 处理主键
673
- const primaryKeys = formData.value.columns
674
- .filter(col => col.isPrimary)
675
- .map(col => quoteIdentifier(col.name));
676
-
677
- if (primaryKeys.length > 0) {
678
- // 先删除旧主键(如果存在)
679
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} DROP PRIMARY KEY`);
680
- // 添加新主键
681
- sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} ADD PRIMARY KEY (${primaryKeys.join(', ')})`);
682
- }
683
-
684
- return sqlStatements.join(';\n') + ';';
685
- }
686
- }
687
-
688
- // 格式化默认值
689
- function formatDefaultValue(value: any, type: string): string {
690
- if (value === null || value === undefined || value === '') {
691
- return 'NULL';
692
- }
693
-
694
- const lowerValue = String(value).toLowerCase();
695
-
696
- // 处理特殊关键字(不加引号)
697
- const specialKeywords = ['current_timestamp', 'now()', 'current_date', 'current_time', 'localtimestamp', 'localtime'];
698
- if (specialKeywords.includes(lowerValue)) {
699
- return value;
700
- }
701
-
702
- const lowerType = type.toLowerCase();
703
-
704
- // 数值类型不加引号
705
- if (isNumericType(lowerType) && !isNaN(value)) {
706
- return String(value);
707
- }
708
-
709
- // 布尔类型
710
- if (isBooleanType(lowerType)) {
711
- return value ? 'TRUE' : 'FALSE';
712
- }
713
-
714
- // 字符串类型加引号
715
- return `'${String(value).replace(/'/g, "''")}'`;
716
- }
717
-
718
- // 提交表单
719
- async function submit() {
720
- try {
721
- if (!formData.value.tableName) {
722
- await modal.warning('请输入表名');
723
- return;
724
- }
725
-
726
- if (formData.value.columns.some(col => !col.name || !col.type)) {
727
- await modal.warning('请完善所有字段信息');
728
- return;
729
- }
730
-
731
- if (!props.connection?.id) {
732
- await modal.warning('请先选择数据库连接');
733
- return;
734
- }
735
-
736
- let result;
737
-
738
- if (props.mode === 'create') {
739
- // 创建表:使用 SQL 语句
740
- const sql = generateSQL();
741
- result = await databaseService.executeQuery(
742
- props.connection.id,
743
- sql,
744
- props.database
745
- );
746
- } else {
747
- // 修改表:使用差异化对比
748
- const tableDiff = calculateTableDiff();
749
-
750
- // 检查是否有实际修改
751
- if (tableDiff.addedColumns.length === 0 &&
752
- tableDiff.modifiedColumns.length === 0 &&
753
- tableDiff.deletedColumns.length === 0 &&
754
- !tableDiff.tableCommentChanged) {
755
- await modal.info('没有检测到任何修改');
756
- return;
757
- }
758
-
759
- // 调用修改表接口(需要后端实现)
760
- result = await databaseService.alterTable(
761
- props.connection.id,
762
- props.database,
763
- tableDiff
764
- );
765
- }
766
-
767
- emit('submit', {
768
- success: result.ret === 0,
769
- message: result.ret === 0 ? '操作成功' : '操作失败',
770
- data: result.data,
771
- mode: props.mode
772
- });
773
-
774
- close();
775
- } catch (error) {
776
- console.error('提交失败:', error);
777
- emit('submit', {
778
- success: false,
779
- message: '操作失败',
780
- mode: props.mode
781
- });
782
- }
783
- }
784
-
785
- // 关闭弹窗
786
- function close() {
787
- emit('close');
788
- }
789
-
790
- // 监听props变化
791
- watch(() => props.visible, (newVal) => {
792
- if (newVal) {
793
- initFormData();
794
- }
795
- });
796
-
797
- // 获取类别标签
798
- function getCategoryLabel(category: string): string {
799
- const labels: Record<string, string> = {
800
- [ColumnCategory.NUMERIC]: '数值类型',
801
- [ColumnCategory.STRING]: '字符串类型',
802
- [ColumnCategory.TEXT]: '文本类型',
803
- [ColumnCategory.DATE_TIME]: '日期时间类型',
804
- [ColumnCategory.BOOLEAN]: '布尔类型',
805
- [ColumnCategory.BINARY]: '二进制类型',
806
- [ColumnCategory.JSON]: 'JSON类型',
807
- [ColumnCategory.ARRAY]: '数组类型',
808
- [ColumnCategory.SPATIAL]: '空间类型',
809
- [ColumnCategory.OTHER]: '其他类型'
810
- };
811
- return labels[category] || category;
812
- }
813
-
814
- // 获取选中的类型信息
815
- function getSelectedType(column: any) {
816
- if (!column.type) return null;
817
-
818
- // 提取类型名称(去掉括号和长度信息)
819
- const typeName = column.type.match(/^[a-zA-Z]+/)?.[0] || column.type;
820
-
821
- // 大小写不敏感匹配
822
- return columnTypes.value.find(t => t.name.toLowerCase() === typeName.toLowerCase());
823
- }
824
-
825
- // 检查类型是否需要长度参数
826
- function needsLength(column: any): boolean {
827
- const typeInfo = getSelectedType(column);
828
- return typeInfo?.requiresLength || false;
829
- }
830
-
831
- // 检查类型是否需要精度参数
832
- function needsPrecision(column: any): boolean {
833
- const typeInfo = getSelectedType(column);
834
- return typeInfo?.requiresPrecision || false;
835
- }
836
-
837
- // 检查类型是否需要小数位数参数
838
- function needsScale(column: any): boolean {
839
- const typeInfo = getSelectedType(column);
840
- return typeInfo?.requiresScale || false;
841
- }
842
-
843
- // 检查类型是否支持自增
844
- function supportsAutoIncrement(column: any): boolean {
845
- const typeInfo = getSelectedType(column);
846
- return typeInfo?.supportsAutoIncrement || false;
847
- }
848
-
849
- // 初始化
850
- onMounted(() => {
851
- if (props.visible) {
852
- initFormData();
853
- }
854
- });
855
- </script>
856
-
857
- <style scoped>
858
- .section-title {
859
- color: #495057;
860
- font-weight: 600;
861
- border-bottom: 2px solid #e9ecef;
862
- padding-bottom: 0.5rem;
863
- margin-bottom: 1rem;
864
- }
865
-
866
- .sql-preview {
867
- font-family: 'Courier New', monospace;
868
- font-size: 0.875rem;
869
- max-height: 200px;
870
- overflow-y: auto;
871
- border: 1px solid #dee2e6;
872
- }
873
-
874
- .table th {
875
- font-size: 0.875rem;
876
- font-weight: 600;
877
- }
878
- .table td {
879
- padding: 0!important;
880
- }
881
- .table .form-control, .table .form-select {
882
- border: none;
883
- border-radius: 0;
884
- box-shadow: none;
885
- }
886
- .form-control-sm, .form-select-sm {
887
- font-size: 0.875rem;
888
- }
889
-
890
- .modal-content {
891
- max-height: 90vh;
892
- overflow-y: auto;
893
- }
894
-
895
- .modal-body {
896
- max-height: calc(90vh - 120px);
897
- overflow-y: auto;
898
- }
899
-
900
- .input-group-sm .form-control {
901
- border-radius: 0;
902
- }
903
-
904
- .input-group-sm .form-control:first-child {
905
- border-top-left-radius: 0.25rem;
906
- border-bottom-left-radius: 0.25rem;
907
- }
908
-
909
- .input-group-sm .form-control:last-child {
910
- border-top-right-radius: 0.25rem;
911
- border-bottom-right-radius: 0.25rem;
912
- }
913
-
914
- .text-muted {
915
- color: #6c757d !important;
916
- }
1
+ <template>
2
+ <div v-if="visible" class="modal fade show" style="display: block; background-color: rgba(0,0,0,0.5);" @click="close">
3
+ <div class="modal-dialog modal-lg" style="width: 80%; max-width: none;" @click.stop>
4
+ <div class="modal-content">
5
+ <div class="modal-header">
6
+ <h5 class="modal-title">
7
+ <i class="bi bi-pencil-square me-2"></i>
8
+ {{ mode === 'create' ? '创建新表' : '修改表结构' }}
9
+ </h5>
10
+ <button type="button" class="btn-close" @click="close"></button>
11
+ </div>
12
+
13
+ <div class="modal-body">
14
+ <!-- 表基本信息 -->
15
+ <div class="table-info-section mb-4">
16
+ <h6 class="section-title">表信息</h6>
17
+ <div class="row">
18
+ <div class="col-md-6">
19
+ <div class="mb-3">
20
+ <label class="form-label">表名</label>
21
+ <input
22
+ v-model="formData.tableName"
23
+ type="text"
24
+ class="form-control"
25
+ :disabled="mode === 'edit'"
26
+ placeholder="请输入表名"
27
+ >
28
+ </div>
29
+ </div>
30
+ <div class="col-md-6">
31
+ <div class="mb-3">
32
+ <label class="form-label">表注释</label>
33
+ <input
34
+ v-model="formData.tableComment"
35
+ type="text"
36
+ class="form-control"
37
+ placeholder="请输入表注释"
38
+ >
39
+ </div>
40
+ </div>
41
+ </div>
42
+ </div>
43
+
44
+ <!-- 字段管理 -->
45
+ <div class="columns-section">
46
+ <div class="section-header d-flex justify-content-between align-items-center mb-3">
47
+ <h6 class="section-title mb-0">字段管理</h6>
48
+ <button class="btn btn-success btn-sm" @click="addColumn">
49
+ <i class="bi bi-plus-lg"></i> 添加字段
50
+ </button>
51
+ </div>
52
+
53
+ <div class="table-responsive">
54
+ <table class="table table-sm table-bordered">
55
+ <thead class="table-light">
56
+ <tr>
57
+ <th width="120">字段名</th>
58
+ <th width="140">数据类型</th>
59
+ <th width="120">长度/精度</th>
60
+ <th width="80">可空</th>
61
+ <th width="100">默认值</th>
62
+ <th width="80">主键</th>
63
+ <th width="80">自增</th>
64
+ <th>注释</th>
65
+ <th width="100">操作</th>
66
+ </tr>
67
+ </thead>
68
+ <tbody>
69
+ <tr v-for="(column, index) in formData.columns" :key="index">
70
+ <td>
71
+ <input
72
+ v-model="column.name"
73
+ type="text"
74
+ class="form-control form-control-sm"
75
+ placeholder="字段名"
76
+ >
77
+ </td>
78
+ <td>
79
+ <select v-model="column.type" class="form-select form-select-sm">
80
+ <option value="">选择类型</option>
81
+ <optgroup v-for="(types, category) in columnTypesByCategory" :key="category" :label="getCategoryLabel(category)">
82
+ <option v-for="type in types" :key="type.name" :value="type.name">
83
+ {{ type.label }}
84
+ </option>
85
+ </optgroup>
86
+ </select>
87
+ </td>
88
+ <td>
89
+ <div class="input-group input-group-sm" v-if="needsLength(column) || needsPrecision(column)">
90
+ <input
91
+ v-if="needsLength(column)"
92
+ v-model="column.length"
93
+ type="number"
94
+ class="form-control form-control-sm"
95
+ placeholder="长度"
96
+ >
97
+ <input
98
+ v-if="needsPrecision(column)"
99
+ v-model="column.precision"
100
+ type="number"
101
+ class="form-control form-control-sm"
102
+ placeholder="精度"
103
+ >
104
+ <input
105
+ v-if="needsScale(column)"
106
+ v-model="column.scale"
107
+ type="number"
108
+ class="form-control form-control-sm"
109
+ placeholder="小数"
110
+ >
111
+ </div>
112
+ <span v-else class="text-muted">-</span>
113
+ </td>
114
+ <td>
115
+ <div class="form-check">
116
+ <input
117
+ v-model="column.nullable"
118
+ type="checkbox"
119
+ class="form-check-input"
120
+ >
121
+ </div>
122
+ </td>
123
+ <td>
124
+ <input
125
+ v-model="column.defaultValue"
126
+ type="text"
127
+ class="form-control form-control-sm"
128
+ placeholder="默认值"
129
+ >
130
+ </td>
131
+ <td>
132
+ <div class="form-check">
133
+ <input
134
+ v-model="column.isPrimary"
135
+ type="checkbox"
136
+ class="form-check-input"
137
+ @change="onPrimaryKeyChange(column, index)"
138
+ >
139
+ </div>
140
+ </td>
141
+ <td>
142
+ <div class="form-check">
143
+ <input
144
+ v-model="column.isAutoIncrement"
145
+ type="checkbox"
146
+ class="form-check-input"
147
+ :disabled="!column.isPrimary || !supportsAutoIncrement(column)"
148
+ >
149
+ </div>
150
+ </td>
151
+ <td>
152
+ <input
153
+ v-model="column.comment"
154
+ type="text"
155
+ class="form-control form-control-sm"
156
+ placeholder="字段注释"
157
+ >
158
+ </td>
159
+ <td>
160
+ <button
161
+ class="btn btn-outline-danger btn-sm"
162
+ @click="removeColumn(index)"
163
+ :disabled="formData.columns.length <= 1"
164
+ >
165
+ <i class="bi bi-trash"></i>
166
+ </button>
167
+ </td>
168
+ </tr>
169
+ </tbody>
170
+ </table>
171
+ </div>
172
+ </div>
173
+
174
+ <!-- SQL预览 -->
175
+ <div class="sql-preview-section mt-4">
176
+ <h6 class="section-title">SQL预览</h6>
177
+ <pre class="sql-preview bg-light p-3 rounded">{{ generateSQL() }}</pre>
178
+ </div>
179
+ </div>
180
+
181
+ <div class="modal-footer">
182
+ <button type="button" class="btn btn-secondary" @click="close">取消</button>
183
+ <button type="button" class="btn btn-primary" @click="submit">保存</button>
184
+ </div>
185
+ </div>
186
+ </div>
187
+ </div>
188
+ </template>
189
+
190
+ <script lang="ts" setup>
191
+ import { ref, computed, watch, onMounted } from 'vue';
192
+ import type { ConnectionEntity, TableEntity } from '@/typings/database';
193
+ import { DatabaseService } from '@/service/database';
194
+ import { modal } from '@/utils/modal';
195
+ import { getColumnTypesByName, ColumnCategory } from '@/typings/database-types';
196
+ import { isNumericType, isBooleanType } from '@/utils/database-types';
197
+
198
+ // Props
199
+ const props = defineProps<{
200
+ visible: boolean;
201
+ connection: ConnectionEntity | null;
202
+ database: string;
203
+ table?: TableEntity | null;
204
+ columns?: any[];
205
+ mode: 'create' | 'edit';
206
+ }>();
207
+
208
+ // Emits
209
+ const emit = defineEmits<{
210
+ close: [];
211
+ submit: [result: any];
212
+ }>();
213
+
214
+ const databaseService = new DatabaseService();
215
+
216
+ // 获取当前数据库支持的列类型
217
+ const columnTypes = computed(() => {
218
+ if (!props.connection) return [];
219
+ return getColumnTypesByName(props.connection.type);
220
+ });
221
+
222
+ // 按类别分组的列类型
223
+ const columnTypesByCategory = computed(() => {
224
+ const categories: Record<string, any[]> = {};
225
+ columnTypes.value.forEach(type => {
226
+ if (!categories[type.category]) {
227
+ categories[type.category] = [];
228
+ }
229
+ categories[type.category].push(type);
230
+ });
231
+ return categories;
232
+ });
233
+
234
+ // 表单数据
235
+ const formData = ref({
236
+ tableName: '',
237
+ tableComment: '',
238
+ columns: [{
239
+ name: '',
240
+ type: '',
241
+ length: '',
242
+ nullable: false,
243
+ defaultValue: '',
244
+ isPrimary: false,
245
+ isAutoIncrement: false,
246
+ comment: ''
247
+ }]
248
+ });
249
+
250
+ // 原始表结构(用于对比差异)
251
+ const originalTableData = ref({
252
+ tableName: '',
253
+ tableComment: '',
254
+ columns: []
255
+ });
256
+
257
+ // 初始化表单数据
258
+ function initFormData() {
259
+ if (props.mode === 'edit' && props.table) {
260
+ const columns = props.columns || props.table.columns || [];
261
+ const tableData = {
262
+ tableName: props.table.name || '',
263
+ tableComment: props.table.comment || '',
264
+ columns: columns.map(col => ({
265
+ name: col.name || '',
266
+ type: col.type || '',
267
+ length: col.length || '',
268
+ precision: col.precision || null,
269
+ scale: col.scale || null,
270
+ nullable: col.nullable || false,
271
+ defaultValue: col.defaultValue || '',
272
+ isPrimary: col.isPrimary || false,
273
+ isAutoIncrement: col.isAutoIncrement || false,
274
+ comment: col.comment || ''
275
+ })) || []
276
+ };
277
+ formData.value = { ...tableData };
278
+ originalTableData.value = JSON.parse(JSON.stringify(tableData));
279
+ } else {
280
+ formData.value = {
281
+ tableName: '',
282
+ tableComment: '',
283
+ columns: [{
284
+ name: '',
285
+ type: '',
286
+ length: '',
287
+ precision: null,
288
+ scale: null,
289
+ nullable: false,
290
+ defaultValue: '',
291
+ isPrimary: false,
292
+ isAutoIncrement: false,
293
+ comment: ''
294
+ }]
295
+ };
296
+ originalTableData.value = {
297
+ tableName: '',
298
+ tableComment: '',
299
+ columns: []
300
+ };
301
+ }
302
+ }
303
+
304
+ // 计算表结构差异
305
+ function calculateTableDiff() {
306
+ const diff = {
307
+ tableName: formData.value.tableName,
308
+ tableCommentChanged: formData.value.tableComment !== originalTableData.value.tableComment,
309
+ tableComment: formData.value.tableComment,
310
+ addedColumns: [],
311
+ modifiedColumns: [],
312
+ deletedColumns: []
313
+ };
314
+
315
+ // 创建原始列的映射(按列名)
316
+ const originalColumnsMap = new Map();
317
+ originalTableData.value.columns.forEach(col => {
318
+ originalColumnsMap.set(col.name, col);
319
+ });
320
+
321
+ // 检查新增和修改的列
322
+ formData.value.columns.forEach(newCol => {
323
+ const originalCol = originalColumnsMap.get(newCol.name);
324
+
325
+ if (!originalCol) {
326
+ // 新增列
327
+ diff.addedColumns.push(newCol);
328
+ } else {
329
+ // 检查列是否被修改
330
+ const isModified =
331
+ newCol.type !== originalCol.type ||
332
+ newCol.length !== originalCol.length ||
333
+ newCol.precision !== originalCol.precision ||
334
+ newCol.scale !== originalCol.scale ||
335
+ newCol.nullable !== originalCol.nullable ||
336
+ newCol.defaultValue !== originalCol.defaultValue ||
337
+ newCol.isPrimary !== originalCol.isPrimary ||
338
+ newCol.isAutoIncrement !== originalCol.isAutoIncrement ||
339
+ newCol.comment !== originalCol.comment;
340
+
341
+ if (isModified) {
342
+ diff.modifiedColumns.push({
343
+ oldColumn: originalCol,
344
+ newColumn: newCol
345
+ });
346
+ }
347
+ }
348
+ });
349
+
350
+ // 检查删除的列
351
+ originalTableData.value.columns.forEach(originalCol => {
352
+ const existsInNew = formData.value.columns.some(newCol => newCol.name === originalCol.name);
353
+ if (!existsInNew) {
354
+ diff.deletedColumns.push(originalCol);
355
+ }
356
+ });
357
+
358
+ return diff;
359
+ }
360
+ // 添加字段
361
+ function addColumn() {
362
+ formData.value.columns.push({
363
+ name: '',
364
+ type: '',
365
+ length: '',
366
+ precision: null,
367
+ scale: null,
368
+ nullable: false,
369
+ defaultValue: '',
370
+ isPrimary: false,
371
+ isAutoIncrement: false,
372
+ comment: ''
373
+ });
374
+ }
375
+
376
+ // 删除字段
377
+ function removeColumn(index: number) {
378
+ if (formData.value.columns.length > 1) {
379
+ formData.value.columns.splice(index, 1);
380
+ }
381
+ }
382
+
383
+ // 主键变更处理
384
+ function onPrimaryKeyChange(column: any, index: number) {
385
+ if (column.isPrimary) {
386
+ // 取消其他字段的主键标记
387
+ formData.value.columns.forEach((col, idx) => {
388
+ if (idx !== index) {
389
+ col.isPrimary = false;
390
+ }
391
+ });
392
+ }
393
+ // 如果取消主键,也取消自增
394
+ if (!column.isPrimary) {
395
+ column.isAutoIncrement = false;
396
+ }
397
+ }
398
+
399
+ // 生成SQL语句
400
+ function generateSQL(): string {
401
+ if (!formData.value.tableName) return '请输入表名';
402
+
403
+ if (props.mode === 'create') {
404
+ // 获取当前数据库的标识符引用方式
405
+ const quoteIdentifier = (name: string) => {
406
+ if (!props.connection) return `"${name}"`;
407
+ switch (props.connection.type.toLowerCase()) {
408
+ case 'mysql': return `\`${name}\``;
409
+ case 'postgres': return `"${name}"`;
410
+ case 'sqlite': return `"${name}"`;
411
+ case 'oracle': return `"${name.toUpperCase()}"`;
412
+ case 'mssql': return `[${name}]`;
413
+ default: return `"${name}"`;
414
+ }
415
+ };
416
+
417
+ // 创建表SQL
418
+ const columnsSQL = formData.value.columns.map(col => {
419
+ if (!col.name || !col.type) return '';
420
+
421
+ let sql = `${quoteIdentifier(col.name)} ${col.type}`;
422
+
423
+ // 处理长度和精度参数(仅当类型中不包含括号时)
424
+ if (!col.type.includes('(') && col.length && (needsLength(col) || col.type.includes('CHAR'))) {
425
+ sql += `(${col.length})`;
426
+ } else if (!col.type.includes('(') && col.precision) {
427
+ if (col.scale) {
428
+ sql += `(${col.precision},${col.scale})`;
429
+ } else {
430
+ sql += `(${col.precision})`;
431
+ }
432
+ }
433
+
434
+ // SQLite 特殊处理:自增主键必须在列定义中包含 PRIMARY KEY
435
+ const isSqliteAutoIncrementPrimary =
436
+ props.connection?.type.toLowerCase() === 'sqlite' &&
437
+ col.isAutoIncrement &&
438
+ col.isPrimary;
439
+
440
+ // 处理NULL约束(SQLite 自增主键不需要 NOT NULL)
441
+ if (!col.nullable && !isSqliteAutoIncrementPrimary) {
442
+ sql += ' NOT NULL';
443
+ }
444
+
445
+ // 处理默认值
446
+ if (col.defaultValue) {
447
+ sql += ` DEFAULT ${formatDefaultValue(col.defaultValue, col.type)}`;
448
+ }
449
+
450
+ // 处理自增(数据库特定语法)
451
+ if (col.isAutoIncrement) {
452
+ switch (props.connection?.type.toLowerCase()) {
453
+ case 'mysql':
454
+ sql += ' AUTO_INCREMENT';
455
+ break;
456
+ case 'postgres':
457
+ // PostgreSQL 使用 SERIAL 类型或 GENERATED AS IDENTITY
458
+ if (col.type.toLowerCase().includes('serial') || col.type.toLowerCase().includes('bigserial')) {
459
+ // 已包含自增
460
+ } else {
461
+ sql += ' GENERATED ALWAYS AS IDENTITY';
462
+ }
463
+ break;
464
+ case 'sqlite':
465
+ // SQLite 中 AUTOINCREMENT 只能用于 INTEGER 类型
466
+ // 如果类型是 INT,需要改为 INTEGER
467
+ if (col.type.toUpperCase() === 'INT') {
468
+ sql = sql.replace(/\bINT\b/, 'INTEGER');
469
+ }
470
+ // SQLite 自增主键必须在列定义中包含 PRIMARY KEY
471
+ if (col.isPrimary) {
472
+ sql += ' PRIMARY KEY AUTOINCREMENT';
473
+ } else {
474
+ sql += ' AUTOINCREMENT';
475
+ }
476
+ break;
477
+ case 'oracle':
478
+ // Oracle 使用序列和触发器,这里简化处理
479
+ break;
480
+ case 'mssql':
481
+ sql += ' IDENTITY(1,1)';
482
+ break;
483
+ }
484
+ }
485
+
486
+ // 处理注释(数据库特定语法)
487
+ if (col.comment) {
488
+ switch (props.connection?.type.toLowerCase()) {
489
+ case 'mysql':
490
+ sql += ` COMMENT '${col.comment}'`;
491
+ break;
492
+ case 'postgres':
493
+ // PostgreSQL 注释需要单独的 COMMENT 语句
494
+ break;
495
+ case 'sqlite':
496
+ // SQLite 不支持列注释
497
+ break;
498
+ case 'oracle':
499
+ sql += ` COMMENT '${col.comment}'`;
500
+ break;
501
+ case 'mssql':
502
+ // SQL Server 使用扩展属性
503
+ break;
504
+ }
505
+ }
506
+
507
+ return sql;
508
+ }).filter(sql => sql).join(',\n ');
509
+
510
+ // 处理主键
511
+ const primaryKeys = formData.value.columns
512
+ .filter(col => col.isPrimary)
513
+ .map(col => quoteIdentifier(col.name));
514
+
515
+ let sql = `CREATE TABLE ${quoteIdentifier(formData.value.tableName)} (\n ${columnsSQL}`;
516
+
517
+ // SQLite 自增主键已经在列定义中包含 PRIMARY KEY,不需要再添加
518
+ const hasSqliteAutoIncrementPrimary = props.connection?.type.toLowerCase() === 'sqlite' &&
519
+ formData.value.columns.some(col => col.isAutoIncrement && col.isPrimary);
520
+
521
+ if (primaryKeys.length > 0 && !hasSqliteAutoIncrementPrimary) {
522
+ sql += `,\n PRIMARY KEY (${primaryKeys.join(', ')})`;
523
+ }
524
+
525
+ sql += '\n)';
526
+
527
+ // 处理表注释和引擎(数据库特定)
528
+ switch (props.connection?.type.toLowerCase()) {
529
+ case 'mysql':
530
+ if (formData.value.tableComment) {
531
+ sql += ` COMMENT='${formData.value.tableComment}'`;
532
+ }
533
+ sql += ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4';
534
+ break;
535
+ case 'postgres':
536
+ // PostgreSQL 表注释需要单独的 COMMENT 语句
537
+ break;
538
+ case 'oracle':
539
+ if (formData.value.tableComment) {
540
+ sql += ` COMMENT '${formData.value.tableComment}'`;
541
+ }
542
+ break;
543
+ case 'mssql':
544
+ // SQL Server 使用扩展属性
545
+ break;
546
+ }
547
+
548
+ // 生成额外的注释语句(如果需要)
549
+ if (props.connection?.type.toLowerCase() === 'postgres') {
550
+ const commentStatements = [];
551
+ if (formData.value.tableComment) {
552
+ commentStatements.push(`COMMENT ON TABLE ${quoteIdentifier(formData.value.tableName)} IS '${formData.value.tableComment}'`);
553
+ }
554
+ formData.value.columns.forEach(col => {
555
+ if (col.comment) {
556
+ commentStatements.push(`COMMENT ON COLUMN ${quoteIdentifier(formData.value.tableName)}.${quoteIdentifier(col.name)} IS '${col.comment}'`);
557
+ }
558
+ });
559
+ if (commentStatements.length > 0) {
560
+ sql += ';\n' + commentStatements.join(';\n');
561
+ }
562
+ }
563
+
564
+ return sql;
565
+ } else {
566
+ // 修改表SQL
567
+ const quoteIdentifier = (name: string) => {
568
+ if (!props.connection) return `"${name}"`;
569
+ switch (props.connection.type.toLowerCase()) {
570
+ case 'mysql': return `\`${name}\``;
571
+ case 'postgres': return `"${name}"`;
572
+ case 'sqlite': return `"${name}"`;
573
+ case 'oracle': return `"${name.toUpperCase()}"`;
574
+ case 'mssql': return `[${name}]`;
575
+ default: return `"${name}"`;
576
+ }
577
+ };
578
+
579
+ const sqlStatements: string[] = [];
580
+ const tableName = formData.value.tableName;
581
+
582
+ // 修改表注释
583
+ if (formData.value.tableComment) {
584
+ switch (props.connection?.type.toLowerCase()) {
585
+ case 'mysql':
586
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} COMMENT='${formData.value.tableComment}'`);
587
+ break;
588
+ case 'postgres':
589
+ sqlStatements.push(`COMMENT ON TABLE ${quoteIdentifier(tableName)} IS '${formData.value.tableComment}'`);
590
+ break;
591
+ case 'oracle':
592
+ sqlStatements.push(`COMMENT ON TABLE ${quoteIdentifier(tableName)} IS '${formData.value.tableComment}'`);
593
+ break;
594
+ case 'mssql':
595
+ sqlStatements.push(`EXEC sp_addextendedproperty 'MS_Description', '${formData.value.tableComment}', 'SCHEMA', 'dbo', 'TABLE', '${tableName}'`);
596
+ break;
597
+ }
598
+ }
599
+
600
+ // 生成列修改语句
601
+ formData.value.columns.forEach(col => {
602
+ if (!col.name || !col.type) return;
603
+
604
+ let columnSQL = `${quoteIdentifier(col.name)} ${col.type}`;
605
+
606
+ // 处理长度和精度参数(仅当类型中不包含括号时)
607
+ if (!col.type.includes('(') && col.length && (needsLength(col) || col.type.includes('CHAR'))) {
608
+ columnSQL += `(${col.length})`;
609
+ } else if (!col.type.includes('(') && col.precision) {
610
+ if (col.scale) {
611
+ columnSQL += `(${col.precision},${col.scale})`;
612
+ } else {
613
+ columnSQL += `(${col.precision})`;
614
+ }
615
+ }
616
+
617
+ // 处理NULL约束
618
+ if (!col.nullable) {
619
+ columnSQL += ' NOT NULL';
620
+ } else {
621
+ columnSQL += ' NULL';
622
+ }
623
+
624
+ // 处理默认值
625
+ if (col.defaultValue) {
626
+ columnSQL += ` DEFAULT ${formatDefaultValue(col.defaultValue, col.type)}`;
627
+ }
628
+
629
+ // 处理注释
630
+ let commentStatement = '';
631
+ if (col.comment) {
632
+ switch (props.connection?.type.toLowerCase()) {
633
+ case 'mysql':
634
+ columnSQL += ` COMMENT '${col.comment}'`;
635
+ break;
636
+ case 'postgres':
637
+ commentStatement = `COMMENT ON COLUMN ${quoteIdentifier(tableName)}.${quoteIdentifier(col.name)} IS '${col.comment}'`;
638
+ break;
639
+ case 'oracle':
640
+ columnSQL += ` COMMENT '${col.comment}'`;
641
+ break;
642
+ case 'mssql':
643
+ commentStatement = `EXEC sp_addextendedproperty 'MS_Description', '${col.comment}', 'SCHEMA', 'dbo', 'TABLE', '${tableName}', 'COLUMN', '${col.name}'`;
644
+ break;
645
+ }
646
+ }
647
+
648
+ // 根据数据库类型生成 ALTER COLUMN 或 MODIFY COLUMN 语句
649
+ switch (props.connection?.type.toLowerCase()) {
650
+ case 'mysql':
651
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} MODIFY COLUMN ${columnSQL}`);
652
+ break;
653
+ case 'postgres':
654
+ case 'mssql':
655
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} ALTER COLUMN ${columnSQL}`);
656
+ break;
657
+ case 'oracle':
658
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} MODIFY ${columnSQL}`);
659
+ break;
660
+ case 'sqlite':
661
+ // SQLite 不支持直接修改列,需要重建表
662
+ sqlStatements.push(`-- SQLite 不支持直接修改列,需要重建表`);
663
+ break;
664
+ }
665
+
666
+ // 添加注释语句
667
+ if (commentStatement) {
668
+ sqlStatements.push(commentStatement);
669
+ }
670
+ });
671
+
672
+ // 处理主键
673
+ const primaryKeys = formData.value.columns
674
+ .filter(col => col.isPrimary)
675
+ .map(col => quoteIdentifier(col.name));
676
+
677
+ if (primaryKeys.length > 0) {
678
+ // 先删除旧主键(如果存在)
679
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} DROP PRIMARY KEY`);
680
+ // 添加新主键
681
+ sqlStatements.push(`ALTER TABLE ${quoteIdentifier(tableName)} ADD PRIMARY KEY (${primaryKeys.join(', ')})`);
682
+ }
683
+
684
+ return sqlStatements.join(';\n') + ';';
685
+ }
686
+ }
687
+
688
+ // 格式化默认值
689
+ function formatDefaultValue(value: any, type: string): string {
690
+ if (value === null || value === undefined || value === '') {
691
+ return 'NULL';
692
+ }
693
+
694
+ const lowerValue = String(value).toLowerCase();
695
+
696
+ // 处理特殊关键字(不加引号)
697
+ const specialKeywords = ['current_timestamp', 'now()', 'current_date', 'current_time', 'localtimestamp', 'localtime'];
698
+ if (specialKeywords.includes(lowerValue)) {
699
+ return value;
700
+ }
701
+
702
+ const lowerType = type.toLowerCase();
703
+
704
+ // 数值类型不加引号
705
+ if (isNumericType(lowerType) && !isNaN(value)) {
706
+ return String(value);
707
+ }
708
+
709
+ // 布尔类型
710
+ if (isBooleanType(lowerType)) {
711
+ return value ? 'TRUE' : 'FALSE';
712
+ }
713
+
714
+ // 字符串类型加引号
715
+ return `'${String(value).replace(/'/g, "''")}'`;
716
+ }
717
+
718
+ // 提交表单
719
+ async function submit() {
720
+ try {
721
+ if (!formData.value.tableName) {
722
+ await modal.warning('请输入表名');
723
+ return;
724
+ }
725
+
726
+ if (formData.value.columns.some(col => !col.name || !col.type)) {
727
+ await modal.warning('请完善所有字段信息');
728
+ return;
729
+ }
730
+
731
+ if (!props.connection?.id) {
732
+ await modal.warning('请先选择数据库连接');
733
+ return;
734
+ }
735
+
736
+ let result;
737
+
738
+ if (props.mode === 'create') {
739
+ // 创建表:使用 SQL 语句
740
+ const sql = generateSQL();
741
+ result = await databaseService.executeQuery(
742
+ props.connection.id,
743
+ sql,
744
+ props.database
745
+ );
746
+ } else {
747
+ // 修改表:使用差异化对比
748
+ const tableDiff = calculateTableDiff();
749
+
750
+ // 检查是否有实际修改
751
+ if (tableDiff.addedColumns.length === 0 &&
752
+ tableDiff.modifiedColumns.length === 0 &&
753
+ tableDiff.deletedColumns.length === 0 &&
754
+ !tableDiff.tableCommentChanged) {
755
+ await modal.info('没有检测到任何修改');
756
+ return;
757
+ }
758
+
759
+ // 调用修改表接口(需要后端实现)
760
+ result = await databaseService.alterTable(
761
+ props.connection.id,
762
+ props.database,
763
+ tableDiff
764
+ );
765
+ }
766
+
767
+ emit('submit', {
768
+ success: result.ret === 0,
769
+ message: result.ret === 0 ? '操作成功' : '操作失败',
770
+ data: result.data,
771
+ mode: props.mode
772
+ });
773
+
774
+ close();
775
+ } catch (error) {
776
+ console.error('提交失败:', error);
777
+ emit('submit', {
778
+ success: false,
779
+ message: '操作失败',
780
+ mode: props.mode
781
+ });
782
+ }
783
+ }
784
+
785
+ // 关闭弹窗
786
+ function close() {
787
+ emit('close');
788
+ }
789
+
790
+ // 监听props变化
791
+ watch(() => props.visible, (newVal) => {
792
+ if (newVal) {
793
+ initFormData();
794
+ }
795
+ });
796
+
797
+ // 获取类别标签
798
+ function getCategoryLabel(category: string): string {
799
+ const labels: Record<string, string> = {
800
+ [ColumnCategory.NUMERIC]: '数值类型',
801
+ [ColumnCategory.STRING]: '字符串类型',
802
+ [ColumnCategory.TEXT]: '文本类型',
803
+ [ColumnCategory.DATE_TIME]: '日期时间类型',
804
+ [ColumnCategory.BOOLEAN]: '布尔类型',
805
+ [ColumnCategory.BINARY]: '二进制类型',
806
+ [ColumnCategory.JSON]: 'JSON类型',
807
+ [ColumnCategory.ARRAY]: '数组类型',
808
+ [ColumnCategory.SPATIAL]: '空间类型',
809
+ [ColumnCategory.OTHER]: '其他类型'
810
+ };
811
+ return labels[category] || category;
812
+ }
813
+
814
+ // 获取选中的类型信息
815
+ function getSelectedType(column: any) {
816
+ if (!column.type) return null;
817
+
818
+ // 提取类型名称(去掉括号和长度信息)
819
+ const typeName = column.type.match(/^[a-zA-Z]+/)?.[0] || column.type;
820
+
821
+ // 大小写不敏感匹配
822
+ return columnTypes.value.find(t => t.name.toLowerCase() === typeName.toLowerCase());
823
+ }
824
+
825
+ // 检查类型是否需要长度参数
826
+ function needsLength(column: any): boolean {
827
+ const typeInfo = getSelectedType(column);
828
+ return typeInfo?.requiresLength || false;
829
+ }
830
+
831
+ // 检查类型是否需要精度参数
832
+ function needsPrecision(column: any): boolean {
833
+ const typeInfo = getSelectedType(column);
834
+ return typeInfo?.requiresPrecision || false;
835
+ }
836
+
837
+ // 检查类型是否需要小数位数参数
838
+ function needsScale(column: any): boolean {
839
+ const typeInfo = getSelectedType(column);
840
+ return typeInfo?.requiresScale || false;
841
+ }
842
+
843
+ // 检查类型是否支持自增
844
+ function supportsAutoIncrement(column: any): boolean {
845
+ const typeInfo = getSelectedType(column);
846
+ return typeInfo?.supportsAutoIncrement || false;
847
+ }
848
+
849
+ // 初始化
850
+ onMounted(() => {
851
+ if (props.visible) {
852
+ initFormData();
853
+ }
854
+ });
855
+ </script>
856
+
857
+ <style scoped>
858
+ .section-title {
859
+ color: #495057;
860
+ font-weight: 600;
861
+ border-bottom: 2px solid #e9ecef;
862
+ padding-bottom: 0.5rem;
863
+ margin-bottom: 1rem;
864
+ }
865
+
866
+ .sql-preview {
867
+ font-family: 'Courier New', monospace;
868
+ font-size: 0.875rem;
869
+ max-height: 200px;
870
+ overflow-y: auto;
871
+ border: 1px solid #dee2e6;
872
+ }
873
+
874
+ .table th {
875
+ font-size: 0.875rem;
876
+ font-weight: 600;
877
+ }
878
+ .table td {
879
+ padding: 0!important;
880
+ }
881
+ .table .form-control, .table .form-select {
882
+ border: none;
883
+ border-radius: 0;
884
+ box-shadow: none;
885
+ }
886
+ .form-control-sm, .form-select-sm {
887
+ font-size: 0.875rem;
888
+ }
889
+
890
+ .modal-content {
891
+ max-height: 90vh;
892
+ overflow-y: auto;
893
+ }
894
+
895
+ .modal-body {
896
+ max-height: calc(90vh - 120px);
897
+ overflow-y: auto;
898
+ }
899
+
900
+ .input-group-sm .form-control {
901
+ border-radius: 0;
902
+ }
903
+
904
+ .input-group-sm .form-control:first-child {
905
+ border-top-left-radius: 0.25rem;
906
+ border-bottom-left-radius: 0.25rem;
907
+ }
908
+
909
+ .input-group-sm .form-control:last-child {
910
+ border-top-right-radius: 0.25rem;
911
+ border-bottom-right-radius: 0.25rem;
912
+ }
913
+
914
+ .text-muted {
915
+ color: #6c757d !important;
916
+ }
917
917
  </style>