Class: PG::FTS::Index::Self

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#catalogObject (readonly)

Returns the value of attribute catalog.



2
3
4
# File 'lib/pg/fts/index/self.rb', line 2

def catalog
  @catalog
end

#fieldsObject (readonly)

Returns the value of attribute fields.



2
3
4
# File 'lib/pg/fts/index/self.rb', line 2

def fields
  @fields
end

#keyObject (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

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/pg/fts/index/self.rb', line 2

def name
  @name
end

#tableObject (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_queryObject



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
  "  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}\";\n  SQL\nend\n".gsub(/^ {4}/, '')

#on_document_delete_procedureObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/pg/fts/index/self.rb', line 95

def on_document_delete_procedure
  "  CREATE FUNCTION \"\#{on_document_delete_procedure_name}\"() 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



85
86
87
88
89
90
91
92
93
# File 'lib/pg/fts/index/self.rb', line 85

def on_document_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_id\" = OLD.\"\#{key}\"\n    AND \"\#{PG::FTS.table}\".\"document_table\" = '\#{document}';\n  SQL\nend\n".gsub(/^ {4}/, '')

#on_document_delete_triggerObject



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

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/pg/fts/index/self.rb', line 53

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

#on_document_insert_queryObject



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
  "  INSERT INTO \"\#{PG::FTS.table}\" (\n    \"document_id\",\n    \"document_table\",\n    \"source_id\",\n    \"source_table\",\n    \"tsv\")\n  VALUES (\n    NEW.\"\#{key}\",\n    '\#{document}',\n    NEW.\"\#{key}\",\n    '\#{source}',\n    \#{ts_vector});\n  SQL\nend\n".gsub(/^ {4}/, '')

#on_document_insert_triggerObject



125
126
127
128
129
130
131
132
# File 'lib/pg/fts/index/self.rb', line 125

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



114
115
116
117
118
119
120
121
122
123
# File 'lib/pg/fts/index/self.rb', line 114

def on_document_truncate_procedure
  "  CREATE FUNCTION \"\#{on_document_truncate_procedure_name}\"() 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



106
107
108
109
110
111
112
# File 'lib/pg/fts/index/self.rb', line 106

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



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

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/pg/fts/index/self.rb', line 74

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

#on_document_update_queryObject



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

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

#on_document_update_triggerObject



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

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}/, '')