A method and device for constructing a SQL language editor
By building a SQL parsing component and integrating the Monaco SQL language support library, we solve the problem that SQL language editors in the existing technology cannot accurately verify, realize highlighting and automatic completion of multiple SQL languages, improve development efficiency and Simplified dependency package management.
Patent Information
- Application Number
- CN202410944097.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2024-10-15
- Estimated Expiration
- 2044-07-15
AI Technical Summary
Existing SQL language editors cannot accurately highlight, prompt, verify, and report errors for each SQL language, resulting in SQL statement modification or rework, which reduces development efficiency.
Build a SQL parsing component, integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, encapsulate it as a React component, and determine whether to integrate the Monaco SQL language support library according to the target SQL language type to achieve syntax analysis and verification of multiple SQL statements. It provides highlighting, auto-completion and error reporting functions.
It implements precise checksum prompts for each SQL language, improves development efficiency, avoids unnecessary increases in dependency packages, and simplifies the development process.
Smart Images

Figure CN118484200B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of big data development, and in particular to a method and device for constructing an SQL language editor. Background Art
[0002] As big data is developing rapidly, practitioners in the big data industry, especially big data developers, often need to use simple Structured Query Language (SQL) statements. At this time, they hope to have a tool that can intuitively provide feedback to them on whether the SQL statements they entered are correct, the location of the errors, and prompts when entering SQL statements. However, existing editors are either unable to recognize the SQL language at all, or only provide general prompts for similar syntax, and are unable to accurately highlight, prompt, verify, and report errors for each SQL language being edited. This often leads to the problem that a large number of SQL statements need to be modified or reworked because syntax errors are not promptly prompted, or sometimes some errors are not for the SQL type currently being developed, greatly reducing the development efficiency of developers. Summary of the invention
[0003] The purpose of the present invention is to provide a method and device for constructing a SQL language editor, so as to solve the problem in the prior art that the editor cannot accurately highlight, prompt, check and report errors for each SQL language being edited, resulting in SQL statement modification or rework.
[0004] To achieve the above objectives, this application adopts the following technical solutions:
[0005] The method for constructing a SQL language editor of the present application comprises the following steps:
[0006] Constructing an SQL parsing component, wherein the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements;
[0007] Integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes several contribution files and worker files for a specific SQL language;
[0008] Encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration;
[0009] It is determined whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, a target SQL language editor is obtained.
[0010] Preferably, the step of constructing the SQL parsing component includes:
[0011] Create a lexical file and a grammar file, list basic data types in the lexical file, and organize the keywords corresponding to the SQL language in the grammar file;
[0012] Defining lexical rules according to the keywords, and defining grammatical rules based on the lexical rules and the basic data types;
[0013] Write the SQL statement corresponding to each grammar rule, and use Jest to verify the correctness of the SQL statement.
[0014] Preferably, the construction of the SQL parsing component further includes:
[0015] The method abstract class BasicSQL is constructed, and the BasicSQL is used to implement syntax checking, lexical analysis, entity collection and automatic completion.
[0016] Preferably, the grammatical analysis and verification of various types of SQL statements includes:
[0017] Convert the input SQL statement into an input stream of Antlr4, and generate a syntax tree according to the input stream;
[0018] Traverse the syntax tree, check whether each SQL statement conforms to the syntax rules, collect error information and specific entities, and record the association between the specific entity and its corresponding SQL statement;
[0019] Generates prompt information based on the cursor position and current grammar rules.
[0020] Preferably, the integration of the Monaco SQL language support library through the extension mechanism of the Monaco editor includes:
[0021] Create a configuration file for each SQL language and define conf constants and language constants;
[0022] Create a [sqlName].contribution.ts file, and call the registerLanguage method and the setupLanguageFeatures method in the [sqlName].contribution.ts file to register the editor and call the configuration file;
[0023] The [sqlName] class is derived from the SQL parsing component, and a SQLWorker class corresponding to the BasicSQLWorker in the SQL parsing component is created.
[0024] Preferably, the step of determining whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtaining the target SQL language editor comprises:
[0025] Determine whether the target SQL language type is included in the Monaco editor. If so, import the contribution file into the React component through the MonacoEditorWebpackPlugin plug-in and set customLanguages, where customLanguages is used to record the contribution file path and SQLWorker file path of the corresponding SQL language;
[0026] Call the LanguageIdEnum method and setupLanguageFeatures method of the Monaco SQL language support library to set the language features;
[0027] The pre-built MonacoEditor class component and MonacoDiffEditor class component are imported from the React component through import, and the call result of the LanguageIdEnum method is imported into the MonacoEditor class component or the MonacoDiffEditor class component.
[0028] Preferably, the method further comprises:
[0029] When the target SQL language type is not included in the Monaco editor, the pre-built MonacoEditor class component and the MonacoDiffEditor class component are imported from the React component through import, and the MonacoEditor class component or the MonacoDiffEditor class component is determined to be called.
[0030] A device for constructing a SQL language editor, comprising:
[0031] A first building module is used to build an SQL parsing component, and the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements;
[0032] A second building module is used to integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes a number of contribution files and worker files for a specific SQL language;
[0033] The encapsulation module is used to encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration;
[0034] The integration module is used to determine whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtain the target SQL language editor.
[0035] An electronic device comprises a memory and a processor, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement a method for constructing a SQL language editor as described in any one of the above.
[0036] A computer-readable storage medium storing a computer program, wherein the computer program enables a computer to implement a method for constructing a SQL language editor as described in any one of the above when executed.
[0037] The present invention has the following beneficial effects:
[0038] This application successfully decouples the functions by exporting each component separately, so that when developing, you only need to introduce the required language packages according to the target, without unnecessarily increasing the size of the dependent packages, which is simple and convenient. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a flowchart of a method for constructing a SQL language editor provided by an embodiment of the present invention;
[0040] Figure 2 It is a composition relationship diagram of the SQL language editor provided by an embodiment of the present invention;
[0041] Figure 3 It is a structural schematic diagram of a device for constructing a SQL language editor provided by an embodiment of the present invention;
[0042] Figure 4 It is a schematic diagram of an electronic device for implementing a method for constructing a SQL language editor provided in an embodiment of the present application. DETAILED DESCRIPTION
[0043] To make the technical solution of the present invention clearer, the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. The terms "first", "second", etc. in the claims and specification of the present application are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances. This is only to describe the distinction method adopted when describing objects with the same attributes in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that a process, method, system, product or device containing a series of units is not necessarily limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or devices.
[0044] In one embodiment, Figure 1 As shown, a method for constructing a SQL language editor is provided, comprising the following steps:
[0045] S110, constructing an SQL parsing component, wherein the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements;
[0046] S120, integrating a Monaco SQL language support library through an extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes a number of contribution files and worker files for a specific SQL language;
[0047] S130. Encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration;
[0048] S140, judging whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtaining a target SQL language editor.
[0049] In this embodiment, if Figure 2 As shown in the figure, the SQL language editor mainly consists of three parts, namely the SQL parsing component sql-parser, the Monaco SQL language support library mnonaco-sql-languages, and the React component react-monaco-editor encapsulated by the Monaco editor.
[0050] Among them, sql-parser provides syntax parsing, syntax error collection and automatic completion functions for multiple SQL languages. It uses antlr4 internally to implement syntax parsing and syntax error collection functions, and uses antlr4-c3 to implement automatic completion function. The function here is mainly collection and is not applied to the editor.
[0051] monaco-sql-languages provides language functions for multiple SQL Languages. By introducing each SQL of sql-parser to provide necessary information for the red prompt function and automatic completion function, that is, sql syntax parsing, the red prompt function and automatic completion function can be made smarter and converted into the basic settings of the editor's specific language, syntax highlighting function, error red prompt and automatic completion function.
[0052] react-monaco-editor is a ReactComponent that encapsulates the native MonacoEditor. Its main goal is to make it easier to use MonacoEditor in React. To be more specific, it makes it as easy to use MonacoEditor in React as to use a normal Form. At the same time, it opens up specific language settings, so you can use monaco-sql-languages or other default settings to update the editor.
[0053] In this embodiment, each component is exported separately for developers to use, so that the functions are successfully decoupled, so that the size of the dependent package will not be unnecessarily increased during development, and only the required language package needs to be introduced as needed.
[0054] In one embodiment, a lexical file and a grammar file are created, and basic data types are listed in the lexical file, and keywords corresponding to the SQL language are sorted in the grammar file;
[0055] Defining lexical rules according to the keywords, and defining grammatical rules based on the lexical rules and the basic data types;
[0056] Write the SQL statement corresponding to each grammar rule, and use Jest to verify the correctness of the SQL statement.
[0057] sql-parser is a project developed based on antlr4. Through the Parser, Visitor and Listener generated by antlr4, it can perform syntax validation, lexical analysis and traversal of AST nodes of SQL statements. In addition, it also provides some auxiliary methods, such as SQL splitting, automatic completion, etc. The g4 file corresponding to the SQL statement is compiled through antlr4. The compiled product is in the lib folder, which is a corresponding SQL name directory. It collects specific entities such as Catalog, Table, Database, View, Function, Procedure and Column, and collects their usage tense and creation tense accordingly, providing a basis for the subsequent automatic completion function. First, these entity types are named according to specific naming rules in advance, and they can be identified by name later. After identification, they can be prompted in specific places in SQL statements, such as the table creation statement CREATE TABLE [tableName]([columnName],...), so the place where the user needs to enter the tableName is collected in advance. When the user input cursor reaches this place, the user can be prompted that the tableName content needs to be entered here.
[0058] To this end, first create the lexical file sqlLexer.g4 and the grammar file sqlParser.g4 according to the official documentation of the SQL language, that is, convert the official documentation of the SQL language into a language that can be recognized by antlr4 and antlr4-c3, organize and list basic data types and specific characters in the lexical file. Basic data types include integers, letters, and special characters, and specific characters include comments, such as ` / * * / ` in flinkSQL and `--` in hiveSQL, both of which are written in the antlr4 grammar format; then, according to the keyword module provided by the official documentation, organize the corresponding sql keywords into the lexical file, and use 'KW_' as the beginning as an identifier. Most official SQL documents have keywords, and some official documents may require you to organize which ones are keywords, such as the table creation statement CREATE TABLE In [tableName]([columnName],...), CREATE and TABLE are keywords, which are reserved words. That is, they cannot be used as names when defining variables. At the same time, they will be assigned a corresponding fixed color when highlighting later to inform users that they are keywords. Lexical rules are created to be used in grammar rules. For example, in the table creation statement CREATETABLE [tableName] ([columnName],...), the corresponding CREATE and TABLE are keywords, which are fixed reserved words in the grammar and cannot be changed. TableName and columnName are custom variables, and their naming rules require some lexical rules. For example, tableName can support any characters such as letters and numbers except special characters. Refer to the official grammar documentation of the corresponding SQL, use antlr4 grammar to implement the corresponding grammar rules, write them to the grammar file, and name the specific entities with ${Entity}Name for easy use in the grammar.
[0059] Next, write SQL statements for each grammar rule and verify the correctness of these SQL statements through Jest unit testing.
[0060] Create a public method abstract class BasicSQL for implementing syntax checking, lexical analysis, entity collection and automatic completion functions. Based on BasicSQL, first convert the input SQL statement into the input stream of Antlr4 by calling the method of antlr4-c3, and generate a syntax tree according to the input stream, decompose the SQL statement into tokens, traverse the syntax tree, check whether each SQL statement conforms to the syntax rules, and collect the errors if they do not conform to the syntax rules and return them. In the process of checking each SQL statement, compare the type of each token, and collect the corresponding entity types that need to be collected, bind them to the attributes of the current statement, and return them. Then find the statement where the cursor is located, identify what type its syntax rule corresponds to, generate the token index according to the cursor position, and prompt what can be entered at the corresponding index according to the syntax, and then collect the prompted content and return it.
[0061] The specific functions of each SQL language are implemented by creating a specific class that inherits from BasicSQL. Taking FlinkSQL as an example, create a FlinkSQL class that inherits from BasicSQL, create a protected variable in the class, and store the rules of the entity type that needs to be collected. For example, if you want to collect TABLE entities, then the places where it appears are divided into two situations: use and creation. The creation is basically the CREATE TABLE *** statement (each SQL type is different, so don't put it in the right place). Then the syntax for creating a new table needs to be put into this array, and so on for other situations.
[0062] In one embodiment, a configuration file is created for each SQL language, and conf constants and language constants are defined;
[0063] Create a [sqlName].contribution.ts file, and call the registerLanguage method and the setupLanguageFeatures method in the [sqlName].contribution.ts file to register the editor and call the configuration file;
[0064] The [sqlName] class is derived from the SQL parsing component, and a SQLWorker class corresponding to the BasicSQLWorker in the SQL parsing component is created.
[0065] monaco-sql-languages is a SQL language project based on monaco-editor. It not only integrates SQL language functions in various big data fields, such as FlinkSQL, SparkSQL, and HiveSQL, but also integrates code highlighting, syntax checking, and automatic completion functions of sql-parser.
[0066] It first creates a configuration file for each SQL language and defines a conf constant, which is exported for external use. It declares the comments type, brackets type, autoClosingPairs type, and surroundingPairs type, etc. These configurations are derived from the configurations supported by monaco-editor itself, and the configurations corresponding to each SQL language are listed. For example, the comment type of flinkSQL is ' / * * / ', and the comment type of hiveSQL is '--', so the ones declared here are different; at the same time, a language constant is defined, the interface type is the IMonarcoLanguage type in monaco-editor, and TODO is declared in it.
[0067] The public part provides two methods: registerLanguage and setupLanguageFeatures. The corresponding language type and specific SQL configuration file are passed in through registerLanguage, and then the language type and its SQL configuration are stored in conjunction with each other; the setupLanguageFeatures method configures the corresponding configuration into the editor by calling onMessage.
[0068] When implementing the functions of each SQL language, first create a [sqlName].contribution.ts file. In this file, call the registerLanguage and setupLanguageFeatures methods to register the editor and configure the corresponding configuration file; then create a [sqlName]Worker.ts file, export the corresponding [sqlName] class from sql-parser, and create a SQLWorker class that inherits from BasicSQLWorker in sql-parser, mount a parser on its constructor this, the specific value of this parser is an instance of the [sqlName] class, export this SQLWorker class, and export a create method at the same time, which returns an instance of the SQLWorker class; finally, create a [sqlName].worker.ts file, import the corresponding SQLWorker from [sqlName]Worker.ts, mount a global listener onmessage on the window, and implement the return of a corresponding SQLWorker instance.
[0069] react-monaco-editor encapsulates monaco-editor into a React component, and the final export is still the product of monaco-editor. MonacoEditor and MonacoDiffEditor are just used directly in React framework projects in the form of React components.
[0070] Specifically, first create a new MonacoEditor.tsx file, create a MonacoEditor class component in the file, import monaco through the import * as monaco from 'monaco-editor / esm / vs / editor / editor.api' statement, mount a standard codeEditor instance editor of monaco-editor on this, create a ref instance monacoDom, and initialize monaco-editor in the componentDidMount phase. That is, if the monacoDom instance of the current monaco-editor does not exist, an error is thrown. If it exists, the monaco.editor.create method is called, and the first parameter is this.monacoDom.current, and the second parameter is all the options on props to create a mo with actual configuration. naco-eidtor; then initialize the event event, add onChange listening when the content of monaco-editor changes, and whenever the value changes, trigger the onChange of monaco-editor with the value. Similarly, onFocus, onBlur and other events also pass the value of the current instance into it to achieve the effect of monitoring the latest value; finally, in the componentWillUnmount phase, destroy the monaco-editor, call the editorWillUnMount method passed in on props, and call all dispose on the current editor at the same time, destroy the instance of the current editor, and finally export the component.
[0071] Create a new MonacoDiffEditor.tsx file, create a MonacoDiffEditor class component in the file, import the editor and IDisposable to be used from 'monaco-editor / esm / vs / editor / editor.api' through import, and mount a diffEditor instance diffEditor on monaco-editor and a ref instance created by React.createRef, as well as a global boolean variable __prevent_onChange and an IDisposable variable subscription on this; initialize monaco-editor in the componentDidMount phase, that is, call the editor.createModel method, pass the original mounted on props as the parameter, which is the original SQL content, create an editor model before modification--originalModel, and call Use the editor.createModel method, pass the value mounted on props as the parameter, which is the current SQL content, to create a modified editor model--modifiedModel, and then call the this.diffEditor.setModel method, pass an object {original:originalModel,modified:modifiedModel} as the parameter, and set the models before and after the modification respectively in this way to get a diffEditor with the before and after comparison values; at the same time, initialize the event to ensure that when some events on the diffEditor instance are triggered, the corresponding events on the monaco-editor can be accurately triggered; destroy the editor in the componentWillUnmount stage, call the editorWillUnMount method passed in on props, and call the dispose method on the editor if it still exists to destroy it, and finally export the component.
[0072] In one embodiment, it is determined whether the target SQL language type is included in the Monaco editor. If so, the contribution file is imported into the React component through the MonacoEditorWebpackPlugin plug-in, and customLanguages is set, where the customLanguages is used to record the contribution file path and SQLWorker file path of the corresponding SQL language;
[0073] Call the LanguageIdEnum method and setupLanguageFeatures method of the Monaco SQL language support library to set the language features;
[0074] The pre-built MonacoEditor class component and MonacoDiffEditor class component are imported from the React component through import, and the call result of the LanguageIdEnum method is imported into the MonacoEditor class component or the MonacoDiffEditor class component.
[0075] In this embodiment, there are two ways to use the three components. One is to use react-monaco-editor alone, and the other is to introduce the SQL language package developed in the component and then configure it in react-monaco-editor. However, no matter which one you choose, you need to introduce the monaco-editor dependency. The choice of usage depends on whether the target SQL language type is included in monaco-editor. If it is, use the first method. If not, use the second method.
[0076] The first way of using it is to import the MonacoEditor component and MonacoDiffEditor component from react-monaco-editor through import after introducing react-monaco-editor in the corresponding React project. If you only need a simple editor, you only need to call the MonacoEditor component, pass in the specific editor SQL type language to the MonacoEditor component, and you can also set value to it to change the content in the editor box. At the same time, you can pass in a specific method for onChange to implement a series of operations when the content of the edit box changes; if you need a content comparison component, call the MonacoDiffEditor component, pass in original and value to set the original content and current content, and you can also set language as the language type. The API port that can be set is the same as that of monaco-editor. However, this can only support the types supported by monaco-editor internally, and cannot accurately verify each SQL language.
[0077] The second way of using it is to first import the contribution file of the language, that is, the [sqlName].contribution.ts file created by monaco-sql-languages, which can be integrated through the monaco-editor-webpack-plugin plug-in. Set customLanguages in the MonacoEditorWebpackPlugin plug-in configuration to pass in an array. Each item in the array corresponds to a SQL language type, and each item is an object type. This object specifies three fields: label, entry, and worker. Among them, the label field is the SQL language name, and the entry is the contribution file address corresponding to the SQL language. The worker is an object, which specifies the id as the folder address corresponding to the SQL language, and the entry is the worker file address corresponding to the SQL language. If you use other methods, you need to manually introduce the corresponding contribution file, use the contribution file address of the language type corresponding to import to achieve it, and then call setupLanguage. The geFeatures method sets the language features, such as setting the auto-completion function of FlinkSql. By importing LanguageIdEnum, the corresponding SQL type enumeration, and setupLanguageFeatures, the language configuration, from monaco-sql-languages, the first parameter is passed in a certain SQL language in LanguageIdEnum, and the second parameter is passed in an object. The enable of completionItems is specified as true, indicating that auto-completion is turned on. Finally, the MonacoEditor or MonacoDiffEditor component is imported by using the method in react-monaco-editor alone, and then the LanguageIdEnum enumeration above is imported. In addition, language can be set on the MonacoEditor or MonacoDiffEditor component as a specific SQL language in the LanguageIdEnum enumeration. The final effect is that this editor can accurately verify the specified SQL language.
[0078] The present invention successfully decouples functions by exporting each component separately, so that during development, it is only necessary to introduce the required language package according to the target, and the volume of the dependent package will not be unnecessarily increased, which is simple and convenient.
[0079] In one embodiment, in order to better implement the above-mentioned method for constructing a SQL language editor, based on the method for constructing a SQL language editor, another embodiment of the present invention also provides a device for constructing a SQL language editor, such as Figure 3 As shown, the construction device of the SQL language editor includes:
[0080] A first building module is used to build an SQL parsing component, and the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements;
[0081] A second building module is used to integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes a number of contribution files and worker files for a specific SQL language;
[0082] The encapsulation module is used to encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration;
[0083] The integration module is used to determine whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtain the target SQL language editor.
[0084] The SQL language editor construction device provided in the above embodiment can implement the technical solution described in the above database code editor processing method embodiment. The specific implementation principles of the above modules or units can refer to the corresponding contents in the above database code editor processing method embodiment, which will not be repeated here.
[0085] In one embodiment, Figure 4 As shown, a computer device and an electronic device are provided, including a memory 401 and a processor 402, wherein the memory 401 is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor 402 to implement the above-mentioned multi-tenant alarm management method.
[0086] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the electronic device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0087] A computer-readable storage medium storing a computer program, wherein the computer program enables a computer to implement a multi-tenant alarm management method as described above when executed.
[0088] Exemplarily, the computer program may be divided into one or more modules / units, one or more modules / units are stored in the memory 401 and executed by the processor 402, and the I / O interface transmission of data is completed by the input interface 405 and the output interface 406 to complete the present invention. The one or more modules / units may be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program in the computer device.
[0089] The computer device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The computer device may include, but is not limited to, a memory 401 and a processor 402. Those skilled in the art may understand that this embodiment is only an example of a computer device and does not constitute a limitation on the computer device. The computer device may include more or fewer components, or a combination of certain components, or different components. For example, the computer device may also include an input device 407, a network access device, a bus, etc.
[0090] The processor 402 may be a central processing unit (CPU), or other general-purpose processors 402, digital signal processors 402 (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor 402 may be a microprocessor 402 or the processor 402 may also be any conventional processor 402, etc.
[0091] The memory 401 may be an internal storage unit of a computer device, such as a hard disk or memory of the computer device. The memory 401 may also be an external storage device of the computer device, such as a plug-in hard disk equipped on the computer device, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. Further, the memory 401 may include both an internal storage unit of the computer device and an external storage device. The memory 401 is used to store computer programs and other programs and data required by the computer device. The memory 401 may also be used to temporarily store in the output device 408, and the aforementioned storage media include various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory ROM403, a random access memory RAM404, a disk or an optical disk.
[0092] The above-mentioned embodiments only express several implementation methods of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the patent of the present invention. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.
Claims
1. A method for constructing a SQL language editor, characterized in that: The method comprises the following steps: Constructing an SQL parsing component, wherein the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements; Integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes several contribution files and worker files for a specific SQL language; Encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration; Determine whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtain the target SQL language editor; The integration of the Monaco SQL language support library through the extension mechanism of the Monaco editor includes: Create a configuration file for each SQL language and define conf constants and language constants; Create a [sqlName].contribution.ts file, and call the registerLanguage method and the setupLanguageFeatures method in the [sqlName].contribution.ts file to register the editor and call the configuration file; Export the [sqlName] class from the SQL parsing component, and create a SQLWorker class corresponding to BasicSQLWorker in the SQL parsing component; The step of determining whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtaining the target SQL language editor, includes: Determine whether the target SQL language type is included in the Monaco editor. If so, import the contribution file into the React component through the MonacoEditorWebpackPlugin plug-in and set customLanguages, where customLanguages is used to record the contribution file path and SQLWorker file path of the corresponding SQL language; Call the LanguageIdEnum method and setupLanguageFeatures method of the Monaco SQL language support library to set the language features; The pre-built MonacoEditor class component and MonacoDiddEditor class component are imported from the React component through import, and the call result of the LanguageIdEnum method is imported into the MonacoEditor class component or MonacoDiffEditor class component.
2. The method for constructing a SQL language editor according to claim 1, characterized in that: The construction of the SQL parsing component includes: Create a lexical file and a grammar file, list basic data types in the lexical file, and organize the keywords corresponding to the SQL language in the grammar file; Defining lexical rules according to the keywords, and defining grammatical rules based on the lexical rules and the basic data types; Write the SQL statement corresponding to each grammar rule, and use Jest to verify the correctness of the SQL statement.
3. The method for constructing a SQL language editor according to claim 2, characterized in that: The construction of the SQL parsing component also includes: The method abstract class BasicSQL is constructed, and the BasicSQL is used to implement syntax checking, lexical analysis, entity collection and automatic completion.
4. The method for constructing a SQL language editor according to claim 3, characterized in that: The grammatical analysis and verification of various types of SQL statements include: Convert the input SQL statement into an input stream of Antlr4, and generate a syntax tree according to the input stream; Traverse the syntax tree, check whether each SQL statement conforms to the syntax rules, collect error information and specific entities, and record the association between the specific entity and its corresponding SQL statement. The specific entity includes Catalog, Table, Database, View, Function, Procedure, and Column. Generates prompt information based on the cursor position and current grammar rules.
5. The method for constructing a SQL language editor according to claim 4, characterized in that: The method further comprises: When the target SQL language type is not included in the Monaco editor, the pre-built MonacoEditor class component and the MonacoDiffEditor class component are imported from the React component through import, and the MonacoEditor class component or the MonacoDiffEditor class component is called.
6. A device for constructing a SQL language editor, characterized in that: include: A first building module is used to build an SQL parsing component, and the SQL parsing component is used to perform syntax analysis and verification on various types of SQL statements; A second building module is used to integrate the Monaco SQL language support library through the extension mechanism of the Monaco editor, wherein the Monaco SQL language support library includes a number of contribution files and worker files for a specific SQL language; The encapsulation module is used to encapsulate the Monaco editor as a React component and allow it to specify the SQL language type through configuration; An integration module, used for determining whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtaining the target SQL language editor; The integration of the Monaco SQL language support library through the extension mechanism of the Monaco editor includes: Create a configuration file for each SQL language and define conf constants and language constants; Create a [sqlName].contribution.ts file, and call the registerLanguage method and the setupLanguageFeatures method in the [sqlName].contribution.ts file to register the editor and call the configuration file; Export the [sqlName] class from the SQL parsing component, and create a SQLWorker class corresponding to BasicSQLWorker in the SQL parsing component; The step of determining whether to integrate the Monaco SQL language support library into the React component according to the target SQL language type, and if so, obtaining the target SQL language editor, includes: Determine whether the target SQL language type is included in the Monaco editor. If so, import the contribution file into the React component through the MonacoEditorWebpackPlugin plug-in and set customLanguages, where customLanguages is used to record the contribution file path and SQLWorker file path of the corresponding SQL language; Call the LanguageIdEnum method and setupLanguageFeatures method of the Monaco SQL language support library to set the language features; The pre-built MonacoEditor class component and MonacoDiddEditor class component are imported from the React component through import, and the call result of the LanguageIdEnum method is imported into the MonacoEditor class component or MonacoDiffEditor class component.
7. An electronic device, characterized in that: It comprises a memory and a processor, wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions are executed by the processor to implement a method for constructing a SQL language editor as described in any one of claims 1 to 5.
8. A computer-readable storage medium storing a computer program, characterized in that: The computer program enables a computer to implement a method for constructing a SQL language editor as claimed in any one of claims 1 to 5 when executed.
