| 1 | package Fey::SQL::Update; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 26 | our $VERSION = '0.43'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use Fey::Exceptions qw( param_error ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 26 | ▶ | use Fey::Literal; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 26 | use Fey::Types qw( ArrayRef CanQuote ColumnWithTable NonNullableUpdateValue | |||||||||||||||||||||||||||||||||||||||||
| 12 | 26 | ▶ | NullableUpdateValue Table ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 26 | ▶ | use overload (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 26 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 26 | ▶ | use Moose 2.1200; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 26 | ▶ | use MooseX::Params::Validate 0.21 qw( pos_validated_list ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 26 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 26 | ▶ | use MooseX::StrictConstructor 0.13; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 26 | with 'Fey::Role::SQL::HasOrderByClause', 'Fey::Role::SQL::HasLimitClause'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 26 | with 'Fey::Role::SQL::HasWhereClause' => { | |||||||||||||||||||||||||||||||||||||||||
| 24 | -excludes => 'bind_params', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | -alias => { bind_params => '_where_clause_bind_params' }, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 26 | with 'Fey::Role::SQL::HasBindParams' => { | |||||||||||||||||||||||||||||||||||||||||
| 29 | -excludes => 'bind_params', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | -alias => { bind_params => '_update_bind_params' }, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | has '_update' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 26 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 26 | init_arg => undef, | |||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | has '_set_pairs' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 41 | traits => ['Array'], | ||||||||||||||||||||||||||||||||||||||||||
| 42 | is => 'bare', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | isa => ArrayRef [ArrayRef], | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 26 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 26 | handles => { | |||||||||||||||||||||||||||||||||||||||||
| 46 | _add_set_pair => 'push', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | _set_pairs => 'elements', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 26 | with 'Fey::Role::SQL::Cloneable'; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub update { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 26 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 26 | ▶ | my $count = @_ ? @_ : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 26 | my (@tables) = pos_validated_list( | |||||||||||||||||||||||||||||||||||||||||
| 59 | \@_, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ( ( { isa => Table } ) x $count ), | ||||||||||||||||||||||||||||||||||||||||||
| 61 | MX_PARAMS_VALIDATE_NO_CACHE => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 25 | $self->_set_update( \@tables ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 25 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | sub set { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 23 | ▶ | if ( !@_ || @_ % 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @_ or @_ % 2
MC/DC100%2/2
not @_ or @_ % 2
not @_
@_ % 2
Truth table100%3/3
not(A) or B
A= not @_B= @_ % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my $count = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | param_error | |||||||||||||||||||||||||||||||||||||||||
| 75 | "The set method expects a list of paired column objects and values but you passed $count parameters"; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 21 | my @spec; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 21 | ▶ | for ( my $x = 0; $x < @_; $x += 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 25 | push @spec, { isa => ColumnWithTable }; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 25 | ▶ | push @spec, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_[$x]) and $_[$x]->is_nullable
MC/DC50%1/2
builtin::blessed($_[$x]) and $_[$x]->is_nullable
builtin::blessed($_[$x])
$_[$x]->is_nullable
Truth table66%2/3
A and B
A= builtin::blessed($_[$x])B= $_[$x]->is_nullable
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | blessed $_[$x] && $_[$x]->is_nullable() | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ? { isa => NullableUpdateValue } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | : { isa => NonNullableUpdateValue }; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | my @set | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 21 | = pos_validated_list( \@_, @spec, MX_PARAMS_VALIDATE_NO_CACHE => 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | ▶ | for ( my $x = 0; $x < @_; $x += 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 25 | my $val = $_[ $x + 1 ]; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 25 | ▶ | $val .= '' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($val) and overload::Overloaded($val)
MC/DC100%2/2
builtin::blessed($val) and overload::Overloaded($val)
builtin::blessed($val)
overload::Overloaded($val)
Truth table100%3/3
A and B
A= builtin::blessed($val)B= overload::Overloaded($val)
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | if blessed $val && overload::Overloaded($val); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 25 | ▶ | if ( !blessed $val ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 16 | ▶ | if ( defined $val && $self->auto_placeholders() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $val and $self->auto_placeholders
MC/DC100%2/2
defined $val and $self->auto_placeholders
defined $val
$self->auto_placeholders
Truth table100%3/3
A and B
A= defined $valB= $self->auto_placeholders
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | $self->_add_bind_param($val); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 8 | $val = Fey::Placeholder->new(); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | $val = Fey::Literal->new_from_scalar($val); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 25 | $self->_add_set_pair( [ $_[$x], $val ] ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 21 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | sub sql { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | my ($dbh) = pos_validated_list( \@_, { isa => CanQuote } ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | join ' ', | |||||||||||||||||||||||||||||||||||||||||
| 119 | $self->update_clause($dbh), | ||||||||||||||||||||||||||||||||||||||||||
| 120 | $self->set_clause($dbh), | ||||||||||||||||||||||||||||||||||||||||||
| 121 | $self->where_clause($dbh), | ||||||||||||||||||||||||||||||||||||||||||
| 122 | $self->order_by_clause($dbh), | ||||||||||||||||||||||||||||||||||||||||||
| 123 | $self->limit_clause($dbh), | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | sub update_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 6 | ▶ | return 'UPDATE ' . $_[0]->_tables_subclause( $_[1] ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_clause: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | sub _tables_subclause { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 133 | join ', ', | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 6 | ▶ | map { $_[1]->quote_identifier( $_->name() ) } @{ $_[0]->_update() } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tables_subclause: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub set_clause { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 21 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_clause: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 21 | my $dbh = shift; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | # SQLite objects when the table name is provided ("User"."email") | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # on the LHS of the set. I'm hoping that a DBMS which allows a | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # multi-table update also allows the table name in the LHS. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 21 | ▶ | my $col_quote = @{ $self->_update() } > 1 ? '_name_and_table' : '_name'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 'SET ' . ( | ||||||||||||||||||||||||||||||||||||||||||
| 149 | join ', ', | ||||||||||||||||||||||||||||||||||||||||||
| 150 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 21 | my $val = $_->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 24 | my $val_sql = $val->sql($dbh); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 24 | ▶ | $val_sql = "($val_sql)" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
builtin::blessed($val) and $val->can("does")
MC/DC33%1/3
builtin::blessed($val) and $val->can("does") and $val->does("Fey::Role::SQL::ReturnsData")
builtin::blessed($val)
$val->can("does")
$val->does("Fey::Role::SQL::ReturnsData")
Truth table50%2/4
A and B and C
A= builtin::blessed($val)B= $val->can("does")C= $val->does("Fey::Role::SQL::ReturnsData")
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | if blessed $val | ||||||||||||||||||||||||||||||||||||||||||
| 155 | && $val->can('does') | ||||||||||||||||||||||||||||||||||||||||||
| 156 | && $val->does('Fey::Role::SQL::ReturnsData'); | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 24 | $self->$col_quote( $_->[0], $dbh ) . ' = ' . $val_sql; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } $self->_set_pairs() | ||||||||||||||||||||||||||||||||||||||||||
| 159 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | sub _name_and_table { | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | return $_[1]->sql( $_[2] ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_name_and_table: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | sub _name { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 23 | ▶ | return $_[2]->quote_identifier( $_[1]->name() ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | sub bind_params { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bind_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 5 | $self->_update_bind_params(), | |||||||||||||||||||||||||||||||||||||||||
| 176 | $self->_where_clause_bind_params(), | ||||||||||||||||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 26 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 26 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | # ABSTRACT: Represents a UPDATE query | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Fey::SQL::Update - Represents a UPDATE query | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | version 0.43 | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | my $sql = Fey::SQL->new_update(); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | # UPDATE Part | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # SET quantity = 10 | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # WHERE part_id IN (1, 5) | ||||||||||||||||||||||||||||||||||||||||||
| 205 | $sql->update($Part); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | $sql->set( $quantity, 10 ); | ||||||||||||||||||||||||||||||||||||||||||
| 207 | $sql->where( $part_id, 'IN', 1, 5 ); | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | print $sql->sql($dbh); | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | This class represents a C<UPDATE> query. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | This class provides the following methods: | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =head2 Constructor | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | To construct an object of this class, call C<< $query->update() >> on | ||||||||||||||||||||||||||||||||||||||||||
| 222 | a C<Fey::SQL> object. | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | =head2 $update->update() | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | This method specifies the C<UPDATE> clause of the query. It expects | ||||||||||||||||||||||||||||||||||||||||||
| 227 | one or more L<Fey::Table> objects (not aliases). Most RDBMS | ||||||||||||||||||||||||||||||||||||||||||
| 228 | implementations only allow for a single table here, but some (like | ||||||||||||||||||||||||||||||||||||||||||
| 229 | MySQL) do allow for multi-table updates. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =head2 $update->set(...) | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | This method takes a list of key/value pairs. The keys should be column | ||||||||||||||||||||||||||||||||||||||||||
| 234 | objects, and the value can be one of the following: | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | =item * a plain scalar, including undef | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | This will be passed to C<< Fey::Literal->new_from_scalar() >>. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =item * C<Fey::Literal> object | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | =item * C<Fey::Column> object | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | A column alias cannot be used. | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | =item * C<Fey::Placeholder> object | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | =head2 $update->where(...) | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | See the L<Fey::SQL section on WHERE Clauses|Fey::SQL/WHERE Clauses> | ||||||||||||||||||||||||||||||||||||||||||
| 255 | for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =head2 $update->order_by(...) | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | See the L<Fey::SQL section on ORDER BY Clauses|Fey::SQL/ORDER BY | ||||||||||||||||||||||||||||||||||||||||||
| 260 | Clauses> for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | =head2 $update->limit(...) | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | See the L<Fey::SQL section on LIMIT Clauses|Fey::SQL/LIMIT Clauses> | ||||||||||||||||||||||||||||||||||||||||||
| 265 | for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =head2 $update->sql($dbh) | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | Returns the full SQL statement which this object represents. A DBI | ||||||||||||||||||||||||||||||||||||||||||
| 270 | handle must be passed so that identifiers can be properly quoted. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 $update->bind_params() | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | See the L<Fey::SQL section on Bind Parameters|Fey::SQL/Bind | ||||||||||||||||||||||||||||||||||||||||||
| 275 | Parameters> for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | =head2 $update->update_clause() | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | Returns the C<UPDATE> clause portion of the SQL statement as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =head2 $update->set_clause() | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | Returns the C<SET> clause portion of the SQL statement as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =head2 $update->where_clause() | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | Returns the C<WHERE> clause portion of the SQL statement as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | =head2 $update->order_by_clause() | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | Returns the C<ORDER BY> clause portion of the SQL statement as a | ||||||||||||||||||||||||||||||||||||||||||
| 292 | string. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | =head2 $update->limit_clause() | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | Returns the C<LIMIT> clause portion of the SQL statement as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | =head1 ROLES | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =item * L<Fey::Role::SQL::HasBindParams> | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =item * L<Fey::Role::SQL::HasWhereClause> | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =item * L<Fey::Role::SQL::HasOrderByClause> | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =item * L<Fey::Role::SQL::HasLimitClause> | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =item * L<Fey::Role::SQL::Cloneable> | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | See L<Fey> for details on how to report bugs. | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||||||||||||||||