Showing 4 changed files with 72 additions and 0 deletions
+1
Changes
... ...
@@ -1,4 +1,5 @@
1 1
 0.1686
2
+    - added EXPERIMENTAL col method.
2 3
     - set reserved_word_quote automatically from driver name
3 4
 0.1685
4 5
     - insert_at, update_at, delete_at, select_at is DEPRECATED!
+14
lib/DBIx/Custom.pm
... ...
@@ -171,6 +171,20 @@ sub assign_param {
171 171
     return $tag;
172 172
 }
173 173
 
174
+sub col {
175
+    my ($self, $table, $columns) = @_;
176
+    
177
+    # Reserved word quote
178
+    my $q = $self->reserved_word_quote;
179
+    
180
+    # Column clause
181
+    my @column;
182
+    $columns ||= [];
183
+    push @column, "$q$table$q.$q$_$q as $q${table}.$_$q" for @$columns;
184
+    
185
+    return join (', ', @column);
186
+}
187
+
174 188
 sub column {
175 189
     my ($self, $table, $columns) = @_;
176 190
     
+29
lib/DBIx/Custom/Model.pm
... ...
@@ -85,6 +85,23 @@ sub column {
85 85
     return $self->dbi->column($table, $columns);
86 86
 }
87 87
 
88
+sub col {
89
+    my ($self, $table, $columns) = @_;
90
+    
91
+    $self->{_table_alias} ||= {};
92
+    my $dist;
93
+    $dist = $self->dbi->{_table_alias}{$table}
94
+          ? $self->dbi->{_table_alias}{$table}
95
+          : $table;
96
+    
97
+    $self->dbi->{_model_from} ||= {};
98
+    my $model = $self->dbi->{_model_from}->{$dist};
99
+    
100
+    $columns ||= $self->model($model)->columns;
101
+    
102
+    return $self->dbi->col($table, $columns);
103
+}
104
+
88 105
 sub DESTROY { }
89 106
 
90 107
 sub method {
... ...
@@ -225,6 +242,18 @@ Create column clause. The follwoing column clause is created.
225 242
 
226 243
 If column names is omitted, C<columns> attribute of the model is used.
227 244
 
245
+=head2 C<col> EXPERIMETNAL
246
+
247
+    my $column = $self->col(book => ['author', 'title']);
248
+    my $column = $self->col('book');
249
+
250
+Create column clause. The follwoing column clause is created.
251
+
252
+    book.author as "book.author",
253
+    book.title as "book.title"
254
+
255
+If column names is omitted, C<columns> attribute of the model is used.
256
+
228 257
 =head2 C<delete>
229 258
 
230 259
     $table->delete(...);
+28
t/dbix-custom-core-sqlite.t
... ...
@@ -1457,6 +1457,12 @@ $result = $dbi->select(
1457 1457
 );
1458 1458
 is($result->fetch_first->[0], 'B');
1459 1459
 
1460
+$result = $dbi->select(
1461
+    table => 'company', relation => {'company.location_id' => 'location.id'},
1462
+    column => ['location.name as "location.name"']
1463
+);
1464
+is($result->fetch_first->[0], 'B');
1465
+
1460 1466
 test 'Model class';
1461 1467
 use MyDBI1;
1462 1468
 $dbi = MyDBI1->connect($NEW_ARGS->{0});
... ...
@@ -2516,4 +2522,26 @@ is($row->{key1}, 1);
2516 2522
 is($row->{key2}, 2);
2517 2523
 is($row->{key3}, 3);
2518 2524
 
2525
+test 'col';
2526
+$dbi = MyDBI7->connect($NEW_ARGS->{0});
2527
+$dbi->execute($CREATE_TABLE->{0});
2528
+$dbi->execute($CREATE_TABLE->{2});
2529
+$dbi->setup_model;
2530
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2531
+$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 3});
2532
+$model = $dbi->model('table1');
2533
+$result = $model->select(
2534
+    column => [$model->col('table2')],
2535
+    where => {'table1.key1' => 1}
2536
+);
2537
+is_deeply($result->one,
2538
+          {'table2.key1' => 1, 'table2.key3' => 3});
2539
+
2540
+$result = $model->select(
2541
+    column => [$model->col('table2' => [qw/key1 key3/])],
2542
+    where => {'table1.key1' => 1}
2543
+);
2544
+is_deeply($result->one,
2545
+          {'table2.key1' => 1, 'table2.key3' => 3});
2546
+
2519 2547
 =cut