Class: PG::FTS::Index::ManyToOne
- Inherits:
-
Object
- Object
- PG::FTS::Index::ManyToOne
- 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
-
#catalog ⇒ Object
readonly
Returns the value of attribute catalog.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
(also: #source)
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#build_query ⇒ Object
rubocop:enable Metrics/ParameterLists.
-
#initialize(table, *fields, document: nil, key: nil, foreign_key: nil, catalog: nil, as: nil) ⇒ ManyToOne
constructor
rubocop:disable Metrics/ParameterLists.
- #on_document_delete_procedure ⇒ Object
- #on_document_delete_query ⇒ Object
- #on_document_delete_trigger ⇒ Object
- #on_document_insert_procedure ⇒ Object
- #on_document_insert_query ⇒ Object
- #on_document_insert_trigger ⇒ Object
- #on_document_truncate_procedure ⇒ Object
- #on_document_truncate_query ⇒ Object
- #on_document_truncate_trigger ⇒ Object
- #on_document_update_procedure ⇒ Object
- #on_document_update_query ⇒ Object
- #on_document_update_trigger ⇒ Object
- #on_source_delete_procedure ⇒ Object
- #on_source_delete_query ⇒ Object
- #on_source_delete_trigger ⇒ Object
- #on_source_insert_procedure ⇒ Object
- #on_source_insert_query ⇒ Object
- #on_source_insert_trigger ⇒ Object
- #on_source_truncate_procedure ⇒ Object
- #on_source_truncate_query ⇒ Object
- #on_source_truncate_trigger ⇒ Object
- #on_source_update_procedure ⇒ Object
- #on_source_update_query ⇒ Object
- #on_source_update_trigger ⇒ Object
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
#catalog ⇒ Object (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 |
#document ⇒ Object (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 |
#fields ⇒ Object (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_key ⇒ Object (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 |
#key ⇒ Object (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 |
#name ⇒ Object (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 |
#table ⇒ Object (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_query ⇒ Object
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 <<-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}"."#{foreign_key}" = "#{source}"."#{key}"; SQL end |
#on_document_delete_procedure ⇒ Object
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 <<-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_query ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') DELETE FROM "#{PG::FTS.table}" WHERE "#{PG::FTS.table}"."source_id" = OLD."#{foreign_key}" AND "#{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_trigger ⇒ Object
308 309 310 311 312 313 314 315 |
# File 'lib/pg/fts/index/many_to_one.rb', line 308 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_procedure ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') CREATE FUNCTION "#{on_document_insert_procedure_name}"() RETURNS TRIGGER AS $$ BEGIN IF NEW."#{foreign_key}" IS NULL THEN RETURN NEW; END IF; #{on_document_insert_query.gsub(/^/, ' ')} RETURN NEW; END; $$ LANGUAGE plpgsql; SQL end |
#on_document_insert_query ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') INSERT INTO "#{PG::FTS.table}" ( "document_id", "document_table", "source_id", "source_table", "tsv") SELECT NEW."#{key}", '#{document}', NEW."#{foreign_key}", '#{source}', #{ts_vector(source)} FROM "#{source}" WHERE "#{source}"."#{key}" = NEW."#{foreign_key}"; SQL end |
#on_document_insert_trigger ⇒ Object
290 291 292 293 294 295 296 297 |
# File 'lib/pg/fts/index/many_to_one.rb', line 290 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_procedure ⇒ Object
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 <<-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_query ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/pg/fts/index/many_to_one.rb', line 270 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_trigger ⇒ Object
317 318 319 320 321 322 323 324 |
# File 'lib/pg/fts/index/many_to_one.rb', line 317 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_procedure ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') CREATE FUNCTION "#{on_document_update_procedure_name}"() RETURNS TRIGGER AS $$ BEGIN #{on_document_update_query.gsub(/^/, ' ')} IF NOT FOUND THEN #{on_document_insert_query.gsub(/^/, ' ')} IF NOT FOUND THEN #{on_document_delete_query.gsub(/^/, ' ')} END IF; RETURN NEW; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; SQL end |
#on_document_update_query ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') UPDATE "#{PG::FTS.table}" SET "tsv" = (#{ts_vector(source)}), "source_table" = '#{source}', "source_id" = NEW."#{foreign_key}" FROM "#{source}" WHERE "#{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."#{key}" AND "#{PG::FTS.table}"."document_table" = '#{document}'; SQL end |
#on_document_update_trigger ⇒ Object
299 300 301 302 303 304 305 306 |
# File 'lib/pg/fts/index/many_to_one.rb', line 299 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 |
#on_source_delete_procedure ⇒ Object
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 <<-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_query ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/pg/fts/index/many_to_one.rb', line 102 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_trigger ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/pg/fts/index/many_to_one.rb', line 161 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_procedure ⇒ Object
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 <<-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_query ⇒ Object
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 <<-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}" WHERE "#{document}"."#{foreign_key}" = NEW."#{key}"; SQL end |
#on_source_insert_trigger ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/pg/fts/index/many_to_one.rb', line 143 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_procedure ⇒ Object
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 <<-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_query ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/pg/fts/index/many_to_one.rb', line 123 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_trigger ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/pg/fts/index/many_to_one.rb', line 170 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_procedure ⇒ Object
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 <<-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_query ⇒ Object
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 <<-SQL.gsub(/^ {4}/, '') UPDATE "#{PG::FTS.table}" SET "tsv" = (#{ts_vector}), "document_table" = '#{document}', "document_id" = "#{document}"."#{key}" FROM "#{document}" WHERE "#{document}"."#{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_trigger ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/pg/fts/index/many_to_one.rb', line 152 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 |