From e6172a61fae0ea398d52e362fc1d1bde32748336 Mon Sep 17 00:00:00 2001 From: Torsten Friebe Date: Wed, 6 Apr 2022 21:47:24 +0200 Subject: [PATCH] #22 downgrade to java 11 and fixes dependencies, upgrades documentation for simpler installation for pl/java --- README.md | 28 ++++---- pom.xml | 67 +++++++++++-------- src/main/java/org/sep3tools/Launch.java | 6 +- .../java/org/sep3tools/PLJavaConnector.java | 19 +++--- src/main/java/org/sep3tools/PetroVisitor.java | 50 ++++++++------ .../deployment => test/resources}/sep3.ddr | 10 +-- 6 files changed, 100 insertions(+), 80 deletions(-) rename src/{main/resources/deployment => test/resources}/sep3.ddr (77%) diff --git a/README.md b/README.md index 906df04..eda6b91 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Download dictionary ("Woerterbuch") data from https://www.lbeg.niedersachsen.de/ - "Schlüssellisten mit Kürzeln und zugehörigem Klartext und Typisierungen" - "Wörterbuch" - "Juli 2021" -It's a ZIP file which contains the accdb and mdb files. +It's a ZIP file which contains the `accdb` and `mdb` files. Convert to PostgreSQL, e.g. using [mdbtools](https://github.com/mdbtools/mdbtools): @@ -30,7 +30,7 @@ $ mdb-schema -T Woerterbuch Woerterbuch_Austausch_Internet_accdb.accdb postgres $ mdb-export -D %F Woerterbuch_Austausch_Internet_accdb.accdb Woerterbuch > Woerterbuch.csv ``` -Create the "Schluesseltypen" and "Woerterbuch" tables in your PostgreSQL database, e.g. in their own "woerterbuch" schema: +Create the _"Schluesseltypen"_ and _"Woerterbuch"_ tables in your PostgreSQL database, e.g. in their own _"woerterbuch"_ schema: ``` sep3=# create schema woerterbuch; @@ -42,7 +42,7 @@ sep3=# set datestyle to 'SQL,MDY'; sep3=# \copy "Woerterbuch" from '/tmp/Woerterbuch.csv' CSV HEADER ``` -Test your conversion by e.g. retrieving all "Woerterbuch" table entries for the "PETRO" data field: +Test your conversion by e.g. retrieving all _"Woerterbuch"_ table entries for the _"PETRO"_ data field: ``` sep3=# select "Typ", "Langtext" as "Typbezeichnung", "Kuerzel", "Klartext" from "Woerterbuch" w join "Schluesseltypen" s on w."Typ" = s."Nebentypbez" where s."Datenfeld" = 'PETRO' order by "Typ", "Kuerzel"; @@ -97,9 +97,9 @@ the [PL/Java](https://tada.github.io/pljava) library to execute the parser via S Follow the [PL/Java installation guide](https://tada.github.io/pljava/install/install.html) and install a JDK 16 on the machine running the PostgreSQL database. -The installation steps for PostgreSQL 12 with OpenJDK 16 and PL/Java v1.6.4 on Ubuntu 20.04.3 LTS in a nutshell: +The installation steps for PostgreSQL 12 with OpenJDK 11 and PL/Java v1.6.4 on Ubuntu 20.04.3 LTS in a nutshell: ```shell -apt-get update && apt-get -yq install postgresql-server-dev-12 openjdk-16-jdk git gcc libssl-dev libkrb5-dev +apt-get update && apt-get -yq install postgresql-server-dev-12 openjdk-11-jdk git gcc libssl-dev libkrb5-dev git clone https://github.com/tada/pljava.git cd pljava git checkout tags/V1_6_4 @@ -107,6 +107,11 @@ mvn install sudo java -jar pljava-packaging/target/pljava-pg12.jar ``` +or install the latest PL/Java version using: +```shell +apt-get update && apt-get install postgresql-12-pljava +``` + ### Install SEP3-Tools in PostgreSQL Now PL/Java is installed and the SEP3-Tools library needs to be loaded into the database. Connect to the PostgreSQL database with @@ -118,22 +123,13 @@ CREATE EXTENSION pljava; SELECT sqlj.install_jar('file:////target/sep3-parser-0.0.1-SNAPSHOT-jar-with-dependencies.jar', 'sep3', true); SELECT sqlj.set_classpath('public', 'sep3'); CREATE OR REPLACE FUNCTION parseS3( \ - wb pg_catalog.varchar, st pg_catalog.varchar, s3code pg_catalog.varchar) \ - RETURNS pg_catalog.varchar \ - LANGUAGE java VOLATILE \ - AS 'java.lang.String=org.sep3tools.Launch.parseS3(java.lang.String, java.lang.String, java.lang.String)'; - - CREATE OR REPLACE FUNCTION parseS3( \ s3code pg_catalog.varchar) \ RETURNS pg_catalog.varchar \ LANGUAGE java VOLATILE \ AS 'java.lang.String=org.sep3tools.Launch.parseS3(java.lang.String)'; ``` -Verify the installation by executing the function: +Verify the installation by executing the function `parseS3()`: ```postgres-sql -#using the builtin hardcoded values -SELECT parseS3('','','^u'); -#using the database tables imported from the Access Database -SELECT parseS3('woerterbuch."Woerterbuch"','woerterbuch."Schluesseltypen"','^k'); +SELECT parseS3('^u'); ``` diff --git a/pom.xml b/pom.xml index 3923180..79485a9 100644 --- a/pom.xml +++ b/pom.xml @@ -14,9 +14,10 @@ UTF-8 - 16 - 4.9.2 - 2.17.1 + 11 + 1.6.4 + 4.9.3 + 2.17.2 @@ -94,7 +95,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.10.1 ${java.version} ${java.version} @@ -119,7 +120,7 @@ org.apache.maven.plugins maven-jar-plugin - 3.2.0 + 3.2.2 @@ -199,19 +200,16 @@ ${antlr.version} - org.slf4j - slf4j-api - 1.7.30 - - - org.apache.logging.log4j - log4j-slf4j-impl - ${log4j2.version} + org.postgresql + pljava-api + ${pljava.version} + provided - org.apache.logging.log4j - log4j-core - ${log4j2.version} + org.postgresql + postgresql + 42.3.3 + provided junit @@ -219,16 +217,31 @@ 4.13.2 test - - org.postgresql - pljava-api - 1.6.4 - - - org.postgresql - postgresql - 42.3.3 - - + + + + logging + + + org.slf4j + slf4j-api + 1.7.36 + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j2.version} + runtime + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + runtime + + + + + diff --git a/src/main/java/org/sep3tools/Launch.java b/src/main/java/org/sep3tools/Launch.java index 5f40f1d..a623b89 100644 --- a/src/main/java/org/sep3tools/Launch.java +++ b/src/main/java/org/sep3tools/Launch.java @@ -4,6 +4,7 @@ import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; +import org.postgresql.pljava.annotation.Function; import org.sep3tools.gen.*; public class Launch { @@ -25,8 +26,8 @@ else if (args.length == 1) { sep3String = args[0]; } else { - System.out.println("Aufruf mir folgenden Parametern:\n" - + "JDBC-URL DB-User DB-Pass Woerterbuch-Tabelle Schlüsseltypen-Tabelle SEP3-String\n\n" + System.out.println("Aufruf mit folgenden Parametern:\n" + + "[JDBC-URL] [DB-User] [DB-Passwort] [Woerterbuch-Tabelle] [Schlüsseltypen-Tabelle] \n\n" + "Beispiel für Parameter:\n" + "\"jdbc:postgresql://localhost/petroparser\" " + "\"petroDB\" " + "\"PetroPass\" " + "\"woerterbuch.\\\"\"Woerterbuch\\\"\" " + "\"woerterbuch.\\\"\"Schluesseltypen\\\"\" " @@ -37,6 +38,7 @@ else if (args.length == 1) { System.out.println(visit); } + @Function public static String parseS3(String s3String) { CharStream input = CharStreams.fromString(s3String); PetroGrammarLexer lexer = new PetroGrammarLexer(input); diff --git a/src/main/java/org/sep3tools/PLJavaConnector.java b/src/main/java/org/sep3tools/PLJavaConnector.java index f753d8e..f98a6a6 100644 --- a/src/main/java/org/sep3tools/PLJavaConnector.java +++ b/src/main/java/org/sep3tools/PLJavaConnector.java @@ -27,17 +27,14 @@ public static String getS3Name(String sep3Code) throws SQLException { + "where (s.\"Datenfeld\" = 'PETRO' OR s.\"Datenfeld\" = 'diverse') AND \"Kuerzel\"="; PreparedStatement stmt = conn.prepareStatement(query + "'" + sep3Code + "'"); - ResultSet rs = stmt.executeQuery(); - boolean validRS = rs.next(); - String ret = ""; - if (validRS) - ret = rs.getString(2); - - stmt.close(); - conn.close(); - - return (ret); - + try (ResultSet rs = stmt.executeQuery()) { + boolean validRS = rs.next(); + String result = ""; + if (validRS) { + result = rs.getString(2); + } + return result; + } } } diff --git a/src/main/java/org/sep3tools/PetroVisitor.java b/src/main/java/org/sep3tools/PetroVisitor.java index 5b8ba5b..aed48d6 100644 --- a/src/main/java/org/sep3tools/PetroVisitor.java +++ b/src/main/java/org/sep3tools/PetroVisitor.java @@ -1,13 +1,10 @@ package org.sep3tools; -//import org.slf4j.Logger; - import org.sep3tools.gen.*; import java.sql.*; import static java.util.Objects.isNull; -import static org.slf4j.LoggerFactory.getLogger; public class PetroVisitor extends PetroGrammarBaseVisitor { @@ -54,12 +51,16 @@ public String visitBestandteil(PetroGrammarParser.BestandteilContext ctx) { } } - return switch (boden) { - case "^u" -> "Schluffstein"; - case "^ms" -> "Mittelsandstein"; - case "^gs" -> "Grobsandstein"; - default -> boden; - }; + switch (boden) { + case "^u": + return "Schluffstein"; + case "^ms": + return "Mittelsandstein"; + case "^gs": + return "Grobsandstein"; + default: + return boden; + } } @Override @@ -78,17 +79,26 @@ public String visitAttr(PetroGrammarParser.AttrContext ctx) { } } } - return switch (attr) { - case "r2" -> "kantengerundet"; - case "r3" -> "mäßig gerundet"; - case "tw" -> "teilweise"; - case "lw" -> "lagenweise"; - case "gs" -> "grobsandig"; - case "t" -> "tonig"; - case "nf" -> "Nachfall"; - case "bei" -> "bei"; - default -> attr; - }; + switch (attr) { + case "r2": + return "kantengerundet"; + case "r3": + return "mäßig gerundet"; + case "tw": + return "teilweise"; + case "lw": + return "lagenweise"; + case "gs": + return "grobsandig"; + case "t": + return "tonig"; + case "nf": + return "Nachfall"; + case "bei": + return "bei"; + default: + return attr; + } } @Override diff --git a/src/main/resources/deployment/sep3.ddr b/src/test/resources/sep3.ddr similarity index 77% rename from src/main/resources/deployment/sep3.ddr rename to src/test/resources/sep3.ddr index fcd51f5..40450df 100644 --- a/src/main/resources/deployment/sep3.ddr +++ b/src/test/resources/sep3.ddr @@ -1,7 +1,8 @@ SQLActions[ ] = { "BEGIN INSTALL CREATE SCHEMA sep3; - BEGIN PostgreSQL SET search_path TO sep3,public END postgreSQL; + BEGIN PostgreSQL + SET search_path TO sep3,public END postgreSQL; CREATE FUNCTION sep3.java_getSystemProperty(varchar) RETURNS varchar @@ -12,10 +13,11 @@ SQLActions[ ] = { RETURNS varchar AS 'java.lang.String=org.sep3tools.Launch.parseS3(java.lang.String, java.lang.String, java.lang.String)' LANGUAGE java; - + END PostgreSQL; END INSTALL", - - "BEGIN REMOVE + "BEGIN REMOVE + BEGIN PostgreSQL DROP SCHEMA sep3 CASCADE; + END PostgreSQL; END REMOVE" } \ No newline at end of file