Newer Older
227 lines | 4.353kb
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

            
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
54
sub insert {
55
    my $self = shift;
56
    $self->dbi->insert(table => $self->table, @_);
57
}
58

            
59
sub update {
60
    my $self = shift;
61
    $self->dbi->update(table => $self->table, @_)
62
}
63

            
64
sub update_all {
65
    my $self = shift;
66
    $self->dbi->update_all(table => $self->table, @_);
67
}
68

            
69
sub delete {
70
    my $self = shift;
71
    $self->dbi->delete(table => $self->table, @_);
72
}
73

            
74
sub delete_all {
75
    my $self = shift;
76
    $self->dbi->delete_all(table => $self->table, @_);
77
}
78

            
79
sub select {
80
    my $self = shift;
81
    $self->dbi->select(
82
        table => $self->table,
83
        relation => $self->relation,
84
        @_
85
    );
86
}
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
87

            
88
sub update_at {
89
    my $self = shift;
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
90
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
91
    return $self->dbi->update_at(
92
        table => $self->table,
93
        primary_key => $self->primary_key,
94
        @_
95
    );
96
}
97

            
98
sub delete_at {
99
    my $self = shift;
100
    
101
    return $self->dbi->delete_at(
102
        table => $self->table,
103
        primary_key => $self->primary_key,
104
        @_
105
    );
106
}
107

            
108
sub select_at {
109
    my $self = shift;
many changed
Yuki Kimoto authored on 2011-01-23
110
    
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
111
    return $self->dbi->select_at(
112
        table => $self->table,
113
        primary_key => $self->primary_key,
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
114
        relation => $self->relation,
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
115
        @_
116
    );
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
117
}
118

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

            
121
1;
122

            
123
=head1 NAME
124

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

            
127
=head1 SYNOPSIS
128

            
129
use DBIx::Custom::Table;
130

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

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

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

            
137
    my $columns = $model->columns;
138
    $model      = $model->columns(['id', 'number']);
139

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

            
142
    my $dbi = $model->dbi;
143
    $model  = $model->dbi($dbi);
144

            
145
L<DBIx::Custom> object.
146

            
147
=head2 C<table>
148

            
149
    my $table = $model->table;
150
    $model    = $model->table('book');
151

            
152
Table name.
153
    
154
=head2 C<primary_key>
155

            
156
    my $primary_key = $model->primary_key;
157
    $model          = $model->primary_key(['id', 'number']);
158

            
159
Foreign key. This is used by C<update_at()>, C<delete_at()>,
160
C<select_at()>.
161

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

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

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
170
    $table->delete(...);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
171
    
172
Same as C<delete()> 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<delete_all>
176

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

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

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
184
    $table->method(
185
        count => sub {
186
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
187
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
188
            return $self->select(column => 'count(*)', @_)
189
                        ->fetch_first->[0];
190
        }
191
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
192
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
193
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
194

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

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

            
202
=head2 C<new>
203

            
204
    my $table = DBIx::Custom::Table->new;
205

            
206
Create a L<DBIx::Custom::Table> object.
207

            
208
=head2 C<select>
209

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

            
215
=head2 C<update>
216

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

            
222
=head2 C<update_all>
223

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