com.yahoo.squidb.sql.Query.from()方法的使用及代码示例

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

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

Query.from介绍

[英]Set the SqlTable this query selects from
[中]设置此查询从中选择的SqlTable

代码示例

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

  1. private Query inferTableForQuery(Class<? extends AbstractModel> modelClass, Query query) {
  2. if (!query.hasTable() && modelClass != null) {
  3. SqlTable<?> table = getSqlTable(modelClass);
  4. if (table == null) {
  5. throw new IllegalArgumentException("Query has no FROM clause and model class "
  6. + modelClass.getSimpleName() + " has no associated table");
  7. }
  8. query = query.from(table); // If argument was frozen, we may get a new object
  9. }
  10. return query;
  11. }

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

  1. /**
  2. * Construct a new Select statement that queries all the {@link Field Fields} of the given {@link View}
  3. *
  4. * @param view the View to select from
  5. * @return a new Query object
  6. */
  7. public static Query fromView(View view) {
  8. return Query.select(view.qualifiedFields()).from(view);
  9. }

代码示例来源: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. /**
  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. 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. * 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. public void testObservableEmitsCustomObject() {
  2. final AtomicBoolean objectsMatch = new AtomicBoolean(false);
  3. final Query originalQuery = Query.select().from(TestModel.TABLE);
  4. Observable<Query> observable = database.observeTableAndEmit(TestModel.TABLE, originalQuery, true);
  5. observable.subscribe(new Action1<Query>() {
  6. @Override
  7. public void call(Query query) {
  8. objectsMatch.set(originalQuery == query);
  9. }
  10. });
  11. assertTrue(objectsMatch.get());
  12. }

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

  1. @Override
  2. public void run() {
  3. database.query(TestViewModel.class, Query.select().from(TestViewModel.VIEW));
  4. }
  5. });

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

  1. @Override
  2. public void run() {
  3. database.query(TestModel.class, Query.select().from(TestModel.TABLE));
  4. }
  5. });

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

  1. public void testAllFields() {
  2. Query query = Query.select().from(TestViewModel.VIEW)
  3. .leftJoin(Thing.TABLE, TestViewModel.TEST_MODEL_ID.eq(Thing.ID));
  4. List<Field<?>> fields = query.getFields();
  5. for (Property<?> p : TestViewModel.PROPERTIES) {
  6. assertTrue(fields.contains(p));
  7. }
  8. for (Property<?> p : Thing.PROPERTIES) {
  9. assertTrue(fields.contains(p));
  10. }
  11. assertEquals(TestViewModel.PROPERTIES.length + Thing.PROPERTIES.length, fields.size());
  12. }

代码示例来源: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 testEnumResolvedUsingName() {
  2. Query query = Query.select(TestModel.SOME_ENUM).from(TestModel.TABLE)
  3. .where(TestModel.SOME_ENUM.eq(TestEnum.APPLE));
  4. CompiledStatement compiledStatement = query.compile(database.getCompileContext());
  5. verifyCompiledSqlArgs(compiledStatement, 1, "APPLE");
  6. }

代码示例来源: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. @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 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. @Override
  2. public void run() {
  3. // insert into testModels (firstName, lastName) select (firstName, lastName, creationDate) from
  4. // testModels where luckyNumber = 9;
  5. Query query = Query.select(TestModel.FIRST_NAME, TestModel.LAST_NAME, TestModel.BIRTHDAY)
  6. .from(TestModel.TABLE)
  7. .where(TestModel.LUCKY_NUMBER.eq(9));
  8. Insert insert = Insert.into(TestModel.TABLE).columns(TestModel.FIRST_NAME, TestModel.LAST_NAME)
  9. .select(query);
  10. insert.compile(database.getCompileContext());
  11. }
  12. }, IllegalStateException.class);

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

  1. public void testIntersect() {
  2. Query query = Query.select().from(Employee.TABLE).where(Employee.MANAGER_ID.eq(1))
  3. .intersect(Query.select().from(Employee.TABLE).where(Employee.ID.eq(2)))
  4. .orderBy(Employee.ID.asc());
  5. SquidCursor<Employee> cursor = database.query(Employee.class, query);
  6. try {
  7. assertEquals(1, cursor.getCount());
  8. cursor.moveToFirst();
  9. assertEquals(cookieMonster, new Employee(cursor));
  10. } finally {
  11. cursor.close();
  12. }
  13. }

代码示例来源: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. 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. public void testInsertWithQuery() {
  2. double pi = Math.PI;
  3. Criterion criterion = Thing.QUX.gt(pi);
  4. int numThingsMatching = database.count(Thing.class, criterion);
  5. // insert into testModels select foo, bar, isAlive from things where qux > 3.1415...;
  6. Query query = Query.select(Thing.FOO, Thing.BAR, Thing.IS_ALIVE).from(Thing.TABLE).where(criterion);
  7. Insert insert = Insert.into(TestModel.TABLE).columns(TestModel.LAST_NAME, TestModel.LUCKY_NUMBER,
  8. TestModel.IS_HAPPY).select(query);
  9. CompiledStatement compiled = insert.compile(database.getCompileContext());
  10. verifyCompiledSqlArgs(compiled, 1, pi);
  11. int testModelsBeforeInsert = database.countAll(TestModel.class);
  12. assertEquals(3, database.insert(insert));
  13. int testModelsAfterInsert = database.countAll(TestModel.class);
  14. assertEquals(testModelsBeforeInsert + numThingsMatching, testModelsAfterInsert);
  15. }

相关文章