Skip to content

Commit d5d050a

Browse files
committed
Tests for capturing query options in the Statement object
1 parent 2da4940 commit d5d050a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

ext/mysql2/statement.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql, VALUE options) {
111111
}
112112

113113
rb_obj_call_init(rb_stmt, 0, NULL);
114-
rb_iv_set(rb_stmt, "@query_options", options);
114+
rb_iv_set(rb_stmt, "@query_options", rb_hash_dup(options));
115115

116116
// instantiate stmt
117117
{
@@ -364,7 +364,7 @@ static VALUE rb_mysql_stmt_execute(int argc, VALUE *argv, VALUE self) {
364364

365365
is_streaming = (Qtrue == rb_hash_aref(current, sym_stream));
366366
if (!is_streaming) {
367-
// recieve the whole result set from the server
367+
// Receive the whole result set from the server
368368
if (rb_thread_call_without_gvl(nogvl_stmt_store_result, stmt, RUBY_UBF_IO, 0) == Qfalse) {
369369
mysql_free_result(metadata);
370370
rb_raise_mysql2_stmt_error(stmt_wrapper);

spec/mysql2/statement_spec.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@
5959
expect(rows).to eq([{ "1" => 1 }])
6060
end
6161

62+
it "should keep query options per prepared statement" do
63+
stmt1 = @client.prepare 'SELECT 1 AS a', :as => :hash
64+
stmt2 = @client.prepare 'SELECT 1 AS a', :as => :array
65+
66+
expect(stmt1.execute.first).to eq("a" => 1)
67+
expect(stmt2.execute.first).to eq([1])
68+
69+
expect(stmt1.query_options).to include(:as => :hash)
70+
expect(stmt2.query_options).to include(:as => :array)
71+
end
72+
73+
it "should capture query options when preparing the statement" do
74+
@client.query_options.merge!(:as => :hash)
75+
stmt1 = @client.prepare 'SELECT 1 AS a'
76+
77+
@client.query_options.merge!(:as => :array)
78+
stmt2 = @client.prepare 'SELECT 1 AS a'
79+
80+
expect(stmt1.execute.first).to eq("a" => 1)
81+
expect(stmt2.execute.first).to eq([1])
82+
83+
expect(stmt1.query_options).to include(:as => :hash)
84+
expect(stmt2.query_options).to include(:as => :array)
85+
end
86+
6287
it "should keep its result after other query" do
6388
@client.query 'USE test'
6489
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
@@ -180,14 +205,14 @@
180205
# note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
181206
# The drawback of this is that args of Result#each is ignored...
182207

183-
it "should yield rows as hash's" do
208+
it "should yield rows as hashes" do
184209
result = @client.prepare("SELECT 1").execute
185210
result.each do |row|
186211
expect(row).to be_an_instance_of(Hash)
187212
end
188213
end
189214

190-
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
215+
it "should yield rows as hashes with symbol keys if :symbolize_keys was set to true" do
191216
result = @client.prepare("SELECT 1", :symbolize_keys => true).execute
192217
result.each do |row|
193218
expect(row.keys.first).to be_an_instance_of(Symbol)

0 commit comments

Comments
 (0)