Class: PG::FTS::Index::OMMO

Inherits:
Object
  • Object
show all
Includes:
PG::FTS::Index, Naming, TSVector
Defined in:
lib/pg/fts/index/ommo.rb

Overview

one document, many links, many links, one source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, *fields, document: nil, link: nil, key: nil, foreign_key: nil, fordocument_key: nil, catalog: nil, as: nil) ⇒ OMMO

rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pg/fts/index/ommo.rb', line 14

def initialize(table, *fields,
               document: nil, link: nil,
               key: nil, foreign_key: nil, fordocument_key: nil,
               catalog: nil,
               as: nil)
  @table = table
  @fields = fields
  @catalog = catalog || PG::FTS.catalog
  @document = document || fail(ArgumentError, ':document required')
  @link = link || fail(ArgumentError, ':link required')
  @key = key || :id
  @foreign_key = foreign_key || :"#{table}_id"
  @fordocument_key = fordocument_key || :"#{document}_id"
  @name = as
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def catalog
  @catalog
end

#documentObject (readonly)

Returns the value of attribute document.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def document
  @document
end

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def fields
  @fields
end

#fordocument_keyObject (readonly)

Returns the value of attribute fordocument_key.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def fordocument_key
  @fordocument_key
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def foreign_key
  @foreign_key
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def key
  @key
end

Returns the value of attribute link.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def link
  @link
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def name
  @name
end

#tableObject (readonly) Also known as: source

Returns the value of attribute table.



3
4
5
# File 'lib/pg/fts/index/ommo.rb', line 3

def table
  @table
end

Instance Method Details

#build_queryObject

rubocop:enable Metrics/ParameterLists, Metrics/CyclomaticComplexity



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pg/fts/index/ommo.rb', line 31

def build_query
  <<-SQL.gsub(/^ {4}/, '')
  INSERT INTO "#{PG::FTS.table}" (
    "document_id",
    "document_table",
    "source_id",
    "source_table",
    "tsv")
  SELECT "#{document}"."#{key}",
         '#{document}',
         "#{source}"."#{key}",
         '#{source}',
         #{ts_vector(source)}
  FROM "#{document}", "#{source}", "#{link}"
  WHERE "#{document}"."#{key}" = "#{link}"."#{fordocument_key}"
    AND "#{link}"."#{foreign_key}" = "#{source}"."#{key}";
  SQL
end

#on_document_delete_procedureObject



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/pg/fts/index/ommo.rb', line 246

def on_document_delete_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_document_delete_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_document_delete_query.gsub(/^/, '    ')}
      RETURN OLD;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_document_delete_queryObject



237
238
239
240
241
242
243
244
# File 'lib/pg/fts/index/ommo.rb', line 237

def on_document_delete_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
  WHERE "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_id" = OLD."#{key}"
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end

#on_document_delete_triggerObject



296
297
298
299
300
301
302
303
# File 'lib/pg/fts/index/ommo.rb', line 296

def on_document_delete_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_document_delete_trigger_name}"
  AFTER DELETE ON "#{document}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_document_delete_procedure_name}"();
  SQL
end

#on_document_insert_procedureObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/pg/fts/index/ommo.rb', line 208

def on_document_insert_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_document_insert_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_document_insert_query.gsub(/^/, '    ')}
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_document_insert_queryObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pg/fts/index/ommo.rb', line 188

def on_document_insert_query
  <<-SQL.gsub(/^ {4}/, '')
  INSERT INTO "#{PG::FTS.table}" (
    "document_id",
    "document_table",
    "source_id",
    "source_table",
    "tsv")
  SELECT
    NEW."#{key}",
    '#{document}',
    "#{source}"."#{key}",
    '#{source}',
    #{ts_vector(source)}
  FROM "#{source}", "#{link}"
  WHERE "#{source}"."#{key}" = "#{link}"."#{foreign_key}"
    AND "#{link}"."#{fordocument_key}" = NEW."#{key}";
  SQL
end

#on_document_insert_triggerObject



278
279
280
281
282
283
284
285
# File 'lib/pg/fts/index/ommo.rb', line 278

def on_document_insert_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_document_insert_trigger_name}"
  AFTER INSERT ON "#{document}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_document_insert_procedure_name}"();
  SQL
end

#on_document_truncate_procedureObject



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/pg/fts/index/ommo.rb', line 266

def on_document_truncate_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_document_truncate_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_document_truncate_query.gsub(/^/, '    ')}
      RETURN NULL;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_document_truncate_queryObject



258
259
260
261
262
263
264
# File 'lib/pg/fts/index/ommo.rb', line 258

def on_document_truncate_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
  WHERE "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end

#on_document_truncate_triggerObject



305
306
307
308
309
310
311
312
# File 'lib/pg/fts/index/ommo.rb', line 305

