Database persistence method, terminal device and computer-readable storage medium

By dynamically generating database storage scripts, persistence processing of multi-table and batch data is achieved, solving the problem of not supporting multi-table and batch data submission in the existing technology, and reducing development and maintenance costs.

CN119311755BActive Publication Date: 2025-05-02SHENZHEN DENGKE CLOUD SOFTWARE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411861987.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-17
Publication Date
2025-05-02
Estimated Expiration
2044-12-17

AI Technical Summary

Technical Problem

The existing persistence framework does not support the submission of multi-table and batch data, resulting in high development and maintenance costs.

Method used

By traversing the data model and its included data collections, dynamically generate database storage scripts, and directly persist the data model into the database without presetting configuration files and mapping relationships.

Benefits of technology

It reduces development and maintenance costs, supports persistent processing of multi-table and batch data, and improves the efficiency and reliability of data persistence.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119311755B_ABST
    Figure CN119311755B_ABST
Patent Text Reader

Abstract

The present application belongs to the field of database technology, and in particular, relates to a database persistence method, terminal device, and computer-readable storage medium, including: traversing each first data model in a first data set to be persisted, and obtaining the attribute information of each first data model; generating a storage script of the database of each first data model according to the attribute information in each first data model that meets the first preset condition; wherein the first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model; and persisting each first data model as the first storage model in the database according to the storage script of each first data model. Through the above method, the development cost and maintenance cost of the database can be effectively reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of database technology, and in particular, relates to a database persistence method, a terminal device, and a computer-readable storage medium. Background Art

[0002] Data persistence is a general term for converting the data model in memory to the storage model, and vice versa. The data model is the model for storing data in memory, and the storage model is the model for storing data in the database.

[0003] Existing persistence frameworks usually do not support the submission of multiple tables or batch data, that is, they do not support the submission of add, delete, and modify data of multiple tables at one time. When processing multiple tables or batch data, it is usually necessary to split the data and configure a large number of configuration files, mapping relationships, etc., which requires a large development workload and the subsequent maintenance cost of the persistence framework is also high. Summary of the invention

[0004] The embodiments of the present application provide a database persistence method, a terminal device, and a computer-readable storage medium, which can effectively reduce the development cost and maintenance cost of the database.

[0005] In a first aspect, an embodiment of the present application provides a data persistence method, including:

[0006] Traversing each first data model in the first data set to be persisted, and obtaining attribute information of each first data model; wherein the data model is a model for storing data in the memory;

[0007] Generate a storage script for the database of each first data model according to the attribute information in each first data model that meets the first preset condition; wherein the first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model; the storage model is a model for storing data in the database;

[0008] Each of the first data models is persisted as a first storage model in the database according to the storage script of each of the first data models.

[0009] In an embodiment of the present application, in the process of converting the data model in the memory into the storage model of the database, a storage script of the database can be generated according to the attribute information that meets the first preset condition. Since the storage script is dynamically generated, there is no need to pre-set the configuration file and mapping relationship between the storage model and the data model, which greatly reduces the development cost; in addition, when the database field is changed, there is no need to maintain the configuration file and mapping relationship, which greatly reduces the maintenance cost of the database.

[0010] In a possible implementation manner of the first aspect, traversing each first data model in the first data set to be persisted to obtain attribute information of each first data model includes:

[0011] If the attribute information of the first data model includes a second data set, each second data model in the second data set is traversed to obtain the attribute information of each second data model; wherein the attribute information of the first data model includes the attribute information of the second data model.

[0012] In the above embodiment, during the data persistence process, all attribute information of the data set included in the data model is obtained by traversing the data model and the data set included in the data model. The above method is helpful to realize the persistence processing of multi-table and batch data. In addition, compared with the prior art, there is no need to split the multi-table and batch data before persistence, but to traverse the attribute information during the persistence process, which can effectively reduce the development workload.

[0013] In a possible implementation manner of the first aspect, the attribute information includes an attribute name and a data type; and the method further includes:

[0014] For each of the first data models, if the table name corresponding to the first data model is consistent with the table name corresponding to the first storage model in the database, determine whether the first name is consistent with the second name; wherein the first name is the attribute name in the attribute information of the first data model, and the second name is the attribute name in the attribute information of the first storage model;

[0015] If the first name is consistent with the second name, determining whether the data type corresponding to the first name is consistent with the data type corresponding to the second name;

[0016] If the data type corresponding to the first name is consistent with the data type corresponding to the second name, it is determined that the first name meets the first preset condition.

[0017] In the above implementation, by comparing the table name, attribute name and data type in sequence, the attribute information of the data model and the storage model can be matched more accurately, which helps to improve the reliability of data persistence. In addition, through the above method, the attribute information of the data model and the storage model can be automatically and dynamically matched during the data persistence process, without the need to split multiple tables or batches of data before data persistence, effectively reducing the development workload of the persistence framework.

[0018] In a possible implementation manner of the first aspect, the attribute information includes a data state and a data value corresponding to each of the attribute names;

[0019] The step of generating a storage script for the database of each first data model according to the attribute information satisfying the first preset condition in each first data model comprises:

[0020] Generate a script template according to the data status in the attribute information of the first data model;

