public string Generate(TriggerEntity trigger)
        {
            if (trigger == null)
            {
                throw new ArgumentNullException(nameof(trigger));
            }

            var sql = new StringBuilder();

            sql.Append("CREATE TRIGGER ");

            if (trigger.ExistsCheck)
            {
                sql.Append("IF NOT EXISTS ");
            }

            sql.Append($"\"{trigger.Name}\" ");

            sql.Append($"{GetTriggerEventString(trigger.EventType)} ");
            sql.Append($"{GetTriggerActionString(trigger.ActionType)} ");

            sql.Append($"ON \"{trigger.TableName}\" ");

            sql.AppendLine();

            if (!string.IsNullOrEmpty(trigger.Condition))
            {
                sql.Append("WHEN ");
                sql.Append(trigger.Condition);
                sql.Append(" ");
                sql.AppendLine();
            }

            sql.Append("BEGIN ");
            sql.AppendLine();

            foreach (var line in trigger.Script.ToString()
                     .Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries))
            {
                sql.Append(_indent);
                sql.Append(line.Trim());
                sql.Append(";");
                sql.AppendLine();
            }

            sql.Append("END;");

            return(sql.ToString());
        }
示例#2
0
 public TriggerBuilder(string name)
 {
     _trigger      = new TriggerEntity();
     _trigger.Name = name;
 }