packaging one directory
|
1 |
package DBIx::Custom::SQLite; |
2 |
use base 'DBIx::Custom::Basic'; |
|
3 | ||
4 |
use warnings; |
|
5 |
use strict; |
|
6 |
use Carp 'croak'; |
|
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 |
sub connect { |
|
17 |
my $self = shift; |
|
18 |
|
|
19 |
if (!$self->data_source && (my $database = $self->database)) { |
|
20 |
$self->data_source("dbi:SQLite:dbname=$database"); |
|
21 |
} |
|
22 |
|
|
23 |
return $self->SUPER::connect; |
|
24 |
} |
|
25 | ||
26 |
sub connect_memory { |
|
27 |
my $self = shift; |
|
28 |
|
|
29 |
# Data source for memory database |
|
30 |
$self->data_source('dbi:SQLite:dbname=:memory:'); |
|
31 |
|
|
32 |
# Already connected |
|
33 |
croak("Already connected") if $self->connected; |
|
34 |
|
|
35 |
# Connect |
|
36 |
$self->connect; |
|
37 |
|
|
38 |
return $self; |
|
39 |
} |
|
40 | ||
41 |
sub reconnect_memory { |
|
42 |
my $self = shift; |
|
43 | ||
44 |
# Data source for memory database |
|
45 |
$self->data_source('dbi:SQLite:dbname=:memory:'); |
|
46 |
|
|
47 |
# Reconnect |
|
48 |
$self->reconnect; |
|
49 |
|
|
50 |
return $self; |
|
51 |
} |
|
52 | ||
add Oracle, DB2, Pg,
|
53 |
sub last_insert_id { |
54 |
my $self = shift; |
|
55 |
|
|
56 |
croak "Not yet connected" unless $self->connected; |
|
57 |
|
|
58 |
my $last_insert_id = $self->dbh->func('last_insert_rowid'); |
|
59 |
|
|
60 |
return $last_insert_id; |
|
61 |
} |
|
packaging one directory
|
62 | |
63 |
=head1 NAME |
|
64 | ||
65 |
DBIx::Custom::SQLite - DBIx::Custom SQLite implementation |
|
66 | ||
67 |
=head1 Version |
|
68 | ||
69 |
Version 0.0201 |
|
70 | ||
71 |
=head1 Synopsys |
|
72 | ||
73 |
use DBIx::Custom::SQLite; |
|
74 |
|
|
75 |
# New |
|
76 |
my $dbi = DBIx::Custom::SQLite->new(user => 'taro', $password => 'kliej&@K', |
|
77 |
database => 'sample.db'); |
|
78 |
|
|
79 |
# Insert |
|
80 |
$dbi->insert('books', {title => 'perl', author => 'taro'}); |
|
81 |
|
|
82 |
# Update |
|
83 |
# same as 'update books set (title = 'aaa', author = 'ken') where id = 5; |
|
84 |
$dbi->update('books', {title => 'aaa', author => 'ken'}, {id => 5}); |
|
85 |
|
|
86 |
# Delete |
|
87 |
$dbi->delete('books', {author => 'taro'}); |
|
88 |
|
|
89 |
# select * from books; |
|
90 |
$dbi->select('books'); |
|
91 |
|
|
92 |
# select * from books where ahthor = 'taro'; |
|
93 |
$dbi->select('books', {author => 'taro'}); |
|
94 |
|
|
95 |
# select author, title from books where author = 'taro' |
|
96 |
$dbi->select('books', [qw/author title/], {author => 'taro'}); |
|
97 |
|
|
98 |
# select author, title from books where author = 'taro' order by id limit 1; |
|
99 |
$dbi->select('books', [qw/author title/], {author => 'taro'}, |
|
100 |
'order by id limit 1'); |
|
101 | ||
102 |
=head1 See DBIx::Custom and DBI::Custom::Basic documentation |
|
103 | ||
104 |
This class is L<DBIx::Custom::Basic> subclass. |
|
105 |
and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass |
|
106 | ||
107 |
You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom> |
|
108 |
Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation |
|
109 | ||
110 |
=head1 Object methods |
|
111 | ||
112 |
=head2 connect |
|
113 | ||
114 |
This override L<DBIx::Custom> connect. |
|
115 | ||
116 |
# Connect to database |
|
117 |
$dbi->connect; |
|
118 | ||
119 |
If database attribute is set, automatically data source is created and connect |
|
120 | ||
121 |
=head2 connect_memory |
|
122 | ||
123 |
# Connect memory database |
|
124 |
$self = $dbi->connect_memory; |
|
125 | ||
126 |
=head2 reconnect_memory |
|
127 | ||
128 |
# Reconnect memory database |
|
129 |
$self = $dbi->reconnect_memory; |
|
130 | ||
add Oracle, DB2, Pg,
|
131 |
=head2 last_insert_id |
132 | ||
133 |
# Get last insert id |
|
134 |
$last_insert_id = $self->last_insert_id; |
|
135 | ||
136 |
This is equal to SQLite function |
|
137 | ||
138 |
last_insert_rowid() |
|
139 | ||
packaging one directory
|
140 |
=head1 Author |
141 | ||
142 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
143 | ||
144 |
Github L<http://github.com/yuki-kimoto> |
|
145 | ||
146 |
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
147 | ||
148 |
=head1 Copyright & lisence |
|
149 | ||
150 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
151 | ||
152 |
This program is free software; you can redistribute it and/or modify it |
|
153 |
under the same terms as Perl itself. |
|
154 |