From f75d1055ad0581130be97a90ee6677c9a3552e17 Mon Sep 17 00:00:00 2001 From: Jesus Zazueta Date: Sat, 14 Jan 2023 19:51:58 -0500 Subject: [PATCH] - Dependency upgrades. - Updated test cases. --- README.md | 3 ++- mt-annotations/build.gradle.kts | 4 ++- mt-codegen/build.gradle.kts | 6 +++-- .../io/vacco/metolithe/core/MtReadDao.java | 17 +++++++----- mt-test/build.gradle.kts | 6 ++--- .../schema/{User.java => DbUser.java} | 2 +- .../metolithe/schema/DeviceLocation.java | 2 +- .../io/vacco/metolithe/schema/UserFollow.java | 4 +-- .../test/dao/{UserDao.java => DbUserDao.java} | 26 +++++++++---------- .../metolithe/test/MtAnnotationsSpec.java | 2 +- .../io/vacco/metolithe/test/MtDaoSpec.java | 22 ++++++++-------- .../java/io/vacco/metolithe/test/MtSpec.java | 6 ++--- 12 files changed, 54 insertions(+), 46 deletions(-) rename mt-test/src/main/java/io/vacco/metolithe/schema/{User.java => DbUser.java} (90%) rename mt-test/src/main/java/io/vacco/metolithe/test/dao/{UserDao.java => DbUserDao.java} (68%) diff --git a/README.md b/README.md index 5177812..16e50a1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/mt-annotations/build.gradle.kts b/mt-annotations/build.gradle.kts index a813e4a..4cd103c 100644 --- a/mt-annotations/build.gradle.kts +++ b/mt-annotations/build.gradle.kts @@ -1 +1,3 @@ -configure { sharedLibrary(true, false) } \ No newline at end of file +configure { + sharedLibrary(true, false) +} diff --git a/mt-codegen/build.gradle.kts b/mt-codegen/build.gradle.kts index e370d16..e479d84 100644 --- a/mt-codegen/build.gradle.kts +++ b/mt-codegen/build.gradle.kts @@ -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") } } diff --git a/mt-core/src/main/java/io/vacco/metolithe/core/MtReadDao.java b/mt-core/src/main/java/io/vacco/metolithe/core/MtReadDao.java index af40e17..4c4a8fa 100644 --- a/mt-core/src/main/java/io/vacco/metolithe/core/MtReadDao.java +++ b/mt-core/src/main/java/io/vacco/metolithe/core/MtReadDao.java @@ -18,18 +18,21 @@ public MtReadDao(String schemaName, FluentJdbc jdbc, MtDescriptor d, MtIdFn 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 load(K id) { Optional 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(); diff --git a/mt-test/build.gradle.kts b/mt-test/build.gradle.kts index 4f1e0b9..b63f122 100644 --- a/mt-test/build.gradle.kts +++ b/mt-test/build.gradle.kts @@ -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") } diff --git a/mt-test/src/main/java/io/vacco/metolithe/schema/User.java b/mt-test/src/main/java/io/vacco/metolithe/schema/DbUser.java similarity index 90% rename from mt-test/src/main/java/io/vacco/metolithe/schema/User.java rename to mt-test/src/main/java/io/vacco/metolithe/schema/DbUser.java index c2f1922..e1db58d 100644 --- a/mt-test/src/main/java/io/vacco/metolithe/schema/User.java +++ b/mt-test/src/main/java/io/vacco/metolithe/schema/DbUser.java @@ -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; diff --git a/mt-test/src/main/java/io/vacco/metolithe/schema/DeviceLocation.java b/mt-test/src/main/java/io/vacco/metolithe/schema/DeviceLocation.java index 3f5d113..9b9c585 100644 --- a/mt-test/src/main/java/io/vacco/metolithe/schema/DeviceLocation.java +++ b/mt-test/src/main/java/io/vacco/metolithe/schema/DeviceLocation.java @@ -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; diff --git a/mt-test/src/main/java/io/vacco/metolithe/schema/UserFollow.java b/mt-test/src/main/java/io/vacco/metolithe/schema/UserFollow.java index 9c685a9..8548b44 100644 --- a/mt-test/src/main/java/io/vacco/metolithe/schema/UserFollow.java +++ b/mt-test/src/main/java/io/vacco/metolithe/schema/UserFollow.java @@ -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; } diff --git a/mt-test/src/main/java/io/vacco/metolithe/test/dao/UserDao.java b/mt-test/src/main/java/io/vacco/metolithe/test/dao/DbUserDao.java similarity index 68% rename from mt-test/src/main/java/io/vacco/metolithe/test/dao/UserDao.java rename to mt-test/src/main/java/io/vacco/metolithe/test/dao/DbUserDao.java index 330d88b..512e99a 100644 --- a/mt-test/src/main/java/io/vacco/metolithe/test/dao/UserDao.java +++ b/mt-test/src/main/java/io/vacco/metolithe/test/dao/DbUserDao.java @@ -14,7 +14,7 @@ /************************************************** * Generated source file. Do not modify directly. * **************************************************/ -public class UserDao extends MtWriteDao { +public class DbUserDao extends MtWriteDao { public static final String pk_uid = "uid"; @@ -24,15 +24,15 @@ public class UserDao extends MtWriteDao idFn) { - super(schema, jdbc, new MtDescriptor<>(io.vacco.metolithe.schema.User.class, fmt), idFn); + public DbUserDao(String schema, MtCaseFormat fmt, FluentJdbc jdbc, MtIdFn idFn) { + super(schema, jdbc, new MtDescriptor<>(io.vacco.metolithe.schema.DbUser.class, fmt), idFn); } - public Collection loadWherePwEq(java.lang.String pw) { + public Collection loadWherePwEq(java.lang.String pw) { return loadWhereEq(fld_pw, pw); } - public final Map> loadWherePwIn(java.lang.String ... values) { + public final Map> loadWherePwIn(java.lang.String ... values) { return loadWhereIn(fld_pw, values); } @@ -40,11 +40,11 @@ public long deleteWherePwEq(java.lang.String pw) { return deleteWhereEq(fld_pw, pw); } - public Collection loadWhereAliasEq(java.lang.String alias) { + public Collection loadWhereAliasEq(java.lang.String alias) { return loadWhereEq(fld_alias, alias); } - public final Map> loadWhereAliasIn(java.lang.String ... values) { + public final Map> loadWhereAliasIn(java.lang.String ... values) { return loadWhereIn(fld_alias, values); } @@ -52,11 +52,11 @@ public long deleteWhereAliasEq(java.lang.String alias) { return deleteWhereEq(fld_alias, alias); } - public Collection loadWhereEmailEq(java.lang.String email) { + public Collection loadWhereEmailEq(java.lang.String email) { return loadWhereEq(fld_email, email); } - public final Map> loadWhereEmailIn(java.lang.String ... values) { + public final Map> loadWhereEmailIn(java.lang.String ... values) { return loadWhereIn(fld_email, values); } @@ -64,11 +64,11 @@ public long deleteWhereEmailEq(java.lang.String email) { return deleteWhereEq(fld_email, email); } - public Collection loadWhereTidEq(java.lang.Long tid) { + public Collection loadWhereTidEq(java.lang.Long tid) { return loadWhereEq(fld_tid, tid); } - public final Map> loadWhereTidIn(java.lang.Long ... values) { + public final Map> loadWhereTidIn(java.lang.Long ... values) { return loadWhereIn(fld_tid, values); } @@ -76,11 +76,11 @@ public long deleteWhereTidEq(java.lang.Long tid) { return deleteWhereEq(fld_tid, tid); } - public Collection loadWhereTagSignatureEq(java.lang.String tagSignature) { + public Collection loadWhereTagSignatureEq(java.lang.String tagSignature) { return loadWhereEq(fld_tagSignature, tagSignature); } - public final Map> loadWhereTagSignatureIn(java.lang.String ... values) { + public final Map> loadWhereTagSignatureIn(java.lang.String ... values) { return loadWhereIn(fld_tagSignature, values); } diff --git a/mt-test/src/test/java/io/vacco/metolithe/test/MtAnnotationsSpec.java b/mt-test/src/test/java/io/vacco/metolithe/test/MtAnnotationsSpec.java index a0f56b0..f8b4064 100644 --- a/mt-test/src/test/java/io/vacco/metolithe/test/MtAnnotationsSpec.java +++ b/mt-test/src/test/java/io/vacco/metolithe/test/MtAnnotationsSpec.java @@ -45,7 +45,7 @@ private static void logDescriptor(MtDescriptor 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); }); diff --git a/mt-test/src/test/java/io/vacco/metolithe/test/MtDaoSpec.java b/mt-test/src/test/java/io/vacco/metolithe/test/MtDaoSpec.java index 554659f..4d295d7 100644 --- a/mt-test/src/test/java/io/vacco/metolithe/test/MtDaoSpec.java +++ b/mt-test/src/test/java/io/vacco/metolithe/test/MtDaoSpec.java @@ -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; @@ -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; @@ -41,7 +40,6 @@ public class MtDaoSpec extends MtSpec { .build(); private static final MtIdFn m3Ifn = new MtMurmur3IFn(); - private static final PhoneDao pDao = new PhoneDao(schema, fmt, jdbc, m3Ifn); static { describe("Query parameter building", () -> { @@ -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); @@ -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 dDao = new MtWriteDao<>( schema, jdbc, new MtDescriptor<>(Device.class, fmt), new MtMurmur3LFn()); - MtWriteDao uDao = new MtWriteDao<>( - schema, jdbc, new MtDescriptor<>(User.class, fmt), m3Ifn); + MtWriteDao uDao = new MtWriteDao<>( + schema, jdbc, new MtDescriptor<>(DbUser.class, fmt), m3Ifn); MtWriteDao dtDao = new MtWriteDao<>( schema, jdbc, new MtDescriptor<>(DeviceTag.class, fmt), new MtMurmur3LFn()); MtWriteDao ufDao = new MtWriteDao<>( @@ -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(""); @@ -177,6 +177,6 @@ public class MtDaoSpec extends MtSpec { log.info("{}", kv("page1", page1)); }); }); - } + } } diff --git a/mt-test/src/test/java/io/vacco/metolithe/test/MtSpec.java b/mt-test/src/test/java/io/vacco/metolithe/test/MtSpec.java index ed18f7a..9e51d6f 100644 --- a/mt-test/src/test/java/io/vacco/metolithe/test/MtSpec.java +++ b/mt-test/src/test/java/io/vacco/metolithe/test/MtSpec.java @@ -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(); @@ -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 }; }