Data query method, system, electronic device and storage medium

By preserving multiple database information and preset query expressions in the data query method and system, a query expression adapted to the target database is generated, which solves the problems of high development costs and low data security caused by differences in different database languages, and realizes the Efficient and secure data query.

CN114547080BActive Publication Date: 2024-12-20CHONGQING UNISINSIGHT TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210172125.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-24
Publication Date
2024-12-20
Estimated Expiration
2042-02-24

AI Technical Summary

Technical Problem

In the development of traditional application software RESTFUL interface, due to the language differences between different databases, the development and maintenance of interfaces consume a lot of time and costs, and the direct way of passing in query statements can easily lead to data leakage and reduced security.

Method used

Provide a data query method and system, by pre-stored multiple types of data source information and preset query expressions, generate query expressions adapted to the target database, obtain data from the target database, and send the results to the client to block it. The differences in structured query languages ​​of different databases improve the universality and expansion of interfaces and ensure data security.

Benefits of technology

It reduces interface development costs, improves interface universality and expansion, and ensures data security through abstract query processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114547080B_ABST
    Figure CN114547080B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of data processing, and provides a data query method, system, electronic device and storage medium. The electronic device pre-stores multiple types of data source information, each data source information includes the information of the corresponding database and the data source identifier; after listening to the request of the client, the request is parsed to obtain the target data source identifier representing the target database and the query parameters representing the query conditions; then, according to the target data source identifier, the query parameters and the preset query expression, the preset query expression represents the preset query rules adapted to multiple types of databases, and the target query expression corresponding to the target database is generated; then, according to the target query expression, the target data that meets the query conditions is obtained from the target database; finally, the query result is obtained according to the target data and sent to the client. Through the query expression, the differences between different database languages ​​are shielded, the versatility and expansibility of the interface are improved, the development cost is reduced, and the security of the data is ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing, and in particular to a data query method, system, electronic device and storage medium. Background Art

[0002] Traditional application software RESTFUL interface development basically uses hard coding to access the database and obtain the required business data. Due to the differences in the languages ​​of different databases, the development and maintenance of interfaces will consume a lot of time and cost. Summary of the invention

[0003] In view of this, an object of the present invention is to provide a data query method, system, electronic device and storage medium.

[0004] In order to achieve the above purpose, the technical solution adopted by the embodiment of the present invention is as follows:

[0005] In a first aspect, the present invention provides a data query method, which is applied to an electronic device, wherein the electronic device is connected to a client for communication, and the electronic device pre-stores multiple types of data source information, each of which includes information of a corresponding database and a data source identifier; the method comprises:

[0006] After monitoring the request of the client, the request is parsed to obtain a target data source identifier and a query parameter; the target data source identifier corresponds to a target database; and the query parameter represents a query condition;

[0007] Generate a target query expression corresponding to the target database according to the target data source identifier, the query parameter and a preset query expression; the preset query expression represents a preset query rule adapted to multiple types of databases;

[0008] According to the target query expression, acquiring target data that meets the query condition from the target database;

[0009] A query result is obtained according to the target data and sent to the client.

[0010] In an optional implementation, the preset query expression includes a database attribute item and a plurality of rule attribute items, each of the rule attribute items has a corresponding rule parameter, and the rule parameter has a corresponding data structure type; the query parameter includes a target rule parameter and its parameter value;

[0011] The step of generating a target query expression corresponding to the target database according to the target data source identifier, the query parameters and the preset query expression comprises:

[0012] Convert the parameter value of the target rule parameter into its corresponding data structure type to obtain the target object;

[0013] Taking the target object as the value of a target rule attribute item, the target rule attribute represents a rule attribute item corresponding to the target rule parameter;

[0014] The target data source identifier is used as the value of the database attribute item to obtain the target query expression.

[0015] In an optional implementation, the plurality of rule attribute items include a paging rule attribute item;

[0016] The step of acquiring target data that meets the query condition from the target database according to the target query expression includes:

[0017] If the target rule attribute item includes the paging rule attribute item, a paging total query statement and a paging list query statement corresponding to the target database are generated according to the target query expression;

[0018] Execute the total number of pages query statement to obtain the total number of pages from the target database;

[0019] Execute the paging list query statement to obtain list data from the target database;

[0020] The total number of pages and the list data are combined to obtain the target data.

[0021] In an optional implementation, the plurality of rule attribute items include a paging rule attribute item;

[0022] The step of acquiring target data that meets the query condition from the target database according to the target query expression includes:

[0023] If the target rule attribute item does not include the paging rule attribute item, a non-paged query statement corresponding to the target database is generated according to the target query expression;

[0024] The non-paged query statement is executed to obtain the target data from the target database.

[0025] In an optional implementation manner, the electronic device further pre-stores a data source connection pool, the data source connection pool being used to cache data source connections for accessing the database, and each of the data source information includes connection information of each of the databases;

[0026] The step of executing the non-paged query statement to obtain the target data from the target database includes:

