Search in sources :

Example 1 with CUBRIDConnectionProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.

the class QueryEditorPart method deleteResult.

/**
	 * Delete data through data OID
	 *
	 * @param strOid String[]
	 * @throws SQLException if failed
	 */
public void deleteResult(String[] strOid) throws SQLException {
    if (strOid == null || strOid.length <= 0)
        return;
    connection.checkAndConnectQuietly();
    for (int i = 0; i < strOid.length; i++) {
        CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) connection.getConnection(), strOid[i]).remove();
    }
    if (isAutocommit) {
        queryAction(QUERY_ACTION.COMMIT);
    }
    setHaveActiveTransaction(true);
}
Also used : CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) Point(org.eclipse.swt.graphics.Point)

Example 2 with CUBRIDConnectionProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy 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)

Example 3 with CUBRIDConnectionProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.

the class ConnectDatabaseExecutor method internalConnect.

private boolean internalConnect(String brokerIP, String brokerPort, String dbName, String userName, String password, String charset, String jdbcAttrs, String driverVersion, boolean autoCommit, boolean isShard) {
    Display display = Display.getDefault();
    CUBRIDConnectionProxy connection = null;
    try {
        connection = (CUBRIDConnectionProxy) JDBCConnectionManager.getConnection(brokerIP, brokerPort, dbName, userName, password, charset, jdbcAttrs, driverVersion, autoCommit, isShard);
        if (connection == null) {
            display.syncExec(new Runnable() {

                public void run() {
                    CommonUITool.openErrorBox(Messages.msgConnectBrokerFailure);
                }
            });
            return false;
        }
        String jdbcVersion = connection.getJdbcVersion();
        jdbcVersion = jdbcVersion == null ? "" : jdbcVersion.replaceAll("CUBRID-JDBC-", "");
        if (jdbcVersion.lastIndexOf(".") > 0) {
            jdbcVersion = jdbcVersion.substring(0, jdbcVersion.lastIndexOf("."));
        }
        String dbVersion = connection.getMetaData().getDatabaseProductVersion();
        dbVersion = dbVersion == null ? "" : dbVersion;
        String minorDbVersion = dbVersion;
        if (minorDbVersion.lastIndexOf(".") > 0) {
            minorDbVersion = minorDbVersion.substring(0, minorDbVersion.lastIndexOf("."));
        }
        if (CompatibleUtil.compareVersion(jdbcVersion, minorDbVersion) == 0) {
            return true;
        } else {
            final String finalDbVersion = dbVersion;
            display.syncExec(new Runnable() {

                public void run() {
                    isConnectSuccess = CommonUITool.openConfirmBox(Messages.bind(Messages.tipNoSupportJdbcVersion, finalDbVersion));
                }
            });
            return isConnectSuccess;
        }
    } catch (final SQLException e) {
        LOGGER.error(e.getMessage(), e);
        display.syncExec(new Runnable() {

            public void run() {
                CommonUITool.openErrorBox(Messages.bind(Messages.errCommonTip, e.getErrorCode(), e.getMessage()));
            }
        });
        return false;
    } catch (final Exception e) {
        display.syncExec(new Runnable() {

            public void run() {
                CommonUITool.openErrorBox(e.getMessage());
            }
        });
        return false;
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                LOGGER.error(e.getMessage());
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) SQLException(java.sql.SQLException) Display(org.eclipse.swt.widgets.Display)

Example 4 with CUBRIDConnectionProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.

the class CubridWorkbenchContrItem method connectDatabaseWithErrMsg.

/**
	 *
	 * Connect the database
	 *
	 * @param dbInfo DatabaseInfo
	 * @return error messages String
	 */
public static String connectDatabaseWithErrMsg(DatabaseInfo dbInfo) {
    // FIXME extract
    if (dbInfo == null || dbInfo.getServerInfo() == null) {
        return "";
    }
    Map<String, String> jdbcVersionMap = CubridJdbcManager.getInstance().getLoadedJdbc();
    if (jdbcVersionMap == null || jdbcVersionMap.get(dbInfo.getServerInfo().getJdbcDriverVersion()) == null) {
        return Messages.errNoSupportDriver;
    }
    CUBRIDConnectionProxy connection = null;
    try {
        connection = (CUBRIDConnectionProxy) JDBCConnectionManager.getConnection(dbInfo, false);
        DbUserInfo userInfo = dbInfo.getAuthLoginedDbUserInfo();
        IsDBAUserTask checkTask = new IsDBAUserTask(dbInfo);
        checkTask.execute();
        userInfo.setDbaAuthority(checkTask.isDBAUser());
        dbInfo.setLogined(true);
        dbInfo.setRunningType(DbRunningType.CS);
        dbInfo.getServerInfo().setConnected(true);
        dbInfo.setLogined(true);
        return null;
    } catch (SQLException e) {
        return Messages.bind(Messages.errCommonTip, e.getErrorCode(), e.getMessage());
    } catch (Exception e) {
        return e.getMessage();
    } finally {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                connection = null;
            }
        }
    }
}
Also used : DbUserInfo(com.cubrid.cubridmanager.core.cubrid.user.model.DbUserInfo) SQLException(java.sql.SQLException) IsDBAUserTask(com.cubrid.cubridmanager.core.cubrid.user.task.IsDBAUserTask) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) SQLException(java.sql.SQLException) PartInitException(org.eclipse.ui.PartInitException)

