Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler plugin validations for PostgreSQL data types #265

Merged
merged 7 commits into from
Nov 30, 2023
Merged
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
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.2.0"
version = "1.2.1"
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.persist"
artifactId = "persist-native"
version = "1.2.0"
path = "../native/build/libs/persist-native-1.2.0.jar"
version = "1.2.1"
path = "../native/build/libs/persist-native-1.2.1-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.2.0.jar"
path = "../compiler-plugin/build/libs/persist-compiler-plugin-1.2.1-SNAPSHOT.jar"
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ modules = [
[[package]]
org = "ballerina"
name = "persist"
version = "1.2.0"
version = "1.2.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

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

### Changed

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

@Test
public void validateEntityFieldTypeForPostgresql() {
List<Diagnostic> diagnostics = getErrorDiagnostics("project_6", "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 mysql:Client-typed field",
"an entity does not support mysql: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:16)",
"(31:4,31:18)",
"(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"

[persist]
datastore = "postgresql"
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ballerina/time;
import ballerinax/mysql;
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;

mysql:Client clientType;
mysql:Client[] clientArrayType;

Gender gender;
Gender[] genderArray;
|};
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static final class Datastores {

public static final String MYSQL = "mysql";
public static final String MSSQL = "mssql";
public static final String POSTGRESQL = "postgresql";
public static final String IN_MEMORY = "inmemory";
public static final String GOOGLE_SHEETS = "googlesheets";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
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:
Expand All @@ -107,6 +109,8 @@
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:
Expand All @@ -122,6 +126,8 @@
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:
Expand Down Expand Up @@ -159,6 +165,20 @@
}
}

public static boolean isValidPostgresqlType(String type) {
switch (type) {
case INT:
case BOOLEAN:
case DECIMAL:
case FLOAT:
case STRING:
case ENUM:
return true;
default:
return false;
}
}

public static boolean isValidInMemoryType(String type) {
return true;
}
Expand Down Expand Up @@ -195,6 +215,15 @@
}
}

public static boolean isValidPostgresqlArrayType(String type) {
switch (type) {
case BYTE:
return true;
default:
return false;
}
}

public static boolean isValidInMemoryArrayType(String type) {
return true;
}
Expand Down Expand Up @@ -233,6 +262,21 @@
}
}

public static boolean isValidPostgresqlImportedType(String modulePrefix, String identifier) {
if (!modulePrefix.equals(TIME_MODULE)) {
return false;
}
switch (identifier) {
case DATE:
case TIME_OF_DAY:
case UTC:
case CIVIL:
return true;
default:
return false;

Check warning on line 276 in compiler-plugin/src/main/java/io/ballerina/stdlib/persist/compiler/utils/ValidatorsByDatastore.java

View check run for this annotation

Codecov / codecov/patch

compiler-plugin/src/main/java/io/ballerina/stdlib/persist/compiler/utils/ValidatorsByDatastore.java#L276

Added line #L276 was not covered by tests
}
}

public static boolean isValidInMemoryImportedType(String modulePrefix, String identifier) {
return true;
}
Expand Down
Loading