| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # DBO::Visitor::Select | ||
| 3 | # | ||
| 4 | # DESCRIPTION | ||
| 5 | # | ||
| 6 | # AUTHOR | ||
| 7 | # Gareth Rees | ||
| 8 | # | ||
| 9 | # COPYRIGHT | ||
| 10 | # Copyright (c) 1999 Canon Research Centre Europe Ltd. | ||
| 11 | # | ||
| 12 | # $Id: Select.pm,v 1.3 1999/06/29 17:09:31 garethr Exp $ | ||
| 13 | #------------------------------------------------------------------------------ | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | |||
| 17 | package DBO::Visitor::Select; | ||
| 18 | use base qw(DBO::Visitor); | ||
| 19 | use Class::Multimethods; | ||
| 20 | |||
| 21 | multimethod visit_table => | ||
| 22 | qw(DBO::Visitor::Select DBO::Table DBO::Handle::DBI) => | ||
| 23 | sub { | ||
| 24 | my ($vis, $table, $dbh) = @_; | ||
| 25 | |||
| 26 | my @sql = ("SELECT * FROM", $table->{name}, "WHERE"); | ||
| 27 | foreach my $col (@{$table->{columns}}) { | ||
| 28 | $vis->{sql} = []; | ||
| 29 | visit_column($vis, $col, $dbh); | ||
| 30 | push @sql, @{$vis->{sql}}, "AND" if @{$vis->{sql}}; | ||
| 31 | } | ||
| 32 | pop @sql; | ||
| 33 | my $sql = join ' ', @sql; | ||
| 34 | my $sth = $dbh->prepare($sql) or die DBO::Exception | ||
| 35 | (PREPARE => "Failed to prepare SQL statement %s: %s.", $sql, $dbh->errstr); | ||
| 36 | $sth->execute or die DBO::Exception | ||
| 37 | (EXECUTE => "Failed to execute insert: %s.", $dbh->errstr); | ||
| 38 | |||
| 39 | # Fetch all records as hash references | ||
| 40 | my @records; | ||
| 41 | while (my $record = $sth->fetchrow_hashref) { | ||
| 42 | # DBI doesn't promise not to reuse the hash, so take a copy. | ||
| 43 | push @records, { %$record }; | ||
| 44 | } | ||
| 45 | \@records; | ||
| 46 | }; | ||
| 47 | |||
| 48 | multimethod visit_column => | ||
| 49 | qw(DBO::Visitor::Select DBO::Column::String DBO::Handle::DBI) => | ||
| 50 | sub { | ||
| 51 | my ($vis, $col, $dbh) = @_; | ||
| 52 | my $value = $vis->{record}{$col->{name}}; | ||
| 53 | push @{$vis->{sql}}, $col->{name},'=',$dbh->quote($value) if defined $value; | ||
| 54 | }; | ||
| 55 | |||
| 56 | multimethod visit_column => | ||
| 57 | qw(DBO::Visitor::Select DBO::Column::Number DBO::Handle::DBI) => | ||
| 58 | sub { | ||
| 59 | my ($vis, $col, $dbh) = @_; | ||
| 60 | my $value = $vis->{record}{$col->{name}}; | ||
| 61 | push @{$vis->{sql}}, $col->{name}, '=', $value if defined $value; | ||
| 62 | }; | ||
| 63 | |||
| 64 | 1; |