Newer Older
133 lines | 2.971kb
packing
yuki-kimoto authored on 2009-11-12
1
package DBIx::Custom::SQLite;
2
use base 'DBIx::Custom::Basic';
3

            
4
use warnings;
5
use strict;
6
use Carp 'croak';
7

            
8
our $VERSION = '0.0101';
9

            
10
my $class = __PACKAGE__;
11

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

            
18
sub connect {
19
    my $self = shift;
20
    
21
    if (!$self->data_source && (my $database = $self->database)) {
22
        $self->data_source("dbi:SQLite:dbname=$database");
23
    }
24
    
25
    return $self->SUPER::connect;
26
}
27

            
28
sub connect_memory {
29
    my $self = shift;
30
    
31
    # Data source for memory database
32
    $self->data_source('dbi:SQLite:dbname=:memory:');
33
    
34
    # Already connected
35
    croak("Already connected") if $self->connected;
36
    
37
    # Connect
38
    $self->connect;
39
    
40
    return $self;
41
}
42

            
43
sub reconnect_memory {
44
    my $self = shift;
45

            
46
    # Data source for memory database
47
    $self->data_source('dbi:SQLite:dbname=:memory:');
48
    
49
    # Reconnect
50
    $self->reconnect;
51
    
52
    return $self;
53
}
54

            
55

            
56
=head1 NAME
57

            
58
DBIx::Custom::SQLite - DBIx::Custom SQLite implementation
59

            
60
=head1 VERSION
61

            
62
Version 0.0101
63

            
64
=head1 SYNOPSYS
65

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

            
97
This module automatically encode_utf8 or decode_utf8
98
If you do not want to this, you set 
99
    
100
    $dbi->bind_filter(undef);
101
    $dbi->fetch_filter(undef);
102

            
103
=head1 OBJECT METHOD
104

            
105
=head2 connect
106

            
107
    This method override DBIx::Custom::connect
108
    
109
    If database is set, automatically data source is created and connect
110

            
111
=head2 connect_memory
112

            
113
    # Connect memory database
114
    $self = $dbi->connect_memory;
115

            
116
=head2 reconnect_memory
117

            
118
    # Reconnect memory database
119
    $self = $dbi->reconnect_memory;
120

            
121
=head1 AUTHOR
122

            
123
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
124

            
125
Github L<http://github.com/yuki-kimoto>
126

            
127
=head1 COPYRIGHT & LICENSE
128

            
129
Copyright 2009 Yuki Kimoto, all rights reserved.
130

            
131
This program is free software; you can redistribute it and/or modify it
132
under the same terms as Perl itself.
133