def on_document_truncate_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_document_truncate_trigger_name}"
  AFTER TRUNCATE ON "#{document}"
  FOR EACH STATEMENT
  EXECUTE PROCEDURE "#{on_document_truncate_procedure_name}"();
  SQL
end

#on_document_update_procedureObject



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/pg/fts/index/ommo.rb', line 225

def on_document_update_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_document_update_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_document_update_query.gsub(/^/, '    ')}
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_document_update_queryObject



220
221
222
223
# File 'lib/pg/fts/index/ommo.rb', line 220

def on_document_update_query
  <<-SQL.gsub(/^ {4}/, '')
  SQL
end

#on_document_update_triggerObject



287
288
289
290
291
292
293
294
# File 'lib/pg/fts/index/ommo.rb', line 287

def on_document_update_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_document_update_trigger_name}"
  AFTER UPDATE ON "#{document}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_document_update_procedure_name}"();
  SQL
end


397
398
399
400
401
402
403
404
405
406
407
# File 'lib/pg/fts/index/ommo.rb', line 397

def on_link_delete_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_link_delete_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_link_delete_query.gsub(/^/, '    ')}
      RETURN OLD;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end


384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/pg/fts/index/ommo.rb', line 384

def on_link_delete_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
        USING "#{link}", "#{document}", "#{source}"
  WHERE OLD."#{fordocument_key}" = "#{document}"."#{key}"
    AND OLD."#{foreign_key}" = "#{source}"."#{key}"
    AND "#{PG::FTS.table}"."source_id" = "#{source}"."#{key}"
    AND "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_id" = "#{document}"."#{key}"
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end


447
448
449
450
451
452
453
454
# File 'lib/pg/fts/index/ommo.rb', line 447

def on_link_delete_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_link_delete_trigger_name}"
  AFTER DELETE ON "#{link}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_link_delete_procedure_name}"();
  SQL
end


334
335
336
337
338
339
340
341
342
343
344
# File 'lib/pg/fts/index/ommo.rb', line 334

def on_link_insert_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_link_insert_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_link_insert_query.gsub(/^/, '    ')}
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/pg/fts/index/ommo.rb', line 314

def on_link_insert_query
  <<-SQL.gsub(/^ {4}/, '')
  INSERT INTO "#{PG::FTS.table}" (
    "document_id",
    "document_table",
    "source_id",
    "source_table",
    "tsv")
  SELECT
    "#{document}"."#{key}",
    '#{document}',
    "#{source}"."#{key}",
    '#{source}',
    #{ts_vector(source)}
  FROM "#{document}", "#{source}"
  WHERE "#{document}"."#{key}" = NEW."#{fordocument_key}"
    AND "#{source}"."#{key}" = NEW."#{foreign_key}";
  SQL
end


429
430
431
432
433
434
435
436
# File 'lib/pg/fts/index/ommo.rb', line 429

def on_link_insert_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_link_insert_trigger_name}"
  AFTER INSERT ON "#{link}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_link_insert_procedure_name}"();
  SQL
end


417
418
419
420
421
422
423
424
425
426
427
# File 'lib/pg/fts/index/ommo.rb', line 417

def on_link_truncate_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_link_truncate_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_link_truncate_query.gsub(/^/, '    ')}
      RETURN NULL;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end


409
410
411
412
413
414
415
# File 'lib/pg/fts/index/ommo.rb', line 409

def on_link_truncate_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
  WHERE "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end


456
457
458
459
460
461
462
463
# File 'lib/pg/fts/index/ommo.rb', line 456

def on_link_truncate_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_link_truncate_trigger_name}"
  AFTER TRUNCATE ON "#{link}"
  FOR EACH STATEMENT
  EXECUTE PROCEDURE "#{on_link_truncate_procedure_name}"();
  SQL
end


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/pg/fts/index/ommo.rb', line 365

def on_link_update_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_link_update_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_link_update_query.gsub(/^/, '    ')}
      IF NOT FOUND THEN
        #{on_link_insert_query.gsub(/^/, '      ')}
        IF NOT FOUND THEN
          #{on_link_delete_query.gsub(/^/, '        ')}
        END IF;
        RETURN NEW;
      END IF;
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/pg/fts/index/ommo.rb', line 346

def on_link_update_query
  <<-SQL.gsub(/^ {4}/, '')
  UPDATE "#{PG::FTS.table}"
  SET
    "tsv" = (#{ts_vector(source)}),
    "document_table" = '#{document}',
    "document_id" = NEW."#{fordocument_key}",
    "source_table" = '#{source}',
    "source_id" = NEW."#{foreign_key}"
  FROM "#{document}", "#{source}"
  WHERE "#{document}"."#{key}" = NEW."#{fordocument_key}"
    AND "#{source}"."#{key}" = NEW."#{foreign_key}"
    AND "#{PG::FTS.table}"."source_id" = OLD."#{foreign_key}"
    AND "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_id" = OLD."#{fordocument_key}"
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end


438
439
440
441
442
443
444
445
# File 'lib/pg/fts/index/ommo.rb', line 438

def on_link_update_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_link_update_trigger_name}"
  AFTER UPDATE ON "#{link}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_link_update_procedure_name}"();
  SQL
