Skip to content

Commit cf4466b

Browse files
committed
Add Model.qualified_primary_key for returning a qualified version of the model's primary key
1 parent 8ac4d10 commit cf4466b

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== master
22

3+
* Add Model.qualified_primary_key for returning a qualified version of the model's primary key (jeremyevans)
4+
35
* Support sql_literal_allow_caching? method on objects that respond to sql_literal_append/sql_literal (jeremyevans)
46

57
* Support set_column_not_null as a reversible migration method (jeremyevans)

lib/sequel/model/base.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,28 @@ def primary_key_hash(value)
533533
end
534534
end
535535

536+
# Return a qualified identifier or array of qualified identifiers for
537+
# the model's primary key. Uses the given qualifier if provided, or
538+
# the table_name otherwise. If the model does not have a primary key,
539+
# raises an +Error+.
540+
#
541+
# Artist.order(Artist.qualified_primary_key)
542+
# # SELECT * FROM artists ORDER BY artists.id
543+
def qualified_primary_key(qualifier=table_name)
544+
case key = @primary_key
545+
when Symbol
546+
SQL::QualifiedIdentifier.new(qualifier, key)
547+
when Array
548+
key.map{|k| SQL::QualifiedIdentifier.new(qualifier, k)}
549+
else
550+
raise(Error, "#{self} does not have a primary key")
551+
end
552+
end
553+
536554
# Return a hash where the keys are qualified column references. Uses the given
537555
# qualifier if provided, or the table_name otherwise. This is useful if you
538556
# plan to join other tables to this table and you want the column references
539-
# to be qualified.
557+
# to be qualified. If the model does not have a primary key, raises an +Error+.
540558
#
541559
# Artist.where(Artist.qualified_primary_key_hash(1))
542560
# # SELECT * FROM artists WHERE (artists.id = 1)

spec/model/base_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,32 @@ class ::Leopard < Feline; end
548548
end
549549
end
550550

551+
describe "Model.qualified_primary_key" do
552+
before do
553+
@c = Class.new(Sequel::Model(:items))
554+
end
555+
556+
it "should handle a single primary key" do
557+
@c.qualified_primary_key.must_equal(Sequel.qualify(:items, :id))
558+
end
559+
560+
it "should handle a composite primary key" do
561+
@c.set_primary_key([:id1, :id2])
562+
@c.qualified_primary_key.must_equal([Sequel.qualify(:items, :id1), Sequel.qualify(:items, :id2)])
563+
end
564+
565+
it "should raise an error for no primary key" do
566+
@c.no_primary_key
567+
proc{@c.qualified_primary_key}.must_raise(Sequel::Error)
568+
end
569+
570+
it "should allow specifying a different qualifier" do
571+
@c.qualified_primary_key(:apple).must_equal(Sequel.qualify(:apple, :id))
572+
@c.set_primary_key([:id1, :id2])
573+
@c.qualified_primary_key(:bear).must_equal([Sequel.qualify(:bear, :id1), Sequel.qualify(:bear, :id2)])
574+
end
575+
end
576+
551577
describe "Model.db" do
552578
before do
553579
@db = Sequel.mock

0 commit comments

Comments
 (0)