Query object mapping method, device, and product

By defining query objects and using field mapping methods and logical AND operators to combine query clauses, the problems of large coding volume and high debugging difficulty in existing ORM technologies are solved, and efficient database access is achieved.

WO2025214037A1PCT designated stage Publication Date: 2025-10-16XIANGYANG DOYTOWIN SOFTWARE TECHNOLOGY CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2025/081757
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2024-04-11
Filing Date
2025-03-11
Publication Date
2025-10-16

AI Technical Summary

Technical Problem

Existing ORM technologies require users to write their own code when constructing database query clauses, resulting in a large amount of coding, high debugging difficulty, and long development time.

Method used

By defining the concept of query objects, query conditions are centrally defined in the form of fields within the query objects, and appropriate field mapping methods and logical AND operators are used to combine them into query clauses, simplifying the database access process.

Benefits of technology

It simplifies the writing of database access-related code, improves development efficiency, and reduces debugging difficulty and code volume.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2025081757_16102025_PF_FP_ABST
    Figure CN2025081757_16102025_PF_FP_ABST
Patent Text Reader

Abstract

The present invention relates to the technical field of database access. Disclosed are a query object mapping method, a device, and a product. The method comprises: using a matching field mapping method to obtain query conditions corresponding to assigned fields in instances of a query object; and using logical operators to combine the query conditions into query clauses. Then, a complete database query statement is constructed on the basis of the query clauses to complete database access. Compared with the ORM technology and the concept of an entity object in the prior art, the present invention newly defines the concept of a query object, and provides a query object mapping method used for mapping instances of a query object into query clauses, so that the code writing related to database access is simplified, thereby improving the development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Query object mapping method, device and product

[0001] The present application claims priority to the Chinese patent application No. 202410437379.6, filed on April 11, 2024, and entitled "Query object mapping method, device and product", the whole content of which is incorporated herein by reference. TECHNICAL FIELD

[0002] The present application relates to the technical field of database access, in particular to a query object mapping method, device and product. BACKGROUND

[0003] When developing software using ORM technology, a database query statement needs to be constructed according to the query parameters submitted by the user and the information of the target database table to complete the access to the database. One of the key steps is to construct the query clause in the database query statement from the query parameters. Each ORM framework provides different implementation methods, including but not limited to writing SQL statements by the user, such as MyBatis, SpringJdbc, etc., or using class SQL technology, such as HQL in Hibernate, JPQL in SpringDataJPA, LINQ in C# and the like, or using some construction for conversion, such as the Criteria-related interface of Hibernate, the Specification interface of SpringDataJPA, etc., or using a chain construction method, such as jOOQ, etc. The reason for this phenomenon is that the object concept in the ORM technology only includes entity objects, which cannot cover the construction of the query clause, so each ORM framework has proposed some solutions. However, these solutions require the user to write code to construct the query clause, which not only increases the amount of coding, but also increases the difficulty of debugging and the development time. Therefore, how to efficiently construct the query clause has become a problem to be solved. SUMMARY

[0004] In a first aspect, the present application provides a query object mapping method, comprising:

[0005] By applying an adapted field mapping method, the query conditions corresponding to the assigned fields in the instance of the query object are obtained; the field mapping method is used to map the fields in the query object that meet a specific feature to the corresponding query conditions;

[0006] Using a logical AND operator, the query conditions are combined into a query clause.

[0007] In a second aspect, the present application provides a query object mapping method, comprising:

[0008] The field mapping method is adapted and applied to all fields of the query object to obtain corresponding query conditions, and a query object mapping code is generated; the query object mapping code is called using an instance of the corresponding query object, and after execution, corresponding query conditions of each assigned field in the instance of the query object are obtained; the query conditions can be combined into a query clause using a logical AND operator.

[0009] In a third aspect, the application provides a data access device, comprising: an initialization module configured to obtain a table name of a target database table and each column name according to a definition of an entity object; a database query statement mapping module configured to map an instance of a query object into a query clause based on the query object mapping method, and splice the query clause with the table name of the target database table and each column name to obtain a database query statement; and a database query statement execution module configured to submit the database query statement to a database for execution, and return a database execution result after processing.

[0010] In a fourth aspect, the application provides a computer program product, comprising a computer program which, when executed by a processor, implements the steps of the query object mapping method of the first aspect.

[0011] According to the embodiments of the application, the following technical effects are achieved:

[0012] The application provides a query object mapping method, device and product, and relates to the technical field of database access. The method comprises the following steps: obtaining corresponding query conditions of each assigned field in an instance of a query object by applying an adapted field mapping method; and combining each query condition into a query clause using a logical AND operator. A complete database query statement is constructed based on the query clause, and then the access to the database is completed. Compared with the ORM technology and the concept of entity object in the prior art, the application newly defines the concept of query object, and provides a query object mapping method for mapping an instance of a query object into a query clause, thereby simplifying the code writing related to the database access and improving the development efficiency. BRIEF DESCRIPTION OF DRAWINGS

[0013] In order to more clearly illustrate the technical solutions of the embodiments of the application or the prior art, the following will briefly introduce the drawings needed in the embodiments. Obviously, the drawings in the following description are only some embodiments of the application, and for those skilled in the art, other drawings can also be obtained without creative labor based on these drawings.

[0014] Fig. 1 is a flowchart of mapping an instance of a query object into a query clause according to an embodiment of the application;

[0015] Fig. 2 is a technical concept diagram of mapping an instance of a query object to a query clause according to an embodiment of the present application;

[0016] Fig. 3 is a flow chart of obtaining the assignment of a field in the instance of the query object by using reflection technology, constructing a corresponding query condition for the assigned field, and combining the query conditions to form a query clause according to an embodiment of the present application;

[0017] Fig. 4 is a flow chart of the calling of the modules of the data access device according to an embodiment of the present application;

[0018] Fig. 5 is a diagram of the combination of a development language and a database according to embodiments 5-8 of the present application. DETAILED DESCRIPTION

[0019] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all the other embodiments obtained by those skilled in the art without creative efforts belong to the scope of the present application.

[0020] The object of the present application is to provide a query object mapping method, device and product, which constructs a query clause by using a query object. Compared with the widely used ORM technology and the concept of entity object in the prior art, the present application newly defines a concept of query object, which is used to declare the fields that can be constructed as query conditions of a corresponding database table, and defines the query conditions of each database table in the form of fields in the corresponding query object. The present application provides a query object mapping method, which maps an instance of a query object created by a user to a query clause, and then obtains a complete database query statement. Based on the query object mapping method in the present application, the user only needs to write the code for defining the query object, without writing the code for constructing the query clause. The instance of the constructed query object calls the query object mapping method encapsulated in the data access device to obtain the query clause corresponding to the instance of the query object, and then the table name and the column name of the database table are spliced to perform the database access. The computer only needs to automatically generate the query clause according to the code of the query object written by the user according to the steps of the query object mapping method in the present application, so as to simplify the writing of the database access code and improve the development efficiency.

[0021] In order to make the above-mentioned objects, features and advantages of the present application more apparent and easy to understand, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0022] Embodiment 1

[0023] As shown in Fig. 1 and Fig. 2, the present application provides a query object mapping method, which comprises:

[0024] Step 100: obtaining the query condition corresponding to each assigned field in the instance of the query object by applying the adapted field mapping method; the field mapping method is used to map the field in the query object meeting a specific feature to the corresponding query condition.

[0025] The concept of the query object is a new definition of the application for mapping the query clause, and the query condition corresponding to each database table is defined in the form of field in the corresponding query object. The query object is mainly used for mapping the query clause; the query object includes the definition of the query object and the instance of the query object; the definition of the query object is defined in advance by the user according to the query clause to be mapped, including the name and field of the query object; part or all of the fields of the query object are used to map the query condition; the field used to map the query condition is called the mapping field, and the field not used to map the query condition is called the non-mapping field; the name and type of the mapping field are defined in advance according to the query condition to be mapped by reversely applying the field mapping method; the instance of the query object is created and assigned by the user according to the definition of the query object.

[0026] Step 200: combining each query condition into a query clause using a logical AND operator.

[0027] In the application, since all the query conditions need to be met at the same time when querying the data, after obtaining the corresponding query condition, each query condition is combined into a query clause using a logical AND operator. Different types of databases use different database query languages, relational databases use SQL, and non-relational databases use a custom query language, so when combining the query clause, the specification of the corresponding database query language needs to be met, and the expression form of the logical AND operator used for combination is also different. For example, the logical AND operator of SQL is AND, and before being combined into a query clause, parentheses need to be added for the query condition containing the OR operator. Therefore, in one optional embodiment of the application, each query condition is combined into a query clause using a logical AND operator, specifically: each query condition is combined into a query clause according to the format of the corresponding database query language using the logical AND operator of the corresponding database query language.

[0028] The application can obtain the assignment of the field in the instance of the query object by applying the reflection technology, and obtain the query condition corresponding to the assigned field, therefore, one optional embodiment of step 100:

[0029] The reflection technology is applied to obtain the assignment of the field in the instance of the query object; the field mapping method is adapted and applied to the assigned field to obtain the corresponding query condition. As shown in FIG. 3, this step specifically includes:

[0030] 1) apply reflection technique to obtain all fields of the query object;

[0031] 2) traverse the i-th field of the query object, determine whether all the fields are all traversed, and obtain a first traversal determination result; i = 1, 2,..., N;

[0032] 3) when the first traversal determination result is no, apply reflection technique to read the assignment of the i-th field, and determine whether the i-th field has been assigned, and obtain a first assignment determination result;

[0033] 4) when the first assignment determination result is no, let i = i + 1, and return to execute the step of "traverse the i-th field of the query object";

[0034] 5) when the first assignment determination result is yes, adapt and apply the field mapping method for the i-th field, and obtain the query condition corresponding to the i-th field.

[0035] 6) when the first traversal determination result is yes, obtain the query condition corresponding to each assigned field in the instance of the query object.

[0036] The following is a more specific example process based on a relational database and Java implementation:

[0037] (1) Suppose a user has created a table in a relational database for storing user scores and notes, etc. The table name is user, and contains the following columns: column one, named id, type int; column two, named score, type int; column three, named memo, type varchar(255). Now the user needs to query data based on the size of the score and whether there is a note as a condition. Therefore, the user defines the following query object using Java language:

[0038] Among them, scoreGt represents querying data greater than a certain score, scoreLt represents querying data less than a certain score, and memoNull represents data with empty notes.

[0039] The user defines the fields of the query object by using the reverse application field mapping method. Taking the suffix mapping method as an example, the suffix mapping method is used to map the fields with a specific suffix in the query object to the corresponding query condition. The field name of the query object can be defined as a free combination of the column name (here, id, score, and memo) and the suffix name, such as id+Gt, score+Lt, and the like. If the user wants to query the data with a score less than a certain value, the query condition "score<?" is used. When the field name is defined, the column name score of the score and the suffix Lt corresponding to the less than sign are combined to obtain scoreLt as the name. After the instance is created, the corresponding value of the field scoreLt is assigned.

[0040] (2) The user creates a UserQuery instance and assigns values. It is assumed that the user wants to query the data with a score less than 60 and without a memo. The value 60 is assigned to the field scoreLt, and the value true is assigned to the field memoNull.

[0041] (3) The reflection technology is applied to obtain the field assignment of the UserQuery instance, and the value of the field scoreGt is null, the value of the field scoreLt is 60, and the value of the field memoNull is true.

[0042] (4) The field mapping method is adapted and applied to the assigned field, and the corresponding query condition score<? and the parameter list

[0060] are obtained.

[0043] (5) The query condition score<? AND memo IS NULL and the parameter list

[0060] are obtained by using the AND operator to combine the obtained query conditions.

[0044] Since repeatedly adapting the field mapping method for the field will cause a certain impact on the performance, the field mapping method can be adapted for all the fields of the query object in advance, and the adaptation relationship is cached as a field mapping adaptation table, which is used to improve the adaptation speed of the field mapping method. The corresponding relationship between each field of the query object and the adapted field mapping method is recorded in the field mapping adaptation table. Therefore, another optional implementation of step 100 is:

[0045] (1) The field mapping method is adapted for all the fields of the query object, and the field mapping adaptation table corresponding to the query object is obtained;

[0046] (2) The reflection technology is applied to obtain the field assignment in the instance of the query object;

[0047] (3) adapting and applying the field mapping method according to the field mapping adaptation table to the assigned field to obtain the corresponding query condition.

[0048] With the above example as a reference, the improved steps are as follows:

[0049] (1) The user defines the same query object UserQuery.

[0050] (2) The field mapping method is adapted for all fields of UserQuery to obtain the field mapping adaptation table corresponding to UserQuery: the suffix mapping method corresponding to scoreGt, the suffix mapping method corresponding to scoreLt, and the suffix mapping method corresponding to memoNull.

[0051] (3) The user creates an instance of UserQuery and assigns values. Assume that the user assigns the value 60 to the field scoreLt and the value true to the field memoNull.

[0052] (4) The reflection technology is applied to obtain the field assignments of the UserQuery instance, and it is obtained that the value of the field scoreGt is null, the value of the field scoreLt is 60, and the value of the field memoNull is true.

[0053] (5) According to the aforementioned field mapping adaptation table, the suffix mapping method corresponding to the assigned field scoreLt is obtained, the query condition score<? is obtained by applying the suffix mapping method, the suffix mapping method corresponding to the assigned field memoNull is obtained, and the query condition memo IS NULL is obtained by applying the suffix mapping method. Finally, the query condition corresponding to the aforementioned instance is obtained: score<? and memo IS NULL, and the parameter list is:

[0060] .

[0054] (6) The obtained query conditions are combined into a query clause using the AND operator: score<? AND memo IS NULL, and the parameter list is:

[0060] .

[0055] The present application includes various field mapping methods corresponding to different types of query conditions. The form of field definition is different for different development languages, and the form of query condition obtained is different for different database query languages, so each field mapping method needs to be implemented according to the specific development language and database. Among them, a field mapping method that can be applied to different development languages and databases is called a suffix mapping method, which is used to map fields in the field name of the query object that contain a specific suffix to the corresponding query condition, or can also be described as mapping fields in the query object that contain a specific suffix to the corresponding query condition. Therefore, in an optional implementation manner of an embodiment of the present application, the target field is mapped by applying the suffix mapping method, specifically including:

[0056] (1) The suffix of the target field is determined by performing suffix matching on the target field according to a suffix mapping table; the suffix mapping table includes a suffix name and an operator corresponding to different databases; the target field refers to a field adapted to the suffix mapping method. The process of suffix matching can be completed by using a regular expression. Part of the suffix mapping table is shown in Table 1.

[0057] (2) The field suffix of the target field is removed to obtain a target column name;

[0058] (3) The operator corresponding to the suffix of the target field is obtained according to the suffix mapping table;

[0059] (4) The query condition corresponding to the target field is obtained according to the target column name, the operator, the assignment corresponding to the target field, and the format of the corresponding database query condition. If the user configures a column name transformation strategy, the target column name needs to be further processed. For example, the field column name is createTimeGt, the suffix Gt is removed to obtain createTime, and then the user's configuration is performed on createTimeGt to obtain the final column name.

[0060] Table 2 shows the implementation comparison of the suffix mapping method in relational databases and MongoDB. For convenience of comparison, the SQL query condition in the example contains specific parameter values when the table is prepared. However, in implementation, to prevent SQL injection, it is recommended to replace the parameter values in the query condition with placeholders, which is a widely recognized method in the industry.

