Grammar conversion method and device for structured query language

By splitting SQL statements into morpheme streams, building an abstract syntax tree and analyzing the differences in grammar rules, the problem of low efficiency in manual rewriting SQL statements in database migration is solved, and efficient database adaptation is achieved.

CN120387445APending Publication Date: 2025-07-29CHINA MOBILE GROUP ZHEJIANG +2
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510513736.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-23
Publication Date
2025-07-29

AI Technical Summary

Technical Problem

In the prior art, SQL statements need to be manually rewrite during database migration, resulting in low efficiency and high labor costs, and it is impossible to efficiently realize syntax adaptation between different databases.

Method used

By obtaining SQL statements, splitting them into morpheme streams based on separators, building an abstract syntax tree, analyzing the differences in grammar rules, formulating conversion strategies, and realizing automatic conversion of SQL statements between different databases.

Benefits of technology

Automatic conversion of SQL statements between different databases is realized, which improves adaptation efficiency and reduces labor costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120387445A_ABST
    Figure CN120387445A_ABST
Patent Text Reader

Abstract

The invention provides a grammar conversion method and device for a structured query language, and the method comprises the steps: obtaining a first structured query language SQL statement of a first database, and splitting the first SQL statement based on a separator to obtain a morpheme flow of the first SQL statement; constructing an abstract syntax tree based on the morpheme flow and an SQL syntax rule of a first database; and based on the abstract syntax tree, the SQL syntax rule of the first database and the SQL syntax rule of the second database, converting the first SQL statement to obtain an SQL statement adapted to the second database. The SQL statement adaptive to the second database is obtained by rewriting the SQL statement through the determined abstract syntax tree, analyzing the syntax rule difference to determine the conversion strategy and applying the conversion strategy to adjust the initial query statement, so that the automatic conversion process of the SQL statement among different databases is realized, and the adaptation efficiency of the SQL statement among different databases is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular, to a method and device for converting the syntax of a Structured Query Language (SQL). Background Art

[0002] There are syntax differences between different relational databases. For example, when dealing with time and dates, the functions used by different databases are significantly different. In view of these differences, when an application system needs to be migrated from one database to another, adaptation and transformation work must be carried out.

[0003] The existing adaptation methods mainly rely on manual rewriting of SQL statements in the Structured Query Language (SQL). The manual rewriting process requires a large number of technical personnel to participate in the rewriting work of SQL statements, increasing the labor cost and resulting in low rewriting efficiency. Summary of the Invention

[0004] The present invention provides a method and device for converting the syntax of a Structured Query Language (SQL) to solve the problem of improving the adaptation efficiency of SQL statements between different databases.

[0005] The present invention provides a method for converting the syntax of a Structured Query Language (SQL), including the following steps: Obtain a first SQL statement of a first database, and split the first SQL statement based on a delimiter to obtain a token stream of the first SQL statement; Based on the token stream and the SQL syntax rules of the first database, construct an Abstract Syntax Tree (AST) of the first SQL statement; Based on the AST, the SQL syntax rules of the first database, and the SQL syntax rules of a second database, convert the first SQL statement to obtain a SQL statement adapted to the second database.

[0006] According to the method for converting the syntax of a Structured Query Language (SQL) provided by the present invention, the constructing an Abstract Syntax Tree (AST) of the first SQL statement based on the token stream and the SQL syntax rules of the first database includes: Based on the identification category, divide the token stream to determine multiple lexical units of the first SQL statement; Match the lexical units one by one based on the SQL syntax rules of the first database to construct an Abstract Syntax Tree (AST) of the first SQL statement. The nodes of the AST include the attributes of the syntax structure and child nodes, and the attributes are used to represent the semantic information of the syntax structure.

[0007] A method for grammar conversion of a structured query language provided by the present invention, wherein the identification categories include grammar word categories, object categories, literal constant categories, operator categories, and punctuation symbol categories.

[0008] A method for grammar conversion of a structured query language provided by the present invention, which converts the first SQL statement based on the abstract syntax tree, the SQL grammar rules of the first database, and the SQL grammar rules of the second database to obtain an SQL statement adapted to the second database, including: Rewriting the first SQL statement based on the abstract syntax tree and the SQL grammar rules of the second database to construct an initial query statement; Determining a conversion strategy based on the differences between the SQL grammar rules of the first database and the SQL grammar rules of the second database; Adjusting the initial query statement based on the conversion strategy to obtain an SQL statement adapted to the second database.

[0009] A method for grammar conversion of a structured query language provided by the present invention, which rewrites the first SQL statement based on the abstract syntax tree and the SQL grammar rules of the second database to construct an initial query statement, including: Based on the Visitor access mode, calling the nodes in the abstract syntax tree through a display method, and rewriting the first SQL statement based on the called nodes and the SQL grammar rules of the second database to construct an initial query statement.

[0010] A method for grammar conversion of a structured query language provided by the present invention, before splitting the first SQL statement based on delimiters, further including: Judging the first SQL statement based on the grammar rules of the first database to determine that the first SQL statement conforms to the grammar rules of the first database.

[0011] The present invention also provides a grammar conversion device for a structured query language, including the following modules: A lexical analysis module, configured to obtain a first structured query language SQL statement of a first database, and split the first SQL statement based on delimiters to obtain a morpheme stream of the first SQL statement; A syntax analysis module, configured to construct an abstract syntax tree of the first SQL statement based on the morpheme stream and the SQL grammar rules of the first database; A syntax rewriting module, configured to convert the first SQL statement based on the abstract syntax tree and the SQL grammar rules of the second database to obtain an SQL statement adapted to the second database.

[0012] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored on the memory and running on the processor. When the processor executes the program, the method for syntax conversion of the structured query language as described in any one of the above is implemented.

[0013] The present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for syntax conversion of the structured query language as described in any one of the above is implemented.

[0014] The present invention also provides a computer program product, including a computer program. When the computer program is executed by a processor, the method for syntax conversion of the structured query language as described in any one of the above is implemented.

[0015] The method and device for syntax conversion of the structured query language provided by the present invention obtain a first SQL statement of a first database, split it into a morpheme stream based on a delimiter, and construct an abstract syntax tree. Based on the determined abstract syntax tree, rewrite the SQL statement, analyze the differences in syntax rules to determine a conversion strategy, and apply the conversion strategy to adjust the initial query statement to obtain an SQL statement adapted to a second database, realizing the automatic conversion process of the SQL statement between different databases and improving the adaptation efficiency of the SQL statement between different databases. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0017] Figure 1 It is a schematic diagram of database adaptation in related methods.

[0018] Figure 2 It is a schematic flowchart of the method for syntax conversion of the structured query language provided by the present invention.

[0019] Figure 3 It is a schematic structural diagram of the implementation of the syntax conversion tool provided by the present invention.

[0020] Figure 4 It is a schematic flowchart of the implementation of the syntax conversion tool provided by the present invention.

[0021] Figure 5 It is a schematic diagram of the generation logic of the abstract syntax tree provided by the present invention.

[0022] Figure 6It is a schematic structural diagram of the abstract syntax tree provided by the present invention.

[0023] Figure 7 It is a schematic structural diagram of the syntax conversion device for the structured query language provided by the present invention.

[0024] Figure 8 It is a schematic structural diagram of the electronic device provided by the present invention. Detailed implementation manners

[0025] To make the objectives, technical solutions and advantages of the present invention clearer, the technical solutions in the present invention will be clearly and completely described below with reference to the accompanying drawings in the present invention. Apparently, the described embodiments are some but not all of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in the present invention without making creative efforts shall fall within the protection scope of the present invention.

[0026] There are some syntax differences between different relational databases. For example, when dealing with time and date, the functions used are significantly different.

