Structured query language formatting method and device, equipment and program product
By decomposing SQL statements into semantic units and formatting based on semantic categories and syntax states, the inefficiency and resource consumption problems caused by relying on abstract syntax trees in the prior art are solved, and more efficient SQL formatting is achieved.
Patent Information
- Application Number
- CN202510552094.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-29
- Publication Date
- 2025-08-15
AI Technical Summary
Existing SQL formatting methods rely on abstract syntax trees, resulting in inefficient processing and consume large amounts of memory and computing resources.
Decompose the original SQL statement into multiple semantic units, mark the semantic categories and syntax states of the semantic units, and format operations based on the semantic categories and syntax states to avoid relying on abstract syntax trees.
It improves processing efficiency, reduces the consumption of memory and computing resources, and adapts to complex and diverse formatted task scenarios.
Smart Images

Figure CN120492473A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a formatting method, device, equipment and program product of a structured query language. Background Art
[0002] Structured Query Language (SQL) is a language widely used to manage and query relational databases. Use SQL formatting tools to make SQL statements more clearly structured and easier to read and maintain.
[0003] Current SQL formatting tools generate an abstract syntax tree during the formatting process to facilitate syntax checking, error notifications, and semantic optimization of SQL statements. However, for simple tasks such as formatting SQL statements without syntax checking or optimization, this approach incurs unnecessary overhead, leading to low processing efficiency and consuming large amounts of memory and computing resources. Summary of the Invention
[0004] The present invention provides a structured query language formatting method, apparatus, device and program product to solve the problem that the existing SQL statement formatting method needs to rely on an abstract syntax tree, resulting in low processing efficiency and consumption of large amounts of memory and computing resources.
[0005] In a first aspect, an embodiment of the present invention provides a method for formatting a structured query language, comprising:
[0006] Decompose the original SQL statement into multiple semantic units;
[0007] marking the semantic category and grammatical status of the semantic unit;
[0008] The semantic unit in the original SQL statement is formatted according to the semantic category and the grammatical state to obtain a formatted SQL statement.
[0009] In a second aspect, an embodiment of the present invention provides a formatting device for structured query language, comprising:
[0010] Decomposition module, used to decompose the original SQL statement into multiple semantic units;
[0011] a marking module, used for marking the semantic category and grammatical status of the semantic unit;
[0012] The formatting module is used to perform a formatting operation on the semantic unit in the original SQL statement according to the semantic category and the grammatical state to obtain a formatted SQL statement.
[0013] In a third aspect, an embodiment of the present invention provides an electronic device, comprising:
[0014] at least one processor; and
[0015] a memory communicatively connected to the at least one processor; wherein,
[0016] The memory stores a computer program executable by the at least one processor. The computer program is executed by the at least one processor to enable the at least one processor to perform the formatting method of the structured query language according to any embodiment of the present invention.
[0017] In a fourth aspect, an embodiment of the present invention provides a computer program product including a computer program, wherein when the computer program is executed by a processor, the method for formatting a structured query language according to any embodiment of the present invention is implemented.
[0018] The technical solution of an embodiment of the present invention decomposes an original SQL statement into multiple semantic units; marks the semantic categories and grammatical states of the semantic units; and formats the semantic units in the original SQL statement according to the semantic categories and grammatical states to obtain a formatted SQL statement. By decomposing the SQL statement into semantic units and performing formatting operations based on the semantic categories and grammatical states of the semantic units, formatting is performed without relying on an abstract syntax tree. This solves the problem that existing SQL statement formatting methods rely on abstract syntax trees, resulting in low processing efficiency and large consumption of memory and computing resources. This improves processing efficiency and reduces memory and computing resource consumption.
[0019] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present invention, nor is it intended to limit the scope of the present invention. Other features of the present invention will become readily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0021] Figure 1 A flowchart of a formatting method for structured query language provided in the first embodiment of the present invention;
[0022] Figure 2 A flowchart of a formatting method for structured query language provided in the second embodiment of the present invention;
[0023] Figure 3 A schematic diagram of the structure of a formatting device for structured query language provided in the third embodiment of the present invention;
[0024] Figure 4 A schematic diagram of the structure of an electronic device for implementing the formatting method of structured query language according to an embodiment of the present invention. DETAILED DESCRIPTION
[0025] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.
[0026] It should be noted that the terms "first", "second", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0027] Example 1
[0028] Figure 1 This is a flowchart of a formatting method for structured query language provided in the first embodiment of the present invention. This embodiment is applicable to formatting structured query language SQL statements without using a syntax tree. The method can be executed by a formatting device for structured query language. The formatting device for structured query language can be implemented in the form of hardware and / or software. The formatting device for structured query language can be configured in an electronic device. Figure 1 As shown, the method includes:
[0029] S110: Decompose the original SQL statement into multiple semantic units.
[0030] A raw SQL statement is a SQL statement that requires formatting. It typically consists of a series of strings that are not in a standard format and are therefore less readable than standard SQL statements. A token is the smallest unit of SQL with independent meaning, capable of expressing a complete concept or operation. It is the basic building block of the language or code. A token can be an operator, punctuation mark, database object, or operator.
[0031] Specifically, the original SQL statement is decomposed into a series of ordered semantic unit tokens according to predefined segmentation markers. During the decomposition process, only the segmentation markers need to be decomposed, and no lexical and grammatical parsing is required. Therefore, the performance overhead generated by grammatical parsing can be reduced and computing resources can be saved.
[0032] For example, a string tokenizer is used to decompose the original SQL statement into a series of semantic units called tokens. The string tokenizer can decompose the string into semantic units based on a specified delimiter. The delimiter can be a single character or a combination of multiple characters, and the delimiter itself can be a semantic unit.
[0033] S120. Mark the semantic category and grammatical status of the semantic unit.
[0034] The semantic category of a semantic unit can be understood as the category of the semantic unit, and the semantic category can include at least one of whether it is an identifier, a reserved word, a keyword, and a custom category. Identifiers can be composed of characters other than punctuation marks and operations in SQL, for example, letters (az or AZ), numbers, and special symbols (such as $%@#&\"\\[\\]). Identifiers are used to name database objects (tables, columns, views, stored procedures, etc.). Reserved words are predefined words or symbols with special meanings in the SQL language. These words are reserved by the SQL language for specific grammatical structures and functions and cannot be used for other purposes. Keywords are part of the reserved words and can be understood as reserved words for key purposes. Keywords can be classified into function keywords, branch structure keywords, loop structure keywords, clause keywords, and conditional keywords according to their functions, which are not listed here one by one.
[0035] The grammatical state can be understood as the processing logic defined based on the position and function of the grammatical elements corresponding to a semantic unit. For example, it can be whether it is a SELECT option column or an IF condition column. It is understood that the grammatical state of a semantic unit is used to express the processing logic for the semantic unit and does not necessarily have actual meaning in SQL.
[0036] Specifically, by marking the semantic category and grammatical status of the semantic unit, the semantic category and grammatical status can be dynamically tracked during the formatting process of the semantic unit in the original SQL statement, so as to process the formatting task step by step.
[0037] For example, for each semantic unit decomposed from the original SQL statement, the semantic category of the semantic unit can be marked based on the content and function of the semantic unit in the SQL statement. The grammatical state of a semantic unit can be determined based on the semantic category of the semantic unit and the semantic categories of the preceding and following semantic units, or based on the grammatical states of the preceding and following semantic units. For example, if the semantic category of the semantic unit preceding a semantic unit is the clause keyword SELECT, and the semantic category of the semantic unit is an identifier and is not a reserved word, then the grammatical state of the semantic unit is a SELECT option column.
[0038] S130: Formatting the semantic units in the original SQL statement according to the semantic category and grammatical status to obtain a formatted SQL statement.
[0039] The term "formatted SQL statement" can be understood as a standard formatted SQL statement. It is understood that the standard format of the formatted SQL statement can be set by the system default, customized by the user, or a combination of the two, with some customized by the user and some using the system default. User-defined formatting can enhance the flexibility of SQL statement formatting.
[0040] Specifically, the semantic categories and grammatical states of the semantic units in the original SQL statement are tracked, and based on the processing methods for different semantic categories and grammatical states defined in the formatting processing rules, the semantic units in the original SQL statement are formatted to obtain a formatted SQL statement.
[0041] An embodiment of the present invention provides a formatting method for structured query language (SQL). The method decomposes an original SQL statement into multiple semantic units; marks the semantic categories and grammatical states of the semantic units; and formats the semantic units in the original SQL statement based on the semantic categories and grammatical states to obtain a formatted SQL statement. By decomposing the SQL statement into semantic units and performing formatting operations based on the semantic categories and grammatical states of the semantic units, formatting eliminates the need for an abstract syntax tree (AST), improves processing efficiency, and reduces memory and computing resource consumption.
[0042] Example 2
[0043] Figure 2This is a flowchart of a formatting method for a structured query language provided by the second embodiment of the present invention. This embodiment further refines the marking process of semantic categories and grammatical states, as well as the formatting operation process, based on the above embodiment. Figure 2 As shown, the method includes:
[0044] S210: Decompose the original SQL statement into multiple semantic units.
[0045] Specifically, to facilitate the classification and status tagging of semantic units, during initialization, index IDs can be assigned based on the order of the semantic units in the original SQL statement. The number of semantic unit tokens decomposed from the original SQL statement is counted: int tokensCnt = oriSqlTokens.length() . Each token is assigned an ID based on a natural number (0, 1, 2, ..., tokensCnt). oriSqlTokens is the token array obtained after the original SQL statement is decomposed, and tokensCnt is the number of semantic unit tokens.
[0046] Optionally, decomposing the original SQL statement into multiple semantic units includes: segmenting characters in the original SQL statement based on segmentation marks to obtain multiple semantic units; the segmentation marks include punctuation marks, operators, and separators.
[0047] The delimiter can include system-set delimiters and user-defined delimiters.
[0048] Specifically, the original SQL statement is segmented according to punctuation marks, operators, and separators to obtain multiple semantic units, which can be stored in a predefined array for subsequent use.
[0049] Optionally, after decomposing the original SQL statement into semantic units, the method further includes: merging adjacent semantic units that meet a merging condition into one semantic unit; the merging condition includes semantic units that need to be used in combination.
[0050] Specifically, when decomposing the original SQL statement into semantic units, it is segmented according to the delimiting markers containing punctuation, operators, and delimiters. However, some semantic units need to be used in combination. Using them individually is meaningless or fails to express the correct logic. For example, the inequality sign "!=" and the adjacent left bracket "(<[{" and right bracket ")>]}" cannot correctly express the logic if decomposed into two semantic units. There may also be some reserved words that need to be used in combination, such as GROUP BY. If the decomposition does not accurately express the meaning, it can easily lead to misreading. In addition, the tab character (\t) at the end of a paragraph can be merged into the space before the next semantic unit.
[0051] This embodiment can adapt to the grammatical information of actual SQL and further improve the readability of SQL statements by subsequently formatting adjacent semantic units that need to be used in combination as a whole.
[0052] S220 , for each semantic unit, mark the semantic category of the semantic unit according to a semantic category condition; the semantic category includes whether it is an identifier, whether it is a reserved word, whether it is a keyword, and whether it is a user-defined category.
[0053] The semantic category condition can be understood as a condition for identifying the semantic category of a semantic unit. Semantic category conditions can include conditions for identifying whether a semantic unit is an identifier, a reserved word, a keyword, or a user-defined category.
[0054] Exemplarily, the condition for identifying whether a semantic unit is an identifier can use a regular expression, such as identifierPattern = Pattern.compile("^\"?([a-zA-Z0-9_$%@#&\"\\[\\]])+(\\.+([a-zA-Z0-9_$%@#&\"\\[\\]])+)*\"?$",Pattern.CASE_INSENSITIVE). The condition for identifying whether a semantic unit is a reserved word can be a predefined reserved word list, such as ReservedWordsArr = {"select","from","where","having",...}; there are many reserved words in SQL, and the reserved word lists of different databases are slightly different, which are not listed here one by one. The condition for identifying whether a semantic unit is a keyword can be a predefined keyword list. For example, the clause keyword list ClauseKeywordArr = {"select", "from", "where", ...}, the condition keyword list ConditionKeywordArr = {"and", "or", ...}, the function keyword list FunctionKeywordArr = {"create function", "returns", "end", ...}, the branch structure keyword list BranchKeywordArr = {"case", "if", "else", ...}, the loop structure keyword list LoopKeywordArr = {"while", "do", "loop", ...}, etc. A custom category can be defined by defining some identifiers as a category, for example, Type1 = {select, from, where}; Type2 = {aaaaa, bbbbb, select}; Type3 = {aaaa, cccc, dddddd}, etc.
[0055] The semantic category of the semantic unit can also be predefined, and a unified naming can be used to represent universality, such as isIdentifierToken=ture(T) / false(F) to indicate whether the semantic unit is an identifier, isReservedToken=ture(T) / false(F) to indicate whether the semantic unit is a reserved word; isTokenTypeTN=ture(T) / false(F) to indicate whether the semantic category of the semantic unit is TN, such as tokenTypeClauseKeyword=ture(T) / false(F) to indicate whether it is a clause keyword.
[0056] Specifically, the semantic category of each semantic unit in the original SQL statement is identified in turn, and the semantic category is marked as whether it is an identifier, a reserved word, a keyword, or a user-defined category.
[0057] S230 : For each semantic unit, mark the grammatical status of the semantic unit according to at least two of the semantic category of the semantic unit, the semantic category of an adjacent semantic unit of the semantic unit, and the grammatical status of an adjacent semantic unit of the semantic unit.
[0058] The grammatical status can be customized by the user according to grammatical or formatting requirements and in combination with grammatical element category information.
[0059] Specifically, the way to mark the grammatical status of a semantic unit can be to mark the grammatical status of the semantic unit according to the semantic category of the semantic unit and the semantic category of the adjacent semantic units of the semantic unit; or the way to mark the grammatical status of the semantic unit can be to mark the grammatical status of the semantic unit according to the semantic category of the semantic unit and the grammatical status of the adjacent semantic units of the semantic unit; or the way to mark the grammatical status of the semantic unit can be to mark the grammatical status of the semantic unit according to the semantic category of the adjacent semantic units of the semantic unit and the grammatical status of the adjacent semantic units of the semantic unit.
[0060] Optionally, marking the grammatical state of a semantic unit according to at least two of the semantic category of the semantic unit, the semantic category of an adjacent semantic unit of the semantic unit, and the grammatical state of the adjacent semantic unit of the semantic unit may also be: marking at least one first grammatical state of the semantic unit according to at least two of the semantic category of the semantic unit, the semantic category of an adjacent semantic unit of the semantic unit, and the grammatical state of the adjacent semantic unit of the semantic unit; determining the second grammatical state of the semantic unit according to at least one of the semantic category of the semantic unit, the semantic category of an adjacent semantic unit of the semantic unit, and the grammatical state of the adjacent semantic unit of the semantic unit and at least one of the first grammatical states, and determining the grammatical state of the semantic unit as the sum of the first grammatical state and the second grammatical state. For example, determining the first grammatical state of the semantic unit as in a SELECT statement, and determining the second grammatical state of the semantic unit as a SELECT option column in combination with the semantic category of the semantic unit.
[0061] For example, the grammatical state can be named using stateTypeXxx, such as the identifier between semantic units of semantic categories T1 and T2 is marked as stateTypeS1; the identifier between semantic units of semantic category T3 is marked as stateTypeS2.
[0062] S240. For the current semantic unit in the original SQL statement, based on the formatting processing rules, format the current semantic unit according to the semantic category and grammatical state of the current semantic unit; the formatting operation includes at least one of a case operation, an indentation operation, and a line break operation.
[0063] The formatting rules can be understood as the rules used to format SQL statements. They can be set by system default or partially customized by the user. For example, the user can define whether to use uppercase or lowercase letters or the number of characters to indent special lines. The current semantic unit can be understood as the currently selected semantic unit, that is, the current object of the formatting operation.
[0064] Specifically, based on pre-configured and user-defined formatting processing rules, formatting operations such as case operations, indentation operations, and line breaks are determined according to the semantic category and grammatical status marked by the current semantic unit, and the format of the current semantic unit is adjusted based on the formatting operations, thereby achieving formatting of the current semantic unit.
[0065] As an optional implementation of this embodiment, the formatting operation on the current semantic unit based on the formatting processing rule according to the semantic category and grammatical state of the current semantic unit includes:
[0066] S241: If the semantic category of the current semantic unit is a reserved word or the grammatical state of the current semantic unit is a first predefined grammatical state, perform a case operation on the current semantic unit based on the case rule in the formatting processing rule.
[0067] The first predefined grammatical state can be understood as a grammatical state that requires a forced uppercase operation or a forced lowercase operation. The capitalization rule can be a forced uppercase operation or a forced lowercase operation, and can be set by the user or the system.
[0068] Specifically, based on the case rules in the formatting processing rules, case manipulation is performed on the current semantic unit whose semantic category is marked as a reserved word, so that all reserved words in the original SQL statement are unified into uppercase or lowercase, thereby improving neatness. Based on the case rules in the formatting processing rules, case manipulation is performed on the current semantic unit whose grammatical state is a first predefined grammatical state, thereby standardizing the case of semantic units in specific positions or states in the original SQL statement, further improving neatness.
[0069] S242: If the semantic category of the current semantic unit is a predefined keyword, perform an indentation operation and / or a line break operation on the current semantic unit based on a paragraph rule in the formatting processing rule.
[0070] Paragraph rules may include rules regarding line breaks and indentation values, which may be user-defined or configured by the system by default.
[0071] Specifically, for the current semantic unit whose semantic category is a predefined keyword, an indentation operation and / or a line break operation is performed so that each predefined keyword occupies a line alone or is indented to a certain extent, which can directly and clearly display the keyword information in the SQL statement and is conducive to expressing the structural relationship of the SQL statement.
[0072] S243. If the grammatical state of the current semantic unit is the second predefined grammatical state, then based on the paragraph rule in the formatting processing rule, an alignment operation is performed on the current semantic unit according to the number of blank characters and line breaks before the previous semantic unit; the alignment operation includes an indentation operation and a line break operation; the previous semantic unit is the semantic unit before the current semantic unit.
[0073] Among them, the second predefined state can be understood as a grammatical state that requires alignment operation. The alignment operation can be implemented through indentation operation and line break operation. The previous semantic unit is the semantic unit before the current semantic unit. For example, if the current semantic unit is the semantic unit token[i] with index number i, then the previous semantic unit is the semantic unit of the semantic unit token[i-1] with index number i-1. It can be understood that the first semantic unit does not require alignment operation. The number of blank characters before the previous semantic unit can be understood as the number of blank characters between the previous semantic unit and the previous semantic unit (a semantic unit before the previous semantic unit).
[0074] Specifically, in order to determine the alignment position of the current semantic unit, it is necessary to obtain the number of blank characters and line breaks before the previous semantic unit of the current semantic unit; based on the paragraph rules in the formatting processing rules, the current semantic unit of the second predefined grammatical state is indented and wrapped according to the number of blank characters and line breaks, so that the current semantic unit is aligned with the previous semantic unit.
[0075] It is understandable that after the formatting operation is performed on the semantic unit, the number of blank characters and line breaks before the semantic unit may change. Therefore, each time the alignment operation is performed on the current semantic unit, the number of blank characters and line breaks before the previous semantic unit needs to be re-acquired.
[0076] Optionally, performing an alignment operation on the current semantic unit based on the paragraph rule in the formatting processing rule and according to the number of blank characters and line breaks before the previous semantic unit includes:
[0077] A. If the grammatical state of the previous semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is greater than zero, an alignment operation is performed on the current semantic unit according to the number of blank characters before the previous semantic unit to align the position of the current semantic unit with that of the previous semantic unit.
[0078] Specifically, if the grammatical state of the previous semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is greater than zero, the previous semantic unit is an independent line. Therefore, the character distance between the previous semantic unit and the beginning of the line is the number of blank characters before the previous semantic unit. A line break operation is performed on the current semantic unit, and an indentation operation is performed based on the number of blank characters before the previous semantic unit to align the current semantic unit with the previous semantic unit.
[0079] B. If the grammatical state of the previous semantic unit of the current semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is equal to zero, then an alignment operation is performed on the current semantic unit according to the character distance between the previous semantic unit and the beginning of the line, so that the position of the current semantic unit is aligned with that of the previous semantic unit; the character distance between the previous semantic unit and the beginning of the line is the sum of the character lengths of all semantic units between the previous semantic unit and the beginning of the line and the number of blank characters.
[0080] Specifically, if the grammatical state of the previous semantic unit of the current semantic unit is the same as the grammatical state of the current semantic unit, both being the second predefined grammatical state, and the number of line breaks before the previous semantic unit is zero, it means that the previous semantic unit has no line break, and there is a semantic unit before the same line of the previous semantic unit. The current semantic unit needs to be aligned with the starting character position of the previous semantic unit, but since there are other semantic units before the same line of the previous semantic unit, it is not the first semantic unit in a line, so the character distance between the previous semantic unit and the beginning of the line cannot be determined based on the number of blank characters before the previous semantic unit; instead, it is determined based on the sum of the character lengths and the number of blank characters of all semantic units between the previous semantic unit and the beginning of the line. Perform a line break operation on the current semantic unit, and include an indentation operation based on the character distance between the previous semantic unit and the beginning of the line, so that the current semantic unit is aligned with the position of the previous semantic unit.
[0081] Furthermore, if the grammatical state of the preceding semantic unit of the current semantic unit is not the second predefined grammatical state, that is, the grammatical state of the preceding semantic unit is different from the grammatical state of the current semantic unit, this indicates that the current semantic unit may be the first semantic unit whose grammatical state is the second predefined grammatical state. Generally, no alignment or formatting operations are required. Of course, user-defined formatting operations may also be performed, which will not be described in detail in this embodiment.
[0082] S250: Obtain the next semantic unit in the original SQL statement as the current semantic unit.
[0083] Specifically, the semantic units are generally formatted in the order in which they appear in the original SQL statement. After assigning index numbers to the semantic units, the semantic units are sequentially retrieved according to their index numbers as the current semantic unit. If the current semantic unit is the semantic unit token[i] with index i, after completing the formatting operation on token[i], the semantic unit token[i+1] with index i+1 is used as the current semantic unit.
[0084] S260: Return to the steps of formatting the current semantic unit based on the formatting processing rules and the semantic category and grammatical state of the current semantic unit, until all the semantic units in the original SQL statement are traversed and a formatted SQL statement is output.
[0085] Specifically, after determining the new current semantic unit, the process returns to S240 and performs a formatting operation on the current semantic unit based on the formatting processing rule and the semantic category and grammatical state of the current semantic unit.
[0086] An embodiment of the present invention provides a formatting method for a structured query language, comprising: decomposing an original SQL statement into multiple semantic units; marking the semantic category of each semantic unit according to a semantic category condition; marking the grammatical status of each semantic unit according to the semantic category of the semantic unit, the semantic category of an adjacent semantic unit, or the grammatical status of an adjacent semantic unit; performing a formatting operation on the current semantic unit in the original SQL statement according to the semantic category and grammatical status of the current semantic unit based on a formatting processing rule; obtaining the next semantic unit in the original SQL statement as the current semantic unit; and returning to the steps of formatting the current semantic unit according to the semantic category and grammatical status of the current semantic unit based on the formatting processing rule until all semantic units in the original SQL statement are traversed and a formatted SQL statement is output. By decomposing the SQL statement into semantic units and performing formatting operations based on the semantic categories and grammatical status of the semantic units, formatting does not require reliance on an abstract syntax tree, thereby improving processing efficiency and reducing memory and computing resource consumption. Furthermore, dynamic tracking of the semantic categories and grammatical status of semantic units during the formatting operation can adapt to complex and diverse formatting task scenarios such as nested queries and keyword processing.
[0087] Next, we will describe the specific process of implementing the structured query language formatting method in a computer, using the aforementioned embodiments. The first step is to initialize the parsing environment. This process aims to integrate dialect-specific extensions: For SQL formatting tools, it is necessary to expand the reserved word and keyword arrays to recognize and process syntax, functions, and operators unique to different database systems. Define any variables that may be used. For example, Table 1 shows a variable definition table.
[0088] Table 1
[0089] variable name describe formatSqlBuilder Array to store formatted results reservedWordsArr Reserved word array identifierPattern Regular expressions for identifiers tokenTypeTXArr Custom semantic category array stateTypeSX Custom grammar status array
[0090] Through variable definitions, features from different dialects can be easily integrated to support a wide range of SQL standards and custom configurations: 1) Create a StringBuilder object, formatSqlBuilder, to store the formatted result. 2) Set SQL identifiers, reserved words, and regular expressions, as well as specific SQL token categories. The identifier regular expression can be: identifierPattern = Pattern.compile("^\"?([a-zA-Z0-9_$%@#&\"\\[\\]])+(\\.+([a-zA-Z0-9_$%@#&\"\\[\\]])+)*\"?$",Pattern.CASE_INSENSITIVE); the reserved word array can be: reservedWordsArr = {"select","from","where","having",...}; reserved words can include clause keywords, condition keywords, function keywords, branch structure keywords, and loop structure keywords. SQL has a large number of reserved words and keywords, and these differ slightly from database to database. These can be configured based on your specific environment and are not listed here. Various keywords can be stored in an array named tokenTypeTXArr, such as ClauseKeywordArr for storing clause keywords; FunctionKeywordArr for storing function keywords.
[0091] The second step is to decompose the semantic units of SQL statements.
[0092] (1) Initialize relevant data structures.
[0093] To decompose the semantic unit token of SQL statements using a lightweight tokenization mechanism, it is necessary to define the variables described in Table 2.
[0094] Table 2
[0095] variable name Meaning description unformattedSQL Initial SQL statement oriSqlTokens Initial semantic unit array customCharacter Custom separator tokensCnt Number of semantic units sqlTokens Semantic unit array after processing whitespace characters preWhitespace Number of whitespace characters before a semantic unit preLbs Number of line breaks before a semantic unit isReservedToken Indicates whether the semantic unit is a reserved word isTokenTypeTX Indicates whether the semantic unit is of TX semantic category isStateTypeSX Indicates whether the semantic unit is in SX syntax state
[0096] The initial SQL statement is used as the input statement and is defined as unformattedSQL = inputSQL;
[0097] The decomposed semantic unit tokens are stored in the initial semantic unit array, which is defined as oriSqlTokens;
[0098] The separator can include a user-defined separator, defined as customCharacter.
[0099] Number of semantic units: int tokensCnt = oriSqlTokens.length();
[0100] Process the token array after the blank character: sqlTokens = new String[tokensCnt];
[0101] Formatting context array: number of blank characters before the semantic unit: preWhitespace = new int[tokensCnt];
[0102] The number of line breaks before a semantic unit: preLbs = new short [tokensCnt].
[0103] Semantic category identifier array:
[0104] Identify whether the semantic unit is a reserved word: isReservedToken = new boolean[tokensCnt];
[0105] Customized semantic category identification array: isTokenTypeTX = new boolean[tokensCnt], used to identify whether the semantic unit is of the TX semantic category.
[0106] Syntax state flag array:
[0107] Customized syntax state identification array: isStateTypeSX = new boolean[tokensCnt], used to identify whether the semantic unit is in the SX syntax state.
[0108] (2) Initialize the SQL semantic unit context
[0109] Define the number of blank characters before the next non-empty semantic unit: nbrOfSpaces; the number of line breaks before the next non-empty semantic unit: nbrOfLinebreaks;
[0110] The execution steps are as follows: 1) Split the inputSQL into tokens through the string tokenizer StringTokenizer to obtain the initial semantic unit array oriSqlTokens after segmentation; 2) Read the token with index idx from oriSqlTokens: idxToken = oriSqlTokens[idx]; i identifies the i-th non-empty token to be processed; 3) Identify and process whitespace characters (\n\b\f), and count the number of spaces nbrOfSpaces and line breaks nbrOfLinebreaks: If the current word If the current character is "\", "\f", or "\b": nbrOfSpaces++, idx++, jump to step 2); if the current character is "\n": nbrOfLinebreaks++, nbrOfSpaces = 0, idx++, jump to step 2); if the current character is not a blank character, idx++, sqlTokens[i] = oriSqlTokens[idx], preWhitespace[i] = nbrOfSpaces, preLbs[i] = nbrOfLinebreaks, i++, jump to step 2). 4) Repeat the above steps until all semantic units are processed.
[0111] The third step is to mark the semantic category and grammatical status of the semantic unit.
[0112] (1) Merge and adjust adjacent semantic units. Traverse the sqlTokens array and merge some tokens that need to be used in combination into one token. For example, merge some symbol combinations: :=, *=, !=, <>, (), merge the tab character (\t) into the space before the next token, etc.
[0113] (2) Identify the semantic category of the semantic unit.
[0114] Traverse the sqlTokens array and identify the semantic category of each semantic unit token. Read the token with index idx from sqlTokens: idxToken = sqlTokens[idx]. For the semantic unit token with index idx, the following processing can be performed: determine whether the current semantic unit is an identifier: isIdentifierToken[idx] = true / false; determine whether the current semantic unit is a reserved word: isReservedToken[idx] = true / false; determine whether the current semantic unit is of semantic category TX: isTokenTypeTX[idx] = true / false; repeat the above steps until all tokens are traversed.
[0115] (3) Identify the grammatical status of the semantic unit.
[0116] Traverse the sqlTokens array and identify the syntax state of each semantic unit token. Read the token with index idx from sqlTokens: idxToken = sqlTokens[idx]. For the token with index idx, the processing flow can be: determine whether idxToken is in the stateTypeSX state, and continue until all tokens are processed.
[0117] Exemplarily, the stateTypeS1 state can meet the following two conditions: a. idxToken is an identifier, that is, idxToken needs to satisfy the identifierPattern regular match; b. idxToken is between tokens of token category T1 and token category T2, that is: the previous token exists and is of token category T1: isTokenTypeT1[idx-1]=true; the latter token exists and is of token category T2: isTokenTypeT2[idx+1]=true; if the above two conditions are met, isStateTypeS1[idx]=true, otherwise, isStateTypeS1[idx]=false.
[0118] Alternatively, the stateTypeS2 state can meet the following two conditions: a. idxToken is an identifier, that is, idxToken must satisfy the identifierPattern regular expression. b. idxToken is between tokens of token type T3, that is, if the previous token exists and is of token type T3, isTokenTypeT3[idx-1] = true; if the next token exists and is of token type T3, isTokenTypeT3[idx+1] = true. If both of these conditions are met, isStateTypeS2[idx] = true; otherwise, isStateTypeS2[idx] = false.
[0119] It is understandable that the conditions satisfied by the stateTypeS1 state and the stateTypeS2 state are only examples, and the conditions satisfied by the grammatical state can be configured according to format requirements or customized by the user.
[0120] Step 4: Formatting operations based on rule-driven.
[0121] According to the semantic category and grammatical status of the semantic unit, formatted SQL is output based on the formatting rules.
[0122] (1) Custom formatting rules, including but not limited to: capitalization rules, indentation rules, line breaking rules and alignment rules. For example, R1 means that reserved words are forced to be uppercase; R2 means that the token of category tokenTypeT1Arr is wrapped before the line. The token of category KeywordsT2Arr is wrapped before the line, and a preset number of spaces (such as 4) are added as indentation; for the token of stateTypeS1, if the token is an identifier and it is not the first identifier, the token is wrapped and aligned with the first identifier; the token of stateTypeS1 is forced to be lowercase.
[0123] (2) Output formatted SQL
[0124] For custom formatting rules, traverse sqlTokens to process each token. idxToken is the token with index idx. The processing steps can be: P1: If idxToken is a reserved word: isReservedWords[idx] = true, then capitalize it. P2: If idxToken is of tokenTypeT1 and is not the first syntax element, that is, isTokenTypeT1[idx] = true && idx! = 0, then add a line break before idxToken: preLbs[idx] + = 1. P3: If idxToken is of tokenTypeT2 and is not the first syntax element, then add a line break and four spaces as indentation: preLbs[idx] + = 1, preWhitespace[idx] + = 4. P4: If idxToken is in stateTypeS1, that is, isStateTypeS1[idx] = true; calculate the alignment position, wrap the line, and align with the previous keyword. P5: If idxToken is in state stateTypeS2, that is, isStateTypeS2[idx] = true, then lowercase...; store the formatted SQL until all tokens are processed.
[0125] The following example uses the SQL query "select col1, col2, col3 from table1 where col1 = 1 and col2 > = 1 or col3 ! = 1" to explain how to format Structured Query Language (SQL). This SQL statement consists of the select clause, the from clause, and the where clause, and contains the reserved words select, from, where, and, and or. The reserved word "select," which begins a clause, is called a clause keyword. The three query conditions are connected by the reserved words and and or, which are called condition keywords.
[0126] The formatting steps are as follows:
[0127] (1) Enter the initial SQL statement, inputSQL="select col1,col2,col3 from table1 where col1=1and col2>=1or col3!=1;".
[0128] (2) Using the string tokenizer StringTokenizer to split the inputSQL into semantic units tokens, we can get the number of semantic units tokensCnt = 23; the semantic unit array after processing whitespace characters sqlTokens = {"select","col1",","col2",",","col3","from","table1","where","col1","=","1","and","col2",">","=","1","or","col3","!","=","1",";"}; the number of whitespace characters before each semantic unit preWhitespace = {_,1,_,1,_,1,1,1,1,1,1,1,1,1,1,_,1,1,1,_,1,1}; the number of line breaks before each semantic unit preLbs = {_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_}.
[0129] (3) Merge and adjust adjacent tokens, merge the adjacent semantic units “>” and “=” into “>=”, and merge the adjacent semantic units “!” and “=” into one semantic unit “!=”. After merging, the semantic unit array sqlTokens = {"select","col1",","col2",",","col3","from","table1","where","col1","=","1","and","col2",">=","1","or","col3","!=","1",";"}; the number of semantic units tokensCnt = 21; the number of blank characters before each semantic unit preWhitespace = {_,_,1,_,1,_,1,1,1,1,1,1,1,1,1,1,1,1,1,1,_}; the number of line breaks before each semantic unit preLbs = {_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_}.
[0130] (4) Identify the semantic category of the semantic unit.
[0131] Traverse sqlTokens and process each token. Define idxToken to represent the token with index idx. The specific processing steps for idxToken are as follows:
[0132] a. Determine whether the current semantic unit is a reserved word, ie, isReservedToken[idx] = reservedWordsArr.contains(idxToken);
[0133] b. Determine whether the current semantic unit is a clause keyword, ie, isTokenTypeClauseKeyword[idx] = tokenTypeClauseKeywordArr.contains(idxToken);
[0134] c. Determine whether the current semantic unit is a conditional keyword, that is, isTokenTypeClauseKeyword[idx]=tokenTypeClauseKeywordArr.contains(idxToken).
[0135] d. Repeat steps a to c until all tokens are processed.
[0136] The token result for reserved words in SQL is: isReservedToken = {T,F,F,F,F,F,T,F,T,F,F,F,F,T,F,F,F,F,F};
[0137] The token result for the SQL semantic category of clause keyword is isTokenTypeClauseKeyword = {T,F,F,F,F,F,T,F,T,F,F,F,F,F,F,F,F,F,F,F}.
[0138] The token result for the SQL semantic category of condition keyword is isTokenTypeConditionKeyword = {F,F,F,F,F,F,F,F,F,F,T,F,F,F,T,F,F,F,F,F}.
[0139] (4) Identify the grammatical status of the semantic unit.
[0140] In the above example, the grammatical status of the semantic unit includes whether it is a SELECT option column.
[0141] tokenIsSelectList=false: This variable is used to identify whether the next token may be a SELECT query column.
[0142] The state information array of the token isStateTypeSelectList is initialized with a default value of false.
[0143] Traverse sqlTokens and process each token. idxToken represents the token with index idx. The specific processing steps are as follows:
[0144] a. Read the current semantic unit: idxToken = sqlTokens[idx]
[0145] b. If the current semantic unit is a clause keyword: isTokenTypeClauseKeyword[idx] = true:
[0146] idxToken=="select": tokenIsSelectList=true, jump to process a;
[0147] idxToken!= "select": tokenIsSelectList=false, idx++, jump to process a;
[0148] c. If tokenIsSelectList = true, and idxToken satisfies the identifierPattern regular expression, and isReservedToken[idx] = false, then idxToken is a SELECT option column: isStateTypeSelectList[idx] = true;
[0149] d.idx++, repeat the above operation until all semantic units are traversed.
[0150] The result of marking the SELECT option column in the SQL syntax state is: isStateTypeSelectList = {F,T,F,T,F,T,F,F,F,F,F,F,F,F,F,F,F,F,F,F}.
[0151] (5) Format SQL output.
[0152] Users can set, but are not limited to, the following formatting rules: R1: Capitalize reserved words; R2: Wrap before clause keywords; R3: Wrap before condition keywords and add 4 spaces as indentation; R4: Select query columns, only display one column per row, and align them. Based on the above formatting rules, the formatting process for semantic units is as follows:
[0153] Traverse sqlTokens to process each token. For the token with index idx, that is, idxToken, the corresponding formatting process is:
[0154] P1: If idxToken is a reserved word: isReservedWords[idx] = T, convert it to uppercase;
[0155] P2: If idxToken is a clause keyword and is not the first syntax element, add a line break before it, preLbs[idx] += 1;
[0156] P3: If idxToken is a conditional keyword, add a line break and add 4 spaces as indentation, preLbs[idx]+=1, preWhitespace[idx]+=4;
[0157] P4: If idxToken is a SELECT option column: isSelectListToken[idx] = T.
[0158] Except for the first identifier, the remaining identifiers are wrapped and aligned with the previous identifier. The specific processing steps are as follows:
[0159] If the index of the previous identifier lastIdentifierToken is lastIdentifierIdx, and the previous identifier also belongs to the select option column: isSelectListToken[lastIdentifierIdx] = T, then the current idxToken is wrapped and aligned with lastIdentifierToken. Calculate the alignment position alignPos of idxToken: if preLbs[lastIdentifierIdx]>0: alignPos = preWhitespace[lastIdentifierIdx]; otherwise, it means that lastIdentifierToken is the first column of the select option column, and calculate the distance from lastIdentifierToken to the beginning of the row: the distance from lastIdentifierToken to the beginning of the row is the sum of the character lengths of all semantic units between the previous semantic unit and the beginning of the row and the number of whitespace characters.
[0160] Output and store formatted SQL:
[0161] formatSql="SELECT col1,\n_______col2,\n_______col3\nFROM table1\nWHERE col1=1\n____and col2>=1\n____or col3!=1;";preWhitespace={_,1,_,7,_,7,_,1,_,1,1,1,4,1,1,1,4,1,1,1,_};preLbs={_,_,_,1,_,1,1,_,1,_,_,_,1,_,_,_,1,_,_,_};Underline indicates space.
[0162] The formatted SQL for the initial SQL statement "select col1,col2,col3 from table1 where col1=1and col2>=1or col3!=1" is as follows:
[0163] “
[0164] SELECT col1,
[0165] col2,
[0166] col3
[0167] FROM table1
[0168] WHERE col1=1
[0169] and col2>=1
[0170] or col3!=1;
[0171] ”.
[0172] Example 3
[0173] Figure 3 This is a schematic diagram of a formatting device for a structured query language provided by the third embodiment of the present invention. Figure 3 As shown, the device includes: a decomposition module 310, a marking module 320 and a formatting module 330; wherein,
[0174] Decomposition module 310, for decomposing the original SQL statement into multiple semantic units;
[0175] a marking module 320 for marking the semantic category and grammatical status of the semantic unit;
[0176] The formatting module 330 is configured to perform a formatting operation on the semantic unit in the original SQL statement according to the semantic category and the grammatical state to obtain a formatted SQL statement.
[0177] An embodiment of the present invention provides a formatting device for structured query language (SQL). The device decomposes an original SQL statement into multiple semantic units; marks the semantic categories and grammatical states of the semantic units; and formats the semantic units in the original SQL statement based on the semantic categories and grammatical states to obtain a formatted SQL statement. By decomposing the SQL statement into semantic units and performing formatting operations based on the semantic categories and grammatical states of the semantic units, the device eliminates the need for formatting through an abstract syntax tree (AST), improves processing efficiency, and reduces the consumption of memory and computing resources.
[0178] Optionally, the marking module 320 is specifically configured to:
[0179] For each semantic unit, the semantic category of the semantic unit is marked according to the semantic category condition; the semantic category includes whether it is an identifier, whether it is a reserved word, whether it is a keyword, and whether it is a custom category;
[0180] For each semantic unit, the grammatical state of the semantic unit is marked according to at least two items of the semantic category of the semantic unit, the semantic category of the adjacent semantic unit of the semantic unit, and the grammatical state of the adjacent semantic unit of the semantic unit.
[0181] Optionally, also include:
[0182] The merging module is used to merge adjacent semantic units that meet the merging conditions into one semantic unit after decomposing the original SQL statement into semantic units and before marking the semantic categories of the semantic units according to the semantic category conditions; the merging conditions include the semantic units that need to be combined for use.
[0183] Optionally, the formatting module 330 includes:
[0184] a formatting unit configured to perform a formatting operation on a current semantic unit in the original SQL statement based on a formatting processing rule and in accordance with the semantic category and grammatical state of the current semantic unit; the formatting operation comprising at least one of a case-sensing operation, an indentation operation, and a line-wrapping operation;
[0185] A semantic unit determining unit, configured to obtain the next semantic unit in the original SQL statement as the current semantic unit;
[0186] A loop execution unit is used to return to the relevant steps of formatting the current semantic unit based on the formatting processing rules according to the semantic category and grammatical state of the current semantic unit, until the semantic units in the original SQL statement are traversed and the formatted SQL statement is output.
[0187] Optionally, the formatting unit includes:
[0188] a case operation subunit, configured to perform a case operation on the current semantic unit based on the case rule in the formatting processing rule if the semantic category of the current semantic unit is a reserved word or the grammatical state of the current semantic unit is a first predefined grammatical state;
[0189] a paragraph processing sub-unit, configured to perform an indentation operation and / or a line break operation on the current semantic unit based on the paragraph rule in the formatting processing rule if the semantic category of the current semantic unit is a predefined keyword;
[0190] An alignment operation sub-unit is used to perform an alignment operation on the current semantic unit based on the paragraph rules in the formatting processing rules and according to the number of blank characters and line breaks before the previous semantic unit if the grammatical state of the current semantic unit is the second predefined grammatical state; the alignment operation includes an indentation operation and a line break operation; the previous semantic unit is the semantic unit before the current semantic unit.
[0191] Optionally, the alignment operation subunit is specifically used to:
[0192] If the grammatical state of the previous semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is greater than zero, performing an alignment operation on the current semantic unit according to the number of blank characters before the previous semantic unit, so that the current semantic unit is aligned with the position of the previous semantic unit;
[0193] If the grammatical state of the previous semantic unit of the current semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is equal to zero, then an alignment operation is performed on the current semantic unit according to the character distance between the previous semantic unit and the beginning of the line, so that the position of the current semantic unit is aligned with that of the previous semantic unit; the character distance between the previous semantic unit and the beginning of the line is the sum of the character lengths of all semantic units between the previous semantic unit and the beginning of the line and the number of blank characters.
[0194] Optionally, the decomposition module 310 is specifically configured to:
[0195] The characters in the original SQL statement are segmented based on segmentation marks to obtain multiple semantic units; the segmentation marks include punctuation marks, operators and separators.
[0196] The formatting device for structured query language provided in the embodiment of the present invention can execute the formatting method for structured query language provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0197] Example 4
[0198] Figure 4 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or claimed herein.
[0199] like Figure 4 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., which is communicatively connected to the at least one processor 11. The memory stores a computer program that can be executed by the at least one processor. The processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 into the random access memory (RAM) 13. Various programs and data required for the operation of the electronic device 10 can also be stored in the RAM 13. The processor 11, ROM 12, and RAM 13 are connected to each other via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0200] Multiple components in the electronic device 10 are connected to the I / O interface 15, including an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a magnetic disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.
[0201] The processor 11 may be any general-purpose and / or specialized processing component with processing and computing capabilities. Examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various processors for running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the formatting method of the structured query language.
[0202] In some embodiments, the structured query language formatting method may be implemented as a computer program tangibly embodied in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on electronic device 10 via ROM 12 and / or communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the structured query language formatting method described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to execute the structured query language formatting method in any other appropriate manner (e.g., via firmware).
[0203] Various embodiments of the systems and techniques described herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), system-on-chip systems (SOCs), programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0204] In some embodiments, the formatting method of the structured query language can be implemented as a computer program, which is invisibly included in a computer program product. When the computer program is executed by a processor, it implements the formatting method of the structured query language of the present invention. The computer program product can be understood as a software product whose solution is mainly implemented by the computer program. The computer program for implementing the method of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flowchart and / or block diagram are implemented. The computer program can be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0205] In the context of the present invention, computer-readable storage media can be tangible media that can contain or store a computer program for use with an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. Computer-readable storage media can include but are not limited to electronic, magnetic, optical, electromagnetic, infrared or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, computer-readable storage media can be machine-readable signal media. More specific examples of machine-readable storage media can include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0206] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0207] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0208] A computing system may include clients and servers. The clients and servers are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host. This server is a hosting product within the cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosting and VPS services.
[0209] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in the present invention can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solution of the present invention can be achieved. This is not limited herein.
[0210] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.
Claims
1. A formatting method for structured query language, characterized in that: include: Decompose the original SQL statement into multiple semantic units; marking the semantic category and grammatical status of the semantic unit; The semantic unit in the original SQL statement is formatted according to the semantic category and the grammatical state to obtain a formatted SQL statement.
2. The method according to claim 1, characterized in that The marking of the semantic category and grammatical status of the semantic unit includes: For each semantic unit, the semantic category of the semantic unit is marked according to the semantic category condition; the semantic category includes whether it is an identifier, whether it is a reserved word, whether it is a keyword, and whether it is a custom category; For each semantic unit, the grammatical state of the semantic unit is marked according to at least two items of the semantic category of the semantic unit, the semantic category of the adjacent semantic unit of the semantic unit, and the grammatical state of the adjacent semantic unit of the semantic unit.
3. The method according to claim 2, characterized in that After decomposing the original SQL statement into semantic units and before marking the semantic categories of the semantic units according to the semantic category conditions, the method further includes: Adjacent semantic units that meet a merging condition are merged into one semantic unit; the merging condition includes semantic units that need to be used in combination.
4. The method according to claim 1, wherein The formatting operation on the semantic unit in the original SQL statement according to the semantic category and the grammatical state to obtain a formatted SQL statement includes: For the current semantic unit in the original SQL statement, formatting the current semantic unit according to the semantic category and grammatical state of the current semantic unit based on the formatting processing rules; the formatting operation includes at least one of a case operation, an indentation operation, and a line break operation; Obtaining the next semantic unit in the original SQL statement as the current semantic unit; Return to the steps of formatting the current semantic unit according to the semantic category and grammatical state of the current semantic unit based on the formatting processing rules, until the semantic units in the original SQL statement are traversed and the formatted SQL statement is output.
5. The method according to claim 4, characterized in that The formatting operation on the current semantic unit based on the formatting processing rule and according to the semantic category and grammatical state of the current semantic unit includes: If the semantic category of the current semantic unit is a reserved word or the grammatical state of the current semantic unit is a first predefined grammatical state, performing a case operation on the current semantic unit based on the case rule in the formatting processing rule; If the semantic category of the current semantic unit is a predefined keyword, performing an indentation operation and / or a line break operation on the current semantic unit based on the paragraph rule in the formatting processing rule; If the grammatical state of the current semantic unit is the second predefined grammatical state, then based on the paragraph rules in the formatting processing rules, an alignment operation is performed on the current semantic unit according to the number of blank characters and line breaks before the previous semantic unit; the alignment operation includes an indentation operation and a line break operation; the previous semantic unit is the semantic unit before the current semantic unit.
6. The method according to claim 5, characterized in that The performing an alignment operation on the current semantic unit based on the paragraph rule in the formatting processing rule and according to the number of blank characters and line breaks before the previous semantic unit includes: If the grammatical state of the previous semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is greater than zero, performing an alignment operation on the current semantic unit according to the number of blank characters before the previous semantic unit, so that the current semantic unit is aligned with the position of the previous semantic unit; If the grammatical state of the previous semantic unit of the current semantic unit is the second predefined grammatical state, and the number of line breaks before the previous semantic unit is equal to zero, then an alignment operation is performed on the current semantic unit according to the character distance between the previous semantic unit and the beginning of the line, so that the position of the current semantic unit is aligned with that of the previous semantic unit; the character distance between the previous semantic unit and the beginning of the line is the sum of the character lengths of all semantic units between the previous semantic unit and the beginning of the line and the number of blank characters.
7. The method according to claim 1, characterized in that The original SQL statement is decomposed into multiple semantic units, including: The characters in the original SQL statement are segmented based on segmentation marks to obtain multiple semantic units; the segmentation marks include punctuation marks, operators and separators.
8. A formatting device for structured query language, characterized in that: include: Decomposition module, used to decompose the original SQL statement into multiple semantic units; a marking module, used for marking the semantic category and grammatical status of the semantic unit; The formatting module is used to perform a formatting operation on the semantic unit in the original SQL statement according to the semantic category and the grammatical state to obtain a formatted SQL statement.
9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor. The computer program is executed by the at least one processor to enable the at least one processor to perform the formatting method of the structured query language according to any one of claims 1 to 7.
10. A computer program product, characterized in that The computer program product comprises a computer program, which, when executed by a processor, implements the formatting method of the structured query language according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method and system for converting query sentence of database
CN101788992A
Multi-source database statement checking method and device
CN111949541A
SQL (Structured Query Language) statement formatting method and system based on SQL grammar
CN114969090A
Distributed database SQL auditing and optimization suggestion method and system
CN115934721A
System and method for efficient enrichment of business data
US20050060340A1