add last_id method (last_insert_id)
This commit is contained in:
parent
3a32dc79ec
commit
e20df5c140
@ -252,6 +252,15 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
|
|||||||
return rb_mysql_result_to_obj(result);
|
return rb_mysql_result_to_obj(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE rb_mysql_client_last_id(VALUE self) {
|
||||||
|
MYSQL * client;
|
||||||
|
my_ulonglong id;
|
||||||
|
GetMysql2Client(self, client);
|
||||||
|
|
||||||
|
id = mysql_insert_id(client);
|
||||||
|
return ULL2NUM(id);
|
||||||
|
}
|
||||||
|
|
||||||
/* Mysql2::Result */
|
/* Mysql2::Result */
|
||||||
static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
|
static VALUE rb_mysql_result_to_obj(MYSQL_RES * r) {
|
||||||
VALUE obj;
|
VALUE obj;
|
||||||
@ -449,6 +458,7 @@ void Init_mysql2_ext() {
|
|||||||
rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0);
|
rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0);
|
||||||
rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0);
|
rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0);
|
||||||
rb_define_method(cMysql2Client, "async_result", rb_mysql_client_async_result, 0);
|
rb_define_method(cMysql2Client, "async_result", rb_mysql_client_async_result, 0);
|
||||||
|
rb_define_method(cMysql2Client, "last_id", rb_mysql_client_last_id, 0);
|
||||||
|
|
||||||
cMysql2Error = rb_define_class_under(mMysql2, "Error", rb_eStandardError);
|
cMysql2Error = rb_define_class_under(mMysql2, "Error", rb_eStandardError);
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ static VALUE rb_mysql_client_info(VALUE self);
|
|||||||
static VALUE rb_mysql_client_server_info(VALUE self);
|
static VALUE rb_mysql_client_server_info(VALUE self);
|
||||||
static VALUE rb_mysql_client_socket(VALUE self);
|
static VALUE rb_mysql_client_socket(VALUE self);
|
||||||
static VALUE rb_mysql_client_async_result(VALUE self);
|
static VALUE rb_mysql_client_async_result(VALUE self);
|
||||||
|
static VALUE rb_mysql_client_last_id(VALUE self);
|
||||||
void rb_mysql_client_free(void * client);
|
void rb_mysql_client_free(void * client);
|
||||||
|
|
||||||
/* Mysql2::Result */
|
/* Mysql2::Result */
|
||||||
|
@ -116,4 +116,19 @@ describe Mysql2::Client do
|
|||||||
result = @client.async_result
|
result = @client.async_result
|
||||||
result.class.should eql(Mysql2::Result)
|
result.class.should eql(Mysql2::Result)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should respond to #last_id" do
|
||||||
|
@client.should respond_to(:last_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "#last_id should return a Fixnum, the from the last INSERT/UPDATE" do
|
||||||
|
@client.last_id.should eql(0)
|
||||||
|
@client.query "USE test"
|
||||||
|
@client.query "CREATE TABLE lastIdTest (`id` int(11) NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))"
|
||||||
|
@client.last_id.should eql(0)
|
||||||
|
@client.query "INSERT INTO lastIdTest (blah) VALUES (1234)"
|
||||||
|
@client.last_id.should eql(1)
|
||||||
|
@client.query "DROP TABLE lastIdTest"
|
||||||
|
@client.last_id.should eql(0)
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user