Method, device, electronic device and storage medium for converting PLSQL code

By generating a syntax tree and identifying the node position order, the automatic translation problem of Oracle PLSQL code is solved, and efficient scenario type adaptive conversion and improved code readability are achieved.

CN114816422BActive Publication Date: 2025-09-16CHINA PING AN LIFE INSURANCE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210440076.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-25
Publication Date
2025-09-16
Estimated Expiration
2042-04-25

AI Technical Summary

Technical Problem

In the existing technology, Oracle PLSQL code translation solutions have a labor-intensive, time-consuming and labor-intensive manual translation method, and machine automatic translation has difficulty handling complex contextual relationships and scenario types, resulting in rigid translated code that does not conform to development practices.

Method used

By obtaining the PLSQL code text, generating a syntax tree and traversing the node position order, identifying the scenario type, and automatically translating and converting based on the context relationship of the syntax tree, the target code that conforms to the coding conventions is generated.

Benefits of technology

It realizes automatic translation of PLSQL code and targeted conversion of scenario types, improves the readability and compliance of the code, and conforms to the coding habits of the target language.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114816422B_ABST
    Figure CN114816422B_ABST
Patent Text Reader

Abstract

The embodiment of the present invention provides a method, device, electronic device and storage medium for converting PLSQL code, including: obtaining a PLSQL code text to be converted that carries a loop block statement object, a cursor loop fetch statement object and an exit when statement object; translating the PLSQL code text to generate a syntax tree, the syntax tree including corresponding loop stmt nodes, fetch stmt nodes and exit stmt nodes; traversing nodes in the syntax tree and obtaining a node position sequence between the loop stmt nodes, fetch stmt nodes and exit stmt nodes in the syntax tree; identifying the PLSQL code text according to the node position sequence to obtain a scenario type of the PLSQL code text; and converting the PLSQL code text according to the scenario type to obtain a target code text. The embodiment of the present invention can not only realize automatic translation, but also make the target code more consistent with coding habits, greatly improve code readability, and can also perform targeted conversion processing for different scenario types.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of artificial intelligence technology, and in particular relates to a method, device, electronic device and storage medium for converting PLSQL code. Background Art

[0002] Many companies currently rely on Oracle for data storage. Oracle provides packages that facilitate organizing business logic, but this database-centric business logic struggles to keep pace with business growth. Therefore, Oracle often needs to be repurposed for data storage, extracting the business logic from the package and translating it into other languages.

[0003] Currently, Oracle's translation solutions mainly include the following two:

[0004] First, manual translation is used. The advantage of this method is that old code can be refactored. The disadvantage is that it is too labor-intensive and time-consuming, which is unrealistic for many businesses, especially those with heavy functional logic.

[0005] Second, machine translation generally relies on string matching. This approach is suitable for translating simple PLSQL statements sentence by sentence, but struggles with complex contexts. The resulting code is rigid and doesn't conform to development coding practices. Furthermore, it's difficult to translate PLSQL code tailored to specific scenarios. Summary of the Invention

[0006] The following is a summary of the subject matter described in detail herein. This summary is not intended to limit the scope of the claims.

[0007] The embodiments of the present invention provide a method, device, electronic device, and storage medium for converting PLSQL code, which can not only achieve automatic translation, but also make the target code more consistent with coding practices, greatly improve code readability, and perform targeted conversion processing for different scenario types.

[0008] In a first aspect, an embodiment of the present invention provides a method for converting PLSQL code, comprising: obtaining PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exitwhen statement object; performing translation processing on the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loopstmt node corresponding to the loop block statement object, a fetchstmt node corresponding to the cursor loop fetch statement object, and an exitstmt node corresponding to the exitwhen statement object; traversing nodes in the syntax tree and obtaining a node position sequence between the loopstmt node, the fetchstmt node, and the exitstmt node in the syntax tree; performing identification processing on the PLSQL code text according to the node position sequence to obtain a scenario type of the PLSQL code text; and performing conversion processing on the PLSQL code text according to the scenario type to obtain a target code text.

[0009] In some embodiments, traversing the nodes in the syntax tree and obtaining the node position order between the loop stmt node, the fetch stmt node and the exit stmt node in the syntax tree includes: traversing the nodes in the syntax tree and determining that the node position order of the syntax tree is: the first child node of the loop stmt node is the fetch stmt node, and the second child node of the loop stmt node is the exit stmt node.

[0010] In some embodiments, the identifying and processing the PLSQL code text according to the node position sequence to obtain the scenario type of the PLSQL code text includes: when the node position sequence is that the first child node of the loop stmt node is the fetch stmt node, and the second child node of the loop stmt node is the exit stmt node, identifying and processing the PLSQL code text to obtain the scenario type of the PLSQL code text is: the cursor loop fetch statement object and the exit when statement object are both located in the loop loop block statement object.

[0011] In some embodiments, converting the PLSQL code text according to the scenario type to obtain a target code text includes: in the process of converting the PLSQL code text, converting the loop block statement object into a while loop block statement object, using whether the cursor has the next data as an entry condition for the while loop block statement object, and deleting the exit when statement object.

[0012] In some embodiments, traversing the nodes in the syntax tree and obtaining the node position order between the loop stmt node, the fetch stmt node and the exit stmt node in the syntax tree includes: traversing the nodes in the syntax tree and determining that the node position order of the syntax tree is: the first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node.

[0013] In some embodiments, the identifying and processing the PLSQL code text according to the node position order to obtain the scenario type of the PLSQL code text includes: when the node position order is that the first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node, identifying and processing the PLSQL code text to obtain the scenario type of the PLSQL code text is: the cursor loop fetch statement object is located before the loop loop block statement object, and the exitwhen statement objects are all located in the loop loop block statement object.

[0014] In some embodiments, converting the PLSQL code text according to the scenario type to obtain a target code text includes: in the process of converting the PLSQL code text, converting the loop block statement object into a while loop block statement object, using whether cursor data is found as an entry condition of the while loop block statement object, and deleting the exit when statement object.

[0015] In a second aspect, an embodiment of the present invention further provides an apparatus for converting PLSQL code, comprising: an acquisition unit, configured to acquire PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object; a translation unit, configured to translate the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; a traversal unit, configured to traverse nodes in the syntax tree and obtain a node position sequence between the loop stmt node, the fetch stmt node, and the exit stmt node in the syntax tree; an identification unit, configured to identify the PLSQL code text according to the node position sequence to obtain a scenario type of the PLSQL code text; and a conversion unit, configured to convert the PLSQL code text according to the scenario type to obtain a target code text.

[0016] In a third aspect, an embodiment of the present invention further provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for converting PLSQL code as described in the first aspect above is implemented.

[0017] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions are used to execute the method for converting PLSQL code as described in the first aspect above.

[0018] An embodiment of the present invention includes: first, obtaining a PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object; then, translating the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; then, traversing the nodes in the syntax tree and obtaining a node position sequence between the loop stmt node, the fetchstmt node, and the exit stmt node in the syntax tree; then, identifying the PLSQL code text according to the node position sequence to obtain a scenario type of the PLSQL code text; and finally, converting the PLSQL code text according to the scenario type to obtain a target code text. According to the technical solution of the embodiment of the present invention, firstly, the embodiment of the present invention can realize the automatic translation of PLSQL code text carrying loop block statement objects, cursor loop fetch statement objects, and exit when statement objects; and based on the contextual relationship of the syntax tree, the target code can be made more consistent with coding practices, greatly improving the code readability; and the scenario type of the PLSQL code text can be determined based on the node position sequence of the syntax tree, and targeted conversion processing can be performed for different scenario types.

[0019] Other features and advantages of the present invention will be described in the following description, and in part will become apparent from the description, or will be understood by practicing the present invention. The purposes and other advantages of the present invention can be realized and obtained by the structures particularly pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] The accompanying drawings are used to provide a further understanding of the technical solution of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the technical solution of the present invention and do not constitute a limitation to the technical solution of the present invention.

[0021] Figure 1 This is a flowchart of a method for converting PLSQL code provided by one embodiment of the present invention;

[0022] Figure 2 yes Figure 1 A flowchart of an embodiment of step S300;

[0023] Figure 3 yes Figure 1 A flowchart of an embodiment of step S400;

[0024] Figure 4 yes Figure 1 A flowchart of an embodiment of step S500;

[0025] Figure 5 yes Figure 1 Flowchart of another embodiment of step S300;

[0026] Figure 6 yes Figure 1 Flowchart of another embodiment of step S400;