[0021] A storage script for a database of the first data model is generated according to the script template and data values ​​corresponding to attribute names in the first data model that meet a first preset condition.

[0022] Through the above implementation, database scripts are dynamically generated according to the data status during data persistence, without the need to pre-write scripts before data persistence, thereby greatly reducing the development workload of the persistence framework. In addition, since the database scripts are dynamically generated, when the database fields change, the database scripts can also change dynamically, without the need to maintain a large number of scripts, greatly reducing the maintenance cost of the persistence framework.

[0023] In a possible implementation manner of the first aspect, the method further includes:

[0024] Generate a query script for the database according to the query instruction sent by the application;

[0025] Acquire a third data set from the database according to a query script of the database; wherein the third data set includes at least one second storage model;

[0026] Obtaining attribute information of a third data model according to the model type corresponding to the query script; wherein the third data model is used to generate a query result corresponding to the query instruction;

[0027] The third data model is obtained by converting the attribute information that satisfies a second preset condition in each of the second storage models of the third data set; wherein the second preset condition includes that the attribute information of the second storage model is consistent with the attribute information of the third data model.

[0028] In the embodiment of the present application, in the process of data persistence, the attribute information of the data model and the storage model can be automatically matched, and there is no need to pre-set the configuration file and mapping relationship between the storage model and the data model, which greatly reduces the development cost. In addition, through the above method, all storage models associated with the query script can be obtained, thereby obtaining more comprehensive query information, realizing the query of multiple tables or batch data, and can effectively improve the accuracy of data query.

[0029] In a possible implementation manner of the first aspect, acquiring the third data set from the database according to a query script of the database includes:

[0030] Acquire a second storage model corresponding to the query script from the database to obtain first data;

[0031] A second storage model associated with the first data is acquired from the database to obtain second data; wherein the third data set includes the first data and the second data.

[0032] Through the above method, more comprehensive data related to the query script can be obtained, the situation of missed data is reduced, and the accuracy of data query can be effectively improved.

[0033] In a possible implementation manner of the first aspect, the attribute information includes an attribute name and a data value corresponding to the attribute name;

[0034] The step of generating the third data model according to the attribute information satisfying the second preset condition in each of the second storage models of the third data set comprises:

[0035] For each of the second storage models, if the third name is consistent with the fourth name, determine whether the data type corresponding to the third name is consistent with the data type corresponding to the fourth name; wherein the third name is the attribute name in the attribute information of the second storage model, and the fourth name is the attribute name in the attribute information of the third data model;

[0036] If the third name is consistent with the fourth name, the data value corresponding to the third name in the second storage model is assigned to the member corresponding to the fourth name in the third data model.

[0037] Through the above method, in the process of data persistence, the attribute information of the data model and the storage model can be automatically matched, without the need to pre-set the configuration files and mapping relationship between the storage model and the data model, which greatly reduces the development cost.

[0038] In a possible implementation manner of the first aspect, the method further includes:

[0039] After obtaining the query result corresponding to the query instruction, the connection between the application and the database is disconnected.

[0040] By disconnecting the connection between the application and the database in a timely manner, the operating pressure of the database can be reduced.

[0041] In a second aspect, an embodiment of the present application provides a data persistence device, including:

[0042] The traversal unit is used to traverse each first data model in the first data set to be persisted to obtain the attribute information of each first data model; wherein the data model is a model for storing data in the memory.

[0043] A generation unit, used to generate a storage script for the database of each first data model based on the attribute information in each first data model that meets the first preset condition; wherein the first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model; and the storage model is a model for storing data in the database.

[0044] A persistence unit is used to persist each of the first data models as a first storage model in the database according to the storage script of each of the first data models.

[0045] In a third aspect, an embodiment of the present application provides a terminal device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the data persistence method as described in any one of the first aspects above is implemented.

[0046] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the data persistence method as described in any one of the above-mentioned first aspects is implemented.

[0047] In a fifth aspect, an embodiment of the present application provides a computer program product. When the computer program product is run on a terminal device, the terminal device executes the data persistence method described in any one of the above-mentioned first aspects.

[0048] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0049] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0050] Figure 1 It is a structural diagram of the persistence framework provided in the embodiment of the present application;

[0051] Figure 2 It is a flowchart of the data persistence method provided in the embodiment of the present application;

[0052] Figure 3 It is a flowchart of the data persistence method provided in the embodiment of the present application;

[0053] Figure 4 It is a structural block diagram of a data persistence device provided in an embodiment of the present application;

[0054] Figure 5 It is a schematic diagram of the structure of the terminal device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0055] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.

[0056] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.

[0057] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0058] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.

[0059] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.

[0060] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. appearing in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways.

[0061] Data persistence is a general term for converting the data model in memory to the storage model, and vice versa. The data model is the model for storing data in memory, and the storage model is the model for storing data in the database.

[0062] For example, when the data model is an object model and the storage model is a relational model, data persistence refers to the conversion between the object model and the relational model. One of the storage characteristics of relational databases is to query related information through foreign keys to other tables. Therefore, in order to avoid data redundancy, business documents generally only store the codes of basic data. For example, the order table only stores the product code, and the product name, specification, unit and other information are queried through the associated product table. Therefore, the data model displayed to the user is different from the storage model. The data model displayed to the user often has more field data of the associated table, such as the product name, specification, unit and other information mentioned above.

