Skip to content

Commit

Permalink
pg, re-introduce PostgreSQL::Utils to unify schema/table extraction.
Browse files Browse the repository at this point in the history
Partial revert of c0bfc3f
  • Loading branch information
senny committed May 19, 2014
1 parent 881cab4 commit d4cec31
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@ def quote_string(s) #:nodoc:
# - "schema.name".table_name
# - "schema.name"."table.name"
def quote_table_name(name)
schema, name_part = extract_pg_identifier_from_name(name.to_s)

unless name_part
quote_column_name(schema)
schema, table = Utils.extract_schema_and_table(name.to_s)
if schema
"#{quote_column_name(schema)}.#{quote_column_name(table)}"
else
table_name, name_part = extract_pg_identifier_from_name(name_part)
"#{quote_column_name(schema)}.#{quote_column_name(table_name)}"
quote_column_name(table)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def tables(name = nil)
# If the schema is not specified as part of +name+ then it will only find tables within
# the current schema search path (regardless of permissions to access tables in other schemas)
def table_exists?(name)
schema, table = extract_schema_and_table(name.to_s)
schema, table = Utils.extract_schema_and_table(name.to_s)
return false unless table

exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
Expand Down Expand Up @@ -488,23 +488,6 @@ def columns_for_distinct(columns, orders) #:nodoc:

[super, *order_columns].join(', ')
end

private

# 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:
#
# * <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>
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
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:
#
# * <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_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/statement_pool'

require 'active_record/connection_adapters/postgresql/utils'
require 'active_record/connection_adapters/postgresql/column'
require 'active_record/connection_adapters/postgresql/oid'
require 'active_record/connection_adapters/postgresql/quoting'
Expand Down
15 changes: 0 additions & 15 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,21 +352,6 @@ def test_schema_exists?
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, @connection.send(:extract_schema_and_table, given)
end
end

private
def columns(table_name)
@connection.send(:column_definitions, table_name).map do |name, type, default|
Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/adapters/postgresql/utils_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'cases/helper'

class PostgreSQLUtilsTest < ActiveSupport::TestCase
include ActiveRecord::ConnectionAdapters::PostgreSQL::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 d4cec31

Please sign in to comment.