[0027] Figure 7 yes Figure 1 Flowchart of another embodiment of step S500;

[0028] Figure 8 This is a schematic diagram of a syntax tree structure in the first scenario type provided by an embodiment of the present invention;

[0029] Figure 9 This is a schematic diagram of a syntax tree structure in the second scenario type provided by an embodiment of the present invention;

[0030] Figure 10 1 is a schematic diagram of the structure of a device for converting PLSQL code provided by one embodiment of the present invention;

[0031] Figure 11 It is a schematic diagram of the hardware structure of an electronic device provided by one embodiment of the present invention. DETAILED DESCRIPTION

[0032] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0033] It should be noted that although the device schematics illustrate functional module divisions and the flowcharts illustrate logical sequences, in certain circumstances, the steps shown or described may be performed in a sequence that differs from the module divisions in the device or the sequence in the flowcharts. The terms "first," "second," and so on, in the specification, claims, and drawings, are used to distinguish similar items and are not necessarily used to describe a specific sequence or precedence.

[0034] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which the present invention pertains. The terms used herein are for the purpose of describing embodiments of the present invention only and are not intended to limit the present invention.

[0035] First, some terms involved in this invention are analyzed:

[0036] Artificial intelligence (AI) is a new technical discipline that studies and develops theories, methods, technologies, and application systems for simulating, extending, and expanding human intelligence. A branch of computer science, AI seeks to understand the essence of intelligence and produce new intelligent machines that can respond in a manner similar to human intelligence. Research in this field includes robotics, speech recognition, image recognition, natural language processing, and expert systems. AI can simulate the information processes of human consciousness and thinking. It also encompasses the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, to perceive the environment, acquire knowledge, and use that knowledge to achieve optimal results.

[0037] Natural language processing (NLP): NLP uses computers to process, understand, and apply human languages ​​(such as Chinese and English). A branch of artificial intelligence, NLP is an interdisciplinary field between computer science and linguistics, often referred to as computational linguistics. Natural language processing encompasses grammatical analysis, semantic analysis, and discourse comprehension. Natural language processing is commonly used in technical fields such as machine translation, handwritten and printed character recognition, speech recognition and text-to-speech conversion, information retrieval, information extraction and filtering, text classification and clustering, public opinion analysis, and opinion mining. It encompasses data mining, machine learning, knowledge acquisition, knowledge engineering, artificial intelligence research related to language processing, and linguistics research related to language computing.

[0038] Information Extraction (NER): A text processing technology that extracts specified types of entity, relationship, event, and other factual information from natural language text and forms structured data output. Information extraction is a technology that extracts specific information from text data. Text data is composed of some specific units, such as sentences, paragraphs, and chapters. Text information is composed of some small specific units, such as characters, words, phrases, sentences, paragraphs, or a combination of these specific units. Extracting noun phrases, names, place names, etc. from text data is all text information extraction. Of course, the information extracted by text information extraction technology can be of various types.

[0039] PLSQL (Procedural Language / SQL): A programming language, also known as procedural SQL. PLSQL is an Oracle database extension of SQL statements. By adding programming language features to regular SQL statements, PLSQL organizes data manipulation and query statements within procedural units within the PLSQL code, implementing complex functions or calculations through logical judgments, loops, and other operations. Specifically, PLSQL is a third-generation language that, like languages ​​like C, C++, and JAVA, focuses on processing details and can be used to implement relatively complex business logic. It allows SQL's data manipulation language and query statements to be incorporated into block structures and procedural code, making PLSQL a powerful transaction processing language.

[0040] Oracle: Oracle Database, also known as Oracle RDBMS, is a relational database management system from Oracle Corporation. It has consistently held a leading position in the database field. The Oracle database system is considered the world's most popular relational database management system, offering excellent portability, ease of use, and powerful functionality. It is suitable for a wide range of large, medium, and small computer environments. It is a highly efficient, reliable, and high-throughput database solution.

[0041] JAVA: An object-oriented programming language, it incorporates the strengths of C++ while eliminating C++'s complex concepts like multiple inheritance and pointers. This makes JAVA both powerful and easy to use. As a representative static object-oriented programming language, JAVA perfectly implements object-oriented theory, allowing programmers to approach complex programming with an elegant mindset. JAVA boasts simplicity, object orientation, distributed architecture, robustness, security, platform independence and portability, multithreading, and dynamism. JAVA can be used to develop desktop applications, web applications, distributed systems, and embedded systems.

