@Test public void Add() { assertToString("cat.bodyWeight + ?1", cat.bodyWeight.add(10)); }
@Test public void OrderExpressionInFunctionalWay() { cat.bodyWeight.asc(); cat.bodyWeight.add(kitten.bodyWeight).asc(); }
@Test public void BasicOperations3() { assertToString( "cat.bodyWeight + kitten.bodyWeight = kitten.bodyWeight", cat.bodyWeight.add(kitten.bodyWeight).eq(kitten.bodyWeight)); }
@Test public void Add_And_Compare() { assertToString("cat.bodyWeight + ?1 < ?1", cat.bodyWeight.add(10.0).lt(10.0)); }
@Test public void Add_And_Multiply() { assertToString("(cat.bodyWeight + ?1) * ?2", cat.bodyWeight.add(10).multiply(20)); }
@Test public void Arithmetic_And_Arithmetic2() { QCat c1 = new QCat("c1"); QCat c2 = new QCat("c2"); QCat c3 = new QCat("c3"); assertToString("c1.id + c2.id * c3.id", c1.id.add(c2.id.multiply(c3.id))); assertToString("c1.id * (c2.id + c3.id)", c1.id.multiply(c2.id.add(c3.id))); assertToString("(c1.id + c2.id) * c3.id", c1.id.add(c2.id).multiply(c3.id)); }
@Test public void MathematicalOperations() { // mathematical operators +, -, *, / cat.bodyWeight.add(kitten.bodyWeight); cat.bodyWeight.subtract(kitten.bodyWeight); cat.bodyWeight.multiply(kitten.bodyWeight); cat.bodyWeight.divide(kitten.bodyWeight); }
@Test public void NormalizeNumericArgs() { JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT); NumberPath<Double> doublePath = new NumberPath<Double>(Double.class, "doublePath"); serializer.handle(doublePath.add(1)); serializer.handle(doublePath.between((float)1.0, 1l)); serializer.handle(doublePath.lt((byte)1)); for (Object constant : serializer.getConstantToLabel().keySet()) { assertEquals(Double.class, constant.getClass()); } }
@Test @ExcludeIn(ORACLE) public void Add_BigDecimal() { QSimpleTypes entity = new QSimpleTypes("entity1"); QSimpleTypes entity2 = new QSimpleTypes("entity2"); NumberPath<BigDecimal> bigd1 = entity.bigDecimal; NumberPath<BigDecimal> bigd2 = entity2.bigDecimal; query().from(entity, entity2) .where(bigd1.add(bigd2).loe(new BigDecimal("1.00"))) .list(entity); }
@Test @ExcludeIn({Target.DERBY, Target.ORACLE}) public void Union5() { SAnimal cat2 = new SAnimal("cat2"); List<Tuple> rows = query().union( new SQLSubQuery().from(cat).join(cat2).on(cat2.id.eq(cat.id.add(1))).list(cat.id, cat2.id), new SQLSubQuery().from(cat).join(cat2).on(cat2.id.eq(cat.id.add(1))).list(cat.id, cat2.id)) .list(); assertEquals(5, rows.size()); for (Tuple row : rows) { int first = row.get(cat.id).intValue(); int second = row.get(cat2.id).intValue(); assertEquals(first + 1, second); } }
@Test @ExcludeIn(MYSQL) // doesn't work in Eclipselink public void Case4() { NumberExpression<Float> numExpression = cat.bodyWeight.floatValue().divide(otherCat.bodyWeight.floatValue()).multiply(100); NumberExpression<Float> numExpression2 = cat.id.when(0).then(0.0F).otherwise(numExpression); assertEquals(ImmutableList.of(200, 150, 133, 125, 120), query().from(cat, otherCat) .where(cat.id.eq(otherCat.id.add(1))) .orderBy(cat.id.asc(), otherCat.id.asc()) .list(numExpression2.intValue())); }
@Test public void ToString() { assertToString("cat", cat); assertToString("cat.alive", cat.alive); assertToString("cat.bodyWeight", cat.bodyWeight); assertToString("cat.name", cat.name); assertToString("cust.name", cust.name); assertToString("cust.name.firstName = ?1", cust.name.firstName.eq("Martin")); // toString("cat.kittens as kitten", cat.kittens.as(kitten)); assertToString("cat.bodyWeight + ?1", cat.bodyWeight.add(10)); assertToString("cat.bodyWeight - ?1", cat.bodyWeight.subtract(10)); assertToString("cat.bodyWeight * ?1", cat.bodyWeight.multiply(10)); assertToString("cat.bodyWeight / ?1", cat.bodyWeight.divide(10)); // toString("cat.bodyWeight as bw", cat.bodyWeight.as("bw")); assertToString("kitten in elements(cat.kittens)", kitten.in(cat.kittens)); // toString("distinct cat.bodyWeight", distinct(cat.bodyWeight)); }
@Test public void CaseBuilder() { QCat cat2 = new QCat("cat2"); NumberExpression<Integer> casex = new CaseBuilder() .when(cat.weight.isNull().and(cat.weight.isNull())).then(0) .when(cat.weight.isNull()).then(cat2.weight) .when(cat2.weight.isNull()).then(cat.weight) .otherwise(cat.weight.add(cat2.weight)); query().from(cat, cat2).orderBy(casex.asc()).list(cat.id, cat2.id); query().from(cat, cat2).orderBy(casex.desc()).list(cat.id, cat2.id); }