Newer Older
113 lines | 2.838kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::MySQL;
2
use base 'DBIx::Custom::Basic';
3

            
4
use warnings;
5
use strict;
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
6
use Carp 'croak';
packaging one directory
yuki-kimoto authored on 2009-11-16
7

            
8
my $class = __PACKAGE__;
9

            
10
$class->add_format(
11
    datetime => $class->formats->{SQL99_datetime},
12
    date     => $class->formats->{SQL99_date},
13
    time     => $class->formats->{SQL99_time},
14
);
15

            
16

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

            
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
38
sub last_insert_id {
39
    my $self = shift;
40
    
41
    croak "Not yet connected" unless $self->connected;
42
    
43
    my $last_insert_id = $self->dbh->{mysql_insertid};
44
    
45
    return $last_insert_id;
46
}
47

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

            
50
DBIx::Custom::MySQL - DBIx::Custom MySQL implementation
51

            
52
=head1 Synopsys
53

            
54
    # New
55
    my $dbi = DBIx::Custom::MySQL->new(user => 'taro', $password => 'kliej&@K',
56
                                      database => 'sample_db');
57
    # Insert 
58
    $dbi->insert('books', {title => 'perl', author => 'taro'});
59
    
60
    # Update 
61
    # same as 'update books set (title = 'aaa', author = 'ken') where id = 5;
62
    $dbi->update('books', {title => 'aaa', author => 'ken'}, {id => 5});
63
    
64
    # Delete
65
    $dbi->delete('books', {author => 'taro'});
66
    
67
    # select * from books;
68
    $dbi->select('books');
69
    
70
    # select * from books where ahthor = 'taro'; 
71
    $dbi->select('books', {author => 'taro'});
72

            
73
=head1 See DBIx::Custom and DBI::Custom::Basic documentation
74

            
75
This class is L<DBIx::Custom::Basic> subclass,
76
and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass.
77

            
78
You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
79
Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation.
80

            
81
=head1 Object methods
82

            
83
=head2 connect
84

            
85
    This method override DBIx::Custom::connect
86
    
87
    If database attribute is set, automatically data source is created and connect
88

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

            
91
    # Get last insert id
92
    $last_insert_id = $self->last_insert_id;
93

            
94
This is equal to MySQL function
95

            
96
    last_insert_id()
97
    
packaging one directory
yuki-kimoto authored on 2009-11-16
98
=head1 Author
99

            
100
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
101

            
102
Github L<http://github.com/yuki-kimoto>
103

            
104
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom>
105

            
106
=head1 Copyright & license
107

            
108
Copyright 2009 Yuki Kimoto, all rights reserved.
109

            
110
This program is free software; you can redistribute it and/or modify it
111
under the same terms as Perl itself.
112

            
113