Search in sources :

Example 6 with ModificationFactory

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

the class AbstractSQLIndexing method createAssoInsert.

protected InsertStatement createAssoInsert(QNameInfo qNameInfo, SQLVendor vendor, Integer amountOfParams) {
    ModificationFactory m = vendor.getModificationFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    QueryFactory q = vendor.getQueryFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    String schemaName = this._state.schemaName().get();
    // "INSERT INTO " + "%s" + "." + "%s" + "\n" + //
    // "SELECT " + "?, " + "?, " + ENTITY_TABLE_PK_COLUMN_NAME + "\n" + // <-- here is 4 params
    // when many-asso
    // "FROM " + "%s" + "." + ENTITY_TABLE_NAME + "\n" + //
    // "WHERE " + ENTITY_TABLE_IDENTITY_COLUMN_NAME + " = " + "?";
    QuerySpecificationBuilder qBuilder = q.querySpecificationBuilder();
    for (Integer x = 0; x < amountOfParams; ++x) {
        qBuilder.getSelect().addUnnamedColumns(c.colExp(l.param()));
    }
    qBuilder.getSelect().addUnnamedColumns(c.colName(DBNames.ENTITY_TABLE_PK_COLUMN_NAME));
    qBuilder.getFrom().addTableReferences(t.tableBuilder(t.table(t.tableName(schemaName, DBNames.ENTITY_TABLE_NAME))));
    qBuilder.getWhere().reset(b.eq(c.colName(DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME), l.param()));
    return m.insert().setTableName(t.tableName(schemaName, qNameInfo.getTableName())).setColumnSource(m.columnSourceByQuery(q.createQuery(qBuilder.createExpression()))).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) QuerySpecificationBuilder(org.sql.generation.api.grammar.builders.query.QuerySpecificationBuilder) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 7 with ModificationFactory

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

the class AbstractSQLIndexing method createUpdateEntityTableStatement.

protected UpdateStatement createUpdateEntityTableStatement(String schemaName, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    // "UPDATE " + "%s" + "." + ENTITY_TABLE_NAME + "\n" + //
    // "SET " + ENTITY_TABLE_IDENTITY_COLUMN_NAME + " = ?, " + //
    // ENTITY_TABLE_MODIFIED_COLUMN_NAME + " = ?, " + //
    // ENTITY_TABLE_VERSION_COLUMN_NAME + " = ?, " + //
    // ENTITY_TABLE_APPLICATION_VERSION_COLUMN_NAME + " = ?" + "\n" + //
    // "WHERE " + ENTITY_TABLE_PK_COLUMN_NAME + " = ?" + "\n" + //
    // ";" //
    UpdateSourceByExpression paramSource = m.updateSourceByExp(l.param());
    UpdateBySearchBuilder builder = m.updateBySearch();
    builder.setTargetTable(m.createTargetTable(t.tableName(schemaName, DBNames.ENTITY_TABLE_NAME))).addSetClauses(m.setClause(DBNames.ENTITY_TABLE_IDENTITY_COLUMN_NAME, paramSource), m.setClause(DBNames.ENTITY_TABLE_MODIFIED_COLUMN_NAME, paramSource), m.setClause(DBNames.ENTITY_TABLE_VERSION_COLUMN_NAME, paramSource), m.setClause(DBNames.ENTITY_TABLE_APPLICATION_VERSION_COLUMN_NAME, paramSource)).getWhereBuilder().reset(b.eq(c.colName(DBNames.ENTITY_TABLE_PK_COLUMN_NAME), l.param()));
    return builder.createExpression();
}
Also used : UpdateSourceByExpression(org.sql.generation.api.grammar.modification.UpdateSourceByExpression) ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) UpdateBySearchBuilder(org.sql.generation.api.grammar.builders.modification.UpdateBySearchBuilder) 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 8 with ModificationFactory

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

the class AbstractSQLStartup method createSchemaAndRequiredTables.