Example 5 with CUBRIDConnectionProxy

use of com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy in project cubrid-manager by CUBRID.

the class RowDetailDialog method export.

/**
	 * Export data to file
	 *
	 * @param filePath String
	 * @param oidStr String
	 * @param value String
	 * @param fileCharset String
	 * @throws IOException The exception
	 * @throws SQLException The exception
	 */
private void export(final String filePath, String oidStr, String value, String fileCharset) throws IOException, SQLException {
    // FIXME move this logic to core module
    OutputStream fs = null;
    Writer writer = null;
    InputStream in = null;
    Reader reader = null;
    ResultSet rs = null;
    try {
        ColumnInfo columnInfo = columnInfoList.get(selComboIndex);
        String type = columnInfo.getType();
        String completedType = columnInfo.getComleteType();
        String colName = columnInfo.getName();
        if (DataType.DATATYPE_BLOB.equals(type) && DataType.BLOB_EXPORT_FORMAT.equals(value)) {
            CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
            rs = oidPxory.getValues(new String[] { colName });
            rs.next();
            Blob blob = rs.getBlob(colName);
            in = blob.getBinaryStream();
            fs = new BufferedOutputStream(new FileOutputStream(filePath));
            byte[] bArr = new byte[512];
            int count = in.read(bArr);
            while (count > 0) {
                fs.write(bArr, 0, count);
                count = in.read(bArr);
            }
            fs.flush();
        } else {
            if (DataType.DATATYPE_CLOB.equals(type) && DataType.CLOB_EXPORT_FORMAT.equals(value)) {
                CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
                rs = oidPxory.getValues(new String[] { colName });
                rs.next();
                Clob clob = rs.getClob(colName);
                reader = clob.getCharacterStream();
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), fileCharset));
                char[] charArr = new char[512];
                int count = reader.read(charArr);
                while (count > 0) {
                    writer.write(charArr, 0, count);
                    count = reader.read(charArr);
                }
                writer.flush();
            } else if (DataType.DATATYPE_BIT.equals(type) || DataType.DATATYPE_BIT_VARYING.equals(type)) {
                byte[] bArr = null;
                if (DataType.BIT_EXPORT_FORMAT.equals(value)) {
                    CUBRIDOIDProxy oidPxory = CUBRIDOIDProxy.getNewInstance((CUBRIDConnectionProxy) qe.getQueryEditor().getConnection().checkAndConnect(), oidStr);
                    rs = oidPxory.getValues(new String[] { colName });
                    rs.next();
                    bArr = rs.getBytes(colName);
                } else {
                    FormatDataResult result = new FormatDataResult();
                    DBAttrTypeFormatter.formatBit(completedType, value, result, dbCharset);
                    bArr = (byte[]) result.getFormatedJavaObj();
                }
                fs = new BufferedOutputStream(new FileOutputStream(filePath));
                fs.write(bArr);
                fs.flush();
            } else {
                byte[] bArr = value.getBytes(fileCharset);
                fs = new BufferedOutputStream(new FileOutputStream(filePath));
                fs.write(bArr);
                fs.flush();
            }
        }
    } finally {
        if (fs != null) {
            try {
                fs.close();
                fs = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                LOGGER.error("", e);
            }
        }
        if (in != null) {
            try {
                in.close();
                in = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
                reader = null;
            } catch (IOException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : CUBRIDOIDProxy(com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy) Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Reader(java.io.Reader) ColumnInfo(com.cubrid.common.ui.query.control.ColumnInfo) CUBRIDConnectionProxy(com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) Clob(java.sql.Clob) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

CUBRIDConnectionProxy (com.cubrid.jdbc.proxy.driver.CUBRIDConnectionProxy)7 SQLException (java.sql.SQLException)6 DbUserInfo (com.cubrid.cubridmanager.core.cubrid.user.model.DbUserInfo)2 IsDBAUserTask (com.cubrid.cubridmanager.core.cubrid.user.task.IsDBAUserTask)2 CUBRIDOIDProxy (com.cubrid.jdbc.proxy.driver.CUBRIDOIDProxy)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 PartInitException (org.eclipse.ui.PartInitException)2 ColumnInfo (com.cubrid.common.ui.query.control.ColumnInfo)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 CUBRIDBlobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDBlobProxy)1 CUBRIDClobProxy (com.cubrid.jdbc.proxy.driver.CUBRIDClobProxy)1 CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)1 CUBRIDProxyException (com.cubrid.jdbc.proxy.manage.CUBRIDProxyException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1