Search in sources :

Example 1 with SQLDataType

use of org.sql.generation.api.grammar.common.datatypes.SQLDataType in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method appendColumnDefinitionsForProperty.

private void appendColumnDefinitionsForProperty(TableElementListBuilder builder, QNameInfo qNameInfo) {
    Type finalType = qNameInfo.getFinalType();
    if (finalType instanceof ParameterizedType) {
        finalType = ((ParameterizedType) finalType).getRawType();
    }
    Class<?> finalClass = (Class<?>) finalType;
    SQLDataType sqlType = null;
    String valueRefTableName = null;
    String valueRefTablePKColumnName = null;
    if (qNameInfo.isFinalTypePrimitive()) {
        if (this._customizableTypes.keySet().contains(finalClass) && qNameInfo.getPropertyDescriptor().accessor().isAnnotationPresent(SQLTypeInfo.class)) {
            sqlType = this._customizableTypes.get(finalClass).customizeType(finalClass, qNameInfo.getPropertyDescriptor().accessor().getAnnotation(SQLTypeInfo.class));
        } else if (Enum.class.isAssignableFrom(finalClass)) {
            // Enum - reference the lookup table
            sqlType = this._primitiveTypes.get(Integer.class);
            valueRefTableName = ENUM_LOOKUP_TABLE_NAME;
            valueRefTablePKColumnName = ENUM_LOOKUP_TABLE_PK_COLUMN_NAME;
        } else {
            // Primitive type, default sqlType
            sqlType = this._primitiveTypes.get(finalClass);
        }
        if (sqlType == null) {
            throw new InternalError("Could not find sql type for java type [" + finalType + "]");
        }
    } else {
        // Value composite - just need used class
        sqlType = this._primitiveTypes.get(Integer.class);
        valueRefTableName = USED_CLASSES_TABLE_NAME;
        valueRefTablePKColumnName = USED_CLASSES_TABLE_PK_COLUMN_NAME;
    }
    SQLVendor vendor = this._vendor;
    DefinitionFactory d = vendor.getDefinitionFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, sqlType, qNameInfo.getCollectionDepth() > 0)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
    if (valueRefTableName != null && valueRefTablePKColumnName != null) {
        builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_COLUMN_NAME).setTargetTableName(t.tableName(this._state.schemaName().get(), valueRefTableName)).addTargetColumns(valueRefTablePKColumnName).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.RESTRICT).createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE));
    }
}
Also used : SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLTypeInfo(org.qi4j.index.sql.support.api.SQLTypeInfo) ParameterizedType(java.lang.reflect.ParameterizedType) BigInteger(java.math.BigInteger) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLVendor(org.sql.generation.api.vendor.SQLVendor) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory)

Example 2 with SQLDataType

use of org.sql.generation.api.grammar.common.datatypes.SQLDataType in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method initTypes.

private void initTypes() {
    DataTypeFactory dt = this._vendor.getDataTypeFactory();
    this._primitiveTypes = new HashMap<>();
    this._primitiveTypes.put(Boolean.class, dt.sqlBoolean());
    this._primitiveTypes.put(Byte.class, dt.smallInt());
    this._primitiveTypes.put(Short.class, dt.smallInt());
    this._primitiveTypes.put(Integer.class, dt.integer());
    this._primitiveTypes.put(Long.class, dt.bigInt());
    this._primitiveTypes.put(Float.class, dt.real());
    this._primitiveTypes.put(Double.class, dt.doublePrecision());
    this._primitiveTypes.put(Date.class, dt.timeStamp(true));
    this._primitiveTypes.put(Character.class, dt.integer());
    this._primitiveTypes.put(String.class, dt.sqlVarChar(5000));
    this._primitiveTypes.put(BigInteger.class, dt.decimal());
    this._primitiveTypes.put(BigDecimal.class, dt.decimal());
    Map<Class<?>, Integer> jdbcTypes = new HashMap<>();
    jdbcTypes.put(Boolean.class, Types.BOOLEAN);
    jdbcTypes.put(Byte.class, Types.SMALLINT);
    jdbcTypes.put(Short.class, Types.SMALLINT);
    jdbcTypes.put(Integer.class, Types.INTEGER);
    jdbcTypes.put(Long.class, Types.BIGINT);
    jdbcTypes.put(Float.class, Types.REAL);
    jdbcTypes.put(Double.class, Types.DOUBLE);
    jdbcTypes.put(Date.class, Types.TIMESTAMP);
    jdbcTypes.put(Character.class, Types.INTEGER);
    jdbcTypes.put(String.class, Types.VARCHAR);
    jdbcTypes.put(BigInteger.class, Types.NUMERIC);
    jdbcTypes.put(BigDecimal.class, Types.NUMERIC);
    this._state.javaTypes2SQLTypes().set(jdbcTypes);
    this._customizableTypes = new HashMap<>();
    //
    this._customizableTypes.put(//
    String.class, new SQLTypeCustomizer() {

        @Override
        public SQLDataType customizeType(Type propertyType, SQLTypeInfo sqlTypeInfo) {
            return _vendor.getDataTypeFactory().sqlVarChar(sqlTypeInfo.maxLength());
        }
    });
    //
    this._customizableTypes.put(//
    BigInteger.class, new SQLTypeCustomizer() {

        @Override
        public SQLDataType customizeType(Type propertyType, SQLTypeInfo sqlTypeInfo) {
            return _vendor.getDataTypeFactory().decimal(sqlTypeInfo.maxLength());
        }
    });
    //
    this._customizableTypes.put(//
    BigDecimal.class, new SQLTypeCustomizer() {

        @Override
        public SQLDataType customizeType(Type propertyType, SQLTypeInfo sqlTypeInfo) {
            return _vendor.getDataTypeFactory().decimal(sqlTypeInfo.maxLength());
        }
    });
}
Also used : BigInteger(java.math.BigInteger) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) HashMap(java.util.HashMap) DataTypeFactory(org.sql.generation.api.grammar.factories.DataTypeFactory) SQLTypeInfo(org.qi4j.index.sql.support.api.SQLTypeInfo)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 BigInteger (java.math.BigInteger)2 SQLTypeInfo (org.qi4j.index.sql.support.api.SQLTypeInfo)2 QNameType (org.qi4j.index.sql.support.common.QNameInfo.QNameType)2 SQLDataType (org.sql.generation.api.grammar.common.datatypes.SQLDataType)2 ObjectType (org.sql.generation.api.grammar.manipulation.ObjectType)2 HashMap (java.util.HashMap)1 DataTypeFactory (org.sql.generation.api.grammar.factories.DataTypeFactory)1 DefinitionFactory (org.sql.generation.api.grammar.factories.DefinitionFactory)1 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)1 SQLVendor (org.sql.generation.api.vendor.SQLVendor)1