[0027] In the first database, the CONVERT function is a structural call of (numeric value, type). For example: "SELECT CONVERT('2024-12-19', DATE) from DUAL"; In the second database, the CONVERT function is a structural call of (type, numeric value). For example: "SELECT CONVERT(DATE, '2024-12-19') from DUAL"; In view of these differences, when the application system needs to be migrated from the first database to the second database, adaptation and transformation work must be carried out. The database adaptation method in the related method mainly relies on manual rewriting of SQL statements. As Figure 1 As shown in the database adaptation schematic diagram in the related method, technicians need to transform and rewrite each SQL statement in the first database according to the specifications of the second database in the data persistence layer to ensure that the SQL statement can be correctly executed on the second database.

[0028] The manual rewriting process requires a large number of technicians to participate in the rewriting work of SQL statements, increasing the labor cost and resulting in low rewriting efficiency.

[0029] In view of the defects in the related method, the present invention provides a syntax conversion method for the structured query language. Figure 2 It is a schematic flowchart of the syntax conversion method for the structured query language provided by the present invention. As Figure 2 shown, the method includes the following: Step 210: Obtain the first Structured Query Language (SQL) statement of the first database, and split the first SQL statement based on delimiters to obtain the token stream of the first SQL statement. Step 220: Based on the token stream and the SQL syntax rules of the first database, construct the abstract syntax tree of the first SQL statement. Step 230: Based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, convert the first SQL statement to obtain the SQL statement adapted to the second database.

[0030] The execution subject of the syntax conversion method for Structured Query Language provided by the present invention can be an electronic device, a component in the electronic device, an integrated circuit, or a chip. The electronic device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a laptop computer, a handheld computer, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc., and the non-mobile electronic device can be a server, a Network Attached Storage (NAS), or a personal computer (PC), etc. The present invention does not make specific limitations.

[0031] Taking a computer executing the syntax conversion method for Structured Query Language provided by the present invention as an example, the technical solution of the present invention will be described in detail below.

[0032] In step 210, obtain the first Structured Query Language (SQL) statement of the first database, and split the first SQL statement based on delimiters to obtain the token stream of the first SQL statement.

[0033] Receive a query request sent by an application, where the request contains the SQL statement to be executed. Extract the first SQL statement from the request.

[0034] The delimiters of the SQL statement include spaces, commas, parentheses, semicolons, etc. The SQL statement can be split into a series of tokens using regular expressions or a lexical analyzer. In step 220, based on the token stream and the SQL syntax rules of the first database, construct the abstract syntax tree of the first SQL statement.

[0035] An Abstract Syntax Tree (AST) is a tree-like data structure used to represent the syntax structure of an SQL query. Each node represents a component of the SQL statement, and the child nodes further refine the details of these components.

[0036] A recursive descent parser or a syntax analyzer can be used to construct an abstract syntax tree from the token stream according to the SQL syntax rules of the first database. The parser reads the token stream and gradually constructs the tree structure according to the predefined syntax rules.

[0037] It should be noted that the abstract syntax tree of the first SQL statement constructed is a structured representation of the first SQL statement, containing each component of the query (such as the SELECT clause, FROM clause, WHERE condition, etc.). Each node in the abstract syntax tree represents a syntax element of the SQL statement.

[0038] In step 230, based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, the first SQL statement is transformed to obtain an SQL statement adapted to the second database.

[0039] Specifically, the AST can be traversed, and according to the SQL syntax rules of the second database, the AST nodes can be transformed into a form that conforms to the syntax of the second database. The rewritten AST is transformed back into an SQL string to form an initial query statement.

[0040] The initial query statement may still need to be further adjusted to fully adapt to the syntax of the second database. By comparing the SQL syntax rules of the first database and the second database, the differences can be identified. The differences may include: differences in keywords and functions (such as LIMIT vs FETCH FIRST); differences in data types and formats (such as DATETIME vs TIMESTAMP); differences in the implementation methods of specific functions (such as auto-increment fields, string concatenation, etc.).