[0042] A syntax tree is a graphical representation of a sentence structure. It represents the derivation of a sentence and helps us understand the hierarchy of the sentence's grammatical structure. Simply put, a syntax tree is the tree formed when deducing according to a certain rule.

[0043] Currently, many companies rely on Oracle for data storage. Oracle provides packages that facilitate organizing business logic, but this database-centric business logic struggles to keep pace with business growth. Therefore, Oracle often needs to be repurposed for data storage, extracting the business logic from the package and translating it into other languages.

[0044] In the existing technology, Oracle translation solutions mainly include the following two:

[0045] First, manual translation is used. The advantage of this method is that old code can be refactored. The disadvantage is that it is too labor-intensive and time-consuming, which is unrealistic for many businesses, especially those with heavy functional logic.

[0046] Second, automatic machine translation generally employs string matching. This approach is suitable for translating simple PLSQL statements sentence by sentence, but struggles with complex contextual relationships. The resulting code is rigid and doesn't conform to development coding conventions. Currently, PLSQL business logic often uses a cursor fetch loop to retrieve data. Within the loop block, a fetchcursor into is called to retrieve a single piece of data, and the loop is exited when cursor% not found is detected. Conventional machine translation solutions often use a while loop, exiting with a break when cursor% not found is detected. This translation logic aligns well with PLSQL coding conventions, but often doesn't conform to the target code. Furthermore, it's difficult to perform targeted conversions for PLSQL code in different scenarios.

[0047] Based on this, an embodiment of the present invention provides a method for converting PLSQL code, an apparatus for converting PLSQL code, an electronic device, and a computer-readable storage medium. First, a PLSQL code text to be converted is obtained, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object. Then, the PLSQL code text is translated to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetchstmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object. Then, the nodes in the syntax tree are traversed to obtain a node position sequence between the loop stmt node, the fetch stmt node, and the exit stmt node in the syntax tree. Then, the PLSQL code text is identified based on the node position sequence to obtain a scenario type of the PLSQL code text. Finally, the PLSQL code text is converted based on the scenario type to obtain a target code text. According to the technical solution of the embodiment of the present invention, firstly, the embodiment of the present invention can realize the automatic translation of PLSQL code text carrying loop block statement objects, cursor loop fetch statement objects, and exit when statement objects; and based on the contextual relationship of the syntax tree, the target code can be made more consistent with coding practices, greatly improving the code readability; and the scenario type of the PLSQL code text can be determined based on the node position sequence of the syntax tree, and targeted conversion processing can be performed for different scenario types.

[0048] The method for converting PLSQL code, the apparatus for converting PLSQL code, the electronic device, and the computer-readable storage medium provided by the embodiments of the present invention are specifically described through the following embodiments. First, the method for converting PLSQL code in the embodiments of the present invention is described.

[0049] Embodiments of the present invention can acquire and process relevant data based on artificial intelligence technology. Artificial Intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to achieve optimal results.

[0050] Fundamental AI technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interaction systems, and mechatronics. AI software technologies primarily encompass computer vision, robotics, biometrics, speech processing, natural language processing, and machine learning / deep learning.

[0051] The method for converting PLSQL code provided in an embodiment of the present invention relates to the field of artificial intelligence technology. The method for converting PLSQL code provided in an embodiment of the present invention can be applied in a terminal, can be applied in a server side, or can be software running in a terminal or a server side. In some embodiments, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, etc.; the server side can be configured as an independent physical server, or can be configured as a server cluster or a distributed system composed of multiple physical servers, or can be configured as a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application that implements the method for converting PLSQL code, etc., but is not limited to the above forms.

[0052] The present invention can be used in a wide variety of general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, and the like. The present invention can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present invention can also be practiced in distributed computing environments in which tasks are performed by remote processing devices connected via a communications network. In a distributed computing environment, program modules can be located in local and remote computer storage media, including storage devices.

[0053] like Figure 1 As shown, Figure 1 1 is a flowchart of a method for converting PLSQL code provided by an embodiment of the present invention; the method for converting PLSQL code according to the embodiment of the present invention may include but is not limited to step S100, step S200, step S300, step S400 and step S500.

[0054] Step S100: Obtain PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object;

[0055] Step S200: translating the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object;

