CN117762973A - Database grammar conversion method, device, electronic equipment and computer readable medium - Google Patents

Database grammar conversion method, device, electronic equipment and computer readable medium Download PDF

Info

Publication number
CN117762973A
CN117762973A CN202311707812.5A CN202311707812A CN117762973A CN 117762973 A CN117762973 A CN 117762973A CN 202311707812 A CN202311707812 A CN 202311707812A CN 117762973 A CN117762973 A CN 117762973A
Authority
CN
China
Prior art keywords
database
dynamic
sql statement
database sql
file
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311707812.5A
Other languages
Chinese (zh)
Inventor
许少伟
魏聪惠
黄献景
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Construction Bank Corp
CCB Finetech Co Ltd
Original Assignee
China Construction Bank Corp
CCB Finetech Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Construction Bank Corp, CCB Finetech Co Ltd filed Critical China Construction Bank Corp
Priority to CN202311707812.5A priority Critical patent/CN117762973A/en
Publication of CN117762973A publication Critical patent/CN117762973A/en
Pending legal-status Critical Current

Links

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a database grammar conversion method, a device, electronic equipment and a computer readable medium, and relates to the technical field of database grammar. The method comprises the following steps: writing a first database SQL sentence carrying a dynamic tag into a file, and writing text contents of each row in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute; extracting a first database SQL sentence which does not carry a dynamic tag from the file; converting the first database SQL statement without the dynamic label into a second database SQL statement without the dynamic label; and replacing the second database SQL statement without the dynamic label into the sequence table, so as to obtain the second database SQL statement with the dynamic label. The implementation method can solve the technical problem that the database statement conversion carrying the dynamic tag is not supported.

Description

