CN115756634A - Database object relational mapping model establishing method, device, equipment and medium - Google Patents

Database object relational mapping model establishing method, device, equipment and medium Download PDF

Info

Publication number
CN115756634A
CN115756634A CN202211387932.7A CN202211387932A CN115756634A CN 115756634 A CN115756634 A CN 115756634A CN 202211387932 A CN202211387932 A CN 202211387932A CN 115756634 A CN115756634 A CN 115756634A
Authority
CN
China
Prior art keywords
data
database
character strings
mapping model
relational mapping
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211387932.7A
Other languages
Chinese (zh)
Inventor
郭国峰
甘国强
祝磊
于惠洋
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen Huayun Information System Co ltd
Original Assignee
Shenzhen Huayun Information System Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shenzhen Huayun Information System Co ltd filed Critical Shenzhen Huayun Information System Co ltd
Priority to CN202211387932.7A priority Critical patent/CN115756634A/en
Publication of CN115756634A publication Critical patent/CN115756634A/en
Pending legal-status Critical Current

Links

Images

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The application provides a method, a device, equipment and a medium for establishing a database object relational mapping model, which comprise the following steps: carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of character strings for expanding the source code so as to generate an SQL (structured query language) statement when a program is started; configuring a data table of a plurality of character strings, and fields, comments and attributes which belong to the data table; when the system runs, functions, field expressions, inter-table relations and data structures of source code extension are stored in a memory, a database object relational mapping model is established, SQL statements corresponding to the model are generated, the database object relational mapping model is loaded when data migration is carried out, data persistent storage is achieved, and operation complexity is reduced.

Description

