Sha256: c339d0978dafecf5d17b7d09423973d0becc73bd021198261cadc681bb1c3f08

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      # Value Object to hold a schema qualified name.
      # This is usually the name of a PostgreSQL relation but it can also represent
      # schema qualified type names. +schema+ and +identifier+ are unquoted to prevent
      # double quoting.
      class Name # :nodoc:
        SEPARATOR = "."
        attr_reader :schema, :identifier

        def initialize(schema, identifier)
          @schema, @identifier = unquote(schema), unquote(identifier)
        end

        def to_s
          parts.join SEPARATOR
        end

        def quoted
          parts.map { |p| PGconn.quote_ident(p) }.join SEPARATOR
        end

        def ==(o)
          o.class == self.class && o.parts == parts
        end
        alias_method :eql?, :==

        def hash
          parts.hash
        end

        protected
          def unquote(part)
            return unless part
            part.gsub(/(^"|"$)/,'')
          end

          def parts
            @parts ||= [@schema, @identifier].compact
          end
      end

      module Utils # :nodoc:
        extend self

        # Returns an instance of <tt>ActiveRecord::ConnectionAdapters::PostgreSQL::Name</tt>
        # extracted from +string+.
        # +schema+ is nil if not specified in +string+.
        # +schema+ and +identifier+ exclude surrounding quotes (regardless of whether provided in +string+)
        # +string+ supports the range of schema/table references understood by PostgreSQL, for example:
        #
        # * <tt>table_name</tt>
        # * <tt>"table.name"</tt>
        # * <tt>schema_name.table_name</tt>
        # * <tt>schema_name."table.name"</tt>
        # * <tt>"schema_name".table_name</tt>
        # * <tt>"schema.name"."table name"</tt>
        def extract_schema_qualified_name(string)
          table, schema = string.scan(/[^".\s]+|"[^"]*"/)[0..1].reverse
          PostgreSQL::Name.new(schema, table)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord-4.2.0.beta4 lib/active_record/connection_adapters/postgresql/utils.rb
activerecord-4.2.0.beta3 lib/active_record/connection_adapters/postgresql/utils.rb
activerecord-4.2.0.beta2 lib/active_record/connection_adapters/postgresql/utils.rb
activerecord-4.2.0.beta1 lib/active_record/connection_adapters/postgresql/utils.rb