Search in sources :

Example 66 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project cubrid-manager by CUBRID.

the class GetPartitionedClassListTask method getExprDataType.

/**
	 * 
	 * Get partition expression data type
	 * 
	 * @param tableName The table name
	 * @param expr The partition expression
	 * @return The string
	 */
private String getExprDataType(String tableName, String expr) {
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String sql = "SELECT " + expr + " FROM " + QuerySyntax.escapeKeyword(tableName) + " WHERE ROWNUM = 1";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sql);
        ResultSetMetaData rsmt = rs.getMetaData();
        return rsmt.getColumnTypeName(1);
    } catch (SQLException e) {
        return null;
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 67 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project cubrid-manager by CUBRID.

the class GetAllSchemaTask method getExprDataType.

/**
	 *
	 * Get partition expression data type
	 *
	 * @param tableName The table name
	 * @param expr The partition expression
	 * @return The string
	 */
private String getExprDataType(String tableName, String expr) {
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String sql = "SELECT " + expr + " FROM " + QuerySyntax.escapeKeyword(tableName) + " WHERE ROWNUM = 1";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(sql);
        ResultSetMetaData rsmt = rs.getMetaData();
        return rsmt.getColumnTypeName(1);
    } catch (SQLException e) {
        LOGGER.error("", e);
        return null;
    } finally {
        QueryUtil.freeQuery(stmt, rs);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 68 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project ats-framework by Axway.

the class MssqlDbProvider method getFirstRow.

/**
     * Gets a first row of a result set of a query and returns it as a HashMap.
     *
     * @param sQuery the query to be executed - expected to return only one row
     * @return HashMap object with the column names and values of the row
     * @exception SQLException - if a database error occurs
     */
public Map<String, String> getFirstRow(String sQuery) throws DbException {
    ResultSet rs = null;
    Connection connection = ConnectionPool.getConnection(dbConnection);
    HashMap<String, String> hash = new HashMap<String, String>();
    PreparedStatement stmnt = null;
    try {
        stmnt = connection.prepareStatement(sQuery);
        rs = stmnt.executeQuery();
        ResultSetMetaData rsmd = rs.getMetaData();
        // get the first row
        if (rs.next()) {
            // iterate the columns and fill the hash map
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String columnName = rsmd.getColumnName(i);
                String columnValue = rs.getString(i);
                hash.put(columnName, columnValue);
            }
        }
    } catch (SQLException ex) {
        log.error(ExceptionUtils.getExceptionMsg(ex));
    } finally {
        DbUtils.closeResultSet(rs);
        DbUtils.close(connection, stmnt);
    }
    return hash;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 69 with ResultSetMetaData

use of java.sql.ResultSetMetaData 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 70 with ResultSetMetaData

use of java.sql.ResultSetMetaData 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)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9