Background Image

BLOG

?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

Written by Youngjin Joo on 09/30/2021

 

CUBRID DBMS (hereinafter 'CUBRID') does not support PL/SQL.

 

If you want to continue your project by creating functions or subprograms with PL/SQL syntax in CUBRID, you need to convert them to Java Stored Function/Procedure (hereinafter 'Java SP').

 

Database developers, administrators, and engineers are often familiar with PL/SQL syntax but not with programming languages. In addition, application development depends very little on the DBMS used, but converting PL/SQL to Java SP seems difficult because it feels like you're developing a new system.

 

Therefore, while I am looking for an easy way to convert PL/SQL to Java SP, I found out about ANTLR. 

 

ANTLR is a tool for generating parsers.

With the help of contributors around the world, ANTLR supports grammar files for parsing various programming languages.  

 

The official website introduces ANTLR as follows.

 

"ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.  (https://www.antlr.org/ - What is ANTLR?)"

 

In this article, we will learn how to configure the ANTLR development environment and how to create parser classes from PL/SQL grammar files.

 

Now, let’s test the class that converts pre-developed PL/SQL to Java SP.

 

The ANTLR development environment can be configured using various IDE tools such as Intellij, NetBeans, Eclipse, Visual Studio Code, Visual Studio, and jEdit. In this article, we will use the Eclipse IDE tool. 

 

 

1. Installing the 'ANTLR 4 IDE' in Eclipse

 

To use ANTLR in Eclipse, you need to install ANTLR 4 IDE from 'Help > Eclipse Marketplace...'.

image1.png

 

 

After installing the ANTLR 4 IDE, create a project with 'General > ANTLR 4 Project'

image2.png

 

 

After creating the ANTLR project, select 'Project Facets > Java' in the project settings.

image3.png

 

 

2. Adding antlr-4.9-complete.jar file to project 'Java Build Path > Libraries'

 

Even though the ANTLR 4 IDE is installed, we still need antlr-4.9-complete.jar file to use ANTLR.

 

This file can be download from ANTLR official website, and it must be added to  'Java Build Path > Libraries'.

 

    - download : https://www.antlr.org/download.html

 

image4.png

 

image9.PNG

 

 

3. How to create parser classes from PL/SQL grammar files

 

Up to this point, the configuration of the ANTLR development environment has been completed.

To create PL/SQL parser classes with ANTLR, we need the PL/SQL grammar file.

 

The grammar file is supported by ANTLR and can be downloaded from GitHub (antlr/grammars-v4).

After creating the parser classes, we also need to download the necessary basic parser class files.

 

  - download: https://github.com/antlr/grammars-v4/tree/master/sql/plsql
  - download file:
    * java/PlSqlLexerBase.java
    * java/PlSqlParserBase.java
    * PlSqlLexer.g4
    * PlSqlParser.g4

 

When parsing a PL/SQL code, an error occurs if it is in lowercase letters. To resolve this issue, ANTLR asks you to capitalize the PL/SQL code before parsing.

 

You can bypass this problem by downloading the CaseChangingCharStream class and using it before parsing.

 

We are using the CaseChangingCharStream class before passing the PL/SQL code to the PlSqlLexerd class, which is shown in the main function. 

 

  - download:  https://github.com/antlr/antlr4/tree/master/doc/resources
  - download file: 
    * CaseChangingCharStream.java

 

image5.PNG

 

 

4. How to create parser classes from PL/SQL grammar file

 

If you add the downloaded grammar files to the ANTLR project and run 'Run AS > Generate ANTLR Recognizer', parser classes for parsing PL/SQL are created.

 

If these class files are in the Default Package state, they cannot be used as an Import when developing separate parser classes.

 

When 'Run AS > Generate ANTLR Recognizer' is executed, if the package setting is added to the Generate ANTLR Recognizer option, *.java files are created.

 

Simply add '-package <package name>' to 'Run As > External Tools Configurations... > ANTLR > Arguments'.

 

The package setting must be done with both grammar files (PlSqlLexer.g4, PlSqlParser.g4).

image6.png

 

When you run 'Run AS > Generate ANTLR Recognizer', the parser classes are created in the 'target > generated-sources > antlr4' directory even if they are bundled into a package.

 

To make Eclipse aware of these files as sources, you must add the directory 'target > generated-source > antlr4' to 'Java Build Path > Source'.

image10.PNG

 

image11.PNG

 

 

5. Parsing PL/SQL with the created parser classes

 

The MigrationPlsqlToJavaSP class reads the PL/SQL code in the compute_bonus.sql file and converts it to Java SP.

 

The PlSqlLexer and PlSqlParser classes are parser class files created by ANTLR using PL/SQL grammar files.

 

PL/SQL parsing starts with the code 'parser.sql_script();'.

 

By following the child class of the Sql_scriptContext class returned as a result of parsing, you can extract and process the data needed to create Java SP.

 

Lastly, the make() method, which is called by the MigrationPlsqlToJavaSP class, uses StringTemplate to create Java SP class file.

 

image7.png

 

6. Testing the class that converts pre-developed PL/SQL to Java SP

 

The following figure is the result of running PL/SQL in the compute_bonus.sql on Oracle. 

image8.png

 

By following the result of parsing the compute_bonus.sql file, we extract and process the data needed to create a Java SP.

 

image14.PNG

 

StringTempate, which provides template syntax, is used to make data to be parsed and extracted into Java SP.

 