[0063] The data persistence framework is an important tool for the interaction between the application system and the database, and is mainly used to implement operations such as querying, adding, modifying, and deleting data. In the actual development of persistence framework programs, it is usually necessary to process multiple tables or batch data. For example, the preservation of purchase orders requires not only the preservation of the main table purchase order number, supplier and other data, but also the preservation of multiple data such as the purchase of goods from the table, and involves operations such as adding, deleting, and modifying these batch data.

[0064] The existing data persistence frameworks of object-oriented programming languages, such as Hibernate, MyBatis, MyBatis-Plus, Spring Data JPA on the Java platform, Entity Framework, Dapper, NHibernate on the .Net platform, and SQLAlchemy, Tortoise ORM, Django ORM on the Python platform, usually do not support the submission of multi-table and batch data, that is, they do not support the submission of add, delete, and modify data of multiple tables at one time. For example, the existing persistence framework divides the submitted data into add, modify, and delete according to the data status, and then saves them in batches to the database, which greatly increases the difficulty and workload of development.

[0065] When processing multiple tables or batch data, the existing persistence framework requires a large number of configuration files. One database table may correspond to multiple configuration files. Each configuration file includes a large number of scripts such as add, delete, modify, and query, as well as various parameters required by the scripts. Developers need to do a lot of configuration work, and debugging is also very difficult, which makes it inefficient to use. In order to achieve the conversion between the data model and the relational model, the existing persistence framework needs to indicate the mapping relationship between the attributes of the data model and the fields of the storage model, which requires a lot of repetitive development work.

[0066] In addition, when the database fields are changed, the above-mentioned configuration files, mapping relationships, etc. need to be adjusted, and the subsequent maintenance workload is large.

[0067] Based on this, an embodiment of the present application provides a persistence framework, which does not require the setting of configuration files and mapping relationships, etc., and can reduce the development cost and subsequent maintenance cost of the persistence framework.

[0068] See also Figure 1 , is a schematic diagram of the structure of the persistence framework provided in the embodiment of the present application. As an example and not a limitation, Figure 1 As shown, the persistence framework may include an interface layer 11 and a data processing layer 12. The interface layer 11 is used to provide a calling interface for the persistence method, such as receiving an object model list and receiving an object model list returned by a query script. The data processing layer 12 is used to implement the data persistence method provided in the embodiment of the present application, such as interpreting the data model list to generate a database script for adding, deleting, and modifying, executing the database script to save the data to the database, and executing the database query script to obtain the result set returned by the database, converting the result set into a data model list and returning it to the memory.

[0069] The data processing layer 12 may include a first processing module 121 of the storage method and a second processing module 122 of the query method. Correspondingly, the interface layer 11 may include a first interface 111 of the storage method and a second interface 112 of the query method.

[0070] For example, in the data persistence process of storing memory data in the database, the memory calls the storage method, and the storage method calls the first processing module 121 of the data processing layer 12 through the first interface 111 of the interface layer 11. Accordingly, the first processing module 121 executes the following Figure 2 The data persistence method of the embodiment interprets the data model in the memory to generate a database script for adding, deleting and modifying data, and executes the database script through a database operation component to save the data to the database, so as to realize the conversion from the data model to the storage model.

[0071] For another example, in the data persistence process of querying data from the database, the memory calls the query method, and the query method calls the second processing module 122 of the data processing layer 12 through the second interface 112 of the interface layer 11, and sends a query instruction to the second processing module 122. Accordingly, the second processing module 122 executes after receiving the query instruction. Figure 3 The data persistence method of the embodiment generates a query script for the database according to a query instruction, executes the query script through a database operation component to obtain a storage model from the database, converts the storage model into a data model, and finally returns the data model to the memory.

[0072] Based on the above persistence framework, the data persistence method provided by the embodiment of the present application is introduced below. As mentioned above, data persistence may include the process of converting the data model in the memory into the storage model, and converting the storage model into the data model in the memory. The two processes of data persistence are introduced separately below.

[0073] See also Figure 2 , is a flowchart of the data persistence method provided in the embodiment of the present application. As an example and not a limitation, Figure 2 The process of converting a data model into a storage model, that is, storing memory data in a database, is shown. The method may include the following steps:

[0074] S201, traverse each first data model in the first data set to be persisted, and obtain attribute information of each first data model.

[0075] The data model is a model for storing data in memory. The types of data models may include value object model VO (Value Object), persistent object model PO (Persistent Object), business object model BO (BusinessObject), data transfer object model DTO (Data Transfer Object), etc.

[0076] In the embodiment of the present application, a value object model can be used as a data model. Among them, the attribute information of the value object model may include a database connection identifier, a table name, and a data state. Since the value object model contains more data attributes and more comprehensive information, the use of the value object model as a data model can reduce the conversion process between multiple models and help reduce the development workload.

[0077] The attribute information of the data model may include the attribute name, data type, data state, and the data value corresponding to each attribute name. For example, the following table is an example of a data model.

[0078] XX

[0079]

[0080] In the data model shown above, "XX" is the table name. The information in the first row, first column to the third column of the "XX" table is the attribute name, that is, the data model includes 3 attribute names, namely "Product Name", "Quantity" and "Unit Price". The information in the second row to the fourth row of the first column is the data value corresponding to "Product Name", the information in the second row to the fourth row of the second column is the data value corresponding to "Quantity", and the information in the second row to the fourth row of the third column is the data value corresponding to "Unit Price". The information in the second row to the fourth row of the fourth column indicates the data status corresponding to the corresponding row of data values. For example, "Add" in the second row of the fourth column indicates that the data value of the second row is newly added data, "Modify" in the third row of the fourth column indicates that the data value of the third row is modified data, and "Delete" in the fourth row of the fourth column indicates that the data value of the fourth row is deleted.

[0081] It should be noted that the above data model is presented in a tabular form. In practical applications, the data model may also be in other forms, which is not specifically limited in the embodiments of the present application.

[0082] In one implementation, S201 includes:

[0083] Traverse the rows and columns of the first data model to obtain attribute information of the first data model.

[0084] For example, as in the above-mentioned "XX" table, the attribute information of the table can be obtained by traversing the rows and columns of the table, wherein the attribute names in the attribute information include "product name", "quantity" and "unit price".

[0085] In actual applications, the first data model may not be a single data model, but may be a collection of other data models. In another implementation, S201 includes:

[0086] If the attribute information of the first data model includes the second data set, each second data model in the second data set is traversed to obtain the attribute information of each second data model; wherein the attribute information of the first data model includes the attribute information of the second data model.

[0087] For example, the following table shows another example of a data model.

[0088] YY

[0089]

[0090] As shown in the table above, "YY" is the table name. The information "Unit Price" in the first row and third column of the "YY" table is the attribute name. The information in the second to fourth rows of the third column is the data value corresponding to "Unit Price". However, it can be seen from the table above that each data value corresponding to "Unit Price" is a table name (each table name corresponds to a data model), the data value in the second row and third column corresponds to the "Y1" table, the data value in the third row and third column corresponds to the "Y2" table, and the data value in the fourth row and third column corresponds to the "Y3" table. In other words, the "YY" table data model includes a data set, namely the three data models of the "Y1" table, the "Y2" table, and the "Y3" table.

[0091] In this case, it is necessary not only to traverse the rows and columns in the "YY" table, but also to traverse the rows and columns in the "Y1" table, the "Y2" table, and the "Y3" table included in the "YY" table.

[0092] Take the "Y1" table as an example, assuming that the "Y1" table is as follows:

[0093] Y1

[0094]

[0095] The attribute names of the attribute information obtained by traversing the "Y1" table include "Product Number" and "Unit Price". Then the attribute information of the "YY" table includes not only the attribute names "Product Name", "Quantity" and "Unit Price" in the first row of the "YY" table, but also the attribute name "Product Number" in the first row of the "Y1" table.

[0096] After traversing the rows and columns in the "Y1" table, the "Y2" table and the "Y3" table included in the "YY" table, the attribute names of the attribute information of the "YY" table include the attribute names in the first row of the "YY" table, the attribute names in the "Y1" table, the attribute names in the "Y2" table and the attribute names in the "Y3" table.

[0097] It can be understood that, in the above example, the "YY" table can be regarded as the first data model, and the "Y1" table, the "Y2" table and the "Y3" table can be regarded as the second data model.

[0098] In the above embodiment, during the data persistence process, all attribute information of the data set included in the data model is obtained by traversing the data model and the data set included in the data model. The above method is helpful to realize the persistence processing of multi-table and batch data. In addition, compared with the prior art, there is no need to split the multi-table and batch data before persistence, but to traverse the attribute information during the persistence process, which can effectively reduce the development workload.

[0099] S202: Generate a storage script for the database of each first data model according to the attribute information that meets the first preset condition in each first data model.

[0100] The storage model is the model for storing data in the database.

[0101] The first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model.

[0102] Usually, the attribute information displayed to the user is more than the attribute information of the storage model. For example, the following table shows an example of a storage model.

[0103] ZZ

[0104]

[0105] As shown in the table above, "ZZ" is the table name. The first row of the "ZZ" table contains attribute names, including "Product Name" and "Unit Price". Compared with the above "XX" table, the attribute names of the "ZZ" table lack "Quantity".

[0106] In some judgment methods of the first preset condition, only the first name and the second name can be compared to see whether they are consistent; if the first name and the second name are consistent, then it is determined that the first name meets the first preset condition. Wherein, the first name is the attribute name in the attribute information of the first data model, and the second name is the attribute name in the attribute information of the first storage model.

[0107] In some other implementations of the first preset condition, the following may be included:

[0108] For each first data model, if the table name corresponding to the first data model is consistent with the table name corresponding to the first storage model in the database, determine whether the first name is consistent with the second name; wherein the first name is the attribute name in the attribute information of the first data model, and the second name is the attribute name in the attribute information of the first storage model;

[0109] If the first name is consistent with the second name, determining whether the data type corresponding to the first name is consistent with the data type corresponding to the second name;

[0110] If the data type corresponding to the first name is consistent with the data type corresponding to the second name, it is determined that the first name meets the first preset condition.

