Disclosure of Invention
Embodiments of the present invention provide a method, apparatus, device, and medium for automatic mapping of a database based on structure serialization and deserialization, thereby overcoming one or more of the problems due to the limitations and disadvantages of the related art to at least a certain extent.
According to one aspect of the present disclosure, there is provided a database auto-mapping method based on structure serialization and anti-serialization, including:
According to the C++ language environment, a serialization reload function is generated by defining a serialization operator of the data structure type;
according to the C++ language environment, a deserialization reload function is generated by defining a deserialization operator of the data structure type;
Defining the function of the query template through a template programming technology according to the serialization reload function and the anti-serialization reload function to generate a query template function;
And according to the query template function, generating a database query result by performing splicing query on metadata of the query structure, and performing assignment of the structure field based on the database query result to realize automatic mapping of the database query structure.
In one exemplary embodiment of the present disclosure, defining a serialization operator for a data structure type in a programming language includes:
Recording metadata of the data structure body through a serialization technology based on a C++ language environment to generate metadata information;
Based on the metadata information, a serialized override function is generated by defining a serialized operator for a data structure type in a programming language.
In one exemplary embodiment of the present disclosure, by defining the deserialization operator for the underlying data type, it comprises:
Based on the C++ language environment, analyzing the given character string by a deserialization technology to generate analysis data information;
based on the parsed data information, a deserialized reload function is generated by defining a deserialized operator of a data structure type in a programming language.
In one exemplary embodiment of the present disclosure, defining the function of the query template by a template programming technique includes:
based on the serialization reload function, carrying out serialization definition on the query structure in the database through a template programming technology to generate serialization parameters;
Based on the inverse serialization reload function, performing inverse serialization definition on the query structure in the database through a template programming technology to generate an inverse serialization parameter;
based on the sub-variable quantity parameters of the database, the field quantity parameters are generated by defining the field quantity of the query structure body in the database;
based on the sub-variable sequence parameters of the database, the field sequence parameters are generated by defining the field sequence of the query structure in the database;
And generating a library table query structure body by integrating the serialization parameter, the deserialization parameter, the field number parameter and the field sequence parameter.
In one exemplary embodiment of the present disclosure, defining the function of the query template by a template programming technique includes:
based on the field quantity parameter and the field sequence parameter, acquiring the query condition of the database query instruction through the serialization parameter, and completing the construction of the search function;
The query result is inversely sequenced into a given type of structure body through the inverse serialization parameters, and the construction of an assignment function is completed;
And integrating the search function and the assignment function to generate a query template function.
In one exemplary embodiment of the present disclosure, by performing a splice query on metadata of a query structure, comprising:
acquiring metadata of the query structure body through the query template function based on a preset query structure body type, and generating the metadata of the query structure body;
Generating a query statement by splicing the structured query language statement based on the query structure metadata;
and generating a database query result by querying the structural body in the database based on the query statement.
In an exemplary embodiment of the present disclosure, assignment of a structure field based on the database query result includes:
Generating a query structure subvariable by performing deserialization assignment on the queried data stream based on the database query result;
generating a dynamic array list by storing the query structure sub-variables in a list;
In one aspect of the present disclosure, there is provided an automatic mapping apparatus for a database based on structure serialization and anti-serialization, including:
The serialization reload function construction module is used for generating a serialization reload function by defining a serialization operator of the type of the data structure body in a C++ language environment;
The anti-serialization reload function construction module is used for generating an anti-serialization reload function by defining an anti-serialization operator of the type of the data structure body in a C++ language environment;
The query template function construction module is used for defining the function of the query template through a template programming technology according to the serialization reload function and the anti-serialization reload function to generate a query template function;
And the database execution module is used for generating a database query result by splicing and querying the metadata of the query structure according to the query template function, and carrying out assignment of the structure field based on the database query result so as to realize automatic mapping of the database query structure.
In one aspect of the present disclosure, there is provided an electronic device comprising:
Processor, and
A memory having stored thereon computer readable instructions which, when executed by the processor, implement a method according to any of the above.
In one aspect of the present disclosure, a computer readable storage medium is provided, on which a computer program is stored, which when executed by a processor, implements a method according to any of the above.
The beneficial effects brought by the invention are as follows:
From the above scheme, the embodiment of the invention provides an automatic mapping method for a database based on structural body serialization and deserialization. The method comprises the steps of firstly, defining a serialization operator and an deserialization operator of a data structure type in a C++ language environment to generate a serialization reload function and an deserialization reload function. And defining the function of the query template by a template programming technology based on the serialization reload function and the anti-serialization reload function to generate the query template function. Finally, based on the query template function, the metadata of the query structure body is subjected to splicing query to generate a database query result, and assignment of the structure body field is performed according to the database query result, so that automatic mapping of the database query structure body is realized. Therefore, the embodiment of the disclosure provides a scheme for automatically synthesizing SQL sentences and automatically filling and assigning query results of a dream database query structure oriented to a C++ programming language by using a template programming technology, a structure serialization technology and a reverse serialization technology. The synthesis of the automatic database statement and the mapping of the query result are realized, meanwhile, the reliability and the reusability of codes are ensured, and the application range of C++ is improved.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
The technical scheme of the present disclosure is described in further detail below through the accompanying drawings and examples.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the present invention more apparent, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are some embodiments of the present invention, but not all embodiments of the present invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the invention.
[ PREPARATION ] A method for producing a polypeptide
SQL, structured Query Language, structured query language, is a standard programming language for managing relational databases. Its main functions are to query, insert, update and delete data in the database, and also to create, modify database structures and control access rights for the database.
The c++ programming language, a general-purpose, efficient programming language, was developed by Bjarne Stroustrup in the early 1980 s. It was developed based on the C language, incorporating the feature of Object Oriented Programming (OOP).
The template < class AR > AR & serialize (AR & AR) { } function, which is a written method of a template function, is widely used in a serialization framework in c++, and can enable data storage and reading between different types of data objects (e.g., instances of classes) and different types of streams (e.g., output streams, input streams, or other data formats).
STRINGSTREAM, in C++, is a standard library class belonging to the < sstream > header file for processing the character stream. It may write data to the string or read data from the string.
In the embodiment of the disclosure, a database automatic mapping method based on structure serialization and inverse serialization is provided first, referring to fig. 1, the database automatic mapping method based on structure serialization and inverse serialization may include the following steps:
step S110, according to the C++ language environment, a serialization reload function is generated by defining a serialization operator of the data structure type;
Step S120, according to the C++ language environment, generating an anti-serialization reload function by defining an anti-serialization operator of the data structure type;
Step S130, defining the function of the query template through a template programming technology according to the serialization reload function and the anti-serialization reload function to generate a query template function;
And step 140, according to the query template function, generating a database query result by performing splicing query on metadata of the query structure, and performing assignment of the structure field based on the database query result to realize automatic mapping of the database query structure.
A method for automatically mapping a database based on structure serialization and deserialization in an embodiment of the present disclosure will be further described below.
In step S110, a serialized override function may be generated by defining a serialized operator for a data structure type according to the c++ language environment.
In some alternative embodiments of the present example, serialization of basic data structure types common in the c++11 programming language is implemented in functions by defining serialization functions, with the purpose of adding a sequence override function defining serialization operators for all common data types, in which the type and name information of variables are recorded, which play a role in indexing values during SQL statement synthesis and query result assignment.
In particular, a serialization function may be defined for common underlying data types (e.g., uint32_t, int32_t, uint64_t, int64_ t, float, double, etc.) in the buffer class first. Each data type has a corresponding serialized reload function that implements the output stream of writing data to the buffer class. The serialization function is not only responsible for converting the data into a format suitable for storage, but also records the type and name information of each field, which is critical in SQL statement synthesis and query result mapping.
Preferably, the serialization function of the buffer class can be further expanded on the basis of implementing the serialization of the basic data types, so as to support more complex data structures, such as std:: list, std:: map, array and the like. For these container types, elements therein need to be traversed and separately serialized, with each element serialized following a similar flow as the underlying data type. In addition, in order to support structure serialization, a template serialization function can be added in the buffer class to process the serialization of the structure members one by one, so that the complex types can be stored and restored as required.
In addition, the buffer class provides the function of converting the format of the serialized data for convenient debugging and output, which allows the result to be more clear and easier to read. And, to support readability and formatting, the buffer class also supports adding indentation characters in the output, making the generated serialized data structure clearer.
In step S120, an deserialized reload function may be generated by defining deserialized operators of the data structure type according to the c++ language environment.
In some alternative embodiments of the present example, an inverse serialization override function of a common data structure type in the c++11 standard is defined to support parsing of a given string, and mapping assignment of query result fields is performed on parsed data information, so as to provide the underlying indexing and positioning assignment functions for automatic mapping assignment of query results required later.
In one specific example, data is parsed from a given string by a parse class and is de-serialized into a structure or other data type. When the constructor accepts a string (query result) and an integer (input encoding type) as input parameters. The inside of the parse class maintains a plurality of private members such as m_ sName (for storing the structure name), m_ oPath (for storing the path information, for locating the field), m_ sErrMsg (error information), m_ oJsonValue (for storing JSON parse results), etc. By initializing these members, the parse class can effectively handle error handling and path parsing problems in the de-serialization process.
At the same time, the parse class provides a series of heavy-duty deserialized operators in order to deserialize the query results into the underlying data types in the structure. Each base type (e.g., uint32_t, int32_t, uint64_t, int64_ t, float, double, etc.) requires a corresponding de-serialized reload function. For example, by parsing the data in the string to assign values to the corresponding fields and to handle possible coding or type conversion errors. For each data type, the inverse serialization function extracts the corresponding value and maps it into a member variable of the structure. In addition, the mapping between the field name of the query result and the field of the structure is also considered, so that the data can be correctly assigned to the corresponding position of the structure. Error information and debug information are recorded in m_ sErrMsg to help locate problems in deserialization.
Preferably, the parsing class also supports the deserialization of more complex data types (such as std:: list, std:: map, std:: vector, etc.) on the basis of implementing the deserialization of the underlying data types. For container types, it is also necessary to traverse each element in the container and de-serialize each element into a corresponding data type. In particular, for std:map, it is necessary to deserialize each key-value pair and ensure that the type of key and value is consistent with the type of the target structure field.
Through the above serialization and deserialization definition of the common data structure types, the field names and field type information in the C++ structure can be expanded, so that the external function can acquire the information.
In step S130, a function of the query template may be defined by a template programming technique according to the serialized reload function and the anti-serialized reload function, so as to generate a query template function.
In some alternative embodiments of the present example, it may also be desirable to define serialization and de-serialization functions for a particular library table query structure. And ensuring that the number and sequence of the added fields in the database table query structure and the sub-variables in the database table with query are in one-to-one correspondence.
In one specific example, this is achieved by a template < classAR > AR & serialize (AR & AR) { } function. First, we define the NAME of the library table query structure, e.g., stuctTrainxCateInfo, which contains a number of fields, such as category ID (CATE _ID), category NAME (CATE _NAME), PARENT ID (part_ID), category hierarchy (CATE _level), and ADD TIME (ADD_TIME). In this architecture we use a template function named serialize that supports the serialization and deserialization of different types of data by templating.
And carrying out serialization definition on the fields of the library table query structure body by serialize functions to generate serialization parameters. And sending each field into a serializer ar for processing by calling the macro, and finally returning the processed object by the serializer to finish deserialization definition and generate deserialization parameters. These fields include different types of data (e.g., integer type, string type) and the serializer will automatically convert to the appropriate format depending on the type of field. Thus, the universal support for fields of various types is realized, and meanwhile, the simplicity and the expandability of codes are ensured.
Meanwhile, the number and sequence of the added fields in the database table query structure must be in one-to-one correspondence with the sub-variables in the database table to be queried. This means that we need to define the number of field parameters and the sequence of field parameters in the database table query structure according to the number of sub-variables information and the sequence of sub-variables information in the database table to be queried. And a change in the number and order of fields during serialization may result in a failure of deserialization. Thus, when adding new fields, it is necessary to ensure that the order of the new fields is uniform with the existing fields and that the expressions in the serialization functions are able to handle the new field types correctly.
In some alternative embodiments of the present example, the query template function may be split into two parts, namely a find function and a assign function. Firstly, according to the field quantity parameter and the field sequence parameter of the library table query structure body, obtaining the query condition. And then, executing SQL query according to query conditions through the serialization parameters by utilizing a template programming technology, if the query is successful, entering a processing stage of a query result, otherwise, recording errors and returning failure. Thus, the construction of the search function is completed.
After entering the processing stage of the query result, the query result is deserialized and assigned into a structure of a given type through deserialization parameters by using a template programming technology, and the returned record number (record_number) is updated. When the whole query process is finished, the function closes the database handle and releases the resource, and the construction of the assignment function is completed.
In step S140, according to the query template function, a database query result is generated by performing a splice query on metadata of the query structure, and assignment of a structure field is performed based on the database query result, so as to implement automatic mapping on the database query structure.
In some alternative embodiments of the present example, first, the SQL string array is initialized to all zeros using the lookup function described above. A STRINGSTREAM object is created again to dynamically construct SQL query statements. If the table name is empty, the lookup function records errors and returns a query field, and if the table name is not empty, the types and names of all variables in the query structure are acquired, and the metadata of the query structure is generated. And splicing the acquired query structure body metadata into a query part in the SQL sentence through a reflection mechanism.
The lookup function then builds other portions of the SQL query based on the incoming conditions.
(1) If the condition parameters are not null, the condition clauses are needed to be added and spliced into the SQL sentences for filtering the query results.
(2) If the ranking parameters are entered, ranking clauses are also added to specify the ranking rules for the query results.
(3) The lookup function examines the constraint parameters, if not null, concatenates the constraint clauses, specifying the returned record range. If the limit is empty, the limit default value of 0,20 is used, indicating the first 20 records to query.
Finally, the query statement generated by splicing all conditions comprises a search statement, a query condition, a sorting condition, a limiting condition and the like, so that the query can be executed according to the designated condition, and the desired database query result is obtained.
In some optional embodiments of the present example, according to the database query result, automatic mapping of the database query structure is achieved by performing deserialization assignment on the queried data stream, and performing list storage on the query structure subvariables obtained by the assignment.
In one specific example, first, the assignment function uses "rolling acquisitions" to get the next row of data for the database query results. If the operation to acquire the data fails, an error message is output and the error code is checked, when there is no more data available to acquire, the flag is set to true and the loop is skipped. If the query is successful and data is returned, the query result is processed continuously and is ready for the deserialization assignment operation. The key to this section is to capture and handle the failure of the database query, ensuring that the program can properly handle the exception and end conditions when executing the query.
After the data is successfully obtained by the query, the program constructs a character string for deserialization, and converts the query result into the character string which accords with the format required by the structural body.
Finally, the program parses and deserializes it using the parse class. In the parsing and deserializing process, each line of data is processed, the program will increment a record number indicating the number of records successfully processed. This ensures that all query results are stored in a list in order.
By the technical scheme, the public query template function can be called as long as the specific serialization and inverse serialization functions are added to any structure body, automatic combination of SQL sentences and automatic assignment of query results are completed, and automatic mapping of the database query structure body is realized.
It should be noted that although the steps of the methods of the present disclosure are illustrated in a particular order in the figures, this does not require or imply that the steps must be performed in that particular order or that all of the illustrated steps must be performed in order to achieve desirable results. Additionally or alternatively, certain steps may be omitted, multiple steps combined into one step to perform, and/or one step decomposed into multiple steps to perform, etc.
In addition, in the present exemplary embodiment, a database automatic mapping apparatus based on structure serialization and deserialization is also provided. Referring to fig. 2, the database auto-mapping apparatus 200 based on structure serialization and deserialization may include a serialization override function construction module 210, an deserialization override function construction module 220, a query template function construction module 230, and a database execution module 340. Wherein:
a serialized override function construction module 210, configured to generate a serialized override function by defining a serialized operator of a data structure type in a c++ language environment;
The deserialization reload function construction module 220 is configured to generate a deserialization reload function by defining a deserialization operator of the data structure type in the c++ language environment;
The query template function construction module 230 is configured to define a function of a query template by a template programming technique according to the serialized reload function and the anti-serialized reload function, so as to generate a query template function;
The database execution module 340 is configured to generate a database query result by performing a splice query on metadata of a query structure according to the query template function, and perform assignment of a structure field based on the database query result, so as to implement automatic mapping on the database query structure.
The database automatic mapping device based on structure body serialization and inverse serialization in the embodiment of the present disclosure corresponds to the embodiment of the database automatic mapping method based on structure body serialization and inverse serialization in the present disclosure, and the relevant contents may be referred to each other and will not be described herein. The beneficial technical effects corresponding to the database automatic mapping device based on structure serialization and inverse serialization in the embodiments of the present disclosure may refer to the corresponding beneficial technical effects of the corresponding exemplary method section, and are not described herein.
It should be noted that although several modules or units of the structure-based serialization and de-serialization based database auto-mapping apparatus 300 are mentioned in the detailed description above, such partitioning is not mandatory. Indeed, the features and functionality of two or more modules or units described above may be embodied in one module or unit in accordance with embodiments of the present disclosure. Conversely, the features and functions of one module or unit described above may be further divided into a plurality of modules or units to be embodied.
Next, an electronic device according to an embodiment of the present disclosure is described with reference to fig. 3. The electronic device may be either or both of the first device and the second device, or a stand-alone device independent thereof, which may communicate with the first device and the second device to receive the acquired input signals therefrom.
Fig. 3 illustrates a block diagram of an electronic device according to an embodiment of the present disclosure.
As shown in fig. 3, the electronic device includes one or more processors and memory.
The processor may be a Central Processing Unit (CPU) or other form of processing unit having data processing and/or instruction execution capabilities, and may control other components in the electronic device to perform the desired functions.
The memory may store one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and/or nonvolatile memory. The volatile memory may include, for example, random Access Memory (RAM) and/or cache memory (cache), and the like. The non-volatile memory may include, for example, read Only Memory (ROM), hard disk, flash memory, and the like. One or more computer program products may be stored on the computer readable storage medium that can be run by a processor to implement the various embodiments methods of the present disclosure and/or other desired functions as described above.
In one example, the electronic device may also include an input device and an output device, which are interconnected by a bus system and/or other form of connection mechanism (not shown).
In addition, the input device may include, for example, a keyboard, a mouse, and the like.
The output device may output various information including the determined distance information, direction information, etc., to the outside. The output device may include, for example, a display, speakers, a printer, and a communication network and remote output devices connected thereto, etc.
Of course, only some of the components of the electronic device relevant to the present disclosure are shown in fig. 3 for simplicity, components such as buses, input/output interfaces, etc. being omitted. In addition, the electronic device may include any other suitable components depending on the particular application.
In addition to the methods and apparatus described above, embodiments of the present disclosure may also be a computer program product comprising computer program instructions which, when executed by a processor, cause the processor to perform steps in a method according to various embodiments of the present disclosure described in the above section of the specification.
The computer program product may write program code for performing the operations of embodiments of the present disclosure in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device, partly on a remote computing device, or entirely on the remote computing device or server.
Furthermore, embodiments of the present disclosure may also be a computer-readable storage medium, having stored thereon computer program instructions, which when executed by a processor, cause the processor to perform steps in a method according to various embodiments of the present disclosure described in the above section of the present disclosure.
The computer readable storage medium may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may include, for example, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of a readable storage medium include an electrical connection having one or more wires, a portable disk, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The foregoing is a preferred embodiment of the present invention and it should be noted that modifications and adaptations to those skilled in the art may be made without departing from the principles of the present invention and are intended to be comprehended within the scope of the present invention.