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

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

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

Dao.executeRaw介绍

[英]Run a raw execute SQL statement to the database. The arguments are optional but can be set with strings to expand ? type of SQL. If you have no arguments, you may want to call #executeRawNoArgs(String).
[中]对数据库运行原始的execute SQL语句。参数是可选的,但可以使用字符串进行设置以展开?SQL的类型。如果没有参数,则可能需要调用#executeRawNoArgs(String)。

代码示例

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

  1. private boolean migrateFrom1To2() {
  2. try {
  3. Logger.getLogger(AuthorizedUserRepository.class).info("Starting " + VERSION_ENTITY_NAME + " DB migration from version 1 to version 2");
  4. dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN active BOOLEAN DEFAULT true;");
  5. dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN lockedUntil DATETIME;");
  6. dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN chatLockedUntil DATETIME;");
  7. dao.executeRaw("ALTER TABLE authorized_user ADD COLUMN lastConnection DATETIME;");
  8. RepositoryUtil.updateVersion(dao.getConnectionSource(), VERSION_ENTITY_NAME, DB_VERSION);
  9. Logger.getLogger(AuthorizedUserRepository.class).info("Migration finished.");
  10. return true;
  11. } catch (SQLException ex) {
  12. Logger.getLogger(AuthorizedUserRepository.class).error("Error while migrating from version 1 to version 2 - ", ex);
  13. return false;
  14. }
  15. }
  16. }

