packaging one directory
|
1 |
package DBIx::Custom::MySQL; |
2 |
use base 'DBIx::Custom::Basic'; |
|
3 | ||
4 |
use warnings; |
|
5 |
use strict; |
|
add Oracle, DB2, Pg,
|
6 |
use Carp 'croak'; |
packaging one directory
|
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
|
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
|
33 |
} |
34 |
|
|
35 |
return $self->SUPER::connect; |
|
36 |
} |
|
37 | ||
add Oracle, DB2, Pg,
|
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
|
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 |
|
update document
|
61 |
# same as 'update books set title = 'aaa', author = 'ken' where id = 5; |
packaging one directory
|
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 | ||
update document
|
73 |
=head1 See DBIx::Custom and DBI::Custom::Basic documentation at first |
packaging one directory
|
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 | ||
update document
|
85 |
Connect to database |
86 | ||
87 |
$self = $self->connect; |
|
packaging one directory
|
88 |
|
update document
|
89 |
# Sample |
90 |
$dbi->connect; |
|
91 | ||
92 |
This override L<DBIx::Custom> connect. |
|
93 | ||
94 |
If you set database, host, or port, data source is automatically created and connect |
|
packaging one directory
|
95 | |
add Oracle, DB2, Pg,
|
96 |
=head2 last_insert_id |
97 | ||
98 |
$last_insert_id = $self->last_insert_id; |
|
update document
|
99 |
|
100 |
# Sample |
|
101 |
$dbi->insert('books', {title => 'Perl', author => 'taro'}); |
|
102 |
$last_insert_id = $dbi->last_insert_id; |
|
add Oracle, DB2, Pg,
|
103 | |
104 |
This is equal to MySQL function |
|
105 | ||
106 |
last_insert_id() |
|
107 |
|
|
packaging one directory
|
108 |
=head1 Author |
109 | ||
110 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
111 | ||
112 |
Github L<http://github.com/yuki-kimoto> |
|
113 | ||
114 |
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
115 | ||
116 |
=head1 Copyright & license |
|
117 | ||
118 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
119 | ||
120 |
This program is free software; you can redistribute it and/or modify it |
|
121 |
under the same terms as Perl itself. |
|
122 | ||
123 |