Search in sources :

Example 1 with HashedCompareData

use of com.cubrid.common.ui.compare.data.model.HashedCompareData in project cubrid-manager by CUBRID.

the class DataCompareEditorPart method compareHashedCompareDataCompatible.

private void compareHashedCompareDataCompatible(Connection conn, DataCompare dataCompare, List<HashedCompareData> dataList) {
    // FIXME logic code move to core module
    if (dataList == null || dataList.size() == 0) {
        // TODO error
        return;
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    SchemaInfo schemaInfo = dataCompare.getSchemaInfo();
    String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
    StringBuilder sql = new StringBuilder();
    sql.append(" SELECT * FROM ").append(escapedTableName);
    sql.append(" WHERE ");
    {
        HashedCompareData data = dataList.get(0);
        for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
            if (i > 0) {
                sql.append(" AND ");
            }
            String columnName = data.getPkColumns().get(i);
            sql.append(QuerySyntax.escapeKeyword(columnName)).append(" = ?");
        }
    }
    for (HashedCompareData data : dataList) {
        if (runningState != RUNNING_SATE_ONLINE) {
            return;
        }
        boolean exists = false;
        StringBuilder hash = new StringBuilder();
        try {
            pstmt = conn.prepareStatement(sql.toString());
            for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
                String columnValue = data.getPkValues().get(i);
                pstmt.setString(i + 1, columnValue);
            }
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                exists = false;
            } else {
                exists = true;
                ResultSetMetaData meta = rs.getMetaData();
                for (int j = 1, len = meta.getColumnCount(); j <= len; j++) {
                    String columnName = meta.getColumnName(j);
                    boolean isPk = data.getPkColumns().contains(columnName);
                    if (isPk) {
                        continue;
                    }
                    String val = null;
                    if (DataType.isSetDataType(meta.getColumnTypeName(j))) {
                        String colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, meta.getColumnTypeName(j));
                        val = (String) FieldHandlerUtils.getRsValueForExport(colType, (CUBRIDResultSetProxy) rs, j, "{NULL}");
                    } else {
                        val = rs.getString(j);
                    }
                    if (val == null) {
                        val = "{NULL}";
                    }
                    hash.append(StringUtil.md5(val));
                }
            }
        } catch (SQLException e) {
            exists = false;
            printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
            printToConsoleOnWorkThread(e.getMessage(), true);
            LOGGER.error(sql.toString(), e);
        } finally {
            QueryUtil.freeQuery(pstmt, rs);
        }
        dataCompare.increaseProgress();
        if (!exists) {
            dataCompare.increaseNotExists();
            addLog(schemaInfo.getClassname(), data, charset);
        } else {
            if (data.getHash().equals(StringUtil.md5(hash.toString()))) {
                dataCompare.increaseMatches();
            } else {
                dataCompare.increaseNotMatches();
                addLog(schemaInfo.getClassname(), data, charset);
            }
        }
        completedRecords++;
        refreshProgressBar();
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                compareTableViewer.refresh();
            }
        });
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) HashedCompareData(com.cubrid.common.ui.compare.data.model.HashedCompareData) PreparedStatement(java.sql.PreparedStatement) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 2 with HashedCompareData

use of com.cubrid.common.ui.compare.data.model.HashedCompareData in project cubrid-manager by CUBRID.

the class DataCompareEditorPart method compareHashedCompareData.

