Newer Older
287 lines | 6.566kb
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

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
3
use Object::Simple -base;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
4

            
5
use Carp 'croak';
cleanup
Yuki Kimoto authored on 2011-04-25
6
use DBIx::Custom::Util '_subname';
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
7

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

            
updatedd pod
Yuki Kimoto authored on 2011-06-12
11
has [qw/dbi name table view/],
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
12
    columns => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
13
    join => sub { [] },
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
14
    type => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
15
    primary_key => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
16

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
17
our $AUTOLOAD;
18

            
19
sub AUTOLOAD {
20
    my $self = shift;
21

            
22
    # Method name
23
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
24

            
25
    # Method
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
26
    $self->{_methods} ||= {};
27
    if (my $method = $self->{_methods}->{$mname}) {
28
        return $self->$method(@_)
29
    }
30
    elsif (my $dbi_method = $self->dbi->can($mname)) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
31
        $self->dbi->$dbi_method(@_);
32
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
33
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
34
        $self->dbi->dbh->$dbh_method(@_);
35
    }
36
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
37
        croak qq{Can't locate object method "$mname" via "$package" }
38
            . _subname;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
39
    }
40
}
41

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
42
my @methods = qw/insert insert_at update update_at update_all
43
                 delete delete_at delete_all select select_at/;
44
foreach my $method (@methods) {
45

            
46
    my $code = sub {
47
        my $self = shift;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
48

            
added tests
Yuki Kimoto authored on 2011-06-08
49
        my @args = (
50
            table => $self->table,
51
            type => $self->type,
52
            primary_key => $self->primary_key
53
        );
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
54
        push @args, (join => $self->join) if $method =~ /^select/;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
55
        unshift @args, shift if @_ % 2;
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
56
        
57
        $self->dbi->$method(@args, @_);
58
    };
59
    
60
    no strict 'refs';
61
    my $class = __PACKAGE__;
62
    *{"${class}::$method"} = $code;
63
}
64

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
65
sub DESTROY { }
66

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
67
sub method {
68
    my $self = shift;
69
    
70
    # Merge
71
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
72
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
73
    
74
    return $self;
75
}
76

            
cleanup
Yuki Kimoto authored on 2011-03-21
77
sub mycolumn {
78
    my $self = shift;
79
    my $table = shift unless ref $_[0];
80
    my $columns = shift;
81
    
82
    $table ||= $self->table || '';
83
    $columns ||= $self->columns;
84
    
85
    return $self->dbi->mycolumn($table, $columns);
86
}
87

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
88
sub new {
89
    my $self = shift->SUPER::new(@_);
90
    
91
    # Check attribute names
92
    my @attrs = keys %$self;
93
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
94
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
95
          unless $self->can($attr);
96
    }
97
    
98
    return $self;
99
}
100

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
101
# DEPRECATED!
102
has filter => sub { [] };
103

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
104
1;
105

            
106
=head1 NAME
107

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
108
DBIx::Custom::Model - Model
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
109

            
110
=head1 SYNOPSIS
111

            
112
use DBIx::Custom::Table;
113

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

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

            
118
=head2 C<dbi>
119

            
120
    my $dbi = $model->dbi;
121
    $model  = $model->dbi($dbi);
122

            
123
L<DBIx::Custom> object.
124

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
125
=head2 C<name>
126

            
127
    my $name = $model->name;
128
    $model   = $model->name('book');
129

            
130
Model name.
131

            
update pod
Yuki Kimoto authored on 2011-03-13
132
=head2 C<join>
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
133

            
134
    my $join = $model->join;
135
    $model   = $model->join(
136
        ['left outer join company on book.company_id = company.id']
137
    );
138
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
139
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
140

            
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
141
=head2 C<primary_key>
142

            
143
    my $primary_key = $model->primary_key;
144
    $model          = $model->primary_key(['id', 'number']);
145

            
146
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
147
C<delete_at()>,C<select_at()>.
148

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
149
=head2 C<table>
150

            
151
    my $table = $model->table;
152
    $model    = $model->table('book');
153

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
154
Table name, this is used as C<select()> C<table> option.
155
Generally, this is automatically set from class name.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
156

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
157
=head2 C<type>
158

            
159
    my $type = $model->type;
160
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
161
    
162
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
163
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>,
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
164
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
165

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
166
=head2 C<view>
167

            
168
    my $view = $model->view;
169
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
170

            
171
View. This view is registered by C<view()> of L<DBIx::Custom> when
172
model is included by C<include_model>.
173

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

            
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
176
L<DBIx::Custom::Model> inherits all methods from L<Object::Simple>,
177
and you can use all methods of L<DBIx::Custom> and L<DBI>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
178
and implements the following new ones.
179

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

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

            
187
=head2 C<delete_all>
188

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

            
194
=head2 C<insert>
195

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
201
=head2 C<method>
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
202

            
203
    $model->method(
204
        update_or_insert => sub {
205
            my $self = shift;
206
            
207
            # ...
208
        },
209
        find_or_create   => sub {
210
            my $self = shift;
211
            
212
            # ...
213
    );
214

            
215
Register method. These method is called directly from L<DBIx::Custom::Model> object.
216

            
217
    $model->update_or_insert;
218
    $model->find_or_create;
219

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
220
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
221

            
222
    my $column = $self->mycolumn;
223
    my $column = $self->mycolumn(book => ['author', 'title']);
224
    my $column = $self->mycolumn(['author', 'title']);
225

            
226
Create column clause for myself. The follwoing column clause is created.
227

            
228
    book.author as author,
229
    book.title as title
230

            
231
If table name is ommited, C<table> attribute of the model is used.
232
If column names is omitted, C<columns> attribute of the model is used.
233

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
234
=head2 C<new>
235

            
236
    my $table = DBIx::Custom::Table->new;
237

            
238
Create a L<DBIx::Custom::Table> object.
239

            
240
=head2 C<select>
241

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

            
247
=head2 C<update>
248

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

            
254
=head2 C<update_all>
255

            
256
    $table->update_all(param => \%param);
257
    
258
Same as C<update_all()> of L<DBIx::Custom> except that
259
you don't have to specify table name.
update pod
Yuki Kimoto authored on 2011-02-28
260

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
261
=head2 C<update_at> DEPRECATED!
update pod
Yuki Kimoto authored on 2011-02-28
262

            
263
    $table->update_at(...);
264
    
265
Same as C<update_at()> of L<DBIx::Custom> except that
266
you don't have to specify C<table> and C<primary_key> option.
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
267

            
268
=head2 C<select_at> DEPRECATED!
269

            
270
    $table->select_at(...);
271
    
272
Same as C<select_at()> of L<DBIx::Custom> except that
273
you don't have to specify C<table> and C<primary_key> option.
274

            
275
=head2 C<insert_at> DEPRECATED!
276

            
277
    $table->insert_at(...);
278
    
279
Same as C<insert_at()> of L<DBIx::Custom> except that
280
you don't have to specify C<table> and C<primary_key> option.
281

            
282
=head2 C<delete_at> DEPRECATED!
283

            
284
    $table->delete_at(...);
285
    
286
Same as C<delete()> of L<DBIx::Custom> except that
287
you don't have to specify C<table> and C<primary_key> option.