/** * Construct a StringProperty from a literal value and with the given alias, e.g. "3.14 AS pi" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static DoubleProperty literal(double literal, String selectAs) { return new DoubleProperty(null, String.valueOf(literal), selectAs, null); }
/** * Construct a StringProperty from a literal value and with the given alias, e.g. "3.14 AS pi" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static DoubleProperty literal(double literal, String selectAs) { return new DoubleProperty(null, String.valueOf(literal), selectAs, null); }
/** * Construct a DoubleProperty from a {@link Function} and with the given alias, e.g. "AVG(column) AS average" * * @param function the function * @param selectAs the alias to use. May be null. */ public static DoubleProperty fromFunction(Function<Double> function, String selectAs) { return new DoubleProperty(function, selectAs); }
values.put(TestModel.BIRTHDAY.getName(), 1); // Putting an int where long expected values.put(TestModel.IS_HAPPY.getName(), 1); // Putting an int where boolean expected values.put(TestModel.SOME_DOUBLE.getName(), 1); // Putting an int where double expected values.put(TestModel.$_123_ABC.getName(), "1"); // Putting a String where int expected assertTrue(checkTypesOn.get(TestModel.BIRTHDAY.getName()) instanceof Long); assertTrue(checkTypesOn.get(TestModel.IS_HAPPY.getName()) instanceof Boolean); assertTrue(checkTypesOn.get(TestModel.SOME_DOUBLE.getName()) instanceof Double); assertTrue(checkTypesOn.get(TestModel.$_123_ABC.getName()) instanceof Integer);
public void testLiteralProperties() { StringProperty stringLiteral = StringProperty.literal("abc", "strLit"); assertEquals("SELECT 'abc' AS strLit", Query.select(stringLiteral).toString()); IntegerProperty intLiteral = IntegerProperty.literal(1, "intLit"); assertEquals("SELECT 1 AS intLit", Query.select(intLiteral).toString()); long longVal = System.currentTimeMillis(); LongProperty longLiteral = LongProperty.literal(longVal, "longLit"); assertEquals("SELECT " + longVal + " AS longLit", Query.select(longLiteral).toString()); DoubleProperty doubleLiteral = DoubleProperty.literal(1.1, "doubleLit"); assertEquals("SELECT 1.1 AS doubleLit", Query.select(doubleLiteral).toString()); BooleanProperty trueLiteral = BooleanProperty.literal(true, "trueLit"); assertEquals("SELECT 1 AS trueLit", Query.select(trueLiteral).toString()); BooleanProperty falseLiteral = BooleanProperty.literal(false, "falseLit"); assertEquals("SELECT 0 AS falseLit", Query.select(falseLiteral).toString()); EnumProperty enumLiteral = EnumProperty.literal(TestEnum.APPLE, "enumLit"); assertEquals("SELECT 'APPLE' AS enumLit", Query.select(enumLiteral).toString()); }
public void testInsertWithQuery() { double pi = Math.PI; Criterion criterion = Thing.QUX.gt(pi); int numThingsMatching = database.count(Thing.class, criterion); // insert into testModels select foo, bar, isAlive from things where qux > 3.1415...; Query query = Query.select(Thing.FOO, Thing.BAR, Thing.IS_ALIVE).from(Thing.TABLE).where(criterion); Insert insert = Insert.into(TestModel.TABLE).columns(TestModel.LAST_NAME, TestModel.LUCKY_NUMBER, TestModel.IS_HAPPY).select(query); CompiledStatement compiled = insert.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 1, pi); int testModelsBeforeInsert = database.countAll(TestModel.class); assertEquals(3, database.insert(insert)); int testModelsAfterInsert = database.countAll(TestModel.class); assertEquals(testModelsBeforeInsert + numThingsMatching, testModelsAfterInsert); }
public void testAvgAndAvgDistinct() { setUpAggregateTest(); DoubleProperty avg = DoubleProperty.fromFunction(Function.avg(TestModel.LUCKY_NUMBER), "avg"); DoubleProperty avgDistinct = DoubleProperty.fromFunction( Function.avgDistinct(TestModel.LUCKY_NUMBER), "avgDistinct"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(avg, avgDistinct)); try { cursor.moveToFirst(); assertEquals(2.0, cursor.get(avg)); assertEquals(4.0, cursor.get(avgDistinct)); } finally { cursor.close(); } }
/** * Construct a DoubleProperty from a {@link Function} and with the given alias, e.g. "AVG(column) AS average" * * @param function the function * @param selectAs the alias to use. May be null. */ public static DoubleProperty fromFunction(Function<Double> function, String selectAs) { return new DoubleProperty(function, selectAs); }