private void createSchemaAndRequiredTables(Connection connection, String schemaName, Map<String, Long> tablePKs) throws SQLException {
    boolean schemaFound = false;
    ResultSet rs = connection.getMetaData().getSchemas();
    try {
        while (rs.next() && !schemaFound) {
            schemaFound = rs.getString(1).equals(schemaName);
        }
    } finally {
        SQLUtil.closeQuietly(rs);
    }
    SQLVendor vendor = this._vendor;
    DefinitionFactory d = vendor.getDefinitionFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    Statement stmt = connection.createStatement();
    // @formatter:off
    try {
        if (!schemaFound) {
            stmt.execute(vendor.toString(d.createSchemaDefinitionBuilder().setSchemaName(schemaName).createExpression()));
            LOGGER.debug("Database schema created");
        }
        this.testRequiredCapabilities(connection);
        LOGGER.debug("Underlying database fullfill required capabilities");
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, USED_CLASSES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(USED_CLASSES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(USED_CLASSES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(USED_CLASSES_TABLE_CLASS_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(USED_CLASSES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_TYPE_PK_TYPE), false)).addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(ENTITY_TYPES_TABLE_TYPE_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENTITY_TYPES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false, AutoGenerationPolicy.BY_DEFAULT)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_IDENTITY_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_MODIFIED_COLUMN_NAME, this._primitiveTypes.get(Date.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_VERSION_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(ENTITY_TABLE_APPLICATION_VERSION_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.UNIQUE).addColumns(ENTITY_TABLE_IDENTITY_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENTITY_TABLE_NAME, 0L);
        stmt.execute(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENTITY_TYPES_JOIN_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENTITY_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_PK_TYPE), false)).addTableElement(d.createColumnDefinition(ENTITY_TYPES_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(ENTITY_TYPE_PK_TYPE), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENTITY_TABLE_PK_COLUMN_NAME, ENTITY_TYPES_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TABLE_NAME)).addTargetColumns(ENTITY_TABLE_PK_COLUMN_NAME).setOnDelete(ReferentialAction.CASCADE).setOnUpdate(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.INITIALLY_DEFERRED_DEFERRABLE)).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).setTargetTableName(t.tableName(schemaName, ENTITY_TYPES_TABLE_NAME)).addTargetColumns(ENTITY_TYPES_TABLE_PK_COLUMN_NAME).setOnDelete(ReferentialAction.RESTRICT).setOnDelete(ReferentialAction.CASCADE).createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE)).createExpression()).createExpression().toString());
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ENUM_LOOKUP_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(ENUM_LOOKUP_TABLE_PK_COLUMN_NAME, this._primitiveTypes.get(Integer.class), false)).addTableElement(d.createColumnDefinition(ENUM_LOOKUP_TABLE_ENUM_VALUE_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ENUM_LOOKUP_TABLE_PK_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        tablePKs.put(ENUM_LOOKUP_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, USED_QNAMES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(USED_QNAMES_TABLE_QNAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createColumnDefinition(USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(USED_QNAMES_TABLE_QNAME_COLUMN_NAME, USED_QNAMES_TABLE_TABLE_NAME_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, ALL_QNAMES_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().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)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression())).addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(ENTITY_TABLE_PK_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)).createExpression()).createExpression()));
        tablePKs.put(ALL_QNAMES_TABLE_NAME, 0L);
        stmt.execute(vendor.toString(d.createTableDefinitionBuilder().setTableName(t.tableName(schemaName, APP_VERSION_TABLE_NAME)).setTableContentsSource(d.createTableElementListBuilder().addTableElement(d.createColumnDefinition(APP_VERSION_PK_COLUMN_NAME, this._primitiveTypes.get(String.class), false)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(APP_VERSION_PK_COLUMN_NAME).createExpression())).createExpression()).createExpression()));
        ModificationFactory m = vendor.getModificationFactory();
        PreparedStatement ps = connection.prepareStatement(vendor.toString(m.insert().setTableName(t.tableName(schemaName, APP_VERSION_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(vendor.getLiteralFactory().param()).createExpression()).createExpression()));
        ps.setString(1, this._app.version());
        ps.execute();
    // TODO INDICES!!!!
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
    // @formatter:on
    LOGGER.debug("Indexing SQL database tables created");
}
Also used : BigInteger(java.math.BigInteger) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) SQLVendor(org.sql.generation.api.vendor.SQLVendor) PreparedStatement(java.sql.PreparedStatement) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory) Date(java.util.Date)

Example 9 with ModificationFactory

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

the class AbstractSQLStartup method createInsertStatementForQNameInfo.

private InsertStatement createInsertStatementForQNameInfo(Connection connection, String schemaName, SQLVendor vendor) {
    ModificationFactory m = vendor.getModificationFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    LiteralFactory l = vendor.getLiteralFactory();
    return m.insert().setTableName(t.tableName(schemaName, USED_QNAMES_TABLE_NAME)).setColumnSource(m.columnSourceByValues().addValues(l.param(), l.param()).createExpression()).createExpression();
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory)

Example 10 with ModificationFactory

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

the class AbstractSQLStartup method clearSchema.

private static void clearSchema(Connection connection, String schemaName, SQLVendor vendor) throws SQLException {
    ModificationFactory m = vendor.getModificationFactory();
    Statement stmt = null;
    try {
        connection.setReadOnly(false);
        stmt = connection.createStatement();
        stmt.execute(m.deleteBySearch().setTargetTable(m.createTargetTable(vendor.getTableReferenceFactory().tableName(schemaName, DBNames.ENTITY_TABLE_NAME))).createExpression().toString());
        connection.commit();
    } finally {
        SQLUtil.closeQuietly(stmt);
    }
}
Also used : ModificationFactory(org.sql.generation.api.grammar.factories.ModificationFactory) InsertStatement(org.sql.generation.api.grammar.modification.InsertStatement) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement)

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