/// <summary> /// </summary> public override string UpdateTrigger(string layer, string triggerName, Type rootType, Dictionary <string, string> linkParams, string fromTableName, MapField field, string setCommand, string owner = "", string functionSchema = "") { string triggerCommand = $"CREATE TRIGGER {triggerName}{Environment.NewLine}"; triggerCommand += $"ON {fromTableName} AFTER INSERT, UPDATE AS BEGIN{Environment.NewLine}"; triggerCommand += field.Chain.UpdatedFields(fromTableName).Any() ? "IF " + string.Join(" OR ", field.Chain.UpdatedFields(fromTableName) .Select(fld => $"UPDATE({fld})") .ToArray()) + " BEGIN" + Environment.NewLine : ""; triggerCommand += "UPDATE layer" + Environment.NewLine; triggerCommand += setCommand; triggerCommand += $"FROM {layer} layer INNER JOIN {(field.Chain.TableName == fromTableName ? "inserted" : field.Chain.FullTableName)} {field.Chain.Alias} ON "; triggerCommand += string.Join( " AND ", linkParams.Select( flink => $"layer.{flink.Key}={field.Chain.Alias}.{flink.Value.Replace("@", "")}"). ToArray()) + Environment.NewLine; triggerCommand += FromStatement(fromTableName, field.Chain) + Environment.NewLine; triggerCommand += "END" + Environment.NewLine; triggerCommand += "END"; return(triggerCommand); }
/// <summary> /// </summary> public abstract string UpdateTrigger(string layer, string triggerName, Type rootType, Dictionary <string, string> linkParams, string fromTableName, MapField field, string setCommand, string owner = "", string functionSchema = "");
/// <summary> /// </summary> public override string UpdateTrigger(string layer, string triggerName, Type rootType, Dictionary <string, string> linkParams, string fromTableName, MapField field, string setCommand, string owner, string functionSchema) { string triggerCommand = $"CREATE FUNCTION {functionSchema}{triggerName}_f() RETURNS TRIGGER AS $${Environment.NewLine}"; triggerCommand += $"BEGIN{Environment.NewLine}"; triggerCommand += $"UPDATE {layer} layer{Environment.NewLine}"; triggerCommand += setCommand; triggerCommand += $"FROM {field.Chain.FullTableName} {field.Chain.Alias} "; triggerCommand += $"{FromStatement(fromTableName, field.Chain)}{Environment.NewLine}"; triggerCommand += "WHERE "; var tableAlias = field.Chain.TableName == fromTableName ? "NEW" : field.Chain.Alias; triggerCommand += string.Join(" AND ", linkParams.Select(flink => $"layer.{flink.Key} = {flink.Value.Replace("@", tableAlias + ".")}").ToArray()) + Environment.NewLine; var fromTableChain = field.Chain.FindChain(fromTableName); triggerCommand += $" AND {fromTableChain.Alias}.{fromTableChain.PrimaryKey} = NEW.{fromTableChain.PrimaryKey};{Environment.NewLine}"; triggerCommand += $"RETURN NULL;{Environment.NewLine}"; triggerCommand += $"END{Environment.NewLine}"; triggerCommand += $"$$ LANGUAGE plpgsql;{Environment.NewLine}{Environment.NewLine}"; triggerCommand += $"CREATE TRIGGER {triggerName}{Environment.NewLine}"; triggerCommand += $"AFTER INSERT OR UPDATE"; triggerCommand += field.Chain.UpdatedFields(fromTableName).Any() ? $" OF {string.Join(", ", field.Chain.UpdatedFields(fromTableName).Select(fld => fld).ToArray())}" : ""; triggerCommand += $" ON {fromTableName}{Environment.NewLine}"; triggerCommand += $"FOR EACH ROW{Environment.NewLine}"; triggerCommand += $"EXECUTE PROCEDURE {functionSchema}{triggerName}_f();{Environment.NewLine}"; if (!string.IsNullOrEmpty(owner)) { triggerCommand += $"ALTER FUNCTION {functionSchema}{triggerName}_f() OWNER TO {owner};"; } return(triggerCommand); }
/// <summary> /// </summary> public abstract string Update(MapField field, string setCommand, string layer, Dictionary <string, string> linkParams);