/** * Build a {@link Query} combining this object's internal state with the arguments passed. If a * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the * sortOrder is null or empty, the default order will be used (if one was set). * * @param projection the raw column names to be selected * @param selection a raw selection string * @param selectionArgs array of strings which substitute replaceable arguments in the selection string * @param sortOrder a raw ordering clause * @return a {@link Query} using the projection, selection, selection args, and sort order */ public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) { Query query = Query.select(computeProjection(projection)).from(dataSource); boolean hasUserSelection = !SqlUtils.isEmpty(selection); if (hasUserSelection) { query.where(Criterion.fromRawSelection(selection, selectionArgs)); } if (!SqlUtils.isEmpty(sortOrder)) { query.orderBy(Order.fromExpression(sortOrder)); } else if (defaultOrder != null && defaultOrder.length > 0) { query.orderBy(defaultOrder); } if (strictMode && hasUserSelection) { query.requestValidation(); } return query; }
@Override public void run() { try { blockThread.acquire(); Query query2 = Query.select().from(subqueryTable); query2.requestValidation(); SquidCursor<Thing> cursor = database.query(Thing.class, query2); cursor.close(); blockCriterion.release(); } catch (InterruptedException e) { fail("InterruptedException"); } } });
public void testNeedsValidationUpdatedByQueryFunction() { Query subquery = Query.select(Function.max(Thing.ID)).from(Thing.TABLE).where(Criterion.literal(123)); subquery.requestValidation(); assertTrue(subquery.compile(database.getCompileContext()).sql.contains("WHERE (?)")); Query baseTestQuery = Query.select().from(Thing.TABLE).where(Thing.FOO.isNotEmpty()).freeze(); assertFalse(baseTestQuery.needsValidation()); Query testQuery = baseTestQuery.selectMore(subquery.asFunction()); assertTrue(testQuery.compile(database.getCompileContext()).needsValidation); assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))")); }
public void testNeedsValidationUpdatedBySubqueryTable() { Query subquery = Query.select(Thing.PROPERTIES).from(Thing.TABLE).where(Criterion.literal(123)); subquery.requestValidation(); assertTrue(subquery.compile(database.getCompileContext()).sql.contains("WHERE (?)")); Query baseTestQuery = Query.select().from(Thing.TABLE).where(Thing.FOO.isNotEmpty()).freeze(); assertFalse(baseTestQuery.needsValidation()); Query testQuery = baseTestQuery.from(subquery.as("t1")); assertTrue(testQuery.compile(database.getCompileContext()).needsValidation); assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))")); testQuery = baseTestQuery.innerJoin(subquery.as("t2"), (Criterion[]) null); assertTrue(testQuery.compile(database.getCompileContext()).needsValidation); assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))")); testQuery = baseTestQuery.union(subquery); assertTrue(testQuery.compile(database.getCompileContext()).needsValidation); assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))")); }
/** * Build a {@link Query} combining this object's internal state with the arguments passed. If a * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the * sortOrder is null or empty, the default order will be used (if one was set). * * @param projection the raw column names to be selected * @param selection a raw selection string * @param selectionArgs array of strings which substitute replaceable arguments in the selection string * @param sortOrder a raw ordering clause * @return a {@link Query} using the projection, selection, selection args, and sort order */ public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) { Query query = Query.select(computeProjection(projection)).from(dataSource); boolean hasUserSelection = !SqlUtils.isEmpty(selection); if (hasUserSelection) { query.where(Criterion.fromRawSelection(selection, selectionArgs)); } if (!SqlUtils.isEmpty(sortOrder)) { query.orderBy(Order.fromExpression(sortOrder)); } else if (defaultOrder != null && defaultOrder.length > 0) { query.orderBy(defaultOrder); } if (strictMode && hasUserSelection) { query.requestValidation(); } return query; }