CN113868284A - Database statement conversion method and device, storage medium and electronic equipment - Google Patents

Database statement conversion method and device, storage medium and electronic equipment Download PDF

Info

Publication number
CN113868284A
CN113868284A CN202111124584.XA CN202111124584A CN113868284A CN 113868284 A CN113868284 A CN 113868284A CN 202111124584 A CN202111124584 A CN 202111124584A CN 113868284 A CN113868284 A CN 113868284A
Authority
CN
China
Prior art keywords
database
field
database statement
statement
syntax
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
CN202111124584.XA
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.)
Beijing Kingsoft Office Software Inc
Zhuhai Kingsoft Office Software Co Ltd
Wuhan Kingsoft Office Software Co Ltd
Original Assignee
Beijing Kingsoft Office Software Inc
Zhuhai Kingsoft Office Software Co Ltd
Wuhan Kingsoft Office Software 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 Beijing Kingsoft Office Software Inc, Zhuhai Kingsoft Office Software Co Ltd, Wuhan Kingsoft Office Software Co Ltd filed Critical Beijing Kingsoft Office Software Inc
Priority to CN202111124584.XA priority Critical patent/CN113868284A/en
Publication of CN113868284A publication Critical patent/CN113868284A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/242Query formulation
    • G06F16/2433Query languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/28Databases characterised by their database models, e.g. relational or object models
    • G06F16/284Relational databases

Landscapes

  • Engineering & Computer Science (AREA)
  • Databases & Information Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Computational Linguistics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a database statement conversion method and device, a storage medium and electronic equipment. Wherein, the method comprises the following steps: acquiring a first database statement which accords with a grammar rule of a first database; extracting syntax elements from the first database statement; determining a type of the first database statement based on the syntax element; based on the conversion rule associated with the type, the first database statement is converted into a second database statement conforming to the syntax rule of the second database. The invention solves the technical problem that the data of the domestic database cannot be directly operated by directly using the MySQL grammar.

Description

