PostgreSQLAdapter::Utils seems to be only used from a single spot - quite redundant
This commit is contained in:
parent
ad0ec07f00
commit
c0bfc3f412
@ -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 = PostgreSQLAdapter::Utils.extract_schema_and_table(name.to_s)
|
||||
schema, table = extract_schema_and_table(name.to_s)
|
||||
return false unless table
|
||||
|
||||
exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
|
||||
@ -488,6 +488,24 @@ 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
|
||||
|
@ -502,25 +502,6 @@ def session_auth=(user)
|
||||
exec_query "SET SESSION AUTHORIZATION #{user}"
|
||||
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+)
|
||||
# +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
|
||||
|
||||
def use_insert_returning?
|
||||
@use_insert_returning
|
||||
end
|
||||
|
@ -352,6 +352,21 @@ 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|
|
||||
|
@ -1,20 +0,0 @@
|
||||
require 'cases/helper'
|
||||
|
||||
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
|
Loading…
Reference in New Issue
Block a user