Database query language generation method and device
By generating a compatible third query language, the complexity and inefficiency of heterogeneous database migration are solved, and the automation and efficiency of database migration are achieved.
Patent Information
- Application Number
- CN202411721338.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-03-14
AI Technical Summary
During the migration of heterogeneous databases, the migration of database structure objects and data is complex, and the existing technology relies on manual code modification, resulting in low migration efficiency.
By obtaining the query languages of the first database and the second database, and establishing a transformation mapping table, generating a syntax parsing tree of the query language, determining the identifiers of the target node, modifying the identifiers based on the transformation mapping table, and using a recursive return algorithm to generate a compatible third query language.
It improves the efficiency of database migration, reduces the workload of manually modifying code, and realizes query language compatibility between the first database and the second database.
Smart Images

Figure CN119621752A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method and device for generating a database query language. Background Art
[0002] The process of heterogeneous database migration (for example, from Oracle to TeleDB for xscale) involves the migration of multiple database structure objects (such as tables, views, sequences, stored procedures, functions, triggers, packages, etc.) and data, which makes the heterogeneous database migration process very complicated.
[0003] In the related art, database migration is often achieved by manually modifying the code. However, facing the increasingly large database, the workload of manual modification is relatively large, which makes the efficiency of database migration low. Summary of the invention
[0004] Based on this, it is necessary to provide a database query language generation method, device, computer equipment, computer-readable storage medium and computer program product that can improve database migration efficiency in response to the above technical problems.
[0005] In a first aspect, the present application provides a method for generating a database query language, the method comprising:
[0006] Obtaining a first query language of a first database and a second query language of a second database, and obtaining a conversion mapping table between the first query language and the second query language;
[0007] Generate a syntax parse tree of the first query language;
[0008] Determine a first identifier of a target node in a syntax parse tree of the first query language;
[0009] Determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier;
[0010] Based on a recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0011] In one embodiment, determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier, includes:
[0012] When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
[0013] In one embodiment, determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier, includes:
[0014] When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, the first identifier is deleted from the syntax parse tree.
[0015] In one embodiment, determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier, includes:
[0016] When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, prompt information is formed.
[0017] In one embodiment, after generating the syntax parse tree of the first query language, the following steps are performed:
[0018] Determine a global variable declaration area node in the syntax parse tree;
[0019] Determine a global variable mapping table based on the global variable declaration area node;
[0020] The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes:
[0021] When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed;
[0022] The step of combining the modified first identifier and the grammar parse tree based on a recursive return algorithm to generate a third query language includes:
[0023] Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0024] In one embodiment, determining a global variable mapping table based on the global variable declaration area node includes:
[0025] Determine variable information of each global variable in the global variable declaration area node, the variable information including variable name, operation type and complete definition;
[0026] Based on the variable information of each global variable, construct a plurality of key-value pairs, wherein the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition;
[0027] Based on the multiple key-value pairs, a global variable mapping table is determined.
[0028] In one embodiment, determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier, includes:
[0029] In a case where the first identifier includes a loop node, determining an assignment statement in the loop node;
[0030] The assignment statement is reconstructed using a preset rule in the second identifier to form a modified first identifier.
[0031] In one embodiment, determining a first identifier of a target node in a syntax parse tree of the first query language includes:
[0032] Determining the target node in a syntax parse tree of the first query language;
[0033] Determine the function name and / or parameter list of the target node;
[0034] The function name and / or the parameter list is determined as the first identifier.
[0035] In one embodiment, determining the first identifier of the target node in the syntax parse tree of the first query language includes:
[0036] Get the preset node list and the current node;
[0037] When the current node is found in the node list, the current node is determined as the target node.
[0038] In a second aspect, the present application further provides a database query language generation device, the device comprising:
[0039] An acquisition module, configured to acquire a first query language of a first database and a second query language of a second database, and acquire a conversion mapping table between the first query language and the second query language;
[0040] A generating module, used for generating a syntax parsing tree of the first query language;
[0041] A determination module, configured to determine a first identifier in a syntax parse tree of the first query language;
[0042] a modification module, configured to determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier;
[0043] A concatenation module is used to concatenate the syntax parse tree and the modified first identifier based on a recursive return algorithm to generate a third query language.
[0044] 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:
[0045] Obtaining a first query language of a first database and a second query language of a second database, and obtaining a conversion mapping table between the first query language and the second query language;
[0046] Generate a syntax parse tree of the first query language;
[0047] Determine a first identifier of a target node in a syntax parse tree of the first query language;
[0048] Determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier;
[0049] Based on a recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0050] 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:
[0051] Obtaining a first query language of a first database and a second query language of a second database, and obtaining a conversion mapping table between the first query language and the second query language;
[0052] Generate a syntax parse tree of the first query language;
[0053] Determine a first identifier of a target node in a syntax parse tree of the first query language;
[0054] Determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier;
[0055] Based on a recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0056] 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:
[0057] Obtaining a first query language of a first database and a second query language of a second database, and obtaining a conversion mapping table between the first query language and the second query language;
[0058] Generate a syntax parse tree of the first query language;
[0059] Determine a first identifier of a target node in a syntax parse tree of the first query language;
[0060] Determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier;
[0061] Based on a recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0062] The above-mentioned database query language generation method, device, computer equipment, computer-readable storage medium and computer program product, by setting a conversion mapping table between the first query language and the second query language, can quickly modify the first identifier based on the second identifier, and then obtain a third query language compatible with the first database and the second database. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] 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.
[0064] Figure 1 A schematic diagram of a method for generating a database query language provided in the first embodiment;
[0065] Figure 2 A schematic diagram of a method for generating a database query language provided in a second embodiment;
[0066] Figure 3 A schematic diagram of a method for generating a database query language provided in a third embodiment;
[0067] Figure 4 A schematic diagram of a method for generating a database query language provided in a fourth embodiment;
[0068] Figure 5 A schematic diagram of a method for generating a database query language provided in a fifth embodiment;
[0069] Figure 6 A schematic diagram of a database query language generation method provided in a sixth embodiment;
[0070] Figure 7 A schematic diagram of a method for generating a database query language provided in a seventh embodiment;
[0071] Figure 8 A schematic diagram of a method for generating a database query language provided in an eighth embodiment;
[0072] Fig. 9 A schematic diagram of a method for generating a database query language provided in a ninth embodiment;
[0073] Fig.10 A schematic diagram of a database query language generation method provided in a tenth embodiment;
[0074] Fig.11 A structural block diagram of a database query language generating device provided in one embodiment;
[0075] Fig.12 The figure is a diagram of the internal structure of a computer device provided in one embodiment. DETAILED DESCRIPTION
[0076] 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.
[0077] The database query language generation method provided in the embodiment of the present application can be applied to environments such as environments or terminals. The terminal can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, Internet of Things devices, etc. The server can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides cloud computing services.
[0078] like Figures 1 to 10 As shown, the present application provides different embodiments of the method for generating a database query language. This embodiment uses the method applied to a terminal as an example. It is understandable that the method can also be applied to a server, 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. Figure 1 In this embodiment, the method includes the following steps:
[0079] Step S2: obtaining a first query language of the first database and a second query language of the second database, and obtaining a conversion mapping table between the first query language and the second query language.
[0080] Step S4: Generate a syntax parse tree of the first query language.
[0081] Step S6: Determine a first identifier of a target node in a syntax parse tree of the first query language.
[0082] Step S8: Determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier.
[0083] Step S10: Based on the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0084] In step S2, as an example, the first database may be Oracle, and the second database may be TeleDB for Xscale. Of course, the first database and the second database of this embodiment are not limited to the above specific databases. The first query language and the second query language refer to the SQL query languages used by the two databases respectively.
[0085] The conversion mapping table may be a pre-set mapping table. As an example, the conversion mapping table may include a data type mapping table, a system function mapping table, a syntax conversion mapping table, and the like.
[0086] In step S4, a syntax parse tree can be used to represent the syntax structure of the first query language. The syntax parse tree can clearly show the various components of the first query language and the relationship between them. As an example, the first query language can be parsed using the open source tool antlr4 to generate a syntax parse tree.
[0087] In step S6, as an example, the visitor code for deep traversal of the syntax tree generated by antlr4 can be used to traverse the generated syntax parse tree to find the target node. The target node can be a node in the syntax parse tree that needs to be converted or modified. These nodes can correspond to specific elements in the query statement, such as function names, keywords or data types.
[0088] The first identifier is the representation of the target node in the first query language. For example, if the target node is a function name, the first identifier is the name of the function in Oracle.
[0089] In step S8, the second identifier is a representation of the target node in the second query language. At the same time, the second identifier may be provided with a corresponding modification rule. Afterwards, the first identifier may be modified according to the modification rule. As an example, the first identifier is the Oracle-side Substr, and the corresponding second identifier is the TeleDB for xscale substring function. Afterwards, the TeleDB for xscale substring function call expression is assembled according to the parsed function parameter list, and finally the original Oracle Substr function call expression is replaced by it.
[0090] In step S10, the recursive return algorithm can be used to traverse and build a tree structure. In this process, the recursive return algorithm can be used to traverse the modified syntax parse tree, and generate the third query language according to the tree structure and node content. As an example, the text of all leaf nodes will be assembled during the deep traversal process, and the third query language will be assembled when the deep traversal is completed. Of course, please refer to Fig.10 , the above steps can be repeated multiple times.
[0091] The third query language may be a query statement that complies with the query syntax of both the first database and the second database, that is, the third query language may be compatible with the first database and the second database.
[0092] In this embodiment, by setting a conversion mapping table between the first query language and the second query language, the first identifier can be quickly modified based on the second identifier, thereby obtaining a third query language compatible with the first database and the second database.
[0093] For example, see Figure 2 , Figure 3 and Figure 4 , step S8 comprises:
[0094] Step S82: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
[0095] Step S84: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, delete the first identifier in the syntax parsing tree.
[0096] Step S86: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, generate prompt information.
[0097] In step S82, if there is a modification rule that clearly indicates the first identifier and the second identifier in the conversion mapping table, the first identifier can be replaced by the second identifier. As an example, when the Oracle data type is FLOAT, the data type of TeleDB for xscale corresponding to FLOAT can be DOUBLE PRECISION.
[0098] In step S84, if there is no modification rule for the first identifier and the second identifier in the conversion mapping table, it means that the first identifier cannot be applied to the second database. At this time, the first identifier can be deleted in the syntax parsing tree. Of course, deleting the first identifier may include deleting the subtree where the first identifier is located and its subordinate nodes. As an example, when the Oracle system function is xmlparser.freeParser, the system function of TeleDB for xscale is incompatible. At this time, xmlparser.freeParser can be removed.
[0099] In step S86, when the second identifier corresponding to the first identifier shown in the conversion mapping table includes a prompt rule, it means that the first identifier can be replaced after modification. As an example, at this time, a comment can be added to the SQL statement to prompt the user to modify it by modifying the business logic. As an example, when the Oracle system function is DBMS_METADATA.set_transform_param, TeleDB for xscale does not support it and can prompt the user to modify the business logic.
[0100] The following exemplifies a method for converting a package when the first database may be Oracle and the second database may be TeleDB forxscale.
[0101] Specifically, in package, data types are usually used when declaring variables. Variable declarations are usually followed by variable names followed by variable types. First, when the syntax tree parsing code visitor generated by antlr4 visits the function or stored procedure variable declaration subtree of the package, the variable type is parsed. Then, the corresponding target data type is found according to the Oracle to TeleDB for Xscale data type mapping table (for example, Oracle's BLOB corresponds to TeleDB for Xscale's BYTEA). Finally, when traversing the variable definition subtree and assembling the third query language, the variable type is replaced with the target type to complete the data type mapping conversion. The conversion process for system functions can be the same as the conversion of data types.
[0102] Specifically, for syntax conversion, in the first embodiment, please refer to Figure 5 , step S4 may include:
[0103] Step S5: Determine the global variable declaration area node in the syntax parse tree.
[0104] Step S52: Determine a global variable mapping table based on the global variable declaration area node.
[0105] Accordingly, step S8 may include:
[0106] Step S88: When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed.
[0107] Step S10 includes:
[0108] Step S102: Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0109] In step S5, nodes representing global variable declaration areas can be found in the generated syntax parse tree. These nodes usually contain declaration information of global variables defined in the query.
[0110] In step S52, a global variable mapping table may be constructed according to the content of the global variable declaration area node.
[0111] In step S88, when the first identifier currently being processed is found in the global variable mapping table, it indicates that it is a global variable. At this point, a local variable mapping table can be further created for the global variable. It can be understood that the local variable mapping table is mainly used for the second database.
[0112] In step S102, a recursive return algorithm is used to traverse the modified syntax parse tree. During the traversal process, the modified first identifier is spliced with the corresponding node of the syntax parse tree according to the results of the local variable mapping table and the global variable mapping table. For global variables, it is necessary to refer to the local variable mapping table to determine the mapping relationship. Finally, a third query language compatible with the first database and the second database is generated.
[0113] For details, please refer to Figure 6 , step S52 comprises:
[0114] Step S522: Determine the variable information of each global variable in the global variable declaration area node, the variable information includes the variable name, operation type and complete definition.
[0115] Step S524: Based on the variable information of each global variable, multiple key-value pairs are constructed, where the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition.
[0116] Step S526: Determine a global variable mapping table based on multiple key-value pairs.
[0117] In step S522, the variable name may be a unique identifier of the global variable. The operation type may be an operation type supported by the variable. The complete definition may be a data type, a default value, a scope, etc. of the variable.
[0118] In step S524, the key of the key-value pair may be the name of the global variable, and the value of the key-value pair may be the complete definition of the variable and the operation type, etc. By constructing multiple key-value pairs, each global variable is uniquely identified as a key-value pair, which facilitates subsequent processing and searching.
[0119] In step S526, after multiple key-value pairs are constructed, the multiple key-value pairs can be organized into a global variable mapping table. Through the global variable mapping table, the detailed information of any global variable can be quickly found, including its name, operation type and complete definition.
[0120] The following is an example of the specific implementation process of the above steps. Global variables are allowed to be declared in Oracle packages, but global variables are not allowed to be declared in TeleDB for xscale packages. Local variables can only be declared in functions or stored procedures of TeleDB for xscale packages. When the third query language is constructed, global variables need to be placed in the local variable declaration area of the specific function or stored procedure used. First, the Oracle package is parsed and a grammar parsing tree is generated. Then, when the package global variable declaration area subtree is traversed, the global variable list is structurally parsed. For example, the syntax tree generated by antlr4 can be used to traverse the visitor code for the package global variable declaration subtree, and then parse out global variables one by one (including variable name, operation type, and complete definition), and then build a global variable mapping table (Global Variable Table), and put the parsed global variables into the global variable mapping table (the key in the table is the variable name, the value is the complete definition of the variable and the operation type, and Delete indicates that the global variable is to be deleted). Then, continue to traverse the function or stored procedure subtree in the package body. If a function or stored procedure is found to use a global variable (queried from the global variable mapping table according to the variable name) during the traversal process, then a local variable mapping table for the function or stored procedure is constructed, and the global variable is copied to the local variable mapping table and marked as a newly added local variable. Finally, when completing the third query language assembly of the function or stored procedure, the global variable copied from the global variable mapping table is added to the local variable subtree (each function or stored procedure in a package has its own local variable definition subtree), thereby completing the migration of global variables to local variables.
[0121] In this embodiment, by determining the global variable mapping table based on the global variable declaration area node, a local variable mapping table can be quickly formed, thereby realizing the process of migrating global variables to local variables.
[0122] In the second example, see Figure 7 , step S8 comprises:
[0123] Step S810: When the first identifier includes a loop node, determine an assignment statement in the loop node.
[0124] Step S812: reconstruct the assignment statement using the preset rules in the second identifier to form a modified first identifier.
[0125] In step S810 to step S812, as an example, the loop node may be a for loop, the assignment statement may be two parameters, such as a subscript and a set in the for loop, etc., and the preset rule may be the for loop syntax of TeleDB for xscale, etc.
[0126] The following is an example of the specific implementation process of the above steps. For loops may be used in functions or stored procedures in a package. The for loop syntax of Oracle and TeleDB for xscale is compatible, but if the xml data type related system functions are used, syntax conversion is required. First, the parse tree visitor code generated by antlr4 can be used to perform structural parsing when traversing the for loop control statement subtree to parse out the subscript, loop lower bound, loop upper bound, and the collection involved. Then, traverse the loop body subtree to see if the assignment statement involves both subscript and collection parameters. Then, the local variable of the assignment statement is used as the parameter variable of the target for loop statement, and the for loop statement is reconstructed into the for loop control statement of TeleDB for xscale. After the reconstruction is completed, the SQL statement of the for loop is assembled.
[0127] In this embodiment, by setting preset rules to reconstruct assignment statements, it is ensured that all for loops are converted according to the same rules, thereby maintaining the consistency and readability of the code. The automated migration can also reduce errors caused by manual operations and improve the generation quality of the third query language.
[0128] In the third embodiment, in Oracle (the first database), XMLPARSER is a built-in type or object for processing XML data. However, in TeleDB for xscale (the second database), there is no type or object directly corresponding to Oracle's XMLPARSER. Therefore, when generating the third query language, it is necessary to delete all variables declared as XMLPARSER types. At the same time, it is necessary to find alternative methods to process XML data, for example, including using the XML functions provided by TeleDB for xscale, or converting the XML data into other formats for processing.
[0129] In the fourth embodiment, in Oracle SQL (the first query language), the loop variable of the for loop usually does not need to be explicitly declared. However, in TeleDB for xscale SQL (the second query language), the loop variable of the for loop usually needs to be explicitly declared. This means that during the migration process, the declaration of the loop variable needs to be added in the variable declaration area. Therefore, when generating the third query language, it is necessary to add an explicit declaration of the for loop variable in the appropriate location (usually the variable declaration area).
[0130] In the fifth embodiment, the xmldom.getNodeValue method in Oracle is used to obtain the value of the XML node, while in TeleDB for Xscale, the mode of obtaining the XML node value may be different. TeleDB for Xscale uses "::varchar" to force a string when processing XML. Therefore, when using ANTLR4 to migrate the code from Oracle to TeleDB forXscale, when ANTLR4 traverses and accesses the subtree called by the xmldom.getNodeValue method in the syntax tree, xmldom.getNodeValue can be replaced by adding "::varchar" to its parameter.
[0131] In the sixth embodiment, the USERENV system function on the Oracle side is used to obtain environment variables, which can be replaced by obtaining environment variables through the pg_settings system table on the TeleDB for Xscale side. As an example, when the syntax tree parsing code generated by antlr4 is parsed to the USERENV function call subtree, the function parameters are extracted, and then replaced with the "select setting into variable from pg_settings where name=parameter" subquery when splicing the third query language.
[0132] In one embodiment, see Figure 8 , step S6 comprises:
[0133] Step S62: Determine a target node in the syntax parse tree of the first query language.
[0134] Step S64: Determine the function name and / or parameter list of the target node.
[0135] Step S66: Determine the function name and / or parameter list as the first identifier.
[0136] In step S62, the target node is used to represent a specific function, or a node of a program structure for further processing.
[0137] In step S64, the target node is structurally parsed to extract information such as the function name and / or parameter list of the target node.
[0138] In step S66, after the function name and the parameter list are extracted, the function name and the parameter list are determined as the first identifier.
[0139] In this embodiment, by performing structured parsing on the target node, the function name and / or parameter list of the target node can be determined as the first identifier, so that a specific subtree in the syntax tree can be accurately located and identified, and complex subtrees can be decomposed so that each subtree can be independently analyzed and processed, thereby reducing the complexity of the overall processing.
[0140] In one embodiment, see Fig. 9 , step S8 comprises:
[0141] Step S814: Obtain a preset node list and the current node.
[0142] Step S816: When the current node is found in the node list, the current node is determined as the target node.
[0143] In step S814 to step S816, the preset node list may be a set of nodes defined or acquired in advance. The preset node list may include an identifier of the first query language that needs to be replaced. The current node may refer to the node being processed, for example, the subtree currently located when traversing the syntax tree. Afterwards, if the current node is successfully queried in the node list, the current node is determined as the target node.
[0144] In this embodiment, by presetting a node list and searching the current node therefrom when needed, the target node can be searched in a targeted manner, thereby improving processing efficiency. Moreover, the preset node list provides a clear search range, which helps to reduce the risk of misjudgment and misoperation, thereby improving processing accuracy.
[0145] 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.
[0146] Based on the same inventive concept, the embodiment of the present application also provides a database query language generation device for implementing the database query language generation 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 language generation device embodiments provided below can refer to the limitations of the database query language generation method above, and will not be repeated here.
[0147] In an exemplary embodiment, Fig.11 As shown, a database query language generation device is provided, including: an acquisition module, a generation module, a determination module, a modification module and a splicing module, wherein:
[0148] The acquisition module is used to acquire a first query language of the first database and a second query language of the second database, and acquire a conversion mapping table between the first query language and the second query language.
[0149] The generating module is used to generate a syntax parsing tree of the first query language.
[0150] The determination module is used to determine a first identifier in a syntax parsing tree of a first query language.
[0151] The modification module is used to determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier.
[0152] The concatenation module is used to concatenate the syntax parse tree and the modified first identifier based on a recursive return algorithm to generate a third query language.
[0153] In one embodiment, the modification module is further configured to, when it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, cause the second identifier to replace the first identifier.
[0154] In one embodiment, the modification module is further configured to delete the first identifier from the syntax parse tree when it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule.
[0155] In one embodiment, the modification module is further configured to generate prompt information when it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule.
[0156] In one embodiment, the database query language generation device includes a declaration module. The declaration module is used to determine the global variable declaration area node in the syntax parse tree; based on the global variable declaration area node, determine the global variable mapping table. The modification module is also used to form a local variable mapping table of the first identifier when the global variable mapping table queries the first identifier. The splicing module is also used to splice the syntax parse tree and the modified first identifier based on the local variable mapping table and the recursive return algorithm to generate a third query language.
[0157] In one embodiment, the declaration module is also used to determine the variable information of each global variable in the global variable declaration area node, the variable information including the variable name, operation type and complete definition; based on the variable information of each global variable, construct multiple key-value pairs, the key value in the key-value pair includes the variable name, the value of the value in the key-value pair includes the corresponding operation type and complete definition; based on multiple key-value pairs, determine the global variable mapping table.
[0158] In one embodiment, the modification module is further used to determine an assignment statement in a loop node when the first identifier includes a loop node; and reconstruct the assignment statement using a preset rule in the second identifier to form a modified first identifier.
[0159] In one embodiment, the determination module is further used to determine a target node in a syntax parse tree of the first query language; determine a function name and / or parameter list of the target node; and determine the function name and / or parameter list as a first identifier.
[0160] In one embodiment, the determination module is further used to obtain a preset node list and a current node; when the current node is found in the node list, the current node is determined as the target node.
[0161] Each module in the above-mentioned database query language generation device can be implemented in whole or in part by software, hardware and a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each of the above modules.
[0162] In addition, the present application may also include other modules, such as a first module, a second module, etc., for implementing other steps, functions, etc. of the present application.
[0163] 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. Fig.12 As 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 database query language generation data. 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 language generation method is implemented.
[0164] Those skilled in the art will understand that Fig.12 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.
[0165] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:
[0166] Step S2: obtaining a first query language of the first database and a second query language of the second database, and obtaining a conversion mapping table between the first query language and the second query language.
[0167] Step S4: Generate a syntax parse tree of the first query language.
[0168] Step S6: Determine a first identifier of a target node in a syntax parse tree of the first query language.
[0169] Step S8: Determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier.
[0170] Step S10: Based on the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0171] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0172] Step S82: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
[0173] Step S84: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, delete the first identifier in the syntax parsing tree.
[0174] Step S86: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, generate prompt information.
[0175] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0176] Step S5: Determine the global variable declaration area node in the syntax parse tree.
[0177] Step S52: Determine a global variable mapping table based on the global variable declaration area node.
[0178] Step S88: When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed.
[0179] Step S102: Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0180] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0181] Step S522: Determine the variable information of each global variable in the global variable declaration area node, the variable information including the variable name, operation type and complete definition.
[0182] Step S524: Based on the variable information of each global variable, multiple key-value pairs are constructed, where the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition.
[0183] Step S526: Determine a global variable mapping table based on multiple key-value pairs.
[0184] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0185] Step S810: When the first identifier includes a loop node, determine an assignment statement in the loop node.
[0186] Step S812: reconstruct the assignment statement using the preset rules in the second identifier to form a modified first identifier.
[0187] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0188] Step S62: Determine a target node in the syntax parse tree of the first query language.
[0189] Step S64: Determine the function name and / or parameter list of the target node.
[0190] Step S66: Determine the function name and / or parameter list as the first identifier.
[0191] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0192] Step S814: Obtain a preset node list and the current node.
[0193] Step S816: When the current node is found in the node list, the current node is determined as the target node.
[0194] In one embodiment, a computer readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
[0195] Step S2: obtaining a first query language of the first database and a second query language of the second database, and obtaining a conversion mapping table between the first query language and the second query language.
[0196] Step S4: Generate a syntax parse tree of the first query language.
[0197] Step S6: Determine a first identifier of a target node in a syntax parse tree of the first query language.
[0198] Step S8: Determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier.
[0199] Step S10: Based on the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0200] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0201] Step S82: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
[0202] Step S84: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, delete the first identifier in the syntax parsing tree.
[0203] Step S86: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, generate prompt information.
[0204] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0205] Step S5: Determine the global variable declaration area node in the syntax parse tree.
[0206] Step S52: Determine a global variable mapping table based on the global variable declaration area node.
[0207] Step S88: When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed.
[0208] Step S102: Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0209] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0210] Step S522: Determine the variable information of each global variable in the global variable declaration area node, the variable information including the variable name, operation type and complete definition.
[0211] Step S524: Based on the variable information of each global variable, multiple key-value pairs are constructed, where the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition.
[0212] Step S526: Determine a global variable mapping table based on multiple key-value pairs.
[0213] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0214] Step S810: When the first identifier includes a loop node, determine an assignment statement in the loop node.
[0215] Step S812: reconstruct the assignment statement using the preset rules in the second identifier to form a modified first identifier.
[0216] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0217] Step S62: Determine a target node in the syntax parse tree of the first query language.
[0218] Step S64: Determine the function name and / or parameter list of the target node.
[0219] Step S66: Determine the function name and / or parameter list as the first identifier.
[0220] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0221] Step S814: Obtain a preset node list and the current node.
[0222] Step S816: When the current node is found in the node list, the current node is determined as the target node.
[0223] In one embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the following steps:
[0224] Step S2: obtaining a first query language of the first database and a second query language of the second database, and obtaining a conversion mapping table between the first query language and the second query language.
[0225] Step S4: Generate a syntax parse tree of the first query language.
[0226] Step S6: Determine a first identifier of a target node in a syntax parse tree of the first query language.
[0227] Step S8: Determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier.
[0228] Step S10: Based on the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0229] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0230] Step S82: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
[0231] Step S84: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, delete the first identifier in the syntax parsing tree.
[0232] Step S86: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, generate prompt information.
[0233] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0234] Step S5: Determine the global variable declaration area node in the syntax parse tree.
[0235] Step S52: Determine a global variable mapping table based on the global variable declaration area node.
[0236] Step S88: When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed.
[0237] Step S102: Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
[0238] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0239] Step S522: Determine the variable information of each global variable in the global variable declaration area node, the variable information includes the variable name, operation type and complete definition.
[0240] Step S524: Based on the variable information of each global variable, multiple key-value pairs are constructed, where the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition.
[0241] Step S526: Determine a global variable mapping table based on multiple key-value pairs.
[0242] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0243] Step S810: When the first identifier includes a loop node, determine an assignment statement in the loop node.
[0244] Step S812: reconstruct the assignment statement using the preset rules in the second identifier to form a modified first identifier.
[0245] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0246] Step S62: Determine a target node in the syntax parse tree of the first query language.
[0247] Step S64: Determine the function name and / or parameter list of the target node.
[0248] Step S66: Determine the function name and / or parameter list as the first identifier.
[0249] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0250] Step S814: Obtain a preset node list and the current node.
[0251] Step S816: When the current node is found in the node list, the current node is determined as the target node.
[0252] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method 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 the present application can include at least one of non-volatile memory 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), magnetic 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, an artificial intelligence (AI) processor, etc., but are not limited to this.
[0253] 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 application.
[0254] 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 generating a database query language, characterized in that: The method comprises: Obtaining a first query language of a first database and a second query language of a second database, and obtaining a conversion mapping table between the first query language and the second query language; Generate a syntax parse tree of the first query language; Determine a first identifier of a target node in a syntax parse tree of the first query language; Determining a second identifier corresponding to the first identifier in the conversion mapping table, and modifying the first identifier based on the second identifier; Based on a recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
2. The method for generating a database query language according to claim 1, characterized in that: The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a replacement rule, the first identifier is replaced by the second identifier.
3. The method for generating a database query language according to claim 1, characterized in that: The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a deletion rule, the first identifier is deleted from the syntax parse tree.
4. The method for generating a database query language according to claim 1, characterized in that: The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes: When it is determined in the conversion mapping table that the second identifier corresponding to the first identifier includes a prompt rule, prompt information is formed.
5. The method for generating a database query language according to claim 1, characterized in that: After generating the syntax parse tree of the first query language, the method further comprises: Determine a global variable declaration area node in the syntax parse tree; Determine a global variable mapping table based on the global variable declaration area node; The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes: When the first identifier is found in the global variable mapping table, a local variable mapping table of the first identifier is formed; The step of combining the modified first identifier and the grammar parse tree based on a recursive return algorithm to generate a third query language includes: Based on the local variable mapping table and the recursive return algorithm, the syntax parse tree and the modified first identifier are concatenated to generate a third query language.
6. The method for generating a database query language according to claim 5, characterized in that: The determining of the global variable mapping table based on the global variable declaration area node comprises: Determine variable information of each global variable in the global variable declaration area node, the variable information including variable name, operation type and complete definition; Based on the variable information of each global variable, construct a plurality of key-value pairs, wherein the key value in the key-value pair includes the variable name, and the value in the key-value pair includes the corresponding operation type and the complete definition; Based on the multiple key-value pairs, a global variable mapping table is determined.
7. The method for generating a database query language according to claim 1, characterized in that: The determining, in the conversion mapping table, a second identifier corresponding to the first identifier, and modifying the first identifier based on the second identifier includes: In a case where the first identifier includes a loop node, determining an assignment statement in the loop node; The assignment statement is reconstructed using a preset rule in the second identifier to form a modified first identifier.
8. The method for generating a database query language according to claim 1, characterized in that: The step of determining a first identifier of a target node in a syntax parse tree of the first query language includes: Determining the target node in a syntax parse tree of the first query language; Determine the function name and / or parameter list of the target node; The function name and / or the parameter list is determined as the first identifier.
9. The method for generating a database query language according to claim 1, characterized in that: The determining the first identifier of the target node in the syntax parse tree of the first query language includes: Get the preset node list and the current node; When the current node is found in the node list, the current node is determined as the target node.
10. A database query language generation device, characterized in that: The device comprises: An acquisition module, configured to acquire a first query language of a first database and a second query language of a second database, and acquire a conversion mapping table between the first query language and the second query language; A generating module, used for generating a syntax parsing tree of the first query language; A determination module, configured to determine a first identifier in a syntax parse tree of the first query language; a modification module, configured to determine a second identifier corresponding to the first identifier in the conversion mapping table, and modify the first identifier based on the second identifier; A concatenation module is used to concatenate the syntax parse tree and the modified first identifier based on a recursive return algorithm to generate a third query language.
Citation Information
Patent Citations
Language conversion method and device of database, electronic equipment and storage medium
CN111061757A
SQL statement generation method and device, computer equipment and storage medium
CN118568123A
Natural language query interface, systems, and methods for a database
US20080235199A1