Newer Older
109 lines | 2.346kb
packing
yuki-kimoto authored on 2009-11-12
1
package DBIx::Custom::Basic;
filter argument exchange key...
yuki-kimoto authored on 2009-11-12
2
use 5.008001;
packing
yuki-kimoto authored on 2009-11-12
3
use base 'DBIx::Custom';
4
use Encode qw/decode encode/;
5

            
6
use warnings;
7
use strict;
8

            
filter argument exchange key...
yuki-kimoto authored on 2009-11-12
9
our $VERSION = '0.0201';
packing
yuki-kimoto authored on 2009-11-12
10

            
11
my $class = __PACKAGE__;
12

            
13
$class->add_filter(
filter argument exchange key...
yuki-kimoto authored on 2009-11-12
14
    encode_utf8 => sub {
15
        my $value = shift;
16
        utf8::upgrade($value) unless Encode::is_utf8($value);
17
        return encode('UTF-8', $value);
18
    },
19
    decode_utf8 => sub { decode('UTF-8', shift) }
packing
yuki-kimoto authored on 2009-11-12
20
);
21

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

            
filter argument exchange key...
yuki-kimoto authored on 2009-11-12
31
# Methods
32
sub utf8_filter_on {
33
    my $self = shift;
34
    $self->bind_filter($self->filters->{encode_utf8});
35
    $self->fetch_filter($self->filters->{decode_utf8});
36
}
37

            
packing
yuki-kimoto authored on 2009-11-12
38
1;
39

            
40
=head1 NAME
41

            
42
DBIx::Custom::Basic - DBIx::Custom basic class
43

            
44
=head1 VERSION
45

            
filter argument exchange key...
yuki-kimoto authored on 2009-11-12
46
Version 0.0201
47

            
48
=head1 FILTERS
49

            
50
=head2 encode_utf8
51

            
52
    # Encode to UTF-8 byte stream (utf8::upgrade is done if need)
53
    $dbi->filters->{encode_utf8}->($value);
54
    
55
This filter is generally used as bind filter
56

            
57
    $dbi->bind_filter($dbi->filters->{encode_utf8});
58

            
59
=head2 decode_utf8
60

            
61
    # Decode to perl internal string
62
    $dbi->filters->{decode_utf8}->($value);
63
    
64
This filter is generally used as fetch filter
65

            
66
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
67

            
68
=head2 FORMATS
69
    
70
strptime formats is available
71
    
72
    # format name        format
73
    'SQL99_date'         '%Y-%m-%d',
74
    'SQL99_datetime'     '%Y-%m-%d %H:%M:%S',
75
    'SQL99_time'         '%H:%M:%S',
76
    'ISO-8601_date'      '%Y-%m-%d',
77
    'ISO-8601_datetime'  '%Y-%m-%dT%H:%M:%S',
78
    'ISO-8601_time'      '%H:%M:%S',
79

            
80
You get format as the following
81

            
82
    my $format = $dbi->formats->{$format_name};
83

            
84
=head1 METHOD
85

            
86
=head2 utf8_filter_on
87

            
88
    # Encode and decode utf8 filter on
89
    $dbi->utf8_filter_on;
90

            
91
This equel to
92

            
93
    $dbi->bind_filter($dbi->filters->{encode_utf8});
94
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
packing
yuki-kimoto authored on 2009-11-12
95

            
96
=head1 AUTHOR
97

            
98
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
99

            
100
Github L<http://github.com/yuki-kimoto>
101

            
102
=head1 COPYRIGHT & LICENSE
103

            
104
Copyright 2009 Yuki Kimoto, all rights reserved.
105

            
106
This program is free software; you can redistribute it and/or modify it
107
under the same terms as Perl itself.
108

            
109
=cut