com.j256.ormlite.dao.Dao.queryBuilder()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(204)

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

Dao.queryBuilder介绍

[英]Create and return a new query builder object which allows you to build a custom SELECT statement. You call methods on the builder to construct your statement and then call QueryBuilder#prepare() once you are ready to build. This returns a PreparedQuery object which gets passed to #query(PreparedQuery) or #iterator(PreparedQuery).
[中]创建并返回一个新的查询生成器对象,该对象允许您生成自定义SELECT语句。您可以在生成器上调用方法来构造语句,然后在准备好构建后调用QueryBuilder#prepare()。这将返回一个PreparedQuery对象,该对象被传递给#query(PreparedQuery)或#迭代器(PreparedQuery)。

代码示例

代码示例来源:origin: BaronZ88/MinimalistWeather

/**
 * 根据城市查询城市信息
 *
 * @param cityId 城市ID
 * @return city
 * @throws SQLException
 */
public City queryCityById(String cityId) throws SQLException {
  QueryBuilder<City, Integer> queryBuilder = cityDaoOperation.queryBuilder();
  queryBuilder.where().eq(City.CITY_ID_FIELD_NAME, cityId);
  return queryBuilder.queryForFirst();
}

代码示例来源:origin: magefree/mage

public AuthorizedUser getByName(String userName) {
  try {
    QueryBuilder<AuthorizedUser, Object> qb = dao.queryBuilder();
    qb.where().eq("name", new SelectArg(userName));
    List<AuthorizedUser> results = dao.query(qb.prepare());
    if (results.size() == 1) {
      return results.get(0);
    }
    return null;
  } catch (SQLException ex) {
    Logger.getLogger(AuthorizedUserRepository.class).error("Error getting a authorized_user - ", ex);
  }
  return null;
}

代码示例来源:origin: magefree/mage

