DS/Importer/ParaQuery.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 60 CC 9 SCAR 42.2
CC
9
Cov
0%
query
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 68.0
1 #!perl
2
3 # ########################################################################## #
4 # Title: Parameterized query builder
5 # Creation date: 2007-03-05
6 # Author: Michael Zedeler
7 # Description: Builds a query based on various parameters and a type
8 # specification.
9 # File: $Source: /data/cvs/lib/DSlib/lib/DS/Importer/ParaQuery.pm,v $
10 # Repository: kronhjorten
11 # State: $State: Exp $
12 # Documentation: inline
13 # Recepient: -
14 # #TODO make this class an importer that produces a DS
15 # #TODO fix direct reference to $typespec->{name} and various obsolete attributes
16 # #TODO Inheritance seems to be broken, since DS::Producer is obsolete
17 # #TODO Probably defunct since 2.0
18 # ########################################################################## #
19
20 package DS::Importer::ParaQuery;
21
22 use base qw { DS::Importer };
23
24 use strict;
25 use Carp::Assert;
26
27 our ($VERSION) = $DS::VERSION;
28 our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/;
29
30
31 sub query {
32 my($self, $filter, $sortby) = @_;
33
34 my $select = "SELECT * FROM " . $self->{typespec}->{name};
35 if(defined($filter)) {
36 assert(ref($filter) eq 'HASH');
37
38 my @ands = ();
39 foreach my $field (keys %$filter) {
40 if(exists($$filter{$field})) {
41 if(defined($$filter{$field})) {
42 push @ands, "$field = ?";
43 } else {
44 push @ands, "$field IS NULL";
45 }
46 }
47 }
48
49 if( $#ands > -1 ) {
50 $select .= " WHERE " . join(" AND ", @ands);
51 }
52 }
53
54 if(defined($sortby)) {
55 assert(ref($sortby) eq 'HASH');
56
57 my @sortkeys = ();
58 foreach my $sortkey (keys %$sortby) {
59 push @sortkeys, "$sortkey " . ($$sortby{$sortkey} < 0 ? 'DESC':'ASC');
60 }
61
62 if( $#sortkeys > -1 ) {
63 $select .= ' ORDER BY ' . join(', ', @sortkeys);
64 }
65 }
66
67 my $sth = $self->{dbh}->prepare_cached( $select, undef, 3 );
68 assert(defined($sth));
69
70 my $p_num = 1;
71 foreach my $field (keys %$filter) {
72 if(defined($$filter{$field})) {
73 $sth->bind_param( $p_num++, $$filter{$field});
74 }
75 }
76
77 #TODO This stuff is probably obsolete
78 $self->bind($sth);
79
80 $sth->execute();
81
82 return $sth;
83 }
84
85 1;