Database grammar conversion method, device, electronic equipment and computer readable medium
Technical Field
The present invention relates to the field of database grammar, and in particular, to a method, an apparatus, an electronic device, and a computer readable medium for converting a database grammar.
Background
SQL grammar conversion refers to the act of converting one SQL grammar to another SQL grammar.
This typically occurs in several cases:
(1) Database migration: when migrating from one database platform to another, it may be necessary to convert SQL statements from one database language to another. For example, from Oracle to Tidb may require conversion of some function or definition of data type.
(2) Performance optimization: sometimes, converting a complex SQL query into another form may improve its performance. For example, some queries may perform poorly on a query optimizer of one database management system, but perform well on another database management system.
(3) Data integration: in a data integration process, SQL queries from multiple source systems may be translated into a unified query language for ease of processing and understanding.
(4) And (3) code reconstruction: during the code reconstruction process, existing SQL queries may need to be reorganized or rewritten to improve readability and maintainability.
In the process of implementing the present invention, the inventor finds that at least the following problems exist in the prior art:
the data interaction DAO layer of a general system integrates ORM frameworks, such as mybatis and the like, which can carry dynamic labels, and the prior art does not support database statement conversion carrying dynamic labels.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method, an apparatus, an electronic device, and a computer readable medium for converting a database grammar, so as to solve the technical problem that the database statement conversion carrying a dynamic tag is not supported.
To achieve the above object, according to one aspect of the embodiments of the present invention, there is provided a database syntax conversion method, including:
writing a first database SQL sentence carrying a dynamic tag into a file, and writing text contents of each row in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute;
extracting a first database SQL sentence which does not carry a dynamic tag from the file;
converting the first database SQL statement without the dynamic label into a second database SQL statement without the dynamic label;
and replacing the second database SQL statement without the dynamic label into the sequence table, so as to obtain the second database SQL statement with the dynamic label.
Optionally, the file is an extensible markup language file;
extracting a first database SQL sentence which does not carry a dynamic tag from the file, wherein the first database SQL sentence comprises:
Converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label;
traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
Optionally, replacing the second database SQL statement that does not carry a dynamic tag into the sequence table, thereby obtaining a second database SQL statement that carries a dynamic tag, including:
according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
Optionally, converting the first database SQL statement without the dynamic label into the second database SQL statement without the dynamic label includes:
generating an abstract syntax tree according to the first database SQL statement without the dynamic tag;
traversing each node of the abstract syntax tree, judging whether a function or a keyword of a first database is hit or not for each node, and if yes, replacing a first database SQL sentence in the node with a second database SQL sentence;
the abstract syntax tree replaced with the second database SQL statement is converted into a second database SQL statement that does not carry a dynamic tag.
Optionally, replacing the first database SQL statement in the node with the second database SQL statement includes:
extracting functions and parameters of the first database from the nodes according to grammar rules of the first database;
and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database.
Optionally, after converting the abstract syntax tree replaced with the second database SQL statement into the second database SQL statement without the dynamic label, the method further comprises:
Generating an abstract syntax tree according to the SQL statement of the second database without the dynamic tag;
and if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct.
In addition, according to another aspect of an embodiment of the present invention, there is provided a database syntax converting apparatus including:
the writing module is used for writing the SQL statement of the first database carrying the dynamic tag into the file and writing the text content of each row in the file into the sequence table; wherein the dynamic tag is written in the file in the form of an attribute;
the extraction module is used for extracting a first database SQL sentence which does not carry a dynamic tag from the file;
the conversion module is used for converting the first database SQL statement without the dynamic tag into a second database SQL statement without the dynamic tag;
and the replacing module is used for replacing the second database SQL statement without the dynamic label into the sequence table so as to obtain the second database SQL statement with the dynamic label.
Optionally, the file is an extensible markup language file;
the extraction module is also used for:
Converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label;
traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
Optionally, the replacing module is further configured to:
according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
Optionally, the conversion module is further configured to:
generating an abstract syntax tree according to the first database SQL statement without the dynamic tag;
Traversing each node of the abstract syntax tree, judging whether a function or a keyword of a first database is hit or not for each node, and if yes, replacing a first database SQL sentence in the node with a second database SQL sentence;
the abstract syntax tree replaced with the second database SQL statement is converted into a second database SQL statement that does not carry a dynamic tag.
Optionally, the conversion module is further configured to:
extracting functions and parameters of the first database from the nodes according to grammar rules of the first database;
and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database.
Optionally, the conversion module is further configured to:
after converting the abstract syntax tree replaced by the second database SQL sentence into a second database SQL sentence without a dynamic tag, generating the abstract syntax tree according to the second database SQL sentence without the dynamic tag;
and if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct.
According to another aspect of an embodiment of the present invention, there is also provided an electronic device including:
One or more processors;
storage means for storing one or more programs,
the one or more processors implement the method of any of the embodiments described above when the one or more programs are executed by the one or more processors.
According to another aspect of an embodiment of the present invention, there is also provided a computer readable medium having stored thereon a computer program which, when executed by a processor, implements the method according to any of the embodiments described above.
According to another aspect of embodiments of the present invention, there is also provided a computer program product comprising a computer program which, when executed by a processor, implements the method according to any of the embodiments described above.
One embodiment of the above invention has the following advantages or benefits: the technical means that the first database SQL statement carrying the dynamic tag is written into the file, the text content of each line in the file is written into the sequence table, then the first database SQL statement not carrying the dynamic tag is extracted from the file, then the first database SQL statement not carrying the dynamic tag is converted into the second database SQL statement not carrying the dynamic tag, and finally the second database SQL statement not carrying the dynamic tag is replaced into the sequence table is adopted, so that the technical problem that the database statement conversion carrying the dynamic tag is not supported in the prior art is solved. According to the embodiment of the invention, the first database SQL statement without the dynamic tag is extracted by temporarily removing the dynamic tag, and is connected with the dynamic tag after the grammar conversion is completed, so that the database statement conversion with the dynamic tag can be supported.
Further effects of the above-described non-conventional alternatives are described below in connection with the embodiments.
Drawings
In order to more clearly illustrate the embodiments of the invention or the technical solutions in the prior art, the drawings that are required in the embodiments or the description of the prior art will be briefly described, it being obvious that the drawings in the following description are only some embodiments of the invention, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art. Wherein:
FIG. 1 is a flowchart of a database grammar conversion method according to an embodiment of the present invention;
FIG. 2 is a flow chart of a database grammar conversion method according to one referenceable embodiment of the invention;
FIG. 3 is a flow chart of a database grammar conversion method according to another referenceable embodiment of the present invention;
FIG. 4 is a schematic diagram of a grammar conversion for bulk SQL files in accordance with the present invention;
FIG. 5 is a schematic diagram of a database grammar conversion apparatus according to an embodiment of the present invention;
FIG. 6 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
fig. 7 is a schematic diagram of a computer system suitable for use in implementing an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention will now be described with reference to the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding, and are to be considered merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
In the technical scheme of the invention, the aspects of acquisition, analysis, use, transmission, storage and the like of the related user personal information all meet the requirements of related laws and regulations, are used for legal and reasonable purposes, are not shared, leaked or sold outside the aspects of legal use and the like, and are subjected to supervision and management of a supervision department. Necessary measures should be taken for the personal information of the user to prevent illegal access to such personal information data, ensure that personnel having access to the personal information data comply with the regulations of the relevant laws and regulations, and ensure the personal information of the user. Once these user personal information data are no longer needed, the risk should be minimized by limiting or even prohibiting the data collection and/or deletion.
User privacy is protected, when applicable, by de-identifying the data, including in some related applications, such as by removing a particular identifier (e.g., date of birth, etc.), controlling the amount or specificity of stored data, controlling how the data is stored, and/or other methods.
Fig. 1 is a flowchart of a database syntax conversion method according to an embodiment of the present invention. As an embodiment of the present invention, as shown in fig. 1, the database syntax conversion method may include:
step 101, writing a first database SQL sentence carrying a dynamic tag into a file, and writing text contents of each row in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute.
In general, the data interaction DAO layer of the system integrates ORM frameworks, such as mybatis (persistent layer frameworks) and the like, and the frameworks can carry dynamic labels.
First, a first database SQL sentence (such as an Oracle SQL sentence) carrying a dynamic tag is written into a file, text contents of each row in the file are written into a sequence table, and the sequence table is stored in a memory. It should be noted that the sequence table may be understood as a snapshot.
Optionally, the file is an extensible markup language file, that is, an xml file, and the sequence table is a list table. In SQL sentences, dynamic labels start with "<" and end with ">, and contents between" < "and" > "are identified as the properties of xml in an xml file, so that mybatis dynamic labels are temporarily removed by means of the xml file, and the problem of subsequent SQL analysis is avoided.
In order to enable the SQL sentence after the subsequent conversion to be in seamless connection with the dynamic label, the embodiment of the invention records the text content of each row of the SQL sentence (namely the original SQL sentence) of the first database carrying the dynamic label in a list object, wherein the list is an ordered list, can store the character strings of each row, and has a row number.
And 102, extracting a first database SQL statement which does not carry a dynamic tag from the file.
After the first database SQL statement carrying the dynamic tag is written into the xml file, text content which does not comprise the dynamic tag, namely the first database SQL statement which does not carry the dynamic tag, is extracted from the xml file, so that the purpose of temporarily removing the mybatis dynamic tag is achieved.
Optionally, step 102 may include: converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label; traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank. In the embodiment of the invention, firstly, text content in an XML file is converted into a DOM node tree, and for each node of the DOM node tree, the text content of the node is a first database SQL sentence without a dynamic label, and the attribute of the node is the dynamic label.
For example, the attributes (i.e., dynamic labels) of the nodes are as follows:
<if test=”@com.base.data.MybatisUtil@isNotEmpty(nisId)”>
for example, the text content of the node (i.e., the first database SQL statement that does not carry a dynamic tag) is as follows:
and record.NID_ID=#{nisId}
then, through calling a node. GetTextContent () function, traversing each node of the DOM node tree, and extracting text content of each node, thereby obtaining a first database SQL sentence without a dynamic label. It should be noted that, in the extracted first database SQL statement that does not carry a dynamic tag, the node attribute (i.e., the dynamic tag) is blank, so as to prevent the dynamic tag from being lost after the grammar conversion.
And step 103, converting the first database SQL statement without the dynamic label into a second database SQL statement without the dynamic label.
After the first database SQL statement without the dynamic tag is extracted, the first database SQL statement without the dynamic tag is converted into a second database SQL statement without the dynamic tag, such as an SQL statement with Oracle converted into Tidb. It should be noted that, in the converted second database SQL statement, the empty row corresponding to the node attribute is still reserved, so as to be convenient to link with the dynamic label.
And step 104, replacing the second database SQL statement without the dynamic label into the sequence table, thereby obtaining the second database SQL statement with the dynamic label.
After the SQL sentence is subjected to grammar conversion, replacing a second database SQL sentence without a dynamic label (namely, the converted SQL sentence without the dynamic label) into a list object, storing each row of SQL sentences into corresponding positions according to position index in the list object at the moment, connecting the SQL sentences with the dynamic label, finally obtaining the second database SQL sentence with the dynamic label, and finally outputting the list as a text and returning the text to a user.
Optionally, step 104 may include: according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table. Because the list is provided with the line number, the SQL sentence of the second database which does not carry the dynamic label can be replaced into the list object line by line according to the line number of each line in the list object, so that the SQL sentence is connected with the dynamic label, and the SQL sentence of the second database which carries the dynamic label is obtained.
It should be noted that, since the empty line corresponds to the node attribute, that is, the dynamic tag, when the list is replaced, if the empty line is traversed, the empty line is not replaced into the list object, so that the dynamic tag remains in the list object.
Through the processing of the steps, the list is the converted SQL sentence of the second database with the dynamic tag, and then each row of character strings in the list object are connected one by one to form a complete SQL sentence, and the complete SQL sentence is returned to the user.
According to the various embodiments described above, it can be seen that the technical means of the embodiments of the present invention that the technical problem that the conversion of the database statement carrying the dynamic tag is not supported in the prior art is solved by writing the first database SQL statement carrying the dynamic tag into the file and writing the text content of each line in the file into the sequence table, then extracting the first database SQL statement carrying no dynamic tag from the file, then converting the first database SQL statement carrying no dynamic tag into the second database SQL statement carrying no dynamic tag, and finally replacing the second database SQL statement carrying no dynamic tag into the sequence table. According to the embodiment of the invention, the first database SQL statement without the dynamic tag is extracted by temporarily removing the dynamic tag, and is connected with the dynamic tag after the grammar conversion is completed, so that the database statement conversion with the dynamic tag can be supported.
Fig. 2 is a flowchart of a database syntax conversion method according to a reference embodiment of the present invention. As yet another embodiment of the present invention, as shown in fig. 2, step 103 may include the steps of:
step 201, generating an abstract syntax tree according to the first database SQL sentence without the dynamic tag.
Alternatively, antlr4 may be used to convert the first database SQL statement that does not carry a dynamic label into an Abstract Syntax Tree (AST), with AST node types mainly including the three abstract types SQLObject, SQLExpr, SQLStatement. Where SQLObject is an SQL object, the strings in an SQL statement can be classified under this class, SQLExpr is an SQL expression, and is generally used for a where condition, such as where name= 'Zhang Sanj', sqlstatent is a database manipulation language, including SELECT/UPDATE/DELETE/INSERT, etc., and mainly handles add-drop-change.
Step 202, traversing each node of the abstract syntax tree, and judging whether a function or a keyword of a first database is hit or not for each node; if yes, go to step 203; if not, go to step 204.
Specifically, a custom traversal tool (e.g., a monitor) may be used to traverse the contents of each node in the abstract syntax tree, and for each stage, determine whether the contents of the node hit a function or key of the first database.
In some embodiments of the present invention, a mapping transformation model may be pre-constructed, the mapping transformation model defining functions or keywords of the first database, grammar rules of the first database, functions or keywords of the second database, grammar rules of the second database.
Taking Oracle and Tidb as examples, the partial mapping rules of the mapping transformation model are as follows:
it should be noted that, the mapping rule list may be added by user-defined according to actual requirements, and the added rules are as follows:
1) Determining function name orcl_func in Oracle SQL statement
If the function is the listagg function of Oracle, then keyWord is listagg;
2) Determining parameter param of function
If LISTAGG (key, ',') WITHIN GROUP (ORDER BY key), parameters of the sentence are determined to be param1=key, param2= ',', param3=order BY key;
3) Determining a function tidb_func where Tidb can achieve the same function
LISTAGG can be replaced by GROUP_CONCAT function in Tidb, so the objective function is GROUP_CONCAT
4) Populating a parameter param to an objective function
According to the mapping rule, GROUP_CONCAT (param 1 param3SEPARATOR param 2)
The regular configuration of other functions is similarly omitted.
Through configuration of the custom mapping rule, equivalent substitution of various function grammars from Oracle to Tidb can be well expanded, and the correctness and applicability of SQL sentence conversion are improved.
Step 203, replacing the first database SQL statement in the node with the second database SQL statement.
Optionally, step 203 may include: extracting functions and parameters of the first database from the nodes according to grammar rules of the first database; and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database. How the node content hits the function or key word of Oracle, the grammar rule of Oracle and the corresponding Tidb grammar rule are obtained from the mapping conversion model, the function of Oracle and the parameters thereof are extracted from the node content according to the grammar rule of Oracle, the function of Tidb is determined according to the Tidb grammar rule, and the parameters corresponding to the function of Oracle are assigned to the parameters corresponding to the function of Tidb. And finally, replacing the converted Tidb SQL statement with the contents of the nodes, thereby completing the conversion of a function. Other nodes and functions and the like are not described in detail.
Step 204, judging whether there is a node which is not traversed; if yes, go through the next node, and execute step 202; if not, step 205 is performed.
Step 205, converting the abstract syntax tree replaced by the second database SQL statement into the second database SQL statement without the dynamic tag.
After traversing and converting all Oracle functions or keywords, the SQL statement of tidb without labels can be obtained.
In order to ensure the correctness of the grammar conversion, after the grammar conversion is completed, whether the SQL statement of the second database is correct or not needs to be further checked. Optionally, after step 205, the method further includes: generating an abstract syntax tree according to the SQL statement of the second database without the dynamic tag; and if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct. After obtaining a second database SQL sentence without a dynamic tag, converting the second database SQL sentence without the dynamic tag into an Abstract Syntax Tree (AST) by adopting antlr4, and if the abstract syntax tree can be successfully generated, indicating that the syntax conversion is correct and the verification is passed; otherwise, the grammar conversion error is indicated, and the check is not passed.
The embodiment of the invention not only can support the statement conversion of the database carrying the dynamic tag, but also can support the grammar conversion of the custom mapping rule, and the custom mapping rule can be flexibly configured, and even some functions with complex functions (such as listagg, nextval, MONTHS _BETWEEN, nvl2 and the like) can realize the grammar conversion.
In addition, in one embodiment of the present invention, the implementation of the database syntax conversion method has been described in detail in the above description, and thus the description thereof will not be repeated here.
Fig. 3 is a flowchart of a database syntax conversion method according to another exemplary embodiment of the present invention. As another embodiment of the present invention, as shown in fig. 3, the database syntax conversion method includes the steps of:
step 301, writing a first database SQL sentence carrying a dynamic tag into an xml file, and writing text contents of each row in the xml file into a list sequence table; wherein the dynamic tag is written in the xml file in the form of an attribute.
Step 302, converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, text content of the nodes is a first database SQL sentence without a dynamic label, and attributes of the nodes are the dynamic labels.
Step 303, traversing each node of the DOM node tree, extracting text content of each node, thereby obtaining the first database SQL statement without dynamic labels; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
Step 304, converting the first database SQL statement without the dynamic tag into a second database SQL statement without the dynamic tag.
Step 305, replacing the second database SQL statement without the dynamic tag into the list sequence table row by row according to the row number of each row in the list sequence table, thereby obtaining a second database SQL statement with the dynamic tag; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
As shown in FIG. 4, in order to improve the conversion efficiency, the embodiment of the invention also provides a batch conversion function, so that a user can upload SQL files in batches and also can input catalogues of the SQL files. In addition, the user may select "normal SQL statement conversion" or "SQL statement conversion with dynamic tag", if the user selects "normal SQL statement conversion", only step 304 is required to be executed, and if the user selects "SQL statement conversion with dynamic tag", steps 301 to 305 are executed. Therefore, the embodiment of the invention supports the grammar conversion of single SQL files and batch SQL files.
The embodiment of the invention also supports various integrated use modes, including visual interface operation, server side API call, SDK integration and the like, and meets various requirements of users.
Fig. 5 is a schematic diagram of a database syntax converting apparatus according to an embodiment of the present invention. As shown in fig. 5, the database grammar conversion apparatus 500 includes a writing module 501, an extracting module 502, a conversion module 503, and a replacing module 504; the writing module 501 is configured to write a first database SQL statement carrying a dynamic tag into a file, and write text content of each line in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute; the extracting module 502 is configured to extract a first database SQL statement that does not carry a dynamic tag from the file; the conversion module 503 is configured to convert the first database SQL statement that does not carry a dynamic tag into a second database SQL statement that does not carry a dynamic tag; the replacing module 504 is configured to replace the second database SQL statement that does not carry a dynamic tag with the sequence table, thereby obtaining the second database SQL statement that carries a dynamic tag.
Optionally, the file is an extensible markup language file;
The extraction module 502 is further configured to:
converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label;
traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
Optionally, the replacing module 504 is further configured to:
according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
Optionally, the conversion module 503 is further configured to:
Generating an abstract syntax tree according to the first database SQL statement without the dynamic tag;
traversing each node of the abstract syntax tree, judging whether a function or a keyword of a first database is hit or not for each node, and if yes, replacing a first database SQL sentence in the node with a second database SQL sentence;
the abstract syntax tree replaced with the second database SQL statement is converted into a second database SQL statement that does not carry a dynamic tag.
Optionally, the conversion module 503 is further configured to:
extracting functions and parameters of the first database from the nodes according to grammar rules of the first database;
and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database.
Optionally, the conversion module 503 is further configured to:
after converting the abstract syntax tree replaced by the second database SQL sentence into a second database SQL sentence without a dynamic tag, generating the abstract syntax tree according to the second database SQL sentence without the dynamic tag;
and if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct.
The specific implementation of the database grammar conversion device of the present invention has been described in detail in the database grammar conversion method, and thus the description thereof will not be repeated here.
Fig. 6 illustrates an exemplary system architecture 600 to which a database syntax conversion method or database syntax conversion apparatus of an embodiment of the present invention may be applied.
As shown in fig. 6, the system architecture 600 may include terminal devices 601, 602, 603, a network 604, and a server 605. The network 604 is used as a medium to provide communication links between the terminal devices 601, 602, 603 and the server 605. The network 604 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
A user may interact with the server 605 via the network 604 using the terminal devices 601, 602, 603 to receive or send messages, etc. Various communication client applications such as shopping class applications, web browser applications, search class applications, instant messaging tools, mailbox clients, social platform software, etc. (by way of example only) may be installed on the terminal devices 601, 602, 603.
The terminal devices 601, 602, 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smartphones, tablets, laptop and desktop computers, and the like.
The server 605 may be a server providing various services, such as a background management server (by way of example only) providing support for shopping-type websites browsed by users using terminal devices 601, 602, 603. The background management server can analyze and other data such as the received article information inquiry request and feed back the processing result to the terminal equipment.
It should be noted that, the method for converting a database grammar provided by the embodiment of the present invention is generally executed by the server 605, and accordingly, the device for converting a database grammar is generally disposed in the server 605. The method for converting the database grammar provided by the embodiment of the invention can also be executed by the terminal equipment 601, 602 and 603, and correspondingly, the device for converting the database grammar can be arranged in the terminal equipment 601, 602 and 603.
It should be understood that the number of terminal devices, networks and servers in fig. 6 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
Referring now to FIG. 7, there is illustrated a schematic diagram of a computer system 700 suitable for use in implementing an embodiment of the present invention. The terminal device shown in fig. 7 is only an example, and should not impose any limitation on the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 7, the computer system 700 includes a Central Processing Unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 702 or a program loaded from a storage section 708 into a Random Access Memory (RAM) 703. In the RAM703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, ROM 702, and RAM703 are connected to each other through a bus 704. An input/output (I/O) interface 705 is also connected to bus 704.
The following components are connected to the I/O interface 705: an input section 706 including a keyboard, a mouse, and the like; an output portion 707 including a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, a speaker, and the like; a storage section 708 including a hard disk or the like; and a communication section 709 including a network interface card such as a LAN card, a modem, or the like. The communication section 709 performs communication processing via a network such as the internet. The drive 710 is also connected to the I/O interface 705 as needed. A removable medium 711 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 710 as necessary, so that a computer program read therefrom is mounted into the storage section 708 as necessary.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication portion 709, and/or installed from the removable medium 711. The above-described functions defined in the system of the present invention are performed when the computer program is executed by a Central Processing Unit (CPU) 701.
The computer readable medium shown in the present invention may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, the computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with the computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer programs according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules involved in the embodiments of the present invention may be implemented in software or in hardware. The described modules may also be provided in a processor, for example, as: a processor comprises a writing module, an extracting module, a converting module and a replacing module, wherein the names of the modules do not in some cases constitute a limitation of the module itself.
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by a device, implement the method of: writing a first database SQL sentence carrying a dynamic tag into a file, and writing text contents of each row in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute; extracting a first database SQL sentence which does not carry a dynamic tag from the file; converting the first database SQL statement without the dynamic label into a second database SQL statement without the dynamic label; and replacing the second database SQL statement without the dynamic label into the sequence table, so as to obtain the second database SQL statement with the dynamic label.
As a further aspect, embodiments of the present invention also provide a computer program product comprising a computer program which, when executed by a processor, implements the method according to any of the above embodiments.
According to the technical scheme of the embodiment of the invention, the technical means that the first database SQL statement carrying the dynamic tag is written into the file and the text content of each row in the file is written into the sequence table, then the first database SQL statement not carrying the dynamic tag is extracted from the file, then the first database SQL statement not carrying the dynamic tag is converted into the second database SQL statement not carrying the dynamic tag, and finally the second database SQL statement not carrying the dynamic tag is replaced into the sequence table is adopted, so that the technical problem that the database statement conversion carrying the dynamic tag is not supported in the prior art is solved. According to the embodiment of the invention, the first database SQL statement without the dynamic tag is extracted by temporarily removing the dynamic tag, and is connected with the dynamic tag after the grammar conversion is completed, so that the database statement conversion with the dynamic tag can be supported.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives can occur depending upon design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.

