Search in sources :

Example 1 with BasicTable

use of ru.curs.celesta.score.BasicTable in project celesta by CourseOrchestra.

the class H2DdlGenerator method createTableTriggersForMaterializedViews.

@Override
public List<String> createTableTriggersForMaterializedViews(BasicTable t) {
    List<String> result = new ArrayList<>();
    List<MaterializedView> mvList = t.getGrain().getElements(MaterializedView.class).values().stream().filter(mv -> mv.getRefTable().getTable().equals(t)).collect(Collectors.toList());
    for (MaterializedView mv : mvList) {
        TriggerQuery query = new TriggerQuery().withSchema(t.getGrain().getName()).withTableName(t.getName());
        String insertTriggerName = mv.getTriggerName(TriggerType.POST_INSERT);
        String updateTriggerName = mv.getTriggerName(TriggerType.POST_UPDATE);
        String deleteTriggerName = mv.getTriggerName(TriggerType.POST_DELETE);
        String sql;
        // INSERT
        sql = String.format("CREATE TRIGGER \"" + insertTriggerName + "\" AFTER INSERT ON " + tableString(t.getGrain().getName(), t.getName()) + " FOR EACH ROW CALL %n " + MaterializedView.CHECKSUM_COMMENT_TEMPLATE + "%n" + "\"%s\"", mv.getChecksum(), MaterializedViewInsertTrigger.class.getName());
        result.add(sql);
        this.rememberTrigger(query.withName(insertTriggerName));
        // UPDATE
        sql = String.format("CREATE TRIGGER \"" + updateTriggerName + "\" AFTER UPDATE ON " + tableString(t.getGrain().getName(), t.getName()) + " FOR EACH ROW CALL \"%s\"", MaterializedViewUpdateTrigger.class.getName());
        result.add(sql);
        this.rememberTrigger(query.withName(updateTriggerName));
        // DELETE
        sql = String.format("CREATE TRIGGER \"" + deleteTriggerName + "\" AFTER DELETE ON " + tableString(t.getGrain().getName(), t.getName()) + " FOR EACH ROW CALL \"%s\"", MaterializedViewDeleteTrigger.class.getName());
        result.add(sql);
        this.rememberTrigger(query.withName(deleteTriggerName));
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) CelestaException(ru.curs.celesta.CelestaException) TableElement(ru.curs.celesta.score.TableElement) DecimalColumn(ru.curs.celesta.score.DecimalColumn) CommonFunctions.getFieldList(ru.curs.celesta.dbutils.adaptors.function.CommonFunctions.getFieldList) TriggerQuery(ru.curs.celesta.event.TriggerQuery) ArrayList(java.util.ArrayList) MaterializedViewDeleteTrigger(ru.curs.celesta.dbutils.h2.MaterializedViewDeleteTrigger) ALTER_TABLE(ru.curs.celesta.dbutils.adaptors.constants.CommonConstants.ALTER_TABLE) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) StringColumn(ru.curs.celesta.score.StringColumn) DBAdaptor(ru.curs.celesta.dbutils.adaptors.DBAdaptor) ParameterizedView(ru.curs.celesta.score.ParameterizedView) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) DBType(ru.curs.celesta.DBType) SQLGenerator(ru.curs.celesta.score.SQLGenerator) IOException(java.io.IOException) Column(ru.curs.celesta.score.Column) MaterializedView(ru.curs.celesta.score.MaterializedView) Collectors(java.util.stream.Collectors) RecVersionCheckTrigger(ru.curs.celesta.dbutils.h2.RecVersionCheckTrigger) ColumnDefinerFactory(ru.curs.celesta.dbutils.adaptors.column.ColumnDefinerFactory) List(java.util.List) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) VersionedElement(ru.curs.celesta.score.VersionedElement) MaterializedViewUpdateTrigger(ru.curs.celesta.dbutils.h2.MaterializedViewUpdateTrigger) MaterializedViewInsertTrigger(ru.curs.celesta.dbutils.h2.MaterializedViewInsertTrigger) Index(ru.curs.celesta.score.Index) ArrayList(java.util.ArrayList) MaterializedView(ru.curs.celesta.score.MaterializedView) TriggerQuery(ru.curs.celesta.event.TriggerQuery)

