| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package DBomb::GluedQuery; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | DBomb::GluedQuery - A query that is glued to a DBomb meta object by primary key. | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =cut | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 8 |  |  | 8 |  | 47 | use strict; | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 295 |  | 
| 10 | 8 |  |  | 8 |  | 41 | use warnings; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 440 |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '$Revision: 1.13 $'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 8 |  |  | 8 |  | 62 | use Carp::Assert; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 73 |  | 
| 14 | 8 |  |  | 8 |  | 1306 | use Carp qw(carp croak); | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 523 |  | 
| 15 | 8 |  |  | 8 |  | 12690 | use DBomb; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | use DBomb::Value::Key; | 
| 17 |  |  |  |  |  |  | use base qw(DBomb::Query); | 
| 18 |  |  |  |  |  |  | use Class::MethodMaker | 
| 19 |  |  |  |  |  |  | 'get_set' => [ qw(columns_list),     # [ column_info, ... ] | 
| 20 |  |  |  |  |  |  | qw(peer),        # a DBomb::Base object | 
| 21 |  |  |  |  |  |  | qw(primary_key_idx), # [ index, index ] | 
| 22 |  |  |  |  |  |  | ], | 
| 23 |  |  |  |  |  |  | ; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | ## new DBomb::GluedQuery($dbh,$columns_list) | 
| 26 |  |  |  |  |  |  | ## new DBomb::GluedQuery($dbh,$peer,$columns_list) | 
| 27 |  |  |  |  |  |  | ## new DBomb::GluedQuery($peer,$columns_list) | 
| 28 |  |  |  |  |  |  | ## new DBomb::GluedQuery($columns_list) | 
| 29 |  |  |  |  |  |  | sub init | 
| 30 |  |  |  |  |  |  | { | 
| 31 |  |  |  |  |  |  | my $self = shift; | 
| 32 |  |  |  |  |  |  | my $columns; | 
| 33 |  |  |  |  |  |  | $self->columns_list([]); | 
| 34 |  |  |  |  |  |  | $self->primary_key_idx([]); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | ## First argument might be a dbh or peer object | 
| 37 |  |  |  |  |  |  | for(@_){ | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | if (UNIVERSAL::isa($_,'DBI::db')){ | 
| 40 |  |  |  |  |  |  | $self->dbh($_); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | elsif (UNIVERSAL::isa($_,'DBomb::Base')){ | 
| 43 |  |  |  |  |  |  | $self->peer($_); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | elsif (UNIVERSAL::isa($_, 'ARRAY')){ | 
| 46 |  |  |  |  |  |  | $columns = $_; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | else{ | 
| 49 |  |  |  |  |  |  | croak "invalid paramter to GluedQuery->new()"; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | ## verify args | 
| 54 |  |  |  |  |  |  | assert(defined($columns),        'GluedQuery requires a [columns_list]'); | 
| 55 |  |  |  |  |  |  | assert(UNIVERSAL::isa($columns,'ARRAY'), 'GluedQuery requires a [columns_list]'); | 
| 56 |  |  |  |  |  |  | for(@$columns){ | 
| 57 |  |  |  |  |  |  | assert(UNIVERSAL::isa($_,'DBomb::Meta::ColumnInfo'), 'GluedQuery requires columninfo objs') | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | ## Save the indexes of the primary key columns. | 
| 61 |  |  |  |  |  |  | my %unseen_pk_columns = map { $_->name => $_ } @{$columns->[0]->table_info->primary_key->columns_list}; | 
| 62 |  |  |  |  |  |  | for my $i (0..$#$columns) { | 
| 63 |  |  |  |  |  |  | if ( $columns->[$i]->is_in_primary_key ){ | 
| 64 |  |  |  |  |  |  | push @{$self->primary_key_idx},  $i; | 
| 65 |  |  |  |  |  |  | delete $unseen_pk_columns{$columns->[$i]->name}; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | ## Add the primary key columns that were not in the column list. | 
| 70 |  |  |  |  |  |  | ## Only do this for peerless queries | 
| 71 |  |  |  |  |  |  | if (not defined $self->peer){ | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | for (values %unseen_pk_columns){ | 
| 74 |  |  |  |  |  |  | push @$columns, $_; | 
| 75 |  |  |  |  |  |  | push @{$self->primary_key_idx}, $#$columns; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | for (@$columns){ | 
| 81 |  |  |  |  |  |  | push @{$self->columns_list}, $_; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | $self->SUPER::init(map {$_->fq_name} @{$self->columns_list}); | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub from | 
| 89 |  |  |  |  |  |  | { | 
| 90 |  |  |  |  |  |  | my ($self, $table_info) = @_; | 
| 91 |  |  |  |  |  |  | assert(UNIVERSAL::isa($table_info,'DBomb::Meta::TableInfo'), 'valid parameters'); | 
| 92 |  |  |  |  |  |  | return $self->SUPER::from($table_info->name); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub join       { die "not implemented" } | 
| 96 |  |  |  |  |  |  | sub on         { die "not implemented" } | 
| 97 |  |  |  |  |  |  | sub right_join { die "not implemented" } | 
| 98 |  |  |  |  |  |  | sub left_join  { die "not implemented" } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub fetchrow_arrayref | 
| 102 |  |  |  |  |  |  | { | 
| 103 |  |  |  |  |  |  | my $self = shift; | 
| 104 |  |  |  |  |  |  | return $self->SUPER::fetchrow_arrayref(@_); | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub fetchall_arrayref | 
| 108 |  |  |  |  |  |  | { | 
| 109 |  |  |  |  |  |  | my $self = shift; | 
| 110 |  |  |  |  |  |  | my $a = []; | 
| 111 |  |  |  |  |  |  | while (my $obj = $self->fetchrow_objectref){ | 
| 112 |  |  |  |  |  |  | push @$a, $obj; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | return $a; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub fetch | 
| 118 |  |  |  |  |  |  | { | 
| 119 |  |  |  |  |  |  | fetchrow_objectref(@_); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | sub fetchrow_objectref | 
| 123 |  |  |  |  |  |  | { | 
| 124 |  |  |  |  |  |  | my $self = shift; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | my $row = $self->SUPER::fetchrow_arrayref; | 
| 127 |  |  |  |  |  |  | return undef unless $row && @$row; | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | my $columns = $self->columns_list; | 
| 130 |  |  |  |  |  |  | my $tinfo = $columns->[0]->table_info; | 
| 131 |  |  |  |  |  |  | my $class = $tinfo->class; | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | my $obj = $self->peer; | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | if (not $obj) { | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # pluck the primary keys first. | 
| 138 |  |  |  |  |  |  | my $pkv = new DBomb::Value::Key($tinfo->primary_key, | 
| 139 |  |  |  |  |  |  | [ map { $row->[$_] }  @{$self->primary_key_idx} ]); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | $obj = $class->new($pkv); | 
| 142 |  |  |  |  |  |  | $obj->dbh($self->dbh); | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | assert($obj, 'has a peer, or, PK is in query'); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | my $values = $obj->_dbo_values; | 
| 148 |  |  |  |  |  |  | for my $i (0..$#$row){ | 
| 149 |  |  |  |  |  |  | next if $columns->[$i]->is_in_primary_key; # skip pks. | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | #my $accessor = $columns->[$i]->accessor; | 
| 152 |  |  |  |  |  |  | #$obj->$accessor($row->[$i]); | 
| 153 |  |  |  |  |  |  | $values->{$columns->[$i]->name}->set_value_from_select($row->[$i]); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | return $obj; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | ## selectall_arrayref() | 
| 160 |  |  |  |  |  |  | ## selectall_arrayref($dbh,@bind_values) | 
| 161 |  |  |  |  |  |  | ## selectall_arrayref(@bind_values) | 
| 162 |  |  |  |  |  |  | sub selectall_arrayref | 
| 163 |  |  |  |  |  |  | { | 
| 164 |  |  |  |  |  |  | my $self = shift; | 
| 165 |  |  |  |  |  |  | my (@bind_values) = @_; | 
| 166 |  |  |  |  |  |  | my $dbh = $self->dbh; | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | if (UNIVERSAL::isa($bind_values[0],'DBI::db')){ | 
| 169 |  |  |  |  |  |  | $dbh = $self->dbh(shift @bind_values); | 
| 170 |  |  |  |  |  |  | } | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | assert(defined($dbh), 'selectall_arrayref requires a $dbh'); | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | $self->prepare($dbh); | 
| 175 |  |  |  |  |  |  | $self->execute(@bind_values); | 
| 176 |  |  |  |  |  |  | return $self->fetchall_arrayref; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | 1; | 
| 181 |  |  |  |  |  |  | __END__ |