Skip to content

Commit

Permalink
Merge pull request #72 from daneshk/h2-support
Browse files Browse the repository at this point in the history
Add H2 database support for the persist SQL
  • Loading branch information
daneshk committed Jul 8, 2024
2 parents b162813 + ff1938f commit 453b5fd
Show file tree
Hide file tree
Showing 30 changed files with 4,563 additions and 117 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerinax"
name = "persist.sql"
version = "1.3.0"
version = "1.4.0"
authors = ["Ballerina"]
keywords = ["persist", "sql", "mysql", "mssql", "sql-server"]
repository = "https://github.com/ballerina-platform/module-ballerinax-persist.sql"
Expand All @@ -15,8 +15,8 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.sql-native"
version = "1.3.0"
path = "../native/build/libs/persist.sql-native-1.3.0.jar"
version = "1.4.0"
path = "../native/build/libs/persist.sql-native-1.4.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id = "persist.sql-compiler-plugin"
class = "io.ballerina.stdlib.persist.sql.compiler.PersistSqlCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/persist.sql-compiler-plugin-1.3.0.jar"
path = "../compiler-plugin/build/libs/persist.sql-compiler-plugin-1.4.0-SNAPSHOT.jar"

[[dependency]]
path = "./lib/persist-native-1.3.0.jar"
Expand Down
35 changes: 31 additions & 4 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.7.0"
version = "2.7.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -66,7 +66,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.11.0"
version = "2.11.2"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "auth"},
Expand Down Expand Up @@ -294,7 +294,7 @@ modules = [
[[package]]
org = "ballerina"
name = "sql"
version = "1.13.0"
version = "1.13.2"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down Expand Up @@ -383,6 +383,31 @@ modules = [
{org = "ballerinai", packageName = "transaction", moduleName = "transaction"}
]

[[package]]
org = "ballerinax"
name = "h2.driver"
version = "1.1.0"
scope = "testOnly"
modules = [
{org = "ballerinax", packageName = "h2.driver", moduleName = "h2.driver"}
]

[[package]]
org = "ballerinax"
name = "java.jdbc"
version = "1.11.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"},
{org = "ballerina", name = "sql"},
{org = "ballerina", name = "time"}
]
modules = [
{org = "ballerinax", packageName = "java.jdbc", moduleName = "java.jdbc"}
]

[[package]]
org = "ballerinax"
name = "mssql"
Expand Down Expand Up @@ -435,7 +460,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "persist.sql"
version = "1.3.0"
version = "1.4.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "log"},
Expand All @@ -444,6 +469,8 @@ dependencies = [
{org = "ballerina", name = "test"},
{org = "ballerina", name = "time"},
{org = "ballerinai", name = "transaction"},
{org = "ballerinax", name = "h2.driver"},
{org = "ballerinax", name = "java.jdbc"},
{org = "ballerinax", name = "mssql"},
{org = "ballerinax", name = "mssql.driver"},
{org = "ballerinax", name = "mysql"},
Expand Down
6 changes: 3 additions & 3 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This package provides relational database support for the `bal persist` feature, which provides functionality to store and query data from a relational database through a data model instead of writing SQL.

Currently, this package supports MySQL, MSSQL, and PostgreSQL databases. However, we are also planning to add support for other relational databases such as Oracle.
Currently, this package supports MySQL, MSSQL, H2 and PostgreSQL databases. However, we are also planning to add support for other relational databases such as Oracle.

## How to use with `bal persist`

Expand All @@ -13,7 +13,7 @@ By default, `bal persist` utilizes the in-memory data store. Therefore, you must
1. Initialize `bal persist` and integrate to `bal build` using the following command,

```
$ bal persist add --datastore [mysql/mssql/postgresql] --module <module_name>
$ bal persist add --datastore [mysql/mssql/postgresql/h2] --module <module_name>
```

2. After defining the entities, build the application using the following command,
Expand All @@ -33,7 +33,7 @@ By default, `bal persist` utilizes the in-memory data store. Therefore, you must
2. Generate the persist client using the following command,

```
$ bal persist generate --datastore [mysql/mssql/postgresql] --module <module_name>
$ bal persist generate --datastore [mysql/mssql/postgresql/h2] --module <module_name>
```

## Supported Ballerina Types
Expand Down
100 changes: 50 additions & 50 deletions ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -204,65 +204,65 @@ task stopMySQLTestDockerContainer() {
}

task createMSSQLTestDockerImage(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c', "docker build -f $project.projectDir/tests/resources/mssql/Dockerfile -t ballerina-persist-mssql" +
" -q $project.projectDir/tests/resources/mssql/"
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
sleep(10 * 1000)
}
}
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c', "docker build -f $project.projectDir/tests/resources/mssql/Dockerfile -t ballerina-persist-mssql" +
" -q $project.projectDir/tests/resources/mssql/"
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
sleep(10 * 1000)
}
}
}

