Search in sources :

Example 11 with PlatformColumn

use of org.jumpmind.db.model.PlatformColumn in project symmetric-ds by JumpMind.

the class PostgreSqlDdlReader method readColumn.

@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    Column column = super.readColumn(metaData, values);
    PlatformColumn platformColumn = column.findPlatformColumn(platform.getName());
    if (platformColumn != null && "serial".equals(platformColumn.getType()) || "serial4".equals(platformColumn.getType())) {
        platformColumn.setType("int4");
    } else if (platformColumn != null && "bigserial".equals(platformColumn.getType()) || "serial8".equals(platformColumn.getType())) {
        platformColumn.setType("int8");
    }
    if (column.getSize() != null) {
        if (column.getSizeAsInt() <= 0) {
            column.setSize(null);
            // map them to LONGVARBINARY/LONGVARCHAR
            if (column.getMappedTypeCode() == Types.BINARY) {
                column.setMappedTypeCode(Types.LONGVARBINARY);
            } else if (column.getMappedTypeCode() == Types.VARCHAR) {
                column.setMappedTypeCode(Types.LONGVARCHAR);
            }
        } else // on columns defined as TEXT.
        if (column.getSizeAsInt() == Integer.MAX_VALUE) {
            column.setSize(null);
            if (column.getMappedTypeCode() == Types.VARCHAR) {
                column.setMappedTypeCode(Types.LONGVARCHAR);
            } else if (column.getMappedTypeCode() == Types.BINARY) {
                column.setMappedTypeCode(Types.LONGVARBINARY);
            }
        } else if (column.getSizeAsInt() == 131089 && column.getJdbcTypeCode() == Types.NUMERIC) {
            column.setSizeAndScale(0, 0);
            column.setMappedTypeCode(Types.DECIMAL);
            if (platformColumn != null) {
                platformColumn.setSize(-1);
                platformColumn.setDecimalDigits(-1);
            }
        }
    }
    String defaultValue = column.getDefaultValue();
    if ((defaultValue != null) && (defaultValue.length() > 0)) {
        // then it is an auto-increment column
        if (defaultValue.startsWith("nextval(") || (PostgreSqlDdlBuilder.isUsePseudoSequence() && defaultValue.endsWith("seq()"))) {
            column.setAutoIncrement(true);
            defaultValue = null;
        } else {
            // "'some value'::character varying" or "'2000-01-01'::date"
            switch(column.getMappedTypeCode()) {
                case Types.INTEGER:
                case Types.BIGINT:
                case Types.DECIMAL:
                case Types.NUMERIC:
                    defaultValue = extractUndelimitedDefaultValue(defaultValue);
                    break;
                case Types.CHAR:
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:
                    defaultValue = extractDelimitedDefaultValue(defaultValue);
                    break;
            }
            if (TypeMap.isTextType(column.getMappedTypeCode())) {
                // We assume escaping via double quote (see also the
                // backslash_quote setting:
                // http://www.postgresql.org/docs/7.4/interactive/runtime-config.html#RUNTIME-CONFIG-COMPATIBLE)
                defaultValue = unescape(defaultValue, "'", "''");
            }
        }
        column.setDefaultValue(defaultValue);
    }
    return column;
}
Also used : Column(org.jumpmind.db.model.Column) PlatformColumn(org.jumpmind.db.model.PlatformColumn) PlatformColumn(org.jumpmind.db.model.PlatformColumn)

Aggregations

PlatformColumn (org.jumpmind.db.model.PlatformColumn)11 Column (org.jumpmind.db.model.Column)7 IndexColumn (org.jumpmind.db.model.IndexColumn)4 Table (org.jumpmind.db.model.Table)4 IOException (java.io.IOException)2 ForeignKey (org.jumpmind.db.model.ForeignKey)2 IIndex (org.jumpmind.db.model.IIndex)2 Reference (org.jumpmind.db.model.Reference)2 IoException (org.jumpmind.exception.IoException)2 Test (org.junit.Test)2 StringReader (java.io.StringReader)1 SQLException (java.sql.SQLException)1 NonUniqueIndex (org.jumpmind.db.model.NonUniqueIndex)1 UniqueIndex (org.jumpmind.db.model.UniqueIndex)1 SqlException (org.jumpmind.db.sql.SqlException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1