Claims (15)

1. A method for converting a grammar in a database, comprising:
writing a first database SQL sentence carrying a dynamic tag into a file, and writing text contents of each row in the file into a sequence table; wherein the dynamic tag is written in the file in the form of an attribute;
extracting a first database SQL sentence which does not carry a dynamic tag from the file;
converting the first database SQL statement without the dynamic label into a second database SQL statement without the dynamic label;
and replacing the second database SQL statement without the dynamic label into the sequence table, so as to obtain the second database SQL statement with the dynamic label.
2. The method of claim 1, wherein the file is an extensible markup language file;
extracting a first database SQL sentence which does not carry a dynamic tag from the file, wherein the first database SQL sentence comprises:
converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label;
Traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
3. The method of claim 1, wherein replacing the second database SQL statement that does not carry a dynamic tag into the order table, thereby obtaining a second database SQL statement that carries a dynamic tag, comprises:
according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
4. The method of claim 1, wherein converting the first database SQL statement that does not carry a dynamic tag to a second database SQL statement that does not carry a dynamic tag comprises:
Generating an abstract syntax tree according to the first database SQL statement without the dynamic tag;
traversing each node of the abstract syntax tree, judging whether a function or a keyword of a first database is hit or not for each node, and if yes, replacing a first database SQL sentence in the node with a second database SQL sentence;
the abstract syntax tree replaced with the second database SQL statement is converted into a second database SQL statement that does not carry a dynamic tag.
5. The method of claim 4, wherein replacing the first database SQL statement in the node with the second database SQL statement comprises:
extracting functions and parameters of the first database from the nodes according to grammar rules of the first database;
and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database.
6. The method of claim 4, further comprising, after converting the abstract syntax tree replaced with the second database SQL statement into the second database SQL statement without the dynamic label:
generating an abstract syntax tree according to the SQL statement of the second database without the dynamic tag;
And if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct.
7. A database grammar conversion apparatus, comprising:
the writing module is used for writing the SQL statement of the first database carrying the dynamic tag into the file and writing the text content of each row in the file into the sequence table; wherein the dynamic tag is written in the file in the form of an attribute;
the extraction module is used for extracting a first database SQL sentence which does not carry a dynamic tag from the file;
the conversion module is used for converting the first database SQL statement without the dynamic tag into a second database SQL statement without the dynamic tag;
and the replacing module is used for replacing the second database SQL statement without the dynamic label into the sequence table so as to obtain the second database SQL statement with the dynamic label.
8. The apparatus of claim 7, wherein the file is an extensible markup language file;
the extraction module is also used for:
converting text content in the file into a DOM node tree; in the nodes of the DOM node tree, the text content of the nodes is a first database SQL sentence without a dynamic label, and the attribute of the nodes is the dynamic label;
Traversing each node of the DOM node tree, and extracting text content of each node so as to obtain the first database SQL sentence without the dynamic label; in the first database SQL statement without the dynamic label, the attribute corresponding to each node is blank.
9. The apparatus of claim 7, wherein the replacement module is further to:
according to the line number of each line in the sequence table, replacing the second database SQL statement without the dynamic label into the sequence table line by line, so as to obtain the second database SQL statement with the dynamic label; and in the replacing process, if the empty line is traversed in the SQL statement of the second database which does not carry the dynamic tag, the empty line is not replaced into the sequence table, so that the dynamic tag is reserved in the sequence table.
10. The apparatus of claim 7, wherein the conversion module is further configured to:
generating an abstract syntax tree according to the first database SQL statement without the dynamic tag;
traversing each node of the abstract syntax tree, judging whether a function or a keyword of a first database is hit or not for each node, and if yes, replacing a first database SQL sentence in the node with a second database SQL sentence;
The abstract syntax tree replaced with the second database SQL statement is converted into a second database SQL statement that does not carry a dynamic tag.
11. The apparatus of claim 10, wherein the conversion module is further configured to:
extracting functions and parameters of the first database from the nodes according to grammar rules of the first database;
and determining the function of the second database according to the grammar rule of the second database and assigning the parameters to the parameters of the function of the second database.
12. The apparatus of claim 10, wherein the conversion module is further configured to:
after converting the abstract syntax tree replaced by the second database SQL sentence into a second database SQL sentence without a dynamic tag, generating the abstract syntax tree according to the second database SQL sentence without the dynamic tag;
and if the abstract syntax tree is successfully generated, judging that the syntax conversion of the SQL sentence of the second database which does not carry the dynamic label is correct.
13. An electronic device, comprising:
one or more processors;
storage means for storing one or more programs,
the one or more processors implement the method of any of claims 1-6 when the one or more programs are executed by the one or more processors.
14. A computer readable medium, on which a computer program is stored, characterized in that the program, when being executed by a processor, implements the method according to any of claims 1-6.
15. A computer program product comprising a computer program which, when executed by a processor, implements the method according to any of claims 1-6.
CN202311707812.5A 2023-12-12 2023-12-12 Database grammar conversion method, device, electronic equipment and computer readable medium Pending CN117762973A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311707812.5A CN117762973A (en) 2023-12-12 2023-12-12 Database grammar conversion method, device, electronic equipment and computer readable medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311707812.5A CN117762973A (en) 2023-12-12 2023-12-12 Database grammar conversion method, device, electronic equipment and computer readable medium

Publications (1)

Publication Number Publication Date
CN117762973A true CN117762973A (en) 2024-03-26

Family

ID=90315439

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311707812.5A Pending CN117762973A (en) 2023-12-12 2023-12-12 Database grammar conversion method, device, electronic equipment and computer readable medium

Country Status (1)

Country Link
CN (1) CN117762973A (en)

Similar Documents

Publication Publication Date Title
US20230259502A1 (en) Query Processor
US8935230B2 (en) Self-learning semantic search engine
CN109492053B (en) Method and device for accessing data
US11030242B1 (en) Indexing and querying semi-structured documents using a key-value store
KR20190060725A (en) SQL review methods, devices, servers, and storage media
CN105550206B (en) The edition control method and device of structured query sentence
US20130198605A1 (en) Document Merge Based on Knowledge of Document Schema
CN112930529A (en) Generating software artifacts from conceptual data models
CN108287901A (en) Method and apparatus for generating information
CN113760948A (en) Data query method and device
CN114091426A (en) Method and device for processing field data in data warehouse
CN110109983B (en) Method and device for operating Redis database
CN111125064A (en) Method and device for generating database mode definition statement
CN115599386A (en) Code generation method, device, equipment and storage medium
CN109460363B (en) Automatic testing method and device, electronic equipment and computer readable medium
CN111782738B (en) Method and device for constructing database table level blood relationship
CN108694172B (en) Information output method and device
WO2023103432A1 (en) Code file editing method and apparatus, electronic device, and storage medium
CN115510247A (en) Method, device, equipment and storage medium for constructing electric carbon policy knowledge graph
CN108614821B (en) Geological data interconnection and mutual-checking system
CN117762973A (en) Database grammar conversion method, device, electronic equipment and computer readable medium
CN113127496A (en) Method, apparatus, medium, and device for determining change data in database
CN112988778A (en) Method and device for processing database query script
US11748395B2 (en) Developing object ontologies and data usage models using machine learning
US20220114189A1 (en) Extraction of structured information from unstructured documents

Legal Events

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