com.yahoo.squidb.sql.Query类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(302)

本文整理了Java中com.yahoo.squidb.sql.Query类的一些代码示例,展示了Query类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query类的具体详情如下:
包路径:com.yahoo.squidb.sql.Query
类名称:Query

Query介绍

[英]Builder class for a SQLite SELECT statement
[中]SQLite SELECT语句的生成器类

代码示例

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Build a {@link Query} combining this object's internal state with the arguments passed. If a
  3. * {@link ProjectionMap} is set, the projection elements will be evaluated and transformed accordingly. If the
  4. * sortOrder is null or empty, the default order will be used (if one was set).
  5. *
  6. * @param projection the raw column names to be selected
  7. * @param selection a raw selection string
  8. * @param selectionArgs array of strings which substitute replaceable arguments in the selection string
  9. * @param sortOrder a raw ordering clause
  10. * @return a {@link Query} using the projection, selection, selection args, and sort order
  11. */
  12. public Query build(String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  13. Query query = Query.select(computeProjection(projection)).from(dataSource);
  14. boolean hasUserSelection = !SqlUtils.isEmpty(selection);
  15. if (hasUserSelection) {
  16. query.where(Criterion.fromRawSelection(selection, selectionArgs));
  17. }
  18. if (!SqlUtils.isEmpty(sortOrder)) {
  19. query.orderBy(Order.fromExpression(sortOrder));
  20. } else if (defaultOrder != null && defaultOrder.length > 0) {
  21. query.orderBy(defaultOrder);
  22. }
  23. if (strictMode && hasUserSelection) {
  24. query.requestValidation();
  25. }
  26. return query;
  27. }

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Construct a new Select statement that queries all the {@link Field}s of the given subquery
  3. *
  4. * @param subquery the inner Query object
  5. * @param subqueryAlias the name to alias the subquery
  6. * @return a new Query object
  7. */
  8. public static Query fromSubquery(Query subquery, String subqueryAlias) {
  9. SubqueryTable table = subquery.as(subqueryAlias);
  10. return Query.select(table.qualifiedFields()).from(table);
  11. }

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Count the number of rows matching a given {@link Criterion}. Use null to count all rows.
  3. *
  4. * @param modelClass the model class corresponding to the table
  5. * @param criterion the criterion to match
  6. * @return the number of rows matching the given criterion
  7. */
  8. public int count(Class<? extends AbstractModel> modelClass, Criterion criterion) {
  9. Property.IntegerProperty countProperty = Property.IntegerProperty.countProperty();
  10. Query query = Query.select(countProperty);
  11. if (criterion != null) {
  12. query.where(criterion);
  13. }
  14. query = inferTableForQuery(modelClass, query);
  15. CompiledStatement compiled = query.compile(getCompileContext());
  16. acquireNonExclusiveLock();
  17. try {
  18. return (int) getDatabase().simpleQueryForLong(compiled.sql, compiled.sqlArgs);
  19. } finally {
  20. releaseNonExclusiveLock();
  21. }
  22. }

代码示例来源:origin: yahoo/squidb

  1. protected <TYPE extends AbstractModel> SquidCursor<TYPE> fetchFirstItem(Class<TYPE> modelClass, Query query) {
  2. boolean immutableQuery = query.isImmutable();
  3. Field<Integer> beforeLimit = query.getLimit();
  4. SqlTable<?> beforeTable = query.getTable();
  5. query = query.limit(1); // If argument was frozen, we may get a new object
  6. SquidCursor<TYPE> cursor = query(modelClass, query);
  7. if (!immutableQuery) {
  8. query.from(beforeTable).limit(beforeLimit); // Reset for user
  9. }
  10. cursor.moveToFirst();
  11. return cursor;
  12. }

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Set the {@link SqlTable table} this query selects from
  3. *
  4. * @param table the table to select from
  5. * @return this Query object, to allow chaining method calls
  6. */
  7. public Query from(SqlTable<?> table) {
  8. if (immutable) {
  9. return fork().from(table);
  10. }
  11. if (this.table != table) {
  12. this.table = table;
  13. if (selectAllCache != null) {
  14. selectAllCache.clear();
  15. }
  16. invalidateCompileCache();
  17. }
  18. return this;
  19. }

代码示例来源:origin: yahoo/squidb

  1. public void testNeedsValidationUpdatedBySubqueryTable() {
  2. Query subquery = Query.select(Thing.PROPERTIES).from(Thing.TABLE).where(Criterion.literal(123));
  3. subquery.requestValidation();
  4. assertTrue(subquery.compile(database.getCompileContext()).sql.contains("WHERE (?)"));
  5. Query baseTestQuery = Query.select().from(Thing.TABLE).where(Thing.FOO.isNotEmpty()).freeze();
  6. assertFalse(baseTestQuery.needsValidation());
  7. Query testQuery = baseTestQuery.from(subquery.as("t1"));
  8. assertTrue(testQuery.compile(database.getCompileContext()).needsValidation);
  9. assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))"));
  10. testQuery = baseTestQuery.innerJoin(subquery.as("t2"), (Criterion[]) null);
  11. assertTrue(testQuery.compile(database.getCompileContext()).needsValidation);
  12. assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))"));
  13. testQuery = baseTestQuery.union(subquery);
  14. assertTrue(testQuery.compile(database.getCompileContext()).needsValidation);
  15. assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))"));
  16. }

