Example usage for org.apache.commons.dbutils QueryRunner query

List of usage examples for org.apache.commons.dbutils QueryRunner query

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner query.

Prototype

public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException 

Source Link

Document

Execute an SQL SELECT query with replacement parameters.

Usage

From source file:azkaban.executor.JdbcExecutorLoader.java

@Override
public List<ExecutableJobInfo> fetchJobInfoAttempts(int execId, String jobId) throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();

    try {/*from   w  w w.  j  a  v  a  2 s .  c  om*/
        List<ExecutableJobInfo> info = runner.query(FetchExecutableJobHandler.FETCH_EXECUTABLE_NODE_ATTEMPTS,
                new FetchExecutableJobHandler(), execId, jobId);
        if (info == null || info.isEmpty()) {
            return null;
        }
        return info;
    } catch (SQLException e) {
        throw new ExecutorManagerException("Error querying job info " + jobId, e);
    }
}

From source file:azkaban.project.JdbcProjectLoader.java

@Override
public List<Flow> fetchAllProjectFlows(Project project) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();
    ProjectFlowsResultHandler handler = new ProjectFlowsResultHandler();

    List<Flow> flows = null;
    try {/*from   w w w. ja  v  a2s  .  c om*/
        flows = runner.query(ProjectFlowsResultHandler.SELECT_ALL_PROJECT_FLOWS, handler, project.getId(),
                project.getVersion());
    } catch (SQLException e) {
        throw new ProjectManagerException(
                "Error fetching flows from project " + project.getName() + " version " + project.getVersion(),
                e);
    }

    return flows;
}

From source file:azkaban.project.JdbcProjectLoader.java

@Override
public Map<String, Props> fetchProjectProperties(int projectId, int version) throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    ProjectPropertiesResultsHandler handler = new ProjectPropertiesResultsHandler();
    try {/*ww w .j  a va  2  s.c  o m*/
        List<Pair<String, Props>> properties = runner
                .query(ProjectPropertiesResultsHandler.SELECT_PROJECT_PROPERTIES, handler, projectId, version);

        if (properties == null || properties.isEmpty()) {
            return null;
        }

        HashMap<String, Props> props = new HashMap<String, Props>();
        for (Pair<String, Props> pair : properties) {
            props.put(pair.getFirst(), pair.getSecond());
        }
        return props;
    } catch (SQLException e) {
        throw new ProjectManagerException("Error fetching properties", e);
    }
}

From source file:azkaban.executor.JdbcExecutorLoader.java

@Override
public int fetchNumExecutableFlows(int projectId, String flowId) throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();

    IntHandler intHandler = new IntHandler();
    try {//from ww  w.j  a v  a  2  s  . c o m
        int count = runner.query(IntHandler.NUM_FLOW_EXECUTIONS, intHandler, projectId, flowId);
        return count;
    } catch (SQLException e) {
        throw new ExecutorManagerException("Error fetching num executions", e);
    }
}

From source file:azkaban.executor.JdbcExecutorLoader.java

@Override
public int fetchNumExecutableNodes(int projectId, String jobId) throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();

    IntHandler intHandler = new IntHandler();
    try {/*from  www  .  j  a v a 2  s.c o  m*/
        int count = runner.query(IntHandler.NUM_JOB_EXECUTIONS, intHandler, projectId, jobId);
        return count;
    } catch (SQLException e) {
        throw new ExecutorManagerException("Error fetching num executions", e);
    }
}

From source file:azkaban.project.JdbcProjectLoader.java

private synchronized Project createNewProject(Connection connection, String name, String description,
        User creator) throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    ProjectResultHandler handler = new ProjectResultHandler();

    // See if it exists first.
    try {//from www  .ja v  a 2s. c  om
        List<Project> project = runner.query(connection, ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME,
                handler, name);
        if (!project.isEmpty()) {
            throw new ProjectManagerException("Active project with name " + name + " already exists in db.");
        }
    } catch (SQLException e) {
        logger.error(e);
        throw new ProjectManagerException("Checking for existing project failed. " + name, e);
    }

    final String INSERT_PROJECT = "INSERT INTO projects ( name, active, modified_time, create_time, version, last_modified_by, description, enc_type, settings_blob) values (?,?,?,?,?,?,?,?,?)";
    // Insert project
    try {
        long time = System.currentTimeMillis();
        int i = runner.update(connection, INSERT_PROJECT, name, true, time, time, null, creator.getUserId(),
                description, defaultEncodingType.getNumVal(), null);
        if (i == 0) {
            throw new ProjectManagerException("No projects have been inserted.");
        }
        connection.commit();

    } catch (SQLException e) {
        logger.error(INSERT_PROJECT + " failed.");
        try {
            connection.rollback();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        throw new ProjectManagerException("Insert project for existing project failed. " + name, e);
    }

    // Do another query to grab and return the project.
    Project project = null;
    try {
        List<Project> projects = runner.query(connection, ProjectResultHandler.SELECT_ACTIVE_PROJECT_BY_NAME,
                handler, name);
        if (projects.isEmpty()) {
            throw new ProjectManagerException("No active project with name " + name + " exists in db.");
        } else if (projects.size() > 1) {
            throw new ProjectManagerException("More than one active project " + name);
        }

        project = projects.get(0);
    } catch (SQLException e) {
        logger.error(e);
        throw new ProjectManagerException("Checking for existing project failed. " + name, e);
    }

    return project;
}

