Skip to content
Browse files

Make Database#schema_and_table always return strings for the schema a…

…nd table

Before, it sometimes returned a string, and sometimes returned a symbol.

While here, update the documentation to reflect that this method
does not handle more than one level of qualification.
  • Loading branch information...
1 parent 0057793 commit 988f426416eb720c38b943377d14402a97313245 @jeremyevans committed
Showing with 44 additions and 4 deletions.
  1. +2 −0 CHANGELOG
  2. +11 −4 lib/sequel/dataset/sql.rb
  3. +31 −0 spec/core/dataset_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Make Database#schema_and_table always return strings for the schema and table (jeremyevans)
+
* Skip stripping of blob columns in the string_stripper plugin (jeremyevans) (#593)
* Allow Database#get to take an array to return multiple values, similar to map/select_map (jeremyevans)
View
15 lib/sequel/dataset/sql.rb
@@ -636,19 +636,26 @@ def quoted_identifier_append(sql, name)
sql << QUOTE << name.to_s.gsub(QUOTE_RE, DOUBLE_QUOTE) << QUOTE
end
- # Split the schema information from the table
+ # Split the schema information from the table, returning two strings,
+ # one for the schema and one for the table. The returned schema may
+ # be nil, but the table will always have a string value.
+ #
+ # Note that this function does not handle tables with more than one
+ # level of qualification (e.g. database.schema.table on Microsoft
+ # SQL Server).
def schema_and_table(table_name)
sch = db.default_schema if db
+ sch = sch.to_s if sch
case table_name
when Symbol
s, t, a = split_symbol(table_name)
[s||sch, t]
when SQL::QualifiedIdentifier
- [table_name.table, table_name.column]
+ [table_name.table.to_s, table_name.column.to_s]
when SQL::Identifier
- [sch, table_name.value]
+ [sch, table_name.value.to_s]
when String
- [sch, table_name]
+ [sch, table_name.to_s]
else
raise Error, 'table_name should be a Symbol, SQL::QualifiedIdentifier, SQL::Identifier, or String'
end
View
31 spec/core/dataset_spec.rb
@@ -4448,3 +4448,34 @@ class << Sequel
proc{@ds.extension(:foo2)}.should raise_error(Sequel::Error)
end
end
+
+describe "Dataset#schema_and_table" do
+ before do
+ @ds = Sequel.mock[:test]
+ end
+
+ it "should correctly handle symbols" do
+ @ds.schema_and_table(:s).should == [nil, 's']
+ @ds.schema_and_table(:s___a).should == [nil, 's']
+ @ds.schema_and_table(:t__s).should == ['t', 's']
+ @ds.schema_and_table(:t__s___a).should == ['t', 's']
+ end
+
+ it "should correctly handle strings" do
+ @ds.schema_and_table('s').should == [nil, 's']
+ end
+
+ it "should correctly handle identifiers" do
+ @ds.schema_and_table(Sequel.identifier(:s)).should == [nil, 's']
+ end
+
+ it "should correctly handle qualified identifiers" do
+ @ds.schema_and_table(Sequel.qualify(:t, :s)).should == ['t', 's']
+ end
+
+ it "should respect default_schema" do
+ @ds.db.default_schema = :foo
+ @ds.schema_and_table(:s).should == ['foo', 's']
+ end
+end
+

0 comments on commit 988f426

Please sign in to comment.
Something went wrong with that request. Please try again.