[0061] In addition to the suffix mapping method, the present application further provides various field mapping methods. The specific implementation of the field mapping method is affected by the database and the programming language, and cannot be listed one by one, and only some main field mapping methods for relational databases and Java programming language are described.

[0062] As an example, in the embodiments of the present application, the field mapping method further includes an annotation mapping method, which is suitable for fields added with query mapping annotations. Taking the account field as an example, the steps of implementing the annotation mapping method are as follows:

[0063] @QueryField(and="(username=?OR email=?OR mobile=?)")

[0064] private String account;

[0065] 1) read the string of the @QueryField annotation of the field as the query condition (username=?OR email=?OR mobile=?);

[0066] 2) add the corresponding parameter values according to the number of placeholders in the query condition.

[0067] As an example, in the embodiments of the present application, the field mapping method further includes an OR suffix mapping method, which is suitable for fields with the name suffix Or or _or; when multiple query conditions are obtained, a logical or operator is used for combination; in implementation, corresponding steps are executed according to the field type.

[0068] When the field type is a query object, the query object also contains multiple fields that can be mapped into query conditions. For instances of such objects, step 100 is recursively executed to map the assigned fields into query conditions, and a logical or operator is used for combination. The corresponding steps are as follows:

[0069] 1) execute step 100 for the assigned instances of the field to obtain the query conditions corresponding to the assigned fields;

[0070] 2) combine all the query conditions using a logical or operator.

[0071] The following is a specific example:

[0072] Define the field accountOr in the query object UserQuery, which is of another object Account.

[0073] Example: 1) When 3 fields in the assigned Account instance of the accountOr field are all assigned, according to step 100, get the query condition corresponding to each of the 3 fields: username =?, email =?, mobile =?; 2) Combine all query conditions using the OR operator to get the query condition corresponding to the accountOr field: username =? OR email =? OR mobile =?.

[0074] When the field type is a set and the instance of the set is a query object, the corresponding steps are as follows:

[0075] 1) According to the query object mapping method, map each instance in the set to get a corresponding group of subquery clauses; the subquery clauses are used to combine into a query condition;

[0076] Each instance is used as an instance of a query object, and the query object mapping method is applied recursively to get a corresponding subquery clause. There are multiple instances in a set, and each instance corresponds to a subquery clause, so multiple groups of subquery clauses are obtained.

[0077] 2) Combine all subquery clauses using the logical OR operator.

[0078] For example, for the accountsOr field defined as follows:

[0079] public class UserQuery{

[0080] private List <account>accountsOr;

[0081] }

[0082] Suppose the list assigned to the accountsOr field contains 3 instances of Account, the first instance has username and mobile fields assigned, the second instance has email and mobile fields assigned, and the third instance has all fields assigned. The specific mapping steps are as follows:

[0083] 1) The first instance is mapped to: (username =? AND mobile =?); the second instance is mapped to: (email =? AND mobile =?); and the third instance is mapped to: (username =? AND email =? AND mobile =?).

[0084] 2) The subquery clauses are spliced using the OR operator to obtain the query condition corresponding to the field accountsOr: (username =? AND mobile =?) OR (email =? AND mobile =?) OR (username =? AND email =? AND mobile =?).

[0085] When the field type is a set and the instances of the set are basic objects (such as strings, integers, floating-point numbers, Booleans, etc.), the corresponding steps are as follows:

[0086] 1) Remove the suffix Or or _or from the field name and apply the suffix mapping method to obtain the corresponding template query condition;

[0087] 2) According to the number of elements of the set that are assigned, obtain the corresponding number of template query conditions, and combine all the template query conditions using the OR operator to obtain the query condition corresponding to the field.

[0088] For example, for the usernameOr field defined as follows:

[0089] public class UserQuery{

[0090] private List <string>usernameContainOr;

[0091] }

[0092] 1) remove the suffix Or from usernameContainOr to get usernameContain, and apply the suffix mapping method to usernameContain to get the query condition: username LIKE?;

[0093] 2) assuming the assigned set has 3 elements, three identical query conditions are obtained; use the OR operator to combine all query conditions, and the query condition corresponding to the usernameContainOr field is: username LIKE? OR username LIKE? OR username LIKE?.

[0094] When the field type is a basic object, the corresponding steps are as follows:

[0095] Remove the suffix Or or _or of the field name, and apply the suffix mapping method to obtain the corresponding query condition.

[0096] When the field type is other types, it can be supported according to the implementation.

[0097] As an example, in the embodiment of the present application, the field mapping method includes: a subquery mapping method, which is applicable to fields added with a subquery annotation. The field naming format can be column name+operator suffix (such as Eq, Lt, Ge, etc.) + Any / All. Take the scoreLtAll field as an example, the steps of the subquery mapping method are as follows:

[0098] @Subquery(select="score",from=UserEntity.class)

[0099] UserQuery scoreLtAll;

[0100] 1) split scoreLtAll to get score < ALL;

[0101] 2) get the subquery statement SELECT score FROM t_user from the annotation @Subquery;

[0102] 3) according to the query object mapping method, get the query clause corresponding to the instance assigned by the field scoreLtAll, as the WHERE clause of the above subquery statement, and add parentheses to get: (SELECT score FROM t_user WHERE...);

[0103] 4) Concatenate the sentence fragments obtained in step 1) and step 3) to obtain the query condition score < ALL(SELECT score FROM t_user WHERE...).

