Skip to content

Commit abd06c5

Browse files
committed
Meld rb_raise_mysql2_stmt_error2 into rb_raise_mysql2_stmt_error
1 parent 1d4de89 commit abd06c5

File tree

3 files changed

+26
-54
lines changed

3 files changed

+26
-54
lines changed

ext/mysql2/result.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,7 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
357357
}
358358

359359
if (mysql_stmt_bind_result(wrapper->stmt_wrapper->stmt, wrapper->result_buffers)) {
360-
rb_raise_mysql2_stmt_error2(wrapper->stmt_wrapper->stmt
361-
#ifdef HAVE_RUBY_ENCODING_H
362-
, conn_enc
363-
#endif
364-
);
360+
rb_raise_mysql2_stmt_error(wrapper->stmt_wrapper);
365361
}
366362

367363
{
@@ -372,11 +368,7 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
372368

373369
case 1:
374370
/* error */
375-
rb_raise_mysql2_stmt_error2(wrapper->stmt_wrapper->stmt
376-
#ifdef HAVE_RUBY_ENCODING_H
377-
, conn_enc
378-
#endif
379-
);
371+
rb_raise_mysql2_stmt_error(wrapper->stmt_wrapper);
380372

381373
case MYSQL_NO_DATA:
382374
/* no more row */

ext/mysql2/statement.c

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
VALUE cMysql2Statement;
44
extern VALUE mMysql2, cMysql2Error, cBigDecimal, cDateTime, cDate;
5-
static VALUE sym_stream, intern_error_number_eql, intern_sql_state_eql, intern_each;
5+
static VALUE sym_stream, intern_new_with_args, intern_each;
66
static VALUE intern_usec, intern_sec, intern_min, intern_hour, intern_day, intern_month, intern_year;
77

88
#define GET_STATEMENT(self) \
@@ -31,15 +31,17 @@ void decr_mysql2_stmt(mysql_stmt_wrapper *stmt_wrapper) {
3131
}
3232
}
3333

34-
VALUE rb_raise_mysql2_stmt_error2(MYSQL_STMT *stmt
35-
#ifdef HAVE_RUBY_ENCODING_H
36-
, rb_encoding *conn_enc
37-
#endif
38-
) {
39-
VALUE rb_error_msg = rb_str_new2(mysql_stmt_error(stmt));
40-
VALUE rb_sql_state = rb_tainted_str_new2(mysql_stmt_sqlstate(stmt));
41-
VALUE e = rb_exc_new3(cMysql2Error, rb_error_msg);
34+
35+
void rb_raise_mysql2_stmt_error(mysql_stmt_wrapper *stmt_wrapper) {
36+
VALUE e;
37+
GET_CLIENT(stmt_wrapper->client);
38+
VALUE rb_error_msg = rb_str_new2(mysql_stmt_error(stmt_wrapper->stmt));
39+
VALUE rb_sql_state = rb_tainted_str_new2(mysql_stmt_sqlstate(stmt_wrapper->stmt));
40+
4241
#ifdef HAVE_RUBY_ENCODING_H
42+
rb_encoding *conn_enc;
43+
conn_enc = rb_to_encoding(wrapper->encoding);
44+
4345
rb_encoding *default_internal_enc = rb_default_internal_encoding();
4446

4547
rb_enc_associate(rb_error_msg, conn_enc);
@@ -49,30 +51,13 @@ VALUE rb_raise_mysql2_stmt_error2(MYSQL_STMT *stmt
4951
rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc);
5052
}
5153
#endif
52-
rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_stmt_errno(stmt)));
53-
rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state);
54-
rb_exc_raise(e);
55-
return Qnil;
56-
}
5754

