diff --git a/src/main/java/jef/DBSet.java b/src/main/java/jef/DbSet.java similarity index 92% rename from src/main/java/jef/DBSet.java rename to src/main/java/jef/DbSet.java index 99ce354..1e8e14b 100644 --- a/src/main/java/jef/DBSet.java +++ b/src/main/java/jef/DbSet.java @@ -4,16 +4,20 @@ import jef.expressions.Expression; import jef.expressions.SelectExpression; import jef.expressions.TableExpression; import jef.expressions.selectable.DatabaseSelectAllExpression; +import jef.serializable.SerializableObject; +import lombok.Getter; -import java.io.Serializable; import java.util.Iterator; import java.util.List; import java.util.Spliterator; -public class DBSet implements Queryable { +public class DbSet implements Queryable { + @Getter + private final Class clazz; private final String table; - public DBSet(String table) { + public DbSet(Class clazz, String table) { + this.clazz = clazz; this.table = table; } @@ -48,22 +52,22 @@ public class DBSet implements Queryable { } @Override - public DBSet sequential() { + public DbSet sequential() { return this; } @Override - public DBSet parallel() { + public DbSet parallel() { return this; } @Override - public DBSet unordered() { + public DbSet unordered() { return this; } @Override - public DBSet onClose(Runnable runnable) { + public DbSet onClose(Runnable runnable) { return this; } diff --git a/src/main/java/jef/serializable/SerializableObject.java b/src/main/java/jef/serializable/SerializableObject.java new file mode 100644 index 0000000..96dcf64 --- /dev/null +++ b/src/main/java/jef/serializable/SerializableObject.java @@ -0,0 +1,6 @@ +package jef.serializable; + +import java.io.Serializable; + +public class SerializableObject implements Serializable { +} diff --git a/src/test/java/jef/operations/FilterOpTest.java b/src/test/java/jef/operations/FilterOpTest.java index 5ea7bdc..dda869e 100644 --- a/src/test/java/jef/operations/FilterOpTest.java +++ b/src/test/java/jef/operations/FilterOpTest.java @@ -1,6 +1,7 @@ package jef.operations; -import jef.DBSet; +import jef.DbSet; +import jef.serializable.SerializableObject; import lombok.Getter; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -14,7 +15,7 @@ public class FilterOpTest { @Test public void testTrue() { String act; - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> true) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE 1", act); @@ -23,43 +24,43 @@ public class FilterOpTest { @Test public void testCompareWithEntityMember() { String act; - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i != 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` <> 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i < 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` < 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i > 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` > 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i <= 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` <= 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i >= 1) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` >= 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 1337) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1337", act); @@ -69,7 +70,7 @@ public class FilterOpTest { public void testContainsWithEntityMember() { var s = List.of(1, 3); String act; - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> s.contains(e.i)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` IN (1, 3)", act); @@ -79,7 +80,7 @@ public class FilterOpTest { public void testMultipleFilter() { String act; var s = List.of(1, 3); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> s.contains(e.i)) .filter(e -> e.i == 1337) .toString(); @@ -90,31 +91,31 @@ public class FilterOpTest { public void testComplexExpression() { String act; var s = List.of(1, 3); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> s.contains(e.i) && e.i == 1337) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` IN (1, 3) AND `a`.`i` = 1337", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> s.contains(e.i) && e.i == 1337 && e.i == 420) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` IN (1, 3) AND `a`.`i` = 1337 AND `a`.`i` = 420", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 1337 || e.i != 420 || s.contains(e.i)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1337 OR `a`.`i` <> 420 OR `a`.`i` IN (1, 3)", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 1337 || e.i == 420 || s.contains(e.i)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1337 OR `a`.`i` = 420 OR `a`.`i` IN (1, 3)", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 1337 || s.contains(e.i) || e.i == 420) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1337 OR `a`.`i` IN (1, 3) OR `a`.`i` = 420", act); @@ -124,13 +125,13 @@ public class FilterOpTest { public void testComplexExpressionMixedAndOr() { String act; var s = List.of(1, 3); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> s.contains(e.i) && (e.i == 1337 || e.i == 420)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` IN (1, 3) AND (`a`.`i` = 1337 OR `a`.`i` = 420)", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> (e.i == 1337 || e.i == 420) && s.contains(e.i)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE (`a`.`i` = 1337 OR `a`.`i` = 420) AND `a`.`i` IN (1, 3)", act); @@ -140,19 +141,19 @@ public class FilterOpTest { public void testObject() { String act; var s = Arrays.asList(null, 4); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.o == null) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`o` IS NULL", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.o != null) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`o` IS NOT NULL", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.o != null || s.contains(e.o)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`o` IS NOT NULL OR `a`.`o` IN (NULL, 4)", act); @@ -162,19 +163,19 @@ public class FilterOpTest { public void testFloatingPoint() { String act; var s = Arrays.asList(null, 2); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.d == 3.14d) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`d` = 3.14", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.f == 3.14f) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`f` = 3.14", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.d != 1d || e.f != 1 || s.contains(e.d)) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`d` <> 1 OR `a`.`f` <> 1 OR `a`.`d` IN (NULL, 2)", act); @@ -183,25 +184,25 @@ public class FilterOpTest { @Test public void testIDFLConst() { String act; - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.i == 0 || e.i == 1 || e.i == 2 || e.i == 3 || e.i == 4 || e.i == 5) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 0 OR `a`.`i` = 1 OR `a`.`i` = 2 OR `a`.`i` = 3 OR `a`.`i` = 4 OR `a`.`i` = 5", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.d == 0D || e.d == 1D) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`d` = 0 OR `a`.`d` = 1", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.f == 0F || e.f == 1F || e.f == 2F) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`f` = 0 OR `a`.`f` = 1 OR `a`.`f` = 2", act); - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.l == 0L || e.l == 1L) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`l` = 0 OR `a`.`l` = 1", act); @@ -210,14 +211,14 @@ public class FilterOpTest { @Test public void testMemberViaGetter() { String act; - act = new DBSet("table1") + act = new DbSet<>(TestClass.class, "table1") .filter(e -> e.getI() == 1337) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a WHERE `a`.`i` = 1337", act); } @Getter - public static class TestClass implements Serializable { + public static class TestClass extends SerializableObject { public int i = 1; public Object o = new Object(); public double d; diff --git a/src/test/java/jef/operations/LimitOpTest.java b/src/test/java/jef/operations/LimitOpTest.java index 83c8ea7..b3b747c 100644 --- a/src/test/java/jef/operations/LimitOpTest.java +++ b/src/test/java/jef/operations/LimitOpTest.java @@ -1,6 +1,6 @@ package jef.operations; -import jef.DBSet; +import jef.DbSet; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -8,25 +8,25 @@ public class LimitOpTest { @Test public void test() { String act; - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .limit(10).skip(5) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a LIMIT 10 OFFSET 5", act); - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .skip(5).limit(10) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a LIMIT 10 OFFSET 5", act); - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .skip(5) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a OFFSET 5", act); - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .limit(10) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a LIMIT 10", act); diff --git a/src/test/java/jef/operations/SortOpTest.java b/src/test/java/jef/operations/SortOpTest.java index 65de68e..8cf0f69 100644 --- a/src/test/java/jef/operations/SortOpTest.java +++ b/src/test/java/jef/operations/SortOpTest.java @@ -1,6 +1,6 @@ package jef.operations; -import jef.DBSet; +import jef.DbSet; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -8,27 +8,27 @@ class SortOpTest { @Test public void test() { String act; - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .sorted(e -> e.getI()).thenSorted(e -> e.getD()).thenSorted(e -> e.getF()).thenSorted(e -> e.getL()).thenSorted(e -> e.getO()) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a ORDER BY `a`.`i` ASC, `a`.`d` ASC, `a`.`f` ASC, `a`.`l` ASC, `a`.`o` ASC", act); - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .sortedDescending(e -> e.getI()).thenSortedDescending(e -> e.getD()).thenSortedDescending(e -> e.getF()) /**/.thenSortedDescending(e -> e.getL()).thenSortedDescending(e -> e.getO()) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a ORDER BY `a`.`i` DESC, `a`.`d` DESC, `a`.`f` DESC, `a`.`l` DESC, `a`.`o` DESC", act); //alternating patterns - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .sortedDescending(e -> e.getI()).thenSorted(e -> e.getD()).thenSortedDescending(e -> e.getF()).thenSorted(e -> e.getL()) /**/.thenSortedDescending(e -> e.getO()) .toString(); Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a ORDER BY `a`.`i` DESC, `a`.`d` ASC, `a`.`f` DESC, `a`.`l` ASC, `a`.`o` DESC", act); - act = new DBSet("table1") + act = new DbSet<>(FilterOpTest.TestClass.class, "table1") .sorted(e -> e.getI()).thenSortedDescending(e -> e.getD()).thenSorted(e -> e.getF()).thenSortedDescending(e -> e.getL()) /**/.thenSorted(e -> e.getO()) .toString(); diff --git a/src/test/java/jef/visitors/DebugExpressionVisitorTest.java b/src/test/java/jef/visitors/DebugExpressionVisitorTest.java index 49ee245..b3ba492 100644 --- a/src/test/java/jef/visitors/DebugExpressionVisitorTest.java +++ b/src/test/java/jef/visitors/DebugExpressionVisitorTest.java @@ -1,6 +1,6 @@ package jef.visitors; -import jef.DBSet; +import jef.DbSet; import jef.Queryable; import jef.expressions.visitors.DebugExpressionVisitor; import jef.operations.FilterOpTest; @@ -12,11 +12,11 @@ class DebugExpressionVisitorTest { @Test public void test() { var s = List.of(1, 3); - Queryable q = new DBSet("table1") + Queryable q = new DbSet<>(FilterOpTest.TestClass.class, "table1") .filter(e -> s.contains(e.i) && e.i == 1337 && e.i == 420); new DebugExpressionVisitor().visit(q.getExpression()); - Queryable q2 = new DBSet("table1") + Queryable q2 = new DbSet<>(FilterOpTest.TestClass.class, "table1") .filter(e -> s.contains(e.i) || e.i == 1337); new DebugExpressionVisitor().visit(q2.getExpression()); }