[0111] Optionally, the table names are the same, which may refer to the same table names or the same table names. For example, the table name of the first data model is "XX" and the table name of the first storage model is "ZZ". Although the table names are different, they are related, that is, the data of the "XX" table in the memory is stored as the "ZZ" table in the database.

[0112] Optionally, the first name and the second name are consistent, which may mean that the first name and the second name are the same, or that the first name and the second name have the same reference. For example, if the first name is "product name" and the second name is "product name", and they have the same reference, then the first name and the second name are consistent.

[0113] As an example of judging the first preset condition, assume that the "XX" table is the first data model and the "ZZ" table is the first storage model. The attribute name "Product Name" in the "XX" table is consistent with the "Product Name" in the "ZZ" table, then the attribute name "Product Name" in the "XX" table meets the first preset condition. The attribute name "Quantity" does not exist in the "ZZ" table, that is, the attribute name "Quantity" in the "XX" table does not meet the first preset condition. The attribute name "Unit Price" in the "XX" table is consistent with the "Unit Price" in the "ZZ" table, then the attribute name "Unit Price" in the "XX" table meets the first preset condition.

[0114] After determining the first preset condition, a storage script is generated according to the data values ​​and data states corresponding to the first names that meet the first preset condition, ie, "product name" and "unit price", respectively.

[0115] In the above implementation, by comparing the table name, attribute name and data type in sequence, the attribute information of the data model and the storage model can be matched more accurately, which helps to improve the reliability of data persistence. In addition, through the above method, the attribute information of the data model and the storage model can be automatically and dynamically matched during the data persistence process, without the need to split multiple tables or batches of data before data persistence, effectively reducing the development workload of the persistence framework.

[0116] In one implementation, S202 may include:

[0117] Generate a script template according to the data state in the attribute information of the first data model;

[0118] A storage script for the database of the first data model is generated according to the script template and the data values ​​corresponding to the attribute names that meet the first preset condition in the first data model.

[0119] Continuing with the above example, the data status of the second row in the "XX" table is "Newly Added", and a script template corresponding to "Newly Added" is generated, such as the pseudo code "Insert into orderdtl (matecode, inprince)values('Product Name','Unit Price')"; then the data values ​​corresponding to "Product Name" and "Unit Price" in the second row are added to the corresponding positions in the script template to obtain a storage script, such as the pseudo code "Insert into orderdtl (0001, 2)values('Product Name','Unit Price')".

[0120] The data status of the third row in the "XX" table is "modify", and the script template corresponding to "modify" is generated, such as the pseudo code "Update orderdtl set matecode='product name', inprice='unit price', where id='primary key'"; then the data values ​​corresponding to "product name" and "unit price" in the third row are added to the corresponding positions in the script template to obtain a storage script, such as the pseudo code "Update orderdtl set matecode=0002, inprice=3, where id=3". Among them, "primary key" is the unique identifier of the row in the table.

[0121] In the delete operation, all the data in a row can be deleted. For example, the data status of the fourth row in the "XX" table is "delete", and the script template corresponding to "delete" is generated, such as the pseudo code "Delete from orderdtl where id='primary key'"; then the storage script is generated according to the attribute information of the fourth row, such as the pseudo code "Delete from orderdtl whereid='4'". It can be understood that in the delete operation, only the data value under a certain attribute name in a row of data can be deleted. In this case, the process of generating a storage script is the same as the principle of the above-mentioned "modify" and "add" examples, which will not be repeated here.

[0122] It should be noted that the pseudo code examples of the script templates and stored scripts in the above examples are only used for illustrative purposes, and the code formats of the script templates and stored scripts are not specifically limited in the embodiments of the present application.

[0123] Through the above implementation, database scripts are dynamically generated according to the data status during data persistence, without the need to pre-write scripts before data persistence, thereby greatly reducing the development workload of the persistence framework. In addition, since the database scripts are dynamically generated, when the database fields change, the database scripts can also change dynamically, without the need to maintain a large number of scripts, greatly reducing the maintenance cost of the persistence framework.

[0124] S203: Persist each first data model as a first storage model in the database according to the storage script of each first data model.

[0125] In one implementation, Figure 1 In the persistence framework shown, after the first processing module 121 obtains the storage script, the storage script can be executed by the database operation component to persist the first data model into the first storage model, thereby realizing the storage of the memory data into the database.

[0126] Figure 2 In the embodiment, during the data persistence process, the database script can be dynamically generated without pre-setting the configuration files and mapping relationships between the storage model and the data model, which greatly reduces the development cost; in addition, when the database field is changed, there is no need to maintain the configuration files and mapping relationships, which greatly reduces the maintenance cost of the database.

[0127] See also Figure 3 , is a flowchart of the data persistence method provided in the embodiment of the present application. As an example and not a limitation, Figure 3 The process of converting a storage model into a data model, that is, reading the stored data of the database into the memory, is shown. The method may include the following steps:

[0128] S301, generating a query script for a database according to a query instruction sent by an application.

[0129] In some implementations, such as Figure 1 In the persistence framework shown, the user can input query information by interacting with the application; accordingly, the application receives the query information input by the user, generates a query instruction based on the query information, and calls the second interface 112 to send the query instruction to the second processing module 122; the second processing module 122 receives the query instruction and generates a query script based on the query instruction.

