Newer Older
302 lines | 7.457kb
added Next version
Yuki Kimoto authored on 2011-11-16
1
package DBIx::Custom::Next::Model;
2
use Object::Simple -base;
3

            
4
use Carp 'croak';
5
use DBIx::Custom::Next::Util '_subname';
6

            
7
# Carp trust relationship
8
push @DBIx::Custom::Next::CARP_NOT, __PACKAGE__;
9

            
10
has [qw/dbi table created_at updated_at bind_type join primary_key/],
11
    columns => sub { [] };
12

            
13
our $AUTOLOAD;
14

            
15
sub AUTOLOAD {
16
    my $self = shift;
17

            
18
    # Method name
19
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
20

            
21
    # Method
22
    $self->{_methods} ||= {};
23
    if (my $method = $self->{_methods}->{$mname}) {
24
        return $self->$method(@_)
25
    }
26
    elsif (my $dbi_method = $self->dbi->can($mname)) {
27
        $self->dbi->$dbi_method(@_);
28
    }
29
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
30
        $self->dbi->dbh->$dbh_method(@_);
31
    }
32
    else {
33
        croak qq{Can't locate object method "$mname" via "$package" }
34
            . _subname;
35
    }
36
}
37

            
38
my @methods = qw/insert update update_all delete delete_all select count/;
39
for my $method (@methods) {
40
    
41
    my $code =
42
         qq/sub {/ .
43
         qq/my \$self = shift;/ .
44
         qq/\$self->dbi->$method(/;
45
    
46
    $code .= qq/shift,/
47
      if $method eq  'insert' || $method eq 'update' || $method eq 'update_all';
48
    
49
    my @attrs = qw/table primary_key bind_type/;
50
    my @insert_attrs = qw/created_at updated_at/;
51
    my @update_attrs = qw/updated_at/;
52
    my @select_attrs = qw/join/;
53
    if ($method eq 'insert') { push @attrs, @insert_attrs }
54
    elsif ($method eq 'update') { push @attrs, @update_attrs }
55
    elsif (index($method, 'select') != -1) { push @attrs, @select_attrs }
56
    
57
    for my $attr (@attrs) {
58
        $code .= "exists \$self->{$attr} ? ($attr => \$self->{$attr}) : (),";
59
    }
60
    
61
    $code .= qq/\@_);/ .
62
         qq/}/;
63
    
64
    no strict 'refs';
65
    *{__PACKAGE__ . "::$method"} = eval $code;
66
    croak $code if $@;
67
}
68

            
69
sub update_or_insert {
70
    my ($self, $param, %opt) = @_;
71
    
72
    croak "update_or_insert method need primary_key and id option "
73
      unless (defined $opt{id} || defined $self->{id})
74
          && (defined $opt{primary_key} || defined $self->{primary_key});
75
    
76
    my $statement_opt = $opt{option} || {};
77
    my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
78
    if (@$rows == 0) {
79
        return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
80
    }
81
    elsif (@$rows == 1) {
82
        return $self->update($param, %opt, %{$statement_opt->{update} || {}});
83
    }
84
    else {
85
        croak "selected row must be one " . _subname;
86
    }
87
}
88

            
89
sub DESTROY { }
90

            
91
sub helper {
92
    my $self = shift;
93
    
94
    # Merge
95
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
96
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
97
    
98
    return $self;
99
}
100

            
101
sub mycolumn {
102
    my $self = shift;
103
    my $table = shift unless ref $_[0];
104
    my $columns = shift;
105
    
106
    $table ||= $self->table || '';
107
    $columns ||= $self->columns;
108
    
109
    return $self->dbi->mycolumn($table, $columns);
110
}
111

            
112
sub new {
113
    my $self = shift->SUPER::new(@_);
114
    
115
    # Check attribute names
116
    my @attrs = keys %$self;
117
    for my $attr (@attrs) {
118
        croak qq{"$attr" is invalid attribute name } . _subname
119
          unless $self->can($attr);
120
    }
121
    
122
    # Cache
123
    for my $attr (qw/dbi table created_at updated_at bind_type join primary_key/) {
124
        $self->$attr;
125
        $self->{$attr} = undef unless exists $self->{$attr};
126
    }
127
    $self->columns;
128
    
129
    return $self;
130
}
131

            
132
1;
133

            
134
=head1 NAME
135

            
136
DBIx::Custom::Next::Model - Model
137

            
138
=head1 SYNOPSIS
139

            
140
use DBIx::Custom::Next::Model;
141

            
142
my $model = DBIx::Custom::Next::Model->new(table => 'books');
143

            
144
=head1 ATTRIBUTES
145

            
146
=head2 C<dbi>
147

            
148
    my $dbi = $model->dbi;
149
    $model = $model->dbi($dbi);
150

            
151
L<DBIx::Custom::Next> object.
152

            
153
=head2 C<created_at EXPERIMENTAL>
154

            
155
    my $created_at = $model->created_at;
156
    $model = $model->created_at('created_datatime');
157

            
158
Create timestamp column, this is passed to C<insert> or C<update> method.
159

            
160
=head2 C<join>
161

            
162
    my $join = $model->join;
163
    $model = $model->join(
164
        ['left outer join company on book.company_id = company.id']
165
    );
166
    
167
Join clause, this value is passed to C<select> method.
168

            
169
=head2 C<primary_key>
170

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

            
174
Primary key,this is passed to C<insert>, C<update>,
175
C<delete>, and C<select> method.
176

            
177
=head2 C<table>
178

            
179
    my $model = $model->table;
180
    $model = $model->table('book');
181

            
182
Table name, this is passed to C<select> method.
183

            
184
=head2 C<bind_type>
185

            
186
    my $type = $model->bind_type;
187
    $model = $model->bind_type(['image' => DBI::SQL_BLOB]);
188
    
189
Database data type, this is used as type optioon of C<insert>, 
190
C<update>, C<update_all>, C<delete>, C<delete_all>,
191
and C<select> method
192

            
193
=head2 C<updated_at EXPERIMENTAL>
194

            
195
    my $updated_at = $model->updated_at;
196
    $model = $model->updated_at('updated_datatime');
197

            
198
Updated timestamp column, this is passed to C<update> method.
199

            
200
=head1 METHODS
201

            
202
L<DBIx::Custom::Next::Model> inherits all methods from L<Object::Simple>,
203
and you can use all methods of L<DBIx::Custom::Next> and L<DBI>
204
and implements the following new ones.
205

            
206
=head2 C<count>
207

            
208
    my $count = $model->count;
209

            
210
Get rows count.
211

            
212
Options is same as C<select> method's ones.
213

            
214
=head2 C<delete>
215

            
216
    $model->delete(...);
217
    
218
Same as C<delete> of L<DBIx::Custom::Next> except that
219
you don't have to specify options if you set attribute in model.
220

            
221
=head2 C<delete_all>
222

            
223
    $model->delete_all(...);
224
    
225
Same as C<delete_all> of L<DBIx::Custom::Next> except that
226
you don't have to specify options if you set attribute in model.
227

            
228
=head2 C<insert>
229

            
230
    $model->insert(...);
231
    
232
Same as C<insert> of L<DBIx::Custom::Next> except that
233
you don't have to specify options if you set attribute in model.
234

            
235
=head2 C<helper>
236

            
237
    $model->helper(
238
        update_or_insert => sub {
239
            my $self = shift;
240
            
241
            # ...
242
        },
243
        find_or_create   => sub {
244
            my $self = shift;
245
            
246
            # ...
247
    );
248

            
249
Register helper. These helper is called directly from L<DBIx::Custom::Next::Model> object.
250

            
251
    $model->update_or_insert;
252
    $model->find_or_create;
253

            
254
=head2 C<mycolumn>
255

            
256
    my $column = $self->mycolumn;
257
    my $column = $self->mycolumn(book => ['author', 'title']);
258
    my $column = $self->mycolumn(['author', 'title']);
259

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

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

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

            
268
=head2 C<new>
269

            
270
    my $model = DBIx::Custom::Next::Model->new;
271

            
272
Create a L<DBIx::Custom::Next::Model> object.
273

            
274
=head2 C<select>
275

            
276
    $model->select(...);
277
    
278
Same as C<select> of L<DBIx::Custom::Next> except that
279
you don't have to specify options if you set attribute in model.
280

            
281
=head2 C<update>
282

            
283
    $model->update(...);
284
    
285
Same as C<update> of L<DBIx::Custom::Next> except that
286
you don't have to specify options if you set attribute in model.
287

            
288
=head2 C<update_all>
289

            
290
    $model->update_all(param => \%param);
291
    
292
Same as C<update_all> of L<DBIx::Custom::Next> except that
293
you don't have to specify options if you set attribute in model.
294

            
295
=head2 C<update_or_insert>
296

            
297
    $model->update_or_insert(...);
298
    
299
Same as C<update> of L<DBIx::Custom::Next> except that
300
you don't have to specify options if you set attribute in model.
301

            
302
=cut