[0027] If there is a target data source connection for accessing the target database in the data source connection pool, access the target database through the target data source connection, execute the non-paged query statement, and obtain the target data;

[0028] If the target data source connection for accessing the target database does not exist in the data source connection pool, the target data source connection is created according to the connection information of the target database, and the target database is accessed through the target data source connection, the non-paged query statement is executed, and the target data is obtained.

[0029] In an optional implementation manner, the target data includes a data type; the request includes a request type, and the request type includes a synchronous type and an asynchronous type;

[0030] The step of obtaining a query result according to the target data and sending the query result to the client comprises:

[0031] Convert the data type of the target data into a preset data type to obtain the query result;

[0032] If the request type in the request is the synchronization type, sending the query result to the client;

[0033] If the request type in the request is the asynchronous type, the request further includes a data receiving address, and the query result is sent to the client according to the data receiving address.

[0034] In a second aspect, the present invention provides a data query system, the data query system comprising: a data source management module and a general query module;

[0035] The data source management module is used to store various types of data source information, each of which includes corresponding database information and a data source identifier;

[0036] The general query module is used to monitor the request of the client application, parse the request, and obtain the target data source identifier and query parameters; the target data source identifier corresponds to the target database; the query parameters represent the query conditions; the client application is an application that supports the Hypertext Transfer Protocol;

[0037] Generate a target query expression corresponding to the target database according to the target data source identifier, the query parameter and a preset query expression; the preset query expression represents a preset query rule adapted to multiple types of databases;

[0038] According to the target query expression, acquiring target data that meets the query condition from the target database;

[0039] A query result is obtained according to the target data and sent to the client application.

[0040] In an optional implementation, the preset query expression includes a database attribute item and a plurality of rule attribute items, each of the rule attribute items has a corresponding rule parameter, and the rule parameter has a corresponding data structure type; the query parameter includes a target rule parameter and its parameter value; the general query module is specifically used to:

[0041] Convert the parameter value of the target rule parameter into its corresponding data structure type to obtain the target object;

[0042] Taking the target object as the value of a target rule attribute item, the target rule attribute represents a rule attribute item corresponding to the target rule parameter;

[0043] The target data source identifier is used as the value of the database attribute item to obtain the target query expression.

[0044] In a third aspect, the present invention provides an electronic device, including a processor and a memory, wherein the memory stores a computer program, and when the processor executes the computer program, the method described in any one of the aforementioned embodiments is implemented.

[0045] In a fourth aspect, the present invention provides a storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in any one of the aforementioned embodiments.

[0046] The data query method, system, electronic device and storage medium provided by the embodiment of the present invention are pre-stored with various types of data source information in the electronic device, each data source information includes the information of the corresponding database and the data source identifier; the electronic device listens to the request of the client, parses the request to obtain the target data source identifier representing the target database and the query parameter representing the query condition; then, according to the target data source identifier, the query parameter and the preset query expression, the preset query expression represents the preset query rule adapted to various types of databases, generates the target query expression corresponding to the target database; then, according to the target query expression, obtains the target data that meets the query condition from the target database; finally, obtains the query result according to the target data and sends it to the client. Through the query expression, the differences of structured query languages ​​of different databases are shielded, the versatility and extensibility of the interface are improved, and the interface development cost is reduced. And compared with the method of directly passing in the query statement, the security of the data is ensured.

[0047] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0049] Figure 1 A block diagram of a data query system provided by an embodiment of the present invention is shown;

[0050] Figure 2 A schematic diagram of a general query module provided by an embodiment of the present invention is shown;

[0051] Figure 3 A block diagram of an electronic device provided by an embodiment of the present invention is shown;

[0052] Figure 4 A schematic diagram of a flow chart of a data query method provided by an embodiment of the present invention is shown;

[0053] Figure 5 Another schematic diagram of a flow chart of a data query method provided by an embodiment of the present invention is shown;

[0054] Figure 6 Another schematic diagram of a flow chart of a data query method provided by an embodiment of the present invention is shown;

[0055] Figure 7 Another schematic diagram of a flow chart of a data query method provided by an embodiment of the present invention is shown;

[0056] Figure 8 Another schematic diagram of a flow chart of a data query method provided by an embodiment of the present invention is shown;

[0057] Fig. 9 An example diagram of a data query method provided by an embodiment of the present invention is shown.

[0058] Icon: 300 - data query system; 310 - data source management module; 330 - general query module; 100 - electronic device; 110 - bus; 120 - processor; 130 - memory; 170 - communication interface. DETAILED DESCRIPTION

[0059] The following will be combined with the accompanying drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.

[0060] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present invention.

[0061] It should be noted that relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0062] Traditional application software RESTFUL interface development basically uses hard coding to access the database and obtain the required business data. Due to the differences in the languages ​​of different databases, the development and maintenance of interfaces will consume a lot of time and cost. Although there is currently a method of directly passing database query statements through the interface to reduce the cost of interface development, this method is prone to data leakage and illegal tampering, reducing the security of data. Furthermore, an embodiment of the present invention provides a data query method to solve the above problems.

