Newer Older
145 lines | 2.946kb
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1
package DBIx::Custom::Model;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
13
__PACKAGE__->attr(['dbi', 'table']);
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
    if (my $method = $self->{_methods}->{$mname}) {
26
        return $self->$method(@_)
27
    }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
28
    elsif ($self->dbi->can($mname)) {
29
        $self->dbi->$mname(@_);
30
    }
31
    elsif ($self->dbi->dbh->can($mname)) {
32
        $self->dbi->dbh->$mname(@_);
33
    }
34
    else {
35
        croak qq/Can't locate object method "$mname" via "$package"/
36
    }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
37
}
38

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

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

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

            
68
1;
69

            
70
=head1 NAME
71

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

            
74
=head1 SYNOPSIS
75

            
76
use DBIx::Custom::Table;
77

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
78
my $table = DBIx::Custom::Model->new(table => 'books');
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
79

            
80
=head1 METHODS
81

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

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

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

            
93
=head2 C<delete_all>
94

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

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

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

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

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

            
120
=head2 C<new>
121

            
122
    my $table = DBIx::Custom::Table->new;
123

            
124
Create a L<DBIx::Custom::Table> object.
125

            
126
=head2 C<select>
127

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

            
133
=head2 C<update>
134

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

            
140
=head2 C<update_all>
141

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