58-
static void rb_raise_mysql2_stmt_error(VALUE self) {
59-
#ifdef HAVE_RUBY_ENCODING_H
60-
rb_encoding *conn_enc;
61-
#endif
62-
GET_STATEMENT(self);
63-
64-
#ifdef HAVE_RUBY_ENCODING_H
65-
{
66-
GET_CLIENT(stmt_wrapper->client);
67-
conn_enc = rb_to_encoding(wrapper->encoding);
68-
}
69-
#endif
70-
71-
rb_raise_mysql2_stmt_error2(stmt_wrapper->stmt
72-
#ifdef HAVE_RUBY_ENCODING_H
73-
, conn_enc
74-
#endif
75-
);
55+
e = rb_funcall(cMysql2Error, intern_new_with_args, 4,
56+
rb_error_msg,
57+
LONG2FIX(wrapper->server_version),
58+
UINT2NUM(mysql_stmt_errno(stmt_wrapper->stmt)),
59+
rb_sql_state);
60+
rb_exc_raise(e);
7661
}
7762

7863

@@ -146,7 +131,7 @@ VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql) {
146131
args.sql_len = RSTRING_LEN(sql);
147132

148133
if ((VALUE)rb_thread_call_without_gvl(nogvl_prepare_statement, &args, RUBY_UBF_IO, 0) == Qfalse) {
149-
rb_raise_mysql2_stmt_error(rb_stmt);
134+
rb_raise_mysql2_stmt_error(stmt_wrapper);
150135
}
151136
}
152137

@@ -335,13 +320,13 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
335320
// copies bind_buffers into internal storage
336321
if (mysql_stmt_bind_param(stmt, bind_buffers)) {
337322
FREE_BINDS;
338-
rb_raise_mysql2_stmt_error(self);
323+
rb_raise_mysql2_stmt_error(stmt_wrapper);
339324
}
340325
}
341326

342327
if ((VALUE)rb_thread_call_without_gvl(nogvl_execute, stmt, RUBY_UBF_IO, 0) == Qfalse) {
343328
FREE_BINDS;
344-
rb_raise_mysql2_stmt_error(self);
329+
rb_raise_mysql2_stmt_error(stmt_wrapper);
345330
}
346331

347332
FREE_BINDS;
@@ -352,7 +337,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
352337
// either CR_OUT_OF_MEMORY or CR_UNKNOWN_ERROR. both fatal.
353338

354339
MARK_CONN_INACTIVE(stmt_wrapper->client);
355-
rb_raise_mysql2_stmt_error(self);
340+
rb_raise_mysql2_stmt_error(stmt_wrapper);
356341
}
357342
// no data and no error, so query was not a SELECT
358343
return Qnil;
@@ -367,7 +352,7 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
367352
// recieve the whole result set from the server
368353
if (rb_thread_call_without_gvl(nogvl_stmt_store_result, stmt, RUBY_UBF_IO, 0) == Qfalse) {
369354
mysql_free_result(metadata);
370-
rb_raise_mysql2_stmt_error(self);
355+
rb_raise_mysql2_stmt_error(stmt_wrapper);
371356
}
372357
MARK_CONN_INACTIVE(stmt_wrapper->client);
373358
}
@@ -440,8 +425,7 @@ void init_mysql2_statement() {
440425

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

443-
intern_error_number_eql = rb_intern("error_number=");
444-
intern_sql_state_eql = rb_intern("sql_state=");
428+
intern_new_with_args = rb_intern("new_with_args");
445429
intern_each = rb_intern("each");
446430

447431
intern_usec = rb_intern("usec");

ext/mysql2/statement.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ void init_mysql2_statement();
1313
void decr_mysql2_stmt(mysql_stmt_wrapper *stmt_wrapper);
1414

1515
VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql);
16-
VALUE rb_raise_mysql2_stmt_error2(MYSQL_STMT *stmt
17-
#ifdef HAVE_RUBY_ENCODING_H
18-
, rb_encoding* conn_enc
19-
#endif
20-
);
16+
void rb_raise_mysql2_stmt_error(mysql_stmt_wrapper *stmt_wrapper);
2117

2218
#endif

0 commit comments

Comments
 (0)