File Coverage

blib/lib/DBIx/Romani/Query/Select.pm
Criterion Covered Total %
statement 12 91 13.1
branch 0 10 0.0
condition n/a
subroutine 4 30 13.3
pod 0 26 0.0
total 16 157 10.1


line stmt bran cond sub pod time code
1              
2             package DBIx::Romani::Query::Select;
3              
4 1     1   593 use DBIx::Romani::Query::Select::Result;
  1         3  
  1         25  
5 1     1   541 use DBIx::Romani::Query::Select::Join;
  1         4  
  1         26  
6 1     1   534 use DBIx::Romani::Query::Select::OrderBy;
  1         2  
  1         23  
7 1     1   5 use strict;
  1         1  
  1         872  
8              
9             sub new
10             {
11 0     0 0   my $class = shift;
12 0           my $args = shift;
13            
14 0           my $self = {
15             from => [],
16             result => [],
17             where => undef,
18             join => undef,
19             group_by => [],
20             order_by => [],
21             limit => undef,
22             offset => undef,
23             distinct => 0,
24             };
25              
26 0           bless $self, $class;
27 0           return $self;
28             }
29              
30 0     0 0   sub get_from { return shift->{from}; }
31 0     0 0   sub get_result { return shift->{result}; }
32 0     0 0   sub get_where { return shift->{where}; }
33 0     0 0   sub get_group_by { return shift->{group_by}; }
34 0     0 0   sub get_order_by { return shift->{order_by}; }
35 0     0 0   sub get_join { return shift->{join}; }
36 0     0 0   sub get_limit { return shift->{limit}; }
37 0     0 0   sub get_offset { return shift->{offset}; }
38 0     0 0   sub get_distinct { return shift->{distinct}; }
39              
40 0     0 0   sub clear_from { shift->{from} = [ ]; }
41 0     0 0   sub clear_result { shift->{result} = [ ]; }
42 0     0 0   sub clear_where { shift->{where} = undef; }
43 0     0 0   sub clear_group_by { shift->{group_by} = [ ]; }
44 0     0 0   sub clear_order_by { shift->{order_by} = [ ]; }
45              
46             sub clear_limit
47             {
48 0     0 0   my $self = shift;
49              
50             # must clear both for sanity
51 0           $self->{limit} = undef;
52 0           $self->{offset} = undef;
53             }
54              
55             sub add_from
56             {
57 0     0 0   my ($self, $table_name) = @_;
58            
59 0           foreach my $other ( @{$self->get_from()} )
  0            
60             {
61 0 0         if ( $table_name eq $other )
62             {
63             # don't add it twice!!
64 0           return;
65             }
66             }
67              
68 0           push @{$self->{from}}, $table_name;
  0            
69             }
70              
71             sub add_result
72             {
73 0     0 0   my $self = shift;
74              
75 0           my $result = DBIx::Romani::Query::Select::Result->new( @_ );
76            
77 0           my $name = $result->get_name();
78 0 0         if ( defined $name )
79             {
80 0           my @temp = grep { $_->get_name() eq $name } @{$self->{result}};
  0            
  0            
81 0 0         if ( scalar @temp > 0 )
82             {
83 0           die "Cannot add two results with the same name";
84             }
85             }
86              
87 0           push @{$self->{result}}, $result;
  0            
88             }
89              
90             sub add_group_by
91             {
92 0     0 0   my ($self, $result) = @_;
93 0           push @{$self->{group_by}}, $result;
  0            
94             }
95              
96             sub add_order_by
97             {
98 0     0 0   my $self = shift;
99 0           my $order_by = DBIx::Romani::Query::Select::OrderBy->new( @_ );
100 0           push @{$self->{order_by}}, $order_by;
  0            
101             }
102              
103             sub set_where
104             {
105 0     0 0   my ($self, $where) = @_;
106 0           $self->{where} = $where;
107             }
108              
109             sub set_join
110             {
111 0     0 0   my $self = shift;
112 0           my $join = DBIx::Romani::Query::Select::Join->new( @_ );
113 0           $self->{join} = $join;
114             }
115              
116             sub set_limit
117             {
118 0     0 0   my ($self, $limit, $offset) = @_;
119 0           $self->{limit} = $limit;
120 0           $self->{offset} = $offset;
121             }
122              
123             sub set_distinct
124             {
125 0     0 0   my ($self, $distinct) = @_;
126 0           $self->{distinct} = $distinct;
127             }
128              
129             sub visit
130             {
131 0     0 0   my ($self, $visitor) = @_;
132 0           return $visitor->visit_select( $self );
133             }
134              
135             sub clone
136             {
137 0     0 0   my $self = shift;
138              
139 0           my $query = DBIx::Romani::Query::Select->new();
140              
141             # from
142 0           foreach my $from ( @{$self->get_from()} )
  0            
143             {
144 0           $query->add_from( $from );
145             }
146              
147             # result
148 0           foreach my $result ( @{$self->get_result()} )
  0            
149             {
150             # A little non-standard
151 0           push @{$query->{result}}, $result->clone();
  0            
152             }
153              
154             # where
155 0 0         if ( defined $query->get_where() )
156             {
157 0           $query->set_where( $query->get_where()->clone() );
158             }
159              
160             # join
161 0 0         if ( defined $query->get_join() )
162             {
163 0           $query->set_join( $query->get_join()->clone() );
164             }
165              
166             # group by
167 0           foreach my $group_by ( @{$self->get_group_by()} )
  0            
168             {
169 0           $query->add_group_by( $group_by );
170             }
171              
172             # order by
173 0           foreach my $order_by ( @{$self->get_order_by()} )
  0            
174             {
175 0           $query->add_order_by( $order_by );
176             }
177              
178 0           return $query;
179             }
180              
181             1;
182