Newer Older
150 lines | 3.508kb
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',
73
                                       database => 'sample.db');
74
    
75
    # Insert 
76
    $dbi->insert('books', {title => 'perl', author => 'taro'});
77
    
78
    # Update 
79
    # same as 'update books set (title = 'aaa', author = 'ken') where id = 5;
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

            
98
=head1 See DBIx::Custom and DBI::Custom::Basic documentation
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

            
106
=head1 Object methods
107

            
108
=head2 connect
109

            
110
This override L<DBIx::Custom> connect.
111

            
112
    # Connect to database
113
    $dbi->connect;
114

            
115
If database attribute is set, automatically data source is created and connect
116

            
117
=head2 connect_memory
118

            
119
    # Connect memory database
120
    $self = $dbi->connect_memory;
121

            
122
=head2 reconnect_memory
123

            
124
    # Reconnect memory database
125
    $self = $dbi->reconnect_memory;
126

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

            
129
    # Get last insert id
130
    $last_insert_id = $self->last_insert_id;
131

            
132
This is equal to SQLite function
133

            
134
    last_insert_rowid()
135

            
packaging one directory
yuki-kimoto authored on 2009-11-16
136
=head1 Author
137

            
138
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
139

            
140
Github L<http://github.com/yuki-kimoto>
141

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

            
144
=head1 Copyright & lisence
145

            
146
Copyright 2009 Yuki Kimoto, all rights reserved.
147

            
148
This program is free software; you can redistribute it and/or modify it
149
under the same terms as Perl itself.
150