[0041] For each difference point, specific transformation rules are formulated. The transformation strategy should ensure that the semantics of the SQL statement remain unchanged during the transformation process. According to the determined transformation strategy, the initial query statement is adjusted.

[0042] The adjustment process can specifically include: keyword replacement, directly replacing incompatible keywords; function adjustment, modifying the function call method to adapt to the syntax of the second database; data type conversion, adjusting the data type to ensure compatibility; implementation of specific functions, implementing specific functions in a way supported by the second database.

[0043] The syntax conversion method of the structured query language provided by the present invention obtains the first SQL statement of the first database, splits it into a token stream based on delimiters, and constructs an abstract syntax tree. Based on the determined abstract syntax tree, the SQL statement is rewritten, the conversion strategy is determined by analyzing the syntax rule differences, and the initial query statement is adjusted by applying the conversion strategy to obtain the SQL statement adapted to the second database, realizing the automatic conversion process of the SQL statement between different databases and improving the adaptation efficiency of the SQL statement between different databases.

[0044] In one embodiment, constructing the abstract syntax tree of the first SQL statement based on the token stream and the SQL syntax rules of the first database includes: dividing the token stream based on the identification category to determine multiple lexical units of the first SQL statement; matching the lexical units one by one according to the SQL syntax rules of the first database to construct the abstract syntax tree of the first SQL statement, and the nodes of the abstract syntax tree include the attributes of the syntax structure and child nodes, and the attributes are used to represent the semantic information of the syntax structure.

[0045] The identification category includes syntax word category, object category, literal constant category, operator category, and punctuation symbol category.

[0046] Use a lexical analyzer to read the first SQL statement and split it into a token stream. The lexical analyzer identifies the type (identification category) of each token according to predefined regular expressions or rules.

[0047] The identification category can specifically be 5 types: syntax word category, words reserved in SQL with specific syntax meanings, such as SELECT, INSERT, UPDATE, DELETE, etc.; object category, used to represent the names of databases, tables, columns, or other data objects; literal constant category, symbols representing fixed values in SQL statements, such as numeric constants like 10, 2e3, and strings like "2024"; operator category, commonly including arithmetic operators (+ - × / ), relational operators (<, >, <=,!=), and logical operators (NOT, AND, OR); punctuation symbol category, including parentheses, commas, semicolons, colons, asterisks, etc.

[0048] Traverse the token stream and classify the tokens into the corresponding identification categories according to their types. Each lexical unit contains the value and type information of the token.

[0049] Use a syntax analyzer to match the lexical units one by one according to the SQL syntax rules of the first database. The syntax analyzer usually adopts methods such as recursive descent parsing, LL parsing, or LR parsing.

[0050] Each abstract syntax tree node represents a syntactic structure of an SQL statement. The node contains attributes for characterizing the semantic information of the syntactic structure (such as column names, table names, conditions, etc.). The node also contains child nodes representing the components of the syntactic structure.

[0051] The process of constructing the abstract syntax tree may include: Initializing the root node: The root node represents the entire SQL statement and has a type of Query.

[0052] Matching the SELECT clause: Identifying the SELECT keyword and creating a SelectClause node. Matching column names or expressions as child nodes of the SelectClause.

[0053] Matching the FROM clause: Identifying the FROM keyword and creating a FromClause node. Matching table names or subqueries as child nodes of the FromClause.

[0054] Matching the WHERE clause: Identifying the WHERE keyword and creating a WhereClause node. Matching conditional expressions as child nodes of the WhereClause.

[0055] Processing other clauses: According to the SQL syntax rules, matching and processing other clauses (such as GROUP BY, HAVING, ORDER BY, LIMIT, etc.).

[0056] Constructing the complete abstract syntax tree: Connecting each clause node as a child node to the root node to form a complete abstract syntax tree. Each node in the constructed abstract syntax tree contains attributes for storing the semantic information of the syntactic structure.

