Newer Older
179 lines | 3.582kb
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

            
3
use strict;
4
use warnings;
5

            
6
use base 'Object::Simple';
7

            
8
use Carp 'croak';
9

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

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
13
__PACKAGE__->attr(
14
    ['dbi', 'table'],
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
15
    columns => sub { [] },
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
16
    primary_key => sub { [] },
17
    relation => sub { {} }
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
18
);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
19

            
20
our $AUTOLOAD;
21

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
25
    # Method name
26
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
27

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
28
    # Method
29
    $self->{_methods} ||= {};
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
30
    if (my $method = $self->{_methods}->{$mname}) {
31
        return $self->$method(@_)
32
    }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
33
    elsif ($self->dbi->can($mname)) {
34
        $self->dbi->$mname(@_);
35
    }
36
    elsif ($self->dbi->dbh->can($mname)) {
37
        $self->dbi->dbh->$mname(@_);
38
    }
39
    else {
40
        croak qq/Can't locate object method "$mname" via "$package"/
41
    }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
42
}
43

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
44
sub method {
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
45
    my $self = shift;
46
    
47
    # Merge
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
48
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
49
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
50
    
51
    return $self;
52
}
53

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
54
sub new {
55
    my $self = shift->SUPER::new(@_);
56
    
many changed
Yuki Kimoto authored on 2011-01-23
57
    # Methods
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
58
    my @methods = qw/insert update update_all delete delete_all select/;
59
    foreach my $method (@methods) {
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
60
        $self->method(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
61
            $method => sub {
62
                my $self = shift;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
63
                return $self->dbi->$method(table => $self->table, @_);
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
64
            }
65
        );
66
    }
many changed
Yuki Kimoto authored on 2011-01-23
67
    
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
68
    return $self;
69
}
70

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
71
sub DESTROY { }
72

            
73
1;
74

            
75
=head1 NAME
76

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
77
DBIx::Custom::Model - Model (experimental)
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
78

            
79
=head1 SYNOPSIS
80

            
81
use DBIx::Custom::Table;
82

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

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

            
add DBIx::Custom::Model colu...
Yuki Kimoto authored on 2011-02-21
87
=head2 C<(experimental) columns>
88

            
89
    my $columns = $model->columns;
90
    $model      = $model->columns(['id', 'number']);
91

            
add DBIx::Custom::Model fore...
Yuki Kimoto authored on 2011-02-21
92
=head2 C<dbi>
93

            
94
    my $dbi = $model->dbi;
95
    $model  = $model->dbi($dbi);
96

            
97
L<DBIx::Custom> object.
98

            
99
=head2 C<table>
100

            
101
    my $table = $model->table;
102
    $model    = $model->table('book');
103

            
104
Table name.
105
    
106
=head2 C<primary_key>
107

            
108
    my $primary_key = $model->primary_key;
109
    $model          = $model->primary_key(['id', 'number']);
110

            
111
Foreign key. This is used by C<update_at()>, C<delete_at()>,
112
C<select_at()>.
113

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

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

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

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

            
127
=head2 C<delete_all>
128

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

            
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
134
=head2 C<method>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
135

            
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
136
    $table->method(
137
        count => sub {
138
            my $self = shift;
simplified DBIx::Custom::Mod...
Yuki Kimoto authored on 2011-01-02
139
        
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
140
            return $self->select(column => 'count(*)', @_)
141
                        ->fetch_first->[0];
142
        }
143
    );
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
144
    
renamed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-25
145
Add method to a L<DBIx::Custom::Table> object.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-01
146

            
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
147
=head2 C<insert>
148

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

            
154
=head2 C<new>
155

            
156
    my $table = DBIx::Custom::Table->new;
157

            
158
Create a L<DBIx::Custom::Table> object.
159

            
160
=head2 C<select>
161

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

            
167
=head2 C<update>
168

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

            
174
=head2 C<update_all>
175

            
176
    $table->update_all(param => \%param);
177
    
178
Same as C<update_all()> of L<DBIx::Custom> except that
179
you don't have to specify table name.