-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pg, re-introduce
PostgreSQL::Utils
to unify schema/table extraction.
Partial revert of c0bfc3f
- Loading branch information
Showing
6 changed files
with
51 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |