Newer Older
141 lines | 2.77kb
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
1
package DBIx::Custom::Table;
2

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
10
# Carp trust relationship
11
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
12

            
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
13
__PACKAGE__->attr(['dbi', 'name']);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
14

            
15
our $AUTOLOAD;
16

            
17
sub AUTOLOAD {
18
    my $self = shift;
19

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
20
    # Method name
21
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
22

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
23
    # Method
24
    $self->{_methods} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
25
    
26
    # Method
27
    if (my $method = $self->{_methods}->{$mname}) {
28
        return $self->$method(@_)
29
    }
30
    
31
    # DBI method
32
    return $self->dbi->$mname(@_);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
33
}
34

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
35
sub method {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
36
    my $self = shift;
37
    
38
    # Merge
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
39
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
40
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
41
    
42
    return $self;
43
}
44

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
45
sub new {
46
    my $self = shift->SUPER::new(@_);
47
    
many changed
Yuki Kimoto authored on 2011-01-23
48
    # Methods
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
49
    my @methods = qw/insert update update_all delete delete_all select/;
50
    foreach my $method (@methods) {
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
51
        $self->method(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
52
            $method => sub {
53
                my $self = shift;
54
                return $self->dbi->$method(table => $self->name, @_);
55
            }
56
        );
57
    }
many changed
Yuki Kimoto authored on 2011-01-23
58
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
59
    return $self;
60
}
61

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
62
sub DESTROY { }
63

            
64
1;
65

            
66
=head1 NAME
67

            
add examples
Yuki Kimoto authored on 2011-01-07
68
DBIx::Custom::Table - Table base class(experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
69

            
70
=head1 SYNOPSIS
71

            
72
use DBIx::Custom::Table;
73

            
74
my $table = DBIx::Custom::Table->new(name => 'books');
75

            
76
=head1 METHODS
77

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
78
L<DBIx::Custom> inherits all methods from L<Object::Simple>,
79
and you can use all methods of the object set to C<dbi>.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
80
and implements the following new ones.
81

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
82
=head2 C<delete>
83

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
84
    $table->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
85
    
86
Same as C<delete()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
87
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
88

            
89
=head2 C<delete_all>
90

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
91
    $table->delete_all(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
92
    
93
Same as C<delete_all()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
94
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
95

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
96
=head2 C<method>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
97

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
98
    $table->method(
99
        count => sub {
100
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
101
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
102
            return $self->select(column => 'count(*)', @_)
103
                        ->fetch_first->[0];
104
        }
105
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
106
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
107
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
108

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
109
=head2 C<insert>
110

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
111
    $table->insert(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
112
    
113
Same as C<insert()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
114
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
115

            
116
=head2 C<new>
117

            
118
    my $table = DBIx::Custom::Table->new;
119

            
120
Create a L<DBIx::Custom::Table> object.
121

            
122
=head2 C<select>
123

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
124
    $table->select(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
125
    
126
Same as C<select()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
127
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
128

            
129
=head2 C<update>
130

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
131
    $table->update(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
132
    
133
Same as C<update()> of L<DBIx::Custom> except that
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
134
you don't have to specify C<table> option.
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
135

            
136
=head2 C<update_all>
137

            
138
    $table->update_all(param => \%param);
139
    
140
Same as C<update_all()> of L<DBIx::Custom> except that
141
you don't have to specify table name.