[0104] As an example, in the embodiments of the present application, the field mapping method further comprises a nested query mapping method, which is applicable to fields added with a nested query annotation. The following is the flow of the nested query mapping method:

[0105] Suppose we have a user table t_user and a role table t_role, and the user table and the role table are in a many-to-many relationship, saved in an intermediate table a_user_and_role, the field user_id in a_user_and_role is a foreign key pointing to the id field of the t_user table, and the field role_id in a_user_and_role is a foreign key pointing to the id field of the t_role table.

[0106] Now we want to query which users are assigned the role with the name "admin". We define a query object RoleQuery for the role table, which contains a field with the type String and the name roleName. Then we add a field named role in the UserQuery class, which is of the type RoleQuery and annotated with @DomainPath({"user","role”}):

[0107] We can obtain the following information from the annotation @DomainPath({"user","role”}): the names of the two domain tables, t_user and t_role; the name of the intermediate table, a_user_and_role; and the names of the two foreign key columns, user_id and role_id. According to this information, the query condition template corresponding to the field role can be generated: id IN(SELECT user_id FROM a_user_and_role WHERE role_id IN(SELECT id FROM t_role[WHERE])).

[0108] In the query, first, an instance of RoleQuery is created, and the field roleName is assigned the value "admin". Then an instance of UserQuery is created, and the instance of RoleQuery is assigned to the role field of the UserQuery instance. The specific mapping steps of the UserQuery instance are as follows:

[0109] 1) According to the query object mapping method, the query clause is mapped from the instance of the role field: role_name = 'admin';

[0110] 2) Substituting the query clause into the WHERE clause part of the query condition template, the query condition corresponding to the field role is obtained: id IN (SELECT user_id FROM a_user_and_role WHERE role_id IN (SELECT id FROM t_role WHERE role_name = 'admin')).

[0111] Because multiple subquery statements are nested in the query condition, it is named as a nested query mapping method.

[0112] In the present application, the field mapping method includes multiple types. Before applying the field mapping method to the field, the corresponding field mapping method needs to be adapted to the field according to the characteristics of the field of the query object. The specific steps are as follows:

[0113] (i) Obtain the field of the query object, and judge whether the current field mapping method is adapted to the field in the preset order of the field mapping method, to obtain an adaptation judgment result;

[0114] (ii) When the adaptation judgment result is yes, the current field mapping method is taken as the adaptation result;

[0115] (iii) When the adaptation judgment result is no, the adaptation judgment of the next field mapping method is performed; when there is no next field mapping method, the suffix mapping method is taken as the adaptation result.

[0116] In the present application, when step 100 is performed and no query condition is obtained, step 200 cannot obtain the query clause either. When the field mapping method is applied to the field and the query object mapping method is recursively executed, the query clause may not be obtained, in which case the query condition obtained is empty.

[0117] In the present application, the query clause is obtained according to the scheme of the present embodiment, and a complete database query statement is further obtained, so as to complete the database access. In an optional implementation of the present embodiment, the query clause is spliced with a corresponding table name, column name, sorting clause and paging clause to obtain the database query statement of the database to be accessed; the table name and the column name are table names and column names in a database table of the database to be accessed; the table name and the column name are obtained through entity object mapping; the sorting clause and the paging clause are obtained according to a paging object inherited by the query object; the paging object contains three non-mapping fields: an integer type page number field pageNumber, an integer type page size field pageSize, and a string type sorting field sort. The fields pageNumber and pageSize are used to construct the paging clause. The field sort is used to construct the sorting clause. The specific construction manner of the paging clause and the sorting clause can refer to the prior art.

[0118] In the present embodiment, since the product implemented according to the present application is used, the user only needs to define an entity object and a query object for each data table, and define an implementation of a corresponding data access interface according to the accessed database, so as to easily map the objects into a query statement conforming to the format of a corresponding database query language, and then complete the database access.

[0119] In the ORM technology, in order to complete the mapping of the objects to the database query statement, the ORM framework usually provides an intermediate carrier, and the user needs to create an instance of the intermediate carrier according to the query parameters through code writing, and then the ORM framework maps the instance of the intermediate carrier into the database query statement. The code writing of this part is usually complex, and the code amount is large. On the basis of the prior art, the present application can directly map the objects into the database query statement through the definition of the query object and the field mapping method, and removes the construction of the intermediate carrier, so as to reduce the code amount and the debugging difficulty, shorten the development time, and improve the development efficiency. Since one intermediate step is removed, the present application can save all the codes corresponding to the step from the perspective of the developer, and only needs to follow certain predetermined rules when defining the objects; from the perspective of the scheme implementation, the present application simplifies one intermediate step, so that the present application has a wider application range.

[0120] Embodiment 2

[0121] As shown in FIGS. 1 and 2, different from the embodiment 1, the present embodiment adopts the manner of generating the query object mapping code, and obtains the query conditions corresponding to the assigned fields in the instance of the query object by calling and executing the generated query object mapping code.

[0122] In the scheme of the embodiment, the query object mapping code can be generated in advance so that the corresponding query conditions of the instances of the query object can be directly obtained by subsequent calling. Therefore, the application provides a query object mapping method, comprising:

[0123] The field mapping method is adapted and applied to all fields of the query object to obtain corresponding query conditions, and a query object mapping code is generated.

[0124] Specifically, the generation of the query object mapping code comprises:

[0125] (1) All fields of the query object are obtained by abstract syntax tree technology.

[0126] (2) The field mapping method is adapted and applied to all fields of the query object to obtain corresponding query conditions; a set of corresponding mapping instructions is generated for each field of the query object; the mapping instructions at least include an assignment judgment instruction and a query condition collection instruction; the assignment judgment instruction is used to judge whether the corresponding field has been assigned; the query condition collection instruction is used to collect the query conditions corresponding to the assigned field.

[0127] As an example, the step specifically comprises:

[0128] 1) The jth field of the query object is traversed to judge whether all the fields are all traversed, and a second traversal judgment result is obtained; j = 1, 2,..., M.

[0129] 2) When the second traversal judgment result is no, the field mapping method is adapted and applied according to the characteristics of the jth field to obtain the query condition corresponding to the jth field, and a set of mapping instructions is generated for the jth field.

