Newer Older
345 lines | 8.19kb
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

            
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
231
Create column clause. The follwoing column clause is created.
232

            
233
    book.author as "book.author",
234
    book.title as "book.title"
235

            
cleanup
Yuki Kimoto authored on 2011-06-13
236
If Second argument is ommited, all columns set by C<columns> is used.
added EXPERIMENTAL col metho...
Yuki Kimoto authored on 2011-06-08
237

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

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

            
245
=head2 C<delete_all>
246

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

            
252
=head2 C<insert>
253

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

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

            
261
    $model->method(
262
        update_or_insert => sub {
263
            my $self = shift;
264
            
265
            # ...
266
        },
267
        find_or_create   => sub {
268
            my $self = shift;
269
            
270
            # ...
271
    );
272

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

            
275
    $model->update_or_insert;
276
    $model->find_or_create;
277

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

            
280
    my $column = $self->mycolumn;
281
    my $column = $self->mycolumn(book => ['author', 'title']);
282
    my $column = $self->mycolumn(['author', 'title']);
283

            
284
Create column clause for myself. The follwoing column clause is created.
285

            
286
    book.author as author,
287
    book.title as title
288

            
289
If table name is ommited, C<table> attribute of the model is used.
290
If column names is omitted, C<columns> attribute of the model is used.
291

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

            
294
    my $table = DBIx::Custom::Table->new;
295

            
296
Create a L<DBIx::Custom::Table> object.
297

            
298
=head2 C<select>
299

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

            
305
=head2 C<update>
306

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

            
312
=head2 C<update_all>
313

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

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

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

            
326
=head2 C<select_at> DEPRECATED!
327

            
328
    $table->select_at(...);
329
    
330
Same as C<select_at()> of L<DBIx::Custom> except that
331
you don't have to specify C<table> and C<primary_key> option.
332

            
333
=head2 C<insert_at> DEPRECATED!
334

            
335
    $table->insert_at(...);
336
    
337
Same as C<insert_at()> of L<DBIx::Custom> except that
338
you don't have to specify C<table> and C<primary_key> option.
339

            
340
=head2 C<delete_at> DEPRECATED!
341

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