代码示例来源:origin: yahoo/squidb

  1. public void testNeedsValidationUpdatedByQueryFunction() {
  2. Query subquery = Query.select(Function.max(Thing.ID)).from(Thing.TABLE).where(Criterion.literal(123));
  3. subquery.requestValidation();
  4. assertTrue(subquery.compile(database.getCompileContext()).sql.contains("WHERE (?)"));
  5. Query baseTestQuery = Query.select().from(Thing.TABLE).where(Thing.FOO.isNotEmpty()).freeze();
  6. assertFalse(baseTestQuery.needsValidation());
  7. Query testQuery = baseTestQuery.selectMore(subquery.asFunction());
  8. assertTrue(testQuery.compile(database.getCompileContext()).needsValidation);
  9. assertTrue(testQuery.sqlForValidation(database.getCompileContext()).contains("WHERE ((?))"));
  10. }

代码示例来源:origin: yahoo/squidb

  1. public void testValidationPropagatesToSubqueryJoinAndCompoundSelect() {
  2. Query subquery = Query.select(Thing.FOO).from(Thing.TABLE).where(Thing.BAR.gt(0));
  3. Query joinSubquery = Query.select(Thing.BAR).from(Thing.TABLE).where(Thing.FOO.isNotEmpty());
  4. Query compoundSubquery = Query.select(Thing.BAZ).from(Thing.TABLE).where(Thing.IS_ALIVE.isTrue());
  5. SubqueryTable subqueryTable = subquery.as("t1");
  6. SubqueryTable joinTable = joinSubquery.as("t2");
  7. Query query = Query.select().from(subqueryTable).innerJoin(joinTable, (Criterion[]) null)
  8. .union(compoundSubquery);
  9. final int queryLength = query.compile(database.getCompileContext()).sql.length();
  10. String withValidation = query.sqlForValidation(database.getCompileContext());
  11. assertEquals(queryLength + 6, withValidation.length());
  12. }

代码示例来源:origin: yahoo/squidb

  1. public void testSubqueryJoin() {
  2. StringProperty managerName = Employee.NAME.as("managerName");
  3. Query query = Query
  4. .fromSubquery(Query.select(Employee.MANAGER_ID).from(Employee.TABLE).groupBy(Employee.MANAGER_ID),
  5. "subquery");
  6. query.selectMore(managerName);
  7. query.join(Join.inner(Employee.TABLE, query.getTable().qualifyField(Employee.MANAGER_ID).eq(Employee.ID)))
  8. .orderBy(Employee.MANAGER_ID.asc());
  9. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  10. try {
  11. assertEquals(3, cursor.getCount());
  12. cursor.moveToFirst();
  13. assertEquals("bigBird", cursor.get(managerName));
  14. cursor.moveToNext();
  15. assertEquals("cookieMonster", cursor.get(managerName));
  16. cursor.moveToNext();
  17. assertEquals("bert", cursor.get(managerName));
  18. } finally {
  19. cursor.close();
  20. }
  21. }

代码示例来源:origin: yahoo/squidb

  1. public void testQueryFreeze() {
  2. Query base = Query.select().from(Employee.TABLE).limit(1).freeze();
  3. Query fork = base.limit(2);
  4. assertFalse(base == fork);
  5. assertEquals(Field.field("1"), base.getLimit());
  6. assertEquals(Field.field("2"), fork.getLimit());
  7. assertEquals(base.getTable(), fork.getTable());
  8. }

