Search in sources :

Example 11 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class CharRDBMSMapping method initialize.

/**
 * Method to initialise the column mapping. Provides default length specifications for the CHAR column to
 * fit the data being stored.
 */
protected void initialize() {
    if (column != null) {
        // Default Length
        if (getJavaTypeMapping() instanceof SingleFieldMapping && column.getColumnMetaData().getLength() == null) {
            SingleFieldMapping m = (SingleFieldMapping) getJavaTypeMapping();
            if (m.getDefaultLength(0) > 0) {
                // No column length provided by user and the type has a default length so use it
                column.getColumnMetaData().setLength(m.getDefaultLength(0));
            }
        }
        column.getColumnMetaData().setJdbcType("CHAR");
        column.checkString();
        // Valid Values
        if (getJavaTypeMapping() instanceof SingleFieldMapping) {
            Object[] validValues = ((SingleFieldMapping) getJavaTypeMapping()).getValidValues(0);
            if (validValues != null) {
                column.setCheckConstraints(getDatastoreAdapter().getCheckConstraintForValues(column.getIdentifier(), validValues, column.isNullable()));
            }
        }
        if (getJavaTypeMapping().getJavaType() == Boolean.class) {
            // With a Boolean we'll store it as "Y", "N" (see setBoolean/getBoolean methods)
            column.getColumnMetaData().setLength(1);
            StringBuilder constraints = new StringBuilder("CHECK (" + column.getIdentifier() + " IN ('Y','N')");
            if (column.isNullable()) {
                constraints.append(" OR " + column.getIdentifier() + " IS NULL");
            }
            constraints.append(')');
            column.setCheckConstraints(constraints.toString());
        }
        // Check on max length of the type against the length we have set
        SQLTypeInfo typeInfo = getTypeInfo();
        int maxlength = typeInfo.getPrecision();
        if (column.getColumnMetaData().getLength().intValue() <= 0 || column.getColumnMetaData().getLength().intValue() > maxlength) {
            if (typeInfo.isAllowsPrecisionSpec()) {
                throw new NucleusUserException("String max length of " + column.getColumnMetaData().getLength() + " is outside the acceptable range [0, " + maxlength + "] for column \"" + column.getIdentifier() + "\"");
            }
        }
    }
    initTypeInfo();
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) SingleFieldMapping(org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping) SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Example 12 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class PostgreSQLAdapter method newSQLTypeInfo.

public SQLTypeInfo newSQLTypeInfo(ResultSet rs) {
    SQLTypeInfo info = new PostgreSQLTypeInfo(rs);
    // JDBC and PostgreSQL data types.  We filter the returned type info to be sure we use the appropriate base PostgreSQL types for the important JDBC types.
    if (psqlTypes == null) {
        psqlTypes = new ConcurrentHashMap<>();
        psqlTypes.put("" + Types.BIT, "bool");
        psqlTypes.put("" + Types.TIMESTAMP, "timestamptz");
        psqlTypes.put("" + Types.BIGINT, "int8");
        psqlTypes.put("" + Types.CHAR, "char");
        psqlTypes.put("" + Types.DATE, "date");
        psqlTypes.put("" + Types.DOUBLE, "float8");
        psqlTypes.put("" + Types.INTEGER, "int4");
        psqlTypes.put("" + Types.LONGVARCHAR, "text");
        psqlTypes.put("" + Types.CLOB, "text");
        psqlTypes.put("" + Types.BLOB, "bytea");
        psqlTypes.put("" + Types.NUMERIC, "numeric");
        psqlTypes.put("" + Types.REAL, "float4");
        psqlTypes.put("" + Types.SMALLINT, "int2");
        psqlTypes.put("" + Types.TIME, "time");
        psqlTypes.put("" + Types.VARCHAR, "varchar");
        // TODO Enable alternative OTHER types
        psqlTypes.put("" + Types.OTHER, "uuid");
        psqlTypes.put("" + Types.ARRAY, "VARCHAR(255) ARRAY");
        psqlTypes.put("" + Types.ARRAY, "INT ARRAY");
    // PostgreSQL provides 2 types for "char" mappings: "char", "bpchar"; PostgreSQL recommend bpchar for default usage, but sadly you cannot say "bpchar(200)" in an SQL statement.
    // Due to this we use "char" since you can say "char(100)" (and internally in PostgreSQL it becomes bpchar).
    // PostgreSQL 8.1 JDBC driver somehow puts "char" as Types.OTHER rather than Types.CHAR ! so this is faked in createTypeInfo() above.
    // PostgreSQL (7.3, 7.4) doesn't provide a SQL type to map to JDBC types FLOAT, DECIMAL, BLOB, BOOLEAN
    }
    Object obj = psqlTypes.get("" + info.getDataType());
    if (obj != null) {
        String psql_type_name = (String) obj;
        if (!info.getTypeName().equalsIgnoreCase(psql_type_name)) {
            // We don't support this JDBC type using *this* PostgreSQL SQL type
            NucleusLogger.DATASTORE.debug(Localiser.msg("051007", info.getTypeName(), getNameForJDBCType(info.getDataType())));
            return null;
        }
    }
    return info;
}
Also used : SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Example 13 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class RDBMSStoreManager method getSQLTypeInfoForJDBCType.

/**
 * Accessor for the SQL type info for the specified JDBC type.
 * @param jdbcType JDBC type
 * @param sqlType The SQL type name (if known, otherwise uses the default for this JDBC type).
 * @return SQL type info
 * @throws UnsupportedDataTypeException If the JDBC type is not found
 */
public SQLTypeInfo getSQLTypeInfoForJDBCType(int jdbcType, String sqlType) throws UnsupportedDataTypeException {
    RDBMSTypesInfo typesInfo = (RDBMSTypesInfo) schemaHandler.getSchemaData(null, RDBMSSchemaHandler.TYPE_TYPES, null);
    JDBCTypeInfo jdbcTypeInfo = (JDBCTypeInfo) typesInfo.getChild("" + jdbcType);
    if (jdbcTypeInfo.getNumberOfChildren() == 0) {
        // No sql-type for this jdbc-type so unsupported
        throw new UnsupportedDataTypeException(Localiser.msg("051005", dba.getNameForJDBCType(jdbcType)));
    }
    SQLTypeInfo sqlTypeInfo = (SQLTypeInfo) jdbcTypeInfo.getChild(sqlType != null ? sqlType : "DEFAULT");
    if (sqlTypeInfo == null && sqlType != null) {
        // Try uppercase form of sql-type
        sqlTypeInfo = (SQLTypeInfo) jdbcTypeInfo.getChild(sqlType.toUpperCase());
        if (sqlTypeInfo == null) {
            // Try lowercase form of sql-type
            sqlTypeInfo = (SQLTypeInfo) jdbcTypeInfo.getChild(sqlType.toLowerCase());
            if (sqlTypeInfo == null) {
                // fallback to DEFAULT
                NucleusLogger.DATASTORE_SCHEMA.debug("Attempt to find JDBC driver 'typeInfo' for jdbc-type=" + dba.getNameForJDBCType(jdbcType) + " but sql-type=" + sqlType + " is not found. Using default sql-type for this jdbc-type.");
                sqlTypeInfo = (SQLTypeInfo) jdbcTypeInfo.getChild("DEFAULT");
            }
        }
    }
    return sqlTypeInfo;
}
Also used : JDBCTypeInfo(org.datanucleus.store.rdbms.schema.JDBCTypeInfo) RDBMSTypesInfo(org.datanucleus.store.rdbms.schema.RDBMSTypesInfo) UnsupportedDataTypeException(org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException) SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Example 14 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class OracleAdapter method initialiseTypes.

/**
 * Initialise the types for this datastore.
 * @param handler SchemaHandler that we initialise the types for
 * @param mconn Managed connection to use
 */
