A code writing processing method and system compatible with multiple types of databases
By identifying and analyzing the query code entered by users, and generating the object code that passes the compatibility test, the problem of insufficient support for non-relational databases in the existing technology is solved, and efficient compatibility and query performance of multi-type databases are achieved.
Patent Information
- Application Number
- CN202411943926.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-04-08
AI Technical Summary
Existing database tools cannot effectively support non-relational databases, resulting in high limitations on database code queries.
By identifying the query code entered by the user, multiple database types are determined, and corresponding query statements are generated based on the query code analysis. Then, the preset query code base and adapter are called to generate the object code that passes the compatibility test, and finally generate the query command to execute the query service.
It realizes compatibility support for multi-type databases, ensures efficient query performance and code availability, reduces code logic transformation and operation, and improves user experience.
Smart Images

Figure CN119781752A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of information data processing, and in particular to a method and system for writing and processing codes compatible with multiple types of databases. Background Art
[0002] With the development of the data age, the types and sizes of databases are constantly expanding, which brings certain challenges to database management.
[0003] Currently, most data applications tend to use ORM (object-relational mapping) layers such as Hibernate, Entity Framework, or Django ORM. These ORM tools provide an abstraction layer that enables developers to use objects and classes to interact with the database, thereby achieving a certain degree of compatibility between different databases. However, these ORM tools can only focus on relational databases, and their support for non-relational databases is relatively limited. At the same time, with the rise of NoSQL databases, many applications have begun to adopt multi-model databases to take advantage of the advantages of both relational and non-relational databases. However, existing ORM tools may not be able to fully meet the needs of these applications. Summary of the invention
[0004] The present invention provides a code writing processing method and system compatible with multiple types of databases to solve the technical problem that existing database tools in the prior art cannot provide corresponding support and application for non-relational databases, resulting in high limitations on code queries for databases.
[0005] In order to solve the above technical problems, an embodiment of the present invention provides a method for writing code compatible with multiple types of databases, including: In response to a query code input by a user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine a query statement corresponding to the query code; Determine the corresponding execution logic and the target database according to the query statement, call a preset query code library corresponding to the target database, and determine the corresponding first logic code according to the execution logic; Inputting the execution logic into an adapter corresponding to the target database, and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating a corresponding second logic code according to the execution logic and the structural features; Performing a compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, using the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code; A corresponding query command is generated according to the target code, so that a corresponding query service is executed according to the query command, thereby completing the processing of the query code.
[0006] As a preferred solution, in response to the query code input by the user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine the query statement corresponding to the query code specifically includes: Responding to a query code input by a user; Analyze the query code to obtain the grammatical structure, usage functions and operators corresponding to the query code; Determine the data type and data structure of the query code according to the grammatical structure, the use function and the operator; According to the syntax structure of the query code, the used functions and operators, and the data type and data structure of the query code, identify the type of database that needs to be queried corresponding to the query code; According to the database type, the grammatical structure, usage functions and operators of the query code are analyzed to determine the standard query statement corresponding to the query code.
[0007] As a preferred solution, the step of determining the corresponding execution logic and the target database according to the query statement, calling a preset query code library corresponding to the target database, and determining the corresponding first logic code according to the execution logic specifically includes: Determine the corresponding execution logic according to the grammatical structure of the query code in the standard query statement, the used functions and operators; Determine the corresponding target database according to the database type of the query code in the standard query statement and the corresponding driver or library; Call a preset query code library corresponding to the target database, and determine, based on the query logic, a logic code in the preset query code library that has a grammatical structure of the query code in the standard query statement corresponding to the query logic and uses the same functions and operators as the first logic code; wherein each target database corresponds to a preset query code library, and a plurality of logic codes are stored in the preset query code library.
[0008] As a preferred solution, the step of inputting the execution logic into an adapter corresponding to the target database and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating the corresponding second logic code according to the execution logic and the structural features, specifically includes: Inputting the execution logic into the adapter of the corresponding target database; wherein each target database is provided with an adapter, and each adapter performs configuration optimization on the database corresponding to it; Determine the type of the target database; wherein the type of the target database includes a relational database and a non-relational database; If the target database is a relational database, determining the structured syntax, table operation, relational operation, transaction control and database function corresponding to the execution logic; generating a second logic code corresponding to the relational database according to the structured syntax, table operation, relational operation, transaction control and database function and based on the execution logic; If the target database is a non-relational database, determine the data model, declarative query, nested and hierarchical data, and horizontal expansion information corresponding to the execution logic; according to the data model, declarative query, nested and hierarchical data, and horizontal expansion information, and based on the execution logic, generate a second logic code corresponding to the non-relational database.
[0009] As a preferred solution, the compatibility test is performed on the first logic code and the second logic code, and after at least one logic code passes the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic, specifically including: Performing a compatibility test on the first logic code and the second logic code; wherein the compatibility test is to perform a compatibility test on the logic code with a corresponding target database; If the first logic code passes the compatibility test and the second logic code fails the compatibility test, the first logic code is used as the target code corresponding to the execution logic; If the second logic code passes the compatibility test and the first logic code fails the compatibility test, the second logic code is used as the target code corresponding to the execution logic; If both the first logic code and the second logic code pass the compatibility test, determining the number of bytes of the first logic code and the second logic code, and using the logic code with the least number of bytes as the target code corresponding to the execution logic; If both the first logic code and the second logic code fail the compatibility test, a prompt indicating a query code error is generated.
[0010] As a preferred solution, the compatibility test of the first logic code and the second logic code specifically includes: According to the target database, determining corresponding test data and expected results; Based on the test data, the first logic code and the second logic code are respectively run, and during the running process, intermediate running data corresponding to the first logic code and the second logic code are continuously collected; Determine whether the first logic code and the second logic code have execution errors according to the intermediate operation data; if the first logic code has execution errors, continue to execute the second logic code; if the second logic code has execution errors, continue to execute the first logic code; If not, after obtaining the test results corresponding to the first logic code and the second logic code respectively, the first logic code and the second logic code are respectively compared and analyzed with their expected results, and the logic code with the same comparison and analysis is used as the logic code that passes the compatibility test.
[0011] As a preferred solution, when the number of the target databases is greater than 1, the preset query code libraries corresponding to the target databases are respectively called, and a corresponding first logic code is determined from each preset query code library according to the execution logic; Inputting the execution logic into the adapter corresponding to each target database, and determining the structural features corresponding to the execution logic according to the type of each target database, thereby generating a second logic code corresponding to each target database according to the execution logic and the structural features; Performing compatibility test on the first logic code and the second logic code in the same target database, and after at least one of the logic codes passes the compatibility test, taking the logic code with the least number of bytes as the target code corresponding to the execution logic; The target codes corresponding to each target database are merged to produce a complete code, and a corresponding query statement is generated based on the complete code.
[0012] Accordingly, the present invention also provides a multi-type database compatible code writing processing system, comprising: a response module, a first logic module, a second logic module, a target module and an execution module; The response module is used to respond to the query code input by the user, identify multiple corresponding database types according to the query code, and analyze the query code according to the database type to determine the query statement corresponding to the query code; The first logic module is used to determine the corresponding execution logic and the target database according to the query statement, and call the preset query code library corresponding to the target database to determine the corresponding first logic code according to the execution logic; The second logic module is used to input the execution logic into an adapter corresponding to the target database, and determine the structural features corresponding to the execution logic according to the type of the target database, so as to generate the corresponding second logic code according to the execution logic and the structural features; The target module is used to perform compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, use the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code; The execution module is used to generate a corresponding query command according to the target code, so that the corresponding query service is executed according to the query command, thereby completing the processing of the query code.
[0013] Correspondingly, the present invention also provides a terminal device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, and when the processor executes the computer program, it implements the multi-type database compatible code writing processing method as described in any one of the above.
[0014] Correspondingly, the present invention also provides a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the multi-type database compatible code writing processing method as described in any one of the above.
[0015] Compared with the prior art, the embodiments of the present invention have the following beneficial effects: The technical solution of the present invention determines the corresponding database type and the corresponding query statement by responding to the query code input by the user, thereby determining the corresponding execution logic and the target database, and determines the first logic code through a preset query code library, and produces the second logic code through the structural features corresponding to the execution logic, so that the logic code that passes the compatibility test selects the logic code with the least number of bytes as the target code, thereby ensuring that less data is occupied during the corresponding database query process and maintaining a high query performance. At the same time, by determining the corresponding first logic code and the second logic code, different types of databases can be queried from a dual-dimensional perspective, avoiding the problem of ignoring the compatibility of another type of database while focusing on the availability of a single database, thereby ensuring corresponding support and application of non-relational databases, thereby improving the availability of the code, and the reduced code logic transformation and operation also improves the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 : A flowchart of a method for writing a code compatible with multiple types of databases provided in an embodiment of the present invention; Figure 2 : A structural diagram of a code writing and processing device compatible with multiple types of databases provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0017] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0018] Embodiment 1 Please refer to Figure 1 , a multi-type database compatible code writing processing method provided by an embodiment of the present invention includes the following steps S101-S105: Step S101: In response to a query code input by a user, multiple corresponding database types are identified according to the query code, and the query code is analyzed according to the database type to determine a query statement corresponding to the query code.
[0019] As a preferred solution of this embodiment, in response to the query code input by the user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine the query statement corresponding to the query code specifically includes: Responding to a query code input by a user; analyzing the query code to obtain a grammatical structure, usage functions and operators corresponding to the query code; determining the data type and data structure of the query code based on the grammatical structure, usage functions and operators; identifying the database type that needs to be queried by the query code based on the grammatical structure, usage functions and operators of the query code, as well as the data type and data structure of the query code; analyzing the grammatical structure, usage functions and operators of the query code based on the database type, so as to determine the standard query statement corresponding to the query code.
[0020] In this embodiment, the query code entered by the user can be the basic query statement code of various types of databases. Through the query codes of various types of databases entered by the user, the embodiment of the present invention will determine the query statement corresponding to the query code, and can generate the corresponding execution logic and locate the target database, thereby realizing the data query of the corresponding target database.
[0021] Exemplarily, the query statement may be a SQL query statement, that is, SQL is the most commonly used query language in relational databases, or may be a corresponding query statement in non-relational databases such as MongoDB.
[0022] In this embodiment, by obtaining the query code input by the user, the corresponding query code can be analyzed to obtain the grammatical structure, the use of functions and operators corresponding to the corresponding query code. Then, in combination with the corresponding grammatical structure, the use of functions and operators, and the data type and data structure of the query code, the database type that needs to be queried by the corresponding query code can be identified. For example, by determining whether the corresponding query code uses SQL SELECT, FROM, WHERE and other structures, or MongoDB db.collection.find ({}) such a JSON-like structure, or by using functions and operators, that is, using typical SQL functions and operators such as LIKE, JOIN, ORDER BY, or using MongoDB $gte, $in and other operators, as well as data types and data structures, for example, whether the data is organized in the form of a table with clear columns and rows, or stored in the form of documents, key-value pairs or graphs. Finally, the syntax structure, usage functions and operators of the query code can be analyzed according to the database type, so as to quickly determine the standard query statement corresponding to the query code, that is, analyze the syntax structure, operators and data structure of the query code to determine which database type it is suitable for, and then further analyze the specific syntax and function usage of the query code according to the identified database type to ensure that it meets the standards of the database, thereby considering the existence of the ORM framework, converting the ORM code into the corresponding database query statement, and then analyzing it.
[0023] Step S102: determining the corresponding execution logic and the target database according to the query statement, calling a preset query code library corresponding to the target database, and determining the corresponding first logic code according to the execution logic.
[0024] As a preferred solution of this embodiment, the step of determining the corresponding execution logic and the target database according to the query statement, calling a preset query code library corresponding to the target database, and determining the corresponding first logic code according to the execution logic specifically includes: The corresponding execution logic is determined according to the grammatical structure of the query code in the standard query statement, and the functions and operators used; the corresponding target database is determined according to the database type of the query code in the standard query statement, and the corresponding driver or library; the preset query code library corresponding to the target database is called, and according to the query logic, the logic code in the preset query code library that has the same grammatical structure, functions and operators as the query code in the standard query statement corresponding to the query logic is determined as the first logic code; wherein each target database corresponds to a preset query code library, and the preset query code library stores a plurality of logic codes.
[0025] In this embodiment, by analyzing the grammatical structure of the query statement, key parts such as SELECT, FROM, WHERE, ORDERBY, JOIN, GROUP BY, etc. are identified, and the functions and operators used, such as aggregate functions, comparison operators, logical operators, etc., are identified. Then, based on these components, the execution logic of the database is inferred, including table access, data screening, sorting, connection, grouping and aggregation calculation, so as to consider the situation of subqueries, process the inner subqueries first, and then process the outer queries, and finally use the database execution plan tool, such as MySQL's EXPLAIN, etc., to obtain specific execution logic information.
[0026] In this embodiment, the database type of the query code in the standard query statement and the corresponding driver or library are used to analyze the grammatical structure of the query statement to determine whether it is an SQL or non-SQL query language, check the driver or library used, and determine the corresponding database type according to the name of the driver or library, so as to consider the ORM framework or configuration file, confirm whether there is a specific database configuration, and finally identify the dialect or function of the specific database in the query statement to further confirm the target database.
[0027] In this embodiment, by calling the preset query code library corresponding to the target database, the code in the preset query code library can be code data corresponding to the mapping relationship between the target database and the unified abstract language, that is, by determining the execution logic, the unified abstract language corresponding to the execution logic can be determined, and the corresponding first logic code can be determined in the preset query code library through the unified abstract language. Specifically, the first logic code can be determined by determining the syntax structure of the query code in the standard query statement corresponding to the query logic in the preset query code library, and using the same logic code with functions and operators.
[0028] Step S103: input the execution logic into the adapter corresponding to the target database, and determine the structural features corresponding to the execution logic according to the type of the target database, so as to generate the corresponding second logic code according to the execution logic and the structural features.
[0029] As a preferred solution of this embodiment, the step of inputting the execution logic into an adapter corresponding to the target database and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating the corresponding second logic code according to the execution logic and the structural features, specifically includes: The execution logic is input into the adapter of the corresponding target database; wherein each target database is provided with an adapter, and each adapter performs configuration optimization on the corresponding database; the type of the target database is determined; wherein the types of the target database include relational databases and non-relational databases; if the target database is a relational database, the structured syntax, table operations, relational operations, transaction controls and database functions corresponding to the execution logic are determined; according to the structured syntax, table operations, relational operations, transaction controls and database functions, and based on the execution logic, a second logic code corresponding to the relational database is generated; if the target database is a non-relational database, the data model, declarative query, nested and hierarchical data and horizontal expansion information corresponding to the execution logic are determined; according to the data model, declarative query, nested and hierarchical data and horizontal expansion information, and based on the execution logic, a second logic code corresponding to the non-relational database is generated.
[0030] In this embodiment, the adapter can implement this interface for each database type, converting the general method into a device for the native operation of a specific database, and this interface can be applicable to all types of databases, so that developers can write code with the help of this interface, and the specific implementation can be completed by the adapter of each database.
[0031] In this embodiment, by determining the type of the target database, that is, determining whether the target database is a relational database or a non-relational database, it is possible to determine the structured syntax, table operations, relational operations, transaction control and database functions corresponding to the execution logic, or determine the data model, declarative query, nested and hierarchical data and horizontal expansion information corresponding to the execution logic. It is understandable that the SQL statement of the relational database is more structured and requires the specification of table names and fields, while the query statement of the non-relational database is more like an object query, which directly uses the key-value pairs of the document to filter data. Therefore, through this step, the corresponding logic code of different types of databases can be determined quickly and accurately, avoiding the error caused by the corresponding language conversion caused by the direct use of a unified abstract query language in step S102. At the same time, the logic code is determined by the two dimensions of step S102 and step S103, which can also guarantee the feasibility and reliability of the logic code to a high degree.
[0032] It should be noted that for relational databases, the structured syntax, table operations, relational operations, transaction control, and database functions corresponding to the execution logic are the salient features of SQL query statements. Among them, SQL statements have strict grammatical structures, such as SELECT, FROM, WHERE, GROUP BY, etc., which must be written in a certain order and format. SQL operates on tables, and data is organized in the form of rows and columns. In addition, when querying, specific table names and fields need to be specified. SQL query statements support JOIN operations, which can associate and query data across multiple tables. At the same time, it also supports transactions, which can control the submission and rollback of data to ensure data consistency. It also includes rich functions. SQL provides a large number of built-in functions, including string functions, numerical functions, date functions, etc., for data processing and conversion.
[0033] It should be noted that for non-relational databases, the data model corresponding to the execution logic, declarative queries, nested and hierarchical data, and horizontal expansion information are the salient features of the corresponding query statements. Among them, regarding flexible data models, non-relational databases usually use data models such as documents, key-value pairs, or graphs, so query statements are more flexible and do not require strict definition of table structures. For declarative queries, many non-relational databases support declarative query methods. For nested and hierarchical data, nested and hierarchical data structures are supported, and complex data relationships can be processed in one query. For horizontal expansion, non-relational databases are usually designed for distributed storage of large-scale data, and query statements also support this scalability.
[0034] Step S104: Perform compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, use the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code.
[0035] As a preferred solution of this embodiment, the compatibility test is performed on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic, which specifically includes: Perform a compatibility test on the first logic code and the second logic code; wherein the compatibility test is to perform a compatibility test on the logic code with the corresponding target database; if the first logic code passes the compatibility test and the second logic code fails the compatibility test, the first logic code is used as the target code corresponding to the execution logic; if the second logic code passes the compatibility test and the first logic code fails the compatibility test, the second logic code is used as the target code corresponding to the execution logic; if both the first logic code and the second logic code pass the compatibility test, the number of bytes of the first logic code and the second logic code is determined, and the logic code with the least number of bytes is used as the target code corresponding to the execution logic; if both the first logic code and the second logic code fail the compatibility test, a prompt of a query code error is generated.
[0036] In this embodiment, compatibility tests are performed on the first logic code and the second logic code respectively, so that the logic code with the best compatibility while maintaining the query performance of the code can be extracted. After at least one logic code can pass the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the executed logic, which can ensure the query performance of the code to the greatest extent and avoid the reduction of query performance due to a large number of cumbersome bytes.
[0037] As a preferred solution of this embodiment, the compatibility test of the first logic code and the second logic code specifically includes: According to the target database, the corresponding test data and expected results are determined; based on the test data, the first logic code and the second logic code are respectively run, and the intermediate running data corresponding to the first logic code and the second logic code are continuously collected during the running process; according to the intermediate running data, whether the first logic code and the second logic code have execution errors; if the first logic code has execution errors, the second logic code continues to run; if the second logic code has execution errors, the first logic code continues to run; if not, after obtaining the test results corresponding to the first logic code and the second logic code respectively, the first logic code and the second logic code are respectively compared and analyzed with their expected results, and the logic code with the same comparison and analysis is used as the logic code that has passed the compatibility test.
[0038] In this embodiment, for databases such as MySQL, PostgreSQL, SQLite, MongoDB, etc., it is necessary to first determine a set of test data and expected results, and then different drivers and connection methods are required for different databases, and the connection string is configured. After listing the database types that need to be supported and writing corresponding test cases for each database, considering their characteristics and compatibility points, and configuring continuous integration tools such as Jenkins or Travis CI to automatically run these tests, using container technologies such as Docker, build test environments for different databases, and then handle transaction and data initialization issues, ensure the consistency and correctness of test data, and finally gradually expand the supported database types and iterate step by step.
[0039] Step S105: Generate a corresponding query command according to the target code, so that a corresponding query service is executed according to the query command, thereby completing the processing of the query code.
[0040] In this embodiment, the corresponding target code can be used to generate the corresponding query command, which can then be directly executed to query different types of database information, thereby completing the writing of compatible codes for multiple types of databases and improving the code capabilities and availability of database queries.
[0041] As a preferred solution of this embodiment, when the number of the target databases is greater than 1, the preset query code libraries corresponding to the target databases are respectively called, and a corresponding first logic code is determined from each preset query code library according to the execution logic; The execution logic is input into the adapter corresponding to each target database, and the structural features corresponding to the execution logic are determined according to the type of each target database, so as to generate a second logic code corresponding to each target database according to the execution logic and the structural features; the first logic code and the second logic code in the same target database are tested for compatibility, and after at least one logic code can pass the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic; the target codes corresponding to each target database are merged to produce a complete code, and the corresponding query statement is generated according to the complete code.
[0042] In this embodiment, when the number of target databases is greater than 1, the above-mentioned dual-dimensional logic code analysis is combined to quickly analyze the logic codes of different dimensions. That is, when the number of target databases is too large, the compatibility test of the corresponding first logic code and the second logic code can be directly performed, and then the simplified code statements can be merged to store and execute the corresponding code with the minimum number of bytes, thereby improving code performance.
[0043] Implementing the above embodiments has the following effects: The technical solution of the present invention determines the corresponding database type and the corresponding query statement by responding to the query code input by the user, thereby determining the corresponding execution logic and the target database, and determines the first logic code through a preset query code library, and produces the second logic code through the structural features corresponding to the execution logic, so that the logic code that passes the compatibility test selects the logic code with the least number of bytes as the target code, thereby ensuring that less data is occupied during the corresponding database query process and maintaining a high query performance. At the same time, by determining the corresponding first logic code and the second logic code, different types of databases can be queried from a dual-dimensional perspective, avoiding the problem of ignoring the compatibility of another type of database while focusing on the availability of a single database, thereby ensuring corresponding support and application of non-relational databases, thereby improving the availability of the code, and the reduced code logic transformation and operation also improves the user experience.
[0044] Embodiment 2 Please refer to the figure, which is a code writing processing system compatible with multiple types of databases, including: a response module 201, a first logic module 202, a second logic module 203, a target module 204 and an execution module 205; The response module 201 is used to respond to the query code input by the user, identify multiple corresponding database types according to the query code, and analyze the query code according to the database type to determine the query statement corresponding to the query code; The first logic module 202 is used to determine the corresponding execution logic and the target database according to the query statement, and call the preset query code library corresponding to the target database to determine the corresponding first logic code according to the execution logic; The second logic module 203 is used to input the execution logic into the adapter corresponding to the target database, and determine the structural features corresponding to the execution logic according to the type of the target database, so as to generate the corresponding second logic code according to the execution logic and the structural features; The target module 204 is used to perform compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, use the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code; The execution module 205 is used to generate a corresponding query command according to the target code, so that the corresponding query service is executed according to the query command, thereby completing the processing of the query code.
[0045] As a preferred solution, in response to the query code input by the user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine the query statement corresponding to the query code specifically includes: Responding to a query code input by a user; Analyze the query code to obtain the grammatical structure, usage functions and operators corresponding to the query code; Determine the data type and data structure of the query code according to the grammatical structure, the use function and the operator; According to the syntax structure of the query code, the used functions and operators, and the data type and data structure of the query code, identify the type of database that needs to be queried corresponding to the query code; According to the database type, the grammatical structure, usage functions and operators of the query code are analyzed to determine the standard query statement corresponding to the query code.
[0046] As a preferred solution, the step of determining the corresponding execution logic and the target database according to the query statement, calling a preset query code library corresponding to the target database, and determining the corresponding first logic code according to the execution logic specifically includes: Determine the corresponding execution logic according to the grammatical structure of the query code in the standard query statement, the used functions and operators; Determine the corresponding target database according to the database type of the query code in the standard query statement and the corresponding driver or library; Call a preset query code library corresponding to the target database, and determine, based on the query logic, a logic code in the preset query code library that has a grammatical structure of the query code in the standard query statement corresponding to the query logic and uses the same functions and operators as the first logic code; wherein each target database corresponds to a preset query code library, and a plurality of logic codes are stored in the preset query code library.
[0047] As a preferred solution, the step of inputting the execution logic into an adapter corresponding to the target database and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating the corresponding second logic code according to the execution logic and the structural features, specifically includes: Inputting the execution logic into the adapter of the corresponding target database; wherein each target database is provided with an adapter, and each adapter performs configuration optimization on the database corresponding to it; Determine the type of the target database; wherein the type of the target database includes a relational database and a non-relational database; If the target database is a relational database, determining the structured syntax, table operation, relational operation, transaction control and database function corresponding to the execution logic; generating a second logic code corresponding to the relational database according to the structured syntax, table operation, relational operation, transaction control and database function and based on the execution logic; If the target database is a non-relational database, determine the data model, declarative query, nested and hierarchical data, and horizontal expansion information corresponding to the execution logic; according to the data model, declarative query, nested and hierarchical data, and horizontal expansion information, and based on the execution logic, generate a second logic code corresponding to the non-relational database.
[0048] As a preferred solution, the compatibility test is performed on the first logic code and the second logic code, and after at least one logic code passes the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic, specifically including: Performing a compatibility test on the first logic code and the second logic code; wherein the compatibility test is to perform a compatibility test on the logic code with a corresponding target database; If the first logic code passes the compatibility test and the second logic code fails the compatibility test, the first logic code is used as the target code corresponding to the execution logic; If the second logic code passes the compatibility test and the first logic code fails the compatibility test, the second logic code is used as the target code corresponding to the execution logic; If both the first logic code and the second logic code pass the compatibility test, determining the number of bytes of the first logic code and the second logic code, and using the logic code with the least number of bytes as the target code corresponding to the execution logic; If both the first logic code and the second logic code fail the compatibility test, a prompt indicating a query code error is generated.
[0049] As a preferred solution, the compatibility test of the first logic code and the second logic code specifically includes: According to the target database, determining corresponding test data and expected results; Based on the test data, the first logic code and the second logic code are respectively run, and during the running process, intermediate running data corresponding to the first logic code and the second logic code are continuously collected; Determine whether the first logic code and the second logic code have execution errors according to the intermediate operation data; if the first logic code has execution errors, continue to execute the second logic code; if the second logic code has execution errors, continue to execute the first logic code; If not, after obtaining the test results corresponding to the first logic code and the second logic code respectively, the first logic code and the second logic code are respectively compared and analyzed with their expected results, and the logic code with the same comparison and analysis is used as the logic code that passes the compatibility test.
[0050] As a preferred solution, when the number of the target databases is greater than 1, the preset query code libraries corresponding to the target databases are respectively called, and a corresponding first logic code is determined from each preset query code library according to the execution logic; Inputting the execution logic into the adapter corresponding to each target database, and determining the structural features corresponding to the execution logic according to the type of each target database, thereby generating a second logic code corresponding to each target database according to the execution logic and the structural features; Performing compatibility test on the first logic code and the second logic code in the same target database, and after at least one of the logic codes passes the compatibility test, taking the logic code with the least number of bytes as the target code corresponding to the execution logic; The target codes corresponding to each target database are merged to produce a complete code, and a corresponding query statement is generated based on the complete code.
[0051] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0052] Implementing the above embodiments has the following effects: The technical solution of the present invention determines the corresponding database type and the corresponding query statement by responding to the query code input by the user, thereby determining the corresponding execution logic and the target database, and determines the first logic code through a preset query code library, and produces the second logic code through the structural features corresponding to the execution logic, so that the logic code that passes the compatibility test selects the logic code with the least number of bytes as the target code, thereby ensuring that less data is occupied during the corresponding database query process and maintaining a high query performance. At the same time, by determining the corresponding first logic code and the second logic code, different types of databases can be queried from a dual-dimensional perspective, avoiding the problem of ignoring the compatibility of another type of database while focusing on the availability of a single database, thereby ensuring corresponding support and application of non-relational databases, thereby improving the availability of the code, and the reduced code logic transformation and operation also improves the user experience.
[0053] Embodiment 3 Correspondingly, the present invention also provides a terminal device, comprising: a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, the multi-type database compatible code writing processing method as described in any one of the above embodiments is implemented.
[0054] The terminal device of this embodiment includes: a processor, a memory, and a computer program and a computer instruction stored in the memory and executable on the processor. When the processor executes the computer program, each step in the above-mentioned embodiment 1 is implemented, for example: Figure 1 Alternatively, the processor implements the functions of each module / unit in the above-mentioned device embodiment, such as the target module 204, when executing the computer program.
[0055] Exemplarily, the computer program may be divided into one or more modules / units, and the one or more modules / units are stored in the memory and executed by the processor to complete the present invention. The one or more modules / units may be a series of computer program instruction segments that can perform specific functions, and the instruction segments are used to describe the execution process of the computer program in the terminal device. For example, the target module 204 is used to perform a compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic.
[0056] The terminal device may be a computing device such as a desktop computer, a notebook, a PDA, and a cloud server. The terminal device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the schematic diagram is merely an example of a terminal device and does not constitute a limitation on the terminal device. The terminal device may include more or fewer components than shown in the diagram, or may combine certain components, or different components. For example, the terminal device may also include an input / output device, a network access device, a bus, etc.
[0057] The processor may be a central processing unit (CPU), other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc. The processor is the control center of the terminal device, and uses various interfaces and lines to connect various parts of the entire terminal device.
[0058] The memory can be used to store the computer program and / or module, and the processor realizes various functions of the terminal device by running or executing the computer program and / or module stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function, etc.; the data storage area can store data created according to the use of the mobile terminal, etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (SecureDigital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0059] Wherein, if the module / unit integrated in the terminal device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0060] Embodiment 4 Correspondingly, the present invention also provides a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the multi-type database compatible code writing processing method described in any one of the above embodiments.
[0061] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for writing and processing codes compatible with multiple types of databases, characterized in that: include: In response to a query code input by a user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine a query statement corresponding to the query code; Determine the corresponding execution logic and the target database according to the query statement, call a preset query code library corresponding to the target database, and determine the corresponding first logic code according to the execution logic; Inputting the execution logic into an adapter corresponding to the target database, and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating a corresponding second logic code according to the execution logic and the structural features; Performing a compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, using the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code; A corresponding query command is generated according to the target code, so that a corresponding query service is executed according to the query command, thereby completing the processing of the query code.
2. A method for writing and processing codes compatible with multiple types of databases as claimed in claim 1, characterized in that: The step of responding to a query code input by a user, identifying multiple corresponding database types according to the query code, and analyzing the query code according to the database type to determine a query statement corresponding to the query code specifically includes: Responding to a query code input by a user; Analyze the query code to obtain the grammatical structure, usage functions and operators corresponding to the query code; Determine the data type and data structure of the query code according to the grammatical structure, the use function and the operator; According to the syntax structure of the query code, the used functions and operators, and the data type and data structure of the query code, identify the type of database that needs to be queried corresponding to the query code; According to the database type, the grammatical structure, usage functions and operators of the query code are analyzed to determine the standard query statement corresponding to the query code.
3. A method for writing and processing codes compatible with multiple types of databases as claimed in claim 2, characterized in that: The determining the corresponding execution logic and the target database according to the query statement, calling a preset query code library corresponding to the target database, and determining the corresponding first logic code according to the execution logic specifically includes: Determine the corresponding execution logic according to the grammatical structure of the query code in the standard query statement, the used functions and operators; Determine the corresponding target database according to the database type of the query code in the standard query statement and the corresponding driver or library; Call a preset query code library corresponding to the target database, and determine, based on the query logic, a logic code in the preset query code library that has a grammatical structure of the query code in the standard query statement corresponding to the query logic and uses the same functions and operators as the first logic code; wherein each target database corresponds to a preset query code library, and a plurality of logic codes are stored in the preset query code library.
4. A method for writing and processing codes compatible with multiple types of databases as claimed in claim 3, characterized in that: The step of inputting the execution logic into an adapter corresponding to the target database and determining the structural features corresponding to the execution logic according to the type of the target database, thereby generating the corresponding second logic code according to the execution logic and the structural features, specifically includes: Inputting the execution logic into the adapter of the corresponding target database; wherein each target database is provided with an adapter, and each adapter performs configuration optimization on the database corresponding to it; Determine the type of the target database; wherein the type of the target database includes a relational database and a non-relational database; If the target database is a relational database, determining the structured syntax, table operation, relational operation, transaction control and database function corresponding to the execution logic; generating a second logic code corresponding to the relational database according to the structured syntax, table operation, relational operation, transaction control and database function and based on the execution logic; If the target database is a non-relational database, determine the data model, declarative query, nested and hierarchical data, and horizontal expansion information corresponding to the execution logic; according to the data model, declarative query, nested and hierarchical data, and horizontal expansion information, and based on the execution logic, generate a second logic code corresponding to the non-relational database.
5. A method for writing and processing codes compatible with multiple types of databases as claimed in claim 4, characterized in that: The compatibility test is performed on the first logic code and the second logic code, and after at least one logic code passes the compatibility test, the logic code with the least number of bytes is used as the target code corresponding to the execution logic, specifically including: Performing a compatibility test on the first logic code and the second logic code; wherein the compatibility test is to perform a compatibility test on the logic code with a corresponding target database; If the first logic code passes the compatibility test and the second logic code fails the compatibility test, the first logic code is used as the target code corresponding to the execution logic; If the second logic code passes the compatibility test and the first logic code fails the compatibility test, the second logic code is used as the target code corresponding to the execution logic; If both the first logic code and the second logic code pass the compatibility test, determining the number of bytes of the first logic code and the second logic code, and using the logic code with the least number of bytes as the target code corresponding to the execution logic; If both the first logic code and the second logic code fail the compatibility test, a prompt indicating a query code error is generated.
6. A method for writing and processing codes compatible with multiple types of databases as claimed in claim 5, characterized in that: The performing compatibility test on the first logic code and the second logic code specifically includes: According to the target database, determining corresponding test data and expected results; Based on the test data, the first logic code and the second logic code are respectively run, and during the running process, intermediate running data corresponding to the first logic code and the second logic code are continuously collected; Determine whether the first logic code and the second logic code have execution errors according to the intermediate operation data; if the first logic code has execution errors, continue to execute the second logic code; if the second logic code has execution errors, continue to execute the first logic code; If not, after obtaining the test results corresponding to the first logic code and the second logic code respectively, the first logic code and the second logic code are respectively compared and analyzed with their expected results, and the logic code with the same comparison and analysis is used as the logic code that passes the compatibility test.
7. A method for writing and processing codes compatible with multiple types of databases as described in any one of claims 1 to 6, characterized in that: When the number of the target databases is greater than 1, the preset query code libraries corresponding to the target databases are called respectively, and a corresponding first logic code is determined from each preset query code library according to the execution logic; Inputting the execution logic into the adapter corresponding to each target database, and determining the structural features corresponding to the execution logic according to the type of each target database, thereby generating a second logic code corresponding to each target database according to the execution logic and the structural features; Performing compatibility test on the first logic code and the second logic code in the same target database, and after at least one of the logic codes passes the compatibility test, taking the logic code with the least number of bytes as the target code corresponding to the execution logic; The target codes corresponding to each target database are merged to produce a complete code, and a corresponding query statement is generated based on the complete code.
8. A code writing and processing system compatible with multiple types of databases, characterized in that: include: A response module, a first logic module, a second logic module, a target module and an execution module; The response module is used to respond to the query code input by the user, identify multiple corresponding database types according to the query code, and analyze the query code according to the database type to determine the query statement corresponding to the query code; The first logic module is used to determine the corresponding execution logic and the target database according to the query statement, and call the preset query code library corresponding to the target database to determine the corresponding first logic code according to the execution logic; The second logic module is used to input the execution logic into the adapter corresponding to the target database, and determine the structural features corresponding to the execution logic according to the type of the target database, so as to generate the corresponding second logic code according to the execution logic and the structural features; The target module is used to perform compatibility test on the first logic code and the second logic code, and after at least one logic code can pass the compatibility test, use the logic code with the least number of bytes as the target code corresponding to the execution logic; wherein the logic code includes the first logic code and the second logic code; The execution module is used to generate a corresponding query command according to the target code, so that the corresponding query service is executed according to the query command, thereby completing the processing of the query code.
9. A terminal device, characterized in that: The invention comprises a processor, a memory and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, the method for writing and processing codes compatible with multiple types of databases as claimed in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the multi-type database compatible code writing processing method as described in any one of claims 1 to 7.