SQL statement generation method and device, SQL statement execution method and device and storage medium
Through entity relationship diagram design and API parameters combined with Hall logical reasoning, SQL statements are automatically generated, which solves the problems of high difficulty in SQL statement generation and difficulty in multi-table association management, realizes convenient SQL statement generation and chain query automation, and reduces reduce operation and maintenance costs.
Patent Information
- Application Number
- CN202310366968.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-06
- Publication Date
- 2024-10-15
AI Technical Summary
In the existing technology, SQL statement generation is difficult, multi-table association management is difficult, performance issues arise, and ORM technology requires injecting associated information into business objects, which increases operation and maintenance costs. Query and update operation coding is cumbersome, especially when chained queries require Extract parameters as input parameters.
By obtaining the design results of the entity relationship diagram, adding declarative annotations, obtaining API parameters, using the preset derivation algorithm to generate SQL statements, and combining Hall logical reasoning to automatically identify the database operation intention, eliminating the need for coding to extract calling parameters, especially in chained multi-step processes. There is no need to write parameter extraction code when querying.
It reduces the management difficulty of SQL statement generation, improves convenience, automatically identifies database operation intentions, and reduces coding workload. Especially in multi-table related queries, the previous step result is directly used as the next step condition without additional coding.
Smart Images

Figure CN118778927A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software, and in particular to a SQL statement generating method, executing method, device and storage medium. Background Art
[0002] In today's software development field, SQL or equivalent native development language code is usually used to implement database addition, deletion, modification and query operations. Due to its inherent characteristics, common problems include: high SQL overlap and difficulty in management, multi-table association is difficult to implement and maintain and prone to performance problems, and the conversion of query result sets into business objects is cumbersome. Later, R&D personnel solved the above problems through ORM technology.
[0003] However, ORM technology also has its shortcomings. In order to automatically generate SQL for multi-table association queries, the ORM engine needs to inject association information into the business object through annotations. This method results in: the business object corresponding to the intermediate table must be established. Even if the business object is not needed, the association information of the same table will be scattered across multiple business objects, increasing operation and maintenance costs. At the same time, when calling query, update and delete operations, it is necessary to write cumbersome code to extract call parameters, especially when chaining queries, it is necessary to extract parameters from the query results as input parameters for the next query. Summary of the invention
[0004] In view of this, in order to solve at least one of the above technical problems, an object of the present invention is to provide a SQL statement generation method, device, equipment and storage medium to reduce management difficulty and improve convenience.
[0005] An embodiment of the present invention provides a method for generating a SQL statement, comprising:
[0006] Obtaining an entity relationship diagram design result; the entity relationship diagram design result includes a definition of entity design elements and relationship information of entity design elements;
[0007] Adding a declarative annotation bound to the entity design element to a database operation related class; the declarative annotation includes a relationship between the business object class and the entity design element;
[0008] Obtaining API parameters, and extracting class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations;
[0009] Derivation of SQL statement generation parameters based on the entity relationship diagram design result, the API parameters and the class meta-information by a preset derivation algorithm;
[0010] At least one SQL statement is generated according to the SQL statement generation parameters.
[0011] Further, the adding of declarative annotations bound to the entity design elements to the database operation related classes includes:
[0012] In response to the adding instruction, triggering a language tool of the target development language in the class definition source code;
[0013] By using a language tool of the target development language, adding a declarative annotation bound to the entity design element to a database operation related class;
[0014] Among them, the database operation related classes include at least one of the query condition class, the Entity class, and the query result class; the relationship between the business object class and the entity design element includes at least one of the binding relationship and the default sorting relationship.
[0015] Further, the acquisition of API parameters includes:
[0016] In response to the writing instruction, generate the code for calling the API package according to the preset business requirements;
[0017] The API parameters of the interface encapsulated by the API are obtained through the code.
[0018] Further, the deducing SQL statement generation parameters according to the entity relationship diagram design result, the API parameters and the class meta-information through a preset deduction algorithm includes:
[0019] Performing intent recognition processing according to the API parameters and the class meta information to obtain intent meta information; the intent meta information represents the intent requirement of the database operation;
[0020] According to the intention meta-information and the entity relationship diagram design result, the SQL statement generation parameters are obtained through Hall logic reasoning.
[0021] Furthermore, the SQL statement generation parameters are obtained through Hall logic reasoning according to the intention meta information and the entity relationship diagram design result, including:
[0022] According to the intention meta information and the entity relationship diagram design result, the SQL statement generation parameters are derived with the preset logical rule expression as a constraint;
[0023] The SQL statement generation parameters include at least one of a target field, an assignment relationship, a target expression and a target condition, a target business object class requirement, and a target association logic.
[0024] Further, generating at least one SQL statement according to the SQL statement generation parameters includes:
[0025] Generate at least one SQL statement according to the SQL statement generation parameters by a code generator;
[0026] or,
[0027] The code generator generates parameters according to the SQL statement and combines at least one of equivalent transformation processing, SQL splitting processing, cache processing, and client association processing to generate an SQL statement.
[0028] The embodiment of the present invention also provides a SQL statement execution method, including:
[0029] Obtain at least one SQL statement; the SQL statement is obtained by the SQL statement generation method according to any one of claims 1 to 6;
[0030] At least one of the SQL statements is executed in sequence through the ORM framework to obtain an execution result.
[0031] The embodiment of the present invention also provides a SQL statement generating device, comprising:
[0032] A first acquisition module is used to acquire an entity relationship diagram design result; the entity relationship diagram design result includes a definition of entity design elements and relationship information of entity design elements;
[0033] An adding module, used to add a declarative annotation bound to the entity design element to the database operation related class; the declarative annotation includes the relationship between the business object class and the entity design element;
[0034] A second acquisition module is used to acquire API parameters and extract class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations;
[0035] A derivation module, used to derive SQL statement generation parameters according to the entity relationship diagram design result, the API parameters and the class meta-information through a preset derivation algorithm;
[0036] A generating module is used to generate at least one SQL statement according to the SQL statement generating parameters.
[0037] An embodiment of the present invention also provides an SQL statement device, which includes a processor and a memory, wherein the memory stores at least one instruction, at least one program, a code set or an instruction set, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement the SQL statement generation method or the SQL statement execution method.
[0038] An embodiment of the present invention also provides a computer-readable storage medium, in which at least one instruction, at least one program, a code set or an instruction set is stored. The at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by a processor to implement the SQL statement generation method or the SQL statement execution method.
[0039] The beneficial effects of the present invention are:
[0040] By obtaining the design result of the entity relationship diagram; the design result of the entity relationship diagram includes the definition of the entity design element and the relationship information of the entity design element, adding declarative annotations bound to the entity design element to the database operation related classes, the declarative annotations include the relationship between the business object class and the entity design element, obtaining API parameters, and extracting the class meta information corresponding to the target business object class in the API parameters, the class meta information includes declarative annotations, and by making full use of the design result of the entity relationship diagram, the work of annotating the relationship information of the business object table is omitted, and the problem of scattered and difficult management of table relationship information is solved. According to the design result of the entity relationship diagram, the API parameters and the class meta information, the SQL statement generation parameters are deduced through a preset deduction algorithm, and at least one SQL statement is generated according to the SQL statement generation parameters. By reasonably designing declarative annotations to realize automatic identification of the intention of database operation, the SQL statement generation parameters are automatically deduced in combination with the entity relationship diagram design result, the API parameters and the class meta information, which can save the coding of extracting calling parameters, especially for chained multi-step queries, the query result of the previous step can be directly used as the condition of the next step query without writing the parameter extraction code, thereby improving convenience.
[0041] For better understanding and implementation, the present invention is described in detail below with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 A schematic diagram of the steps of the SQL statement generation method of the present invention;
[0043] Figure 2 This is a flow chart of a method for generating SQL statements according to a specific embodiment of the present invention;
[0044] Figure 3 It is a schematic diagram of the SQL statement execution method of the present invention. DETAILED DESCRIPTION
[0045] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0046] The terms "first", "second", "third" and "fourth" etc. in the specification and claims of the present application and the drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0047] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0048] like Figure 1 and Figure 2 As shown, an embodiment of the present invention provides a method for generating a SQL statement, comprising steps S100-S500:
[0049] S100, obtaining the entity relationship diagram design result.
[0050] In an embodiment of the present invention, the entity relationship diagram design result includes the definition of entity design elements and the relationship information of entity design elements. Optionally, the entity relationship diagram design result (ER design result) is designed in advance in the software design stage, and the entity relationship diagram design result is formed into an ER design file, and then imported into an interpretable format through a tool, so that the ER design file can be interpreted as an object later for the subsequent use of the entity relationship diagram design result. It should be noted that in the entity relationship diagram design result, the entity design elements include but are not limited to physical tables (or ER tables), views, business primary keys, fields of physical tables (or ER fields), etc., the definitions include but are not limited to the table definitions of physical tables and the view definitions of views, and the relationship information includes but is not limited to the declaration of association relationships between tables, business foreign key relationships (for example: the mapping relationship between the value of field A and the record of the dictionary table) and business primary key declarations and other logical primary key conventions, business constraints (for example: the fields that constitute the logical primary key of a certain entity), etc.
[0051] like Figure 1 and Figure 2 As shown, S200, adding a declarative annotation bound to an entity design element to a database operation related class.
[0052] In an embodiment of the present invention, a declarative annotation that is bound to an entity design element (ER design element) is added to a class related to database operations. Optionally, when the declarative annotation is bound, in response to the adding instruction, the language tool of the target development language is triggered in the class definition source code, and the declarative annotation that is bound to the entity design element is added to the class related to database operations through the language tool of the target development language (such as Java Annotation). Among them, the declarative annotation includes dynamic declaration and static declaration, and the static declaration includes the relationship between the business object class and the entity design element, including at least one of the binding relationship and the default sorting relationship, for example, the binding relationship between the business object class and the physical table, the binding relationship between the attribute of the business object class and the field of the physical table, and the field binding annotation and table binding annotation. Among them, the field binding annotation and the declarative annotation of the field binding relationship can express the binding relationship between the attribute of the business object class and the field, the field value mapping expression (field mapping annotation), the aggregation expression, the assignment expression, the conditional expression, and the like. Optionally, database operation related classes include query result class, query condition class, Entity class, condition object, assignment object, sorting and paging object, main map add / delete / modify / query, etc.
[0053] Optionally, dynamic declarations are added to business object classes and attributes of business object classes. For example, annotations expressing the intention of dynamic declarations can be added to query result classes, query condition classes, Entity classes, condition objects, assignment objects, sorting and paging objects, main image addition / deletion / modification / query, etc.
[0054] like Figure 1 and Figure 2 As shown, S300, obtain API parameters, and extract class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations.
[0055] Optionally, API parameters are obtained from the API interface, and after the API parameters are obtained, the API parameters are processed to extract class meta information corresponding to the target business object class in the API parameters, and the class meta information includes declarative annotations. It should be noted that the target business object class refers to the business object class currently required according to user needs.
[0056] Optionally, obtaining API parameters in step S300 includes steps S310-S320:
[0057] S310. In response to the writing instruction, generate a code for calling the API package according to preset business requirements.
[0058] Optionally, the writing instructions include but are not limited to instructions input by the user through the keyboard, mouse, etc. When the user inputs the writing instructions, the system responds to the writing instructions and writes and generates code for calling the API package according to preset business requirements.
[0059] S320. Obtain API parameters of the API encapsulated interface through the code.
[0060] Optionally, the API parameters of the API encapsulated interface are obtained through the code. It should be noted that, in the embodiment of the present invention, the operational business intent of the database operation related class is expressed through the API encapsulated interface, i.e., the API interface, and the API interface serves as an encapsulation interface for the ORM framework, and the API interface is, for example, an interface of the APIWrapper class. Optionally, the database operation related class corresponds to the business object class and the attributes of the business object class, including but not limited to the And relationship class, the OR relationship class, the paging sorting declaration class, add / delete / modify / check (query record method, insert record method, delete record method, update record method), database operation related classes, etc. Therefore, the API parameters include but are not limited to the And relationship class, the OR relationship class, the paging sorting declaration class, add / delete / modify / check (query record method, insert record method, delete record method, update record method), and the parameters of the database operation related class. Among them, the And relationship class indicates that the relationship between multiple elements is AND, and the elements can be condition objects, And relationship objects, and OR relationship objects; the OR relationship class indicates that the relationship between multiple elements is OR, and the elements can be condition objects, And relationship objects, and OR relationship objects; the paging sorting declaration class declares the sorting requirements and paging requirements of the query results in a combined manner. Query record method: an entry for querying single or multiple records in a single table or multiple tables by condition, method input parameters: the object (multiple) containing the query condition information can be either a dedicated query condition class object or a query result class object, the result object type declares the object indicating the query condition, the result object class, the paging sorting declaration object, and the method return parameter: a single object or a collection of the result object class. Insert record method: an entry for inserting a single record or inserting multiple records in batches; method input parameters: a business object or a list of business objects, and the method return parameter: the number of affected records. Delete record method: the entry for deleting records of a single table according to conditions. Method input parameters: object containing condition information (multiple), bound target table type or annotation containing target table information. Method return parameters: number of affected records. Update record method: update records of a single table according to conditions. Method input parameters: object to be written back to the database, object containing condition information (multiple), method return parameters: number of affected records.
[0061] like Figure 1 and Figure 2 As shown, S400, according to the entity relationship diagram design result, API parameters and class meta-information, the SQL statement is deduced through a preset deduction algorithm to generate parameters.
[0062] Optionally, step S400 includes steps S410-S420:
[0063] S410: Perform intent recognition processing according to API parameters and class meta information to obtain intent meta information.
[0064] In an embodiment of the present invention, intent recognition processing is performed based on API parameters and class meta information to obtain intent meta information. It should be noted that the intent meta information represents the intent requirements of database operations, including but not limited to selecting a set of records, including which projection fields, field value mapping requirements (whether aggregation and aggregation requirements are required), set operations, which tables are included in table connections, aggregation, sorting and paging requirements, field assignment requirements, adding / deleting / modifying records, etc., and at least one of the types of operations. It should be noted that the algorithm used for intent recognition processing can be trained in advance using training data.
[0065] S420. According to the intention meta-information and the entity relationship diagram design result, the SQL statement generation parameters are obtained through Hall logic reasoning.
[0066] In an embodiment of the present invention, according to the intention meta-information and the result of the entity relationship diagram design, the preset logic rule expression is used as a constraint through Hall logic reasoning, so as to derive parameters that can be used to instruct the code generator to generate SQL that meets the requirements and constraint rules, that is, SQL statement generation parameters. Optionally, the SQL statement generation parameters include at least one of the target field, assignment relationship, target expression and target condition, target business object class requirements and target association logic, for example, the target field includes but is not limited to the Select field and the Insert field; the target expression includes but is not limited to the GroupBy expression and the OrderBy expression; the target condition includes but is not limited to the Where condition, the Join table and the On condition; the target business object class requirements include but are not limited to the query driving table and the add / delete / modify main table.
[0067] Specifically, according to the intention meta-information and the entity relationship diagram design result, the SQL statement generation parameters are derived through Hall logic reasoning with the preset logical rule expression as a constraint, including steps S4201-S4204:
[0068] S4201: Combine the entity relationship diagram design result and the preset logic rule expression, and derive the target table or the set of tables included in the Cartesian product according to the intention meta-information. The target table or the set of tables included in the Cartesian product can be used to extract the target field.
[0069] S4202. Combining the entity relationship diagram design results and the preset logical rule expressions, derive all projection fields and aggregation fields according to the intent meta-information, as well as derive target fields such as grouping requirements, sorting and paging requirements, and target business object class requirements.
[0070] S4203. Combine the entity relationship diagram design results and the preset logical rule expression, and derive the assignment relationship according to the intention meta-information.
[0071] S4204. Combining the entity relationship diagram design result and the preset logic rule expression, derive the Cartesian product association logic and WHERE condition and other target association logic and target conditions according to the intention meta-information.
[0072] Optionally, the preset logic rule expression is established based on performance factors or business constraints. By combing and analyzing the implementation code of the database addition, deletion, modification and query operations of past projects, the logic rules can be sorted out according to the logical dependencies; the preset logic rules include but are not limited to the rules for sub-library and sub-table, performance calculation formulas, cache usage constraints, etc., which can be expanded as needed. For example, the rules for sub-library and sub-table specifically refer to whether the target database table is designed as a sub-library or sub-table, and how to divide the database and table; the performance calculation formula specifically refers to the computing cost and resource consumption of the target SQL, combined with the statistical information related to the database table performance to judge the quality of SQL performance. The statistical information related to database performance includes the performance indicators of the target database, the data volume of the target table, the index definition, the performance of the index, etc.; the cache usage constraint specifically refers to whether the data of the target database table uses the cache to reduce the number of database queries, under what conditions to use the cache, under what conditions to refresh the cache, use the local cache or the remote cache, etc.
[0073] S500: Generate at least one SQL statement according to SQL statement generation parameters.
[0074] Optionally, step S500 includes step S510 or S520:
[0075] S510: Generate at least one SQL statement according to SQL statement generation parameters through a code generator.
[0076] Optionally, the SQL generation parameters are used as input to the code generator, and the code generator generates at least one SQL statement and related calling parameters according to the SQL statement generation parameters.
[0077] S520. Generate SQL statements by using a code generator to generate parameters according to SQL statements and combining at least one of equivalent transformation processing, SQL splitting processing, cache processing, and client association processing.
[0078] Optionally, parameters are generated according to the SQL statement to generate an SQL statement with higher execution performance, under the premise that SQL that meets the operation requirements can be generated, by combining at least one of equivalent transformation processing, SQL splitting processing, cache processing, and client association processing.
[0079] Optionally, equivalent transformation means include, but are not limited to, equivalent transformation of multi-table association and subquery, equivalent transformation of multi-table association of physical table and subtable association of physical table, equivalent transformation of subquery of different table combinations, equivalent transformation of association of different subtables, equivalent transformation of subquery and IN condition, and other relational algebra transformations. SQL splitting processing refers to splitting specific SQL in different ways under the constraints of preset rules. For example, for subqueries with a query result set size within a threshold, it can be split into subquery SQL and outer main SQL. Cache processing refers to using cached results instead of querying the database for IN conditions with small data volumes, provided that the preset rule constraints are met. Client association processing refers to associating two or more SQL query results in the code running environment rather than the database server.
[0080] The following is a specific example:
[0081] 1. Currently, you need to query the latest 10 orders of the currently logged-in user according to the input conditions. You need to display the order serial number, order title, order amount, order time, and order status. The order status needs to be translated from the dictionary table to the name. Assume that the ER design results include: order master table order (including fields: id, order_no, title, create_time, user_id, order_amount, state), dictionary item table dict_item (including fields: id, dict_code, item_code, item_name), user table user (including field: id), foreign key declaration order status translation (pseudo code representation: order.state = dict_item.item_code AND dict_item.dict_code = 'ORDER_STATE'), foreign key declaration order user (pseudo code representation: order.user_id = user.id). Assume that the source code includes: input condition class OrderQueryCondVo (including attribute: titleLike), query result class OrderBriefVo (including attributes: orderNo, userId, title, totalAmount, createTime, state, stateName), user identity class UserIdneity (including attribute: userId), and interface getUserIdentity() for querying the currently logged-in user.
[0082] 2. For step S200, first import the ER design results, further add the annotation BindColumn(tabRef="order",columnRef="title",criteriaExpr="LIKE'%?%'") to the titleLike attribute of OrderQueryCondVo, add the annotation BindColumn(tabRef="order",columnRef="order_no") to the orderNo attribute of OrderBriefVo, add the annotation BindColumn(tabRef="order",columnRef="order_no") to the userId attribute of OrderBriefVo, add the annotation BindColumn(tabRef="order",columnRef="user_id") to the title attribute of OrderBriefVo, add the annotation BindColumn(tabRef="order",columnRef="title") to the tota attribute of OrderBriefVo Add the annotation BindColumn(tabRef="order",columnRef="order_amount") to the lAmount attribute, add the annotation BindColumn(tabRef="order",columnRef="create_time") to the createTime attribute of OrderBriefVo, add the annotation BindColumn(tabRef="order",columnRef="state") to the state attribute of OrderBriefVo, add the annotation BindColumn(tabRef="order",columnRef="state") to the stateName attribute of OrderBriefVo, add the annotation BindColumn(tabRef="dict_item",columnRef="item_name") to the userId attribute of UserIdentity
[0083] 3. For step S300, 1) generate the code for calling the API encapsulation according to the preset business requirements, and extract the binding relationship between the orderNo, userId, title, createTime, state, stateName attributes and the corresponding fields of the order table and dict_item table from the input parameter OrderBriefVo class; 2) extract the binding relationship between the titleLike attribute of the OrderQueryCondVo class and the title field of the order table from the input parameter condVo, and the binding relationship between the userId attribute of the UserIdentity class and the id field of the user table can be extracted from the input parameter userIdentity.
[0084] The pseudo code is as follows:
[0085] OrderByAndPagingDeclaration oapDcl=new
[0086] OrderByAndPagingDeclaration(“create_time”,false,10);
[0087] UserIdentity userIdentity=getUserIdentity();
[0088] List <orderbriefvo>rst=api.selectMany(OrderBriefVo.class,oapDcl,condVo,userIdentity)。
[0089] 4. For step S400, 1) according to the API interface input parameters (API parameters) and the extracted class meta information, the intent meta information, hereinafter referred to as meta information, can be identified, specifically including: selecting the record set including the order record set and the user record set, the two sets are in a union relationship, the projected fields include the order_no, title, create_time, user_id, order_amount, and state of the order table, and the item_name field of the dict_item table, the field value mapping is to take the original value, the table connection includes the order table and the dict_item table, there is no aggregation requirement, the first 10 records are sorted in descending order by the create_time field of the order table, and there is no word Segment assignment meta information, not the intention of adding, deleting or modifying; 2) According to the projection field meta information, field value mapping, combined with ER design information and preset logical rules, it is deduced that the SELECT field should be the order_no, title, create_time, user_id, order_amount, state fields of the order table, and the item_name field of the dict_item table, all of which are original value mappings; 3) According to the preset logical rules, it is deduced that the query driving table is the order table. According to the table connection meta information, combined with the ER design results and the preset logical rules, the pseudo code of the association logic between the order table and the dict_item table is deduced as order.state=dict_item.item_code AND dict_item.dict_code='ORDER_STATE', and the pseudo code of the association condition between the user record set represented by the input parameter userIdentity and the order record set represented by the input parameter condVo is deduced as order.user_id=user.id. Because there is no projection field of the user table, the removal can be replaced with an equivalent condition. The pseudo code is order.user_id=#{userIdentity.userId}. 4) According to the set operation meta-information and the selected record set meta-information, combined with the ER design results and the preset logical rules, the WHERE condition is derived (the pseudo code is order.title LIKE CONCAT('%',#{condVo.title},'%') AND order.user_id=#{userIdentity.userId} (where #{} represents the placeholder for the call parameter). According to the sorting and paging meta-information, the expression of OrderBy is derived as order.create_time DESC LIMIT 10. So far, the SQL statement generation parameters used to generate SQL are derived.
[0090] 5. For step S500, this embodiment uses the rule 1 in the assumption: the dictionary table records use the local cache rule, and finds that the dict_item table does not appear in the WHERE condition and can be split. After the split, the query operation is implemented in three steps: the first step is to query the order table records, the second step is to extract the state attribute set in the first step result record and query the dict_item table, and the third step is to assign the results of the second step to the stateName attribute of the first step result record according to the association condition.
[0091] 6. Execute at least one SQL statement: 1) Pass SQL (SELECT t.order_no, t.title, t.user_id, t.create_time, t.order_amount, t.state FROM order t WHERE t.title LIKE CONCAT('%', #{condVo.titleLike}, '%') AND t.user_id=#{userIdentity.userId}) into the ORM framework for execution to obtain a set of no more than 10 OrderBriefVo objects. 2) Extract the state attribute set from the OrderBriefVo object set to determine whether the corresponding OrderItem object exists in the cache. If not, pass the SQL (SELECT t.item_code, t.item_name FROM dict_item t WHERE t.dict_code = 'ORDER_STATE' AND t.item_code IN) and the state set as a parameter into the ORM framework to obtain the Map of the state attribute translation name, and put the query into the cache; 3) Traverse the OrderBriefVo object set, query the translation value according to the value of the state attribute, and fill in the stateName attribute to obtain the execution result.
[0092] Compared with the prior art, the SQL statement generation method of the embodiment of the present invention can 1) be expanded to support mapping external query interfaces to tables, can be expanded to client-based sub-libraries and sub-tables, and can be expanded to use database views as virtual tables; 2) can automatically generate code that meets the requirements of database addition, deletion, query and modification operations according to the intention of the API encapsulation method, without the need to write SQL statements and extract code for calling parameters, which can greatly reduce the development workload, developers do not need to know the field-level information details of multi-table associations, and reduce the workload of SQL transformation during performance optimization; 3) improve performance by equivalently splitting SQL through the code generator, and reduce the cost of code transformation by adjusting rules during performance optimization; 4) the present invention provides a concise API I encapsulation, without increasing the development workload, to achieve the purpose of the present invention; 5), taking the expansion of ORM (object relationship mapping) technology as the starting point, by making full use of the entity relationship diagram design results to save the work of annotating the table association relationship information of the business object and solve the problem of scattered table association information being difficult to manage; by reasonably designing declarative annotations to automatically identify the details of the database addition, deletion, query and modification intentions, combined with the entity relationship diagram design results, the preset logical rules and Hall logic analyzed and sorted out, the SQL and call parameters of the addition, deletion, query and modification operations can be automatically generated, and the coding and extraction of call parameters can be saved, especially for chained multi-step queries. The query result of the previous step can be directly used as the condition for the next query without writing the extraction parameter code. For the implementation and operation of multi-table association queries, developers only need to know which business objects can specify the target knowledge without knowing the association details and multi-table association performance specifications, solving the problem of multi-table association development and operation difficulties.
[0093] The embodiment of the present invention also provides a SQL statement generating device, comprising:
[0094] The first acquisition module is used to acquire the entity relationship diagram design result; the entity relationship diagram design result includes the definition of entity design elements and the relationship information of entity design elements;
[0095] Add a module for adding declarative annotations to database operation related classes that are bound to entity design elements; the declarative annotations include the relationship between business object classes and entity design elements;
[0096] The second acquisition module is used to acquire API parameters and extract class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations;
[0097] The derivation module is used to derive SQL statements to generate parameters based on the entity relationship diagram design results, API parameters and class meta-information through a preset derivation algorithm;
[0098] The generating module is used to generate at least one SQL statement according to the SQL statement generating parameters.
[0099] The contents of the above method embodiments are all applicable to the present device embodiments. The functions specifically implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments, and will not be repeated herein.
[0100] like Figure 3 As shown, the present invention also provides a SQL statement execution method, comprising steps S600-S700:
[0101] S600: Obtain at least one SQL statement.
[0102] It should be noted that the SQL statement is obtained through the above-mentioned SQL statement generation method.
[0103] S700. Execute at least one SQL statement in sequence through the ORM framework to obtain an execution result.
[0104] Optionally, the SQL statement is passed to the ORM framework, and at least one SQL statement is executed sequentially through the ORM framework. For each SQL statement, the ORM framework executes and maps the execution result to an object. The execution may include reading the cache or updating the cache, executing object associations, converting SQL query results into call parameters, etc., to obtain the execution result.
[0105] An embodiment of the present invention also provides an SQL statement device, which includes a processor and a memory, wherein the memory stores at least one instruction, at least one program, code set or instruction set, and the at least one instruction, at least one program, code set or instruction set is loaded and executed by the processor to implement the SQL statement generation method or SQL statement execution method of the aforementioned embodiment.
[0106] The contents of the above method embodiments are all applicable to the present device embodiments. The functions specifically implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments, and will not be repeated herein.
[0107] An embodiment of the present invention also provides a computer-readable storage medium, in which at least one instruction, at least one program, code set or instruction set is stored, and the at least one instruction, at least one program, code set or instruction set is loaded and executed by a processor to implement the SQL statement generation method or SQL statement execution method of the aforementioned embodiment.
[0108] The embodiment of the present invention also provides a computer program product or a computer program, which includes a computer instruction stored in a computer-readable storage medium. A processor of a computer device reads the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the computer device executes the SQL statement generation method or the SQL statement execution method of the aforementioned embodiment.
[0109] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0110] It should be understood that in the embodiments of the present invention, "at least one (item)" refers to one or more, and "plurality" refers to two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can represent: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and subsequent associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or plural.
[0111] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point, the coupling or direct coupling or communication connection between each other shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms. The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, each functional unit in each embodiment of the present application can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units.
[0112] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including multiple instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of each embodiment method of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store programs.
[0113] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some of the technical features therein by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.< / orderbriefvo>
Claims
1. A method for generating a SQL statement, characterized in that: include: Get the entity relationship diagram design results; The entity relationship diagram design result includes the definition of entity design elements and the relationship information of entity design elements; Adding a declarative annotation bound to the entity design element to a database operation related class; the declarative annotation includes a relationship between the business object class and the entity design element; Obtaining API parameters, and extracting class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations; Derivation of SQL statement generation parameters based on the entity relationship diagram design result, the API parameters and the class meta-information by a preset derivation algorithm; At least one SQL statement is generated according to the SQL statement generation parameters.
2. The method for generating SQL statements according to claim 1, characterized in that: The adding of a declarative annotation bound to the entity design element to the database operation related class includes: In response to the adding instruction, triggering a language tool of the target development language in the class definition source code; By using a language tool of the target development language, adding a declarative annotation bound to the entity design element to a database operation related class; Among them, the database operation related classes include at least one of the query condition class, the Entity class, and the query result class; the relationship between the business object class and the entity design element includes at least one of the binding relationship and the default sorting relationship.
3. The method for generating SQL statements according to claim 1, characterized in that: The acquisition API parameters include: In response to the writing instruction, generate the code for calling the API package according to the preset business requirements; The API parameters of the interface encapsulated by the API are obtained through the code.
4. The method for generating a SQL statement according to any one of claims 1 to 3, characterized in that: The step of deducing SQL statement generation parameters according to the entity relationship diagram design result, the API parameters and the class meta-information by using a preset deduction algorithm includes: Performing intent recognition processing according to the API parameters and the class meta information to obtain intent meta information; the intent meta information represents the intent requirement of the database operation; According to the intention meta-information and the entity relationship diagram design result, the SQL statement generation parameters are obtained through Hall logic reasoning.
5. The method for generating SQL statements according to claim 4, characterized in that: The SQL statement generation parameters are obtained according to the intention meta information and the entity relationship diagram design result through Hall logic reasoning, including: According to the intention meta information and the entity relationship diagram design result, the SQL statement generation parameters are derived with the preset logical rule expression as a constraint; The SQL statement generation parameters include at least one of a target field, an assignment relationship, a target expression and a target condition, a target business object class requirement, and a target association logic.
6. The method for generating SQL statements according to claim 1, characterized in that: The step of generating at least one SQL statement according to the SQL statement generation parameters includes: Generate at least one SQL statement according to the SQL statement generation parameters by a code generator; or, The code generator generates parameters according to the SQL statement and combines at least one of equivalent transformation processing, SQL splitting processing, cache processing, and client association processing to generate an SQL statement.
7. A method for executing SQL statements, characterized in that: include: Get at least one SQL statement; The SQL statement is obtained by the SQL statement generation method according to any one of claims 1 to 6; At least one of the SQL statements is executed in sequence through the ORM framework to obtain an execution result.
8. A SQL statement generating device, characterized in that: include: The first acquisition module is used to obtain the entity relationship diagram design result; The entity relationship diagram design result includes the definition of entity design elements and the relationship information of entity design elements; An adding module, used to add a declarative annotation bound to the entity design element to the database operation related class; the declarative annotation includes the relationship between the business object class and the entity design element; A second acquisition module is used to acquire API parameters and extract class meta information corresponding to the target business object class in the API parameters; the class meta information includes declarative annotations; A derivation module, used to derive SQL statement generation parameters according to the entity relationship diagram design result, the API parameters and the class meta-information through a preset derivation algorithm; A generating module is used to generate at least one SQL statement according to the SQL statement generating parameters.
9. A SQL statement device, characterized in that: The SQL statement device includes a processor and a memory, wherein the memory stores at least one instruction, at least one program, a code set or an instruction set, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement the method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that: The storage medium stores at least one instruction, at least one program, a code set or an instruction set, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement the method as described in any one of claims 1-7.
Citation Information
Cited By
Operation method and device for business object in service system corresponding to database
CN119166728A
Data query method and apparatus, and storage medium and computer program product
WO2026091610A1