public void initialiseTypes(StoreSchemaHandler handler, ManagedConnection mconn) {
    super.initialiseTypes(handler, mconn);
    // Add on any missing JDBC types
    SQLTypeInfo sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo("CLOB", (short) Types.CLOB, 1073741823, "'", "'", null, 1, true, (short) 0, false, false, false, "CLOB", (short) 0, (short) 0, 10);
    // Can't add precision on a CLOB
    sqlType.setAllowsPrecisionSpec(false);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.CLOB, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo("DATE", (short) Types.DATE, 7, null, null, null, 1, false, (short) 3, false, false, false, "DATE", (short) 0, (short) 0, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.DATE, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo("DECIMAL", (short) Types.DECIMAL, 38, null, null, null, 1, false, (short) 3, false, true, false, "NUMBER", (short) -84, (short) 127, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.DECIMAL, sqlType, true);
    // Oracle has a synonym "DOUBLE PRECISION" (can't specify precision/scale) mapping to sql type of "FLOAT"
    sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo("DOUBLE PRECISION", (short) Types.DOUBLE, 38, null, null, null, 1, false, (short) 3, false, true, false, "NUMBER", (short) -84, (short) 127, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.DOUBLE, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo(OracleTypeInfo.TYPES_NAME_SYS_XMLTYPE, (short) OracleTypeInfo.TYPES_SYS_XMLTYPE, 1073741823, "'", "'", null, 1, true, (short) 0, false, false, false, OracleTypeInfo.TYPES_NAME_SYS_XMLTYPE, (short) 0, (short) 0, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) OracleTypeInfo.TYPES_SYS_XMLTYPE, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.OracleTypeInfo("NVARCHAR2", (short) Types.NVARCHAR, 4000, "'", "'", null, 1, true, (short) 3, false, false, false, "NVARCHAR2", (short) 0, (short) 0, 10);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.NVARCHAR, sqlType, true);
    // Update any types that need extra info relative to the JDBC info
    Collection<SQLTypeInfo> sqlTypes = getSQLTypeInfoForJdbcType(handler, mconn, (short) Types.BLOB);
    if (sqlTypes != null) {
        Iterator<SQLTypeInfo> iter = sqlTypes.iterator();
        while (iter.hasNext()) {
            sqlType = iter.next();
            // Can't add precision on a BLOB
            sqlType.setAllowsPrecisionSpec(false);
        }
    }
    sqlTypes = getSQLTypeInfoForJdbcType(handler, mconn, (short) Types.CLOB);
    if (sqlTypes != null) {
        Iterator<SQLTypeInfo> iter = sqlTypes.iterator();
        while (iter.hasNext()) {
            sqlType = iter.next();
            // Can't add precision on a CLOB
            sqlType.setAllowsPrecisionSpec(false);
        }
    }
    supportedJdbcTypesById.put(OracleTypeInfo.TYPES_SYS_XMLTYPE, "XMLTYPE");
}
Also used : SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Example 15 with SQLTypeInfo

use of org.datanucleus.store.rdbms.schema.SQLTypeInfo in project datanucleus-rdbms by datanucleus.

the class NuoDBAdapter method initialiseTypes.

public void initialiseTypes(StoreSchemaHandler handler, ManagedConnection mconn) {
    super.initialiseTypes(handler, mconn);
    SQLTypeInfo sqlType = new org.datanucleus.store.rdbms.adapter.NuoDBTypeInfo("FLOAT", (short) Types.DOUBLE, 53, null, null, null, 1, false, (short) 2, false, false, false, null, (short) 0, (short) 0, 2);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.DOUBLE, sqlType, true);
    sqlType = new org.datanucleus.store.rdbms.adapter.NuoDBTypeInfo("TEXT", (short) Types.CLOB, 2147483647, null, null, null, 1, true, (short) 1, false, false, false, "TEXT", (short) 0, (short) 0, 0);
    addSQLTypeForJDBCType(handler, mconn, (short) Types.CLOB, sqlType, true);
}
Also used : SQLTypeInfo(org.datanucleus.store.rdbms.schema.SQLTypeInfo)

Aggregations

SQLTypeInfo (org.datanucleus.store.rdbms.schema.SQLTypeInfo)18 JDBCTypeInfo (org.datanucleus.store.rdbms.schema.JDBCTypeInfo)3 RDBMSTypesInfo (org.datanucleus.store.rdbms.schema.RDBMSTypesInfo)3 Iterator (java.util.Iterator)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 SingleFieldMapping (org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping)2 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)1 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)1 UnsupportedDataTypeException (org.datanucleus.store.rdbms.exceptions.UnsupportedDataTypeException)1 DatastoreIdentifier (org.datanucleus.store.rdbms.identifier.DatastoreIdentifier)1 IdentifierFactory (org.datanucleus.store.rdbms.identifier.IdentifierFactory)1 RDBMSColumnInfo (org.datanucleus.store.rdbms.schema.RDBMSColumnInfo)1 RDBMSSchemaInfo (org.datanucleus.store.rdbms.schema.RDBMSSchemaInfo)1 RDBMSTableInfo (org.datanucleus.store.rdbms.schema.RDBMSTableInfo)1