SQL编辑器自动提示库、表、列逻辑优化,更加好用

This commit is contained in:
暮光:城中城
2021-06-20 17:47:46 +08:00
parent a30d9318da
commit 5abe56caad
14 changed files with 209 additions and 174 deletions

View File

@@ -346,8 +346,7 @@
let choiceData = this.choiceResultObj[this.executeShowTable] || [];
if (choiceData.length > 0) {
let dataCols = this.executeResultList.find(item => item.name === this.executeShowTable).dataCols;
let tableName = '`' + this.pageParam.dbName + '`.`' + this.pageParam.tableName + '`';
let copyData = copyFormatter.format('update', this.editorDbProduct, dataCols, choiceData, this.conditionDataColsChoice, tableName);
let copyData = copyFormatter.format('update', this.pageParam.dbType, dataCols, choiceData, this.conditionDataColsChoice, this.pageParam.dbName, this.pageParam.tableName);
this.conditionDataColsChoice = [];
this.exportConditionVisible = false;
this.$copyText(copyData).then(
@@ -366,8 +365,7 @@
this.exportConditionVisible = true;
return;
}
let tableName = '`' + this.pageParam.dbName + '`.`' + this.pageParam.tableName + '`';
let copyData = copyFormatter.format(type, this.editorDbProduct, dataCols, choiceData, '', tableName);
let copyData = copyFormatter.format(type, this.pageParam.dbType, dataCols, choiceData, '', this.pageParam.dbName, this.pageParam.tableName);
this.$copyText(copyData).then(
res => this.$message.success("内容已复制到剪切板!"),
err => this.$message.error("抱歉,复制失败!")

View File

@@ -304,6 +304,7 @@
this.choiceDatasourceId = this.datasourceList[0].id;
this.executorSource = {sourceId: this.choiceDatasourceId};
this.loadDatabaseList();
this.loadSourceBaseInfo();
}
});
},
@@ -319,13 +320,10 @@
}
});
},
loadEditorData() {
datasourceApi.getEditorData({sourceId: this.choiceDatasourceId}).then(json => {
loadSourceBaseInfo() {
datasourceApi.getSourceBaseInfo({sourceId: this.choiceDatasourceId}).then(json => {
let data = json.data || {};
this.editorDbInfo = data.db || [];
this.editorDbProduct = data.product || '';
this.editorDbTableInfo = data.table || {};
this.editorColumnInfo = data.column || {};
});
},
sourceGroupChangeEvents() {
@@ -341,11 +339,13 @@
this.choiceDatasourceId = datasourceOptions[0].id;
this.executorSource = {sourceId: this.choiceDatasourceId};
this.loadDatabaseList();
this.loadSourceBaseInfo();
}
},
datasourceChangeEvents() {
this.executorSource = {sourceId: this.choiceDatasourceId};
this.loadDatabaseList();
this.loadSourceBaseInfo();
},
databaseChangeEvents() {
this.executorSource = {sourceId: this.choiceDatasourceId, dbName: this.choiceDatabase};
@@ -392,7 +392,7 @@
let choiceData = this.choiceResultObj[this.executeShowTable] || [];
if (choiceData.length > 0) {
let dataCols = this.executeResultList.find(item => item.name === this.executeShowTable).dataCols;
let copyData = copyFormatter.format('update', this.editorDbProduct, dataCols, choiceData, this.conditionDataColsChoice, '`table`');
let copyData = copyFormatter.format('update', this.editorDbProduct, dataCols, choiceData, this.conditionDataColsChoice);
this.conditionDataColsChoice = [];
this.exportConditionVisible = false;
this.$copyText(copyData).then(
@@ -411,7 +411,7 @@
this.exportConditionVisible = true;
return;
}
let copyData = copyFormatter.format(type, this.editorDbProduct, dataCols, choiceData, '', '`table`');
let copyData = copyFormatter.format(type, this.editorDbProduct, dataCols, choiceData, '');
this.$copyText(copyData).then(
res => this.$message.success("内容已复制到剪切板!"),
err => this.$message.error("抱歉,复制失败!")

View File

@@ -4,12 +4,14 @@
* @since 2021年5月23日
*/
export default {
insert(dataCols, choiceData, tableName = '`table`') {
insert(dataCols, choiceData, dbName, tableName) {
let tableNameRes = (!!dbName) ? '`' + dbName + '`.`' : '';
tableNameRes += (!!tableName) ? tableName : '`table`';
// 复制为insert语句
let copyData = '';
let names = '';
dataCols.forEach(col => {
if (names.length > 0) names += ', '
if (names.length > 0) names += ', ';
names += col.prop;
});
choiceData.forEach(item => {
@@ -24,40 +26,44 @@ export default {
values += "'" + val + "'";
}
});
copyData += 'insert into ' + tableName + ' (' + names + ') values (' + values + ');\n';
copyData += 'insert into ' + tableNameRes + ' (' + names + ') values (' + values + ');\n';
});
return copyData;
},
update(dataCols, choiceData, condition=[], tableName = '`table`') {
update(dataCols, choiceData, condition=[], dbName, tableName) {
let tableNameRes = (!!dbName) ? '`' + dbName + '`.`' : '';
tableNameRes += (!!tableName) ? tableName : '`table`';
// 复制为update语句
let copyData = '';
choiceData.forEach(item => {
let values = '', where = '';
dataCols.forEach(col => {
if (values.length > 0) values += ', ';
values += col.prop + '=';
let val = item[col.prop] || '';
if (typeof val === 'number' && !isNaN(val)) {
values += val;
if (condition.indexOf(col.prop) >= 0) {
if (condition.indexOf(col.prop) >= 0) {
if (typeof val === 'number' && !isNaN(val)) {
if (where.length > 0) where += ' and ';
where += col.prop + ' = ' + val;
}
} else {
val = String(val).replaceAll('\'', '\'\'');
values += "'" + val + "'";
if (condition.indexOf(col.prop) >= 0) {
} else {
if (where.length > 0) where += ' and ';
where += col.prop + ' = ' + "'" + val + "'";
}
} else {
if (values.length > 0) values += ', ';
values += col.prop + '=';
if (typeof val === 'number' && !isNaN(val)) {
values += val;
} else {
val = String(val).replaceAll('\'', '\'\'');
values += "'" + val + "'";
}
}
});
if (where.length > 0) where = ' where ' + where;
copyData += 'update ' + tableName + ' set ' + values + where + ';\n';
copyData += 'update ' + tableNameRes + ' set ' + values + where + ';\n';
});
return copyData;
},
json(dataCols, choiceData, tableName) {
json(dataCols, choiceData, dbName, tableName) {
// 复制为json
return JSON.stringify(choiceData);
},

View File

@@ -1,22 +1,24 @@
import base from './base'
import sqlserver from './sqlserver'
export default {
format(type, product, dataCols, choiceData, condition, tableName) {
format(type, product, dataCols, choiceData, condition, dbName, tableName) {
let formatter = this.getProduct(product);
if (type === 'insert') {
// 复制为insert语句
return formatter.insert(dataCols, choiceData, tableName);
return formatter.insert(dataCols, choiceData, dbName, tableName);
} else if (type === 'update') {
// 复制为update语句
return formatter.update(dataCols, choiceData, condition, tableName);
return formatter.update(dataCols, choiceData, condition, dbName, tableName);
} else if (type === 'json') {
// 复制为json
return formatter.json(dataCols, choiceData, tableName);
return formatter.json(dataCols, choiceData, dbName, tableName);
}
},
getProduct(product) {
if (product === 'mysql') {
// 不同数据源类型可以用不用的处理器类型,暂时只实现了一套基础的
// 不同数据源类型可以用不用的处理器类型
if (product === 'sqlserver') {
return sqlserver;
}
return base;
},

View File

@@ -0,0 +1,70 @@
/**
* 通用的转换器,如果通用的不能满足需要添加转换器实现转换
* @author 暮光:城中城
* @since 2021年5月23日
*/
export default {
insert(dataCols, choiceData, dbName, tableName) {
let tableNameRes = (!!dbName) ? dbName + '..' : '';
tableNameRes += (!!tableName) ? tableName : 'table';
// 复制为insert语句
let copyData = '';
let names = '';
dataCols.forEach(col => {
if (names.length > 0) names += ', ';
names += col.prop;
});
choiceData.forEach(item => {
let values = '';
dataCols.forEach(col => {
if (values.length > 0) values += ', ';
let val = item[col.prop] || '';
if (typeof val === 'number' && !isNaN(val)) {
values += val;
} else {
val = String(val).replaceAll('\'', '\'\'');
values += "'" + val + "'";
}
});
copyData += 'insert into ' + tableNameRes + ' (' + names + ') values (' + values + ');\n';
});
return copyData;
},
update(dataCols, choiceData, condition=[], dbName, tableName) {
let tableNameRes = (!!dbName) ? dbName + '..' : '';
tableNameRes += (!!tableName) ? tableName : 'table';
// 复制为update语句
let copyData = '';
choiceData.forEach(item => {
let values = '', where = '';
dataCols.forEach(col => {
let val = item[col.prop] || '';
if (condition.indexOf(col.prop) >= 0) {
if (typeof val === 'number' && !isNaN(val)) {
if (where.length > 0) where += ' and ';
where += col.prop + ' = ' + val;
} else {
if (where.length > 0) where += ' and ';
where += col.prop + ' = ' + "'" + val + "'";
}
} else {
if (values.length > 0) values += ', ';
values += col.prop + '=';
if (typeof val === 'number' && !isNaN(val)) {
values += val;
} else {
val = String(val).replaceAll('\'', '\'\'');
values += "'" + val + "'";
}
}
});
if (where.length > 0) where = ' where ' + where;
copyData += 'update ' + tableNameRes + ' set ' + values + where + ';\n';
});
return copyData;
},
json(dataCols, choiceData, dbName, tableName) {
// 复制为json
return JSON.stringify(choiceData);
},
}