Database adaptation method and system based on object relation mapping
Through the database adaptation method based on object-relational mapping, the database access framework and dialect package module provide unified interfaces and callback functions, solving the problems of multi-database compatibility and domestic database adaptation, achieving efficient and easy-to-maintain database operations, and improving the Development efficiency and system compatibility.
Patent Information
- Application Number
- CN202411794014.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-01-10
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing technology has limitations in supporting multiple databases and achieving compatibility, especially when it is necessary to support multiple domestic databases, additional adaptation measures are usually required, resulting in code redundancy, high maintenance difficulty, low development efficiency and database switching. complex.
Adopt database adaptation method based on object-relational mapping, build database access framework and dialect package module, provide unified interface and callback function mechanism, generate adapted SQL statements, reduce the need for developers to write specific database logic, and pass links Tracking and logging interface modules uniformly manage database operations.
Significantly reduces code redundancy, improves code maintainability and system scalability, reduces development and adaptation difficulty, improves development efficiency and system portability, and realizes high compatibility and unified database operation management. .
Smart Images

Figure CN119293073A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of database adaptation, and in particular to a database adaptation method, system, computer device and computer program product based on object-relational mapping. Background Art
[0002] At present, when implementing database compatibility, application systems usually need to write database operation logic for each database separately to adapt to its unique syntax and operation rules. Although this method can meet the access requirements of different databases, it often leads to the following problems:
[0003] Code redundancy and maintenance difficulty: Different database operations require special logic processing at the application layer and independent database access logic. This not only increases the amount of code, but also makes system maintenance and upgrades difficult.
[0004] Low development efficiency: Since each database requires specialized logical support, developers need to handle different database operation modes separately during system development and subsequent maintenance, resulting in a large amount of manpower and time spent on database adaptation.
[0005] Complexity of database switching: In actual business needs, enterprises sometimes need to replace or use multiple databases at the same time. However, due to the different syntax and operation methods of different databases, it is often difficult to achieve flexible switching and compatibility of databases, and the portability of application systems is greatly limited.
[0006] In order to solve the above problems, the industry usually uses object-relational mapping (ORM) technology to simplify database operations and to some extent abstract database operations. However, the existing ORM framework still has limitations in supporting multiple databases and achieving compatibility. In particular, when it is necessary to support multiple domestic databases, additional adaptation measures are usually required. This brings challenges to achieving cross-platform database compatibility, and the syntax differences of different databases still cannot be effectively solved through a unified interface. Summary of the invention
[0007] The present application provides a database adaptation method, system, computer device and storage medium based on object-relational mapping, aiming to solve the problem that the existing technology still has limitations in supporting multiple databases and achieving compatibility, especially when it is necessary to support multiple domestic databases, additional adaptation measures are usually required.
[0008] In a first aspect, a database adaptation method based on object-relational mapping is provided, the method comprising:
[0009] Build a database access framework, which is used to provide a unified interface for upper-layer applications and implement access to multiple databases through object-relational mapping;
[0010] registering a plurality of dialect packages in the database access framework, wherein the dialect packages store syntax and data type information related to the target database;
[0011] When the upper layer application calls a database operation, it calls the callback function of the dialect package and generates an adapted SQL statement according to the target database; wherein the SQL statement is constructed by calling the interface of the database access framework, and the generated SQL statement is adapted by the dialect package module;
[0012] The call chain and log information of database operations are recorded through the link tracking and log interface modules.
[0013] In the above solution, optionally, when a new database dialect package is added, the method further includes:
[0014] Call the Initialize interface to initialize the database dialect package, configure and register callback functions related to database operations;
[0015] Configure the database migration strategy through the Migrator interface;
[0016] Replace the symbolic syntax of different databases through the QuoteTo interface;
[0017] The data type of the programming language is mapped to the data type of the database through the DataTypeOf interface.
[0018] In the above solution, optionally, when the upper layer application calls a database operation, calling the callback function of the dialect package and generating an adapted SQL statement according to the target database includes:
[0019] The SQL clause generation logic is executed in different database environments through the custom ClauseBuilder module to adapt to the specific syntax differences between databases.
[0020] In a second aspect, a database adaptation system based on object-relational mapping is provided, the system comprising:
[0021] Database access framework, used to provide a unified database access interface for upper-layer applications. The framework is built based on object-relational mapping technology to adapt to a variety of domestic and overseas databases;
[0022] A dialect package module, used to store and manage specific dialect packages for different databases, wherein the dialect package contains configuration information related to database syntax and data types;
[0023] The callback function module is used to call the corresponding dialect package according to the operation request of the upper-layer application and map the database operation of the application layer into the SQL statement of the target database;
[0024] The link tracking and log interface module is used to track the call links of database operations and uniformly manage the operation logs.
[0025] In the above scheme, optionally, the database access framework provides multiple standard external interfaces, including template functions, positioning functions, and query functions, which are used to construct SQL statements at the application layer. The generated SQL statements are adapted to the dialect syntax of the target database through a callback function module.
[0026] In the above solution, optionally, the dialect package module includes the following interfaces:
[0027] Initialize interface: used to initialize the database dialect package and register the callback function;
[0028] Migrator interface: configure database type and related database migration parameters;
[0029] QuoteTo interface: replaces the symbol syntax in the database to adapt to the character distinction rules of different databases;
[0030] DataTypeOf interface: maps the data type of the programming language to the data type of the database.
[0031] In the above solution, optionally, the callback function module extends the clause generation logic of a specific database by customizing the ClauseBuilder module, so as to adjust the execution logic of SQL statements in different database environments.
[0032] In a third aspect, a computer device includes a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0033] Build a database access framework, which is used to provide a unified interface for upper-layer applications and implement access to multiple databases through object-relational mapping;
[0034] registering a plurality of dialect packages in the database access framework, wherein the dialect packages store syntax and data type information related to the target database;
[0035] When the upper layer application calls a database operation, it calls the callback function of the dialect package and generates an adapted SQL statement according to the target database; wherein the SQL statement is constructed by calling the interface of the database access framework, and the generated SQL statement is adapted by the dialect package module;
[0036] The call chain and log information of database operations are recorded through the link tracking and log interface modules.
[0037] In a fourth aspect, a computer program product comprises a computer program / instruction, wherein when the computer program / instruction is executed by a processor, the following steps are implemented:
[0038] Build a database access framework, which is used to provide a unified interface for upper-layer applications and implement access to multiple databases through object-relational mapping;
[0039] registering a plurality of dialect packages in the database access framework, wherein the dialect packages store syntax and data type information related to the target database;
[0040] When the upper layer application calls a database operation, it calls the callback function of the dialect package and generates an adapted SQL statement according to the target database; wherein the SQL statement is constructed by calling the interface of the database access framework, and the generated SQL statement is adapted by the dialect package module;
[0041] The call chain and log information of database operations are recorded through the link tracking and log interface modules.
[0042] Compared with the prior art, this application has at least the following beneficial effects:
[0043] Based on further analysis and research on the problems of the prior art, this application recognizes that the prior art still has limitations in supporting multiple databases and achieving compatibility, especially when it is necessary to support multiple domestic databases, additional adaptation measures are usually required. Through a unified database access framework and a standardized call interface, the upper-level application can access multiple databases through the same logic, avoiding the need to write independent operation logic for different databases, significantly reducing code redundancy, thereby improving the maintainability of the code and the scalability of the system. The dialect package module and callback function mechanism are adopted, so that developers do not need to write specific access codes for each database. They only need to call a unified interface at the application layer to implement operations on different databases. This effectively reduces the difficulty of development and adaptation, reduces the development workload caused by differences in database syntax, and improves development efficiency. The method provides technical support for the flexible switching of application systems between different databases, and improves the portability and adaptability of the system. Through the link tracking and log interface modules, the call chains and log information of various database operations are uniformly managed, which is convenient for monitoring and troubleshooting database operations, and improves the controllability and traceability of system operation.
[0044] By implementing callback functions, a unified external function is made compatible with different database types. The compatibility of various domestic databases in the information creation process has reached a high level, and almost all the common domestic databases in the current market can be included. At the same time, you can also register multiple databases for use at the same time through registration, ignoring the differences in various database environments and syntax, and achieving a unified purpose in use. In this application, the ability of link tracking and log unified interface is also included, which unifies the originally messy database logs and call chains into a set of logic, thereby reducing the cost of adapting to different databases.
[0045] This application significantly reduces the cost of using and understanding different types of databases and increases code compatibility by unifying the capabilities of external functions and callback functions. Unifying external functions allows the application layer to process the database without perception, and the seven callback functions can cover all database operation-related content, ensuring low difficulty in database compatibility and ensuring the coverage of database operations. Given the large differences between domestic databases and overseas databases, the present invention makes up for the differences between databases to a certain extent, and provides developers with a simple, easy-to-understand and highly compatible method for using databases. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 A flowchart of a database adaptation method based on object-relational mapping provided in one embodiment of the present application;
[0047] Figure 2 A first schematic diagram of the architecture of a database adaptation method based on object-relational mapping provided by an embodiment of the present application;
[0048] Figure 3 A second schematic diagram of the architecture of a database adaptation method based on object-relational mapping provided by an embodiment of the present application;
[0049] Figure 4 A schematic diagram of the execution flow of a database adaptation method based on object-relational mapping provided in one embodiment of the present application;
[0050] Figure 5 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0051] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0052] In one embodiment, Figure 1As shown, a database adaptation method based on object-relational mapping is provided, comprising the following steps:
[0053] Build a database access framework, which is used to provide a unified interface for upper-layer applications and implement access to multiple databases through object-relational mapping;
[0054] registering a plurality of dialect packages in the database access framework, wherein the dialect packages store syntax and data type information related to the target database;
[0055] When the upper layer application calls a database operation, it calls the callback function of the dialect package and generates an adapted SQL statement according to the target database; wherein the SQL statement is constructed by calling the interface of the database access framework, and the generated SQL statement is adapted by the dialect package module;
[0056] The call chain and log information of database operations are recorded through the link tracking and log interface modules.
[0057] This embodiment solves the problems of difficulty in multi-database adaptation, code redundancy and complex maintenance in the prior art through the combination of object-relational mapping and dialect package modules, and provides a highly compatible and easy-to-maintain database adaptation method that meets the compatibility requirements in a multi-database environment.
[0058] In this embodiment, a database access framework is constructed, especially a framework based on ORM (object-relational mapping), specifically:
[0059] Design the database model: First define the data table structure, including table name, field name, field type, index and other information.
[0060] Encapsulate database operations: Use the database API provided by the language to perform operations such as add, delete, modify, and query, and encapsulate it into a class or module.
[0061] Implement ORM core code: abstract database operations into object methods, implement ORM core functions, and allow developers to indirectly operate the database by operating objects.
[0062] Support multiple databases: ORM frameworks usually need to support multiple databases, and can allow users to choose which database to use through configuration files and other means.
[0063] Support transactions and connection pooling: The ORM framework should support advanced features such as transactions and connection pooling to improve the efficiency and reliability of database operations.
[0064] Write test cases: Write a series of test cases to test the ORM framework to ensure its correct functionality. Release and maintain: Release the ORM framework and maintain it continuously, fix bugs and add new features.
[0065] Unified interface provision: The database access framework provides a unified interface for upper-level applications, allowing applications to interact with different types of databases through these interfaces without having to worry about the specific implementation details of the underlying database.
[0066] Dialect Package Registration: Register multiple dialect packages in the database access framework. The dialect package stores syntax and data type information related to the target database. In this way, the framework can generate adaptive SQL statements according to different database environments.
[0067] Callback function call: When the upper-layer application calls a database operation, it calls the callback function of the dialect package and generates adapted SQL statements based on the target database. These SQL statements are constructed by calling the interface of the database access framework and adapted by the dialect package module.
[0068] Link tracking and log interface module: The link tracking and log interface module records the call chain and log information of database operations, unifying the originally messy and different database logs and call chains into a set of logic, thereby reducing the cost of adapting to different databases.
[0069] Through the above steps, a database access framework based on ORM is built to achieve unified access and adaptation to multiple databases, reduce the cost of using and understanding different types of databases, and increase code compatibility.
[0070] In this embodiment, when a new database dialect package is added, the method further includes:
[0071] Call the Initialize interface to initialize the database dialect package, configure and register callback functions related to database operations;
[0072] Configure the database migration strategy through the Migrator interface;
[0073] Replace the symbolic syntax of different databases through the QuoteTo interface;
[0074] The data type of the programming language is mapped to the data type of the database through the DataTypeOf interface.
[0075] This embodiment simplifies database operations at the application layer through a unified interface, improves the versatility and maintainability of the code, and significantly improves the portability and adaptability of the system in a multi-database environment.
[0076] In this embodiment, when the upper layer application calls a database operation, the callback function of the dialect package is called, and the adapted SQL statement generated according to the target database includes:
[0077] The SQL clause generation logic is executed in different database environments through the custom ClauseBuilder module to adapt to the specific syntax differences between databases.
[0078] This embodiment reduces the difficulty of adapting different database syntaxes through the string concatenation mechanism of the dialect package, improves the flexibility and scalability of the system, and improves the generation and execution efficiency of SQL statements in a multi-database environment.
[0079] This embodiment implements modular management of dialect packages, automated support for database migration, unified replacement of symbolic syntax, and precise mapping of data types through interfaced dialect package configuration management, thereby significantly enhancing the adaptability and compatibility of the system in a multi-database environment.
[0080] In one embodiment, a method is provided that is compatible with most databases, so that developers only need to use one set of logic to be compatible with various types of databases, thereby improving development efficiency and reducing code redundancy.
[0081] Figure 2 It is a summary diagram of the technical solution of the present invention. Figure 3 It is an architecture diagram of the technical solution of the present invention, which explains the main working mode and supported database types of this embodiment (database link middleware).
[0082] The database connection middleware provides unified external functions to upper-level applications in the form of a framework. By calling the functions of the database connection middleware, the upper-level applications can access different types of databases, including various mainstream databases at home and abroad. The database connection middleware is mainly implemented based on object-relational mapping (ORM), and can cross databases and support the use of multiple databases at the same time.
[0083] Figure 4 This is the execution flow of this embodiment. The database link middleware can register the dialect package to the ORM Callbacks function by requesting the specified dialect package, and all subsequent query, insert, delete, and update operations are executed by the Callbacks function.
[0084] The application layer can use various types of external functions to assemble a unified SQL statement, such as Figure 3 The template function, positioning function, and query function can be spliced into a query SQL statement to realize the query capability.
[0085] The database connection middleware implements different capabilities for external functions through callback functions, such as the ability to insert in the create function and the ability to query in the Query() function. Different database dialect packages implement the corresponding callback functions and register them with the database connection middleware to achieve compatibility with different database syntaxes. In the callback function, the dialect package implements the corresponding database SQL statement through string concatenation, and the external function passes the variables and values of the SQL in the application to the callback function to participate in SQL statement concatenation.
[0086] To add a new database dialect package, you need to import its corresponding go language database package, and also complete the following interfaces:
[0087] Initialize: used to initialize the database dialect package, register the callback function and the configuration items corresponding to the callback function;
[0088] Migrator: used to initialize database migration capabilities. Database type and some database configurations must be provided.
[0089] QuoteTo: used to replace database symbol syntax. For example, MySQL uses backticks to distinguish variables, while PostgreSQL uses double quotes to distinguish variables.
[0090] DataTypeOf: used to translate the data type of the Go language into the data type of the database. For example, in the DAMO database, the Bool type of Go needs to be translated into the BIT type.
[0091] Some databases have large differences, and the above interfaces cannot cover all the differences. You can define the logic of translating ORM clauses into SQL statements by customizing ClauseBuilder. For example, if the DAMO database requires all table names and column names to be enclosed in double quotes, you can implement the logic of adding double quotes to table names and column names in the callback function before executing SQL and after generating SQL, thereby realizing DAMO's adaptation capabilities. In the Gaussian database, the OnConflict logic cannot exist at the same time as the Returning logic. You can redefine the OnConflict logic through ClauseBuilder, thereby separating Returning from its logic and modifying it to other methods of obtaining the last insert id.
[0092] The key point of this embodiment is to achieve unified external functions compatible with different database types by implementing callback functions. The compatibility of various domestic databases in the information creation process has reached a high level, and almost all common domestic databases in the current market can be included. At the same time, you can also register multiple databases for use at the same time through registration, ignoring the differences in various database environments and syntax, and achieving a unified purpose in use.
[0093] In this embodiment, the link tracking and log unified interface capabilities are also included, which unifies the originally messy and different database logs and call chains into a set of logic, thereby reducing the cost of adapting to different databases.
[0094] This embodiment greatly reduces the cost of using and understanding different types of databases and increases the compatibility of the code by unifying the capabilities of external functions and callback functions. Unifying external functions allows the application layer to process the database without perception, and the seven callback functions can cover all database operation-related content, ensuring low difficulty in database compatibility and ensuring the coverage of database operations. Given the large differences between domestic databases and overseas databases, the present invention makes up for the differences between databases to a certain extent, and provides developers with a simple, easy-to-understand and highly compatible method for using databases.
[0095] In one embodiment, a database adaptation system based on object-relational mapping is provided, comprising:
[0096] Database access framework, used to provide a unified database access interface for upper-layer applications. The framework is built based on object-relational mapping technology to adapt to a variety of domestic and overseas databases;
[0097] A dialect package module, used to store and manage specific dialect packages for different databases, wherein the dialect package contains configuration information related to database syntax and data types;
[0098] The callback function module is used to call the corresponding dialect package according to the operation request of the upper-layer application and map the database operation of the application layer into the SQL statement of the target database;
[0099] The link tracking and log interface module is used to track the call links of database operations and uniformly manage the operation logs.
[0100] In this embodiment, the database access framework provides multiple standard external interfaces, including but not limited to template functions, positioning functions, and query functions, which are used to build SQL statements at the application layer. The generated SQL statements are adapted to the dialect syntax of the target database through a callback function module.
[0101] In this embodiment, the dialect package module includes the following interfaces:
[0102] Initialize interface: used to initialize the database dialect package and register the callback function;
[0103] Migrator interface: configure database type and related database migration parameters;
[0104] QuoteTo interface: replaces the symbol syntax in the database to adapt to the character distinction rules of different databases;
[0105] DataTypeOf interface: maps the data type of the programming language to the data type of the database.
[0106] In this embodiment, the callback function module extends the clause generation logic of a specific database by customizing the ClauseBuilder module, so as to adjust the execution logic of SQL statements in different database environments.
[0107] The specific implementation content of each module can be found in the above definition of the database adaptation method based on object-relational mapping, which will not be repeated here.
[0108] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 5 As shown. The computer device includes a processor, a memory, a communication interface, a display screen and an input system connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities, and the communication interface is used to communicate with an external terminal in a wired or wireless manner. The wireless method can be achieved through WIFI, an operator network, NFC (near field communication) or other technologies. The computer device implements the above-mentioned database adaptation method based on object-relational mapping by loading and running a computer program. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input system of the computer device can be a touch layer covered on the display screen, or a button, trackball or touchpad set on the computer device shell, or an external keyboard, touchpad or mouse.
[0109] Those skilled in the art will understand that Figure 5 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0110] In one embodiment, a computer program product is also provided, including a computer program / instruction, which, when executed by a processor, involves all or part of the process in the above-mentioned embodiment method.
[0111] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
Claims
1. A database adaptation method based on object-relational mapping, characterized in that: The method comprises: Build a database access framework, which is used to provide a unified interface for upper-layer applications and implement access to multiple databases through object-relational mapping; registering a plurality of dialect packages in the database access framework, wherein the dialect packages store syntax and data type information related to the target database; When the upper layer application calls a database operation, it calls the callback function of the dialect package and generates an adapted SQL statement according to the target database; wherein the SQL statement is constructed by calling the interface of the database access framework, and the generated SQL statement is adapted by the dialect package module; The call chain and log information of database operations are recorded through the link tracking and log interface modules.
2. The database adaptation method based on object relational mapping according to claim 1 is characterized in that: When a new database dialect package is added, the method further includes: Call the Initialize interface to initialize the database dialect package, configure and register callback functions related to database operations; Configure the database migration strategy through the Migrator interface; Replace the symbolic syntax of different databases through the QuoteTo interface; The data type of the programming language is mapped to the data type of the database through the DataTypeOf interface.
3. The database adaptation method based on object relational mapping according to claim 1 is characterized in that: When the upper layer application calls a database operation, the callback function of the dialect package is called, and an adapted SQL statement is generated according to the target database, including: The SQL clause generation logic is executed in different database environments through the custom ClauseBuilder module to adapt to the specific syntax differences between databases.
4. A database adaptation system based on object-relational mapping, characterized in that: The system comprises: Database access framework, used to provide a unified database access interface for upper-layer applications. The framework is built based on object-relational mapping technology to adapt to a variety of domestic and overseas databases; A dialect package module, used to store and manage specific dialect packages for different databases, wherein the dialect package contains configuration information related to database syntax and data types; The callback function module is used to call the corresponding dialect package according to the operation request of the upper-layer application and map the database operation of the application layer into the SQL statement of the target database; The link tracking and log interface module is used to track the call links of database operations and uniformly manage the operation logs.
5. The database adaptation system based on object relational mapping according to claim 4 is characterized in that: The database access framework provides multiple standard external interfaces, including template functions, positioning functions, and query functions, which are used to construct SQL statements at the application layer. The generated SQL statements are adapted to the dialect syntax of the target database through a callback function module.
6. The database adaptation system based on object relational mapping according to claim 4 is characterized in that: The dialect package module includes the following interfaces: Initialize interface: used to initialize the database dialect package and register the callback function; Migrator interface: configure database type and related database migration parameters; QuoteTo interface: replaces the symbol syntax in the database to adapt to the character distinction rules of different databases; DataTypeOf interface: maps the data type of the programming language to the data type of the database.
7. The database adaptation system based on object relational mapping according to claim 4 is characterized in that: The callback function module extends the clause generation logic of a specific database by customizing the ClauseBuilder module, and is used to adjust the execution logic of SQL statements in different database environments.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 3 are implemented.
9. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the method according to any one of claims 1 to 3 are implemented.
Citation Information
Patent Citations
Database adaptation method and device, equipment, storage medium and computer program
CN113722387A
Method for recording data change operation logs in microservices
CN114036167A
Message processing method and device, electronic equipment and storage medium
CN115422294A
Database data desensitization method, system and device and readable storage medium
CN117633863A
Cited By
Database access method and device
CN120723834A
Method, device and equipment for generating database operation log in network system
CN121658439A