aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-05-30 10:53:24 +0200
committerYves Senn <yves.senn@gmail.com>2014-05-30 10:53:24 +0200
commit7b8d95d58fbadf71e1f27a848427de27e0a2742a (patch)
treec9f644d0e87e4de43cfc93550b4cf18409bf812f /activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
parent6963e33829c11f7036b8c8015e2c5222a469c118 (diff)
downloadrails-7b8d95d58fbadf71e1f27a848427de27e0a2742a.tar.gz
rails-7b8d95d58fbadf71e1f27a848427de27e0a2742a.tar.bz2
rails-7b8d95d58fbadf71e1f27a848427de27e0a2742a.zip
pg, `PostgreSQL::Name` to hold schema qualified names.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/utils.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/utils.rb55
1 files changed, 48 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb b/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
index 60ffd3a114..0290bcb48c 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
@@ -1,13 +1,54 @@
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 array of <tt>[schema_name, table_name]</tt> extracted from +name+.
- # +schema_name+ is nil if not specified in +name+.
- # +schema_name+ and +table_name+ exclude surrounding quotes (regardless of whether provided in +name+)
- # +name+ supports the range of schema/table references understood by PostgreSQL, for example:
+ # 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>
@@ -15,9 +56,9 @@ module ActiveRecord
# * <tt>schema_name."table.name"</tt>
# * <tt>"schema_name".table_name</tt>
# * <tt>"schema.name"."table name"</tt>
- def extract_schema_and_table(name)
- table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
- [schema, table]
+ def extract_schema_qualified_name(string)
+ table, schema = string.scan(/[^".\s]+|"[^"]*"/)[0..1].reverse
+ PostgreSQL::Name.new(schema, table)
end
end
end