Newer Older
147 lines | 2.944kb
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

            
cleanup
Yuki Kimoto authored on 2011-01-23
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} ||= {};
25
    croak qq/Can't locate object method "$mname" via "$package"/
26
      unless my $method = $self->{_methods}->{$mname};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
27

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
28
    # Execute
29
    return $self->$method(@_);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
30
}
31

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

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

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

            
61
1;
62

            
63
=head1 NAME
64

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

            
67
=head1 SYNOPSIS
68

            
69
use DBIx::Custom::Table;
70

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

            
73
=head1 METHODS
74

            
75
L<DBIx::Custom> inherits all methods from L<Object::Simple>
76
and implements the following new ones.
77

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

            
80
    $table->delete(where => \%where);
81
    
82
Same as C<delete()> of L<DBIx::Custom> except that
83
you don't have to specify table name.
84

            
85
=head2 C<delete_all>
86

            
87
    $table->delete_all(param => $param);
88
    
89
Same as C<delete_all()> of L<DBIx::Custom> except that
90
you don't have to specify table name.
91

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
94
    $table->method(insert => sub {
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
95
        my $self = shift;
96
        
97
        return $self->dbi->insert(table => $self->name, @_);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
98
    });
99
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
100
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
101

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

            
104
    $table->insert(param => \%param);
105
    
106
Same as C<insert()> of L<DBIx::Custom> except that
107
you don't have to specify table name.
108

            
109
=head2 C<method>
110

            
111
    $table->method(
112
        select_complex => sub {
113
            my $self = shift;
114
            
115
            return $self->dbi->select($self->name, ...);
116
        },
117
        some_method => sub { ... }
118
    );
119

            
120
Define method.
121

            
122
=head2 C<new>
123

            
124
    my $table = DBIx::Custom::Table->new;
125

            
126
Create a L<DBIx::Custom::Table> object.
127

            
128
=head2 C<select>
129

            
130
    $table->select(param => $param);
131
    
132
Same as C<select()> of L<DBIx::Custom> except that
133
you don't have to specify table name.
134

            
135
=head2 C<update>
136

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

            
142
=head2 C<update_all>
143

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