Script conversion method and related device
By lexical analysis of SQL scripts, building an abstract syntax tree, and converting layer by layer according to target database rules, the complexity of SQL script migration and conversion in multi-database environment is solved, and efficient and accurate cross-database SQL script conversion is achieved. .
Patent Information
- Application Number
- CN202411527028.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-30
- Publication Date
- 2025-02-14
Smart Images

Figure CN119441262A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of financial technology, and in particular to a script conversion method and related devices. Background Art
[0002] With the development of cloud computing and big data technology, enterprises and organizations are increasingly relying on a variety of database management systems (DBMS) to process, store and analyze data. This multi-database environment brings new challenges, especially in data migration, synchronization and integration. Different database systems, such as Oracle, MySQL, PostgreSQL, GBase 8a, Hive, etc., each use a specific SQL (Structured Query Language) dialect, which leads to complexity and compatibility issues when migrating data and applications between different systems.
[0003] With the shift of enterprise IT (Internet Technology) architecture to cloud service and microservice architecture and the demand for information technology transformation, the demand for SQL script conversion across database management systems is increasing. How to achieve SQL script conversion across database management systems is a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0004] In view of the above problems, the present application provides a script conversion method and related devices to achieve the purpose of SQL script conversion across database management systems. The specific scheme is as follows:
[0005] The first aspect of the present application provides a script conversion method, comprising:
[0006] Get the script to be converted from the source database;
[0007] Performing lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units;
[0008] Performing a combination operation on the basic grammatical units to obtain an intermediate expression; the intermediate expression includes the logical structure data in the script to be converted;
[0009] Constructing an abstract syntax tree corresponding to the intermediate expression;
[0010] Obtaining conversion rules of the target database; the conversion rules include multiple mapping methods and grammar conversion methods;
[0011] According to the type of the intermediate expression corresponding to each unit in the abstract syntax tree, selecting a target mapping mode matching each unit from the multiple mapping modes;
[0012] Each unit in the abstract syntax tree is recursively converted layer by layer using the target mapping method matched by each unit. During the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain the target script of the target database.
[0013] In a possible implementation, lexical analysis and decomposition operations are performed on the script to be converted to obtain basic grammatical units, including:
[0014] Using a regular expression matching method, lexical analysis and decomposition operations are performed on the script to be converted to obtain basic grammatical units;
[0015] Or, based on the context information of the field in the script to be converted, a finite state machine is used to track the lexical structure to determine the state of the field and obtain a basic grammatical unit.
[0016] In a possible implementation, the basic grammar units are combined to obtain an intermediate expression, including:
[0017] Use recursive descent parsing to build the parser;
[0018] The parser is used to perform a combination operation on the basic grammatical units. During the combination operation, the combination process is corrected by backtracking and looking ahead to obtain an intermediate expression.
[0019] In a possible implementation, obtaining the conversion rule of the target database includes:
[0020] Acquire common dialect base class data of each database; the common dialect base class data includes script conversion basic rules common to each database;
[0021] Acquire specific sub-category data of the target database; the specific sub-category data includes script conversion rules specific to the target database;
[0022] The common dialect base class data and the specific subclass data are combined to obtain conversion rules for the target database.
[0023] In a possible implementation, according to the type of the intermediate expression corresponding to each unit in the abstract syntax tree, selecting a target mapping mode matching each unit from the multiple mapping modes includes:
[0024] For each unit in the abstract syntax tree, determining an associated unit in the abstract syntax tree corresponding to the unit;
[0025] Obtaining the type of the intermediate expression of the association unit;
[0026] A target mapping method corresponding to the type of the intermediate expression of the association unit is selected from the multiple mapping methods.
[0027] In a possible implementation, each unit in the abstract syntax tree is recursively converted layer by layer using a target mapping method matched by each unit, and in the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain a target script of the target database, including:
[0028] Starting from the bottom layer of the abstract syntax tree, using the target mapping method matched by each unit in the bottom layer, performing conversion operations on the units to obtain conversion results corresponding to each unit, and adjusting the conversion results using the syntax conversion method to obtain target conversion results for each unit in the bottom layer;
[0029] Utilizing the target conversion results of each unit in the bottom layer, in accordance with the hierarchical order of the abstract syntax tree from the bottom layer to the top layer, using the target mapping method that matches each unit in the conversion rule, the target conversion results of each unit in the bottom layer are spliced layer by layer to obtain the target script of the target database; wherein, during the layer-by-layer splicing process, if there is a conversion conflict, a backtracking method is used to correct the erroneous target conversion result.
[0030] A second aspect of the present application provides a script conversion device, comprising:
[0031] A script acquisition module is used to acquire the script to be converted from the source database;
[0032] A script processing module, used for performing lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units;
[0033] A combination module, used for performing a combination operation on the basic grammar units to obtain an intermediate expression; the intermediate expression includes the logical structure data in the script to be converted;
[0034] A construction module, used to construct an abstract syntax tree corresponding to the intermediate expression;
[0035] A rule acquisition module is used to acquire conversion rules of a target database; the conversion rules include multiple mapping methods and grammar conversion methods;
[0036] A mode selection module, configured to select a target mapping mode matching each unit from the plurality of mapping modes according to the type of the intermediate expression corresponding to each unit in the abstract syntax tree;
[0037] The conversion module is used to recursively convert each unit in the abstract syntax tree layer by layer using the target mapping method matched by each unit. During the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain the target script of the target database.
[0038] A third aspect of the present application provides a computer program product, including computer-readable instructions, which, when executed on an electronic device, enables the electronic device to implement the above-mentioned script conversion method.
[0039] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0040] The memory is used to store computer programs;
[0041] The processor is used to execute the computer program so that the electronic device can implement the above-mentioned script conversion method.
[0042] A fifth aspect of the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement the above-mentioned script conversion method.
[0043] By means of the above technical scheme, the present application provides a script conversion method and related devices. In the present application, when the script to be converted of the source database needs to be converted, the script to be converted is subjected to lexical analysis and decomposition operations to obtain basic grammatical units, and the basic grammatical units are combined to obtain intermediate expressions, wherein the intermediate expressions include the logical structure data in the script to be converted, and the abstract syntax tree corresponding to the intermediate expression is subsequently constructed, and the script conversion is performed based on the abstract syntax tree, thereby achieving the purpose of SQL script conversion across database management systems. In addition, the invention introduces intermediate expressions, and script conversion is performed based on the intermediate expressions. In the scenario where a script needs to be converted into multiple other scripts, only one intermediate expression needs to be generated, which can reduce the parsing operation of the source script, thereby simplifying the script conversion process and improving the script conversion efficiency. In addition, in the present invention, when the script is converted, based on the type of the intermediate expression corresponding to each unit in the abstract syntax tree, a suitable target mapping method is selected, and the corresponding syntax conversion is performed during the conversion process, and a layer-by-layer recursive conversion method is adopted, so that the corresponding mapping method can be used for conversion layer by layer during the script conversion process, and the content of the conversion meets the grammatical requirements, thereby improving the accuracy of the script conversion. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0045] Figure 1 A schematic diagram of a system architecture provided for this application;
[0046] Figure 2 A schematic diagram of a terminal structure provided for this application;
[0047] Figure 3 A schematic diagram of a server structure provided for this application;
[0048] Figure 4 A flowchart of a script conversion method provided for this application;
[0049] Figure 5 A schematic diagram of the structure of a script conversion device provided by the present application;
[0050] Figure 6 A schematic diagram of the structure of an electronic device provided in this application. DETAILED DESCRIPTION
[0051] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.
[0052] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0053] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0054] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards of relevant countries and regions.
[0055] In order to enable those skilled in the art to more clearly understand the present invention, the professional terms in the present invention are now explained.
[0056] 1.Multi-database SQL script conversion:
[0057] Convert SQL scripts or stored procedures written for a specific database management system (DBMS) into a SQL dialect supported by another DBMS. This process involves understanding the differences in syntax, data types, functions, and features between different databases, and mapping and converting them accordingly to achieve compatibility and functionality of database scripts on multiple database platforms. Its purpose is to facilitate database migration, enhance data interoperability between different database systems, and improve development and maintenance efficiency across database environments, especially in multi-cloud and heterogeneous database environments.
[0058] 2. Basic grammatical unit (Token)
[0059] In the process of SQL statement parsing, the basic syntax unit (Token) refers to the smallest syntax component into which the SQL statement text is decomposed. Each Token represents a basic element in the SQL statement, such as a keyword (such as SELECT, FROM), an identifier (such as a table name, a column name), an operator (such as +, -, =) or other syntax structures (such as commas, parentheses). Token is the basis for building an SQL parse tree, which is used to describe the structure and components of an SQL statement in detail. By decomposing the SQL text into a series of Tokens, the parser can more easily identify the various components in the statement and the relationship between them, laying the foundation for further syntax analysis and semantic understanding.
[0060] 3. Intermediate Expression
[0061] Intermediate expressions are custom terms that refer to database-independent abstract representations created during the conversion of SQL statements from the source database dialect to the target database dialect. These expressions capture the logic and structure of SQL statements without relying on any specific database syntax rules. Intermediate expressions usually exist in a tree structure, where each node represents an operation (such as arithmetic operations, logical operations), a function call, or a data access operation. Intermediate expressions serve as a bridge in the SQL script conversion process. By converting the original SQL script into a series of intermediate expressions, the conversion framework can abstract SQL logic that is independent of the specific database. Subsequently, these intermediate expressions are converted and compiled into SQL statements supported by the target database according to the rules of the target database.
[0062] With the development of cloud computing and big data technology, enterprises and organizations are increasingly relying on a variety of database management systems (DBMS) to process, store and analyze data. This multi-database environment brings new challenges, especially in data migration, synchronization and integration. Different database systems, such as Oracle, MySQL, PostgreSQL, GBase 8a, Hive, etc., each use a specific SQL dialect, which leads to complexity and compatibility issues when migrating data and applications between different systems.
[0063] As enterprise IT architecture shifts to cloud services and microservices architecture and the need for ICT transformation increases, the need for SQL script conversion across database management systems is growing. For example, in a multi-cloud, multi-database modern computing environment or during ICT database migration, a solution is needed that can seamlessly migrate and convert SQL scripts between different database management systems (DBMS) to support data migration, system upgrades, or platform migration projects. How to achieve SQL script conversion across database management systems is a technical problem that technicians in this field urgently need to solve.
[0064] In the related art, the source SQL is directly converted into the target SQL. When the same source SQL needs to be converted into multiple target SQLs, the conversions need to be performed one by one, such as converting the source SQL into the first target SQL, and then converting the source SQL into the second target SQL... If there are ten target SQLs, then the conversion needs to be performed ten times, which reduces the conversion efficiency.
[0065] In addition, the related technology also has the following disadvantages:
[0066] 1. Limited target database support:
[0067] Since the relevant technologies are mainly designed for specific database systems, their conversion capabilities are usually limited to a few predefined databases, such as converting from Oracle to MySQL or SQL Server, etc. This limited support range cannot meet the growing demand for cross-database platform migration, especially in the current era of multi-cloud and microservice architecture.
[0068] 2. Conversion coverage is not comprehensive:
[0069] Related conversion methods are often limited to basic SQL statements, such as simple SELECT, INSERT, UPDATE, and DELETE operations. These technologies are often powerless for more complex database objects and operations, such as stored procedures, triggers, and views, resulting in incomplete conversion coverage and inability to fully support the migration and conversion of complex database objects.
[0070] To this end, in order to achieve efficient and accurate conversion of SQL scripts across database platforms, a structured language conversion framework is adopted in the present invention to solve the technical problem of converting SQL scripts across database platforms. The core principle is based on an in-depth analysis of the SQL syntax differences, data type incompatibilities, and execution differences of functions and stored procedures between different database management systems (DBMS). Through this analysis, a conversion system is constructed that can automatically convert SQL scripts from one database dialect to another. It mainly includes the following steps:
[0071] 1. Parsing of SQL statements of the source database
[0072] This step focuses on accurately identifying and parsing various elements of SQL scripts in the source database, including but not limited to keywords, expressions, function calls, and stored procedures. Through highly accurate syntax analysis technology, this process can decompose complex SQL scripts into a series of basic syntax units (tokens) and further build them into intermediate representations that capture the logical structure of SQL scripts without relying on any specific database dialect.
[0073] 2. Generate SQL statements for the target database
[0074] Based on the in-depth analysis of the source database script and the construction of the intermediate representation, the framework then converts these intermediate representations (Expressions) into SQL statements supported by the target database. This process not only covers the conversion of syntax, but also includes complex operations such as data type mapping, function and stored procedure adaptation, etc.
[0075] The present invention has the following advantages:
[0076] 1. Support a wide range of target database systems:
[0077] A multi-database SQL script conversion method is provided. Compared with the above-mentioned one-to-one conversion method which is only suitable for the scenario of converting script files of limited database systems, the implementation of the present invention is not limited to the conversion between two databases, but can support a wide range of database systems. The supported target databases include but are not limited to Oracle, MySQL, PostgreSQL, GBase8a, Hive, etc., thereby improving the applicability and flexibility of the conversion tool.
[0078] 2. Comprehensive coverage of SQL statements and database object conversion:
[0079] In addition to supporting the conversion of basic SQL statements, compared with related technologies that only support SQL conversion, the present invention also supports the conversion of complex database objects including stored procedures, triggers, views, etc. This comprehensive coverage makes the database migration and conversion process more complete, reduces the need for manual intervention that may occur during the migration process, and improves migration efficiency and accuracy.
[0080] 3. Provides deeper conversion support.
[0081] It supports the conversion of different types of SQL statements, such as DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), and the conversion of more complex database object (such as partition table) operations.
[0082] 4. The present invention introduces an intermediate expression and performs script conversion based on the intermediate expression. In a scenario where a script needs to be converted into multiple other scripts, it is only necessary to generate the intermediate expression once, which can reduce the parsing operation of the source script, thereby simplifying the script conversion process and improving the script conversion efficiency.
[0083] Through these advantages, the present invention solves the problems of low conversion efficiency, limited support range of target databases, and incomplete conversion coverage in related technologies. The implementation of the present invention aims to provide users with a more flexible, comprehensive and efficient database SQL script conversion tool to adapt to more types of database systems and conversion requirements in today's ever-changing database technology environment, and better meet the application requirements in multi-cloud and heterogeneous database environments.
[0084] To this end, the present application provides a script conversion method and related devices. In the present application, when the script to be converted of the source database needs to be converted, the script to be converted is subjected to lexical analysis and decomposition operations to obtain basic grammatical units, and the basic grammatical units are combined to obtain intermediate expressions, wherein the intermediate expressions include the logical structure data in the script to be converted, and the abstract syntax tree corresponding to the intermediate expression is subsequently constructed, and the script conversion is performed based on the abstract syntax tree, thereby achieving the purpose of SQL script conversion across database management systems. In addition, the invention introduces intermediate expressions, and script conversion is performed based on the intermediate expressions. In the scenario where a script needs to be converted into multiple other scripts, only one intermediate expression needs to be generated, which can reduce the parsing operation of the source script, thereby simplifying the script conversion process and improving the script conversion efficiency. In addition, in the present invention, when the script is converted, based on the type of the intermediate expression corresponding to each unit in the abstract syntax tree, a suitable target mapping method is selected, and the corresponding syntax conversion is performed during the conversion process, and a layer-by-layer recursive conversion method is adopted, so that the corresponding mapping method can be used for conversion layer by layer during the script conversion process, and the content of the conversion meets the grammatical requirements, thereby improving the accuracy of the script conversion.
[0085] The script conversion method and related devices provided by the present invention can be used in the financial field or other fields, for example, can be used in script conversion application scenarios in the financial field. Other fields are any fields other than the financial field, for example, the database field. The above is only an example and does not limit the application field of the script conversion method and related devices provided by the present invention.
[0086] See also Figure 1 , Figure 1 A schematic diagram of a system architecture is shown. The system may include a terminal 100 and a server 200. The server 200 may include one or more servers ( Figure 1 In the example, a server is included, and the server 200 can provide the method provided in the embodiment of the present application for one or more terminals.
[0087] The terminal 100 may send the source SQL to be converted to the server 200 , and the server 200 may obtain a processing result based on the received parameters and return the processing result to the terminal 100 .
[0088] It should be understood that in some optional implementations, the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without the cooperation of the server, and the embodiments of the present application are not limited to this.
[0089] Next describe Figure 1 The product form of the mid-terminal 100;
[0090] The terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc., and the embodiment of the present application does not impose any limitation on this.
[0091] Figure 2 An optional hardware structure diagram of the terminal 100 is shown.
[0092] refer to Figure 2 As shown, the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), a microphone 162 (optional), an earphone jack 163 (optional), a processor 170, an external interface 180, a power supply 190 and other components. Those skilled in the art will appreciate that Figure 2 These are merely examples of terminals or multi-function devices and do not constitute limitations on the terminals or multi-function devices, which may include more or fewer components than those shown in the figures, or combinations of certain components, or different components.
[0093] The input unit 130 can be used to receive input digital or character information, and generate key signal input related to the user settings and function control of the portable multifunctional device. Specifically, the input unit 130 may include a touch screen 131 (optional) and / or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operation on or near the touch screen using any suitable object such as fingers, joints, stylus, etc.), and drive the corresponding connection device according to a pre-set program. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute the command sent by the processor 170; the touch signal at least includes the touch point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, the touch screen can be implemented using multiple types such as resistive, capacitive, infrared and surface acoustic wave. In addition to the touch screen 131, the input unit 130 can also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.
[0094] Among them, the input device 132 can receive input data and the like.
[0095] The display unit 140 can be used to display information input by the user or provided to the user, various menus of the terminal 100, interactive interfaces, file display and / or playback of any multimedia file. In the embodiment of the present application, the display unit 140 can be used to display the interface of script conversion, processing results, etc.
[0096] The memory 120 can be used to store instructions and data. The memory 120 can mainly include an instruction storage area and a data storage area. The data storage area can store various data, such as multimedia files, texts, etc.; the instruction storage area can store software units such as operating systems, applications, instructions required for at least one function, or their subsets and extensions. It can also include a non-volatile random access memory; provide the processor 170 with hardware, software and data resources including management of computing and processing equipment, and support control software and applications. It is also used for the storage of multimedia files, and the storage of running programs and applications.
[0097] The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100. By running or executing instructions stored in the memory 120 and calling data stored in the memory 120, it executes various functions of the terminal 100 and processes data, thereby controlling the terminal device as a whole. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 170. In some embodiments, the processor and the memory may be implemented on a single chip, and in some embodiments, they may also be implemented separately on separate chips. The processor 170 may also be used to generate corresponding operation control signals, send them to corresponding components of the computing and processing device, read and process data in the software, especially read and process data and programs in the memory 120, so that each functional module therein performs corresponding functions, thereby controlling the corresponding components to act according to the requirements of the instructions.
[0098] Among them, the memory 120 can be used to store software codes related to the script conversion method, the processor 170 can execute the steps of the script conversion method, and can also schedule other units (such as the above-mentioned input unit 130 and display unit 140) to implement corresponding functions.
[0099] The radio frequency unit 110 (optional) can be used for receiving and sending information or receiving and sending signals during a call, for example, after receiving the downlink information of the base station, it is sent to the processor 170 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, LNA), a duplexer, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (Global System of Mobile communication, GSM), General Packet Radio Service (General Packet Radio Service, GPRS), Code Division Multiple Access (Code Division Multiple Access, CDMA), Wideband Code Division Multiple Access (Wideband Code Division Multiple Access, WCDMA), Long Term Evolution (Long Term Evolution, LTE), email, Short Messaging Service (SMS), etc.
[0100] In this embodiment of the present application, the RF unit 110 can send data to the server 200 and receive processing results sent by the server 200.
[0101] It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.
[0102] The terminal 100 also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that the power management system can manage functions such as charging, discharging, and power consumption.
[0103] The terminal 100 further includes an external interface 180 , which may be a standard Micro USB interface or a multi-pin connector, and may be used to connect the terminal 100 to communicate with other devices, or to connect a charger to charge the terminal 100 .
[0104] Although not shown, the terminal 100 may also include a flashlight, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which are not described in detail here. Some or all of the methods described below may be applied in the following embodiments. Figure 2 In the terminal 100 shown.
[0105] Next describe Figure 1 The product form of the server 200;
[0106] Figure 3 A structural diagram of a server 200 is provided, such as Figure 3 As shown, the server 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.
[0107] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0108] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0109] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard drive (HDD), or a solid state drive (SSD).
[0110] The memory 204 may be used to store software codes related to the script conversion method, the processor 202 may execute the steps of the script conversion method of the chip, and may also schedule other units to implement corresponding functions.
[0111] It should be understood that the above-mentioned terminal 100 and server 200 can be centralized or distributed devices, and the processors in the above-mentioned terminal 100 and server 200 (such as processor 170 and processor 202) can be hardware circuits (such as application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processor (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the processor can be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.
[0112] In order to solve the above problems, the present application embodiment provides a script conversion method. The script conversion method of the present application embodiment is described in detail below with reference to the accompanying drawings.
[0113] Reference Figure 4 , Figure 4 A schematic diagram of a script conversion method provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, a script conversion method provided by an embodiment of the present application may include:
[0114] S11. Obtain the script to be converted from the source database.
[0115] The source database in the embodiment of the present invention refers to the initial database before script conversion. The present invention supports a wide range of source databases, such as Oracle, MySQL, PostgreSQL, GBase 8a, Hive, etc. Corresponding to the source database is the target database, that is, the database after script conversion. If the source database is Oracle and the target database is MySQL, then the Oracle SQL script needs to be converted into the MySQL SQL script.
[0116] The source database and target database list supported by the embodiment of the present invention include but are not limited to common relational and non-relational databases, and the types of source database and target database can be configured by the user interface module according to the user's needs. In addition, the user can also configure any specific conversion options.
[0117] The script to be converted is a script that complies with the format of the source database. The script uses SQL statements, and the SQL statements may be DDL, DML, DQL, and other statements.
[0118] When uploading, the script to be converted of the source database may be an SQL script or statement of the source database uploaded by the user.
[0119] The user interface module is responsible for receiving the SQL scripts or statements of the source database uploaded by the user, and transmitting the SQL scripts or statements of the source database to the parsing module for parsing operations.
[0120] The user interface module allows users to provide SQL scripts or statements in a variety of ways, including but not limited to direct text input, file upload, or passing data through API (Application Programming Interface) calls. For example, users upload through the text input function or file upload function of the user interface, or users upload through the API.
[0121] S12: performing lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units.
[0122] Step S12 is implemented by a parsing module, which can be implemented by a SQL parser. The present invention can develop and integrate a powerful SQL parser that can read and understand SQL statements in various database dialects. It should be ensured that the parser can handle complex database objects including stored procedures, triggers, views, etc. For statements containing specific database dialects, the parser must be able to accurately identify and appropriately convert them into intermediate representations.
[0123] As the starting point of the conversion process, the parsing module first receives the SQL script input by the user, performs lexical and grammatical analysis, decomposes the SQL string in the input SQL script into a sequence of tokens, and then assembles these tokens into intermediate expressions (Expressions) and outputs the intermediate expressions. These intermediate expressions are independent of the specific database and represent the logical structure and intent of the SQL script.
[0124] Specifically, the SQL string in the input script to be converted is decomposed into a Token sequence through the lexical analysis function in the parsing module. Token represents the basic building blocks of SQL statements, such as keywords (such as SELECT, FROM), identifiers (such as table names, column names), strings, operators (such as +, -, =) or other grammatical structures (such as commas, brackets), etc.
[0125] Among them, during lexical analysis, a custom lexical analyzer (Tokenizer) is used. The lexical analyzer decomposes the input character sequence into a series of tokens. The lexical analyzer outputs the Token sequence to provide input for the syntax analyzer, so that the syntax analyzer combines the Tokens to obtain the intermediate expression.
[0126] In an actual scenario, a regular expression matching method may be used to perform lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units.
[0127] Specifically, a regular expression is used to define the matching pattern of each token. For example, an identifier may consist of letters, numbers, and underscores, but not start with a number. Then, based on the configured regular expression, the script to be converted is lexically analyzed and decomposed to obtain a token.
[0128] In addition to using regular expressions, a state machine can also be used to decompose SQL. Specifically, based on the context information of the fields in the script to be converted, a finite state machine is used to track the lexical structure to determine the field state and obtain a basic grammar unit.
[0129] In detail, a finite state machine (FSM) is used internally to utilize the contextual information of the field, such as the field connected by the context, to track the current state of the lexical analysis, and decide which state to transfer to based on the sequence of input characters until a complete token is recognized.
[0130] In this embodiment, the obtained Token includes the Token content and type. In addition, when performing SQL decomposition, error handling is supported. When the current character sequence cannot be matched to any defined Token pattern, the lexical analyzer needs to be able to report an error, such as an illegal character or an unsupported grammatical structure.
[0131] S13. Perform a combination operation on the basic grammatical units to obtain an intermediate expression.
[0132] The intermediate expression includes the logical structure data in the script to be converted.
[0133] Step S13 is still implemented by the above-mentioned parsing module. The parsing module uses the internal syntax analysis function to convert the token into an intermediate expression (Expressions), accurately reflecting the structure and logic of the SQL statement, and providing basic data for the subsequent construction of the abstract syntax tree (AST).
[0134] Specifically, the syntax analysis function is implemented by a syntax analyzer (Parser), the input of which is the token sequence output by the lexical analyzer. According to the syntax rules of SQL statements, the token sequence is organized into a higher-level structure, such as an expression.
[0135] The specific implementation technologies are:
[0136] Recursive descent parsing: The parser is constructed using recursive descent parsing technology. In the parser, each grammar rule corresponds to one or more functions. The advantage of this method is that it is intuitive, easy to understand, and easy to implement and maintain.
[0137] The parser is then used to perform a combination operation on the basic syntax unit Token. In the combination operation process, backtracking and look-ahead are supported. Specifically, in order to handle complex SQL syntax and eliminate ambiguity, the syntax analyzer needs to correct the combination process through backtracking (undoing some previous Token consumption decisions) and look-ahead (looking at future Tokens to determine the current step) technology. For example, when encountering a syntax error, the error location and type are reported, and error recovery is performed as much as possible to continue analyzing subsequent statements and finally obtain the intermediate expression.
[0138] In this embodiment, the constructed intermediate expressions include SQL logical structure data, representing the abstract semantics of the SQL script, and these intermediate expressions are then transmitted to the generation module. Each intermediate expression has corresponding content and type.
[0139] It should be noted that, in the scenario of converting a source script into multiple other target scripts, the source script needs to be parsed and converted when converting to each target script, while in the present invention, the source script is parsed only once, the source script is parsed into an intermediate expression, and then the intermediate expression is sequentially converted into the corresponding target script. That is, the present invention reduces the parsing process, and thus can improve efficiency in the scenario of converting a source script into multiple other target scripts.
[0140] S13. Construct an abstract syntax tree corresponding to the intermediate expression.
[0141] In order to construct the logical relationship between each intermediate expression, an abstract syntax tree (AST) can be constructed. Each node represents a component of the SQL statement, such as an intermediate expression, such as a SELECT clause, a WHERE condition, etc., or a structure that decomposes the intermediate expression. For example, max(A) is located in an intermediate expression and is subdivided into four parts: max, (, A, and ). In the abstract syntax tree, max(A) is the upper level of max, (, A, and ), and the implementation process of other expressions is similar.
[0142] Through the above method, the original script to be converted can be constructed into an abstract syntax tree. The top layer is the script to be converted, and the nodes are decomposed in order. The lower the level, the less the content of the node. The decomposition process can refer to the above max(A). For example, in the generation process, each SQL statement (such as a SELECT statement) is first parsed into a top-level expression (such as SelectExpression), which contains several sub-expressions, such as a table name expression (TableNameExpression), a FROM clause expression (FromExpression), etc. These expressions form a tree structure, and the tree result is the abstract syntax tree. The abstract syntax tree is output to provide a structure-based SQL expression for the generation module.
[0143] S14, obtaining conversion rules of the target database; the conversion rules include multiple mapping methods and grammar conversion methods.
[0144] Specifically, the generation module receives the intermediate expression from the parsing module, and converts and optimizes the intermediate expression according to the conversion rules of the target database, and finally converts the intermediate expression into a SQL statement compatible with the target database to generate an executable SQL script.
[0145] Among them, the conversion rules define clear rules for each conversion from the source database to the target database. According to the SQL statement specifications of each database, a comprehensive rule base is established, which not only covers basic data definition and operation statements, but also includes support for advanced features and complex objects.
[0146] The conversion rules include various mapping methods such as data type mapping, function mapping, etc., to ensure the correctness and optimality of the converted SQL in the target database. Some examples of data type mapping relationships can be referred to Table 1 and Table 2.
[0147] Table 1
[0148]
[0149] Table 2
[0150]
[0151]
[0152] In addition, when the mapping method is used, multiple mapping methods are used in combination with the syntax conversion method, so that the converted SQL script conforms to the syntax structure of the target database.
[0153] In another implementation of the present invention, obtaining the conversion rule of the target database may include:
[0154] 1) Acquire the common dialect base class data of each database, wherein the common dialect base class data includes the script conversion basic rules common to each database.
[0155] Specifically, the common dialect base class data adopts a dialect-based architecture design.
[0156] In the code implementation, a dialect base class is designed, which describes the basic script conversion rules common to all databases when parsing and generating SQL statements, including basic parsing rules, generation rules, and token rules. This base class provides a common framework and interface for all supported database systems.
[0157] 2) Acquire specific sub-category data of the target database; the specific sub-category data includes script conversion rules specific to the target database.
[0158] In addition to the common dialect base class data mentioned above, each database also has its own unique script conversion rules, which can be designed independently for each database. Database, such as the target database-specific script conversion rules can be implemented using database-specific subclasses.
[0159] Specifically, for each database, there is a subclass inherited from the dialect base class, which implements the parsing rules, generation rules, and token rules of the corresponding database. This design pattern makes it more flexible and simple to expand support for new databases.
[0160] In this embodiment, by setting a subclass inherited from the dialect base class, the rule base is concretized in the dialect subclass and customized for each database, including data type mapping, function mapping, and grammar conversion rules, which are directly embedded in the corresponding dialect subclass implementation, improving the accuracy and efficiency of rule application.
[0161] 3) Combining the common dialect base class data and the specific subclass data to obtain conversion rules for the target database.
[0162] The above-mentioned dialect base class and subclass are combined so that the subclass inherits the dialect base class, that is, the common dialect base class data and the specific subclass data are combined to obtain the conversion rules of the target database.
[0163] In this embodiment, when determining the database conversion rules, the database dialect-oriented design realizes the dynamic adaptation of each database-specific syntax and conversion rules. The implementation of each database dialect inherits from a common base class, which is convenient for expansion and support of new database systems, realizes the dynamic adaptation of database dialects and conversion rules, simplifies the rule generation process, and improves the efficiency of rule generation.
[0164] S15. According to the type of the intermediate expression corresponding to each unit in the abstract syntax tree, select a target mapping mode that matches each unit from the multiple mapping modes.
[0165] In practical applications, there are various nodes in the abstract syntax tree. Each node is a unit that needs to be processed and needs to undergo corresponding SQL conversion operations. During the specific conversion, it is necessary to select a suitable mapping method from the multiple mapping methods mentioned above, and then use the mapping relationship to convert the unit.
[0166] When a mapping method is specifically selected, a target mapping method matching each of the units may be selected from the multiple mapping methods based on the type of the intermediate expression corresponding to the unit.
[0167] S16. Recursively convert each unit in the abstract syntax tree layer by layer using the target mapping method matched by each unit. In the process of layer-by-layer recursive conversion, adjust the conversion result using the syntax conversion method to obtain the target script of the target database.
[0168] Specifically, after determining the target mapping mode for each unit to match, the SQL conversion is performed layer by layer according to the bottom-up processing logic of the abstract syntax tree, that is, the process of generating SQL statements is recursive. Starting from the bottom-level expression, according to the expression type and content, as well as the specific rules of the target database, the SQL fragments of each sub-expression are recursively generated, and finally spliced into a complete SQL statement. This recursive process ensures the flexibility of SQL statement generation and the correctness of the structure.
[0169] And during the conversion process, the conversion result is adjusted using the syntax conversion method, and after the top-level conversion is completed, the target script of the target database can be obtained.
[0170] In this embodiment, when it is necessary to convert the script to be converted in the source database, the script to be converted is subjected to lexical analysis and decomposition operations to obtain basic grammatical units, and the basic grammatical units are combined to obtain an intermediate expression, which includes the logical structure data in the script to be converted, and then an abstract syntax tree corresponding to the intermediate expression is constructed, and the script conversion is performed based on the abstract syntax tree, thereby achieving the purpose of SQL script conversion across database management systems. In addition, the invention introduces intermediate expressions, and script conversion is performed based on the intermediate expressions. In the scenario where a script needs to be converted into multiple other scripts, only one intermediate expression needs to be generated, which can reduce the parsing operation of the source script, thereby simplifying the script conversion process and improving the script conversion efficiency. In addition, in the present invention, when converting the script, based on the type of the intermediate expression corresponding to each unit in the abstract syntax tree, a suitable target mapping method is selected, and the corresponding syntax conversion is performed during the conversion process, and a layer-by-layer recursive conversion method is adopted, so that during the script conversion process, the corresponding mapping method can be used for conversion layer by layer according to the level, and the converted content meets the grammatical requirements, thereby improving the accuracy of the script conversion.
[0171] In another implementation of the present invention, selecting a target mapping method matching each unit from the multiple mapping methods according to the type of the intermediate expression corresponding to each unit in the abstract syntax tree may include:
[0172] 1) For each unit in the abstract syntax tree, determine an associated unit corresponding to the unit in the abstract syntax tree.
[0173] In this embodiment, the generation module does not simply use a static SQL template to perform SQL conversion, but dynamically generates SQL statements through a dynamic template engine according to the type and content of the current expression and the rules defined by the dialect subclass of the target database. This method allows for more flexible handling of differences between different databases.
[0174] When specifically determining the mapping method, for each unit in the abstract syntax tree, such as the above-mentioned "max,(,A,)", its corresponding associated unit can be the upper-level unit "max(A)" and / or all its subordinate units.
[0175] 2) Obtain the type of the intermediate expression of the association unit.
[0176] In this embodiment, taking "max(A)" as an example, the type of "max(A)" is a function. The type of "max(A)" is also the type of "max, (, A,)".
[0177] 3) From the multiple mapping modes, select a target mapping mode corresponding to the type of the intermediate expression of the association unit.
[0178] In this embodiment, since "max(A)" is a function, the function mapping in the mapping mode can be used.
[0179] If the association unit "max(A)" is not considered, and only "max, (, A,)" is directly processed, its meaning cannot be clarified, and an incorrect mapping method may be used during conversion, resulting in a mapping error. That is, by using the content and type of the association unit (such as the upper level unit) in the present invention, the mapping method can be accurately selected.
[0180] In another implementation of the present invention, using the target mapping method matched by each of the units, recursively converting each unit in the abstract syntax tree layer by layer, and adjusting the conversion result by using the syntax conversion method during the layer-by-layer recursive conversion process to obtain the target script of the target database may include:
[0181] 1) Starting from the bottom layer of the abstract syntax tree, using the target mapping method matched by each unit in the bottom layer, the unit is converted to obtain the conversion result corresponding to each unit, and the conversion result is adjusted using the syntax conversion method to obtain the target conversion result of each unit in the bottom layer.
[0182] Specifically, during the layer-by-layer recursive conversion process, starting from the bottom layer of the abstract syntax tree, the target mapping method matched by each unit in the bottom layer is used to convert the unit to obtain the corresponding conversion result of each unit. The matching target mapping method is determined by the type of the intermediate expression of the unit at the previous level and all its subordinate expressions.
[0183] The to_char function in Oracle can convert numeric or date types into string types according to the format, and different conversion methods are performed according to the number of parameters and parameter types. If to_char(sysdate,'yyyy-mm-ddHH:MI:SS') is taken as an example, the parsing process will parse it into a function expression, and further into a to_char expression. During the generation process, according to the expression type (to_char), the mapping method of to_char is entered. In the to_char mapping method, two subordinate expression parameters are recognized, and 'yyyy-mm-dd HH:MI:SS' is a date format, so it backtracks to enter the to_char mapping method of the date type. Then, according to the underlying expressions "to_char, (, sysdate, 'yyyy-mm-dd HH:MI:SS',)" in to_char(sysdate,'yyyy-mm-dd HH:MI:SS'), the conversion is performed to finally obtain the target conversion result, such as date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss') in Hive.
[0184] 2) Utilizing the target conversion results of each unit in the bottom layer, in accordance with the hierarchical order of the abstract syntax tree from bottom layer to top layer, using the target mapping method that matches each unit in the conversion rule, the target conversion results of each unit in the bottom layer are spliced layer by layer to obtain the target script of the target database.
[0185] Among them, after obtaining the target conversion results of each unit in the lowest layer, the penultimate layer is processed, and the conversion results of the penultimate layer are obtained according to the unit relationship between the penultimate layer and the lowest layer. For example, the conversion result of each of "max, (, A,)" is used to obtain the conversion result of "max(A)". During the conversion, the target mapping method corresponding to "max(A)" is also considered, that is, the type of the intermediate expression of the previous level of "max(A)". The conversion results of each of "max, (, A,)" are spliced to finally obtain the conversion result of "max(A)". After the processing of each unit in the penultimate layer is completed, the third-to-last layer is processed. The processing process of the third-to-last layer is similar to that of the penultimate layer. After the processing process from bottom to top, after the top layer is processed, the target script of the target database can be obtained.
[0186] In addition, conflicts may occur during the layer-by-layer splicing generation process, which requires correction. When there is a conversion conflict, the wrong target conversion result is corrected by backtracking. For example, in the actual splicing process, the return value of the lower-level expression is not directly spliced. Take decode(sign(1-2),1,'1>2',-1,'1<2','1?2dont know') as an example: the parsing result is the decode expression, and the lower level of the decode expression will be three sub-expressions. The first two sub-expressions are if expressions, and the third is a default expression. When the three sub-expressions are returned to the upper level (decode expression), they cannot be simply spliced. Because decode has structural changes. In this case, the structural order of each unit will be adjusted during the generation process of the upper layer (decode expression) to meet the grammatical structure of the target database. For example, the conversion result in Hive can be: case whensign(1-2)=1then'1>2'when sign(1-2)=-1then'1<2'else'1?2dont konw'end.
[0187] Conflict detection can be achieved through the Error Handling Module, which designs an error handling algorithm that can automatically provide or recommend solutions and perform corresponding adjustment operations based on the error type and context.
[0188] In addition, the error reporting module monitors the entire process, and any problems and errors found during the parsing or generation phase will be captured and reported. It not only provides error details, but also tries to provide correction suggestions (including error location, cause and solution, among which the solution is used to guide users on how to modify SQL scripts or refer users to more detailed documents to solve problems, so as to help users understand and correct problems.
[0189] In addition, the error reporting module can also manage and schedule data verification tasks, including task creation, execution, monitoring and status update, to optimize task execution speed and resource utilization.
[0190] By configuring the error prompt module, an advanced error detection and reporting mechanism is implemented, which can accurately identify the link where the problem occurs and display the error information through a user-friendly interface.
[0191] After the SQL conversion is completed, the generated SQL statements of the target database and any warning or error messages generated during the conversion process are displayed to the user through the user interface module through a graphical user interface or API response. The user interface module also provides an interactive feedback mechanism, allowing users to modify and resubmit SQL scripts directly on the interface according to the error information and solution suggestions provided by the prompt error reporting module. As can be seen from the above, the user interface module plays an interactive role. Users submit SQL scripts through this module and select the source database and target database types. After the conversion is completed, the module displays the generated SQL statements and any warning or error messages to the user. For error messages, users can modify and resubmit the original SQL script based on the feedback error and warning information on the same interface, and the module will start the conversion process again. For error-free SQL scripts, users can directly execute the generated SQL statements in the target database system.
[0192] In another implementation of the present invention, some conversions can be shown in Table 3. Table 3 shows the conversion results of the same SQL in different databases.
[0193] Table 3
[0194]
[0195]
[0196]
[0197] In another implementation of the present invention, in order to achieve the correct conversion of SQL, the conversion engine will be extensively tested for conversion performance of large-scale databases, including unit testing, integration testing and performance testing, and will be optimized accordingly to ensure the accuracy and efficiency of the conversion. Optimization will be performed for the problems and performance bottlenecks found.
[0198] In addition, in addition to supporting the conversion of basic SQL statements, the present invention also supports the conversion of complex database objects including stored procedures, triggers, views, etc. When implementing the specific conversion, the conversion strategy of complex database objects such as stored procedures, triggers, views, etc. can be configured, and the conversion can be performed according to the strategy.
[0199] In this embodiment, the generation module converts the intermediate expression into a target database compatible SQL statement according to the rules of the target database. This process uses database-specific conversion rules and templates to generate target database compatible SQL statements that correspond to the original SQL scripts of the source database in structure and function, but conform to the syntax and best practices of the target database.
[0200] In addition, in the SQL conversion process, the abstract syntax tree is used to structure and process complex SQL statements, ensuring the flexibility of the conversion process and the accuracy of SQL statement generation. This method is particularly suitable for processing nested queries and complex SQL structures.
[0201] In addition, an interactive error handling and user feedback mechanism is introduced, which captures and reports problems in real time during the conversion process through the prompt error reporting module and provides correction suggestions. This not only improves the fault tolerance of the conversion process, but also greatly enhances the user experience.
[0202] In addition, a highly user-friendly interface module is designed to support multiple data input methods, configure conversion options, and display conversion results and feedback. The module provides services through the Web (World Wide Web) interface and API, and also supports the integration of automation tools and third-party systems to meet the needs of different user scenarios.
[0203] As can be seen from the above, the present invention provides a multi-database SQL script conversion method, which uses a unified framework to process multiple database dialects, including a custom lexical analyzer and a syntax analyzer, and an object-oriented intermediate expression construction method. The framework allows efficient parsing of the source database SQL script and converts it into a target database compatible SQL statement, aiming to solve the key technical problems encountered in cross-database migration and data integration. The advantages and beneficial effects of the inference analysis of the present invention are as follows:
[0204] First, by implementing a unified parsing and generation framework and a dynamic adaptation system for database dialects, the present invention significantly improves the accuracy and flexibility of cross-database SQL script conversion. This solution allows the system to accurately understand and process SQL scripts from different source databases, including complex nested queries and specific database dialects, and then generate optimized and efficiently executed SQL statements based on the rules and best practices of the target database. Therefore, the present invention solves conversion errors and performance issues caused by differences in database dialects, ensuring smooth data migration and application integration.
[0205] Secondly, the generation and processing mechanism of the abstract syntax tree, as well as the introduction of interactive error handling and user feedback mechanisms, have greatly improved the user-friendliness and fault tolerance of the conversion process. Users can not only intuitively understand the process and results of SQL script conversion, but also get clear error prompts and correction suggestions when encountering problems. This lowers the technical threshold, allowing non-expert users to effectively perform cross-database conversion of SQL scripts. At the same time, this feedback mechanism also improves the efficiency of the conversion process. Users can quickly identify and correct problems, avoiding the time wasted on trial and error.
[0206] In summary, the present invention effectively solves the accuracy, flexibility and user-friendliness problems of cross-database SQL script conversion through technological innovation, and provides a comprehensive and efficient solution for data migration and application integration. These advantages and beneficial effects directly respond to the actual needs faced by enterprises and developers in the current multi-database environment, and help to improve the success rate of database migration projects and reduce costs and time expenses. In addition, a more general, flexible and comprehensive database SQL script conversion method is provided to meet the challenges of multi-database, multi-cloud and heterogeneous database environments. In addition, in addition to supporting the conversion of basic SQL statements, the present invention also supports the conversion of complex database objects including stored procedures, triggers, views, etc. This comprehensive coverage makes the database migration and conversion process more complete, reduces the need for manual intervention that may occur during the migration process, and improves migration efficiency and accuracy.
[0207] In order for technicians to better understand the conversion process in the present invention, detailed developer documentation and user manuals can be written, including conversion rules, usage examples, and FAQs. In addition, a user feedback mechanism is established to collect problems and suggestions during use and continuously improve the product.
[0208] A script conversion method provided by an embodiment of the present application is introduced above, and a device for executing the above script conversion method will be introduced below.
[0209] See also Figure 5 , Figure 5 This is a schematic diagram of the structure of a script conversion device provided in an embodiment of the present application. Figure 5 As shown, the script conversion device comprises:
[0210] The script acquisition module 11 is used to acquire the script to be converted from the source database;
[0211] The script processing module 12 is used to perform lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units;
[0212] A combination module 13, used for performing a combination operation on the basic grammar units to obtain an intermediate expression; the intermediate expression includes the logical structure data in the script to be converted;
[0213] A construction module 14 is used to construct an abstract syntax tree corresponding to the intermediate expression;
[0214] A rule acquisition module 15 is used to acquire conversion rules of a target database; the conversion rules include multiple mapping methods and grammar conversion methods;
[0215] A mode selection module 16, configured to select a target mapping mode matching each unit from the plurality of mapping modes according to the type of the intermediate expression corresponding to each unit in the abstract syntax tree;
[0216] The conversion module 17 is used to recursively convert each unit in the abstract syntax tree layer by layer using the target mapping method matched by each unit. During the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain the target script of the target database.
[0217] In a possible implementation, the script processing module 12 is specifically used for:
[0218] The script to be converted is subjected to lexical analysis and decomposition operations by using a regular expression matching method to obtain basic grammatical units; or, based on context information of fields in the script to be converted, a finite state machine is used to track lexical information to determine field states to obtain basic grammatical units.
[0219] In a possible implementation, the combining module 13 includes:
[0220] The parser building submodule is used to build the parser using recursive descent parsing;
[0221] The first combination submodule is used to use the parser to perform a combination operation on the basic grammatical units. During the combination operation, the combination process is corrected by backtracking and looking ahead to obtain an intermediate expression.
[0222] In a possible implementation, the rule acquisition module 15 includes:
[0223] The first acquisition submodule is used to acquire common dialect base class data of each database; the common dialect base class data includes script conversion basic rules common to each database;
[0224] The second acquisition submodule is used to acquire specific sub-category data of the target database; the specific sub-category data includes script conversion rules specific to the target database;
[0225] The second combining submodule is used to combine the common dialect base class data and the specific subclass data to obtain the conversion rules of the target database.
[0226] In a possible implementation, the mode selection module 16 includes:
[0227] A unit determination submodule, configured to determine, for each unit in the abstract syntax tree, an associated unit corresponding to the unit in the abstract syntax tree;
[0228] A type acquisition submodule, used to acquire the type of the intermediate expression of the association unit;
[0229] The mode determination submodule is used to select a target mapping mode corresponding to the type of the intermediate expression of the association unit from the multiple mapping modes.
[0230] In a possible implementation, the conversion module 17 includes:
[0231] A conversion submodule is used to start from the bottom layer of the abstract syntax tree, use the target mapping method matched by each unit in the bottom layer to perform a conversion operation on the unit, obtain a conversion result corresponding to each unit, and adjust the conversion result using the syntax conversion method to obtain a target conversion result of each unit in the bottom layer;
[0232] The splicing submodule is used to utilize the target conversion results of each unit in the bottom layer, according to the hierarchical order of the abstract syntax tree from the bottom layer to the top layer, and use the target mapping method that matches each unit in the conversion rule to splice the target conversion results of each unit in the bottom layer layer by layer to obtain the target script of the target database; wherein, during the layer-by-layer splicing process, if there is a conversion conflict, a backtracking method is used to correct the erroneous target conversion result.
[0233] In this embodiment, when it is necessary to convert the script to be converted in the source database, the script to be converted is subjected to lexical analysis and decomposition operations to obtain basic grammatical units, and the basic grammatical units are combined to obtain an intermediate expression, which includes the logical structure data in the script to be converted, and then an abstract syntax tree corresponding to the intermediate expression is constructed, and the script conversion is performed based on the abstract syntax tree, thereby achieving the purpose of SQL script conversion across database management systems. In addition, the invention introduces intermediate expressions, and script conversion is performed based on the intermediate expressions. In the scenario where a script needs to be converted into multiple other scripts, only one intermediate expression needs to be generated, which can reduce the parsing operation of the source script, thereby simplifying the script conversion process and improving the script conversion efficiency. In addition, in the present invention, when converting the script, based on the type of the intermediate expression corresponding to each unit in the abstract syntax tree, a suitable target mapping method is selected, and the corresponding syntax conversion is performed during the conversion process, and a layer-by-layer recursive conversion method is adopted, so that during the script conversion process, the corresponding mapping method can be used for conversion layer by layer according to the level, and the converted content meets the grammatical requirements, thereby improving the accuracy of the script conversion.
[0234] It should be noted that, for the working process of each module and sub-module in this embodiment, please refer to the corresponding description in the above embodiment, which will not be repeated here.
[0235] An embodiment of the present application also provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0236] The memory is used to store computer programs;
[0237] The processor is used to execute the computer program so that the electronic device can implement the above-mentioned script conversion method.
[0238] refer to Figure 6 As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present application. The electronic device in the embodiments of the present application may include but is not limited to fixed terminals such as mobile phones, notebook computers, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Figure 6 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0239] like Figure 6 As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0240] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a memory card, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0241] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any script conversion method provided in the embodiment of the present application.
[0242] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any script conversion method provided in the embodiment of the present application.
[0243] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0244] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0245] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0246] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.
Claims
1. A script conversion method, characterized in that: include: Get the script to be converted from the source database; Performing lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units; Performing a combination operation on the basic grammatical units to obtain an intermediate expression; the intermediate expression includes the logical structure data in the script to be converted; Constructing an abstract syntax tree corresponding to the intermediate expression; Obtaining conversion rules of the target database; the conversion rules include multiple mapping methods and grammar conversion methods; According to the type of the intermediate expression corresponding to each unit in the abstract syntax tree, selecting a target mapping mode matching each unit from the multiple mapping modes; Each unit in the abstract syntax tree is recursively converted layer by layer using the target mapping method matched by each unit. During the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain the target script of the target database.
2. The script conversion method according to claim 1, characterized in that: Perform lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units, including: Using a regular expression matching method, lexical analysis and decomposition operations are performed on the script to be converted to obtain basic grammatical units; Or, based on the context information of the field in the script to be converted, a finite state machine is used to track the lexical structure to determine the state of the field and obtain a basic grammatical unit.
3. The script conversion method according to claim 1, characterized in that: The basic grammatical units are combined to obtain an intermediate expression, including: Use recursive descent parsing to build the parser; The parser is used to perform a combination operation on the basic grammatical units. During the combination operation, the combination process is corrected by backtracking and looking ahead to obtain an intermediate expression.
4. The script conversion method according to claim 1, characterized in that: Get the conversion rules of the target database, including: Acquire common dialect base class data of each database; the common dialect base class data includes script conversion basic rules common to each database; Acquire specific sub-category data of the target database; the specific sub-category data includes script conversion rules specific to the target database; The common dialect base class data and the specific subclass data are combined to obtain conversion rules for the target database.
5. The script conversion method according to claim 1, characterized in that: According to the type of the intermediate expression corresponding to each unit in the abstract syntax tree, a target mapping method matching each unit is selected from the multiple mapping methods, including: For each unit in the abstract syntax tree, determining an associated unit in the abstract syntax tree corresponding to the unit; Obtaining the type of the intermediate expression of the association unit; A target mapping method corresponding to the type of the intermediate expression of the association unit is selected from the multiple mapping methods.
6. The script conversion method according to claim 1, characterized in that: Using the target mapping method matched by each of the units, recursively converting each unit in the abstract syntax tree layer by layer, and in the recursive conversion process layer by layer, adjusting the conversion result by using the syntax conversion method to obtain the target script of the target database, including: Starting from the bottom layer of the abstract syntax tree, using the target mapping method matched by each unit in the bottom layer, performing conversion operations on the units to obtain conversion results corresponding to each unit, and adjusting the conversion results using the syntax conversion method to obtain target conversion results for each unit in the bottom layer; Utilizing the target conversion results of each unit in the bottom layer, in accordance with the hierarchical order of the abstract syntax tree from the bottom layer to the top layer, using the target mapping method that matches each unit in the conversion rule, the target conversion results of each unit in the bottom layer are spliced layer by layer to obtain the target script of the target database; wherein, during the layer-by-layer splicing process, if there is a conversion conflict, a backtracking method is used to correct the erroneous target conversion result.
7. A script conversion device, characterized in that: include: A script acquisition module is used to acquire the script to be converted from the source database; A script processing module, used for performing lexical analysis and decomposition operations on the script to be converted to obtain basic grammatical units; A combination module, used for performing a combination operation on the basic grammar units to obtain an intermediate expression; the intermediate expression includes the logical structure data in the script to be converted; A construction module, used to construct an abstract syntax tree corresponding to the intermediate expression; A rule acquisition module is used to acquire conversion rules of a target database; the conversion rules include multiple mapping methods and grammar conversion methods; A mode selection module, configured to select a target mapping mode matching each unit from the plurality of mapping modes according to the type of the intermediate expression corresponding to each unit in the abstract syntax tree; The conversion module is used to recursively convert each unit in the abstract syntax tree layer by layer using the target mapping method matched by each unit. During the layer-by-layer recursive conversion process, the conversion result is adjusted using the syntax conversion method to obtain the target script of the target database.
8. A computer program product, characterized in that It comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the script conversion method as claimed in any one of claims 1 to 6.
9. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the script conversion method as described in any one of claims 1 to 6.
10. A computer storage medium, characterized in that: The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the script conversion method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Method and device for SQL statement conversion
CN109062918A
Method and device for converting SQL (Structured Query Language) statements between relational databases
CN117290377A
Method for constructing syntax tree and method for generating retrieval statement
CN118445312A
Cited By
Method for using Python ORM framework by GBASE8a
CN120085856A
Cross-normal-form automatic conversion method and system for parameterized geometric modeling script and medium
CN122020760A
A cross-paradigm automatic conversion method, system and medium for a parameterized geometry modeling script
CN122020760B