Skip to content

ResultSet#each and Statement#each should splat record passed to block #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def fetch_hash(with_table=nil)
def each(&block)
return enum_for(:each) unless block
while rec = fetch
block.call rec
block.call *rec
end
self
end
Expand Down Expand Up @@ -965,7 +965,7 @@ def bind_result(*args)
def each(&block)
return enum_for(:each) unless block
while rec = fetch
block.call rec
block.call *rec
end
self
end
Expand Down
96 changes: 48 additions & 48 deletions spec/mysql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@

it '#each iterate block with a record' do
expect = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
@res.each do |a|
@res.each do |*a|
a.should == expect.shift
end
end
Expand Down Expand Up @@ -1030,7 +1030,7 @@
[2, 'def', Mysql::Time.new(2112,9,3,12,34,56)],
[3, '123', nil],
]
@s.each do |a|
@s.each do |*a|
a.should == expect.shift
end
end
Expand All @@ -1045,7 +1045,7 @@
@s.prepare 'insert into t values (?)'
@s.execute 123
@s.execute '456'
@m.query('select * from t').entries.should == [['123'], ['456']]
@m.query('select * from t').entries.should == ['123', '456']
end

it '#execute with various arguments' do
Expand Down Expand Up @@ -1173,16 +1173,16 @@
@s.execute
if defined? Encoding
@s.entries.should == [
["\x00".force_encoding('ASCII-8BIT')],
["\xff".force_encoding('ASCII-8BIT')],
["\x7f".force_encoding('ASCII-8BIT')],
["\xff".force_encoding('ASCII-8BIT')],
["\xff".force_encoding('ASCII-8BIT')],
["\xff".force_encoding('ASCII-8BIT')],
["\xff".force_encoding('ASCII-8BIT')],
"\x00".force_encoding('ASCII-8BIT'),
"\xff".force_encoding('ASCII-8BIT'),
"\x7f".force_encoding('ASCII-8BIT'),
"\xff".force_encoding('ASCII-8BIT'),
"\xff".force_encoding('ASCII-8BIT'),
"\xff".force_encoding('ASCII-8BIT'),
"\xff".force_encoding('ASCII-8BIT'),
]
else
@s.entries.should == [["\x00"], ["\xff"], ["\x7f"], ["\xff"], ["\xff"], ["\xff"], ["\xff"]]
@s.entries.should == ["\x00", "\xff", "\x7f", "\xff", "\xff", "\xff", "\xff"]
end
end

Expand All @@ -1193,19 +1193,19 @@
@s.execute
if defined? Encoding
@s.entries.should == [
["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
"\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT'),
"\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT'),
"\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT'),
"\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT'),
"\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT'),
]
else
@s.entries.should == [
["\x00\x00\x00\x00\x00\x00\x00\x00"],
["\xff\xff\xff\xff\xff\xff\xff\xff"],
["\x00\x00\x00\x01\x00\x00\x00\x00"],
["\xff\xff\xff\xff\xff\xff\xff\xff"],
["\xff\xff\xff\xff\xff\xff\xff\xff"],
"\x00\x00\x00\x00\x00\x00\x00\x00",
"\xff\xff\xff\xff\xff\xff\xff\xff",
"\x00\x00\x00\x01\x00\x00\x00\x00",
"\xff\xff\xff\xff\xff\xff\xff\xff",
"\xff\xff\xff\xff\xff\xff\xff\xff",
]
end
end
Expand All @@ -1215,79 +1215,79 @@
@m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [-1], [127], [-128], [127], [-128]]
@s.entries.should == [0, -1, 127, -128, 127, -128]
end

it '#fetch tinyint unsigned column' do
@m.query 'create temporary table t (i tinyint unsigned)'
@m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [0], [127], [0], [255], [0], [255]]
@s.entries.should == [0, 0, 127, 0, 255, 0, 255]
end

it '#fetch smallint column' do
@m.query 'create temporary table t (i smallint)'
@m.query 'insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [-1], [32767], [-32768], [32767], [-32768], [32767]]
@s.entries.should == [0, -1, 32767, -32768, 32767, -32768, 32767]
end

it '#fetch smallint unsigned column' do
@m.query 'create temporary table t (i smallint unsigned)'
@m.query 'insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [0], [32767], [0], [65535], [0], [65535]]
@s.entries.should == [0, 0, 32767, 0, 65535, 0, 65535]
end

it '#fetch mediumint column' do
@m.query 'create temporary table t (i mediumint)'
@m.query 'insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [-1], [8388607], [-8388608], [8388607], [-8388608], [8388607]]
@s.entries.should == [0, -1, 8388607, -8388608, 8388607, -8388608, 8388607]
end

it '#fetch mediumint unsigned column' do
@m.query 'create temporary table t (i mediumint unsigned)'
@m.query 'insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [0], [8388607], [0], [16777215], [0], [16777215]]
@s.entries.should == [0, 0, 8388607, 0, 16777215, 0, 16777215]
end