Example 2 with BasicTable

use of ru.curs.celesta.score.BasicTable in project celesta by CourseOrchestra.

the class MsSqlDdlGenerator method dropTableTriggersForMaterializedViews.

@Override
public List<String> dropTableTriggersForMaterializedViews(Connection conn, BasicTable t) {
    List<String> result = new ArrayList<>();
    List<MaterializedView> mvList = t.getGrain().getElements(MaterializedView.class).values().stream().filter(mv -> mv.getRefTable().getTable().equals(t)).collect(Collectors.toList());
    TriggerQuery query = new TriggerQuery().withSchema(t.getGrain().getName()).withTableName(t.getName());
    for (MaterializedView mv : mvList) {
        String insertTriggerName = mv.getTriggerName(TriggerType.POST_INSERT);
        String deleteTriggerName = mv.getTriggerName(TriggerType.POST_DELETE);
        query.withName(insertTriggerName);
        if (this.triggerExists(conn, query)) {
            result.add(dropTrigger(query));
        }
        query.withName(deleteTriggerName);
        if (this.triggerExists(conn, query)) {
            result.add(dropTrigger(query));
        }
    }
    if (!mvList.isEmpty()) {
        // Set excessive 'rec_version' trigger to zero.
        query.withName(t.getName() + "_upd");
        if (this.triggerExists(conn, query)) {
            result.add(dropTrigger(query));
        }
        if (t instanceof VersionedElement && ((VersionedElement) t).isVersioned()) {
            result.add(createVersioningTrigger(t));
            this.rememberTrigger(query);
        }
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) DateTimeColumn(ru.curs.celesta.score.DateTimeColumn) CelestaException(ru.curs.celesta.CelestaException) TableElement(ru.curs.celesta.score.TableElement) LoggerFactory(org.slf4j.LoggerFactory) CommonFunctions.getFieldList(ru.curs.celesta.dbutils.adaptors.function.CommonFunctions.getFieldList) TriggerQuery(ru.curs.celesta.event.TriggerQuery) Grain(ru.curs.celesta.score.Grain) ArrayList(java.util.ArrayList) ALTER_TABLE(ru.curs.celesta.dbutils.adaptors.constants.CommonConstants.ALTER_TABLE) DbIndexInfo(ru.curs.celesta.dbutils.meta.DbIndexInfo) AbstractView(ru.curs.celesta.score.AbstractView) Count(ru.curs.celesta.score.Count) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) Map(java.util.Map) DBAdaptor(ru.curs.celesta.dbutils.adaptors.DBAdaptor) ParameterizedView(ru.curs.celesta.score.ParameterizedView) PrintWriter(java.io.PrintWriter) Logger(org.slf4j.Logger) Expr(ru.curs.celesta.score.Expr) StringWriter(java.io.StringWriter) DBType(ru.curs.celesta.DBType) SQLGenerator(ru.curs.celesta.score.SQLGenerator) IOException(java.io.IOException) Column(ru.curs.celesta.score.Column) MaterializedView(ru.curs.celesta.score.MaterializedView) Collectors(java.util.stream.Collectors) ColumnDefinerFactory(ru.curs.celesta.dbutils.adaptors.column.ColumnDefinerFactory) List(java.util.List) TriggerType(ru.curs.celesta.event.TriggerType) BasicTable(ru.curs.celesta.score.BasicTable) VersionedElement(ru.curs.celesta.score.VersionedElement) Optional(java.util.Optional) Sum(ru.curs.celesta.score.Sum) Index(ru.curs.celesta.score.Index) VersionedElement(ru.curs.celesta.score.VersionedElement) ArrayList(java.util.ArrayList) MaterializedView(ru.curs.celesta.score.MaterializedView) TriggerQuery(ru.curs.celesta.event.TriggerQuery)

Example 3 with BasicTable

use of ru.curs.celesta.score.BasicTable in project celesta by CourseOrchestra.

the class DbUpdater method updateTable.

