Skip to content

Postgres Lexer and Parser. Java library for parse/deparse/fingerprint/Lexical participle Postgres SQL statements

License

Notifications You must be signed in to change notification settings

jiangtao69039/pg_parser_java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d3b6d9e · Dec 10, 2022

History

11 Commits
Dec 3, 2022
Dec 10, 2022
Dec 3, 2022
Dec 6, 2022
Dec 3, 2022
Dec 3, 2022
Dec 3, 2022
Dec 3, 2022
Dec 6, 2022

Repository files navigation

pg_parser_java

Java library for accessing the PostgreSQL parser outside of the server.

This library uses the base library libpg_query

Required tools before install

  • wget
  • make
  • gcc
  • maven
  • java 8+

This was built under the following conditions.

  1. open jdk 8u302
  2. GNU Make 4.3
  3. gcc 12.2.0
  4. maven 3.8.6
  5. OS: arch linux x86_64

In addition: It's works fine on openjdk-8-slim-buster docker image. But you must build the libpg_query.so in the image to avoid GBLIC version differences.

This was tested almost not at all. Good luck!

Installation

mvn clean package -Pdownload -PbuildLib

-Pdownload: download libpg_query source code into target/downloads/
-PbuildLib: generate libpg_query.so into src/main/resource/libpg_query.so

Test

mvn test 

Usage: Split multiple sql statements

import com.github.ttttz.pgParser.split.PgQuerySplitResult;
import com.github.ttttz.pgParser.split.PgQuerySplitStmt;
import com.sun.jna.ptr.PointerByReference;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class PgLibTest {

    @Test
    public void test_split(){
        String input = "select * from t;select * from t;";
        PgQuerySplitResult.ByValue byValue = PgQueryLibInterface
                .INSTANCE
                .pg_query_split_with_parser(input);
        PointerByReference stmts = byValue.stmts;
        int pointIndex = 0;
        int pointSize = 8; //bytes
        for(int i=0;i<byValue.n_stmts;i++){
            pointIndex = i*pointSize;
            PgQuerySplitStmt.ByReference pgQuerySplitStmt = new PgQuerySplitStmt.ByReference(stmts.getPointer().getPointer(pointIndex));
            pgQuerySplitStmt.read();
            String split = input.substring(pgQuerySplitStmt.stmt_location, pgQuerySplitStmt.stmt_location+ pgQuerySplitStmt.stmt_len);
            assertEquals("select * from t",split);
        }
        PgQueryLibInterface
                .INSTANCE.pg_query_free_split_result(byValue);
    }
}

Authors

Change log

  • 2022-12-02: add support of pg_query_split_with_parser
  • 2022-12-02: add support of pg_query_split_with_scanner
  • 2022-12-02: add support of pg_query_free_split_result

About

Postgres Lexer and Parser. Java library for parse/deparse/fingerprint/Lexical participle Postgres SQL statements

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages