Database query statement checking method and device, computer equipment, storage medium and computer program product
By extracting keywords in database query statements, determining statement types and grammatical rules, and performing word segmentation checks and combining results, the problem of low accuracy in database query statements in the prior art is solved, and a more efficient and accurate inspection process is achieved.
Patent Information
- Application Number
- CN202411909052.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-24
- Publication Date
- 2025-03-25
AI Technical Summary
In the prior art, there are subjective factors in the inspection of database query statements, resulting in low inspection accuracy.
By obtaining the keywords in the database query statement, determining their target statement types and target grammar rules, performing word segmentation processing and checking, and finally combining the inspection results to improve the accuracy of the inspection.
It improves the inspection accuracy of database query statements, reduces manual intervention, avoids errors caused by subjective factors, and enhances the inspection efficiency of database query statements.
Smart Images

Figure CN119690990A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a database query statement checking method, device, computer equipment, computer-readable storage medium and computer program product. Background Art
[0002] At present, in order to ensure the accuracy of data acquisition, it is crucial to check database query statements.
[0003] In the traditional technology, in the process of checking database query statements, manual checking is generally adopted; however, this manual checking method has subjective factors and is prone to errors, resulting in low accuracy of checking database query statements. Summary of the invention
[0004] Based on this, it is necessary to provide a database query statement checking method, device, computer equipment, computer readable storage medium and computer program product that can improve the checking accuracy of database query statements in response to the above technical problems.
[0005] In a first aspect, the present application provides a method for checking a database query statement, comprising:
[0006] Get the database query statement to be analyzed;
[0007] Extracting keywords from the database query statement;
[0008] Determining, based on the keyword, a target statement type corresponding to the database query statement;
[0009] Determining a target grammar rule corresponding to the database query statement according to the target statement type;
[0010] Performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain inspection results corresponding to each word segment;
[0011] The inspection results corresponding to the respective word segments are combined to obtain the target inspection result corresponding to the database query statement.
[0012] In one embodiment, obtaining a database query statement to be analyzed includes:
[0013] Acquire data source information; the data source information includes at least database address information, database driver information and database identification information;
[0014] Configuring a link database corresponding to the data source information;
[0015] The database query statement to be analyzed is obtained through the linked database.
[0016] In one embodiment, determining the target statement type corresponding to the database query statement according to the keyword includes:
[0017] Performing feature extraction processing on the keyword through a feature extraction model to obtain a feature vector corresponding to the keyword;
[0018] Inputting the feature vector into the trained sentence type prediction model to obtain the prediction probability of the database query sentence under each preset sentence type;
[0019] From the preset sentence types, the preset sentence type with the highest prediction probability is selected as the target sentence type.
[0020] In one embodiment, the trained sentence type prediction model is trained in the following manner:
[0021] Get sample database query statements;
[0022] Extracting sample keywords from the sample database query statement;
[0023] Performing feature extraction processing on the sample keyword through the feature extraction model to obtain a sample feature vector corresponding to the sample keyword;
[0024] Inputting the sample feature vector into a sentence type prediction model to be trained to obtain a predicted sentence type corresponding to the sample database query sentence;
[0025] The actual statement type corresponding to the sample database query statement is obtained, and according to the difference between the predicted statement type and the actual statement type, the statement type prediction model to be trained is iteratively trained to obtain the trained statement type prediction model.
[0026] In one embodiment, the combination processing of the inspection results corresponding to the word segments to obtain the target inspection result corresponding to the database query statement includes:
[0027] Obtaining position information corresponding to each of the segmentations;
[0028] Determine the combination order of the inspection results corresponding to each of the segmentations according to the position information corresponding to each of the segmentations;
[0029] According to the combination order of the inspection results corresponding to the respective word segmentations, the inspection results corresponding to the respective word segmentations are combined and processed to obtain the target inspection result corresponding to the database query statement.
[0030] In one embodiment, after combining the inspection results corresponding to the word segments to obtain the target inspection result corresponding to the database query statement, the method further includes:
[0031] If the target inspection result indicates that the database query statement is an abnormal statement, generating a modification instruction corresponding to the database query statement;
[0032] The modification instruction is sent to a target terminal associated with the database query statement, so that the target terminal modifies the database query statement according to the modification instruction.
[0033] In a second aspect, the present application also provides a database query statement checking device, comprising:
[0034] A statement acquisition module is used to acquire the database query statement to be analyzed;
[0035] A keyword extraction module, used to extract keywords from the database query statement;
[0036] A type determination module, used to determine the target statement type corresponding to the database query statement according to the keyword;
[0037] A rule determination module, used to determine the target grammar rule corresponding to the database query statement according to the target statement type;
[0038] A word segmentation checking module is used to perform word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and to check and process each word segment according to the target grammar rule to obtain a check result corresponding to each word segment;
[0039] The result determination module is used to combine the inspection results corresponding to the segmented words to obtain the target inspection result corresponding to the database query statement.
[0040] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0041] Get the database query statement to be analyzed;
[0042] Extracting keywords from the database query statement;
[0043] Determining, based on the keyword, a target statement type corresponding to the database query statement;
[0044] Determining a target grammar rule corresponding to the database query statement according to the target statement type;
[0045] Performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain inspection results corresponding to each word segment;
[0046] The inspection results corresponding to each of the word segments are combined to obtain the target inspection result corresponding to the database query statement.
[0047] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0048] Get the database query statement to be analyzed;
[0049] Extracting keywords from the database query statement;
[0050] Determining, based on the keyword, a target statement type corresponding to the database query statement;
[0051] Determining a target grammar rule corresponding to the database query statement according to the target statement type;
[0052] Performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain inspection results corresponding to each word segment;
[0053] The inspection results corresponding to each of the word segments are combined to obtain the target inspection result corresponding to the database query statement.
[0054] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:
[0055] Get the database query statement to be analyzed;
[0056] Extracting keywords from the database query statement;
[0057] Determining, based on the keyword, a target statement type corresponding to the database query statement;
[0058] Determining a target grammar rule corresponding to the database query statement according to the target statement type;
[0059] Performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain inspection results corresponding to each word segment;
[0060] The inspection results corresponding to each of the word segments are combined to obtain the target inspection result corresponding to the database query statement.
[0061] The above-mentioned database query statement checking method, device, computer equipment, storage medium and computer program product first obtain the database query statement to be analyzed, and extract the keywords in the database query statement, and then determine the target statement type corresponding to the database query statement based on the keywords. Then, according to the target statement type, determine the target grammar rule corresponding to the database query statement, and then perform word segmentation processing on the database query statement to obtain multiple word segmentations corresponding to the database query statement. According to the target grammar rule, each word segmentation is checked and processed to obtain the inspection result corresponding to each word segmentation. Finally, the inspection results corresponding to each word segmentation are combined and processed to obtain the target inspection result corresponding to the database query statement. In this way, in the process of checking the database query statement, the target statement type corresponding to the database query statement can be obtained according to the keywords in the extracted database query statement, so that the target grammar rule corresponding to the database query statement can be accurately determined, and then the target grammar rule can be used to check and process each word segment corresponding to the database query statement, which is beneficial to improve the accuracy of determining the target inspection result corresponding to the database query statement, thereby improving the inspection accuracy of the database query statement; moreover, the entire process does not require human intervention, avoiding the subjective factors and easy errors in the manual inspection method, resulting in low inspection accuracy of the database query statement, and further improving the inspection accuracy of the database query statement. BRIEF DESCRIPTION OF THE DRAWINGS
[0062] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0063] Figure 1 A schematic diagram of a flow chart of a method for checking a database query statement in one embodiment;
[0064] Figure 2 It is a flowchart of a method for checking a database query statement in another embodiment;
[0065] Figure 3A schematic diagram of a basic architecture of a SQL specification review method based on multiple data sources in one embodiment;
[0066] Figure 4 is a schematic diagram of an abstract syntax tree in one embodiment;
[0067] Figure 5 It is a structural block diagram of a database query statement checking device in one embodiment;
[0068] Figure 6 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0069] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0070] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.
[0071] In an exemplary embodiment, Figure 1 As shown, a method for checking a database query statement is provided. This embodiment uses the method applied to a server as an example for illustration; it is understandable that the method can also be applied to a terminal, and can also be applied to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. Among them, the terminal can be but is not limited to various personal computers, laptops, smart phones and tablet computers; the server can be implemented as an independent server or a server cluster composed of multiple servers. In this embodiment, the method includes the following steps:
[0072] Step S101, obtaining a database query statement to be analyzed.
[0073] The database query statement is also called a SQL (Structured Query Language) statement or an SQL function.
[0074] Exemplarily, the server obtains the database query statement to be analyzed through a pre-configured link database. In addition, the server can also obtain the database query statement to be analyzed through manual input by the user, or obtain the database query statement to be analyzed from a text file imported by the user.
[0075] Step S102: extract keywords from the database query statement.
[0076] Among them, keywords refer to data query keywords, such as SELECT, FROM, WHERE, etc.
[0077] Exemplarily, the server identifies keywords contained in the keyword database in the database query statement through a preset keyword database, and extracts these keywords to obtain the keywords in the database query statement.
[0078] Step S103: determining the target statement type corresponding to the database query statement according to the keyword.
[0079] The target statement type refers to the statement type corresponding to the database query statement, such as DDL (Data Definition Language), DML (Data Manipulation Language), and DQL (Data Query Language).
[0080] Exemplarily, the server queries the corresponding relationship between the keyword and the statement type according to the keyword, obtains the statement type corresponding to the keyword, and uses it as the target statement type corresponding to the database query statement.
[0081] Step S104: determining the target grammar rule corresponding to the database query statement according to the target statement type.
[0082] The target grammatical rules refer to the grammatical rules corresponding to the database query statements.
[0083] Exemplarily, the server obtains the grammatical rules corresponding to the target statement type according to the correspondence between the target statement type, the query statement type and the grammatical rules as the target grammatical rules corresponding to the database query statement.
[0084] Step S105 , performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain the inspection result corresponding to each word segment.
[0085] Among them, word segmentation refers to a single word or symbol in a database query statement.
[0086] The inspection result corresponding to each word segment is used to indicate whether each word segment conforms to the judgment result corresponding to the target grammatical rule.
[0087] Exemplarily, the server performs word segmentation processing on the database query statement according to a preset word segmentation method to obtain multiple word segments corresponding to the database query statement; then, the server checks and processes the data type and value corresponding to each word segment according to the target grammar rules to obtain the inspection result corresponding to each word segment.
[0088] Step S106, combining and processing the inspection results corresponding to each word segmentation to obtain a target inspection result corresponding to the database query statement.
[0089] Among them, the target check result is used to indicate the comprehensive judgment result corresponding to whether the database query statement conforms to the target grammatical rule.
[0090] Exemplarily, the server combines the inspection results corresponding to each word segmentation according to a preset combination method to obtain a combined inspection result as a target inspection result corresponding to the database query statement.
[0091] In the above-mentioned database query statement checking method, the database query statement to be analyzed is first obtained, and the keywords in the database query statement are extracted, and then the target statement type corresponding to the database query statement is determined based on the keywords. Then, according to the target statement type, the target grammar rule corresponding to the database query statement is determined, and then, the database query statement is segmented to obtain multiple segmented words corresponding to the database query statement. According to the target grammar rule, each segmented word is checked and processed to obtain the inspection result corresponding to each segmented word. Finally, the inspection results corresponding to each segmented word are combined to obtain the target inspection result corresponding to the database query statement. In this way, in the process of checking the database query statement, the target statement type corresponding to the database query statement can be obtained according to the keywords in the extracted database query statement, so that the target grammar rule corresponding to the database query statement can be accurately determined, and then the target grammar rule can be used to check and process each word segment corresponding to the database query statement, which is beneficial to improve the accuracy of determining the target inspection result corresponding to the database query statement, thereby improving the inspection accuracy of the database query statement; moreover, the entire process does not require human intervention, avoiding the subjective factors and easy errors in the manual inspection method, resulting in low inspection accuracy of the database query statement, and further improving the inspection accuracy of the database query statement.
[0092] In an exemplary embodiment, the above step S101, obtaining the database query statement to be analyzed, specifically includes the following contents: obtaining data source information; the data source information at least includes database address information, database driver information and database identification information; configuring a linked database corresponding to the data source information; and obtaining the database query statement to be analyzed through the linked database.
[0093] The data source information includes at least database address information, database driver information and database identification information.
[0094] The database address information is used to indicate the location information of the database in the network, such as the IP (Internet Protocol) address of the database.
[0095] The database driver information is used to indicate the driver program corresponding to the database.
[0096] The database identification information is used to indicate the name corresponding to the database.
[0097] The linked database refers to a communication connection channel between an application and a database established based on data source information.
[0098] Exemplarily, the server obtains database address information, database driver information and database identification information, and preprocesses the database address information, database driver information and database identification information to obtain preprocessed database address information, preprocessed database driver information and preprocessed database identification information; then, the server uses the preprocessed database address information, preprocessed database driver information and preprocessed database identification information as data source information; then, the server configures a linked database corresponding to the data source information; then, the server determines a database connection pool corresponding to the linked database, and obtains a database query statement to be analyzed through the database connection pool.
[0099] In this embodiment, by configuring the linked database corresponding to the database address information, database driver information and database identification information, the database query statement to be analyzed can be directly obtained, which is conducive to improving the convenience of obtaining the database query statement and providing a data basis for subsequent data analysis.
[0100] In an exemplary embodiment, the above step S103 determines the target statement type corresponding to the database query statement based on the keyword, and specifically includes the following contents: performing feature extraction processing on the keyword through a feature extraction model to obtain a feature vector corresponding to the keyword; inputting the feature vector into a trained statement type prediction model to obtain the prediction probability of the database query statement under each preset statement type; and screening out the preset statement type with the largest prediction probability from each preset statement type as the target statement type.
[0101] Among them, the feature extraction model refers to a network model that can perform feature extraction processing on data, such as the transformer model.
[0102] Among them, the feature vector is used to represent the characterization vector corresponding to the keyword.
[0103] Among them, the statement type prediction model refers to a network model that can predict the target statement type corresponding to the database query statement, such as a convolutional neural network model, a recurrent neural network model, etc.
[0104] The preset statement type refers to a preset statement type, such as DDL, DML, and DQL. It should be noted that the preset statement type depends on the situation.
[0105] The prediction probability is used to indicate the possibility that the statement type prediction model determines that the database query statement belongs to the preset statement type.
[0106] Exemplarily, the server inputs the keywords in the database query statement into the feature extraction model, performs feature extraction processing on the keywords through the feature extraction model, and obtains the feature vector corresponding to the keyword; then, the server inputs the feature vector into the trained sentence type prediction model, obtains the prediction probability of the feature vector under each preset sentence type, and uses it as the prediction probability of the database query statement under each preset sentence type; then, the server selects the preset statement type with the largest prediction probability from each preset statement type, and uses the preset statement type as the target sentence type.
[0107] In this embodiment, by using the feature extraction model and the trained statement type prediction model, the entire process realizes automated database query statement type judgment. Compared with manual analysis of which type the database query statement belongs to, this method can accurately classify the database query statement, which is beneficial to improving the accuracy of determining the target statement type corresponding to the database query statement.
[0108] In an exemplary embodiment, the database query statement checking method provided by the present application also includes a training step of a trained statement type prediction model, which specifically includes the following contents: obtaining a sample database query statement; extracting sample keywords in the sample database query statement; performing feature extraction processing on the sample keywords through a feature extraction model to obtain a sample feature vector corresponding to the sample keyword; inputting the sample feature vector into the statement type prediction model to be trained to obtain a predicted statement type corresponding to the sample database query statement; obtaining the actual statement type corresponding to the sample database query statement, and iteratively training the statement type prediction model to be trained based on the difference between the predicted statement type and the actual statement type to obtain a trained statement type prediction model.
[0109] The sample database query statement refers to a database query statement used to train a statement type prediction model.
[0110] The sample keywords refer to keywords in the sample database query statements.
[0111] The sample feature vector refers to the feature vector corresponding to the sample keyword.
[0112] The predicted statement type corresponding to the sample database query statement is used to represent the predicted value of the statement type corresponding to the sample database query statement.
[0113] The actual statement type corresponding to the sample database query statement is used to represent the actual value of the statement type corresponding to the sample database query statement.
[0114] Exemplarily, in response to a model training instruction for a sentence type prediction model to be trained, the server obtains a sample database query statement from a database; then, the server extracts sample keywords from the sample database query statement; then, the server performs feature extraction processing on the sample keywords through a feature extraction model to obtain a sample feature vector corresponding to the sample keyword; then, the server inputs the sample feature vector into the sentence type prediction model to be trained to obtain a predicted sentence type corresponding to the sample database query statement; then, the server obtains the actual statement type corresponding to the sample database query statement, and obtains a loss value based on the difference between the predicted statement type and the actual statement type; then, the server adjusts the model parameters of the sentence type prediction model to be trained based on the loss value; then, the server retrains the sentence type prediction model after the model parameters are adjusted until the loss value obtained by the trained sentence type prediction model is less than the loss value threshold, then stops the training, and uses the trained sentence type prediction model as the trained sentence type prediction model.
[0115] In this embodiment, by pre-training the sentence type prediction model, it is convenient to predict the target sentence type corresponding to the database query statement after obtaining the feature vector corresponding to the keyword in the database query statement in actual application; moreover, the sentence type prediction model receives new data in each round of iteration, and performs internal improvement and optimization of the model, so as to facilitate more effective prediction, which is beneficial to improving the prediction accuracy of the sentence type prediction model.
[0116] In an exemplary embodiment, the above step S106 combines the inspection results corresponding to each participle to obtain the target inspection result corresponding to the database query statement, which specifically includes the following contents: obtaining the position information corresponding to each participle; determining the combination order of the inspection results corresponding to each participle according to the position information corresponding to each participle; combining the inspection results corresponding to each participle according to the combination order of the inspection results corresponding to each participle to obtain the target inspection result corresponding to the database query statement.
[0117] The position information is used to indicate the position order of each word in the database query statement.
[0118] The combination order is used to indicate the processing order of combining the inspection results corresponding to each word segment.
[0119] Exemplarily, the server obtains the starting position and the ending position corresponding to each word segment by means of character index as the position information corresponding to each word segment; then, the server queries the correspondence between the position information and the combination order according to the position information corresponding to each word segment, and obtains the combination order of the inspection results corresponding to each word segment; for example, the position information corresponding to word segment a is 1, then the combination order of the inspection results corresponding to word segment a is 1; the position information corresponding to word segment b is 2, then the combination order of the inspection results corresponding to word segment b is 2; then, the server combines the inspection results corresponding to each word segment according to the combination order of the inspection results corresponding to each word segment, and obtains the combined inspection result as the target inspection result corresponding to the database query statement.
[0120] In this embodiment, the combination order of the inspection results is determined by obtaining the position information of each word segmentation, so that the word segmentation inspection situations can be combined and processed in a reasonable order, thereby making the target inspection results corresponding to the database query statement more reasonable, facilitating more accurate judgment of whether each part of the statement is correct, and helping to improve the accuracy of the overall inspection of the database query statement.
[0121] In an exemplary embodiment, the above step S106, after combining the inspection results corresponding to each word segmentation to obtain the target inspection result corresponding to the database query statement, specifically includes the following contents: when the target inspection result indicates that the database query statement is an abnormal statement, generating a modification instruction corresponding to the database query statement; sending the modification instruction to the target terminal associated with the database query statement, so that the target terminal modifies the database query statement according to the modification instruction.
[0122] The modification instruction is used to indicate instruction information for modifying a database query statement.
[0123] The target terminal refers to the terminal associated with the database query statement.
[0124] Exemplarily, the server judges the target inspection result; when the target inspection result indicates that the database query statement is an abnormal statement, the server generates a modification instruction corresponding to the abnormal type according to the abnormal type of the database query statement, as the modification instruction corresponding to the database query statement; then, the server determines the terminal associated with the database query statement as the target terminal; then, the server establishes a network path between the target terminal and the target terminal, and sends the modification instruction to the target terminal through the network path; after receiving the modification instruction, the target terminal modifies the database query statement according to the modification instruction.
[0125] In this embodiment, by automatically generating modification instructions corresponding to database query statements and quickly modifying the database query statements according to the modification instructions, the tedious process of manually troubleshooting errors and then manually writing modification plans is avoided, thereby being able to quickly respond to exceptions and reduce system delays caused by erroneous statements, which is beneficial to improving the stability of the entire database system.
[0126] In an exemplary embodiment, Figure 2 As shown, another database query statement checking method is provided, which is described by taking the method applied to a server as an example, and includes the following steps:
[0127] Step S201, obtaining data source information; the data source information at least includes database address information, database driver information and database identification information.
[0128] Step S202, configuring a link database corresponding to the data source information; obtaining a database query statement to be analyzed through the link database.
[0129] Step S203: extract keywords from the database query statement.
[0130] Step S204: Perform feature extraction processing on the keyword through the feature extraction model to obtain a feature vector corresponding to the keyword.
[0131] Step S205 , inputting the feature vector into the trained sentence type prediction model to obtain the prediction probability of the database query sentence under each preset sentence type.
[0132] Step S206 , selecting the preset sentence type with the highest prediction probability from the preset sentence types as the target sentence type.
[0133] Step S207: determining the target grammar rule corresponding to the database query statement according to the target statement type.
[0134] Step S208, performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain the inspection result corresponding to each word segment.
[0135] Step S209, obtaining the position information corresponding to each participle; and determining the combination order of the inspection results corresponding to each participle according to the position information corresponding to each participle.
[0136] Step S210 , combining the inspection results corresponding to the respective word segments according to the combination order of the inspection results corresponding to the respective word segments, and obtaining the target inspection result corresponding to the database query statement.
[0137] In the above-mentioned database query statement checking method, in the process of checking the database query statement, the target statement type corresponding to the database query statement can be obtained according to the keywords in the extracted database query statement, so that the target grammar rule corresponding to the database query statement can be accurately determined, and then the target grammar rule can be used to check and process each word segment corresponding to the database query statement, which is conducive to improving the accuracy of determining the target inspection result corresponding to the database query statement, thereby improving the inspection accuracy of the database query statement; moreover, the entire process does not require human intervention, avoiding the subjective factors in the manual inspection method, which is prone to errors and leads to the defect of low inspection accuracy of the database query statement, and further improves the inspection accuracy of the database query statement.
[0138] In an exemplary embodiment, in order to more clearly illustrate the database query statement checking method provided in the embodiment of the present application, the database query statement checking method is specifically described below with a specific embodiment. In one embodiment, Figure 3 As shown, the present application also provides a SQL specification review method based on multiple data sources. In the process of checking the database query statement, the database query statement to be analyzed is first obtained, and the keywords in the database query statement are extracted. Then, based on the keywords, the target statement type corresponding to the database query statement is determined. Then, based on the target statement type, the target grammar rule corresponding to the database query statement is determined. Then, the database query statement is segmented to obtain multiple segmented words corresponding to the database query statement. According to the target grammar rule, each segmented word is checked and processed to obtain the inspection result corresponding to each segmented word. Finally, the inspection results corresponding to each segmented word are combined and processed to obtain the target inspection result corresponding to the database query statement. Specifically including the following contents:
[0139] 1. Technical architecture and principles:
[0140] (1) SQL acquisition module: responsible for acquiring SQL statements. There are multiple implementation methods, such as manually entering SQL statements or acquiring from imported text files. SQL statements are acquired by linking to a database and configuring the corresponding data source information, such as the address, driver, database, and other detailed information of the MySQL (relational database management system).
[0141] (2) SQL check module: The core part is responsible for SQL parsing. The most important are the three types of SQL rules: DDL, DML, and DQL. The corresponding syntax rules are written based on historical experience and the specifications summarized from the industry's best practices. One rule corresponds to one class file. If the rules are expanded, they can be easily added to the core module.
[0142] (3) Report rendering module: used to render inspection reports. The reports can be rendered on the VUE (a progressive framework for building user interfaces) page in a table format or exported as Excel files.
[0143] 2. Standardize registration and parsing logic:
[0144] (1) Extensibility design: The SPI (Service Provider Interface) mechanism of Java (a programming language) is adopted. SPI is a service discovery mechanism used to implement extension points of frameworks or libraries. SPI allows service providers to implement their own services according to the rules given by SPI. Java applications do not need to perform additional configuration when using services provided by SPI rules, and can replace services at any time without modifying business code. In layman's terms, we define a set of standards, and others implement them. Then, when the program is executed, it discovers the specific content of its implementation according to our agreement and executes it.
[0145] (2) Abstract Syntax Tree: The parsing process of high-level languages all relies on the parse tree (Parse Tree). The Abstract Syntax Tree (AST) ignores some of the grammatical information contained in the parse tree and strips away some unimportant details. It is an abstract representation of the grammatical structure of the source code, such as Figure 4 As shown. The structure of the programming language is expressed in a tree form, and each node of the tree represents a structure in the source code; AST has its own implementation in different languages. When parsing SQL, SQL is disassembled into a structure tree as shown in the figure below, where the content in the dotted box is a keyword, which effectively identifies each part of the SQL, so as to perform targeted extraction and parsing, and judge it as valid information, which is convenient for the code to classify rules and output results.
[0146] (3) The key point is that when the SQL is submitted to the SQL engine, it will first undergo a lexical analysis to perform a "word segmentation" operation, and then use a syntax parser to perform a syntax analysis and form an AST.
[0147] (4) Lexical and syntactic analysis: Lexical analysis is to decompose SQL statements into individual words or symbols and store them in Token objects. Syntactic analysis is to convert Token objects into syntax trees and store them in preset objects. The parser is to convert the grammar book into corresponding Java objects.
[0148] 3. Configuration specifications and output results:
[0149] (1) Input source: There are SQL editors, file imports, and SQL acquisition from multiple data sources. The editor can perform a preliminary review of the SQL and format its content. File upload can batch and effectively limit unstructured data that cannot be parsed, such as compressed packages and images. For multiple data sources, users can fill in the database connection information, test the connection and save the information of successful connection, and can also obtain script information such as database functions.
[0150] (2) Configuration specifications: Users can select and configure based on existing specifications. For different SQL statements, there is a set of matching inspection specifications. Selecting matching specifications can more accurately and quickly identify potential non-compliance issues in SQL statements.
[0151] (3) Output results: Based on different input sources, the output results are integrated together for easy search and comparison. They can also be exported to Excel and other documents for persistent storage.
[0152] In the above embodiment, during the process of checking the database query statement, the target statement type corresponding to the database query statement can be obtained according to the keywords in the extracted database query statement, so that the target grammar rule corresponding to the database query statement can be accurately determined, and then the target grammar rule can be used to check and process each word segment corresponding to the database query statement, which is beneficial to improve the accuracy of determining the target inspection result corresponding to the database query statement, thereby improving the inspection accuracy of the database query statement; moreover, the entire process does not require human intervention, avoiding the subjective factors and proneness to errors in the manual inspection method, resulting in low inspection accuracy of the database query statement, and further improving the inspection accuracy of the database query statement.
[0153] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0154] Based on the same inventive concept, the embodiment of the present application also provides a database query statement checking device for implementing the database query statement checking method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more database query statement checking device embodiments provided below can refer to the limitations of the database query statement checking method above, and will not be repeated here.
[0155] In an exemplary embodiment, Figure 5 As shown, a database query statement checking device is provided, comprising: a statement acquisition module 501, a keyword extraction module 502, a type determination module 503, a rule determination module 504, a word segmentation checking module 505 and a result determination module 506, wherein:
[0156] The statement acquisition module 501 is used to acquire the database query statement to be analyzed.
[0157] The keyword extraction module 502 is used to extract keywords from the database query statement.
[0158] The type determination module 503 is used to determine the target statement type corresponding to the database query statement according to the keyword.
[0159] The rule determination module 504 is used to determine the target grammar rule corresponding to the database query statement according to the target statement type.
[0160] The word segmentation checking module 505 is used to perform word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and to check and process each word segment according to the target grammar rule to obtain a check result corresponding to each word segment.
[0161] The result determination module 506 is used to combine the inspection results corresponding to each word segmentation to obtain the target inspection result corresponding to the database query statement.
[0162] In an exemplary embodiment, the statement acquisition module 501 is also used to obtain data source information; the data source information includes at least database address information, database driver information and database identification information; configure a link database corresponding to the data source information; and obtain the database query statement to be analyzed through the link database.
[0163] In an exemplary embodiment, the type determination module 503 is also used to perform feature extraction processing on the keyword through a feature extraction model to obtain a feature vector corresponding to the keyword; input the feature vector into a trained sentence type prediction model to obtain the predicted probability of the database query sentence under each preset sentence type; and from each preset sentence type, select the preset sentence type with the largest predicted probability as the target sentence type.
[0164] In an exemplary embodiment, the database query statement checking device also includes a model training module, which is used to obtain a sample database query statement; extract sample keywords in the sample database query statement; perform feature extraction processing on the sample keywords through a feature extraction model to obtain a sample feature vector corresponding to the sample keyword; input the sample feature vector into a statement type prediction model to be trained to obtain a predicted statement type corresponding to the sample database query statement; obtain the actual statement type corresponding to the sample database query statement, and iteratively train the statement type prediction model to be trained based on the difference between the predicted statement type and the actual statement type to obtain a trained statement type prediction model.
[0165] In an exemplary embodiment, the result determination module 506 is also used to obtain the position information corresponding to each participle; determine the combination order of the inspection results corresponding to each participle according to the position information corresponding to each participle; combine the inspection results corresponding to each participle according to the combination order of the inspection results corresponding to each participle, and obtain the target inspection result corresponding to the database query statement.
[0166] In an exemplary embodiment, the database query statement inspection device also includes a statement modification module, which is used to generate a modification instruction corresponding to the database query statement when the target inspection result indicates that the database query statement is an abnormal statement; and send the modification instruction to the target terminal associated with the database query statement, so that the target terminal modifies the database query statement according to the modification instruction.
[0167] Each module in the above-mentioned database query statement checking device can be implemented in whole or in part by software, hardware and their combination. Each module can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to each module above.
[0168] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 6As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data such as target statement types and target grammar rules. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a database query statement checking method is implemented.
[0169] Those skilled in the art will understand that Figure 6 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0170] In an exemplary embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above-mentioned method embodiments when executing the computer program.
[0171] In an exemplary embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0172] In an exemplary embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.
[0173] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0174] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0175] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A method for checking a database query statement, characterized in that: The method comprises: Get the database query statement to be analyzed; Extracting keywords from the database query statement; Determining, based on the keyword, a target statement type corresponding to the database query statement; Determining a target grammar rule corresponding to the database query statement according to the target statement type; Performing word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and performing inspection processing on each word segment according to the target grammar rule to obtain inspection results corresponding to each word segment; The inspection results corresponding to the respective word segments are combined to obtain the target inspection result corresponding to the database query statement.
2. The method according to claim 1, characterized in that The step of obtaining a database query statement to be analyzed includes: Acquire data source information; the data source information includes at least database address information, database driver information and database identification information; Configuring a link database corresponding to the data source information; The database query statement to be analyzed is obtained through the linked database.
3. The method according to claim 1, characterized in that The step of determining, based on the keyword, a target statement type corresponding to the database query statement includes: Performing feature extraction processing on the keyword through a feature extraction model to obtain a feature vector corresponding to the keyword; Inputting the feature vector into the trained sentence type prediction model to obtain the prediction probability of the database query sentence under each preset sentence type; From the preset sentence types, the preset sentence type with the highest prediction probability is selected as the target sentence type.
4. The method according to claim 3, characterized in that The trained sentence type prediction model is obtained by training in the following way: Get sample database query statements; Extracting sample keywords from the sample database query statement; Performing feature extraction processing on the sample keyword through the feature extraction model to obtain a sample feature vector corresponding to the sample keyword; Inputting the sample feature vector into a sentence type prediction model to be trained to obtain a predicted sentence type corresponding to the sample database query sentence; The actual statement type corresponding to the sample database query statement is obtained, and according to the difference between the predicted statement type and the actual statement type, the statement type prediction model to be trained is iteratively trained to obtain the trained statement type prediction model.
5. The method according to claim 1, characterized in that The combining and processing of the inspection results corresponding to the word segments to obtain the target inspection result corresponding to the database query statement includes: Obtaining position information corresponding to each of the segmentations; Determine the combination order of the inspection results corresponding to each of the segmentations according to the position information corresponding to each of the segmentations; According to the combination order of the inspection results corresponding to the respective word segmentations, the inspection results corresponding to the respective word segmentations are combined and processed to obtain the target inspection result corresponding to the database query statement.
6. The method according to any one of claims 1 to 5, characterized in that: After combining the inspection results corresponding to the word segments to obtain the target inspection result corresponding to the database query statement, the method further includes: If the target inspection result indicates that the database query statement is an abnormal statement, generating a modification instruction corresponding to the database query statement; The modification instruction is sent to a target terminal associated with the database query statement, so that the target terminal modifies the database query statement according to the modification instruction.
7. A database query statement checking device, characterized in that: The device comprises: A statement acquisition module is used to acquire the database query statement to be analyzed; A keyword extraction module, used to extract keywords from the database query statement; A type determination module, used to determine the target statement type corresponding to the database query statement according to the keyword; A rule determination module, used to determine the target grammar rule corresponding to the database query statement according to the target statement type; A word segmentation checking module is used to perform word segmentation processing on the database query statement to obtain multiple word segments corresponding to the database query statement, and to check and process each word segment according to the target grammar rule to obtain a check result corresponding to each word segment; The result determination module is used to combine the inspection results corresponding to the segmented words to obtain the target inspection result corresponding to the database query statement.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.