Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Feb 22, 2025
1 parent 3667a5b commit a66c331
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.57
version=0.9.58
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,17 @@ private void addScalarProviderImpl(ImmutableProp prop, ScalarProvider<?, ?> scal
"please use property-specific scalar provider"
);
}
if (ReaderManager.isStandardScalarType((Class<?>) scalarType)) {
throw new IllegalStateException(
"Illegal global type scalar provider type \"" +
scalarProvider.getClass().getName() +
"\" its scalar type argument cannot be \"" +
scalarType +
"\" because it is standard type. Please " +
"use non-standard type or " +
"use property level scalar provider"
);
}
if (typeScalarProviderMap.containsKey(scalarType)) {
throw new IllegalStateException(
"Cannot set scalar provider for scalar type \"" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,6 @@ static void validateScalarType(Type scalarType) {
"\", scalar provider does not support tuple type"
);
}
if (ReaderManager.isStandardScalarType(scalarClass)) {
throw new IllegalArgumentException(
"Illegal scalar type \"" +
((Class<?>)scalarType).getName() +
"\", scalar provider does not support standard scalar type"
);
}
Class<?> annotationType = getOrmAnnotationType(scalarClass);
if (annotationType != null) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.babyfish.jimmer.sql.model.pg;

import org.babyfish.jimmer.sql.runtime.ScalarProvider;
import org.jetbrains.annotations.NotNull;
import org.postgresql.util.PGobject;

public class MacAddressScalarProvider implements ScalarProvider<String, PGobject> {

@Override
public String toScalar(@NotNull PGobject sqlValue) throws Exception {
return sqlValue.getValue();
}

@Override
public PGobject toSql(@NotNull String scalarValue) throws Exception {
PGobject po = new PGobject();
po.setType("macaddr");
po.setValue(scalarValue);
return po;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.babyfish.jimmer.sql.model.pg;

import org.babyfish.jimmer.sql.*;

@Entity
@DatabaseValidationIgnore
public interface PgTypeRow {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id();

String macAddress();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.babyfish.jimmer.sql.mutation;

import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
import org.babyfish.jimmer.sql.common.AbstractMutationTest;
import org.babyfish.jimmer.sql.common.NativeDatabases;
import org.babyfish.jimmer.sql.dialect.PostgresDialect;
import org.babyfish.jimmer.sql.meta.impl.IdentityIdGenerator;
import org.babyfish.jimmer.sql.model.Immutables;
import org.babyfish.jimmer.sql.model.pg.MacAddressScalarProvider;
import org.babyfish.jimmer.sql.model.pg.PgTypeRow;
import org.babyfish.jimmer.sql.model.pg.PgTypeRowProps;
import org.junit.jupiter.api.Test;

public class PgTypeRowTest extends AbstractMutationTest {

@Test
public void insert() {

NativeDatabases.assumeNativeDatabase();

PgTypeRow row = Immutables.createPgTypeRow(draft -> {
draft.setMacAddress("08:00:2b:01:02:03");
});

resetIdentity(NativeDatabases.POSTGRES_DATA_SOURCE, "pg_type_row");
executeAndExpectResult(
NativeDatabases.POSTGRES_DATA_SOURCE,
getSqlClient(it -> {
it.setDialect(new PostgresDialect());
it.setIdGenerator(IdentityIdGenerator.INSTANCE);
it.setScalarProvider(PgTypeRowProps.MAC_ADDRESS, new MacAddressScalarProvider());
}).saveCommand(row).setMode(SaveMode.INSERT_ONLY),
ctx -> {
ctx.statement(it -> {
it.sql("insert into PG_TYPE_ROW(MAC_ADDRESS) values(?) returning ID");
});
ctx.entity(it -> {
it.modified(
"{\"id\":100,\"macAddress\":\"08:00:2b:01:02:03\"}"
);
});
}
);
}
}
11 changes: 10 additions & 1 deletion project/jimmer-sql/src/test/resources/database-postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,13 @@ insert into permission(id, name, role_id, deleted, created_time, modified_time)
(1000, 'p_1', 100, false, '2022-10-03 00:00:00', '2022-10-03 00:10:00'),
(2000, 'p_2', 100, true, '2022-10-03 00:00:00', '2022-10-03 00:10:00'),
(3000, 'p_3', 200, false, '2022-10-03 00:00:00', '2022-10-03 00:10:00'),
(4000, 'p_4', 200, true, '2022-10-03 00:00:00', '2022-10-03 00:10:00');
(4000, 'p_4', 200, true, '2022-10-03 00:00:00', '2022-10-03 00:10:00');



create table pg_type_row(
id bigint not null generated by default as identity(start with 100 increment by 1),
mac_address macaddr
);
alter table pg_type_row
add primary key(id);

0 comments on commit a66c331

Please sign in to comment.