Permalink
Browse files

Add Dataset#split_qualifiers helper method for splitting a qualifier …

…identifier into array of strings
  • Loading branch information...
1 parent 988f426 commit eedda9fb79c1e4e303cb541b1b6cfa30644b4bee @jeremyevans committed Dec 13, 2012
Showing with 66 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +5 −0 lib/sequel/database/schema_methods.rb
  3. +17 −2 lib/sequel/dataset/sql.rb
  4. +42 −0 spec/core/dataset_spec.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Add Dataset#split_qualifiers helper method for splitting a qualifier identifier into array of strings (jeremyevans)
+
* 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)
@@ -715,6 +715,11 @@ def schema_utility_dataset
@schema_utility_dataset ||= dataset
end
+ # Split the schema information from the table
+ def split_qualifiers(table_name)
+ schema_utility_dataset.split_qualifiers(table_name)
+ end
+
# Whether the database supports combining multiple alter table
# operations into a single query, false by default.
def supports_combining_alter_table_ops?
View
@@ -643,8 +643,7 @@ def quoted_identifier_append(sql, name)
# 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
+ def schema_and_table(table_name, sch=(db.default_schema if db))
sch = sch.to_s if sch
case table_name
when Symbol
@@ -661,6 +660,22 @@ def schema_and_table(table_name)
end
end
+ # Splits table_name into an array of strings.
+ #
+ # ds.split_qualifiers(:s) # ['s']
+ # ds.split_qualifiers(:t__s) # ['t', 's']
+ # ds.split_qualifiers(Sequel.qualify(:d, :t__s)) # ['d', 't', 's']
+ # ds.split_qualifiers(Sequel.qualify(:h__d, :t__s)) # ['h', 'd', 't', 's']
+ def split_qualifiers(table_name, *args)
+ case table_name
+ when SQL::QualifiedIdentifier
+ split_qualifiers(table_name.table, nil) + split_qualifiers(table_name.column, nil)
+ else
+ sch, table = schema_and_table(table_name, *args)
+ sch ? [sch, table] : [table]
+ end
+ end
+
# SQL fragment for specifying subscripts (SQL array accesses)
def subscript_sql_append(sql, s)
literal_append(sql, s.f)
View
@@ -4476,6 +4476,48 @@ class << Sequel
it "should respect default_schema" do
@ds.db.default_schema = :foo
@ds.schema_and_table(:s).should == ['foo', 's']
+ @ds.schema_and_table(:s, nil).should == [nil, 's']
+ end
+end
+
+describe "Dataset#split_qualifiers" do
+ before do
+ @ds = Sequel.mock[:test]
+ end
+
+ it "should correctly handle symbols" do
+ @ds.split_qualifiers(:s).should == ['s']
+ @ds.split_qualifiers(:s___a).should == ['s']
+ @ds.split_qualifiers(:t__s).should == ['t', 's']
+ @ds.split_qualifiers(:t__s___a).should == ['t', 's']
+ end
+
+ it "should correctly handle strings" do
+ @ds.split_qualifiers('s').should == ['s']
+ end
+
+ it "should correctly handle identifiers" do
+ @ds.split_qualifiers(Sequel.identifier(:s)).should == ['s']
+ end
+
+ it "should correctly handle simple qualified identifiers" do
+ @ds.split_qualifiers(Sequel.qualify(:t, :s)).should == ['t', 's']
+ end
+
+ it "should correctly handle complex qualified identifiers" do
+ @ds.split_qualifiers(Sequel.qualify(:d__t, :s)).should == ['d', 't', 's']
+ @ds.split_qualifiers(Sequel.qualify(Sequel.qualify(:d, :t), :s)).should == ['d', 't', 's']
+ @ds.split_qualifiers(Sequel.qualify(:d, :t__s)).should == ['d', 't', 's']
+ @ds.split_qualifiers(Sequel.qualify(:d, Sequel.qualify(:t, :s))).should == ['d', 't', 's']
+ @ds.split_qualifiers(Sequel.qualify(:d__t, :s__s2)).should == ['d', 't', 's', 's2']
+ @ds.split_qualifiers(Sequel.qualify(Sequel.qualify(:d, :t), Sequel.qualify(:s, :s2))).should == ['d', 't', 's', 's2']
+ end
+
+ it "should respect default_schema" do
+ @ds.db.default_schema = :foo
+ @ds.split_qualifiers(:s).should == ['foo', 's']
+ @ds.split_qualifiers(:s, nil).should == ['s']
+ @ds.split_qualifiers(Sequel.qualify(:d__t, :s)).should == ['d', 't', 's']
end
end

0 comments on commit eedda9f

Please sign in to comment.