[0130] Optionally, the process of generating a query script according to a query instruction may be to convert the query instruction into a database language to obtain a query script. For example, if the query instruction is in C language or JAVA language, the query instruction is converted into a SQL language of the database to obtain a query script.

[0131] S302: Acquire a third data set from the database according to a query script of the database.

[0132] Wherein, the third data set includes at least one second storage model.

[0133] In the embodiment of the present application, the query script may include a table name.

[0134] In one implementation, a storage model matching the table name of the query script is obtained from the database as the second storage model.

[0135] As can be seen from the example of the "YY" table above, in actual applications, data models may be associated with each other. Correspondingly, storage models in the database may also be associated with each other, that is, a certain attribute information in one storage model may be the table name of another storage model. For example, the following table shows an example of a storage model.

[0136] AA

[0137]

[0138] As shown in the table above, "AA" is the table name. The first row of the "AA" table contains attribute names, including "Product Name" and "Unit Price". Each data value corresponding to "Unit Price" is a table name (each table name corresponds to a storage model), the data value in the second row and second column corresponds to the "A1" table, the data value in the third row and second column corresponds to the "A2" table, and the data value in the fourth row and second column corresponds to the "A3" table. In other words, the "AA" table is associated with the "A1" table, the "A2" table, and the "A3" table.

[0139] In this case, if you only obtain the storage model that matches the table name of the query script, some data may be missed, reducing the accuracy of the query results.

[0140] To solve the above problem, in another implementation, S302 may include:

[0141] Acquire a second storage model corresponding to the query script from the database to obtain the first data;

[0142] A second storage model associated with the first data is acquired from the database to obtain second data; wherein the third data set includes the first data and the second data.

[0143] For example, the "YY" table is stored as the "AA" table in the database. When the table name of the query script is "YY", the corresponding storage model is the "AA" table, that is, the first data; then the storage model associated with the "AA" table is obtained, such as the "A1" table, the "A2" table, and the "A3" table, and the second data includes the "A1" table, the "A2" table, and the "A3" table. Then the third data corresponding to the query script includes the "AA" table, the "A1" table, the "A2" table, and the "A3" table.

[0144] Through the above method, more comprehensive data related to the query script can be obtained, the situation of missed data is reduced, and the accuracy of data query can be effectively improved.

[0145] S303: Obtain attribute information of the third data model according to the model type corresponding to the query script.

[0146] The third data model is used to generate a query result corresponding to the query instruction. It can be understood that the third data model is the query data obtained by the application, and the application can convert the third data model into interactive information (i.e., query results) that can be viewed by the user and display it to the user.

[0147] Continuing with the above example, assuming that the table name of the query script is "YY", the "YY" table can be traversed to obtain the attribute information in the "YY" table, such as the attribute name, etc. It can be understood that the process of obtaining the attribute information of the third data model is the same as the process of obtaining the attribute information of the first data model in step S201. For details, please refer to the description in the S201 embodiment, which will not be repeated here.

[0148] S304: Obtain a third data model according to the attribute information satisfying the second preset condition in each second storage model of the third data set.

[0149] The second preset condition includes that the attribute information of the second storage model is consistent with the attribute information of the third data model.

[0150] In one implementation, S304 may include:

[0151] For each second storage model, if the third name is consistent with the fourth name, determine whether the data type corresponding to the third name is consistent with the data type corresponding to the fourth name; wherein the third name is the attribute name in the attribute information of the second storage model, and the fourth name is the attribute name in the attribute information of the third data model;

[0152] If the third name is consistent with the fourth name, the data value corresponding to the third name in the second storage model is assigned to the member corresponding to the fourth name in the third data model.

[0153] Exemplarily, the third data model is a "YY" table, which includes the attribute names "Product Name"

[0154] , "Quantity", "Product Number" and "Unit Price". The second storage model included in the third data set includes the "AA" table, the "A1" table, the "A2" table and the "A3" table. Among them, the attribute names included in the "AA" table are "Product Name" and "Unit Price", the attribute names included in the "A1" table are "Product Number" and "Unit Price", the attribute names included in the "A2" table are "Product Number" and "Unit Price", and the attribute names included in the "A3" table are "Product Number" and "Unit Price", that is, the attribute names included in the "AA" table are "Product Name", "Unit Price" and "Product Number".

[0155] Among them, the "Product Name" in the "YY" table is consistent with the "Product Name" in the "AA" table, the "Product Number" in the "YY" table is consistent with the "Product Number" in the "AA" table, and the "Unit Price" in the "YY" table is consistent with the "Unit Price" in the "AA" table. Then, the data value of the column where the "Product Name" in the "AA" table is located is assigned to the column where the "Product Name" in the "YY" table is located, the data value of the column where the "Product Number" in the "AA" table is located is assigned to the column where the "Product Number" in the "YY" table is located, and the data value of the column where the "Unit Price" in the "AA" table is located is assigned to the column where the "Unit Price" in the "YY" table is located.

