Fix get_insert_id function of Net::MySQL

After I try to find Pure Perl of MySQL network protocol interface, I've found Net::MySQL on CPAN but there have some problem if you use get_insert_id and AUTO_INCREMENT value of table more than 255 the function will always return 254.

Current version of Net::MySQL 0.09 released on 01 Apr 2006

Here is fixed

Assume that you install it at /usr/lib/perl5/site_perl/5.8.6/Net/MySQL.pm
change bold version to be the perl version that you installed.

Change from

PERL:
  1. sub _get_insert_id
  2. {
  3.        my $self = shift;
  4.        my $packet = shift;
  5.        return ord(substr $packet, 6, 1) if ord(substr $packet, 6, 1) != 0xfc;
  6.        unpack 'v', substr $packet, 7, 2;
  7. }

To:

PERL:
  1. sub _get_insert_id
  2. {
  3.         my $self = shift;
  4.         my $packet = shift;
  5.         my $packet_lenght;
  6.         my $insert_id_value;
  7.  
  8.         $packet_lenght = ord(substr $packet, 0, 1);
  9.  
  10.         if ($packet_lenght == 7) {
  11.              $insert_id_valueord(substr $packet, 6, 1);
  12.         } elsif ($packet_lenght == 8) {
  13.              $insert_id_valueord(substr $packet, 7, 1) + (unpack('C', substr($packet, 8, 1)) <<8);
  14.         }  elsif ($packet_lenght == 9) {
  15.              $insert_id_valueord(substr $packet, 7, 1) + (unpack('C', substr($packet, 8, 1)) <<8);
  16.         } elsif ($packet_lenght == 10) {
  17.              $insert_id_valueord(substr $packet, 7, 1) + (unpack('C', substr($packet, 8, 1)) <<8) + (unpack('C', substr($packet, 9, 1)) <<16);
  18.         } elsif ($packet_lenght> 10) {
  19.              $insert_id_valueord(substr $packet, 7, 1) + (unpack('C', substr($packet, 8, 1)) <<8) + (unpack('C', substr($packet, 9, 1)) <<16) + (unpack('C', substr($packet, 10, 1)) <<24);
  20.         } else {
  21.             $insert_id_valueord(substr $packet, 7, 1) + (unpack('C', substr($packet, 8, 1)) <<8) + (unpack('C', substr($packet, 9, 1)) <<16)  + (unpack('C', substr($packet, 10, 1)) <<24);
  22.         }
  23.         return $insert_id_value;
  24.         unpack 'v', substr $packet, 7, 2;
  25. }

I test and it work for my program.

Let me know if you have question or any problem to change.


Leave a Reply

You must be logged in to post a comment.