Class: PgTrigger::Trigger
- Inherits:
-
Object
- Object
- PgTrigger::Trigger
- 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
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#timing ⇒ Object
readonly
Returns the value of attribute timing.
Class Method Summary collapse
Instance Method Summary collapse
- #after(*events) ⇒ Object
- #before(*events) ⇒ Object
- #create_function? ⇒ Boolean
- #create_function_sql ⇒ Object
- #create_trigger_sql ⇒ Object
- #drop_function_sql ⇒ Object
- #drop_trigger_sql ⇒ Object
-
#initialize ⇒ Trigger
constructor
A new instance of Trigger.
- #name ⇒ Object
- #named(name) ⇒ Object
- #nowrap ⇒ Object
- #nowrap? ⇒ Boolean
- #of(*columns) ⇒ Object
- #on(table_name) ⇒ Object
-
#same?(other) ⇒ Boolean
Compare content without taking indentation into account.
- #set_content_from_function(str) ⇒ Object
- #where(condition) ⇒ Object
- #where_clause ⇒ Object
Constructor Details
#initialize ⇒ Trigger
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
62 63 64 |
# File 'lib/pg_trigger/trigger.rb', line 62 def columns @columns end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
62 63 64 |
# File 'lib/pg_trigger/trigger.rb', line 62 def content @content end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
62 63 64 |
# File 'lib/pg_trigger/trigger.rb', line 62 def events @events end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
62 63 64 |
# File 'lib/pg_trigger/trigger.rb', line 62 def table @table end |
#timing ⇒ Object (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
130 131 132 |
# File 'lib/pg_trigger/trigger.rb', line 130 def create_function? !@options[:nowrap] end |
#create_function_sql ⇒ Object
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_sql ⇒ Object
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_sql ⇒ Object
172 173 174 |
# File 'lib/pg_trigger/trigger.rb', line 172 def drop_function_sql "DROP FUNCTION IF EXISTS #{name};" end |
#drop_trigger_sql ⇒ Object
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 |
#name ⇒ Object
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 |
#nowrap ⇒ Object
104 105 106 |
# File 'lib/pg_trigger/trigger.rb', line 104 def nowrap @options[:nowrap] = true end |
#nowrap? ⇒ 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
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_clause ⇒ Object
114 |
# File 'lib/pg_trigger/trigger.rb', line 114 def where_clause = @where |