Skip to content

Commit 941e5ed

Browse files
nyaxtjustincase
authored andcommitted
port #each/#fields test from result_spec.rb / all test pass
1 parent 7138a9b commit 941e5ed

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

ext/mysql2/result.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,10 @@ static VALUE rb_mysql_result_each(int argc, VALUE * argv, VALUE self) {
941941
rb_warn("cacheRows is ignored if streaming is true");
942942
}
943943

944+
if(wrapper->stmt && !args.cacheRows && !args.streaming) {
945+
rb_warn("cacheRows is forced for prepared statements (if not streaming)");
946+
}
947+
944948
dbTz = rb_hash_aref(opts, sym_database_timezone);
945949
if (dbTz == sym_local) {
946950
db_timezone = intern_local;

spec/mysql2/statement_spec.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,91 @@
148148
end
149149
end
150150

151+
context "#each" do
152+
# note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
153+
# The drawback of this is that args of Result#each is ignored...
154+
155+
it "should yield rows as hash's" do
156+
@result = @client.prepare("SELECT 1").execute
157+
@result.each do |row|
158+
row.class.should eql(Hash)
159+
end
160+
end
161+
162+
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
163+
@client.query_options[:symbolize_keys] = true
164+
@result = @client.prepare("SELECT 1").execute
165+
@result.each do |row|
166+
row.keys.first.class.should eql(Symbol)
167+
end
168+
@client.query_options[:symbolize_keys] = false
169+
end
170+
171+
it "should be able to return results as an array" do
172+
@client.query_options[:as] = :array
173+
174+
@result = @client.prepare("SELECT 1").execute
175+
@result.each do |row|
176+
row.class.should eql(Array)
177+
end
178+
179+
@client.query_options[:as] = :hash
180+
end
181+
182+
it "should cache previously yielded results by default" do
183+
@result = @client.prepare("SELECT 1").execute
184+
@result.first.object_id.should eql(@result.first.object_id)
185+
end
186+
187+
it "should yield different value for #first if streaming" do
188+
@client.query_options[:stream] = true
189+
@client.query_options[:cache_rows] = false
190+
191+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
192+
result.first.should_not eql(result.first)
193+
194+
@client.query_options[:stream] = false
195+
@client.query_options[:cache_rows] = true
196+
end
197+
198+
it "should yield the same value for #first if streaming is disabled" do
199+
@client.query_options[:stream] = false
200+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
201+
result.first.should eql(result.first)
202+
end
203+
204+
it "should throw an exception if we try to iterate twice when streaming is enabled" do
205+
@client.query_options[:stream] = true
206+
@client.query_options[:cache_rows] = false
207+
208+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
209+
210+
expect {
211+
result.each {}
212+
result.each {}
213+
}.to raise_exception(Mysql2::Error)
214+
215+
@client.query_options[:stream] = false
216+
@client.query_options[:cache_rows] = true
217+
end
218+
end
219+
220+
context "#fields" do
221+
before(:each) do
222+
@client.query "USE test"
223+
@test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute
224+
end
225+
226+
it "method should exist" do
227+
@test_result.should respond_to(:fields)
228+
end
229+
230+
it "should return an array of field names in proper order" do
231+
result = @client.prepare("SELECT 'a', 'b', 'c'").execute
232+
result.fields.should eql(['a', 'b', 'c'])
233+
end
234+
end
235+
151236
context "row data type mapping" do
152237
before(:each) do
153238
@client.query "USE test"

0 commit comments

Comments
 (0)