Database object relational mapping model establishing method, device, equipment and medium
Technical Field
The present application relates to the field of software technologies, and in particular, to a method, an apparatus, a device, and a medium for establishing a database object relational mapping model.
Background
The Mapping from the persistent database to the memory Object includes an Object Relational Mapping (ORM) model, which mainly completes the Mapping of the data of the persistent database to the class Object of C + +, and can support the operation of the corresponding database data when the user adds, updates, deletes, and searches the corresponding class Object data. The SQL interface is packaged by the model, the SQL (structured query language) script does not need to be written manually, and a series of problems of complicated script writing, field content use analysis, field use communication among engineers, manual operation during version upgrading, authority examination and approval and the like can be saved in the development stage. When the database is used, the problem that the code is inconsistent with the use of the database design is avoided, misoperation risks possibly encountered by operating the database are avoided, testers do not fully test the migration script, the upgrading execution is not timely, updating is omitted, and the like. Many engineering problems can be solved using the ORM model.
In the C + + domain, there are some existing schemes such as ODB, liteSQL, etc., and other languages such as Gorm from Golang can support table structure migration without the need for a specialized model generator.
For ODB, a special annotation needs to be added to the C + + code, a special ODB compiler is needed, the special C + + code is generated, and the code is generated statically during compiling. Classes that can support C + + generate corresponding SQL code.
For LiteSQL, which also needs to add annotations, a special model generation tool is used for analyzing the C + + code to generate a corresponding C + + code, and the C + + class is supported to generate a corresponding SQL code.
Because ODB and LiteSQL need special code generation tools and use generated codes, the use experience is inconsistent, the influence of the generated codes on actual use needs to be checked, and when the model structure is changed, codes need to be regenerated, and the automatic migration of the model is not supported.
The prior art has the disadvantages that the code needs to be regenerated, and the native code and the generated code use experience are inconsistent because C + + does not support annotation and a special generator is used for parsing the C + + code. Meanwhile, the automatic migration of the structural model is not supported, extra manpower burden is caused to a series of engineering problems of code and SQL change, upgrade support, manual operation and maintenance, operation risk and the like, and a code generation mode is high in cost, complex in operation, not beneficial to maintenance and not easy to expand.
Disclosure of Invention
The embodiment of the application provides a database object relational mapping model establishing method, device, equipment and medium, and aims to solve the problems that in the prior art, extra labor load is caused to a series of engineering problems such as code and SQL change, upgrade support, manual operation and maintenance, operation risk and the like, and a code generating mode is high in cost, complex in operation, not beneficial to maintenance and not easy to expand in the related art.
In order to solve the above technical problem, the embodiments of the present application are implemented as follows:
in a first aspect, an embodiment of the present application provides a method for building a database object relational mapping model, where the method includes:
carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded to the source code so as to automatically generate a corresponding SQL statement when a program is started;
configuring a data table corresponding to the character strings, and fields, comments and attributes of the fields in the data table, to which the character strings belong;
and during operation, storing the functions, field representation, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model and SQL sentences corresponding to the generation model according to the extension functions, the field representation, the type representation, the attribute representation, the inter-table relations and the data structures of the optimized character strings and the fields of the character strings in the data table so as to load the database object relational mapping model during data migration and realize data persistent storage.
Optionally, after the macro expansion processing is performed on the source code of the original file to obtain an expanded code corresponding to the source code, the method further includes:
adding its annotations and properties within the fixed format of the expanded code.
Optionally, after the building a database object relational mapping model according to the optimized extension function, field representation, type representation, attribute representation, inter-table relationship, and data structure of the plurality of character strings and the fields of the plurality of character strings in the data table, and the generating an SQL statement corresponding to the model, the method further includes:
when loading database data, loading the database object relational mapping model and connecting the database;
acquiring a table structure stored in the database according to the table data loaded in the database;
applying for an object size memory;
converting the table data into target type data according to the field type of the table structure;
acquiring a field offset pointer;
and copying the target type data to a memory space corresponding to the applied object size memory according to the field offset pointer.
Optionally, after the establishing a database object relational mapping model according to the optimized extension functions, field representation, type representation, attribute representation, inter-table relations, and data structures of the plurality of character strings and the fields of the plurality of character strings in the data table, and generating SQL statements corresponding to the model, the method further includes:
loading the database object relational mapping model and connecting a database when the table structure is migrated;
acquiring table structure information of the database and database extension information;
and generating a modified data table statement based on the database object relational mapping model, the table structure information and the database extension information, and executing the modified data table statement.
Optionally, the modifying the data table statement includes: at least one of a data addition statement, a data deletion statement, a data modification statement, and a data query statement.
In a second aspect, an embodiment of the present application provides an apparatus for building a database object relational mapping model, where the apparatus includes:
the expanded code acquisition module is used for carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded on the source code so as to automatically generate a corresponding SQL statement when a program is started;
the field configuration module is used for configuring data tables corresponding to the character strings, and fields, comments and attributes of the fields in the data tables, to which the character strings belong;
and the mapping model establishing module is used for storing the functions, field representations, inter-table relations and data structures of the source code extension in a memory during operation, establishing a database object relation mapping model according to the extended functions, the field representations, the type representations, the attribute representations, the inter-table relations and the data structures of the character strings after optimization, and the fields of the character strings in the data table, and generating SQL sentences corresponding to the model so as to load the database object relation mapping model during data migration and realize data persistent storage.
Optionally, the apparatus further comprises:
and the comment attribute adding module is used for adding the comment and the attribute of the expanded code in the fixed format.
Optionally, the apparatus further comprises:
the first model loading module is used for loading the database object relational mapping model and connecting the database when loading database data;
the table structure acquisition module is used for acquiring the table structure stored in the database according to the table data loaded in the database;
the memory application module is used for applying for the memory with the size of the object;
the destination data conversion module is used for converting the table data into destination type data according to the field type of the table structure;
the offset pointer acquisition module is used for acquiring a field offset pointer;
and the destination data storage module is used for copying the destination type data to the memory space corresponding to the applied object size memory according to the field offset pointer.
Optionally, the apparatus further comprises:
the second model loading module is used for loading the database object relational mapping model and connecting the database when the table structure is migrated;
the extended information acquisition module is used for acquiring the table structure information of the database and the database extended information;
and the data table statement execution module is used for generating a modified data table statement based on the database object relational mapping model, the table structure information and the database extension information and executing the modified data table statement.
Optionally, the modifying the data table statement includes: at least one of a data addition statement, a data deletion statement, a data modification statement, and a data query statement.
In a third aspect, an embodiment of the present application provides an electronic device, including:
a memory, a processor and a computer program stored on the memory and executable on the processor, the computer program when executed by the processor implementing the test result determination method of any one of the above.
In a fourth aspect, embodiments of the present application provide a readable storage medium, where instructions that, when executed by a processor of an electronic device, enable the electronic device to perform any one of the test result determination methods described above.
In the embodiment of the application, a source code of an original file is subjected to macro expansion processing to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded to the source code so as to automatically generate a corresponding SQL statement when a program is started; configuring a data table corresponding to the character strings, and fields, comments and attributes of the fields in the data table, to which the character strings belong; and during operation, storing the functions, field representation, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model and SQL sentences corresponding to the generation model according to the extension functions, the field representation, the type representation, the attribute representation, the inter-table relations and the data structures of the optimized character strings and the fields of the character strings in the data table so as to load the database object relational mapping model during data migration and realize data persistent storage. According to the method and the device, the object relation mapping based on the C + + macro-expansion precompiled reflection is adopted, the problems that reflection of different data types cannot be completed in C + + is solved, reflection from external data to memory objects cannot be completed in the object relation, SQL (structured query language) needs a special tool to be generated, corresponding table building, data query and data updating cannot be directly generated from the C + + type, SQL does not support automatic structure model migration and the like are solved, and communication and risk cost of a development stage, an upgrading stage, an operation and maintenance stage and the like can be remarkably saved.
The foregoing description is only an overview of the technical solutions of the present application, and the present application can be implemented according to the content of the description in order to make the technical means of the present application more clearly understood, and the following detailed description of the present application is given in order to make the above and other objects, features, and advantages of the present application more clearly understandable.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the description of the embodiments of the present application will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art that other drawings can be obtained according to the drawings without creative efforts.
Fig. 1 is a flowchart illustrating steps of a database object relational mapping model building method according to an embodiment of the present disclosure;
fig. 2 is a schematic diagram of a table structure data storage process according to an embodiment of the present application;
fig. 3 is a schematic diagram of a data copying process according to an embodiment of the present application;
fig. 4 is a schematic diagram of an old table object deleting process according to an embodiment of the present disclosure;
fig. 5 is a schematic diagram of a data modification process provided in an embodiment of the present application;
FIG. 6 is a schematic diagram of a data query process according to an embodiment of the present application;
FIG. 7 is a schematic diagram of a table structure cloning flow provided in an embodiment of the present application;
fig. 8 is a schematic diagram of a JSON data mapping process provided in an embodiment of the present application;
fig. 9 is a schematic diagram of a JSON data loading process provided in an embodiment of the present application;
fig. 10 is a schematic structural diagram of an apparatus for building a database object relational mapping model according to an embodiment of the present application;
fig. 11 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are some, but not all, of the embodiments of the present application. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Referring to fig. 1, a flowchart illustrating steps of a database object relational mapping model establishment method provided in an embodiment of the present application is shown, and as shown in fig. 1, the database object relational mapping model establishment method may include the following steps:
step 101: carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded to the source code so as to automatically generate a corresponding SQL statement when a program is started.
In the embodiment of the application, the mapping between the C + + class members and the table structure can be completed in a macro-expansion precompiled form. And different types of data reflection are completed by loading data mapping to the C + + class object. And (4) generating SQL statements such as table building, inserting, updating, replacing, inquiring and the like by loading the mapping model during operation, completing the conversion from the C + + class object to the SQL statements, and completing the persistence of corresponding data.
When a database object relational mapping model is established, macro expansion processing can be carried out on a source code of an original file to obtain an expanded code corresponding to the source code, the expanded code can comprise a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded on the source code so as to automatically generate a corresponding SQL statement when a program is started. In the specific implementation, during the precompilation of class names, byte alignment attributes and different types of members, the class names, the types, the names, the capacity sizes, the comments and the byte alignment attributes of the members can be separately compiled into character strings in a fixed format, the fixed format supports the addition and the expansion of extension information, additional attribute data can be added to a comment part, the original class structure is not influenced, the precompilation and the separation of functions of C + + class codes and runtime codes are completed, the static code generation is completed, the runtime class structure information and the extension information are collected, the definition of the C + + class and the collection of the runtime essential information and the extension information are completed by the same code, and the migration, the cloning and the mapping of SQL statement fields and the type information of the list structure in the runtime are supported.
After the macro expansion processing is performed on the source code of the original file to obtain the expanded code corresponding to the source code, step 102 is executed.
Step 102: and configuring a data table corresponding to the character strings, and fields, comments and attributes of the fields in the data table, to which the character strings belong.
After the source code of the original file is subjected to macro expansion processing to obtain the expanded code corresponding to the source code, data packets corresponding to a plurality of character strings and fields of the plurality of character strings in the data table can be configured. Namely, the corresponding relation between a plurality of character strings and the data table is established, and the field of each character string in the data table is configured.
Meanwhile, the fields of the plurality of character strings in the data table, comments, attributes and other information can be configured.
After configuring the data table corresponding to the plurality of character strings, and the fields, comments and attributes thereof to which the plurality of character strings belong in the data table, step 103 is executed.
Step 103: and during operation, storing the functions, field representations, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model according to the extended functions, the field representations, the type representations, the attribute representations, the inter-table relations and the data structures after the character strings are optimized and the fields of the character strings in the data table, and generating SQL statements corresponding to the model so as to load the database object relational mapping model during data migration and realize data persistent storage.
After configuring the data tables corresponding to the plurality of character strings and the fields, comments and attributes of the fields, the relationships among the tables, the data structures and the like of the source codes can be stored in the memory, and a database object relational mapping model is established according to the extended functions, the field representations, the type representations, the attribute representations, the relationships among the tables, the data structures and the fields, to which the plurality of character strings belong, of the data tables optimized by the plurality of character strings, and SQL statements corresponding to the model are generated so as to load the database object relational mapping model during data migration and realize persistent data storage.
The above process may be as shown in fig. 3, and specifically may include the following steps:
1. macro-expanding may be performed on class members during pre-compilation;
2. removing the extended information;
3. after removing the extended information, acquiring the type and the member name;
4. removing redundant information (such as brackets and the like);
5. and ending the traversal of the class members.
When a member function is added, the following steps are executed:
1. acquiring a new member function;
2. carrying out macro expansion on the newly added class members in a pre-compiling period;
3. after macro expansion is carried out, a class member name character string is obtained;
4. acquiring a class member type character string;
5. expanding and searching whether a vector identifier exists or not;
6. if the corresponding vector identification exists, the vector capacity is obtained, and the class member byte offset is calculated according to the vector capacity;
7. if the corresponding vector identification does not exist, acquiring the number of type bytes, and calculating the offset of the class member bytes according to the number of the type bytes;
8. acquiring extension information such as comments, attributes and the like;
9. and storing the table structure data until the traversal of the class members is finished.
In this embodiment, after the database object relational mapping model is established, the data table operation may be performed according to the database object relational mapping model. The implementation can be described in detail in conjunction with the following specific implementation.
In a specific implementation manner of the present application, after the step 103, the method may further include:
step S1: and when loading database data, loading the database object relational mapping model and connecting the database.
In this embodiment, the process of loading database data can be described in detail in conjunction with fig. 2.
When loading database data, the database object relational mapping model can be loaded and connected with the database.
After loading the database object relational mapping model connection database, step S2 is performed.
Step S2: and acquiring a table structure stored in the database according to the table data loaded in the database.
After the database object relational mapping model is loaded and connected with the database, the table structure stored in the database can be obtained according to the table data loaded in the database.
And step S3: and applying for the memory with the size of the object.
When loading database data, an object-sized memory can be applied.
And step S4: and converting the table data into destination type data according to the field type of the table structure.
The table data may then be converted to destination type data based on the field type of the table structure.
Step S5: a field offset pointer is obtained.
Thereafter, a field offset pointer may be obtained.
Step S6: and copying the target type data to a memory space corresponding to the applied object size memory according to the field offset pointer.
After the field offset pointer is obtained, the target type data can be copied to a memory space corresponding to the applied object size memory according to the field offset pointer.
In a specific implementation, the mapping of the database data to the C + + class object may be: when the system is initialized, the database data needs to be loaded, and a general function of mapping the database table data to the C + + class object is very needed. Other implementations of C + + schemes are based on generating good code, requiring re-generation each time a modification or upgrade is made, requiring skill in the tools used to generate the code, and requiring checking that the resulting code is consistent with the design. Based on the result of precompilation analysis, the method stores the analyzed result in the runtime, and then performs field alignment mapping on the table structure in the runtime when the data is loaded, so as to achieve the mapping from the data to the C + + class object.
After the database object mapping model is built, table structure migration may be performed according to the database object mapping model. The implementation can be described in detail in conjunction with the following specific implementation.
In another specific implementation manner of the present application, after the step 103, the method may further include:
step N1: and loading the database object relational mapping model and connecting the database when the table structure is migrated.
In this embodiment, the implementation process for table structure migration may be as shown in fig. 4.
When the table structure is migrated, the database object relational mapping model can be loaded, the database is connected, and the database is logged in and configured.
And step N2: and acquiring the table structure information of the database and the database extension information.
After configuring the database, table structure information of the database, and database extension information may be obtained.
After the table structure information and the database extension information of the database are acquired, step N3 is performed.
And step N3: and generating a modified data table statement based on the database object relational mapping model, the table structure information and the database extension information, and executing the modified data table statement.
After the table structure information and the database extension information of the database are obtained, a modified data table statement may be generated based on the database object relational mapping model, the table structure information, and the database extension information, and the modified data table statement may be executed.
For table structure migration: the present application supports runtime table structure migration. After the table structure information is collected in the pre-compiling stage, the table structure is migrated when the system is started. And establishing connection with a database, obtaining table structure information of the current database and database extension information such as comments, a database storage engine and the like, comparing the table structure information with the latest table structure information collected in the compiling stage by field name, field type, field sequence and the like, generating a modified SQL statement, and executing.
In this embodiment, modifying the data table statement includes: at least one of a data addition statement, a data deletion statement, a data modification statement, and a data query statement.
The following detailed description of the data table operation is provided in connection with the accompanying drawings.
Referring to fig. 5, a schematic diagram of a data modification process provided in an embodiment of the present application is shown. The embodiment of the application supports runtime, and generates a dynamic SQL statement for data modification operation. And generating a preposed fixed statement after the table structure of the system is migrated according to the table structure information obtained in the pre-compiling stage, and when the data is changed and needs to be inserted, updated, replaced and the like, formatting the SQL statement on the data, splicing the SQL statement into a new SQL statement and sending the new SQL statement to a database for execution. And analyzing the database information of the response, and returning success or failure information.
As shown in fig. 5, the data modification process may include the following steps:
1. firstly, a database can be connected, and a configuration database is logged in;
2. reserving a maximum space of the SQL character string;
3. acquiring a character type;
4. acquiring field section number offset according to the acquired character type;
5. converting the table data into destination type data according to the field section number offset;
6. judging whether the target type data is a character type, if so, replacing the target type data with translatable characters, and splicing to obtain a field SQL; if not, formatting the target type data, and splicing to obtain a field SQL;
7. judging whether the class member traversal is finished, if not, continuing to perform traversal, and if so, splicing the preposed SQL (insertion, replacement, updating and the like);
8. and sending the response message to the database for execution and acquiring the response message returned by the database.
The operations for data query may be as described in connection with fig. 6.
The embodiment of the application supports the generation of dynamic SQL sentences for data query operation during operation. And generating a preposed fixed statement after the table structure of the system is migrated according to the table structure information obtained in the pre-compiling stage, and when data query is needed, carrying out SQL statement formatting on the condition data, splicing the condition data into a new SQL statement and sending the new SQL statement to a database for execution. And mapping the database data to the C + + class object according to the runtime table structure information for the responded database information, and returning query result information.
As shown in FIG. 6, the data query flow may include the following steps (the database object mapping model may be loaded at runtime):
1. connecting a database and logging in a configuration database;
2. sending and reading SQL, and reading returned row data;
3. acquiring a stored table structure according to the returned row data;
4. applying for the size of an object memory;
5. then, converting the query data into target type data according to the field type of the query data;
6. acquiring a field offset pointer, and copying query data to an object memory according to the field offset pointer;
7. and judging whether the class member traversal is finished, if so, returning the object, ending the traversal, returning a response message, and ending the query process.
The cloning procedure for the table structure can be as shown in FIG. 7.
The embodiment of the application can naturally support the cloning of the table structure in the runtime after the table structure in the runtime is migrated. And correspondingly modifying the table name of the table structure information stored in the running process, supporting modification of the table name into an autonomous key, and performing new table migration after copying the memory. And for the new table structure, automatically supporting the migration of the table structure and completing the cloning of the table structure in the operation process. As shown in fig. 7, the implementation flow may include the following steps:
1. when the table structure is migrated, a new table name may be configured;
2. configuring extended attributes and the like (self-adding attributes can be also realized), and cloning table objects;
3. modifying the expansion attribute of the clone table object and creating an old table object;
4. reading the field information of the old table of the database;
5. storing the read old table field information to a table object;
6. reading information such as a database extended information storage engine and comments, and storing the extended information into a table object;
7. generating an SQL type corresponding to the member type of the new table;
8. judging whether the old table member is not empty, if not, returning to create a new table SQL, and ending the flow; if yes, updating the extension information (such as information of a storage engine, comments and the like);
9. judging whether an old member exists, if so, creating a modified field SQL; if not, creating a newly added field SQL;
10. and judging whether the traversal of the class members is finished, if so, deleting the old table object, and finishing the process.
In this embodiment, mapping from the C + + class object to the JSON character string may also be implemented at runtime, specifically, data formatting is performed for the class object members, and JSON data mapping is completed. The flow is similar to runtime dynamic SQL function statements (insert, update, replace), except formatted in JSON strings, rather than in SQL.
As shown in fig. 8, the implementation process may specifically include the following steps:
1. reserving a JSON character string maximum space;
2. acquiring a corresponding field type, and acquiring the byte number offset of the field according to the field type;
3. converting the data into target type data according to the byte number offset of the field;
4. judging whether the target type data is a character string type, if not, formatting the target type data, and obtaining a spliced field according to the formatted target type data: the value JSON; if yes, obtaining a splicing field according to the target type data: the value JSON;
5. and judging whether the traversal process is finished or not, and if so, finishing the mapping process.
The embodiment of the application can also realize the mapping from the JSON character string to the C + + class object during the operation, specifically, the analyzed result can be pre-compiled, the analyzed result is stored during the operation, and then the field alignment mapping is carried out on the table structure during the operation when the JSON data is loaded, so that the mapping from the JSON data to the C + + class object is achieved. The process is similar to the run-time dynamic SQL function statement (query), and is different in that the JSON character string is traversed during formatting, and the corresponding field name is copied to the corresponding offset address and then converted into the object.
This implementation may be described as follows in conjunction with fig. 9. As shown in fig. 9, the mapping process may include the following steps:
1. reading a JSON character string, and analyzing the JSON character string into a kv character string map;
2. acquiring a stored table structure;
3. applying for an object size memory;
4. converting the JSON character string into target type data according to the field type of the JSON character string;
5. acquiring a field offset pointer according to the converted target type data;
6. copying the data to the object memory according to the field offset pointer;
7. and judging whether the class member traversal is finished or not, if so, returning the object, and finishing the mapping process.
According to the method and the device, the basic information and the extended information of the C + + class members are expanded in a pre-compiling mode, and the work of adding annotations, compiling special model analysis tools and code analysis tools, generating special codes and the like is omitted. And the same code completes table structure migration, cloning, reading and writing generation of SQL sentences, type reflection and the like. The method can be extended to other format data such as JSON data strings.
According to the method for establishing the database object relational mapping model, expanded codes corresponding to source codes are obtained by carrying out macro expansion processing on the source codes of original files, the expanded codes comprise data models consistent with the definition of the data structures of the original files and runtime functions for expanding the data structures, and the runtime functions comprise a plurality of different types of character strings for expanding the source codes so as to automatically generate corresponding SQL sentences when a program is started; configuring a data table corresponding to a plurality of character strings, and fields, comments and attributes of the fields in the data table, which the character strings belong to; and during operation, storing the functions, field representations, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model according to the extended functions, the field representations, the type representations, the attribute representations, the inter-table relations and the data structures after the character strings are optimized and the fields of the character strings in the data table, and generating SQL statements corresponding to the model so as to load the database object relational mapping model during data migration and realize data persistent storage. The embodiment of the application expands the object relation mapping of the precompiled reflection based on the C + + macro. The problems that reflection of different data types is difficult to complete in C + +, reflection from external data to memory objects cannot be completed in object relation, SQL needs a special tool to generate, and corresponding table building, data query and data update cannot be generated directly from the C + + type are solved, and SQL does not support automatic structure model migration and the like. And further, communication and risk costs in a development stage, an upgrade stage, an operation and maintenance stage and the like can be obviously saved.
Referring to fig. 10, a schematic structural diagram of a database object relational mapping model building apparatus provided in an embodiment of the present application is shown, and as shown in fig. 10, the database object relational mapping model building apparatus 1000 may include the following modules:
an expanded code obtaining module 1010, configured to perform macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, where the expanded code includes a data model consistent with a data structure definition of the original file and a runtime function expanding a data structure, and the runtime function includes multiple different types of character strings expanded for the source code, so as to automatically generate a corresponding SQL statement when a program is started;
a field configuration module 1020, configured to configure a data table corresponding to the plurality of character strings, and fields, comments and attributes of the fields in the data table, to which the plurality of character strings belong;
a mapping model establishing module 1030, configured to store the extended function, field representation, inter-table relationship, and data structure of the source code in a memory during operation, and establish a database object relationship mapping model according to the extended function, field representation, type representation, attribute representation, inter-table relationship, and data structure after the character strings are optimized, and the fields of the character strings in the data table, and generate an SQL statement corresponding to the model, so as to load the database object relationship mapping model during data migration, and implement persistent data storage.
Optionally, the apparatus further comprises:
and the comment attribute adding module is used for adding the comment and the attribute of the expanded code in the fixed format.
Optionally, the apparatus further comprises:
the first model loading module is used for loading the database object relational mapping model and connecting the database when loading database data;
the table structure acquisition module is used for acquiring the table structure stored in the database according to the table data loaded in the database;
the memory application module is used for applying for the memory with the size of the object;
the destination data conversion module is used for converting the table data into destination type data according to the field type of the table structure;
the offset pointer acquisition module is used for acquiring a field offset pointer;
and the destination data storage module is used for copying the destination type data to the memory space corresponding to the applied object size memory according to the field offset pointer.
Optionally, the apparatus further comprises:
the second model loading module is used for loading the database object relational mapping model and connecting the database when the table structure is migrated;
the extended information acquisition module is used for acquiring the table structure information of the database and the extended information of the database;
and the data table statement execution module is used for generating a statement for modifying the data table based on the database object relational mapping model, the table structure information and the database extension information and executing the statement for modifying the data table.
Optionally, the modifying the data table statement includes: at least one of a data add statement, a data delete statement, a data change statement, and a data query statement.
According to the method for establishing the database object relational mapping model, expanded codes corresponding to source codes are obtained by carrying out macro expansion processing on the source codes of original files, the expanded codes comprise data models consistent with the definition of the data structures of the original files and runtime functions for expanding the data structures, and the runtime functions comprise a plurality of different types of character strings for expanding the source codes so as to automatically generate corresponding SQL sentences when a program is started; configuring a data table corresponding to a plurality of character strings, and fields, comments and attributes of the fields in the data table, which the character strings belong to; and during operation, storing the functions, field representations, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model according to the extended functions, the field representations, the type representations, the attribute representations, the inter-table relations and the data structures after the character strings are optimized and the fields of the character strings in the data table, and generating SQL statements corresponding to the model so as to load the database object relational mapping model during data migration and realize data persistent storage. The embodiment of the application expands the object relation mapping of the precompiled reflection based on the C + + macro. The problems that reflection of different data types is difficult to complete in C + +, reflection from external data to memory objects cannot be completed in object relation, SQL needs a special tool to generate, and corresponding table building, data query and data update cannot be generated directly from the C + + type are solved, and SQL does not support automatic structure model migration and the like. And further, communication and risk costs in a development stage, an upgrade stage, an operation and maintenance stage and the like can be obviously saved.
Additionally, an embodiment of the present application further provides an electronic device, including: the system comprises a memory, a processor and a computer program which is stored on the memory and can run on the processor, wherein when the computer program is executed by the processor, the method for establishing the database object relational mapping model is realized.
Fig. 11 shows a schematic structural diagram of an electronic device 1100 according to an embodiment of the invention. As shown in fig. 11, electronic device 1100 includes a Central Processing Unit (CPU) 1101 that can perform various appropriate actions and processes in accordance with computer program instructions stored in a Read Only Memory (ROM) 1102 or loaded from a storage unit 1108 into a Random Access Memory (RAM) 1103. In the RAM1103, various programs and data necessary for the operation of the electronic device 1100 may also be stored. The CPU1101, ROM1102, and RAM1103 are connected to each other by a bus 1104. An input/output (I/O) interface 1105 is also connected to bus 1104.
A number of components in electronic device 1100 connect to I/O interface 1105, including: an input unit 1106 such as a keyboard, a mouse, a microphone, and the like; an output unit 1107 such as various types of displays, speakers, and the like; a storage unit 1108, such as a magnetic disk, optical disk, or the like; and a communication unit 1109 such as a network card, a modem, a wireless communication transceiver, and the like. The communication unit 1109 allows the electronic device 1100 to exchange information/data with other devices through a computer network such as the internet and/or various telecommunication networks.
The various processes and processes described above may be performed by processing unit 1101. For example, the methods of any of the embodiments described above may be implemented as a computer software program tangibly embodied on a computer-readable medium, such as storage unit 1108. In some embodiments, part or all of the computer program may be loaded and/or installed onto the electronic device 1100 via the ROM1102 and/or the communication unit 1109. When the computer program is loaded into the RAM1103 and executed by the CPU1101, one or more acts of the methods described above may be performed.
The embodiment of the present application further provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the computer program implements each process of the above-mentioned database object relational mapping model establishment method embodiment, and can achieve the same technical effect, and in order to avoid repetition, the details are not repeated here. The computer-readable storage medium may be a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising a component of' 8230; \8230;" does not exclude the presence of another like element in a process, method, article, or apparatus that comprises the element.
Through the above description of the embodiments, those skilled in the art will clearly understand that the method of the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but in many cases, the former is a better implementation manner. Based on such understanding, the technical solutions of the present application may be embodied in the form of a software product, which is stored in a storage medium (such as ROM/RAM, magnetic disk, optical disk) and includes instructions for enabling a terminal (such as a mobile phone, a computer, a server, an air conditioner, or a network device) to execute the method according to the embodiments of the present application.
While the present embodiments have been described with reference to the accompanying drawings, it is to be understood that the present embodiments are not limited to those precise embodiments, which are intended to be illustrative rather than restrictive, and that various changes and modifications may be effected therein by one skilled in the art without departing from the scope of the appended claims.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed in the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
It can be clearly understood by those skilled in the art that, for convenience and simplicity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one position, or may be distributed on multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit.
The functions may be stored in a computer-readable storage medium if they are implemented in the form of software functional units and sold or used as separate products. Based on such understanding, the technical solution of the present application or portions thereof that substantially contribute to the prior art may be embodied in the form of a software product stored in a storage medium and including instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present application. And the aforementioned storage medium includes: a U disk, a removable hard disk, a ROM, a RAM, a magnetic disk or an optical disk, and various media capable of storing program codes.
The above description is only for the specific embodiments of the present application, but the scope of the present application is not limited thereto, and any person skilled in the art can easily think of the changes or substitutions within the technical scope of the present application, and shall be covered by the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims (10)

