Newer Older
200 lines | 4.233kb
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 DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
13
__PACKAGE__->attr(
14
    ['dbi', 'table'],
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
15
    columns => sub { [] },
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
16
    primary_key => sub { [] },
17
    relation => sub { {} }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
18
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
19

            
20
our $AUTOLOAD;
21

            
22
sub AUTOLOAD {
23
    my $self = shift;
24

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
28
    # Method
29
    $self->{_methods} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
30
    if (my $method = $self->{_methods}->{$mname}) {
31
        return $self->$method(@_)
32
    }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
33
    elsif ($self->dbi->can($mname)) {
34
        $self->dbi->$mname(@_);
35
    }
36
    elsif ($self->dbi->dbh->can($mname)) {
37
        $self->dbi->dbh->$mname(@_);
38
    }
39
    else {
40
        croak qq/Can't locate object method "$mname" via "$package"/
41
    }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
42
}
43

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

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
54
sub insert     { my $self = shift; $self->dbi->insert(table => $self->table, @_) }
55
sub update     { my $self = shift; $self->dbi->update(table => $self->table, @_) }
56
sub update_all { my $self = shift; $self->dbi->update_all(table => $self->table, @_) }
57
sub delete     { my $self = shift; $self->dbi->delete(table => $self->table, @_) }
58
sub delete_all { my $self = shift; $self->dbi->delete_all(table => $self->table, @_) }
59
sub select     { my $self = shift; $self->dbi->select(table => $self->table, @_) }
60

            
61
sub update_at {
62
    my $self = shift;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
63
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
64
    return $self->dbi->update_at(
65
        table => $self->table,
66
        primary_key => $self->primary_key,
67
        @_
68
    );
69
}
70

            
71
sub delete_at {
72
    my $self = shift;
73
    
74
    return $self->dbi->delete_at(
75
        table => $self->table,
76
        primary_key => $self->primary_key,
77
        @_
78
    );
79
}
80

            
81
sub select_at {
82
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
83
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
84
    return $self->dbi->select_at(
85
        table => $self->table,
86
        primary_key => $self->primary_key,
87
        @_
88
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
89
    return $self;
90
}
91

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

            
94
1;
95

            
96
=head1 NAME
97

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
98
DBIx::Custom::Model - Model (experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
99

            
100
=head1 SYNOPSIS
101

            
102
use DBIx::Custom::Table;
103

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

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
106
=head1 ATTRIBUTES
107

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
108
=head2 C<(experimental) columns>
109

            
110
    my $columns = $model->columns;
111
    $model      = $model->columns(['id', 'number']);
112

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
113
=head2 C<dbi>
114

            
115
    my $dbi = $model->dbi;
116
    $model  = $model->dbi($dbi);
117

            
118
L<DBIx::Custom> object.
119

            
120
=head2 C<table>
121

            
122
    my $table = $model->table;
123
    $model    = $model->table('book');
124

            
125
Table name.
126
    
127
=head2 C<primary_key>
128

            
129
    my $primary_key = $model->primary_key;
130
    $model          = $model->primary_key(['id', 'number']);
131

            
132
Foreign key. This is used by C<update_at()>, C<delete_at()>,
133
C<select_at()>.
134

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
135
=head1 METHODS
136

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

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

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

            
148
=head2 C<delete_all>
149

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

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
157
    $table->method(
158
        count => sub {
159
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
160
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
161
            return $self->select(column => 'count(*)', @_)
162
                        ->fetch_first->[0];
163
        }
164
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
165
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
166
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
167

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

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

            
175
=head2 C<new>
176

            
177
    my $table = DBIx::Custom::Table->new;
178

            
179
Create a L<DBIx::Custom::Table> object.
180

            
181
=head2 C<select>
182

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

            
188
=head2 C<update>
189

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

            
195
=head2 C<update_all>
196

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