[0056] Step S300: traverse the nodes in the syntax tree and obtain the node position sequence between the loop stmt node, the fetchstmt node and the exit stmt node in the syntax tree;

[0057] Step S400: Identify the PLSQL code text according to the node position sequence to obtain the scene type of the PLSQL code text;

[0058] Step S500: convert the PLSQL code text according to the scenario type to obtain the target code text.

[0059] Specifically, for a PLSQL code text carrying a loop block statement object, a cursor loop fetch statement object, and an exitwhen statement object, it is first translated into a syntax tree. The generated syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetchstmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object. Then, the scenario type of the PLSQL code text is inferred based on the node position sequence in the syntax tree, and different conversion methods are selected for specific scenario types.

[0060] According to the technical solution of the embodiment of the present invention, first, the embodiment of the present invention can realize the automatic translation of PLSQL code text carrying loop block statement objects, cursor loop fetch statement objects, and exit when statement objects through the above-mentioned steps S100 to S500. Secondly, the embodiment of the present invention can make the target code more consistent with coding practices based on the contextual relationship of the syntax tree, and the code readability is greatly improved. In addition, the embodiment of the present invention can also determine the scenario type of the PLSQL code text based on the node position sequence of the syntax tree, and perform targeted conversion processing for different scenario types.

[0061] In addition, if Figure 2 As shown, Figure 2 yes Figure 1 Flowchart of an embodiment of step S300 in the above step S300; regarding traversing the nodes in the syntax tree and obtaining the node position sequence between the loop stmt node, the fetch stmt node and the exit stmt node in the syntax tree, it may include but is not limited to steps S311 and S312.

[0062] Step S311, traverse the nodes in the syntax tree;

[0063] Step S312: Determine the node position sequence of the syntax tree as follows: the first child node of the loop stmt node is the fetch stmt node, and the second child node of the loop stmt node is the exit stmt node.

[0064] Specifically, since the PLSQL code text to be converted carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object, the generated syntax tree will correspondingly have a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; an embodiment of the present invention can obtain a node position order by traversing the nodes in the syntax tree. When the cursor loop fetch statement object and the exit when statement object are both located in the loop block statement object, the obtained node position order will be: the first child node of the loop stmt node is the fetchstmt node, and the second child node of the loop stmt node is the exit stmt node.

[0065] In addition, if Figure 3 As shown, Figure 3 yes Figure 1 Flowchart of an embodiment of step S400; regarding the above step S400 in which the PLSQL code text is identified and processed according to the node position order to obtain the scenario type of the PLSQL code text, it may include but is not limited to step S411 and step S412.

[0066] Step S411: In the case where the first child node of the loop stmt node is the fetch stmt node and the second child node of the loop stmt node is the exit stmt node,

[0067] Step S412: Identify and process the PLSQL code text to obtain a scenario type of the PLSQL code text: the cursor loop fetch statement object and the exit when statement object are both located in the loop block statement object.

[0068] Specifically, when the obtained node position sequence is that the first child node of the loop stmt node is the fetchstmt node, and the second child node of the loop stmt node is the exit stmt node, it can be inferred that the scenario type of the PLSQL code text is: the cursor loop fetch statement object and the exit when statement object are both located in the loop loop block statement object.

[0069] In addition, if Figure 4 As shown, Figure 4 yes Figure 1 Flowchart of an embodiment of step S500; regarding the conversion of the PLSQL code text according to the scenario type in the above step S500 to obtain the target code text, it may include but is not limited to step S511 and step S512.

[0070] Step S511: in the process of converting the PLSQL code text, converting the loop block statement object into a while loop block statement object;

[0071] Step S512: Whether the cursor has the next piece of data is used as the entry condition of the while loop block statement object, and the exit when statement object is deleted.

[0072] Specifically, when the scenario type of the PLSQL code text is: the cursor loop fetch statement object and the exit when statement object are both located in the loop block statement object, during the conversion process, the embodiment of the present invention will convert the loop block statement object into a while loop block statement object, and at the same time, use whether the cursor has the next data as the entry condition of the while loop block statement object, and delete the exit when statement object.

[0073] In addition, if Figure 5 As shown, Figure 5 yes Figure 1 Flowchart of another embodiment of step S300 in the above step S300; regarding traversing the nodes in the syntax tree and obtaining the node position sequence between the loop stmt node, the fetch stmt node and the exit stmt node in the syntax tree, it may include but is not limited to steps S321 and S322.