it '#fetch int column' do
@m.query 'create temporary table t (i int)'
@m.query 'insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [-1], [2147483647], [-2147483648], [2147483647], [-2147483648], [2147483647]]
@s.entries.should == [0, -1, 2147483647, -2147483648, 2147483647, -2147483648, 2147483647]
end

it '#fetch int unsigned column' do
@m.query 'create temporary table t (i int unsigned)'
@m.query 'insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [0], [2147483647], [0], [4294967295], [0], [4294967295]]
@s.entries.should == [0, 0, 2147483647, 0, 4294967295, 0, 4294967295]
end

it '#fetch bigint column' do
@m.query 'create temporary table t (i bigint)'
@m.query 'insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [-1], [9223372036854775807], [-9223372036854775808], [9223372036854775807], [-9223372036854775808], [9223372036854775807]]
@s.entries.should == [0, -1, 9223372036854775807, -9223372036854775808, 9223372036854775807, -9223372036854775808, 9223372036854775807]
end

it '#fetch bigint unsigned column' do
@m.query 'create temporary table t (i bigint unsigned)'
@m.query 'insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [0], [9223372036854775807], [0], [18446744073709551615], [0], [18446744073709551615]]
@s.entries.should == [0, 0, 9223372036854775807, 0, 18446744073709551615, 0, 18446744073709551615]
end

it '#fetch float column' do
Expand Down Expand Up @@ -1343,15 +1343,15 @@
@m.query 'insert into t values (0),(9999999999),(-9999999999),(10000000000),(-10000000000)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [["0"], ["9999999999"], ["-9999999999"], ["9999999999"], ["-9999999999"]]
@s.entries.should == ["0", "9999999999", "-9999999999", "9999999999", "-9999999999"]
end

it '#fetch decimal unsigned column' do
@m.query 'create temporary table t (i decimal unsigned)'
@m.query 'insert into t values (0),(9999999998),(9999999999),(-9999999998),(-9999999999),(10000000000),(-10000000000)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [["0"], ["9999999998"], ["9999999999"], ["0"], ["0"], ["9999999999"], ["0"]]
@s.entries.should == ["0", "9999999998", "9999999999", "0", "0", "9999999999", "0"]
end

it '#fetch date column' do
Expand Down Expand Up @@ -1404,119 +1404,119 @@
@m.query 'insert into t values (0),(70),(69),(1901),(2155)'
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[0], [1970], [2069], [1901], [2155]]
@s.entries.should == [0, 1970, 2069, 1901, 2155]
end

it '#fetch char column' do
@m.query 'create temporary table t (i char(10))'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ['abc']]
@s.entries.should == [nil, 'abc']
end

it '#fetch varchar column' do
@m.query 'create temporary table t (i varchar(10))'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ['abc']]
@s.entries.should == [nil, 'abc']
end

it '#fetch binary column' do
@m.query 'create temporary table t (i binary(10))'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc\0\0\0\0\0\0\0"]]
@s.entries.should == [nil, "abc\0\0\0\0\0\0\0"]
end

it '#fetch varbinary column' do
@m.query 'create temporary table t (i varbinary(10))'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch tinyblob column' do
@m.query 'create temporary table t (i tinyblob)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch tinytext column' do
@m.query 'create temporary table t (i tinytext)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch blob column' do
@m.query 'create temporary table t (i blob)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch text column' do
@m.query 'create temporary table t (i text)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch mediumblob column' do
@m.query 'create temporary table t (i mediumblob)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch mediumtext column' do
@m.query 'create temporary table t (i mediumtext)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch longblob column' do
@m.query 'create temporary table t (i longblob)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch longtext column' do
@m.query 'create temporary table t (i longtext)'
@m.query "insert into t values (null),('abc')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], ["abc"]]
@s.entries.should == [nil, "abc"]
end

it '#fetch enum column' do
@m.query "create temporary table t (i enum('abc','def'))"
@m.query "insert into t values (null),(0),(1),(2),('abc'),('def'),('ghi')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], [''], ['abc'], ['def'], ['abc'], ['def'], ['']]
@s.entries.should == [nil, '', 'abc', 'def', 'abc', 'def', '']
end

it '#fetch set column' do
@m.query "create temporary table t (i set('abc','def'))"
@m.query "insert into t values (null),(0),(1),(2),(3),('abc'),('def'),('abc,def'),('ghi')"
@s.prepare 'select i from t'
@s.execute
@s.entries.should == [[nil], [''], ['abc'], ['def'], ['abc,def'], ['abc'], ['def'], ['abc,def'], ['']]
@s.entries.should == [nil, '', 'abc', 'def', 'abc,def', 'abc', 'def', 'abc,def', '']
end

it '#field_count' do
Expand Down