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

org.zodiac.sdk.json.util.SQLUtil Maven / Gradle / Ivy

There is a newer version: 1.5.17
Show newest version
package org.zodiac.sdk.json.util;

import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.function.Supplier;

import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
import javax.sql.rowset.serial.SerialException;

import org.zodiac.sdk.json.constants.JSONConstants;
import org.zodiac.sdk.json.exception.JSONSQLRuntimeException;

public abstract class SQLUtil {

    protected SQLUtil() {
    }

    public static String clobToString(Clob clob) {
        if (null == clob)
            return null;
        Reader reader = null;
        StringBuilder buf = new StringBuilder();

        try {
            reader = clob.getCharacterStream();

            char[] chars = new char[2048];
            for (; ; ) {
                int len = reader.read(chars, 0, chars.length);
                if (len < 0) {
                    break;
                }
                buf.append(chars, 0, len);
            }
        } catch (Exception ex) {
            throw new JSONSQLRuntimeException("Read string from Clob failed.", ex);
        }

        String text = buf.toString();
        if (reader != null) {
            try {
                reader.close();
            }catch (Exception ex){
                throw new JSONSQLRuntimeException("Read string from Clob failed.", ex);
            }
        }
        return text;
    }

    public static Clob clob(CharSequence data) throws SerialException, SQLException {
        return clob(null == data ? "" : data.toString());
    }

    public static Clob clob(String data) throws SerialException, SQLException {
        return clob(null == data ? JSONConstants.EMPTY_CHAR_ARRAY : data.toCharArray());
    }

    public static Clob clob(Supplier dataSupplier) throws SerialException, SQLException {
        return clob(null == dataSupplier ? JSONConstants.EMPTY_CHAR_ARRAY : dataSupplier.get());
    }

    public static Clob clob(char[] data) throws SerialException, SQLException {
        return new SerialClob(null == data ? JSONConstants.EMPTY_CHAR_ARRAY : data);
    }

    public static Blob blob(CharSequence data) throws SerialException, SQLException {
        return blob(data, "");
    }

    public static Blob blob(CharSequence data, String charset) throws SerialException, SQLException {
        return blob(data, StringUtil.isNotBlank(charset)  ? Charset.forName(charset) : null);
    }

    public static Blob blob(CharSequence data, Charset charset) throws SerialException, SQLException {
        return blob(null == data ? null : data.toString(), charset);
    }

    public static Blob blob(String data) throws SerialException, SQLException {
        return blob(data, "");
    }

    public static Blob blob(String data, String charset) throws SerialException, SQLException {
        return blob(data, StringUtil.isNotBlank(charset)  ? Charset.forName(charset) : null);
    }

    public static Blob blob(String data, Charset charset) throws SerialException, SQLException {
        return blob(null == data ? JSONConstants.EMPTY_BYTE_ARRAY : data.getBytes(null != charset ? charset : StandardCharsets.UTF_8));
    }

    public static Blob blob(Supplier dataSupplier) throws SerialException, SQLException {
        return blob(null == dataSupplier ? JSONConstants.EMPTY_BYTE_ARRAY : dataSupplier.get());
    }

    public static Blob blob(byte[] data) throws SerialException, SQLException {
        return new SerialBlob(null == data ? JSONConstants.EMPTY_BYTE_ARRAY : data);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy