Skip to content

Commit

Permalink
Merge pull request #2021 from dasch/more_postgres_utils
Browse files Browse the repository at this point in the history
Make #extract_schema_and_table an instance method in Utils
  • Loading branch information
tenderlove committed Aug 29, 2011
2 parents c59c9bb + a982443 commit cebff6d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Expand Up @@ -954,6 +954,8 @@ def distinct(columns, orders) #:nodoc:
end

module Utils
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+)
Expand All @@ -964,7 +966,7 @@ module Utils
# * <tt>schema_name.table_name</tt>
# * <tt>schema_name."table.name"</tt>
# * <tt>"schema.name"."table name"</tt>
def self.extract_schema_and_table(name)
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end
Expand Down
15 changes: 0 additions & 15 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -219,21 +219,6 @@ def test_pk_and_sequence_for_with_schema_specified
end
end

def test_extract_schema_and_table
{
%(table_name) => [nil,'table_name'],
%("table.name") => [nil,'table.name'],
%(schema.table_name) => %w{schema table_name},
%("schema".table_name) => %w{schema table_name},
%(schema."table_name") => %w{schema table_name},
%("schema"."table_name") => %w{schema table_name},
%("even spaces".table) => ['even spaces','table'],
%(schema."table.name") => ['schema', 'table.name']
}.each do |given, expect|
assert_equal expect, ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils.extract_schema_and_table(given)
end
end

def test_current_schema
{
%('$user',public) => 'public',
Expand Down
18 changes: 18 additions & 0 deletions activerecord/test/cases/adapters/postgresql/utils_test.rb
@@ -0,0 +1,18 @@
class PostgreSQLUtilsTest < ActiveSupport::TestCase
include ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils

def test_extract_schema_and_table
{
%(table_name) => [nil,'table_name'],
%("table.name") => [nil,'table.name'],
%(schema.table_name) => %w{schema table_name},
%("schema".table_name) => %w{schema table_name},
%(schema."table_name") => %w{schema table_name},
%("schema"."table_name") => %w{schema table_name},
%("even spaces".table) => ['even spaces','table'],
%(schema."table.name") => ['schema', 'table.name']
}.each do |given, expect|
assert_equal expect, extract_schema_and_table(given)
end
end
end

0 comments on commit cebff6d

Please sign in to comment.