Newer Older
374 lines | 8.934kb
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/],
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
12
    table_alias => sub { {} },
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
13
    columns => sub { [] },
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
14
    filter => sub { [] },
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
15
    result_filter => sub { [] },
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
16
    join => sub { [] },
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
17
    type => sub { [] },
updatedd pod
Yuki Kimoto authored on 2011-06-12
18
    primary_key => sub { [] };
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
19

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

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

            
25
    # Method name
26
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
27

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

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

            
49
    my $code = sub {
50
        my $self = shift;
- fixed bug that model inser...
Yuki Kimoto authored on 2011-06-10
51

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
68
sub column {
69
    my ($self, $table, $columns) = @_;
70
    
71
    $self->{_table_alias} ||= {};
72
    my $dist;
73
    $dist = $self->dbi->{_table_alias}{$table}
74
          ? $self->dbi->{_table_alias}{$table}
75
          : $table;
76
    
77
    $self->dbi->{_model_from} ||= {};
78
    my $model = $self->dbi->{_model_from}->{$dist};
79
    
80
    $columns ||= $self->model($model)->columns;
81
    
cleanup
Yuki Kimoto authored on 2011-03-21
82
    return $self->dbi->column($table, $columns);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
83
}
84

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
85
sub col {
86
    my ($self, $table, $columns) = @_;
87
    
88
    $self->{_table_alias} ||= {};
89
    my $dist;
90
    $dist = $self->dbi->{_table_alias}{$table}
91
          ? $self->dbi->{_table_alias}{$table}
92
          : $table;
93
    
94
    $self->dbi->{_model_from} ||= {};
95
    my $model = $self->dbi->{_model_from}->{$dist};
96
    
97
    $columns ||= $self->model($model)->columns;
98
    
99
    return $self->dbi->col($table, $columns);
100
}
101

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
104
sub method {
105
    my $self = shift;
106
    
107
    # Merge
108
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
109
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
110
    
111
    return $self;
112
}
113

            
cleanup
Yuki Kimoto authored on 2011-03-21
114
sub mycolumn {
115
    my $self = shift;
116
    my $table = shift unless ref $_[0];
117
    my $columns = shift;
118
    
119
    $table ||= $self->table || '';
120
    $columns ||= $self->columns;
121
    
122
    return $self->dbi->mycolumn($table, $columns);
123
}
124

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
125
sub new {
126
    my $self = shift->SUPER::new(@_);
127
    
128
    # Check attribute names
129
    my @attrs = keys %$self;
130
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
131
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
132
          unless $self->can($attr);
133
    }
134
    
135
    return $self;
136
}
137

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
138
1;
139

            
140
=head1 NAME
141

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

            
144
=head1 SYNOPSIS
145

            
146
use DBIx::Custom::Table;
147

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

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

            
152
=head2 C<dbi>
153

            
154
    my $dbi = $model->dbi;
155
    $model  = $model->dbi($dbi);
156

            
157
L<DBIx::Custom> object.
158

            
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
159
=head2 C<filter>
160

            
161
    my $dbi = $model->filter
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
162
    $model  = $model->filter(
163
        title => {out => 'tp_to_date', in => 'date_to_tp'}
164
        author => {out => ..., in => ...},
165
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
166

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
167
This filter is applied when L<DBIx::Custom>'s C<include_model()> is called.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
168

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

            
171
    my $name = $model->name;
172
    $model   = $model->name('book');
173

            
174
Model name.
175

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

            
178
    my $join = $model->join;
179
    $model   = $model->join(
180
        ['left outer join company on book.company_id = company.id']
181
    );
182
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
183
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
184

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

            
187
    my $primary_key = $model->primary_key;
188
    $model          = $model->primary_key(['id', 'number']);
189

            
190
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
191
C<delete_at()>,C<select_at()>.
192

            
updated_pod
Yuki Kimoto authored on 2011-06-12
193
=head2 C<result_filter> EXPERIMENTAL
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
194

            
195
    my $dbi = $model->result_filter
196
    $model  = $model->result_filter(
197
        title => sub { ... },
198
        author => sub { ... }
199
    );
200

            
201
This filter is applied when L<DBIx::Custom>'s C<include_model()> or C<create_model> is called.
202

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

            
205
    my $table = $model->table;
206
    $model    = $model->table('book');
207

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

            
cleanup
Yuki Kimoto authored on 2011-04-25
211
=head2 C<table_alias> EXPERIMENTAL
212

            
213
    my $table_alias = $model->table_alias;
214
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
215

            
216
Table alias. If you define table alias,
217
same filter as the table is avaliable
218
, and can write $dbi->column('user1') to get all columns.
219

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

            
222
    my $type = $model->type;
223
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
224
    
225
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
226
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
227
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
228

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

            
231
    my $view = $model->view;
232
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
233

            
234
View. This view is registered by C<view()> of L<DBIx::Custom> when
235
model is included by C<include_model>.
236

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
243
=head2 C<column> EXPERIMETNAL
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
244

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
245
    my $column = $model->column(book => ['author', 'title']);
246
    my $column = $model->column('book');
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
247

            
cleanup
Yuki Kimoto authored on 2011-03-21
248
Create column clause. The follwoing column clause is created.
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
249

            
cleanup
Yuki Kimoto authored on 2011-03-21
250
    book.author as book__author,
251
    book.title as book__title
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
252

            
cleanup
Yuki Kimoto authored on 2011-03-21
253
If column names is omitted, C<columns> attribute of the model is used.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
254

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
255
=head2 C<col> EXPERIMETNAL
256

            
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
257
    my $column = $model->col(book => ['author', 'title']);
258
    my $column = $model->col('book');
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
259

            
260
Create column clause. The follwoing column clause is created.
261

            
262
    book.author as "book.author",
263
    book.title as "book.title"
264

            
265
If column names is omitted, C<columns> attribute of the model is used.
266

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

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

            
274
=head2 C<delete_all>
275

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

            
281
=head2 C<insert>
282

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

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

            
290
    $model->method(
291
        update_or_insert => sub {
292
            my $self = shift;
293
            
294
            # ...
295
        },
296
        find_or_create   => sub {
297
            my $self = shift;
298
            
299
            # ...
300
    );
301

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

            
304
    $model->update_or_insert;
305
    $model->find_or_create;
306

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

            
309
    my $column = $self->mycolumn;
310
    my $column = $self->mycolumn(book => ['author', 'title']);
311
    my $column = $self->mycolumn(['author', 'title']);
312

            
313
Create column clause for myself. The follwoing column clause is created.
314

            
315
    book.author as author,
316
    book.title as title
317

            
318
If table name is ommited, C<table> attribute of the model is used.
319
If column names is omitted, C<columns> attribute of the model is used.
320

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

            
323
    my $table = DBIx::Custom::Table->new;
324

            
325
Create a L<DBIx::Custom::Table> object.
326

            
327
=head2 C<select>
328

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

            
334
=head2 C<update>
335

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

            
341
=head2 C<update_all>
342

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

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

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

            
355
=head2 C<select_at> DEPRECATED!
356

            
357
    $table->select_at(...);
358
    
359
Same as C<select_at()> of L<DBIx::Custom> except that
360
you don't have to specify C<table> and C<primary_key> option.
361

            
362
=head2 C<insert_at> DEPRECATED!
363

            
364
    $table->insert_at(...);
365
    
366
Same as C<insert_at()> of L<DBIx::Custom> except that
367
you don't have to specify C<table> and C<primary_key> option.
368

            
369
=head2 C<delete_at> DEPRECATED!
370

            
371
    $table->delete_at(...);
372
    
373
Same as C<delete()> of L<DBIx::Custom> except that
374
you don't have to specify C<table> and C<primary_key> option.