Newer Older
83 lines | 1.95kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::MySQL;
2

            
3
use warnings;
4
use strict;
update document
yuki-kimoto authored on 2010-01-30
5

            
6
use base 'DBIx::Custom::Basic';
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
7
use Carp 'croak';
packaging one directory
yuki-kimoto authored on 2009-11-16
8

            
some changed
yuki-kimoto authored on 2010-05-02
9
__PACKAGE__->register_format(
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
10
    datetime => __PACKAGE__->formats->{SQL99_datetime},
11
    date     => __PACKAGE__->formats->{SQL99_date},
12
    time     => __PACKAGE__->formats->{SQL99_time},
packaging one directory
yuki-kimoto authored on 2009-11-16
13
);
14

            
15
sub connect {
16
    my $self = shift;
17
    
update document
yuki-kimoto authored on 2010-01-30
18
    # Create data source
add port and host method
yuki-kimoto authored on 2009-11-16
19
    if (!$self->data_source) {
20
        my $database = $self->database;
21
        my $host     = $self->host;
22
        my $port     = $self->port;
23
        my $data_source = "dbi:mysql:";
24
        my $data_source_original = $data_source;
25
        $data_source .= "database=$database;" if $database;
26
        $data_source .= "host=$host;"         if $host;
27
        $data_source .= "port=$port;"         if $port;
28
        $data_source =~ s/:$// if $data_source eq $data_source_original;
29
        $self->data_source($data_source);
packaging one directory
yuki-kimoto authored on 2009-11-16
30
    }
31
    
32
    return $self->SUPER::connect;
33
}
34

            
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
35
sub last_insert_id {
36
    my $self = shift;
37
    
update document
yuki-kimoto authored on 2010-01-30
38
    # Not connected
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
39
    croak "Not yet connected" unless $self->connected;
40
    
update document
yuki-kimoto authored on 2010-01-30
41
    # Get last insert id
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
42
    my $last_insert_id = $self->dbh->{mysql_insertid};
43
    
44
    return $last_insert_id;
45
}
46

            
packaging one directory
yuki-kimoto authored on 2009-11-16
47
=head1 NAME
48

            
49
DBIx::Custom::MySQL - DBIx::Custom MySQL implementation
50

            
update document
yuki-kimoto authored on 2010-01-30
51
=head1 SYNOPSYS
packaging one directory
yuki-kimoto authored on 2009-11-16
52

            
53
    # New
54
    my $dbi = DBIx::Custom::MySQL->new(user => 'taro', $password => 'kliej&@K',
version 0.0901
yuki-kimoto authored on 2009-12-17
55
                                       database => 'sample_db');
packaging one directory
yuki-kimoto authored on 2009-11-16
56

            
update document
yuki-kimoto authored on 2010-01-30
57
=head1 METHODS
packaging one directory
yuki-kimoto authored on 2009-11-16
58

            
update document
yuki-kimoto authored on 2010-01-30
59
This class is L<DBIx::Custom::Basic> subclass.
60
You can use all methods of L<DBIx::Custom::Basic>
packaging one directory
yuki-kimoto authored on 2009-11-16
61

            
62
=head2 connect
63

            
update document
yuki-kimoto authored on 2009-11-19
64
Connect to database
65

            
version 0.0901
yuki-kimoto authored on 2009-12-17
66
    $self->connect;
update document
yuki-kimoto authored on 2009-11-19
67

            
version 0.0901
yuki-kimoto authored on 2009-12-17
68
If you set database, host, or port, data source is automatically created.
packaging one directory
yuki-kimoto authored on 2009-11-16
69

            
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
70
=head2 last_insert_id
71

            
version 0.0901
yuki-kimoto authored on 2009-12-17
72
    $last_insert_id = $dbi->last_insert_id;
73

            
74
The folloing is last_insert_id sample.
75

            
update document
yuki-kimoto authored on 2009-11-19
76
    $dbi->insert('books', {title => 'Perl', author => 'taro'});
77
    $last_insert_id = $dbi->last_insert_id;
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
78

            
79
This is equal to MySQL function
80

            
81
    last_insert_id()
82
    
update document
yuki-kimoto authored on 2010-01-30
83
=cut