Class: PG::FTS::Index::ManyToOne

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

Overview

many documents, one source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

rubocop:disable Metrics/ParameterLists



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pg/fts/index/many_to_one.rb', line 11

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

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



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

def catalog
  @catalog
end

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



3
4
5
# File 'lib/pg/fts/index/many_to_one.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/many_to_one.rb', line 3

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pg/fts/index/many_to_one.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/many_to_one.rb', line 3

def table
  @table
end

Instance Method Details

#build_queryObject

rubocop:enable Metrics/ParameterLists



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pg/fts/index/many_to_one.rb', line 25

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

#on_document_delete_procedureObject



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/pg/fts/index/many_to_one.rb', line 258

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

#on_document_delete_queryObject



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

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

#on_document_delete_triggerObject



308
309
310
311
312
313
314
315
# File 'lib/pg/fts/index/many_to_one.rb', line 308

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

#on_document_insert_procedureObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/pg/fts/index/many_to_one.rb', line 198

def on_document_insert_procedure
  "  CREATE FUNCTION \"\#{on_document_insert_procedure_name}\"()\n  RETURNS TRIGGER AS $$\n    BEGIN\n      IF NEW.\"\#{foreign_key}\" IS NULL THEN\n        RETURN NEW;\n      END IF;\n      \#{on_document_insert_query.gsub(/^/, '    ')}\n      RETURN NEW;\n    END;\n  $$ LANGUAGE plpgsql;\n  SQL\nend\n".gsub(/^ {4}/, '')

#on_document_insert_queryObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pg/fts/index/many_to_one.rb', line 179

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

#on_document_insert_triggerObject



290
291
292
293
294
295
296
297
# File 'lib/pg/fts/index/many_to_one.rb', line 290

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

#on_document_truncate_procedureObject



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

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

#on_document_truncate_queryObject



270
271
272
273
274
275
276
# File 'lib/pg/fts/index/many_to_one.rb', line 270

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

#on_document_truncate_triggerObject



317
318
319
320
321
322
323
324
# File 'lib/pg/fts/index/many_to_one.rb', line 317

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

#on_document_update_procedureObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/pg/fts/index/many_to_one.rb', line 229

def on_document_update_procedure
  "  CREATE FUNCTION \"\#{on_document_update_procedure_name}\"()\n  RETURNS TRIGGER AS $$\n    BEGIN\n      \#{on_document_update_query.gsub(/^/, '    ')}\n      IF NOT FOUND THEN\n        \#{on_document_insert_query.gsub(/^/, '      ')}\n        IF NOT FOUND THEN\n          \#{on_document_delete_query.gsub(/^/, '        ')}\n        END IF;\n        RETURN NEW;\n      END IF;\n      RETURN NEW;\n    END;\n  $$ LANGUAGE plpgsql;\n  SQL\nend\n".gsub(/^ {4}/, '')

#on_document_update_queryObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pg/fts/index/many_to_one.rb', line 213

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

#on_document_update_triggerObject



299
300
301
302
303
304
305
306
# File 'lib/pg/fts/index/many_to_one.rb', line 299

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

#on_source_delete_procedureObject



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

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

#on_source_delete_queryObject



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

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

#on_source_delete_triggerObject



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

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

#on_source_insert_procedureObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pg/fts/index/many_to_one.rb', line 62

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

#on_source_insert_queryObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pg/fts/index/many_to_one.rb', line 43

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

#on_source_insert_triggerObject



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

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

#on_source_truncate_procedureObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pg/fts/index/many_to_one.rb', line 131

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

#on_source_truncate_queryObject



123
124
125
126
127
128
129
# File 'lib/pg/fts/index/many_to_one.rb', line 123

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

#on_source_truncate_triggerObject



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

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

#on_source_update_procedureObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pg/fts/index/many_to_one.rb', line 90

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

#on_source_update_queryObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pg/fts/index/many_to_one.rb', line 74

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

#on_source_update_triggerObject



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

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