[0057] In one embodiment, based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, converting the first SQL statement to obtain an SQL statement adapted to the second database includes: Rewriting the first SQL statement based on the abstract syntax tree and the SQL syntax rules of the second database to construct an initial query statement; Determining a conversion strategy based on the differences between the SQL syntax rules of the first database and the SQL syntax rules of the second database; Adjusting the initial query statement based on the conversion strategy to obtain an SQL statement adapted to the second database.

[0058] Specifically, the AST can be traversed, and according to the SQL syntax rules of the second database, the AST nodes can be converted into a form that conforms to the syntax of the second database. Converting the rewritten AST back to an SQL string to form an initial query statement.

[0059] The initial query statement may need to be further adjusted to fully conform to the syntax of the second database. By comparing the SQL syntax rules of the first database and the second database, identify the points of difference. The differences may include: differences in keywords and functions (such as LIMIT vs FETCH FIRST); differences in data types and formats (such as DATETIME vs TIMESTAMP); differences in the implementation methods of specific functions (such as auto-increment fields, string concatenation, etc.).

[0060] For each point of difference, formulate specific conversion rules. The conversion strategy should ensure that the semantics of the SQL statement remain unchanged during the conversion process. According to the determined conversion strategy, adjust the initial query statement to obtain the SQL statement adapted to the second database.

[0061] In one embodiment, based on the abstract syntax tree and the SQL syntax rules of the second database, rewrite the first SQL statement to construct an initial query statement, including: based on the Visitor access pattern, call the nodes in the abstract syntax tree through explicit method calls, and rewrite the first SQL statement based on the called nodes and the SQL syntax rules of the second database to construct an initial query statement.

[0062] The Visitor pattern is a behavioral design pattern that allows you to define new operations on data structures without changing the data structures. In the context of an abstract syntax tree, the Visitor pattern allows traversing each node of the abstract syntax tree and performing specific operations on each node.

[0063] Specifically, a Visitor interface can be defined, which contains methods for accessing each type of abstract syntax tree node. For example, for the SELECT clause, FROM clause, WHERE clause, etc., define corresponding methods respectively. Implement the Visitor interface and provide specific logic for each method to rewrite the SQL statement.

[0064] In one embodiment, before splitting the first SQL statement based on delimiters, it further includes: based on the syntax rules of the first database, judge the first SQL statement to determine that the first SQL statement conforms to the syntax rules of the first database.

[0065] The SQL syntax supported by the first database includes keywords, data types, functions, operators, etc. The SQL parser of the first database can be used to parse the SQL statement. The parser checks the structure and syntax of the SQL statement according to the syntax rules.

[0066] Check whether the SQL statement complies with the syntax rules of the first database. Verify the use of keywords, the matching of data types, the correctness of function calls, the matching of parentheses, etc.

[0067] If the SQL statement does not comply with the syntax rules, capture the errors or exceptions thrown by the parser. Provide meaningful error messages to help correct the SQL statement.

[0068] The present invention also provides an SQL syntax conversion tool, which specifically includes a lexical analyzer, a syntax analyzer, and a syntax rewriter.

[0069] The structural schematic diagram implemented based on this tool can be as Figure 3 shown in the structural schematic diagram of the syntax conversion tool provided by the present invention. This tool realizes automatic SQL conversion by adding an independent proxy tool between the application system and the database. The SQL requests of the first database are subjected to lexical analysis, syntax analysis, and syntax rewriting, and finally converted into the syntax of the second database and called. Based on this tool, the application can effectively shield the differences between the underlying database syntaxes of the application and quickly realize the seamless migration from the first database to the second database.

[0070] The specific implementation process can be as Figure 4 shown in the schematic diagram of the implementation process of the syntax conversion tool provided by the present invention.

