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 | ||
added insert, update, update...
|
54 |
sub new { |
55 |
my $self = shift->SUPER::new(@_); |
|
56 |
|
|
many changed
|
57 |
# Methods |
added insert, update, update...
|
58 |
my @methods = qw/insert update update_all delete delete_all select/; |
59 |
foreach my $method (@methods) { |
|
renamed experimental DBIx::C...
|
60 |
$self->method( |
added insert, update, update...
|
61 |
$method => sub { |
62 |
my $self = shift; |
|
add feture. all model class ...
|
63 |
return $self->dbi->$method(table => $self->table, @_); |
added insert, update, update...
|
64 |
} |
65 |
); |
|
66 |
} |
|
many changed
|
67 |
|
added insert, update, update...
|
68 |
return $self; |
69 |
} |
|
70 | ||
added experimental DBIx::Cus...
|
71 |
sub DESTROY { } |
72 | ||
73 |
1; |
|
74 | ||
75 |
=head1 NAME |
|
76 | ||
add DBIx::Custom::Model fore...
|
77 |
DBIx::Custom::Model - Model (experimental) |
added experimental DBIx::Cus...
|
78 | |
79 |
=head1 SYNOPSIS |
|
80 | ||
81 |
use DBIx::Custom::Table; |
|
82 | ||
add feture. all model class ...
|
83 |
my $table = DBIx::Custom::Model->new(table => 'books'); |
added experimental DBIx::Cus...
|
84 | |
add DBIx::Custom::Model fore...
|
85 |
=head1 ATTRIBUTES |
86 | ||
add DBIx::Custom::Model colu...
|
87 |
=head2 C<(experimental) columns> |
88 | ||
89 |
my $columns = $model->columns; |
|
90 |
$model = $model->columns(['id', 'number']); |
|
91 | ||
add DBIx::Custom::Model fore...
|
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...
|
114 |
=head1 METHODS |
115 | ||
table object call dbi object...
|
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...
|
118 |
and implements the following new ones. |
119 | ||
added insert, update, update...
|
120 |
=head2 C<delete> |
121 | ||
table object call dbi object...
|
122 |
$table->delete(...); |
added insert, update, update...
|
123 |
|
124 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
125 |
you don't have to specify C<table> option. |
added insert, update, update...
|
126 | |
127 |
=head2 C<delete_all> |
|
128 | ||
table object call dbi object...
|
129 |
$table->delete_all(...); |
added insert, update, update...
|
130 |
|
131 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
132 |
you don't have to specify C<table> option. |
added insert, update, update...
|
133 | |
renamed experimental DBIx::C...
|
134 |
=head2 C<method> |
added experimental DBIx::Cus...
|
135 | |
table object call dbi object...
|
136 |
$table->method( |
137 |
count => sub { |
|
138 |
my $self = shift; |
|
simplified DBIx::Custom::Mod...
|
139 |
|
table object call dbi object...
|
140 |
return $self->select(column => 'count(*)', @_) |
141 |
->fetch_first->[0]; |
|
142 |
} |
|
143 |
); |
|
added experimental DBIx::Cus...
|
144 |
|
renamed experimental DBIx::C...
|
145 |
Add method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
146 | |
added insert, update, update...
|
147 |
=head2 C<insert> |
148 | ||
table object call dbi object...
|
149 |
$table->insert(...); |
added insert, update, update...
|
150 |
|
151 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
152 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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...
|
162 |
$table->select(...); |
added insert, update, update...
|
163 |
|
164 |
Same as C<select()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
165 |
you don't have to specify C<table> option. |
added insert, update, update...
|
166 | |
167 |
=head2 C<update> |
|
168 | ||
table object call dbi object...
|
169 |
$table->update(...); |
added insert, update, update...
|
170 |
|
171 |
Same as C<update()> of L<DBIx::Custom> except that |
|
table object call dbi object...
|
172 |
you don't have to specify C<table> option. |
added insert, update, update...
|
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. |