[0074] Step S321, traverse the nodes in the syntax tree;

[0075] Step S322: Determine the node position order of the syntax tree: the first child node of the loop stmt node is the exitstmt node, and the fetch stmt node is located before the loop stmt node.

[0076] Specifically, since the PLSQL code text to be converted carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object, the generated syntax tree will correspondingly have a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; an embodiment of the present invention can obtain a node position order by traversing the nodes in the syntax tree. When the cursor loop fetch statement object is located before the loop block statement object, and the exitwhen statement objects are both located in the loop block statement object, the obtained node position order will be: the first child node of the loopstmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node.

[0077] In addition, if Figure 6 As shown, Figure 6 yes Figure 1 Flowchart of another embodiment of step S400; Regarding the above step S400, the PLSQL code text is identified and processed according to the node position order to obtain the scene type of the PLSQL code text, which may include but is not limited to step S421 and step S422.

[0078] Step S421: In the case where the first child node of the loop stmt node is the exit stmt node in the node position sequence, and the fetch stmt node is located before the loop stmt node;

[0079] Step S422: Identify and process the PLSQL code text to obtain a scenario type of the PLSQL code text: the cursor loop fetch statement object is located before the loop block statement object, and the exit when statement objects are all located in the loop block statement object.

[0080] Specifically, when the obtained node position sequence is that the first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node, it can be inferred that the scenario type of the PLSQL code text is: the cursor loop fetch statement object is located before the loop loop block statement object, and the exitwhen statement objects are all located in the loop loop block statement object.

[0081] In addition, if Figure 7 As shown, Figure 7 yes Figure 1 Flowchart of another embodiment of step S500; regarding the conversion of the PLSQL code text according to the scenario type in the above step S500 to obtain the target code text, it may include but is not limited to step S521 and step S522.

[0082] Step S521: in the process of converting the PLSQL code text, convert the loop block statement object into a while loop block statement object;

[0083] Step S522: Whether cursor data is found is used as the entry condition of the while loop block statement object, and the exit when statement object is deleted.

[0084] Specifically, when the scenario type of the PLSQL code text is: the cursor loop fetch statement object is located before the loop block statement object, and the exit when statement objects are all located in the loop block statement object, during the conversion process, the embodiment of the present invention will convert the loop block statement object into a while loop block statement object, and at the same time, use whether cursor data is found as the entry condition of the while loop block statement object, and delete the exit when statement object.

[0085] Based on the above Figures 2 to 7 The method flow in the embodiment of the present invention provides a specific solution for converting PLSQL code text as follows:

[0086] There are two common scenarios for cursor loop fetching. The following uses these two scenarios as examples to explain in detail how the embodiments of the present invention perform structural adjustments to achieve code optimization.

[0087] For example, the PLSQL code text in the first scenario type is as follows:

[0088]

[0089] For example, the PLSQL code text in the second scenario type is as follows:

[0090]

[0091] Taking Java as an example, the target code is generally as follows after machine translation:

[0092] For the PLSQL code text of the first scenario type, the machine-translated code sentence by sentence is as follows:

[0093]

[0094] For the PLSQL code text of the second scenario type, the machine-translated code is as follows:

[0095]

[0096]

[0097] The machine-translated code in the two scenarios above, which is PLSQL-style Java code, does not conform to Java coding conventions. Industry-standard string-matching-based translation solutions, even if they complete the PLSQL code sentence by sentence, make further adjustments to loop structures and code optimization extremely difficult.

[0098] In this regard, the embodiment of the present invention uses a syntax tree to perform code optimization, wherein, for example Figure 8 and Figure 9 As shown, Figure 8 This is a schematic diagram of a syntax tree structure in the first scenario type provided by an embodiment of the present invention; Figure 9 1 is a schematic diagram of a syntax tree structure in a second scenario type provided by an embodiment of the present invention. When traversing and exiting the loop stmt node, the structure of the syntax tree is checked.

[0099] Specifically, if Figure 8 As shown, if it is found that the first child node of loop stmt is the fetch stmt node (fetchv_cur to get data) and the second child node is the exit stmt node (exit when v_cur% not found to exit the loop), then the code that meets the first scenario will use whether the cursor has the next data as the condition for entering the loop when generating the target code of loop stmt. At the same time, the code of the exit when tree node is removed from the loop code block (it is no longer necessary because the exit judgment has been transferred to the while condition). The target code obtained is as follows:

[0100]

[0101] In addition, if Figure 9 As shown, if it is found that the first child node of loop stmt is the exit stmt node (exit whenv_cur% notfound exits the loop), then the fetch stmt node must have been processed before, satisfying the code of the second scenario. When generating the target code of loop stmt, whether the cursor data is found is used as the condition for loop entry. At the same time, the code of the exit when tree node is removed from the loop code block. The target code obtained is as follows:

[0102]

[0103]

[0104] Compared to machine-translated code, the code adjusted using this embodiment is significantly more reasonable and conforms to Java coding conventions. Based on the syntax tree, this embodiment optimizes the code structure of the cursor fetch loop, adjusts the while loop condition, and removes the original exit code from the while loop body, making the target code more consistent with coding conventions and significantly improving code readability.

[0105] See also Figure 10 The embodiment of the present invention further provides a PLSQL code conversion device, which can implement the above-mentioned method of converting PLSQL code. The PLSQL code conversion device 100 includes:

[0106] An acquiring unit 110 is configured to acquire a PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object;

[0107] The translation unit 120 is configured to translate the PLSQL code text and generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object.

[0108] A traversal unit 130 is used to traverse the nodes in the syntax tree and obtain the node position sequence between the loop stmt node, the fetch stmt node and the exit stmt node in the syntax tree;

[0109] The identification unit 140 is used to identify the PLSQL code text according to the node position sequence to obtain the scene type of the PLSQL code text;

[0110] The conversion unit 150 is used to convert the PLSQL code text according to the scenario type to obtain the target code text.

[0111] The specific implementation of the PLSQL code conversion device is basically the same as the specific embodiment of the above-mentioned method for converting PLSQL code, and will not be repeated here.

[0112] An embodiment of the present invention further provides an electronic device comprising: a memory, a processor, a program stored in the memory and executable on the processor, and a data bus for enabling communication between the processor and the memory. When the program is executed by the processor, the method for converting PL / SQL code described above is implemented. The electronic device can be any intelligent terminal, including a tablet computer and an in-vehicle computer.

[0113] See also Figure 11 , Figure 11 The hardware structure of an electronic device according to another embodiment is shown. The electronic device includes:

[0114] The processor 210 may be implemented as a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is configured to execute relevant programs to implement the technical solutions provided by the embodiments of the present invention.

[0115] The memory 220 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 220 can store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 220, and the processor 210 calls and executes the method for converting PLSQL code in the embodiments of the present invention.

[0116] Input / output interface 230, used to implement information input and output;

[0117] Communication interface 240, used to implement communication interaction between this device and other devices, which can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WiFi, Bluetooth, etc.); and

[0118] bus 250 , which transmits information between the various components of the device (e.g., processor 210 , memory 220 , input / output interface 230 , and communication interface 240 );

[0119] The processor 210 , the memory 220 , the input / output interface 230 and the communication interface 240 are connected to each other in communication within the device via the bus 250 .

[0120] An embodiment of the present invention further provides a storage medium, which is a computer-readable storage medium used for computer-readable storage. The storage medium stores one or more programs, and the one or more programs can be executed by one or more processors to implement the above-mentioned method of converting PLSQL code.

[0121] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0122] The embodiments described in the embodiments of the present invention are intended to more clearly illustrate the technical solutions of the embodiments of the present invention and do not constitute a limitation on the technical solutions provided by the embodiments of the present invention. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of the present invention are also applicable to similar technical problems.

[0123] It will be understood by those skilled in the art that Figure 1-7 The technical solutions shown in the figures do not constitute a limitation on the embodiments of the present invention, and may include more or fewer steps than those shown in the figures, or a combination of certain steps, or different steps.

[0124] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, i.e., they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of this embodiment.

[0125] Those skilled in the art will appreciate that all or some of the steps in the methods, systems, and functional modules / units in the devices disclosed above may be implemented as software, firmware, hardware, or appropriate combinations thereof.