[0063] Please refer to Figure 1 , a schematic diagram of a data query system provided by an embodiment of the present invention, wherein the data query system 300 includes a data source management module 310 and a general query module 330 .

[0064] The data source management module 310 is used to store various types of data source information, each of which includes the corresponding database information and data source identification; it can provide data source information and sensitive data table information configuration query function.

[0065] Optionally, the database may be a relational database such as MySQL, PostgreSQL, Oracle, SQL Server, etc. A relational database refers to a database based on a relational model, which processes data with the help of mathematical concepts and methods such as set algebra.

[0066] The general query module 330 is used to monitor the requests of the client application, parse the requests, and obtain the target data source identifier and query parameters; the target data source identifier corresponds to the target database; the query parameters represent the query conditions; based on the target data source identifier, query parameters and preset query expressions, a target query expression corresponding to the target database is generated; the preset query expression represents a preset query rule adapted to various types of databases; based on the target query expression, target data that meets the query conditions is obtained from the target database; the query results are obtained based on the target data and sent to the client application.

[0067] The client application is an application that supports the Hypertext Transfer Protocol, which can be a WEB application, an APP application or other applications. A WEB application refers to a network application developed based on a browser; an APP application is an application developed based on various mobile terminals; and other applications refer to other network applications.

[0068] See also Figure 2 ,The general query module includes an HTTP interface control unit, an ,execution engine unit, an HTTP parameter parsing unit, an SQL parsing unit, an SQL ,execution unit, a data conversion unit and a data pushing unit.

[0069] The HTTP interface control unit is used to receive query requests sent by the client and pass the query requests to the execution engine unit; the execution engine unit is used to control other units to execute corresponding functions; the HTTP parameter parsing unit is used to parse the query requests sent by the client; the SQL parsing unit is used to convert the query expression into the query statement of the corresponding database; the SQL execution unit is used to submit the query statement to the corresponding database and obtain the execution result; the data conversion unit is used to convert the data type of the execution result into the JSON data type, and at the same time blur the key information of the sensitive data; the data push unit is used to push the query result to the client's specified address during asynchronous execution, and the specified address can be an HTTP interface, a message middleware, a Web Socket, etc.

[0070] See also Figure 3 , is a block diagram of an electronic device provided by an embodiment of the present invention. The electronic device 100 includes a bus 110 , a processor 120 , a memory 130 , and a communication interface 170 .

[0071] The bus 110 may be a circuit that connects the above elements to each other and transfers communications (eg, control messages) between the above elements.

[0072] The processor 120 may receive commands from the other elements described above (eg, the memory 130 , the communication interface 170 , etc.) through the bus 110 , may interpret the received commands, and may perform calculations or data processing according to the interpreted commands.

[0073] The processor 120 may be an integrated circuit chip with signal processing capabilities. The processor 120 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.

[0074] The memory 130 may store commands or data received from the processor 120 or other elements (eg, the communication interface 170 , etc.) or commands or data generated by the processor 120 or other elements.

[0075] The memory 130 can be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable read-only memory (EEPROM), etc.

[0076] The communication interface 170 may be used to communicate signaling or data with other node devices.

[0077] Understandably, Figure 3 The structure shown is only a schematic diagram of the structure of the electronic device 100. The electronic device 100 may also include Figure 3 More or fewer components as shown, or with Figure 3 Different configurations shown. Figure 3 Each component shown in the figure can be implemented by hardware, software or a combination thereof.

[0078] The above-mentioned electronic device 100 will be used as an execution subject to execute each step in each method provided in the embodiment of the present invention and achieve corresponding technical effects.

[0079] See also Figure 4 , Figure 4 It is a flowchart of a data query method provided by an embodiment of the present invention.

[0080] Step S202, after monitoring the client's request, parse the request to obtain the target data source identifier and query parameters;

[0081] Among them, the target data source identifier corresponds to the target database; the query parameter represents the query condition.

[0082] In this embodiment, the electronic device pre-stores multiple types of data source information, each of which includes corresponding database information and a data source identifier;

[0083] Each database has a corresponding data source identifier, and each database stores data.

[0084] When the electronic device monitors a request from a client, such as an HTTP (Hypertext Transfer Protocol) request, the electronic device creates a request execution thread for processing the request from the client. During the execution of the request execution thread, the request is parsed to obtain a target data source identifier and query parameters.

[0085] The database corresponding to the target data source identifier is the target database, which means obtaining data from the target database. The query parameter represents the query condition, which can be understood as obtaining data that meets the query condition.

[0086] Step S204, generating a target query expression corresponding to the target database according to the target data source identifier, query parameters and preset query expression;

[0087] The preset query expression represents a preset query rule adapted to various types of databases;

