Class: PgTrigger::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_trigger/trigger.rb

Constant Summary collapse

DEFN_REGEXP =
[
  "\\ACREATE TRIGGER",
  "(?<name>\\w+)",
  "(?<timing>AFTER|BEFORE)",
  "(?<events>(?:INSERT|UPDATE|DELETE)(?: OR (?:INSERT|UPDATE|DELETE))?)",
  "(?:OF(?<columns>(?:\\s[a-z0-9_]+,?)+)\\s)?ON (?:[\\w\"]+\\.)?(?<table>\\w+)",
  "FOR EACH ROW(?: WHEN \\((?<where>\\(?[^\\)]+\\)?)\\))?",
  "EXECUTE FUNCTION (?:\\w+\\.)?(?<fn>\\w+)",
].join("\\s")
.yield_self { |str| Regexp.new(str) }
FN_CONTENT_REGEX =
/BEGIN\s+(?<content>.+;)\n\s+RETURN NULL;/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrigger

Returns a new instance of Trigger.



64
65
66
67
68
69
70
71
72
73
# File 'lib/pg_trigger/trigger.rb', line 64

def initialize
  @name     = nil
  @table    = nil
  @timing   = nil
  @events   = []
  @columns  = []
  @content  = nil
  @where    = nil
  @options  = {}
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



62
63
64
# File 'lib/pg_trigger/trigger.rb', line 62

def columns
  @columns
end

#contentObject (readonly)

Returns the value of attribute content.



62
63
64
# File 'lib/pg_trigger/trigger.rb', line 62

def content
  @content
end

#eventsObject (readonly)

Returns the value of attribute events.



62
63
64
# File 'lib/pg_trigger/trigger.rb', line 62

def events
  @events
end

#tableObject (readonly)

Returns the value of attribute table.



62
63
64
# File 'lib/pg_trigger/trigger.rb', line 62

def table
  @table
end

#timingObject (readonly)

Returns the value of attribute timing.



62
63
64
# File 'lib/pg_trigger/trigger.rb', line 62

def timing
  @timing
end

Class Method Details

.chain(*methods) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pg_trigger/trigger.rb', line 9

def chain(*methods)
  methods.each do |method|
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      alias orig_#{method} #{method}

      def #{method}(*args, &block)
        orig_#{method}(*args)
        @content = normalize_string(yield) if block
        self
      end
    RUBY
  end
end

.from_definition(defn) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pg_trigger/trigger.rb', line 34

def from_definition(defn)
  match = defn.match(DEFN_REGEXP)

  if !match
    raise InvalidTriggerDefinition, defn if PgTrigger.raise_on_invalid_definition
    return
  end

  trigger = new
  trigger.named(match[:name]).on(match[:table])
  trigger.public_send(match[:timing].downcase, *match[:events].split(" OR ").map! { |e| e.downcase.to_sym })

  if (cols = match[:columns])
    trigger.of(*cols.split(", ").map!(&:lstrip))
  end

  if (where = match[:where])
    trigger.where(where)
  end

  if match[:fn] != match[:name]
    trigger.nowrap { "#{match[:fn]}()" }
  end

  trigger
end

Instance Method Details

#after(*events) ⇒ Object



79
80
81
82
# File 'lib/pg_trigger/trigger.rb', line 79

def after(*events)
  @timing = :after
  @events.concat(format_events(events))
end

#before(*events) ⇒ Object



84
85
86
87
# File 'lib/pg_trigger/trigger.rb', line 84

def before(*events)
  @timing = :before
  @events.concat(format_events(events))
end

#create_function?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/pg_trigger/trigger.rb', line 130

def create_function?
  !@options[:nowrap]
end

#create_function_sqlObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pg_trigger/trigger.rb', line 138

def create_function_sql
  str = IndentedString.new(size: 4)
  str += @content

  <<~SQL
    CREATE OR REPLACE FUNCTION #{name}() RETURNS TRIGGER
    AS $$
      BEGIN
    #{str}
        RETURN NULL;
      END
    $$ LANGUAGE plpgsql;
  SQL
end

#create_trigger_sqlObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pg_trigger/trigger.rb', line 153

def create_trigger_sql
  action = if create_function?
    "#{name}();"
  else
    @content
  end

  sql = "CREATE TRIGGER #{name}\n"
  sql << @timing.to_s.upcase
  sql << " #{events.map(&:upcase).join(" OR ")} "
  sql << "OF #{columns.join(", ")} " if columns.any?
  sql << "ON #{adapter.quote_table_name(@table)}\n"
  sql << "FOR EACH ROW\n"
  sql << "WHEN (#{@where})\n" unless @where.nil?
  sql << "EXECUTE FUNCTION #{action}"

  sql
end

#drop_function_sqlObject



172
173
174
# File 'lib/pg_trigger/trigger.rb', line 172

def drop_function_sql
  "DROP FUNCTION IF EXISTS #{name};"
end

#drop_trigger_sqlObject



176
177
178
# File 'lib/pg_trigger/trigger.rb', line 176

def drop_trigger_sql
  "DROP TRIGGER IF EXISTS #{name} ON #{adapter.quote_table_name(@table)};"
end

#nameObject



110
111
112
# File 'lib/pg_trigger/trigger.rb', line 110

def name
  @name ||= inferred_name
end

#named(name) ⇒ Object



100
101
102
# File 'lib/pg_trigger/trigger.rb', line 100

def named(name)
  @name = name
end

#nowrapObject



104
105
106
# File 'lib/pg_trigger/trigger.rb', line 104

def nowrap
  @options[:nowrap] = true
end

#nowrap?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/pg_trigger/trigger.rb', line 134

def nowrap?
  @options[:nowrap]
end

#of(*columns) ⇒ Object



89
90
91
# File 'lib/pg_trigger/trigger.rb', line 89

def of(*columns)
  @columns.concat(columns)
end

#on(table_name) ⇒ Object



75
76
77
# File 'lib/pg_trigger/trigger.rb', line 75

def on(table_name)
  @table = table_name
end

#same?(other) ⇒ Boolean

Compare content without taking indentation into account

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/pg_trigger/trigger.rb', line 117

def same?(other)
  where_clause&.downcase == other&.where_clause&.downcase &&
    content.gsub(/\s+/, " ") == other.content.gsub(/\s+/, " ")
end

#set_content_from_function(str) ⇒ Object



124
125
126
127
128
# File 'lib/pg_trigger/trigger.rb', line 124

def set_content_from_function(str)
  if (match = str.match(FN_CONTENT_REGEX))
    @content = match[:content]
  end
end

#where(condition) ⇒ Object



93
94
95
96
97
98
# File 'lib/pg_trigger/trigger.rb', line 93

def where(condition)
  if condition.start_with?("NOT", "not")
    raise AmbiguousConditionError, "when condition starting with a NOT should be enclosed in parenthesis"
  end
  @where = condition
end

#where_clauseObject



114
# File 'lib/pg_trigger/trigger.rb', line 114

def where_clause = @where