From 5419d4428c86c488a43124f85e5407d7cbae6541 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:17:47 +0200 Subject: Adding upstream version 1.11.1. Signed-off-by: Daniel Baumann --- library/Director/Data/Db/DbDataFormatter.php | 16 ++++- library/Director/Data/Db/DbObject.php | 73 +++++++++++++++++++--- .../Director/Data/Db/ServiceSetQueryBuilder.php | 2 + 3 files changed, 82 insertions(+), 9 deletions(-) (limited to 'library/Director/Data/Db') diff --git a/library/Director/Data/Db/DbDataFormatter.php b/library/Director/Data/Db/DbDataFormatter.php index d6e4eeb..91fc776 100644 --- a/library/Director/Data/Db/DbDataFormatter.php +++ b/library/Director/Data/Db/DbDataFormatter.php @@ -6,7 +6,7 @@ use InvalidArgumentException; class DbDataFormatter { - public static function normalizeBoolean($value) + public static function normalizeBoolean($value): ?string { if ($value === 'y' || $value === '1' || $value === true || $value === 1) { return 'y'; @@ -20,7 +20,19 @@ class DbDataFormatter throw new InvalidArgumentException(sprintf( 'Got invalid boolean: %s', - var_export($value, 1) + var_export($value, true) )); } + + public static function booleanForDbValue($value): ?bool + { + if ($value === 'y') { + return true; + } + if ($value === 'n') { + return false; + } + + return $value; // let this fail elsewhere, if not null + } } diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index 6ecae8b..114b61b 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -80,6 +80,9 @@ abstract class DbObject protected $binaryProperties = []; + /* key/value!! */ + protected $booleans = []; + /** * Filled with object instances when prefetchAll is used */ @@ -346,6 +349,16 @@ abstract class DbObject return $this->$func($value); } + if ($this->getUuidColumn() === $key) { + if (strlen($value) > 16) { + $value = Uuid::fromString($value)->getBytes(); + } + } + + if ($this->propertyIsBoolean($key)) { + $value = DbDataFormatter::normalizeBoolean($value); + } + if (! $this->hasProperty($key)) { throw new InvalidArgumentException(sprintf( 'Trying to set invalid key "%s"', @@ -372,7 +385,10 @@ abstract class DbObject return $this; } if ($key === 'id' || substr($key, -3) === '_id') { - if ((int) $value === (int) $this->properties[$key]) { + if ($value !== null + && $this->properties[$key] !== null + && (int) $value === (int) $this->properties[$key] + ) { return $this; } } @@ -553,7 +569,7 @@ abstract class DbObject /** * Unique key name * - * @return string + * @return string|array */ public function getKeyName() { @@ -706,8 +722,7 @@ abstract class DbObject */ protected function loadFromDb() { - $select = $this->db->select()->from($this->table)->where($this->createWhere()); - $properties = $this->db->fetchRow($select); + $properties = $this->db->fetchRow($this->prepareObjectQuery()); if (empty($properties)) { if (is_array($this->getKeyName())) { @@ -728,6 +743,11 @@ abstract class DbObject return $this->setDbProperties($properties); } + public function prepareObjectQuery() + { + return $this->db->select()->from($this->table)->where($this->createWhere()); + } + /** * @param object|array $row * @param Db $db @@ -878,6 +898,11 @@ abstract class DbObject return in_array($column, $this->binaryProperties) || $this->getUuidColumn() === $column; } + public function propertyIsBoolean($property) + { + return array_key_exists($property, $this->booleans); + } + /** * Store object to database * @@ -959,7 +984,7 @@ abstract class DbObject $this->table, $this->getLogId(), $e->getMessage(), - var_export($this->getProperties(), 1) // TODO: Remove properties + var_export($this->getProperties(), true) // TODO: Remove properties )); } @@ -1027,7 +1052,7 @@ abstract class DbObject if ($this->hasUuidColumn() && $this->properties[$this->uuidColumn] !== null) { return $this->db->quoteInto( sprintf('%s = ?', $this->getUuidColumn()), - $this->connection->quoteBinary($this->getUniqueId()->getBytes()) + $this->connection->quoteBinary($this->getOriginalProperty($this->uuidColumn)) ); } if ($id = $this->getAutoincId()) { @@ -1301,6 +1326,40 @@ abstract class DbObject return $obj; } + /** + * @param $id + * @param DbConnection $connection + * @return static + */ + public static function loadOptional($id, DbConnection $connection): ?DbObject + { + if ($prefetched = static::getPrefetched($id)) { + return $prefetched; + } + /** @var DbObject $obj */ + $obj = new static(); + + if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) { + $table = $obj->getTableName(); + assert($connection instanceof Db); + $uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch()); + if ($uuid) { + return self::$dbObjectStore->load($table, $uuid); + } + + return null; + } + + $obj->setConnection($connection)->setKey($id); + $properties = $connection->getDbAdapter()->fetchRow($obj->prepareObjectQuery()); + if (empty($properties)) { + return null; + } + + $obj->setDbProperties($properties); + return $obj; + } + /** * @param DbConnection $connection * @param \Zend_Db_Select $query @@ -1436,7 +1495,7 @@ abstract class DbObject )); } - public static function loadWithUniqueId(UuidInterface $uuid, DbConnection $connection) + public static function loadWithUniqueId(UuidInterface $uuid, DbConnection $connection): ?DbObject { $db = $connection->getDbAdapter(); $obj = new static; diff --git a/library/Director/Data/Db/ServiceSetQueryBuilder.php b/library/Director/Data/Db/ServiceSetQueryBuilder.php index 7841d1e..597fe0e 100644 --- a/library/Director/Data/Db/ServiceSetQueryBuilder.php +++ b/library/Director/Data/Db/ServiceSetQueryBuilder.php @@ -27,6 +27,8 @@ class ServiceSetQueryBuilder /** @var \Zend_Db_Adapter_Abstract */ protected $db; + protected $searchColumns = []; + /** * @param ?UuidInterface $uuid */ -- cgit v1.2.3