Search in sources :

Example 1 with CUBRIDProxyException

use of com.cubrid.jdbc.proxy.manage.CUBRIDProxyException in project cubrid-manager by CUBRID.

the class DBAttrTypeFormatter method formatMuchValue.

/**
	 * Format much value to JDBC object, this value probably is from file, if it
	 * is file, convert it for JDBC object according to JDBC type
	 * 
	 * @param str The much value
	 * @param type The JDBC type
	 * @param conn The Connection
	 * @param dbCharSet The database charset
	 * @param fileCharSet The file charset
	 * @param isUseNULLValueSetting
	 * @return the real JDBC object
	 */
public static Object formatMuchValue(String str, String type, Connection conn, String dbCharSet, String fileCharSet, boolean isUseNULLValueSetting) {
    String upperType = type.trim().toUpperCase();
    boolean isString = upperType.startsWith(DataType.DATATYPE_VARCHAR) || upperType.startsWith(DataType.DATATYPE_CHAR) || upperType.startsWith(DataType.DATATYPE_STRING);
    boolean isByte = upperType.startsWith(DataType.DATATYPE_BIT_VARYING) || upperType.startsWith(DataType.DATATYPE_BIT) || upperType.startsWith(DataType.DATATYPE_NCHAR) || upperType.startsWith(DataType.DATATYPE_NCHAR_VARYING);
    int size = 0;
    if (isString || isByte) {
        size = DataType.getSize(type);
    }
    Object realObj = null;
    String errorMsg = null;
    File file = new File(str.replaceFirst(FILE_URL_PREFIX, ""));
    if (upperType.startsWith(DataType.DATATYPE_BLOB)) {
        try {
            CUBRIDBlobProxy blob = new CUBRIDBlobProxy((CUBRIDConnectionProxy) conn);
            if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
                InputStream fin = null;
                OutputStream out = null;
                try {
                    fin = new FileInputStream(file);
                    out = blob.setBinaryStream(1);
                    byte[] data = new byte[512];
                    int count = -1;
                    while ((count = fin.read(data)) != -1) {
                        out.write(data, 0, count);
                    }
                    realObj = blob.getProxyObj();
                } catch (IOException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                } finally {
                    try {
                        fin.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                    try {
                        out.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                }
            } else {
                try {
                    byte[] byteArr = str.getBytes(dbCharSet);
                    blob.setBytes(1, byteArr);
                    realObj = blob.getProxyObj();
                } catch (UnsupportedEncodingException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                }
            }
        } catch (CUBRIDProxyException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        } catch (SQLException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        }
    } else if (upperType.startsWith(DataType.DATATYPE_CLOB)) {
        try {
            CUBRIDClobProxy clob = new CUBRIDClobProxy((CUBRIDConnectionProxy) conn, dbCharSet);
            if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
                BufferedReader reader = null;
                Writer writer = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
                    writer = clob.setCharacterStream(1);
                    char[] charArr = new char[512];
                    int count = reader.read(charArr);
                    while (count > 0) {
                        writer.write(charArr, 0, count);
                        count = reader.read(charArr);
                    }
                } catch (IOException e) {
                    errorMsg = e.getMessage();
                    LOGGER.error("", e);
                } finally {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                    try {
                        writer.close();
                    } catch (IOException e) {
                        LOGGER.error("", e);
                    }
                }
            } else {
                clob.setString(1, str);
            }
            realObj = clob.getProxyObj();
        } catch (CUBRIDProxyException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        } catch (SQLException e) {
            errorMsg = e.getMessage();
            LOGGER.error("", e);
        }
    } else if (isString) {
        if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), fileCharSet));
                StringBuffer strBuffer = new StringBuffer();
                char[] charArr = new char[512];
                int count = reader.read(charArr);
                int totalCount = count;
                while (count > 0) {
                    if (count == 512) {
                        strBuffer.append(charArr);
                    } else {
                        char[] tmpChar = new char[count];
                        System.arraycopy(charArr, 0, tmpChar, 0, count);
                        strBuffer.append(tmpChar);
                    }
                    count = reader.read(charArr);
                    totalCount += count;
                }
                if (totalCount > size) {
                    errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
                } else {
                    realObj = strBuffer.toString();
                }
            } catch (IOException e) {
                errorMsg = e.getMessage();
                LOGGER.error("", e);
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
            }
        } else {
            realObj = str;
        }
    } else if (isByte) {
        if (str.startsWith(FILE_URL_PREFIX) && file.exists()) {
            InputStream fin = null;
            try {
                fin = new FileInputStream(file);
                int length = fin.available();
                if (length * 8 > size) {
                    errorMsg = Messages.bind(Messages.fileTooLongMsg, new String[] { str, type });
                } else {
                    byte[] data = new byte[length];
                    if (fin.read(data) != -1) {
                        realObj = data;
                    }
                }
            } catch (IOException e) {
                errorMsg = e.getMessage();
                LOGGER.error("", e);
            } finally {
                try {
                    fin.close();
                } catch (IOException e) {
                    LOGGER.error("", e);
                }
            }
        } else {
            FormatDataResult result = DBAttrTypeFormatter.format(type, str, false, dbCharSet, isUseNULLValueSetting);
            byte[] byteArr = (byte[]) result.getFormatedJavaObj();
            realObj = byteArr;
        }
    }
    if (errorMsg != null) {
        realObj = new Exception(errorMsg);
    }
    return realObj;
}
Also used : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) FileInputStream(java.io.FileInputStream) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) CUBRIDProxyException(com.cubrid.jdbc.proxy.manage.CUBRIDProxyException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CUBRIDProxyException(com.cubrid.jdbc.proxy.manage.CUBRIDProxyException) CUBRIDBlobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy) BufferedReader(java.io.BufferedReader) File(java.io.File) CUBRIDClobProxy(com.cubrid.jdbc.proxy.driver.CUBRIDClobProxy) Writer(java.io.Writer)

Aggregations

CUBRIDBlobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy)1 CUBRIDClobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDClobProxy)1 CUBRIDConnectionProxy (com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy)1 CUBRIDProxyException (com.cubrid.jdbc.proxy.manage.CUBRIDProxyException)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 SQLException (java.sql.SQLException)1 ParseException (java.text.ParseException)1