[0088] In this embodiment, in order to query different databases based on the universal query interface, a preset query expression is set, which represents a query rule that is suitable for various types of databases.

[0089] According to the target data source identifier, query parameters and preset query expressions, a target query expression can be generated, which can be understood as a semantic abstraction of data query, and the target query expression includes information about the target database and query conditions.

[0090] Optionally, the request includes a request type, and the request type includes a synchronous type and an asynchronous type. If the request type is a synchronous type, the following steps are continued in the current request execution thread.

[0091] If the request type is asynchronous, a message indicating that the request has been successfully received is returned to the client in the current request execution thread, and the request execution thread is terminated. A new asynchronous thread is created to execute the following steps through the asynchronous thread. It can be seen that for asynchronous requests, the subsequent steps are processed through the asynchronous thread, which can clear the temporary data in the memory of the request execution thread and release the thread resources it occupies, thereby effectively improving the efficiency of data query.

[0092] Step S206, acquiring target data that meets the query condition from the target database according to the target query expression;

[0093] In this embodiment, a target query statement corresponding to the target database can be generated according to the target query expression, and the target query statement can be executed to obtain target data that meets the query conditions from the target database.

[0094] It can be understood that the target query expression abstracts the semantics of this data query, and then generates a target query statement based on the target query expression, which converts the semantics into the syntax corresponding to the target database. By executing the target query statement, the qualified data, i.e., the target data, can be obtained from the target database. It can be seen that the target query expression can shield the differences in structured query languages ​​of different databases, thereby improving the versatility and extensibility of the interface.

[0095] Step S208, obtaining query results according to the target data and sending them to the client;

[0096] In this embodiment, a query result can be obtained according to the target data, and then the query result is sent to the client, thus completing the data query.

[0097] It can be seen from the above steps that the electronic device listens to the client's request, parses the request to obtain the target data source identifier representing the target database and the query parameters representing the query conditions; then obtains the target query expression corresponding to the target database based on the target data source identifier, query parameters and preset query expression, which represents the preset query rules adapted to various types of databases; then obtains the target data that meets the query conditions from the target database based on the target query expression; finally, obtains the query result based on the target data and sends it to the client. Through the query expression, the differences in structured query languages ​​of different databases are shielded, the versatility and extensibility of the interface are improved, and the interface development cost is reduced. And compared with the method of directly passing in query statements, the security of the data is ensured.

[0098] Optionally, in order to further ensure the security of data, for the above step S204, the embodiment of the present invention provides a possible implementation method. Figure 5 , wherein step S204 includes the following steps:

[0099] Before introducing the specific steps, it should be noted that the preset query expression provided by the embodiment of the present invention includes a database attribute item and a plurality of rule attribute items, and each rule attribute item has a corresponding rule parameter.

[0100] It is understandable that the structure of the query parameters parsed based on the request can be f_ <rule> = <value>, where f represents a specific character, rule represents a rule parameter, and value represents the parameter value of the rule parameter.

[0101] The specific characters are used to filter illegal data in the request, that is, when parsing the request parameters, only parameters starting with specific characters such as "f" can be obtained, and then the rule parameters can be obtained to ensure the security of the data. The specific characters can be designed according to the actual application, and the embodiment of the present invention is not limited.

[0102] The rule parameters and their parameter values ​​are used to represent the query conditions. For ease of understanding, an example is provided in the embodiment of the present invention, as shown in the following Table 1:

[0103] Table 1

[0104]

[0105]

[0106] In the above Table 1, "<>" indicates a dynamic mandatory parameter, and "[]" indicates a dynamic optional parameter.

[0107] The parameter table indicates the table name; the parameter column indicates the column name; the parameter alias indicates the alias; the parameter position indicates the corresponding rule position, the initial value is 1, and each rule changes independently.

[0108] The parameter joinType indicates the association type, including inner (inner join), left (left join), right (right join), and full (full join).

[0109] The parameter operator represents the comparison operator, including eq (equal to), neq (not equal to), gt (greater than), gteq (greater than or equal to), lt (less than), lteq (less than or equal to), in (inclusive), nin (not included), and btw (interval).

[0110] function represents a function and supports common SQL functions in databases.

[0111] The parameter aggregate represents the aggregation function, including count (statistics), sum (sum), avg (average), min (minimum value), and max (maximum value).

[0112] The parameter order indicates the sorting, including asc (ascending order) and desc (descending order).

[0113] The parameter start indicates the starting value; the parameter offset indicates the offset; the parameter object indicates that the value can be a character, a number, or a Boolean.

[0114] In this embodiment, the preset query expression can be represented by a QueryExpression object, the database attribute item can be represented by database, and multiple rule attribute items include: the output column rule attribute item can be represented by items, the query main table and related table rule attribute item can be represented by from, the filtering rule attribute item can be represented by where, the grouping rule attribute item can be represented by groupBy, the group filtering rule attribute item can be represented by having, the sorting rule attribute item can be represented by orderBy, and the paging rule attribute item can be represented by limit.

[0115] Based on the rule parameters and preset query expressions given above, a general method for constructing a target query expression is given below.

[0116] The target data source identifier parsed from the request is stored in the database attribute item database of the QueryExpression object;

[0117] Get the parameter value of the rule parameter starting with "f_item_" from the request, get the output column rule list, and convert the output column rule list into List <queryitem>Object, which stores the output column rule attribute items of the QueryExpression object;

[0118] Get the parameter values ​​of the rule parameters starting with "f_from_", "f_join_", and "f_on_" from the request, convert the parameter values ​​into a single linked list JoinExresion object, and store them in the query main table and association table rule attribute item from of the QueryExpression object;

[0119] Get the parameter value of the rule parameter starting with "f_where_" from the request, get the filter rule list, convert the filter rule list into a doubly linked list ConditionExpression object, and store it in the filter rule attribute item where of the QueryExpression object;

[0120] Get the parameter value of the rule parameter starting with "f_gby_" from the request, get the grouping rule list, and convert the grouping rule list into List <columnexpression>Object, stored in the grouping rule attribute item groupBy of the QueryExpression object;

[0121] Get the parameter value of the rule parameter starting with "f_hav_" from the request, get the grouping filter rule list, convert the grouping filter rule list into a doubly linked list ConditionExpression object, and store it in the grouping filter rule attribute item having of the QueryExpression object;

[0122] Get the parameter value of the rule parameter starting with "f_oby_" from the request, get the sorting rule list, and convert the grouping sorting rule list into a List <orderitem>Object, stored in the sorting rule attribute item orderBy of the QueryExpression object;

[0123] The parameter value of the rule parameter starting with "f_limit" is obtained from the request to obtain the paging rule, and the paging rule parameter is converted into a Limit object, which is stored in the paging rule attribute item limit of the QueryExpression object.

[0124] To facilitate understanding, an embodiment of the present invention provides an example, which queries employee information and department information of employee type 1 and employee age between 40 and 55 years old based on the two tables of staff and department, and returns the first page of data by sorting in descending order based on age.

[0125] The main fields of the employee table are shown in Table 2 below:

[0126] Table 2

[0127] Fields type illustrate number long Employee Number name varchar Name age int age type int Employee Type department long Department

[0128] The main fields of the department table are shown in Table 3 below:

[0129] Table 3

[0130]

[0131]

[0132] Step S204-1, converting the parameter value of the target rule parameter into its corresponding data structure type to obtain the target object;

[0133] In this embodiment, the query parameters obtained by parsing the request include target rule parameters and parameter values ​​thereof.

[0134] For example, the target rule parameters and their parameter values ​​obtained based on the request are as follows:

[0135] f_item_1=s.number_as_staff_num,

[0136] f_item_2=s.name_as_staff_name,

[0137] f_item_3=s.age, f_item_4=s.type,

[0138] f_item_5=d.number_as_dept_num,

[0139] f_item_6=d.name_as_dept_name,

[0140] f_from_1 = staff_as_s,

[0141] f_join_1_left=department_as_d,

[0142] f_on_1_eq_s.department=d.number,

[0143] f_where_eq_s.type = 1,

[0144] f_where_btw_s.age=40,55,

[0145] f_oby_1 = s.age_desc,

[0146] f_limit=0,10

[0147] It is understandable that if a rule position is marked as 1, it can be omitted, such as f_from_1=staff_as_s in the above example can be abbreviated to f_from=staff_as_s. The above example is only used to explain the embodiment of the present invention, and does not constitute a limitation on the present invention.

[0148] Convert the parameter value of the above rule parameter into the corresponding data structure type to obtain the target object. That is, based on the parameter value of the rule parameter item, the target object is List <queryitem>object; based on the parameter values ​​of the rule parameters from, join, and on, the target object is the JoinExpression object; based on the parameter value of the rule parameter where, the target object is the ConditionExpression object; based on the parameter value of the rule parameter oby, the target object is the List <orderitem>object; based on the parameter value of the rule parameter limit, the target object, i.e., the Limit object, is obtained.

[0149] Step S204-3, taking the target object as the value of the target rule attribute item, where the target rule attribute represents the rule attribute item corresponding to the target rule parameter;

[0150] Step S204-5: Use the target data source identifier as the value of the database attribute item to obtain the target query expression.

[0151] In this embodiment, the rule attribute items corresponding to the target rule parameters, namely the target rule attribute items, include output column rule attribute item items, query main table and associated table rule attribute item from, filter rule attribute item where, sorting rule attribute item orderBy and paging rule attribute item limit.

[0152] Each target object is used as the value of its corresponding target rule attribute item. <queryitem>The object is stored in the output column rule attribute item items as the value of the output column rule attribute item items; the JoinExpression object is used as the value of the query main table and associated table rule attribute item from; the ConditionExpression object is used as the value of the filtering rule attribute item where; the List <orderitem>The object is used as the value of the sorting rule attribute item orderBy; the Limit object is used as the value of the paging rule attribute item limit.

