Skip to content

Commit 0059051

Browse files
committed
Merge pull request #672 from jeremy/prepared-writes
Mysql2::Statement#last_id and #affected_rows (rebased)
2 parents b1ca5cb + d12aaa4 commit 0059051

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

ext/mysql2/statement.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,42 @@ static VALUE fields(VALUE self) {
415415
return field_list;
416416
}
417417

418+
/* call-seq:
419+
* stmt.last_id
420+
*
421+
* Returns the AUTO_INCREMENT value from the executed INSERT or UPDATE.
422+
*/
423+
static VALUE rb_mysql_stmt_last_id(VALUE self) {
424+
GET_STATEMENT(self);
425+
return ULL2NUM(mysql_stmt_insert_id(stmt_wrapper->stmt));
426+
}
427+
428+
/* call-seq:
429+
* stmt.affected_rows
430+
*
431+
* Returns the number of rows changed, deleted, or inserted.
432+
*/
433+
static VALUE rb_mysql_stmt_affected_rows(VALUE self) {
434+
my_ulonglong affected;
435+
GET_STATEMENT(self);
436+
437+
affected = mysql_stmt_affected_rows(stmt_wrapper->stmt);
438+
if (affected == (my_ulonglong)-1) {
439+
rb_raise_mysql2_stmt_error(self);
440+
}
441+
442+
return ULL2NUM(affected);
443+
}
444+
418445
void init_mysql2_statement() {
419446
cMysql2Statement = rb_define_class_under(mMysql2, "Statement", rb_cObject);
420447

421448
rb_define_method(cMysql2Statement, "param_count", param_count, 0);
422449
rb_define_method(cMysql2Statement, "field_count", field_count, 0);
423450
rb_define_method(cMysql2Statement, "execute", execute, -1);
424451
rb_define_method(cMysql2Statement, "fields", fields, 0);
452+
rb_define_method(cMysql2Statement, "last_id", rb_mysql_stmt_last_id, 0);
453+
rb_define_method(cMysql2Statement, "affected_rows", rb_mysql_stmt_affected_rows, 0);
425454

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

spec/mysql2/statement_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,74 @@
594594
end
595595
end
596596
end
597+
598+
context 'last_id' do
599+
before(:each) do
600+
@client.query 'USE test'
601+
@client.query 'CREATE TABLE IF NOT EXISTS lastIdTest (`id` BIGINT NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))'
602+
end
603+
604+
after(:each) do
605+
@client.query 'DROP TABLE lastIdTest'
606+
end
607+
608+
it 'should return last insert id' do
609+
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
610+
expect(stmt.last_id).to eq 0
611+
stmt.execute 1
612+
expect(stmt.last_id).to eq 1
613+
end
614+
615+
it 'should handle bigint ids' do
616+
stmt = @client.prepare 'INSERT INTO lastIdTest (id, blah) VALUES (?, ?)'
617+
stmt.execute 5000000000, 5000
618+
expect(stmt.last_id).to eql(5000000000)
619+
620+
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
621+
stmt.execute 5001
622+
expect(stmt.last_id).to eql(5000000001)
623+
end
624+
end
625+
626+
context 'affected_rows' do
627+
before :each do
628+
@client.query 'USE test'
629+
@client.query 'CREATE TABLE IF NOT EXISTS lastIdTest (`id` BIGINT NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))'
630+
end
631+
632+
after :each do
633+
@client.query 'DROP TABLE lastIdTest'
634+
end
635+
636+
it 'should return number of rows affected by an insert' do
637+
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
638+
expect(stmt.affected_rows).to eq 0
639+
stmt.execute 1
640+
expect(stmt.affected_rows).to eq 1
641+
end
642+
643+
it 'should return number of rows affected by an update' do
644+
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
645+
stmt.execute 1
646+
expect(stmt.affected_rows).to eq 1
647+
stmt.execute 2
648+
expect(stmt.affected_rows).to eq 1
649+
650+
stmt = @client.prepare 'UPDATE lastIdTest SET blah=? WHERE blah=?'
651+
stmt.execute 0, 1
652+
expect(stmt.affected_rows).to eq 1
653+
end
654+
655+
it 'should return number of rows affected by a delete' do
656+
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
657+
stmt.execute 1
658+
expect(stmt.affected_rows).to eq 1
659+
stmt.execute 2
660+
expect(stmt.affected_rows).to eq 1
661+
662+
stmt = @client.prepare 'DELETE FROM lastIdTest WHERE blah=?'
663+
stmt.execute 1
664+
expect(stmt.affected_rows).to eq 1
665+
end
666+
end
597667
end

0 commit comments

Comments
 (0)