From efe23ed404bcdc65536aebab708bda93135ba903 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 28 Jan 2016 16:42:59 +0100 Subject: [PATCH] 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 --- classes/Managed_DataObject.php | 24 +++++++++++++++---- plugins/OStatus/classes/HubSub.php | 2 +- plugins/OStatus/classes/Ostatus_profile.php | 2 +- plugins/Oembed/OembedPlugin.php | 2 +- .../StoreRemoteMediaPlugin.php | 2 +- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index 68de8922ce..c02685e55e 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -420,12 +420,16 @@ abstract class Managed_DataObject extends Memcached_DataObject * @param DB_DataObject &$orig Must be "instanceof" $this * @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) { 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 $this->query('BEGIN'); @@ -452,11 +456,23 @@ abstract class Managed_DataObject extends Memcached_DataObject 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()), implode(', ', $parts), - $pid, - $this->_quote($orig->$pid)); + implode(' AND ', $pidWhere)); $result = $this->query($qry); if ($result === false) { diff --git a/plugins/OStatus/classes/HubSub.php b/plugins/OStatus/classes/HubSub.php index e2b5dc2a6d..a2d6e2e51e 100644 --- a/plugins/OStatus/classes/HubSub.php +++ b/plugins/OStatus/classes/HubSub.php @@ -323,7 +323,7 @@ class HubSub extends Managed_DataObject $orig = clone($this); $this->callback = $httpscallback; $this->hashkey = self::hashkey($this->getTopic(), $this->callback); - $this->updateWithKeys($orig, 'hashkey'); + $this->updateWithKeys($orig); return true; } } diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 2a540e21d2..d08f913309 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -1853,7 +1853,7 @@ class Ostatus_profile extends Managed_DataObject } 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); $this->subscribe(); diff --git a/plugins/Oembed/OembedPlugin.php b/plugins/Oembed/OembedPlugin.php index 23ee6148ea..184f962652 100644 --- a/plugins/Oembed/OembedPlugin.php +++ b/plugins/Oembed/OembedPlugin.php @@ -305,7 +305,7 @@ class OembedPlugin extends Plugin $thumbnail->width = $info[0]; // array indexes documented on php.net: $thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php // Throws exception on failure. - $thumbnail->updateWithKeys($orig, 'file_id'); + $thumbnail->updateWithKeys($orig); } public function onPluginVersion(array &$versions) diff --git a/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php b/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php index e3b95410be..34846748e7 100644 --- a/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php +++ b/plugins/StoreRemoteMedia/StoreRemoteMediaPlugin.php @@ -108,7 +108,7 @@ class StoreRemoteMediaPlugin extends Plugin $file->width = $info[0]; // array indexes documented on php.net: $file->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php // Throws exception on failure. - $file->updateWithKeys($orig, 'id'); + $file->updateWithKeys($orig); } // Get rid of the file from memory unset($imgData);