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());
}
}
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;
}
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;
}
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 + ".");
}
}
}
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;
}
Aggregations