def checkMSSQLTestDockerContainerStatus(containerName) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
return exec {
commandLine 'sh', '-c',
"docker exec ${containerName} /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Test123#"
}.exitValue
} catch (all) {
return 1;
}
}
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
return exec {
commandLine 'sh', '-c',
"docker exec ${containerName} /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Test123#"
}.exitValue
} catch (all) {
return 1;
}
}
}

task startMSSQLTestDockerContainer(type: Exec) {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c',
"docker run --rm -d --name ballerina-persist-mssql -e ACCEPT_EULA=1 -e SA_PASSWORD=Test123# -p 1433:1433 -d ballerina-persist-mssql"
def healthCheck = 1;
def counter = 0;
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
while (healthCheck != 0 && counter < 12) {
sleep(5 * 1000)
healthCheck = checkMSSQLTestDockerContainerStatus("ballerina-persist-mssql")
counter = counter + 1;
}
if (healthCheck != 0) {
throw new GradleException("Docker container 'ballerina-persist-mssql' health test exceeded timeout!")
}
}
}
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
def standardOutput = new ByteArrayOutputStream()
commandLine 'sh', '-c',
"docker run --rm -d --name ballerina-persist-mssql -e ACCEPT_EULA=1 -e SA_PASSWORD=Test123# -p 1433:1433 -d ballerina-persist-mssql"
def healthCheck = 1;
def counter = 0;
doLast {
checkExecResult(executionResult, 'Error', standardOutput)
while (healthCheck != 0 && counter < 12) {
sleep(5 * 1000)
healthCheck = checkMSSQLTestDockerContainerStatus("ballerina-persist-mssql")
counter = counter + 1;
}
if (healthCheck != 0) {
throw new GradleException("Docker container 'ballerina-persist-mssql' health test exceeded timeout!")
}
}
}
}

task stopMSSQLTestDockerContainer() {
doLast {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine 'sh', '-c', "docker stop ballerina-persist-mssql"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopTestDockerContainer task")
}
}
}
doLast {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
try {
def stdOut = new ByteArrayOutputStream()
exec {
commandLine 'sh', '-c', "docker stop ballerina-persist-mssql"
standardOutput = stdOut
}
} catch (all) {
println("Process can safely ignore stopTestDockerContainer task")
}
}
}
}

task createPostgreSQLTestDockerImage(type: Exec) {
Expand Down
9 changes: 9 additions & 0 deletions ballerina/constants.bal
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ public final DataSourceSpecifics & readonly POSTGRESQL_SPECIFICS = {
duplicateKeyStartIndicator: "Detail: Key ",
duplicateKeyEndIndicator: " already exists."
};

public final DataSourceSpecifics & readonly H2_SPECIFICS = {
quoteOpen: "\"",
quoteClose: "\"",
constraintViolationErrorMessage: "Referential integrity constraint violation",
duplicateEntryErrorMessage: "Unique index or primary key violation",
duplicateKeyStartIndicator: "Unique index or primary key violation: \"",
duplicateKeyEndIndicator: " ON "
};
5 changes: 5 additions & 0 deletions ballerina/tests/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ password="postgres"
host="localhost"
port=5432
database="test"

[h2]
url="jdbc:h2:./build/test"
user="sa"
password=""
Loading

0 comments on commit 453b5fd

Please sign in to comment.