All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.drunkendev.jdbc.JdbcHelper Maven / Gradle / Ivy

/*
 * JdbcHelper.java    Jul 28 2016, 15:39
 *
 * Copyright 2016 Drunken Dev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.drunkendev.jdbc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Optional;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import static java.lang.Character.isWhitespace;


/**
 * Utilities for working with JDBC.
 *
 * Methods within may be for working with {@link JdbcTemplate} or SQL files.
 *
 * @author  Brett Ryan
 * @since   1.0
 */
public class JdbcHelper {

    private static final int INIT = 0;
    private static final int ISA_MINUS = 1;
    private static final int ISI_LINE_COMMENT = 2;
    private static final int ISA_SLASH = 3;
    private static final int ISI_BLOCK_COMMENT = 4;
    private static final int ISA_BCOMMENT_STAR = 5;
    private static final int ISI_DOUBLE_QUOTE = 6;
    private static final int ISI_SINGLE_QUOTE = 7;
    private static final int ISI_SQUARE_QUOTE = 8;

    /**
     * Given an input file path will return the textual contents with comments removed.
     *
     * SQL files may either contain single line comments {@code --} or block comments /* */
     *
     * @param   file
     *          Input stream containing SQL content.
     * @return  Textual content with both line and block comments removed.
     * @since   1.0
     */
    public static String getSql(String file) {
        return getSql(Paths.get(file));
    }

    /**
     * Given an input file path will return the textual contents with comments removed.
     *
     * SQL files may either contain single line comments {@code --} or block comments /* */
     *
     * @param   file
     *          Input stream containing SQL content.
     * @return  Textual content with both line and block comments removed.
     * @since   1.1
     */
    public static String getSql(Path file) {
        try (InputStream stream = Files.newInputStream(file)) {
            return getSql(stream);
        } catch (IOException ex) {
            throw new UncheckedIOException(ex.getMessage(), ex);
        }
    }

    /**
     * Given an input stream will return the textual contents with comments removed.
     *
     * SQL files may either contain single line comments {@code --} or block comments /* */
     *
     * @param   is
     *          Input stream containing SQL content.
     * @return  Textual content with both line and block comments removed.
     */
    public static String getSql(InputStream is) {
        StringBuilder res = new StringBuilder();
        try (BufferedReader r = new BufferedReader(new InputStreamReader(is))) {
            int cc;
            char c;
            int state = INIT;
            boolean prevNewLine = false;

            while ((cc = r.read()) != -1) {
                c = (char) cc;
                switch (state) {
                    case INIT:
                        switch (c) {
                            case '-':
                                state = ISA_MINUS;
                                break;
                            case '/':
                                state = ISA_SLASH;
                                break;
                            case '\'':
                                state = ISI_SINGLE_QUOTE;
                                res.append(c);
                                break;
                            case '"':
                                state = ISI_DOUBLE_QUOTE;
                                res.append(c);
                                break;
                            case '[':
                                state = ISI_SQUARE_QUOTE;
                                res.append(c);
                                break;
                            default:
                                if (c == '\n' || c == '\r') {
                                    if (!prevNewLine && res.length() > 0) {
                                        res.append('\n');
                                        prevNewLine = true;
                                    }
                                } else if (isWhitespace(c)) {
                                    if (res.length() > 0) {
                                        res.append(c);
                                        prevNewLine = false;
                                    }
                                } else {
                                    res.append(c);
                                    prevNewLine = false;
                                }
                                break;
                        }
                        break;
                    case ISA_MINUS:
                        if (c == '-') {
                            state = ISI_LINE_COMMENT;
                        } else {
                            state = INIT;
                            res.append('-');
                            res.append(c);
                        }
                        break;
                    case ISI_LINE_COMMENT:
                        if (c == '\n' || c == '\r') {
                            if (res.length() > 0) {
                                if (!prevNewLine) {
                                    res.append('\n');
                                }
                                prevNewLine = true;
                            }
                            state = INIT;
                        }
                        break;
                    case ISA_SLASH:
                        if (c == '*') {
                            state = ISI_BLOCK_COMMENT;
                        } else {
                            state = INIT;
                            res.append('/');
                            res.append(c);
                            prevNewLine = false;
                        }
                        break;
                    case ISI_BLOCK_COMMENT:
                        if (c == '*') {
                            state = ISA_BCOMMENT_STAR;
                        }
                        break;
                    case ISA_BCOMMENT_STAR:
                        state = c == '/' ? INIT : ISI_BLOCK_COMMENT;
                        break;
                    case ISI_SINGLE_QUOTE:
                        res.append(c);
                        if (c == '\'') {
                            state = INIT;
                        }
                        break;
                    case ISI_DOUBLE_QUOTE:
                        res.append(c);
                        if (c == '"') {
                            state = INIT;
                        }
                        break;
                    case ISI_SQUARE_QUOTE:
                        res.append(c);
                        if (c == ']') {
                            state = INIT;
                        }
                        break;
                }
            }

            switch (state) {
                case ISA_MINUS:
                    res.append('-');
                    break;
                case ISA_SLASH:
                    res.append('/');
                    break;
            }
        } catch (IOException ex) {
            throw new UncheckedIOException(ex.getMessage(), ex);
        }
        return res.toString();
    }

    public static RowMapper booleanMapper(int col) {
        return (rs, i) -> rs.getBoolean(col);
    }

    public static RowMapper byteMapper(int col) {
        return (rs, i) -> rs.getByte(col);
    }

    public static RowMapper dateMapper(int col) {
        return (rs, i) -> rs.getDate(col);
    }

    public static RowMapper floatMapper(int col) {
        return (rs, i) -> rs.getFloat(col);
    }

    public static RowMapper doubleMapper(int col) {
        return (rs, i) -> rs.getDouble(col);
    }

    public static RowMapper intMapper(int col) {
        return (rs, i) -> rs.getInt(col);
    }

    public static RowMapper longMapper(int col) {
        return (rs, i) -> rs.getLong(col);
    }

    public static RowMapper nstringMapper(int col) {
        return (rs, i) -> rs.getNString(col);
    }

    public static  RowMapper objectMapper(int col) {
        return (rs, i) -> (T) rs.getObject(col);
    }

    public static RowMapper shortMapper(int col) {
        return (rs, i) -> rs.getShort(col);
    }

    public static RowMapper stringMapper(int col) {
        return (rs, i) -> rs.getString(col);
    }

    public static RowMapper




© 2015 - 2024 Weber Informatics LLC | Privacy Policy