Skip to content

Commit

Permalink
Merge pull request #289 from daneshk/h2_support
Browse files Browse the repository at this point in the history
Add H2 database to the persist model validation
  • Loading branch information
daneshk authored Jul 9, 2024
2 parents 982d5e0 + ec1615b commit fa38a75
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 56 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 = "ballerina"
name = "persist"
version = "1.3.0"
version = "1.4.0"
authors = ["Ballerina"]
keywords = ["persist"]
repository = "https://github.com/ballerina-platform/module-ballerina-persist"
Expand All @@ -15,5 +15,5 @@ graalvmCompatible = true
[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist-native"
version = "1.3.0"
path = "../native/build/libs/persist-native-1.3.0.jar"
version = "1.4.0"
path = "../native/build/libs/persist-native-1.4.0-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "persist-compiler-plugin"
class = "io.ballerina.stdlib.persist.compiler.PersistCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/persist-compiler-plugin-1.3.0.jar"
path = "../compiler-plugin/build/libs/persist-compiler-plugin-1.4.0-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.0"
distribution-version = "2201.10.0-20240529-103800-249ab1b0"

[[package]]
org = "ballerina"
Expand All @@ -18,7 +18,7 @@ modules = [
[[package]]
org = "ballerina"
name = "persist"
version = "1.3.0"
version = "1.4.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- [Added compiler plugin validations for H2 as a datasource](https://github.com/ballerina-platform/ballerina-library/issues/5715)


## [1.3.0] - 2024-05-03

### Fixed
- [Fixed the persist compiler crash when no persist configurations are found](https://github.com/ballerina-platform/ballerina-library/issues/6187)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,50 @@ public void validateEntityFieldTypeForPostgresql() {
);
}

@Test
public void validateEntityFieldTypeForH2() {
List<Diagnostic> diagnostics = getErrorDiagnostics("project_11", "field-types.bal", 10);
testDiagnostic(
diagnostics,
new String[]{
PERSIST_306.getCode(),
PERSIST_305.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
PERSIST_305.getCode(),
PERSIST_305.getCode(),
PERSIST_306.getCode(),
PERSIST_305.getCode(),
PERSIST_306.getCode(),
PERSIST_306.getCode(),
},
new String[]{
"an entity does not support boolean array field type",
"an entity does not support json-typed field",
"an entity does not support json array field type",
"an entity does not support time:Civil array field type",
"an entity does not support union-typed field",
"an entity does not support error-typed field",
"an entity does not support error array field type",
"an entity does not support jdbc:Client-typed field",
"an entity does not support jdbc:Client array field type",
"an entity does not support enum array field type"
},
new String[]{
"(18:4,18:13)",
"(20:4,20:8)",
"(21:4,21:10)",
"(24:4,24:16)",
"(25:4,25:21)",
"(27:4,27:9)",
"(28:4,28:11)",
"(30:4,30:15)",
"(31:4,31:17)",
"(34:4,34:12)"
}
);
}

@Test
public void validateEntityFieldTypeForGoogleSheets() {
List<Diagnostic> diagnostics = getErrorDiagnostics("project_3", "field-types.bal", 12);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
org = "root"
name = "project_2"
version = "0.1.0"

[[tool.persist]]
options.datastore = "h2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ballerina/time;
import ballerinax/java.jdbc;
import ballerina/persist as _;

public enum Gender {
M,
F
}

public type MedicalNeed record {|
readonly int needId;
boolean booleanTest;
int itemId;
float floatTest;
decimal decimalTest;
string beneficiaryId;
byte[] beneficiaryIdByteArray;

boolean[] booleanArray;

json jsonTest;
json[] jsonArray;

time:Civil period;
time:Civil[] periodArray;
time:Civil|string unionType;

error errorType;
error[] errorArrayType;

jdbc:Client clientType;
jdbc:Client[] clientArrayType;

Gender gender;
Gender[] genderArray;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static final class Datastores {
public static final String IN_MEMORY = "inmemory";
public static final String GOOGLE_SHEETS = "googlesheets";
public static final String REDIS = "redis";
public static final String H2 = "h2";

private Datastores() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,68 +131,50 @@ public static boolean isValidSimpleType(String type, String datastore) {
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlType(type);
case Constants.Datastores.MSSQL:
return isValidMssqlType(type);
case Constants.Datastores.POSTGRESQL:
return isValidPostgresqlType(type);
case Constants.Datastores.IN_MEMORY:
return isValidInMemoryType(type);
case Constants.Datastores.GOOGLE_SHEETS:
return isValidGoogleSheetsType(type);
case Constants.Datastores.REDIS:
return isValidRedisType(type);
default:
return false;
}
return switch (datastore) {
case Constants.Datastores.MYSQL -> isValidMysqlType(type);
case Constants.Datastores.MSSQL -> isValidMssqlType(type);
case Constants.Datastores.POSTGRESQL -> isValidPostgresqlType(type);
case Constants.Datastores.H2 -> isValidH2Type(type);
case Constants.Datastores.IN_MEMORY -> isValidInMemoryType(type);
case Constants.Datastores.GOOGLE_SHEETS -> isValidGoogleSheetsType(type);
case Constants.Datastores.REDIS -> isValidRedisType(type);
default -> false;
};
}

public static boolean isValidArrayType(String type, String datastore) {
// If the datastore is null(ex: before executing the generate command), ignore the data type validation.
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlArrayType(type);
case Constants.Datastores.MSSQL:
return isValidMssqlArrayType(type);
case Constants.Datastores.POSTGRESQL:
return isValidPostgresqlArrayType(type);
case Constants.Datastores.IN_MEMORY:
return isValidInMemoryArrayType(type);
case Constants.Datastores.GOOGLE_SHEETS:
return isValidGoogleSheetsArrayType(type);
case Constants.Datastores.REDIS:
return isValidRedisArrayType(type);
default:
return false;
}
return switch (datastore) {
case Constants.Datastores.MYSQL -> isValidMysqlArrayType(type);
case Constants.Datastores.MSSQL -> isValidMssqlArrayType(type);
case Constants.Datastores.POSTGRESQL -> isValidPostgresqlArrayType(type);
case Constants.Datastores.H2 -> isValidH2ArrayType(type);
case Constants.Datastores.IN_MEMORY -> isValidInMemoryArrayType(type);
case Constants.Datastores.GOOGLE_SHEETS -> isValidGoogleSheetsArrayType(type);
case Constants.Datastores.REDIS -> isValidRedisArrayType(type);
default -> false;
};
}

public static boolean isValidImportedType(String modulePrefix, String identifier, String datastore) {
// If the datastore is null(ex: before executing the generate command), ignore the data type validation.
if (null == datastore) {
return true;
}
switch (datastore) {
case Constants.Datastores.MYSQL:
return isValidMysqlImportedType(modulePrefix, identifier);
case Constants.Datastores.MSSQL:
return isValidMssqlImportedType(modulePrefix, identifier);
case Constants.Datastores.POSTGRESQL:
return isValidPostgresqlImportedType(modulePrefix, identifier);
case Constants.Datastores.IN_MEMORY:
return isValidInMemoryImportedType(modulePrefix, identifier);
case Constants.Datastores.GOOGLE_SHEETS:
return isValidGoogleSheetsImportedType(modulePrefix, identifier);
case Constants.Datastores.REDIS:
return isValidRedisImportedType(modulePrefix, identifier);
default:
return false;
}
return switch (datastore) {
case Constants.Datastores.MYSQL -> isValidMysqlImportedType(modulePrefix, identifier);
case Constants.Datastores.MSSQL -> isValidMssqlImportedType(modulePrefix, identifier);
case Constants.Datastores.POSTGRESQL -> isValidPostgresqlImportedType(modulePrefix, identifier);
case Constants.Datastores.H2 -> isValidH2ImportedType(modulePrefix, identifier);
case Constants.Datastores.IN_MEMORY -> isValidInMemoryImportedType(modulePrefix, identifier);
case Constants.Datastores.GOOGLE_SHEETS -> isValidGoogleSheetsImportedType(modulePrefix, identifier);
case Constants.Datastores.REDIS -> isValidRedisImportedType(modulePrefix, identifier);
default -> false;
};
}

public static boolean isValidMysqlType(String type) {
Expand Down Expand Up @@ -237,6 +219,13 @@ public static boolean isValidPostgresqlType(String type) {
}
}

private static boolean isValidH2Type(String type) {
return switch (type) {
case INT, BOOLEAN, DECIMAL, FLOAT, STRING, ENUM -> true;
default -> false;
};
}

public static boolean isValidInMemoryType(String type) {
return true;
}
Expand Down Expand Up @@ -296,6 +285,10 @@ public static boolean isValidPostgresqlArrayType(String type) {
}
}

private static boolean isValidH2ArrayType(String type) {
return type.equals(BYTE);
}

public static boolean isValidInMemoryArrayType(String type) {
return true;
}
Expand Down Expand Up @@ -353,6 +346,16 @@ public static boolean isValidPostgresqlImportedType(String modulePrefix, String
}
}

private static boolean isValidH2ImportedType(String modulePrefix, String identifier) {
if (!modulePrefix.equals(TIME_MODULE)) {
return false;
}
return switch (identifier) {
case DATE, TIME_OF_DAY, UTC, CIVIL -> true;
default -> false;
};
}

public static boolean isValidInMemoryImportedType(String modulePrefix, String identifier) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=io.ballerina.stdlib
version=1.3.1-SNAPSHOT
version=1.4.0-SNAPSHOT

puppycrawlCheckstyleVersion=10.12.0
checkstyleToolVersion=7.8.2
Expand All @@ -11,7 +11,7 @@ testngVersion=7.6.1
gsonVersion=2.10.1
ballerinaGradlePluginVersion=2.0.1

ballerinaLangVersion=2201.9.0
ballerinaLangVersion=2201.10.0-20240529-103800-249ab1b0

# Direct Dependencies

Expand Down

0 comments on commit fa38a75

Please sign in to comment.