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