private void compareHashedCompareData(Connection conn, DataCompare dataCompare, List<HashedCompareData> dataList) {
    // FIXME logic code move to core module
    if (dataList == null || dataList.size() == 0) {
        // TODO error
        return;
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    SchemaInfo schemaInfo = dataCompare.getSchemaInfo();
    StringBuilder extraColumns = new StringBuilder();
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT MD5(CONCAT(");
    StringBuilder cols = new StringBuilder();
    List<DBAttribute> attrs = schemaInfo.getAttributes();
    // TODO Collections.sort
    for (int i = 0, len = attrs.size(); i < len; i++) {
        DBAttribute attr = attrs.get(i);
        if (cols.length() > 0) {
            cols.append(",");
        }
        makeColumnsClause(extraColumns, cols, attr);
    }
    sql.append(cols);
    sql.append(")) AS _record_hash_ ");
    if (extraColumns.length() > 0) {
        sql.append(",");
        sql.append(extraColumns);
    }
    String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
    sql.append(" FROM ").append(escapedTableName);
    sql.append(" WHERE ");
    {
        HashedCompareData data = dataList.get(0);
        for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
            if (i > 0) {
                sql.append(" AND ");
            }
            String columnName = data.getPkColumns().get(i);
            sql.append(QuerySyntax.escapeKeyword(columnName)).append("=?");
        }
    }
    for (HashedCompareData data : dataList) {
        if (runningState != RUNNING_SATE_ONLINE) {
            return;
        }
        boolean exists = false;
        String hash = null;
        try {
            pstmt = conn.prepareStatement(sql.toString());
            for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
                String columnValue = data.getPkValues().get(i);
                pstmt.setString(i + 1, columnValue);
            }
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                exists = false;
            } else {
                exists = true;
                hash = rs.getString("_record_hash_");
            }
        } catch (SQLException e) {
            exists = false;
            printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
            printToConsoleOnWorkThread(e.getMessage(), true);
            LOGGER.error(sql.toString(), e);
        } finally {
            QueryUtil.freeQuery(pstmt, rs);
        }
        dataCompare.increaseProgress();
        if (!exists) {
            dataCompare.increaseNotExists();
            addLog(schemaInfo.getClassname(), data, charset);
        } else {
            if (data.getHash().equals(hash)) {
                dataCompare.increaseMatches();
            } else {
                dataCompare.increaseNotMatches();
                addLog(schemaInfo.getClassname(), data, charset);
            }
        }
        completedRecords++;
        refreshProgressBar();
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                compareTableViewer.refresh();
            }
        });
    }
}
Also used : SQLException(java.sql.SQLException) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ResultSet(java.sql.ResultSet) HashedCompareData(com.cubrid.common.ui.compare.data.model.HashedCompareData) PreparedStatement(java.sql.PreparedStatement) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 3 with HashedCompareData

use of com.cubrid.common.ui.compare.data.model.HashedCompareData in project cubrid-manager by CUBRID.

the class DataCompareEditorPart method fetchHashedCompareData.

private List<HashedCompareData> fetchHashedCompareData(Connection conn, SchemaInfo schemaInfo, long start, int rows) {
    // FIXME logic code move to core module
    Statement stmt = null;
    ResultSet rs = null;
    StringBuilder pkColumns = new StringBuilder();
    Constraint constraint = schemaInfo.getPK();
    if (constraint != null) {
        for (String column : constraint.getAttributes()) {
            if (pkColumns.length() > 0) {
                pkColumns.append(",");
            }
            pkColumns.append(QuerySyntax.escapeKeyword(column));
        }
    }
    StringBuilder extraColumns = new StringBuilder();
    StringBuilder sql = new StringBuilder();
    sql.append(" SELECT ").append(pkColumns).append(", ");
    sql.append(" MD5(CONCAT(");
    StringBuilder cols = new StringBuilder();
    List<DBAttribute> attrs = schemaInfo.getAttributes();
    // TODO Collections.sort
    for (int i = 0, len = attrs.size(); i < len; i++) {
        DBAttribute attr = attrs.get(i);
        if (cols.length() > 0) {
            cols.append(",");
        }
        makeColumnsClause(extraColumns, cols, attr);
    }
    sql.append(cols);
    sql.append(")) AS _record_hash_ ");
    if (extraColumns.length() > 0) {
        sql.append(",");
        sql.append(extraColumns);
    }
    String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
    sql.append(" FROM ").append(escapedTableName);
    sql.append(" ORDER BY ").append(pkColumns);
    sql.append(" FOR ORDERBY_NUM() BETWEEN ").append(start + 1);
    sql.append(" AND ").append(start + rows);
    List<HashedCompareData> result = new ArrayList<HashedCompareData>();
    try {
        StringBuilder extra = new StringBuilder();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql.toString());
        ResultSetMetaData meta = rs.getMetaData();
        while (rs.next()) {
            String hash = null;
            boolean isPk = true;
            extra.delete(0, extra.length());
            List<String> pkColumnList = new ArrayList<String>();
            List<String> pkValueList = new ArrayList<String>();
            List<String> pkTypeList = new ArrayList<String>();
            for (int j = 1, len = meta.getColumnCount(); j <= len; j++) {
                String columnName = meta.getColumnName(j);
                String columnType = DataType.isNumberType(meta.getColumnTypeName(j)) ? HashedCompareData.NUMBER_TYPE : HashedCompareData.STRING_TYPE;
                if (columnName.equalsIgnoreCase("_record_hash_")) {
                    isPk = false;
                    hash = rs.getString(j);
                } else if (isPk) {
                    pkColumnList.add(columnName);
                    pkValueList.add(rs.getString(j));
                    pkTypeList.add(columnType);
                } else {
                    String val = null;
                    if (DataType.isSetDataType(meta.getColumnTypeName(j))) {
                        String colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, meta.getColumnTypeName(j));
                        val = (String) FieldHandlerUtils.getRsValueForExport(colType, (CUBRIDResultSetProxy) rs, j, "{NULL}");
                    } else {
                        val = rs.getString(j);
                    }
                    if (val == null) {
                        val = "{NULL}";
                    }
                    extra.append(val);
                }
            }
            HashedCompareData data = new HashedCompareData(pkColumnList, pkValueList, pkTypeList, hash);
            result.add(data);
        }
    } catch (Exception e) {
        printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
        printToConsoleOnWorkThread(e.getMessage(), true);
        LOGGER.error(sql.toString(), e);
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
    return result;
}
Also used : CUBRIDResultSetProxy(com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy) Constraint(com.cubrid.common.core.common.model.Constraint) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResultSetMetaData(java.sql.ResultSetMetaData) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ResultSet(java.sql.ResultSet) HashedCompareData(com.cubrid.common.ui.compare.data.model.HashedCompareData)