From source file:azkaban.project.JdbcProjectLoader.java

private Project fetchProjectById(Connection connection, int id) throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    // Fetch the project
    Project project = null;//from  w w  w.ja va  2 s .c  o  m
    ProjectResultHandler handler = new ProjectResultHandler();
    try {
        List<Project> projects = runner.query(connection, ProjectResultHandler.SELECT_PROJECT_BY_ID, handler,
                id);
        if (projects.isEmpty()) {
            throw new ProjectManagerException("No project with id " + id + " exists in db.");
        }

        project = projects.get(0);
    } catch (SQLException e) {
        logger.error(ProjectResultHandler.SELECT_PROJECT_BY_ID + " failed.");
        throw new ProjectManagerException("Query for existing project failed. Project " + id, e);
    }

    // Fetch the user permissions
    List<Triple<String, Boolean, Permission>> permissions = fetchPermissionsForProject(connection, project);

    for (Triple<String, Boolean, Permission> perm : permissions) {
        if (perm.getThird().toFlags() != 0) {
            if (perm.getSecond()) {
                project.setGroupPermission(perm.getFirst(), perm.getThird());
            } else {
                project.setUserPermission(perm.getFirst(), perm.getThird());
            }
        }
    }

    return project;
}

From source file:azkaban.project.JdbcProjectLoader.java

private Project fetchProjectByName(Connection connection, String name) throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    // Fetch the project
    Project project = null;//from   w  w w  .  ja v  a 2 s  .c  om
    ProjectResultHandler handler = new ProjectResultHandler();
    try {
        List<Project> projects = runner.query(connection, ProjectResultHandler.SELECT_PROJECT_BY_NAME, handler,
                name);
        if (projects.isEmpty()) {
            throw new ProjectManagerException("No project with name " + name + " exists in db.");
        }

        project = projects.get(0);
    } catch (SQLException e) {
        logger.error(ProjectResultHandler.SELECT_PROJECT_BY_NAME + " failed.");
        throw new ProjectManagerException("Query for existing project failed. Project " + name, e);
    }

    // Fetch the user permissions
    List<Triple<String, Boolean, Permission>> permissions = fetchPermissionsForProject(connection, project);

    for (Triple<String, Boolean, Permission> perm : permissions) {
        if (perm.getThird().toFlags() != 0) {
            if (perm.getSecond()) {
                project.setGroupPermission(perm.getFirst(), perm.getThird());
            } else {
                project.setUserPermission(perm.getFirst(), perm.getThird());
            }
        }
    }

    return project;
}

From source file:azkaban.executor.JdbcExecutorLoader.java

@Override
public List<ExecutableFlow> fetchFlowHistory(int skip, int num) throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();

    FetchExecutableFlows flowHandler = new FetchExecutableFlows();

    try {//from ww w .  j  a  v a  2 s.  c o m
        List<ExecutableFlow> properties = runner.query(FetchExecutableFlows.FETCH_ALL_EXECUTABLE_FLOW_HISTORY,
                flowHandler, skip, num);
        return properties;
    } catch (SQLException e) {
        throw new ExecutorManagerException("Error fetching active flows", e);
    }
}

From source file:azkaban.executor.JdbcExecutorLoader.java

/**
 * {@inheritDoc}// w  w w .  j  a  va2 s  . c o m
 *
 * @see azkaban.executor.ExecutorLoader#fetchExecutor(java.lang.String, int)
 */
@Override
public Executor fetchExecutor(String host, int port) throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();
    FetchExecutorHandler executorHandler = new FetchExecutorHandler();

    try {
        List<Executor> executors = runner.query(FetchExecutorHandler.FETCH_EXECUTOR_BY_HOST_PORT,
                executorHandler, host, port);
        if (executors.isEmpty()) {
            return null;
        } else {
            return executors.get(0);
        }
    } catch (Exception e) {
        throw new ExecutorManagerException(String.format("Error fetching executor %s:%d", host, port), e);
    }
}