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

            
4
use strict;
update document
yuki-kimoto authored on 2009-11-17
5
use warnings;
packaging one directory
yuki-kimoto authored on 2009-11-16
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,
yuki-kimoto authored on 2009-11-16
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
yuki-kimoto authored on 2009-11-16
62

            
63
=head1 NAME
64

            
65
DBIx::Custom::SQLite - DBIx::Custom SQLite implementation
66

            
67
=head1 Synopsys
68

            
69
    use DBIx::Custom::SQLite;
70
    
71
    # New
72
    my $dbi = DBIx::Custom::SQLite->new(user => 'taro', $password => 'kliej&@K',
update document
yuki-kimoto authored on 2009-11-19
73
                                        database => 'sample');
packaging one directory
yuki-kimoto authored on 2009-11-16
74
    
75
    # Insert 
76
    $dbi->insert('books', {title => 'perl', author => 'taro'});
77
    
78
    # Update 
update document
yuki-kimoto authored on 2009-11-19
79
    # same as 'update books set title = 'aaa', author = 'ken' where id = 5;
packaging one directory
yuki-kimoto authored on 2009-11-16
80
    $dbi->update('books', {title => 'aaa', author => 'ken'}, {id => 5});
81
    
82
    # Delete
83
    $dbi->delete('books', {author => 'taro'});
84
    
85
    # select * from books;
86
    $dbi->select('books');
87
    
88
    # select * from books where ahthor = 'taro'; 
89
    $dbi->select('books', {author => 'taro'}); 
90
    
91
    # select author, title from books where author = 'taro'
92
    $dbi->select('books', [qw/author title/], {author => 'taro'});
93
    
94
    # select author, title from books where author = 'taro' order by id limit 1;
95
    $dbi->select('books', [qw/author title/], {author => 'taro'},
96
                 'order by id limit 1');
97

            
update document
yuki-kimoto authored on 2009-11-19
98
=head1 See DBIx::Custom and DBI::Custom::Basic documentation at first
packaging one directory
yuki-kimoto authored on 2009-11-16
99

            
100
This class is L<DBIx::Custom::Basic> subclass.
101
and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass
102

            
103
You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
104
Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation
105

            
update document
yuki-kimoto authored on 2009-11-19
106
=head1 methods
packaging one directory
yuki-kimoto authored on 2009-11-16
107

            
108
=head2 connect
109

            
update document
yuki-kimoto authored on 2009-11-19
110
Connect to database
packaging one directory
yuki-kimoto authored on 2009-11-16
111

            
update document
yuki-kimoto authored on 2009-11-19
112
    $self = $self->connect;
113
    
114
    # Sample
packaging one directory
yuki-kimoto authored on 2009-11-16
115
    $dbi->connect;
116

            
update document
yuki-kimoto authored on 2009-11-19
117
This override L<DBIx::Custom> connect.
118

            
119
If you set database, data source is automatically created and connect
packaging one directory
yuki-kimoto authored on 2009-11-16
120

            
121
=head2 connect_memory
122

            
update document
yuki-kimoto authored on 2009-11-19
123
Connect memory database
124

            
125
    $self = $self->connect_memory;
126
    
127
    # Sample
128
    $dbi->connect_memory;
packaging one directory
yuki-kimoto authored on 2009-11-16
129

            
130
=head2 reconnect_memory
131

            
update document
yuki-kimoto authored on 2009-11-19
132
Reconnect to memory databsse
133

            
134
    $self = $self->reconnect_memory;
135
    
136
    # Sample
packaging one directory
yuki-kimoto authored on 2009-11-16
137
    $self = $dbi->reconnect_memory;
138

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

            
update document
yuki-kimoto authored on 2009-11-19
141
Get last insert id
142

            
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
143
    $last_insert_id = $self->last_insert_id;
update document
yuki-kimoto authored on 2009-11-19
144
    
145
    # Sample
146
    $dbi->insert('books', {title => 'Perl', author => 'taro'});
147
    $last_insert_id = $dbi->last_insert_id;
add Oracle, DB2, Pg,
yuki-kimoto authored on 2009-11-16
148

            
149
This is equal to SQLite function
150

            
151
    last_insert_rowid()
152

            
packaging one directory
yuki-kimoto authored on 2009-11-16
153
=head1 Author
154

            
155
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
156

            
157
Github L<http://github.com/yuki-kimoto>
158

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

            
161
=head1 Copyright & lisence
162

            
163
Copyright 2009 Yuki Kimoto, all rights reserved.
164

            
165
This program is free software; you can redistribute it and/or modify it
166
under the same terms as Perl itself.
167