| 1 | package DBIx::ORM::Declarative::Join; | ||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use vars qw(@ISA); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | @ISA = qw(DBIx::ORM::Declarative::Table); | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # For compatibility with old-style join syntax | ||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub _join_clause { ''; } | ||||||||||||||||||||||||||||||
|
Subroutine
_join_clause: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # this is a join | ||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub _isjoin { 1; } | ||||||||||||||||||||||||||||||
|
Subroutine
_isjoin: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | # Create a row in a multi-table join, observing column constraints | ||||||||||||||||||||||||||||||||
| 15 | sub create | ||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
create: not called CC=58 SCAR=81.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | 0 | my ($self, %params) = @_; | |||||||||||||||||||||||||||||||
| 18 | 0 | my $handle = $self->handle; | |||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | carp "can't create without a database handle" and return unless $handle; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$handle || carp(q[can't create without a database handle]) && (return)
carp("can't create without a database handle") and return
MC/DC0%0/2
$handle || carp(q[can't create without a database handle]) && (return)
$handle
carp("can't create without a database handle")
Truth table0%0/3
A || B
A= $handleB= carp("can't create without a database handle")
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # Get table information | ||||||||||||||||||||||||||||||||
| 22 | 0 | my $primary = $self->_primary; | |||||||||||||||||||||||||||||||
| 23 | 0 | my @table_info = $self->_join_info; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # Check the primary table first | ||||||||||||||||||||||||||||||||
| 26 | 0 | my $tab_obj = $self->table($primary); | |||||||||||||||||||||||||||||||
| 27 | 0 | my ($flag) = $self->__check_constraints($tab_obj, %params); | |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | return unless $flag; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 0 | my %primary_map = $tab_obj->_column_map; | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # Assemble the data | ||||||||||||||||||||||||||||||||
| 32 | 0 | my @row_data; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # Do each other table in turn | ||||||||||||||||||||||||||||||||
| 35 | my @backout_cmds; | ||||||||||||||||||||||||||||||||
| 36 | 0 | for my $tab (@table_info) | |||||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||||||
| 38 | # Get a table object | ||||||||||||||||||||||||||||||||
| 39 | 0 | my $table = $tab->{table}; | |||||||||||||||||||||||||||||||
| 40 | 0 | $tab_obj = $self->table($table); | |||||||||||||||||||||||||||||||
| 41 | 0 | my $tab_name = $tab_obj->_sql_name; | |||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | # Copy the primary table parameters to where they map on the secondary | ||||||||||||||||||||||||||||||||
| 44 | 0 | my %p = %params; | |||||||||||||||||||||||||||||||
| 45 | $p{$tab->{columns}{$_}} = delete $p{$_} | ||||||||||||||||||||||||||||||||
| 46 | 0 | foreach grep { exists $p{$_} } keys %{$tab->{columns}}; | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | # Override with any long-named parameters | ||||||||||||||||||||||||||||||||
| 49 | 0 | for my $col (grep { $_->{table} eq $table } $self->_columns) | |||||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||||||
| 51 | # Construct the name, skip this entry if the name is already | ||||||||||||||||||||||||||||||||
| 52 | # constructed | ||||||||||||||||||||||||||||||||
| 53 | 0 | my $nm = $col->{name}; | |||||||||||||||||||||||||||||||
| 54 | 0 | my $tab_alias = $col->{table_alias}; | |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | next if length($nm)>length($tab_alias) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $nm > length $tab_alias and $tab_alias eq substr($nm, length $tab_alias)
MC/DC0%0/2
length $nm > length $tab_alias and $tab_alias eq substr($nm, length $tab_alias)
length $nm > length $tab_alias
$tab_alias eq substr($nm, length $tab_alias)
Truth table0%0/3
A and B
A= length $nm > length $tab_aliasB= $tab_alias eq substr($nm, length $tab_alias)
| |||||||||||||||||||||||||||||||||
| 56 | and $tab_alias eq substr($nm, length($tab_alias)); | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | my $augmented_name = $tab_alias . '_' . $nm; | |||||||||||||||||||||||||||||||
| 59 | $p{$nm} = $params{$augmented_name} | ||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if exists $params{$augmented_name}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 0 | my ($flag, $keys, $values, $npk, @binds) | |||||||||||||||||||||||||||||||
| 64 | = $self->__check_constraints($tab_obj, %p); | ||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if(not $flag) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||
| 67 | # We have a constraint violation | ||||||||||||||||||||||||||||||||
| 68 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 69 | 0 | return; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | # Might as well conditionally create the row | ||||||||||||||||||||||||||||||||
| 73 | 0 | my $sql = "INSERT INTO $tab_name ($keys) SELECT $values FROM DUAL"; | |||||||||||||||||||||||||||||||
| 74 | 0 | my %map = $tab_obj->_column_map; | |||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Check for a defined primary key | ||||||||||||||||||||||||||||||||
| 77 | 0 | my @pk = $tab_obj->_primary_key; | |||||||||||||||||||||||||||||||
| 78 | 0 | my @conditions; | |||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if(@pk and not $npk) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@pk and not $npk
MC/DC0%0/2
@pk and not $npk
@pk
not $npk
Truth table0%0/3
A and not(B)
A= @pkB= not $npk
| |||||||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||||||
| 81 | 0 | my @wk; | |||||||||||||||||||||||||||||||
| 82 | 0 | for my $k (@pk) | |||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if(exists $p{$k}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||
| 86 | 0 | push @wk, $map{$k} . '=?'; | |||||||||||||||||||||||||||||||
| 87 | 0 | push @binds, $p{$k}; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if(@wk) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||||||
| 92 | # We have part or all of the primary key | ||||||||||||||||||||||||||||||||
| 93 | 0 | push @conditions, join(' AND ', @wk); | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | # Check for other unique keys | ||||||||||||||||||||||||||||||||
| 98 | 0 | my @uniques = $tab_obj->_unique_keys; | |||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | shift @uniques if @pk; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 0 | for my $un (@uniques) | |||||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||||||
| 102 | 0 | my @wk; | |||||||||||||||||||||||||||||||
| 103 | 0 | for my $k (@$un) | |||||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if(exists $p{$k}) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||
| 107 | 0 | push @wk, $map{$k} . '=?'; | |||||||||||||||||||||||||||||||
| 108 | 0 | push @binds, $p{$k}; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | else | ||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||
| 112 | 0 | push @wk, $map{$k} . ' IS NULL'; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | # save it if we've got it | ||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | push @conditions, join(' AND ', @wk) if @wk; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | # Add the conditional part | ||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if(@conditions) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||||||
| 122 | $sql .= " WHERE NOT EXISTS (SELECT 1 FROM $tab_name WHERE " | ||||||||||||||||||||||||||||||||
| 123 | 0 | . join(' OR ', map { "($_)" } @conditions) | |||||||||||||||||||||||||||||||
| 124 | . ')'; | ||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | # We have the command - now create the row | ||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | unshift @binds, undef if @binds; # Deal with DBI bone-headedness | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 0 | my $dbres = $handle->do($sql, @binds); | |||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if(not $dbres) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||||||
| 132 | 0 | carp "Database error: " . $handle->errstr; | |||||||||||||||||||||||||||||||
| 133 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | # Get the primary key, if we have one | ||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if($npk) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||||||
| 140 | # Set the data return to a string so we know if we never tried | ||||||||||||||||||||||||||||||||
| 141 | # to get any data from the database. | ||||||||||||||||||||||||||||||||
| 142 | 0 | my $data = 'never called'; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | # See if we actually created a row | ||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if($dbres != 0) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||||||
| 147 | 0 | my $np = $tab_obj->_select_null_primary; | |||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if($np) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||||||
| 150 | 0 | $data = $handle->selectall_arrayref($np); | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | # See if we can find the conflicting row | ||||||||||||||||||||||||||||||||
| 154 | else | ||||||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||||||
| 156 | # Use the first non-primary unique key we have, or | ||||||||||||||||||||||||||||||||
| 157 | # everything if we don't have one. | ||||||||||||||||||||||||||||||||
| 158 | 0 | my ($ign, $un) = $tab_obj->_unique_keys; | |||||||||||||||||||||||||||||||
| 159 | 0 | my @cols; | |||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if($un) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||||||
| 162 | 0 | @cols = @$un; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | else | ||||||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||||||
| 166 | 0 | @cols = grep { exists $p{$_} } | |||||||||||||||||||||||||||||||
| 167 | 0 | map { $_->{name} } | |||||||||||||||||||||||||||||||
| 168 | $tab_obj->_columns; | ||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | 0 | @binds = (); | |||||||||||||||||||||||||||||||
| 172 | # Generate the SQL | ||||||||||||||||||||||||||||||||
| 173 | 0 | $sql = 'SELECT ' . join(',', map { $map{$_} } @pk) | |||||||||||||||||||||||||||||||
| 174 | . " FROM $tab_name WHERE "; | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 0 | my @wk; | |||||||||||||||||||||||||||||||
| 177 | push @wk, $map{$_} . (defined $p{$_}?'=?':' IS NULL') | ||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | foreach @cols; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 0 | push @binds, $p{$_} foreach grep { defined $p{$_} } @cols; | |||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 0 | $sql .= join(' AND ', @wk); | |||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | unshift @binds, undef if @binds; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 0 | $data = $handle->selectall_arrayref($sql, @binds); | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | # check for errors | ||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if(not $data) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 189 | { | ||||||||||||||||||||||||||||||||
| 190 | 0 | carp "Database error: " . $handle->errstr; | |||||||||||||||||||||||||||||||
| 191 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 192 | 0 | return; | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if(ref $data and not defined $data->[0][0]) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $data and not defined $data->[0][0]
MC/DC0%0/2
ref $data and not defined $data->[0][0]
ref $data
not defined $data->[0][0]
Truth table0%0/3
A and not(B)
A= ref $dataB= not defined $data->[0][0]
| |||||||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||||||
| 196 | 0 | carp "Database error: can't find primary key"; | |||||||||||||||||||||||||||||||
| 197 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 198 | 0 | return; | |||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | # Save the primary key data | ||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | @p{@pk} = @{$data->[0]} if ref $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | # We're gonna have problems if we don't have anything in %p by now... | ||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if(not %p) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||||||
| 207 | 0 | carp "Database error: no search parameters"; | |||||||||||||||||||||||||||||||
| 208 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 209 | 0 | return; | |||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | # Find the row for this join | ||||||||||||||||||||||||||||||||
| 213 | # First - create the "WHERE" clause | ||||||||||||||||||||||||||||||||
| 214 | # Note that we're literalizing the values so we can reuse this later | ||||||||||||||||||||||||||||||||
| 215 | 0 | my @cols = map { $_->{name} } $tab_obj->_columns; | |||||||||||||||||||||||||||||||
| 216 | my @wk = map { $map{$_} | ||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | . ((defined $p{$_})?('=' . $handle->quote($p{$_})):(' IS NULL')) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 218 | 0 | grep { exists $p{$_} } @cols; | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 0 | my $table_name = $tab_obj->_sql_name; | |||||||||||||||||||||||||||||||
| 221 | 0 | my $wclause = " FROM $table_name WHERE " . join(' AND ', @wk); | |||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | # Create the SQL | ||||||||||||||||||||||||||||||||
| 224 | 0 | $sql = 'SELECT ' . join(',', map { $map{$_} } @cols) . $wclause; | |||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # Get the data | ||||||||||||||||||||||||||||||||
| 227 | 0 | my $data = $handle->selectall_arrayref($sql); | |||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # blow up if we can't find it | ||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | if(not $data or not $data->[0]) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data and $data->[0]
MC/DC0%0/2
$data and $data->[0]
$data
$data->[0]
Truth table0%0/3
A and B
A= $dataB= $data->[0]
| |||||||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | carp $self->E_NOROWSOUND if $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | carp 'Database error: ', $handle->errstr unless $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 234 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 235 | 0 | return; | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | # Blow up if there's too much of a good thing | ||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if(@$data > 1) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | { | ||||||||||||||||||||||||||||||||
| 241 | 0 | carp $self->E_TOOMANYROWS; | |||||||||||||||||||||||||||||||
| 242 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 243 | 0 | return; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | # Copy stuff back to the %p hash | ||||||||||||||||||||||||||||||||
| 247 | 0 | @p{@cols} = @{$data->[0]}; | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | # Rename it back to what's expected by the primary table | ||||||||||||||||||||||||||||||||
| 250 | $p{$_} = delete $p{$tab->{columns}{$_}} | ||||||||||||||||||||||||||||||||
| 251 | 0 | foreach grep { exists $p{$tab->{columns}{$_}} } | |||||||||||||||||||||||||||||||
| 252 | 0 | keys %{$tab->{columns}}; | |||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | # Copy it back to the %params hash | ||||||||||||||||||||||||||||||||
| 255 | 0 | $params{$_} = $p{$_} foreach grep { exists $p{$_} } | |||||||||||||||||||||||||||||||
| 256 | 0 | keys %{$tab->{columns}}; | |||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | # Save it to the results object | ||||||||||||||||||||||||||||||||
| 259 | 0 | push @row_data, @{$data->[0]}; | |||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | # Save undo instructions | ||||||||||||||||||||||||||||||||
| 262 | 0 | push @backout_cmds, "DELETE $wclause"; | |||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | # Now that we have the secondary rows, create the main one | ||||||||||||||||||||||||||||||||
| 266 | 0 | my ($keys, $values, $npk, @binds); | |||||||||||||||||||||||||||||||
| 267 | 0 | $tab_obj = $self->table($primary); | |||||||||||||||||||||||||||||||
| 268 | 0 | ($flag, $keys, $values, $npk, @binds) = | |||||||||||||||||||||||||||||||
| 269 | $self->__check_constraints($tab_obj, %params); | ||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $self->__do_rollback(@backout_cmds) and return unless $flag; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$flag || $self->__do_rollback(@backout_cmds) && (return)
$self->__do_rollback(@backout_cmds) and return
MC/DC0%0/2
$flag || $self->__do_rollback(@backout_cmds) && (return)
$flag
$self->__do_rollback(@backout_cmds)
Truth table0%0/3
A || B
A= $flagB= $self->__do_rollback(@backout_cmds)
| |||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | # Prepare & execute the statement | ||||||||||||||||||||||||||||||||
| 274 | 0 | my $table_name = $tab_obj->_sql_name; | |||||||||||||||||||||||||||||||
| 275 | 0 | my $sql = "INSERT INTO $table_name ($keys) VALUES ($values)"; | |||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | unshift @binds, undef if @binds; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 0 | my $dbres = $handle->do($sql, @binds); | |||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | # Get any null primary key info | ||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if($npk) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||||||
| 282 | 0 | my @pk = $tab_obj->_primary_key; | |||||||||||||||||||||||||||||||
| 283 | 0 | my $np = $tab_obj->_select_null_primary; | |||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if(@pk and $np) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@pk and $np
MC/DC0%0/2
@pk and $np
@pk
$np
Truth table0%0/3
A and B
A= @pkB= $np
| |||||||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||||||
| 286 | 0 | my $data = $handle->selectall_arrayref($np); | |||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if(not $data or not defined $data->[0][0]) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data and defined $data->[0][0]
MC/DC0%0/2
$data and defined $data->[0][0]
$data
defined $data->[0][0]
Truth table0%0/3
A and B
A= $dataB= defined $data->[0][0]
| |||||||||||||||||||||||||||||||||
| 288 | { | ||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | carp $self->E_NOROWSFOUND if $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | carp "Database error: " . $handle->errstr unless $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 291 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 292 | 0 | return; | |||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | # Save the primary key data | ||||||||||||||||||||||||||||||||
| 296 | 0 | @params{@pk} = @{$data->[0]}; | |||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | # Finally, look for what we just inserted | ||||||||||||||||||||||||||||||||
| 301 | my @search_params = | ||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | map {($_, (defined $params{$_}?(eq => $params{$_}):('isnull')))} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 303 | 0 | grep { exists $params{$_} } | |||||||||||||||||||||||||||||||
| 304 | 0 | map { $_->{name} } | |||||||||||||||||||||||||||||||
| 305 | $self->_columns; | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 0 | my @res = $self->search(\@search_params); | |||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | if(not @res) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 310 | { | ||||||||||||||||||||||||||||||||
| 311 | 0 | $self->__do_rollback(@backout_cmds); | |||||||||||||||||||||||||||||||
| 312 | 0 | return; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | if(@res>1) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||||||
| 317 | # Now we're in somewhat murky water: we have too many matching | ||||||||||||||||||||||||||||||||
| 318 | # rows. We can't delete what we've inserted, because we may | ||||||||||||||||||||||||||||||||
| 319 | # nuke too many rows. We can't delete the secondary table rows | ||||||||||||||||||||||||||||||||
| 320 | # because that may screw up referential integrity. So, just call | ||||||||||||||||||||||||||||||||
| 321 | # rollback on the handle, and if the database is transactional, let | ||||||||||||||||||||||||||||||||
| 322 | # it handle the fallout. Otherwise, there's not a whole lot we can | ||||||||||||||||||||||||||||||||
| 323 | # do... | ||||||||||||||||||||||||||||||||
| 324 | 0 | carp $self->E_TOOMANYROWS; | |||||||||||||||||||||||||||||||
| 325 | 0 | $self->__do_rollback; | |||||||||||||||||||||||||||||||
| 326 | 0 | return; | |||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | # Turn off warnings and commit | ||||||||||||||||||||||||||||||||
| 330 | 0 | local ($SIG{__WARN__}) = $self->w__noop; | |||||||||||||||||||||||||||||||
| 331 | 0 | $handle->commit; | |||||||||||||||||||||||||||||||
| 332 | 0 | return $res[0]; | |||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | sub no_can_do | ||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
no_can_do: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 337 | 0 | my ($self, $method) = @_; | |||||||||||||||||||||||||||||||
| 338 | 0 | carp "Can't $method via a join - use the individual tables for that"; | |||||||||||||||||||||||||||||||
| 339 | return | ||||||||||||||||||||||||||||||||
| 340 | 0 | } | |||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | sub delete { $_[0]->no_can_do('delete'); } | ||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | sub bulk_create { $_[0]->no_can_do('bulk_create'); } | ||||||||||||||||||||||||||||||
|
Subroutine
bulk_create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | sub create_only { $_[0]->no_can_do('create_only'); } | ||||||||||||||||||||||||||||||
|
Subroutine
create_only: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 1 | 1; | |||||||||||||||||||||||||||||||
| 347 | __END__ | ||||||||||||||||||||||||||||||||