Search in sources :

Example 1 with QNameInfo

use of org.qi4j.index.sql.support.common.QNameInfo in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method modifyFromClauseAndWhereClauseToGetValue.

// TODO refactor this monster of a method to something more understandable
protected Integer modifyFromClauseAndWhereClauseToGetValue(final QualifiedName qName, Object value, final Specification<Composite> predicate, final Boolean negationActive, final Integer currentTableIndex, final ModifiableInt maxTableIndex, final String columnName, final String collectionPath, final SQLVendor vendor, final BooleanBuilder whereClause, final BooleanBuilder afterWhere, final TableReferenceBuilder fromClause, final GroupByBuilder groupBy, final BooleanBuilder having, final List<QNameJoin> qNameJoins, Map<String, Object> variables, final List<Object> values, final List<Integer> valueSQLTypes) {
    if (value instanceof Variable) {
        value = variables.get(((Variable) value).variableName());
    }
    final String schemaName = this._state.schemaName().get();
    Integer result = 1;
    final BooleanFactory b = vendor.getBooleanFactory();
    final LiteralFactory l = vendor.getLiteralFactory();
    final ColumnsFactory c = vendor.getColumnsFactory();
    final QueryFactory q = vendor.getQueryFactory();
    final TableReferenceFactory t = vendor.getTableReferenceFactory();
    if (value instanceof Collection<?>) {
        // Collection
        Integer collectionIndex = 0;
        Boolean collectionIsSet = value instanceof Set<?>;
        Boolean topLevel = collectionPath.equals(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME);
        String collTable = TABLE_NAME_PREFIX + currentTableIndex;
        String collCol = DBNames.QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME;
        ColumnReferenceByName collColExp = c.colName(collTable, collCol);
        BooleanBuilder collectionCondition = b.booleanBuilder();
        if (topLevel && negationActive) {
            afterWhere.and(b.booleanBuilder(b.neq(collColExp, l.s(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME))).or(b.isNull(collColExp)).createExpression());
        }
        Integer totalItemsProcessed = 0;
        for (Object item : (Collection<?>) value) {
            String path = collectionPath + DBNames.QNAME_TABLE_COLLECTION_PATH_SEPARATOR + (collectionIsSet ? "*{1,}" : collectionIndex);
            Boolean isCollection = (item instanceof Collection<?>);
            BooleanBuilder newWhere = b.booleanBuilder();
            if (!isCollection) {
                newWhere.reset(b.regexp(collColExp, l.s(path)));
            }
            totalItemsProcessed = totalItemsProcessed + modifyFromClauseAndWhereClauseToGetValue(qName, item, predicate, negationActive, currentTableIndex, maxTableIndex, columnName, path, vendor, newWhere, afterWhere, fromClause, groupBy, having, qNameJoins, variables, values, valueSQLTypes);
            ++collectionIndex;
            collectionCondition.or(newWhere.createExpression());
        }
        result = totalItemsProcessed;
        if (topLevel) {
            if (totalItemsProcessed == 0) {
                collectionCondition.and(b.isNotNull(collColExp)).and(b.eq(collColExp, l.l(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME)));
            } else if (!negationActive) {
                groupBy.addGroupingElements(q.groupingElement(c.colName(TABLE_NAME_PREFIX + currentTableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME)));
                having.and(b.eq(l.func(SQLFunctions.COUNT, c.colName(TABLE_NAME_PREFIX + currentTableIndex, DBNames.QNAME_TABLE_VALUE_COLUMN_NAME)), l.n(totalItemsProcessed)));
            }
        }
        whereClause.and(collectionCondition.createExpression());
    } else if (value instanceof ValueComposite) {
        // @formatter:off
        for (Property<?> property : Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) value).state().properties()) {
            Boolean qNameJoinDone = false;
            Integer sourceIndex = maxTableIndex.getInt();
            Integer targetIndex = sourceIndex + 1;
            for (QNameJoin join : qNameJoins) {
                if (join.getSourceQName().equals(qName)) {
                    sourceIndex = join.getSourceTableIndex();
                    if (join.getTargetQName().equals(spi.propertyDescriptorFor(property).qualifiedName())) {
                        // This join has already been done once
                        qNameJoinDone = true;
                        targetIndex = join.getTargetTableIndex();
                        break;
                    }
                }
            }
            if (!qNameJoinDone) {
                // @formatter:off
                QNameInfo info = _state.qNameInfos().get().get(spi.propertyDescriptorFor(property).qualifiedName());
                String prevTableName = TABLE_NAME_PREFIX + sourceIndex;
                String nextTableName = TABLE_NAME_PREFIX + targetIndex;
                fromClause.addQualifiedJoin(JoinType.LEFT_OUTER, t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(TABLE_NAME_PREFIX + targetIndex)), t.jc(b.booleanBuilder(b.eq(c.colName(prevTableName, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME), c.colName(nextTableName, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).and(b.eq(c.colName(prevTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).createExpression()));
                // @formatter:on
                qNameJoins.add(new QNameJoin(qName, spi.propertyDescriptorFor(property).qualifiedName(), sourceIndex, targetIndex));
                maxTableIndex.setInt(maxTableIndex.getInt() + 1);
            }
            modifyFromClauseAndWhereClauseToGetValue(spi.propertyDescriptorFor(property).qualifiedName(), property.get(), predicate, negationActive, targetIndex, maxTableIndex, columnName, collectionPath, vendor, whereClause, afterWhere, fromClause, groupBy, having, qNameJoins, variables, values, valueSQLTypes);
        }
    // @formatter:on
    } else {
        // Primitive
        ColumnReferenceByName valueCol = c.colName(TABLE_NAME_PREFIX + currentTableIndex, columnName);
        if (value == null) {
            whereClause.and(b.isNull(valueCol));
        } else {
            Object dbValue = value;
            if (Enum.class.isAssignableFrom(value.getClass())) {
                dbValue = this._state.enumPKs().get().get(value.getClass().getName());
            }
            whereClause.and(b.and(b.isNotNull(valueCol), this.getOperator(predicate).getExpression(b, valueCol, l.param())));
            values.add(dbValue);
            valueSQLTypes.add(_typeHelper.getSQLType(value));
            LOGGER.info(TABLE_NAME_PREFIX + currentTableIndex + "." + columnName + " is " + dbValue);
        }
    }
    return result;
}
Also used : QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) Variable(org.qi4j.api.query.grammar.Variable) ResultSet(java.sql.ResultSet) Set(java.util.Set) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) ValueComposite(org.qi4j.api.value.ValueComposite) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnReferenceByName(org.sql.generation.api.grammar.query.ColumnReferenceByName) BooleanBuilder(org.sql.generation.api.grammar.builders.booleans.BooleanBuilder) Collection(java.util.Collection) AccessibleObject(java.lang.reflect.AccessibleObject) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Property(org.qi4j.api.property.Property)

Example 2 with QNameInfo

use of org.qi4j.index.sql.support.common.QNameInfo in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method processOrderBySegments.

protected void processOrderBySegments(OrderBy[] orderBy, SQLVendor vendor, QuerySpecificationBuilder builder) {
    if (orderBy != null) {
        QNameInfo[] qNames = new QNameInfo[orderBy.length];
        QueryFactory q = vendor.getQueryFactory();
        ColumnsFactory c = vendor.getColumnsFactory();
        Integer tableIndex = 0;
        for (Integer idx = 0; idx < orderBy.length; ++idx) {
            if (orderBy[idx] != null) {
                PropertyFunction<?> ref = orderBy[idx].property();
                QualifiedName qName = QualifiedName.fromAccessor(ref.accessor());
                QNameInfo info = this._state.qNameInfos().get().get(qName);
                qNames[idx] = info;
                if (info == null) {
                    throw new InternalError("No qName info found for qName [" + qName + "].");
                }
                tableIndex = this.traversePropertyPath(ref, 0, tableIndex + 1, vendor, builder.getFrom().getTableReferences().iterator().next(), JoinType.LEFT_OUTER);
                Class<?> declaringType = ((Member) ref.accessor()).getDeclaringClass();
                String colName;
                Integer tableIdx;
                if (Identity.class.equals(declaringType)) {
                    colName = DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME;
                    tableIdx = tableIndex - 1;
                } else {
                    colName = DBNames.QNAME_TABLE_VALUE_COLUMN_NAME;
                    tableIdx = tableIndex;
                }
                Ordering ordering = Ordering.ASCENDING;
                if (orderBy[idx].order() == Order.DESCENDING) {
                    ordering = Ordering.DESCENDING;
                }
                builder.getOrderBy().addSortSpecs(q.sortSpec(c.colName(TABLE_NAME_PREFIX + tableIdx, colName), ordering));
            }
        }
    }
}
Also used : QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QualifiedName(org.qi4j.api.common.QualifiedName) Ordering(org.sql.generation.api.grammar.query.Ordering) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Member(java.lang.reflect.Member)

Example 3 with QNameInfo

use of org.qi4j.index.sql.support.common.QNameInfo in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method traversePropertyPath.

protected Integer traversePropertyPath(PropertyFunction<?> reference, Integer lastTableIndex, Integer nextAvailableIndex, SQLVendor vendor, TableReferenceBuilder builder, JoinType joinStyle) {
    Stack<QualifiedName> qNameStack = new Stack<>();
    Stack<PropertyFunction<?>> refStack = new Stack<>();
    while (reference != null) {
        qNameStack.add(QualifiedName.fromAccessor(reference.accessor()));
        refStack.add(reference);
        if (reference.traversedProperty() == null && (reference.traversedAssociation() != null || reference.traversedManyAssociation() != null)) {
            Integer lastAssoTableIndex = this.traverseAssociationPath(new TraversedAssoOrManyAssoRef(reference), lastTableIndex, nextAvailableIndex, vendor, builder, joinStyle, true);
            if (lastAssoTableIndex > lastTableIndex) {
                lastTableIndex = lastAssoTableIndex;
                nextAvailableIndex = lastTableIndex + 1;
            }
        }
        reference = reference.traversedProperty();
    }
    PropertyFunction<?> prevRef = null;
    String schemaName = this._state.schemaName().get();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    while (!qNameStack.isEmpty()) {
        QualifiedName qName = qNameStack.pop();
        PropertyFunction<?> ref = refStack.pop();
        if (!qName.type().equals(Identity.class.getName())) {
            QNameInfo info = this._state.qNameInfos().get().get(qName);
            if (info == null) {
                throw new InternalError("No qName info found for qName [" + qName + "].");
            }
            String prevTableAlias = TABLE_NAME_PREFIX + lastTableIndex;
            String nextTableAlias = TABLE_NAME_PREFIX + nextAvailableIndex;
            TableReferenceByName nextTable = t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(nextTableAlias));
            // @formatter:off
            if (prevRef == null) {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).and(b.isNull(c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).createExpression()));
            } else {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).and(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).createExpression()));
            }
            // @formatter:on
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
            prevRef = ref;
        }
    }
    return lastTableIndex;
}
Also used : TableReferenceByName(org.sql.generation.api.grammar.query.TableReferenceByName) QualifiedName(org.qi4j.api.common.QualifiedName) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) Stack(java.util.Stack) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) PropertyFunction(org.qi4j.api.query.grammar.PropertyFunction) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo)

Example 4 with QNameInfo

use of org.qi4j.index.sql.support.common.QNameInfo in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method processPropertyTypeForQNames.

