Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Model.set_dataset now accepts Identifier, QualifiedIdentifier, and Al…

…iasedExpression arguments
  • Loading branch information...
commit 84e3c255c2b3694adcbece6cc05e1945bdbc8ccf 1 parent 2e15e94
@jeremyevans authored
Showing with 64 additions and 2 deletions.
  1. +2 −0  CHANGELOG
  2. +2 −2 lib/sequel/model/base.rb
  3. +60 −0 spec/model/model_spec.rb
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Model.set_dataset now accepts Identifier, QualifiedIdentifier, and AliasedExpression arguments (jeremyevans)
+
* Fix handling of nil values in bound variables and prepared statement and stored procedure arguments in the jdbc adapter (jeremyevans, wei)
* Allow treating Datasets as Expressions, e.g. DB[:table1].select(:column1) > DB[:table2].select(:column2) (jeremyevans)
View
4 lib/sequel/model/base.rb
@@ -419,7 +419,7 @@ def set_allowed_columns(*cols)
def set_dataset(ds, opts={})
inherited = opts[:inherited]
@dataset = case ds
- when Symbol
+ when Symbol, SQL::Identifier, SQL::QualifiedIdentifier, SQL::AliasedExpression
@simple_table = db.literal(ds)
db[ds]
when Dataset
@@ -427,7 +427,7 @@ def set_dataset(ds, opts={})
@db = ds.db
ds
else
- raise(Error, "Model.set_dataset takes a Symbol or a Sequel::Dataset")
+ raise(Error, "Model.set_dataset takes one of the following classes as an argument: Symbol, SQL::Identifier, SQL::QualifiedIdentifier, SQL::AliasedExpression, Dataset")
end
@dataset.row_proc = Proc.new{|r| load(r)}
@require_modification = Sequel::Model.require_modification.nil? ? @dataset.provides_accurate_rows_matched? : Sequel::Model.require_modification
View
60 spec/model/model_spec.rb
@@ -19,6 +19,27 @@
c.table_name.should == :blah
end
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::Identifier" do
+ c = Sequel::Model(:blah.identifier)
+ c.superclass.should == Sequel::Model
+ c.db.should == @db
+ c.table_name.should == :blah.identifier
+ end
+
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::QualifiedIdentifier" do
+ c = Sequel::Model(:blah.qualify(:boo))
+ c.superclass.should == Sequel::Model
+ c.db.should == @db
+ c.table_name.should == :blah.qualify(:boo)
+ end
+
+ it "should return a model subclass with a dataset with the default database and given table name if given an SQL::AliasedExpression" do
+ c = Sequel::Model(:blah.as(:boo))
+ c.superclass.should == Sequel::Model
+ c.db.should == @db
+ c.table_name.should == :boo
+ end
+
it "should return a model subclass associated to the given database if given a database" do
db = Sequel::Database.new
c = Sequel::Model(db)
@@ -43,6 +64,27 @@ class ::Album < Sequel::Model(:table); end
end.should_not raise_error
end
+ it "should work without raising an exception with an SQL::Identifier " do
+ proc do
+ class ::Album < Sequel::Model(:table.identifier); end
+ class ::Album < Sequel::Model(:table.identifier); end
+ end.should_not raise_error
+ end
+
+ it "should work without raising an exception with an SQL::QualifiedIdentifier " do
+ proc do
+ class ::Album < Sequel::Model(:table.qualify(:schema)); end
+ class ::Album < Sequel::Model(:table.qualify(:schema)); end
+ end.should_not raise_error
+ end
+
+ it "should work without raising an exception with an SQL::AliasedExpression" do
+ proc do
+ class ::Album < Sequel::Model(:table.as(:alias)); end
+ class ::Album < Sequel::Model(:table.as(:alias)); end
+ end.should_not raise_error
+ end
+
it "should work without raising an exception with a database" do
proc do
class ::Album < Sequel::Model(@db); end
@@ -112,6 +154,24 @@ class ::Album < Sequel::Model(@db[:table]); end
@model.table_name.should == :foo
end
+ it "set_dataset should take an SQL::Identifier" do
+ @model.db = MODEL_DB
+ @model.set_dataset(:foo.identifier)
+ @model.table_name.should == :foo.identifier
+ end
+
+ it "set_dataset should take an SQL::QualifiedIdentifier" do
+ @model.db = MODEL_DB
+ @model.set_dataset(:foo.qualify(:bar))
+ @model.table_name.should == :foo.qualify(:bar)
+ end
+
+ it "set_dataset should take an SQL::AliasedExpression" do
+ @model.db = MODEL_DB
+ @model.set_dataset(:foo.as(:bar))
+ @model.table_name.should == :bar
+ end
+
it "table_name should respect table aliases" do
@model.set_dataset(:foo___x)
@model.table_name.should == :x
Please sign in to comment.
Something went wrong with that request. Please try again.