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

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

Source Link

Document

Calls query after checking the parameters to ensure nothing is null.

Usage

From source file:azkaban.project.JdbcProjectLoader.java

/**
 * Get all the logs for a given project//from w  ww .  ja v  a2s.co m
 *
 * @param project
 * @return
 * @throws ProjectManagerException
 */
public List<ProjectLogEvent> getProjectEvents(Project project, int num, int skip)
        throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    ProjectLogsResultHandler logHandler = new ProjectLogsResultHandler();
    List<ProjectLogEvent> events = null;
    try {
        events = runner.query(ProjectLogsResultHandler.SELECT_PROJECT_EVENTS_ORDER, logHandler, project.getId(),
                num, skip);
    } catch (SQLException e) {
        logger.error(e);
    }

    return events;
}

From source file:azkaban.project.JdbcProjectLoader.java

@Override
public Props fetchProjectProperty(int projectId, int projectVer, String propsName)
        throws ProjectManagerException {
    QueryRunner runner = createQueryRunner();

    ProjectPropertiesResultsHandler handler = new ProjectPropertiesResultsHandler();
    try {/*from w  w w .  j a  va  2s.  co m*/
        List<Pair<String, Props>> properties = runner.query(
                ProjectPropertiesResultsHandler.SELECT_PROJECT_PROPERTY, handler, projectId, projectVer,
                propsName);

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

        return properties.get(0).getSecond();
    } catch (SQLException e) {
        throw new ProjectManagerException("Error fetching property " + propsName, e);
    }
}

From source file:azkaban.project.JdbcProjectLoader.java

private ProjectFileHandler getUploadedFile(Connection connection, int projectId, int version)
        throws ProjectManagerException {
    QueryRunner runner = new QueryRunner();
    ProjectVersionResultHandler pfHandler = new ProjectVersionResultHandler();

    List<ProjectFileHandler> projectFiles = null;
    try {/*from   w w  w.j  a v  a  2 s .  c  o  m*/
        projectFiles = runner.query(connection, ProjectVersionResultHandler.SELECT_PROJECT_VERSION, pfHandler,
                projectId, version);
    } catch (SQLException e) {
        logger.error(e);
        throw new ProjectManagerException("Query for uploaded file for project id " + projectId + " failed.",
                e);
    }
    if (projectFiles == null || projectFiles.isEmpty()) {
        return null;
    }

    ProjectFileHandler projHandler = projectFiles.get(0);
    int numChunks = projHandler.getNumChunks();
    BufferedOutputStream bStream = null;
    File file = null;
    try {
        try {
            file = File.createTempFile(projHandler.getFileName(), String.valueOf(version), tempDir);

            bStream = new BufferedOutputStream(new FileOutputStream(file));
        } catch (IOException e) {
            throw new ProjectManagerException("Error creating temp file for stream.");
        }

        int collect = 5;
        int fromChunk = 0;
        int toChunk = collect;
        do {
            ProjectFileChunkResultHandler chunkHandler = new ProjectFileChunkResultHandler();
            List<byte[]> data = null;
            try {
                data = runner.query(connection, ProjectFileChunkResultHandler.SELECT_PROJECT_CHUNKS_FILE,
                        chunkHandler, projectId, version, fromChunk, toChunk);
            } catch (SQLException e) {
                logger.error(e);
                throw new ProjectManagerException("Query for uploaded file for " + projectId + " failed.", e);
            }

            try {
                for (byte[] d : data) {
                    bStream.write(d);
                }
            } catch (IOException e) {
                throw new ProjectManagerException("Error writing file", e);
            }

            // Add all the bytes to the stream.
            fromChunk += collect;
            toChunk += collect;
        } while (fromChunk <= numChunks);
    } finally {
        IOUtils.closeQuietly(bStream);
    }

    // Check md5.
    byte[] md5 = null;
    try {
        md5 = Md5Hasher.md5Hash(file);
    } catch (IOException e) {
        throw new ProjectManagerException("Error getting md5 hash.", e);
    }

    if (Arrays.equals(projHandler.getMd5Hash(), md5)) {
        logger.info("Md5 Hash is valid");
    } else {
        throw new ProjectManagerException("Md5 Hash failed on retrieval of file");
    }

    projHandler.setLocalFile(file);
    return projHandler;
}

From source file:azkaban.executor.JdbcExecutorLoader.java