[0071] In the lexical analysis stage, the lexical analyzer splits the input string stream into a larger-grained morpheme stream according to the delimiter, and then determines whether it complies with the syntax specifications according to the syntax rules of the database language. These morphemes are stored with the identification category Token, and the identification category Token can specifically be 5 types: syntax word category, words reserved in SQL with specific syntax meanings, such as SELECT, INSERT, UPDATE, DELETE, etc.; object category, used to represent the names of databases, tables, columns, or other data objects; literal constant category, symbols representing fixed values in SQL statements, such as numeric constants like 10, 2e3, and the string "2024"; operator category, commonly used arithmetic operators (+ - × / ), relational operators (<, >, <=,!=), and logical operators (NOT, AND, OR); punctuation category, including parentheses, commas, semicolons, colons, asterisks, etc.

[0072] In the syntax parsing stage, the syntax parser matches lexical units one by one according to the syntax rules and constructs an Abstract Syntax Tree (AST). The abstract syntax tree is a hierarchical structure where each node represents a syntax structure in the SQL statement, such as a SELECT statement, an expression, a function, etc. Each node contains corresponding attributes and child nodes. The abstract syntax tree mainly matches lexical units token through rules, performs semantic analysis in combination with the context, and generates the syntax tree using the method of pushing and popping the stack. The specific algorithm logic can be as Figure 5 shown in the schematic diagram of the abstract syntax tree generation logic provided by the present invention. First, the token queue obtains the first token and determines the SQL type based on the first token. Perform operations such as determining the initial rule, creating the current node Node, and creating the battle. Construct the abstract syntax tree according to the hierarchical relationship between nodes.

[0073] Taking the request "SELECT FROM SYS_EMP WHERE DEP_ID = (SELECTID FROM SYS_DEP WHERE NAME = 'Development Department');" to query all employees in the Development Department as an example, the generated abstract syntax tree, the specific implementation structure is as Figure 6 shown in the schematic diagram of the structure of the abstract syntax tree provided by the present invention.

[0074] The syntax rewriter is responsible for transforming the abstract syntax tree generated by the syntax parser. According to the syntax differences between the first database and the second database, different transform conversion strategies are formulated. And based on the Visitor access pattern, the child node information is obtained through explicit method calls and adjusted according to the conversion strategy to achieve the conversion of the SQL statement from the syntax of the first database to the syntax of the second database.

[0075] The database adaptation tool based on the SQL syntax tree realizes the automatic conversion and adaptation of SQL statements. By introducing an independent proxy tool, which is located between the application system and the database, responsible for receiving SQL requests from the application system and automatically converting these SQL requests into corresponding formats according to the characteristics of the target database. Without modifying the SQL code in the data persistence layer (DAO layer), seamless migration from MySQL to the target database is achieved, thereby improving the efficiency and stability of database adaptation.

[0076] Meanwhile, an SQL syntax tree parsing algorithm can be designed to achieve in-depth parsing of SQL statements and construct the corresponding syntax tree structure. On this basis, in order to meet diverse SQL conversion requirements, multiple transform conversion strategies are further formulated. These strategies are designed to flexibly handle different SQL conversion scenarios and ensure that SQL can be converted and processed according to the predetermined rules and requirements.

[0077] The following describes the syntax conversion device for the structured query language provided by the present invention. The syntax conversion device for the structured query language described below can be correspondingly referred to the syntax conversion method for the structured query language described above.

[0078] As Figure 7 shown, the device includes: A lexical analysis module 710, configured to obtain a first structured query language SQL statement of a first database, and based on delimiters, split the first SQL statement to obtain a morpheme stream of the first SQL statement; A syntax analysis module 720, configured to construct an abstract syntax tree of the first SQL statement based on the morpheme stream and the SQL syntax rules of the first database; A syntax rewriting module 730, configured to convert the first SQL statement based on the abstract syntax tree and the SQL syntax rules of a second database to obtain a SQL statement adapted to the second database.

