SQL statement dynamic generation method and device, electronic equipment and storage medium
Through the dynamic generation method of tag variables and tag functions, the complex and inefficient SQL statement generation problems are solved, and flexible SQL statement generation and efficient coding development are realized.
Patent Information
- Application Number
- CN202510539699.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-27
- Publication Date
- 2025-08-08
AI Technical Summary
In the prior art, SQL statement generation is complex, low efficiency and inflexible, static SQL template writing and maintenance costs are high, and errors are prone to occur when requirements change.
By obtaining preset statements and input variables, the mark variables and mark functions are used to generate dynamically, including conditional judgment and null value judgment functions, replace the mark variable based on the input variable and execute the mark function, and generate statements that meet the SQL specifications.
It realizes dynamic generation of SQL statements, improves flexibility, lowers development thresholds, and improves coding efficiency.
Smart Images

Figure CN120448401A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device, electronic device, and storage medium for dynamically generating SQL statements. Background Art
[0002] In today's software development and data-driven business scenarios, database operations are a core component, involving diverse needs such as reading, writing, deleting, modifying, performing statistical analysis, and filtering data. To meet these requirements, developers often need to write numerous SQL statements or interact with the database using the APIs of ORM (Object-Relational Mapping) frameworks like MyBatis.
[0003] Taking MyBatis as an example, while its dynamic SQL implementation simplifies the SQL writing process to a certain extent, developers still need to pre-write static SQL templates in the database tool and then dynamically assemble SQL fragments using tags or annotations in XML files, combined with business logic. However, this process suffers from limited development efficiency, insufficient flexibility, and poor maintainability. Writing and maintaining static SQL templates increases development workload, especially when requirements change frequently or SQL logic is complex. Manually modifying SQL templates is error-prone and inefficient. Furthermore, as the business grows, the complexity of SQL statements increases, increasing maintenance costs. The tight coupling of static SQL templates with business logic makes SQL modification and optimization difficult. Summary of the Invention
[0004] In view of the shortcomings of the related technologies described above, the present application provides a method, device, electronic device and storage medium for dynamically generating SQL statements to solve the technical problems in the related technologies of complex, low-efficiency and inflexible SQL statement generation.
[0005] The present application provides a method for dynamically generating SQL statements, the method comprising: obtaining a preset statement and input variables, the preset statement including a marker variable and a marker function, the marker variable being an assignable parameter, and the marker function being an executable function including the marker variable; replacing the corresponding marker variable based on the input variable to obtain a statement to be parsed, extracting the marker function in the statement to be parsed, and executing the marker function to obtain a function execution result; and replacing the corresponding marker function based on the function execution result to obtain a generated statement.
[0006] In one embodiment of the present application, the marking function includes at least a conditional judgment function and a null value judgment function, and executing the marking function to obtain a function execution result includes: if the replaced marking variable is a null value, the execution result of the null value judgment function is a true value; if the replaced marking variable is a non-null value, the execution result of the null value judgment function is a false value; if the execution result of the null value judgment function is a true value, the conditional judgment function returns a first result, and uses the first result as the function execution result; if the execution result of the null value judgment function is a false value, the conditional judgment function returns a second result, and uses the second result as the function execution result; wherein, the first result is null, and the second result is the SQL clause corresponding to the marking variable.
[0007] In one embodiment of the present application, after obtaining the preset statements and input variables, the method further includes: obtaining multiple groups of input variables, replacing the corresponding label variables based on one group of input variables to obtain the statement to be parsed, extracting the label function in the statement to be parsed, and executing the label function to obtain the function execution result, and replacing the corresponding label function based on the function execution result to obtain a group of generated statements; repeating the above steps to obtain multiple groups of generated statements based on multiple groups of input variables to batch generate SQL statements.
[0008] In one embodiment of the present application, the corresponding marker variable is replaced based on the input variable to obtain a statement to be parsed, and extracting the marker function in the statement to be parsed includes: identifying the marker variable based on the variable marker to replace the corresponding marker variable based on the input variable, and the variable marker is a marker language pre-set in the marker variable; identifying the marker function based on the function marker, so that a function executor executes the marker function based on the function marker, and the function marker is a marker language pre-set in the marker function.
[0009] In one embodiment of the present application, identifying the tag variable based on the variable marker includes: writing a regular expression based on the format of the variable marker to match the variable marker; identifying and extracting the variable name of the tag variable according to the regular expression, so as to replace the input variable with the corresponding tag variable based on the variable name.
[0010] In one embodiment of the present application, identifying the labeled function based on the function identifier includes: identifying the function identifier based on the extensible markup language, and constructing an object tree based on the identification result, the object tree including function nodes for storing the labeled function; traversing from the root node of the object tree, searching all function nodes in the object tree, and extracting the labeled function.
[0011] In one embodiment of the present application, the corresponding marker variable is replaced based on the input variable to obtain a statement to be parsed: if a marker variable fails to be replaced based on the input variable, the marker variable is replaced based on a null character.
[0012] An embodiment of the present application also provides a device for dynamically generating SQL statements, characterized in that the device includes an input module for obtaining a preset statement and input variables, the preset statement includes a tag variable and a tag function, the tag variable is an assignable parameter, and the tag function is an executable function including the tag variable; an execution module for replacing the corresponding tag variable based on the input variable to obtain a statement to be parsed, extracting the tag function in the statement to be parsed, and executing the tag function to obtain a function execution result; and an assembly module for replacing the corresponding tag function based on the function execution result to obtain a generated statement.
[0013] An embodiment of the present application also provides an electronic device, comprising: one or more processors; a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the electronic device implements the method for dynamically generating SQL statements as described in any of the above embodiments.
[0014] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor of a computer, the computer is caused to execute the method for dynamically generating SQL statements as described in any one of the above embodiments.
[0015] Beneficial effects of the present application: The embodiments of the present application provide a method, device, electronic device and storage medium for dynamically generating SQL statements, the method comprising obtaining a preset statement and input variables, the preset statement comprising a tag variable and a tag function, the tag variable being an assignable parameter, the tag function being an executable function comprising the tag variable, replacing the corresponding tag variable based on the input variable to obtain a statement to be parsed, extracting the tag function in the statement to be parsed, executing the tag function to obtain a function execution result, replacing the corresponding tag function based on the function execution result to obtain a generated statement, parsing the SQL preset statement with the tag variable and the tag variable into a generated statement that meets the SQL specification, thereby realizing dynamic generation of the SQL statement, improving the flexibility of the SQL statement, lowering the threshold for writing dynamic SQL statements, and improving the development efficiency of SQL coding.
[0016] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1This is a schematic diagram of an implementation environment of a method for dynamically generating SQL statements, as shown in an exemplary embodiment of the present application;
[0018] Figure 2 This is a flowchart of a method for dynamically generating SQL statements shown in an exemplary embodiment of the present application;
[0019] Figure 3 This is a flowchart illustrating a method for defining and parsing SQL statements according to an exemplary embodiment of the present application;
[0020] Figure 4 This is a block diagram of a device for dynamically generating SQL statements, as shown in an exemplary embodiment of the present application;
[0021] Figure 5 It is a structural diagram of an electronic device shown in an exemplary embodiment of the present application. DETAILED DESCRIPTION
[0022] The following describes the embodiments of the present application through specific examples. Those skilled in the art can easily understand the other advantages and effects of the present application from the content disclosed in this specification. The present application can also be implemented or applied through other different specific embodiments. The details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of the present application. It should be noted that the following embodiments and features in the embodiments can be combined with each other unless they conflict.
[0023] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present application. Therefore, the illustrations only show components related to the present application and are not drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component can be changed at will, and the component layout type may also be more complicated.
[0024] It should be noted that in this application, "first," "second," and the like are merely used to distinguish similar objects, and do not limit the order or precedence of similar objects. The variations of "including," "having," and the like indicate that the scope of the subject of the term is not exclusive, in addition to the examples shown in the term.
[0025] It should be understood that the various numbers, step numbers, and other reference numerals in this application are provided for ease of description and are not intended to limit the scope of this application. The order of reference numerals in this application does not necessarily imply a specific order of execution; the order of execution of each process is determined by its function and inherent logic.
[0026] In the following description, a large number of details are discussed to provide a more thorough explanation of the embodiments of the present application. However, it is obvious to those skilled in the art that the embodiments of the present application can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present application difficult to understand.
[0027] The embodiments of the present application respectively propose a method for dynamically generating SQL statements, a device for dynamically generating SQL statements, an electronic device, a computer-readable storage medium, and a computer program product. These embodiments will be described in detail below.
[0028] See also Figure 1 , Figure 1 This is a schematic diagram of an implementation environment of a method for dynamically generating SQL statements shown in an exemplary embodiment of the present application.
[0029] like Figure 1 As shown, the implementation environment may include an input device 101 and a computer device 102. The computer device 102 may be at least one of a microcomputer, an embedded computer, a neural network computer, etc., and the input device 101 may be a keyboard or a touch screen, etc. The input device 101 obtains a preset statement and input variables, which are then sent to the computer device 102. The computer device 102 replaces the corresponding labeled variables based on the input variables to obtain a statement to be parsed, extracts the labeled function in the statement to be parsed, executes the labeled function to obtain a function execution result, and replaces the corresponding labeled function based on the function execution result to obtain a generated statement.
[0030] See also Figure 2 , Figure 2 This is a flowchart of a method for dynamically generating SQL statements as shown in an exemplary embodiment of the present application. This method can be applied to Figure 1 The implementation environment shown is as follows. The method may also be applicable to other exemplary implementation environments and be specifically executed by devices in other implementation environments. This embodiment does not limit the implementation environment to which the method is applicable.
[0031] like Figure 2 As shown, in an exemplary embodiment, the method for dynamically generating SQL statements includes at least steps S210 to S240, which are described in detail as follows:
[0032] Step S210: Obtain a preset statement and input variables. The preset statement includes a tag variable and a tag function. The tag variable is an assignable parameter, and the tag function is an executable function including the tag variable.
[0033] Exemplarily, an SQL statement with a marked variable and a marked function is input as a preset statement, the marked variable is marked by a variable marker, and the marked function is marked by a function marker.
[0034] For example, the variable marker can be $$_ or $#_. The variable marker plus the variable name is used to create the marker variable. For example, $$_{name} creates the variable name; $$_{city} creates the variable name. A preset function can have multiple marker variables. When extracting a marker variable, the variable name is obtained and the input variable is replaced with the corresponding marker variable based on the variable name.
[0035] For example, a marker function can be $$_if or $#_isNull. Marker functions can be used in SQL statements to quickly and easily write dynamic SQL statements. During runtime, the marker function executor executes these special functions and obtains the return result. Common marker functions are as follows: $$_isNull determines whether the passed value is null, returning true if it is null, and false otherwise; $$_if accepts three parameters: flag, cond1, and cond2. Its function is to determine whether the passed flag is true, returning cond1 if it is true, and cond2 otherwise.
[0036] Step S220 , replacing the corresponding marker variable based on the input variable to obtain a statement to be parsed, extracting the marker function in the statement to be parsed, and executing the marker function to obtain a function execution result.
[0037] In one embodiment of the present application, the corresponding marker variable is replaced based on the input variable to obtain the statement to be parsed, and the extraction of the marker function in the statement to be parsed includes: identifying the marker variable based on the variable marker to replace the corresponding marker variable based on the input variable, the variable marker is a marker language pre-set in the marker variable; identifying the marker function based on the function marker, so that the function executor executes the marker function based on the function marker, the function marker is a marker language pre-set in the marker function.
[0038] In one embodiment of the present application, identifying a marker variable based on a variable marker includes: writing a regular expression based on the format of the variable marker to match the variable marker; identifying and extracting the variable name of the marker variable according to the regular expression to replace the input variable with the corresponding marker variable based on the variable name.
[0039] In one embodiment of the present application, identifying a labeled function based on a function identifier includes: identifying the function identifier based on an extensible markup language, and constructing an object tree based on the identification result, the object tree including function nodes for storing the labeled function; traversing from the root node of the object tree, searching all function nodes in the object tree, and extracting the labeled function.
[0040] Exemplarily, before the preset statement is executed, the marker variable is replaced based on the input variable to obtain the statement to be parsed. For example, the preset statement includes the marker variable $$_{name}, and the input variable includes name: "test", where name is the variable name and test is the corresponding variable value. First, the marker variable $$_{name} is identified by the variable marker $$_, then the variable name name of the marker variable is extracted, and the corresponding input variable is found based on the variable name name. The marker variable $$_{name} is replaced with the variable value test of the corresponding input variable, that is, the marker variable $$_{name} is replaced with test.
[0041] In one embodiment of the present application, the corresponding marker variable is replaced based on the input variable to obtain the statement to be parsed: if a marker variable fails to be replaced based on the input variable, then a marker variable is replaced based on a null character.
[0042] In one embodiment of the present application, the marking function includes at least a conditional judgment function and a null value judgment function, and executing the marking function to obtain a function execution result includes: if the replaced marking variable is a null value, the execution result of the null value judgment function is a true value; if the replaced marking variable is a non-null value, the execution result of the null value judgment function is a false value; if the execution result of the null value judgment function is a true value, the conditional judgment function returns a first result, and the first result is used as the function execution result; if the execution result of the null value judgment function is a false value, the conditional judgment function returns a second result, and the second result is used as the function execution result; wherein, the first result is null, and the second result is the SQL clause corresponding to the marking variable.
[0043] For example, the null judgment function $$_isNull has one parameter, which is used to determine whether the value of the parameter is a null value. If it is a null value, it returns true (true value), otherwise it returns false (false value). The conditional judgment function $$_if has three parameters, namely flag, cond1, and cond2. The first parameter flag is the flag parameter. The conditional judgment function is used to determine whether the flag parameter is a true value. If it is a true value, it returns the first result cond1, and if it is a false value, it returns the second result cond2.
[0044] Exemplarily, the null value judgment function is used to judge whether the replaced marker variable is a null value. For example, the marker function before replacement is $$_isNull('$$_{name}'). Based on the input variable name: "test", the marker variable in the null value judgment function is replaced with $$_isNull('test'). The replaced marker variable is a non-null value, so the execution result of the null value judgment function is false. If the marker function before conversion is $$_isNull('$$_{city}'), and the input variable does not include the variable value of the variable named city, the null value judgment function after replacement is $$_isNull(''). Because the replaced marker variable is a null value, the execution result of the null value judgment function is true.
[0045] Exemplarily, a control judgment function is nested in a conditional judgment function, such as $$_if($$_isNull('$$_{name}'),"","and name='$$_{name}'"). Based on the input variable name: "test", the conditional judgment function is replaced to obtain $$_if($$_isNull('test'),"","and name='test'"). Since the execution result of the null value judgment function $$_isNull('$$_{name}') is a false value, the conditional judgment function returns the second result, "andname='test'".
[0046] For example, $$_if($$_isNull('$$_{city}'),"","and city like'%$$_{city}%'"), if the input variable does not include the variable value named city, the marker variable $$_{city} is replaced with a null value. After replacing the marker variable, the conditional judgment function is $$_if($$_isNull(''),"","and city like'%%'"), because the execution result of the null value judgment function is true, the conditional judgment function returns the first result, which is a null value.
[0047] Step S230: Replace the corresponding marked function based on the function execution result to obtain a generated statement.
[0048] In one embodiment of the present application, after obtaining the preset statements and input variables, the method further includes: obtaining multiple groups of input variables, replacing corresponding marker variables based on one group of input variables to obtain statements to be parsed, extracting marker functions in the statements to be parsed, executing the marker functions to obtain function execution results, and replacing corresponding marker functions based on the function execution results to obtain a group of generated statements; repeating the above steps to obtain multiple groups of generated statements based on multiple groups of input variables to batch generate SQL statements.
[0049] In one embodiment of the present application, multiple groups of input variables are obtained, and SQL statements are generated multiple times based on each group of input variables, so that batch production of SQL statements based on preset statements can be achieved.
[0050] Please refer to Figure 3 , Figure 3 This is a flowchart showing an exemplary embodiment of the present application showing a method for defining and parsing SQL statements. Figure 3 As shown, the process of defining and parsing SQL statements includes inputting marker variables and marker functions into the input device to obtain a preset SQL statement with a marker, parsing and converting the marker variables, inputting the converted marker function into the marker function executor for parsing and execution, obtaining the execution result of the marker function, replacing the marker function based on the execution result of the marker function, and obtaining the assembled SQL generation statement.
[0051] For example, to implement a business scenario where students' personal information is queried based on conditions such as name, grade, and city, the SQL preset statement is:
[0052] Select id, name, age, grade, city, sex from student
[0053] Where 1=1
[0054] $$_if($$_isNull('$$_{name}'),"","and name="$$_{name}'")
[0055] $$_if($$_isNull('$$_{city}'),"","and city like
[0056] '%$$_{city}%'")
[0057] After the SQL preset statement is written, enter the SQL preset statement and the input variables to be used through the input tool, and the marked SQL preset statement can be quickly converted into the final required SQL generated statement. The operations involved are:
[0058] Define special tags. This example defines $$_ as a special tag that needs to be parsed. Define a tag function executor. The tag function executor converts a string into an executable anonymous function through the constructor provided by the Function object. When the function is executed, it injects specific tag functions such as $$_if, $$_isNull, etc. Define tag functions. Tag functions are a series of special functions that do not conform to SQL rules, such as $$_if, $$_isNull, ...isNull, $$_if, $$_isNull, $$_isNull, $$_isNull, $$_isNull, $$_isNull, $$_isNull, $$_isNull, $$_
[0059] $$_isNull and others. These functions can be used in SQL statements to facilitate and quickly complete the writing of dynamic SQL statements. During actual operation, the tag function executor will execute these special functions and obtain the return results. Functions of commonly used tag functions: $$_isNull determines whether the passed value is null. If it is null, it returns true; otherwise, it returns false. $$_if accepts three parameters: flag, cond1, and cond2. Its function is to determine whether the passed flag is true. If it is true, it returns cond1; otherwise, it returns cond2. Extract tag variables. Through methods such as xml parsing or regular parsing, extract the tag variables and their corresponding variable names for conversion. Extraction results are like: $$_{name} and its corresponding variable name is name, $$_{city} and its corresponding variable name is city.
[0060] Tag variable conversion: According to the variable names of the input variables, convert the tag variables in the corresponding SQL preset statements to actual values (the values of the input variables). If the value of the corresponding tag variable cannot be found in the passed input variables (i.e., the tag variable replacement based on the input variables fails), the tag variable is converted to an empty string. Conversion results are like: $$_if($$_isNull('test'), "", "and name = 'test'") and $$_if($$_isNull(''), "", "and city like '%%'").
[0061] Tag function parsing: After the tag variable conversion, extract the tag function statement segments with function tags in the converted SQL through the same method, and put the extracted results into the tag function executor for execution to obtain the function execution results of the tag functions. For example, the function execution result of $$_if($$_isNull('test'), "", "and name = 'test'") is: and name = 'test'. The function execution result of $$_if($$_isNull(''), "", "and city like '%%'") is empty.
[0062] SQL statement assembly: Replace the tag function statement segments with the function execution results of the tag functions to obtain the finally actual SQL generation statement to be executed. For example, the SQL generation statement obtained after assembling the SQL preset statement in the above example is:
[0063] Select id, name, age, grade, city, sex from student
[0064] Where 1 = 1
[0065] and name = 'Test'
[0066] See also Figure 4 , Figure 3 This is a block diagram of a device for dynamically generating SQL statements as shown in an exemplary embodiment of the present application. The device can be applied to Figure 1 The implementation environment shown is as follows. The apparatus may also be applicable to other exemplary implementation environments and specifically configured in other devices. This embodiment does not limit the implementation environment to which the apparatus is applicable.
[0067] like Figure 4 As shown, the exemplary SQL statement dynamic generation device includes:
[0068] Input module 401, used to obtain preset statements and input variables, the preset statements include tag variables and tag functions, the tag variables are assignable parameters, and the tag functions are executable functions including tag variables;
[0069] An execution module 402 is configured to replace corresponding marker variables based on input variables to obtain a statement to be parsed, extract a marker function from the statement to be parsed, and execute the marker function to obtain a function execution result;
[0070] The assembly module 403 is used to replace the corresponding marked function based on the function execution result to obtain a generated statement.
[0071] In one embodiment of the present application, the execution module 402 executes the marking function to obtain the function execution result, including: if the replaced marking variable is a non-null value, the execution result of the null value judgment function is a true value; if the replaced marking variable is a null value, the execution result of the null value judgment function is a false value; if the execution result of the null value judgment function is a true value, the conditional judgment function returns a second result, and the second result is used as the function execution result; if the execution result of the null value judgment function is a false value, the conditional judgment function returns a first result, and the first result is used as the function execution result; wherein, the first result is null, and the second result is the SQL clause corresponding to the marking variable.
[0072] In one embodiment of the present application, after the SQL statement dynamic generation device obtains the preset statement and input variables, it also includes: obtaining multiple groups of input variables, replacing the corresponding marking variables based on one group of input variables to obtain the statement to be parsed, extracting the marking function in the statement to be parsed, and executing the marking function to obtain the function execution result, replacing the corresponding marking function based on the function execution result to obtain a group of generated statements; repeating the above steps to obtain multiple groups of generated statements based on multiple groups of input variables to batch generate SQL statements.
[0073] In one embodiment of the present application, the execution module 402 replaces the corresponding marker variable based on the input variable to obtain the statement to be parsed, and extracting the marker function in the statement to be parsed includes: identifying the marker variable based on the variable marker to replace the corresponding marker variable based on the input variable, and the variable marker is a marker language pre-set in the marker variable; identifying the marker function based on the function marker, so that the function executor executes the marker function based on the function marker, and the function marker is a marker language pre-set in the marker function.
[0074] In one embodiment of the present application, the execution module 402 identifies the tag variable based on the variable marker, including: writing a regular expression based on the format of the variable marker to match the variable marker; identifying and extracting the variable name of the tag variable according to the regular expression, so as to replace the input variable with the corresponding tag variable based on the variable name.
[0075] In one embodiment of the present application, the execution module 402 identifies the marked function based on the function marker, including: identifying the function marker based on the extensible markup language, and constructing an object tree based on the identification result, the object tree including function nodes for storing the marked function; traversing from the root node of the object tree, searching all function nodes in the object tree, and extracting the marked function.
[0076] In one embodiment of the present application, the execution module 402 replaces the corresponding marker variable based on the input variable to obtain a statement to be parsed: if a marker variable fails to be replaced based on the input variable, a marker variable is replaced based on a null character.
[0077] Through the above device, by parsing SQL preset statements with marker variables and marker variables into generated statements that meet SQL specifications, dynamic generation of SQL statements can be achieved, the flexibility of SQL statements can be improved, the threshold for writing dynamic SQL statements can be lowered, and the development efficiency of SQL coding can be improved.
[0078] It can be understood that the SQL statement dynamic generation device provided in the above embodiment and the SQL statement dynamic generation method provided in the above embodiment belong to the same concept, wherein the specific manner in which the SQL statement dynamic generation method performs operations has been described in detail in the above embodiment and will not be repeated here. In actual applications, the SQL statement dynamic generation device provided in the above embodiment can allocate the above functions to different functional modules as needed, that is, divide the internal structure of the SQL statement dynamic generation device into different functional modules, and then implement all or part of the functions of the corresponding functional modules through the SQL statement dynamic generation method described in the above embodiment, and no specific restrictions are imposed here. For example, the input module 401 includes a module for executing step S210 and related steps, the execution module 402 includes a module for executing step S220 and related steps, and the assembly module 403 includes a module for executing step S230 and related steps.
[0079] Figure 5 The following is a schematic diagram showing the structure of a computer system suitable for implementing an electronic device according to an embodiment of the present application. Figure 5 The computer system 500 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0080] like Figure 5 As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 502 or the program loaded from the storage part 508 to the random access memory (RAM) 503, such as executing the method described in the above embodiment. Various programs and data required for system operation are also stored in the RAM 503. The CPU 501, ROM 502 and RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0081] The following components are connected to the I / O interface 505: an input section 506 including a keyboard, a mouse, and the like; an output section 507 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 508 including a hard disk; and a communication section 509 including a network interface card such as a LAN (Local Area Network) card or a modem. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to the I / O interface 505 as needed. Removable media 511, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 510 as needed, so that computer programs read from the removable media can be installed in the storage section 508 as needed.
[0082] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 509, and / or installed from a removable medium 511. When the computer program is executed by the central processing unit (CPU) 501, the various functions defined in the system of the present application are executed.
[0083] It should be noted that the computer-readable medium shown in the embodiments of the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, which carries a computer-readable computer program. This propagated data signal can take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. A computer program embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wired, or any suitable combination thereof.
[0084] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present application. Among them, each box in the flowchart or block diagram can represent a module, program segment, or part of the code, and the above-mentioned module, program segment, or part of the code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0085] The units involved in the embodiments described in this application may be implemented by software or hardware, and the units described may also be set in a processor. In some cases, the names of these units do not constitute limitations on the units themselves.
[0086] Another aspect of the present application provides a computer-readable storage medium having a computer program stored thereon. When executed by a computer processor, the computer program causes the computer to execute the aforementioned method for dynamically generating SQL statements. The computer-readable storage medium may be included in the electronic device described in the above embodiments, or may exist independently and not be incorporated into the electronic device.
[0087] Another aspect of the present application provides a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the method for dynamically generating SQL statements provided in each of the above embodiments.
[0088] The above embodiments are merely illustrative of the principles and effects of this application and are not intended to limit this application. Anyone skilled in the art may modify or alter the above embodiments without departing from the spirit and scope of this application. Therefore, any equivalent modifications or alterations accomplished by a person of ordinary skill in the art without departing from the spirit and technical concepts disclosed in this application shall be covered by the claims of this application.
Claims
1. A method for dynamically generating SQL statements, characterized in that: The dynamic SQL statement parsing method includes: Obtaining a preset statement and input variables, wherein the preset statement includes a tag variable and a tag function, wherein the tag variable is an assignable parameter and the tag function is an executable function including the tag variable; Replacing the corresponding tag variable based on the input variable to obtain a statement to be parsed, extracting a tag function in the statement to be parsed, and executing the tag function to obtain a function execution result; Based on the function execution result, the corresponding marked function is replaced to obtain a generated statement.
2. The method for dynamically generating SQL statements according to claim 1, wherein: The marking function includes at least a condition judgment function and a null value judgment function. Executing the marking function to obtain a function execution result includes: If the replaced tag variable is a null value, the execution result of the null value judgment function is a true value; If the replaced tag variable is a non-null value, the execution result of the null value judgment function is a false value; If the execution result of the null value judgment function is a true value, the condition judgment function returns a first result, and uses the first result as the function execution result; If the execution result of the null value judgment function is a false value, the condition judgment function returns a second result, and uses the second result as the function execution result; The first result is empty, and the second result is the SQL clause corresponding to the marker variable.
3. The method for dynamically generating SQL statements according to claim 1, wherein: After obtaining the preset statements and input variables, it also includes: Obtaining multiple groups of input variables, replacing the corresponding labeling variables based on one group of input variables to obtain a statement to be parsed, extracting a labeling function in the statement to be parsed, executing the labeling function to obtain a function execution result, and replacing the corresponding labeling function based on the function execution result to obtain a set of generated statements; Repeat the above steps to obtain multiple sets of generated statements based on multiple sets of input variables to generate SQL statements in batches.
4. The method for dynamically generating SQL statements according to any one of claims 1 to 3, characterized in that: Replacing the corresponding marker variable based on the input variable to obtain a statement to be parsed, and extracting the marker function in the statement to be parsed includes: Identifying the marker variable based on a variable marker to replace the corresponding marker variable based on the input variable, wherein the variable marker is a markup language pre-set in the marker variable; The marked function is identified based on a function identifier so that a function executor executes the marked function based on the function identifier, wherein the function identifier is a markup language pre-set in the marked function.
5. The method for dynamically generating SQL statements according to claim 4, wherein: Identifying the marker variable based on the variable identifier includes: Writing a regular expression based on the format of the variable marker to match the variable marker; The variable name of the tag variable is identified and extracted according to the regular expression, so as to replace the corresponding tag variable with the input variable based on the variable name.
6. The method for dynamically generating SQL statements according to claim 4, wherein: Identifying the marked function based on the function identifier includes: Identifying the function tag based on extensible markup language, and constructing an object tree based on the identification result, wherein the object tree includes a function node for storing the marked function; Traversing from the root node of the object tree, searching for all function nodes in the object tree, and extracting the marked functions.
7. The method for dynamically generating SQL statements according to any one of claims 1 to 3, characterized in that: Based on the input variable, the corresponding tag variable is replaced to obtain the statement to be parsed: If a certain marker variable fails to be replaced based on the input variable, the certain marker variable is replaced based on a null character.
8. A device for dynamically generating SQL statements, characterized in that: The device comprises: An input module, configured to obtain a preset statement and input variables, wherein the preset statement includes a tag variable and a tag function, wherein the tag variable is an assignable parameter and the tag function is an executable function including the tag variable; an execution module, configured to replace the corresponding tag variable based on the input variable to obtain a statement to be parsed, extract a tag function from the statement to be parsed, and execute the tag function to obtain a function execution result; An assembly module is used to replace the corresponding marked function based on the function execution result to obtain a generated statement.
9. An electronic device, characterized in that: The electronic device comprises: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, enables the electronic device to implement the method for dynamically generating SQL statements according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that A computer program is stored thereon, and when the computer program is executed by a processor of a computer, the computer is caused to execute the method for dynamically generating SQL statements according to any one of claims 1 to 7.
Citation Information
Patent Citations
Functional SQL query method and device supporting dynamic variables, equipment and medium
CN111984669A
Data processing method and device, electronic equipment and computer readable storage medium
CN116414859A
Data cross-database conversion method and device, electronic equipment and storage medium
CN119025502A