private void processPropertyTypeForQNames(PropertyDescriptor pType, Map<QualifiedName, QNameInfo> qNameInfos, Set<QualifiedName> newQNames, List<CompositeDescriptorInfo> vDescriptors, Set<String> enumValues, Boolean setQNameTableNameToNull) {
    QualifiedName qName = pType.qualifiedName();
    if (!newQNames.contains(qName) && !qName.name().equals(Identity.class.getName())) {
        newQNames.add(qName);
        // System.out.println("QName: " + qName + ", hc: " + qName.hashCode());
        QNameInfo info = qNameInfos.get(qName);
        if (info == null) {
            info = QNameInfo.fromProperty(//
            qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + qNameInfos.size()), //
            pType);
            qNameInfos.put(qName, info);
        }
        Type vType = info.getFinalType();
        while (vType instanceof ParameterizedType) {
            vType = ((ParameterizedType) vType).getRawType();
        }
        if (//
        vType instanceof Class<?>) {
            final Class<?> vTypeClass = (Class<?>) vType;
            if (((Class<?>) vType).isInterface()) {
                for (CompositeDescriptorInfo descInfo : vDescriptors) {
                    CompositeDescriptor desc = descInfo.composite;
                    if (desc instanceof ValueDescriptor) {
                        ValueDescriptor vDesc = (ValueDescriptor) desc;
                        // other Serializable
                        if (Iterables.matchesAny(new Specification<Class<?>>() {

                            @Override
                            public boolean satisfiedBy(Class<?> item) {
                                return vTypeClass.isAssignableFrom(item);
                            }
                        }, vDesc.types())) {
                            for (PropertyDescriptor subPDesc : vDesc.state().properties()) {
                                //
                                this.processPropertyTypeForQNames(//
                                subPDesc, //
                                qNameInfos, //
                                newQNames, //
                                vDescriptors, //
                                enumValues, //
                                setQNameTableNameToNull);
                            }
                        }
                    }
                }
            } else if (Enum.class.isAssignableFrom((Class<?>) vType)) {
                for (Object value : ((Class<?>) vType).getEnumConstants()) {
                    enumValues.add(QualifiedName.fromClass((Class<?>) vType, value.toString()).toString());
                }
            }
        }
    }
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) QualifiedName(org.qi4j.api.common.QualifiedName) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) ParameterizedType(java.lang.reflect.ParameterizedType) 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) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Identity(org.qi4j.api.entity.Identity)

Example 5 with QNameInfo

use of org.qi4j.index.sql.support.common.QNameInfo in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method writeAppMetadataToDB.