public List<CardInfo> getMissingCards(List<String> classNames) {
  try {
    QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
    queryBuilder.where().not().in("className", classNames);
    return cardDao.query(queryBuilder.prepare());
  } catch (SQLException ex) {
    Logger.getLogger(CardRepository.class).error("Error getting missing cards from DB : " + ex);
  }
  return Collections.emptyList();
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testQueryForStringInt() throws Exception {
  Dao<Foo, String> fooDao = createTestData();
  QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
  Where<Foo, String> where = qb.where();
  // testing the val column with a integer as a string
  where.eq(Foo.VAL_COLUMN_NAME, Integer.toString(foo1.val));
  List<Foo> list = fooDao.query(qb.prepare());
  assertEquals(1, list.size());
  assertEquals(foo1, list.get(0));
}

代码示例来源:origin: j256/ormlite-core

@Test(expected = IllegalArgumentException.class)
public void testRawArgsNoColumnName() throws Exception {
  Dao<Foo, String> dao = createDao(Foo.class, true);
  dao.queryBuilder().where().raw("id = ?", new SelectArg(7));
}

代码示例来源:origin: magefree/mage

public static boolean isDatabaseObsolete(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
  TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
  Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
  QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
  queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("version", version);
  List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
  if (dbVersions.isEmpty()) {
    DatabaseVersion dbVersion = new DatabaseVersion();
    dbVersion.setEntity(entityName);
    dbVersion.setVersion(version);
    dbVersionDao.create(dbVersion);
  }
  return dbVersions.isEmpty();
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testQueryPrepared() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  Foo foo1 = new Foo();
  assertEquals(1, dao.create(foo1));
  Foo foo2 = new Foo();
  assertEquals(1, dao.create(foo2));
  Where<Foo, Integer> qb = dao.queryBuilder().where().eq(Foo.ID_COLUMN_NAME, foo2.id);
  List<Foo> results = dao.query(qb.prepare());
  assertEquals(1, results.size());
  assertEquals(foo2.id, results.get(0).id);
}

代码示例来源:origin: magefree/mage

public AuthorizedUser getByEmail(String userName) {
  try {
    QueryBuilder<AuthorizedUser, Object> qb = dao.queryBuilder();
    qb.where().eq("email", new SelectArg(userName));
    List<AuthorizedUser> results = dao.query(qb.prepare());
    if (results.size() == 1) {
      return results.get(0);
    }
    return null;
  } catch (SQLException ex) {
    Logger.getLogger(AuthorizedUserRepository.class).error("Error getting a authorized_user - ", ex);
  }
  return null;
}

代码示例来源:origin: j256/ormlite-core

@Test(expected = IllegalArgumentException.class)
public void testInnerQuerySubQueryPrepared() throws Exception {
  Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
  fooDao.queryBuilder()
      .where()
      .in(Foo.ID_COLUMN_NAME,
          // this is a problem because prepare() returns a PreparedStmt not a QueryBuilder
          fooDao.queryBuilder().selectColumns(Foo.ID_COLUMN_NAME).prepare())
      .query();
}

代码示例来源:origin: magefree/mage

public UserStats getUser(String userName) {
  try {
    QueryBuilder<UserStats, Object> qb = dao.queryBuilder();
    qb.limit(1L).where().eq("userName", userName);
    List<UserStats> users = dao.query(qb.prepare());
    if (!users.isEmpty()) {
      return users.get(0);
    }
  } catch (SQLException ex) {
    Logger.getLogger(UserStatsRepository.class).error("Error getting a user from DB - ", ex);
  }
  return null;
}

代码示例来源:origin: magefree/mage

public static boolean isNewBuildRun(ConnectionSource connectionSource, String entityName, Class clazz) throws SQLException {
  // build time checks only for releases, not runtime (e.g. IDE debug)
  // that's check uses for cards db cleanup on new version/build
  String currentBuild = JarVersion.getBuildTime(clazz);
  if (!JarVersion.isBuildTimeOk(currentBuild)) {
    return false;
  }
  TableUtils.createTableIfNotExists(connectionSource, DatabaseBuild.class);
  Dao<DatabaseBuild, Object> dbBuildDao = DaoManager.createDao(connectionSource, DatabaseBuild.class);
  QueryBuilder<DatabaseBuild, Object> queryBuilder = dbBuildDao.queryBuilder();
  queryBuilder.where().eq("entity", new SelectArg(entityName)).and().eq("last_build", currentBuild);
  List<DatabaseBuild> dbBuilds = dbBuildDao.query(queryBuilder.prepare());
  if (dbBuilds.isEmpty()) {
    DatabaseBuild dbBuild = new DatabaseBuild();
    dbBuild.setEntity(entityName);
    dbBuild.setLastBuild(currentBuild);
    dbBuildDao.create(dbBuild);
  }
  return dbBuilds.isEmpty();
}

代码示例来源:origin: magefree/mage

public List<TableRecord> getAfter(long endTimeMs) {
  try {
    QueryBuilder<TableRecord, Object> qb = dao.queryBuilder();
    qb.where().gt("endTimeMs", endTimeMs);
    qb.orderBy("endTimeMs", true);
    return dao.query(qb.prepare());
  } catch (SQLException ex) {
    Logger.getLogger(TableRecordRepository.class).error("Error getting table_records from DB - ", ex);
  }
  return null;
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testReservedWords() throws Exception {
  Dao<Reserved, Integer> dao = createDao(Reserved.class, true);
  QueryBuilder<Reserved, Integer> sb = dao.queryBuilder();
  sb.where().eq(Reserved.FIELD_NAME_GROUP, "something");
  sb.query();
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testMissingAndArg() throws Exception {
  Dao<Foo, String> fooDao = createDao(Foo.class, false);
  QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
  try {
    qb.where().and();
    fail("expected exception");
  } catch (IllegalStateException e) {
    // expected
  }
}

代码示例来源:origin: magefree/mage

public List<ExpansionInfo> getSetsFromBlock(String blockName) {
  List<ExpansionInfo> sets = new LinkedList<>();
  try {
    QueryBuilder<ExpansionInfo, Object> qb = expansionDao.queryBuilder();
    qb.where().eq("blockName", new SelectArg(blockName));
    return expansionDao.query(qb.prepare());
  } catch (SQLException ex) {
    logger.error(ex);
  }
  return sets;
}

代码示例来源:origin: magefree/mage

public static void updateVersion(ConnectionSource connectionSource, String entityName, long version) throws SQLException {
  TableUtils.createTableIfNotExists(connectionSource, DatabaseVersion.class);
  Dao<DatabaseVersion, Object> dbVersionDao = DaoManager.createDao(connectionSource, DatabaseVersion.class);
  QueryBuilder<DatabaseVersion, Object> queryBuilder = dbVersionDao.queryBuilder();
  queryBuilder.where().eq("entity", new SelectArg(entityName));
  List<DatabaseVersion> dbVersions = dbVersionDao.query(queryBuilder.prepare());
  if (!dbVersions.isEmpty()) {
    DeleteBuilder<DatabaseVersion, Object> deleteBuilder = dbVersionDao.deleteBuilder();
    deleteBuilder.where().eq("entity", new SelectArg(entityName));
    deleteBuilder.delete();
  }
  DatabaseVersion databaseVersion = new DatabaseVersion();
  databaseVersion.setEntity(entityName);
  databaseVersion.setVersion(version);
  dbVersionDao.create(databaseVersion);
}

代码示例来源:origin: magefree/mage

public boolean cardExists(String className) {
  try {
    if (classNames == null) {
      QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
      qb.distinct().selectColumns("className").where().isNotNull("className");
      List<CardInfo> results = cardDao.query(qb.prepare());
      classNames = new TreeSet<>();
      for (CardInfo card : results) {
        classNames.add(card.getClassName());
      }
    }
    return classNames.contains(className);
  } catch (SQLException ex) {
  }
  return false;
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testExists() throws Exception {
  Dao<Foo, String> fooDao = createTestData();
  QueryBuilder<Foo, String> innerQb = fooDao.queryBuilder();
  innerQb.where().idEq(foo1.id);
  QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
  qb.where().exists(innerQb);
  List<Foo> results = fooDao.query(qb.prepare());
  assertEquals(2, results.size());
  assertEquals(foo1, results.get(0));
  assertEquals(foo2, results.get(1));
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testNoWhereOperations() throws Exception {
  Dao<Foo, String> fooDao = createTestData();
  QueryBuilder<Foo, String> qb = fooDao.queryBuilder();
  qb.where();
  try {
    fooDao.query(qb.prepare());
    fail("expected exception");
  } catch (IllegalStateException e) {
    // expected
  }
}

代码示例来源:origin: magefree/mage

public List<CardInfo> findCards(String name) {
  try {
    QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
    queryBuilder.where().eq("name", new SelectArg(name));
    return cardDao.query(queryBuilder.prepare());
  } catch (SQLException ex) {
  }
  return Collections.emptyList();
}

相关文章