Newer Older
117 lines | 2.565kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::Basic;
2
use 5.008001;
3
use base 'DBIx::Custom';
4
use Encode qw/decode encode/;
5

            
6
use warnings;
7
use strict;
8

            
9
my $class = __PACKAGE__;
10

            
11
$class->add_filter(
12
    encode_utf8 => sub {
13
        my $value = shift;
14
        utf8::upgrade($value) unless Encode::is_utf8($value);
15
        return encode('UTF-8', $value);
16
    },
17
    decode_utf8 => sub { decode('UTF-8', shift) }
18
);
19

            
20
$class->add_format(
21
    'SQL99_date'        => '%Y-%m-%d',
22
    'SQL99_datetime'    => '%Y-%m-%d %H:%M:%S',
23
    'SQL99_time'        => '%H:%M:%S',
24
    'ISO-8601_date'     => '%Y-%m-%d',
25
    'ISO-8601_datetime' => '%Y-%m-%dT%H:%M:%S',
26
    'ISO-8601_time'     => '%H:%M:%S',
27
);
28

            
29
# Methods
30
sub utf8_filter_on {
31
    my $self = shift;
32
    $self->bind_filter($self->filters->{encode_utf8});
33
    $self->fetch_filter($self->filters->{decode_utf8});
34
}
35

            
36
1;
37

            
38
=head1 NAME
39

            
40
DBIx::Custom::Basic - DBIx::Custom basic implementation
41

            
42
=head1 Version
43

            
44
Version 0.0201
45

            
46
=head1 See DBIx::Custom documentation
47

            
48
This class is L<DBIx::Custom> subclass.
49

            
50
You can use all methods of L<DBIx::Custom>
51

            
52
Please see L<DBIx::Custom> documentation
53

            
54
=head1 Filters
55

            
56
=head2 encode_utf8
57

            
58
    # Encode to UTF-8 byte stream (utf8::upgrade is done if need)
59
    $dbi->filters->{encode_utf8}->($value);
60
    
61
This filter is generally used as bind filter
62

            
63
    $dbi->bind_filter($dbi->filters->{encode_utf8});
64

            
65
=head2 decode_utf8
66

            
67
    # Decode to perl internal string
68
    $dbi->filters->{decode_utf8}->($value);
69
    
70
This filter is generally used as fetch filter
71

            
72
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
73

            
74
=head2 Formats
75
    
76
strptime formats is available
77
    
78
    # format name        format
79
    'SQL99_date'         '%Y-%m-%d',
80
    'SQL99_datetime'     '%Y-%m-%d %H:%M:%S',
81
    'SQL99_time'         '%H:%M:%S',
82
    'ISO-8601_date'      '%Y-%m-%d',
83
    'ISO-8601_datetime'  '%Y-%m-%dT%H:%M:%S',
84
    'ISO-8601_time'      '%H:%M:%S',
85

            
86
You get format as the following
87

            
88
    my $format = $dbi->formats->{$format_name};
89

            
90
=head1 Methods
91

            
92
=head2 utf8_filter_on
93

            
94
    # Encode and decode utf8 filter on
95
    $dbi->utf8_filter_on;
96

            
97
This equel to
98

            
99
    $dbi->bind_filter($dbi->filters->{encode_utf8});
100
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
101

            
102
=head1 AUTHOR
103

            
104
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
105

            
106
Github L<http://github.com/yuki-kimoto>
107

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

            
110
=head1 COPYRIGHT & LICENSE
111

            
112
Copyright 2009 Yuki Kimoto, all rights reserved.
113

            
114
This program is free software; you can redistribute it and/or modify it
115
under the same terms as Perl itself.
116

            
117
=cut