private void writeAppMetadataToDB(Connection connection, ApplicationInfo appInfo, Map<String, Long> tablePKs) throws SQLException {
    String schemaName = this._state.schemaName().get();
    SQLVendor vendor = this._vendor;
    ModificationFactory m = vendor.getModificationFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    // @formatter:off
    PreparedStatement ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        Set<String> insertedTypeNames = new HashSet<>();
        for (EntityDescriptor descriptor : appInfo.entityDescriptors.values()) {
            for (Class<?> entityType : descriptor.types()) {
                String entityTypeName = entityType.getName();
                if (!insertedTypeNames.contains(entityTypeName)) {
                    long pk = tablePKs.get(ENTITY_TYPES_TABLE_NAME);
                    ps.setInt(1, (int) pk);
                    ps.setString(2, entityTypeName);
                    ps.executeUpdate();
                    this._state.entityTypePKs().get().put(entityTypeName, (int) pk);
                    //                      this._state.entityTypeInfos().get().put( entityTypeName, new EntityTypeInfo( descriptor, (int) pk ) );
                    tablePKs.put(ENTITY_TYPES_TABLE_NAME, pk + 1);
                }
            }
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        for (CompositeDescriptorInfo descInfo : appInfo.usedValueComposites) {
            String vDescStr = compositeDescriptorToString(descInfo.layer, descInfo.module, descInfo.composite);
            long pk = tablePKs.get(USED_CLASSES_TABLE_NAME);
            ps.setInt(1, (int) pk);
            ps.setString(2, vDescStr);
            ps.executeUpdate();
            this._state.usedClassesPKs().get().put(descInfo.composite, (int) pk);
            tablePKs.put(USED_CLASSES_TABLE_NAME, pk + 1);
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression()));
    try {
        for (String enumValue : appInfo.enumValues) {
            long pk = tablePKs.get(ENUM_LOOKUP_TABLE_NAME);
            ps.setInt(1, (int) pk);
            ps.setString(2, enumValue);
            ps.executeUpdate();
            this._state.enumPKs().get().put(enumValue, (int) pk);
            tablePKs.put(ENUM_LOOKUP_TABLE_NAME, pk + 1);
        }
    } finally {
        SQLUtil.closeQuietly(ps);
    }
    Statement stmt = connection.createStatement();
    ps = connection.prepareStatement(this.createInsertStatementForQNameInfo(connection, schemaName, vendor).toString());
    try {
        DefinitionFactory d = vendor.getDefinitionFactory();
        for (QNameInfo qNameInfo : this._state.qNameInfos().get().values()) {
            QNameType type = qNameInfo.getQNameType();
            TableElementListBuilder builder = d.createTableElementListBuilder();
            builder.addTableElement(d.createColumnDefinition(ALL_QNAMES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false));
            if (type.equals(QNameType.PROPERTY)) {
                builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, this._primitiveTypes.get(Integer.class), true));
                if (qNameInfo.getCollectionDepth() > 0) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME, this.getCollectionPathDataType(), false));
                }
                this.appendColumnDefinitionsForProperty(builder, qNameInfo);
                builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_PARENT_QNAME_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
            } else {
                if (type.equals(QNameType.ASSOCIATION)) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
                } else if (type.equals(QNameType.MANY_ASSOCIATION)) {
                    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_ASSOCIATION_INDEX_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
                } else {
                    throw new IllegalArgumentException("Did not how to create table for qName type: " + type + ".");
                }
                builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).addTargetColumns(ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
                tablePKs.put(qNameInfo.getTableName(), 0L);
            }
            builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).addTargetColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE));
            stmt.execute(this._vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, qNameInfo.getTableName())).setTableContentsSource(builder.createExpression()).createExpression()));
            //                stmt.execute( "COMMENT ON TABLE " + schemaName + "." + qNameInfo.getTableName() + " IS '"
            //                    + qNameInfo.getQName() + "'" );
            ps.setString(1, qNameInfo.getQName().toString());
            ps.setString(2, qNameInfo.getTableName());
            ps.execute();
        }
    } finally {
        SQLUtil.closeQuietly(stmt);
        SQLUtil.closeQuietly(ps);
    }
// @formatter:off
}
Also used : QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) TableElementListBuilder(org.sql.generation.api.grammar.builders.definition.TableElementListBuilder) PreparedStatement(java.sql.PreparedStatement) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) BigInteger(java.math.BigInteger) ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) SQLVendor(org.sql.generation.api.vendor.SQLVendor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory) HashSet(java.util.HashSet)

Aggregations

QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)10 QualifiedName (org.qi4j.api.common.QualifiedName)6 QNameType (org.qi4j.index.sql.support.common.QNameInfo.QNameType)4 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)4 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)4 PreparedStatement (java.sql.PreparedStatement)3 HashSet (java.util.HashSet)3 BooleanFactory (org.sql.generation.api.grammar.factories.BooleanFactory)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 Set (java.util.Set)2 Stack (java.util.Stack)2 CompositeDescriptor (org.qi4j.api.composite.CompositeDescriptor)2 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)2 LiteralFactory (org.sql.generation.api.grammar.factories.LiteralFactory)2 QueryFactory (org.sql.generation.api.grammar.factories.QueryFactory)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 Member (java.lang.reflect.Member)1