Example usage for java.sql DatabaseMetaData getVersionColumns

List of usage examples for java.sql DatabaseMetaData getVersionColumns

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getVersionColumns.

Prototype

ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException;

Source Link

Document

Retrieves a description of a table's columns that are automatically updated when any value in a row is updated.

Usage

From source file:org.apache.ddlutils.task.DumpMetadataTask.java

/**
 * Dumps the versioned (auto-updating) columns of the indicated table.
 * /*from   w ww.  j  a  va2 s.c o  m*/
 * @param xmlWriter   The xml writer to write to
 * @param metaData    The database metadata
 * @param catalogName The catalog name
 * @param schemaName  The schema name
 * @param tableName   The table name
 */
private void dumpVersionColumns(PrettyPrintingXmlWriter xmlWriter, final DatabaseMetaData metaData,
        final String catalogName, final String schemaName, final String tableName) throws SQLException {
    performResultSetXmlOperation(xmlWriter, null, new ResultSetXmlOperation() {
        public ResultSet getResultSet() throws SQLException {
            return metaData.getVersionColumns(catalogName, schemaName, tableName);
        }

        public void handleRow(PrettyPrintingXmlWriter xmlWriter, ResultSet result) throws SQLException {
            Set columns = getColumnsInResultSet(result);
            String columnName = result.getString("COLUMN_NAME");

            if ((columnName != null) && (columnName.length() > 0)) {
                xmlWriter.writeElementStart(null, "versionedColumn");
                xmlWriter.writeAttribute(null, "column", columnName);

                addIntAttribute(xmlWriter, "typeCode", result, columns, "DATA_TYPE");
                addStringAttribute(xmlWriter, "type", result, columns, "TYPE_NAME");
                addIntAttribute(xmlWriter, "size", result, columns, "BUFFER_LENGTH");
                addIntAttribute(xmlWriter, "precision", result, columns, "COLUMN_SIZE");
                addShortAttribute(xmlWriter, "scale", result, columns, "DECIMAL_DIGITS");
                if (columns.contains("PSEUDO_COLUMN")) {
                    try {
                        switch (result.getShort("PSEUDO_COLUMN")) {
                        case DatabaseMetaData.versionColumnPseudo:
                            xmlWriter.writeAttribute(null, "columnType", "pseudo column");
                            break;
                        case DatabaseMetaData.versionColumnNotPseudo:
                            xmlWriter.writeAttribute(null, "columnType", "real column");
                            break;
                        default:
                            xmlWriter.writeAttribute(null, "columnType", "unknown");
                            break;
                        }
                    } catch (SQLException ex) {
                        log("Could not read the PSEUDO_COLUMN value for versioned colum '" + columnName
                                + "' of table '" + tableName + "' from the result set: " + ex.getStackTrace(),
                                Project.MSG_ERR);
                    }
                }
                xmlWriter.writeElementEnd();
            }
        }

        public void handleError(SQLException ex) {
            log("Could not read the versioned columns for table '" + tableName + "' from the result set: "
                    + ex.getStackTrace(), Project.MSG_ERR);
        }
    });
}