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

The following examples show how to use com.yahoo.squidb.sql.Query#compile() . 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: ContentProviderQueryBuilderTest.java    From squidb with Apache License 2.0 6 votes vote down vote up
public void testRawSelection() {
    String selection = COL_LUCKY_NUMBER + " > ? AND " + COL_IS_HAPPY + " != ?";
    String[] selectionArgs = new String[]{"50", "0"};
    ContentProviderQueryBuilder builder = getBuilder();
    Query query = builder.setDataSource(TestModel.TABLE).build(null, selection, selectionArgs, null);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 2, "50", "0");

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(1, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model2, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 2
Source File: ContentProviderQueryBuilderTest.java    From squidb with Apache License 2.0 6 votes vote down vote up
public void testRawOrderBy() {
    String sortOrder = COL_GIVEN_NAME + " ASC";
    ContentProviderQueryBuilder builder = getBuilder();
    Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, sortOrder);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 0);

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(3, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model3, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model2, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model1, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 3
Source File: ContentProviderQueryBuilderTest.java    From squidb with Apache License 2.0 6 votes vote down vote up
public void testDefaultOrderBy() {
    ContentProviderQueryBuilder builder = getBuilder();
    builder.setDefaultOrder(TestModel.LUCKY_NUMBER.desc());
    Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, null);
    CompiledStatement compiled = query.compile(database.getCompileContext());
    verifyCompiledSqlArgs(compiled, 0);

    SquidCursor<TestModel> cursor = null;
    try {
        cursor = database.query(TestModel.class, query);
        assertEquals(3, cursor.getCount());
        cursor.moveToFirst();
        assertEquals(model2, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model1, buildModelFromCursor(cursor));
        cursor.moveToNext();
        assertEquals(model3, buildModelFromCursor(cursor));
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
 
Example 4
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 5
Source File: SquidDatabase.java    From squidb with Apache License 2.0 5 votes vote down vote up
/**
 * Directly analogous to {@link #query(Class, Query)}, but instead of returning a result, this method just logs the
 * output of EXPLAIN QUERY PLAN for the given query. This is method is intended for debugging purposes only.
 */
public void explainQueryPlan(Class<? extends AbstractModel> modelClass, Query query) {
    query = inferTableForQuery(modelClass, query);
    CompiledStatement compiled = query.compile(getCompileContext());
    ICursor cursor = rawQuery("EXPLAIN QUERY PLAN " + compiled.sql, compiled.sqlArgs);
    try {
        Logger.d(Logger.LOG_TAG, "Query plan for: " + compiled.sql);
        SquidUtilities.dumpCursor(cursor, -1);
    } finally {
        cursor.close();
    }
}
 
Example 6
Source File: SquidDatabase.java    From squidb with Apache License 2.0 2 votes vote down vote up
/**
 * Execute a statement that returns a 1x1 String result. If you know your result set will only have one row and
 * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor.
 * <br>
 * Note: This will throw an exception if the given SQL query returns a result that is not a single column
 *
 * @param query a sql query
 * @return the String result of the query
 */
public String simpleQueryForString(Query query) {
    CompiledStatement compiled = query.compile(getCompileContext());
    return simpleQueryForString(compiled.sql, compiled.sqlArgs);
}
 
Example 7
Source File: SquidDatabase.java    From squidb with Apache License 2.0 2 votes vote down vote up
/**
 * Execute a statement that returns a 1x1 long result. If you know your result set will only have one row and
 * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor.
 * <br>
 * Note: This will throw an exception if the given SQL query returns a result that is not a single column
 *
 * @param query a sql query
 * @return the long result of the query
 */
public long simpleQueryForLong(Query query) {
    CompiledStatement compiled = query.compile(getCompileContext());
    return simpleQueryForLong(compiled.sql, compiled.sqlArgs);
}