Newer Older
357 lines | 8.516kb
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

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
87
sub method {
88
    my $self = shift;
89
    
90
    # Merge
91
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
92
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
93
    
94
    return $self;
95
}
96

            
cleanup
Yuki Kimoto authored on 2011-03-21
97
sub mycolumn {
98
    my $self = shift;
99
    my $table = shift unless ref $_[0];
100
    my $columns = shift;
101
    
102
    $table ||= $self->table || '';
103
    $columns ||= $self->columns;
104
    
105
    return $self->dbi->mycolumn($table, $columns);
106
}
107

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
108
sub new {
109
    my $self = shift->SUPER::new(@_);
110
    
111
    # Check attribute names
112
    my @attrs = keys %$self;
113
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-25
114
        croak qq{"$attr" is invalid attribute name } . _subname
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
115
          unless $self->can($attr);
116
    }
117
    
118
    return $self;
119
}
120

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
121
1;
122

            
123
=head1 NAME
124

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
125
DBIx::Custom::Model - Model
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

            
135
=head2 C<dbi>
136

            
137
    my $dbi = $model->dbi;
138
    $model  = $model->dbi($dbi);
139

            
140
L<DBIx::Custom> object.
141

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

            
144
    my $dbi = $model->filter
added DBIx::Custom result_fi...
Yuki Kimoto authored on 2011-06-12
145
    $model  = $model->filter(
146
        title => {out => 'tp_to_date', in => 'date_to_tp'}
147
        author => {out => ..., in => ...},
148
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
149

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
150
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
151

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

            
154
    my $name = $model->name;
155
    $model   = $model->name('book');
156

            
157
Model name.
158

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

            
161
    my $join = $model->join;
162
    $model   = $model->join(
163
        ['left outer join company on book.company_id = company.id']
164
    );
165
    
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
166
Join clause, this is used as C<select()>'s C<join> option.
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
167

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

            
170
    my $primary_key = $model->primary_key;
171
    $model          = $model->primary_key(['id', 'number']);
172

            
173
Foreign key, this is used as C<primary_key> of C<insert_at>,C<update_at()>,
174
C<delete_at()>,C<select_at()>.
175

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

            
178
    my $dbi = $model->result_filter
179
    $model  = $model->result_filter(
180
        title => sub { ... },
181
        author => sub { ... }
182
    );
183

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

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

            
188
    my $table = $model->table;
189
    $model    = $model->table('book');
190

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

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

            
196
    my $table_alias = $model->table_alias;
197
    $model = $model->table_alias(user1 => 'user', user2 => 'user');
198

            
199
Table alias. If you define table alias,
200
same filter as the table is avaliable
201
, and can write $dbi->column('user1') to get all columns.
202

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

            
205
    my $type = $model->type;
206
    $model   = $model->type(['image' => DBI::SQL_BLOB]);
207
    
208
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>,
209
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
210
C<select()>, C<select_at()>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
211

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

            
214
    my $view = $model->view;
215
    $model   = $model->view('select id, DATE(issue_datetime) as date from book');
216

            
217
View. This view is registered by C<view()> of L<DBIx::Custom> when
218
model is included by C<include_model>.
219

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

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

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

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

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

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

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

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

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

            
243
Create column clause. The follwoing column clause is created.
244

            
245
    book.author as "book.author",
246
    book.title as "book.title"
247

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

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

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

            
257
=head2 C<delete_all>
258

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

            
264
=head2 C<insert>
265

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

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

            
273
    $model->method(
274
        update_or_insert => sub {
275
            my $self = shift;
276
            
277
            # ...
278
        },
279
        find_or_create   => sub {
280
            my $self = shift;
281
            
282
            # ...
283
    );
284

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

            
287
    $model->update_or_insert;
288
    $model->find_or_create;
289

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

            
292
    my $column = $self->mycolumn;
293
    my $column = $self->mycolumn(book => ['author', 'title']);
294
    my $column = $self->mycolumn(['author', 'title']);
295

            
296
Create column clause for myself. The follwoing column clause is created.
297

            
298
    book.author as author,
299
    book.title as title
300

            
301
If table name is ommited, C<table> attribute of the model is used.
302
If column names is omitted, C<columns> attribute of the model is used.
303

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

            
306
    my $table = DBIx::Custom::Table->new;
307

            
308
Create a L<DBIx::Custom::Table> object.
309

            
310
=head2 C<select>
311

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

            
317
=head2 C<update>
318

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

            
324
=head2 C<update_all>
325

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

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

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

            
338
=head2 C<select_at> DEPRECATED!
339

            
340
    $table->select_at(...);
341
    
342
Same as C<select_at()> of L<DBIx::Custom> except that
343
you don't have to specify C<table> and C<primary_key> option.
344

            
345
=head2 C<insert_at> DEPRECATED!
346

            
347
    $table->insert_at(...);
348
    
349
Same as C<insert_at()> of L<DBIx::Custom> except that
350
you don't have to specify C<table> and C<primary_key> option.
351

            
352
=head2 C<delete_at> DEPRECATED!
353

            
354
    $table->delete_at(...);
355
    
356
Same as C<delete()> of L<DBIx::Custom> except that
357
you don't have to specify C<table> and C<primary_key> option.