Search in sources :

Example 1 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class MysqlTable method queryCondition2Select.

protected List<StringBuilder> queryCondition2Select(Query query, StringBuilder select) {
    // Query by conditions
    Collection<Condition> conditions = query.conditions();
    List<StringBuilder> clauses = new ArrayList<>(conditions.size());
    for (Condition condition : conditions) {
        clauses.add(this.condition2Sql(condition));
    }
    WhereBuilder where = this.newWhereBuilder();
    where.and(clauses);
    select.append(where.build());
    return ImmutableList.of(select);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ArrayList(java.util.ArrayList)

Example 2 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionNotIn.

@Test
public void testConditionNotIn() {
    Condition c1 = Condition.nin(HugeKeys.ID, ImmutableList.of(1, 2, "3"));
    Assert.assertFalse(c1.test(1));
    Assert.assertFalse(c1.test(2));
    Assert.assertFalse(c1.test("3"));
    Assert.assertTrue(c1.test(4));
    Assert.assertTrue(c1.test(1.0));
    Assert.assertTrue(c1.test(2.0));
    Assert.assertTrue(c1.test(3));
    Assert.assertTrue(c1.test(123));
    Assert.assertTrue(c1.test(1.01));
    Assert.assertTrue(c1.test("123"));
    Assert.assertTrue(c1.test("123.0"));
    Assert.assertTrue(c1.test("1"));
    Assert.assertTrue(c1.test("2"));
    Assert.assertTrue(c1.test("3.0"));
    Assert.assertTrue(c1.test("1.0"));
    Assert.assertTrue(c1.test((Object) null));
    Assert.assertTrue(c1.test(ImmutableList.of(4)));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition.nin(HugeKeys.ID, null).test("1");
    }, e -> {
        Assert.assertEquals("Can't test null value for `notin`", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition c2 = new SyspropRelation(HugeKeys.ID, RelationType.NOT_IN, "single-value");
        c2.test("singlevalue");
    }, e -> {
        Assert.assertEquals("Can't test 'single-value'(String) " + "for `notin`, expect Collection", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) SyspropRelation(com.baidu.hugegraph.backend.query.Condition.SyspropRelation) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 3 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionAnd.

@Test
public void testConditionAnd() {
    Condition c1 = Condition.gt(HugeKeys.ID, 18);
    Condition c2 = Condition.lt(HugeKeys.ID, 30);
    Condition c3 = c1.and(c2);
    Assert.assertEquals(Condition.and(c1, c2), c3);
    Assert.assertTrue(c3.test(19));
    Assert.assertTrue(c3.test(20));
    Assert.assertTrue(c3.test(29));
    Assert.assertFalse(c3.test(17));
    Assert.assertFalse(c3.test(18));
    Assert.assertFalse(c3.test(30));
    Assert.assertFalse(c3.test(31));
    // null means 0
    Assert.assertFalse(c3.test((Object) null));
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionContainsKey.

@Test
public void testConditionContainsKey() {
    Condition c1 = Condition.containsKey(HugeKeys.ID, "k1");
    Assert.assertTrue(c1.test(ImmutableMap.of("k1", "abc")));
    Assert.assertTrue(c1.test(ImmutableMap.of("k1", "abc", "k2", "123")));
    Assert.assertFalse(c1.test(ImmutableMap.of("k3", "ab")));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test((Object) null);
    }, e -> {
        Assert.assertEquals("Can't execute `containsk` on type null, " + "expect Map", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test("v1");
    }, e -> {
        Assert.assertEquals("Can't execute `containsk` on type String, " + "expect Map", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        c1.test(123f);
    }, e -> {
        Assert.assertEquals("Can't execute `containsk` on type Float, " + "expect Map", e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 5 with Condition

use of com.baidu.hugegraph.backend.query.Condition in project incubator-hugegraph by apache.

the class ConditionTest method testConditionGte.

@Test
public void testConditionGte() {
    Condition c1 = Condition.gte(HugeKeys.ID, 123);
    Assert.assertTrue(c1.test(124));
    Assert.assertTrue(c1.test(200));
    Assert.assertTrue(c1.test(123));
    Assert.assertTrue(c1.test(123.0));
    Assert.assertFalse(c1.test(122));
    Assert.assertFalse(c1.test(20));
    Assert.assertTrue(c1.test("123"));
    Assert.assertTrue(c1.test("123.0"));
    Assert.assertTrue(c1.test("123.01"));
    Assert.assertTrue(c1.test("200"));
    // null means 0
    Assert.assertFalse(c1.test((Object) null));
    Condition c2 = Condition.gte(HugeKeys.ID, 0);
    Assert.assertTrue(c2.test((Object) null));
    Condition c3 = Condition.gte(HugeKeys.ID, -1);
    Assert.assertTrue(c3.test((Object) null));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition.gte(HugeKeys.ID, "123").test(123);
    }, e -> {
        String err = "Can't compare between 123(Integer) and 123(String)";
        Assert.assertEquals(err, e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Condition.gte(HugeKeys.ID, "123").test(new Date(0L));
    }, e -> {
        String err = String.format("Can't compare between %s(Date) " + "and 123(String)", new Date(0L));
        Assert.assertEquals(err, e.getMessage());
    });
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

Condition (com.baidu.hugegraph.backend.query.Condition)49 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)29 Test (org.junit.Test)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)16 ArrayList (java.util.ArrayList)13 Id (com.baidu.hugegraph.backend.id.Id)12 Collection (java.util.Collection)8 Date (java.util.Date)8 SyspropRelation (com.baidu.hugegraph.backend.query.Condition.SyspropRelation)6 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)4 RangeConditions (com.baidu.hugegraph.backend.query.Condition.RangeConditions)4 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)4 HugeException (com.baidu.hugegraph.HugeException)3 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3 HugeType (com.baidu.hugegraph.type.HugeType)3 Directions (com.baidu.hugegraph.type.define.Directions)3 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)3 AndP (org.apache.tinkerpop.gremlin.process.traversal.util.AndP)3 OrP (org.apache.tinkerpop.gremlin.process.traversal.util.OrP)3