代码示例来源:origin: yahoo/squidb

  1. StringProperty subordinates = StringProperty.fromFunction(Function.groupConcat(aliasedId, separator),
  2. "subordinates");
  3. Query subquery = Query.select(aliasedManagerId, subordinates).from(employeesAlias)
  4. .groupBy(aliasedManagerId);
  5. subquery.having(Function.count().gt(1));
  6. SqlTable<?> subTable = subquery.as("subTable");
  7. StringProperty coworkers = subTable.qualifyField(subordinates);
  8. Query query = Query.select(Employee.PROPERTIES).selectMore(coworkers)
  9. .from(Employee.TABLE);
  10. if (leftJoin) {
  11. query.leftJoin(subTable, Employee.MANAGER_ID);
  12. } else {
  13. query.innerJoin(subTable, Employee.MANAGER_ID);

代码示例来源:origin: yahoo/squidb

  1. public void testInCriterion() {
  2. List<String> expectedNames = Arrays.asList("bigBird", "cookieMonster");
  3. Query query = Query.select().where(Employee.NAME.in("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc());
  4. testInQuery(expectedNames, query);
  5. query = Query.select().where(Employee.NAME.notIn("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc());
  6. testInQuery(Arrays.asList("bert", "elmo", "ernie", "oscar"), query);
  7. List<String> list = Arrays.asList("bigBird", "cookieMonster");
  8. query = Query.select().where(Employee.NAME.in(list)).orderBy(Employee.NAME.asc());
  9. testInQuery(expectedNames, query);
  10. // Test off-by-one error that used to occur when the in criterion wasn't the last criterion in the list
  11. query = Query.select().where(Employee.NAME.in(list).or(Field.field("1").neq(1))).orderBy(Employee.NAME.asc());
  12. testInQuery(expectedNames, query);
  13. }

代码示例来源:origin: yahoo/squidb

  1. protected <TYPE extends AbstractModel> SquidCursor<TYPE> fetchFirstItem(Class<TYPE> modelClass,
  2. Criterion criterion, Property<?>... properties) {
  3. return fetchFirstItem(modelClass, Query.select(properties).where(criterion));
  4. }

代码示例来源:origin: yahoo/squidb

  1. public void testDatabaseProvidedArgumentResolver() {
  2. database.useCustomArgumentBinder = true;
  3. Query query = Query.select(TestModel.SOME_ENUM).from(TestModel.TABLE)
  4. .where(TestModel.SOME_ENUM.eq(TestEnum.APPLE));
  5. CompiledStatement compiledStatement = query.compile(database.getCompileContext());
  6. verifyCompiledSqlArgs(compiledStatement, 1, 0);
  7. }

代码示例来源:origin: yahoo/squidb

  1. public void testFork() {
  2. Query base = Query.select().from(Employee.TABLE).limit(1);
  3. Query fork = base.fork().limit(2);
  4. base.limit(3);
  5. assertFalse(base == fork);
  6. assertEquals(Field.field("3"), base.getLimit());
  7. assertEquals(Field.field("2"), fork.getLimit());
  8. assertEquals(base.getTable(), fork.getTable());
  9. }

代码示例来源:origin: yahoo/squidb

  1. Query subquery = Query.select(Thing.FOO).from(Thing.TABLE).where(weirdCriterion);
  2. final SubqueryTable subqueryTable = subquery.as("t1");
  3. Query query = Query.select().from(subqueryTable);
  4. query.requestValidation();

代码示例来源:origin: yahoo/squidb

  1. /**
  2. * Add more {@link Field Fields} to be selected
  3. *
  4. * @param fields the additional Fields to be selected
  5. * @return this Query object, to allow chaining method calls
  6. */
  7. public Query selectMore(List<Field<?>> fields) {
  8. if (immutable) {
  9. return fork().selectMore(fields);
  10. }
  11. if (!isEmpty(fields)) {
  12. if (this.fields == null) {
  13. this.fields = new ArrayList<>(fields);
  14. } else {
  15. this.fields.addAll(fields);
  16. }
  17. if (selectAllCache != null) {
  18. selectAllCache.clear();
  19. }
  20. invalidateCompileCache();
  21. }
  22. return this;
  23. }

代码示例来源:origin: yahoo/squidb

  1. @Override
  2. public void run() {
  3. // insert into testModels select luckyNumber from testModels where luckyNumber = 9;
  4. Query query = Query.select(TestModel.FIRST_NAME, TestModel.LAST_NAME, TestModel.BIRTHDAY)
  5. .from(TestModel.TABLE)
  6. .where(TestModel.LUCKY_NUMBER.eq(9));
  7. Insert insert = Insert.into(TestModel.TABLE).select(query);
  8. insert.compile(database.getCompileContext());
  9. }
  10. }, IllegalStateException.class);

代码示例来源:origin: yahoo/squidb

  1. public void testSimpleSubquerySelect() {
  2. Query query = Query.fromSubquery(Query.select(Employee.NAME).from(Employee.TABLE), "subquery");
  3. StringProperty name = query.getTable().qualifyField(Employee.NAME);
  4. query.where(name.eq("bigBird"));
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(1, cursor.getCount());
  8. cursor.moveToFirst();
  9. Employee employee = new Employee(cursor);
  10. assertEquals("bigBird", employee.getName());
  11. } finally {
  12. cursor.close();
  13. }
  14. }

代码示例来源:origin: yahoo/squidb

  1. public void testLimitAndOffsetWithExpressions() {
  2. // limit = 1 + (count(*) / 4), offset = count(*) / 2
  3. Field<Integer> limit = Function.add(1, Function.divide(
  4. Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 4));
  5. Field<Integer> offset = Function.divide(
  6. Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 2);
  7. Query query = Query.select().orderBy(Employee.NAME.asc()).limit(limit, offset);
  8. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  9. try {
  10. assertEquals(2, cursor.getCount());
  11. cursor.moveToFirst();
  12. assertEquals(elmo, new Employee(cursor));
  13. cursor.moveToNext();
  14. assertEquals(ernie, new Employee(cursor));
  15. } finally {
  16. cursor.close();
  17. }
  18. }

相关文章