[0153] At the same time, the target data source identifier is used as the value of the database property item database to obtain the target query expression.

[0154] Optionally, for the above step S206, the embodiment of the present invention provides two possible implementation methods, one is a non-paged query implementation method, and the other is a paged query implementation method. The implementation method of the non-paged query is first described below, please refer to Figure 6 , wherein step S206 includes the following steps:

[0155] Step S206A-1, if the target rule attribute item does not include the paging rule attribute item, then generate a non-paged query statement corresponding to the target database according to the target query expression;

[0156] In this embodiment, if the target rule attribute item of the target query expression does not include the paging rule attribute item, indicating that the data query is a non-paged query, a non-paged query statement corresponding to the target database, i.e., a non-paged query SQL statement, can be generated according to the target query expression.

[0157] Step S206A-3, execute the non-paged query statement to obtain target data from the target database.

[0158] In this embodiment, the non-paged query SQL statement is executed, that is, the non-paged query SQL statement is submitted to the target database and executed to obtain the execution result, that is, the target data that meets the query condition is obtained from the target database.

[0159] Optionally, for step S206A-3, an embodiment of the present invention provides a possible implementation method. Step S206A-3 may include the following steps:

[0160] S206A-3-1, if there is a target data source connection for accessing the target database in the data source connection pool, access the target database through the target data source connection, execute a non-paged query statement, and obtain the target data;

[0161] It should be noted that the electronic device also pre-stores a data source connection pool; the data source connection pool is used to cache data source connections for accessing the database; each data source information includes connection information of each database.

[0162] In this embodiment, if there is a target data source connection for accessing the target database in the data source connection pool, the target database can be accessed through the target data source connection, the non-paged query SQL statement can be executed, and the execution result of the target database, that is, the target data, can be obtained.

[0163] S206A-3-3, if the target data source connection for accessing the target database does not exist in the data source connection pool, create a target data source connection according to the connection information of the target database, access the target database through the target data source connection, execute a non-paged query statement, and obtain the target data.

[0164] In this embodiment, if the target data source connection for accessing the target database does not exist in the data source connection pool, a target data source connection is created according to the data source information of the target database, which can be understood as creating a target data source connection according to the connection information for logging into the target database; then the target data source connection is stored in the data source connection pool, and the target database is accessed through the target data source connection, the non-paged query SQL statement is executed, and the execution result of the target database, that is, the target data, is obtained.

[0165] The following is an explanation of how to implement paging queries. Figure 7 , wherein step S206 includes the following steps:

[0166] Step S206B-1, if the target rule attribute item includes a paging rule attribute item, then generating a paging total query statement and a paging list query statement corresponding to the target database according to the target query expression;

[0167] In an embodiment of the present invention, if the target rule attribute item of the target query expression includes a paging rule attribute item, indicating that the data query this time is a paging query, then a paging total query statement, i.e., a paging total query SQL statement, and a paging list query statement, i.e., a paging list data query SQL statement corresponding to the target database can be generated according to the target query expression.

[0168] Step S206B-3, execute a query statement for the total number of pages to obtain the total number of pages from the target database;

[0169] Step S206B-5, execute the paging list query statement to obtain the list data from the target database;

[0170] In this embodiment, two asynchronous threads may be created to respectively execute a paging total query statement and a paging list query statement.

[0171] The way to execute the paginated total query statement and the paginated list query statement is similar to the way to execute the non-paged query statement in the above example. That is, determine whether the target data source connection exists in the data source connection pool. If not, create the target data source connection based on the connection information of the target database.

[0172] Access the target database through the target data source connection, execute the total number of pages query statement, that is, the total number of pages query SQL statement, and obtain the execution result of the target database, that is, obtain the total number of pages from the target database.

[0173] Access the target database through the target data source connection, execute the paging list query statement, that is, the paging list data query SQL statement, and obtain the execution result of the target database, that is, obtain the list data from the target database.

[0174] Step S206B-7, merge the total number of pages and the list data to obtain the target data.

[0175] In this embodiment, after the two asynchronous threads are executed, the acquired total number of pages and list data are merged to obtain the target data.

[0176] Optionally, for the above step S208, the embodiment of the present invention provides a possible implementation method, please refer to Figure 8 , wherein step S208 includes the following steps:

[0177] Step S208-1, converting the data type of the target data into a preset data type to obtain a query result;

[0178] It is understandable that different databases store data of different data types, and the data type of the target data corresponds to the target database.

[0179] In this embodiment, the data type of the target data can be converted into a preset data type such as a JSON data type to obtain a query result.

[0180] Optionally, in order to further ensure data security, in addition to converting the data type of the target data into the JSON data type, key information of sensitive data configured with sensitive tags may also be fuzzified to obtain query results.

[0181] Step S208-3A, if the request type in the request is a synchronous type, the query result is sent to the client;