[0156] Of course, it is understandable that in the process of assigning values, the data in each row needs to correspond to each other. For example, if the data in the column of "Product Name" in the "AA" table is assigned to the second to fourth rows of the column of "Product Name" in the "YY" table, then the data in the column of "Unit Price" in the "AA" table is also assigned to the second to fourth rows of the column of "Unit Price" in the "YY" table to ensure data correspondence.

[0157] Figure 3 In the embodiment, in the process of data persistence, the attribute information of the data model and the storage model can be automatically matched, and there is no need to pre-set the configuration file and mapping relationship between the storage model and the data model, which greatly reduces the development cost. In addition, through the above method, all storage models associated with the query script can be obtained, so as to obtain more comprehensive query information, realize the query of multiple tables or batch data, and can effectively improve the accuracy of data query.

[0158] In one embodiment, the method further comprises:

[0159] After obtaining the query result corresponding to the query instruction, the connection between the application and the database is disconnected.

[0160] By disconnecting the connection between the application and the database in a timely manner, the operating pressure of the database can be reduced.

[0161] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.

[0162] Corresponding to the data persistence method described in the above embodiment, Figure 4 This is a structural block diagram of a data persistence device provided in an embodiment of the present application. For ease of explanation, only the parts related to the embodiment of the present application are shown.

[0163] Reference Figure 4 , the device 4 comprises:

[0164] The traversal unit 41 is used to traverse each first data model in the first data set to be persisted, and obtain the attribute information of each first data model; wherein the data model is a model for storing data in the memory.

[0165] A generation unit 42 is used to generate a storage script for the database of each first data model based on the attribute information in each first data model that meets the first preset condition; wherein the first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model; and the storage model is a model for storing data in the database.

[0166] The persistence unit 43 is used to persist each of the first data models as a first storage model in the database according to the storage script of each of the first data models.

[0167] Optionally, the traversal unit 41 is further used for:

[0168] If the attribute information of the first data model includes a second data set, each second data model in the second data set is traversed to obtain the attribute information of each second data model; wherein the attribute information of the first data model includes the attribute information of the second data model.

[0169] Optionally, the generating unit 42 is further configured to:

[0170] For each of the first data models, if the table name corresponding to the first data model is consistent with the table name corresponding to the first storage model in the database, determine whether the first name is consistent with the second name; wherein the first name is the attribute name in the attribute information of the first data model, and the second name is the attribute name in the attribute information of the first storage model;

[0171] If the first name is consistent with the second name, determining whether the data type corresponding to the first name is consistent with the data type corresponding to the second name;

[0172] If the data type corresponding to the first name is consistent with the data type corresponding to the second name, it is determined that the first name meets the first preset condition.

[0173] Optionally, the generating unit 42 is further configured to:

[0174] Generate a script template according to the data status in the attribute information of the first data model;

[0175] A storage script for a database of the first data model is generated according to the script template and data values ​​corresponding to attribute names in the first data model that meet a first preset condition.

[0176] Optionally, the device 4 further comprises:

[0177] A query unit 44, configured to generate a query script for the database according to a query instruction sent by an application program;

[0178] Acquire a third data set from the database according to a query script of the database; wherein the third data set includes at least one second storage model;

[0179] Obtaining attribute information of a third data model according to the model type corresponding to the query script; wherein the third data model is used to generate a query result corresponding to the query instruction;

[0180] The third data model is obtained by converting the attribute information that satisfies a second preset condition in each of the second storage models of the third data set; wherein the second preset condition includes that the attribute information of the second storage model is consistent with the attribute information of the third data model.

[0181] Optionally, the query unit 44 is further configured to:

[0182] Acquire a second storage model corresponding to the query script from the database to obtain first data;

[0183] A second storage model associated with the first data is acquired from the database to obtain second data; wherein the third data set includes the first data and the second data.

[0184] Optionally, the query unit 44 is further configured to:

[0185] For each of the second storage models, if the third name is consistent with the fourth name, determine whether the data type corresponding to the third name is consistent with the data type corresponding to the fourth name; wherein the third name is the attribute name in the attribute information of the second storage model, and the fourth name is the attribute name in the attribute information of the third data model;

[0186] If the third name is consistent with the fourth name, the data value corresponding to the third name in the second storage model is assigned to the member corresponding to the fourth name in the third data model.

[0187] Optionally, the query unit 44 is further configured to:

[0188] After obtaining the query result corresponding to the query instruction, the connection between the application and the database is disconnected.

[0189] It should be noted that the information interaction, execution process, etc. between the above-mentioned devices / units are based on the same concept as the method embodiment of the present application. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.

[0190] in addition, Figure 4 The data persistence device shown may be a software unit, a hardware unit, or a combination of software and hardware, which is built into an existing terminal device, or may be integrated into the terminal device as an independent widget, or may exist as an independent terminal device.

[0191] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0192] Figure 5 Schematic diagram of the structure of the terminal device provided in the embodiment of the present application. Figure 5 As shown, the terminal device 5 of this embodiment includes: at least one processor 50 ( Figure 5 Only one is shown in the figure) a processor, a memory 51, and a computer program 52 stored in the memory 51 and executable on the at least one processor 50, and when the processor 50 executes the computer program 52, the steps in any of the above-mentioned data persistence method embodiments are implemented.

