Database dynamic sql application method, device, medium and product
By parsing and validating the parameters of dynamic SQL, a mapping relationship between parameter tags and formal parameter definitions is constructed, and the actual parameter set is pre-validated. This solves the problem of delayed exposure of parameter errors in dynamic SQL execution, achieving safe and efficient execution while reducing costs.
Patent Information
- Application Number
- CN202611398759.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-09
- Publication Date
- 2026-11-27
AI Technical Summary
In existing technologies, the execution of dynamic SQL cannot expose parameter errors before execution, which affects the accuracy of query results and increases execution costs.
By obtaining SQL statement templates, parameter setting information, and actual parameter data, parameter parsing and validity verification are performed, a mapping relationship between parameter tags and formal parameter definitions is constructed, parameter passing methods are identified, the actual parameter set is pre-verified, and execution results are generated according to the execution plan. Positional parameter passing, named parameter passing, and combined parameter passing are supported.
It enables secure and efficient execution of dynamic SQL, detects parameter errors in advance, avoids delayed exposure, reduces execution costs, and improves user experience.
Smart Images

Figure CN123178433A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database technology, and in particular to a method, device, medium, and product for applying dynamic SQL in a database. Background Technology
[0002] Dynamic SQL is a programming method in the database field that generates and executes SQL statements at runtime. It is widely used in scenarios where business query logic is flexible and varied. Developers can dynamically construct or adjust SQL statements according to business needs to achieve functions such as multi-condition combined queries and dynamic table operations, making it one of the core technologies in database application development.
[0003] Currently, the execution of dynamic SQL mainly involves three steps: concatenating the SQL string, executing the dynamic statement, and obtaining the execution result. Parameter compliance verification can only be completed during the SQL execution phase, making it impossible to expose errors before execution. This can easily affect the accuracy of query results and increase execution costs. Summary of the Invention
[0004] One object of the present invention is to provide a method, device, medium and product for applying dynamic SQL in a database that can solve any of the above problems.
[0005] Specifically, this invention provides a method for applying dynamic SQL in a database, comprising: Obtain the SQL statement template, parameter setting information, and actual parameter data to be executed. The parameter value positions in the SQL statement template are parameter tags identified by preset names. The parameter setting information includes the formal parameter definition corresponding to each parameter tag. The parameter setting information is parsed and its validity is verified to obtain structured parameter metadata, which is then cached. Scan the parameter tags in the SQL statement template and construct a mapping relationship between each parameter tag and the corresponding formal parameter definition in the parameter metadata; Parse the actual parameter data and identify the parameter passing method; Based on the parameter passing method and mapping relationship, determine the actual parameter value that matches the parameter tag for each parameter, and obtain the set of actual parameters arranged in the order of parameters in the SQL statement template; Pre-validate the actual parameter set based on the parameter metadata; Determine the execution plan based on the SQL statement template; The pre-validated set of actual parameters is applied to the execution plan to obtain the execution result.
[0006] Optionally, the steps of parsing and validating the parameter setting information to obtain structured parameter metadata and caching it include: Parse the parameter setting information to obtain all formal parameter definitions. Each formal parameter definition includes the corresponding preset name, parameter type, parameter precision, parameter mode, and parameter default value. Verify whether the parameter type defined for each formal parameter is supported by the database, and whether the parameter type, parameter precision, and parameter default value of each formal parameter are matched. If all are yes, then the formal parameter definition is deemed to be valid. Given that all formal parameter definitions are valid, we obtain parameter metadata composed of all formal parameter definitions.
[0007] Optionally, the step of determining the actual parameter value matching each parameter tag based on the parameter passing method and mapping relationship includes: Extract the parameter markers from the SQL statement template sequentially; Based on the mapping relationship, determine whether the parameter pattern corresponding to the parameter tag is the output pattern. If so, set the parameter tag to match the output variable. If it is not in output mode, check if the parameter marker has a corresponding actual parameter value. If it does, match the actual parameter value with the parameter marker. If not, determine the default value of the parameter corresponding to the parameter marker according to the mapping relationship, and match the default value of the parameter with the parameter marker.
[0008] Optionally, the step of checking whether a parameter marker has a corresponding actual parameter value includes: If the parameter passing method is positional parameter passing, check if there is an actual parameter value in the actual parameter data whose position is consistent with the position of the parameter mark in the SQL statement template. If so, it is determined that the parameter mark has a corresponding actual parameter value; otherwise, it is determined that the parameter mark does not have a corresponding actual parameter value. If the parameter passing method is named parameter passing, check if there is an actual parameter value in the actual parameter data whose preset name is consistent with the preset name of the parameter tag. If so, it is determined that the parameter tag has a corresponding actual parameter value; otherwise, it is determined that the parameter tag does not have a corresponding actual parameter value. If the parameter passing method is combined parameter passing, check whether there is a simple parameter value in the actual parameter data whose position is consistent with the position of the parameter mark in the SQL statement template, or whether there is an actual parameter value whose preset name is consistent with the preset name of the parameter mark. If either one is yes, it is determined that the parameter mark has a corresponding actual parameter value. If neither is yes, it is determined that the parameter mark does not have a corresponding actual parameter value.
[0009] Optionally, the step of pre-validating the actual parameter set based on the parameter metadata includes: Verify one by one whether the data type of each actual parameter value is consistent with the data type of the corresponding parameter label, and whether each actual parameter value meets the precision constraints of the corresponding parameter label; If both are yes, the pre-verification is deemed successful; if any is no, the pre-verification is deemed unsuccessful, the process is terminated, and an error message is output.
[0010] Optionally, the steps for determining the execution plan based on the SQL statement template include: Replace all parameter markers in the SQL statement template with standard position placeholders to obtain a normalized SQL skeleton. Use the text of the normalized SQL skeleton as the cache key to query the execution plan cache pool; If a matching execution plan exists in the execution plan cache pool, then the matching execution plan is reused; If no matching execution plan is found in the execution plan cache pool, an execution plan with a normalized SQL skeleton is generated and written to the execution plan cache pool.
[0011] Optionally, the step of constructing the mapping relationship between each parameter tag and the corresponding formal parameter definition in the parameter metadata includes: The mapping relationship is cached to session-level storage for reuse when the SQL statement template is invoked again.
[0012] According to another aspect of the present invention, a computer device is also provided, including a memory, a processor, and a computer executable program stored in the memory and running on the processor, wherein the processor, when executing the computer executable program, implements the application method of dynamic SQL for a database according to any of the preceding claims.
[0013] According to another aspect of the present invention, a computer-readable storage medium is also provided, on which a computer-executable program is stored, which, when executed by a processor, implements the application method of dynamic SQL for a database according to any one of the preceding claims.
[0014] According to another aspect of the present invention, a computer program product is also provided, comprising a computer executable program that, when executed by a processor, implements the application method of dynamic SQL for a database according to any of the preceding claims.
[0015] The database dynamic SQL application method of this invention obtains SQL statement templates, parameter setting information, and actual parameter data. It parses and validates the parameter setting information to obtain structured parameter metadata, which is then cached. It scans parameter markers in the SQL statement template and constructs a mapping relationship between parameter markers and corresponding formal parameter definitions in the parameter metadata. It parses the actual parameter data and identifies the parameter passing method, obtaining a set of actual parameters arranged in the order of parameters in the SQL statement template. Based on the SQL statement template, it determines the execution plan and applies the pre-validated set of actual parameters to the execution plan to obtain the execution result, thus achieving secure and efficient execution of dynamic SQL. Specifically, it separates the SQL statement template from the parameter definition, allowing pre-validation of the actual parameter set based on parameter metadata before SQL execution. This enables early detection of parameter errors, avoiding delayed error exposure, and timely error detection and process termination, reducing execution costs. Furthermore, it supports various flexible parameter passing methods, including positional parameter passing, named parameter passing, and combined parameter passing, improving the user experience.
[0016] The above and other objects, advantages and features of the present invention will become more apparent to those skilled in the art from the following detailed description of specific embodiments of the invention in conjunction with the accompanying drawings. Attached Figure Description
[0017] The following sections will describe some specific embodiments of the invention in detail by way of example and not limitation, with reference to the accompanying drawings. The same reference numerals in the drawings denote the same or similar parts or portions. Those skilled in the art should understand that these drawings are not necessarily drawn to scale. In the drawings: Figure 1 This is a schematic flowchart illustrating a method for applying dynamic SQL in a database according to an embodiment of the present invention; Figure 2 This is a schematic flowchart illustrating the method for obtaining parameter metadata in a database dynamic SQL application according to an embodiment of the present invention; Figure 3 This is a schematic flowchart illustrating the determination of actual parameter values for parameter tag matching in a database dynamic SQL application method according to an embodiment of the present invention; Figure 4 This is a schematic flowchart illustrating the determination of an execution plan in a database dynamic SQL application method according to an embodiment of the present invention; Figure 5 This is a schematic diagram of a computer device according to an embodiment of the present invention; Figure 6 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present invention; Figure 7 This is a schematic diagram of a computer program product according to an embodiment of the present invention. Detailed Implementation
[0018] Those skilled in the art should understand that the embodiments described below are merely a part of the embodiments of the present invention, and not all of the embodiments of the present invention. These partial embodiments are intended to explain the technical principles of the present invention and are not intended to limit the scope of protection of the present invention. Based on the embodiments provided by the present invention, all other embodiments obtained by those skilled in the art without creative effort should still fall within the scope of protection of the present invention.
[0019] It should be noted that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be specifically implemented in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus or device (such as a computer-based system, a processor-included system or other system that can fetch and execute instructions from, an instruction execution system, apparatus or device).
[0020] The flowcharts provided in this invention are not intended to indicate that the operations of the method will be performed in any particular order, or that all operations of the method are included in every case. Furthermore, the method may include additional operations. Within the scope of the technical concept provided by the method in this embodiment, additional variations can be made to the above method.
[0021] like Figure 1 As shown, in one embodiment, the method for applying dynamic SQL in a database generally includes: Step S101: Obtain the SQL statement template, parameter setting information, and actual parameter data to be executed. The parameter value positions in the SQL statement template are parameter tags identified by preset names, and the parameter setting information includes the formal parameter definition corresponding to each parameter tag.
[0022] Specifically, an SQL statement template is a framework of SQL statements containing several parameter tags. It only describes the logical structure of the query and does not contain the specific parameter values. The parameter tags are identified by preset names and are embedded in the SQL statement where parameter values need to be filled in, such as the filter value position in the WHERE condition or the field value position in the INSERT statement.
[0023] The parameter setting information is a collection of formal parameter definitions for each parameter, with each parameter tag corresponding to one formal parameter definition. The formal parameter definition includes the corresponding default name, parameter type, parameter precision, parameter mode, and parameter default value. Specifically, the parameter type is used to limit the data type of the parameter, such as INT, VARCHAR, DATE, etc.; the parameter precision is used to limit the value range or length of the parameter, for example, 50 in VARCHAR(50) indicates that the maximum string length is 50 bytes; the parameter mode is used to identify the input / output attributes of the parameter, including input mode, output mode, and input / output mode; the parameter default value is a fallback value when the user does not pass in the corresponding parameter.
[0024] The actual parameter data is the specific parameter values passed in by the user during execution.
[0025] For example, the SQL statement template can be "SELECT FROM t1 WHERE id = param1 AND name = param2”, where param1 and param2 are parameter markers identified by preset names. The corresponding parameter setting information can be “param1 INT, param2 VARCHAR (50) DEFAULT 'unknown'”. The actual parameter data can be “(101, 'Alice')” in the positional parameter form, or “param2='Alice',param1=101” in the named parameter form.
[0026] Step S102: Parse and validate the parameter setting information to obtain structured parameter metadata and cache it.
[0027] The parameter metadata is structured data formed after parsing and validation. This step first performs lexical and syntactic parsing on the parameter settings input by the user, extracts the attributes of each formal parameter definition, and then performs validity validation on each one. After the validation is passed, standardized parameter metadata is generated and temporarily stored for subsequent process calls.
[0028] Specifically, refer to Figure 2 As shown, this step includes: Step S201: Parse the parameter setting information to obtain the definitions of all formal parameters.
[0029] Specifically, the system performs syntax parsing on the obtained raw parameter setting information to extract complete information about each formal parameter definition. The specific content of the formal parameter definitions is described above.
[0030] Step S202: Verify whether the parameter type defined for each formal parameter is supported by the database, and whether the parameter type, parameter precision, and parameter default value of each formal parameter are matched. If all are yes, then the formal parameter definition is deemed to be valid.
[0031] Specifically, the checks include verifying whether the parameter type defined for each formal parameter is supported by the database to confirm whether the defined parameter type belongs to the set of legal data types supported by the database. The checks also verify whether the parameter type, parameter precision, and parameter default value match to confirm whether the type of the default value is compatible with the corresponding data type (e.g., integer types cannot have configurable default values, and strings cannot have default values), and whether the precision of the default value meets the corresponding precision constraints (e.g., if the parameter type is VARCHAR(10), but the default value is a string of length 15, then the precision does not match the default value).
[0032] In this step, the system verifies each formal parameter definition one by one. If a formal parameter definition fails the verification, the system can record the error message, terminate the process, and output the error message.
[0033] Step S203: Under the condition that all formal parameter definitions are valid, obtain the parameter meta-information composed of all formal parameter definitions.
[0034] Specifically, this means obtaining data information that corresponds one-to-one with the preset names and formal parameter definitions marked by parameters, caching the preset name, parameter type, parameter precision, parameter mode, and parameter default value attributes of each parameter to form parameter metadata that can be quickly retrieved, making it convenient for subsequent steps to quickly query the corresponding attributes by parameter name.
[0035] Step S103: Scan the parameter markers in the SQL statement template and construct a mapping relationship between each parameter marker and the corresponding formal parameter definition in the parameter metadata.
[0036] Specifically, the mapping relationship is used to establish a one-to-one correspondence between parameter markers appearing in the SQL statement template and the formal parameter definitions in the parameter metadata, specifying the order of each parameter marker in the SQL statement template, as well as its corresponding preset name, parameter type, parameter precision, parameter mode, and parameter default value. By scanning the SQL statement template, the preset names and appearance order of all parameter markers are identified. Then, based on the preset names, the corresponding entries in the parameter metadata are matched to generate a mapping table that records the position of each parameter marker in the SQL statement template, as well as its corresponding preset name, parameter type, parameter precision, parameter mode, and parameter default value.
[0037] Step S104: parse the actual parameter data and identify the parameter transmission method.
[0038] Specifically, parameter passing methods include three categories: positional parameter passing, named parameter passing, and combined parameter passing. This step determines the parameter passing method by parsing the format of the actual parameter data: if the actual parameter data is a set of simple values arranged in sequence, it is determined to be positional parameter passing; if the actual parameter data is in key-value pair format, containing a preset parameter name and its corresponding specific value, it is determined to be named parameter passing; if it contains both simple values and key-value pair values, it is determined to be combined parameter passing.
[0039] Step S105: Determine the actual parameter value that matches each parameter tag according to the parameter passing method and mapping relationship, and obtain the actual parameter set arranged in the order of parameters in the SQL statement template.
[0040] Specifically, this step, based on the identified parameter passing method and the mapping relationship between parameter tags and formal parameter definitions, matches the corresponding actual value for each parameter tag; for parameters for which the user has not passed a value, the default value in the formal parameter definition is automatically used to fill the value; finally, an actual parameter set is generated in the order in which the parameter tags appear in the SQL statement template, providing standardized input for subsequent pre-validation and execution binding.
[0041] Specifically, refer to Figure 3 As shown, this step includes: Step S301: Extract the parameter markers from the SQL statement template sequentially.
[0042] Specifically, parameter tags are extracted and processed one by one according to the order in which they appear in the SQL statement template, ensuring that the final generated set of actual parameters is consistent with the order of parameters in the statement.
[0043] Step S302: Determine whether the parameter mode corresponding to the parameter tag is the output mode according to the mapping relationship. If yes, proceed to step S303; otherwise, proceed to step S304.
[0044] Specifically, output mode, or OUT mode, does not require receiving input values from the caller; instead, it requires receiving a return value as the result. Non-output modes include input mode (IN mode) and input / output mode (INOUT mode), both of which require receiving input values from the caller.
[0045] Step S303: Set the parameter marker to match the output variable.
[0046] In output mode, there is no need for corresponding actual parameter values. Instead, a matching output variable is set, which means preparing a variable container for the parameter marker to receive the return value.
[0047] Step S304: Check if the parameter marker has a corresponding actual parameter value. If yes, proceed to step S305; otherwise, proceed to step S306.
[0048] Specifically, because parameter markers may have corresponding default values, it's possible that no actual parameter value was entered. Therefore, checking if a parameter marker has a corresponding actual parameter value confirms whether the default value should be used.
[0049] Furthermore, if the parameter passing method is positional parameter passing, it checks whether there exists an actual parameter value in the actual parameter data whose position matches the position of the parameter marker in the SQL statement template. If so, it is determined that the parameter marker has a corresponding actual parameter value; otherwise, it is determined that the parameter marker does not have a corresponding actual parameter value. In other words, positional parameter passing strictly corresponds one-to-one with the parameter position, and the Nth actual parameter value corresponds to the Nth parameter marker appearing in the SQL statement template.
[0050] For example, the parameter tags in the SQL statement template are in the order of [param1, param2, param3]. The actual parameter data is ['001', 'Zhang San'] (two values). For the first parameter tag, param1 (position 1), the actual parameter data contains the value "001" (position 1), so it has a corresponding actual parameter value. For the third parameter tag, param3 (position 3), the actual parameter data does not contain a value (position 3), so it does not have a corresponding actual parameter value.
[0051] If the parameter passing method is named parameter passing, the system checks if there is an actual parameter value in the actual parameter data whose preset name matches the preset name of the parameter tag. If so, it determines that the parameter tag has a corresponding actual parameter value; otherwise, it determines that the parameter tag does not have a corresponding actual parameter value. Specifically, named parameter passing matches parameters by name, regardless of the order of the actual parameter values. Users can arbitrarily adjust the order of parameter values, improving the flexibility of parameter passing.
[0052] For example, the parameter tags in the SQL statement template are in the order of [param1, param2, param3]. The actual parameter data is {param2: 'Zhang San', param3: 25}. For parameter tag param2, its default name is "param2", and the actual parameter data contains a value "Zhang San" with the key name "param2", so it has a corresponding actual parameter value. For parameter tag param1, the actual parameter data does not contain a value with the key name "param1", so it does not have a corresponding actual parameter value.
[0053] If the parameter passing method is combined parameter passing, check if there is a simple parameter value in the actual parameter data whose position matches the position of the parameter tag in the SQL statement template, or if there is an actual parameter value whose preset name matches the preset name of the parameter tag. If either is true, it is determined that the parameter tag has a corresponding actual parameter value; if neither is true, it is determined that the parameter tag does not have a corresponding actual parameter value. Combined parameter passing is a scenario where positional parameter passing and named parameter passing are used together.
[0054] For example, the parameter tag order in the SQL statement template is [param1, param2, param3]. The actual parameter data is ['001', param3=25] (one positional parameter and one named parameter). For parameter tag param1 (position 1), the actual parameter data contains the value "001" at position 1, so it has a corresponding actual parameter value. For parameter tag param3 (position 3), although the actual parameter data does not contain a value at position 3, it contains a value 25 with the preset name "param3", so it also has a corresponding actual parameter value. For parameter tag param2 (position 2), there is neither a simple actual parameter value for position 2 (the second actual parameter value has a name and is not a simple value), nor a value with the preset name "param2", so it does not have a corresponding actual parameter value.
[0055] Step S305: Match the detected actual parameter values with the parameter tags.
[0056] If an actual parameter value is found, that value is matched against the parameter tag.
[0057] Step S306: Determine the default value of the parameter corresponding to the parameter tag according to the mapping relationship, and match the default value of the parameter with the parameter tag.
[0058] If no actual parameter value is found, the parameter's default value will be matched with the parameter tag.
[0059] Step S106: Perform pre-verification of the actual parameter set based on the parameter metadata.
[0060] Specifically, pre-validation refers to performing a compliance check on each value in the actual parameter set before the SQL statement enters the database execution stage, based on the constraints such as parameter type and parameter precision defined in the parameter metadata.
[0061] This step checks whether the type of each actual parameter value is consistent with the type of the corresponding formal parameter and whether each actual parameter value meets the precision constraints of the corresponding parameter mark. If both are true, the pre-verification is deemed successful. If either is false, the pre-verification is deemed unsuccessful, the process is terminated, and an error message is output.
[0062] Verify that the data type of the actual parameter value is consistent with the parameter type declared in the formal parameter definition. For example, if the formal parameter definition declares the parameter type as integer, but the actual parameter value is a string, the type validation will fail.
[0063] Verify whether the actual parameter value conforms to the precision constraints of the corresponding parameter tag. For example, if the formal parameter definition declares the parameter type as VARCHAR(10) (variable length string with a maximum length of 10), but the actual parameter value has a length of 15, then the precision check will fail.
[0064] Step S107: Determine the execution plan based on the SQL statement template.
[0065] Specifically, the execution plan is the SQL execution logic scheme generated by the database kernel, including execution strategies such as query optimization, index selection, and table join order. This step generates or reuses the corresponding execution plan based on the structure of the SQL statement template.
[0066] Specifically, refer to Figure 4 As shown, it includes: Step S401: Replace all parameter markers in the SQL statement template with standard position placeholders to obtain the normalized SQL skeleton.
[0067] Specifically, standard positional placeholders typically take the form of "?". After replacement, all parameter markers identified by preset names are eliminated, transforming the SQL logical structure into one with standard positional placeholders. For example, replacing "SELECT..." with "..." Replace "FROM t1WHERE id = param1 AND name = param2" with "SELECT FROM t1 WHERE id = ?AND name = ?", which is the normalized SQL skeleton.
[0068] Step S402: Use the text of the normalized SQL skeleton as the cache key to query the execution plan cache pool.
[0069] Specifically, the execution plan cache pool stores generated execution plans. The cache key uses normalized SQL skeleton text to ensure that statements with the same structure correspond to the same cache key. Through normalization, differences in parameter naming will not affect cache matching. As long as the query logic structure is the same, the same execution plan can be reused, significantly improving the cache hit rate.
[0070] Step S403: If a matching execution plan exists in the execution plan cache pool, then the matching execution plan is reused.
[0071] If an execution plan matching the cache key exists in the execution plan cache pool, the system can directly retrieve and reuse the execution plan without submitting SQL to the database for hard parsing.
[0072] Step S404: If there is no matching execution plan in the execution plan cache pool, then generate an execution plan with a normalized SQL skeleton and write it into the execution plan cache pool.
[0073] If no execution plan matching the cache key exists in the execution plan cache pool, the system submits a normalized SQL skeleton to the database for hard parsing to generate a new execution plan. After generation, the execution plan is stored in the execution plan cache pool using the normalized SQL skeleton text as the cache key, for reuse by subsequent SQL statements with the same structure.
[0074] Step S108: Apply the pre-verified and qualified set of actual parameters to the execution plan to obtain the execution result.
[0075] Specifically, this step binds the actual parameter values arranged in sequence to the corresponding placeholders in the execution plan, generates a complete executable instance, is executed by the database kernel, and returns the final query results.
[0076] In this embodiment, by acquiring the SQL statement template, parameter setting information, and actual parameter data, the parameter setting information is parsed and validated to obtain structured parameter metadata, which is then cached. Parameter markers in the SQL statement template are scanned, and a mapping relationship is constructed between the parameter markers and the corresponding formal parameter definitions in the parameter metadata. The actual parameter data is parsed, and the parameter passing method is identified, resulting in a set of actual parameters arranged in the order of parameters in the SQL statement template. An execution plan is determined based on the SQL statement template, and the pre-validated set of actual parameters is applied to the execution plan to obtain the execution result, thus achieving secure and efficient execution of dynamic SQL. Specifically, the separation of the SQL statement template and parameter definitions is implemented, allowing for pre-validation of the actual parameter set based on the parameter metadata before SQL execution. This enables early detection of parameter errors, avoiding delayed error exposure, and timely error detection and process termination, reducing execution costs. Furthermore, it supports various flexible parameter passing methods, including positional parameter passing, named parameter passing, and combined parameter passing, improving the user experience.
[0077] By uniformly replacing all parameter markers in the SQL statement template with standard position placeholders to obtain a normalized SQL skeleton, and using the text of the normalized SQL skeleton as the cache key to query the execution plan cache pool, it is possible to ensure that SQL statements with the same logical structure (even if the default names of the parameter markers are different) share the same execution plan, thereby significantly improving the reusability of the execution plan, reducing the number of hard parses in the database, and improving the overall performance of the system.
[0078] In other embodiments of the present invention, the method for applying dynamic SQL in a database, compared to the method described above, further includes the following step after the step of constructing the mapping relationship between each parameter marker and the corresponding formal parameter definition in the parameter metadata: The mapping relationship is cached to session-level storage for reuse when the SQL statement template is invoked again.
[0079] The lifecycle of session-level storage is consistent with the database session. The mapping relationship between parameter tags and formal parameter definitions built during the first call is cached. When the same SQL statement template is called again in the same session, the cached mapping relationship can be read directly, skipping the parameter setting parsing and mapping construction steps, thus improving call efficiency.
[0080] In addition, in some other embodiments of the present invention, after obtaining the actual parameter set, the parameter-related information in the temporary cache can be cleared, specifically, the generated intermediate cache data can be cleared to release memory resources in a timely manner.
[0081] This embodiment also provides a computer device and a computer-readable storage medium. Figure 5 This is a schematic diagram of a computer device 10 according to an embodiment of the present invention. Figure 6 This is a schematic diagram of a computer-readable storage medium 20 according to an embodiment of the present invention.
[0082] Computer device 10 may include memory 110, processor 120, and computer executable program 11 stored on memory 110 and running on processor 120. When processor 120 executes computer executable program 11, it implements the application method of database dynamic SQL of any of the above embodiments.
[0083] The computer-readable storage medium 20 stores a computer-executable program 11 thereon, which, when executed by a processor, implements the application method of dynamic SQL in the database according to any of the above embodiments.
[0084] This embodiment also provides a computer program product. Figure 7 This is a schematic diagram of a computer program product 30 according to an embodiment of the present invention. The computer program product 30 includes a computer executable program 11, which, when executed by a processor 120, implements any of the database dynamic SQL application methods described above.
[0085] Specifically, the computer executable program 11 used to perform the operations of the present invention may be assembly instructions, instruction set architecture (ISA) instructions, computer instructions, computer-related instructions, microcode, firmware instructions, status setting data, or source code or object code written in any combination of one or more programming languages.
[0086] For the purposes of this embodiment, the computer-readable storage medium 20 can be any means capable of containing, storing, communicating, propagating, or transmitting a program for use by or in conjunction with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable media include: an electrical connection having one or more wires (electronic device), a portable computer disk drive (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and editable read-only memory (EPROM or flash memory), fiber optic devices, and portable optical disc read-only memory (CDROM). Furthermore, the computer-readable storage medium 20 can even be paper or other suitable media on which the program can be printed, since the program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, interpreting, or otherwise processing as necessary, and then stored in a computer memory.
[0087] It should be understood that various parts of the present invention can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system.
[0088] Computer device 10 can be, for example, a server, desktop computer, laptop computer, tablet computer, or smartphone. In some examples, computer device 10 can be a cloud acquisition node. Computer device 10 can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, program modules can include routines, programs, object programs, components, logic, data structures, etc., that perform specific tasks or implement specific abstract data types. Computer device 10 can be implemented in a distributed cloud acquisition environment where tasks are performed by remote processing devices linked via a communication network. In a distributed cloud acquisition environment, program modules can reside on local or remote acquisition system storage media, including storage devices.
[0089] Computer device 10 may include a processor 120 adapted to execute stored instructions and a memory 110 that provides temporary storage space for the operation of said instructions during operation. Processor 120 may be a single-core processor, a multi-core processor, an acquisition cluster, or any other configuration. Memory 110 may include random access memory (RAM), read-only memory, flash memory, or any other suitable storage system.
[0090] The processor 120 can be connected via a system interconnect (e.g., PCI, PCI-Express, etc.) to an I / O interface (input / output interface) suitable for connecting the computer device 10 to one or more I / O devices (input / output devices). I / O devices may include, for example, a keyboard and indicating devices, where indicating devices may include a touchpad or touchscreen, etc. I / O devices may be built into the computer device 10 or may be external devices connected to the acquisition device.
[0091] The processor 120 may also be linked via a system interconnect to a display interface suitable for connecting the computer device 10 to a display device. The display device may include a display screen that is a built-in component of the computer device 10. The display device may also include an external computer monitor, television, or projector connected to the computer device 10. Furthermore, a network interface controller (NIC) may be adapted to connect the computer device 10 to a network via a system interconnect. In some embodiments, the NIC may use any suitable interface or protocol (such as an Internet Minicomputer System Interface) to transmit data. The network may be a cellular network, a radio network, a wide area network (WAN), a local area network (LAN), or the Internet, etc. Remote devices may connect to the computer device via the network.
[0092] Therefore, those skilled in the art should recognize that although numerous exemplary embodiments of the present invention have been shown and described in detail herein, many other variations or modifications conforming to the principles of the present invention can be directly determined or derived from the disclosure of the present invention without departing from the spirit and scope of the invention. Thus, the scope of the present invention should be understood and construed as covering all such other variations or modifications.
Claims
1. A method for applying dynamic SQL in a database, comprising: Obtain the SQL statement template, parameter setting information, and actual parameter data to be executed. The parameter value positions in the SQL statement template are parameter tags identified by preset names. The parameter setting information includes the formal parameter definition corresponding to each parameter tag. The parameter setting information is parsed and its validity is verified to obtain structured parameter metadata, which is then cached. Scan the parameter tags in the SQL statement template and construct a mapping relationship between each parameter tag and the corresponding formal parameter definition in the parameter metadata; Analyze the actual parameter data and identify the parameter transmission method; Based on the parameter passing method and the mapping relationship, determine the actual parameter value that matches each parameter tag, and obtain the actual parameter set arranged in the parameter order in the SQL statement template; The actual parameter set is pre-validated based on the parameter metadata. Determine the execution plan based on the SQL statement template; The pre-verified set of actual parameters is applied to the execution plan to obtain the execution result.
2. The method for applying dynamic SQL in a database according to claim 1, wherein... The step of parsing and validating the parameter setting information to obtain structured parameter metadata and caching it includes: Parse the parameter setting information to obtain all formal parameter definitions. Each formal parameter definition includes the corresponding preset name, parameter type, parameter precision, parameter mode, and parameter default value. Verify whether the parameter type of each formal parameter definition is supported by the database, and whether the parameter type, parameter precision, and parameter default value of each formal parameter definition match. If all of these are true, then the formal parameter definition is deemed to be valid. Given that all formal parameter definitions are valid, the parameter metadata composed of all formal parameter definitions is obtained.
3. The method for applying dynamic SQL in a database according to claim 1, wherein... The step of determining the actual parameter value matching each parameter tag according to the parameter passing method and the mapping relationship includes: Extract the parameter markers from the SQL statement template sequentially; Based on the mapping relationship, determine whether the parameter pattern corresponding to the parameter tag is an output pattern. If so, set the parameter tag to match the output variable. If it is not an output mode, check if the parameter tag has a corresponding actual parameter value. If it does, match the actual parameter value with the parameter tag. If not, determine the default value of the parameter corresponding to the parameter tag according to the mapping relationship, and match the default value of the parameter with the parameter tag.
4. The method for applying dynamic SQL in a database according to claim 3, wherein... The step of checking whether the parameter marker has a corresponding actual parameter value includes: If the parameter passing method is positional parameter passing, check whether there is an actual parameter value in the actual parameter data whose position is consistent with the position of the parameter mark in the SQL statement template. If yes, it is determined that the parameter mark has a corresponding actual parameter value; otherwise, it is determined that the parameter mark does not have a corresponding actual parameter value. If the parameter passing method is named parameter passing, check whether there is an actual parameter value in the actual parameter data whose preset name is consistent with the preset name of the parameter tag. If yes, it is determined that the parameter tag has a corresponding actual parameter value; otherwise, it is determined that the parameter tag does not have a corresponding actual parameter value. If the parameter passing method is combined parameter passing, check whether there is a simple parameter value in the actual parameter data whose position is consistent with the position of the parameter mark in the SQL statement template, or whether there is an actual parameter value whose preset name is consistent with the preset name of the parameter mark. If either is true, it is determined that the parameter mark has a corresponding actual parameter value. If neither is true, it is determined that the parameter mark does not have a corresponding actual parameter value.
5. The method for applying dynamic SQL in a database according to claim 1, wherein... The step of pre-validating the actual parameter set based on the parameter metadata includes: Verify one by one whether the data type of each actual parameter value is consistent with the data type of the corresponding parameter label, and whether each actual parameter value meets the precision constraints of the corresponding parameter label; If both are yes, the pre-verification is deemed successful; if any is no, the pre-verification is deemed unsuccessful, the process is terminated, and an error message is output.
6. The method for applying dynamic SQL in a database according to claim 1, wherein... The step of determining the execution plan based on the SQL statement template includes: Replace all parameter markers in the SQL statement template with standard position placeholders to obtain a normalized SQL skeleton. Using the text of the normalized SQL skeleton as the cache key, query the execution plan cache pool; If a matching execution plan exists in the execution plan cache pool, the matching execution plan is reused. If no matching execution plan exists in the execution plan cache pool, the execution plan of the normalized SQL skeleton is generated and written to the execution plan cache pool.
7. The method for applying dynamic SQL in a database according to claim 1, wherein... Following the step of constructing the mapping relationship between each parameter marker and the corresponding formal parameter definition in the parameter metadata, the following is included: The mapping relationship is cached in session-level storage for reuse when the SQL statement template is called again.
8. A computer device comprising a memory, a processor, and a computer-executable program stored in the memory and running on the processor, wherein the processor executes the computer-executable program to implement the application method of dynamic SQL for a database according to any one of claims 1 to 7.
9. A computer-readable storage medium having a computer-executable program stored thereon, the computer-executable program, when executed by a processor, implementing the application method of dynamic SQL for a database according to any one of claims 1 to 7.
10. A computer program product comprising a computer executable program, wherein the computer executable program, when executed by a processor, implements the application method of dynamic SQL for a database according to any one of claims 1 to 7.
