Disclosure of Invention
In view of this, the present invention provides a statement generating method, apparatus, device and storage medium, which can reduce the use cost and simplify the code writing process. The specific scheme is as follows:
in a first aspect, the present application discloses a sentence generation method, including:
writing a target query statement interface and an extensible markup language file;
packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file;
and generating a target query statement based on the extensible markup language file and the packaged interface.
Optionally, the binding the preset information in the xml file with the target query statement interface includes:
assembling the interface class name and the method name in the extensible markup language file into a unique identifier;
determining a corresponding relation between the target query statement interface and the extensible markup language file;
binding the unique identifier with a method in the target query statement interface.
Optionally, the writing of the target query statement interface and the extensible markup language file includes:
compiling an extensible markup language file, and setting the preset information in the extensible markup language file; the preset information includes the interface class name corresponding to the target query statement interface, the method name of the target method corresponding to the target query statement interface, a preset query statement, and label information corresponding to a preset requirement.
Optionally, the determining a corresponding relationship between the target query statement interface and the xml file includes:
determining a corresponding relation between the method in the target query statement interface and the label in the extensible markup language file; each select tag in the extensible markup language file corresponds to a method, and the select tags include a cycle tag and a judgment tag.
Optionally, the generating a target query statement based on the xml file and the packaged interface includes:
judging whether the preset parameters are effective parameters according to preset parameter judgment rules; the preset parameters are parameters set for a target query statement interface when the target query statement interface is written;
and when the preset parameters are effective parameters, generating a target query statement based on the extensible markup language file and the preset parameters.
Optionally, the generating a target query statement based on the xml document and the preset parameter includes:
filling the vacant part of the target query statement in the extensible markup language file by using the preset parameters; the vacant part is a part reserved when the extensible markup language file is written.
Optionally, after generating the target query statement based on the xml file and the packaged interface, the method further includes:
and injecting the packaged interface into a Spring container so as to inquire and call a corresponding interface through the corresponding relation between the extensible markup language file and the packaged interface.
In a second aspect, the present application discloses a sentence generation apparatus, comprising:
the interface compiling module is used for compiling a target query statement interface;
the file compiling module is used for compiling the extensible markup language file;
the interface binding module is used for packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file;
and the statement generating module is used for generating a target query statement based on the extensible markup language file and the packaged interface.
In a third aspect, the present application discloses an electronic device, comprising:
a memory for storing a computer program;
a processor for executing the computer program to implement the steps of the statement generation method as disclosed in the foregoing.
In a fourth aspect, the present application discloses a computer readable storage medium for storing a computer program; wherein the computer program, when executed by a processor, implements a statement generation method as disclosed in the preceding.
As can be seen, the present application provides a sentence generation method, including: writing a target query statement interface and an extensible markup language file; packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file; and generating a target query statement based on the extensible markup language file and the packaged interface. Therefore, the query statement is directly generated by writing the target query statement interface and the codes of the corresponding extensible markup language file, the code writing process is simplified, and the use cost is reduced.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
At present, in most of the existing open-source ES frameworks, in order to follow the mainstream OOP thought, a plurality of query statements are excessively packaged, the use feeling is not good, the main reason is that different from SQL statements of a single query rule, DSL query statements of ES are too complex, so that the packaging of the query statements into the query statements is very inconvenient, developers need to write a large amount of query codes in use, and ambiguity is easy to occur in reading, so that certain burden is caused on writing, using and reading. Therefore, the statement generation method can prevent the code writing process from being simplified and reduce the use cost.
The embodiment of the invention discloses a statement generation method, which is shown in figure 1 and comprises the following steps:
step S11: writing a target query statement interface and an extensible markup language file.
In this embodiment, a target query statement interface and an extensible markup language file are written. It can be understood that when the target query statement interface is written, preset parameters are set for the target query statement interface. The target query statement interface is an interface for calling service in a late-stage injection container, and the preset parameters in the target query statement interface participate in DSL statement generation, that is, the key for dynamically generating DSL statements lies in the preset parameters. And writing an XML (Extensible Markup Language) file corresponding to the target query statement interface.
Step S12: and packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file.
In this embodiment, after a target query statement interface and an extensible markup language file are written, the target query statement interface is packaged to obtain a packaged interface, and preset information in the extensible markup language file is used to bind with the target query statement interface. It can be understood that, an extensible markup language file corresponds to a class, the target query statement interface is packaged to obtain a packaged interface, and then preset information is set for the extensible markup language file, where the preset information includes the interface class name corresponding to the target query statement interface, the method name of the target method corresponding to the target query statement interface, a preset query statement, and tag information corresponding to a preset requirement. And binding preset information in the extensible markup language file with the target query statement interface, and determining a corresponding relation between the target query statement interface and the extensible markup language file under resources (a package in a project). And selecting whether the select label comprises a cycle label and a judgment label or not according to the method in the corresponding class of each select label in the extensible markup language file.
It will be appreciated that a class is a template that describes the behavior and state of a class of objects, and that a method of the class corresponds to a behavior of an object. Classes are templates of objects and methods are templates of object behaviors. The hierarchical relationship is as follows: a class contains a plurality of methods (interfaces), an XML file can also be understood as a template (or a carrier), if the class and the XML file are made to correspond, the class name needs to be written in namespace in the XML file, and the method needs to be written in the sub-tag < select >. The class may contain a plurality of methods, and the XML file may also contain a plurality of select tags. The contents written in the select tag are similar to the method written in the method, and the parameters in the method are packaged in the select tag by $ { }. The sub-tag < foreach > and the sub-tag < if > may exist in < select >, or the sub-tag may not exist, but the sub-tag is a key for dynamically implementing the DSL statement, that is, the dynamic state is based on the judgment of one parameter and different branch results are taken. In addition, the suffix must be xml ending.
Step S13: and generating a target query statement based on the extensible markup language file and the packaged interface.
In this embodiment, a target query statement is generated based on the xml document and the packaged interface. It can be understood that a specific DSL query statement is written in a tag in the xml document, but the DSL query statement is incomplete at this time, a symbol $ { } in an interface for packing a preset parameter in the interface after the package is empty, the preset parameter in the interface after the package is obtained, and the preset parameter is filled in the empty portion in the xml document to obtain a complete DSL query statement.
As can be seen, the present application provides a sentence generation method, including: writing a target query statement interface and an extensible markup language file; packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file; and generating a target query statement based on the extensible markup language file and the packaged interface. Therefore, the query statement is directly generated by writing the target query statement interface and the codes of the corresponding extensible markup language file, the code writing process is simplified, and the use cost is reduced.
Referring to fig. 2, the embodiment of the present invention discloses a statement generation method, and compared with the previous embodiment, the present embodiment further describes and optimizes the technical solution.
Step S21: writing a target query statement interface.
Step S22: and compiling an extensible markup language file, and setting the preset information in the extensible markup language file.
In this embodiment, an extensible markup language file is written, and the preset information is set in the extensible markup language file. It can be understood that the preset information includes the interface class name corresponding to the target query statement interface, the method name of the target method corresponding to the target query statement interface, a preset query statement, and label information corresponding to a preset requirement, where the label information includes, but is not limited to, a loop label and a judgment label. For example, a query script supporting circulation and judgment is written in an extensible markup language file, and the characteristics of XML are utilized to support the functions of judgment and circulation when statements are spliced, so that complex DSL statements can be dynamically spliced. The interface can be a Java interface or other interfaces.
It should be noted that to make the class and XML file correspond, the class name is written in namespace, and the method is written in the sub-tag < select >. The method comprises the steps that interface class names are in namespaces of < mapper > tags in extensible markup language files, a method which is expected to correspond to the interfaces is written in ids of sub-tags < select > in the tags, specific DSL query statements are written in the tags, corresponding parameters in the interfaces are packaged by $ { }, if circulation requirements exist, the < foreach > tags are used, if judgment requirements exist, the < if > tags are used, and the DSL. Xml suffix is a custom suffix, but other suffixes are possible, but must end with xml.
For example, the interface class name in the < mapper > tag is:
namespace = "specific name field", id of sub-tag < select = "getoverlallstats" is the method that is expected to correspond to the interface, and the corresponding parameters in the interface are packed with $ { }, such as $ { request.
The specific codes are as follows:
<mapper namespace="XXX">
<select id="getOverallStats" resultType="java.lang.String">
{ "size": 0, "query": { "bool": { "must": [ { "range": { "dtTime": { "gte": ${request.dtTimeFrom}, "lt": ${request.dtTimeTo} } } }
<if test="request.portList != null and request.portList.size() > 0">
, { "terms": { "port": [
<foreach collection="request.portList" item="item" index="index" separator=","> "${item}" </foreach>
] } }
</if>
it should be noted that, the loop sub-tag < foreach > in < select > may or may not be the judgment sub-tag < if >, but the loop sub-tag and the judgment sub-tag are key for implementing dynamic DSL statement writing, that is, different branch results are taken for the judgment of one parameter.
Step S23: and packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file.
In this embodiment, the target query statement interface is packaged to obtain a packaged interface, and the target query statement interface is bound with preset information in the extensible markup language file. It is understood that the interface class name and the method name in the extensible markup language file are assembled into a unique identifier, and then the unique identifier is bound with the target query statement interface. When the unique identifier and any one piece of information in the target query statement interface are acquired, the corresponding other piece of information can be determined through a binding relationship.
Step S24: and judging whether the preset parameters are effective parameters according to a preset parameter judgment rule.
In this embodiment, whether the preset parameter is an effective parameter is determined according to a preset parameter determination rule. It is understood that the preset parameters are involved in the dynamic generation process of DSL, and are not used when they are not valid parameters.
Step S25: and when the preset parameters are effective parameters, generating a target query statement based on the extensible markup language file and the preset parameters.
In this embodiment, when the preset parameter is an effective parameter, a target query statement is generated based on the xml document and the preset parameter. It can be understood that, when the preset parameter is a valid parameter, the empty part of the target query statement in the xml file is filled with the preset parameter; the vacant part is a part reserved when the extensible markup language file is written. And after the preset parameters are used for filling the vacant part of the target query statement in the extensible markup language file, the target query statement code is complete.
Step S26: and injecting the packaged interface into a Spring container so as to inquire and call a corresponding interface through the corresponding relation between the extensible markup language file and the packaged interface.
In this embodiment, the packaged interface is injected into a Spring container, so as to query and call the corresponding interface through the corresponding relationship between the xml file and the packaged interface. One preferred way to invoke the packaged interface is to fill the Spring container at the end, so as to invoke the interface in the development process. It can be understood that the dynamic proxy is used for injecting the Spring container, when in use, only an interface needs to be written and injected, and parameters in the interface are automatically identified when the query statement is dynamically generated, so that the aim of decoupling the query interface is fulfilled, a developer does not need to use bottom-layer logic, and the use cost is reduced. Meanwhile, the method is not influenced by the version characteristics of the ES, only minimal modification is needed when the ES version is upgraded, the upgrading process is smoother, and reading and modification are convenient.
For details of the step S21, reference may be made to corresponding contents disclosed in the foregoing embodiments, and details are not repeated here.
Therefore, the embodiment of the application writes a target query statement interface; compiling an extensible markup language file, and setting the preset information in the extensible markup language file; packaging the target query statement interface by using preset information in the extensible markup language file to obtain a packaged interface; judging whether the preset parameters are effective parameters or not according to preset parameter judgment rules; when the preset parameters are effective parameters, generating target query statements based on the extensible markup language files and the preset parameters; and injecting the packaged interface into a Spring container so as to query a corresponding interface through the corresponding relation between the extensible markup language file and the packaged interface, thereby simplifying the code writing process, reducing the use cost and avoiding the occurrence of logic code pollution.
Referring to fig. 3, an embodiment of the present application further discloses a statement generating apparatus, which includes:
theinterface writing module 11 is used for writing a target query statement interface;
thefile compiling module 12 is used for compiling the extensible markup language file;
aninterface binding module 13, configured to pack the target query statement interface to obtain a packaged interface, and bind the target query statement interface with preset information in the xml file;
and astatement generating module 14, configured to generate a target query statement based on the xml file and the packaged interface.
As can be seen, the present application includes: writing a target query statement interface and an extensible markup language file; packaging the target query statement interface to obtain a packaged interface, and binding the target query statement interface with preset information in the extensible markup language file; and generating a target query statement based on the extensible markup language file and the packaged interface. Therefore, the query statement is directly generated by writing the target query statement interface and the codes of the corresponding extensible markup language file, the code writing process is simplified, and the use cost is reduced.
In some specific embodiments, theinterface writing module 11 specifically includes:
the interface compiling unit is used for compiling a target query statement interface;
and the parameter setting unit is used for setting preset parameters for the target query statement interface.
In some specific embodiments, thefile writing module 12 specifically includes:
the extensible markup language file compiling unit is used for compiling an extensible markup language file;
an information setting unit, configured to set the preset information in the extensible markup language file; the preset information includes the interface class name corresponding to the target query statement interface, the method name of the target method corresponding to the target query statement interface, a preset query statement, and label information corresponding to a preset requirement.
In some specific embodiments, theinterface binding module 13 specifically includes:
the identifier assembling unit is used for assembling the interface class name and the method name in the extensible markup language file into a unique identifier;
a corresponding relation determining unit, configured to determine a corresponding relation between the target query statement interface and the xml file;
and the binding unit is used for binding the unique identifier with the method in the target query statement interface.
In some specific embodiments, thestatement generating module 14 specifically includes:
the parameter judging unit is used for judging whether the preset parameters are effective parameters according to preset parameter judging rules; the preset parameters are parameters set for a target query statement interface when the target query statement interface is written;
a query statement generating unit, configured to generate a target query statement based on the xml file and the preset parameter when the preset parameter is a valid parameter;
and the interface injection unit is used for injecting the packaged interface into a Spring container so as to inquire and call a corresponding interface through the corresponding relation between the extensible markup language file and the packaged interface.
In some specific embodiments, thestatement generating module 14 specifically includes:
the parameter filling unit is used for filling the vacant part of the target query statement in the extensible markup language file by using the preset parameters; the vacant part is a part reserved when the extensible markup language file is written.
Further, the embodiment of the application also provides electronic equipment. FIG. 4 is a block diagram illustrating anelectronic device 20 according to an exemplary embodiment, and the contents of the diagram should not be construed as limiting the scope of use of the present application in any way.
Fig. 4 is a schematic structural diagram of anelectronic device 20 according to an embodiment of the present disclosure. Theelectronic device 20 may specifically include: at least oneprocessor 21, at least onememory 22, a power supply 23, a communication interface 24, an input output interface 25, and acommunication bus 26. Thememory 22 is used for storing a computer program, and the computer program is loaded and executed by theprocessor 21 to implement the relevant steps in the statement generating method disclosed in any of the foregoing embodiments. In addition, theelectronic device 20 in the present embodiment may be specifically an electronic computer.
In this embodiment, the power supply 23 is configured to provide a working voltage for each hardware device on theelectronic device 20; the communication interface 24 can create a data transmission channel between theelectronic device 20 and an external device, and a communication protocol followed by the communication interface is any communication protocol applicable to the technical solution of the present application, and is not specifically limited herein; the input/output interface 25 is configured to obtain external input data or output data to the outside, and a specific interface type thereof may be selected according to specific application requirements, which is not specifically limited herein.
In addition, thestorage 22 is used as a carrier for resource storage, and may be a read-only memory, a random access memory, a magnetic disk or an optical disk, etc., and the resources stored thereon may include an operating system 221, a computer program 222, etc., and the storage manner may be a transient storage or a permanent storage.
The operating system 221 is used for managing and controlling each hardware device on theelectronic device 20 and the computer program 222, and may be Windows Server, Netware, Unix, Linux, or the like. The computer program 222 may further include a computer program that can be used to perform other specific tasks in addition to the computer program that can be used to perform the sentence generation method disclosed by any of the foregoing embodiments and executed by theelectronic device 20.
Further, an embodiment of the present application further discloses a storage medium, in which a computer program is stored, and when the computer program is loaded and executed by a processor, the steps of the statement generation method disclosed in any of the foregoing embodiments are implemented.
The embodiments are described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same or similar parts among the embodiments are referred to each other. The device disclosed by the embodiment corresponds to the method disclosed by the embodiment, so that the description is simple, and the relevant points can be referred to the method part for description.
Finally, it should also be noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
The statement generating method, apparatus, device and storage medium provided by the present invention are described in detail above, and the principle and implementation of the present invention are explained in this document by applying specific examples, and the description of the above examples is only used to help understanding the method and core idea of the present invention; meanwhile, for a person skilled in the art, according to the idea of the present invention, there may be variations in the specific embodiments and the application scope, and in summary, the content of the present specification should not be construed as a limitation to the present invention.