A method and apparatus for generating an operation code
By parsing user configuration files to generate abstract syntax trees and annotating target platform information, database operation code and data definition statements are automatically generated, solving the problem of low efficiency in existing technologies and achieving efficient code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-13
- Publication Date
- 2026-04-14
AI Technical Summary
In existing technologies, database operation code generation is inefficient, requires manual writing and time to familiarize oneself with database operation statements, and lacks versatility.
By parsing table and field references from user-input configuration files, a first abstract syntax tree is generated. Then, a second abstract syntax tree is obtained by parsing the configuration information annotations of the target platform. Finally, database operation code and data definition statements are generated, avoiding manual coding.
It enables the automatic generation of database operation code and data definition statements, improving code generation efficiency and reducing the time spent on manual coding.
Smart Images

Figure CN116107550B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and more specifically to a method and apparatus for generating operation codes. Background Technology
[0002] Database operation code is essential development code for all types of information systems. The work related to database operation code usually involves both the database side and the development side. If either side is modified, inconsistencies will occur between the two sides, which will lead to errors in the information system.
[0003] Currently, database operation code is still generated manually. However, because database operations are not universal, users need to spend time familiarizing themselves with various database operation statements before they can write database operation code, resulting in low code generation efficiency. Summary of the Invention
[0004] In view of this, embodiments of the present invention provide a method and apparatus for generating operation code to solve the problems of low code generation efficiency that exist in the method of manually writing code.
[0005] To achieve the above objectives, the embodiments of the present invention provide the following technical solutions:
[0006] The first aspect of this invention discloses a method for generating operation codes, the method comprising:
[0007] Parse the table references and field references from the configuration file input by the user;
[0008] A first abstract syntax tree is generated based on the table references and the field references;
[0009] The first abstract syntax tree is annotated by parsing the configuration information of the target platform to obtain the second abstract syntax tree, wherein the target platform is the database platform pre-specified by the user;
[0010] Database operation code and database data definition statements are generated based on the second abstract syntax tree.
[0011] Preferably, the database operation code and database data definition statements are generated based on the second abstract syntax tree, including:
[0012] Traverse each child node in the second abstract syntax tree and iterate to generate intermediate code;
[0013] The intermediate code is executed using a virtual machine to generate database operation code and database data definition statements.
[0014] Preferably, before parsing table references and field references from the user-input configuration file, the process also includes:
[0015] Verify whether the configuration file input by the user meets the preset rules;
[0016] If the configuration file meets the preset rules, the step of parsing the table references and field references from the configuration file entered by the user is executed;
[0017] If the configuration file does not meet the preset rules, the first error message is output.
[0018] Preferably, before generating the first abstract syntax tree based on the table reference and the field reference, the method further includes:
[0019] Determine whether the table reference and the field reference satisfy the preset syntax rules;
[0020] If the table reference and the field reference satisfy the preset syntax rules, the step of generating the first abstract syntax tree based on the table reference and the field reference is executed;
[0021] If the table reference and the field reference do not meet the preset syntax rules, output a second error message.
[0022] Preferred options also include:
[0023] When it is detected that the target platform does not support any of the data in the configuration file, a third error message is output to prompt the user to modify the configuration file.
[0024] A second aspect of this invention discloses an apparatus for generating operation codes, the apparatus comprising:
[0025] The parsing unit is used to parse table references and field references from the configuration file input by the user;
[0026] The first generation unit is used to generate a first abstract syntax tree based on the table reference and the field reference;
[0027] The annotation unit is used to annotate the first abstract syntax tree by parsing the configuration information of the target platform to obtain the second abstract syntax tree, wherein the target platform is the database platform pre-specified by the user;
[0028] The second generation unit is used to generate database operation code and database data definition statements based on the second abstract syntax tree.
[0029] Preferably, the second generation unit is specifically used to: traverse each child node in the second abstract syntax tree and iteratively generate intermediate code; and execute the intermediate code using a virtual machine to generate database operation code and database data definition statements.
[0030] Preferred options also include:
[0031] The verification unit is used to verify whether the configuration file input by the user meets the preset rules; if the configuration file meets the preset rules, the parsing unit is executed; if the configuration file does not meet the preset rules, the first error message is output.
[0032] Preferred options also include:
[0033] The judgment unit is used to determine whether the table reference and the field reference meet the preset syntax rules; if the table reference and the field reference meet the preset syntax rules, the first generation unit is executed; if the table reference and the field reference do not meet the preset syntax rules, a second error message is output.
[0034] Preferred options also include:
[0035] The processing unit is configured to output a third error message to prompt the user to modify the configuration file when it is detected that the target platform does not support data of any attribute in the configuration file.
[0036] The present invention provides a method and apparatus for generating operation code based on the above embodiments. The method comprises: parsing table references and field references from a user-input configuration file; generating a first abstract syntax tree (AST) based on the table references and field references; annotating the first AST with configuration information of the target platform to obtain a second AST; and generating database operation code and database data definition statements based on the second AST. In this solution, the user-input configuration file is parsed to obtain table references and field references. A first AST is generated based on the table references and field references. The first AST is annotated with configuration information of the target platform to obtain a second AST, and database operation code and database data definition statements are generated based on the second AST. This method automatically generates database operation code and database data definition statements without manual writing, improving code generation efficiency. Attached Figure Description
[0037] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0038] Figure 1 A flowchart illustrating a method for generating operation codes according to an embodiment of the present invention;
[0039] Figure 2This is a schematic diagram of the layered structure of a specified processing system provided in an embodiment of the present invention;
[0040] Figure 3 This is a schematic diagram of data processing in the configuration layer provided in an embodiment of the present invention;
[0041] Figure 4 This is a schematic diagram of data processing in the parsing layer provided in an embodiment of the present invention;
[0042] Figure 5 This is a schematic diagram of a portion of the first abstract syntax tree provided in an embodiment of the present invention;
[0043] Figure 6 This is a structural block diagram of an operation code generation device provided in an embodiment of the present invention. Detailed Implementation
[0044] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0045] In this application, the terms "comprising," "including," or any other variations thereof are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0046] As the background technology shows, database operation code is still generated manually. However, because database operations are not universal, users need to spend time familiarizing themselves with various database operation statements before they can write database operation code, resulting in low code generation efficiency.
[0047] Therefore, embodiments of the present invention provide a method and apparatus for generating operation code, which parses user-input configuration files to obtain table references and field references. A first abstract syntax tree is generated based on the table references and field references. The first abstract syntax tree is then annotated by parsing the configuration information of the target platform to obtain a second abstract syntax tree. Database operation code and database data definition statements are then generated based on the second abstract syntax tree. This automatic generation of database operation code and database data definition statements eliminates the need for manual writing, thereby improving code generation efficiency.
[0048] See Figure 1 The diagram shows a flowchart of an operation code generation method provided by an embodiment of the present invention. The generation method includes:
[0049] Step S101: Parse the table references and field references from the configuration file input by the user.
[0050] In some embodiments, a configuration file input by the user is obtained before performing step S101; the configuration file contains at least configuration data input by the user (such as fields for database type, target language, database table information, etc.).
[0051] After obtaining the configuration file, check whether the configuration file entered by the user meets the preset rules (equivalent to checking whether the user's input is compliant); for example: check whether the syntax of the configuration data meets the preset rules, check whether the definitions of different rows and columns meet the preset rules, and check whether the type of the configuration file meets the preset rules.
[0052] If the configuration file meets the preset rules, proceed to step S101; if the configuration file does not meet the preset rules, output the first error message to prompt the user that the configuration file is non-compliant.
[0053] It should be noted that the configuration file supports various formats, such as Excel, YAML, or JSON. Field definitions in different formats maintain a consistent style, allowing users to choose the appropriate format to define their configuration data based on their specific needs.
[0054] In the specific implementation step S101, if the configuration file input by the user meets the preset rules, table references and field references are parsed from the configuration file input by the user. The table references and field references are equivalent to the user's configuration items.
[0055] In some embodiments, a unified parsing interface is abstracted; this parsing interface is used to parse different types of configuration files (i.e., the different formats of configuration files mentioned above), wherein parsing different types of configuration files can yield parsing output results with the same data structure; or, different types of configuration files are parsed using the same parsing interface to form parsing output results with a unified data structure.
[0056] Step S102: Generate the first abstract syntax tree based on table references and field references.
[0057] Since different database platforms have different requirements for field definitions, after a user specifies a target platform, it is necessary to verify the validity of table and field references. The target platform is the database platform specified by the user beforehand. In some embodiments, before executing step S102, it is determined whether the table and field references parsed from the configuration file meet preset syntax rules (these syntax rules are related to the target platform). If the table and field references meet the preset syntax rules, step S102 is executed. If the table and field references do not meet the preset syntax rules, a second error message is output to prompt the user that the entered table and field references do not meet the syntax rules.
[0058] For example: Suppose field A should be a number, but the user inputs field A as a string; in this case, output a second error message to indicate that the table reference and field reference entered by the user do not meet the syntax rules.
[0059] For example, suppose the Oracle database platform has a CLOB data type, while the MySQL database platform does not. When the user specifies the target platform as the MySQL database platform, if the user's configuration data includes CLOB fields, a second error message will be output to indicate to the user that the table reference and field reference entered do not meet the syntax rules.
[0060] In the specific implementation step S102, if the table reference and field reference satisfy the preset syntax rules, the first abstract syntax tree (AST) is generated based on the table reference and the field reference.
[0061] Understandably, parsing tools can be used to generate the first abstract syntax tree; specifically, a recursive downward parsing method can be used to generate the first abstract syntax tree.
[0062] Step S103: Annotate the first abstract syntax tree by parsing the configuration information of the target platform to obtain the second abstract syntax tree.
[0063] In some embodiments, after the user specifies the target platform, it checks whether the target platform supports the data for each attribute in the configuration file. When it is detected that the target platform does not support the data for any attribute in the configuration file, a third error message is output to prompt the user to modify the configuration file.
[0064] For example: If the target platform is a MySQL database platform, and the user's configuration file contains a CLOB field, since the MySQL database platform does not support CLOB fields, a third error message will be output to prompt the user to modify the configuration file.
[0065] In the specific implementation step S103, the first abstract syntax tree is annotated by parsing the configuration information of the target platform to supplement the target platform-related content in the first abstract syntax tree (actually, the attributes of the nodes on the first abstract syntax tree are modified), thereby obtaining the second abstract syntax tree.
[0066] Step S104: Generate database operation code and database data definition statements based on the second abstract syntax tree.
[0067] In the specific implementation step S104, each child node in the second abstract syntax tree is traversed, and intermediate code (IR) is generated iteratively. It should be noted that the intermediate code can be used to shield the differences between different database platforms, thereby improving the system processing efficiency.
[0068] After generating intermediate code, the virtual machine executes the intermediate code to generate database operation code (i.e., language code) and database data definition statements.
[0069] It should be noted that database operation code can be Java / Python database operation code. Database data definition statements (database DDL statements) can be database initialization SQL statements, where DDL (Data Definition Language) statements are data definition statements. Database operation code can be executed by the database system, and database data definition statements can be used for system interface calls (or run directly in the target database corresponding to the target platform).
[0070] As can be seen from the above steps S101 to S104, the progress of the entire process of generating database operation code and database data definition statement can be tracked. The entire process of generating database operation code and database data definition statement can be divided into different stages. At the corresponding stage, it is checked whether the configuration file meets the preset rules, whether the table reference and field reference meet the preset syntax rules, and whether the target platform supports the data of each attribute in the configuration file.
[0071] When an error occurs at any stage of the verification process, the corresponding error message (first error message, second error message, or third error message) is output to display detailed error information to the user, enabling the user to quickly locate and fix the error. When an error occurs at any stage of the verification process, intermediate files should also be deleted, the system state reset, and log content printed according to the user-configured log level.
[0072] In some embodiments, an AST file can be obtained from another source, and a second abstract tree can be extracted from that AST file. This second abstract tree is then used to generate intermediate code, which is ultimately used to generate database operation code and database data definition statements. Alternatively, an IR file can be obtained from another source, and intermediate code can be read from that IR file. The intermediate code is then used to generate the database operation code and database data definition statements. In this implementation, the AST and IR files obtained from other sources must meet preset file specifications. Before applying the AST or IR file to generate database operation code and database data definition statements, the applied AST or IR file needs to be checked according to the preset file specifications. The entire process is analogous to a pipeline design pattern, where each step consists of a small tool, and all these tools are chained together to form the steps described above for generating database operation code and database data definition statements.
[0073] In this embodiment of the invention, the user-input configuration file is parsed to obtain table references and field references. A first abstract syntax tree is generated based on the table references and field references. The first abstract syntax tree is annotated by parsing the configuration information of the target platform to obtain a second abstract syntax tree. Database operation code and database data definition statements are generated based on the second abstract syntax tree. This automatic generation of database operation code and database data definition statements eliminates the need for manual writing, improving code generation efficiency.
[0074] In some embodiments, the operation code generation method provided in the above embodiments of the present invention can be applied to a specified processing system; see also Figure 2 This diagram illustrates the layered structure of a specified processing system provided in an embodiment of the present invention. The specified processing system includes a configuration layer 100, a parsing layer 200, a platform layer 300, and a code generation layer 400. Each layer of the specified processing system depends on the services provided by the layers below. The layered structure can be upgraded without affecting the specified processing system. Each layer can correspond to multiple implementation methods while ensuring the same input / output.
[0075] The configuration layer 100, parsing layer 200, platform layer 300, and code generation layer 400 are independent of each other. Each layer can be assigned to the same thread, or, depending on actual needs, to different threads or nodes, thereby improving the data processing efficiency of each layer. The execution principles of the configuration layer 100, parsing layer 200, platform layer 300, and code generation layer 400 are explained below.
[0076] Configuration layer 100 is used to verify whether the configuration file meets the preset rules; if the configuration file meets the preset rules, it parses the table references and field references from the user-input configuration file. Configuration layer 100 then outputs the table references and field references to parsing layer 200.
[0077] Parsing layer 200 is used to determine whether the table references and field references parsed from the configuration file meet the preset syntax rules. If the table references and field references meet the preset syntax rules, a first abstract syntax tree is generated based on the table references and field references. Parsing layer 200 outputs the first abstract syntax tree to platform layer 300.
[0078] Platform layer 300 is used to annotate the first abstract syntax tree by parsing the configuration information of the target platform to obtain the second abstract syntax tree. Platform layer 300 outputs the second abstract syntax tree to code generation layer 400. In addition, platform layer 300 is also used to output a third error message to prompt the user to modify the configuration file when it detects that the target platform does not support data of any attribute in the configuration file.
[0079] Code generation layer 400 is used to generate final code based on the second abstract syntax tree. This final code contains database operation code and database data definition statements.
[0080] It should be noted that the calling relationships between configuration layer 100, parsing layer 200, platform layer 300, and code generation layer 400 determine the high coupling characteristics between data, requiring each layer to process data serially. When encountering complex configuration files, the running time of each layer will increase accordingly; each layer will check the validity of the corresponding data, preventing a large number of errors from occurring all at once in code generation layer 400.
[0081] The aforementioned designated processing system is available for user operation. Users can configure different configuration items in the configuration file within this system. After configuration, the system is run to process the configuration file, thereby generating the configuration content in the corresponding directory. This directory contains database operation code, database data definition statements, and directory structure.
[0082] If the specified processing system outputs error messages (first error message, second error message, and / or third error message) during the processing of the configuration file, the specified processing system will print detailed information about the line with the error and the error type in the log file.
[0083] Users can debug the specified processing system. During the debugging process, the specified processing system can also print debugging information for each layer, allowing users to view the process of generating database operation code and database data definition statements.
[0084] The configuration file read by the configuration layer 100 supports formats such as Excel, YAML, or JSON. The field definition style remains consistent across different formats, and users can choose different formats to define the corresponding configuration data according to their actual needs. The configuration layer 100 can perform lexical validation on the configuration items in the configuration file.
[0085] In determining whether the table and field references parsed from the configuration file meet the preset syntax rules, the parsing layer 200 mainly performs the following two types of checks.
[0086] The first type of check performs lexical validation on table references and field references (equivalent to user input); for example, it checks whether the user-input string is valid and whether the data type of the user-input is a preset type. If the lexical validation fails, a second error message is output.
[0087] The second type of check: Based on the lexical validation, determine whether the content configured by the user in the configuration file conforms to the syntax rules. If it does not conform to the syntax rules, output a second error message.
[0088] After passing the above two types of checks, the parsing layer 200 generates the first abstract syntax tree based on table references and field references.
[0089] It should be noted that each grammar rule corresponds to an abstract syntax tree (equivalent to a sub-abstract syntax tree), and the entire specified processing system is equivalent to a whole grammar rule. Thus, the sub-abstract syntax trees corresponding to each grammar rule can form the first abstract syntax tree, and then the second abstract syntax tree can be generated based on the first abstract syntax tree.
[0090] It should be noted that when defining the syntax rules, the processing system also specifies the format of the intermediate code; therefore, intermediate code can be generated using the syntax rules. When generating intermediate code using the second abstract syntax tree (BAST), each child node in the BAST is traversed, iteratively generating intermediate code, ultimately forming the configuration file conversion.
[0091] The code generation layer 400 generates database operation code and database data definition statements that meet user requirements based on the intermediate code, combined with the database type and programming language of the target platform.
[0092] It is worth noting that in practical applications, users pre-configure configuration files (configuring database table information, field types, length information, etc.), and then the specified processing system generates database operation code and database data definition statements that meet the user's needs.
[0093] The above is a description of the configuration layer 100, parsing layer 200, platform layer 300, and code generation layer 400.
[0094] pass Figure 3 As shown in the schematic diagram of the configuration layer, the configuration layer 100 processes data for different types of configuration files ( Figure 3The configuration file types 1 to 3 in the configuration file adopt different parsing strategies, and finally form a unified internally transferable and recognizable lexical element. The lexical element is provided to the parsing layer 200 for recognition and parsing of syntax rules.
[0095] It's understandable that different types of configuration files have different file formats, but the basic configuration items are the same. For configuration file parsing, this solution uses a unified parsing interface, with different types of configuration files matching the appropriate parsing method.
[0096] It is worth noting that, in order to avoid the inconvenience of managing multiple configuration files of different types due to nesting, this solution allows only one type of configuration file to be configured in an instance, but the configuration file can reference / contain other configuration files of the same type.
[0097] Because of nested usage within configuration files, configuration layer 100 linearizes the references between files, forming linear dependencies, and progressively parses the corresponding configuration files to output the final lexical elements. The lexical elements output by configuration layer 100 serve as the basic units for syntax rules, which are then provided to parsing layer 200 for parsing. Since different types of databases have different lexical definition rules, configuration layer 100 needs to perform basic validation on the configuration content of the user-input configuration file; when the configuration content fails validation, configuration layer 100 outputs the first error message to inform the user that the configuration file is non-compliant.
[0098] The verification of configuration layer 100 mainly includes the following three types.
[0099] The first type: Configuration file compliance validation. Configuration file compliance validation is mainly used to check the validity of the configuration file. For example, if the specified file type is JSON but the user provides XML configuration data, the configuration layer 100 will output the first error message to prompt the user to change the file type.
[0100] The second type: configuration item lexical validity check. Configuration item lexical validity check is used to check the validity of the file content. Each configuration item cannot contain special characters. When configuration layer 100 forms a lexical element, configuration item lexical validity check is performed first.
[0101] The third type: Validity of configuration items for specific database types. Once the target platform is determined, the configuration layer 100 performs basic checks based on the database type corresponding to that target platform.
[0102] Since validation is performed based on the database type corresponding to the target platform, configuration layer 100 should perform different types of checks according to the naming conventions of different databases to avoid providing non-standard data to the next layer. In actual processing, validation should not exit immediately upon detecting an error and specifying a handling system; instead, it should complete subsequent checks. The corresponding error message should only be output after all checks are completed, allowing users to correct all errors at once.
[0103] To improve convenience, error messages from the parsing layer 200 can be output to the terminal for printing or to a file; the output lexical elements are also processed in a similar way.
[0104] like Figure 4 As shown in the provided data processing diagram of the parsing layer, the parsing layer 200 performs normalization processing on different rules based on the lexical elements provided by the configuration layer 100 and the syntax rules of each configuration item, thereby forming the first abstract syntax tree. Each syntax rule is an abstract syntax tree (sub-abstract syntax tree), and the entire configuration file forms the first abstract syntax tree (the final abstract syntax tree). The first abstract syntax tree is used to determine the second abstract syntax tree, which generates intermediate code by recursively calling each sub-abstract syntax tree.
[0105] It's important to note that syntax rules are data organization rules that mask the differences between different databases. The output First Abstract Syntax Tree (BAST) is a structured representation of these syntax rules, independent of the target platform's intermediate data format. The BAST is similar to a syntax tree in compilation technology, but it lacks structures like loops and branches, only representing database table structures to facilitate the generation of subsequent intermediate code. The BAST is related to the configuration file structure; the configuration items in the configuration file are validated using the syntax rules to generate the BAST.
[0106] The parsing layer 200 constructs the first abstract syntax tree by combining the lexical elements extracted by the configuration layer 100 with the syntax rules. Some of the syntax rules are shown in Table 1 below (Table 1 is for illustrative purposes only).
[0107] Table 1:
[0108]
[0109] The parsing layer parses syntax rules to form sub-abstract syntax trees, which are then aggregated to form the first abstract syntax tree. When a configuration item in the configuration file cannot match any syntax rule, the specified processing system will output a second error message to inform the user of the configuration item's error.
[0110] After the configuration file is parsed by parsing layer 200, the first abstract syntax tree generated is shown in the following figure. Figure 5The root node of the first abstract syntax tree is a virtual node, and the remaining child nodes correspond to the various configuration blocks in the configuration file. The parsing layer 200 uses the syntax rules to parse the configuration items in the configuration file one by one through the recursive descent parsing method or the LL(*) parsing method, and finally forms the first abstract syntax tree.
[0111] When adding configuration items to the configuration file, corresponding syntax rules and code generation methods must be added; otherwise, the new configuration items will not take effect, and the parsing layer 200 will output error messages because the new configuration items do not conform to the syntax rules. The syntax rules are not limited to a specific platform; all data types are abstracted and unified. For example, the string column type will generate different defined types on different platforms.
[0112] In summary, the parsing layer 200, based on syntax rules and the input from the configuration layer 100, abstracts the configuration file into a first abstract syntax tree. The configuration file can be an XML file, a JSON file, or an Excel spreadsheet, etc. After being parsed by the parsing layer 200, the first abstract syntax tree is generated, thus masking the differences between different formats and configuration items, thereby forming the internal data structure.
[0113] Since the first abstract syntax tree is platform-independent, the platform layer 300 modifies the first abstract syntax tree according to the differences between different platforms to obtain the second abstract syntax tree. The second abstract syntax tree (the modified first abstract syntax tree) can generate database operation code and database data definition statements (database DDL statements) according to the target platform.
[0114] It should be noted that the process of revising the first abstract syntax tree mainly involves mapping general-purpose to specific configuration items. This mapping includes two parts: database platform mapping and language type mapping. Database platform mapping prepares the database DDL statements, while language type mapping prepares the database operation code.
[0115] After the platform layer 300 corrects the first abstract syntax tree to obtain the second abstract syntax tree, the code generation layer 400 traverses the second abstract syntax tree to generate code; each configuration item needs to generate two types of code simultaneously (database operation code and database data definition statements). To ensure the accuracy of the generated code, each type of code needs to be standardized in accordance with different language features.
[0116] During the code generation process, the code generation layer 400 needs to pre-set certain rules to ensure consistency between the generated code and the configuration items in the configuration file. This primarily ensures consistency in database table names, class names, and member definitions. Table names can use the format "xx_xx", and class names use camelCase, ultimately conforming to naming conventions in program design to facilitate quick understanding of mapping relationships for users.
[0117] During code generation, if the language uses certain packages, the code generation process must import the corresponding paths of those packages and add comments to some class members and methods. The comments are derived from configuration items and default information generated by the specified processing system. Furthermore, the code generation layer 400 provides complete unit test cases. After configuring the database information, users can directly execute the unit test cases, improving user efficiency and code familiarity.
[0118] Corresponding to the method for generating operation codes provided in the above embodiments of the present invention, see also... Figure 6 The present invention also provides a structural block diagram of an operation code generation device, which includes: a parsing unit 601, a first generation unit 602, an annotation unit 603, and a second generation unit 604;
[0119] Parsing unit 601 is used to parse table references and field references from the configuration file input by the user.
[0120] The first generation unit 602 is used to generate the first abstract syntax tree based on table references and field references.
[0121] The annotation unit 603 is used to annotate the first abstract syntax tree by parsing the configuration information of the target platform to obtain the second abstract syntax tree. The target platform is a database platform pre-specified by the user.
[0122] The second generation unit 604 is used to generate database operation code and database data definition statements based on the second abstract syntax tree.
[0123] In a specific implementation, the second generation unit 604 is specifically used to: traverse each child node in the second abstract syntax tree and iteratively generate intermediate code; and use a virtual machine to execute the intermediate code to generate database operation code and database data definition statements.
[0124] In this embodiment of the invention, the user-input configuration file is parsed to obtain table references and field references. A first abstract syntax tree is generated based on the table references and field references. The first abstract syntax tree is annotated by parsing the configuration information of the target platform to obtain a second abstract syntax tree. Database operation code and database data definition statements are generated based on the second abstract syntax tree. This automatic generation of database operation code and database data definition statements eliminates the need for manual writing, improving code generation efficiency.
[0125] Preferred, combined Figure 6 The generating apparatus, as shown, also includes:
[0126] The verification unit is used to verify whether the configuration file input by the user meets the preset rules; if the configuration file meets the preset rules, the parsing unit 601 is executed; if the configuration file does not meet the preset rules, the first error message is output.
[0127] Preferred, combined Figure 6 The generating apparatus, as shown, also includes:
[0128] The judgment unit is used to determine whether the table reference and field reference meet the preset syntax rules; if the table reference and field reference meet the preset syntax rules, the first generation unit 602 is executed; if the table reference and field reference do not meet the preset syntax rules, the second error message is output.
[0129] Preferred, combined Figure 6 The generating apparatus, as shown, also includes:
[0130] The processing unit is used to output a third error message to prompt the user to modify the configuration file when it detects that the target platform does not support data of any attribute in the configuration file.
[0131] In summary, this invention provides a method and apparatus for generating operation code. It parses user-input configuration files to obtain table references and field references. A first abstract syntax tree (AST) is generated based on these table and field references. A second abstract syntax tree is obtained by annotating the first AST by parsing the target platform's configuration information. Database operation code and database data definition statements are then generated based on the second AST. This automatic generation of database operation code and database data definition statements eliminates the need for manual writing, thus improving code generation efficiency.
[0132] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, for system or system embodiments, since they are basically similar to method embodiments, the description is relatively simple, and relevant parts can be referred to the descriptions in the method embodiments. The systems and system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0133] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0134] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for generating operation codes, characterized in that, The method includes: Verify whether the configuration file entered by the user meets the preset rules; If the configuration file meets the preset rules, the step of parsing the table references and field references from the configuration file entered by the user is executed; If the configuration file does not meet the preset rules, a first error message is output to indicate that the user configuration file is non-compliant. Parse the table references and field references from the configuration file input by the user; Determine whether the table reference and the field reference satisfy the preset syntax rules; If the table reference and the field reference satisfy the preset syntax rules, the step of generating the first abstract syntax tree based on the table reference and the field reference is executed; If the table reference and the field reference do not meet the preset syntax rules, output a second error message to prompt the user that the table reference and the field reference entered do not meet the syntax rules; A first abstract syntax tree is generated based on the table references and the field references; The first abstract syntax tree is annotated by parsing the configuration information of the target platform to obtain the second abstract syntax tree, wherein the target platform is the database platform pre-specified by the user; Database operation code and database data definition statements are generated based on the second abstract syntax tree.
2. The method according to claim 1, characterized in that, Based on the second abstract syntax tree, database operation code and database data definition statements are generated, including: Traverse each child node in the second abstract syntax tree and iterate to generate intermediate code; The intermediate code is executed using a virtual machine to generate database operation code and database data definition statements.
3. The method according to claim 1, characterized in that, Also includes: When it is detected that the target platform does not support any of the data in the configuration file, a third error message is output to prompt the user to modify the configuration file.
4. An apparatus for generating operation codes, characterized in that, The device includes: The verification unit is used to verify whether the configuration file input by the user meets the preset rules; if the configuration file meets the preset rules, the parsing unit is executed; if the configuration file does not meet the preset rules, the first error message is output to prompt the user that the configuration file is non-compliant. The parsing unit is used to parse table references and field references from the configuration file input by the user; The judgment unit is used to determine whether the table reference and the field reference meet the preset syntax rules; if the table reference and the field reference meet the preset syntax rules, the first generation unit is executed; if the table reference and the field reference do not meet the preset syntax rules, a second error message is output to prompt the user that the table reference and the field reference entered do not meet the syntax rules. The first generation unit is used to generate a first abstract syntax tree based on the table reference and the field reference; The annotation unit is used to annotate the first abstract syntax tree by parsing the configuration information of the target platform to obtain the second abstract syntax tree, wherein the target platform is the database platform pre-specified by the user; The second generation unit is used to generate database operation code and database data definition statements based on the second abstract syntax tree.
5. The apparatus according to claim 4, characterized in that, The second generation unit is specifically used to: traverse each child node in the second abstract syntax tree and generate intermediate code in turn; and execute the intermediate code using a virtual machine to generate database operation code and database data definition statements.
6. The apparatus according to claim 4, characterized in that, Also includes: The processing unit is configured to output a third error message to prompt the user to modify the configuration file when it is detected that the target platform does not support data of any attribute in the configuration file.
Citation Information
Patent Citations
Database operation code generation method and device
CN106095792A
Data blood relationship processing method and device
CN111538744A