Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error on mapper update/erase if the table has no primary key #285

Merged
merged 1 commit into from
Nov 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions db/sql/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
//! SQL data mapper
class Mapper extends \DB\Cursor {

//@{ Error messages
const
E_PKey='Table %s does not have a primary key';
//@}

protected
//! PDO wrapper
$db,
Expand Down Expand Up @@ -493,7 +498,6 @@ function update() {
$args=[];
$ctr=0;
$pairs='';
$filter='';
$pkeys=[];
foreach ($this->fields as $key=>$field)
if ($field['pkey'])
Expand All @@ -507,13 +511,16 @@ function update() {
$pairs.=($pairs?',':'').$this->db->quotekey($key).'=?';
$args[++$ctr]=[$field['value'],$field['pdo_type']];
}
foreach ($this->fields as $key=>$field)
if ($field['pkey']) {
$filter.=($filter?' AND ':' WHERE ').
$this->db->quotekey($key).'=?';
$args[++$ctr]=[$field['previous'],$field['pdo_type']];
}
if ($pairs) {
$filter='';
foreach ($this->fields as $key=>$field)
if ($field['pkey']) {
$filter.=($filter?' AND ':' WHERE ').
$this->db->quotekey($key).'=?';
$args[++$ctr]=[$field['previous'],$field['pdo_type']];
}
if (!$filter)
user_error(sprintf(self::E_PKey,$this->source),E_USER_ERROR);
$sql='UPDATE '.$this->table.' SET '.$pairs.$filter;
$this->db->exec($sql,$args);
}
Expand Down Expand Up @@ -572,6 +579,8 @@ function erase($filter=NULL,$quick=TRUE) {
$field['previous']=NULL;
unset($field);
}
if (!$filter)
user_error(sprintf(self::E_PKey,$this->source),E_USER_ERROR);
foreach ($this->adhoc as &$field) {
$field['value']=NULL;
unset($field);
Expand Down