Newer Older
113 lines | 2.533kb
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 See DBIx::Custom documentation
43

            
44
This class is L<DBIx::Custom> subclass.
45

            
46
You can use all methods of L<DBIx::Custom>
47

            
48
Please see L<DBIx::Custom> documentation
49

            
50
=head1 Filters
51

            
52
=head2 encode_utf8
53

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

            
59
    $dbi->bind_filter($dbi->filters->{encode_utf8});
60

            
61
=head2 decode_utf8
62

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

            
68
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
69

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

            
82
You get format as the following
83

            
84
    my $format = $dbi->formats->{$format_name};
85

            
86
=head1 Methods
87

            
88
=head2 utf8_filter_on
89

            
90
    # Encode and decode utf8 filter on
91
    $dbi->utf8_filter_on;
92

            
93
This equel to
94

            
95
    $dbi->bind_filter($dbi->filters->{encode_utf8});
96
    $dbi->fetch_filter($dbi->filters->{decode_utf8});
97

            
98
=head1 AUTHOR
99

            
100
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
101

            
102
Github L<http://github.com/yuki-kimoto>
103

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

            
106
=head1 COPYRIGHT & LICENSE
107

            
108
Copyright 2009 Yuki Kimoto, all rights reserved.
109

            
110
This program is free software; you can redistribute it and/or modify it
111
under the same terms as Perl itself.
112

            
113
=cut