Skip to content
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

Improve prepared statement ergonomics #1385

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ group :test do
gem 'rspec', '~> 3.2'

# https://github.com/bbatsov/rubocop/pull/4789
gem 'rubocop', '~> 1.30', '>= 1.30.1' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6')
# 1.51 is the last version supporting Ruby 2.6
gem 'rubocop', '>= 1.30.1', '< 1.51' if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should ensure a consistent rubocop result in local and CI.

end

group :benchmarks, optional: true do
Expand Down
27 changes: 23 additions & 4 deletions ext/mysql2/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ static VALUE intern_sec_fraction, intern_usec, intern_sec, intern_min, intern_ho
#define TypedData_Get_Struct(obj, type, ignore, sval) Data_Get_Struct(obj, type, sval)
#endif

#define GET_STATEMENT(self) \
#define RAW_GET_STATEMENT(self) \
mysql_stmt_wrapper *stmt_wrapper; \
TypedData_Get_Struct(self, mysql_stmt_wrapper, &rb_mysql_statement_type, stmt_wrapper); \

#define GET_STATEMENT(self) \
RAW_GET_STATEMENT(self) \
if (!stmt_wrapper->stmt) { rb_raise(cMysql2Error, "Invalid statement handle"); } \
if (stmt_wrapper->closed) { rb_raise(cMysql2Error, "Statement handle already closed"); }

Expand Down Expand Up @@ -603,12 +606,27 @@ static VALUE rb_mysql_stmt_affected_rows(VALUE self) {
* own prepared statement cache.
*/
static VALUE rb_mysql_stmt_close(VALUE self) {
GET_STATEMENT(self);
stmt_wrapper->closed = 1;
rb_thread_call_without_gvl(nogvl_stmt_close, stmt_wrapper, RUBY_UBF_IO, 0);
RAW_GET_STATEMENT(self);

if (!stmt_wrapper->closed) {
stmt_wrapper->closed = 1;
rb_thread_call_without_gvl(nogvl_stmt_close, stmt_wrapper, RUBY_UBF_IO, 0);
}

byroot marked this conversation as resolved.
Show resolved Hide resolved
return Qnil;
}

/* call-seq:
* stmt.closed?
*
* Returns wheter or not the statement have been closed.
*/
static VALUE rb_mysql_stmt_closed_p(VALUE self) {
RAW_GET_STATEMENT(self);

return stmt_wrapper->closed ? Qtrue : Qfalse;
}

void init_mysql2_statement() {
cDate = rb_const_get(rb_cObject, rb_intern("Date"));
rb_global_variable(&cDate);
Expand All @@ -630,6 +648,7 @@ void init_mysql2_statement() {
rb_define_method(cMysql2Statement, "last_id", rb_mysql_stmt_last_id, 0);
rb_define_method(cMysql2Statement, "affected_rows", rb_mysql_stmt_affected_rows, 0);
rb_define_method(cMysql2Statement, "close", rb_mysql_stmt_close, 0);
rb_define_method(cMysql2Statement, "closed?", rb_mysql_stmt_closed_p, 0);

sym_stream = ID2SYM(rb_intern("stream"));

Expand Down
16 changes: 13 additions & 3 deletions spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require './spec/spec_helper'

RSpec.describe Mysql2::Statement do
RSpec.describe Mysql2::Statement do # rubocop:disable Metrics/BlockLength
byroot marked this conversation as resolved.
Show resolved Hide resolved
before(:example) do
@client = new_client(encoding: "utf8")
end
Expand Down Expand Up @@ -319,8 +319,8 @@ def stmt_count
it "should throw an exception if we try to iterate twice when streaming is enabled" do
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: false)
expect do
result.each {}
result.each {}
result.to_a
result.to_a
end.to raise_exception(Mysql2::Error)
end
end
Expand Down Expand Up @@ -720,5 +720,15 @@ def stmt_count
stmt.close
expect { stmt.execute }.to raise_error(Mysql2::Error, /Invalid statement handle/)
end

it 'should not raise if called multiple times' do
stmt = @client.prepare 'SELECT 1'
expect(stmt).to_not be_closed

3.times do
expect { stmt.close }.to_not raise_error
expect(stmt).to be_closed
end
end
end
end
Loading