[0079] The syntax conversion device for the structured query language provided by the present invention obtains a first SQL statement of a first database, splits it into a morpheme stream based on delimiters, and constructs an abstract syntax tree. By rewriting the SQL statement based on the determined abstract syntax tree, analyzing the differences in syntax rules to determine the conversion strategy, and applying the conversion strategy to adjust the initial query statement, a SQL statement adapted to the second database is obtained, realizing the automatic conversion process of SQL statements between different databases and improving the adaptation efficiency of SQL statements between different databases.

[0080] In one embodiment, the syntax analysis module 720 is specifically configured to: The constructing an abstract syntax tree of the first SQL statement based on the morpheme stream and the SQL syntax rules of the first database includes: Based on the identification categories, dividing the morpheme stream to determine multiple lexical units of the first SQL statement; Based on the SQL syntax rules of the first database, matching the lexical units one by one to construct an abstract syntax tree of the first SQL statement, where the nodes of the abstract syntax tree include the attributes of the syntax structure and child nodes, and the attributes are used to represent the semantic information of the syntax structure.

[0081] In one embodiment, the syntax analysis module 720 is further specifically configured to: The identification categories include syntax word categories, object categories, literal constant categories, operator categories, and punctuation symbol categories.

[0082] In one embodiment, the syntax rewriting module 730 is specifically configured to: Based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, convert the first SQL statement to obtain an SQL statement adapted to the second database, including: Rewrite the first SQL statement based on the abstract syntax tree and the SQL syntax rules of the second database to construct an initial query statement; Determine a conversion strategy based on the differences between the SQL syntax rules of the first database and the SQL syntax rules of the second database; Adjust the initial query statement based on the conversion strategy to obtain an SQL statement adapted to the second database.

[0083] In one embodiment, the syntax rewriting module 730 is further specifically configured to: Rewrite the first SQL statement based on the abstract syntax tree and the SQL syntax rules of the second database to construct an initial query statement, including: Based on the Visitor access pattern, call the nodes in the abstract syntax tree through an explicit method call, and rewrite the first SQL statement based on the called nodes and the SQL syntax rules of the second database to construct an initial query statement.

[0084] In one embodiment, the lexical analysis module 710 is specifically configured to: Before splitting the first SQL statement based on the delimiter, further include: Judge the first SQL statement based on the syntax rules of the first database to determine that the first SQL statement conforms to the syntax rules of the first database.

[0085] Figure 8 An example of a schematic diagram of the entity structure of an electronic device is shown in Figure 8 As shown, the electronic device may include: a processor 810, a communication interface 820, a memory 830, and a communication bus 840. Among them, the processor 810, the communication interface 820, and the memory 830 complete mutual communication through the communication bus 840. The processor 810 may call the logical instructions in the memory 830 to execute the syntax conversion method of the structured query language, and the method includes: obtaining a first structured query language SQL statement of a first database, and splitting the first SQL statement based on a delimiter to obtain a morpheme stream of the first SQL statement; Construct an abstract syntax tree of the first SQL statement based on the morpheme stream and the SQL syntax rules of the first database; Based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, the first SQL statement is converted to obtain an SQL statement adapted to the second database.

[0086] In addition, when the logical instructions in the above-mentioned memory 830 are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.

[0087] On the other hand, the present invention also provides a computer program product. The computer program product includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the syntax conversion method of the structured query language provided by the above-mentioned various methods. The method includes: obtaining a first structured query language SQL statement of a first database, and based on a delimiter, splitting the first SQL statement to obtain a morpheme stream of the first SQL statement; Based on the morpheme stream and the SQL syntax rules of the first database, constructing an abstract syntax tree of the first SQL statement; Based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, converting the first SQL statement to obtain an SQL statement adapted to the second database.

