added entity class to dbset

This commit is contained in:
wea_ondara
2022-07-15 21:18:57 +02:00
parent 5dae201bfb
commit 00b7fb2f45
6 changed files with 61 additions and 50 deletions

View File

@@ -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<T extends Serializable> implements Queryable<T> {
public class DbSet<T extends SerializableObject> implements Queryable<T> {
@Getter
private final Class<T> clazz;
private final String table;
public DBSet(String table) {
public DbSet(Class<T> clazz, String table) {
this.clazz = clazz;
this.table = table;
}
@@ -48,22 +52,22 @@ public class DBSet<T extends Serializable> implements Queryable<T> {
}
@Override
public DBSet<T> sequential() {
public DbSet<T> sequential() {
return this;
}
@Override
public DBSet<T> parallel() {
public DbSet<T> parallel() {
return this;
}
@Override
public DBSet<T> unordered() {
public DbSet<T> unordered() {
return this;
}
@Override
public DBSet<T> onClose(Runnable runnable) {
public DbSet<T> onClose(Runnable runnable) {
return this;
}

View File

@@ -0,0 +1,6 @@
package jef.serializable;
import java.io.Serializable;
public class SerializableObject implements Serializable {
}

View File

@@ -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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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<TestClass>("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;

View File

@@ -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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("table1")
act = new DbSet<>(FilterOpTest.TestClass.class, "table1")
.limit(10)
.toString();
Assertions.assertEquals("SELECT * FROM (SELECT * FROM `table1`) a LIMIT 10", act);

View File

@@ -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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("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<FilterOpTest.TestClass>("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();

View File

@@ -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<FilterOpTest.TestClass> q = new DBSet<FilterOpTest.TestClass>("table1")
Queryable<FilterOpTest.TestClass> 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<FilterOpTest.TestClass> q2 = new DBSet<FilterOpTest.TestClass>("table1")
Queryable<FilterOpTest.TestClass> q2 = new DbSet<>(FilterOpTest.TestClass.class, "table1")
.filter(e -> s.contains(e.i) || e.i == 1337);
new DebugExpressionVisitor().visit(q2.getExpression());
}