Showing 3 changed files with 32 additions and 2 deletions
+1
Changes
... ...
@@ -1,5 +1,6 @@
1 1
 0.1698
2 2
     - fixed DBIx::Custom::Where to_string method small bug
3
+    - added EXPERIMENTAL execute method table_alias option
3 4
 0.1697
4 5
     - added EXPERIMENTAL map_param method
5 6
 0.1696
+17 -2
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.1697';
4
+our $VERSION = '0.1698';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -19,7 +19,7 @@ use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
19 19
 use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
20 20
 
21 21
 our @COMMON_ARGS = qw/bind_type table query filter id primary_key
22
-                      type_rule_off type_rule1_off type_rule2_off type/;
22
+  type_rule_off type_rule1_off type_rule2_off type table_alias/;
23 23
 
24 24
 has [qw/connector dsn password quote user/],
25 25
     cache => 0,
... ...
@@ -305,6 +305,7 @@ sub execute {
305 305
         2 => delete $args{type_rule2_off}
306 306
     };
307 307
     my $query_return = delete $args{query};
308
+    my $table_alias = delete $args{table_alias} || {};
308 309
     
309 310
     # Check argument names
310 311
     foreach my $name (keys %args) {
... ...
@@ -340,11 +341,17 @@ sub execute {
340 341
             foreach my $i (1 .. 2) {
341 342
                 unless ($type_rule_off_parts->{$i}) {
342 343
                     my $into = $self->{"_into$i"} || {};
344
+                    
345
+                    my $alias = $table;
346
+                    $table = $table_alias->{$alias}
347
+                      if defined $alias && $table_alias->{$alias};
348
+                    
343 349
                     if (defined $table && $into->{$table} &&
344 350
                         (my $rule = $into->{$table}->{$column}))
345 351
                     {
346 352
                         $type_filters->{$i}->{$column} = $rule;
347 353
                         $type_filters->{$i}->{"$table.$column"} = $rule;
354
+                        $type_filters->{$i}->{"$alias.$column"} = $rule if $alias ne $table;
348 355
                     }
349 356
                 }
350 357
             }
... ...
@@ -2125,6 +2132,14 @@ This is used to bind parameter by C<bind_param> of statment handle.
2125 2132
 
2126 2133
     $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2127 2134
 
2135
+=item C<table_alias> EXPERIMENTAL
2136
+
2137
+    table_alias => {user => 'hiker'}
2138
+
2139
+Table alias. Key is real table name, value is alias table name.
2140
+If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2141
+on alias table name.
2142
+
2128 2143
 =item C<type_rule_off> EXPERIMENTAL
2129 2144
 
2130 2145
     type_rule_off => 1
+14
t/dbix-custom-core-sqlite.t
... ...
@@ -3272,4 +3272,18 @@ $param = $dbi->map_param(
3272 3272
 );
3273 3273
 is_deeply($param, {'book.price' => '%a'});
3274 3274
 
3275
+
3276
+test 'table_alias';
3277
+$dbi = DBIx::Custom->connect(dsn => 'dbi:SQLite:dbname=:memory:');
3278
+$dbi->execute("create table table1 (key1 Date, key2 datetime)");
3279
+$dbi->type_rule(
3280
+    into1 => {
3281
+        date => sub { uc $_[0] }
3282
+    }
3283
+);
3284
+$dbi->execute("insert into table1 (key1) values (:table2.key1)", {'table2.key1' => 'a'},
3285
+  table_alias => {table2 => 'table1'});
3286
+$result = $dbi->select(table => 'table1');
3287
+is($result->one->{key1}, 'A');
3288
+
3275 3289
 =cut