Romani/Query/Select.pm untested

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