The official website introduces StringTemplate as follows.

 

"StringTemplate is a java template engine (with ports for C#, Objective-C, JavaScript, Scala) for generating source code, web pages, emails, or any other formatted text output. (https://www.stringtemplate.org/ - What is StringTemplate?)"

 

image13.PNG

 

Below is the Java code converted to Java SP.

image12.PNG

 

The example table in Oracle was transferred to CUBRID for testing.

image16.PNG

 

It outputs the same result as when running PL/SQL in Oracle.

 

Last but not least...

 

So far, we can only convert the SELECT queries that are executed in the PL/SQL function.

 

The query can be executed from PL/SQL code or returning result value with just simple operations.

 

ANTLR grammar allows us to parse all these parts. However, there are parts that are difficult to convert to Java code, so we haven't been able to proceed yet.

 

In the future, I would like to make a tool that converts PL/SQL to Java SP easily by refining the part that converts PL/SQL to Java SP and adding parts that can only be parsed but cannot be converted.

 

Since ANLTR supports the grammar of various programming languages besides PL/SQL, I think it is a good tool.

 

 

 


 

Reference 

1. ANTLR
  - homepage: https://www.antlr.org/
  -document: https://github.com/antlr/antlr4/blob/master/doc/index.md
  - grammar files:
    * https://github.com/antlr/grammars-v4
    * https://github.com/antlr/grammars-v4/wiki
  - ANTLR IDE
    * https://www.antlr.org/tools.html
    * https://github.com/jknack/antlr4ide

 

2. StringTemplate
  - homepage: https://www.stringtemplate.org/

 

 

 


  1. Converting PL/SQL to CUBRID Java SP using ANTLR and StringTemplate

    Written by Youngjin Joo on 09/30/2021 CUBRID DBMS (hereinafter 'CUBRID') does not support PL/SQL. If you want to continue your project by creating functions or subprograms with PL/SQL syntax in CUBRID, you need to convert them to Java Stored Function/Procedure (hereinafter 'Java SP'). Database developers, administrators, and engineers are often familiar with PL/SQL syntax but not with programming languages. In addition, application development depends very little on the DBMS used, but converting PL/SQL to Java SP seems difficult because it feels like you're developing a new system. Therefore, while I am looking for an easy way to convert PL/SQL to Java SP, I found out about ANTLR. ANTLR is a tool for generating parsers. With the help of contributors around the world, ANT...
    Read More
  2. Contributing to Open Source Community/Project

      Written by Charis Chau on 06/23/2020      What is an open source project? To answer this, let us start with a burger! Imagine an open source project is a burger selling in a restaurant. Every day, the chef makes thousands of burgers that have the same quality by following the same recipe from the restaurant. One day, customer A comes to the burger place to try the burger, and he/she loves it! Therefore, customer A decides to ask the chef whether he/she can get the recipe. Here, if the restaurant is open source, they will be happy to share the recipe to customer A, vice versa.     After customer A gets the receipt, he/she decide to make the burger at home by him/herself! However, customer A is a meat lover and does not like onion that much, so he/she decide to change the recipe by taking o...
    Read More
  3. CUBRID's Development Culture: The Development Process and Improvements Behind

    Written by Hyung-Gyu, Ryoo on 07/16/2021   Foreword Hello, I am Hyung-Gyu Ryoo, I am working in CUBRID as a research engineer in the R&D department. In this post, I would like to introduce the development process of the open source project CUBRID and the efforts we have made to improve the process.   It has been almost two and a half years since I joined CUBRID.  During this period, as many great fellow developers have joined the CUBRID community, the R&D department of CUBRID has grown from one small team to three developments teams along with a QA team.   After I participated in the first major version release (CUBRID 11), I was able to look back to the release process and improve the development process with my fellow developers.     The Development Process of the Open Source Data...
    Read More
  4. CUBRID License Model

    Written by Charis Chau on 06/08/2020   Why Licenses Matter?   Open source licenses allow software to be freely used, modified, and shared. Choosing a DBMS with suitable licenses could save the development cost of your application or the Total Cost of Ownership (TCO) for your company. Choosing a DBMS without a proper license, you might find yourself situate in a legal grey area!     CUBRID Licenses   Unlike other open source DBMS vendors, CUBRID is solely under open source license instead of having a dual license in both commercial license and open source license. Which means that for you, it is not mandatory to purchase a license or annual subscription; company/organizational users can achieve the saving from Total Cost of Ownership (TCO).   Since CUBRID has been open source DBMS from 2008,...
    Read More
  5. CUBRID Internal: Storage Management (Disk Manager, File Manager)

    Written by Jaeeun, Kim on 08/11/2021 Introduction Database, just as its name implies, it needs spaces to store data. CUBRID, the open source DBMS that operates for the operating system allocates as much space as needed from the operating system and uses it efficiently as needed. In this article, we will talk about how CUBRID internally manages the storage to store data in the persistent storage device. Through this article, we hope developers can access the open source database CUBRID more easily. - The content of this article is based on version 10.2.0-7094ba. (However, it seems to be no difference in the latest develop branch, 11.0.0-c83e33. ) CUBRID Storage Management The CUBRID server has multiple modules that operate and manage data complexly and sophisticatedly. Among them, there are ...
    Read More
Board Pagination Prev 1 2 3 4 5 Next
/ 5

Join the CUBRID Project on