From 3a93fee88dc52d29a77b9b1c907860c750976b3e Mon Sep 17 00:00:00 2001
From: Dmitriy Koltsov <dkoltsov@picodata.io>
Date: Fri, 7 Jun 2024 20:29:07 +0300
Subject: [PATCH] add example for PG driver usage

---
 .gitignore                                    |   1 +
 postgres-java-example/.DS_Store               | Bin 0 -> 6148 bytes
 postgres-java-example/README.md               |  14 +++++
 postgres-java-example/before.sql              |   9 +++
 postgres-java-example/picodata_config.yaml    |  26 ++++++++
 postgres-java-example/pom.xml                 |  40 ++++++++++++
 .../java/com/example/PostgresExample.java     |  58 ++++++++++++++++++
 .../src/test/java/com/example/AppTest.java    |  38 ++++++++++++
 8 files changed, 186 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 postgres-java-example/.DS_Store
 create mode 100644 postgres-java-example/README.md
 create mode 100644 postgres-java-example/before.sql
 create mode 100644 postgres-java-example/picodata_config.yaml
 create mode 100644 postgres-java-example/pom.xml
 create mode 100644 postgres-java-example/src/main/java/com/example/PostgresExample.java
 create mode 100644 postgres-java-example/src/test/java/com/example/AppTest.java

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..53f7426
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+**/target/
\ No newline at end of file
diff --git a/postgres-java-example/.DS_Store b/postgres-java-example/.DS_Store
new file mode 100644
index 0000000000000000000000000000000000000000..4c02d7b8cda2b98142ac81ce2156c20291a3c306
GIT binary patch
literal 6148
zcmeHKJ8A<l5Pg%lFr;x4NT~~?$PI+CPv8s0iI+fFvmxsQms|N<Ia=O)nq`y5;Kt0r
z=t-k_5_-kX4ggtx?Vf-IfH_?eCmp7y`|1<BiHH#CJmU?YXwc&k@00Am1IoR}2RgD2
zBmNfO*z%+XN8+A2Et*~b+P-vMtF*LKAQeajQh`(;6*xnID5CSbGq_1f1yX_QRzSZG
zg|1iw2S@vKaIhADIAhq1bL%CDMFYedI5;vw6Q>fLD$!zy(-|+3R|5w}r$eIokhoc*
zLlL{3=NC(dRL2}sfmC3uz^yMATL0JdfBOG1DOagLDsWW_$ZWM)E%~IVt&_)Ttu6Ft
vy5{`Q)i^f_2P?-!E5}@DIexGt&nr6TehnNPjn25yiTN|2x}>E7msa2x=0O>q

literal 0
HcmV?d00001

diff --git a/postgres-java-example/README.md b/postgres-java-example/README.md
new file mode 100644
index 0000000..bba403a
--- /dev/null
+++ b/postgres-java-example/README.md
@@ -0,0 +1,14 @@
+## How to run
+1. Install picodata 
+2. Run picodata with the following command:
+```bash
+picodata run --config picodata_config.yaml 
+```
+3. Prepare data schema for example app with:
+```bash
+cat before.sql | picodata admin ./admin.sock
+```
+4. Run example app with 
+```bash
+mvn compile && mvn exec:java -Dexec.mainClass="com.example.PostgresExample"
+```
\ No newline at end of file
diff --git a/postgres-java-example/before.sql b/postgres-java-example/before.sql
new file mode 100644
index 0000000..7825f10
--- /dev/null
+++ b/postgres-java-example/before.sql
@@ -0,0 +1,9 @@
+CREATE TABLE "warehouse" (
+            id INTEGER NOT NULL,
+            item TEXT NOT NULL,
+            PRIMARY KEY (id))
+USING memtx DISTRIBUTED BY (id)
+OPTION (TIMEOUT = 3.0);
+CREATE USER "randy" WITH PASSWORD 'P@ssw0rd' USING md5 OPTION (TIMEOUT = 3.0);
+GRANT WRITE ON TABLE "warehouse" to "randy";
+GRANT READ ON TABLE "warehouse" to "randy";
\ No newline at end of file
diff --git a/postgres-java-example/picodata_config.yaml b/postgres-java-example/picodata_config.yaml
new file mode 100644
index 0000000..572f868
--- /dev/null
+++ b/postgres-java-example/picodata_config.yaml
@@ -0,0 +1,26 @@
+cluster:
+  cluster_id: demo 
+  tier:
+    default:
+      replication_factor: 1 
+      can_vote: true 
+  default_replication_factor: 1 
+instance:
+  data_dir: .
+  tier: default 
+  peer: 
+  - 127.0.0.1:3301
+  listen: 127.0.0.1:3301 
+  advertise_address: 127.0.0.1:3301 
+  http_listen: 127.0.0.1:8081 
+  admin_socket: ./admin.sock 
+  plugin_dir: null 
+  audit: null 
+  shredding: false 
+  log:
+    level: info 
+    destination: null 
+    format: plain 
+  pg:
+    listen: 127.0.0.1:5432 
+    ssl: false 
diff --git a/postgres-java-example/pom.xml b/postgres-java-example/pom.xml
new file mode 100644
index 0000000..e55cc48
--- /dev/null
+++ b/postgres-java-example/pom.xml
@@ -0,0 +1,40 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.example</groupId>
+  <artifactId>postgres-java-example</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>postgres-java-example</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>org.postgresql</groupId>
+      <artifactId>postgresql</artifactId>
+      <version>42.5.3</version>
+    </dependency>
+  </dependencies>
+  <build>
+  <plugins>
+    <plugin>
+      <groupId>org.apache.maven.plugins</groupId>
+      <artifactId>maven-compiler-plugin</artifactId>
+      <version>3.8.0</version>
+      <configuration>
+        <release>21</release>
+        <compilerArgs>
+          --enable-preview
+        </compilerArgs>
+      </configuration>
+    </plugin>
+    <plugin>
+      <groupId>org.apache.maven.plugins</groupId>
+      <artifactId>maven-surefire-plugin</artifactId>
+      <version>3.0.0-M3</version>
+      <configuration>
+        <argLine>--enable-preview</argLine>
+      </configuration>
+    </plugin>
+  </plugins>
+ </build>
+</project>
diff --git a/postgres-java-example/src/main/java/com/example/PostgresExample.java b/postgres-java-example/src/main/java/com/example/PostgresExample.java
new file mode 100644
index 0000000..406cb92
--- /dev/null
+++ b/postgres-java-example/src/main/java/com/example/PostgresExample.java
@@ -0,0 +1,58 @@
+package com.example;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+public class PostgresExample {
+    public static void main(String[] args) {
+        var props = new Properties();
+        props.setProperty("user", "randy");
+        props.setProperty("password", "P@ssw0rd");
+        // props.setProperty("dbname", "postgres");
+
+        var connstr = "jdbc:postgresql://localhost:5432/";
+        try (Connection conn = DriverManager.getConnection(connstr, props)) {
+            System.out.println("Connected to the PostgreSQL server successfully.");
+
+            var DELETE_QUERY = """
+                DELETE FROM "warehouse";
+            """;
+            var stmt = conn.prepareStatement(DELETE_QUERY);
+            var deleteRows = stmt.executeUpdate();
+            System.out.println(String.format("%d rows was deleted", deleteRows));
+
+
+            var INSERT_QUERY = """
+                INSERT INTO \"warehouse\" VALUES (?, ?)
+            """;
+            stmt = conn.prepareStatement(INSERT_QUERY);
+            stmt.setInt(1, 1);
+            stmt.setString(2, "Dima");
+            var insertedRows = stmt.executeUpdate();
+            System.out.println(String.format("%d rows was inserted", insertedRows));
+
+            var SELECT_QUERY = """
+                SELECT * FROM "warehouse" WHERE id = ?;
+            """;
+            stmt = conn.prepareStatement(SELECT_QUERY);
+            stmt.setInt(1, 1);
+            var res = stmt.executeQuery();
+            while (res.next()) {
+                System.out.println(
+                    String.format(
+                        "Id is %d, name is %s",
+                        res.getInt(1),
+                        res.getString(2)
+                    )
+                );
+            }
+
+
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+}
+
diff --git a/postgres-java-example/src/test/java/com/example/AppTest.java b/postgres-java-example/src/test/java/com/example/AppTest.java
new file mode 100644
index 0000000..474710c
--- /dev/null
+++ b/postgres-java-example/src/test/java/com/example/AppTest.java
@@ -0,0 +1,38 @@
+package com.example;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}
-- 
GitLab