|
@@ -30,7 +30,10 @@
|
|
|
:name="'fieldName' + scope.$index"
|
|
|
style="margin: 0px; padding: 0px"
|
|
|
>
|
|
|
- <el-input v-model="scope.row.fieldName"></el-input>
|
|
|
+ <el-input
|
|
|
+ v-model="scope.row.fieldName"
|
|
|
+ @blur="fieldNameChange(scope.row)"
|
|
|
+ ></el-input>
|
|
|
</el-form-item>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
@@ -42,6 +45,7 @@
|
|
|
style="margin: 0px; padding: 0px"
|
|
|
>
|
|
|
<el-select
|
|
|
+ :disabled="!scope.row.fieldName"
|
|
|
v-model="scope.row.fieldType"
|
|
|
filterable
|
|
|
@blur="selectBlur(scope)"
|
|
@@ -50,7 +54,9 @@
|
|
|
dataType.find(
|
|
|
(item) => item.label === scope.row.fieldType
|
|
|
),
|
|
|
- scope
|
|
|
+ scope,
|
|
|
+ scope.$index,
|
|
|
+ scope.row
|
|
|
)
|
|
|
"
|
|
|
>
|
|
@@ -81,7 +87,8 @@
|
|
|
<!-- :disabled="handleSelected(dict.type.mysql_data_type.find(item => item.label === scope.row.fieldType), scope)"></el-input>-->
|
|
|
<el-input
|
|
|
v-model="scope.row.fieldLength"
|
|
|
- :disabled="changduclick(scope.$index)"
|
|
|
+ @blur="fieldLengthChange(scope.$index, scope.row)"
|
|
|
+ :disabled="changduclick(scope.$index) || !scope.row.fieldName"
|
|
|
@input="
|
|
|
scope.row.fieldLength = scope.row.fieldLength.replace(
|
|
|
/^(0+)|[^\d]+/g,
|
|
@@ -96,6 +103,7 @@
|
|
|
<el-table-column prop="isNull" label="不是null" width="100">
|
|
|
<template slot-scope="scope">
|
|
|
<el-checkbox
|
|
|
+ :disabled="!scope.row.fieldName"
|
|
|
@change="isNullChange(scope.row)"
|
|
|
v-model="scope.row.isNull"
|
|
|
></el-checkbox>
|
|
@@ -105,21 +113,30 @@
|
|
|
<el-table-column prop="isPrimary" label="键" width="100">
|
|
|
<template slot-scope="scope">
|
|
|
<el-checkbox
|
|
|
+ :disabled="!scope.row.fieldName"
|
|
|
v-model="scope.row.isPrimary"
|
|
|
- @change="hanleCheckbox(scope.$index)"
|
|
|
+ @change="hanleCheckbox(scope.$index, scope.row)"
|
|
|
></el-checkbox>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
|
|
|
|
<el-table-column prop="isAuto" label="自增" width="100">
|
|
|
<template slot-scope="scope">
|
|
|
- <el-checkbox v-model="scope.row.isAuto"></el-checkbox>
|
|
|
+ <el-checkbox
|
|
|
+ :disabled="!scope.row.fieldName"
|
|
|
+ @change="isAutoChange(scope.row)"
|
|
|
+ v-model="scope.row.isAuto"
|
|
|
+ ></el-checkbox>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
|
|
|
|
<el-table-column prop="fieldDescription" label="字段描述" width="200">
|
|
|
<template slot-scope="scope">
|
|
|
- <el-input v-model="scope.row.fieldDescription"></el-input>
|
|
|
+ <el-input
|
|
|
+ :disabled="!scope.row.fieldName"
|
|
|
+ @change="fieldDescriptionChange(scope.row)"
|
|
|
+ v-model="scope.row.fieldDescription"
|
|
|
+ ></el-input>
|
|
|
</template>
|
|
|
</el-table-column>
|
|
|
<el-table-column prop="operate" label="操作">
|
|
@@ -171,6 +188,7 @@ export default {
|
|
|
// ],
|
|
|
data() {
|
|
|
return {
|
|
|
+ sqlStringList: [],
|
|
|
isEdited: false,
|
|
|
testzidian: null,
|
|
|
form: {
|
|
@@ -212,6 +230,11 @@ export default {
|
|
|
Inoputdisabled: [],
|
|
|
// 需要指定长度
|
|
|
mysqlAllowLength: ["char", "varchar", "numeric", "decimal"],
|
|
|
+ modifySQLString: {
|
|
|
+ modify: {},
|
|
|
+ delete: {},
|
|
|
+ addNew: {},
|
|
|
+ },
|
|
|
};
|
|
|
},
|
|
|
async created() {
|
|
@@ -237,7 +260,7 @@ export default {
|
|
|
experienceData: {
|
|
|
handler(n, o) {
|
|
|
// do something
|
|
|
- this.experienceDataForm = {};
|
|
|
+ // this.experienceDataForm = {};
|
|
|
let rulesAdd = {};
|
|
|
this.experienceData.forEach((item, index) => {
|
|
|
this.$set(
|
|
@@ -260,13 +283,264 @@ export default {
|
|
|
// rulesAdd['fieldLength' + index] = item.fieldLength
|
|
|
// this.experienceDataForm = {...rulesAdd}
|
|
|
});
|
|
|
- console.log(this.experienceDataForm);
|
|
|
},
|
|
|
deep: true, // 深度监听父组件传过来对象变化
|
|
|
immediate: true,
|
|
|
},
|
|
|
},
|
|
|
methods: {
|
|
|
+ /* 修改记录回调start */
|
|
|
+ // 字段名称修改回调
|
|
|
+ fieldNameChange(row) {
|
|
|
+ if (!row.fieldName) return;
|
|
|
+ if (this.databaseType == "mysql") {
|
|
|
+ if (row.oldVal) {
|
|
|
+ if (!this.modifySQLString.modify[row.oldVal.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.oldVal.fieldName] = {};
|
|
|
+ }
|
|
|
+ //更改
|
|
|
+ this.modifySQLString.modify[row.oldVal.fieldName].fieldName =
|
|
|
+ this.mysqlMotifySQLString("fieldName", row);
|
|
|
+ this.sqlStringList.push(this.mysqlMotifySQLString("fieldName", row));
|
|
|
+ } else if (row.fieldType) {
|
|
|
+ //新增
|
|
|
+ this.modifySQLString.addNew[row.fieldName] =
|
|
|
+ this.mysqlMotifySQLString("addNew", row);
|
|
|
+ this.sqlStringList.push(this.mysqlMotifySQLString("addNew", row));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 字段类型改变回调
|
|
|
+ fieldTypeChange(index, row) {
|
|
|
+ if (!row.fieldType) return;
|
|
|
+ if (this.databaseType == "mysql") {
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ if (row.oldVal) {
|
|
|
+ this.modifySQLString.modify[row.fieldName].fieldType =
|
|
|
+ this.mysqlMotifySQLString("fieldType", row);
|
|
|
+ this.sqlStringList.push(this.mysqlMotifySQLString("fieldType", row));
|
|
|
+ } else if (row.fieldName) {
|
|
|
+ //新增
|
|
|
+ this.modifySQLString.addNew[row.fieldName] =
|
|
|
+ this.mysqlMotifySQLString("addNew", row);
|
|
|
+ this.sqlStringList.push(this.mysqlMotifySQLString("addNew", row));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 字段长度改变回调
|
|
|
+ fieldLengthChange(idnex, row) {
|
|
|
+ if (!row.fieldLength) return;
|
|
|
+ if (this.databaseType == "mysql") {
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ if (row.oldVal) {
|
|
|
+ this.modifySQLString.modify[row.fieldName].fieldLength =
|
|
|
+ this.mysqlMotifySQLString("fieldLength", row);
|
|
|
+ this.sqlStringList.push(
|
|
|
+ this.mysqlMotifySQLString("fieldLength", row)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ //处理非空选框问题
|
|
|
+ isNullChange(row) {
|
|
|
+ if (!row.isNull) {
|
|
|
+ row.isPrimary = false;
|
|
|
+ }
|
|
|
+ if (this.databaseType == "mysql") {
|
|
|
+ if (row.oldVal) {
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ this.modifySQLString.modify[row.fieldName].isNull = row.isNull
|
|
|
+ ? this.mysqlMotifySQLString("NOT NULL", row)
|
|
|
+ : this.mysqlMotifySQLString("NULL", row);
|
|
|
+ this.sqlStringList.push(
|
|
|
+ row.isNull
|
|
|
+ ? this.mysqlMotifySQLString("NOT NULL", row)
|
|
|
+ : this.mysqlMotifySQLString("NULL", row)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 处理主键选框问题
|
|
|
+ hanleCheckbox(ind, row) {
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ if (row.isPrimary && (!row.oldVal || !row.oldVal.isPrimary)) {
|
|
|
+ //添加新的主键
|
|
|
+ this.modifySQLString.modify[row.fieldName].addPrimary =
|
|
|
+ this.mysqlMotifySQLString("primary", row);
|
|
|
+ this.sqlStringList.push(this.mysqlMotifySQLString("primary", row));
|
|
|
+ }
|
|
|
+ this.experienceData.forEach((item, index) => {
|
|
|
+ if (index != ind) {
|
|
|
+ if (item.oldVal?.isPrimary) {
|
|
|
+ if (!this.modifySQLString.delete[item.fieldName]) {
|
|
|
+ this.modifySQLString.delete[item.fieldName] = {};
|
|
|
+ }
|
|
|
+ //去掉原来的主键
|
|
|
+ this.modifySQLString.delete[item.fieldName].dropPrimary =
|
|
|
+ this.mysqlMotifySQLString("drop primary", item);
|
|
|
+ this.sqlStringList.push(
|
|
|
+ this.mysqlMotifySQLString("drop primary", item)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ item.isPrimary = false;
|
|
|
+ }
|
|
|
+ if (index == ind && item.isPrimary == true) {
|
|
|
+ item.isNull = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ // 是否自增改变
|
|
|
+ isAutoChange(row) {
|
|
|
+ let sqlString = "";
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ if (row.oldVal) {
|
|
|
+ if (row.oldVal.isAuto != row.isAuto) {
|
|
|
+ this.modifySQLString.modify[row.fieldName].increment = row.isAuto
|
|
|
+ ? this.mysqlMotifySQLString("increment", row)
|
|
|
+ : this.mysqlMotifySQLString("no increment", row);
|
|
|
+ sqlString = row.isAuto
|
|
|
+ ? this.mysqlMotifySQLString("increment", row)
|
|
|
+ : this.mysqlMotifySQLString("no increment", row);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.modifySQLString.modify[row.fieldName].increment = row.isAuto
|
|
|
+ ? this.mysqlMotifySQLString("increment", row)
|
|
|
+ : this.mysqlMotifySQLString("no increment", row);
|
|
|
+ sqlString = row.isAuto
|
|
|
+ ? this.mysqlMotifySQLString("increment", row)
|
|
|
+ : this.mysqlMotifySQLString("no increment", row);
|
|
|
+ }
|
|
|
+ if (sqlString) {
|
|
|
+ this.sqlStringList.push(sqlString);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 字段描述改变
|
|
|
+ fieldDescriptionChange(row) {
|
|
|
+ let sqlString = "";
|
|
|
+ if (!this.modifySQLString.modify[row.fieldName]) {
|
|
|
+ this.modifySQLString.modify[row.fieldName] = {};
|
|
|
+ }
|
|
|
+ if (row.oldVal) {
|
|
|
+ if (row.oldVal.fieldDescription != row.fieldDescription) {
|
|
|
+ this.modifySQLString.modify[row.fieldName].fieldDescription =
|
|
|
+ this.mysqlMotifySQLString("describe", row);
|
|
|
+ sqlString = this.mysqlMotifySQLString("describe", row);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.modifySQLString.modify[row.fieldName].fieldDescription =
|
|
|
+ this.mysqlMotifySQLString("describe", row);
|
|
|
+ sqlString = this.mysqlMotifySQLString("describe", row);
|
|
|
+ }
|
|
|
+ if (sqlString) {
|
|
|
+ this.sqlStringList.push(sqlString);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 删除字段回调
|
|
|
+ deleteField(row) {
|
|
|
+ let sqlString = "";
|
|
|
+ if (row.oldVal) {
|
|
|
+ this.modifySQLString.delete[row.fieldName] = this.mysqlMotifySQLString(
|
|
|
+ "deleteField",
|
|
|
+ row
|
|
|
+ );
|
|
|
+ sqlString = this.mysqlMotifySQLString("deleteField", row);
|
|
|
+ }
|
|
|
+ delete this.modifySQLString.modify[row.fieldName];
|
|
|
+ delete this.modifySQLString.addNew[row.fieldName];
|
|
|
+ // delete this.modifySQLString.delete[row.fieldName];
|
|
|
+ if (sqlString) {
|
|
|
+ this.sqlStringList.push(sqlString);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 拼接mysql的修改语句
|
|
|
+ mysqlMotifySQLString(type, row) {
|
|
|
+ let table_name = this.form.tableName,
|
|
|
+ res = "ALTER TABLE";
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case "fieldName": //改字段
|
|
|
+ res += ` ${table_name} CHANGE ${row.oldVal.fieldName} ${row.fieldName} ${row.fieldType};`;
|
|
|
+ break;
|
|
|
+ case "fieldLength": //改长度
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} ${row.fieldType}(${row.fieldLength});`;
|
|
|
+ break;
|
|
|
+ case "fieldType": //改类型
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} ${row.fieldType};`;
|
|
|
+ break;
|
|
|
+ case "NULL": //改允许为NULL
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} ${row.fieldType} NULL;`;
|
|
|
+ break;
|
|
|
+ case "NOT NULL": //改不允许为NULL
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} ${row.fieldType} NOT NULL;`;
|
|
|
+ break;
|
|
|
+ case "primary": //设为主键
|
|
|
+ res += ` ${table_name} ADD PRIMARY KEY (${row.fieldName});`;
|
|
|
+ break;
|
|
|
+ case "drop primary": //删除主键
|
|
|
+ res += ` ${table_name} DROP PRIMARY KEY;`;
|
|
|
+ break;
|
|
|
+ case "increment": //添加自增
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} INT AUTO_INCREMENT;`;
|
|
|
+ break;
|
|
|
+ case "no increment": //删除自增属性
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} INT;`;
|
|
|
+ break;
|
|
|
+ case "describe": //修改字段描述
|
|
|
+ res += ` ${table_name} MODIFY ${row.fieldName} ${row.fieldType}${
|
|
|
+ row.fieldLength ? "" : "(" + row.fieldLength + ")"
|
|
|
+ } comment '${row.fieldDescription}';`;
|
|
|
+ break;
|
|
|
+ case "deleteField":
|
|
|
+ res += ` ${table_name} DROP ${row.fieldName};`;
|
|
|
+ break;
|
|
|
+ case "addNew": //新增列
|
|
|
+ res += ` ${table_name} ADD ${row.fieldName} ${row.fieldType};`;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+ },
|
|
|
+
|
|
|
+ // 获取sql列表
|
|
|
+ getSQLStringList() {
|
|
|
+ let res = [];
|
|
|
+ console.log(this.modifySQLString);
|
|
|
+ let deleteList = Object.values(this.modifySQLString.delete);
|
|
|
+ if (deleteList.length) {
|
|
|
+ for (const value of deleteList) {
|
|
|
+ res.push(Object.values(value)[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let addNewList = Object.values(this.modifySQLString.addNew);
|
|
|
+ if (addNewList.length) {
|
|
|
+ res = [...res, ...addNewList];
|
|
|
+ }
|
|
|
+ let modifyList = Object.values(this.modifySQLString.modify);
|
|
|
+ if (modifyList.length) {
|
|
|
+ for (const value of modifyList) {
|
|
|
+ res.push(Object.values(value)[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(res);
|
|
|
+ return res;
|
|
|
+ },
|
|
|
+
|
|
|
+ /* 修改记录回调start */
|
|
|
+
|
|
|
// 加载数据字典
|
|
|
async getDict() {
|
|
|
let res = await this.getDicts(this.databaseType + "_data_type");
|
|
@@ -296,6 +570,7 @@ export default {
|
|
|
};
|
|
|
tableInfo(data).then((response) => {
|
|
|
response.data.forEach((item) => {
|
|
|
+ item.oldVal = JSON.parse(JSON.stringify(item)); // 保留原始数据
|
|
|
this.experienceData.push(item);
|
|
|
this.handleRules("add");
|
|
|
});
|
|
@@ -314,7 +589,11 @@ export default {
|
|
|
return this.Inoputdisabled[inputIndex];
|
|
|
},
|
|
|
// 下拉选中事件 // 处理选中类型后长度输入框是否禁用
|
|
|
- handleSelected(tem, scope) {
|
|
|
+ handleSelected(tem, scope, index, val) {
|
|
|
+ if (val) {
|
|
|
+ //判断是否是由change事件触发的函数
|
|
|
+ this.fieldTypeChange(index, val);
|
|
|
+ }
|
|
|
if (tem == undefined) return;
|
|
|
if (!this.tableform["fieldLength" + scope.$index]) {
|
|
|
this.tableform["fieldLength" + scope.$index] = {};
|
|
@@ -369,7 +648,15 @@ export default {
|
|
|
this.$message.error(msg);
|
|
|
callback(new Error(msg));
|
|
|
} else {
|
|
|
- callback();
|
|
|
+ //字段名去重校验
|
|
|
+ if (
|
|
|
+ this.experienceData.filter((item) => item.fieldName == value)
|
|
|
+ .length == 1
|
|
|
+ ) {
|
|
|
+ callback();
|
|
|
+ } else {
|
|
|
+ callback(new Error("字段名称不能重复"));
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
// 校验数据表名称
|
|
@@ -437,23 +724,6 @@ export default {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- // 处理主键选框问题
|
|
|
- hanleCheckbox(ind) {
|
|
|
- this.experienceData.forEach((item, index) => {
|
|
|
- if (index != ind) {
|
|
|
- item.isPrimary = false;
|
|
|
- }
|
|
|
- if (index == ind && item.isPrimary == true) {
|
|
|
- item.isNull = true;
|
|
|
- }
|
|
|
- });
|
|
|
- },
|
|
|
- //处理非空选框问题
|
|
|
- isNullChange(row) {
|
|
|
- if (!row.isNull) {
|
|
|
- row.isPrimary = false;
|
|
|
- }
|
|
|
- },
|
|
|
//增加经验行
|
|
|
handleAddExperienceline() {
|
|
|
if (this.experienceData == undefined) {
|
|
@@ -479,9 +749,10 @@ export default {
|
|
|
// console.log(b);
|
|
|
},
|
|
|
//删除经验行
|
|
|
- handleDeleteExperience(index) {
|
|
|
+ handleDeleteExperience(index, row) {
|
|
|
this.experienceData.splice(index, 1);
|
|
|
this.handleRules("del", index);
|
|
|
+ this.deleteField(row);
|
|
|
},
|
|
|
add() {
|
|
|
const rule1 = new Promise((resolve, reject) => {
|
|
@@ -575,6 +846,9 @@ export default {
|
|
|
field.fieldType = `${field.fieldType}(${field.fieldLength})`;
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
+ // console.log(this.sqlStringList);
|
|
|
+ // return;
|
|
|
let query = {
|
|
|
databaseType: this.$store.state.user.dataSource.databaseType,
|
|
|
databaseName: this.$store.state.user.dataSource.databaseName,
|