SQL编辑器自动提示库、表、列逻辑优化,更加好用
This commit is contained in:
@@ -8,6 +8,7 @@ export default {
|
||||
source: {},
|
||||
databaseInfo: {},
|
||||
tableInfo: {},
|
||||
columnInfo: {},
|
||||
lastCallbackArr: [],
|
||||
change(source) {
|
||||
this.source = source;
|
||||
@@ -19,6 +20,7 @@ export default {
|
||||
let languageTools = ace.acequire("ace/ext/language_tools");
|
||||
languageTools.addCompleter(this);
|
||||
}
|
||||
// 初始加载
|
||||
if (!!this.source.sourceId) {
|
||||
// 加载所有库
|
||||
let databaseList = this.databaseInfo[this.source.sourceId] || [];
|
||||
@@ -29,36 +31,52 @@ export default {
|
||||
}
|
||||
// 加载库下所有表
|
||||
if (!!this.source.dbName) {
|
||||
let tableList = this.tableInfo[this.source.sourceId + '_' + this.source.dbName] || [];
|
||||
let tableKey = this.source.sourceId + '_' + this.source.dbName;
|
||||
let tableList = this.tableInfo[tableKey] || [];
|
||||
if (tableList.length <= 0) {
|
||||
datasourceApi.tableList({sourceId: this.source.sourceId, dbName: this.source.dbName}).then(json => {
|
||||
this.tableInfo[this.source.sourceId + '_' + this.source.dbName] = json.data || [];
|
||||
this.tableInfo[tableKey] = json.data || [];
|
||||
});
|
||||
}
|
||||
}
|
||||
// 加载表下所有字段
|
||||
if (!!this.source.tableName) {
|
||||
let columnKey = this.source.sourceId + '_' + this.source.dbName + '_' + this.source.tableName;
|
||||
let columnList = this.columnInfo[columnKey] || [];
|
||||
if (columnList.length <= 0) {
|
||||
datasourceApi.tableColumnList({sourceId: this.source.sourceId, dbName: this.source.dbName, tableName: this.source.tableName}).then(json => {
|
||||
this.columnInfo[columnKey] = json.data.columnList || [];
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
getCompletions(editor, session, pos, prefix, callback) {
|
||||
async getCompletions(editor, session, pos, prefix, callback) {
|
||||
let callbackArr = [];
|
||||
let lineStr = session.getLine(pos.row).substring(0, pos.column - 1);
|
||||
// console.log("Executor.vue getCompletions,sourceId:" + JSON.stringify(this.source) + ', lineStr:'+ lineStr, pos);
|
||||
if (lineStr.endsWith("from ") || lineStr.endsWith("join ")) {
|
||||
this.getDatabasesAndTables(callbackArr);
|
||||
console.log("Executor.vue getCompletions,sourceId:" + JSON.stringify(this.source) + ', lineStr:' + lineStr, pos);
|
||||
if (!!this.source.tableName) {
|
||||
// 如果指定了表名,则只提示字段,其他都不用管,用在表数据查看页面
|
||||
callbackArr = await this.getAssignTableColumns(this.source.dbName, this.source.tableName);
|
||||
callback(null, callbackArr);
|
||||
} else if (lineStr.endsWith("from ") || lineStr.endsWith("join ")) {
|
||||
callbackArr = this.getDatabasesAndTables();
|
||||
this.lastCallbackArr = callbackArr;
|
||||
callback(null, callbackArr);
|
||||
} else if (lineStr.endsWith(".")) {
|
||||
this.getTablesAndColumns(callbackArr, lineStr);
|
||||
callbackArr = await this.getTablesAndColumns(lineStr);
|
||||
this.lastCallbackArr = callbackArr;
|
||||
callback(null, callbackArr);
|
||||
} else if (lineStr.endsWith("select ") || lineStr.endsWith("where ") || lineStr.endsWith("and ")) {
|
||||
this.getTableColumns(callbackArr, session, pos);
|
||||
} else if (lineStr.endsWith("select ") || lineStr.endsWith("where ") || lineStr.endsWith("and ") || lineStr.endsWith("or ")) {
|
||||
callbackArr = await this.getTableColumns(session, pos);
|
||||
this.lastCallbackArr = callbackArr;
|
||||
callback(null, callbackArr);
|
||||
} else {
|
||||
callback(null, this.lastCallbackArr);
|
||||
}
|
||||
},
|
||||
getDatabasesAndTables(callbackArr) {
|
||||
getDatabasesAndTables() {
|
||||
let callbackArr = [];
|
||||
// 所有表
|
||||
let tableList = this.tableInfo[this.source.sourceId + '_' + this.source.dbName] || [];
|
||||
tableList.forEach(item => callbackArr.push({
|
||||
@@ -77,12 +95,15 @@ export default {
|
||||
type: "snippet",
|
||||
score: 1000
|
||||
}));
|
||||
return callbackArr;
|
||||
},
|
||||
getTablesAndColumns(callbackArr, lineStr) {
|
||||
async getTablesAndColumns(lineStr) {
|
||||
let isFound = false;
|
||||
let callbackArr = [];
|
||||
// 匹配 库名. 搜索表名
|
||||
let databaseList = this.databaseInfo[this.source.sourceId] || [];
|
||||
databaseList.forEach(async item => {
|
||||
for (let i = 0; i < databaseList.length; i++) {
|
||||
let item = databaseList[i];
|
||||
if (lineStr.endsWith(item.dbName + ".")) {
|
||||
let tableList = this.tableInfo[this.source.sourceId + '_' + item.dbName] || [];
|
||||
if (tableList.length <= 0) {
|
||||
@@ -99,33 +120,20 @@ export default {
|
||||
}));
|
||||
isFound = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
// 未找到,匹配 表名. 搜索字段名
|
||||
// if (!isFound) {
|
||||
// for (let key in this.editorColumnInfo) {
|
||||
// if (!lineStr.endsWith(key + ".")) {
|
||||
// continue;
|
||||
// }
|
||||
// let columnInfo = this.editorColumnInfo[key];
|
||||
// if (!!columnInfo) {
|
||||
// for (let i = 0; i < columnInfo.length; i++) {
|
||||
// let caption = (!!columnInfo[i].description) ? columnInfo[i].name + "-" + columnInfo[i].description : columnInfo[i].name;
|
||||
// callbackArr.push({
|
||||
// caption: caption,
|
||||
// snippet: columnInfo[i].name,
|
||||
// meta: "column",
|
||||
// type: "snippet",
|
||||
// score: 1000
|
||||
// });
|
||||
// }
|
||||
// isFound = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
if (!isFound) {
|
||||
let tableList = this.tableInfo[this.source.sourceId + '_' + this.source.dbName] || [];
|
||||
for (let i = 0; i < tableList.length; i++) {
|
||||
let tableName = tableList[i].tableName;
|
||||
if (lineStr.endsWith(tableName + ".")) {
|
||||
callbackArr = await this.getAssignTableColumns(this.source.dbName, tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return callbackArr;
|
||||
},
|
||||
getTableColumns(callbackArr, session, pos) {
|
||||
return;
|
||||
let isFound = false;
|
||||
async getTableColumns(session, pos) {
|
||||
let queryText = "";
|
||||
// 往前加
|
||||
for (let i = pos.row; i >= 0; i--) {
|
||||
@@ -144,27 +152,46 @@ export default {
|
||||
}
|
||||
}
|
||||
// 所有表,找下面的字段列表
|
||||
for (let key in this.editorDbTableInfo) {
|
||||
let tableInfo = this.editorDbTableInfo[key];
|
||||
for (let i = 0; i < tableInfo.length; i++) {
|
||||
if (queryText.indexOf(tableInfo[i].tableName) < 0) {
|
||||
continue;
|
||||
}
|
||||
let columnInfo = this.editorColumnInfo[tableInfo[i].tableName];
|
||||
if (!!columnInfo) {
|
||||
for (let j = 0; j < columnInfo.length; j++) {
|
||||
let caption = (!!columnInfo[j].description) ? columnInfo[j].name + "-" + columnInfo[j].description : columnInfo[j].name;
|
||||
callbackArr.push({
|
||||
caption: caption,
|
||||
snippet: columnInfo[j].name,
|
||||
meta: "column",
|
||||
type: "snippet",
|
||||
score: 1000
|
||||
});
|
||||
}
|
||||
isFound = true;
|
||||
}
|
||||
let callbackArr = [];
|
||||
let tableList = this.tableInfo[this.source.sourceId + '_' + this.source.dbName] || [];
|
||||
for (let i = 0; i < tableList.length; i++) {
|
||||
let tableName = tableList[i].tableName;
|
||||
if (queryText.indexOf(tableName) >= 0) {
|
||||
let tempArr = await this.getAssignTableColumns(this.source.dbName, tableName);
|
||||
callbackArr = callbackArr.concat(tempArr);
|
||||
}
|
||||
}
|
||||
return callbackArr;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取指定数据表的字段
|
||||
* @param dbName
|
||||
* @param tableName
|
||||
*/
|
||||
async getAssignTableColumns(dbName, tableName) {
|
||||
let columnKey = this.source.sourceId + '_' + dbName + '_' + tableName;
|
||||
let columnList = this.columnInfo[columnKey] || [];
|
||||
if (columnList.length <= 0) {
|
||||
let res = await datasourceApi.tableColumnList({
|
||||
sourceId: this.source.sourceId,
|
||||
dbName: dbName,
|
||||
tableName: tableName
|
||||
});
|
||||
columnList = res.data.columnList || [];
|
||||
this.columnInfo[columnKey] = columnList;
|
||||
}
|
||||
let callbackArr = [];
|
||||
columnList.forEach(item => {
|
||||
let caption = (!!item.description) ? item.name + "-" + item.description : item.name;
|
||||
callbackArr.push({
|
||||
caption: caption,
|
||||
snippet: item.name,
|
||||
meta: "字段",
|
||||
type: "snippet",
|
||||
score: 1000
|
||||
});
|
||||
});
|
||||
return callbackArr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user