代码示例来源:origin: stackoverflow.com

  1. @Override
  2. public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
  3. if(oldVersion == 1) {
  4. try {
  5. Dao dao = getCardDao();
  6. dao.executeRaw("ALTER TABLE `Card` ADD COLUMN ccv STRING;");
  7. dao.executeRaw("ALTER TABLE `Card` ADD COLUMN validFrom STRING;");
  8. } catch (SQLException e) {
  9. Log.e(DatabaseHelper.class.getName(), "Error ", e);
  10. }
  11. }
  12. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. private void executeSqlQuery(String sqlQuery) {
  2. try {
  3. long startTime = System.currentTimeMillis();
  4. int deleteCount = DaoUtils.getMetricsDoubleTypeDeviceDao().getDao().executeRaw(sqlQuery);
  5. _logger.debug("Sql Query[{}], Deletion count:{}, Time taken:{} ms", sqlQuery, deleteCount,
  6. System.currentTimeMillis() - startTime);
  7. } catch (SQLException ex) {
  8. _logger.error("Exception when executing query[{}] ", sqlQuery, ex);
  9. }
  10. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. @Override
  2. public void dropSequence(String sequenceName) throws SQLException {
  3. int dropCount = DaoUtils.getUserDao().getDao()
  4. .executeRaw("DROP SEQUENCE IF EXISTS " + getSequenceName(sequenceName) + " CASCADE");
  5. _logger.info("Dropped sequence:{}, drop count:{}", sequenceName, dropCount);
  6. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. @Override
  2. public void dropSequence(String sequenceName) throws SQLException {
  3. int dropCount = DaoUtils.getUserDao().getDao()
  4. .executeRaw("DROP SEQUENCE IF EXISTS " + getSequenceName(sequenceName));
  5. _logger.info("Dropped sequence:{}, drop count:{}", sequenceName, dropCount);
  6. }
  7. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. @Override
  2. public void dropSequence(String sequenceName) throws SQLException {
  3. int dropCount = DaoUtils.getUserDao().getDao()
  4. .executeRaw("DROP SEQUENCE IF EXISTS " + getSequenceName(sequenceName));
  5. _logger.info("Dropped sequence:{}, drop count:{}", sequenceName, dropCount);
  6. }
  7. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. public void alterColumn(String tableName, String columnName, String columnDefinition) throws SQLException {
  2. int alterCount = DaoUtils.getUserDao().getDao().executeRaw("ALTER TABLE "
  3. + tableName + " ALTER COLUMN " + getColumnName(columnName) + " TYPE " + columnDefinition);
  4. _logger.debug("Altered column:{}, columnDefinition:{}, table:{}, add count:{}",
  5. columnName, columnDefinition, tableName, alterCount);
  6. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. public void dropTable(String tableName) throws SQLException {
  2. if (hasTable(tableName)) {
  3. int dropCount = DaoUtils.getUserDao().getDao().executeRaw("DROP TABLE " + getTableName(tableName));
  4. _logger.debug("Dropped table:{}, drop count:{}", tableName, dropCount);
  5. } else {
  6. _logger.warn("Selected table[{}] not found!", tableName);
  7. }
  8. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. public void renameTable(String tableName, String newTableName) throws SQLException {
  2. if (hasTable(tableName)) {
  3. int changeCount = DaoUtils.getUserDao().getDao().executeRaw(
  4. "ALTER TABLE " + getTableName(tableName) + " RENAME TO " + getTableName(newTableName));
  5. _logger.debug("Renamed table:{}, NewTable:{}, Change count:{}", tableName, newTableName, changeCount);
  6. } else {
  7. _logger.warn("Selected table[{}] not found!", tableName);
  8. }
  9. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. public void alterColumn(String tableName, String columnName, String columnDefinition) throws SQLException {
  2. int alterCount = DaoUtils.getUserDao().getDao().executeRaw("ALTER TABLE "
  3. + getTableName(tableName) + " MODIFY COLUMN " + getColumnName(columnName) + " " + columnDefinition);
  4. _logger.debug("Altered column:{}, columnDefinition:{}, table:{}, add count:{}",
  5. columnName, columnDefinition, tableName, alterCount);
  6. }

代码示例来源:origin: org.mycontroller.standalone/mycontroller-core

  1. public void renameTable(String tableName, String newTableName) throws SQLException {
  2. if (hasTable(tableName)) {
  3. int changeCount = DaoUtils.getUserDao().getDao().executeRaw(
  4. "RENAME TABLE " + getTableName(tableName) + " TO " + getColumnName(newTableName));
  5. _logger.debug("Renamed table:{}, NewTable:{}, Change count:{}", tableName, newTableName, changeCount);
  6. } else {
  7. _logger.warn("Selected table[{}] not found!", tableName);
  8. }
  9. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. public void renameTable(String tableName, String newTableName) throws SQLException {
  2. if (hasTable(tableName)) {
  3. int changeCount = DaoUtils.getUserDao().getDao().executeRaw(
  4. "RENAME TABLE " + getTableName(tableName) + " TO " + getColumnName(newTableName));
  5. _logger.debug("Renamed table:{}, NewTable:{}, Change count:{}", tableName, newTableName, changeCount);
  6. } else {
  7. _logger.warn("Selected table[{}] not found!", tableName);
  8. }
  9. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. public void renameColumn(String tableName, String oldColumnName, String newColumnName) throws SQLException {
  2. if (hasColumn(tableName, oldColumnName)) {
  3. int dropCount = DaoUtils.getUserDao().getDao().executeRaw(
  4. "ALTER TABLE " + getTableName(tableName) + " RENAME COLUMN "
  5. + getColumnName(oldColumnName) + " TO " + getColumnName(newColumnName));
  6. _logger.debug("Renamed OldColumn:{}, NewColumn:{}, Table:{}, Drop count:{}", oldColumnName, newColumnName,
  7. tableName, dropCount);
  8. } else {
  9. _logger.warn("Selected column[{}] not found! Table:{}", oldColumnName, tableName);
  10. }
  11. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. public void addColumn(String tableName, String columnName, String columnDefinition) throws SQLException {
  2. if (!hasColumn(tableName, columnName)) {
  3. int addCount = DaoUtils
  4. .getUserDao().getDao().executeRaw(
  5. "ALTER TABLE " + getTableName(tableName) + " ADD COLUMN " + getColumnName(columnName)
  6. + " " + columnDefinition);
  7. _logger.debug("Added column:{}, columnDefinition:{}, table:{}, add count:{}",
  8. columnName, columnDefinition, tableName, addCount);
  9. }
  10. }

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

  1. @Test
  2. public void testExecuteRaw() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. expect(dao.executeRaw(null)).andReturn(0);
  7. replay(dao);
  8. rtDao.executeRaw(null);
  9. verify(dao);
  10. }

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

  1. @Test(expected = RuntimeException.class)
  2. public void testExecuteRawThrow() throws Exception {
  3. @SuppressWarnings("unchecked")
  4. Dao<Foo, String> dao = (Dao<Foo, String>) createMock(Dao.class);
  5. RuntimeExceptionDao<Foo, String> rtDao = new RuntimeExceptionDao<Foo, String>(dao);
  6. expect(dao.executeRaw(null)).andThrow(new SQLException("Testing catch"));
  7. replay(dao);
  8. rtDao.executeRaw(null);
  9. verify(dao);
  10. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. @Override
  2. public void createIndex(String indexSuffix, String tableName, String columnName) throws SQLException {
  3. if (hasColumn(tableName, columnName)) {
  4. DaoUtils.getUserDao().getDao().executeRaw(
  5. "CREATE INDEX " + getIndexName(indexSuffix, tableName, columnName) + " ON "
  6. + getTableName(tableName) + "(" + getColumnName(columnName) + ")");
  7. }
  8. }

代码示例来源:origin: mycontroller-org/mycontroller

  1. public void createIndex(String indexSuffix, String tableName, String columnName) throws SQLException {
  2. if (hasColumn(tableName, columnName)) {
  3. DaoUtils.getUserDao().getDao().executeRaw(
  4. "CREATE INDEX " + getIndexName(indexSuffix, tableName, columnName) + " ON "
  5. + getTableName(tableName) + "(" + getColumnName(columnName) + ")");
  6. }
  7. }

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

  1. @Test
  2. public void testExecuteRaw() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. Foo foo1 = new Foo();
  5. assertEquals(1, dao.create(foo1));
  6. Foo foo2 = new Foo();
  7. assertEquals(1, dao.create(foo2));
  8. assertEquals(2, dao.queryForAll().size());
  9. dao.executeRaw("TRUNCATE TABLE FOO");
  10. assertEquals(0, dao.queryForAll().size());
  11. }

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

  1. @Test(expected = SQLException.class)
  2. public void testExecuteRawThrow() throws Exception {
  3. Dao<Foo, Integer> dao = createDao(Foo.class, true);
  4. Foo foo = new Foo();
  5. assertEquals(1, dao.create(foo));
  6. DatabaseConnection conn = connectionSource.getReadWriteConnection(FOO_TABLE_NAME);
  7. try {
  8. conn.close();
  9. dao.executeRaw("TRUNCATE TABLE FOO");
  10. } finally {
  11. connectionSource.releaseConnection(conn);
  12. }
  13. }

相关文章