/**
 * {@inheritDoc}/*www  .j  a  v a 2  s  . c  o  m*/
 *
 * @see azkaban.executor.ExecutorLoader#getExecutorEvents(azkaban.executor.Executor,
 *      int, int)
 */
@Override
public List<ExecutorLogEvent> getExecutorEvents(Executor executor, int num, int offset)
        throws ExecutorManagerException {
    QueryRunner runner = createQueryRunner();

    ExecutorLogsResultHandler logHandler = new ExecutorLogsResultHandler();
    List<ExecutorLogEvent> events = null;
    try {
        events = runner.query(ExecutorLogsResultHandler.SELECT_EXECUTOR_EVENTS_ORDER, logHandler,
                executor.getId(), num, offset);
    } catch (SQLException e) {
        throw new ExecutorManagerException("Failed to fetch events for executor id : " + executor.getId(), e);
    }

    return events;
}

From source file:nl.opengeogroep.dbk.DBKAPI.java

/**
 * Method for requesting a single DBKObject
 * @param request The HttpServletRequest of this request
 * @param method Method containing possible (mandatory!) parameters: the id
 * @return An JSONObject representing the requested DBKObject, of an empty JSONObject if none is found
 * @throws Exception /*from  w  ww. ja  va2 s .c  om*/
 */
private JSONObject processObjectRequest(HttpServletRequest request, String method) throws Exception {
    JSONObject json = new JSONObject();
    boolean hasSrid = request.getParameter(PARAMETER_SRID) != null;
    Connection conn = getConnection();
    if (conn == null) {
        throw new Exception("Connection could not be established");
    }
    MapHandler h = new MapHandler();
    QueryRunner run = new QueryRunner();

    String idString = null;
    Integer id = null;
    try {
        idString = method.substring(method.indexOf(OBJECT) + OBJECT.length(), method.indexOf(JSON));
        id = Integer.parseInt(idString);
    } catch (NumberFormatException ex) {
        throw new IllegalArgumentException("Given id not correct, should be an integer. Was: " + idString);
    }
    try {
        Map<String, Object> feature;
        if (hasSrid) {
            String sridString = request.getParameter(PARAMETER_SRID);
            Integer srid = Integer.parseInt(sridString);
            feature = run.query(conn, "select \"DBKObject\" from dbk.dbkobject_json(?,?)", h, id, srid);
        } else {
            feature = run.query(conn, "select \"DBKObject\" from dbk.dbkobject_json(?)", h, id);
        }
        if (feature == null) {
            throw new IllegalArgumentException("Given id didn't yield any results.");
        }
        JSONObject pgObject = new JSONObject(feature.get("DBKObject"));
        json = new JSONObject();
        json.put("DBKObject", new JSONObject(pgObject.getString("value")));

    } finally {
        DbUtils.close(conn);
    }
    return json;
}

From source file:nl.opengeogroep.dbk.DBKAPI.java

private JSONObject processGebiedRequest(HttpServletRequest request, String method) throws Exception {
    JSONObject json = new JSONObject();
    boolean hasSrid = request.getParameter(PARAMETER_SRID) != null;
    Connection conn = getConnection();
    if (conn == null) {
        throw new Exception("Connection could not be established");
    }/*from   w w  w .  j  a va 2  s  .co m*/
    MapHandler h = new MapHandler();
    QueryRunner run = new QueryRunner();

    String idString = null;
    Integer id = null;
    try {
        idString = method.substring(method.indexOf(GEBIED) + GEBIED.length(), method.indexOf(JSON));
        id = Integer.parseInt(idString);
    } catch (NumberFormatException ex) {
        throw new IllegalArgumentException("Given id not correct, should be an integer. Was: " + idString);
    }
    try {
        Map<String, Object> feature;
        if (hasSrid) {
            String sridString = request.getParameter(PARAMETER_SRID);
            Integer srid = Integer.parseInt(sridString);
            feature = run.query(conn, "select \"DBKGebied\" from dbk.dbkgebied_json(?,?)", h, id, srid);
        } else {
            feature = run.query(conn, "select \"DBKGebied\" from dbk.dbkgebied_json(?)", h, id);
        }
        if (feature == null) {
            throw new IllegalArgumentException("Given id didn't yield any results.");
        }
        JSONObject pgObject = new JSONObject(feature.get("DBKGebied"));
        json = new JSONObject();
        json.put("DBKGebied", new JSONObject(pgObject.getString("value")));

    } finally {
        DbUtils.close(conn);
    }
    return json;
}