From c4abba361a683311c5a6386e27e68d75f50efa58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=AE=97=E6=B4=8B?= Date: Wed, 23 Aug 2023 10:15:28 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Asql=E7=BC=96=E5=86=99=E4=BD=93?= =?UTF-8?q?=E9=AA=8C=E4=BC=98=E5=8C=96=201.=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=85=B3=E9=94=AE=E5=AD=97=202.=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0=E6=B3=A8=E9=87=8A=E5=92=8C?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=203.=E7=82=B9=E5=87=BB=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0limit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mayfly_go_web/src/views/ops/db/SqlExec.vue | 33 ++++++++-- .../src/views/ops/db/component/tab/Query.vue | 15 +++++ mayfly_go_web/src/views/ops/db/lang/mysql.js | 63 +++++++++++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 mayfly_go_web/src/views/ops/db/lang/mysql.js diff --git a/mayfly_go_web/src/views/ops/db/SqlExec.vue b/mayfly_go_web/src/views/ops/db/SqlExec.vue index a8c2a0cb..78f8b137 100644 --- a/mayfly_go_web/src/views/ops/db/SqlExec.vue +++ b/mayfly_go_web/src/views/ops/db/SqlExec.vue @@ -100,6 +100,7 @@ import { defineAsyncComponent, onMounted, reactive, ref, toRefs } from 'vue'; import { ElMessage } from 'element-plus'; import { language as sqlLanguage } from 'monaco-editor/esm/vs/basic-languages/mysql/mysql.js'; +import { language as addSqlLanguage } from './lang/mysql.js'; import * as monaco from 'monaco-editor'; import { editor, languages, Position } from 'monaco-editor'; @@ -111,6 +112,10 @@ import { dbApi } from './api'; const Query = defineAsyncComponent(() => import('./component/tab/Query.vue')); const TableData = defineAsyncComponent(() => import('./component/tab/TableData.vue')); +const sqlCompletionKeywords = [...sqlLanguage.keywords, ...addSqlLanguage.keywords]; +const sqlCompletionOperators = [...sqlLanguage.operators, ...addSqlLanguage.operators]; +const sqlCompletionBuiltinFunctions = [...sqlLanguage.builtinFunctions, ...addSqlLanguage.builtinFunctions]; +const sqlCompletionBuiltinVariables = [...sqlLanguage.builtinVariables, ...addSqlLanguage.builtinVariables]; /** * 树节点类型 */ @@ -604,7 +609,7 @@ const registerSqlCompletionItemProvider = () => { }); // mysql关键字 - sqlLanguage.keywords.forEach((item: any) => { + sqlCompletionKeywords.forEach((item: any) => { suggestions.push({ label: { label: item, @@ -615,8 +620,9 @@ const registerSqlCompletionItemProvider = () => { range, }); }); + // 操作符 - sqlLanguage.operators.forEach((item: any) => { + sqlCompletionOperators.forEach((item: any) => { suggestions.push({ label: { label: item, @@ -627,9 +633,26 @@ const registerSqlCompletionItemProvider = () => { range, }); }); - // 内置函数 - sqlLanguage.builtinFunctions.forEach((item: any) => { + + let replacedFunctions = [] as string[]; + + // 添加的函数 + addSqlLanguage.replaceFunctions.forEach((item: any) => { + replacedFunctions.push(item.label) suggestions.push({ + label: { + label: item.label, + description: item.description, + }, + kind: monaco.languages.CompletionItemKind.Function, + insertText: item.insertText, + range, + }); + }); + + // 内置函数 + sqlCompletionBuiltinFunctions.forEach((item: any) => { + replacedFunctions.indexOf(item) < 0 && suggestions.push({ label: { label: item, description: 'func', @@ -640,7 +663,7 @@ const registerSqlCompletionItemProvider = () => { }); }); // 内置变量 - sqlLanguage.builtinVariables.forEach((item: string) => { + sqlCompletionBuiltinVariables.forEach((item: string) => { suggestions.push({ label: { label: item, diff --git a/mayfly_go_web/src/views/ops/db/component/tab/Query.vue b/mayfly_go_web/src/views/ops/db/component/tab/Query.vue index f944544c..869b35ab 100644 --- a/mayfly_go_web/src/views/ops/db/component/tab/Query.vue +++ b/mayfly_go_web/src/views/ops/db/component/tab/Query.vue @@ -31,6 +31,10 @@ + + + +
@@ -485,6 +489,17 @@ const replaceSelection = (str: string, selection: any) => { }); }; +const onLimit = () => { + let position = monacoEditor.getPosition() as monaco.Position; + let newText = " limit 10"; + monacoEditor?.getModel().applyEdits([ + { + range: new monaco.Range(position.lineNumber, position.column, position.lineNumber, position.column), + text: newText + } + ]); +} + /** * 导出当前页数据 */ diff --git a/mayfly_go_web/src/views/ops/db/lang/mysql.js b/mayfly_go_web/src/views/ops/db/lang/mysql.js new file mode 100644 index 00000000..a180e56c --- /dev/null +++ b/mayfly_go_web/src/views/ops/db/lang/mysql.js @@ -0,0 +1,63 @@ +// src/basic-languages/mysql/mysql.ts +var language = { + keywords: [ + "GROUP BY", + "ORDER BY", + "LEFT JOIN", + "RIGHT JOIN", + "INNER JOIN", + "SELECT * FROM", + ], + operators: [ + ], + builtinFunctions: [ + ], + builtinVariables: [], + replaceFunctions:[ // 自定义修改函数提示 + + /** 字符串相关函数 */ + { label: 'CONCAT', insertText:'CONCAT(str1,str2,...)', description: '多字符串合并' }, + { label: 'ASCII', insertText:'ASCII(char)', description: '返回字符的ASCII值' }, + { label: 'BIT_LENGTH', insertText:'BIT_LENGTH(str1)', description: '多字符串合并' }, + { label: 'INSTR', insertText:'INSTR(str,substr)', description: '返回字符串substr所在str位置' }, + { label: 'LEFT', insertText:'LEFT(str,len)', description: '返回字符串str的左端len个字符' }, + { label: 'RIGHT', insertText:'RIGHT(str,len)', description: '返回字符串str的右端len个字符' }, + { label: 'MID', insertText:'MID(str,pos,len)', description: '返回字符串str的位置pos起len个字符' }, + { label: 'SUBSTRING', insertText:'SUBSTRING(exp, start, length)', description: '截取字符串' }, + { label: 'REPLACE', insertText:'REPLACE(str,from_str,to_str)', description: '替换字符串' }, + { label: 'REPEAT', insertText:'REPEAT(str,count)', description: '重复字符串count遍' }, + { label: 'UPPER', insertText:'UPPER(str)', description: '返回大写的字符串' }, + { label: 'LOWER', insertText:'LOWER(str)', description: '返回小写的字符串' }, + { label: 'TRIM', insertText:'TRIM(str)', description: '去除字符串首尾空格' }, + /** 数学相关函数 */ + { label: 'ABS', insertText:'ABS(n)', description: '返回n的绝对值' }, + { label: 'FLOOR', insertText:'FLOOR(n)', description: '返回不大于n的最大整数' }, + { label: 'CEILING', insertText:'CEILING(n)', description: '返回不小于n的最小整数值' }, + { label: 'ROUND', insertText:'ROUND(n,d)', description: '返回n的四舍五入值,保留d(默认0)位小数' }, + { label: 'RAND', insertText:'RAND()', description: '返回在范围0到1.0内的随机浮点值' }, + + /** 日期函数 */ + { label: 'DATE', insertText:'DATE(\'date\')', description: '返回指定表达式的日期部分' }, + { label: 'WEEK', insertText:'WEEK(\'date\')', description: '返回指定日期是一年中的第几周' }, + { label: 'MONTH', insertText:'MONTH(\'date\')', description: '返回指定日期的月份' }, + { label: 'QUARTER', insertText:'QUARTER(\'date\')', description: '返回指定日期是一年的第几个季度' }, + { label: 'YEAR', insertText:'YEAR(\'date\')', description: '返回指定日期的年份' }, + { label: 'DATE_ADD', insertText:'DATE_ADD(\'date\', interval 1 day)', description: '日期函数加减运算' }, + { label: 'DATE_SUB', insertText:'DATE_SUB(\'date\', interval 1 day)', description: '日期函数加减运算' }, + { label: 'DATE_FORMAT', insertText:'DATE_FORMAT(\'date\', \'%Y-%m-%d %h:%i:%s\')', description: '' }, + { label: 'CURDATE', insertText:'CURDATE()', description: '返回当前日期' }, + { label: 'CURTIME', insertText:'CURTIME()', description: '返回当前时间' }, + { label: 'NOW', insertText:'NOW()', description: '返回当前日期时间' }, + { label: 'DATEDIFF', insertText:'DATEDIFF(expr1,expr2)', description: '返回结束日expr1和起始日expr2之间的天数' }, + { label: 'UNIX_TIMESTAMP', insertText:'UNIX_TIMESTAMP()', description: '返回指定时间(默认当前)unix时间戳' }, + { label: 'FROM_UNIXTIME', insertText:'FROM_UNIXTIME(timestamp)', description: '把时间戳格式为年月日时分秒' }, + + /** 逻辑函数 */ + { label: 'IFNULL', insertText:'IFNULL(expression, alt_value)', description: '表达式为空取第二个参数值,否则取表达式值' }, + { label: 'IF', insertText:'IF(expr1, expr2, expr3)', description: 'expr1为true则取expr2,否则取expr3' }, + { label: 'CASE', insertText:'(CASE \n WHEN expr1 THEN expr2 \n ELSE expr3) col', description: 'CASE WHEN THEN ELSE' }, + ] +}; +export { + language +};