[0130] 3) j = j + 1 and return to the step of "traversing the jth field of the query object".

[0131] 4) When the second traversal judgment result is yes, M sets of mapping instructions corresponding to M fields of the query object are obtained.

[0132] (3) According to the M sets of mapping instructions and other instructions required by the method calling, a query object mapping code is generated, which is output as a source code file and waits for compilation and execution. The generated query object mapping code includes the query conditions corresponding to all fields of the query object.

[0133] The following is another optional embodiment of step 100, which is obtained by directly calling the query object mapping code generated in advance to obtain the query conditions corresponding to the assigned fields in the instances of the query object, and specifically comprises:

[0134] The query object mapping code is invoked and executed using an instance of the query object to obtain query conditions corresponding to each assigned field in the instance of the query object.

[0135] Specifically, the execution step of the query object mapping code is:

[0136] (1) The user creates an instance of the query object, invokes the aforementioned query object mapping code, and executes the assignment judgment instructions in the aforementioned M groups of mapping instructions in sequence to determine whether the corresponding fields have been assigned, obtaining a second assignment judgment result.

[0137] (2) When the second assignment judgment result is yes, the corresponding query condition collection instruction is invoked to collect the corresponding query condition.

[0138] (3) When the second assignment judgment result is no, the next assignment judgment instruction is invoked. After all pre-generated instructions are executed, the query conditions corresponding to each assigned field in the instance of the query object are obtained.

[0139] In this embodiment, the implementation subject of the generation step and the execution step of the query object mapping code is generally not the same: the implementation subject of the generation step is usually an independent software vendor; and the implementation subject of the execution step is usually a company developing a database-based application system, such as various Internet companies, financial companies, etc.

[0140] The following is an example of generating query object mapping code based on Go language and MongoDB database:

[0141] 1. It is assumed that the user defines the following query object using Go language:

[0142] 2. Get all fields of the query object UserQuery, get ScoreGt, ScoreLt, MemeNull, fit the field mapping method for these fields, get the mapping relationship of the fields of the query object UserQuery and the query condition as: {ScoreGt:D{{"score",D{{"$gt",q.ScoreGt}}}, ScoreLt:D{{"score",D{{"$lt",q.ScoreLt}}}},MemoNull:D{{"memo",D{{"$type",10}}}}}. Generate a set of mapping instructions for each set of mapping relationships. Each set of mapping instructions includes at least one assignment judgment instruction and one query condition collection instruction, and only when the judgment is true, the query condition collection instruction is executed. Take the field ScoreGt as an example, the generated assignment judgment instruction is: q.ScoreGt!= nil, and the generated query condition collection instruction is: d = append(d, D{{"score", D{{"$gt", q.ScoreGt}}}}). Each set of mapping instructions is used to determine a corresponding query condition.

[0143] 3. Generate complete package name and reference instructions, method definition instructions, return value definition instructions, return value return instructions, etc. for the entire class, and generate a complete query object mapping code as follows:

[0144] Suppose the user creates an instance of UserQuery and assigns the value 60 to the field scoreLt and true to the field memoNull; the user uses the instance to call the above code, and the query condition obtained after execution is: {"score":{"$lt":60}}, {"memo":{"$type",10}}.

[0145] In addition, for the query condition in the SQL statement, since the placeholder will be used to replace the parameters in the query condition, that is, the field scoreLt will be mapped to score<?, therefore, a corresponding set of mapping instructions also includes a query parameter collection instruction, for example:

[0146] Assignment judgment instruction: if q.ScoreLt!= nil {...}

[0147] Query condition collection instruction: conditions = append(conditions, "score<?")

[0148] Query parameter collection instruction: args = append(args, q.ScoreLt)

[0149] The code example here is mainly used for illustrating the specific form of the assignment judgment instruction of the field and the collection instruction of the query condition, and showing a query object mapping code which can be compiled and executed.

[0150] Embodiment 3

[0151] As shown in FIG. 4, the application provides a data access device, comprising an initialization module, a database query statement mapping module, and a database query statement execution module. The initialization module is used for obtaining the table name of the target database table and each column name according to the definition of the entity object. The database query statement mapping module comprises a query object mapping method based on embodiment 1 or embodiment 2, which maps the instance of the query object into a query clause, and splices the query clause with the table name of the target database table and each column name to obtain the corresponding database query statement. The database query statement execution module is used for submitting the database query statement to the database for execution, and returning the execution result after processing.

[0152] The calling flow of each module of the data access device is as follows:

[0153] Step 1: Create a database and a database table, and write the corresponding entity object and query object for each database table.

[0154] Step 2: Create a data access device, and use the definition of the entity object to call the initialization module to obtain the table name and column name of the database table corresponding to the entity object.

[0155] Step 3: Create an instance of the query object and assign values, and use the instance as a parameter to call the database query statement mapping module of the data access device to obtain the database query statement.

[0156] Step 3.1: Map the instance of the query object into a query clause according to the query object mapping method.

[0157] Step 3.2: Obtain the sorting clause according to the page number and page size fields of the paging object inherited by the query object (if there is assignment).

[0158] Step 3.3: Obtain the paging clause according to the sorting field of the paging object inherited by the query object (if there is assignment).

[0159] Step 3.4: Splice the above clauses and the table name and column name of the database table obtained in step 2 according to the database query statement template to obtain the corresponding database query statement.

[0160] Step 4: Use the database query statement as a parameter to call the database query statement execution module of the data access device to obtain the execution result.

[0161] Embodiment 4

[0162] The present application provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of a query object mapping method as described in Embodiment 1 or Embodiment 2.

[0163] Embodiment 5

[0164] As shown in FIG. 5, the programming languages include several kinds (N kinds), and the types of database query languages also include several kinds (M kinds), then, after the programming languages and the database query languages are specified, the query object mapping methods provided by Embodiment 1 and Embodiment 2 can be applied to the cases of N*M programming languages and database combinations. FIG. 5 shows some programming languages: Java, Go, C++, C#, Python, JavaScript, PHP, C language; some NoSQL databases: MongoDB, Redis, ES, CouchDB, Cassandra; some relational databases: MySQL, SQLServer, Oracle, PostgreSQL.

[0165] The present application is based on a relational database and Java language, wherein the programming language used for defining the query object is Java, and the database to be accessed is a relational database, and the following is a specific mapping process of applying the method provided by Embodiment 1:

[0166] (1) For the user table and the query requirement described in Embodiment 1, the user defines the following query object using Java language:

[0167] (2) The user creates an instance of the query object and assigns values. It is assumed that the user assigns 60 to the field scoreLt and true to the field memoNull.

[0168] (3) The field mapping method is adapted and applied to the assigned fields scoreLt and memoNull, and the corresponding query conditions: score<?, memo IS NULL, and the parameter list:

[0060] are obtained.

[0169] (4) The obtained query conditions are combined into a query clause using the AND operator: score<? AND memo IS NULL, and the parameter list is:

[0060] .

[0170] Embodiment 6

[0171] The embodiment of the application is based on a non-relational database MongoDB and Java language, wherein the programming language used when defining a query object is Java, and the database to be accessed is a non-relational database MongoDB. The following is a specific mapping process of the method provided by the application embodiment 1 or embodiment 2:

[0172] (1) Assuming that a user has created a collection in a MongoDB database for storing data such as scores and notes of users, the collection name is user, and contains the following fields: field one, named id, of type Integer; field two, named score, of type Integer; and field three, named memo, of type String. Now the user needs to query data according to the size of the score and whether there is a note as a condition. Therefore, the user defines a query object as follows using Java language:

[0173] (2) The user creates an instance of the query object and assigns values. Assuming that the field scoreLt is assigned a value of 60, and the field memoNull is assigned a value of true.

[0174] (3) The field mapping method is adapted and applied to the assigned fields scoreLt and memoNull, and the corresponding query conditions are obtained: {"score":{"$lt":60}}, {"memo":{"$type",10}}

[0175] (4) The obtained query conditions are combined into a query clause using the "$and" operator, and the text form of the query clause in MongoDB is: {"$and":[{"score":{"$lt":60}},{"memo":{"$type",10}}]}

[0176] According to the query clause, the document collection to be accessed, and the field name, a query statement in MongoDB can be further obtained. The text form of the query statement executed in the console is:

[0177] db.user.find({"$and":[{"score":{"$lt":60}},{"memo":{"$type",10}}]}).projection("id","score","memo")

[0178] Since the query logic of the data is the same, and both are written in Java, the same point as embodiment 5 is that the defined query object is the same. Since the type of the database to be accessed is different, the different point from embodiment 5 is that the format of the output query statement is different.

[0179] Embodiment 7

[0180] Embodiments of the present application are based on relational databases and Go language, wherein the programming language used when defining the query object is Go, and the database to be accessed is a relational database. The following is a specific mapping process of applying the method provided in Embodiment 1 or Embodiment 2:

[0181] (1) For the user table and query requirements described in Embodiment 1, the user defines the following query object using Go language:

[0182] (2) The user creates an instance of the query object and assigns values. Suppose the user assigns the value 60 to the field ScoreLt and the value true to the field MemoNull.

[0183] (3) The field mapping method is adapted and applied to the assigned fields ScoreLt and MemoNull, and the corresponding query conditions are obtained: score<?, memo IS NULL, and the parameter list is:

[0060] .

[0184] (4) The query conditions obtained are combined into a query clause using the AND operator: score<?AND memo IS NULL, and the parameter list is:

[0060] .

[0185] Since they are all based on relational databases and have the same query logic, the same points as Embodiment 5 are: the output query statements are the same; since the development languages used are different, the differences from Embodiment 5 are: the syntax for defining the query object is different.

[0186] Embodiments 5-7 use two programming languages and two databases that use different database query languages in combination. A person of ordinary skill in the art can use the present application in the implementation of Go language and MongoDB combination without creative labor, as well as the N*M programming language and database combination implementation mentioned in Embodiment 6.

[0187] Embodiment 8

[0188] It should be noted that, for better comparison with ORM technology and to facilitate understanding by those skilled in the art, the technical features and steps of the present application are mainly described in terms of related terms in object-oriented programming, but this does not mean that the present application can only be used in object-oriented programming languages. Taking C language, a representative language in procedural programming language, as an example, the following structure is defined:

[0189] It can be seen that the structure is only different in syntax from the query object defined in Embodiment 5-7, and the relevant steps of the present application are still applicable to the structure. Therefore, the structure in the procedural language for mapping into the query clause should be considered as the same concept as the query object, the structure variable should be considered as the same concept as the instance of the query object, and the field mapping method is also applicable to the member variable in the structure. Thus, the person skilled in the art can apply the present application to other procedural programming languages without creative labor.

[0190] Embodiment 9

[0191] This embodiment is further illustrated on the basis of the scheme of Embodiment 1 or Embodiment 2, the field traversal process of the query object: if the user is allowed to add non-mapping fields in the query object during implementation, then after the traversal judgment result, a judgment on whether the field belongs to the non-mapping field is needed when the fields of the query object are traversed; if the judgment result is yes, then the field is skipped and the next field is traversed. That is, when the fields of the query object are traversed, the non-mapping fields of the query object are skipped.

[0192] Taking Java implementation as an example, static fields, fields annotated with @Transient, fields annotated with @Join, etc. are considered as non-mapping fields.

[0193] Each embodiment focuses on the difference from other embodiments, and the same and similar parts between embodiments can be referred to each other.

[0194] The principles and implementation manners of the present application are described by applying specific examples in the present text, and the above embodiment description is only for helping to understand the method of the present application and its core idea; at the same time, for the person skilled in the art, according to the idea of the present application, the specific implementation manner and application range will have changes. In summary, the content of the present description should not be understood as the limitation of the present application.< / string> < / account>

Claims

1. A query object mapping method, characterized in that: include: By applying the adapted field mapping method, the query conditions corresponding to each assigned field in the query object instance are obtained; The field mapping method is used to map the fields in the query object that meet specific characteristics into corresponding query conditions; The query conditions are combined into a query clause using a logical AND operator.

2. A query object mapping method according to claim 1, characterized in that: By applying the adapted field mapping method, the query conditions corresponding to each assigned field in the query object instance are obtained, including: Applying reflection technology to obtain the value of the field in the instance of the query object; Adapt and apply the field mapping method to the assigned fields to obtain the corresponding query conditions.

3. A query object mapping method according to claim 1, characterized in that: By applying the adapted field mapping method, the query conditions corresponding to each assigned field in the query object instance are obtained, including: Adapting the field mapping method to all fields of the query object to obtain a field mapping adaptation table corresponding to the query object; Applying reflection technology to obtain the value of the field in the instance of the query object; The assigned fields are adapted according to the field mapping adaptation table and the field mapping method is applied to obtain corresponding query conditions.

4. A query object mapping method according to claim 1, characterized in that: By applying the adapted field mapping method, the query conditions corresponding to each assigned field in the query object instance are obtained, including: The query object mapping code is called and executed using an instance of the query object to obtain query conditions corresponding to each assigned field in the instance of the query object; the query object mapping code is generated based on all fields of the query object and the corresponding query conditions obtained by adapting and applying the field mapping method.

5. A query object mapping method, characterized in that: include: Adapt and apply the field mapping method to all fields of the query object to obtain corresponding query conditions, and generate a query object mapping code; The query object mapping code is called using the instance of the query object, and after execution, the query conditions corresponding to each assigned field in the instance of the query object can be obtained; The query conditions can be combined into query clauses using a logical AND operator.

6. A query object mapping method according to claim 1 or 2 or 3 or 4 or 5, characterized in that: The field mapping method includes: The suffix mapping method is used to map fields in the query object whose names contain specific suffixes to corresponding query conditions.

7. A query object mapping method according to claim 1 or 2 or 3 or 4 or 5, characterized in that: Also includes: When traversing the fields of the query object, non-mapped fields of the query object are skipped.

8. A data access device, characterized in that: include: A database query statement mapping module, based on a query object mapping method described in any one of claims 1 to 7, maps an instance of a query object into a query clause, and concatenates the query clause with the table name and each column name of the target database table to obtain a corresponding database query statement.

9. A data access device according to claim 8, characterized in that: Also includes: The initialization module is used to obtain the table name and column names of the target database table according to the definition of the entity object; The database query statement execution module is used to submit the database query statement to the database for execution, and process the execution result and return it.

10. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of a query object mapping method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Multilayer analysis method of structured query statement, computer equipment and storage medium

    CN114756569A

  • Java-based full-automatic object relation mapping method, system and equipment and storage medium

    CN116204598A

  • Query object mapping method and device and product

    CN118227626A

  • Construction method for dynamic structured query language statement

    CN1881208A

  • Module for building database queries

    US20090138455A1