boolean updateTable(BasicTable t, List<DbFkInfo> dbFKeys) {
    // If table was compiled with option NO AUTOUPDATE then nothing is to be done
    if (!t.isAutoUpdate()) {
        return false;
    }
    final Connection conn = schemaCursor.callContext().getConn();
    if (!dbAdaptor.tableExists(conn, t.getGrain().getName(), t.getName())) {
        // Table doesn't exist in the DB, create it from scratch.
        dbAdaptor.createTable(conn, t);
        return true;
    }
    DbPkInfo pkInfo;
    Set<String> dbColumns = dbAdaptor.getColumns(conn, t);
    boolean modified = updateColumns(t, conn, dbColumns, dbFKeys);
    // For versioned tables synchronize 'recversion' field
    if (t instanceof Table) {
        Table tab = (Table) t;
        if (tab.isVersioned()) {
            if (dbColumns.contains(VersionedElement.REC_VERSION)) {
                DbColumnInfo ci = dbAdaptor.getColumnInfo(conn, tab.getRecVersionField());
                if (!ci.reflects(tab.getRecVersionField())) {
                    dbAdaptor.updateColumn(conn, tab.getRecVersionField(), ci);
                    modified = true;
                }
            } else {
                dbAdaptor.createColumn(conn, tab.getRecVersionField());
                modified = true;
            }
        }
    }
    // Once again check the primary key, and if needed (in case it doesn't exist or
    // had been dropped) create it.
    pkInfo = dbAdaptor.getPKInfo(conn, t);
    if (pkInfo.isEmpty()) {
        dbAdaptor.createPK(conn, t);
    }
    dbAdaptor.updateVersioningTrigger(conn, t);
    return modified;
}
Also used : Table(ru.curs.celesta.score.Table) BasicTable(ru.curs.celesta.score.BasicTable) Connection(java.sql.Connection) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) DbPkInfo(ru.curs.celesta.dbutils.meta.DbPkInfo) VersionString(ru.curs.celesta.score.VersionString)

Example 4 with BasicTable

use of ru.curs.celesta.score.BasicTable in project celesta by CourseOrchestra.

the class DbUpdater method dropOrphanedGrainFKeys.

List<DbFkInfo> dropOrphanedGrainFKeys(Grain g) {
    Connection conn = schemaCursor.callContext().getConn();
    List<DbFkInfo> dbFKeys = dbAdaptor.getFKInfo(conn, g);
    Map<String, ForeignKey> fKeys = new HashMap<>();
    for (BasicTable t : g.getElements(BasicTable.class).values()) {
        for (ForeignKey fk : t.getForeignKeys()) {
            fKeys.put(fk.getConstraintName(), fk);
        }
    }
    Iterator<DbFkInfo> i = dbFKeys.iterator();
    while (i.hasNext()) {
        DbFkInfo dbFKey = i.next();
        ForeignKey fKey = fKeys.get(dbFKey.getName());
        if (fKey == null || !dbFKey.reflects(fKey)) {
            dbAdaptor.dropFK(conn, g.getName(), dbFKey.getTableName(), dbFKey.getName());
            i.remove();
        }
    }
    return dbFKeys;
}
Also used : HashMap(java.util.HashMap) Connection(java.sql.Connection) VersionString(ru.curs.celesta.score.VersionString) ForeignKey(ru.curs.celesta.score.ForeignKey) DbFkInfo(ru.curs.celesta.dbutils.meta.DbFkInfo) BasicTable(ru.curs.celesta.score.BasicTable)

Example 5 with BasicTable

use of ru.curs.celesta.score.BasicTable in project celesta by CourseOrchestra.

the class FirebirdAdaptor method getCurrentIdent.

