@@ -2314,6 +2314,26 @@ def ==(o) @h == o.h; end
23142314 end
23152315end
23162316
2317+ describe "Dataset#as_set" do
2318+ before do
2319+ @d = Sequel . mock ( :fetch => [ { :a => 1 , :b => 2 } , { :a => 3 , :b => 4 } , { :a => 5 , :b => 6 } ] ) [ :items ]
2320+ end
2321+
2322+ it "should return set of column elements if column symbol is given" do
2323+ @d . as_set ( :a ) . must_equal Set [ 1 , 3 , 5 ]
2324+ end
2325+
2326+ it "should support set with array elements if an array of column symbols is given" do
2327+ @d . as_set ( [ :a , :b ] ) . must_equal Set [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ]
2328+ end
2329+
2330+ it "should not call the row_proc" do
2331+ @d = @d . with_row_proc ( proc { |r | h = { } ; r . keys . each { |k | h [ k ] = r [ k ] * 2 } ; h } )
2332+ @d . as_set ( :a ) . must_equal Set [ 1 , 3 , 5 ]
2333+ @d . as_set ( [ :a , :b ] ) . must_equal Set [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ]
2334+ end
2335+ end
2336+
23172337describe "Dataset#distinct" do
23182338 before do
23192339 @db = Sequel . mock
@@ -5420,6 +5440,100 @@ def default_timestamp_format
54205440 end
54215441end
54225442
5443+ describe "Sequel::Dataset#select_set" do
5444+ before do
5445+ @ds = Sequel . mock ( :fetch => [ { :c => 1 } , { :c => 2 } ] ) [ :t ]
5446+ end
5447+
5448+ it "should do select and map in one step" do
5449+ @ds . select_set ( :a ) . must_equal Set [ 1 , 2 ]
5450+ @ds . db . sqls . must_equal [ 'SELECT a FROM t' ]
5451+ end
5452+
5453+ it "should handle qualified identifiers in arguments" do
5454+ @ds . select_set ( Sequel [ :a ] [ :b ] ) . must_equal Set [ 1 , 2 ]
5455+ @ds . db . sqls . must_equal [ 'SELECT a.b FROM t' ]
5456+ end
5457+
5458+ with_symbol_splitting "should handle implicit qualifiers in arguments" do
5459+ @ds . select_set ( :a__b ) . must_equal Set [ 1 , 2 ]
5460+ @ds . db . sqls . must_equal [ 'SELECT a.b FROM t' ]
5461+ end
5462+
5463+ it "should raise if multiple arguments and can't determine alias" do
5464+ proc { @ds . select_set ( [ Sequel . function ( :a ) , :b ] ) } . must_raise ( Sequel ::Error )
5465+ proc { @ds . select_set ( Sequel . function ( :a ) ) { b } } . must_raise ( Sequel ::Error )
5466+ proc { @ds . select_set { [ a . function , b ] } } . must_raise ( Sequel ::Error )
5467+ end
5468+
5469+ with_symbol_splitting "should handle implicit aliases in arguments" do
5470+ @ds . select_set ( :a___b ) . must_equal Set [ 1 , 2 ]
5471+ @ds . db . sqls . must_equal [ 'SELECT a AS b FROM t' ]
5472+ end
5473+
5474+ it "should handle aliased expressions in arguments" do
5475+ @ds . select_set ( Sequel [ :a ] . as ( :b ) ) . must_equal Set [ 1 , 2 ]
5476+ @ds . db . sqls . must_equal [ 'SELECT a AS b FROM t' ]
5477+ end
5478+
5479+ it "should handle other objects" do
5480+ @ds . select_set ( Sequel . lit ( "a" ) . as ( :b ) ) . must_equal Set [ 1 , 2 ]
5481+ @ds . db . sqls . must_equal [ 'SELECT a AS b FROM t' ]
5482+ end
5483+
5484+ it "should handle identifiers with strings" do
5485+ @ds . select_set ( [ Sequel ::SQL ::Identifier . new ( 'c' ) , :c ] ) . must_equal Set [ [ 1 , 1 ] , [ 2 , 2 ] ]
5486+ @ds . db . sqls . must_equal [ 'SELECT c, c FROM t' ]
5487+ end
5488+
5489+ it "should raise an error for plain strings" do
5490+ proc { @ds . select_set ( [ 'c' , :c ] ) } . must_raise ( Sequel ::Error )
5491+ @ds . db . sqls . must_equal [ ]
5492+ end
5493+
5494+ it "should handle an expression without a determinable alias" do
5495+ @ds . select_set { a ( t [ c ] ) } . must_equal Set [ 1 , 2 ]
5496+ @ds . db . sqls . must_equal [ 'SELECT a(t.c) AS v FROM t' ]
5497+ end
5498+
5499+ it "should accept a block" do
5500+ @ds . select_set { a ( t [ c ] ) . as ( b ) } . must_equal Set [ 1 , 2 ]
5501+ @ds . db . sqls . must_equal [ 'SELECT a(t.c) AS b FROM t' ]
5502+ end
5503+
5504+ it "should accept a block with an array of columns" do
5505+ @ds . select_set { [ a ( t [ c ] ) . as ( c ) , a ( t [ c ] ) . as ( c ) ] } . must_equal Set [ [ 1 , 1 ] , [ 2 , 2 ] ]
5506+ @ds . db . sqls . must_equal [ 'SELECT a(t.c) AS c, a(t.c) AS c FROM t' ]
5507+ end
5508+
5509+ it "should accept a block with a column" do
5510+ @ds . select_set ( :c ) { a ( t [ c ] ) . as ( c ) } . must_equal Set [ [ 1 , 1 ] , [ 2 , 2 ] ]
5511+ @ds . db . sqls . must_equal [ 'SELECT c, a(t.c) AS c FROM t' ]
5512+ end
5513+
5514+ it "should accept a block and array of arguments" do
5515+ @ds . select_set ( [ :c , :c ] ) { [ a ( t [ c ] ) . as ( c ) , a ( t [ c ] ) . as ( c ) ] } . must_equal Set [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] ]
5516+ @ds . db . sqls . must_equal [ 'SELECT c, c, a(t.c) AS c, a(t.c) AS c FROM t' ]
5517+ end
5518+
5519+ it "should handle an array of columns" do
5520+ @ds . select_set ( [ :c , :c ] ) . must_equal Set [ [ 1 , 1 ] , [ 2 , 2 ] ]
5521+ @ds . db . sqls . must_equal [ 'SELECT c, c FROM t' ]
5522+ @ds . select_set ( [ Sequel . expr ( :d ) . as ( :c ) , Sequel . qualify ( :b , :c ) , Sequel . identifier ( :c ) , Sequel . identifier ( :c ) . qualify ( :b ) ] ) . must_equal Set [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] ]
5523+ @ds . db . sqls . must_equal [ 'SELECT d AS c, b.c, c, b.c FROM t' ]
5524+ end
5525+
5526+ with_symbol_splitting "should handle an array of columns with splittable symbols" do
5527+ @ds . select_set ( [ :a__c , :a__d___c ] ) . must_equal Set [ [ 1 , 1 ] , [ 2 , 2 ] ]
5528+ @ds . db . sqls . must_equal [ 'SELECT a.c, a.d AS c FROM t' ]
5529+ end
5530+
5531+ it "should handle an array with a single element" do
5532+ @ds . select_set ( [ :c ] ) . must_equal Set [ [ 1 ] , [ 2 ] ]
5533+ @ds . db . sqls . must_equal [ 'SELECT c FROM t' ]
5534+ end
5535+ end
5536+
54235537describe "Dataset#lock_style and for_update" do
54245538 before do
54255539 @ds = Sequel . mock . dataset . from ( :t )
0 commit comments