Database statement conversion method and device, storage medium and electronic equipment
Technical Field
The invention relates to the field of computers, in particular to a database statement conversion method and device, a storage medium and electronic equipment.
Background
In the prior art, most data are operated in a MySQL relational database system (MySQL for short). And if the domestic database is used for storing data, the data of the domestic database cannot be operated by using a specific MySQL grammar.
In view of the above problems, no effective solution has been proposed.
Disclosure of Invention
The embodiment of the invention provides a database statement conversion method and device, a storage medium and electronic equipment, which at least solve the technical problem that the data of a domestic database cannot be directly operated by using MySQL syntax.
According to an aspect of an embodiment of the present invention, there is provided a database statement conversion method, including: acquiring a first database statement which accords with a grammar rule of a first database; extracting grammatical elements from the first database statement; determining a type of the first database statement based on the syntax element; and converting the first database statement into a second database statement which accords with the grammar rule of a second database based on the conversion rule associated with the type.
According to another aspect of the embodiments of the present invention, there is also provided a database statement conversion apparatus, including: the system comprises an acquisition unit, a processing unit and a processing unit, wherein the acquisition unit is used for acquiring a first database statement which accords with a grammar rule of a first database; an extraction unit configured to extract a syntax element from the first database sentence; a determining unit configured to determine a type of the first database statement based on the syntax element; and the conversion unit is used for converting the first database statement into a second database statement which accords with the grammar rule of a second database based on the conversion rule associated with the type.
As an alternative example, the conversion unit includes: the searching module is used for searching second data corresponding to the first data from a white list under the condition that the type of the first data is a first type; and the calling module is used for calling an interface corresponding to the second type under the condition that the type of the first data is the second type, and converting the first data into the second data by using the interface.
As an optional example, the extracting unit includes: a creation module for creating a first abstract syntax tree; the parsing module is used for parsing the first database statement based on the grammar rule of the first database to obtain a grammar element; and an insertion module, configured to insert each of the syntax elements into the first abstract syntax tree as a node of the first abstract syntax tree.
As an optional example, the determining unit includes: the first determining module is used for determining whether the grammar element is a preset keyword in a first database statement; a second determining module, configured to determine that the type of the first database statement is a first type when all the syntax elements are not the keywords; a third determining module, configured to determine that the type of the first database statement is a second type when the syntax element is the keyword.
As an optional example, the determining unit includes: a first obtaining module, configured to obtain, based on the syntax element, first target data related to the syntax element from a second database; and the fourth determining module is used for determining the type of the first database statement according to the first target data.
As an optional example, the fourth determining module includes: a first determining submodule, configured to determine that the first database statement is of a first type when the first target data does not meet a first condition; and the second determining submodule is used for determining that the first database statement is of the second type under the condition that the first target data meets the first condition.
As an alternative example, the first condition at least includes one of the following: determining from said first target data that said second database has a field set to an automatically updated value; determining from said first target data that said second database has a field that does not match said first database in data type.
As an alternative example, the conversion unit includes: and the first conversion module is used for directly converting the first database statement into a second database statement which accords with the grammar rule of a second database under the condition that the type of the first database statement is determined to be a first type.
As an optional example, the conversion unit further includes: a second obtaining module, configured to, when it is determined that the type of the first database statement is a second type, obtain, based on the syntax element, second target data related to the syntax element from a second database; a modification module, configured to modify a syntax element in the first database statement based on the second target data; and the second conversion module is used for converting the corrected first database statement into a second database statement which accords with the grammar rule of a second database.
As an optional example, the second obtaining module includes: a third determining submodule, configured to determine, as a target field, a field corresponding to the syntax element when the syntax element is a field having a self-organizing function; a fourth determining sub-module, configured to determine a latest field value in the target fields from the second database, as the second target data related to the syntax element from the second database; the correction module comprises: and a replacing module, configured to replace the field value corresponding to the target field in the first database statement with the maximum field value.
As an optional example, the second obtaining module includes: a fifth determining submodule, configured to determine, when the syntax element is a field value in a first field, that a data type of the field value in the first field is a first data type; a sixth determining submodule, configured to determine, from the second database, that the data type of the first field is a second data type, and use the second data type as second target data; the correction module comprises: a conversion sub-module, configured to convert a field value in the first field of the first database statement from the first data type to the second data type.
As an optional example, the second obtaining module includes: a first obtaining sub-module, configured to obtain, from the second database, field information of a second field when the syntax element is the second field, where the second field is a timestamp data type field, and the field information includes information for determining whether the second field has a timestamp automatic update function; a second obtaining sub-module, configured to obtain the second target data related to the syntax element from the second database using the field information; the correction module comprises: a third obtaining sub-module, configured to obtain a current time when it is determined that the second field has an automatic timestamp updating function based on the second target data; and an updating sub-module, configured to update the field value in the second field of the first database statement to the current time.
As an optional example, the second obtaining module includes: a seventh determining submodule configured to determine, as a third field, a field set as a master key from the second database, when the syntax element is a preset first keyword; an eighth determining submodule configured to obtain a field value of the third field from the syntax element of the first database statement, and use the field value as a first field value; a ninth determining sub-module, configured to obtain field information of the third field from the second database, where the field information includes information for determining whether an entry of the first field value exists in the third field; and a tenth determining submodule, configured to use the field information of the third field as second target data related to the syntax element acquired from a second database.
As an optional example, the modification module includes: a first reading submodule configured to read field information of the third field from the second target data; an eleventh determining sub-module, configured to determine whether an entry of the first field value exists in the second database based on the field information; an insertion submodule, configured to replace, if any, a syntax element related to insertion in the first database statement with a syntax element related to update; a reservation submodule for reserving, in the absence of the first database statement, a syntax element related to the insertion; and the first deleting submodule is used for deleting the grammar elements corresponding to the preset first keyword.
As an optional example, the second obtaining module includes: a fourth obtaining sub-module, configured to obtain configuration information of the second database when the syntax element is a preset second keyword, where the configuration information includes information for determining whether the second database has a function of automatically updating a timestamp; and a fifth obtaining sub-module, configured to obtain the configuration information as second target data related to the syntax element from a second database.
As an optional example, the modification module includes: a second reading submodule, configured to read the configuration information from the second target data; and a second deleting submodule, configured to delete the syntax element corresponding to a second keyword preset in the first database statement when it is determined that the second database does not have a function of automatically updating the timestamp according to the configuration information.
As an optional example, further comprising: and a modification submodule, configured to set a trigger in the second database after deleting the syntax element corresponding to a second keyword preset in the first database statement, where the trigger is configured to automatically modify a field value corresponding to a timestamp data type in an entry of the second database when the entry is updated.
As an optional example, further comprising: a twelfth determining sub-module, configured to determine, after deleting the syntax element corresponding to a second keyword preset in the first database statement, a field restricted by the second keyword as a fourth field; and a setting sub-module configured to set field information of the fourth field in the second database after the syntax element corresponding to a second keyword preset in the first database statement is deleted, where the set field information of the fourth field includes information for determining that the fourth field has an automatic timestamp updating function.
As an alternative example, the extracting sub-module, configured to extract the syntax element from the first database statement, includes: the constructing submodule is used for constructing a first abstract syntax tree of a first database statement according to a syntax rule of a first database and the first database statement, wherein a node of the first abstract syntax tree comprises a syntax element obtained by analyzing the first database statement; when it is determined that the type of the first database statement is a first type, the first conversion module includes: and the first generation submodule is used for generating a second database statement conforming to the grammar rule of the second database according to the grammar rule of the second database and the first abstract grammar tree.
As an optional example, the extracting sub-module includes: a constructing subunit, configured to construct a first abstract syntax tree of a first database statement according to a syntax rule of the first database and the first database statement, where a node of the first abstract syntax tree includes a syntax element parsed from the first database statement; the correction module comprises: and the modification submodule is used for modifying the nodes corresponding to the syntax elements in the first abstract syntax tree based on the second target data to obtain a second abstract syntax tree.
As an optional example, the second conversion module includes: and the second generation submodule is used for generating a second database statement conforming to the second database syntax rule according to the syntax rule of the second database and the second abstract syntax tree.
As an optional example, further comprising: a third obtaining module, configured to obtain target data related to the syntax element from a path preset in a second database, where the target data includes first target data and second target data, and the preset path at least includes one of the following: a cache of the second database, a meta information table of the second database and the second database itself.
As an optional example, further comprising: and an execution unit configured to execute the second database statement in a second database after converting the first database statement into the second database statement conforming to a syntax rule of the second database based on the conversion rule associated with the type.
According to still another aspect of the embodiments of the present invention, there is also provided a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the above database statement conversion method when running.
According to another aspect of the embodiments of the present invention, there is also provided an electronic device, including a memory and a processor, where the memory stores a computer program, and the processor is configured to execute the database statement conversion method through the computer program.
In the embodiment of the present invention, under the condition that a first database statement conforming to a syntax rule of a first database is obtained, a syntax element may be extracted from the first database statement, a type of the first database statement is further determined based on the syntax element, and the first database statement is converted into a second database statement conforming to a syntax rule of a second database based on a conversion rule associated with the type.
Drawings
The accompanying drawings, which are included to provide a further understanding of the invention and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the invention and together with the description serve to explain the invention without limiting the invention. In the drawings:
FIG. 1 is a schematic diagram of an application environment of an alternative database statement translation method according to an embodiment of the present invention;
FIG. 2 is a flow diagram of an alternative database statement translation method according to an embodiment of the present invention;
FIG. 3 is a flow diagram of an alternative database statement translation method according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of data transformation of an alternative database statement transformation method according to an embodiment of the present invention;
FIG. 5 is a flow diagram of an alternative database statement translation method according to an embodiment of the present invention;
fig. 6 is a schematic structural diagram of an alternative database statement conversion apparatus according to an embodiment of the present invention.
Detailed Description
In order to make the technical solutions of the present invention better understood, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that the terms "first," "second," and the like in the description and claims of the present invention and in the drawings described above are used for distinguishing between similar elements and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used is interchangeable under appropriate circumstances such that the embodiments of the invention described herein are capable of operation in sequences other than those illustrated or described herein. Furthermore, the terms "comprises," "comprising," and "having," and any variations thereof, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
According to an aspect of the embodiments of the present invention, a database statement conversion method is provided, and optionally, as an optional implementation manner, the database statement conversion method may be applied, but not limited, to the environment shown in fig. 1.
As shown in fig. 1, the terminal device 102 includes a memory 104 for storing various data generated during the operation of the terminal device 102, a processor 106 for processing and operating the various data, and a display 108 for displaying the operation result of the data of the second database. Terminal device 102 may interact with server 112 via network 110. Server 112 includes a database 114 for storing various data items and a processing engine 116 for processing the various data items. Through steps S102 to S106, the terminal device 102 sends the first database statement to the server 112, and the server 112 processes data in the second database using the second database statement and returns the processing result to the terminal device 102. In this embodiment, the terminal device 102 may determine the second database statement and send the second database statement to the server 112, or the server 112 may send the first database statement 102 to the server 112 and determine the second database statement.
Optionally, in this embodiment, the terminal device may be a terminal device configured with a target client, and may include, but is not limited to, at least one of the following: mobile phones (such as Android phones, iOS phones, etc.), notebook computers, tablet computers, palm computers, MID (Mobile Internet Devices), PAD, desktop computers, smart televisions, etc. The target client may be a video client, an instant messaging client, a browser client, an educational client, etc. Such networks may include, but are not limited to: a wired network, a wireless network, wherein the wired network comprises: a local area network, a metropolitan area network, and a wide area network, the wireless network comprising: bluetooth, WIFI, and other networks that enable wireless communication. The server may be a single server, a server cluster composed of a plurality of servers, or a cloud server. The above is merely an example, and this is not limited in this embodiment.
Optionally, as an optional implementation manner, as shown in fig. 2, the database statement conversion method includes:
s202, acquiring a first database statement conforming to the grammar rule of the first database;
s204, extracting grammatical elements from the first database statement;
s206, determining the type of the first database statement based on the grammar element;
and S208, converting the first database statement into a second database statement which accords with the grammar rule of the second database based on the conversion rule associated with the type.
Alternatively, the above database statement conversion method can be applied to, but not limited to, the data processing process between databases of different kinds. For example, in the case where the terminal device 102 uses the first database, if the data of the second database in the server 112 is to be operated, since the statement of the first database is not suitable for execution in the second database, the first database statement under the first database may be converted into the second database statement under the second database by the above-described database statement conversion method to operate the data of the second database using the second database statement. By the method, the problem that the data of the domestic database cannot be directly operated by using the MySQL grammar in the prior art is solved.
Optionally, in this embodiment, the first database and the second database are different types of databases, and the databases are operated by using database statements with different database syntaxes. For example, the first database may be a MySQL database, and the second database may be a domestic database. Or the first database may be a domestic database, and the second database may be a MySQL database, which is not limited in this embodiment. The domestic database may include a dreams database or a shentong database, among others.
Optionally, in this embodiment, the syntax element may be, but is not limited to, a syntax element that is common to the statements under different rules, such as a field, a field value, a command, and the like; wherein, the field (value) is a field (value) in a database table; the commands are related commands for adding, deleting, modifying and checking the database, such as select, set and other commands in MySQL data.
Optionally, in this embodiment, the type of the first database statement may be, but is not limited to, classified according to a corresponding relationship between the first database statement and the second database statement; for example, the type hypotheses include a first type and a second type; the first type of database statement, the first database statement and the second database statement have a one-to-one corresponding grammatical relationship, and the first database statement can be directly converted into the second database statement, so that the following effects can be achieved: executing a first database statement in a first database to obtain a first execution result; executing the second database statement in the second database to obtain a second execution result; wherein the first execution result and the second execution result are consistent. The first database statement and the second database statement are not in one-to-one correspondence, and the first database statement cannot be directly converted into the second database statement; even if the second database statement may be directly converted, it may occur that the second database statement may not be executed in the second database, and it may occur that a second execution result of executing the second database statement in the second database is inconsistent with a first execution result of executing the first database statement in the first database. That is, the second database statement is not present in the second database, and the same function as the first database statement can be directly realized.
Optionally, in this embodiment, the manner of extracting the syntax element from the first database statement in the above database statement conversion method may be, but is not limited to, any manner that can quickly determine to extract a field, a field value, and a keyword (of a related command), and may be used to determine the type of the statement.
Optionally, in this embodiment, before converting the first database statement into the second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type, target data of the second database, including stored data, field type, and the like, may be obtained, but is not limited to, to determine how to convert the first database statement into the second database statement implementing the same function. It can be seen that the conversion of the database statements in this embodiment is an interactive conversion.
Optionally, in an embodiment, target data of the second database needs to be obtained, and then how to translate the first database statement into the second database statement implementing the same function is determined according to the target data; that is, even if different second databases are databases belonging to the same category, the translation result of the first database statement is different for the second databases storing different data. That is, the first database statement may be converted differently according to different obtained target data of the second database, so as to obtain different second database statements. It can be seen that the conversion of the database statements in this embodiment is a dynamic conversion.
In this embodiment, according to the type of the first database statement, the target data may be acquired from the second database in an interactive manner with the second database, and the first database statement may be further adaptively and dynamically converted into the second database statement according to the acquired target data, so that the function implemented by the converted second database statement is consistent with the function implemented by the first database statement, and the converted second database statement may be executed in the second database, and the execution result thereof may not conflict with the already stored data in the second database.
For further illustration, optional target data of the second database to be determined, for example, in a self-increment scenario, includes: the method comprises the following specific steps of self-increment and column-based latest data:
s1, determining whether a self-increment column exists in the field inquired in the statement of the first database or not based on the meta-information table of the second database, wherein the self-increment column can be used for automatically generating a unique identity (InDesign, ID for short) of a row;
s2, if existing, acquiring the self-increment latest data stored in the second database; optionally, the latest data may be, but is not limited to, be understood as, in the self-increment execution process, self-increment data corresponding to the execution time closest to the current time; or the data that is updated from the addition may also be, but is not limited to, the maximum value of the current column;
s3, replacing the field value of the self-increment corresponding field in the first database statement by using the latest data;
s4, after replacing the field value of the self-increment corresponding field in the first database statement with the latest data, the second database statement may be further converted. (the conversion process may still use the syntax tree technique except that the node data in the syntax tree is modified from the data in the second database).
For further example, optionally, for example, in a scenario where the binary type is forcibly converted into the hexadecimal type, the target data of the second database to be determined includes: the data types allowed to be written and the predefined data type conversion; the method comprises the following specific steps:
s1, when the first database statement includes binary type data;
s2, using a predefined data type conversion strategy, such as converting binary data into hexadecimal types which the second database allows to write;
s3, after converting the binary type data in the first database statement into the hexadecimal type data, the first database statement may be further converted into the second database statement (the conversion process may still use the syntax tree technique, except that the node data in the syntax tree is modified according to the data in the second database).
Further, for example, in an optional scenario where the timestamp (timestamp) is automatically updated, the target data of the second database that needs to be determined includes: whether the set timestamp is automatically updated or not; the method comprises the following specific steps:
s1, determining whether the set timestamp is automatically updated based on the meta information table of the second database; the meta information table may store table information of all tables in the second database, such as names of the tables, fields included in the tables, and the like; it is also possible to store field information for all fields in the table, such as field name, field type, etc.
And S2, if yes, triggering to acquire the current time, and adding the current time as additional information into the first database statement.
S3, after the additional information is added to the first database statement, the first database statement may be further converted into a second database statement. (the conversion process may still use the syntax tree technique except that the node data in the syntax tree is modified from the data in the second database).
In addition, the specific steps of the automatic update of the timestamp can also be as follows:
s1, determining whether the first database statement contains a keyword for automatic update of timestamp;
s2, if yes, the meta information table in the second database is identified to determine which field is the timestamp data type and needs to be updated automatically.
And S3, setting a trigger in the second database.
S4, converting the first database statement into a second database statement.
S5, when the second database statement is executed, the trigger in the step S3 is automatically triggered to automatically update the timestamp.
Further by way of example, optionally, for example, in a scenario of setting a trigger, the trigger may be, but is not limited to, set in the second database, where the trigger may be, but is not limited to, an object related to table operation, and when a specified event occurs on a table where the trigger is located, the object is invoked, that is, an operation event of the table triggers execution of the trigger on the table, as it is assumed:
trigger _ name: the trigger name.
trigger _ time is the trigger time of the trigger. Taking the value as BEFORE or AFTER.
trigger _ event is a listening event of the trigger. The value is INSERT, UPDATE or DELETE.
table _ name is the target of the trigger's snoop. Specifying on which table to set up the trigger.
FOR EACH arch ROW line level monitor, Mysql fixed write, other DBMS different.
trigger _ attributes the trigger performs the action. The method is a list of one or more SQL sentences, and each sentence in the list must use a semicolon; and ending.
For further illustration, in an optional scenario, such as insert. Whether a primary key exists; whether the field value of the field corresponding to the inserted primary key already exists in the second database; the method comprises the following specific steps:
s1, when the first database statement includes a KEY (node) of an insert.
S2, determining whether a primary key exists from the second database; if yes, go to S3;
s3, if the primary key exists, determining whether the field value of the field corresponding to the inserted primary key exists in the second database, if so, executing S4, and if not, executing S5;
s4, converting insert into syntax of update, replacing nodes of the syntax tree, and continuing to execute S6;
s5, replacing the nodes of the syntax tree by using the syntax of insert, and continuing to execute S6;
s6, generating a second database statement using the alternative syntax tree.
Through the embodiment, the first database statement of the first database is converted into the second database statement of the second database through the method, so that the effect of directly operating the data of the domestic database by using the MySQL grammar is realized.
As an alternative example, the extracting of the syntax elements from the first database statement comprises:
s1, creating a first abstract syntax tree;
s2, analyzing the first database statement based on the grammar rule of the first database to obtain a grammar element;
s3, each syntax element is inserted into the first abstract syntax tree as a node of the first abstract syntax tree.
Optionally, in this embodiment, the syntax element extracted from the first database statement in the database statement conversion method may be, but is not limited to, implemented by an abstract syntax tree.
Optionally, in this embodiment, an Abstract Syntax Tree (AST) in this embodiment is a tree representation of an abstract syntax structure of the source code, and each node on the tree represents one structure in the source code.
Alternatively, in this embodiment, the random syntax tree may be traversed from a random node of the abstract syntax tree, or the nodes may be traversed layer by layer from the uppermost node of the random syntax tree to the lower side of the abstract syntax tree. Alternatively, all nodes in the abstract syntax tree may also be traversed in parallel.
Optionally, in this embodiment, for each node in the abstract syntax tree, the first data is included in each node. For first data in a node, a translator may be used to convert the first data to second data under a second database.
Optionally, the translator may be further used in this embodiment to store the corresponding relationship between the first database data and the second database data. If the first database data is acquired, the corresponding second database data can be searched from the translator.
Optionally, in this embodiment, when traversing a node of the abstract syntax tree, for each node, a node type of the node is obtained. In the abstract syntax tree, each node corresponds to a different node type. The node type corresponding to each node in the first abstract syntax tree may be, but is not limited to, representing syntax elements of different syntax types.
As an alternative example, determining the type of the first database statement based on the syntax element includes:
s1, determining whether a grammar element in the first database statement is a preset keyword;
s2, determining the type of the first database statement as a first type under the condition that all the syntactic elements are not keywords;
s3, if there is a grammar element as the keyword, determining the type of the first database statement as the second type.
Optionally, in this embodiment, the keyword may be, but is not limited to, a preset syntax element. For example, the KEY may be set to insert.
As an alternative example, determining the type of the first database statement based on the syntax element includes:
s1, acquiring first target data related to the grammar element from the second database based on the grammar element;
s2, determining the type of the first database statement according to the first target data.
Optionally, in this embodiment, the type is determined based on the first target data, where the first target data may be, but is not limited to, determining whether the syntax element in the first database statement needs to be updated according to the data already stored in the second database. For example, in the scenario of automatic timestamp update, when the value in the preset field is set to change when the table is created, the timestamp needs to be updated at the same time. Then for all subsequent input insert operations it is first checked whether there is a setting in the second database that has been updated with a timeout update for the data table. In addition, the present embodiment may also be applied to the scenarios of self-increment column, binary value to hexadecimal value, etc., which is not limited herein.
As an alternative example, determining the type of the first database statement from the first target data includes:
s1, determining that the first database statement is of a first type under the condition that the first target data does not meet the first condition;
s2, determining the first database statement as a second type under the condition that the first target data meets the first condition.
As an alternative example, the first condition includes at least one of:
determining from the first target data that the second database has a field set to an automatically updated value;
determining from the first target data that the second database has a field that does not match the first database presence data type.
Optionally, in the present embodiment, the field of the automatic update value may be, but is not limited to, a field in a self-increment column or a field in a Timestamp automatic update scene. The fields of the first database where the data types do not match may be, but are not limited to, fields in the context of binary to hexadecimal values.
As an alternative example, converting the first database statement into a second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type includes:
and under the condition that the type of the first database statement is determined to be the first type, converting the first database statement into a second database statement which conforms to the grammar rule of the second database.
Optionally, in this embodiment, a first abstract syntax tree of the first database statement is constructed according to the syntax rule of the first database and the first database statement; and generating a second database statement conforming to the second database grammar rule according to the grammar rule of the second database and the first abstract grammar tree. For example, under the condition that a first database statement under a first database is obtained, the first database statement is analyzed to obtain an abstract syntax tree of the first database statement; determining the node type of each node in the abstract syntax tree; converting the first data of each node in the abstract syntax tree into second data under a second database by using a translator matched with the node type to obtain a translated abstract syntax tree; and analyzing the abstract syntax tree formed by the second data to obtain a second database statement under the second database.
It should be noted that, in the case that the type of the first database statement is determined to be the first type, the first database statement is directly converted into the second database statement that conforms to the syntax rule of the second database.
As an alternative example, converting the first database statement into a second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type, further comprises:
s1, under the condition that the type of the first database statement is determined to be the second type, acquiring second target data related to the grammar element from the second database based on the grammar element;
s2, correcting the grammar element in the first database statement based on the second target data;
and S3, converting the corrected first database statement into a second database statement which accords with the grammar rule of the second database.
As an alternative example, based on the syntax element, obtaining second target data related to the syntax element from a second database includes:
s1, when the grammar element is the field with self-increment function, the field corresponding to the grammar element is determined as the target field;
s2, determining the latest field value in the target field from the second database as the second target data related to the grammar element from the second database;
s3, modifying the syntax element in the first database statement based on the second target data, including:
and S4, replacing the field value corresponding to the target field in the first database statement with the maximum field value.
Alternatively, in this embodiment, the self-organizing function may be, but is not limited to, a function that automatically generates a unique ID for a row, with special significance to different databases or storage engines. The self-increment ID is generally represented by an integer, while other schemes such as UUID are generally represented by a long string (generally 16 bytes), which wastes storage space and network.
Optionally, in this embodiment, the latest field value may be, but is not limited to, a maximum or minimum value or a top or last arranged field value of a current column corresponding to the target field after the field corresponding to the syntax element is determined as the target field.
As an alternative example, based on the syntax element, obtaining second target data related to the syntax element from a second database includes:
s1, determining the data type of the field value of the first field as the first data type under the condition that the syntax element is the field value under the first field;
s2, determining the data type of the first field as a second data type from the second database as second target data;
it should be noted that, in a scenario where a binary type is forcibly converted into a hexadecimal type, when a syntax element is a field value under a first field, a data type of the field value of the first field is determined to be a first data type; and determining the data type of the first field as a second data type from a second database as second target data.
As an alternative example, modifying the syntax element in the first database statement based on the second target data includes:
a field value under a first field in a first database statement is converted from a first data type to a second data type.
As an alternative example, based on the syntax element, obtaining second target data related to the syntax element from a second database includes:
s1, under the condition that the syntax element is a second field, acquiring field information of the second field from a second database, wherein the second field is a field of a timestamp data type, and the field information comprises information for determining whether the second field has a timestamp automatic updating function;
s2, using the field information as second target data related to the grammar element obtained from the second database;
optionally, in this embodiment, in a scenario of Timestamp automatic update, in a case that the syntax element is a second field, field information of the second field is obtained from the second database; and taking the field information as second target data related to the grammar element acquired from a second database.
As an alternative example, modifying the syntax element in the first database statement based on the second target data includes:
s1, acquiring the current time under the condition that the second field is determined to have the automatic timestamp updating function based on the second target data;
s2, updating the field value under the second field in the first database statement to the current time.
As an alternative example, based on the syntax element, obtaining second target data related to the syntax element from a second database includes:
s1, determining a field set as a primary key value (key) from the second database as a third field in case that the syntax element is a preset first key;
s2, obtaining the field value of the third field from the syntax element of the first database statement as the first field value;
s3, obtaining field information of the third field from the second database, wherein the field information comprises information for determining whether an entry of the first field value exists under the third field;
s4, using the field information of the third field as the second target data related to the syntax element obtained from the second database.
Optionally, in this embodiment, the information of the entry of the first field value may be, but is not limited to, insert.
As an alternative example, modifying the syntax element in the first database statement based on the second target data includes:
s1, reading the field information of the third field from the second target data;
s2, determining whether there is an entry of the first field value in the second database based on the field information;
s3, if the grammar element related to the insertion in the first database statement is replaced by the grammar element related to the update, and the step S5 is continuously executed;
s4, if there is no grammar element, keeping the first database statement and inserting relevant grammar element, continuing executing step S5;
and S5, deleting the grammar element corresponding to the preset first keyword.
As an alternative example, based on the syntax element, obtaining second target data related to the syntax element from a second database includes:
s1, acquiring configuration information of the second database under the condition that the syntax element is a preset second keyword, wherein the configuration information comprises information for determining whether the second database has a function of automatically updating the timestamp;
s2, the configuration information is used as second target data related to the syntax element obtained from the second database.
S3, reading the configuration information from the second target data;
and S4, deleting the grammar element corresponding to the second keyword preset in the first database statement when the second database does not have the function of automatically updating the time stamp according to the configuration information.
And S5, setting a trigger in the second database, wherein the trigger is used for automatically changing the field value corresponding to the timestamp data type in the entry under the condition that the entry in the second database is updated.
Alternatively, in the present embodiment, the function of automatic timestamp UPDATE may be implemented by, but is not limited to, ON UPDATE CURRENT _ TIMESTAMP.
As an optional example, after deleting the syntax element corresponding to the preset second keyword in the first database statement, the method further includes:
s1, determining the field limited by the second keyword as a fourth field;
and S2, setting field information of a fourth field in the second database, wherein the set field information of the fourth field comprises information for determining that the fourth field has the automatic timestamp updating function.
Optionally, in this embodiment, in a case that the syntax element is a second field, field information of the second field is obtained from a second database; taking the field information as second target data related to the grammar element acquired from a second database; under the condition that the second field is determined to have the automatic timestamp updating function based on the second target data, the current time is obtained; updating a field value under a second field in the first database statement to be a current time; determining the field limited by the second keyword as a fourth field; and setting field information of a fourth field in the second database, wherein the set field information of the fourth field comprises information for determining that the fourth field has the automatic timestamp updating function.
As an alternative example, the extracting of the syntax elements from the first database statement comprises:
constructing a first abstract syntax tree of the first database statement according to the syntax rules of the first database and the first database statement, wherein the node of the first abstract syntax tree comprises syntax elements obtained by analyzing the first database statement;
optionally, in this embodiment, a first abstract syntax tree of a first database statement may be constructed, but not limited to, in a statement conversion process (e.g., directly converting the first database statement into a second database statement that conforms to the syntax rules of a second database in the case where the type of the first database statement is determined to be the first type), according to the syntax rules of the first database and the first database statement.
As an alternative example, in the case where the type of the first database statement is determined to be the first type, converting the first database statement into the second database statement conforming to the syntax rule of the second database includes:
and generating a second database statement conforming to the second database grammar rule according to the grammar rule of the second database and the first abstract grammar tree.
As an alternative example, the extracting of the syntax elements from the first database statement comprises:
constructing a first abstract syntax tree of the first database statement according to the syntax rules of the first database and the first database statement, wherein the node of the first abstract syntax tree comprises syntax elements obtained by analyzing the first database statement;
as an alternative example, modifying the syntax element in the first database statement based on the second target data includes:
and modifying the nodes corresponding to the syntax elements in the first abstract syntax tree based on the second target data to obtain a second abstract syntax tree.
Optionally, in this embodiment, the process of obtaining the second abstract syntax tree may be, but is not limited to be, understood as a process of performing addition and deletion on nodes.
As an alternative example, converting the revised first database statement into a second database statement that conforms to the syntax rule of the second database includes:
and generating a second database statement conforming to the second database syntax rule according to the syntax rule of the second database and the second abstract syntax tree.
Optionally, in this embodiment, the second abstract syntax tree is traversed again to obtain a complete second database statement.
As an optional example, further comprising:
acquiring target data related to the grammar element from a preset path of a second database, wherein the target data comprises first target data and second target data, and the preset path at least comprises one of the following paths: a cache of the second database, a meta information table of the second database and the second database itself.
It should be noted that the target data related to the syntax element is acquired from a path preset in the second database.
For further example, optionally, for example, first, all data to be checked is obtained from the cache of the second database; then calling a meta-information table of a second database to search meta-information related to the syntax elements; in the case of finding the meta information related to the syntax element, the corresponding target data is called from the above-mentioned all data through the related structure of the second database itself.
As an alternative example, after converting the first database statement into the second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type, the method further includes:
the second database statement is executed in the second database.
Optionally, in this embodiment, after the correction, at least one of the following results may be achieved: executing a first database statement in a first database to obtain a first execution result, and executing a second database statement in a second database to obtain a second execution result; wherein the first execution result and the second execution result are consistent.
The terms referred to in this embodiment:
SQL: structured Query Language, Structured Query Language.
MySQL: a universal open source relational database system.
ODBC: open Database Connectivity, Open Database interconnect, provides a standard API (application programming interface) method to access Database management systems.
Golang: a programming language.
CDC: the Change Data Capture, a database incremental Data Capture tool, can complete the incremental Data synchronization between database Data.
Translating: equivalent translation between two or more SQL grammars.
The embodiment is intended to completely discard the original database, such as MySQL, and directly write the original database into the target database, such as a domestic database, without modifying the service code. The CDC type tool is not needed to perform secondary synchronization of data; multiple target databases can be quickly adapted by realizing a given interface, the service adaptation efficiency is improved, and the complete transparency to a service party is realized; because data migration is not needed, the overhead of the part of the network is saved, and the execution efficiency of the service on the target database is improved.
The idea of the application is that based on the equivalence principle of SQL semantics, original SQL combined with various meta-information data is translated into SQL conforming to the syntax of the target database. The type of data encountered in translation is inconsistent and is stored in a broader range of representations based on the principles of magnification. Specific constraints encountered in translation, such as the inability to update self-populated columns, give equivalent transformations in conjunction with the actual library and abstract them into an interface for subsequent expansion.
Fig. 3 is a schematic diagram of the present embodiment. Through steps S302 to S310, an original SQL request sent by a service end on a connection may be received, and a syntax interpreter is invoked to parse the original SQL request into an abstract syntax tree; traversing the abstract syntax tree, translating each syntax node in the abstract syntax tree, combining a new SQL which accords with the syntax of the target database, and returning the SQL, so that the business SQL completes the conversion and can be directly sent to the target database to execute the converted statement. Fig. 4 is a schematic diagram of parsing a first database statement to obtain an abstract syntax tree, and converting data of each node of the abstract syntax tree to obtain a second database statement. Fig. 5 is a flowchart of traversing the abstract syntax tree according to the present embodiment to translate for each syntax node therein.
Because the standards supported by the SQL syntax are different, the target database may not have a completely equivalent syntax, and the SQL stored procedures need to be written by hand to match.
In general, the translation is to analyze the SQL of MySQL into an Abstract Syntax Tree (AST), and then perform specific conversion according to different SQL types, which simply means to use the syntax of the target database to implement semantically equivalent MySQL statements.
MySQL syntax is divided into two categories on the whole: a data definition language DDL and a data manipulation language DML. The DDL is simple to convert because it has not much syntax structure, and the complexity lies in the processing of some attributes, such as setting a primary key and adding an index, as a complete database DB, which all support statements, which may be some differences in writing. But there are also some special MySQL syntax, such as: the on update current _ timestamp is used to automatically update the field to the current time when updating. To support this grammar, a trigger may be added thereto. The DML treatment is more complex, and the difficulties mainly include: ON private KEY UPDATE, insertion of self-augmented columns, insertion of binary data, and the like. Taking the processing of the self-augmented columns as an example, after the self-augmented columns of MySQL are inserted with 0 or NULL, the MySQL will automatically convert the self-augmented columns into the correct self-increment value, which is the next value of the current maximum value, and at this time, the translation layer needs to find the correct value and complete the replacement, so that the self-augmented columns can be really inserted. When the special grammar is processed, the related interface can be abstracted, so that more target databases can be conveniently accessed subsequently, the background knowledge of a user is reduced as much as possible, and the efficiency is improved.
It should be noted that, for simplicity of description, the above-mentioned method embodiments are described as a series of acts or combination of acts, but those skilled in the art will recognize that the present invention is not limited by the order of acts, as some steps may occur in other orders or concurrently in accordance with the invention. Further, those skilled in the art should also appreciate that the embodiments described in the specification are preferred embodiments and that the acts and modules referred to are not necessarily required by the invention.
According to another aspect of the embodiment of the present invention, there is also provided a database statement conversion apparatus for implementing the above database statement conversion method. As shown in fig. 6, the apparatus includes:
an obtaining unit 602, configured to obtain a first database statement that meets syntax rules of a first database;
an extracting unit 604 for extracting syntax elements from the first database statement;
a determining unit 606 for determining a type of the first database statement based on the syntax element;
a conversion unit 608, configured to convert the first database statement into a second database statement that conforms to the syntax rule of the second database based on the conversion rule associated with the type.
Alternatively, the database statement conversion device can be applied to the data processing process among different types of databases without limitation. For example, in the case of using the first database, if the data of the second database is to be operated, since the statement of the first database cannot be operated on the statement of the second database, the first database statement in the first database may be converted into the second database statement in the second database by the above-mentioned database statement conversion device to operate the data of the second database using the second database statement. By the device, the problem that the data of the domestic database cannot be directly operated by using the MySQL grammar in the prior art is solved.
Optionally, in this embodiment, the first database may be a MySQL database, and the second database may be a domestic database. Or the first database may be a domestic database, and the second database may be a MySQL database, which is not limited in this embodiment.
Optionally, in this embodiment, the syntax element may be, but is not limited to, a syntax element that is common to the statements under different rules, such as a field, a field value, a command, and the like; wherein, the field is a field in the table; the commands are select, set, etc.
Optionally, in this embodiment, the type of the first database statement may be, but is not limited to, a storage type used for representing statements in different databases, for example, the type hypothesis includes a first type and a second type; the first type of database statement, the first database and the second database have a one-to-one corresponding grammatical relationship, and the first database statement can be directly converted into the second database statement, so that the following effects can be achieved: executing a first database statement in a first database to obtain a first execution result; executing the second database statement in the second database to obtain a second execution result; wherein the first execution result and the second execution result are consistent. The grammar relations between the first database and the second database are not in one-to-one correspondence; that is, the second database statement is not present in the second database, and the same function as the first database statement can be directly realized.
Optionally, in this embodiment, the manner of extracting the syntax element from the first database statement in the database statement conversion apparatus may be, but is not limited to, any manner that can quickly determine to extract a field, a field value, and a keyword (of a related command) for determining the statement type service.
Optionally, in this embodiment, before converting the first database statement into the second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type, information of the second database, including stored data, field type, and the like, may be obtained, but is not limited to, to determine how to perform the translation. It can be seen that the conversion of the database statements in this embodiment is an interactive conversion.
Optionally, in this embodiment, since the information of the second database needs to be obtained, it is only then possible to determine how to perform the translation; that is, even if different second databases are of the same type of data, the results of the translation are different for the second databases storing different data. It can be seen that the conversion of the database statements in this embodiment is a dynamic conversion.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
Through the embodiment, the device converts the first database statement of the first database into the second database statement of the second database, so that the effect of directly operating the data of the domestic database by using the MySQL grammar is realized.
As an alternative example, the extracting unit 604 includes:
a creation module for creating a first abstract syntax tree;
the parsing module is used for parsing the first database statement based on the grammar rule of the first database to obtain a grammar element;
and the inserting module is used for inserting each syntax element into the first abstract syntax tree to be used as a node of the first abstract syntax tree.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the determining unit 606 includes:
the first determining module is used for determining whether a grammar element in a first database statement is a preset keyword or not;
the second determining module is used for determining that the type of the first database statement is the first type under the condition that all the syntactic elements are not keywords;
and the third determining module is used for determining the type of the first database statement as the second type under the condition that one grammar element is a keyword.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the determining unit 606 includes:
the first acquisition module is used for acquiring first target data related to the grammar element from a second database based on the grammar element;
and the fourth determining module is used for determining the type of the first database statement according to the first target data.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the fourth determining module includes:
the first determining submodule is used for determining that the first database statement is of a first type under the condition that the first target data does not meet the first condition;
and the second determining submodule is used for determining the first database statement as the second type under the condition that the first target data meets the first condition.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the first condition includes at least one of:
determining from the first target data that the second database has a field set to an automatically updated value;
and determining that the second database has a field which is not matched with the data type of the first database from the first target data.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the conversion unit 608 includes:
and the first conversion module is used for directly converting the first database statement into a second database statement which accords with the grammar rule of the second database under the condition that the type of the first database statement is determined to be the first type.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the conversion unit 608 further includes:
the second acquisition module is used for acquiring second target data related to the grammar element from a second database on the basis of the grammar element under the condition that the type of the first database statement is determined to be a second type;
the correction module is used for correcting the syntactic elements in the first database sentences based on the second target data;
and the second conversion module is used for converting the corrected first database statement into a second database statement which accords with the grammar rule of a second database.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the second obtaining module includes:
a third determining submodule, configured to determine, when the syntax element is a field with a self-increment function, a field corresponding to the syntax element as a target field;
a fourth determining submodule, configured to determine, from the second database, a latest field value in the target field as second target data related to the syntax element, obtained from the second database;
a correction module, comprising:
and the replacing module is used for replacing the field value corresponding to the target field in the first database statement with the maximum field value.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the second obtaining module includes:
a fifth determining submodule, configured to determine, when the syntax element is a field value under the first field, that a data type of the field value of the first field is the first data type;
a sixth determining submodule, configured to determine, from the second database, that the data type of the first field is the second data type, and use the second data type as second target data;
a correction module, comprising:
a conversion sub-module for converting a field value under a first field in the first database statement from the first data type to the second data type.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the second obtaining module includes:
the first obtaining submodule is used for obtaining field information of a second field from a second database under the condition that the syntax element is the second field, wherein the second field is a field of a timestamp data type, and the field information comprises information used for determining whether the second field has a timestamp automatic updating function;
the second obtaining submodule is used for obtaining second target data related to the grammar element from a second database by using the field information;
a correction module, comprising:
the third obtaining submodule is used for obtaining the current time under the condition that the second field is determined to have the automatic timestamp updating function based on the second target data;
and the updating submodule is used for updating the field value under the second field in the first database statement to the current time.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the second obtaining module includes:
a seventh determining submodule for determining a field set as a master key from the second database as a third field in a case where the syntax element is the preset first keyword;
an eighth determining submodule, configured to obtain a field value of a third field from a syntax element of the first database statement, where the field value is used as the first field value;
a ninth determining sub-module, configured to obtain field information of a third field from the second database, where the field information includes information used to determine whether an entry of the first field value exists under the third field;
and the tenth determining submodule is used for taking the field information of the third field as second target data related to the grammar element acquired from the second database.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the modification module includes:
the first reading submodule is used for reading field information of a third field from the second target data;
an eleventh determining sub-module for determining whether an entry of the first field value exists in the second database based on the field information;
an insertion submodule, configured to replace, if any, an insertion-related syntax element in the first database statement with an update-related syntax element;
a retention submodule for retaining, in the absence, syntax elements of the first database statement relating to the insertion;
and the first deleting submodule is used for deleting the grammar elements corresponding to the preset first keywords.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, the second obtaining module includes:
the fourth obtaining submodule is used for obtaining the configuration information of the second database under the condition that the grammar element is a preset second keyword, wherein the configuration information comprises information used for determining whether the second database has a function of automatically updating the time stamp;
and the fifth acquiring submodule is used for acquiring the configuration information as second target data related to the grammar element from the second database.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the modification module includes:
the second reading submodule is used for reading the configuration information from the second target data;
and the second deleting submodule is used for deleting the grammar elements corresponding to the preset second keywords in the first database statement under the condition that the second database does not have the function of automatically updating the timestamp according to the configuration information.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, further comprising:
and the modification submodule is used for setting a trigger in the second database after deleting the grammar element corresponding to the second keyword preset in the first database statement, wherein the trigger is used for automatically modifying the field value corresponding to the timestamp data type in the entry under the condition of updating the entry in the second database.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, further comprising:
a twelfth determining submodule, configured to determine, after deleting a syntax element corresponding to a second keyword preset in the first database statement, a field limited by the second keyword as a fourth field;
and the setting submodule is used for setting field information of a fourth field in the second database after deleting the grammar element corresponding to the second keyword preset in the first database statement, wherein the set field information of the fourth field comprises information used for determining that the fourth field has a timestamp automatic updating function.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the extracting sub-module is configured to extract the syntax element from the first database statement, and includes:
the constructing submodule is used for constructing a first abstract syntax tree of a first database statement according to the syntax rules of the first database and the first database statement, wherein the node of the first abstract syntax tree comprises syntax elements obtained by analyzing the first database statement;
in a case where it is determined that the type of the first database statement is the first type, the first conversion module includes:
and the first generation submodule is used for generating a second database statement conforming to the second database grammar rule according to the grammar rule of the second database and the first abstract grammar tree.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the extraction submodule includes:
the building subunit is used for building a first abstract syntax tree of the first database statement according to the syntax rules of the first database and the first database statement, wherein the node of the first abstract syntax tree comprises syntax elements obtained by parsing the first database statement;
a correction module, comprising:
and the modification submodule is used for modifying the nodes corresponding to the syntax elements in the first abstract syntax tree based on the second target data to obtain a second abstract syntax tree.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an alternative example, the second conversion module includes:
and the second generation submodule is used for generating a second database statement conforming to the second database syntax rule according to the syntax rule of the second database and the second abstract syntax tree.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, further comprising:
a third obtaining module, configured to obtain target data related to the syntax element from a second database in a preset way, where the target data includes the first target data and the second target data, and the preset way includes at least one of the following: a cache of the second database, a meta information table of the second database and the second database itself.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
As an optional example, further comprising:
an execution unit for executing the second database statement in the second database after converting the first database statement into a second database statement conforming to the syntax rule of the second database based on the conversion rule associated with the type.
For other examples of the present embodiment, please refer to the above examples, which are not described herein again.
According to a further aspect of the embodiments of the present invention, there is also provided an electronic device for implementing the database statement conversion method, the electronic device including a memory and a processor, the memory storing a computer program therein, the processor being configured to execute the steps in any of the method embodiments by the computer program.
According to a further aspect of an embodiment of the present invention, there is also provided a computer-readable storage medium having a computer program stored thereon, wherein the computer program is arranged to perform the steps of any of the above method embodiments when executed.
Alternatively, in this embodiment, a person skilled in the art may understand that all or part of the steps in the methods of the foregoing embodiments may be implemented by a program instructing hardware associated with the terminal device, where the program may be stored in a computer-readable storage medium, and the storage medium may include: flash disks, Read-Only memories (ROMs), Random Access Memories (RAMs), magnetic or optical disks, and the like.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
The integrated unit in the above embodiments, if implemented in the form of a software functional unit and sold or used as a separate product, may be stored in the above computer-readable storage medium. Based on such understanding, the technical solution of the present invention may be substantially or partially implemented in the prior art, or all or part of the technical solution may be embodied in the form of a software product stored in a storage medium, and including instructions for causing one or more computer devices (which may be personal computers, servers, or network devices) to execute all or part of the steps of the method according to the embodiments of the present invention.
In the above embodiments of the present invention, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
In the several embodiments provided in the present application, it should be understood that the disclosed client may be implemented in other manners. The above-described embodiments of the apparatus are merely illustrative, and for example, the division of the units is only one type of division of logical functions, and there may be other divisions when actually implemented, for example, a plurality of units or components may be combined or may be integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, units or modules, and may be in an electrical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The foregoing is only a preferred embodiment of the present invention, and it should be noted that, for those skilled in the art, various modifications and decorations can be made without departing from the principle of the present invention, and these modifications and decorations should also be regarded as the protection scope of the present invention.

