A method and device for data query
By automatically generating SQL statements, the inefficiency problem of developers manually modifying SQL statements when data query requirements are updated is solved, and efficient data query and development process is realized.
Patent Information
- Application Number
- CN202110129069.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-29
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-01-29
AI Technical Summary
When the data query requirements are updated in the existing technology, developers need to manually modify SQL statements and perform coding, testing and online processes, resulting in low development efficiency.
By obtaining the business data table identification information, fields to be queried and query conditions in the data query request, the target SQL statement is automatically generated and data query is directly carried out to avoid manually modifying the SQL statement and repeated encoding test processes.
It realizes the automatic generation of SQL statements based on real-time data query requests, which improves the development efficiency of data query methods and data query efficiency.
Smart Images

Figure CN113760948B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a data query method and device. Background Art
[0002] In order to speed up the development of data queries, a persistence layer framework is usually used in Internet projects to support data queries.
[0003] In the persistence layer framework, data query is generally implemented by configuring SQL statements and generating methods of the data access layer. However, data query requirements are constantly updated. When implementing data query using the above method, developers need to modify SQL statements every time the query requirements are updated. For example, every time a new table, query field, or query condition is added to the database, developers need to modify SQL statements accordingly. After modifying the SQL statements, they need to re-encode, test, and launch the process, which leads to low development efficiency of data query methods. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a method and device for data query, which can automatically generate a target SQL statement according to the first identification information of the business data table to be queried, the field to be queried and the query condition in the data query request, and further perform data query through the automatically generated target SQL statement. In this way, the target SQL statement is automatically generated according to the real-time data query request, without the need for developers to manually modify the SQL statement according to the changes of the data table, query field or query condition in the database, and there is no need to re-encode, test and launch the modified SQL statement, thereby improving the development efficiency of the data query method and the efficiency of data query.
[0005] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for data query is provided.
[0006] A data query method according to an embodiment of the present invention comprises: obtaining a data query request, wherein the data query request comprises: first identification information of a business data table to be queried, a field to be queried, and a query condition;
[0007] Generate a target SQL statement according to the first identification information, the field to be queried and the query condition;
[0008] According to the target SQL statement, a business data table corresponding to the first identification information is determined in a database, and business data corresponding to the field to be queried is determined from the business data table according to the query condition.
[0009] Optionally, generating a target SQL statement includes:
[0010] Generate a conditional SQL statement corresponding to the query condition according to the first configuration table and the query condition; wherein the first configuration table includes: one or more conditional SQL statements corresponding to the query condition;
[0011] The conditional SQL statement, the first identification information and the field to be queried are used to replace the default condition field, the default table name and the default query field in the default SQL statement respectively; wherein the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
[0012] Optionally, when there are multiple fields to be queried, generating a conditional SQL statement corresponding to the query condition includes:
[0013] A separator is inserted between every two of the fields to be queried, so as to splice the multiple fields to be queried through the separator, and the spliced fields to be queried are used to replace the default query field.
[0014] Optionally, the query condition includes any one or more of the following: a data type of the field to be queried, a data condition matching method, and a condition value.
[0015] Optionally, when the query condition includes the data type of the field to be queried, and the first configuration table also includes conditional SQL statements corresponding to each data type,
[0016] According to the data type of the field to be queried, determining a conditional SQL statement corresponding to the data type from the first configuration table;
[0017] The determined conditional SQL statement replaces the default condition field to generate the target SQL statement.
[0018] Optionally, the method further comprises:
[0019] When a new query condition is obtained, the new query condition and a conditional SQL statement corresponding to the new query condition are added to the first configuration table.
[0020] Optionally, before generating the target SQL statement, the method further includes:
[0021] According to the data query request, calling a second configuration table; the second configuration table includes: second identification information of one or more business data tables, and a mapping relationship between the second identification information and a business field;
[0022] Determine, from the second configuration table, second identification information that is identical to the first identification information, and determine, according to the mapping relationship, a service field corresponding to the second identification information;
[0023] According to the determined business field, verify whether the field to be queried is legal, and if so, generate a target SQL statement according to the first identification information, the field to be queried and the query condition.
[0024] Optionally, the second configuration table further includes: an alias of the business field, and a mapping relationship between the alias and the business field and the second identification information; when it is determined according to the second configuration table that the field to be queried is an alias of the business field,
[0025] According to the mapping relationship, the business field corresponding to the field to be queried is determined, and according to the determined business field, the target SQL statement is generated.
[0026] Optionally, when the business data table in the database and / or the business field in the business data table is updated, the second identifier and / or the mapping relationship is correspondingly updated in the second configuration table.
[0027] Optionally, the method further comprises:
[0028] The format of the queried business data is converted according to the data format required by the query end that sends the data query request, and the business data after the format conversion is returned to the query end.
[0029] To achieve the above objective, according to another aspect of an embodiment of the present invention, a data query device is provided.
[0030] A data query device according to an embodiment of the present invention comprises: a request acquisition module, an SQL statement generation module and a query module; wherein:
[0031] The request acquisition module is used to acquire a data query request, wherein the data query request includes: first identification information of a business data table to be queried, a field to be queried, and a query condition;
[0032] The SQL statement generating module is used to generate a target SQL statement according to the first identification information, the field to be queried and the query condition;
[0033] The query module is used to determine the business data table corresponding to the first identification information in the database according to the target SQL statement, and determine the business data corresponding to the field to be queried from the business data table according to the query condition.
[0034] Optionally, the SQL statement generation module is used to generate a conditional SQL statement corresponding to the query condition based on the first configuration table and the query condition; wherein the first configuration table includes: conditional SQL statements corresponding to one or more query conditions respectively; using the conditional SQL statement, the first identification information and the field to be queried to replace the default condition field, default table name and default query field in the default SQL statement respectively; wherein the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
[0035] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device for data query is provided.
[0036] An AA electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a data query method according to an embodiment of the present invention.
[0037] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided.
[0038] A computer-readable storage medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, a data query method according to an embodiment of the present invention is implemented.
[0039] One embodiment of the above invention has the following advantages or beneficial effects: according to the first identification information of the business data table to be queried in the data query request, the field to be queried and the query condition, the target SQL statement is automatically generated, and the data query is further performed through the automatically generated target SQL statement. Thus, the target SQL statement is automatically generated according to the real-time data query request, without the need for developers to manually modify the SQL statement according to the changes in the data table, query field or query condition in the database, and without the need to re-encode, test and launch the modified SQL statement, thereby improving the development efficiency of the data query method and the efficiency of data query.
[0040] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0042] Figure 1 is a schematic diagram of main steps of a data query method according to an embodiment of the present invention;
[0043] Figure 2 is an exemplary architecture diagram of a data query device according to an embodiment of the present invention;
[0044] Figure 3 is a schematic diagram of main steps of another data query method according to an embodiment of the present invention;
[0045] Figure 4 is a schematic diagram of main steps of generating a target SQL statement according to an embodiment of the present invention;
[0046] Figure 5 is a schematic diagram of main modules of a data query device according to an embodiment of the present invention;
[0047] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0048] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0049] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0050] It should be pointed out that the embodiments of the present invention and the technical features therein may be combined with each other without conflict.
[0051] Figure 1 It is a schematic diagram of main steps of a data query method according to an embodiment of the present invention.
[0052] like Figure 1 As shown, a data query method according to an embodiment of the present invention mainly includes the following steps S101 to S103:
[0053] Step S101: obtaining a data query request, wherein the data query request includes: first identification information of a business data table to be queried, a field to be queried, and a query condition.
[0054] The device architecture diagram for implementing the data query method provided by the embodiment of the present invention can be as follows: Figure 2 As shown, using Figure 2 The main steps of the data query method provided by the embodiment of the present invention implemented by the architecture shown in the figure can be as follows: Figure 3 Reference Figure 2 and Figure 3 , the device implementing the data query method can provide a query interface to the outside, and the data query end can call the query interface to send a data query request, and the input parameters carried in the data query request include the first identification information of the business data table, the field to be queried, and the query condition. Among them, the query interface can include a single record query interface and a multiple record query interface.
[0055] In Java, the definition of a single record interface can be as follows: Map<String,Object> select One(StringtableName,List <string>columns,List <condition>where Condition). The interface for querying multiple records can be defined as follows: List <Map<String,Object> >select List(String tableName,List <string>columns,List <condition>where Condition).
[0056] The input parameter details carried in the data query request can be shown in the following Table 1:
[0057] Table 1
[0058]
[0059] Taking the Java class as an example, the field definition of the Condition object in Table 1 can be shown in Table 2 below. This field can be used to assemble the conditions in the target SQL statement, and its format can be field to be queried = field value. Of course, the "=" here is only an operation matcher for one query condition, and there can be different operation matchers under different query conditions, such as greater than or less than. The field value is the filter condition value corresponding to the query condition.
[0060] Table 2
[0061]
[0062] In Table 2, the OperatorType enumeration object (data condition matching method) corresponds to the above-mentioned operation matcher, which is the type of matching operation in a condition in an SQL statement. Its specific definition and meaning can be shown in Table 3 below.
[0063] Table 3
[0064]
[0065]
[0066] In an embodiment of the present invention, after obtaining a data query request and before generating a target SQL statement, it may also include: calling a second configuration table according to the data query request; the second configuration table includes: second identification information of one or more business data tables, and a mapping relationship between the second identification information and a business field; determining second identification information that is the same as the first identification information from the second configuration table, and determining the business field corresponding to the second identification information according to the mapping relationship; based on the determined business field, verifying whether the field to be queried is legal, and if so, executing the generation of the target SQL statement based on the first identification information, the field to be queried and the query condition.
[0067] Among them, the second configuration table is Figure 2 The configuration table in the business data table is Figure 2 The second configuration table includes second identification information of one or more business data tables, and the second identification information may be the table name of the business data table. For example, if the table name of a business data table storing user information is t_user, the second identification information may be t_user, and the specific information of the business data table may be as shown in Table 4 below:
[0068] Table 4
[0069]
[0070]
[0071] The second configuration table is used to store the second identification information of one or more business data tables, and the mapping relationship between the second identification information and the business field, wherein the table structure and fields of the second configuration table may be as shown in: 5. In addition, when configuring the second configuration table, the table name of the second configuration table may also be generated accordingly, for example, the following table 5 may be named t_table_config.
[0072] Table 5
[0073]
[0074] Configuring a piece of data in the second configuration table means that a mapping relationship is established between the corresponding business data table and the field. For example, in order to support dynamic query of the business data table t_user, the mapping relationship corresponding to the above table 1 (business data table t_user) needs to be configured in the second configuration table t_table_config. An example of configuration data can be shown in Table 6 below.
[0075] Table 6
[0076]
[0077]
[0078] Thus, by calling the second configuration table, it is possible to determine which business data tables are connected to the system and which fields of these business data tables have established mapping relationships. When the business data tables and / or business fields in the database are updated, the mapping relationships in the second configuration table can be updated accordingly, that is, the latest mapping relationships can be obtained by calling the second configuration table.
[0079] When calling the second configuration table, the second configuration table can be called through the table / field / mapping configuration component in the data query device provided by the embodiment of the present invention. Figure 2 The table / field / mapping configuration component in calls the second configuration table to complete the configuration of the second configuration table.
[0080] After obtaining the data query request, the second configuration table is called to determine the second identification information that is the same as the first identification information of the data query request. Taking the query of the business data table t_user as an example, all the field names (business fields) and data types of the t_user configuration can be queried in the second configuration table by selecting id, table_name, column, data_type from t_table_config where table_name = 't_user'. Then, Figure 2 The interface parameter validity checker in the data query request determines whether the fields to be queried included in the data query request are legal based on the determined business fields. For example, by querying whether the table_name field in the second configuration table contains the second identification information that is the same as the first identification information, the first identification information is verified to be legal. In addition, the business field list obtained by querying the second configuration table table_name='XXX' can be used to verify whether the fields to be queried are legal. In addition, the business field list obtained by querying the second configuration table table_name='XXX' can be used to verify whether the condition field name in the field to be queried is correct. In addition, it is also possible to further verify whether the matching operation type in the condition conforms to the data type of the field, for example, like matching can only support VARCHAR data type, >=, >, <=, < can only support int type, etc. Only when the input parameters are verified to be legal can the target SQL statement be further generated based on the input parameters.
[0081] Step S102: Generate a target SQL statement according to the first identification information, the field to be queried and the query condition.
[0082] In an embodiment of the present invention, a target SQL statement can be generated according to the first configuration table and the default SQL statement. Specifically, in this embodiment, a conditional SQL statement corresponding to the query condition is generated according to the first configuration table and the query condition; wherein the first configuration table includes: conditional SQL statements corresponding to one or more query conditions respectively; the conditional SQL statement, the first identification information and the field to be queried are used to replace the default condition field, the default table name and the default query field in the default SQL statement respectively; wherein the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
[0083] When generating the target SQL statement, you can use Figure 2 The target SQL statement is generated by using the SQL generator in . Specifically, the default SQL statement shown in Table 7 can be pre-stored. Among them, field name 1 and field name are default query fields, "table name" is the default table name, condition 1, condition 2, condition 3 and condition 4 are default condition fields, and select, from and where are connection fields.
[0084] Table 7
[0085]
[0086] When generating a target SQL statement, a conditional SQL statement can be first assembled according to the input parameters of the data query request, and then the first identification information and the field to be queried in the input parameters replace the default table name and the default query field in the default SQL statement to generate the target SQL statement. When generating a conditional SQL statement, a conditional SQL statement corresponding to the query condition of the data query request can be determined according to the SQL statements corresponding to one or more query conditions in the first configuration table. In addition, a conditional SQL statement can also be assembled according to the query conditions in the data query request based on the condition fields shown in Tables 2 and 3.
[0087] In one embodiment of the present invention, when there are multiple fields to be queried, generating a conditional SQL statement corresponding to the query condition includes: inserting a separator between every two fields to be queried, so as to splice the multiple fields to be queried through the separator, and replacing the default query field with the spliced fields to be queried. For example, when the fields to be queried are id, name, and sex, the separator can be an English comma, then the three fields to be queried are spliced into the form of "id, name, sex", and then the spliced "id, name, sex" is used to replace "field name 1, field name 2,..." in the default SQL statement. Secondly, the first identification information in the input parameter can also be used to replace the default table name, such as using the "t_user" in the input parameter to replace the "table name" in the default field, and the replaced default SQL statement is shown in Table 8 below.
[0088] Table 8
[0089]
[0090] It is worth noting that when replacing the field to be queried, if the field to be queried in the input parameter of the data query request is an alias of the business field in the second configuration table, the business field corresponding to the alias can be determined according to the second configuration table, so as to generate the target SQL statement using the business field, thereby facilitating data query in the database. Specifically, in one embodiment of the present invention, the second configuration table also includes: the alias of the business field, and the mapping relationship between the alias and the business field and the second identification information respectively; when it is determined according to the second configuration table that the field to be queried is an alias of the business field, the business field corresponding to the field to be queried is determined according to the mapping relationship, and the target SQL statement is generated according to the determined business field.
[0091] In addition, as shown in Table 2 and Table 3 above, the query condition may include any one or more of the following: the data type of the field to be queried, the data condition matching method, and the condition value. In one embodiment of the present invention, when the query condition includes the data type of the field to be queried, and the first configuration table also includes conditional SQL statements corresponding to each data type, the conditional SQL statement corresponding to the data type is determined from the first configuration table according to the data type of the field to be queried; the determined conditional SQL statement replaces the default condition field to generate the target SQL statement.
[0092] For example, for data types: VARCHAR and non-VARCHAR types, the generated conditional SQL is slightly different. Taking the query condition field name a and value 123 as an example, for VARCHAR and non-VARCHAR types, the generated conditional SQL is shown in the following Table 9 (Table 9 is a partial example of the first configuration table). It can be seen from Table 9 that the conditional SQL generated by the VARCHAR data type has single quotes, while the conditional SQL generated by the non-VARCHAR type does not have single quotes.
[0093] Table 9
[0094]
[0095]
[0096] After the conditional SQL statement is generated, the conditional SQL statement replaces the default condition field in the default SQL statement shown in Table 7. For example, when the generated conditional SQL statement is a=123, it is used to replace the default condition field "condition one and condition two and condition three and condition four and ...", and the generated target SQL statement can be shown in Table 10. The main steps of the process of generating the target SQL statement can be as follows: Figure 4 shown.
[0097] Table 10
[0098]
[0099] In the above method of generating the target SQL statement according to the first configuration table, when there is a new query condition, the new query condition and the conditional SQL statement corresponding to the new query condition are added to the first configuration table. Then, when there is a new query condition in the input parameter of the data query request, the corresponding conditional SQL statement can be generated. There is no need for developers to manually modify the corresponding SQL statement, and there is no need for recoding, testing, and online processes, thereby improving the development efficiency of the data query method.
[0100] Step S103: According to the target SQL statement, determine in the database a business data table corresponding to the first identification information, and determine from the business data table business data corresponding to the field to be queried according to the query condition.
[0101] After generating the target SQL statement, a JDBC (Java Database Connectivity) request can be sent to the database to use the target SQL statement to search for a business data table corresponding to the first identifier in multiple business data tables of the database, and determine the business data corresponding to the field to be queried from the found business data table according to the query conditions to obtain a JDBC data result set.
[0102] Since the JDBC data result set is of ResultSet type, the data contained in the ResultSet type needs to be finally parsed and converted into a Map<String,Object> That is, in the embodiment of the present invention, the format of the queried business data is converted according to the data format required by the query end that sends the data query request, and the business data after the format conversion is returned to the query end.
[0103] For example, you can Figure 2 The result set object parser in the query calls the ResultSet.getObject(index) method to obtain the data corresponding to the field to be queried. The parameter index is the column number of the field in the returned row. For example, if the three fields id, name, and sex are queried, then getObject(1) obtains the value of the id field, getObject(2) obtains the value of the name field, and getObject(3) obtains the value of the sex field. The rule table for parsing and converting field expressions can be shown in Table 11 below:
[0104] Table 11
[0105]
[0106]
[0107] The Java field type corresponding to the database field can be obtained from the table / field / mapping configuration table. When converting, the code needs to call the expression given in the second column of Table 10 according to different Java types to obtain the value. This expression takes into account the case where the field is null. The parsed and converted value is put into the map object, with the field name as the key and the converted value as the value. Finally, it is returned to the interface caller.
[0108] For example, query the Java object type of the single data interface return value: Map<String,Object> , map is a key-value data type. You can put multiple key-value pairs, each of which is the business data corresponding to a field to be queried. The field name is used as the key, and the queried field value is used as the value, which is placed in the map. The Java object type of the query multiple data interface return value: List <Map<String,Object> > is a list, each element in the list is a Map<String,Object> An object that represents a piece of business data.
[0109] For example, through the target SQL statement: select id, name, birth_day from t_user where id = 1, the user data with user ID 1 is queried. Assume that the queried user name = Zhang San, birth_day = 2000-01-01. The row of data returned by name, that is, the data stored in the map, is shown in Table 12 below:
[0110] Table 12
[0111]
[0112]
[0113] According to a method of data query of an embodiment of the present invention, it can be seen that according to the first identification information of the business data table to be queried in the data query request, the field to be queried and the query condition, the target SQL statement is automatically generated, and the data query is further performed through the automatically generated target SQL statement. Thus, the target SQL statement is automatically generated according to the real-time data query request, without the need for developers to manually modify the SQL statement according to the change of the data table, query field or query condition in the database, and no longer need to re-encode, test and go online according to the modified SQL statement, thereby improving the development efficiency of the data query method and the efficiency of data query. In addition, when a new business data table and / or a new business field is added, the second identification information and / or the new business field of the new business data table are added to the second configuration table (such as t_table_config), which can support the query of business data, and finally realize a reusable single-table dynamic query device, improve research and development efficiency, and reduce the development cycle of the project.
[0114] Figure 5 It is a schematic diagram of main modules of a data query device according to an embodiment of the present invention.
[0115] like Figure 5 As shown, a data query device 500 according to an embodiment of the present invention includes: a request acquisition module 501, an SQL statement generation module 502 and a query module 503; wherein,
[0116] The request acquisition module 501 is used to acquire a data query request, wherein the data query request includes: first identification information of a business data table to be queried, a field to be queried, and a query condition;
[0117] The SQL statement generating module 502 is used to generate a target SQL statement according to the first identification information, the field to be queried and the query condition;
[0118] The query module 503 is used to determine the business data table corresponding to the first identification information in the database according to the target SQL statement, and determine the business data corresponding to the field to be queried from the business data table according to the query condition.
[0119] In one embodiment of the present invention, the SQL statement generation module 502 is used to generate a conditional SQL statement corresponding to the query condition according to the first configuration table and the query condition; wherein the first configuration table includes: conditional SQL statements corresponding to one or more query conditions respectively; using the conditional SQL statement, the first identification information and the field to be queried to replace the default condition field, the default table name and the default query field in the default SQL statement respectively; wherein the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
[0120] In one embodiment of the present invention, when there are multiple fields to be queried, the SQL statement generation module 502 is used to insert a separator between every two fields to be queried, so as to splice the multiple fields to be queried through the separator, and replace the default query field with the spliced fields to be queried.
[0121] In one embodiment of the present invention, the query condition includes any one or more of the following: the data type of the field to be queried, the data condition matching method and the condition value.
[0122] In one embodiment of the present invention, when the query condition includes the data type of the field to be queried, and the first configuration table also includes conditional SQL statements corresponding to each data type, the SQL statement generation module 502 is used to determine the conditional SQL statement corresponding to the data type from the first configuration table according to the data type of the field to be queried; replace the default condition field with the determined conditional SQL statement to generate the target SQL statement.
[0123] In one embodiment of the present invention, the data query device 500 also includes an update module 504; wherein the update module 504 is used to add the new query condition and the conditional SQL statement corresponding to the new query condition to the first configuration table when the new query condition is obtained.
[0124] In one embodiment of the present invention, the SQL statement generation module 502 is used to call a second configuration table according to the data query request; the second configuration table includes: second identification information of one or more business data tables, and a mapping relationship between the second identification information and a business field; determine the second identification information that is the same as the first identification information from the second configuration table, and determine the business field corresponding to the second identification information according to the mapping relationship; based on the determined business field, check whether the field to be queried is legal, and if so, execute the generation of the target SQL statement based on the first identification information, the field to be queried and the query condition.
[0125] In one embodiment of the present invention, the second configuration table also includes: an alias of the business field, and a mapping relationship between the alias and the business field and the second identification information respectively; when it is determined according to the second configuration table that the field to be queried is an alias of the business field, the SQL statement generation module 502 is used to determine the business field corresponding to the field to be queried according to the mapping relationship, and generate the target SQL statement according to the determined business field.
[0126] In one embodiment of the present invention, the update module 504 is further used to correspondingly update the second identifier and / or the mapping relationship in the second configuration table when the business data table in the database and / or the business field in the business data table is updated.
[0127] In one embodiment of the present invention, the query module 503 is used to convert the format of the queried business data according to the data format required by the query end that sends the data query request, and return the format-converted business data to the query end.
[0128] According to a data query device of an embodiment of the present invention, it can be seen that according to the first identification information of the business data table to be queried in the data query request, the field to be queried and the query condition, the target SQL statement is automatically generated, and the data query is further performed through the automatically generated target SQL statement. Thus, the target SQL statement is automatically generated according to the real-time data query request, without the need for developers to manually modify the SQL statement according to the change of the data table, query field or query condition in the database, and no longer need to re-encode, test and go online according to the modified SQL statement, thereby improving the development efficiency of the data query method and the efficiency of data query. In addition, when a new business data table and / or a new business field is added, the second identification information and / or the new business field of the new business data table are added to the second configuration table (such as t_table_config), which can support the query of business data, and finally realize a reusable single-table dynamic query device, improve research and development efficiency, and reduce the development cycle of the project.
[0129] Figure 6 An exemplary system architecture 600 is shown to which a method or an apparatus for data query according to an embodiment of the present invention may be applied.
[0130] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0131] The user can use the terminal devices 601, 602, 603 to interact with the server 605 through the network 604 to receive or send messages, etc. Various communication client applications can be installed on the terminal devices 601, 602, 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0132] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.
[0133] The server 605 may be a server that provides various services, such as a backend management server that provides support for shopping websites browsed by users using the terminal devices 601, 602, and 603. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information and product information) to the terminal device.
[0134] It should be noted that a data query method provided in an embodiment of the present invention is generally executed by the server 605 , and accordingly, a data query device is generally disposed in the server 605 .
[0135] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to the implementation requirements.
[0136] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0137] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0138] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.
[0139] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.
[0140] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer 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 disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0141] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0142] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes a request acquisition module, an SQL statement generation module and a query module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the request acquisition module may also be described as a "module for acquiring data query requests".
[0143] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: obtaining a data query request, the data query request includes: first identification information of the business data table to be queried, the field to be queried, and the query condition; generating a target SQL statement according to the first identification information, the field to be queried, and the query condition; determining the business data table corresponding to the first identification information in the database according to the target SQL statement, and determining the business data corresponding to the field to be queried from the business data table according to the query condition.
[0144] According to the technical solution of the embodiment of the present invention, a target SQL statement is automatically generated according to the first identification information of the business data table to be queried in the data query request, the field to be queried and the query condition, and the data query is further performed through the automatically generated target SQL statement. In this way, the target SQL statement is automatically generated according to the real-time data query request, without the need for developers to manually modify the SQL statement according to the changes in the data table, query field or query condition in the database, and without the need to re-encode, test and launch the modified SQL statement, thereby improving the development efficiency of the data query method and the efficiency of data query.
[0145] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / condition> < / string> < / condition> < / string>
Claims
1. A data query method, characterized in that: include: Obtaining a data query request, the data query request including: first identification information of a business data table to be queried, a field to be queried, and a query condition; Generate a target SQL statement according to the first identification information, the field to be queried and the query condition; According to the target SQL statement, determine in a database a business data table corresponding to the first identification information, and determine from the business data table business data corresponding to the field to be queried according to the query condition; The generating of the target SQL statement comprises: According to the first configuration table and the query condition, a conditional SQL statement corresponding to the query condition is generated; and the default condition field, default table name and default query field in the default SQL statement are replaced by the conditional SQL statement, the first identification information and the field to be queried respectively.
2. The method according to claim 1, characterized in that The first configuration table includes: conditional SQL statements corresponding to one or more query conditions; the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
3. The method according to claim 2, characterized in that When there are multiple fields to be queried, generating a conditional SQL statement corresponding to the query condition includes: A separator is inserted between every two of the fields to be queried, so as to splice the multiple fields to be queried through the separator, and the spliced fields to be queried are used to replace the default query field.
4. The method according to claim 2, characterized in that: The query condition includes any one or more of the following: the data type of the field to be queried, the data condition matching method and the condition value.
5. According to the method of claim 4, when the query condition includes the data type of the field to be queried, and the first configuration table also includes conditional SQL statements corresponding to each data type, According to the data type of the field to be queried, determining a conditional SQL statement corresponding to the data type from the first configuration table; The determined conditional SQL statement replaces the default condition field to generate the target SQL statement.
6. The method according to claim 2, characterized in that Also includes: When a new query condition is obtained, the new query condition and a conditional SQL statement corresponding to the new query condition are added to the first configuration table.
7. The method according to claim 1, characterized in that Before generating the target SQL statement, the method further includes: According to the data query request, calling a second configuration table; the second configuration table includes: second identification information of one or more business data tables, and a mapping relationship between the second identification information and a business field; Determine, from the second configuration table, second identification information that is identical to the first identification information, and determine, according to the mapping relationship, a service field corresponding to the second identification information; According to the determined business field, verify whether the field to be queried is legal, and if so, generate a target SQL statement according to the first identification information, the field to be queried and the query condition.
8. The method according to claim 7, characterized in that The second configuration table further includes: an alias of the business field, and a mapping relationship between the alias and the business field and the second identification information; when it is determined according to the second configuration table that the field to be queried is an alias of the business field, According to the mapping relationship, the business field corresponding to the field to be queried is determined, and according to the determined business field, the target SQL statement is generated.
9. The method according to claim 7, characterized in that: When the business data table in the database and / or the business field in the business data table is updated, the second identifier and / or the mapping relationship is correspondingly updated in the second configuration table.
10. The method according to claim 1, characterized in that Also includes: The format of the queried business data is converted according to the data format required by the query end that sends the data query request, and the business data after the format conversion is returned to the query end.
11. A data query device, characterized in that: include: Request acquisition module, SQL statement generation module and query module; among them, The request acquisition module is used to acquire a data query request, wherein the data query request includes: first identification information of a business data table to be queried, a field to be queried, and a query condition; The SQL statement generating module is used to generate a target SQL statement according to the first identification information, the field to be queried and the query condition; The query module is used to determine the business data table corresponding to the first identification information in the database according to the target SQL statement, and determine the business data corresponding to the field to be queried from the business data table according to the query condition; The SQL statement generation module is used to generate a conditional SQL statement corresponding to the query condition according to the first configuration table and the query condition; and use the conditional SQL statement, the first identification information and the field to be queried to replace the default condition field, the default table name and the default query field in the default SQL statement respectively.
12. The device according to claim 11, characterized in that The first configuration table includes: conditional SQL statements corresponding to one or more query conditions; the default SQL statement includes: the default condition field, the default table name, the default query field and the connection field.
13. An electronic device for data query, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 10.
14. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.
Citation Information
Patent Citations
Program code automatic generator of computer
CN101419544A
Method for automatically generating SQL statements and electronic device thereof
CN102073642A