Class: PG::FTS::Index::Self
- Inherits:
-
Object
- Object
- PG::FTS::Index::Self
- Includes:
- PG::FTS::Index, Naming, TSVector
- Defined in:
- lib/pg/fts/index/self.rb
Instance Attribute Summary collapse
-
#catalog ⇒ Object
readonly
Returns the value of attribute catalog.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#key ⇒ Object
(also: #foreign_key)
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
(also: #source, #document)
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #build_query ⇒ Object
-
#initialize(table, *fields, key: nil, catalog: nil, as: nil) ⇒ Self
constructor
A new instance of Self.
- #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
Constructor Details
#initialize(table, *fields, key: nil, catalog: nil, as: nil) ⇒ Self
Returns a new instance of Self.
11 12 13 14 15 16 17 |
# File 'lib/pg/fts/index/self.rb', line 11 def initialize(table, *fields, key: nil, catalog: nil, as: nil) @table = table @fields = fields @key = key || :id @catalog = catalog || PG::FTS.catalog @name = as end |
Instance Attribute Details
#catalog ⇒ Object (readonly)
Returns the value of attribute catalog.
2 3 4 |
# File 'lib/pg/fts/index/self.rb', line 2 def catalog @catalog end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
2 3 4 |
# File 'lib/pg/fts/index/self.rb', line 2 def fields @fields end |
#key ⇒ Object (readonly) Also known as: foreign_key
Returns the value of attribute key.
2 3 4 |
# File 'lib/pg/fts/index/self.rb', line 2 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/pg/fts/index/self.rb', line 2 def name @name end |
#table ⇒ Object (readonly) Also known as: source, document
Returns the value of attribute table.
2 3 4 |
# File 'lib/pg/fts/index/self.rb', line 2 def table @table end |
Instance Method Details
#build_query ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pg/fts/index/self.rb', line 19 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}"; SQL end |
#on_document_delete_procedure ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/pg/fts/index/self.rb', line 95 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
85 86 87 88 89 90 91 92 93 |
# File 'lib/pg/fts/index/self.rb', line 85 def on_document_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."#{key}" AND "#{PG::FTS.table}"."document_table" = '#{document}'; SQL end |
#on_document_delete_trigger ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/pg/fts/index/self.rb', line 143 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pg/fts/index/self.rb', line 53 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pg/fts/index/self.rb', line 36 def on_document_insert_query <<-SQL.gsub(/^ {4}/, '') INSERT INTO "#{PG::FTS.table}" ( "document_id", "document_table", "source_id", "source_table", "tsv") VALUES ( NEW."#{key}", '#{document}', NEW."#{key}", '#{source}', #{ts_vector}); SQL end |
#on_document_insert_trigger ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/pg/fts/index/self.rb', line 125 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
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pg/fts/index/self.rb', line 114 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
106 107 108 109 110 111 112 |
# File 'lib/pg/fts/index/self.rb', line 106 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
152 153 154 155 156 157 158 159 |
# File 'lib/pg/fts/index/self.rb', line 152 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
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pg/fts/index/self.rb', line 74 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
64 65 66 67 68 69 70 71 72 |
# File 'lib/pg/fts/index/self.rb', line 64 def on_document_update_query <<-SQL.gsub(/^ {4}/, '') UPDATE "#{PG::FTS.table}" SET "tsv" = (#{ts_vector}) WHERE "#{PG::FTS.table}"."source_id" = NEW."#{key}" AND "#{PG::FTS.table}"."source_table" = '#{source}' AND "#{PG::FTS.table}"."document_id" = NEW."#{key}" AND "#{PG::FTS.table}"."document_table" = '#{document}'; SQL end |
#on_document_update_trigger ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/pg/fts/index/self.rb', line 134 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 |