@Override
public int getCurrentIdent(Connection conn, BasicTable t) {
    IntegerColumn idColumn = t.getPrimaryKey().values().stream().filter(c -> c instanceof IntegerColumn).map(c -> (IntegerColumn) c).filter(ic -> ic.getSequence() != null).findFirst().get();
    final SequenceElement s = idColumn.getSequence();
    String curValueProcName = sequenceCurValueProcString(s.getGrain().getName(), s.getName());
    String sql = String.format("EXECUTE PROCEDURE %s(null)", curValueProcName);
    try (Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql)) {
        rs.next();
        return rs.getInt(1);
    } catch (SQLException e) {
        throw new CelestaException(e.getMessage());
    }
}
Also used : ParameterSetter(ru.curs.celesta.dbutils.stmt.ParameterSetter) AnsiQuotedIdentifierParser(ru.curs.celesta.score.validator.AnsiQuotedIdentifierParser) Connection(java.sql.Connection) DateTimeColumn(ru.curs.celesta.score.DateTimeColumn) CelestaException(ru.curs.celesta.CelestaException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) FKRule(ru.curs.celesta.score.FKRule) SequenceElement(ru.curs.celesta.score.SequenceElement) Grain(ru.curs.celesta.score.Grain) SqlUtils.executeUpdate(ru.curs.celesta.dbutils.jdbc.SqlUtils.executeUpdate) Matcher(java.util.regex.Matcher) DbIndexInfo(ru.curs.celesta.dbutils.meta.DbIndexInfo) DbColumnInfo(ru.curs.celesta.dbutils.meta.DbColumnInfo) StringColumn(ru.curs.celesta.score.StringColumn) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataGrainElement(ru.curs.celesta.score.DataGrainElement) DbPkInfo(ru.curs.celesta.dbutils.meta.DbPkInfo) FromClause(ru.curs.celesta.dbutils.query.FromClause) DBType(ru.curs.celesta.DBType) Set(java.util.Set) SchemalessFunctions(ru.curs.celesta.dbutils.adaptors.function.SchemalessFunctions) BooleanColumn(ru.curs.celesta.score.BooleanColumn) ConnectionPool(ru.curs.celesta.ConnectionPool) PreparedStatement(java.sql.PreparedStatement) Column(ru.curs.celesta.score.Column) ZoneId(java.time.ZoneId) List(java.util.List) BasicTable(ru.curs.celesta.score.BasicTable) Pattern(java.util.regex.Pattern) IntegerColumn(ru.curs.celesta.score.IntegerColumn) TableElement(ru.curs.celesta.score.TableElement) DecimalColumn(ru.curs.celesta.score.DecimalColumn) DatabaseMetaData(java.sql.DatabaseMetaData) HashMap(java.util.HashMap) DbFkInfo(ru.curs.celesta.dbutils.meta.DbFkInfo) TriggerQuery(ru.curs.celesta.event.TriggerQuery) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) FireBirdConstants(ru.curs.celesta.dbutils.adaptors.constants.FireBirdConstants) SqlUtils(ru.curs.celesta.dbutils.jdbc.SqlUtils) DbSequenceInfo(ru.curs.celesta.dbutils.meta.DbSequenceInfo) BinaryColumn(ru.curs.celesta.score.BinaryColumn) DdlGenerator(ru.curs.celesta.dbutils.adaptors.ddl.DdlGenerator) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DdlConsumer(ru.curs.celesta.dbutils.adaptors.ddl.DdlConsumer) Statement(java.sql.Statement) NamedElement(ru.curs.celesta.score.NamedElement) FirebirdDdlGenerator(ru.curs.celesta.dbutils.adaptors.ddl.FirebirdDdlGenerator) IntegerColumn(ru.curs.celesta.score.IntegerColumn) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SequenceElement(ru.curs.celesta.score.SequenceElement) ResultSet(java.sql.ResultSet) CelestaException(ru.curs.celesta.CelestaException)

Aggregations

BasicTable (ru.curs.celesta.score.BasicTable)30 CelestaException (ru.curs.celesta.CelestaException)23 Connection (java.sql.Connection)18 DbColumnInfo (ru.curs.celesta.dbutils.meta.DbColumnInfo)18 Grain (ru.curs.celesta.score.Grain)17 List (java.util.List)15 Column (ru.curs.celesta.score.Column)15 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 MaterializedView (ru.curs.celesta.score.MaterializedView)14 TableElement (ru.curs.celesta.score.TableElement)14 SQLException (java.sql.SQLException)13 Collectors (java.util.stream.Collectors)13 DBType (ru.curs.celesta.DBType)13 TriggerQuery (ru.curs.celesta.event.TriggerQuery)13 SequenceElement (ru.curs.celesta.score.SequenceElement)13 StringColumn (ru.curs.celesta.score.StringColumn)13 Map (java.util.Map)12 DateTimeColumn (ru.curs.celesta.score.DateTimeColumn)12 DecimalColumn (ru.curs.celesta.score.DecimalColumn)12