Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 290 additions & 0 deletions ojp-grpc-client-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
# OJP gRPC Go Client (Application Layout)

This folder contains a Go application client for OJP.
It connects to `ojp-server` over gRPC and runs a simple CRUD flow.

## Folder Structure

```text
ojp-grpc-client-go/
cmd/ojp-grpc-client/ # executable entrypoint (main package)
internal/client/ # client-side connection/load-balancing helpers
internal/gen/ # generated protobuf/gRPC Go stubs
scripts/ # integration test runners
testdata/connection.csv # test/demo CSV lines (not required in production)
testdata/postgres/init/ # postgres18 compose init SQL
testdata/mysql/init/ # mysql compose init SQL
testdata/mariadb/init/ # mariadb compose init SQL
docker-compose.postgres18.yml
docker-compose.mysql.yml
docker-compose.mariadb.yml
go.mod
go.sum
```

## Configuration

Primary (normal use case):

- `OJP_JDBC_LINE`

Format:

```text
jdbc:ojp[host:port]_backendJdbcUrl,user,password
```

Example:

```text
jdbc:ojp[localhost:1059]_postgresql://localhost:5432/defaultdb,testuser,testpassword
```

## What `main` Does

1. Parses connection values from env (`addr`, backend JDBC URL, user, password).
2. Opens gRPC connection to OJP server.
3. Runs CRUD on table `demo`:
- `CREATE TABLE IF NOT EXISTS`
- insert
- read
- update
- read
- delete
- read

## Run the Client

From `ojp-grpc-client-go`:

```bash
$env:OJP_JDBC_LINE='jdbc:ojp[localhost:1059]_h2:~/test,sa,'
go run ./cmd/ojp-grpc-client
```

Expected output:

```text
READ after CREATE/INSERT:
opResult: type=RESULT_SET_DATA uuid=...
READ after UPDATE:
opResult: type=RESULT_SET_DATA uuid=...
READ after DELETE:
opResult: type=RESULT_SET_DATA uuid=...
```

## Unit Tests

Unit tests are in:

- `cmd/ojp-grpc-client/main_test.go`

Covered functions:

- `parseOjpCsvLine`
- `selectCsvLine`
- `drainQueryStream` (with mock streaming client)

Run:

```bash
go test ./cmd/ojp-grpc-client
```

## End-to-End Test (OJP + H2)

### 1) Prepare OJP server drivers

```bash
cd ..\ojp-server
bash -lc "tr -d '\r' < download-drivers.sh | bash"
```

This creates `ojp-server/ojp-libs/` and downloads H2 + other open-source JDBC drivers.

### 2) Build project artifacts

From repository root (`ojp/`):

```bash
mvn clean install "-DskipTests=true" "-Dgpg.skip=true" "-Dcheckstyle.skip=true"
```

### 3) Start OJP server

From `ojp-server`:

```bash
java -Duser.timezone=UTC -Dojp.libs.path=.\ojp-libs -jar .\target\ojp-server-0.4.17-SNAPSHOT-shaded.jar
```

### 4) Run Go client against H2

From `ojp-grpc-client-go`:

```bash
$env:OJP_JDBC_LINE='jdbc:ojp[localhost:1059]_h2:~/test,sa,'
go run ./cmd/ojp-grpc-client
```

If you want to test using CSV test data:

```bash
$env:OJP_JDBC_CSV = Get-Content -Raw -Path .\testdata\connection.csv
$env:OJP_JDBC_CSV_INDEX='0'
Remove-Item Env:OJP_JDBC_LINE -ErrorAction SilentlyContinue
go run ./cmd/ojp-grpc-client
```

## End-to-End Test (PostgreSQL 18 via Docker Compose)

This repository now includes an automated PostgreSQL 18 + OJP + Go CRUD test setup.

### PowerShell (recommended on Windows)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-postgres18-crud-test.ps1 -SkipBuild
```

Options:

- Remove `-SkipBuild` to run Maven install before the test.

What this script does:

1. Starts `postgres:18` using `docker-compose.postgres18.yml`
2. Waits for healthy Postgres
3. Ensures PostgreSQL JDBC driver exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with `OJP_JDBC_LINE` targeting `testdb`

### Bash

From `ojp-grpc-client-go`:

```bash
bash scripts/run-postgres18-crud-test.sh -SkipBuild
```

The bash script is a standalone implementation that follows the same steps as the PowerShell version.

## End-to-End Test (MySQL via Docker Compose)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-mysql-crud-test.ps1 -SkipBuild
```

Or:

```bash
bash scripts/run-mysql-crud-test.sh -SkipBuild
```

This follows the same steps as the PostgreSQL script:

1. Starts `mysql:8.4` using `docker-compose.mysql.yml`
2. Waits for healthy MySQL
3. Ensures MySQL JDBC driver exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with MySQL `OJP_JDBC_LINE`

## End-to-End Test (MariaDB via Docker Compose)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-mariadb-crud-test.ps1 -SkipBuild
```

Or:

```bash
bash scripts/run-mariadb-crud-test.sh -SkipBuild
```

This follows the same steps as the PostgreSQL script:

1. Starts `mariadb:11` using `docker-compose.mariadb.yml`
2. Waits for healthy MariaDB
3. Ensures MariaDB JDBC driver exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with MariaDB `OJP_JDBC_LINE`

## End-to-End Test (DB2 via Docker Compose)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-db2-crud-test.ps1 -SkipBuild
```

Or:

```bash
bash scripts/run-db2-crud-test.sh -SkipBuild
```

This follows the same steps as the PostgreSQL script:

1. Starts `icr.io/db2_community/db2` using `docker-compose.db2.yml` with a dynamically assigned host port
2. Waits for healthy DB2 (up to 10 min, 5s intervals)
3. Ensures DB2 JDBC driver (`jcc-*.jar`) exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with DB2 `OJP_JDBC_LINE`

> **Note:** DB2 uses a stricter SQL dialect. The `demo` table creation requires `NOT NULL` on primary key columns —
> this is already handled in the Go clients CREATE TABLE statement.

## End-to-End Test (SQL Server via Docker Compose)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-mssql-crud-test.ps1 -SkipBuild
```

Or:

```bash
bash scripts/run-mssql-crud-test.sh -SkipBuild
```

This follows the same steps as the PostgreSQL script:

1. Starts `mcr.microsoft.com/mssql/server:2022-latest` using `docker-compose.mssql.yml` with a dynamically assigned host port
2. Waits for healthy MSSQL (up to 4 min, 3s intervals)
3. Ensures MSSQL JDBC driver (`mssql-jdbc-*.jar`) exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with SQL Server `OJP_JDBC_LINE`

## End-to-End Test (Oracle XE via Docker Compose)

From `ojp-grpc-client-go`:

```powershell
powershell -ExecutionPolicy Bypass -File .\scripts\run-oracle-crud-test.ps1 -SkipBuild
```

Or:

```bash
bash scripts/run-oracle-crud-test.sh -SkipBuild
```

This follows the same steps as the PostgreSQL script:

1. Starts `gvenzl/oracle-xe` using `docker-compose.oracle.yml` with a dynamically assigned host port
2. Waits for healthy Oracle (up to 6 min, 4s intervals)
3. Ensures Oracle JDBC driver (`ojdbc*.jar`) exists in `ojp-server/ojp-libs`
4. (Optional) builds OJP modules
5. Starts OJP server on dynamically selected free ports
6. Runs `go run ./cmd/ojp-grpc-client` with Oracle `OJP_JDBC_LINE`
Loading