use of org.sql.generation.api.grammar.factories.TableReferenceFactory in project qi4j-sdk by Qi4j.
the class AbstractSQLIndexing method createQueryEntityPkByIdentityStatement.
protected QueryExpression createQueryEntityPkByIdentityStatement(String schemaName, SQLVendor vendor) {
BooleanFactory b = vendor.getBooleanFactory();
LiteralFactory l = vendor.getLiteralFactory();
ColumnsFactory c = vendor.getColumnsFactory();
TableReferenceFactory t = vendor.getTableReferenceFactory();
QueryFactory q = vendor.getQueryFactory();
// "SELECT " + ENTITY_TABLE_PK_COLUMN_NAME + "\n" + //
// "FROM " + "%s" + "." + ENTITY_TABLE_NAME + "\n" + //
// "WHERE " + ENTITY_TABLE_IDENTITY_COLUMN_NAME + " = ?" + "\n" + //
// ";" //
QuerySpecificationBuilder query = q.querySpecificationBuilder();
query.getSelect().addUnnamedColumns(c.colName(DBNames.ENTITY_TABLE_PK_COLUMN_NAME));
query.getFrom().addTableReferences(t.tableBuilder(t.table(t.tableName(schemaName, DBNames.ENTITY_TABLE_NAME))));
query.getWhere().reset(b.eq(c.colName(DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME), l.param()));
return q.createQuery(query.createExpression());
}
use of org.sql.generation.api.grammar.factories.TableReferenceFactory in project qi4j-sdk by Qi4j.
the class AbstractSQLIndexing method createDeleteFromTableStatement.
protected DeleteStatement createDeleteFromTableStatement(String schemaName, String tableName, String columnName, SQLVendor vendor) {
ModificationFactory m = vendor.getModificationFactory();
BooleanFactory b = vendor.getBooleanFactory();
LiteralFactory l = vendor.getLiteralFactory();
ColumnsFactory c = vendor.getColumnsFactory();
TableReferenceFactory t = vendor.getTableReferenceFactory();
// "DELETE FROM " + "%s" + "." + "%s" + "\n" + //
// "WHERE " + "%s" + " = ? " + "\n" + //
// ";" //
DeleteBySearchBuilder delete = m.deleteBySearch();
delete.setTargetTable(m.createTargetTable(t.tableName(schemaName, tableName))).getWhere().reset(b.eq(c.colName(columnName), l.param()));
return delete.createExpression();
}
use of org.sql.generation.api.grammar.factories.TableReferenceFactory 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));
}
}
use of org.sql.generation.api.grammar.factories.TableReferenceFactory in project qi4j-sdk by Qi4j.
the class AbstractSQLStartup method readAppMetadataFromDB.
private void readAppMetadataFromDB(Connection connection, Map<String, EntityDescriptor> entityDescriptors) throws SQLException {
String schemaName = this._state.schemaName().get();
Statement stmt = connection.createStatement();
SQLVendor vendor = this._vendor;
QueryFactory q = vendor.getQueryFactory();
TableReferenceFactory t = vendor.getTableReferenceFactory();
try {
// @formatter:off
q.simpleQueryBuilder().select(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME).from(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).createExpression();
ResultSet rs = stmt.executeQuery(vendor.toString(q.simpleQueryBuilder().select(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME).from(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).createExpression()));
long pk = 0L;
try {
while (rs.next()) {
pk = rs.getInt(1);
String entityTypeName = rs.getString(2);
this._state.entityTypePKs().get().put(entityTypeName, (int) pk);
// this._state.entityTypeInfos().get()
// .put( entityTypeName, new EntityTypeInfo( entityDescriptors.get( entityTypeName ), (int) pk ) );
}
} finally {
SQLUtil.closeQuietly(rs);
}
rs = stmt.executeQuery(vendor.toString(q.simpleQueryBuilder().select(USED_CLASSES_TABLE_PK_COLUMN_NAME, USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME).from(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).createExpression()));
try {
while (rs.next()) {
pk = rs.getInt(1);
String descriptorTextualFormat = rs.getString(2);
this._state.usedClassesPKs().get().put(stringToCompositeDescriptor(ValueDescriptor.class, this._app.descriptor(), descriptorTextualFormat), (int) pk);
}
} finally {
SQLUtil.closeQuietly(rs);
}
rs = stmt.executeQuery(vendor.toString(q.simpleQueryBuilder().select(ENUM_LOOKUP_TABLE_PK_COLUMN_NAME, ENUM_LOOKUP_TABLE_ENUM_VALUE_NAME).from(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).createExpression()));
try {
while (rs.next()) {
pk = rs.getInt(1);
String enumName = rs.getString(2);
this._state.enumPKs().get().put(enumName, (int) pk);
}
} finally {
SQLUtil.closeQuietly(rs);
}
rs = stmt.executeQuery(q.simpleQueryBuilder().select(USED_QNAMES_TABLE_QNAME_COLUMN_NAME, USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME).from(t.tableName(schemaName, USED_QNAMES_TABLE_NAME)).createExpression().toString());
try {
while (rs.next()) {
String qName = rs.getString(1);
String tableName = rs.getString(2);
this._state.qNameInfos().get().get(QualifiedName.fromFQN(qName)).setTableName(tableName);
}
} finally {
SQLUtil.closeQuietly(rs);
}
// @formatter:on
} finally {
SQLUtil.closeQuietly(stmt);
}
}
use of org.sql.generation.api.grammar.factories.TableReferenceFactory 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<String>();
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
}
Aggregations