Newer Older
136 lines | 3.18kb
packaging one directory
yuki-kimoto authored on 2009-11-16
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

            
53

            
54
=head1 NAME
55

            
56
DBIx::Custom::SQLite - DBIx::Custom SQLite implementation
57

            
58
=head1 Version
59

            
60
Version 0.0201
61

            
62
=head1 Synopsys
63

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

            
93
=head1 See DBIx::Custom and DBI::Custom::Basic documentation
94

            
95
This class is L<DBIx::Custom::Basic> subclass.
96
and L<DBIx::Custom::Basic> is L<DBIx::Custom> subclass
97

            
98
You can use all methods of L<DBIx::Custom::Basic> and <DBIx::Custom>
99
Please see L<DBIx::Custom::Basic> and <DBIx::Custom> documentation
100

            
101
=head1 Object methods
102

            
103
=head2 connect
104

            
105
This override L<DBIx::Custom> connect.
106

            
107
    # Connect to database
108
    $dbi->connect;
109

            
110
If database attribute is set, automatically data source is created and connect
111

            
112
=head2 connect_memory
113

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

            
117
=head2 reconnect_memory
118

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

            
122
=head1 Author
123

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

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

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

            
130
=head1 Copyright & lisence
131

            
132
Copyright 2009 Yuki Kimoto, all rights reserved.
133

            
134
This program is free software; you can redistribute it and/or modify it
135
under the same terms as Perl itself.
136