added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Table; |
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 | ||
cleanup
|
13 |
__PACKAGE__->attr(['dbi', 'name']); |
added experimental DBIx::Cus...
|
14 | |
15 |
our $AUTOLOAD; |
|
16 | ||
17 |
sub AUTOLOAD { |
|
18 |
my $self = shift; |
|
19 | ||
20 |
# Method |
|
21 |
my ($package, $method) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
|
22 | ||
23 |
# Helper |
|
24 |
$self->{_helpers} ||= {}; |
|
25 |
croak qq/Can't locate object method "$method" via "$package"/ |
|
26 |
unless my $helper = $self->{_helpers}->{$method}; |
|
27 | ||
28 |
# Run |
|
29 |
return $self->$helper(@_); |
|
30 |
} |
|
31 | ||
32 |
sub helper { |
|
33 |
my $self = shift; |
|
34 |
|
|
35 |
# Merge |
|
36 |
my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
37 |
$self->{_helpers} = {%{$self->{_helpers} || {}}, %$helpers}; |
|
38 |
|
|
39 |
return $self; |
|
40 |
} |
|
41 | ||
added insert, update, update...
|
42 |
sub new { |
43 |
my $self = shift->SUPER::new(@_); |
|
44 |
|
|
many changed
|
45 |
# Methods |
added insert, update, update...
|
46 |
my @methods = qw/insert update update_all delete delete_all select/; |
47 |
foreach my $method (@methods) { |
|
48 |
$self->helper( |
|
49 |
$method => sub { |
|
50 |
my $self = shift; |
|
51 |
return $self->dbi->$method(table => $self->name, @_); |
|
52 |
} |
|
53 |
); |
|
54 |
} |
|
many changed
|
55 |
|
added insert, update, update...
|
56 |
return $self; |
57 |
} |
|
58 | ||
added experimental DBIx::Cus...
|
59 |
sub DESTROY { } |
60 | ||
61 |
1; |
|
62 | ||
63 |
=head1 NAME |
|
64 | ||
add examples
|
65 |
DBIx::Custom::Table - Table base class(experimental) |
added experimental DBIx::Cus...
|
66 | |
67 |
=head1 SYNOPSIS |
|
68 | ||
69 |
use DBIx::Custom::Table; |
|
70 | ||
71 |
my $table = DBIx::Custom::Table->new(name => 'books'); |
|
72 | ||
73 |
=head1 METHODS |
|
74 | ||
75 |
L<DBIx::Custom> inherits all methods from L<Object::Simple> |
|
76 |
and implements the following new ones. |
|
77 | ||
added insert, update, update...
|
78 |
=head2 C<delete> |
79 | ||
80 |
$table->delete(where => \%where); |
|
81 |
|
|
82 |
Same as C<delete()> of L<DBIx::Custom> except that |
|
83 |
you don't have to specify table name. |
|
84 | ||
85 |
=head2 C<delete_all> |
|
86 | ||
87 |
$table->delete_all(param => $param); |
|
88 |
|
|
89 |
Same as C<delete_all()> of L<DBIx::Custom> except that |
|
90 |
you don't have to specify table name. |
|
91 | ||
added experimental DBIx::Cus...
|
92 |
=head2 C<helper> |
93 | ||
94 |
$table->helper(insert => sub { |
|
simplified DBIx::Custom::Mod...
|
95 |
my $self = shift; |
96 |
|
|
97 |
return $self->dbi->insert(table => $self->name, @_); |
|
added experimental DBIx::Cus...
|
98 |
}); |
99 |
|
|
simplified DBIx::Custom::Mod...
|
100 |
Add helper method to a L<DBIx::Custom::Table> object. |
added experimental DBIx::Cus...
|
101 | |
added insert, update, update...
|
102 |
=head2 C<insert> |
103 | ||
104 |
$table->insert(param => \%param); |
|
105 |
|
|
106 |
Same as C<insert()> of L<DBIx::Custom> except that |
|
107 |
you don't have to specify table name. |
|
108 | ||
109 |
=head2 C<method> |
|
110 | ||
111 |
$table->method( |
|
112 |
select_complex => sub { |
|
113 |
my $self = shift; |
|
114 |
|
|
115 |
return $self->dbi->select($self->name, ...); |
|
116 |
}, |
|
117 |
some_method => sub { ... } |
|
118 |
); |
|
119 | ||
120 |
Define method. |
|
121 | ||
122 |
=head2 C<new> |
|
123 | ||
124 |
my $table = DBIx::Custom::Table->new; |
|
125 | ||
126 |
Create a L<DBIx::Custom::Table> object. |
|
127 | ||
128 |
=head2 C<select> |
|
129 | ||
130 |
$table->select(param => $param); |
|
131 |
|
|
132 |
Same as C<select()> of L<DBIx::Custom> except that |
|
133 |
you don't have to specify table name. |
|
134 | ||
135 |
=head2 C<update> |
|
136 | ||
137 |
$table->update(param => \%param, where => \%where); |
|
138 |
|
|
139 |
Same as C<update()> of L<DBIx::Custom> except that |
|
140 |
you don't have to specify table name. |
|
141 | ||
142 |
=head2 C<update_all> |
|
143 | ||
144 |
$table->update_all(param => \%param); |
|
145 |
|
|
146 |
Same as C<update_all()> of L<DBIx::Custom> except that |
|
147 |
you don't have to specify table name. |