Java Code Examples for com.yahoo.squidb.sql.Query#select()

The following examples show how to use com.yahoo.squidb.sql.Query#select() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SquidDatabase.java    From squidb with Apache License 2.0 6 votes vote down vote up
/**
 * Count the number of rows matching a given {@link Criterion}. Use null to count all rows.
 *
 * @param modelClass the model class corresponding to the table
 * @param criterion the criterion to match
 * @return the number of rows matching the given criterion
 */
public int count(Class<? extends AbstractModel> modelClass, Criterion criterion) {
    Property.IntegerProperty countProperty = Property.IntegerProperty.countProperty();
    Query query = Query.select(countProperty);
    if (criterion != null) {
        query.where(criterion);
    }
    query = inferTableForQuery(modelClass, query);
    CompiledStatement compiled = query.compile(getCompileContext());
    acquireNonExclusiveLock();
    try {
        return (int) getDatabase().simpleQueryForLong(compiled.sql, compiled.sqlArgs);
    } finally {
        releaseNonExclusiveLock();
    }
}
 
Example 2
Source File: SquidCursorTest.java    From squidb with Apache License 2.0 5 votes vote down vote up
public void testTypesafeGetter() {
    StringProperty literalString = StringProperty.literal("literalString", "testStr");
    IntegerProperty literalInteger = IntegerProperty.literal(2, "testInt");
    BooleanProperty literalBoolean = BooleanProperty.literal(true, "testBool");

    // Test casting Integer to Boolean
    BooleanProperty castBool = BooleanProperty.literal(false, literalInteger.getName());

    // Test casting Boolean to Integer
    IntegerProperty castInt = IntegerProperty.literal(0, literalBoolean.getName());

    // Test casting Integer to String
    StringProperty castString = StringProperty.literal("", literalInteger.getName());

    Query query = Query.select(literalString, literalInteger, literalBoolean);
    SquidCursor<?> cursor = database.query(null, query);
    try {
        assertTrue(cursor.moveToFirst());

        assertEquals("literalString", cursor.get(literalString));
        assertEquals(2, cursor.get(literalInteger).intValue());
        assertTrue(cursor.get(literalInteger) instanceof Integer);
        assertTrue(cursor.get(literalBoolean));

        assertTrue(cursor.get(castBool));
        assertEquals(1, cursor.get(castInt).intValue());
        assertEquals("2", cursor.get(castString));
    } finally {
        cursor.close();
    }
}
 
Example 3
Source File: JSONFunctionTest.java    From squidb with Apache License 2.0 5 votes vote down vote up
private <T> void testJsonExtractSinglePathInternal(String json, String path, T expectedValue) {
    Function<T> extract = JSONFunctions.jsonExtract(json, path);
    Query sql = Query.select(extract);
    Object value;
    if (expectedValue instanceof String || expectedValue == null) {
        value = database.simpleQueryForString(sql);
        assertEquals(expectedValue, value);
    } else if (expectedValue instanceof Number) {
        value = database.simpleQueryForLong(sql);
        assertEquals(((Number) expectedValue).longValue(), value);
    } else {
        fail("Invalid expected value");
    }
}
 
Example 4
Source File: SquidCursorAdapterTest.java    From squidb with Apache License 2.0 5 votes vote down vote up
public void testNoIdColumnForNonTableModels() {
    Query query = Query.select(TestViewModel.PROPERTIES);
    testCursorAdapterInternal(new TestViewModel(), null, query, new CursorAdapterTest() {
        @Override
        public void testCursorAdapter(SquidCursorAdapter<AbstractModel> adapter) {
            assertFalse(adapter.hasStableIds());
            assertEquals(0, adapter.getItemId(0));
            assertEquals(0, adapter.getItemId(1));
        }
    });
}