Showing 9 changed files with 266 additions and 63 deletions
+1
Changes
... ...
@@ -1,5 +1,6 @@
1 1
 0.1712
2 2
     - you can set any string as separator
3
+    - fixed memory leak and connection increasing bug, and query_buider method return new object.
3 4
 0.1711
4 5
     - renamed EXPERIMENTAL available_type_name to available_typename
5 6
     - renamed EXPERIMENTAL available_data_type to available_datatype
+45 -27
lib/DBIx/Custom.pm
... ...
@@ -51,13 +51,34 @@ has [qw/connector dsn password quote user/],
51 51
     },
52 52
     last_sql => '',
53 53
     models => sub { {} },
54
-    query_builder => sub { DBIx::Custom::QueryBuilder->new(dbi => shift) },
55 54
     result_class  => 'DBIx::Custom::Result',
56 55
     safety_character => '\w',
57 56
     separator => '.',
58 57
     stash => sub { {} },
59 58
     tag_parse => 1;
60 59
 
60
+sub query_builder {
61
+    my $self = shift;
62
+    my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self);
63
+    
64
+    # DEPRECATED
65
+    $builder->register_tag(
66
+        '?'     => \&DBIx::Custom::Tag::placeholder,
67
+        '='     => \&DBIx::Custom::Tag::equal,
68
+        '<>'    => \&DBIx::Custom::Tag::not_equal,
69
+        '>'     => \&DBIx::Custom::Tag::greater_than,
70
+        '<'     => \&DBIx::Custom::Tag::lower_than,
71
+        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
72
+        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
73
+        'like'  => \&DBIx::Custom::Tag::like,
74
+        'in'    => \&DBIx::Custom::Tag::in,
75
+        'insert_param' => \&DBIx::Custom::Tag::insert_param,
76
+        'update_param' => \&DBIx::Custom::Tag::update_param
77
+    );
78
+    $builder->register_tag($self->{_tags} || {});
79
+    return $builder;
80
+}
81
+
61 82
 our $AUTOLOAD;
62 83
 sub AUTOLOAD {
63 84
     my $self = shift;
... ...
@@ -221,7 +242,9 @@ sub delete {
221 242
 
222 243
 sub delete_all { shift->delete(allow_delete_all => 1, @_) }
223 244
 
224
-sub DESTROY { }
245
+sub DESTROY {
246
+
247
+}
225 248
 
226 249
 sub create_model {
227 250
     my $self = shift;
... ...
@@ -707,21 +730,6 @@ sub new {
707 730
           unless $self->can($attr);
708 731
     }
709 732
     
710
-    # DEPRECATED!
711
-    $self->query_builder->{tags} = {
712
-        '?'     => \&DBIx::Custom::Tag::placeholder,
713
-        '='     => \&DBIx::Custom::Tag::equal,
714
-        '<>'    => \&DBIx::Custom::Tag::not_equal,
715
-        '>'     => \&DBIx::Custom::Tag::greater_than,
716
-        '<'     => \&DBIx::Custom::Tag::lower_than,
717
-        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
718
-        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
719
-        'like'  => \&DBIx::Custom::Tag::like,
720
-        'in'    => \&DBIx::Custom::Tag::in,
721
-        'insert_param' => \&DBIx::Custom::Tag::insert_param,
722
-        'update_param' => \&DBIx::Custom::Tag::update_param
723
-    };
724
-    
725 733
     return $self;
726 734
 }
727 735
 
... ...
@@ -1624,8 +1632,25 @@ sub insert_at {
1624 1632
 
1625 1633
 # DEPRECATED!
1626 1634
 sub register_tag {
1635
+    my $self = shift;
1636
+    
1627 1637
     warn "register_tag is DEPRECATED!";
1628
-    shift->query_builder->register_tag(@_)
1638
+    
1639
+    # Merge tag
1640
+    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1641
+    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1642
+    
1643
+    return $self;
1644
+}
1645
+
1646
+# DEPRECATED!
1647
+sub register_tag_processor {
1648
+    my $self = shift;
1649
+    warn "register_tag_processor is DEPRECATED!";
1650
+    # Merge tag
1651
+    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1652
+    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1653
+    return $self;
1629 1654
 }
1630 1655
 
1631 1656
 # DEPRECATED!
... ...
@@ -1690,12 +1715,6 @@ sub insert_param_tag {
1690 1715
     return shift->insert_param(@_);
1691 1716
 }
1692 1717
 
1693
-# DEPRECATED!
1694
-sub register_tag_processor {
1695
-    warn "register_tag_processor is DEPRECATED!";
1696
-    return shift->query_builder->register_tag_processor(@_);
1697
-}
1698
-
1699 1718
 # DEPRECATED!
1700 1719
 sub update_param_tag {
1701 1720
     warn "update_param_tag is DEPRECATED! " .
... ...
@@ -1936,10 +1955,9 @@ Password, used when C<connect> method is executed.
1936 1955
 
1937 1956
 =head2 C<query_builder>
1938 1957
 
1939
-    my $sql_class = $dbi->query_builder;
1940
-    $dbi = $dbi->query_builder(DBIx::Custom::QueryBuilder->new);
1958
+    my $builder = $dbi->query_builder;
1941 1959
 
1942
-Query builder, default to L<DBIx::Custom::QueryBuilder> object.
1960
+Creat query builder. This is L<DBIx::Custom::QueryBuilder>.
1943 1961
 
1944 1962
 =head2 C<quote>
1945 1963
 
+16 -4
t/common-mysql.t
... ...
@@ -10,10 +10,22 @@ use DBIx::Custom;
10 10
 {
11 11
     package DBIx::Custom;
12 12
 
13
-    my $date = 'Date';
14
-    my $time = 'Time';
15
-    my $datetime = 'Datetime';
13
+    my $date_typename = 'Date';
14
+    my $time_typename = 'Time';
15
+    my $datetime_typename = 'Datetime';
16 16
     
17
+    sub date_typename { lc $date_typename }
18
+    sub time_typename { lc $time_typename }
19
+    sub datetime_typename { lc $datetime_typename }
20
+
21
+    my $date_datatype = 9;
22
+    my $time_datatype = 'Time';
23
+    my $datetime_datatype = 11;
24
+    
25
+    sub date_datatype { lc $date_datatype }
26
+    sub time_datatype { lc $time_datatype }
27
+    sub datetime_datatype { lc $datetime_datatype }
28
+
17 29
     no warnings 'redefine';
18 30
     has dsn => "dbi:mysql:database=dbix_custom";
19 31
     has user => 'dbix_custom';
... ...
@@ -22,7 +34,7 @@ use DBIx::Custom;
22 34
     sub create_table1 { 'create table table1 (key1 varchar(255), key2 varchar(255)) engine=InnoDB;' }
23 35
     sub create_table1_2 {'create table table1 (key1 varchar(255), key2 varchar(255), '
24 36
      . 'key3 varchar(255), key4 varchar(255), key5 varchar(255)) engine=InnoDB;' }
25
-    sub create_table1_type { 'create table table1 (key1 Date, key2 datetime) engine=InnoDB;;' }
37
+    sub create_table1_type { "create table table1 (key1 $date_typename, key2 $datetime_typename) engine=InnoDB;" }
26 38
     sub create_table1_highperformance { "create table table1 (ab varchar(255), bc varchar(255), "
27 39
       . "ik varchar(255), hi varchar(255), ui varchar(255), pq varchar(255), dc varchar(255)) engine=InnoDB;" }
28 40
     sub create_table2 { 'create table table2 (key1 varchar(255), key3 varchar(255)) engine=InnoDB;' }
+16 -4
t/common-postgresql.t
... ...
@@ -11,9 +11,21 @@ use DBIx::Custom;
11 11
     package DBIx::Custom;
12 12
     no warnings 'redefine';
13 13
 
14
-    my $date = 'Date';
15
-    my $time = 'Time';
16
-    my $datetime = 'Timestamp';
14
+    my $date_typename = 'Date';
15
+    my $time_typename = 'Time';
16
+    my $datetime_typename = 'Timestamp';
17
+
18
+    sub date_typename { lc $date_typename }
19
+    sub time_typename { lc $time_typename }
20
+    sub datetime_typename { 'timestamp without time zone' }
21
+
22
+    my $date_datatype = 91;
23
+    my $time_datatype = 'Time';
24
+    my $datetime_datatype = 11;
25
+
26
+    sub date_datatype { lc $date_datatype }
27
+    sub time_datatype { lc $time_datatype }
28
+    sub datetime_datatype { lc $datetime_datatype }
17 29
 
18 30
     has dsn => "dbi:Pg:dbname=dbix_custom";
19 31
     has user  => 'dbix_custom';
... ...
@@ -22,7 +34,7 @@ use DBIx::Custom;
22 34
     sub create_table1 { 'create table table1 (key1 varchar(255), key2 varchar(255));' }
23 35
     sub create_table1_2 {'create table table1 (key1 varchar(255), key2 varchar(255), '
24 36
      . 'key3 varchar(255), key4 varchar(255), key5 varchar(255));' }
25
-    sub create_table1_type { 'create table table1 (key1 Date, key2 varchar(255));' }
37
+    sub create_table1_type { "create table table1 (key1 $date_typename, key2 $datetime_typename);" }
26 38
     sub create_table1_highperformance { "create table table1 (ab varchar(255), bc varchar(255), "
27 39
       . "ik varchar(255), hi varchar(255), ui varchar(255), pq varchar(255), dc varchar(255));" }
28 40
     sub create_table2 { 'create table table2 (key1 varchar(255), key3 varchar(255));' }
+16 -4
t/common-sqlite-quote.t
... ...
@@ -9,15 +9,27 @@ use DBIx::Custom;
9 9
     package DBIx::Custom;
10 10
     no warnings 'redefine';
11 11
 
12
-    my $date = 'Date';
13
-    my $time = 'Time';
14
-    my $datetime = 'Datetime';
12
+    my $date_typename = 'Date';
13
+    my $time_typename = 'Time';
14
+    my $datetime_typename = 'Datetime';
15
+    
16
+    sub date_typename { lc $date_typename }
17
+    sub time_typename { lc $time_typename }
18
+    sub datetime_typename { lc $datetime_typename }
19
+
20
+    my $date_datatype = 'Date';
21
+    my $time_datatype = 'Time';
22
+    my $datetime_datatype = 'Datetime';
23
+    
24
+    sub date_datatype { lc $date_datatype }
25
+    sub time_datatype { lc $time_datatype }
26
+    sub datetime_datatype { lc $datetime_datatype }
15 27
 
16 28
     has dsn => 'dbi:SQLite:dbname=:memory:';
17 29
     sub quote { '""' }
18 30
     sub create_table1 { 'create table table1 (key1 varchar, key2 varchar);' }
19 31
     sub create_table1_2 {'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);' }
20
-    sub create_table1_type { 'create table table1 (key1 Date, key2 datetime);' }
32
+    sub create_table1_type { "create table table1 (key1 $date_typename, key2 $datetime_typename);" }
21 33
     
22 34
     sub create_table1_highperformance { "create table table1 (ab varchar, bc varchar, ik varchar, hi varchar, ui varchar, pq varchar, dc varchar);" }
23 35
     
+17 -4
t/common-sqlite.t
... ...
@@ -9,14 +9,26 @@ use DBIx::Custom;
9 9
     package DBIx::Custom;
10 10
     no warnings 'redefine';
11 11
     
12
-    my $date = 'Date';
13
-    my $time = 'Time';
14
-    my $datetime = 'Datetime';
12
+    my $date_typename = 'Date';
13
+    my $time_typename = 'Time';
14
+    my $datetime_typename = 'Datetime';
15
+    
16
+    sub date_typename { lc $date_typename }
17
+    sub time_typename { lc $time_typename }
18
+    sub datetime_typename { lc $datetime_typename }
19
+
20
+    my $date_datatype = 'Date';
21
+    my $time_datatype = 'Time';
22
+    my $datetime_datatype = 'Datetime';
23
+    
24
+    sub date_datatype { lc $date_datatype }
25
+    sub time_datatype { lc $time_datatype }
26
+    sub datetime_datatype { lc $datetime_datatype }
15 27
     
16 28
     has dsn => 'dbi:SQLite:dbname=:memory:';
17 29
     sub create_table1 { 'create table table1 (key1 varchar, key2 varchar);' }
18 30
     sub create_table1_2 {'create table table1 (key1 varchar, key2 varchar, key3 varchar, key4 varchar, key5 varchar);' }
19
-    sub create_table1_type { "create table table1 (key1 $date, key2 $time);" }
31
+    sub create_table1_type { "create table table1 (key1 $date_typename, key2 $datetime_typename);" }
20 32
     
21 33
     sub create_table1_highperformance { "create table table1 (ab varchar, bc varchar, ik varchar, hi varchar, ui varchar, pq varchar, dc varchar);" }
22 34
     
... ...
@@ -24,6 +36,7 @@ use DBIx::Custom;
24 36
     sub create_table2_2 { "create table table2 (key1 varchar, key2 varchar, key3 varchar)" }
25 37
     sub create_table3 { "create table table3 (key1 varchar, key2 varchar, key3 varchar)" }
26 38
     sub create_table_reserved { 'create table "table" ("select" varchar, "update" varchar)' }
39
+    
27 40
 }
28 41
 
29 42
 require "$FindBin::Bin/common.t";
+154 -5
t/common.t
... ...
@@ -153,6 +153,12 @@ my $create_table3 = $dbi->create_table3;
153 153
 my $create_table_reserved = $dbi->create_table_reserved;
154 154
 my $q = substr($dbi->quote, 0, 1);
155 155
 my $p = substr($dbi->quote, 1, 1) || $q;
156
+my $date_typename = $dbi->date_typename;
157
+my $time_typename = $dbi->time_typename;
158
+my $datetime_typename = $dbi->datetime_typename;
159
+my $date_datatype = $dbi->date_datatype;
160
+my $time_datatype = $dbi->time_datatype;
161
+my $datetime_datatype = $dbi->datetime_datatype;
156 162
 
157 163
 # Variable
158 164
 # Variables
... ...
@@ -188,8 +194,151 @@ my $binary;
188 194
 
189 195
 # Drop table
190 196
 eval { $dbi->execute('drop table table1') };
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+test 'type_rule into';
212
+$dbi = DBIx::Custom->connect;
213
+eval { $dbi->execute('drop table table1') };
214
+$dbi->execute($create_table1_type);
215
+$dbi->type_rule(
216
+    into1 => {
217
+        $date_typename => sub { '2010-' . $_[0] }
218
+    }
219
+);
220
+$dbi->insert({key1 => '01-01'}, table => 'table1');
221
+$result = $dbi->select(table => 'table1');
222
+is($result->one->{key1}, '2010-01-01');
223
+
224
+$dbi = DBIx::Custom->connect;
225
+eval { $dbi->execute('drop table table1') };
226
+$dbi->execute($create_table1_type);
227
+$dbi->type_rule(
228
+    into1 => [
229
+         [$date_typename, $datetime_typename] => sub {
230
+            my $value = shift;
231
+            $value =~ s/02/03/g;
232
+            return $value;
233
+         }
234
+    ]
235
+);
236
+$dbi->insert({key1 => '2010-01-02', key2 => '2010-01-01 01:01:02'}, table => 'table1');
237
+$result = $dbi->select(table => 'table1');
238
+$row = $result->one;
239
+is($row->{key1}, '2010-01-03');
240
+is($row->{key2}, '2010-01-01 01:01:03');
241
+
242
+$dbi = DBIx::Custom->connect;
243
+eval { $dbi->execute('drop table table1') };
244
+$dbi->execute($create_table1_type);
245
+$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
246
+$dbi->type_rule(
247
+    into1 => [
248
+        [$date_typename, $datetime_typename] => sub {
249
+            my $value = shift;
250
+            $value =~ s/02/03/g;
251
+            return $value;
252
+        }
253
+    ]
254
+);
255
+$result = $dbi->execute(
256
+    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
257
+    param => {key1 => '2010-01-03', 'table1.key2' => '2010-01-01 01:01:02'}
258
+);
259
+$row = $result->one;
260
+is($row->{key1}, '2010-01-03');
261
+is($row->{key2}, '2010-01-01 01:01:03');
262
+
263
+$dbi = DBIx::Custom->connect;
264
+eval { $dbi->execute('drop table table1') };
265
+$dbi->execute($create_table1_type);
266
+$dbi->insert({key1 => '2010-01-03', key2 => '2010-01-01 01:01:03'}, table => 'table1');
267
+$dbi->type_rule(
268
+    into1 => [
269
+        [$date_typename, $datetime_typename] => sub {
270
+            my $value = shift;
271
+            $value =~ s/02/03/g;
272
+            return $value;
273
+        }
274
+    ]
275
+);
276
+$result = $dbi->execute(
277
+    "select * from table1 where key1 = :key1 and key2 = :table1.key2;",
278
+    param => {key1 => '2010-01-02', 'table1.key2' => '2010-01-01 01:01:02'},
279
+    table => 'table1'
280
+);
281
+$row = $result->one;
282
+is($row->{key1}, '2010-01-03');
283
+is($row->{key2}, '2010-01-01 01:01:03');
284
+
285
+$dbi = DBIx::Custom->connect;
286
+eval { $dbi->execute('drop table table1') };
287
+$dbi->execute($create_table1_type);
288
+$dbi->register_filter(convert => sub {
289
+    my $value = shift;
290
+    $value =~ s/02/03/;
291
+    return $value;
292
+});
293
+$dbi->type_rule(
294
+    from1 => {
295
+        $date_datatype => 'convert',
296
+    },
297
+    into1 => {
298
+        $date_typename => 'convert',
299
+    }
300
+);
301
+$dbi->insert({key1 => '2010-02-02'}, table => 'table1');
302
+$result = $dbi->select(table => 'table1');
303
+is($result->fetch->[0], '2010-03-03');
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
191 339
 
192 340
 # Create table
341
+eval { $dbi->execute('drop table table1') };
193 342
 $dbi->execute($create_table1);
194 343
 $model = $dbi->create_model(table => 'table1');
195 344
 $model->insert({key1 => 1, key2 => 2});
... ...
@@ -661,6 +810,7 @@ like($@, qr/"where" must be specified/,
661 810
 eval{$dbi->delete(table => 'table1', where => {';' => 1})};
662 811
 like($@, qr/safety/);
663 812
 
813
+$dbi = undef;
664 814
 $dbi = DBIx::Custom->connect;
665 815
 eval { $dbi->execute("drop table ${q}table$p") };
666 816
 $dbi->execute($create_table_reserved);
... ...
@@ -1707,14 +1857,14 @@ $dbi = DBIx::Custom->connect;
1707 1857
 $dbi->register_tag_processor(
1708 1858
     a => sub { 1 }
1709 1859
 );
1710
-is($dbi->query_builder->tag_processors->{a}->(), 1);
1860
+is($dbi->{_tags}->{a}->(), 1);
1711 1861
 
1712 1862
 test 'register_tag';
1713 1863
 $dbi = DBIx::Custom->connect;
1714 1864
 $dbi->register_tag(
1715 1865
     b => sub { 2 }
1716 1866
 );
1717
-is($dbi->query_builder->tags->{b}->(), 2);
1867
+is($dbi->{_tags}->{b}->(), 2);
1718 1868
 
1719 1869
 test 'table not specify exception';
1720 1870
 $dbi = DBIx::Custom->connect;
... ...
@@ -3346,13 +3496,14 @@ $result = $dbi->select(
3346 3496
 );
3347 3497
 is_deeply($result->all, [{'table2.key3' => 4}]);
3348 3498
 
3499
+
3349 3500
 test 'table_alias';
3350 3501
 $dbi = DBIx::Custom->connect;
3351 3502
 eval { $dbi->execute('drop table table1') };
3352 3503
 $dbi->execute($create_table1_type);
3353 3504
 $dbi->type_rule(
3354 3505
     into1 => {
3355
-        date => sub { '2010-' . $_[0] }
3506
+        $date_typename => sub { '2010-' . $_[0] }
3356 3507
     }
3357 3508
 );
3358 3509
 $dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => '01-01'},
... ...
@@ -3360,6 +3511,4 @@ $dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1'
3360 3511
 $result = $dbi->select(table => 'table1');
3361 3512
 is($result->one->{key1}, '2010-01-01');
3362 3513
 
3363
-
3364
-
3365 3514
 1;
+1 -1
t/mysql.t
... ...
@@ -43,7 +43,7 @@ $dbi->delete_all(table => 'table1');
43 43
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
44 44
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4});
45 45
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6});
46
-$dbi->query_builder->register_tag_processor(
46
+$dbi->register_tag(
47 47
     limit => sub {
48 48
         my ($count, $offset) = @_;
49 49
         
-14
t/sqlite.t
... ...
@@ -179,20 +179,6 @@ my $binary;
179 179
 $dbi = DBIx::Custom->connect;
180 180
 
181 181
 ### a little complex test
182
-test 'table_alias';
183
-$dbi = DBIx::Custom->connect;
184
-eval { $dbi->execute('drop table table1') };
185
-$dbi->execute("create table table1 (key1 Date, key2 datetime)");
186
-$dbi->type_rule(
187
-    into1 => {
188
-        date => sub { uc $_[0] }
189
-    }
190
-);
191
-$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
192
-  table_alias => {table2 => 'table1'});
193
-$result = $dbi->select(table => 'table1');
194
-is($result->one->{key1}, 'A');
195
-
196 182
 test 'type_rule into';
197 183
 $dbi = DBIx::Custom->connect;
198 184
 $dbi->execute("create table table1 (key1 Date, key2 datetime)");