1. A database object relational mapping model building method is characterized by comprising the following steps:
carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded to the source code so as to automatically generate a corresponding SQL statement when a program is started;
configuring a data table corresponding to a plurality of character strings, and fields, comments and attributes of the fields in the data table, which the character strings belong to;
and during operation, storing the functions, field representation, inter-table relations and data structures of the source code extension into a memory, and establishing a database object relational mapping model and SQL sentences corresponding to the generation model according to the extension functions, the field representation, the type representation, the attribute representation, the inter-table relations and the data structures of the optimized character strings and the fields of the character strings in the data table so as to load the database object relational mapping model during data migration and realize data persistent storage.
2. The method of claim 1, wherein after the macro expanding processing is performed on the source code of the original file to obtain an expanded code corresponding to the source code, the method further comprises:
adding its annotations and attributes within the fixed format of the expanded code.
3. The method according to claim 1, further comprising, after the building a database object relational mapping model according to the optimized extension functions, field representations, type representations, attribute representations, relationships between tables, and data structures of the plurality of character strings and the fields of the plurality of character strings in the data tables, and the generating SQL statements corresponding to the model, the method further comprising:
when loading database data, loading the database object relational mapping model and connecting the database;
acquiring a table structure stored in the database according to the table data loaded in the database;
applying for an object size memory;
converting the table data into target type data according to the field type of the table structure;
acquiring a field offset pointer;
and copying the target type data to a memory space corresponding to the applied object size memory according to the field offset pointer.
4. The method according to claim 2, further comprising, after said building a database object relational mapping model according to a plurality of optimized extension functions of said character strings, field representations, type representations, attribute representations, relationships between tables, data structures, and fields of said character strings in said data tables, and generating SQL statements corresponding to the model, the steps of:
loading the database object relational mapping model and connecting a database when the table structure is migrated;
acquiring table structure information of the database and database extension information;
and generating a modified data table statement based on the database object relational mapping model, the table structure information and the database extension information, and executing the modified data table statement.
5. The method of claim 4, wherein modifying the spreadsheet statement comprises: at least one of a data addition statement, a data deletion statement, a data modification statement, and a data query statement.
6. An apparatus for building a database object relational mapping model, the apparatus comprising:
the expanded code acquisition module is used for carrying out macro expansion processing on a source code of an original file to obtain an expanded code corresponding to the source code, wherein the expanded code comprises a data model consistent with the definition of a data structure of the original file and a runtime function for expanding the data structure, and the runtime function comprises a plurality of different types of character strings expanded on the source code so as to automatically generate a corresponding SQL statement when a program is started;
the field configuration module is used for configuring data tables corresponding to the character strings, fields of the character strings in the data tables, comments and attributes of the fields, and data tables corresponding to the character strings and fields of the character strings in the data tables;
and the mapping model establishing module is used for storing the extended functions, field representation, inter-table relation and data structures of the source codes in a memory during operation, establishing a database object relational mapping model according to the extended functions, the field representation, the type representation, the attribute representation, the inter-table relation and the data structures of the optimized character strings and the fields of the character strings in the data table, and generating SQL sentences corresponding to the model so as to load the database object relational mapping model during data migration and realize persistent data storage.
7. The apparatus of claim 6, further comprising:
and the comment attribute adding module is used for adding the comment and the attribute of the expanded code in the fixed format.
8. The apparatus of claim 6, further comprising:
the first model loading module is used for loading the database object relational mapping model when loading database data and connecting the database;
the table structure acquisition module is used for acquiring the table structure stored in the database according to the table data loaded in the database;
the memory application module is used for applying for the memory with the size of the object;
the destination data conversion module is used for converting the table data into destination type data according to the field type of the table structure;
the offset pointer acquisition module is used for acquiring a field offset pointer;
and the destination data storage module is used for copying the destination type data to the memory space corresponding to the applied object size memory according to the field offset pointer.
9. An electronic device, comprising:
a memory, a processor and a computer program stored on the memory and executable on the processor, the computer program, when executed by the processor, implementing the database object relational mapping model building method according to any one of claims 1 to 5.
10. A readable storage medium, wherein instructions in the storage medium, when executed by a processor of an electronic device, enable the electronic device to perform the database object relational mapping model establishment method according to any one of claims 1 to 5.
CN202211387932.7A 2022-11-03 2022-11-03 Database object relational mapping model establishing method, device, equipment and medium Pending CN115756634A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211387932.7A CN115756634A (en) 2022-11-03 2022-11-03 Database object relational mapping model establishing method, device, equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211387932.7A CN115756634A (en) 2022-11-03 2022-11-03 Database object relational mapping model establishing method, device, equipment and medium

