Class: PG::FTS::Index::OneToMany
- Inherits:
-
Object
- Object
- PG::FTS::Index::OneToMany
- Includes:
- PG::FTS::Index, Naming, TSVector
- Defined in:
- lib/pg/fts/index/one_to_many.rb
Overview
one document, many sources
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) ⇒ OneToMany
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) ⇒ OneToMany
rubocop:disable Metrics/ParameterLists
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pg/fts/index/one_to_many.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 || :"#{document}_id" @name = as end |
Instance Attribute Details
#catalog ⇒ Object (readonly)
Returns the value of attribute catalog.
3 4 5 |
# File 'lib/pg/fts/index/one_to_many.rb', line 3 def catalog @catalog end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
3 4 5 |
# File 'lib/pg/fts/index/one_to_many.rb', line 3 def document @document end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
3 4 5 |
# File 'lib/pg/fts/index/one_to_many.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/one_to_many.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/one_to_many.rb', line 3 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/pg/fts/index/one_to_many.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/one_to_many.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/one_to_many.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}"."#{key}" = "#{source}"."#{foreign_key}"; SQL end |
#on_document_delete_procedure ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/pg/fts/index/one_to_many.rb', line 247 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
238 239 240 241 242 243 244 245 |
# File 'lib/pg/fts/index/one_to_many.rb', line 238 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_trigger ⇒ Object
297 298 299 300 301 302 303 304 |
# File 'lib/pg/fts/index/one_to_many.rb', line 297 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
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/pg/fts/index/one_to_many.rb', line 209 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_query ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pg/fts/index/one_to_many.rb', line 190 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}" WHERE "#{source}"."#{foreign_key}" = NEW."#{key}"; SQL end |
#on_document_insert_trigger ⇒ Object
279 280 281 282 283 284 285 286 |
# File 'lib/pg/fts/index/one_to_many.rb', line 279 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
267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pg/fts/index/one_to_many.rb', line 267 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
259 260 261 262 263 264 265 |
# File 'lib/pg/fts/index/one_to_many.rb', line 259 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
306 307 308 309 310 311 312 313 |
# File 'lib/pg/fts/index/one_to_many.rb', line 306 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
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/pg/fts/index/one_to_many.rb', line 226 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_query ⇒ Object
221 222 223 224 |
# File 'lib/pg/fts/index/one_to_many.rb', line 221 def on_document_update_query <<-SQL.gsub(/^ {4}/, '') SQL end |
#on_document_update_trigger ⇒ Object
288 289 290 291 292 293 294 295 |
# File 'lib/pg/fts/index/one_to_many.rb', line 288 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
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pg/fts/index/one_to_many.rb', line 122 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
112 113 114 115 116 117 118 119 120 |
# File 'lib/pg/fts/index/one_to_many.rb', line 112 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_id" = OLD."#{foreign_key}" AND "#{PG::FTS.table}"."document_table" = '#{document}'; SQL end |
#on_source_delete_trigger ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/pg/fts/index/one_to_many.rb', line 172 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 73 74 75 |
# File 'lib/pg/fts/index/one_to_many.rb', line 62 def on_source_insert_procedure <<-SQL.gsub(/^ {4}/, '') CREATE FUNCTION "#{on_source_insert_procedure_name}"() RETURNS TRIGGER AS $$ BEGIN IF NEW."#{foreign_key}" IS NULL THEN RETURN NEW; END IF; #{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/one_to_many.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}"."#{key}" = NEW."#{foreign_key}"; SQL end |
#on_source_insert_trigger ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/pg/fts/index/one_to_many.rb', line 154 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
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/pg/fts/index/one_to_many.rb', line 142 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
134 135 136 137 138 139 140 |
# File 'lib/pg/fts/index/one_to_many.rb', line 134 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
181 182 183 184 185 186 187 188 |
# File 'lib/pg/fts/index/one_to_many.rb', line 181 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pg/fts/index/one_to_many.rb', line 93 def on_source_update_procedure <<-SQL.gsub(/^ {4}/, '') CREATE FUNCTION "#{on_source_update_procedure_name}"() RETURNS TRIGGER AS $$ BEGIN #{on_source_update_query.gsub(/^/, ' ')} IF NOT FOUND THEN #{on_source_insert_query.gsub(/^/, ' ')} IF NOT FOUND THEN #{on_source_delete_query.gsub(/^/, ' ')} END IF; RETURN NEW; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; SQL end |
#on_source_update_query ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pg/fts/index/one_to_many.rb', line 77 def on_source_update_query <<-SQL.gsub(/^ {4}/, '') UPDATE "#{PG::FTS.table}" SET "tsv" = (#{ts_vector}), "document_table" = '#{document}', "document_id" = NEW."#{foreign_key}" FROM "#{document}" WHERE "#{document}"."#{key}" = NEW."#{foreign_key}" AND "#{PG::FTS.table}"."source_id" = OLD."#{key}" AND "#{PG::FTS.table}"."source_table" = '#{source}' AND "#{PG::FTS.table}"."document_id" = OLD."#{foreign_key}" AND "#{PG::FTS.table}"."document_table" = '#{document}'; SQL end |
#on_source_update_trigger ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/pg/fts/index/one_to_many.rb', line 163 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 |