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