Publications (1)

Publication Number Publication Date
CN115756634A true CN115756634A (en) 2023-03-07

Family

ID=85357253

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211387932.7A Pending CN115756634A (en) 2022-11-03 2022-11-03 Database object relational mapping model establishing method, device, equipment and medium

Country Status (1)

Country Link
CN (1) CN115756634A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115994205A (en) * 2023-03-22 2023-04-21 广东粤港澳大湾区国家纳米科技创新研究院 Self-assembly attribute extraction method and device
CN117112538A (en) * 2023-09-05 2023-11-24 中电金信软件有限公司 Database operation method, database operation device, computer equipment and storage medium

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115994205A (en) * 2023-03-22 2023-04-21 广东粤港澳大湾区国家纳米科技创新研究院 Self-assembly attribute extraction method and device
CN115994205B (en) * 2023-03-22 2023-06-02 广东粤港澳大湾区国家纳米科技创新研究院 Self-assembly attribute extraction method and device
CN117112538A (en) * 2023-09-05 2023-11-24 中电金信软件有限公司 Database operation method, database operation device, computer equipment and storage medium

Similar Documents

Publication Publication Date Title
CN115756634A (en) Database object relational mapping model establishing method, device, equipment and medium
CN103019928B (en) A kind of automated testing method and system
CN106598612B (en) Operation method and system for data table in database
CN101174240A (en) Software testing management method and system thereof
CN111722839A (en) Code generation method and device, electronic equipment and storage medium
CN112130891B (en) Method and equipment for continuously deploying database
CN111125064B (en) Method and device for generating database schema definition statement
CN106095501A (en) A kind of application section arranging method and device
CN114780109B (en) Python project third-party library dependent automatic analysis and installation method
CN111367893A (en) Method and device for database version iteration
US20060136471A1 (en) Differential management of database schema changes
CN102521008A (en) Program compiling device and method for compiling program
CN113127357B (en) Unit test method, apparatus, device, storage medium, and program product
US20200097260A1 (en) Software application developer tools platform
CN110058849A (en) Generation method, device, computer equipment and the storage medium of flow chart
CN108694172B (en) Information output method and device
JP7318704B2 (en) Test equipment, test method and program
CN116414774A (en) File migration method, device, equipment and storage medium
CN110321138A (en) A kind of program updates, moving method and device
CN114416099A (en) Model integration method based on information physical system and related components
CN113448852A (en) Test case obtaining method and device, electronic equipment and storage medium
CN113806327A (en) Database design method and device and related equipment
Burd et al. Enriching program comprehension for software reuse
CN114089976B (en) Method, apparatus, and medium for generating database operation statements
Aboulsamh et al. A formal modeling approach to information systems evolution and data migration

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
CB02 Change of applicant information

Address after: 9th Floor, Block A, Shenzhen National Engineering Laboratory Building, No. 20, Gaoxin South 7th Road, High-tech Zone Community, Yuehai Street, Nanshan District, Shenzhen, Guangdong 518000

Applicant after: Shenzhen Huayun Information System Technology Co.,Ltd.

Address before: 9th Floor, Block A, Shenzhen National Engineering Laboratory Building, No. 20, Gaoxin South 7th Road, High-tech Zone Community, Yuehai Street, Nanshan District, Shenzhen, Guangdong 518000

Applicant before: Shenzhen Huayun Information System Co.,Ltd.

CB02 Change of applicant information