... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1626 |
|
2 |
+ simplified DBIx::Custom::Model and DBIx::Custom::Table |
|
1 | 3 |
0.1625 |
2 | 4 |
added experimental DBIx::Custom::Model and DBIx::Custom::Table |
3 | 5 |
0.1624 |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1625'; |
|
3 |
+our $VERSION = '0.1626'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -523,7 +523,7 @@ sub register_filter { |
523 | 523 |
} |
524 | 524 |
|
525 | 525 |
our %VALID_SELECT_ARGS |
526 |
- = map { $_ => 1 } qw/auto_filter_table table column where append relation filter param/; |
|
526 |
+ = map { $_ => 1 } qw/auto_filter_table table column where append relation filter/; |
|
527 | 527 |
|
528 | 528 |
sub select { |
529 | 529 |
my ($self, %args) = @_; |
... | ... |
@@ -6,18 +6,27 @@ use warnings; |
6 | 6 |
use base 'Object::Simple'; |
7 | 7 |
|
8 | 8 |
use Carp 'croak'; |
9 |
-use DBIx::Custom::Table; |
|
10 | 9 |
|
11 | 10 |
__PACKAGE__->attr(dbi => sub { DBIx::Custom->new }); |
11 |
+__PACKAGE__->attr(table_class => 'DBIx::Custom::Table'); |
|
12 |
+__PACKAGE__->attr(tables => sub { {} }); |
|
12 | 13 |
|
13 | 14 |
sub table { |
14 |
- my ($self, $table) = @_; |
|
15 |
- |
|
16 |
- $self->{tables}{$table} |
|
17 |
- = DBIx::Custom::Table->new(name => $table, dbi => $self->dbi) |
|
18 |
- unless defined $self->{tables}{$table}; |
|
15 |
+ my ($self, $name) = @_; |
|
19 | 16 |
|
20 |
- return $self->{tables}{$table}; |
|
17 |
+ # Table class |
|
18 |
+ my $table_class = $self->table_class; |
|
19 |
+ croak qq{Invalid table class name "$table_class"} |
|
20 |
+ unless $table_class =~ /^[\w:]+$/; |
|
21 |
+ eval "use $table_class"; |
|
22 |
+ croak $@ if $@; |
|
23 |
+ |
|
24 |
+ # Create table |
|
25 |
+ $self->tables->{$name} |
|
26 |
+ = $table_class->new(name => $name, dbi => $self->dbi) |
|
27 |
+ unless defined $self->tables->{$name}; |
|
28 |
+ |
|
29 |
+ return $self->{tables}{$name}; |
|
21 | 30 |
} |
22 | 31 |
|
23 | 32 |
1; |
... | ... |
@@ -35,14 +44,15 @@ use base 'DBIx::Custom::Model'; |
35 | 44 |
sub new { |
36 | 45 |
my $self = shift->SUPER::new(@_); |
37 | 46 |
|
38 |
- $self->table('books')->helper( |
|
39 |
- insert_multi => sub { |
|
47 |
+ my $dbi = DBIx::Custom->connect(...); |
|
48 |
+ |
|
49 |
+ $self->dbi($dbi); |
|
50 |
+ |
|
51 |
+ $self->table('book')->helper( |
|
52 |
+ insert => sub { |
|
40 | 53 |
my $self = shift; |
41 | 54 |
|
42 |
- my $dbi = $self->dbi; |
|
43 |
- |
|
44 |
- # ... |
|
45 |
- |
|
55 |
+ return $self->dbi->insert(table => $self->name, @_); |
|
46 | 56 |
} |
47 | 57 |
); |
48 | 58 |
|
... | ... |
@@ -56,7 +66,7 @@ and implements the following new ones. |
56 | 66 |
|
57 | 67 |
=head2 C<table> |
58 | 68 |
|
59 |
- my $table = $model->table('books'); |
|
69 |
+ my $table = $model->table('book'); |
|
60 | 70 |
|
61 |
-Create a table object if not exists or get it. |
|
71 |
+Get a L<DBIx::Custom::Table>, or create a L<DBIx::Custom::Table> object if not exists. |
|
62 | 72 |
|
... | ... |
@@ -9,87 +9,6 @@ use Carp 'croak'; |
9 | 9 |
|
10 | 10 |
__PACKAGE__->attr(['dbi', 'name']); |
11 | 11 |
|
12 |
-sub new { |
|
13 |
- my $self = shift->SUPER::new(@_); |
|
14 |
- |
|
15 |
- my $insert = sub { |
|
16 |
- my $self = shift; |
|
17 |
- |
|
18 |
- return $self->dbi->insert(table => $self->name, param => shift); |
|
19 |
- }; |
|
20 |
- |
|
21 |
- my $update = sub { |
|
22 |
- my $self = shift; |
|
23 |
- |
|
24 |
- return $self->dbi->update(table => $self->name, param => shift, |
|
25 |
- where => shift); |
|
26 |
- }; |
|
27 |
- |
|
28 |
- my $update_all = sub { |
|
29 |
- my $self = shift; |
|
30 |
- |
|
31 |
- return $self->dbi->update_all(table => $self->name, param => shift); |
|
32 |
- }; |
|
33 |
- |
|
34 |
- my $delete = sub { |
|
35 |
- my $self = shift; |
|
36 |
- |
|
37 |
- return $self->dbi->delete(table => $self->name, where => shift); |
|
38 |
- }; |
|
39 |
- |
|
40 |
- my $delete_all = sub { |
|
41 |
- my $self = shift; |
|
42 |
- |
|
43 |
- return $self->dbi->delete_all(table => $self->name); |
|
44 |
- }; |
|
45 |
- |
|
46 |
- my $select = sub { |
|
47 |
- my $self = shift; |
|
48 |
- |
|
49 |
- my $where = {}; |
|
50 |
- my $column = ['*']; |
|
51 |
- my $append = ''; |
|
52 |
- |
|
53 |
- foreach my $arg (@_) { |
|
54 |
- my $type = ref $arg; |
|
55 |
- |
|
56 |
- if ($type eq 'ARRAY') { |
|
57 |
- $column = $arg; |
|
58 |
- } |
|
59 |
- elsif ($type eq 'HASH') { |
|
60 |
- $where = $arg; |
|
61 |
- } |
|
62 |
- else { |
|
63 |
- $append = $arg; |
|
64 |
- } |
|
65 |
- } |
|
66 |
- |
|
67 |
- return $self->dbi->select( |
|
68 |
- table => $self->name, |
|
69 |
- where => $where, |
|
70 |
- column => $column, |
|
71 |
- append => $append |
|
72 |
- ); |
|
73 |
- }; |
|
74 |
- |
|
75 |
- $self->helper( |
|
76 |
- insert => $insert, |
|
77 |
- insert_simple => $insert, |
|
78 |
- update => $update, |
|
79 |
- update_simple => $update, |
|
80 |
- update_all => $update_all, |
|
81 |
- update_all_simple => $update_all, |
|
82 |
- delete => $delete, |
|
83 |
- delete_simple => $delete, |
|
84 |
- delete_all => $delete_all, |
|
85 |
- delete_all_simple => $delete_all, |
|
86 |
- select => $select, |
|
87 |
- select_simple => $select |
|
88 |
- ); |
|
89 |
- |
|
90 |
- return $self; |
|
91 |
-} |
|
92 |
- |
|
93 | 12 |
our $AUTOLOAD; |
94 | 13 |
|
95 | 14 |
sub AUTOLOAD { |
... | ... |
@@ -123,7 +42,7 @@ sub DESTROY { } |
123 | 42 |
|
124 | 43 |
=head1 NAME |
125 | 44 |
|
126 |
-DBIx::Custom::Model - Modele base class(experimental) |
|
45 |
+DBIx::Custom::Model - Table base class(experimental) |
|
127 | 46 |
|
128 | 47 |
=head1 SYNOPSIS |
129 | 48 |
|
... | ... |
@@ -139,69 +58,10 @@ and implements the following new ones. |
139 | 58 |
=head2 C<helper> |
140 | 59 |
|
141 | 60 |
$table->helper(insert => sub { |
142 |
- # ... |
|
61 |
+ my $self = shift; |
|
62 |
+ |
|
63 |
+ return $self->dbi->insert(table => $self->name, @_); |
|
143 | 64 |
}); |
144 |
- |
|
145 |
-=head2 C<new> |
|
146 |
- |
|
147 |
- my $table = DBIx::Custom->new; |
|
148 |
- |
|
149 |
-=head2 C<insert> |
|
150 |
- |
|
151 |
- $table->insert(\%param); |
|
152 |
- |
|
153 |
-Insert. |
|
154 |
- |
|
155 |
-=head2 C<insert_simple> |
|
156 |
- |
|
157 |
-Same as C<insert()>. |
|
158 |
- |
|
159 |
-=head2 C<update> |
|
160 |
- |
|
161 |
- $table->update(\%param, \%where); |
|
162 |
- |
|
163 |
-Update. |
|
164 |
- |
|
165 |
-=head2 C<update_simple> |
|
166 |
- |
|
167 |
-Same as C<update()>. |
|
168 |
- |
|
169 |
-=head2 C<update_all> |
|
170 |
- |
|
171 |
- $table->update_all(\%param); |
|
172 |
- |
|
173 |
-Update all. |
|
174 |
- |
|
175 |
-=head2 C<update_all_simple> |
|
176 |
- |
|
177 |
-Same as C<update_all>. |
|
178 |
- |
|
179 |
-=head2 C<delete> |
|
180 |
- |
|
181 |
- $table->delete(\%where); |
|
182 |
- |
|
183 |
-=head2 C<delete_simple()> |
|
184 |
- |
|
185 |
-Same as C<delete_all()>. |
|
186 |
- |
|
187 |
-=head2 C<delete_all> |
|
188 |
- |
|
189 |
- $table->delete_all(\%where); |
|
190 |
- |
|
191 |
-=head2 C<delete_all_simple()> |
|
192 |
- |
|
193 |
-Same as C<delete_all()>. |
|
194 |
- |
|
195 |
-=head2 C<select> |
|
196 |
- |
|
197 |
- $table->select(\%where); |
|
198 |
- $table->select(\@column); |
|
199 |
- $table->select($append); |
|
200 | 65 |
|
201 |
- # And any combination |
|
202 |
- $table->select(\%where, \@column, $append); |
|
203 |
- |
|
204 |
-=head2 C<select_simple> |
|
205 |
- |
|
206 |
-Same as C<select()>. |
|
66 |
+Add helper method to a L<DBIx::Custom::Table> object. |
|
207 | 67 |
|
... | ... |
@@ -754,31 +754,66 @@ test 'model'; |
754 | 754 |
package MyModel1; |
755 | 755 |
|
756 | 756 |
use base 'DBIx::Custom::Model'; |
757 |
+ |
|
758 |
+ sub new { |
|
759 |
+ my $self = shift->SUPER::new(@_); |
|
760 |
+ |
|
761 |
+ my $dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
762 |
+ |
|
763 |
+ $self->dbi($dbi); |
|
764 |
+ |
|
765 |
+ $self->table('table1')->helper( |
|
766 |
+ insert => sub { |
|
767 |
+ my $self = shift; |
|
768 |
+ $self->dbi->insert(table => $self->name, @_); |
|
769 |
+ }, |
|
770 |
+ update => sub { |
|
771 |
+ my $self = shift; |
|
772 |
+ $self->dbi->update(table => $self->name, @_); |
|
773 |
+ }, |
|
774 |
+ update_all => sub { |
|
775 |
+ my $self = shift; |
|
776 |
+ $self->dbi->update_all(table => $self->name, @_); |
|
777 |
+ }, |
|
778 |
+ delete => sub { |
|
779 |
+ my $self = shift; |
|
780 |
+ $self->dbi->delete(table => $self->name, @_); |
|
781 |
+ }, |
|
782 |
+ delete_all => sub { |
|
783 |
+ my $self = shift; |
|
784 |
+ $self->dbi->delete_all(table => $self->name, @_); |
|
785 |
+ }, |
|
786 |
+ select => sub { |
|
787 |
+ my $self = shift; |
|
788 |
+ $self->dbi->select(table => $self->name, @_); |
|
789 |
+ }, |
|
790 |
+ ); |
|
791 |
+ return $self; |
|
792 |
+ } |
|
757 | 793 |
} |
758 | 794 |
$model = MyModel1->new; |
759 |
-$model->dbi->connect($NEW_ARGS->{0}); |
|
760 | 795 |
$model->dbi->execute($CREATE_TABLE->{0}); |
761 | 796 |
$table = $model->table('table1'); |
762 |
-$table->insert({key1 => 1, key2 => 2}); |
|
763 |
-$table->insert_simple({key1 => 3, key2 => 4}); |
|
797 |
+$table->insert(param => {key1 => 1, key2 => 2}); |
|
798 |
+$table->insert(param => {key1 => 3, key2 => 4}); |
|
764 | 799 |
$rows = $table->select->fetch_hash_all; |
765 | 800 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], |
766 | 801 |
"$test: select"); |
767 |
-$rows = $table->select({key2 => 2}, |
|
768 |
- 'order by key1', ['key1', 'key2'])->fetch_hash_all; |
|
802 |
+$rows = $table->select(where => {key2 => 2}, append => 'order by key1', |
|
803 |
+ column => ['key1', 'key2'])->fetch_hash_all; |
|
769 | 804 |
is_deeply($rows, [{key1 => 1, key2 => 2}], |
770 |
- "$test: insert insert_simple select select_simple"); |
|
771 |
-$table->update({key1 => 3}, {key2 => 2}); |
|
772 |
-$table->update_simple({key1 => 5}, {key2 => 4}); |
|
773 |
-$rows = $table->select_simple({key2 => 2})->fetch_hash_all; |
|
805 |
+ "$test: insert insert select"); |
|
806 |
+$table->update(param => {key1 => 3}, where => {key2 => 2}); |
|
807 |
+$table->update(param => {key1 => 5}, where => {key2 => 4}); |
|
808 |
+$rows = $table->select(where => {key2 => 2})->fetch_hash_all; |
|
774 | 809 |
is_deeply($rows, [{key1 => 3, key2 => 2}], |
775 | 810 |
"$test: update"); |
776 |
-$table->delete({key2 => 2}); |
|
811 |
+$table->delete(where => {key2 => 2}); |
|
777 | 812 |
$rows = $table->select->fetch_hash_all; |
778 | 813 |
is_deeply($rows, [{key1 => 5, key2 => 4}], "$test: delete"); |
814 |
+$table->update_all(param => {key1 => 3}); |
|
815 |
+$rows = $table->select->fetch_hash_all; |
|
816 |
+is_deeply($rows, [{key1 => 3, key2 => 4}], "$test: update_all"); |
|
779 | 817 |
$table->delete_all; |
780 | 818 |
$rows = $table->select->fetch_hash_all; |
781 | 819 |
is_deeply($rows, [], "$test: delete_all"); |
782 |
-$table->helper('insert' => sub { 5 }); |
|
783 |
-is($table->insert, 5, "$test : helper"); |
|
784 |
- |