updateWithKeys now understands multi-column keys

and automatically identifies _which_ columns are the right ones,
so for example 'uri' primary keys don't need to be explicitly set
This commit is contained in:
Mikael Nordfeldth 2016-01-28 16:42:59 +01:00
parent d94f9031ff
commit efe23ed404
5 changed files with 24 additions and 8 deletions

View File

@ -420,12 +420,16 @@ abstract class Managed_DataObject extends Memcached_DataObject
* @param DB_DataObject &$orig Must be "instanceof" $this * @param DB_DataObject &$orig Must be "instanceof" $this
* @param string $pid Primary ID column (no escaping is done on column name!) * @param string $pid Primary ID column (no escaping is done on column name!)
*/ */
public function updateWithKeys(Managed_DataObject $orig, $pid='id') public function updateWithKeys(Managed_DataObject $orig, $pid=null)
{ {
if (!$orig instanceof $this) { if (!$orig instanceof $this) {
throw new ServerException('Tried updating a DataObject with a different class than itself.'); throw new ServerException('Tried updating a DataObject with a different class than itself.');
} }
if ($this->N <1) {
throw new ServerException('DataObject must be the result of a query (N>=1) before updateWithKeys()');
}
// do it in a transaction // do it in a transaction
$this->query('BEGIN'); $this->query('BEGIN');
@ -452,11 +456,23 @@ abstract class Managed_DataObject extends Memcached_DataObject
return true; return true;
} }
$qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s = %4$s', if ($pid === null) {
$schema = static::schemaDef();
$pid = $schema['primary key'];
unset($schema);
}
$pidWhere = array();
foreach((array)$pid as $pidCol) {
$pidWhere[] = sprintf('%1$s = %2$s', $pidCol, $this->_quote($orig->$pidCol));
}
if (empty($pidWhere)) {
throw new ServerException('No primary ID column(s) set for updateWithKeys');
}
$qry = sprintf('UPDATE %1$s SET %2$s WHERE %3$s',
common_database_tablename($this->tableName()), common_database_tablename($this->tableName()),
implode(', ', $parts), implode(', ', $parts),
$pid, implode(' AND ', $pidWhere));
$this->_quote($orig->$pid));
$result = $this->query($qry); $result = $this->query($qry);
if ($result === false) { if ($result === false) {

View File

@ -323,7 +323,7 @@ class HubSub extends Managed_DataObject
$orig = clone($this); $orig = clone($this);
$this->callback = $httpscallback; $this->callback = $httpscallback;
$this->hashkey = self::hashkey($this->getTopic(), $this->callback); $this->hashkey = self::hashkey($this->getTopic(), $this->callback);
$this->updateWithKeys($orig, 'hashkey'); $this->updateWithKeys($orig);
return true; return true;
} }
} }

View File

@ -1853,7 +1853,7 @@ class Ostatus_profile extends Managed_DataObject
} }
common_debug('URIFIX Updating Ostatus_profile URI for '.$orig->uri.' to '.$this->uri); common_debug('URIFIX Updating Ostatus_profile URI for '.$orig->uri.' to '.$this->uri);
$this->updateWithKeys($orig, 'uri'); // 'uri' is the primary key column $this->updateWithKeys($orig); // Will use the PID column(s) in the 'UPDATE ... WHERE [unique selector]'
common_debug('URIFIX Subscribing/renewing feedsub for Ostatus_profile '.$this->uri); common_debug('URIFIX Subscribing/renewing feedsub for Ostatus_profile '.$this->uri);
$this->subscribe(); $this->subscribe();

View File

@ -305,7 +305,7 @@ class OembedPlugin extends Plugin
$thumbnail->width = $info[0]; // array indexes documented on php.net: $thumbnail->width = $info[0]; // array indexes documented on php.net:
$thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php $thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
// Throws exception on failure. // Throws exception on failure.
$thumbnail->updateWithKeys($orig, 'file_id'); $thumbnail->updateWithKeys($orig);
} }
public function onPluginVersion(array &$versions) public function onPluginVersion(array &$versions)

View File

@ -108,7 +108,7 @@ class StoreRemoteMediaPlugin extends Plugin
$file->width = $info[0]; // array indexes documented on php.net: $file->width = $info[0]; // array indexes documented on php.net:
$file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php $file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
// Throws exception on failure. // Throws exception on failure.
$file->updateWithKeys($orig, 'id'); $file->updateWithKeys($orig);
} }
// Get rid of the file from memory // Get rid of the file from memory
unset($imgData); unset($imgData);