end

#on_source_delete_procedureObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pg/fts/index/ommo.rb', line 120

def on_source_delete_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_source_delete_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_source_delete_query.gsub(/^/, '    ')}
      RETURN OLD;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_source_delete_queryObject



111
112
113
114
115
116
117
118
# File 'lib/pg/fts/index/ommo.rb', line 111

def on_source_delete_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
  WHERE "#{PG::FTS.table}"."source_id" = OLD."#{key}"
    AND "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end

#on_source_delete_triggerObject



170
171
172
173
174
175
176
177
# File 'lib/pg/fts/index/ommo.rb', line 170

def on_source_delete_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_source_delete_trigger_name}"
  AFTER DELETE ON "#{source}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_source_delete_procedure_name}"();
  SQL
end

#on_source_insert_procedureObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pg/fts/index/ommo.rb', line 70

def on_source_insert_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_source_insert_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_source_insert_query.gsub(/^/, '    ')}
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_source_insert_queryObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pg/fts/index/ommo.rb', line 50

def on_source_insert_query
  <<-SQL.gsub(/^ {4}/, '')
  INSERT INTO "#{PG::FTS.table}" (
    "document_id",
    "document_table",
    "source_id",
    "source_table",
    "tsv")
  SELECT
    "#{document}"."#{key}",
    '#{document}',
    NEW."#{key}",
    '#{source}',
    #{ts_vector}
  FROM "#{document}", "#{link}"
  WHERE "#{document}"."#{key}" = "#{link}"."#{fordocument_key}"
    AND "#{link}"."#{foreign_key}" = NEW."#{key}";
  SQL
end

#on_source_insert_triggerObject



152
153
154
155
156
157
158
159
# File 'lib/pg/fts/index/ommo.rb', line 152

def on_source_insert_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_source_insert_trigger_name}"
  AFTER INSERT ON "#{source}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_source_insert_procedure_name}"();
  SQL
end

#on_source_truncate_procedureObject



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pg/fts/index/ommo.rb', line 140

def on_source_truncate_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_source_truncate_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_source_truncate_query.gsub(/^/, '    ')}
      RETURN NULL;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_source_truncate_queryObject



132
133
134
135
136
137
138
# File 'lib/pg/fts/index/ommo.rb', line 132

def on_source_truncate_query
  <<-SQL.gsub(/^ {4}/, '')
  DELETE FROM "#{PG::FTS.table}"
  WHERE "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end

#on_source_truncate_triggerObject



179
180
181
182
183
184
185
186
# File 'lib/pg/fts/index/ommo.rb', line 179

def on_source_truncate_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_source_truncate_trigger_name}"
  AFTER TRUNCATE ON "#{source}"
  FOR EACH STATEMENT
  EXECUTE PROCEDURE "#{on_source_truncate_procedure_name}"();
  SQL
end

#on_source_update_procedureObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pg/fts/index/ommo.rb', line 99

def on_source_update_procedure
  <<-SQL.gsub(/^ {4}/, '')
  CREATE FUNCTION "#{on_source_update_procedure_name}"()
  RETURNS TRIGGER AS $$
    BEGIN
      #{on_source_update_query.gsub(/^/, '    ')}
      RETURN NEW;
    END;
  $$ LANGUAGE plpgsql;
  SQL
end

#on_source_update_queryObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pg/fts/index/ommo.rb', line 82

def on_source_update_query
  <<-SQL.gsub(/^ {4}/, '')
  UPDATE "#{PG::FTS.table}"
  SET
    "tsv" = (#{ts_vector}),
    "document_table" = '#{document}',
    "document_id" = "#{document}"."#{key}"
  FROM "#{document}", "#{link}"
  WHERE "#{document}"."#{key}" = "#{link}"."#{fordocument_key}"
    AND "#{link}"."#{foreign_key}" = NEW."#{key}"
    AND "#{PG::FTS.table}"."source_id" = OLD."#{key}"
    AND "#{PG::FTS.table}"."source_table" = '#{source}'
    AND "#{PG::FTS.table}"."document_id" = "#{document}"."#{key}"
    AND "#{PG::FTS.table}"."document_table" = '#{document}';
  SQL
end

#on_source_update_triggerObject



161
162
163
164
165
166
167
168
# File 'lib/pg/fts/index/ommo.rb', line 161

def on_source_update_trigger
  <<-SQL.gsub(/^ {4}/, '')
  CREATE TRIGGER "#{on_source_update_trigger_name}"
  AFTER UPDATE ON "#{source}"
  FOR EACH ROW
  EXECUTE PROCEDURE "#{on_source_update_procedure_name}"();
  SQL
end