[0182] Step S208-3B, if the request type in the request is an asynchronous type, the request also includes a data receiving address, and the query result is sent to the client according to the data receiving address.

[0183] In this embodiment, the request includes a request type, and the request type includes a synchronous type and an asynchronous type. If the request type in the request is a synchronous type, the query result is sent to the client; if the request type in the request is an asynchronous type, the request also includes a data receiving address provided by the client, and the query result is pushed to the data receiving address.

[0184] It should be noted that the data query method provided in the embodiment of the present invention can also be applied to data query of non-relational databases such as Elasticsearch and mongodb databases, as well as other non-relational databases that support single-table search scenarios.

[0185] Optionally, the embodiment of the present invention also provides an example diagram of an SQL expression class, see Fig. 9 , which includes the relationship between QueryExpression, QueryItem, JoinExpression, ColumnExpression, ConditionExpression, OrderItem, Limit and other objects in the embodiment of the present invention. In the figure, the solid line of the hollow arrow represents inheritance, the solid line of the pointed arrow represents dependency, and the dotted line of the hollow arrow represents interface implementation.

[0186] SQLExpression is the top-level expression interface class, and its direct subclasses include QueryExpression, TableExpression, JoinExpression, ConditionExpression, FunctionExpression, ValueExpression, and Limit.

[0187] The subclass QueryExpression is a query expression object. The database attribute stores the data source identifier, the items attribute stores the query data table column information, the from attribute stores the query main table information and related table information, the where attribute stores the query conditions, the groupBy attribute stores the group query information, the having attribute stores the group query conditions, the orderBy attribute stores the result sorting information, and the limit attribute stores the paging information.

[0188] The subclass TableExpression is a data table expression. The attribute schema stores the table schema, the attribute tableName stores the table name, and the attribute tableAlias ​​stores the table alias. Its direct subclasses are ColumnExpression, and its indirect subclasses are OrderItem and QueryItem. ColumnExpression is a data table column expression, inheriting all the properties of TableExpression. Its own attribute columnName stores the column name, and the attribute columnAlias ​​stores the column alias. OrderItem is a sorting expression, inheriting all the properties of ColumnExpression, and its own attribute order stores the sorting method. QueryItem is a query item expression, inheriting all the properties of ColumnExpression, and its own attribute fun stores SQL function information, and the attribute value stores SQL value.

[0189] The subclass JoinExpression is used to query the main table, associated table, and associated condition expression. It uses a one-way linked list to store associated table information. The attribute type stores the associated type, the attribute on stores the associated condition, the attribute table stores the table information, and the attribute join stores other associated table information.

[0190] The subclass ConditionExpression is an abstract class of conditional expressions. It uses a bidirectional linked list to store condition information. The attribute left stores the left condition, and the attribute right stores the right condition. Its direct subclasses AndExpression, OrExpression, and CompareExpression all rely on the SQL operator enumeration class SQLOperator. AndExpression is an AND expression, inheriting all the properties of ConditionExpression, and the attribute operation value is the AND operator; OrExpression is an OR expression, inheriting all the properties of ConditionExpression, and the attribute operation value is the OR operator; CompareExpression is a comparison expression, and the attribute operation value is the comparison operator. The corresponding comparison operator is filled in according to the query condition, including EQ (equal to), NEQ (not equal to), GT (greater than), GTEQ (greater than or equal to), LT (less than), LTEQ (less than or equal to), IN (inclusive), NIN (not included), and BTW (interval).

[0191] The subclass FunctionExpression is a function expression. The attribute name stores the function name, and the attribute params stores the function parameters.

[0192] The subclass ValueExpression is a value object expression, the attribute value stores the value content, and the attribute type stores the database value type.

[0193] The subclass Limit is a paging expression. The attribute start stores the query start data position, and the attribute offset stores the number of data to be obtained.

[0194] An embodiment of the present invention further provides an electronic device, including a processor 120 and a memory 130. The memory 130 stores a computer program. When the processor executes the computer program, the data query method disclosed in the above embodiment is implemented.

[0195] The embodiment of the present invention further provides a storage medium on which a computer program is stored. When the computer program is executed by the processor 120, the data query method disclosed in the embodiment of the present invention is implemented.

[0196] In summary, the data query method, system, electronic device and storage medium provided by the embodiments of the present invention. The electronic device pre-stores various types of data source information, each data source information includes the information of the corresponding database and the data source identifier; when the electronic device monitors the request of the client, it parses the request to obtain the target data source identifier representing the target database and the query parameters representing the query conditions; then, according to the target data source identifier, the query parameters and the preset query expression, the preset query expression represents the preset query rules adapted to various types of databases, and generates the target query expression corresponding to the target database; then, according to the target query expression, the target data that meets the query conditions is obtained from the target database; finally, the query result is obtained according to the target data and sent to the client. Through the query expression, the differences between different database languages ​​are shielded, the versatility and extensibility of the interface are improved, the development cost is reduced, and the security of the data is ensured.

