Skip to content

Commit

Permalink
- Dependency upgrades.
Browse files Browse the repository at this point in the history
- Updated test cases.
  • Loading branch information
jjzazuet committed Jan 15, 2023
1 parent 621641a commit f75d105
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 46 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ A minimal, opinionated, flat key/value JDBC object storage framework.
- [ ] Strategies for choosing primary key component fields (or not at all).
- [ ] `int` vs `Integer`, when to use which?
- [ ] How to handle schema migrations with Liquibase generated change sets.
- [ ] Always make sure that your schema class names do not clash with database keywords.

Entities which do not define a primary key field can only be saved, loaded or deleted using one of its
Classes which do not define a primary key field can only be saved, loaded or deleted using one of its
attributes. Therefore, calls to `update` or `merge` on a DAO instance will fail.

This restriction is by design in this framework.
Expand Down
4 changes: 3 additions & 1 deletion mt-annotations/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
configure<io.vacco.oss.gitflow.GsPluginProfileExtension> { sharedLibrary(true, false) }
configure<io.vacco.oss.gitflow.GsPluginProfileExtension> {
sharedLibrary(true, false)
}
6 changes: 4 additions & 2 deletions mt-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ dependencies {
api(project(":mt-annotations"))
api(project(":mt-core"))
api("io.vacco.oriax:oriax:0.1.1")
api("org.jooq:joox:1.6.2")
api("org.jooq:joox:2.0.0")
api("io.marioslab.basis:template:1.7")
api("org.liquibase:liquibase-core:4.6.2") {
api("org.liquibase:liquibase-core:4.18.0") {
exclude("ch.qos.logback", "logback-classic")
exclude("org.slf4j", "slf4j-api")
exclude("org.yaml", "snakeyaml")
exclude("com.opencsv", "opencsv")
}
}
17 changes: 10 additions & 7 deletions mt-core/src/main/java/io/vacco/metolithe/core/MtReadDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ public MtReadDao(String schemaName, FluentJdbc jdbc, MtDescriptor<T> d, MtIdFn<K
}

protected String getSelectWhereEqQuery(String field) {
String fn = dsc.getFormat().of(field);
return getQueryCache().computeIfAbsent("selectWhereEq" + fn,
k -> format("select %s from %s where %s = :%s",
propNamesCsv(dsc, true), getSchemaName(), fn, fn)
);
var fn = dsc.getFormat().of(field);
var qk = "selectWhereEq" + fn;
return getQueryCache().computeIfAbsent(qk, k -> {
var pNames = propNamesCsv(dsc, true);
return format("select %s from %s where %s = :%s", pNames, getSchemaName(), fn, fn);
});
}

public Optional<T> load(K id) {
Optional<MtFieldDescriptor> pkf = dsc.getPkField();
if (pkf.isPresent()) {
return sql().query().select(getSelectWhereEqQuery(pkf.get().getFieldName()))
.namedParam(pkf.get().getFieldName(), id)
var q = getSelectWhereEqQuery(pkf.get().getFieldName());
var fn = pkf.get().getFieldName();
return sql().query().select(q)
.namedParam(fn, id)
.firstResult(mapToDefault());
}
return Optional.empty();
Expand Down
6 changes: 3 additions & 3 deletions mt-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
implementation(project(":mt-core"))
implementation(project(":mt-codegen"))

implementation("io.vacco.shax:shax:1.7.30.0.0.7")
implementation("com.h2database:h2:1.4.197")
implementation("org.apache.commons:commons-lang3:3.11")
implementation("io.vacco.shax:shax:2.0.6.0.1.0")
implementation("com.h2database:h2:2.1.214")
implementation("org.apache.commons:commons-lang3:3.12.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.vacco.metolithe.annotations.*;

@MtEntity public class User {
@MtEntity public class DbUser {
@MtPk public int uid;
@St32 public String pw;
@St64 public String alias;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@MtFk(Device.class)
@MtUnique(idx = 0, inPk = false) public long did;
@MtFk(User.class) public int uid;
@MtFk(DbUser.class) public int uid;

// not the best composite index, but a good example anyway.
@St16 @MtCompIndex(name = idxName, idx = 0) public String geoHash2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.vacco.metolithe.annotations.*;

@MtEntity public class UserFollow {
@MtFk(User.class)
@MtFk(DbUser.class)
@MtUnique(idx = 0, inPk = false) public int fromUid;
@MtFk(User.class)
@MtFk(DbUser.class)
@MtUnique(idx = 1, inPk = false) public int toUid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**************************************************
* Generated source file. Do not modify directly. *
**************************************************/
public class UserDao extends MtWriteDao<io.vacco.metolithe.schema.User, java.lang.Integer> {
public class DbUserDao extends MtWriteDao<io.vacco.metolithe.schema.DbUser, java.lang.Integer> {

public static final String pk_uid = "uid";

Expand All @@ -24,63 +24,63 @@ public class UserDao extends MtWriteDao<io.vacco.metolithe.schema.User, java.lan
public static final String fld_tid = "tid";
public static final String fld_tagSignature = "tagSignature";

public UserDao(String schema, MtCaseFormat fmt, FluentJdbc jdbc, MtIdFn<java.lang.Integer> idFn) {
super(schema, jdbc, new MtDescriptor<>(io.vacco.metolithe.schema.User.class, fmt), idFn);
public DbUserDao(String schema, MtCaseFormat fmt, FluentJdbc jdbc, MtIdFn<java.lang.Integer> idFn) {
super(schema, jdbc, new MtDescriptor<>(io.vacco.metolithe.schema.DbUser.class, fmt), idFn);
}

public Collection<io.vacco.metolithe.schema.User> loadWherePwEq(java.lang.String pw) {
public Collection<io.vacco.metolithe.schema.DbUser> loadWherePwEq(java.lang.String pw) {
return loadWhereEq(fld_pw, pw);
}

public final Map<java.lang.String, List<io.vacco.metolithe.schema.User>> loadWherePwIn(java.lang.String ... values) {
public final Map<java.lang.String, List<io.vacco.metolithe.schema.DbUser>> loadWherePwIn(java.lang.String ... values) {
return loadWhereIn(fld_pw, values);
}

public long deleteWherePwEq(java.lang.String pw) {
return deleteWhereEq(fld_pw, pw);
}

public Collection<io.vacco.metolithe.schema.User> loadWhereAliasEq(java.lang.String alias) {
public Collection<io.vacco.metolithe.schema.DbUser> loadWhereAliasEq(java.lang.String alias) {
return loadWhereEq(fld_alias, alias);
}

public final Map<java.lang.String, List<io.vacco.metolithe.schema.User>> loadWhereAliasIn(java.lang.String ... values) {
public final Map<java.lang.String, List<io.vacco.metolithe.schema.DbUser>> loadWhereAliasIn(java.lang.String ... values) {
return loadWhereIn(fld_alias, values);
}

public long deleteWhereAliasEq(java.lang.String alias) {
return deleteWhereEq(fld_alias, alias);
}

public Collection<io.vacco.metolithe.schema.User> loadWhereEmailEq(java.lang.String email) {
public Collection<io.vacco.metolithe.schema.DbUser> loadWhereEmailEq(java.lang.String email) {
return loadWhereEq(fld_email, email);
}

public final Map<java.lang.String, List<io.vacco.metolithe.schema.User>> loadWhereEmailIn(java.lang.String ... values) {
public final Map<java.lang.String, List<io.vacco.metolithe.schema.DbUser>> loadWhereEmailIn(java.lang.String ... values) {
return loadWhereIn(fld_email, values);
}

public long deleteWhereEmailEq(java.lang.String email) {
return deleteWhereEq(fld_email, email);
}

public Collection<io.vacco.metolithe.schema.User> loadWhereTidEq(java.lang.Long tid) {
public Collection<io.vacco.metolithe.schema.DbUser> loadWhereTidEq(java.lang.Long tid) {
return loadWhereEq(fld_tid, tid);
}

public final Map<java.lang.Long, List<io.vacco.metolithe.schema.User>> loadWhereTidIn(java.lang.Long ... values) {
public final Map<java.lang.Long, List<io.vacco.metolithe.schema.DbUser>> loadWhereTidIn(java.lang.Long ... values) {
return loadWhereIn(fld_tid, values);
}

public long deleteWhereTidEq(java.lang.Long tid) {
return deleteWhereEq(fld_tid, tid);
}

public Collection<io.vacco.metolithe.schema.User> loadWhereTagSignatureEq(java.lang.String tagSignature) {
public Collection<io.vacco.metolithe.schema.DbUser> loadWhereTagSignatureEq(java.lang.String tagSignature) {
return loadWhereEq(fld_tagSignature, tagSignature);
}

public final Map<java.lang.String, List<io.vacco.metolithe.schema.User>> loadWhereTagSignatureIn(java.lang.String ... values) {
public final Map<java.lang.String, List<io.vacco.metolithe.schema.DbUser>> loadWhereTagSignatureIn(java.lang.String ... values) {
return loadWhereIn(fld_tagSignature, values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static <T> void logDescriptor(MtDescriptor<T> d, T data) {
.forEach(d -> log.info(d.toString()))
);
it("Can extract primary key components from entities", () -> {
logDescriptor(new MtDescriptor<>(User.class, UPPER_CASE), u0);
logDescriptor(new MtDescriptor<>(DbUser.class, UPPER_CASE), u0);
logDescriptor(new MtDescriptor<>(Device.class, LOWER_CASE), d0);
logDescriptor(new MtDescriptor<>(Phone.class, KEEP_CASE), p0);
});
Expand Down
22 changes: 11 additions & 11 deletions mt-test/src/test/java/io/vacco/metolithe/test/MtDaoSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.vacco.metolithe.core.*;
import io.vacco.metolithe.schema.*;
import io.vacco.metolithe.test.dao.PhoneDao;
import io.vacco.metolithe.test.dao.DbUserDao;
import io.vacco.metolithe.util.MtPage;
import j8spec.annotation.DefinedOrder;
import j8spec.junit.J8SpecRunner;
Expand All @@ -20,8 +21,6 @@
import static org.junit.Assert.*;
import static io.vacco.shax.logging.ShArgument.*;

import io.vacco.metolithe.test.dao.UserDao;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -41,7 +40,6 @@ public class MtDaoSpec extends MtSpec {
.build();

private static final MtIdFn<Integer> m3Ifn = new MtMurmur3IFn();
private static final PhoneDao pDao = new PhoneDao(schema, fmt, jdbc, m3Ifn);

static {
describe("Query parameter building", () -> {
Expand All @@ -54,13 +52,13 @@ public class MtDaoSpec extends MtSpec {
});
});

File xmlFile = new File("/tmp", "mt-test.xml");
File xmlFile = new File("./build", "mt-test.xml");
ds.setURL("jdbc:h2:mem:public;DB_CLOSE_DELAY=-1");

describe("Schema code generation", () -> {
it("Generates typed field DAO definitions", () -> {
File out = new File(".", "src/main/java");
new MtDaoMapper().mapSchema(out, "io.vacco.metolithe.test.dao", fmt, Phone.class, User.class);
new MtDaoMapper().mapSchema(out, "io.vacco.metolithe.test.dao", fmt, Phone.class, DbUser.class);
});
it("Generates Liquibase changelogs", () -> {
Match lbChangeLog = new MtLbMapper().mapSchema(fmt, testSchema);
Expand All @@ -71,21 +69,23 @@ public class MtDaoSpec extends MtSpec {
});
it("Creates an in-memory database and applies the generated change logs.", () -> {
JdbcConnection c = new JdbcConnection(ds.getConnection());
ResourceAccessor ra = new FileSystemResourceAccessor(xmlFile.getParentFile());
Liquibase lb = new Liquibase(xmlFile.getAbsolutePath(), ra, c);
ResourceAccessor ra = new DirectoryResourceAccessor(xmlFile.getParentFile());
Liquibase lb = new Liquibase(xmlFile.getName(), ra, c);
lb.update(new Contexts(), new LabelExpression());
assertNotNull(c);
c.close();
});
});

PhoneDao pDao = new PhoneDao(schema, fmt, jdbc, m3Ifn);

describe("Type safe DAOs", () -> {
it("Creates base DAOs for data access", () -> {

MtWriteDao<Device, Long> dDao = new MtWriteDao<>(
schema, jdbc, new MtDescriptor<>(Device.class, fmt), new MtMurmur3LFn());
MtWriteDao<User, Integer> uDao = new MtWriteDao<>(
schema, jdbc, new MtDescriptor<>(User.class, fmt), m3Ifn);
MtWriteDao<DbUser, Integer> uDao = new MtWriteDao<>(
schema, jdbc, new MtDescriptor<>(DbUser.class, fmt), m3Ifn);
MtWriteDao<DeviceTag, Long> dtDao = new MtWriteDao<>(
schema, jdbc, new MtDescriptor<>(DeviceTag.class, fmt), new MtMurmur3LFn());
MtWriteDao<UserFollow, Void> ufDao = new MtWriteDao<>(
Expand Down Expand Up @@ -140,7 +140,7 @@ public class MtDaoSpec extends MtSpec {
});

it("Uses generated POJO DAOs for data access", () -> {
UserDao ud = new UserDao(schema, fmt, jdbc, new MtMurmur3IFn());
var ud = new DbUserDao(schema, fmt, jdbc, new MtMurmur3IFn());
log.info("{}", kv("loadWhereEqJane", ud.loadWhereAliasEq("Jane")));
log.info("{}", kv("loadWhereEmailEq", ud.loadWhereEmailIn("joe@me.com")));
log.info("");
Expand Down Expand Up @@ -177,6 +177,6 @@ public class MtDaoSpec extends MtSpec {
log.info("{}", kv("page1", page1));
});
});
}

}
}
6 changes: 3 additions & 3 deletions mt-test/src/test/java/io/vacco/metolithe/test/MtSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public abstract class MtSpec {
protected static final JdbcDataSource ds = new JdbcDataSource();
protected static final MtCaseFormat fmt = MtCaseFormat.KEEP_CASE;

public static User u0 = new User();
public static User u1 = new User();
public static DbUser u0 = new DbUser();
public static DbUser u1 = new DbUser();

public static Phone p0 = new Phone();
public static Phone p1 = new Phone();
Expand Down Expand Up @@ -55,6 +55,6 @@ public abstract class MtSpec {

public static Class<?>[] testSchema = new Class<?>[] {
Device.class, DeviceLocation.class, DeviceTag.class,
Phone.class, User.class, UserFollow.class
Phone.class, DbUser.class, UserFollow.class
};
}

0 comments on commit f75d105

Please sign in to comment.