Claims (25)

1. A database statement conversion method, comprising:
acquiring a first database statement which accords with a grammar rule of a first database;
extracting syntax elements from the first database statement;
determining a type of the first database statement based on the syntax element;
and converting the first database statement into a second database statement conforming to the grammar rule of a second database based on the conversion rule associated with the type.
2. The method of claim 1, wherein said extracting syntax elements from said first database statement comprises:
creating a first abstract syntax tree;
analyzing the first database statement based on the grammar rule of the first database to obtain a grammar element;
inserting each of the syntax elements into the first abstract syntax tree as a node of the first abstract syntax tree.
3. The method of claim 1, wherein said determining a type of the first database statement based on the syntax element comprises:
determining whether a keyword with a preset grammar element exists in a first database statement;
determining that the type of the first database statement is a first type under the condition that all the syntax elements are not the keywords;
determining the type of the first database statement as a second type if one of the syntax elements is the keyword.
4. The method of claim 1, wherein said determining a type of the first database statement based on the syntax element comprises:
acquiring first target data related to the grammar element from a second database based on the grammar element;
determining the type of the first database statement according to the first target data.
5. The method of claim 4, wherein said determining the type of the first database statement from the first target data comprises:
determining that the first database statement is of a first type if the first target data does not meet a first condition;
determining that the first database statement is of a second type if the first target data meets a first condition.
6. The method of claim 5,
the first condition includes at least one of:
determining from the first target data that the second database has a field set to an automatically updated value;
determining from the first target data that the second database has a field that does not match the first database presence data type.
7. The method of claim 1, wherein said converting the first database statement into a second database statement that conforms to a syntax rule of a second database based on a conversion rule associated with the type comprises:
and under the condition that the type of the first database statement is determined to be the first type, converting the first database statement into a second database statement which accords with the grammar rule of a second database.
8. The method of claim 1, wherein converting the first database statement into a second database statement that conforms to a syntax rule of a second database based on a conversion rule associated with the type, further comprises:
under the condition that the type of the first database statement is determined to be a second type, second target data related to the grammar element is obtained from a second database based on the grammar element;
modifying the syntactic elements in the first database statement based on the second target data;
and converting the corrected first database statement into a second database statement which accords with the grammar rule of a second database.
9. The method of claim 8, wherein obtaining second target data associated with the syntax element from a second database based on the syntax element comprises:
determining a field corresponding to the syntax element as a target field when the syntax element is a field with a self-increment function;
determining a latest field value in the target fields from the second database as the second target data related to the syntax element from the second database;
the modifying the syntax element in the first database statement based on the second target data comprises:
and replacing the field value corresponding to the target field in the first database statement with the largest field value.
10. The method of claim 8, wherein obtaining second target data associated with the syntax element from a second database based on the syntax element comprises:
determining that a data type of a field value of a first field is a first data type if the syntax element is the field value under the first field;
determining the data type of the first field as a second data type from the second database, and using the second data type as second target data;
the modifying the syntax element in the first database statement based on the second target data comprises:
converting a field value under the first field in the first database statement from the first data type to the second data type.
11. The method of claim 8, wherein obtaining second target data associated with the syntax element from a second database based on the syntax element comprises:
under the condition that the syntax element is a second field, acquiring field information of the second field from the second database, wherein the second field is a field of a timestamp data type, and the field information comprises information for determining whether the second field has a timestamp automatic updating function;
taking the field information as the second target data related to the grammar element obtained from the second database;
the modifying the syntax element in the first database statement based on the second target data comprises:
acquiring current time under the condition that the second field is determined to have a time stamp automatic updating function based on the second target data;
updating a field value under the second field in the first database statement to the current time.
12. The method of claim 8, wherein obtaining second target data associated with the syntax element from a second database based on the syntax element comprises:
determining a field set as a primary key value from the second database as a third field in case that the syntax element is a preset first key;
obtaining a field value of the third field from the syntax element of the first database statement as a first field value;
obtaining field information of the third field from the second database, wherein the field information comprises information for determining whether an entry of the first field value exists under the third field;
and taking the field information of the third field as second target data related to the grammar element acquired from a second database.
13. The method of claim 12, wherein modifying the syntax element in the first database statement based on the second target data comprises:
reading field information of the third field from the second target data;
determining whether an entry for the first field value exists in the second database based on the field information;
replacing, if present, an insertion-related grammar element in the first database statement with an update-related grammar element;
in the absence, retaining insertion-related syntax elements in the first database statement;
and deleting the grammar elements corresponding to the preset first keywords.
14. The method of claim 8, wherein obtaining second target data associated with the syntax element from a second database based on the syntax element comprises:
acquiring configuration information of the second database under the condition that the syntax element is a preset second keyword, wherein the configuration information comprises information for determining whether the second database has a function of automatically updating a timestamp;
and taking the configuration information as second target data related to the grammar element acquired from a second database.
15. The method of claim 14, wherein modifying the syntax element in the first database statement based on the second target data comprises:
reading the configuration information from the second target data;
and deleting the grammar elements corresponding to the preset second keywords in the first database statement under the condition that the second database does not have the function of automatically updating the timestamp according to the configuration information.
16. The method according to claim 15, wherein after the deleting the syntax element corresponding to the preset second keyword in the first database statement, the method further comprises:
and setting a trigger on the second database, wherein the trigger is used for automatically changing a field value corresponding to the timestamp data type in an entry under the condition that the entry of the second database is updated.
17. The method according to claim 15, wherein after the deleting the syntax element corresponding to the preset second keyword in the first database statement, the method further comprises:
determining the field limited by the second keyword as a fourth field;
setting field information of the fourth field in the second database, wherein the set field information of the fourth field comprises information for determining that the fourth field has a timestamp automatic updating function.
18. The method of claim 7,
the extracting syntax elements from the first database statement comprises:
constructing a first abstract syntax tree of a first database statement according to a syntax rule of the first database and the first database statement, wherein a node of the first abstract syntax tree comprises a syntax element obtained by analyzing the first database statement;
the converting the first database statement into a second database statement conforming to a syntax rule of a second database if it is determined that the type of the first database statement is the first type includes:
and generating a second database statement conforming to the second database grammar rule according to the grammar rule of the second database and the first abstract grammar tree.
19. The method according to any one of claims 8 to 17,
the extracting syntax elements from the first database statement comprises:
constructing a first abstract syntax tree of a first database statement according to a syntax rule of the first database and the first database statement, wherein a node of the first abstract syntax tree comprises a syntax element obtained by analyzing the first database statement;
the modifying the syntax element in the first database statement based on the second target data comprises:
and modifying the nodes corresponding to the syntax elements in the first abstract syntax tree based on the second target data to obtain a second abstract syntax tree.
20. The method of claim 19, wherein converting the revised first database statement into a second database statement that conforms to the grammar rules of a second database comprises:
and generating a second database statement conforming to the second database syntax rule according to the syntax rule of the second database and the second abstract syntax tree.
21. The method of claim 4 or 10, further comprising:
acquiring target data related to the grammar element from a preset path of a second database, wherein the target data comprises first target data and second target data, and the preset path at least comprises one of the following paths: a cache of the second database, a meta information table of the second database and the second database itself.
22. The method of claim 1, further comprising, after the converting the first database statement into a second database statement that conforms to a syntax rule of a second database based on the type-associated conversion rule:
executing the second database statement in the second database.
23. A database statement conversion apparatus, comprising:
the system comprises an acquisition unit, a processing unit and a processing unit, wherein the acquisition unit is used for acquiring a first database statement which accords with a grammar rule of a first database;
an extraction unit configured to extract a syntax element from the first database statement;
a determining unit configured to determine a type of the first database statement based on the syntax element;
and the conversion unit is used for converting the first database statement into a second database statement which accords with the grammar rule of a second database based on the conversion rule associated with the type.
24. A computer-readable storage medium, in which a computer program is stored, which computer program, when running, performs the method of any one of claims 1 to 22.
25. An electronic device comprising a memory and a processor, characterized in that the memory has stored therein a computer program, the processor being arranged to execute the method of any of claims 1 to 22 by means of the computer program.
CN202111124584.XA 2021-09-24 2021-09-24 Database statement conversion method and device, storage medium and electronic equipment Pending CN113868284A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111124584.XA CN113868284A (en) 2021-09-24 2021-09-24 Database statement conversion method and device, storage medium and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111124584.XA CN113868284A (en) 2021-09-24 2021-09-24 Database statement conversion method and device, storage medium and electronic equipment

Publications (1)

Publication Number Publication Date
CN113868284A true CN113868284A (en) 2021-12-31

Family

ID=78994021

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111124584.XA Pending CN113868284A (en) 2021-09-24 2021-09-24 Database statement conversion method and device, storage medium and electronic equipment

Country Status (1)

Country Link
CN (1) CN113868284A (en)

Similar Documents

Publication Publication Date Title
CN107402992B (en) Distributed NewSQL database system and full-text retrieval establishing method
CN107038207B (en) Data query method, data processing method and device
CN110908997B (en) Data blood relationship construction method and device, server and readable storage medium
US9830341B2 (en) Resource name generation and derivation utilizing attribute space monikers and their associated context
CN109426725B (en) Data desensitization method, device and computer readable storage medium
CN107038222B (en) Database cache implementation method and system
CN112434059B (en) Data processing method, data processing device, computer equipment and storage medium
KR101083563B1 (en) Method and System for Managing Database
CN106407360B (en) Data processing method and device
CN111177788A (en) Hive dynamic desensitization method and dynamic desensitization system
WO2021179722A1 (en) Sql statement parsing method and system, and computer device and storage medium
CN113204571B (en) SQL execution method and device related to write-in operation and storage medium
CN111708805A (en) Data query method and device, electronic equipment and storage medium
CN112579610A (en) Multi-data source structure analysis method, system, terminal device and storage medium
CN110659283A (en) Data label processing method and device, computer equipment and storage medium
CN114579561A (en) Data processing method and device, and storage medium
CN106570095B (en) XML data operation method and equipment
CN110309214B (en) Instruction execution method and equipment, storage medium and server thereof
CN112000690B (en) Method and device for analyzing structured operation statement
CN108694172B (en) Information output method and device
CN111125129A (en) Data processing method and device, storage medium and processor
CN113868284A (en) Database statement conversion method and device, storage medium and electronic equipment
CN116010461A (en) Data blood relationship analysis method and device, storage medium and electronic equipment
CN115525671A (en) Data query method, device, equipment and storage medium
CN114547083A (en) Data processing method and device and electronic equipment

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