fdb2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +21 -0
- package/.editorconfig +11 -0
- package/.eslintrc.cjs +14 -0
- package/.eslintrc.json +7 -0
- package/.prettierrc.js +3 -0
- package/.tpl.env +22 -0
- package/README.md +260 -0
- package/bin/build.sh +28 -0
- package/bin/deploy.sh +8 -0
- package/bin/dev.sh +10 -0
- package/bin/docker/.env +4 -0
- package/bin/docker/dev-docker-compose.yml +43 -0
- package/bin/docker/dev.Dockerfile +24 -0
- package/bin/docker/prod-docker-compose.yml +17 -0
- package/bin/docker/prod.Dockerfile +29 -0
- package/bin/fdb2.js +142 -0
- package/data/connections.demo.json +32 -0
- package/env.d.ts +1 -0
- package/nw-build.js +120 -0
- package/nw-dev.js +65 -0
- package/package.json +114 -0
- package/public/favicon.ico +0 -0
- package/public/index.html +9 -0
- package/public/modules/header.tpl +14 -0
- package/public/modules/initial_state.tpl +55 -0
- package/server/index.ts +677 -0
- package/server/model/connection.entity.ts +66 -0
- package/server/model/database.entity.ts +246 -0
- package/server/service/connection.service.ts +334 -0
- package/server/service/database/base.service.ts +363 -0
- package/server/service/database/database.service.ts +510 -0
- package/server/service/database/index.ts +7 -0
- package/server/service/database/mssql.service.ts +723 -0
- package/server/service/database/mysql.service.ts +761 -0
- package/server/service/database/oracle.service.ts +839 -0
- package/server/service/database/postgres.service.ts +744 -0
- package/server/service/database/sqlite.service.ts +559 -0
- package/server/service/session.service.ts +158 -0
- package/server.js +128 -0
- package/src/adapter/ajax.ts +135 -0
- package/src/assets/base.css +1 -0
- package/src/assets/database.css +950 -0
- package/src/assets/images/collapse.png +0 -0
- package/src/assets/images/no-login.png +0 -0
- package/src/assets/images/svg/illustrations/illustration-1.svg +1 -0
- package/src/assets/images/svg/illustrations/illustration-2.svg +2 -0
- package/src/assets/images/svg/illustrations/illustration-3.svg +50 -0
- package/src/assets/images/svg/illustrations/illustration-4.svg +1 -0
- package/src/assets/images/svg/illustrations/illustration-5.svg +73 -0
- package/src/assets/images/svg/illustrations/illustration-6.svg +89 -0
- package/src/assets/images/svg/illustrations/illustration-7.svg +39 -0
- package/src/assets/images/svg/illustrations/illustration-8.svg +1 -0
- package/src/assets/images/svg/separators/curve-2.svg +3 -0
- package/src/assets/images/svg/separators/curve.svg +3 -0
- package/src/assets/images/svg/separators/line.svg +3 -0
- package/src/assets/images/theme/light/screen-1-1000x800.jpg +0 -0
- package/src/assets/images/theme/light/screen-2-1000x800.jpg +0 -0
- package/src/assets/login/bg.jpg +0 -0
- package/src/assets/login/bg.png +0 -0
- package/src/assets/login/left.jpg +0 -0
- package/src/assets/logo.svg +73 -0
- package/src/assets/logo.webp +0 -0
- package/src/assets/main.css +1 -0
- package/src/base/config.ts +20 -0
- package/src/base/detect.ts +134 -0
- package/src/base/entity.ts +92 -0
- package/src/base/eventBus.ts +37 -0
- package/src/base//345/237/272/347/241/200/345/261/202.md +7 -0
- package/src/components/connection-editor/index.vue +590 -0
- package/src/components/dataGrid/index.vue +105 -0
- package/src/components/dataGrid/pagination.vue +106 -0
- package/src/components/loading/index.vue +43 -0
- package/src/components/modal/index.ts +181 -0
- package/src/components/modal/index.vue +560 -0
- package/src/components/toast/index.ts +44 -0
- package/src/components/toast/toast.vue +58 -0
- package/src/components/user/name.vue +104 -0
- package/src/components/user/selector.vue +416 -0
- package/src/domain/SysConfig.ts +74 -0
- package/src/platform/App.vue +8 -0
- package/src/platform/database/components/connection-detail.vue +1154 -0
- package/src/platform/database/components/data-editor.vue +478 -0
- package/src/platform/database/components/data-import-export.vue +1602 -0
- package/src/platform/database/components/database-detail.vue +1173 -0
- package/src/platform/database/components/database-monitor.vue +1086 -0
- package/src/platform/database/components/db-tools.vue +577 -0
- package/src/platform/database/components/query-history.vue +1349 -0
- package/src/platform/database/components/sql-executor.vue +738 -0
- package/src/platform/database/components/sql-query-editor.vue +1046 -0
- package/src/platform/database/components/table-detail.vue +1376 -0
- package/src/platform/database/components/table-editor.vue +690 -0
- package/src/platform/database/explorer.vue +1840 -0
- package/src/platform/database/index.vue +1193 -0
- package/src/platform/database/layout.vue +367 -0
- package/src/platform/database/router.ts +37 -0
- package/src/platform/database/styles/common.scss +602 -0
- package/src/platform/database/types/common.ts +445 -0
- package/src/platform/database/utils/export.ts +232 -0
- package/src/platform/database/utils/helpers.ts +437 -0
- package/src/platform/index.ts +33 -0
- package/src/platform/router.ts +41 -0
- package/src/service/base.ts +128 -0
- package/src/service/database.ts +500 -0
- package/src/service/login.ts +121 -0
- package/src/shims-vue.d.ts +7 -0
- package/src/stores/connection.ts +266 -0
- package/src/stores/session.ts +87 -0
- package/src/typings/database-types.ts +413 -0
- package/src/typings/database.ts +364 -0
- package/src/typings/global.d.ts +58 -0
- package/src/typings/pinia.d.ts +8 -0
- package/src/utils/clipboard.ts +30 -0
- package/src/utils/database-types.ts +243 -0
- package/src/utils/modal.ts +124 -0
- package/src/utils/request.ts +55 -0
- package/src/utils/sleep.ts +4 -0
- package/src/utils/toast.ts +73 -0
- package/src/utils/util.ts +171 -0
- package/src/utils/xlsx.ts +228 -0
- package/tsconfig.json +33 -0
- package/tsconfig.server.json +19 -0
- package/view/index.html +9 -0
- package/view/modules/header.tpl +14 -0
- package/view/modules/initial_state.tpl +20 -0
- package/vite.config.ts +384 -0
|
@@ -0,0 +1,690 @@
|
|
|
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
|
+
|
|
197
|
+
// Props
|
|
198
|
+
const props = defineProps<{
|
|
199
|
+
visible: boolean;
|
|
200
|
+
connection: ConnectionEntity | null;
|
|
201
|
+
database: string;
|
|
202
|
+
table?: TableEntity | null;
|
|
203
|
+
mode: 'create' | 'edit';
|
|
204
|
+
}>();
|
|
205
|
+
|
|
206
|
+
// Emits
|
|
207
|
+
const emit = defineEmits<{
|
|
208
|
+
close: [];
|
|
209
|
+
submit: [result: any];
|
|
210
|
+
}>();
|
|
211
|
+
|
|
212
|
+
const databaseService = new DatabaseService();
|
|
213
|
+
|
|
214
|
+
// 获取当前数据库支持的列类型
|
|
215
|
+
const columnTypes = computed(() => {
|
|
216
|
+
if (!props.connection) return [];
|
|
217
|
+
return getColumnTypesByName(props.connection.type);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// 按类别分组的列类型
|
|
221
|
+
const columnTypesByCategory = computed(() => {
|
|
222
|
+
const categories: Record<string, any[]> = {};
|
|
223
|
+
columnTypes.value.forEach(type => {
|
|
224
|
+
if (!categories[type.category]) {
|
|
225
|
+
categories[type.category] = [];
|
|
226
|
+
}
|
|
227
|
+
categories[type.category].push(type);
|
|
228
|
+
});
|
|
229
|
+
return categories;
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// 表单数据
|
|
233
|
+
const formData = ref({
|
|
234
|
+
tableName: '',
|
|
235
|
+
tableComment: '',
|
|
236
|
+
columns: [{
|
|
237
|
+
name: '',
|
|
238
|
+
type: '',
|
|
239
|
+
length: '',
|
|
240
|
+
nullable: false,
|
|
241
|
+
defaultValue: '',
|
|
242
|
+
isPrimary: false,
|
|
243
|
+
isAutoIncrement: false,
|
|
244
|
+
comment: ''
|
|
245
|
+
}]
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// 初始化表单数据
|
|
249
|
+
function initFormData() {
|
|
250
|
+
if (props.mode === 'edit' && props.table) {
|
|
251
|
+
formData.value = {
|
|
252
|
+
tableName: props.table.name || '',
|
|
253
|
+
tableComment: props.table.comment || '',
|
|
254
|
+
columns: props.table.columns?.map(col => ({
|
|
255
|
+
name: col.name || '',
|
|
256
|
+
type: col.type || '',
|
|
257
|
+
length: col.length || '',
|
|
258
|
+
precision: col.precision || null,
|
|
259
|
+
scale: col.scale || null,
|
|
260
|
+
nullable: col.nullable || false,
|
|
261
|
+
defaultValue: col.defaultValue || '',
|
|
262
|
+
isPrimary: col.isPrimary || false,
|
|
263
|
+
isAutoIncrement: col.isAutoIncrement || false,
|
|
264
|
+
comment: col.comment || ''
|
|
265
|
+
})) || []
|
|
266
|
+
};
|
|
267
|
+
} else {
|
|
268
|
+
formData.value = {
|
|
269
|
+
tableName: '',
|
|
270
|
+
tableComment: '',
|
|
271
|
+
columns: [{
|
|
272
|
+
name: '',
|
|
273
|
+
type: '',
|
|
274
|
+
length: '',
|
|
275
|
+
precision: null,
|
|
276
|
+
scale: null,
|
|
277
|
+
nullable: false,
|
|
278
|
+
defaultValue: '',
|
|
279
|
+
isPrimary: false,
|
|
280
|
+
isAutoIncrement: false,
|
|
281
|
+
comment: ''
|
|
282
|
+
}]
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 添加字段
|
|
288
|
+
function addColumn() {
|
|
289
|
+
formData.value.columns.push({
|
|
290
|
+
name: '',
|
|
291
|
+
type: '',
|
|
292
|
+
length: '',
|
|
293
|
+
precision: null,
|
|
294
|
+
scale: null,
|
|
295
|
+
nullable: false,
|
|
296
|
+
defaultValue: '',
|
|
297
|
+
isPrimary: false,
|
|
298
|
+
isAutoIncrement: false,
|
|
299
|
+
comment: ''
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// 删除字段
|
|
304
|
+
function removeColumn(index: number) {
|
|
305
|
+
if (formData.value.columns.length > 1) {
|
|
306
|
+
formData.value.columns.splice(index, 1);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 主键变更处理
|
|
311
|
+
function onPrimaryKeyChange(column: any, index: number) {
|
|
312
|
+
if (column.isPrimary) {
|
|
313
|
+
// 取消其他字段的主键标记
|
|
314
|
+
formData.value.columns.forEach((col, idx) => {
|
|
315
|
+
if (idx !== index) {
|
|
316
|
+
col.isPrimary = false;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
// 如果取消主键,也取消自增
|
|
321
|
+
if (!column.isPrimary) {
|
|
322
|
+
column.isAutoIncrement = false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// 生成SQL语句
|
|
327
|
+
function generateSQL(): string {
|
|
328
|
+
if (!formData.value.tableName) return '请输入表名';
|
|
329
|
+
|
|
330
|
+
if (props.mode === 'create') {
|
|
331
|
+
// 获取当前数据库的标识符引用方式
|
|
332
|
+
const quoteIdentifier = (name: string) => {
|
|
333
|
+
if (!props.connection) return `"${name}"`;
|
|
334
|
+
switch (props.connection.type.toLowerCase()) {
|
|
335
|
+
case 'mysql': return `\`${name}\``;
|
|
336
|
+
case 'postgres': return `"${name}"`;
|
|
337
|
+
case 'sqlite': return `"${name}"`;
|
|
338
|
+
case 'oracle': return `"${name.toUpperCase()}"`;
|
|
339
|
+
case 'mssql': return `[${name}]`;
|
|
340
|
+
default: return `"${name}"`;
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// 创建表SQL
|
|
345
|
+
const columnsSQL = formData.value.columns.map(col => {
|
|
346
|
+
if (!col.name || !col.type) return '';
|
|
347
|
+
|
|
348
|
+
let sql = `${quoteIdentifier(col.name)} ${col.type}`;
|
|
349
|
+
|
|
350
|
+
// 处理长度和精度参数
|
|
351
|
+
if (col.length && (needsLength(col) || col.type.includes('CHAR'))) {
|
|
352
|
+
sql += `(${col.length})`;
|
|
353
|
+
} else if (col.precision) {
|
|
354
|
+
if (col.scale) {
|
|
355
|
+
sql += `(${col.precision},${col.scale})`;
|
|
356
|
+
} else {
|
|
357
|
+
sql += `(${col.precision})`;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// SQLite 特殊处理:自增主键必须在列定义中包含 PRIMARY KEY
|
|
362
|
+
const isSqliteAutoIncrementPrimary =
|
|
363
|
+
props.connection?.type.toLowerCase() === 'sqlite' &&
|
|
364
|
+
col.isAutoIncrement &&
|
|
365
|
+
col.isPrimary;
|
|
366
|
+
|
|
367
|
+
// 处理NULL约束(SQLite 自增主键不需要 NOT NULL)
|
|
368
|
+
if (!col.nullable && !isSqliteAutoIncrementPrimary) {
|
|
369
|
+
sql += ' NOT NULL';
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// 处理默认值
|
|
373
|
+
if (col.defaultValue) {
|
|
374
|
+
sql += ` DEFAULT ${formatDefaultValue(col.defaultValue, col.type)}`;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// 处理自增(数据库特定语法)
|
|
378
|
+
if (col.isAutoIncrement) {
|
|
379
|
+
switch (props.connection?.type.toLowerCase()) {
|
|
380
|
+
case 'mysql':
|
|
381
|
+
sql += ' AUTO_INCREMENT';
|
|
382
|
+
break;
|
|
383
|
+
case 'postgres':
|
|
384
|
+
// PostgreSQL 使用 SERIAL 类型或 GENERATED AS IDENTITY
|
|
385
|
+
if (col.type.toLowerCase().includes('serial') || col.type.toLowerCase().includes('bigserial')) {
|
|
386
|
+
// 已包含自增
|
|
387
|
+
} else {
|
|
388
|
+
sql += ' GENERATED ALWAYS AS IDENTITY';
|
|
389
|
+
}
|
|
390
|
+
break;
|
|
391
|
+
case 'sqlite':
|
|
392
|
+
// SQLite 中 AUTOINCREMENT 只能用于 INTEGER 类型
|
|
393
|
+
// 如果类型是 INT,需要改为 INTEGER
|
|
394
|
+
if (col.type.toUpperCase() === 'INT') {
|
|
395
|
+
sql = sql.replace(/\bINT\b/, 'INTEGER');
|
|
396
|
+
}
|
|
397
|
+
// SQLite 自增主键必须在列定义中包含 PRIMARY KEY
|
|
398
|
+
if (col.isPrimary) {
|
|
399
|
+
sql += ' PRIMARY KEY AUTOINCREMENT';
|
|
400
|
+
} else {
|
|
401
|
+
sql += ' AUTOINCREMENT';
|
|
402
|
+
}
|
|
403
|
+
break;
|
|
404
|
+
case 'oracle':
|
|
405
|
+
// Oracle 使用序列和触发器,这里简化处理
|
|
406
|
+
break;
|
|
407
|
+
case 'mssql':
|
|
408
|
+
sql += ' IDENTITY(1,1)';
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// 处理注释(数据库特定语法)
|
|
414
|
+
if (col.comment) {
|
|
415
|
+
switch (props.connection?.type.toLowerCase()) {
|
|
416
|
+
case 'mysql':
|
|
417
|
+
sql += ` COMMENT '${col.comment}'`;
|
|
418
|
+
break;
|
|
419
|
+
case 'postgres':
|
|
420
|
+
// PostgreSQL 注释需要单独的 COMMENT 语句
|
|
421
|
+
break;
|
|
422
|
+
case 'sqlite':
|
|
423
|
+
// SQLite 不支持列注释
|
|
424
|
+
break;
|
|
425
|
+
case 'oracle':
|
|
426
|
+
sql += ` COMMENT '${col.comment}'`;
|
|
427
|
+
break;
|
|
428
|
+
case 'mssql':
|
|
429
|
+
// SQL Server 使用扩展属性
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
return sql;
|
|
435
|
+
}).filter(sql => sql).join(',\n ');
|
|
436
|
+
|
|
437
|
+
// 处理主键
|
|
438
|
+
const primaryKeys = formData.value.columns
|
|
439
|
+
.filter(col => col.isPrimary)
|
|
440
|
+
.map(col => quoteIdentifier(col.name));
|
|
441
|
+
|
|
442
|
+
let sql = `CREATE TABLE ${quoteIdentifier(formData.value.tableName)} (\n ${columnsSQL}`;
|
|
443
|
+
|
|
444
|
+
// SQLite 自增主键已经在列定义中包含 PRIMARY KEY,不需要再添加
|
|
445
|
+
const hasSqliteAutoIncrementPrimary = props.connection?.type.toLowerCase() === 'sqlite' &&
|
|
446
|
+
formData.value.columns.some(col => col.isAutoIncrement && col.isPrimary);
|
|
447
|
+
|
|
448
|
+
if (primaryKeys.length > 0 && !hasSqliteAutoIncrementPrimary) {
|
|
449
|
+
sql += `,\n PRIMARY KEY (${primaryKeys.join(', ')})`;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
sql += '\n)';
|
|
453
|
+
|
|
454
|
+
// 处理表注释和引擎(数据库特定)
|
|
455
|
+
switch (props.connection?.type.toLowerCase()) {
|
|
456
|
+
case 'mysql':
|
|
457
|
+
if (formData.value.tableComment) {
|
|
458
|
+
sql += ` COMMENT='${formData.value.tableComment}'`;
|
|
459
|
+
}
|
|
460
|
+
sql += ' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4';
|
|
461
|
+
break;
|
|
462
|
+
case 'postgres':
|
|
463
|
+
// PostgreSQL 表注释需要单独的 COMMENT 语句
|
|
464
|
+
break;
|
|
465
|
+
case 'oracle':
|
|
466
|
+
if (formData.value.tableComment) {
|
|
467
|
+
sql += ` COMMENT '${formData.value.tableComment}'`;
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
case 'mssql':
|
|
471
|
+
// SQL Server 使用扩展属性
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// 生成额外的注释语句(如果需要)
|
|
476
|
+
if (props.connection?.type.toLowerCase() === 'postgres') {
|
|
477
|
+
const commentStatements = [];
|
|
478
|
+
if (formData.value.tableComment) {
|
|
479
|
+
commentStatements.push(`COMMENT ON TABLE ${quoteIdentifier(formData.value.tableName)} IS '${formData.value.tableComment}'`);
|
|
480
|
+
}
|
|
481
|
+
formData.value.columns.forEach(col => {
|
|
482
|
+
if (col.comment) {
|
|
483
|
+
commentStatements.push(`COMMENT ON COLUMN ${quoteIdentifier(formData.value.tableName)}.${quoteIdentifier(col.name)} IS '${col.comment}'`);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
if (commentStatements.length > 0) {
|
|
487
|
+
sql += ';\n' + commentStatements.join(';\n');
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
return sql;
|
|
492
|
+
} else {
|
|
493
|
+
// 修改表SQL(简化版,实际应该对比差异)
|
|
494
|
+
return `-- 表结构修改SQL(需要对比差异生成)\n-- 当前表名: ${formData.value.tableName}`;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// 格式化默认值
|
|
499
|
+
function formatDefaultValue(value: any, type: string): string {
|
|
500
|
+
if (value === null || value === undefined || value === '') {
|
|
501
|
+
return 'NULL';
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const lowerType = type.toLowerCase();
|
|
505
|
+
|
|
506
|
+
// 数值类型不加引号
|
|
507
|
+
if (isNumberInput(lowerType) && !isNaN(value)) {
|
|
508
|
+
return String(value);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// 布尔类型
|
|
512
|
+
if (isBooleanInput(lowerType)) {
|
|
513
|
+
return value ? 'TRUE' : 'FALSE';
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// 字符串类型加引号
|
|
517
|
+
return `'${String(value).replace(/'/g, "''")}'`;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// 提交表单
|
|
521
|
+
async function submit() {
|
|
522
|
+
try {
|
|
523
|
+
if (!formData.value.tableName) {
|
|
524
|
+
await modal.warning('请输入表名');
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (formData.value.columns.some(col => !col.name || !col.type)) {
|
|
529
|
+
await modal.warning('请完善所有字段信息');
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const sql = generateSQL();
|
|
534
|
+
|
|
535
|
+
if (!props.connection?.id) {
|
|
536
|
+
await modal.warning('请先选择数据库连接');
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const result = await databaseService.executeQuery(
|
|
541
|
+
props.connection.id,
|
|
542
|
+
sql,
|
|
543
|
+
props.database
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
emit('submit', {
|
|
547
|
+
success: result.ret === 0,
|
|
548
|
+
message: result.ret === 0 ? '操作成功' : '操作失败',
|
|
549
|
+
data: result.data,
|
|
550
|
+
mode: props.mode
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
close();
|
|
554
|
+
} catch (error) {
|
|
555
|
+
console.error('提交失败:', error);
|
|
556
|
+
emit('submit', {
|
|
557
|
+
success: false,
|
|
558
|
+
message: '操作失败',
|
|
559
|
+
mode: props.mode
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// 关闭弹窗
|
|
565
|
+
function close() {
|
|
566
|
+
emit('close');
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// 监听props变化
|
|
570
|
+
watch(() => props.visible, (newVal) => {
|
|
571
|
+
if (newVal) {
|
|
572
|
+
initFormData();
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
// 获取类别标签
|
|
577
|
+
function getCategoryLabel(category: string): string {
|
|
578
|
+
const labels: Record<string, string> = {
|
|
579
|
+
[ColumnCategory.NUMERIC]: '数值类型',
|
|
580
|
+
[ColumnCategory.STRING]: '字符串类型',
|
|
581
|
+
[ColumnCategory.TEXT]: '文本类型',
|
|
582
|
+
[ColumnCategory.DATE_TIME]: '日期时间类型',
|
|
583
|
+
[ColumnCategory.BOOLEAN]: '布尔类型',
|
|
584
|
+
[ColumnCategory.BINARY]: '二进制类型',
|
|
585
|
+
[ColumnCategory.JSON]: 'JSON类型',
|
|
586
|
+
[ColumnCategory.ARRAY]: '数组类型',
|
|
587
|
+
[ColumnCategory.SPATIAL]: '空间类型',
|
|
588
|
+
[ColumnCategory.OTHER]: '其他类型'
|
|
589
|
+
};
|
|
590
|
+
return labels[category] || category;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// 获取选中的类型信息
|
|
594
|
+
function getSelectedType(column: any) {
|
|
595
|
+
return columnTypes.value.find(t => t.name === column.type);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// 检查类型是否需要长度参数
|
|
599
|
+
function needsLength(column: any): boolean {
|
|
600
|
+
const typeInfo = getSelectedType(column);
|
|
601
|
+
return typeInfo?.requiresLength || false;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// 检查类型是否需要精度参数
|
|
605
|
+
function needsPrecision(column: any): boolean {
|
|
606
|
+
const typeInfo = getSelectedType(column);
|
|
607
|
+
return typeInfo?.requiresPrecision || false;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// 检查类型是否需要小数位数参数
|
|
611
|
+
function needsScale(column: any): boolean {
|
|
612
|
+
const typeInfo = getSelectedType(column);
|
|
613
|
+
return typeInfo?.requiresScale || false;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// 检查类型是否支持自增
|
|
617
|
+
function supportsAutoIncrement(column: any): boolean {
|
|
618
|
+
const typeInfo = getSelectedType(column);
|
|
619
|
+
return typeInfo?.supportsAutoIncrement || false;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// 初始化
|
|
623
|
+
onMounted(() => {
|
|
624
|
+
if (props.visible) {
|
|
625
|
+
initFormData();
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
</script>
|
|
629
|
+
|
|
630
|
+
<style scoped>
|
|
631
|
+
.section-title {
|
|
632
|
+
color: #495057;
|
|
633
|
+
font-weight: 600;
|
|
634
|
+
border-bottom: 2px solid #e9ecef;
|
|
635
|
+
padding-bottom: 0.5rem;
|
|
636
|
+
margin-bottom: 1rem;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
.sql-preview {
|
|
640
|
+
font-family: 'Courier New', monospace;
|
|
641
|
+
font-size: 0.875rem;
|
|
642
|
+
max-height: 200px;
|
|
643
|
+
overflow-y: auto;
|
|
644
|
+
border: 1px solid #dee2e6;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
.table th {
|
|
648
|
+
font-size: 0.875rem;
|
|
649
|
+
font-weight: 600;
|
|
650
|
+
}
|
|
651
|
+
.table td {
|
|
652
|
+
padding: 0!important;
|
|
653
|
+
}
|
|
654
|
+
.table .form-control, .table .form-select {
|
|
655
|
+
border: none;
|
|
656
|
+
border-radius: 0;
|
|
657
|
+
box-shadow: none;
|
|
658
|
+
}
|
|
659
|
+
.form-control-sm, .form-select-sm {
|
|
660
|
+
font-size: 0.875rem;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
.modal-content {
|
|
664
|
+
max-height: 90vh;
|
|
665
|
+
overflow-y: auto;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
.modal-body {
|
|
669
|
+
max-height: calc(90vh - 120px);
|
|
670
|
+
overflow-y: auto;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
.input-group-sm .form-control {
|
|
674
|
+
border-radius: 0;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
.input-group-sm .form-control:first-child {
|
|
678
|
+
border-top-left-radius: 0.25rem;
|
|
679
|
+
border-bottom-left-radius: 0.25rem;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
.input-group-sm .form-control:last-child {
|
|
683
|
+
border-top-right-radius: 0.25rem;
|
|
684
|
+
border-bottom-right-radius: 0.25rem;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
.text-muted {
|
|
688
|
+
color: #6c757d !important;
|
|
689
|
+
}
|
|
690
|
+
</style>
|