/// <summary> /// </summary> public override string CommandTextForTriggerForUpdateSpatialObject(string layer, string triggerName, Dictionary <string, string> linkParams, bool clearWithoutLink, Chain chain, List <MapExpressionField> expressionFields, Dictionary <string, string> simpleFields, string owner = "", string functionSchema = "") { string triggerCommand = $"CREATE FUNCTION {functionSchema}{triggerName}_f() RETURNS TRIGGER AS $${Environment.NewLine}"; triggerCommand += $"BEGIN{Environment.NewLine}"; triggerCommand += $"UPDATE {layer} l{Environment.NewLine}SET{Environment.NewLine}"; var updates = expressionFields.Select(field => $"{field.LayerField} = {chain.ReplacePath(field.Paths, field.Expression)}").ToList(); updates.AddRange(simpleFields.Select(field => field.Key + " = " + chain.ReplacePath(field.Value)).ToArray()); if (!updates.Any()) { return(""); } triggerCommand += string.Join($",{Environment.NewLine}", updates) + Environment.NewLine; triggerCommand += $"FROM {layer} layer {(clearWithoutLink ? "LEFT" : "INNER")} JOIN {chain.FullTableName} {chain.Alias} ON "; triggerCommand += $"{string.Join(" AND ", linkParams.Select(links => $"layer.{links.Key} = {links.Value.Replace("@", chain.Alias + ".")}").ToArray())}{Environment.NewLine}"; triggerCommand += FromStatement("", chain); triggerCommand += $"WHERE NEW.{SpatialIdField} = layer.{SpatialIdField} AND l.{SpatialIdField} = layer.{SpatialIdField};{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 OF {string.Join(", ", linkParams.Select(link => link.Key).ToArray())} ON {layer}{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 override string CommandTextForTriggerForUpdateSpatialObject(string layer, string triggerName, Dictionary <string, string> linkParams, bool clearWithoutLink, Chain chain, List <MapExpressionField> expressionFields, Dictionary <string, string> simpleFields, string owner = "", string functionSchema = "") { string result = $@"CREATE TRIGGER {triggerName}{Environment.NewLine}"; result += $"ON {layer}{Environment.NewLine}"; result += "AFTER INSERT, UPDATE AS BEGIN" + Environment.NewLine; result += $"IF {string.Join(" OR ", linkParams.Select(link => $"UPDATE({link.Key})").ToArray())} BEGIN{Environment.NewLine}"; result += $"UPDATE layer SET{Environment.NewLine}"; var updates = expressionFields.Select(field => $"{field.LayerField} = {chain.ReplacePath(field.Paths, field.Expression)}").ToList(); updates.AddRange(simpleFields.Select(field => field.Key + " = " + chain.ReplacePath(field.Value)).ToArray()); result += string.Join(", ", updates) + Environment.NewLine; result += $"FROM {layer} layer {(clearWithoutLink ? "LEFT" : "INNER")} JOIN {chain.FullTableName} {chain.Alias} ON "; result += string.Join(" AND ", linkParams.Select(links => $"layer.{links.Key} = {chain.Alias}.{links.Value.Replace("@", "")}") .ToArray()) + Environment.NewLine; result += FromStatement("", chain); result += $"INNER JOIN inserted ON inserted.{SpatialIdField} = layer.{SpatialIdField} "; result += "END END"; return(result); }