class PG::FTS::Index::MOMO

many documents, one link, many links, one source

Attributes

catalog[R]
document[R]
fields[R]
foreign_key[R]
key[R]
name[R]
source[R]
table[R]

Public Class Methods

new(table, *fields, document: nil, link: nil, key: nil, foreign_key: nil, forlink_key: nil, catalog: nil, as: nil) click to toggle source

rubocop:disable Metrics/ParameterLists, Metrics/CyclomaticComplexity

# File lib/pg/fts/index/momo.rb, line 14
def initialize(table, *fields,
               document: nil, link: nil,
               key: nil, foreign_key: nil, forlink_key: nil,
               catalog: nil,
               as: nil)
  @table = table
  @fields = fields
  @catalog = catalog || PG::FTS.catalog
  @document = document || fail(ArgumentError, ':document required')
  @link = link || fail(ArgumentError, ':link required')
  @key = key || :id
  @foreign_key = foreign_key || :"#{table}_id"
  @forlink_key = forlink_key || :"#{link}_id"
  @name = as
end

Public Instance Methods

build_query() click to toggle source

rubocop:enable Metrics/ParameterLists, Metrics/CyclomaticComplexity

# File lib/pg/fts/index/momo.rb, line 31
  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}", "#{link}"
    WHERE "#{document}"."#{forlink_key}" = "#{link}"."#{key}"
      AND "#{link}"."#{foreign_key}" = "#{source}"."#{key}";
    SQL
  end
on_document_delete_procedure() click to toggle source
# File lib/pg/fts/index/momo.rb, line 285
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 273
  def on_document_delete_query
    <<-SQL.gsub(/^ {4}/, '')
    DELETE FROM "#{PG::FTS.table}"
          USING "#{link}"
    WHERE "#{link}"."#{key}" = OLD."#{forlink_key}"
      AND "#{PG::FTS.table}"."source_id" = "#{link}"."#{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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 335
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 208
  def on_document_insert_procedure
    <<-SQL.gsub(/^ {4}/, '')
    CREATE FUNCTION "#{on_document_insert_procedure_name}"()
    RETURNS TRIGGER AS $$
      BEGIN
        IF NEW."#{forlink_key}" IS NULL THEN
          RETURN NEW;
        END IF;
        #{on_document_insert_query.gsub(/^/, '    ')}
        RETURN NEW;
      END;
    $$ LANGUAGE plpgsql;
    SQL
  end
on_document_insert_query() click to toggle source
# File lib/pg/fts/index/momo.rb, line 188
  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}", "#{link}"
    WHERE "#{source}"."#{key}" = "#{link}"."#{foreign_key}"
      AND "#{link}"."#{key}" = NEW."#{forlink_key}";
    SQL
  end
on_document_insert_trigger() click to toggle source
# File lib/pg/fts/index/momo.rb, line 317
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 305
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 297
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 344
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 243
  def on_document_update_procedure
    <<-SQL.gsub(/^ {4}/, '')
    CREATE FUNCTION "#{on_document_update_procedure_name}"()
    RETURNS TRIGGER AS $$
      BEGIN
        IF NEW."#{forlink_key}" IS NULL AND OLD."#{forlink_key}" IS NULL THEN
          RETURN NEW;
        END IF;
        IF NEW."#{forlink_key}" IS NULL THEN
          #{on_document_delete_query.gsub(/^/, '      ')}
          RETURN NEW;
        END IF;
        IF OLD."#{forlink_key}" IS NULL THEN
          #{on_document_insert_query.gsub(/^/, '      ')}
          RETURN NEW;
        END IF;
        #{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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 223
  def on_document_update_query
    <<-SQL.gsub(/^ {4}/, '')
    UPDATE "#{PG::FTS.table}"
    SET
      "tsv" = (#{ts_vector(source)}),
      "source_table" = '#{source}',
      "source_id" = "#{source}"."#{key}"
    FROM "#{source}" AS "OLD_#{source}", "#{source}",
          "#{link}" AS "OLD_#{link}", "#{link}"
    WHERE "OLD_#{source}"."#{key}" = "OLD_#{link}"."#{foreign_key}"
      AND "OLD_#{link}"."#{key}" = OLD."#{forlink_key}"
      AND "#{source}"."#{key}" = "#{link}"."#{foreign_key}"
      AND "#{link}"."#{key}" = NEW."#{forlink_key}"
      AND "#{PG::FTS.table}"."source_id" = "OLD_#{link}"."#{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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 326
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 120
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 111
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 170
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 70
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 50
  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}", "#{link}"
    WHERE "#{document}"."#{forlink_key}" = "#{link}"."#{key}"
      AND "#{link}"."#{foreign_key}" = NEW."#{key}";
    SQL
  end
on_source_insert_trigger() click to toggle source
# File lib/pg/fts/index/momo.rb, line 152
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 140
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 132
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 179
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 99
  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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 82
  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}", "#{link}"
    WHERE "#{document}"."#{forlink_key}" = "#{link}"."#{key}"
      AND "#{link}"."#{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() click to toggle source
# File lib/pg/fts/index/momo.rb, line 161
  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