| 1 | package DBomb::Query::Delete; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | DBomb::Query::Delete - An SQL DELETE wrapper | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | our $VERSION = '$Revision: 1.5 $'; | ||
| 14 | |||
| 15 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use DBomb; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use DBomb::Conf; | ||
| 18 | use Carp::Assert; | ||
| 19 | use Class::MethodMaker | ||
| 20 | 'new_with_init' => 'new', | ||
| 21 | 'get_set' => [ qw(dbh), | ||
| 22 | qw(sth), | ||
| 23 | qw(table_name), | ||
| 24 | qw(where_obj), # expr | ||
| 25 | ], | ||
| 26 | ; | ||
| 27 | |||
| 28 | ## init() | ||
| 29 | ## init($dbh) | ||
| 30 | sub init | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | $self->where_obj(new DBomb::Query::Expr()); | ||
| 34 | |||
| 35 | ## First argument might be a dbh or peer object | ||
| 36 | for(@_){ | ||
| 37 | if (UNIVERSAL::isa($_,'DBI::db')){ | ||
| 38 | $self->dbh($_); | ||
| 39 | } | ||
| 40 | else{ | ||
| 41 | croak "unrecognized argument to DBomb::Query::Delete->new"; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | ## from($tables) | ||
| 47 | sub from | ||
| 48 | { | ||
| 49 | my $self = shift; | ||
| 50 | my $table_name = shift; | ||
| 51 | assert(@_ == 0, 'parameter count to from()'); | ||
| 52 | assert(defined($table_name), 'table name must be defined'); | ||
| 53 | |||
| 54 | $self->table_name(new DBomb::Query::Text( $table_name )); | ||
| 55 | return $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | ## Same as prepare->execute | ||
| 59 | ## delete() | ||
| 60 | ## delete(@bind_values) | ||
| 61 | ## delete($dbh,@bind_values) | ||
| 62 | sub delete | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | my @bv; | ||
| 66 | |||
| 67 | for (@_){ | ||
| 68 | if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } | ||
| 69 | else { push @bv, $_ } | ||
| 70 | } | ||
| 71 | assert(defined($self->dbh), 'delete requires a dbh'); | ||
| 72 | assert(defined($self->table_name), 'delete requires a table'); | ||
| 73 | |||
| 74 | if (not $self->sth){ $self->prepare } | ||
| 75 | return $self->execute(@bv); | ||
| 76 | } | ||
| 77 | |||
| 78 | ## execute() | ||
| 79 | ## execute(@bind_values) | ||
| 80 | ## execute($dbh,@bind_values) | ||
| 81 | sub execute | ||
| 82 | { | ||
| 83 | my $self = shift; | ||
| 84 | my @bv; | ||
| 85 | |||
| 86 | for (@_){ | ||
| 87 | if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } | ||
| 88 | else { push @bv, $_ } | ||
| 89 | } | ||
| 90 | assert(defined($self->dbh), 'delete requires a dbh'); | ||
| 91 | |||
| 92 | if (not $self->sth){ $self->prepare } | ||
| 93 | return $self->sth->execute((@{$self->bind_values},@bv)); | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | ## prepare() | ||
| 98 | ## prepare($dbh) | ||
| 99 | sub prepare | ||
| 100 | { | ||
| 101 | my $self = shift; | ||
| 102 | |||
| 103 | for (@_){ | ||
| 104 | $self->dbh($_) if UNIVERSAL::isa($_, 'DBI::db'); | ||
| 105 | } | ||
| 106 | assert(defined($self->dbh), 'prepare requires a dbh'); | ||
| 107 | |||
| 108 | if ($DBomb::Conf::prepare_cached){ | ||
| 109 | $self->sth($self->dbh->prepare_cached(scalar $self->sql)); | ||
| 110 | }else{ | ||
| 111 | $self->sth($self->dbh->prepare(scalar $self->sql)); | ||
| 112 | } | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | ## where(EXPR, @bind_values) | ||
| 117 | sub where | ||
| 118 | { | ||
| 119 | my $self = shift; | ||
| 120 | $self->where_obj->append( @_ ); | ||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | ## and (EXPR, @bind_values) | ||
| 125 | sub and | ||
| 126 | { | ||
| 127 | my $self = shift; | ||
| 128 | $self->where_obj->and(@_); | ||
| 129 | $self | ||
| 130 | } | ||
| 131 | |||
| 132 | ## or (EXPR, @bind_values) | ||
| 133 | sub or | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | $self->where_obj->or(@_); | ||
| 137 | $self | ||
| 138 | } | ||
| 139 | |||
| 140 | sub sql | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | $self->dbh(shift) if @_; | ||
| 144 | |||
| 145 | assert($self->dbh, 'sql requires a dbh'); | ||
| 146 | |||
| 147 | my $sql = "DELETE"; | ||
| 148 | |||
| 149 | assert(defined($self->table_name), "DELETE without table name."); | ||
| 150 | $sql .= " FROM " . $self->table_name->sql; | ||
| 151 | |||
| 152 | if ( $self->where_obj->is_not_empty ){ | ||
| 153 | $sql .= " WHERE "; | ||
| 154 | $sql .= $self->where_obj->sql($self->dbh); | ||
| 155 | } | ||
| 156 | return $sql; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub bind_values | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | my $bv = []; | ||
| 163 | assert(@_==0, 'bind_values takes no arguments'); | ||
| 164 | |||
| 165 | push @$bv, @{$self->where_obj->bind_values}; | ||
| 166 | return $bv; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | 1; | ||
| 172 | __END__ | ||