Search in sources :

Example 6 with QNameInfo

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

the class AbstractSQLStartup method initConnection.

@Override
public void initConnection() throws SQLException {
    this._configuration.refresh();
    this.initTypes();
    this.modifyPrimitiveTypes(this._primitiveTypes, this._state.javaTypes2SQLTypes().get());
    String schemaName = this._configuration.get().schemaName().get();
    if (schemaName == null) {
        schemaName = DEFAULT_SCHEMA_NAME;
    } else {
        this.checkSchemaName(schemaName);
    }
    LOGGER.debug("Will use '{}' as schema name", schemaName);
    this._state.schemaName().set(schemaName);
    this._state.entityTypePKs().set(new HashMap<String, Integer>());
    this._state.usedClassesPKs().set(new HashMap<CompositeDescriptor, Integer>());
    this._state.entityUsedQNames().set(new HashMap<EntityDescriptor, Set<QualifiedName>>());
    this._state.qNameInfos().set(new HashMap<QualifiedName, QNameInfo>());
    this._state.enumPKs().set(new HashMap<String, Integer>());
    Connection connection = this._dataSource.getConnection();
    try {
        connection.setAutoCommit(true);
        connection.setReadOnly(false);
        this.syncDB(connection);
    } finally {
        SQLUtil.closeQuietly(connection);
    }
    if (LOGGER.isDebugEnabled()) {
        String newline = "\n";
        String tab = "\t";
        String colonspace = ": ";
        StringBuilder report = new StringBuilder();
        report.append("schemaName: ").append(_state.schemaName().get()).append(newline);
        report.append("qNameInfos: ").append(newline);
        for (Map.Entry<QualifiedName, QNameInfo> entry : _state.qNameInfos().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("entityUsedQNames:").append(newline);
        for (Map.Entry<EntityDescriptor, Set<QualifiedName>> entry : _state.entityUsedQNames().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("usedClassesPKs:").append(newline);
        for (Map.Entry<CompositeDescriptor, Integer> entry : _state.usedClassesPKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("javaTypes2SQLTypes:").append(newline);
        for (Map.Entry<Class<?>, Integer> entry : _state.javaTypes2SQLTypes().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("entityTypePKs:").append(newline);
        for (Map.Entry<String, Integer> entry : _state.entityTypePKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("enumPKs:").append(newline);
        for (Map.Entry<String, Integer> entry : _state.enumPKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        LOGGER.debug("SQLDBState after initConnection:\n{}", report.toString());
    }
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) QualifiedName(org.qi4j.api.common.QualifiedName) Connection(java.sql.Connection) BigInteger(java.math.BigInteger) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with QNameInfo

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

the class AbstractSQLIndexing method storeCollectionProperty.

private Integer storeCollectionProperty(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertAllQNamesPS, Integer propertyPK, Long entityPK, QualifiedName qName, Collection<?> property, Integer parentQNameID) throws SQLException {
    QNameInfo info = this._state.qNameInfos().get().get(qName);
    PreparedStatement ps = qNameInsertPSs.get(qName);
    propertyPK = this.storeCollectionInfo(insertAllQNamesPS, propertyPK, entityPK, parentQNameID, ps, info);
    propertyPK = this.storeCollectionItems(qNameInsertPSs, property, insertAllQNamesPS, DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME, ps, info.getTableName(), propertyPK, entityPK, parentQNameID, info.getFinalType(), info.isFinalTypePrimitive());
    return propertyPK;
}
Also used : PreparedStatement(java.sql.PreparedStatement) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo)

Example 8 with QNameInfo

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

the class AbstractSQLIndexing method storePrimitiveProperty.

private Integer storePrimitiveProperty(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertAllQNamesPS, Integer propertyPK, Long entityPK, QualifiedName qName, Object property, Integer parentQNameID) throws SQLException {
    QNameInfo info = this._state.qNameInfos().get().get(qName);
    insertAllQNamesPS.setInt(1, propertyPK);
    insertAllQNamesPS.setLong(2, entityPK);
    insertAllQNamesPS.addBatch();
    PreparedStatement ps = qNameInsertPSs.get(qName);
    ps.setInt(1, propertyPK);
    ps.setLong(2, entityPK);
    ps.setObject(3, parentQNameID, Types.INTEGER);
    Type type = info.getFinalType();
    this.storePrimitiveUsingPS(ps, 4, property, type);
    ps.addBatch();
    return propertyPK + 1;
}
Also used : QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) PreparedStatement(java.sql.PreparedStatement) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo)

Example 9 with QNameInfo

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

the class AbstractSQLIndexing method syncQNamesInsertPSs.

private void syncQNamesInsertPSs(Connection connection, Map<QualifiedName, PreparedStatement> qNameInsertPSs, Set<QualifiedName> qNames) throws SQLException {
    Set<QualifiedName> copy = new HashSet<QualifiedName>(qNames);
    copy.removeAll(qNameInsertPSs.keySet());
    for (QualifiedName qName : copy) {
        QNameInfo info = this._state.qNameInfos().get().get(qName);
        if (info == null) {
            throw new InternalError("Could not find database information about qualified name [" + qName + "]");
        }
        QNameType type = info.getQNameType();
        if (type.equals(QNameType.PROPERTY)) {
            qNameInsertPSs.put(qName, this.createInsertPropertyPS(connection, info));
        } else if (type.equals(QNameType.ASSOCIATION)) {
            qNameInsertPSs.put(qName, this.createInsertAssociationPS(connection, info));
        } else if (type.equals(QNameType.MANY_ASSOCIATION)) {
            qNameInsertPSs.put(qName, this.createInsertManyAssociationPS(connection, info));
        } else {
            throw new IllegalArgumentException("Did not know what to do with QName of type " + type + ".");
        }
    }
}
Also used : QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) QualifiedName(org.qi4j.api.common.QualifiedName) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) HashSet(java.util.HashSet)

Example 10 with QNameInfo

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

the class AbstractSQLQuerying method traverseAssociationPath.

protected Integer traverseAssociationPath(TraversedAssoOrManyAssoRef reference, Integer lastTableIndex, Integer nextAvailableIndex, SQLVendor vendor, TableReferenceBuilder builder, JoinType joinStyle, Boolean includeLastTable) {
    Stack<QualifiedName> qNameStack = new Stack<>();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    String schemaName = this._state.schemaName().get();
    while (reference._hasRefs) {
        qNameStack.add(QualifiedName.fromAccessor(reference.getAccessor()));
        reference = reference.getTraversedAssociation();
    }
    while (!qNameStack.isEmpty()) {
        QualifiedName qName = qNameStack.pop();
        QNameInfo info = this._state.qNameInfos().get().get(qName);
        if (info == null) {
            throw new InternalError("No qName info found for qName [" + qName + "].");
        }
        // @formatter:off
        builder.addQualifiedJoin(joinStyle, t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(TABLE_NAME_PREFIX + nextAvailableIndex)), t.jc(b.eq(c.colName(TABLE_NAME_PREFIX + lastTableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(TABLE_NAME_PREFIX + nextAvailableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))));
        lastTableIndex = nextAvailableIndex;
        ++nextAvailableIndex;
        if (includeLastTable || !qNameStack.isEmpty()) {
            builder.addQualifiedJoin(joinStyle, t.table(t.tableName(schemaName, DBNames.ENTITY_TABLE_NAME), t.tableAlias(TABLE_NAME_PREFIX + nextAvailableIndex)), t.jc(b.eq(c.colName(TABLE_NAME_PREFIX + lastTableIndex, DBNames.QNAME_TABLE_VALUE_COLUMN_NAME), c.colName(TABLE_NAME_PREFIX + nextAvailableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))));
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
        }
    // @formatter:on
    }
    return lastTableIndex;
}
Also used : TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QualifiedName(org.qi4j.api.common.QualifiedName) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) Stack(java.util.Stack)

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