[0088] On another aspect, the present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, it is implemented to execute the syntax conversion method of the structured query language provided by the above-mentioned various methods. The method includes: obtaining a first structured query language SQL statement of a first database, and based on a delimiter, splitting the first SQL statement to obtain a morpheme stream of the first SQL statement; Based on the morpheme stream and the SQL syntax rules of the first database, constructing an abstract syntax tree of the first SQL statement; Based on the abstract syntax tree, the SQL syntax rules of the first database, and the SQL syntax rules of the second database, the first SQL statement is converted to obtain an SQL statement adapted to the second database.

[0089] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art can understand and implement it without creative work.

[0090] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, and of course also by hardware. Based on such an understanding, the essence of the above technical solution or the part that contributes to the prior art can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0091] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments or equivalently replace some of the technical features. These modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of each embodiment of the present invention.

Claims

1. A method for grammar conversion of Structured Query Language, characterized in that, including: Obtain the first Structured Query Language (SQL) statement of the first database, and split the first SQL statement based on a delimiter to obtain a token stream of the first SQL statement; Construct an Abstract Syntax Tree (AST) of the first SQL statement based on the token stream and the SQL syntax rules of the first database; Convert the first SQL statement based on the AST, the SQL syntax rules of the first database, and the SQL syntax rules of the second database to obtain an SQL statement adapted to the second database.

2. The method for converting the syntax of the Structured Query Language according to claim 1, characterized in that, The constructing an Abstract Syntax Tree (AST) of the first SQL statement based on the token stream and the SQL syntax rules of the first database includes: Divide the token stream based on identification categories to determine multiple lexical units of the first SQL statement; Match the lexical units one by one based on the SQL syntax rules of the first database to construct an Abstract Syntax Tree (AST) of the first SQL statement, where the nodes of the AST include attributes of the syntax structure and child nodes, and the attributes are used to represent semantic information of the syntax structure.

3. The method for grammar conversion of the structured query language according to claim 2, characterized in that, The identification categories include syntax word categories, object categories, literal constant categories, operator categories, and punctuation symbol categories.

4. The method for grammar conversion of Structured Query Language according to claim 1, wherein The converting the first SQL statement based on the AST, the SQL syntax rules of the first database, and the SQL syntax rules of the second database to obtain an SQL statement adapted to the second database includes: Rewrite the first SQL statement based on the AST and the SQL syntax rules of the second database to construct an initial query statement; Determine a conversion strategy based on the differences between the SQL syntax rules of the first database and the SQL syntax rules of the second database; Adjust the initial query statement based on the conversion strategy to obtain an SQL statement adapted to the second database.

5. The method for grammar conversion of Structured Query Language according to claim 4, wherein The rewriting the first SQL statement based on the AST and the SQL syntax rules of the second database to construct an initial query statement includes: Based on the Visitor access pattern, call the nodes in the AST through an explicit method call, and rewrite the first SQL statement based on the called nodes and the SQL syntax rules of the second database to construct an initial query statement.

6. The method for converting the syntax of the structured query language according to claim 1, wherein Before splitting the first SQL statement based on the delimiter, it further includes: Judge the first SQL statement based on the syntax rules of the first database to determine that the first SQL statement conforms to the syntax rules of the first database.

7. A syntax conversion device for a structured query language, characterized in that including: A lexical analysis module, configured to obtain the first Structured Query Language (SQL) statement of the first database, and split the first SQL statement based on a delimiter to obtain a token stream of the first SQL statement; A syntax analysis module, configured to construct an Abstract Syntax Tree (AST) of the first SQL statement based on the token stream and the SQL syntax rules of the first database; A syntax rewriting module, configured to convert the first SQL statement based on the abstract syntax tree and the SQL syntax rules of the second database, so as to obtain an SQL statement adapted to the second database.

8. An electronic device, comprising a memory, a processor, and a computer program stored on the memory and running on the processor, characterized in that, When the processor executes the computer program, the syntax conversion method of the structured query language according to any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, the syntax conversion method of the structured query language according to any one of claims 1 to 6 is implemented.

10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, the syntax conversion method of the structured query language according to any one of claims 1 to 6 is implemented.