Method and device for automatically generating script code, electronic equipment and medium
By obtaining the mapping relationship between source files and user-defined, and using preset algorithms to generate high-quality SQL scripts, the accuracy problem of automated script generation in the existing technology is solved, and efficient and accurate SQL scripts are achieved Generate and cloud platform integration.
Patent Information
- Application Number
- CN202411446355.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-16
- Publication Date
- 2025-02-11
AI Technical Summary
Existing automated script generation methods are difficult to accurately understand business needs when dealing with complex business logic, resulting in the generated SQL scripts that may have syntax or logical errors and cannot fully meet business needs.
By obtaining the mapping relationship between the source file containing the pending data and the source file input from the user to the target file, the corresponding SQL script code is generated using the preset algorithm. This method includes data cleaning, type conversion, complex data processing and multi-table association processing, and supports error detection and correction.
The generated SQL scripts are of high quality, can accurately meet business needs, reduce the time for developers to write code manually, improve the readability and maintainability of the code, and support the integration and automated execution of the cloud platform.
Smart Images

Figure CN119415102A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of software development, and in particular to a method, device, electronic device and medium for automatically generating script codes. Background Art
[0002] With the continuous advancement of information technology, the construction and application of data middle platform has become particularly critical. It provides decision support for enterprises through centralized management and in-depth analysis of data. As the core tool of data middle platform, SQL script is responsible for realizing key functions such as data extraction, transformation, loading (ETL), report generation and data mining. It is the basis for ensuring the effective flow and accurate display of data. However, writing SQL script is not easy. It involves technical challenges such as defining field mapping and processing logic, processing the relationship between tables, and applying complex filtering conditions.
[0003] At present, although automated script generation methods improve efficiency by using large machine learning models to analyze metadata and historical script cases, these methods are still limited in practical applications by insufficient understanding of complex business logic, resulting in the possibility of syntax or logic errors in the generated scripts, which cannot fully meet business needs. For example, in complex business scenarios, automated script generation models may not accurately identify the relationship between multiple data sources, or cannot correctly handle data conversion and filtering logic, resulting in erroneous results. Summary of the invention
[0004] The present disclosure provides a method, device, electronic device and medium for automatically generating script codes, aiming to automatically analyze the structure and business rules of a data source, generate high-quality SQL scripts, and improve development efficiency and the automation level of data management.
[0005] A first aspect of the present disclosure provides a method for automatically generating script code, comprising:
[0006] Obtaining source files containing data to be processed;
[0007] Receive a mapping relationship between the source file and the target file input by a user, wherein the mapping relationship is used to define a conversion rule and path for the source file data;
[0008] Based on the mapping relationship, the corresponding SQL script code is generated through a preset algorithm.
[0009] In an embodiment of the first aspect, when the source file is one, the mapping relationship includes:
[0010] one or more source fields in the source document;
[0011] one or more target fields in the target file;
[0012] The data type of the source field;
[0013] The data type of the target field;
[0014] Data processing logic that converts the source field to the target field.
[0015] In an embodiment of the first aspect, the data processing logic includes a combination of one or more of the following operations:
[0016] A data cleaning operation is used to pre-process the source fields to eliminate redundant, erroneous or inconsistent data;
[0017] A data type conversion operation, used to perform data type conversion when the data type of the source field is inconsistent with the data type of the target field;
[0018] Data calculation operation, used to perform mathematical operation or logical deduction on the source field to generate the target data;
[0019] Complex data processing operations are used to perform user-defined complex calculations and logical processing when there are multiple source fields to meet specific business logic requirements.
[0020] In an embodiment of the first aspect, when there are multiple source files, the mapping relationship further includes:
[0021] The association relationship between different source files is used to define the data logical connection between each source file, and the data logical connection is used to indicate how to associate the data in different source files based on common fields or logical conditions.
[0022] In an embodiment of the first aspect, when the data of the source file needs to be screened, the mapping relationship further includes:
[0023] The filter condition is used to filter out data that conforms to the business logic in the source field.
[0024] In an embodiment of the first aspect, after the SQL script code is generated, it further includes:
[0025] Parsing the generated SQL script code into an abstract syntax tree for syntax and structure analysis;
[0026] If the analysis finds that the SQL script code has errors, the errors are identified and corrected;
[0027] Based on the error correction results, regenerate the corrected SQL script code.
[0028] In an embodiment of the first aspect, after the SQL script code is generated, it further includes:
[0029] Integrating the generated SQL script code into the cloud platform through a predetermined interface;
[0030] Receive task scheduling instructions input by users;
[0031] In response to the task scheduling instruction, a task is created on the cloud platform and the SQL script code is scheduled to be executed.
[0032] A second aspect of the present disclosure discloses a device for automatically generating script code, comprising:
[0033] The acquisition module is used to obtain the source file containing the business logic;
[0034] The template engine is used to receive the mapping relationship between the source file and the target file input by the user;
[0035] The code generation module is used to generate corresponding SQL script codes through a preset algorithm based on the mapping relationship.
[0036] The third aspect of the present disclosure discloses an electronic device, comprising: a processor and a memory; wherein the memory is used to store a computer program; the processor is used to execute the computer program stored in the memory, so that the electronic device executes the method for automatically generating script code as described in any one of the first aspects.
[0037] A fourth aspect of the present disclosure discloses a computer-readable storage medium having a computer program stored thereon, which, when executed by an electronic device, implements the method for automatically generating script codes as described in any one of the first aspects.
[0038] As described above, the present disclosure provides a method, device, electronic device and medium for automatically generating script code, which at least include the following technical effects:
[0039] (1) Generate the corresponding SQL script code through preset algorithms, reducing the time developers spend manually writing SQL scripts, especially when dealing with repetitive tasks that require a large amount of similar code. Through automated processes, standardized code can be quickly generated, significantly saving human and material resources, allowing development teams to focus on higher-level innovation and optimization work.
[0040] (2) The automatically generated SQL scripts follow a consistent structure and style, which helps improve the readability and maintainability of the code. Unified code standards reduce the complexity of maintenance work and reduce errors caused by inconsistent styles or chaotic structures, thereby improving the stability and development efficiency of the entire project.
[0041] (3) It supports seamless integration with third-party cloud platforms, allowing the generated SQL scripts to be directly deployed to the cloud environment. This integration simplifies the code deployment and management process. At the same time, by using the scheduling function of the cloud platform, regular or triggered data tasks can be automatically executed, further improving the efficiency and response speed of data processing.
[0042] (4) The error detection and correction mechanism in the automated script generation process ensures the accuracy and reliability of the code. Through abstract syntax tree (AST) analysis and error correction, potential syntax errors or logic problems can be identified and resolved in advance, thereby reducing the risk of runtime errors and system failures and improving the accuracy of data processing results. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 Shown is a schematic diagram of a scene of an electronic device disclosed in one embodiment.
[0044] Figure 2 A flowchart of a method for automatically generating script codes in an embodiment of the present disclosure is shown.
[0045] Figure 3 A schematic diagram showing a template engine in one embodiment of the present disclosure.
[0046] Figure 4 A schematic diagram of a flow chart showing whether there is an error in an SQL script code in one embodiment of the present disclosure is shown.
[0047] Figure 5 A schematic diagram showing the flow of subsequent use of SQL script codes in an embodiment of the present disclosure.
[0048] Figure 6 A schematic diagram showing a method for automatically generating script codes in an embodiment of the present disclosure.
[0049] Figure 7 A schematic diagram of a device module for automatically generating script codes in an embodiment of the present disclosure is shown.
[0050] Figure 8 A schematic diagram of the circuit structure of an electronic device in an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0051] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0052] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention, and thus the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0053] In the field of automatic SQL script generation, despite the continuous advancement of technology, there are still several key challenges. First, the processing of field mapping and processing logic is a core issue in automatic script generation. This involves determining which specific fields of which data tables the generated fields should be extracted from, and what form of processing is required for these fields, such as data conversion, calculation or formatting, to ensure that they meet the requirements of the target data structure. Secondly, the relationship between tables is also a difficulty in the automation process. The script generator must be able to understand and handle the complex associations between multiple data tables, including identifying primary keys, foreign keys, and the types of connections between them, which is crucial to maintaining the integrity and accuracy of the data. Third, each data table usually has specific business filtering logic, and these filtering conditions are the basis for accurate data retrieval and analysis. The automated script generator needs to be able to accurately understand and apply these filtering conditions to ensure that the generated scripts can meet specific business needs.
[0054] Currently, mainstream automated script generation methods rely on large machine learning models that generate scripts by analyzing auxiliary information such as metadata information and historical script cases. However, this approach has limitations in practical applications, especially when dealing with complex business logic. Large models may find it difficult to accurately understand business requirements, resulting in syntax errors or logical deviations in the generated scripts. These challenges become more pronounced in complex business contexts. For example, a scenario involving multiple data sources and complex business rules may require scripts to be able to perform multi-table association queries, apply specific filtering conditions, and perform complex processing on data. If the automated script generator cannot accurately understand these requirements, the generated script may not execute correctly or fail to produce the expected business insights.
[0055] To this end, the present disclosure aims to provide a technical solution for generating high-quality SQL scripts that meet business requirements.
[0056] The following embodiments of the present invention provide a method for recognizing multilingual text, which can be applied to Figure 1The electronic device shown. The electronic device described in the present invention may include a mobile phone 11 with a wireless charging function, a tablet computer 12, a laptop computer 13, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc. The embodiment of the present invention does not impose any restrictions on the specific type of the electronic device.
[0057] For example, the electronic device may be a station (STAION, ST) in a WLAN with a wireless charging function, a cellular phone, a cordless phone, a Session Initiation Protocol (SIP) phone, a Wireless Local Loop (WLL) station, a Personal Digital Assistant (PDA) device, a handheld device with a wireless charging function, a computing device or other processing device, a computer, a laptop computer, a handheld communication device, a handheld computing device, and / or other devices for communicating on a wireless system and a next generation communication system, such as a mobile terminal in a 5G network, a mobile terminal in a future evolved Public Land Mobile Network (PLMN), or a mobile terminal in a future evolved Non-terrestrial Network (NTN).
[0058] For example, the electronic device can communicate with the network and other devices through wireless communication. The above wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobilecommunication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Messaging Service (SMS), BT, GNSS, WLAN, NFC, FM, and / or IR technology. The GNSS can include Global Positioning System (GPS), Global Navigation Satellite System (GLONASS), BeiDou navigation Satellite System (BDS), Quasi-Zenith Satellite System (QZSS) and / or Satellite Based Augmentation Systems (SBAS).
[0059] The technical solutions in the embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0060] like Figure 2 As shown, a flow chart of a method for automatically generating script code in an embodiment of the first aspect of the present disclosure is shown, including steps S10-S30, wherein:
[0061] Step S10: Obtain a source file containing data to be processed.
[0062] Specifically, the initial stage of automatically generating SQL scripts involves accurately locating and extracting data files containing key business information from the enterprise's data storage system. This process involves a variety of data sources and formats, such as:
[0063] 1. Database tables and views: Extract data tables and views directly from relational databases such as MySQL, PostgreSQL or enterprise-level Oracle, SQL Server. These tables and views usually have organized structured data for daily business operations.
[0064] 2. Temporary query results: During the data analysis process, it is often necessary to execute complex SQL queries based on specific requirements and use the results as source data for further processing.
[0065] 3. Data files in the file system: including files in CSV, JSON, XML, Excel and other formats stored in servers or cloud storage. For example, CSV files are often used to store flat table data, while Excel spreadsheets contain more complex data organization and formula calculations.
[0066] When processing these source files, it is necessary to carefully identify the field names, data types, number of records, and the relationship between fields in each file. For example, an Excel file of sales data may contain fields such as "Customer ID", "Product Name", "Sales Date", and "Amount", and the data types of these fields are integers, strings, dates, and floating point numbers, respectively. The automated system needs to be able to identify these fields and understand the relationship between them, such as "Customer ID" may be associated with the primary key of the customer information table in the database.
[0067] Step S20: receiving the mapping relationship between the source file and the target file input by the user.
[0068] Specifically, in the process of automatically generating SQL scripts, the template engine receives user input. Figure 3 As shown, the template engine provides a user-friendly interface that enables users to easily define and refine the data mapping relationship from source files to target files.
[0069] Through this interface, users can specify the corresponding field in the target file for each selected field, ensuring that data can be accurately migrated from the source to the target location. This graphical interface or form-driven approach significantly reduces the complexity of manual code writing and reduces the possibility of errors.
[0070] More importantly, the template engine also allows users to preview and test the validity of the mapping relationship before generating the final script, which not only improves the accuracy of data processing, but also ensures the reliability of the data migration and processing process.
[0071] In some embodiments, when there is only one source file, the mapping relationship includes: one or more source fields in the source file; one or more target fields in the target file; the data type of the source field; the data type of the target field; and data processing logic for converting the source field to the target field.
[0072] Specifically, mapping relationships first involve identifying and defining one or more fields in the source file that contain the original data that needs to be processed or migrated. The identification of source fields includes clarifying the names of the fields, their locations in the data table or file structure, and the data content they carry.
[0073] Next, you need to specify one or more fields in the target file, which are where the data will be stored after processing. The specification of the target field requires the user to clearly specify the name of the field and their corresponding position in the target database or file structure.
[0074] To ensure data consistency and accuracy, the mapping relationship also needs to include the data types of the source and target fields. Data types include but are not limited to integer, floating point, string, date, etc. Correct data type matching is the key to ensuring that data does not lose precision and format during the migration process.
[0075] The core part of the mapping relationship is the data processing logic, which defines how to convert the data in the source field into the data in the target field.
[0076] Furthermore, the data processing logic includes a combination of one or more of the following operations:
[0077] 1. Data cleaning operation, used to pre-process the source fields to eliminate redundant, erroneous or inconsistent data.
[0078] Specifically, data cleaning operations involve performing a series of preprocessing steps on source fields to purify the data and eliminate redundancies, errors, or inconsistencies that may affect the accuracy of data processing. This may include removing or filling in null values, standardizing data formats, correcting data entry errors, removing duplicate records, and filtering out data items that do not meet the criteria.
[0079] 2. A data type conversion operation is used to perform data type conversion when the data type of the source field is inconsistent with the data type of the target field.
[0080] Specifically, when the data type of the source field does not match the data type of the target field, the data type conversion operation ensures that the data can maintain its integrity and accuracy during the conversion process. For example, converting a string type date to a date type field, or converting a text-based number to a numeric type for mathematical calculations.
[0081] 3. Data calculation operation, used to perform mathematical operations or logical deductions on the source fields to generate the target data.
[0082] Specifically, data calculation operations allow mathematical operations or logical deductions to be performed on source fields to generate target data. This may include basic arithmetic operations (such as addition, subtraction, multiplication, and division), statistical calculations (such as average, sum, maximum, minimum), and more complex mathematical function applications.
[0083] 4. Complex data processing operations are used to perform user-defined complex calculations and logical processing when there are multiple source fields to meet specific business logic requirements.
[0084] Specifically, for complex data processing requirements involving multiple source fields, users can define specific complex data processing logic. This involves cross-field calculations, conditional logic (such as using IF-THEN-ELSE statements), data aggregation (such as the application of GROUP BY clauses), and nested queries. These operations enable data processing to meet specific business logic and analysis requirements. Among them,
[0085] Cross-field calculations: Users can define calculation rules involving multiple fields, such as using the values of source fields A and B to calculate a new value for target field C. For example, if field A contains the discount rate and field B contains the original price, field C may need to represent the discounted price, which requires a cross-field multiplication operation.
[0086] Conditional logic: With IF-THEN-ELSE type conditional logic, users can define data processing rules based on specific conditions. For example, if field A (sales) exceeds a certain threshold, field B (sales status) can be marked as "high", otherwise it is marked as "low".
[0087] Data Aggregation: Users can apply data aggregation functions, such as SUM, AVG, COUNT, MAX, MIN, etc., to process data. For example, summing up all order amounts for each sales region to calculate the total sales usually involves the use of a GROUP BY clause.
[0088] Nested query: For more complex data relationships, users may need to define nested queries. For example, first filter out the IDs of all valid orders from the order table, and then nested query the customer information of these orders.
[0089] In some embodiments, when there are multiple source files, the mapping relationship further includes: association relationships between different source files, where the association relationships are used to define the data logical connection between the source files. The data logical connection refers to explicitly specifying how to associate data in different source files based on common fields or logical conditions.
[0090] Specifically, in a multi-data source environment, each source file may contain interrelated data. For example, one file may contain customer information, while another file may contain order details, and the two are related by customer ID. The mapping relationship needs to clearly indicate this relationship (i.e., the logical connection of data) so that the data can be correctly referenced and connected in the generated SQL script.
[0091] Relationships are usually defined by common keys (such as customer ID, product number, etc.). In the template engine, users need to specify these keys to ensure that the system can recognize and use these keys to establish logical connections between different data sources.
[0092] In addition, users need to specify the type of association, such as one-to-one, one-to-many, or many-to-many. This information is crucial for generating correct SQL queries because different types of associations affect the structure and performance of queries.
[0093] After defining the association relationship, users also need to define how to integrate data from different data sources. This includes data merging, data priority rules (for example, when multiple data sources contain the same information, which data source's data is used first) and how to handle data conflicts.
[0094] Data merging: Users need to define how to merge data from multiple sources into a unified data structure. This may involve merging fields from different tables horizontally (using UNION or JOIN operations) or vertically (such as adding new data rows in time series data).
[0095] Data priority rules: In situations where multiple data sources contain the same information, users can set data priority rules. For example, you can designate information from a certain data source as authoritative data and take precedence when conflicts arise. This may be based on the source of the data, timestamp, data quality rating, or other business-related criteria.
[0096] Handling data conflicts: Users need to define strategies to handle data conflicts, that is, solutions when two or more data sources provide contradictory information. This may include: Select the latest data: give priority to the data with the latest timestamp; Select the most consistent data: use the data that is least inconsistent with other data; Data review: mark the conflicting data as pending review and manually resolve it by business personnel; Custom logic: write complex data decision logic based on specific business rules.
[0097] In order to filter out data that conforms to business logic, in some implementations, Figure 2 , further comprising: receiving a filter condition input by a user, wherein the filter condition is used to filter out data that conforms to business logic in the source field.
[0098] Specifically, users can enter specific filtering conditions through the template engine, which are used to filter data from source fields. Filtering conditions can be based on the range of field values, specific patterns, or logical expressions related to business rules. Filtering conditions can include but are not limited to equal conditions (for example, the date is equal to a specific value), unequal conditions (for example, the age is greater than a certain value), pattern matching (for example, a string contains specific text), and compound conditions (using logical operators AND, OR, NOT to combine multiple conditions).
[0099] User-defined filter conditions are integrated into the automatically generated SQL script to ensure that only data that meets these conditions is selected during data query or processing. This is usually achieved through the WHERE clause in the SQL statement.
[0100] In some complex business scenarios, the filtering conditions may need to change according to dynamic parameters. The template engine allows users to define these dynamic parameters and embed them into the filtering logic during the script generation process.
[0101] For example, suppose an e-commerce platform needs to generate a report showing the user's purchase behavior within a specific time period. Users may want to view data based on different time ranges (such as the past day, week, or month). Therefore, the filter conditions need to change dynamically based on the time period selected by the user.
[0102] Users select the time period they are interested in through a web interface. This time period is the dynamic parameter, which can be called @SelectedDateRange. The developer defines a template in the template engine, which contains a SQL query template that uses a placeholder {date_range} to represent the filter condition for the time period. When the user selects a time period from the interface (for example, "the past week"), this selection is passed to the template engine as the dynamic parameter @SelectedDateRange.
[0103] After receiving the @SelectedDateRange parameter, the template engine converts it into a specific date range (such as '2023-01-01' to '2023-01-08') and embeds it into the filter condition of the SQL script.
[0104] Before applying the filter conditions, the system provides a validation mechanism to ensure that the defined filter conditions are logically correct and compatible with the fields in the data source. This helps avoid errors or omissions of important data during data processing.
[0105] For example, in a sales data analysis scenario, the user may need to filter out all records whose sales exceed a certain threshold. Through the template engine, the user can define a filtering condition, such as "sales>10000", and the system will then implement this logic in the generated SQL script.
[0106] In addition, the process steps of the verification mechanism include:
[0107] First, the system checks the existence of each referenced field to ensure that all specified fields actually exist in the data source. Next, a data type check is performed to ensure that the field type matches the filtering logic, such as preventing text matching operations on numeric fields. Logical consistency checks ensure that conditional expressions using logical operators such as AND and OR are complete and reasonable. The system also verifies conditions involving ranges, such as dates or numeric ranges, to ensure their rationality, such as the start date is earlier than the end date.
[0108] In addition, if dependencies are set between filter conditions, the system will check whether these dependencies are met and will not cause conflicts. The system may also estimate the amount of data in the query results to ensure that the feasibility of the query and the impact on database performance are within a controllable range. User permissions must also be part of the verification to ensure that the user executing the query has permission to access all relevant fields.
[0109] This implementation improves the flexibility and accuracy of data processing while reducing the risk of errors and workload of manual data screening.
[0110] Step S30: Based on the mapping relationship, a corresponding SQL script code is generated by a preset algorithm.
[0111] Specifically, the template engine first receives user-defined mapping relationships, which define the data flow between the source file and the target file.
[0112] Next, the system uses a preset algorithm to process these mapping relationships and generate column mapping scripts to ensure that the conversion of data from source fields to target fields is accurate and in the expected format. When encountering data type mismatches, the system automatically performs necessary type conversions to ensure data compatibility.
[0113] For data that requires special processing, users can define custom processing logic, and the system will embed this logic into SQL scripts to meet complex data processing requirements.
[0114] When processing the association of multiple data tables, an association script is generated based on the information in the mapping relationship, including defining the relationship between the main table and the associated table and the association conditions.
[0115] To improve the readability and maintainability of the script, the system assigns aliases to the tables involved and uses these aliases in the generated script. In addition, data is filtered according to user-defined filter conditions to ensure that only data that meets specific business logic is included in the final data processing results.
[0116] Finally, all these elements are integrated to generate complete SQL script codes, which can be executed directly in the database management system to achieve data extraction, transformation and loading (ETL) and other operations.
[0117] This automated process not only improves the efficiency and accuracy of data processing, but also reduces the workload and complexity of manually writing and debugging SQL scripts, ensuring the consistency and reliability of data processing.
[0118] For the purpose of improving the executableness of the generated code, in some implementations, such as Figure 4 As shown, it also includes steps S41 to S43, wherein:
[0119] Step S41: Parse the generated SQL script code into an abstract syntax tree to perform syntax and structure analysis.
[0120] Specifically, an abstract syntax tree is a tree representation of the abstract syntax structure of the source code. During the compilation or interpretation process of a programming language, AST is used to represent the structure and operation of the code. AST consists of nodes, each of which represents a construct in the source code, such as an expression, statement, or declaration. The root node of the tree represents the beginning of the entire program or code snippet, while the leaf nodes usually represent basic input elements, such as variable names or literal values. The parent-child relationship between nodes represents the grammatical rules, and the child nodes are components of the parent nodes. For example, in the AST of an addition expression a+b, + is the root node, and a and b are child nodes.
[0121] The detailed process of parsing the generated SQL script code into an abstract syntax tree includes:
[0122] Before parsing into AST, the system first performs lexical analysis (LexicalAnalysis) to decompose the SQL script into a series of tokens, such as keywords (SELECT, FROM), identifiers, operators, and literals. Then syntax analysis (SyntaxAnalysis) is performed to organize these tokens into a structured tree model, namely AST, according to the syntax rules of the SQL language. This step checks whether the combination of tokens conforms to the syntax of SQL. The process of building AST involves mapping syntax units (such as expressions and statements) to nodes in the tree and determining the hierarchical relationship between nodes according to syntax rules. AST facilitates the verification of the correctness of SQL statements and further optimization. For example, you can check whether the data types are consistent and whether there are undefined variables or functions. AST can be traversed and transformed to perform code generation, query optimization, and other processing. Traversing AST allows the system to check and modify different parts of the code without affecting other parts.
[0123] Step S42: If the analysis finds that the SQL script code has errors, the errors are identified and corrected.
[0124] Specifically, if errors are found in the SQL script code during the analysis process, error identification will be performed automatically. This includes locating the location of the error, identifying the type of error (such as syntax error, type mismatch, logical error, etc.), and making corresponding correction suggestions or automatically correcting it according to preset rules and strategies. A variety of technologies are used to implement error correction, such as using preset error correction templates, applying machine learning algorithms to predict possible correction solutions, or performing intelligent corrections based on historical data and empirical rules.
[0125] Step S43: regenerate the corrected SQL script code based on the error correction result.
[0126] Specifically, based on the results of the error correction, the corrected SQL script code will be regenerated. This step ensures that the final generated script code is accurate and can be executed smoothly. The corrected script code will be parsed and analyzed again with AST to verify the effectiveness of the correction and ensure that no new errors are introduced.
[0127] This implementation significantly improves the efficiency of data processing, reduces debugging and maintenance costs, and improves developer productivity.
[0128] In some implementations, after the SQL script code is generated, Figure 5 As shown, it also includes steps S51 to S53, wherein:
[0129] Step S51: Integrate the generated SQL script code into the cloud platform through a predetermined interface.
[0130] Specifically, the generated SQL script code is uploaded and integrated into the cloud platform through a predetermined interface. The predetermined interface is specially designed for the cloud platform, which supports communication and data exchange with cloud services. The interface may be based on RESTful API, SOAP or other communication protocols supported by the cloud platform to ensure data security and transmission reliability.
[0131] Step S52: receiving a task scheduling instruction input by a user.
[0132] Specifically, users can enter task scheduling instructions through the cloud platform user interface or programmatically. These instructions define the execution plan of the SQL script, including execution time, frequency, dependencies, and other execution parameters.
[0133] Step S53: In response to the task scheduling instruction, a task is created on the cloud platform and the SQL script code is scheduled to be executed.
[0134] Specifically, after receiving the task scheduling instructions input by the user, the cloud platform will create corresponding tasks in the cloud environment according to these instructions. The task scheduler is responsible for managing and optimizing the execution order of tasks to ensure that tasks are executed according to the predetermined schedule and resource allocation. When a task is created and scheduled, the cloud platform will execute the SQL script code.
[0135] During the execution process, the execution status of the SQL script is monitored and detailed log information is recorded. These logs include execution time, execution results, consumed resources, and any errors or exceptions encountered.
[0136] After execution is complete, the results of the execution are fed back to the user, including any generated output data, statistics, or execution reports. The user can use this feedback to evaluate the effectiveness of the script and make necessary adjustments.
[0137] In order to better illustrate the method of automatically generating script code introduced in the present disclosure, a specific example is given below. Figure 6 The schematic diagram of this example is shown in Figure 1.
[0138] To develop a daily sales reporting system for an e-commerce platform, we designed an automated process to generate a report detailing the total purchase amount for each customer.
[0139] First, the developers used the template engine to define the data mapping relationship and processing logic. In the Orders table, they specified the order amount as the source column and mapped this data to the customer's total purchase amount in the Daily Sales report table. The processing logic is to sum up all order amounts for each customer to calculate the total purchase amount.
[0140] Next, the relationship between the tables is defined, and the daily sales report table is established as the main table, and a one-to-many relationship is established with the order table. The association condition is that the customer IDs in the two tables match. In addition, a filter condition is set to ensure that only orders for the current day are considered.
[0141] All this template information is stored in the database, providing the necessary input for the code generator. Based on this information, the code generator automatically generates a SQL script that joins the order table and the customer table through a JOIN operation, groups by customer ID, and calculates the total purchase amount of each customer. At the same time, the script contains a WHERE clause to filter the orders for the day.
[0142] The generated SQL script is syntax-checked by the SQL parser to ensure that there are no errors. The script is then uploaded to the cloud platform and a scheduled task is set for automatic execution every day. The cloud platform automatically runs the script at a predetermined time (for example, every morning) and stores the results in the database, allowing the reporting system to access and display the latest sales data.
[0143] Through the above steps, the e-commerce platform can efficiently and accurately generate daily sales reports, helping management to understand sales trends in a timely manner and make more informed business decisions.
[0144] It should be particularly noted that the process or method represented by the flowchart of the above embodiment of the present disclosure can be understood as representing a module, fragment or part of a code including one or more sets of executable instructions configured to implement the steps of a specific logical function or process. And the scope of the preferred embodiment of the present disclosure includes other implementations, in which the functions may not be performed in the order shown or discussed, including performing the functions in a substantially simultaneous manner or in a reverse order according to the functions involved.
[0145] like Figure 7 As shown, the device 70 for automatically generating script code of the present disclosure is shown. It should be noted that the principle and technical implementation of the device for automatically generating script code can refer to the method embodiment for automatically generating script code in the previous embodiment (for example Figure 2 ), so it will not be repeated in this embodiment.
[0146] Specifically, the device 70 for automatically generating script code includes: an acquisition module 71, a template engine 72, and a code generation module 73, wherein:
[0147] The acquisition module 71 is used to acquire a source file containing business logic;
[0148] The template engine 72 is used to receive the mapping relationship between the source file and the target file input by the user;
[0149] The code generation module 73 is used to generate corresponding SQL script codes based on the mapping relationship through a preset algorithm.
[0150] It should be noted that Figure 7 Each functional module in the embodiment can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a program instruction product. The program instruction product includes one or a group of program instructions. When the program instruction is loaded and executed on a computer, the process or function according to the present disclosure is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network or other programmable devices. The program instruction can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium.
[0151] and, Figure 7 The device disclosed in the embodiment can be implemented by other module division methods. The device embodiments shown above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as a group of modules or modules can be combined or dynamically added to another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or modules can be electrical or other forms.
[0152] in addition, Figure 7 Each functional module and submodule in the embodiment can be dynamically in a processing component, or each module can exist physically separately, or two or more modules can be dynamically in one component. The above-mentioned dynamic components can be implemented in the form of hardware or in the form of software functional modules. If the above-mentioned dynamic components are implemented in the form of software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. The storage medium can be a read-only memory, a disk or an optical disk, etc.
[0153] like Figure 8 As shown, a schematic diagram of the structure of an electronic device in an embodiment of the present disclosure is shown.
[0154] The electronic device can execute the following steps by running the computer program instructions: Figure 1 The method in any one of the items. Exemplarily, the electronic device may be a server group / server, a desktop computer, a laptop computer, etc., or a server / server group in the cloud that communicates remotely with a local terminal, a distributed computing node system, etc.
[0155] The electronic device 80 includes a bus 81, a processor 82, and a memory 83. The processor 82 and the memory 83 can communicate with each other through the bus 81. The memory 83 can store program instructions. The processor 82 implements the method steps in the previous embodiment by running the program instructions in the memory 83, such as Figure 2 Any method.
[0156] The bus 81 may be a peripheral component interconnect standard bus or an extended industry standard architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, although only one thick line is used in the figure, it does not mean that there is only one bus or one type of bus.
[0157] In some embodiments, the processor 82 may be implemented as a central processing unit (CPU), a microprocessing unit (MCU), a system on chip (System On Chip), or a field programmable logic array (FPGA). The memory 83 may include a volatile memory (Volatile Memory) for temporary storage of data when running a program, such as a random access memory (Random Access Memory, RAM).
[0158] The memory 83 may also include a non-volatile memory (non-volatile memory) for data storage, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD) or a solid-state disk (SSD).
[0159] In some embodiments, the electronic device 80 may further include a communicator 84. The communicator 84 is used to communicate with the outside. In a specific example, the communicator 84 may include one or a group of wired and / or wireless communication circuit modules. For example, the communicator 84 may include one or more of a wired network card, a USB module, a serial interface module, etc. The wireless communication protocols followed by the wireless communication module include: for example, near field wireless communication (NFC) technology, infrared (IR) technology, global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time division code division multiple access (TD-SCDMA), long term evolution (LTE), Bluetooth (BT), global navigation satellite system (GNSS), etc. One or more of the above.
[0160] In an embodiment of the present disclosure, a computer-readable storage medium may be provided, wherein program instructions are stored, and the program instructions are executed by running, for example Figure 2 A method for automatically generating script code in an embodiment.
[0161] That is, the method steps in the above embodiments are implemented as software or computer code that can be stored in a recording medium (such as a CD ROM, RAM, floppy disk, hard disk or magneto-optical disk), or are implemented as computer code originally stored in a remote recording medium or a non-temporary machine-readable medium downloaded via a network and to be stored in a local recording medium, so that the method represented herein can be stored in such software processing on a recording medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware (such as an ASIC or FPGA).
[0162] In summary, the identification method, device, electronic device and medium for automatically generating script codes disclosed in the present invention obtain source files, receive user-defined mapping relationships, and automatically generate SQL scripts based on these mapping relationships. The solution supports data cleaning, type conversion, complex calculations and multi-table association processing to ensure that data is converted accurately. In addition, it also has error detection and correction functions, which can automatically optimize the generated scripts. Finally, the script can be integrated and scheduled for execution through the cloud platform to achieve automation of data processing and cloud deployment.
[0163] The above embodiments are merely illustrative of the principles and effects of the present disclosure, and are not intended to limit the present disclosure. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present disclosure. Therefore, all equivalent modifications or changes made by a person of ordinary skill in the art without departing from the spirit and technical ideas disclosed in the present disclosure shall still be covered by the claims of the present disclosure.
Claims
1. A method for automatically generating script code, characterized in that: include: Obtaining source files containing data to be processed; Receive a mapping relationship between the source file and the target file input by a user, wherein the mapping relationship is used to define a conversion rule and path for the source file data; Based on the mapping relationship, the corresponding SQL script code is generated through a preset algorithm.
2. The method for automatically generating script code according to claim 1, characterized in that: When there is only one source file, the mapping relationship includes: one or more source fields in the source document; one or more target fields in the target file; The data type of the source field; The data type of the target field; Data processing logic that converts the source field to the target field.
3. The method for automatically generating script code according to claim 2, characterized in that: The data processing logic includes a combination of one or more of the following operations: A data cleaning operation is used to pre-process the source fields to eliminate redundant, erroneous or inconsistent data; A data type conversion operation, used to perform data type conversion when the data type of the source field is inconsistent with the data type of the target field; Data calculation operation, used to perform mathematical operation or logical deduction on the source field to generate the target data; Complex data processing operations are used to perform user-defined complex calculations and logical processing when there are multiple source fields to meet specific business logic requirements.
4. The method for automatically generating script code according to claim 2, characterized in that: When there are multiple source files, the mapping relationship also includes: The association relationship between different source files is used to define the data logical connection between each source file, and the data logical connection is used to indicate how to associate the data in different source files based on common fields or logical conditions.
5. The method for automatically generating script code according to claim 2, characterized in that: When the data of the source file needs to be screened, the mapping relationship also includes: The filter condition is used to filter out data that conforms to the business logic in the source field.
6. The method for automatically generating script code according to claim 1, characterized in that: When the SQL script code is generated, it also includes: Parsing the generated SQL script code into an abstract syntax tree for syntax and structure analysis; If the analysis finds that the SQL script code has errors, the errors are identified and corrected; Based on the error correction results, regenerate the corrected SQL script code.
7. The method for automatically generating script code according to claim 1, characterized in that: When the SQL script code is generated, it also includes: Integrating the generated SQL script code into the cloud platform through a predetermined interface; Receive task scheduling instructions input by users; In response to the task scheduling instruction, a task is created on the cloud platform and the SQL script code is scheduled to be executed.
8. A device for automatically generating script code, characterized in that: include: The acquisition module is used to obtain the source file containing the business logic; The template engine is used to receive the mapping relationship between the source file and the target file input by the user; The code generation module is used to generate corresponding SQL script codes through a preset algorithm based on the mapping relationship.
9. An electronic device, characterized in that: The electronic device comprises: Processor and memory; Wherein, the memory is used to store computer programs; The processor is used to execute the computer program stored in the memory so that the electronic device executes the method for automatically generating script code as described in any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by an electronic device, the method for automatically generating script codes as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method for customizing script automatization using configuration file
CN101355774A
Code fixing method, device and electronic equipment
CN107015813A
Code generating method and device for migration of data in isomerous database
CN107958057A
SQL interpreter of HBase and optimization method
CN111309757A
Method and device for generating ETL codes
CN111324647A
Cited By
Code generation method and device, computer equipment and readable storage medium
CN120929085A