private boolean removeDbEntry(final String resourceId) { try { final int nAffectedRows = this.databaseOperator.update(SQL_DELETE_RESOURCE_ID, resourceId); return nAffectedRows > 0; } catch (final SQLException e) { log.error("Error while deleting DB metadata resource ID: " + resourceId, e); } return false; }
private boolean getDbStatus() { try { return this.dbOperator.query("SELECT 1", rs -> true); } catch (final SQLException e) { log.error("DB Error", e); } return false; }
private void executeTransaction(final SQLTransaction<Integer> tran) { try { this.dbOperator.transaction(tran); } catch (final SQLException ex) { handleSQLException(ex); } }
@Before public void setUp() throws Exception { this.databaseOperator = mock(DatabaseOperator.class); this.storage = mock(Storage.class); when(this.databaseOperator.query( eq(StorageCleaner.SQL_FETCH_PVR), anyObject(), eq(TEST_PROJECT_ID))) .thenReturn(Arrays.asList("14/14-9.zip", "14/14-8.zip", "14/14-7.zip")); when(this.storage.delete("14/14-8.zip")).thenReturn(true); when(this.storage.delete("14/14-7.zip")).thenReturn(false); when(this.databaseOperator.update(any(), anyVararg())).thenReturn(1); }
.query(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME, handler, name); if (!projects.isEmpty()) { throw new ProjectManagerException( final int numRowsInserted = this.dbOperator.transaction(insertProject); if (numRowsInserted == 0) { throw new ProjectManagerException("No projects have been inserted.");
if (this.dbOperator.getDataSource().allowsOnDuplicateKey()) { final String INSERT_PROJECT_PERMISSION = "INSERT INTO project_permissions (project_id, modified_time, name, permissions, isGroup) values (?,?,?,?,?)" + "ON DUPLICATE KEY UPDATE modified_time = VALUES(modified_time), permissions = VALUES(permissions)"; this.dbOperator .update(INSERT_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup); } else { "MERGE INTO project_permissions (project_id, modified_time, name, permissions, isGroup) KEY (project_id, name) values (?,?,?,?,?)"; this.dbOperator .update(MERGE_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup);
@Before public void setUp() throws Exception { this.queryRunner = mock(QueryRunner.class); this.conn = this.datasource.getConnection(); final DataSource mockDataSource = mock(this.datasource.getClass()); when(this.queryRunner.getDataSource()).thenReturn(mockDataSource); when(mockDataSource.getConnection()).thenReturn(this.conn); this.dbOperator = new DatabaseOperator(this.queryRunner); list.add(index_1); list.add(index_2); // valid query returns correct value when(this.queryRunner.query("select * from blah where ? = ?", this.handler, "id", 2)) .thenReturn(index_2); // If select an non-existing entry, handler returns 0. when(this.queryRunner.query("select * from blah where ? = ?", this.handler, "id", 3)) .thenReturn(0); //If typos, throw Exceptions. doThrow(SQLException.class).when(this.queryRunner) .query("sele * from blah where ? = ?", this.handler, "id", 2); doAnswer(invocation -> { index_1 = 26; return 1; }).when(this.queryRunner).update("update blah set ? = ?", "1", 26); }
.query(ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME, handler, name); if (!projects.isEmpty()) { throw new ProjectManagerException( final int numRowsInserted = this.dbOperator.transaction(insertProject); if (numRowsInserted == 0) { throw new ProjectManagerException("No projects have been inserted.");
if (this.dbOperator.getDataSource().allowsOnDuplicateKey()) { final String INSERT_PROJECT_PERMISSION = "INSERT INTO project_permissions (project_id, modified_time, name, permissions, isGroup) values (?,?,?,?,?)" + "ON DUPLICATE KEY UPDATE modified_time = VALUES(modified_time), permissions = VALUES(permissions)"; this.dbOperator .update(INSERT_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup); } else { "MERGE INTO project_permissions (project_id, modified_time, name, permissions, isGroup) KEY (project_id, name) values (?,?,?,?,?)"; this.dbOperator .update(MERGE_PROJECT_PERMISSION, project.getId(), updateTime, name, perm.toFlags(), isGroup);
public static DatabaseOperator initTestDB() throws Exception { final AzkabanDataSource dataSource = new EmbeddedH2BasicDataSource(); final String sqlScriptsDir = new File("../azkaban-db/src/main/sql/").getCanonicalPath(); final DatabaseSetup setup = new DatabaseSetup(dataSource, sqlScriptsDir); setup.updateDatabase(); return new DatabaseOperator(new QueryRunner(dataSource)); }
void removeActiveExecutableReference(final int execId) throws ExecutorManagerException { final String DELETE = "DELETE FROM active_executing_flows WHERE exec_id=?"; try { this.dbOperator.update(DELETE, execId); } catch (final SQLException e) { throw new ExecutorManagerException( "Error deleting active flow reference " + execId, e); } }
private List<String> fetchResourceIdOrderedList(final int projectId) { try { return this.databaseOperator.query(SQL_FETCH_PVR, rs -> { final List<String> results = new ArrayList<>(); while (rs.next()) { results.add(rs.getString(1)); } return results; }, projectId); } catch (final SQLException e) { log.error("Error performing cleanup of Project: " + projectId, e); } return Collections.emptyList(); } }
public void uploadLogFile(final int execId, final String name, final int attempt, final File... files) throws ExecutorManagerException { final SQLTransaction<Integer> transaction = transOperator -> { uploadLogFile(transOperator, execId, name, attempt, files, this.defaultEncodingType); transOperator.getConnection().commit(); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("uploadLogFile failed.", e); throw new ExecutorManagerException("uploadLogFile failed.", e); } }
public static DatabaseOperator initQuartzDB() throws Exception { final AzkabanDataSource dataSource = new EmbeddedH2BasicDataSource(); final String sqlScriptsDir = new File("../azkaban-web-server/src/test/resources/") .getCanonicalPath(); final DatabaseSetup setup = new DatabaseSetup(dataSource, sqlScriptsDir); setup.updateDatabase(); return new DatabaseOperator(new QueryRunner(dataSource)); }
private void executeUpdate(final String query, final Object... params) { try { this.dbOperator.update(query, params); } catch (final SQLException ex) { handleSQLException(ex); } }
@Override public List<Trigger> getUpdatedTriggers(final long lastUpdateTime) throws TriggerLoaderException { logger.info("Loading triggers changed since " + new DateTime(lastUpdateTime).toString()); final ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler(); try { final List<Trigger> triggers = this.dbOperator .query(GET_UPDATED_TRIGGERS, handler, lastUpdateTime); logger.info("Loaded " + triggers.size() + " triggers."); return triggers; } catch (final SQLException ex) { throw new TriggerLoaderException("Loading triggers from db failed.", ex); } }
@Override public void addProjectVersion( final int projectId, final int version, final File localFile, final String uploader, final byte[] md5, final String resourceId) throws ProjectManagerException { // when one transaction completes, it automatically commits. final SQLTransaction<Integer> transaction = transOperator -> { addProjectToProjectVersions(transOperator, projectId, version, localFile, uploader, md5, resourceId); return 1; }; try { this.dbOperator.transaction(transaction); } catch (final SQLException e) { logger.error("addProjectVersion failed.", e); throw new ProjectManagerException("addProjectVersion failed.", e); } }
private void addExecutorHelper(final String host, final int port) throws ExecutorManagerException { final String INSERT = "INSERT INTO executors (host, port) values (?,?)"; try { this.dbOperator.update(INSERT, host, port); } catch (final SQLException e) { throw new ExecutorManagerException(String.format("Error adding %s:%d ", host, port), e); } }
@Override public List<Trigger> loadTriggers() throws TriggerLoaderException { logger.info("Loading all triggers from db."); final ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler(); try { final List<Trigger> triggers = this.dbOperator.query(GET_ALL_TRIGGERS, handler); logger.info("Loaded " + triggers.size() + " triggers."); return triggers; } catch (final SQLException ex) { throw new TriggerLoaderException("Loading triggers from db failed.", ex); } }
this.dbOperator.transaction(uploadProjectFileTransaction); } catch (final SQLException e) { logger.error("upload project files failed.", e);