[0193] The terminal device may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal device may include, but is not limited to, a processor and a memory. Those skilled in the art will understand that Figure 5 It is only an example of the terminal device 5 and does not constitute a limitation on the terminal device 5. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, it may also include input and output devices, network access devices, etc.

[0194] The processor 50 may be a central processing unit (CPU), or 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. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0195] In some embodiments, the memory 51 may be an internal storage unit of the terminal device 5, such as a hard disk or memory of the terminal device 5. In other embodiments, the memory 51 may also be an external storage device of the terminal device 5, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the terminal device 5. Further, the memory 51 may also include both an internal storage unit and an external storage device of the terminal device 5. The memory 51 is used to store an operating system, an application program, a boot loader, data, and other programs, such as the program code of the computer program. The memory 51 may also be used to temporarily store data that has been output or is to be output.

[0196] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.

[0197] An embodiment of the present application provides a computer program product. When the computer program product is run on a terminal device, the terminal device can implement the steps in the above-mentioned method embodiments when executing the computer program product.

[0198] If the integrated unit 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 this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned various method embodiments can be implemented. Among them, 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 at least include: any entity or device that can carry the computer program code to the device / terminal device, recording medium, 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. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.

[0199] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0200] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0201] In the embodiments provided in the present application, it should be understood that the disclosed devices / terminal equipment and methods can be implemented in other ways. For example, the device / terminal equipment embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0202] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0203] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A method for processing data persistence of multiple tables, characterized in that: include: Traverse the rows and columns of each first data model in the first data set to be persisted, and obtain the attribute information of each first data model; wherein the data model is a model for storing data in memory; the attribute information of the data model includes attribute name, data type, data state and data value corresponding to each attribute name; the data state includes three operation types: adding, modifying and deleting; the first data model is a collection of multiple data models; Generate a storage script for the database of each first data model according to the attribute information in each first data model that meets the first preset condition; wherein the first preset condition includes that the attribute information of the first data model is consistent with the attribute information of the first storage model; the storage model is a model for storing data in the database; Persisting each of the first data models as a first storage model in the database according to the storage script of each of the first data models; The traversing each first data model in the first data set to be persisted to obtain the attribute information of each first data model includes: If the attribute information of the first data model includes a second data set, traversing each second data model in the second data set to obtain the attribute information of each second data model; wherein the attribute information of the first data model includes the attribute information of the second data model; The step of generating a storage script for a database of each first data model according to the attribute information satisfying the first preset condition in each first data model comprises: Generate a script template corresponding to the data state in the attribute information of the first data model; A storage script for a database of the first data model is generated according to the script template and data values ​​corresponding to attribute names in the first data model that meet a first preset condition.

2. The method for processing multi-table data persistence as claimed in claim 1, characterized in that: The attribute information includes an attribute name and a data type; the method further includes: For each of the first data models, if the table name corresponding to the first data model is consistent with the table name corresponding to the first storage model in the database, determine whether the first name is consistent with the second name; wherein the first name is the attribute name in the attribute information of the first data model, and the second name is the attribute name in the attribute information of the first storage model; If the first name is consistent with the second name, determining whether the data type corresponding to the first name is consistent with the data type corresponding to the second name; If the data type corresponding to the first name is consistent with the data type corresponding to the second name, it is determined that the first name meets the first preset condition.

3. The method for processing multi-table data persistence as claimed in claim 1 or 2, characterized in that: The method further comprises: Generate a query script for the database according to the query instruction sent by the application; Acquire a third data set from the database according to a query script of the database; wherein the third data set includes at least one second storage model; Obtaining attribute information of a third data model according to the model type corresponding to the query script; wherein the third data model is used to generate a query result corresponding to the query instruction; The third data model is obtained by converting the attribute information that satisfies a second preset condition in each of the second storage models of the third data set; wherein the second preset condition includes that the attribute information of the second storage model is consistent with the attribute information of the third data model.

4. The method for processing multi-table data persistence as claimed in claim 3, characterized in that: The acquiring a third data set from the database according to a query script of the database comprises: Acquire a second storage model corresponding to the query script from the database to obtain first data; A second storage model associated with the first data is acquired from the database to obtain second data; wherein the third data set includes the first data and the second data.

5. The method for processing multi-table data persistence as claimed in claim 3, characterized in that: The attribute information includes an attribute name and a data value corresponding to the attribute name; The step of generating the third data model according to the attribute information satisfying the second preset condition in each of the second storage models of the third data set comprises: For each of the second storage models, if the third name is consistent with the fourth name, determine whether the data type corresponding to the third name is consistent with the data type corresponding to the fourth name; wherein the third name is the attribute name in the attribute information of the second storage model, and the fourth name is the attribute name in the attribute information of the third data model; If the third name is consistent with the fourth name, the data value corresponding to the third name in the second storage model is assigned to the member corresponding to the fourth name in the third data model.

6. The method for processing multi-table data persistence as claimed in claim 3, characterized in that: The method further comprises: After obtaining the query result corresponding to the query instruction, the connection between the application and the database is disconnected.

7. A terminal device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 6 is implemented.

8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Data storage method and device

    CN114490651A

  • Method, apparatus, and computer program product for persisting objects in a relational database

    US6418451B1