Class: Replicator::Trigger

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, opts) ⇒ Trigger

Returns a new instance of Trigger.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/replicator.rb', line 57

def initialize(table, opts)
  @from       = table
  @to         = opts[:to]
  @name       = opts[:name]
  @key        = opts[:key] || 'id'
  @condition  = opts[:condition] || opts[:if]
  @prefix     = opts[:prefix]
  @prefix_map = opts[:prefix_map]
  @timestamps = opts[:timestamps]
  @dependent  = opts[:dependent]

  if opts[:through]
    @through_table, @through_key = opts[:through].split('.')
    raise "through must be of the form 'table.field'" unless @through_table and @through_key
  end

  # Use opts[:prefixes] to specify valid prefixes and use the identity mapping.
  if @prefix_map.nil? and opts[:prefixes]
    @prefix_map = {}
    opts[:prefixes].each do |prefix|
      @prefix_map[prefix] = prefix
    end
  end

  if @prefix_map
    @prefix, prefix_table = @prefix.split('.').reverse
    
    if prefix_table.nil? or prefix_table == from
      @prefix_through = false
    elsif prefix_table == through_table
      @prefix_through = true
    else
      "unknown prefix table: #{prefix_table}" 
    end
  end

  @fields = {}
  opts[:fields] = [opts[:fields]] unless opts[:fields].kind_of?(Array)
  opts[:fields].each do |field|
    if field.kind_of?(Hash)
      @fields.merge!(field)
    else
      @fields[field] = field
    end
  end
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



55
56
57
# File 'lib/replicator.rb', line 55

def condition
  @condition
end

#dependentObject (readonly)

Returns the value of attribute dependent.



55
56
57
# File 'lib/replicator.rb', line 55

def dependent
  @dependent
end

#fromObject (readonly)

Returns the value of attribute from.



55
56
57
# File 'lib/replicator.rb', line 55

def from
  @from
end

#keyObject (readonly)

Returns the value of attribute key.



55
56
57
# File 'lib/replicator.rb', line 55

def key
  @key
end

#prefixObject (readonly)

Returns the value of attribute prefix.



55
56
57
# File 'lib/replicator.rb', line 55

def prefix
  @prefix
end

#prefix_mapObject (readonly)

Returns the value of attribute prefix_map.



55
56
57
# File 'lib/replicator.rb', line 55

def prefix_map
  @prefix_map
end

#through_keyObject (readonly)

Returns the value of attribute through_key.



55
56
57
# File 'lib/replicator.rb', line 55

def through_key
  @through_key
end

#through_tableObject (readonly)

Returns the value of attribute through_table.



55
56
57
# File 'lib/replicator.rb', line 55

def through_table
  @through_table
end

#toObject (readonly)

Returns the value of attribute to.



55
56
57
# File 'lib/replicator.rb', line 55

def to
  @to
end

Instance Method Details

#create_sqlObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/replicator.rb', line 123

def create_sql
  %{
    CREATE OR REPLACE FUNCTION #{name}() RETURNS TRIGGER AS $$
      DECLARE
        ROW     RECORD;
        THROUGH RECORD;
      BEGIN
        IF (TG_OP = 'DELETE') THEN
          ROW := OLD;
        ELSE
          ROW := NEW;
        END IF;
        #{loop_sql}
          IF #{conditions_sql} THEN
            IF (TG_OP = 'DELETE') THEN
              #{delete_sql(:indent => 18)}
            ELSE
              IF COUNT(*) = 0 FROM #{to} WHERE id = #{primary_key} THEN
                #{insert_sql}
              END IF;
              #{update_all_sql(:indent => 18)}
            END IF;
          END IF;
        #{end_loop_sql}
        RETURN NULL;
      END;
    $$ LANGUAGE plpgsql;
    CREATE TRIGGER #{name} AFTER INSERT OR UPDATE OR DELETE ON #{from}
      FOR EACH ROW EXECUTE PROCEDURE #{name}();
  }
end

#drop_sqlObject



198
199
200
# File 'lib/replicator.rb', line 198

def drop_sql
  "DROP FUNCTION IF EXISTS #{name}() CASCADE"
end

#fields(opts = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/replicator.rb', line 104

def fields(opts = nil)
  if opts
    opts[:row] ||= 'ROW'
    # Add the prefixes and return an array of arrays.
    @fields.collect do |from_field, to_field|
      if from_field.kind_of?(Array)
        from_field = from_field.collect {|f| "COALESCE(#{opts[:row]}.#{f}, '')"}.join(" || ' ' || ") 
      else
        from_field = "#{opts[:row]}.#{from_field}"
      end
      to_field = [opts[:prefix], to_field].compact.join('_')
      [from_field, to_field]
    end
  else
    # Just return the hash.
    @fields
  end
end

#initialize_sqlObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/replicator.rb', line 155

def initialize_sql
  sql_by_slice = {}
  
  if through?
    tables = "#{from}, #{through_table}"
    join   = "#{from}.id = #{through_table}.#{through_key}"
  else
    tables = from
  end

  if prefix_map
    prefix_map.each do |prefix_value, mapping|
      slice_fields = []
      field_sql = fields(:prefix => mapping, :row => from).collect do |from_field, to_field|
        slice_fields << to_field
        "#{from_field} AS #{to_field}"
      end.join(', ')
      where_sql  = "WHERE " << [join, "#{prefix_field(true)} = '#{prefix_value}'"].compact.join(' AND ')
      table_name = "#{name}_#{mapping}"
      
      slice = {:name => table_name, :fields => slice_fields}
      sql_by_slice[slice] = %{
        CREATE TABLE #{table_name} AS
          SELECT #{primary_key(true)} AS id, #{field_sql} FROM #{tables} #{where_sql}
      }
    end
  else
    slice_fields = []
    field_sql = fields(:prefix => prefix, :row => from).collect do |from_field, to_field|
      slice_fields << to_field
      "#{from_field} AS #{to_field}"
    end.join(', ')
    where_sql  = "WHERE #{join}" if join
    
    slice = {:name => name, :fields => slice_fields}
    sql_by_slice[slice] = %{
      CREATE TABLE #{name} AS
        SELECT #{primary_key(true)} AS id, #{field_sql} FROM #{tables} #{where_sql}
    }
  end
  sql_by_slice
end

#prefix_through?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/replicator.rb', line 210

def prefix_through?
  @prefix_through
end

#through?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/replicator.rb', line 206

def through?
  not through_table.nil?
end

#timestamps?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/replicator.rb', line 202

def timestamps?
  @timestamps
end