... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1714 |
|
2 |
+ - fixed not backword compatible change in 0.1712 query_buider |
|
1 | 3 |
0.1713 |
2 | 4 |
- fixed memory leak and connection increasing bug when using model. |
3 | 5 |
0.1712 |
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
use Object::Simple -base; |
3 | 3 |
|
4 |
-our $VERSION = '0.1713'; |
|
4 |
+our $VERSION = '0.1714'; |
|
5 | 5 |
use 5.008001; |
6 | 6 |
|
7 | 7 |
use Carp 'croak'; |
... | ... |
@@ -15,7 +15,7 @@ use DBIx::Custom::Tag; |
15 | 15 |
use DBIx::Custom::Order; |
16 | 16 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
17 | 17 |
use Encode qw/encode encode_utf8 decode_utf8/; |
18 |
-use Scalar::Util qw/weaken isweak/; |
|
18 |
+use Scalar::Util qw/weaken/; |
|
19 | 19 |
|
20 | 20 |
use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0; |
21 | 21 |
use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8'; |
... | ... |
@@ -52,6 +52,12 @@ has [qw/connector dsn password quote user exclude_table/], |
52 | 52 |
}, |
53 | 53 |
last_sql => '', |
54 | 54 |
models => sub { {} }, |
55 |
+ query_builder => sub { |
|
56 |
+ my $self = shift; |
|
57 |
+ my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self); |
|
58 |
+ weaken $builder->{dbi}; |
|
59 |
+ return $builder; |
|
60 |
+ }, |
|
55 | 61 |
result_class => 'DBIx::Custom::Result', |
56 | 62 |
safety_character => '\w', |
57 | 63 |
separator => '.', |
... | ... |
@@ -252,9 +258,7 @@ sub delete { |
252 | 258 |
|
253 | 259 |
sub delete_all { shift->delete(allow_delete_all => 1, @_) } |
254 | 260 |
|
255 |
-sub DESTROY { |
|
256 |
- |
|
257 |
-} |
|
261 |
+sub DESTROY { } |
|
258 | 262 |
|
259 | 263 |
sub create_model { |
260 | 264 |
my $self = shift; |
... | ... |
@@ -269,7 +273,7 @@ sub create_model { |
269 | 273 |
|
270 | 274 |
# Create model |
271 | 275 |
my $model = $model_class->new($args); |
272 |
- weaken $self unless isweak $self; |
|
276 |
+ weaken $model->{dbi}; |
|
273 | 277 |
$model->name($model_name) unless $model->name; |
274 | 278 |
$model->table($model_table) unless $model->table; |
275 | 279 |
|
... | ... |
@@ -746,24 +750,9 @@ sub new { |
746 | 750 |
croak qq{"$attr" is wrong name } . _subname |
747 | 751 |
unless $self->can($attr); |
748 | 752 |
} |
749 |
- |
|
750 |
- return $self; |
|
751 |
-} |
|
752 |
- |
|
753 |
-my $not_exists = bless {}, 'DBIx::Custom::NotExists'; |
|
754 |
-sub not_exists { $not_exists } |
|
755 |
- |
|
756 |
-sub order { |
|
757 |
- my $self = shift; |
|
758 |
- return DBIx::Custom::Order->new(dbi => $self, @_); |
|
759 |
-} |
|
760 | 753 |
|
761 |
-sub query_builder { |
|
762 |
- my $self = shift; |
|
763 |
- my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self); |
|
764 |
- |
|
765 | 754 |
# DEPRECATED |
766 |
- $builder->register_tag( |
|
755 |
+ $self->{_tags} = { |
|
767 | 756 |
'?' => \&DBIx::Custom::Tag::placeholder, |
768 | 757 |
'=' => \&DBIx::Custom::Tag::equal, |
769 | 758 |
'<>' => \&DBIx::Custom::Tag::not_equal, |
... | ... |
@@ -775,9 +764,17 @@ sub query_builder { |
775 | 764 |
'in' => \&DBIx::Custom::Tag::in, |
776 | 765 |
'insert_param' => \&DBIx::Custom::Tag::insert_param, |
777 | 766 |
'update_param' => \&DBIx::Custom::Tag::update_param |
778 |
- ); |
|
779 |
- $builder->register_tag($self->{_tags} || {}); |
|
780 |
- return $builder; |
|
767 |
+ }; |
|
768 |
+ |
|
769 |
+ return $self; |
|
770 |
+} |
|
771 |
+ |
|
772 |
+my $not_exists = bless {}, 'DBIx::Custom::NotExists'; |
|
773 |
+sub not_exists { $not_exists } |
|
774 |
+ |
|
775 |
+sub order { |
|
776 |
+ my $self = shift; |
|
777 |
+ return DBIx::Custom::Order->new(dbi => $self, @_); |
|
781 | 778 |
} |
782 | 779 |
|
783 | 780 |
sub register_filter { |
... | ... |
@@ -248,8 +248,9 @@ sub _parse_tag { |
248 | 248 |
next; |
249 | 249 |
} |
250 | 250 |
# Get tag |
251 |
+ $self->dbi->{_tags} ||= {}; |
|
251 | 252 |
my $tag = $self->tag_processors->{$tag_name} |
252 |
- || $self->tags->{$tag_name}; |
|
253 |
+ || $self->dbi->{_tags}->{$tag_name}; |
|
253 | 254 |
# Tag is not registered |
254 | 255 |
croak qq{Tag "$tag_name" is not registered } . _subname |
255 | 256 |
unless $tag; |
... | ... |
@@ -4,6 +4,7 @@ use warnings; |
4 | 4 |
use Encode qw/encode_utf8/; |
5 | 5 |
use FindBin; |
6 | 6 |
use lib "$FindBin::Bin/common"; |
7 |
+use Scalar::Util 'isweak'; |
|
7 | 8 |
|
8 | 9 |
my $dbi; |
9 | 10 |
|
... | ... |
@@ -208,6 +209,7 @@ $dbi->insert({key1 => '01-01'}, table => 'table1'); |
208 | 209 |
$result = $dbi->select(table => 'table1'); |
209 | 210 |
is($result->one->{key1}, '2010-01-01'); |
210 | 211 |
|
212 |
+$DB::single = 1; |
|
211 | 213 |
$dbi = DBIx::Custom->connect; |
212 | 214 |
eval { $dbi->execute('drop table table1') }; |
213 | 215 |
$dbi->execute($create_table1_type); |
... | ... |
@@ -3142,6 +3144,7 @@ is_deeply($result->all, [{'table1-key1' => 1, 'table1-key2' => 3}, |
3142 | 3144 |
{'table1-key1' => 2, 'table1-key2' => 2}]); |
3143 | 3145 |
|
3144 | 3146 |
test 'tag_parse'; |
3147 |
+$DB::single = 1; |
|
3145 | 3148 |
$dbi = DBIx::Custom->connect; |
3146 | 3149 |
$dbi->tag_parse(0); |
3147 | 3150 |
eval { $dbi->execute('drop table table1') }; |
... | ... |
@@ -3327,14 +3330,16 @@ $datas = [ |
3327 | 3330 |
|
3328 | 3331 |
for (my $i = 0; $i < @$datas; $i++) { |
3329 | 3332 |
my $data = $datas->[$i]; |
3330 |
- my $builder = DBIx::Custom->new->query_builder; |
|
3333 |
+ my $dbi = DBIx::Custom->new; |
|
3334 |
+ my $builder = $dbi->query_builder; |
|
3331 | 3335 |
my $query = $builder->build_query($data->{source}); |
3332 | 3336 |
is($query->{sql}, $data->{sql_expected}, "$data->{name} : sql"); |
3333 | 3337 |
is_deeply($query->columns, $data->{columns_expected}, "$data->{name} : columns"); |
3334 | 3338 |
} |
3335 | 3339 |
|
3336 |
-$builder = DBIx::Custom->new->query_builder; |
|
3337 |
-$ret_val = $builder->register_tag( |
|
3340 |
+$dbi = DBIx::Custom->new; |
|
3341 |
+$builder = $dbi->query_builder; |
|
3342 |
+$dbi->register_tag( |
|
3338 | 3343 |
p => sub { |
3339 | 3344 |
my @args = @_; |
3340 | 3345 |
|
... | ... |
@@ -3347,9 +3352,6 @@ $ret_val = $builder->register_tag( |
3347 | 3352 |
$query = $builder->build_query("{p a b}"); |
3348 | 3353 |
is($query->{sql}, "? a b;", "register_tag sql"); |
3349 | 3354 |
is_deeply($query->{columns}, [2], "register_tag columns"); |
3350 |
-isa_ok($ret_val, 'DBIx::Custom::QueryBuilder'); |
|
3351 |
- |
|
3352 |
-$builder = DBIx::Custom->new->query_builder; |
|
3353 | 3355 |
|
3354 | 3356 |
eval{$builder->build_query('{? }')}; |
3355 | 3357 |
like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
... | ... |
@@ -3357,35 +3359,33 @@ like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments"); |
3357 | 3359 |
eval{$builder->build_query("{a }")}; |
3358 | 3360 |
like($@, qr/\QTag "a" is not registered/, "tag not exist"); |
3359 | 3361 |
|
3360 |
-$builder->register_tag({ |
|
3362 |
+$dbi->register_tag({ |
|
3361 | 3363 |
q => 'string' |
3362 | 3364 |
}); |
3363 | 3365 |
|
3364 | 3366 |
eval{$builder->build_query("{q}", {})}; |
3365 | 3367 |
like($@, qr/Tag "q" must be sub reference/, "tag not code ref"); |
3366 | 3368 |
|
3367 |
-$builder->register_tag({ |
|
3369 |
+$dbi->register_tag({ |
|
3368 | 3370 |
r => sub {} |
3369 | 3371 |
}); |
3370 | 3372 |
|
3371 | 3373 |
eval{$builder->build_query("{r}")}; |
3372 | 3374 |
like($@, qr/\QTag "r" must return [STRING, ARRAY_REFERENCE]/, "tag return noting"); |
3373 | 3375 |
|
3374 |
-$builder->register_tag({ |
|
3376 |
+$dbi->register_tag({ |
|
3375 | 3377 |
s => sub { return ["a", ""]} |
3376 | 3378 |
}); |
3377 | 3379 |
|
3378 | 3380 |
eval{$builder->build_query("{s}")}; |
3379 | 3381 |
like($@, qr/\QTag "s" must return [STRING, ARRAY_REFERENCE]/, "tag return not array columns"); |
3380 | 3382 |
|
3381 |
-$builder->register_tag( |
|
3383 |
+$dbi->register_tag( |
|
3382 | 3384 |
t => sub {return ["a", []]} |
3383 | 3385 |
); |
3384 | 3386 |
|
3385 | 3387 |
|
3386 |
-test 'General error case'; |
|
3387 |
-$builder = DBIx::Custom->new->query_builder; |
|
3388 |
-$builder->register_tag( |
|
3388 |
+$dbi->register_tag( |
|
3389 | 3389 |
a => sub { |
3390 | 3390 |
return ["? ? ?", ['']]; |
3391 | 3391 |
} |