Sha256: e1d5eb23f5593098a666237016a995b2b48a1dcd4b147e1bc97a8f224c68963d

Contents?: true

Size: 947 Bytes

Versions: 7

Compression:

Stored size: 947 Bytes

Contents

require 'pg_query'

##
# Reads directives from SQL-comments
#
#    --!depends_on [name_of_dependency]
#
# name_of_dependency: short or full name of object as well as object_name
#
class PgObjects::Parser
  include Import['parsed_object_factory']
  include Memery

  PG_ENTITIES = %i[operator_class trigger define_statement conversion event_trigger type function table].freeze

  def load(source)
    @source = source
    self
  end

  def fetch_directives
    {
      depends_on: fetch_dependencies
    }
  end

  def fetch_object_name
    parse_query
    parsed_object.name
  rescue PgQuery::ParseError, NoMethodError
    nil
  end

  private

  attr_reader :parsed

  def parse_query
    @parsed = PgQuery.parse(@source)
  end

  memoize
  def parsed_object
    parsed_object_factory.create_object(parsed)
  end

  def fetch_dependencies
    @source.split("\n").grep(/^(--|#)!/).map { |ln| ln.split[1] if ln =~ /!depends_on/ }.compact
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_objects-1.4.0 lib/pg_objects/parser.rb
pg_objects-1.3.3 lib/pg_objects/parser.rb
pg_objects-1.3.2 lib/pg_objects/parser.rb
pg_objects-1.3.1 lib/pg_objects/parser.rb
pg_objects-1.3.0 lib/pg_objects/parser.rb
pg_objects-1.2.2 lib/pg_objects/parser.rb
pg_objects-1.2.1 lib/pg_objects/parser.rb