add feture. all model class ...
|
1 |
package DBIx::Custom::Model; |
added experimental DBIx::Cus...
|
2 | |
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use Carp 'croak'; |
|
9 | ||
added experimental DBIx::Cus...
|
10 |
# Carp trust relationship |
11 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
12 | ||
add DBIx::Custom::Model fore...
|
13 |
__PACKAGE__->attr( |
14 |
['dbi', 'table'], |
|
add DBIx::Custom::Model colu...
|
15 |
columns => sub { [] }, |
add experimental DBIx::Custo...
|
16 |
primary_key => sub { [] }, |
17 |
relation => sub { {} } |
|
add DBIx::Custom::Model fore...
|
18 |
); |
added experimental DBIx::Cus...
|
19 | |
20 |
our $AUTOLOAD; |
|
21 | ||
22 |
sub AUTOLOAD { |
|
23 |
my $self = shift; |
|
24 | ||
renamed experimental DBIx::C...
|
25 |
# Method name |
26 |
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
added experimental DBIx::Cus...
|
27 | |
renamed experimental DBIx::C...
|
28 |
# Method |
29 |
$self->{_methods} ||= {}; |
|
table object call dbi object...
|
30 |
if (my $method = $self->{_methods}->{$mname}) { |
31 |
return $self->$method(@_) |
|
32 |
} |
|
add feture. all model class ...
|
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...
|
42 |
} |
43 | ||
renamed experimental DBIx::C...
|
44 |
sub method { |
added experimental DBIx::Cus...
|
45 |
my $self = shift; |
46 |
|
|
47 |
# Merge |
|
renamed experimental DBIx::C...
|
48 |
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
49 |
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
added experimental DBIx::Cus...
|
50 |
|
51 |
return $self; |
|
52 |
} |
|
53 | ||
add experimental DBIx::Custo...
|
54 |
sub insert { my $self = shift; $self->dbi->insert(table => $self->table, @_) } |
55 |
sub update { my $self = shift; $self->dbi->update(table => $self->table, @_) } |
|
56 |
sub update_all { my $self = shift; $self->dbi->update_all(table => $self->table, @_) } |
|
57 |
sub delete { my $self = shift; $self->dbi->delete(table => $self->table, @_) } |
|
58 |
sub delete_all { my $self = shift; $self->dbi->delete_all(table => $self->table, @_) } |
|
59 |
sub select { my $self = shift; $self->dbi->select(table => $self->table, @_) } |
|
60 | ||
61 |
sub update_at { |
|
62 |
my $self = shift; |
|
added insert, update, update...
|
63 |
|
add experimental DBIx::Custo...
|
64 |
return $self->dbi->update_at( |
65 |
table => $self->table, |
|
66 |
primary_key => $self->primary_key, |
|
67 |
@_ |
|
68 |
); |
|
69 |
} |
|
70 | ||
71 |
sub delete_at { |
|
72 |
my $self = shift; |
|
73 |
|
|
74 |
return $self->dbi->delete_at( |
|
75 |
table => $self->table, |
|
76 |
primary_key => $self->primary_key, |
|
77 |
@_ |
|
78 |
); |
|
79 |
} |
|
80 | ||
81 |
sub select_at { |
|
82 |
my $self = shift; |
|
many changed
|
83 |
|
add experimental DBIx::Custo...
|
84 |
return $self->dbi->select_at( |
85 |
table => $self->table, |
|
86 |
primary_key => $self->primary_key, |
|
87 |
@_ |
|
88 |
); |
|
added insert, update, update...
|
89 |
return $self; |
90 |
} |
|
91 | ||
added experimental DBIx::Cus...
|
92 |
sub DESTROY { } |
93 | ||
94 |
1; |
|
95 | ||
96 |
=head1 NAME |
|
97 | ||
add DBIx::Custom::Model fore...
|
98 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
99 | |
100 |
=head1 SYNOPSIS |
|
101 | ||
102 |
use DBIx::Custom::Table; |
|
103 | ||
add feture. all model class ...
|
104 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
105 | |
add DBIx::Custom::Model fore...
|
106 |
=head1 ATTRIBUTES |
107 | ||
add DBIx::Custom::Model colu...
|
108 |
=head2 C<(experimental) columns> |
109 | ||
110 |
my $columns = $model->columns; |
|
111 |
$model = $model->columns(['id', 'number']); |
|
112 | ||
add DBIx::Custom::Model fore...
|
113 |
=head2 C<dbi> |
114 | ||
115 |
my $dbi = $model->dbi; |
|
116 |
$model = $model->dbi($dbi); |
|
117 | ||
118 |
L<DBIx::Custom> object. |
|
119 | ||
120 |
=head2 C<table> |
|
121 | ||
122 |
my $table = $model->table; |
|
123 |
$model = $model->table('book'); |
|
124 | ||
125 |
Table name. |
|
126 |
|
|
127 |
=head2 C<primary_key> |
|
128 | ||
129 |
my $primary_key = $model->primary_key; |
|
130 |
$model = $model->primary_key(['id', 'number']); |
|
131 | ||
132 |
Foreign key. This is used by C<update_at()>, C<delete_at()>, |
|
133 |
C<select_at()>. |
|
134 | ||
added experimental DBIx::Cus...
|
135 |
=head1 METHODS |
136 | ||
table object call dbi object...
|
137 |
L<DBIx::Custom> inherits all methods from L<Object::Simple>, |
138 |
and you can use all methods of the object set to C<dbi>. |
|
added experimental DBIx::Cus...
|
139 |
and implements the following new ones. |
140 | ||
added insert, update, update...
|
141 |
=head2 C<delete> |
142 | ||
table object call dbi object...
|
143 |
$table->delete(...); |
added insert, update, update...
|
144 |
|
145 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
146 |
you don't have to specify C<table> option. |
added insert, update, update...
|
147 | |
148 |
=head2 C<delete_all> |
|
149 | ||
table object call dbi object...
|
150 |
$table->delete_all(...); |
added insert, update, update...
|
151 |
|
152 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
153 |
you don't have to specify C<table> option. |
added insert, update, update...
|
154 | |
renamed experimental DBIx::C...
|
155 |
=head2 C<method> |
added experimental DBIx::Cus...
|
156 | |
table object call dbi object...
|
157 |
$table->method( |
158 |
count => sub { |
|
159 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
160 |
|
table object call dbi object...
|
161 |
return $self->select(column => 'count(*)', @_) |
162 |
->fetch_first->[0]; |
|
163 |
} |
|
164 |
); |
|
added experimental DBIx::Cus...
|
165 |
|
renamed experimental DBIx::C...
|
166 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
167 | |
added insert, update, update...
|
168 |
=head2 C<insert> |
169 | ||
table object call dbi object...
|
170 |
$table->insert(...); |
added insert, update, update...
|
171 |
|
172 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
173 |
you don't have to specify C<table> option. |
added insert, update, update...
|
174 | |
175 |
=head2 C<new> |
|
176 | ||
177 |
my $table = DBIx::Custom::Table->new; |
|
178 | ||
179 |
Create a L<DBIx::Custom::Table> object. |
|
180 | ||
181 |
=head2 C<select> |
|
182 | ||
table object call dbi object...
|
183 |
$table->select(...); |
added insert, update, update...
|
184 |
|
185 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
186 |
you don't have to specify C<table> option. |
added insert, update, update...
|
187 | |
188 |
=head2 C<update> |
|
189 | ||
table object call dbi object...
|
190 |
$table->update(...); |
added insert, update, update...
|
191 |
|
192 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
193 |
you don't have to specify C<table> option. |
added insert, update, update...
|
194 | |
195 |
=head2 C<update_all> |
|
196 | ||
197 |
$table->update_all(param => \%param); |
|
198 |
|
|
199 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
200 |
you don't have to specify table name. |