File Coverage

blib/lib/Class/ReluctantORM/SQL/OutputColumn.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 8 0.0
condition 0 4 0.0
subroutine 7 10 70.0
pod 3 3 100.0
total 31 71 43.6


line stmt bran cond sub pod time code
1             package Class::ReluctantORM::SQL::OutputColumn;
2              
3             =head1 NAME
4              
5             Class::ReluctantORM::SQL::OutputColumn - Represent an Output Column from a SQL statment
6              
7             =head1 SYNOPSIS
8              
9             use Class::ReluctantORM::SQL::Aliases;
10              
11             # You get OutputColumns back from statements:
12             my @ocs = $sql->output_columns();
13              
14             # You can make them implicitly
15             my $oc = $sql->add_output(Column->new(column => 'foo'));
16             my $oc = $sql->add_output($expression);
17              
18             # Or explcitly
19             my $oc = OutputColumn->new($column);
20             my $oc = OutputColumn->new($expression);
21             $sql->add_output($oc);
22              
23             # Set/read column alias
24             my $alias = $oc->alias();
25             $oc->alias($new_alias);
26              
27             # Set/read PK flag (needed by some drivers)
28             $oc->is_primary_key(1);
29              
30             # Get expression - the "payload" of the output
31             # (usually just a Column)
32             my $exp = $oc->expression();
33              
34             # TODO DOCS - aggregate support
35              
36             # Fetching results
37             my $result = $oc->output_value();
38              
39             # Used in driver code when reading from a fetch
40             $oc->output_value($value);
41              
42             =head1 DESCRIPTION
43              
44             Represents an output "column" in a SELECT SQL statement (or an UPDATE or INSERT when used with RETURNING). Contains the column or expression that is the source of the data, any column alias, and provides access to the column value.
45              
46             =cut
47              
48 1     1   4 use strict;
  1         3  
  1         23  
49 1     1   5 use warnings;
  1         3  
  1         19  
50              
51 1     1   5 use Data::Dumper;
  1         2  
  1         40  
52 1     1   5 use Class::ReluctantORM::Exception;
  1         2  
  1         20  
53 1     1   5 use Class::ReluctantORM::Utilities qw(install_method check_args);
  1         3  
  1         40  
54              
55 1     1   12 use Class::ReluctantORM::SQL::Aliases;
  1         2  
  1         95  
56 1     1   5 use base 'Class::Accessor::Fast';
  1         2  
  1         404  
57              
58             our $DEBUG = 0;
59              
60             =head1 CONSTRUCTORS
61              
62             =cut
63              
64              
65             =head2 $col = OutputColumn->new($column);
66              
67             =head2 $col = OutputColumn->new($expression);
68              
69             =head2 $col = OutputColumn->new(expression => $expression, alias => $alias, is_primary_key => 0, ...);
70              
71             Makes a new OutputColumn object.
72              
73             In the first form, creates an OutputColumn sourced on the given Column.
74              
75             In the first form, creates an OutputColumn sourced on the given Expression. Since a Column is a subclass of Expression, forms one and two are actually the same.
76              
77             In the third form, the expression, column alias, and primary key flag are provided explicitly. Alias and primary key flag are optional.
78              
79             In the first and second forms, a column alias will be generated at render time if one is not assigned before then.
80              
81             =cut
82              
83             sub new {
84 0     0 1   my $class = shift;
85 0           my %args;
86 0 0         if (@_ == 1) {
    0          
87 0           $args{expression} = shift;
88             } elsif (@_ % 2) {
89 0           Class::ReluctantORM::Exception::Param::ExpectedHash->croak();
90             } else {
91 0           %args = @_;
92             }
93 0           %args = check_args(args => [ %args ], required => [qw(expression)], optional => [qw(alias is_primary_key)]);
94              
95 0           my $self = bless {}, $class;
96 0           $self->expression($args{expression});
97 0           $self->alias($args{alias});
98 0   0       $self->is_primary_key($args{is_primary_key} || 0);
99              
100             # Start with a NULL output
101 0           $self->set('output_value', undef);
102              
103 0           return $self;
104             }
105              
106              
107             =head1 ACCESSORS AND MUTATORS
108              
109             =cut
110              
111             =head2 $col_alias_name = $oc->alias();
112              
113             =head2 $oc->alias($col_alias_name);
114              
115             Reads or sets the column alias.
116              
117             =cut
118              
119             __PACKAGE__->mk_accessors(qw(alias));
120              
121             =head2 $exp = $oc->expression();
122              
123             =head2 $oc->expression($expression);
124              
125             Reads or sets the Expression that acts as the source for the data. $expression is commonly a simple Column.
126              
127             =cut
128              
129             __PACKAGE__->mk_accessors(qw(expression));
130              
131              
132             =head2 $value = $oc->output_value();
133              
134             =head2 $oc->output_value($value);
135              
136             Reads or sets the output value of the column. An undef should interpreted as NULL.
137              
138             =cut
139              
140             __PACKAGE__->mk_accessors(qw(output_value));
141              
142             =head2 $bool = $oc->is_primary_key();
143              
144             =head2 $oc->is_primary_key($bool);
145              
146             Reads or sets the primary key flag for the output column. Set to true if the column is a member of the primary key on the base table. Some drivers - those that don't support RETURNING clauses - require this to determine whether to do a second fetch to populate primary kyes ine memory.
147              
148             =cut
149              
150             __PACKAGE__->mk_accessors(qw(is_primary_key));
151              
152              
153             =head2 $str = $oc->pretty_print();
154              
155             Renders a human-readable representation of the OutputColumn.
156              
157             =cut
158              
159             sub pretty_print {
160 0     0 1   my $self = shift;
161 0           my %args = @_;
162 0 0         if ($args{one_line}) {
163 0 0         if ($self->alias) { return $self->alias; }
  0            
164 0           return $self->expression->pretty_print(%args);
165             } else {
166 0   0       return ($args{prefix} || '' ) . 'OUTPUT ' . $self->pretty_print(one_line => 1) . "\n";
167             }
168             }
169              
170             =head2 $clone = $oc->clone()
171              
172             Copies the output column, by deeply cloning the expression, and then directly copying the alias, is_primary_key flag, and output_value, if any.
173              
174             =cut
175              
176             sub clone {
177 0     0 1   my $self = shift;
178 0           my $class = ref $self;
179 0           my $other = $class->new(
180             alias => $self->alias,
181             expression => $self->expression->clone(),
182             is_primary_key => $self->is_primary_key(),
183             );
184 0           $other->output_value($self->output_value());
185 0           return $other;
186              
187             }
188              
189              
190              
191             =head1 AUTHOR
192              
193             Clinton Wolfe
194              
195             =cut
196              
197             1;