[0197] In several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical functions. 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 consecutive boxes 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 and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.

[0198] In addition, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.

[0199] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0200] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / orderitem> < / queryitem> < / orderitem> < / queryitem> < / orderitem> < / columnexpression> < / queryitem> < / value> < / rule>

Claims

1. A data query method, characterized in that: Applied to an electronic device, the electronic device is connected to a client for communication, the electronic device pre-stores multiple types of data source information, each of the data source information includes information of a corresponding database and a data source identifier; the method includes: After monitoring the request of the client, the request is parsed to obtain a target data source identifier and a query parameter; the target data source identifier corresponds to a target database; the query parameter represents a query condition; Generate a target query expression corresponding to the target database according to the target data source identifier, the query parameter and a preset query expression; the preset query expression represents a preset query rule adapted to multiple types of databases; According to the target query expression, acquiring target data that meets the query condition from the target database; Obtaining query results according to the target data and sending the query results to the client; The preset query expression includes a database attribute item and a plurality of rule attribute items, each of the rule attribute items has a corresponding rule parameter, and the rule parameter has a corresponding data structure type; the query parameter includes a target rule parameter and its parameter value; The step of generating a target query expression corresponding to the target database according to the target data source identifier, the query parameters and the preset query expression comprises: Convert the parameter value of the target rule parameter into its corresponding data structure type to obtain the target object; Taking the target object as the value of a target rule attribute item, the target rule attribute represents a rule attribute item corresponding to the target rule parameter; The target data source identifier is used as the value of the database attribute item to obtain the target query expression.

2. The method according to claim 1, characterized in that The plurality of rule attribute items include a paging rule attribute item; The step of acquiring target data that meets the query condition from the target database according to the target query expression includes: If the target rule attribute item includes the paging rule attribute item, a paging total query statement and a paging list query statement corresponding to the target database are generated according to the target query expression; Execute the total number of pages query statement to obtain the total number of pages from the target database; Execute the paging list query statement to obtain list data from the target database; The total number of pages and the list data are combined to obtain the target data.

3. The method according to claim 1, characterized in that The plurality of rule attribute items include a paging rule attribute item; The step of acquiring target data that meets the query condition from the target database according to the target query expression includes: If the target rule attribute item does not include the paging rule attribute item, a non-paged query statement corresponding to the target database is generated according to the target query expression; The non-paged query statement is executed to obtain the target data from the target database.

4. The method according to claim 3, characterized in that The electronic device also pre-stores a data source connection pool, which is used to cache data source connections for accessing the database, and each of the data source information includes connection information of each of the databases; The step of executing the non-paged query statement to obtain the target data from the target database includes: If there is a target data source connection for accessing the target database in the data source connection pool, access the target database through the target data source connection, execute the non-paged query statement, and obtain the target data; If the target data source connection for accessing the target database does not exist in the data source connection pool, the target data source connection is created according to the connection information of the target database, and the target database is accessed through the target data source connection, the non-paged query statement is executed, and the target data is obtained.

5. The method according to claim 1, characterized in that: The target data includes a data type; the request includes a request type, and the request type includes a synchronous type and an asynchronous type; The step of obtaining a query result according to the target data and sending the query result to the client comprises: Convert the data type of the target data into a preset data type to obtain the query result; If the request type in the request is the synchronization type, sending the query result to the client; If the request type in the request is the asynchronous type, the request further includes a data receiving address, and the query result is sent to the client according to the data receiving address.

6. A data query system, characterized in that: The data query system includes: a data source management module and a general query module; The data source management module is used to store various types of data source information, each of which includes corresponding database information and a data source identifier; The general query module is used to monitor the request of the client application, parse the request, and obtain the target data source identifier and query parameters; the target data source identifier corresponds to the target database; the query parameters represent the query conditions; the client application is an application that supports the Hypertext Transfer Protocol; Generate a target query expression corresponding to the target database according to the target data source identifier, the query parameter and a preset query expression; the preset query expression represents a preset query rule adapted to multiple types of databases; According to the target query expression, acquiring target data that meets the query condition from the target database; Obtaining query results according to the target data and sending them to the client application; The preset query expression includes a database attribute item and multiple rule attribute items, each of the rule attribute items has a corresponding rule parameter, and the rule parameter has a corresponding data structure type; the query parameter includes a target rule parameter and its parameter value; the general query module is specifically used to: convert the parameter value of the target rule parameter into its corresponding data structure type to obtain a target object; use the target object as the value of the target rule attribute item, and the target rule attribute represents the rule attribute item corresponding to the target rule parameter; use the target data source identifier as the value of the database attribute item to obtain the target query expression.

7. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program, and when the processor executes the computer program, the method according to any one of claims 1 to 5 is implemented.

8. A storage medium, characterized in that: The storage medium stores a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Data retrieval method supporting heterogeneous database

    CN108446289A