Search in sources :

Example 1 with ModificationFactory

use of org.sql.generation.api.grammar.factories.ModificationFactory 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();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) DeleteBySearchBuilder(org.sql.generation.api.grammar.builders.modification.DeleteBySearchBuilder) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 2 with ModificationFactory

use of org.sql.generation.api.grammar.factories.ModificationFactory 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)

Example 3 with ModificationFactory

use of org.sql.generation.api.grammar.factories.ModificationFactory in project qi4j-sdk by Qi4j.

the class PostgreSQLIndexing method createInsertStatementWithAutoGeneratedIDForEntitiesTable.

@Override
protected InsertStatement createInsertStatementWithAutoGeneratedIDForEntitiesTable(String schemaName, String tableName, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    ColumnSourceByValuesBuilder columnBuilder = m.columnSourceByValues();
    columnBuilder.addValues(ValueSource.Default.INSTANCE);
    for (Integer x = 1; x < AMOUNT_OF_COLUMNS_IN_ENTITY_TABLE; ++x) {
        columnBuilder.addValues(l.param());
    }
    return ((PgSQLInsertStatementBuilder) m.insert()).setReturningClause(vendor.getQueryFactory().columnsBuilder().addUnnamedColumns(c.colName(DBNames.ENTITY_TABLE_PK_COLUMN_NAME)).createExpression()).setTableName(t.tableName(schemaName, tableName)).setColumnSource(columnBuilder.createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) ColumnSourceByValuesBuilder(org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 4 with ModificationFactory

use of org.sql.generation.api.grammar.factories.ModificationFactory in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method createInsertStatement.

protected InsertStatement createInsertStatement(String schemaName, String tableName, Integer amountOfColumns, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    ColumnSourceByValuesBuilder columnBuilder = m.columnSourceByValues();
    for (Integer x = 0; x < amountOfColumns; ++x) {
        columnBuilder.addValues(l.param());
    }
    return m.insert().setTableName(t.tableName(schemaName, tableName)).setColumnSource(columnBuilder.createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnSourceByValuesBuilder(org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 5 with ModificationFactory

use of org.sql.generation.api.grammar.factories.ModificationFactory in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method createPropertyInsert.

protected InsertStatement createPropertyInsert(QNameInfo qNameInfo, SQLVendor vendor) {
    String tableName = qNameInfo.getTableName();
    ModificationFactory m = vendor.getModificationFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    ColumnSourceByValuesBuilder columnBuilder = m.columnSourceByValues().addValues(l.param(), l.param(), l.param());
    if (qNameInfo.getCollectionDepth() > 0) {
        columnBuilder.addValues(l.func("text2ltree", l.param()));
    }
    columnBuilder.addValues(l.param());
    return m.insert().setTableName(t.tableName(this._state.schemaName().get(), tableName)).setColumnSource(columnBuilder.createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnSourceByValuesBuilder(org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Aggregations

ModificationFactory (org.sql.generation.api.grammar.factories.ModificationFactory)10 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)9 LiteralFactory (org.sql.generation.api.grammar.factories.LiteralFactory)8 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)4 PreparedStatement (java.sql.PreparedStatement)3 Statement (java.sql.Statement)3 ColumnSourceByValuesBuilder (org.sql.generation.api.grammar.builders.modification.ColumnSourceByValuesBuilder)3 BooleanFactory (org.sql.generation.api.grammar.factories.BooleanFactory)3 InsertStatement (org.sql.generation.api.grammar.modification.InsertStatement)3 BigInteger (java.math.BigInteger)2 DefinitionFactory (org.sql.generation.api.grammar.factories.DefinitionFactory)2 SQLVendor (org.sql.generation.api.vendor.SQLVendor)2 ResultSet (java.sql.ResultSet)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)1 QNameType (org.qi4j.index.sql.support.common.QNameInfo.QNameType)1 TableElementListBuilder (org.sql.generation.api.grammar.builders.definition.TableElementListBuilder)1 DeleteBySearchBuilder (org.sql.generation.api.grammar.builders.modification.DeleteBySearchBuilder)1