[0126] The terms "first," "second," "third," "fourth," and the like (if any) in the description of the present invention and the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a particular order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate so that the embodiments of the present invention described herein can be implemented in orders other than those illustrated or described herein. In addition, the terms "including" and "having," as well as any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units that are not explicitly listed or that are inherent to such processes, methods, products, or apparatus.

[0127] It should be understood that in the present invention, "at least one (item)" refers to one or more, and "plurality" refers to two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can represent: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.

[0128] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the above units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0129] The units described above as separate components may or may not be physically separate, and 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 these units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0130] In addition, the functional units in the various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0131] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes multiple instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), magnetic disk or optical disk, and other media that can store programs.

[0132] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the invention is not limited thereby. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and essence of the invention should be within the scope of the invention.

Claims

1. A method for converting PLSQL code, characterized in that: include: Obtaining a PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object; Translate the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; Traversing the nodes in the syntax tree, determining that the node position order of the syntax tree is: the first child node of the loop stmt node is the fetch stmt node, and the second child node of the loop stmt node is the exit stmt node; In a case where the node position sequence is such that the first child node of the loop stmt node is the fetch stmt node, and the second child node of the loop stmt node is the exit stmt node, the PLSQL code text is identified and processed to obtain a scenario type of the PLSQL code text: the cursor loop fetch statement object and the exit when statement object are both located in the loop block statement object; The PLSQL code text is converted according to the scenario type to obtain a target code text.

2. The method for converting PLSQL code according to claim 1, wherein: The converting process of the PLSQL code text according to the scenario type to obtain a target code text includes: In the process of converting the PLSQL code text, the loop block statement object is converted into a while loop block statement object, and whether the cursor has the next data is used as the entry condition of the while loop block statement object, and the exit when statement object is deleted.

3. A method for converting PLSQL code, characterized in that: include: Obtaining a PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object; Translate the PLSQL code text to generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; Traversing the nodes in the syntax tree, determining that the node position order of the syntax tree is: the first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node; In a case where the node position sequence is such that the first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node, the scenario type of the PLSQL code text obtained by performing recognition processing on the PLSQL code text is: the cursor loop fetch statement object is located before the loop block statement object, and the exit when statement objects are all located in the loop block statement object; The PLSQL code text is converted according to the scenario type to obtain a target code text.

4. A device for converting PLSQL code, characterized in that: include: An acquiring unit, configured to acquire a PLSQL code text to be converted, wherein the PLSQL code text carries a loop block statement object, a cursor loop fetch statement object, and an exit when statement object; a translation unit, configured to translate the PLSQL code text and generate a syntax tree corresponding to the PLSQL code text, wherein the syntax tree includes a loop stmt node corresponding to the loop block statement object, a fetch stmt node corresponding to the cursor loop fetch statement object, and an exit stmt node corresponding to the exit when statement object; a traversal unit, configured to traverse the nodes in the syntax tree and determine that a node position order of the syntax tree is as follows: a first child node of the loop stmt node is the fetch stmt node, and a second child node of the loop stmt node is the exit stmt node; or, to traverse the nodes in the syntax tree and determine that a node position order of the syntax tree is as follows: a first child node of the loop stmt node is the exit stmt node, and the fetch stmt node is located before the loop stmt node; an identification unit configured to, when the node position sequence is that the first child node of the loop stmt node is the fetch stmt node and the second child node of the loop stmt node is the exit stmt node, perform identification processing on the PLSQL code text to obtain a scenario type of the PLSQL code text: the cursor loop fetch statement object and the exit when statement object are both located in the loop block statement object; or, when the node position sequence is that the first child node of the loop stmt node is the exit stmt node and the fetch stmt node is located before the loop stmt node, perform identification processing on the PLSQL code text to obtain a scenario type of the PLSQL code text: the cursor loop fetch statement object is located before the loop block statement object and the exit when statement objects are both located in the loop block statement object; The conversion unit is used to convert the PLSQL code text according to the scenario type to obtain a target code text.

5. An electronic device, characterized in that: include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method for converting PLSQL code according to any one of claims 1 to 3 when executing the computer program.

6. A computer-readable storage medium, characterized in that Computer-executable instructions are stored, and the computer-executable instructions are used to execute the method for converting PLSQL code according to any one of claims 1 to 3.

Citation Information

Patent Citations

  • JAVA conversion method and device of SQL codes, computer equipment and storage medium

    CN112363727A

  • Programming language conversion method and device thereof

    CN113504900A