Example 4 with HashedCompareData

use of com.cubrid.common.ui.compare.data.model.HashedCompareData in project cubrid-manager by CUBRID.

the class DataCompareEditorPart method fetchHashedCompareDataCompatible.

private List<HashedCompareData> fetchHashedCompareDataCompatible(Connection conn, SchemaInfo schemaInfo, long start, int rows) {
    // FIXME logic code move to core module
    Statement stmt = null;
    ResultSet rs = null;
    StringBuilder pkColumns = new StringBuilder();
    Constraint constraint = schemaInfo.getPK();
    if (constraint != null) {
        for (String column : constraint.getAttributes()) {
            if (pkColumns.length() > 0) {
                pkColumns.append(",");
            }
            pkColumns.append(QuerySyntax.escapeKeyword(column));
        }
    }
    String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
    StringBuilder sql = new StringBuilder();
    sql.append(" SELECT * FROM ").append(escapedTableName);
    sql.append(" ORDER BY ").append(pkColumns);
    sql.append(" FOR ORDERBY_NUM() BETWEEN ").append(start + 1);
    sql.append(" AND ").append(start + rows);
    List<HashedCompareData> result = new ArrayList<HashedCompareData>();
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql.toString());
        ResultSetMetaData meta = rs.getMetaData();
        while (rs.next()) {
            StringBuilder hash = new StringBuilder();
            boolean isPk = true;
            List<String> pkColumnList = new ArrayList<String>();
            List<String> pkValueList = new ArrayList<String>();
            List<String> pkTypeList = new ArrayList<String>();
            // TODO : sort
            for (int j = 1, len = meta.getColumnCount(); j <= len; j++) {
                String columnName = meta.getColumnName(j);
                String columnType = DataType.isNumberType(meta.getColumnTypeName(j)) ? HashedCompareData.NUMBER_TYPE : HashedCompareData.STRING_TYPE;
                isPk = constraint.getAttributes().contains(columnName);
                if (isPk) {
                    pkColumnList.add(columnName);
                    pkValueList.add(rs.getString(j));
                    pkTypeList.add(columnType);
                } else {
                    String val = null;
                    if (DataType.isSetDataType(meta.getColumnTypeName(j))) {
                        String colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, meta.getColumnTypeName(j));
                        val = (String) FieldHandlerUtils.getRsValueForExport(colType, (CUBRIDResultSetProxy) rs, j, "{NULL}");
                    } else {
                        val = rs.getString(j);
                    }
                    if (val == null) {
                        val = "{NULL}";
                    }
                    hash.append(StringUtil.md5(val));
                }
            }
            HashedCompareData data = new HashedCompareData(pkColumnList, pkValueList, pkTypeList, StringUtil.md5(hash.toString()));
            result.add(data);
        }
    } catch (Exception e) {
        printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
        printToConsoleOnWorkThread(e.getMessage(), true);
        LOGGER.error(sql.toString(), e);
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
    return result;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) HashedCompareData(com.cubrid.common.ui.compare.data.model.HashedCompareData)

Aggregations

Constraint (com.cubrid.common.core.common.model.Constraint)4 HashedCompareData (com.cubrid.common.ui.compare.data.model.HashedCompareData)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 ResultSetMetaData (java.sql.ResultSetMetaData)3 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)2 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 PartInitException (org.eclipse.ui.PartInitException)2 CUBRIDResultSetProxy (com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy)1