CN118035274A - SQL sentence processing method and device - Google Patents

SQL sentence processing method and device Download PDF

Info

Publication number
CN118035274A
CN118035274A CN202410234115.0A CN202410234115A CN118035274A CN 118035274 A CN118035274 A CN 118035274A CN 202410234115 A CN202410234115 A CN 202410234115A CN 118035274 A CN118035274 A CN 118035274A
Authority
CN
China
Prior art keywords
sql statement
sql
analysis
parsing
parser
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410234115.0A
Other languages
Chinese (zh)
Inventor
吴荣鑫
林立
郝宗寅
王壮达
范刚
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Alipay Hangzhou Information Technology Co Ltd
Original Assignee
Alipay Hangzhou Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Alipay Hangzhou Information Technology Co Ltd filed Critical Alipay Hangzhou Information Technology Co Ltd
Priority to CN202410234115.0A priority Critical patent/CN118035274A/en
Publication of CN118035274A publication Critical patent/CN118035274A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/242Query formulation
    • G06F16/2433Query languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/242Query formulation
    • G06F16/2433Query languages
    • G06F16/2448Query languages for particular applications; for extensibility, e.g. user defined types

Landscapes

  • Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Theoretical Computer Science (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The embodiment of the specification provides a method and a device for processing SQL sentences, wherein the method for processing SQL sentences comprises the following steps: in the SQL sentence processing process, the SQL sentence is parsed through a first parser to obtain an abnormal field contained in the SQL sentence, the parsing configuration file of the first parser is configured and updated based on the abnormal field, a second parser is generated according to the obtained updated configuration file, the SQL sentence is further parsed through the second parser to obtain a grammar structure, a variable relation graph is constructed according to the parsing result of the variable relation parsing of the grammar structure, pruning processing is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to the pruning processing result.

Description

SQL sentence processing method and device
Technical Field
The present document relates to the field of database technologies, and in particular, to a method and an apparatus for processing an SQL statement.
Background
A Database management system (DBMS, database MANAGEMENT SYSTEM) is a large software for manipulating and managing databases, and is used for creating, using and maintaining databases, and for uniformly managing and controlling the databases to ensure the security and integrity of the databases, where the DBMS provides a data definition language DDL (Data Definition Language) and a data operation language DML (Data Manipulation Language) for users to define the schema structure and authority constraints of the databases, and to implement operations such as querying, adding, modifying, deleting data, etc. In an actual data operation scene, the situation of SQL (Structured Query Language) query errors is unavoidable, and in the case of complex SQL query, the reasons of the SQL query errors are often difficult to locate.
Disclosure of Invention
One or more embodiments of the present disclosure provide a method for processing an SQL statement, including: and analyzing the SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence. And carrying out configuration updating on the analysis configuration file of the first analyzer based on the abnormal field, and generating a second analyzer according to the obtained updating configuration file. Analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result. Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
One or more embodiments of the present specification provide an apparatus for processing an SQL statement, including: the statement analyzing module is configured to analyze the SQL statement through the first analyzer to obtain an abnormal field contained in the SQL statement. And the configuration updating module is configured to update the configuration file of the first parser based on the abnormal field, and generate a second parser according to the obtained updated configuration file. And the variable analysis module is configured to analyze the SQL sentence through the second analyzer to obtain a grammar structure, analyze the variable relation of the grammar structure and construct a variable relation graph according to an analysis result. And the pruning processing module is configured to prune the variable relation graph according to a pruning strategy and generate a target SQL sentence according to pruning processing results.
One or more embodiments of the present specification provide an apparatus for processing an SQL statement, including: a processor; and a memory configured to store computer-executable instructions that, when executed, cause the processor to: and analyzing the SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence. And carrying out configuration updating on the analysis configuration file of the first analyzer based on the abnormal field, and generating a second analyzer according to the obtained updating configuration file. Analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result. Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
One or more embodiments of the present specification provide a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the following: and analyzing the SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence. And carrying out configuration updating on the analysis configuration file of the first analyzer based on the abnormal field, and generating a second analyzer according to the obtained updating configuration file. Analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result. Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
Drawings
For a clearer description of one or more embodiments of the present description or of the solutions of the prior art, the drawings that are needed in the description of the embodiments or of the prior art will be briefly described below, it being obvious that the drawings in the description that follow are only some of the embodiments described in the present description, from which other drawings can be obtained, without inventive faculty, for a person skilled in the art;
FIG. 1 is a schematic diagram of an implementation environment of a method for processing SQL statements according to one or more embodiments of the present disclosure;
FIG. 2 is a process flow diagram of a method for processing SQL statements according to one or more embodiments of the disclosure;
FIG. 3 is a flow chart illustrating a method for processing SQL statements applied to an SQL anomaly detection scenario according to one or more embodiments of the present disclosure;
FIG. 4 is a process flow diagram of a method for processing SQL statements applied to an SQL query scenario, provided in one or more embodiments of the present disclosure;
FIG. 5 is a schematic diagram of an embodiment of a processing apparatus for SQL statements provided in one or more embodiments of the present disclosure;
Fig. 6 is a schematic structural diagram of an SQL statement processing device according to one or more embodiments of the present disclosure.
Detailed Description
In order to enable a person skilled in the art to better understand the technical solutions in one or more embodiments of the present specification, the technical solutions in one or more embodiments of the present specification will be clearly and completely described below with reference to the drawings in one or more embodiments of the present specification, and it is obvious that the described embodiments are only some embodiments of the present specification, not all embodiments. All other embodiments, which can be made by one or more embodiments of the present disclosure without inventive effort, are intended to be within the scope of the present disclosure.
The method for processing the SQL statement provided in one or more embodiments of the present disclosure may be applied to an implementation environment of SQL exception detection, and referring to fig. 1, the implementation environment at least includes:
A first parser 101 for parsing the SQL statement, a second parser 102 for generating a parse configuration file based on the update, and a pruning processor 103 for performing syntax structure parsing and pruning, wherein the implementation environment may include a parse generating tool 104 for generating the first parser 101 and the second parser 102, and may further include a database system 105 for performing verification on the SQL statement and the target SQL statement;
In this implementation environment, by analyzing the SQL statement through the first analyzer 101, obtaining an abnormal field contained in the SQL statement, performing configuration update on an analysis configuration file of the first analyzer 101 from the abnormal field, and generating a second analyzer 102 capable of analyzing the abnormal field according to the obtained update configuration file, where the first analyzer 101 may be generated by the analysis generating tool 104 according to the analysis configuration file, and the second analyzer 102 may be generated by the analysis generating tool 104 according to the update configuration file; on the basis of generating the second parser 102, parsing the SQL sentence by the second parser 102 to obtain a grammar structure, further performing variable relation parsing on the grammar structure, constructing a variable relation graph according to a parsing result, pruning the variable relation graph according to a pruning strategy after the variable relation graph is obtained, and generating a target SQL sentence according to a pruning processing result to perform exception positioning on the SQL sentence by the target SQL sentence.
One or more embodiments of a method for processing an SQL statement provided in the present specification are as follows:
Referring to fig. 2, the method for processing an SQL statement according to the present embodiment specifically includes steps S202 to S208.
Step S202, analyzing the SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence.
In this embodiment, firstly, an SQL statement for performing a data operation is obtained, where the SQL statement may be an SQL query statement for performing a data query, or may be an SQL add statement, an SQL modify statement, or an SQL delete statement for performing data addition, data modification, or data deletion.
On the basis of acquiring the SQL statement, analyzing the SQL statement through a first analyzer, and determining an abnormal field contained in the SQL statement through analyzing the SQL statement; in an optional implementation manner provided in this embodiment, the parsing, by the first parser, the SQL statement to obtain an exception field included in the SQL statement includes:
Reading the analysis configuration file through an analysis generating tool to generate an analysis device, and obtaining the first analysis device;
and analyzing the fields contained in the SQL statement through the first analyzer, and carrying out field marking on the fields with failed analysis to obtain the abnormal fields.
The parsing generation tool may be a tool for generating a parser of an SQL statement, such as ANTLR (Another Tool for Language Recognition) tools.
Specifically, in the process of analyzing the SQL sentence, the SQL sentence can be analyzed from two dimensions of word analysis and grammar analysis, so that the accuracy and the comprehensiveness of the analysis of the SQL sentence are improved through the two dimensions of word analysis and grammar analysis.
The first parser is a pre-configured standard parser for parsing SQL sentences. Optionally, the first parser specifically includes a word parser for word parsing of the SQL statement and/or a grammar parser for grammar parsing of the SQL statement. The word parser may specifically be a code for performing word parsing processing, which is generated by a parsing tool according to a word parsing rule; the syntax parser may specifically be a code for performing syntax parsing processing generated by a parsing generation tool according to syntax parsing rules.
Here, in the process of generating the parser by reading the parsing configuration file through the parsing generation tool, the following alternative embodiments may be adopted for generating the parser: and reading the word analysis rule of the analysis configuration file record through an analysis generating tool to generate a word analyzer, and reading the grammar analysis rule of the analysis configuration file record through the analysis generating tool to generate a grammar analyzer.
For example, in the process of generating a word parser and a grammar parser using ANTLR (Another Tool for Language Recognition) tools, lexical rules (word parsing rules) and grammar rules (grammar parsing rules) are defined in the parsing configuration file in advance, the lexical rules are used for specifying how to split an input SQL sentence into a series of token, and each lexical rule corresponds to one "word unit" in the input SQL sentence, such as a keyword, an identifier, a numerical constant, etc.; grammar rules describe how valid statements or expressions are combined between tokens, the grammar rules being used to construct abstract grammar trees (AST, abstract Syntax Tree);
After reading the analysis configuration file in the g4 format, the ANTLR tool automatically generates codes of a corresponding word analyzer according to lexical rules recorded in the analysis configuration file, and the generated codes of the word analyzer can perform lexical analysis on an input SQL sentence to obtain a series of token; meanwhile, the ANTLR tool automatically generates a code of a corresponding grammar parser according to grammar rules recorded in the parsing configuration file, and the generated code of the grammar parser can parse a series of input token.
In a specific implementation process, on the basis of the word parser and the grammar parser generated by the parsing generation tool, in an optional implementation manner provided in this embodiment, parsing, by the first parser, a field included in an SQL statement includes:
performing word splitting processing on the SQL sentence through the word parser to obtain a word unit;
and inputting the word units into the grammar parser for grammar parsing processing to obtain the fields with parsing failure.
In practical applications, one or more database dialects may be included in the SQL statement, where a database dialect that cannot be identified or resolved by the current word resolver may exist in the database dialects, in this case, a field corresponding to the database dialect that cannot be identified or resolved by the word resolver included in the SQL statement may fail to be identified or resolved, where a field corresponding to the database dialect that cannot be identified or resolved by the word resolver is marked, and the field corresponding to the database dialect that cannot be identified or resolved by the word resolver is a field that fails to be resolved by the word resolver.
And step S204, carrying out configuration updating on the analysis configuration file of the first analyzer based on the abnormal field, and generating a second analyzer according to the obtained updated configuration file.
In this regard, the embodiment obtains the second parser by optimizing or enhancing the first parser to completely analyze the SQL statement by the optimized or enhanced second parser.
The second parser in this embodiment may be an enhancement parser obtained by optimizing or enhancing the first parser, or may be an adaptive parser capable of completely parsing an SQL statement. Optionally, the second parser specifically includes a word parser for performing word parsing on the SQL statement and/or a grammar parser for performing grammar parsing on the SQL statement. The word parser may specifically be a code for performing word parsing processing, which is generated by a parsing tool according to a word parsing rule; the syntax parser may specifically be a code for performing syntax parsing processing generated by a parsing generation tool according to syntax parsing rules.
In a specific execution process, in order to achieve the integrity of analyzing the SQL sentence, based on the analysis configuration file of the first analyzer, starting from an abnormal field obtained by analyzing the SQL sentence by the first analyzer, performing configuration update on the analysis configuration file of the first analyzer based on the abnormal field, and generating a second analyzer according to the obtained updated configuration file, so that the first analyzer is subjected to targeted configuration update starting from the abnormal field, and the second analyzer generated after configuration update can perform complete analysis on the SQL sentence.
In an optional implementation manner provided in this embodiment, performing configuration update on the analysis configuration file of the first resolver based on the exception field includes: and determining an analysis rule corresponding to the abnormal field, and updating the analysis rule into the analysis configuration file to obtain the updated configuration file.
The parsing configuration file is a file for recording parsing configuration of a parser for generating an SQL statement, and the parsing configuration of the parser for the SQL statement specifically includes word parsing rules for performing word parsing processing on the SQL statement and/or syntax parsing rules for performing syntax parsing processing on the SQL statement, that is: the analysis configuration file is recorded with word analysis rules and/or grammar analysis rules.
Specifically, in the process of determining the parsing rule corresponding to the abnormal field, from the perspective of the database dialect of the abnormal field, the parsing rule of the database dialect type is determined as the parsing rule corresponding to the abnormal field according to the database dialect type of the abnormal field. Specifically, the determined parsing rule corresponding to the abnormal field may be a parsing rule corresponding to the abnormal field, updating the parsing rule to the parsing configuration file to obtain an updated configuration file, specifically, updating the parsing rule corresponding to the abnormal field to a syntax parsing rule of a syntax parser included in the first parser to obtain an updated parsing rule of the syntax parser, and recording the word parsing rule of the word parser and the parsing configuration file of the updated syntax parsing rule as the updated configuration file.
In a specific implementation, after the configuration update is performed on the analysis configuration file of the first resolver based on the exception field to obtain an update configuration file, a second resolver is generated according to the obtained update configuration file, and in an alternative implementation provided in this embodiment, the generating the second resolver according to the obtained update configuration file includes:
a word parser is generated by reading word parsing rules of the updated configuration file records through a parsing generation tool;
And reading the grammar parsing rule of the update configuration file record through the parsing generation tool to generate a grammar parser.
Generating a word parser for lexically parsing the SQL sentence, a grammar parser for grammar parsing the SQL sentence by using an ANTLR tool, and performing word splitting processing on the SQL sentence by using the word parser to obtain word units, inputting the word units into the grammar parser to perform grammar parsing processing to obtain abnormal fields with parsing failure, and determining corresponding grammar parsing rules according to the dialect types of the abnormal fields, namely the grammar parsing rules capable of parsing the dialect types of the abnormal fields;
The grammar analysis rule of the grammar analyzer is updated through the determined grammar analysis rule, the updated grammar analysis rule of the grammar analyzer is obtained, the ANTLR tool automatically generates the code of the corresponding grammar analyzer according to the updated grammar analysis rule, and the generated code of the grammar analyzer can carry out more comprehensive and complete grammar analysis.
And S206, analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result.
As described above, the second parser is obtained by optimizing or enhancing the first parser, so as to obtain a second parser capable of completely parsing the SQL statement, based on which, the second parser parses the SQL statement to obtain a grammar structure, and the grammar structure is parsed by performing variable relation parsing, so as to construct a variable relation graph according to the parsing result obtained by performing the variable relation parsing.
In an optional implementation manner provided in this embodiment, parsing the SQL statement by a second parser to obtain a syntax structure includes:
splitting the SQL sentence through the word parser to obtain a word unit;
and inputting the word units into the grammar parser for grammar structure generation, and obtaining the grammar structure.
It should be noted that, the process of the second parser for parsing the SQL statement may be similar to the process of the first parser for parsing the SQL statement, where the difference is that the first parser cannot completely parse the SQL statement, and after parsing, cannot obtain a complete grammar structure, and may obtain an exception field with parsing failure; the second parser may parse the SQL statement completely, and may be capable of parsing the SQL statement completely into a syntax structure, such as an abstract syntax tree (AST, abstract Syntax Tree).
In a specific implementation, on the basis of obtaining a grammar structure of the SQL statement by performing complete analysis on the SQL statement by the second analyzer, performing variable relation analysis on the grammar structure and constructing a variable relation graph according to an analysis result of the variable relation analysis, in an alternative implementation provided by the embodiment, performing variable relation analysis on the grammar structure and constructing the variable relation graph according to the analysis result, the method includes:
Performing variable element analysis and variable element relation analysis on the grammar structure to obtain variable elements and variable element relations contained in the grammar structure;
A definition-use relationship (def-use) graph is constructed as the variable relationship graph based on the variable elements and the variable element relationships.
And step S208, pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning processing results.
In this embodiment, variable relation analysis is performed on the grammar structure, and on the basis of constructing and obtaining a variable relation graph according to an analysis result, pruning is performed on the variable relation graph according to a pruning strategy, and a target SQL statement is generated according to a pruning result, so that specific fields in the SQL statement are deleted by performing pruning on the variable relation graph and generating the target SQL statement according to the pruning result, thereby simplifying the SQL statement or extracting key fields of the SQL statement.
In an actual scene, in an abnormality detection or abnormality positioning scene of the SQL statement, a developer or an access user can be helped to find the abnormality of the SQL statement by simplifying the SQL statement or extracting the key field of the SQL statement, and optionally, a target SQL statement generated according to a pruning processing result can be a target SQL statement carrying an abnormal field; or generating a target SQL statement according to the pruning result so as to perform exception positioning on the SQL statement according to the target SQL statement.
In addition, the SQL query efficiency in a data access (such as SQL query) scene can be improved by simplifying the SQL statement or extracting key fields of the SQL statement, and the generated target SQL statement is used for data access in the target database in the SQL query scene, namely, the target SQL statement is generated according to pruning processing results so as to access data in the target database through the target SQL statement.
In an optional implementation manner provided in this embodiment, pruning processing is performed on the variable relationship graph according to a pruning policy, including:
Determining a specific element to be pruned in the variable relation diagram;
And deleting the specific element to be pruned from the variable relation graph, or deleting the specific element to be pruned and the associated element with the specific association relationship of the specific element to be pruned from the variable relation graph.
The pruning strategy is a strategy for pruning elements in the variable relation graph, and the pruning strategy adopted for pruning is as follows: deleting the specific element or deleting the association element having a specific association relationship with the specific element and the specific element.
For example, in the case that the variable relation graph adopts a definition-use relation (def-use) graph, pruning processing is performed on the variable relation graph according to a pruning strategy, and the variable relation graph can be specifically implemented by adopting the following optional embodiments: determining definition elements to be pruned in the definition-use relationship graph; and deleting the definition elements to be pruned and the association elements with the use relationship from the definition-use relationship graph. The pruning strategy employed in this case is: deleting a specific definition element and an associated element having a usage relationship with the definition element.
Or in the case that the variable relation graph adopts a definition-use relation (def-use) graph, pruning is carried out on the variable relation graph according to a pruning strategy, and the variable relation graph can be realized by adopting the following optional embodiments: determining that no definition element of a usage relationship exists in the definition-usage relationship graph; deleting the definition elements without the use relation from the definition-use relation graph. The pruning strategy employed in this case is: the definition elements for which no usage relationship exists are deleted.
As described above, in the scene of abnormality detection or abnormality location of the SQL statement, the developer or the visiting user can be helped to find the abnormality of the SQL statement by simplifying the SQL statement or extracting the key field of the SQL statement, and the SQL statement can be verified by means of the test database when the abnormality detection or abnormality location is performed on the SQL statement in the actual execution process, and if the verification of the execution indicates that the abnormality exists in the SQL statement, the abnormality detection or abnormality location is performed on the SQL statement by the above-mentioned processing.
Specifically, in an alternative implementation manner provided in this embodiment, before an SQL statement is parsed by a first parser, execution verification is performed on the SQL statement based on a test database, so as to obtain a first execution result; if the first execution result contains the execution exception information, which indicates that the SQL sentence is abnormal, performing exception detection processing on the SQL sentence by executing the steps S202 to S208; if the first execution result does not contain the execution exception information, the SQL statement is indicated to have no exception, and the processing is not performed.
Further, in the process of executing the steps S202 to S208 on the basis of executing the verification of the SQL statement based on the test database, after the variable relation graph is pruned according to the pruning policy, in the process of generating the target SQL statement according to the pruning result, the execution verification of the target SQL statement may be further performed, and by comparing the execution results of executing the verification of the SQL statement and the target SQL statement, whether the obtained target SQL statement has the same abnormality as the SQL statement may be determined, so as to improve the effectiveness of abnormality detection and the efficiency of abnormality detection of the SQL statement in the abnormality detection scenario.
For example, in an alternative implementation manner provided in this embodiment, generating the target SQL statement according to the pruning result includes:
converting a pruning variable relation diagram contained in the pruning processing result into an SQL sentence to be verified;
Performing execution verification on the SQL statement to be verified based on the test database to obtain a second execution result;
And if the execution exception information contained in the second verification result is the same as the execution exception information contained in the first execution result, determining the SQL sentence to be verified as the target SQL sentence.
In summary, in the processing method of the SQL statement provided in this embodiment, the first parser parses the SQL statement to obtain the exception field included in the SQL statement, so as to determine the database dialect that may exist in the SQL statement and that the first parser cannot parse, then, based on the exception field, perform configuration update on the parsing configuration file of the first parser, and generate the second parser according to the obtained updated configuration file, so as to obtain the second parser by optimizing or enhancing the first parser, so as to obtain the second parser capable of completely parsing the SQL statement, that is, parse the database dialect that the first parser cannot parse, and perform complete parsing on the SQL statement to obtain the syntax structure by the second parser;
Further, variable relation analysis is carried out on the grammar structure, a variable relation graph is constructed according to the analysis result, pruning processing is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to the pruning processing result, so that specific fields in the SQL sentence are deleted in a mode of carrying out pruning processing on the variable relation graph and generating the target SQL sentence according to the pruning processing result, the simplification of the SQL sentence is realized, or the extraction of key fields of the SQL sentence is realized, and therefore, the SQL sentence analysis and the anomaly detection of the cross-database dialect are realized.
The following describes, with reference to fig. 3, a further explanation of the method for processing an SQL statement provided in this embodiment, where the method for processing an SQL statement is applied to an SQL abnormality detection scenario, and referring to fig. 3, the method for processing an SQL statement applied to an SQL abnormality detection scenario specifically includes the following steps.
Step S302, performing execution verification on the SQL statement based on the test database to obtain first execution exception information.
In step S304, if the first execution result includes the execution exception information, the first parser parses the SQL statement to obtain the exception field included in the SQL statement.
Step S306, the analysis configuration file of the first analyzer is configured and updated based on the abnormal field, and a second analyzer is generated according to the obtained updated configuration file.
Step S308, analyzing the SQL sentence through a second analyzer to obtain an abstract syntax tree.
And step S310, performing variable relation analysis on the abstract syntax tree, and constructing a def-use graph according to the analysis result.
And step S312, pruning the def-use graph according to a pruning strategy to obtain a pruned def-use graph.
Step S314, converting the pruned def-use graph into an SQL sentence to be verified.
And step S316, performing execution verification on the SQL statement to be verified based on the test database to obtain second execution exception information.
In step S318, if the first execution exception information is the same as the second execution exception information, the SQL statement to be verified is determined as the target SQL statement for exception detection.
It should be noted that any one step or any combination of steps from step S302 to step S320 may be replaced by the corresponding technical means provided in step S202 to step S208 according to the requirement of implementing deployment, which is not described herein.
The following further describes the method for processing the SQL statement provided in this embodiment with reference to fig. 4 by taking the application of the method for processing the SQL statement provided in this embodiment to the SQL query scene as an example, and referring to fig. 4, the method for processing the SQL statement applied to the SQL query scene specifically includes the following steps.
Step S402, word splitting processing is carried out on the SQL query statement through a word parser, and word units are obtained.
Step S404, inputting the word units into a grammar parser for grammar parsing processing to obtain exception fields with parsing failure.
Step S406, determining the analysis rule corresponding to the abnormal field, and updating the analysis rule to the grammar analysis configuration of the grammar analyzer to obtain the updated grammar analysis configuration.
In step S408, the adaptive syntax parser is generated by reading the updated syntax parsing configuration by the parsing generating tool.
Step S410, inputting the word units into the adaptive grammar parser for grammar structure generation to obtain grammar structure.
Step S412, performing variable relation analysis on the grammar structure, and constructing a variable relation graph according to the analysis result.
And step S414, pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
It should be noted that any one step or a combination of any multiple steps from step S402 to step S414 may be replaced by the corresponding technical means provided in step S202 to step S208 according to the requirement of implementing deployment, which is not described herein.
The embodiment of the processing device for the SQL sentence provided in the specification is as follows:
In the foregoing embodiments, a method for processing an SQL statement is provided, and a device for processing an SQL statement is provided correspondingly, which will be described with reference to the accompanying drawings.
Referring to fig. 5, a schematic diagram of an embodiment of a processing apparatus for an SQL statement provided in this embodiment is shown.
Since the apparatus embodiments correspond to the method embodiments, the description is relatively simple, and the relevant portions should be referred to the corresponding descriptions of the method embodiments provided above. The device embodiments described below are merely illustrative.
The embodiment provides a processing device of an SQL sentence, which comprises:
The statement analyzing module 502 is configured to analyze an SQL statement through a first analyzer to obtain an exception field contained in the SQL statement;
A configuration updating module 504 configured to perform configuration updating on the analysis configuration file of the first resolver based on the anomaly field, and generate a second resolver according to the obtained updated configuration file;
The variable parsing module 506 is configured to parse the SQL statement through the second parser to obtain a grammar structure, parse the variable relation of the grammar structure, and construct a variable relation graph according to the parsing result;
And the pruning processing module 508 is configured to prune the variable relation graph according to a pruning strategy and generate a target SQL sentence according to pruning processing results.
The embodiment of the processing device for the SQL statement provided in the specification is as follows:
In response to the above-described method for processing an SQL statement, one or more embodiments of the present disclosure further provide an SQL statement processing device, where the SQL statement processing device is configured to execute the above-provided method for processing an SQL statement, and fig. 6 is a schematic structural diagram of the SQL statement processing device provided by the one or more embodiments of the present disclosure.
The processing device for an SQL statement provided in this embodiment includes:
As shown in fig. 6, the processing device of the SQL statement may have a relatively large difference due to different configurations or performances, and may include one or more processors 601 and a memory 602, where one or more storage applications or data may be stored in the memory 602. Wherein the memory 602 may be transient storage or persistent storage. The application program stored in the memory 602 may include one or more modules (not shown in the figures), each of which may include a series of computer executable instructions in the processing device of the SQL statement. Still further, the processor 601 may be arranged to communicate with the memory 602, executing a series of computer executable instructions in the memory 602 on a processing device of an SQL statement. The processing device of the SQL statement may also include one or more power supplies 603, one or more wired or wireless network interfaces 604, one or more input/output interfaces 605, one or more keyboards 606, etc.
In a particular embodiment, a processing device for an SQL statement includes a memory, and one or more programs, wherein the one or more programs are stored in the memory, and the one or more programs may include one or more modules, and each module may include a series of computer-executable instructions in the processing device for an SQL statement, and configured to be executed by the one or more processors, the one or more programs comprising computer-executable instructions for:
analyzing an SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence;
Based on the abnormal field, carrying out configuration updating on the analysis configuration file of the first analyzer, and generating a second analyzer according to the obtained updating configuration file;
analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result;
Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
An embodiment of a computer-readable storage medium provided in the present specification is as follows:
In response to the above-described method for processing an SQL statement, one or more embodiments of the present disclosure further provide a computer-readable storage medium based on the same technical concept.
The computer readable storage medium provided in this embodiment is configured to store computer executable instructions, where the computer executable instructions when executed implement the following steps:
analyzing an SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence;
Based on the abnormal field, carrying out configuration updating on the analysis configuration file of the first analyzer, and generating a second analyzer according to the obtained updating configuration file;
analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result;
Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
It should be noted that, in the present specification, an embodiment of a computer readable storage medium and an embodiment of a method for processing an SQL statement in the present specification are based on the same inventive concept, so that a specific implementation of the embodiment may refer to an implementation of the foregoing corresponding method, and a repetition is omitted.
In this specification, each embodiment is described in a progressive manner, and the same or similar parts of each embodiment are referred to each other, and each embodiment focuses on differences from other embodiments, for example, an apparatus embodiment, and a computer readable storage medium embodiment, which are similar to a method embodiment, so that description is relatively simple, and relevant content in reading apparatus embodiments, and computer readable storage medium embodiments is referred to in the description of the method embodiment.
The foregoing describes specific embodiments of the present disclosure. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims can be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
In the 30 s of the 20 th century, improvements to one technology could clearly be distinguished as improvements in hardware (e.g., improvements to circuit structures such as diodes, transistors, switches, etc.) or software (improvements to the process flow). However, with the development of technology, many improvements of the current method flows can be regarded as direct improvements of hardware circuit structures. Designers almost always obtain corresponding hardware circuit structures by programming improved method flows into hardware circuits. Therefore, an improvement of a method flow cannot be said to be realized by a hardware entity module. For example, a programmable logic device (Programmable Logic Device, PLD) (e.g., field programmable gate array (Field Programmable GATE ARRAY, FPGA)) is an integrated circuit whose logic functions are determined by user programming of the device. A designer programs to "integrate" a digital system onto a PLD without requiring the chip manufacturer to design and fabricate application-specific integrated circuit chips. Moreover, nowadays, instead of manually manufacturing integrated circuit chips, such programming is mostly implemented with "logic compiler (logic compiler)" software, which is similar to the software compiler used in program development and writing, and the original code before being compiled is also written in a specific programming language, which is called hardware description language (Hardware Description Language, HDL), but HDL is not just one, but a plurality of kinds, such as ABEL(Advanced Boolean Expression Language)、AHDL(Altera Hardware Description Language)、Confluence、CUPL(Cornell University Programming Language)、HDCal、JHDL(Java Hardware Description Language)、Lava、Lola、MyHDL、PALASM、RHDL(Ruby Hardware Description Language), and VHDL (Very-High-SPEED INTEGRATED Circuit Hardware Description Language) and Verilog are currently most commonly used. It will also be apparent to those skilled in the art that a hardware circuit implementing the logic method flow can be readily obtained by merely slightly programming the method flow into an integrated circuit using several of the hardware description languages described above.
The controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, a microprocessor or processor and a computer readable medium storing computer readable program code (e.g., software or firmware) executable by the (micro) processor, logic gates, switches, application SPECIFIC INTEGRATED Circuits (ASICs), programmable logic controllers, and embedded microcontrollers, examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, atmel AT91SAM, microchip PIC18F26K20, and Silicone Labs C8051F320, the memory controller may also be implemented as part of the control logic of the memory. Those skilled in the art will also appreciate that, in addition to implementing the controller in a pure computer readable program code, it is well possible to implement the same functionality by logically programming the method steps such that the controller is in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers, etc. Such a controller may thus be regarded as a kind of hardware component, and means for performing various functions included therein may also be regarded as structures within the hardware component. Or even means for achieving the various functions may be regarded as either software modules implementing the methods or structures within hardware components.
The system, apparatus, module or unit set forth in the above embodiments may be implemented in particular by a computer chip or entity, or by a product having a certain function. One typical implementation is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
For convenience of description, the above devices are described as being functionally divided into various units, respectively. Of course, the functions of each unit may be implemented in the same piece or pieces of software and/or hardware when implementing the embodiments of the present specification.
One skilled in the relevant art will recognize that one or more embodiments of the present description may be provided as a method, system, or computer program product. Accordingly, one or more embodiments of the present description may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present description can take the form of a computer program product on one or more computer-readable storage media (including, but not limited to, magnetic disk storage, CD-ROM, optical storage, etc.) having computer-usable program code embodied therein.
The present description is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the specification. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In one typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include volatile memory in a computer-readable medium, random Access Memory (RAM) and/or nonvolatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of computer-readable media.
Computer readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer-readable storage media include, but are not limited to, phase-change memory (PRAM), static Random Access Memory (SRAM), dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), read Only Memory (ROM), electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), digital Versatile Disks (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium, which can be used to store information that can be accessed by a computing device. Computer-readable media, as defined herein, does not include transitory computer-readable media (transmission media), such as modulated data signals and carrier waves.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising at least one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
One or more embodiments of the present specification may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. One or more embodiments of the specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
The foregoing description is by way of example only and is not intended to limit the present disclosure. Various modifications and changes may occur to those skilled in the art. Any modifications, equivalent substitutions, improvements, etc. that fall within the spirit and principles of the present document are intended to be included within the scope of the claims of the present document.

Claims (14)

1. A method for processing an SQL statement, comprising:
analyzing an SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence;
Based on the abnormal field, carrying out configuration updating on the analysis configuration file of the first analyzer, and generating a second analyzer according to the obtained updating configuration file;
analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result;
Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
2. The method for processing an SQL statement according to claim 1, wherein the parsing the SQL statement by the first parser to obtain the exception field contained in the SQL statement comprises:
Reading the analysis configuration file through an analysis generating tool to generate an analysis device, and obtaining the first analysis device;
and analyzing the fields contained in the SQL statement through the first analyzer, and carrying out field marking on the fields with failed analysis to obtain the abnormal fields.
3. The method for processing the SQL statement according to claim 2, wherein the parsing generating by reading the parsing configuration file through a parsing generating tool performs parser generation, and the obtaining the first parser includes:
Reading a word analysis rule of the analysis configuration file record through the analysis generating tool to generate a word analyzer, and reading a grammar analysis rule of the analysis configuration file record through the analysis generating tool to generate a grammar analyzer;
correspondingly, the parsing, by the first parser, the fields contained in the SQL statement includes:
performing word splitting processing on the SQL sentence through the word parser to obtain a word unit;
and inputting the word units into the grammar parser for grammar parsing processing to obtain the fields with parsing failure.
4. The method for processing the SQL statement according to claim 1, wherein the updating the configuration of the parsing configuration file of the first parser based on the exception field comprises:
And determining an analysis rule corresponding to the abnormal field, and updating the analysis rule into the analysis configuration file to obtain the updated configuration file.
5. The method for processing the SQL statement according to claim 1, wherein the generating the second parser according to the obtained update configuration file comprises:
a word parser is generated by reading word parsing rules of the updated configuration file records through a parsing generation tool;
And reading the grammar parsing rule of the update configuration file record through the parsing generation tool to generate a grammar parser.
6. The method for processing the SQL statement according to claim 5, wherein the parsing the SQL statement by the second parser to obtain the syntax structure comprises:
splitting the SQL sentence through the word parser to obtain a word unit;
and inputting the word units into the grammar parser for grammar structure generation, and obtaining the grammar structure.
7. The method for processing the SQL statement according to claim 1, wherein the parsing the variable relation of the syntax structure and constructing a variable relation graph according to the parsing result comprises:
Performing variable element analysis and variable element relation analysis on the grammar structure to obtain variable elements and variable element relations contained in the grammar structure;
a definition-use relationship graph is constructed based on the variable elements and the variable element relationships as the variable relationship graph.
8. The method for processing the SQL statement according to claim 7, wherein pruning the variable relation graph according to a pruning strategy comprises:
Determining definition elements to be pruned in the definition-use relationship graph;
and deleting the definition element to be pruned and the association element with the use relation from the definition-use relation graph.
9. The method for processing the SQL statement according to claim 7, wherein pruning the variable relation graph according to a pruning strategy comprises:
determining that no definition element of a usage relationship exists in the definition-usage relationship graph;
deleting the definition elements without the use relation from the definition-use relation graph.
10. The method for processing an SQL statement according to claim 1, wherein before the step of parsing the SQL statement by the first parser to obtain the exception field included in the SQL statement is performed, further comprising:
performing execution verification on the SQL statement based on a test database to obtain a first execution result;
and if the first execution result contains the execution exception information, executing the step of analyzing the SQL sentence by the first analyzer to obtain the exception field contained in the SQL sentence.
11. The method for processing the SQL statement according to claim 10, wherein the generating the target SQL statement according to the pruning result comprises:
converting a pruning variable relation diagram contained in the pruning processing result into an SQL sentence to be verified;
Performing execution verification on the SQL statement to be verified based on the test database to obtain a second execution result;
And if the execution exception information contained in the second verification result is the same as the execution exception information contained in the first execution result, determining the SQL sentence to be verified as the target SQL sentence.
12. A processing apparatus of an SQL statement, comprising:
the statement analyzing module is configured to analyze the SQL statement through the first analyzer to obtain an abnormal field contained in the SQL statement;
The configuration updating module is configured to update the analysis configuration file of the first analyzer based on the abnormal field and generate a second analyzer according to the obtained updated configuration file;
The variable analysis module is configured to analyze the SQL sentence through the second analyzer to obtain a grammar structure, analyze the variable relation of the grammar structure and construct a variable relation graph according to an analysis result;
and the pruning processing module is configured to prune the variable relation graph according to a pruning strategy and generate a target SQL sentence according to pruning processing results.
13. A processing device of an SQL statement, comprising:
A processor; and a memory configured to store computer-executable instructions that, when executed, cause the processor to:
analyzing an SQL sentence through a first analyzer to obtain an abnormal field contained in the SQL sentence;
Based on the abnormal field, carrying out configuration updating on the analysis configuration file of the first analyzer, and generating a second analyzer according to the obtained updating configuration file;
analyzing the SQL sentence through the second analyzer to obtain a grammar structure, analyzing the variable relation of the grammar structure, and constructing a variable relation graph according to an analysis result;
Pruning is carried out on the variable relation graph according to a pruning strategy, and a target SQL sentence is generated according to pruning results.
14. A computer readable storage medium storing computer executable instructions which, when executed, implement the steps of the method of claim 1.
CN202410234115.0A 2024-02-29 2024-02-29 SQL sentence processing method and device Pending CN118035274A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410234115.0A CN118035274A (en) 2024-02-29 2024-02-29 SQL sentence processing method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410234115.0A CN118035274A (en) 2024-02-29 2024-02-29 SQL sentence processing method and device

Publications (1)

Publication Number Publication Date
CN118035274A true CN118035274A (en) 2024-05-14

Family

ID=90989249

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410234115.0A Pending CN118035274A (en) 2024-02-29 2024-02-29 SQL sentence processing method and device

Country Status (1)

Country Link
CN (1) CN118035274A (en)

Similar Documents

Publication Publication Date Title
CN107545030B (en) Method, device and equipment for processing data blood relationship
CN110245002B (en) System interaction method, device, equipment and storage medium
CN111400681B (en) Data authority processing method, device and equipment
WO2019237333A1 (en) Converting database language statements between dialects
CN110569428A (en) recommendation model construction method, device and equipment
CN115756449B (en) Page multiplexing method and device, storage medium and electronic equipment
CN113434533A (en) Data tracing tool construction method, data processing method, device and equipment
CN114547024A (en) SQL statement risk detection method, device, equipment and medium
CN116610568A (en) Method, device, equipment and medium for identifying dependency relationship of codes
CN116560642A (en) Code generation method and device, electronic equipment and storage medium
CN117828360A (en) Model training method, model training device, model code generating device, storage medium and storage medium
CN110245137B (en) Index processing method, device and equipment
CN108008947B (en) Intelligent prompting method and device for programming statement, server and storage medium
CN114169003A (en) Dynamic desensitization method and system based on syntax tree analysis and result set rewriting
CN109582300A (en) Code analysis on altered project method, device and equipment based on path
CN113608748A (en) Data processing method, device and equipment for converting C language into Java language
CN116432185B (en) Abnormality detection method and device, readable storage medium and electronic equipment
CN110245136B (en) Data retrieval method, device, equipment and storage equipment
CN116483676A (en) Log inverse solution method, device and equipment
CN118035274A (en) SQL sentence processing method and device
CN116010419A (en) Method and device for creating unique index and optimizing logic deletion
CN115391426A (en) Data query method and device, storage medium and electronic equipment
CN111966709B (en) Data query method and device and electronic equipment
CN117743189A (en) Code analysis method and device
CN117931672A (en) Query processing method and device applied to code change

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination