Showing 3 changed files with 99 additions and 33 deletions
+1
Changes
... ...
@@ -1,6 +1,7 @@
1 1
 0.1715
2 2
     - default quote attribute in ODBC driver is changed to "[]"
3 3
     - fixed some bug in Microsoft SQL Server
4
+    - added EXPERIMENTAL execute method sqlfilter option
4 5
 0.1714
5 6
     - fixed not backword compatible change in 0.1712 query_buider
6 7
 0.1713
+81 -33
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package DBIx::Custom;
2 2
 use Object::Simple -base;
3 3
 
4
-our $VERSION = '0.1714';
4
+our $VERSION = '0.1715';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -344,7 +344,7 @@ sub each_table {
344 344
 
345 345
 our %VALID_ARGS = map { $_ => 1 } qw/append allow_delete_all
346 346
   allow_update_all bind_type column filter id join param prefix primary_key
347
-  query relation table table_alias type type_rule_off type_rule1_off
347
+  query relation sqlfilter table table_alias type type_rule_off type_rule1_off
348 348
   type_rule2_off wrap/;
349 349
 
350 350
 sub execute {
... ...
@@ -370,6 +370,7 @@ sub execute {
370 370
     };
371 371
     my $query_return = delete $args{query};
372 372
     my $table_alias = delete $args{table_alias} || {};
373
+    my $sqlfilter = $args{sqlfilter};
373 374
     
374 375
     # Check argument names
375 376
     foreach my $name (keys %args) {
... ...
@@ -377,8 +378,7 @@ sub execute {
377 378
           unless $VALID_ARGS{$name};
378 379
     }
379 380
     
380
-    # Create query
381
-    $query = $self->_create_query($query) unless ref $query;
381
+    $query = $self->_create_query($query, $sqlfilter) unless ref $query;
382 382
     
383 383
     # Save query
384 384
     $self->last_sql($query->sql);
... ...
@@ -448,7 +448,7 @@ sub execute {
448 448
         $type_filters,
449 449
         $bind_type
450 450
     );
451
-    
451
+
452 452
     # Execute
453 453
     my $sth = $query->sth;
454 454
     my $affected;
... ...
@@ -1119,7 +1119,7 @@ sub where { DBIx::Custom::Where->new(dbi => shift, @_) }
1119 1119
 
1120 1120
 sub _create_query {
1121 1121
     
1122
-    my ($self, $source) = @_;
1122
+    my ($self, $source, $sqlfilter) = @_;
1123 1123
     
1124 1124
     # Cache
1125 1125
     my $cache = $self->cache;
... ...
@@ -1163,7 +1163,16 @@ sub _create_query {
1163 1163
             }
1164 1164
         ) if $cache;
1165 1165
     }
1166
-    
1166
+
1167
+    # Filter SQL
1168
+    if ($sqlfilter) {
1169
+        my $sql = $query->sql;
1170
+        $sql =~ s/\s*;$//;
1171
+        $sql = $sqlfilter->($sql);
1172
+        $sql .= ';';
1173
+        $query->sql($sql);
1174
+    }
1175
+        
1167 1176
     # Save sql
1168 1177
     $self->last_sql($query->sql);
1169 1178
     
... ...
@@ -2253,6 +2262,17 @@ The following opitons are available.
2253 2262
 
2254 2263
 =over 4
2255 2264
 
2265
+=item C<bind_type>
2266
+
2267
+Specify database bind data type.
2268
+
2269
+    bind_type => [image => DBI::SQL_BLOB]
2270
+    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2271
+
2272
+This is used to bind parameter by C<bind_param> of statment handle.
2273
+
2274
+    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2275
+
2256 2276
 =item C<filter>
2257 2277
     
2258 2278
     filter => {
... ...
@@ -2313,6 +2333,29 @@ Note that $row must be simple hash reference, such as
2313 2333
 {title => 'Perl', author => 'Ken'}.
2314 2334
 and don't forget to sort $row values by $row key asc order.
2315 2335
 
2336
+=item C<sqlfilter EXPERIMENTAL> 
2337
+
2338
+SQL filter function.
2339
+
2340
+    sqlfilter => $code_ref
2341
+
2342
+This option is generally for Oracle and SQL Server paging process.
2343
+    
2344
+    my $limit = sub {
2345
+        my ($sql, $count, $offset) = @_;
2346
+        
2347
+        my $min = $offset + 1;
2348
+        my $max = $offset + $count;
2349
+        
2350
+        $sql = "select * from ( $sql ) as t where rnum >= $min rnum <= $max";
2351
+        
2352
+        return $sql;
2353
+    }
2354
+    $dbi->select(... column => ['ROWNUM rnom'], sqlfilter => sub {
2355
+        my $sql = shift;
2356
+        return $limit->($sql, 100, 50);
2357
+    })
2358
+
2316 2359
 =item C<table>
2317 2360
     
2318 2361
     table => 'author'
... ...
@@ -2329,17 +2372,6 @@ You must set C<table> option.
2329 2372
       "select * from book where title = :book.title and author = :book.author",
2330 2373
       {title => 'Perl', author => 'Ken');
2331 2374
 
2332
-=item C<bind_type>
2333
-
2334
-Specify database bind data type.
2335
-
2336
-    bind_type => [image => DBI::SQL_BLOB]
2337
-    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2338
-
2339
-This is used to bind parameter by C<bind_param> of statment handle.
2340
-
2341
-    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2342
-
2343 2375
 =item C<table_alias> EXPERIMENTAL
2344 2376
 
2345 2377
     table_alias => {user => 'hiker'}
... ...
@@ -2416,6 +2448,10 @@ prefix before table name section.
2416 2448
 
2417 2449
 Same as C<execute> method's C<query> option.
2418 2450
 
2451
+=item C<sqlfilter EXPERIMENTAL>
2452
+
2453
+Same as C<execute> method's C<sqlfilter> option.
2454
+
2419 2455
 =item C<table>
2420 2456
 
2421 2457
     table => 'book'
... ...
@@ -2479,6 +2515,10 @@ The following opitons are available.
2479 2515
 
2480 2516
 Same as C<select> method's C<append> option.
2481 2517
 
2518
+=item C<bind_type>
2519
+
2520
+Same as C<execute> method's C<bind_type> option.
2521
+
2482 2522
 =item C<filter>
2483 2523
 
2484 2524
 Same as C<execute> method's C<filter> option.
... ...
@@ -2524,16 +2564,16 @@ Primary key. This is used by C<id> option.
2524 2564
 
2525 2565
 Same as C<execute> method's C<query> option.
2526 2566
 
2567
+=item C<sqlfilter EXPERIMENTAL>
2568
+
2569
+Same as C<execute> method's C<sqlfilter> option.
2570
+
2527 2571
 =item C<table>
2528 2572
 
2529 2573
     table => 'book'
2530 2574
 
2531 2575
 Table name.
2532 2576
 
2533
-=item C<bind_type>
2534
-
2535
-Same as C<execute> method's C<bind_type> option.
2536
-
2537 2577
 =item C<type_rule_off> EXPERIMENTAL
2538 2578
 
2539 2579
 Same as C<execute> method's C<type_rule_off> option.
... ...
@@ -2832,6 +2872,10 @@ The following opitons are available.
2832 2872
     append => 'order by title'
2833 2873
 
2834 2874
 Append statement to last of SQL.
2875
+
2876
+=item C<bind_type>
2877
+
2878
+Same as C<execute> method's C<bind_type> option.
2835 2879
     
2836 2880
 =item C<column>
2837 2881
     
... ...
@@ -2968,9 +3012,9 @@ Primary key. This is used by C<id> option.
2968 3012
 
2969 3013
 Same as C<execute> method's C<query> option.
2970 3014
 
2971
-=item C<bind_type>
3015
+=item C<sqlfilter EXPERIMENTAL>
2972 3016
 
2973
-Same as C<execute> method's C<bind_type> option.
3017
+Same as C<execute> method's C<sqlfilter> option
2974 3018
 
2975 3019
 =item C<table>
2976 3020
 
... ...
@@ -3026,7 +3070,7 @@ Where clause.
3026 3070
 
3027 3071
 Wrap statement. This is array reference.
3028 3072
 
3029
-    $dbi->select(wrap => ['select * from (', ') as t where ROWNUM < 10']);
3073
+    wrap => ['select * from (', ') as t where ROWNUM < 10']
3030 3074
 
3031 3075
 This option is for Oracle and SQL Server paging process.
3032 3076
 
... ...
@@ -3051,6 +3095,10 @@ The following opitons are available.
3051 3095
 
3052 3096
 Same as C<select> method's C<append> option.
3053 3097
 
3098
+=item C<bind_type>
3099
+
3100
+Same as C<execute> method's C<bind_type> option.
3101
+
3054 3102
 =item C<filter>
3055 3103
 
3056 3104
 Same as C<execute> method's C<filter> option.
... ...
@@ -3097,20 +3145,16 @@ Primary key. This is used by C<id> option.
3097 3145
 
3098 3146
 Same as C<execute> method's C<query> option.
3099 3147
 
3148
+=item C<sqlfilter EXPERIMENTAL>
3149
+
3150
+Same as C<execute> method's C<sqlfilter> option.
3151
+
3100 3152
 =item C<table>
3101 3153
 
3102 3154
     table => 'book'
3103 3155
 
3104 3156
 Table name.
3105 3157
 
3106
-=item C<where>
3107
-
3108
-Same as C<select> method's C<where> option.
3109
-
3110
-=item C<bind_type>
3111
-
3112
-Same as C<execute> method's C<bind_type> option.
3113
-
3114 3158
 =item C<type_rule_off> EXPERIMENTAL
3115 3159
 
3116 3160
 Same as C<execute> method's C<type_rule_off> option.
... ...
@@ -3127,6 +3171,10 @@ Same as C<execute> method's C<type_rule1_off> option.
3127 3171
 
3128 3172
 Same as C<execute> method's C<type_rule2_off> option.
3129 3173
 
3174
+=item C<where>
3175
+
3176
+Same as C<select> method's C<where> option.
3177
+
3130 3178
 =back
3131 3179
 
3132 3180
 =head2 C<update_all>
+17
t/common.t
... ...
@@ -3411,6 +3411,23 @@ $dbi->select(
3411 3411
 };
3412 3412
 like($@, qr/array/);
3413 3413
 
3414
+test 'select() sqlfilter option';
3415
+$dbi = DBIx::Custom->connect;
3416
+eval { $dbi->execute('drop table table1') };
3417
+$dbi->execute($create_table1);
3418
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
3419
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
3420
+$rows = $dbi->select(
3421
+    table => 'table1',
3422
+    column => 'key1',
3423
+    sqlfilter => sub {
3424
+        my $sql = shift;
3425
+        $sql = "select * from ( $sql ) as t where key1 = 1";
3426
+        return $sql;
3427
+    }
3428
+)->all;
3429
+is_deeply($rows, [{key1 => 1}]);
3430
+
3414 3431
 test 'dbi method from model';
3415 3432
 $dbi = MyDBI9->connect;
3416 3433
 eval { $dbi->execute('drop table table1') };