... | ... |
@@ -1,7 +1,11 @@ |
1 |
+package DBIx::Custom; |
|
2 |
+use base 'Object::Simple::Base'; |
|
3 |
+ |
|
4 |
+use strict; |
|
5 |
+use warnings; |
|
6 |
+ |
|
1 | 7 |
use 5.008001; |
2 | 8 |
|
3 |
-package DBIx::Custom; |
|
4 |
-use Object::Simple; |
|
5 | 9 |
|
6 | 10 |
our $VERSION = '0.0901'; |
7 | 11 |
|
... | ... |
@@ -10,42 +14,33 @@ use DBI; |
10 | 14 |
use DBIx::Custom::Result; |
11 | 15 |
use DBIx::Custom::SQL::Template; |
12 | 16 |
|
17 |
+my $p = __PACKAGE__; |
|
13 | 18 |
|
14 |
-### Accessors |
|
15 |
-sub dbh : Attr {} |
|
19 |
+$p->attr('dbh'); |
|
16 | 20 |
|
17 |
-sub _query_caches : ClassAttr { type => 'hash', build => sub {{}} } |
|
18 |
-sub _query_cache_keys : ClassAttr { type => 'array', build => sub {[]} } |
|
19 |
-sub query_cache_max : ClassAttr { build => 50 } |
|
21 |
+$p->class_attr(_query_caches => (type => 'hash', default => sub { {} })) |
|
22 |
+ ->class_attr(_query_cache_keys => (type => 'array', default => sub { [] })) |
|
23 |
+ ->class_attr(query_cache_max => 50); |
|
20 | 24 |
|
21 |
-sub user : HybridAttr { clone => 'scalar' } |
|
22 |
-sub password : HybridAttr { clone => 'scalar' } |
|
23 |
-sub data_source : HybridAttr { clone => 'scalar' } |
|
24 |
-sub database : HybridAttr { clone => 'scalar' } |
|
25 |
-sub host : HybridAttr { clone => 'scalar' } |
|
26 |
-sub port : HybridAttr { clone => 'scalar' } |
|
27 |
-sub bind_filter : HybridAttr { clone => 'scalar' } |
|
28 |
-sub fetch_filter : HybridAttr { clone => 'scalar' } |
|
25 |
+$p->hybrid_attr([qw/user password data_source |
|
26 |
+ database host port |
|
27 |
+ bind_filter fetch_filter/] => (clone => 'scalar')); |
|
29 | 28 |
|
30 |
-sub no_bind_filters : HybridAttr { type => 'array', build => sub {[]}, |
|
31 |
- clone => 'array' } |
|
29 |
+$p->hybrid_attr([qw/no_bind_filters no_fetch_filters/] |
|
30 |
+ => (type => 'array', default => sub { [] }, clone => 'array')); |
|
32 | 31 |
|
33 |
-sub no_fetch_filters : HybridAttr { type => 'array', build => sub { [] }, |
|
34 |
- clone => 'array' } |
|
32 |
+$p->hybrid_attr(options => (type => 'hash', default => sub { {} }, |
|
33 |
+ clone => 'hash')); |
|
35 | 34 |
|
36 |
-sub options : HybridAttr { type => 'hash', build => sub {{}}, clone => 'hash' } |
|
35 |
+$p->hybrid_attr([qw/filters formats/] |
|
36 |
+ => (type => 'hash', default => sub { {} }, |
|
37 |
+ deref => 1, clone => 'hash')); |
|
37 | 38 |
|
38 |
-sub filters : HybridAttr { type => 'hash', build => sub {{}}, |
|
39 |
- clone => 'hash', deref => 1 } |
|
39 |
+$p->hybrid_attr(result_class => (default => 'DBIx::Custom::Result', |
|
40 |
+ clone => 'scalar')); |
|
40 | 41 |
|
41 |
-sub formats : HybridAttr { type => 'hash', build => sub { {} }, |
|
42 |
- clone => 'hash', deref => 1 } |
|
43 |
- |
|
44 |
-sub result_class : HybridAttr { build => 'DBIx::Custom::Result', |
|
45 |
- clone => 'scalar' } |
|
46 |
- |
|
47 |
-sub sql_tmpl : HybridAttr { build => sub {DBIx::Custom::SQL::Template->new}, |
|
48 |
- clone => sub {$_[0] ? $_[0]->clone : undef} } |
|
42 |
+$p->hybrid_attr(sql_tmpl => (default => sub {DBIx::Custom::SQL::Template->new}, |
|
43 |
+ clone => sub {$_[0] ? $_[0]->clone : undef})); |
|
49 | 44 |
|
50 | 45 |
### Methods |
51 | 46 |
|
... | ... |
@@ -736,8 +731,6 @@ sub filter_off { |
736 | 731 |
return $self; |
737 | 732 |
} |
738 | 733 |
|
739 |
-Object::Simple->build_class; |
|
740 |
- |
|
741 | 734 |
=head1 NAME |
742 | 735 |
|
743 | 736 |
DBIx::Custom - Customizable DBI |
... | ... |
@@ -5,12 +5,12 @@ use warnings; |
5 | 5 |
use strict; |
6 | 6 |
use Carp 'croak'; |
7 | 7 |
|
8 |
-my $class = __PACKAGE__; |
|
8 |
+my $p = __PACKAGE__; |
|
9 | 9 |
|
10 |
-$class->add_format( |
|
11 |
- datetime => $class->formats->{SQL99_datetime}, |
|
12 |
- date => $class->formats->{SQL99_date}, |
|
13 |
- time => $class->formats->{SQL99_time}, |
|
10 |
+$p->add_format( |
|
11 |
+ datetime => $p->formats->{SQL99_datetime}, |
|
12 |
+ date => $p->formats->{SQL99_date}, |
|
13 |
+ time => $p->formats->{SQL99_time}, |
|
14 | 14 |
); |
15 | 15 |
|
16 | 16 |
|
... | ... |
@@ -1,26 +1,29 @@ |
1 | 1 |
package DBIx::Custom::Query; |
2 |
-use Object::Simple; |
|
2 |
+use base 'Object::Simple::Base'; |
|
3 | 3 |
|
4 | 4 |
use strict; |
5 | 5 |
use warnings; |
6 | 6 |
|
7 |
-sub sql : Attr {} |
|
8 |
-sub key_infos : Attr {} |
|
9 |
-sub bind_filter : Attr {} |
|
10 |
-sub fetch_filter : Attr {} |
|
11 |
-sub sth : Attr {} |
|
7 |
+my $p = __PACKAGE__; |
|
12 | 8 |
|
13 |
-sub no_bind_filters : Attr { type => 'array', trigger => sub { |
|
9 |
+$p->attr([qw/sql key_infos bind_filter fetch_filter sth/]) |
|
10 |
+ ->attr(_no_bind_filters_map => sub { {} }) |
|
11 |
+ ->attr(no_fetch_filters => (type => 'array', default => sub { [] })); |
|
12 |
+ |
|
13 |
+$p->attr(no_bind_filters => (type => 'array', trigger => sub { |
|
14 | 14 |
my $self = shift; |
15 | 15 |
my $no_bind_filters = $self->no_bind_filters || []; |
16 | 16 |
my %no_bind_filters_map = map {$_ => 1} @{$no_bind_filters}; |
17 | 17 |
$self->_no_bind_filters_map(\%no_bind_filters_map); |
18 |
-}} |
|
19 |
-sub _no_bind_filters_map : Attr {default => sub { {} }} |
|
20 |
- |
|
21 |
-sub no_fetch_filters : Attr { type => 'array', default => sub { [] } } |
|
18 |
+})); |
|
22 | 19 |
|
23 |
-Object::Simple->build_class; |
|
20 |
+sub new { |
|
21 |
+ my $self = shift->SUPER::new(@_); |
|
22 |
+ |
|
23 |
+ Object::Simple::Util->init_attrs($self, 'no_bind_filters'); |
|
24 |
+ |
|
25 |
+ return $self; |
|
26 |
+} |
|
24 | 27 |
|
25 | 28 |
=head1 NAME |
26 | 29 |
|
... | ... |
@@ -92,6 +95,12 @@ Set and get key informations |
92 | 95 |
$query = $query->key_infos($key_infos); |
93 | 96 |
$key_infos = $query->key_infos; |
94 | 97 |
|
98 |
+=head1 Methods |
|
99 |
+ |
|
100 |
+=head2 new |
|
101 |
+ |
|
102 |
+ my $query = DBIx::Custom::Query->new; |
|
103 |
+ |
|
95 | 104 |
=head1 AUTHOR |
96 | 105 |
|
97 | 106 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
... | ... |
@@ -1,23 +1,31 @@ |
1 | 1 |
package DBIx::Custom::Result; |
2 |
-use Object::Simple; |
|
2 |
+use base 'Object::Simple::Base'; |
|
3 | 3 |
|
4 | 4 |
use strict; |
5 | 5 |
use warnings; |
6 | 6 |
use Carp 'croak'; |
7 | 7 |
|
8 |
-# Attributes |
|
9 |
-sub _dbi : Attr {} |
|
10 |
-sub sth : Attr {} |
|
11 |
-sub fetch_filter : Attr {} |
|
8 |
+use Object::Simple::Util; |
|
12 | 9 |
|
13 |
-sub no_fetch_filters : Attr { type => 'array', trigger => sub { |
|
10 |
+my $p = __PACKAGE__; |
|
11 |
+ |
|
12 |
+$p->attr([qw/_dbi sth fetch_filter/]) |
|
13 |
+ ->attr(_no_fetch_filters_map => sub { {} }); |
|
14 |
+ |
|
15 |
+$p->attr(no_fetch_filters => (type => 'array', trigger => sub { |
|
14 | 16 |
my $self = shift; |
15 | 17 |
my $no_fetch_filters = $self->no_fetch_filters || []; |
16 | 18 |
my %no_fetch_filters_map = map {$_ => 1} @{$no_fetch_filters}; |
17 | 19 |
$self->_no_fetch_filters_map(\%no_fetch_filters_map); |
18 |
-}} |
|
20 |
+})); |
|
19 | 21 |
|
20 |
-sub _no_fetch_filters_map : Attr {default => sub { {} }} |
|
22 |
+sub new { |
|
23 |
+ my $self = shift->SUPER::new(@_); |
|
24 |
+ |
|
25 |
+ Object::Simple::Util->init_attrs($self, 'no_fetch_filters'); |
|
26 |
+ |
|
27 |
+ return $self; |
|
28 |
+} |
|
21 | 29 |
|
22 | 30 |
# Fetch (array) |
23 | 31 |
sub fetch { |
... | ... |
@@ -193,8 +201,6 @@ sub error { |
193 | 201 |
return wantarray ? ($sth->errstr, $sth->err, $sth->state) : $sth->errstr; |
194 | 202 |
} |
195 | 203 |
|
196 |
-Object::Simple->build_class; |
|
197 |
- |
|
198 | 204 |
=head1 NAME |
199 | 205 |
|
200 | 206 |
DBIx::Custom::Result - DBIx::Custom Resultset |
... | ... |
@@ -238,6 +244,10 @@ Set and Get no filter keys when fetching |
238 | 244 |
|
239 | 245 |
=head1 Methods |
240 | 246 |
|
247 |
+=head2 new |
|
248 |
+ |
|
249 |
+ my $result = DBIx::Custom::Result->new; |
|
250 |
+ |
|
241 | 251 |
=head2 fetch |
242 | 252 |
|
243 | 253 |
Fetch a row |
... | ... |
@@ -1,5 +1,5 @@ |
1 | 1 |
package DBIx::Custom::SQL::Template; |
2 |
-use Object::Simple; Object::Simple->build_class; |
|
2 |
+use base 'Object::Simple::Base'; |
|
3 | 3 |
|
4 | 4 |
use strict; |
5 | 5 |
use warnings; |
... | ... |
@@ -7,7 +7,17 @@ use Carp 'croak'; |
7 | 7 |
|
8 | 8 |
use DBIx::Custom::Query; |
9 | 9 |
|
10 |
-__PACKAGE__->add_tag_processor( |
|
10 |
+my $p = __PACKAGE__; |
|
11 |
+ |
|
12 |
+$p->hybrid_attr(tag_processors => (type => 'hash', default => sub { {} }, |
|
13 |
+ deref => 1, clone => 'hash')); |
|
14 |
+ |
|
15 |
+$p->hybrid_attr(tag_start => (default => '{', clone => 'scalar')) |
|
16 |
+ ->hybrid_attr(tag_end => (default => '}', clone => 'scalar')); |
|
17 |
+ |
|
18 |
+$p->hybrid_attr(tag_syntax => (clone => 'scalar')); |
|
19 |
+ |
|
20 |
+$p->add_tag_processor( |
|
11 | 21 |
'?' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
12 | 22 |
'=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
13 | 23 |
'<>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
... | ... |
@@ -21,7 +31,7 @@ __PACKAGE__->add_tag_processor( |
21 | 31 |
'update' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_update_tag |
22 | 32 |
); |
23 | 33 |
|
24 |
-__PACKAGE__->tag_syntax(<< 'EOS'); |
|
34 |
+$p->tag_syntax(<< 'EOS'); |
|
25 | 35 |
[tag] [expand] |
26 | 36 |
{? name} ? |
27 | 37 |
{= name} name = ? |
... | ... |
@@ -39,16 +49,6 @@ __PACKAGE__->tag_syntax(<< 'EOS'); |
39 | 49 |
{update key1 key2} set key1 = ?, key2 = ? |
40 | 50 |
EOS |
41 | 51 |
|
42 |
-# Accessor is created by Object::Simple. |
|
43 |
- |
|
44 |
-### Class-Object accessors |
|
45 |
- |
|
46 |
- |
|
47 |
-sub tag_start : HybridAttr { build => '{', clone => 'scalar' } |
|
48 |
-sub tag_end : HybridAttr { build => '}', clone => 'scalar' } |
|
49 |
-sub tag_syntax : HybridAttr { clone => 'scalar' } |
|
50 |
-sub tag_processors : HybridAttr { type => 'hash', build => sub {{}}, |
|
51 |
- clone => 'hash', deref => 1 } |
|
52 | 52 |
|
53 | 53 |
# Add Tag processor |
54 | 54 |
sub add_tag_processor { |
... | ... |
@@ -5,12 +5,12 @@ use strict; |
5 | 5 |
use warnings; |
6 | 6 |
use Carp 'croak'; |
7 | 7 |
|
8 |
-my $class = __PACKAGE__; |
|
8 |
+my $p = __PACKAGE__; |
|
9 | 9 |
|
10 |
-$class->add_format( |
|
11 |
- datetime => $class->formats->{SQL99_datetime}, |
|
12 |
- date => $class->formats->{SQL99_date}, |
|
13 |
- time => $class->formats->{SQL99_time}, |
|
10 |
+$p->add_format( |
|
11 |
+ datetime => $p->formats->{SQL99_datetime}, |
|
12 |
+ date => $p->formats->{SQL99_date}, |
|
13 |
+ time => $p->formats->{SQL99_time}, |
|
14 | 14 |
); |
15 | 15 |
|
16 | 16 |
sub connect { |
... | ... |
@@ -730,8 +730,8 @@ test 'Cache'; |
730 | 730 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
731 | 731 |
DBIx::Custom->query_cache_max(2); |
732 | 732 |
$dbi->do($CREATE_TABLE->{0}); |
733 |
-DBIx::Custom->delete_class_attr('_query_caches'); |
|
734 |
-DBIx::Custom->delete_class_attr('_query_cache_keys'); |
|
733 |
+delete $DBIx::Custom::CLASS_ATTRS->{_query_caches}; |
|
734 |
+delete $DBIx::Custom::CLASS_ATTRS->{_query_cache_keys}; |
|
735 | 735 |
$tmpls[0] = "insert into table1 {insert key1 key2}"; |
736 | 736 |
$queries[0] = $dbi->create_query($tmpls[0]); |
737 | 737 |
is(DBIx::Custom->_query_caches->{$tmpls[0]}{sql}, $queries[0]->sql, "$test : sql first"); |