diff --git a/src/daos/ItemsInterface.php b/src/daos/ItemsInterface.php index bb2bb102ac..1dcba9ed5d 100644 --- a/src/daos/ItemsInterface.php +++ b/src/daos/ItemsInterface.php @@ -6,6 +6,7 @@ use DateTime; use DateTimeImmutable; +use Generator; /** * Interface describing concrete DAO for working with items. @@ -184,9 +185,9 @@ public function bulkStatusUpdate(array $statuses): void; /** * returns raw items table contents * - * @return array[] of all items + * @return Generator of all items */ - public function getRaw(): array; + public function getRaw(): Generator; /** * inserts raw data into items table diff --git a/src/daos/SourcesInterface.php b/src/daos/SourcesInterface.php index 0b9947047e..e8039f6653 100644 --- a/src/daos/SourcesInterface.php +++ b/src/daos/SourcesInterface.php @@ -4,6 +4,8 @@ namespace daos; +use Generator; + /** * Interface describing concrete DAO for working with sources. */ @@ -110,9 +112,9 @@ public function checkIfExists(string $title, string $spout, array $params): int; /** * returns raw sources table contents * - * @return array[] of all sources + * @return Generator of all sources */ - public function getRaw(): array; + public function getRaw(): Generator; /** * inserts raw data into sources table diff --git a/src/daos/StatementsInterface.php b/src/daos/StatementsInterface.php index 59333cc5e1..5cc2ac26e6 100644 --- a/src/daos/StatementsInterface.php +++ b/src/daos/StatementsInterface.php @@ -4,6 +4,8 @@ namespace daos; +use Generator; + /** * Interface for class providing SQL helpers. */ @@ -100,16 +102,14 @@ public static function bool(bool $bool): string; public static function datetime(\DateTime $date): string; /** - * Ensure row values have the appropriate PHP type. This assumes we are - * using buffered queries (sql results are in PHP memory);. + * Ensure row values have the appropriate PHP type. * - * @param array $rows array of associative array representing row results + * @param iterable $rows array of associative array representing row results * @param array $expectedRowTypes associative array mapping columns to PDO types * - * @return array of associative array representing row results having - * expected types + * @return Generator of associative array representing row results having expected types */ - public static function ensureRowTypes(array $rows, array $expectedRowTypes): array; + public static function ensureRowTypes(iterable $rows, array $expectedRowTypes): Generator; /** * convert string array to string for storage in table row diff --git a/src/daos/mysql/Items.php b/src/daos/mysql/Items.php index 56be3c5c6e..3a2015bdda 100644 --- a/src/daos/mysql/Items.php +++ b/src/daos/mysql/Items.php @@ -8,6 +8,7 @@ use daos\ItemOptions; use DateTime; use DateTimeImmutable; +use Generator; use helpers\Configuration; use function helpers\functions\map; use Monolog\Logger; @@ -407,7 +408,7 @@ public function sync(int $sinceId, DateTime $notBefore, DateTime $since, int $ho * @return int lowest id of interest */ public function lowestIdOfInterest(): int { - $lowest = static::$stmt::ensureRowTypes( + $lowests = static::$stmt::ensureRowTypes( $this->database->exec( 'SELECT id FROM ' . $this->configuration->dbPrefix . 'items AS items WHERE ' . static::$stmt::isTrue('unread') . @@ -415,8 +416,8 @@ public function lowestIdOfInterest(): int { ' ORDER BY id LIMIT 1'), ['id' => DatabaseInterface::PARAM_INT] ); - if ($lowest) { - return $lowest[0]['id']; + foreach ($lowests as $lowest) { + return $lowest['id']; } return 0; @@ -667,7 +668,7 @@ public function bulkStatusUpdate(array $statuses): void { /** * {@inheritdoc} */ - public function getRaw(): array { + public function getRaw(): Generator { $stmt = static::$stmt; $items = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'items'); diff --git a/src/daos/mysql/Sources.php b/src/daos/mysql/Sources.php index 9e1907e7b6..c7bbda3354 100644 --- a/src/daos/mysql/Sources.php +++ b/src/daos/mysql/Sources.php @@ -5,6 +5,7 @@ namespace daos\mysql; use daos\DatabaseInterface; +use Generator; use helpers\Configuration; use function helpers\Functions\map; @@ -281,7 +282,7 @@ public function checkIfExists(string $title, string $spout, array $params): int /** * {@inheritdoc} */ - public function getRaw(): array { + public function getRaw(): Generator { $stmt = static::$stmt; $sources = $this->database->exec('SELECT * FROM ' . $this->configuration->dbPrefix . 'sources'); diff --git a/src/daos/mysql/Statements.php b/src/daos/mysql/Statements.php index 3b455551ec..01cff4e48a 100644 --- a/src/daos/mysql/Statements.php +++ b/src/daos/mysql/Statements.php @@ -5,6 +5,7 @@ namespace daos\mysql; use daos\DatabaseInterface; +use Generator; /** * MySQL specific statements @@ -138,17 +139,15 @@ public static function datetime(\DateTime $date): string { } /** - * Ensure row values have the appropriate PHP type. This assumes we are - * using buffered queries (sql results are in PHP memory). + * Ensure row values have the appropriate PHP type. * - * @param array $rows array of associative array representing row results + * @param iterable $rows array of associative array representing row results * @param array $expectedRowTypes associative array mapping columns to PDO types * - * @return array of associative array representing row results having - * expected types + * @return Generator of associative array representing row results having expected types */ - public static function ensureRowTypes(array $rows, array $expectedRowTypes): array { - foreach ($rows as $rowIndex => $row) { + public static function ensureRowTypes(iterable $rows, array $expectedRowTypes): Generator { + foreach ($rows as $row) { foreach ($expectedRowTypes as $columnIndex => $type) { if (array_key_exists($columnIndex, $row)) { if ($type & DatabaseInterface::PARAM_NULL) { @@ -185,13 +184,13 @@ public static function ensureRowTypes(array $rows, array $expectedRowTypes): arr $value = null; } if ($value !== null) { - $rows[$rowIndex][$columnIndex] = $value; + $row[$columnIndex] = $value; } } } - } - return $rows; + yield $row; + } } /** diff --git a/src/daos/pgsql/Statements.php b/src/daos/pgsql/Statements.php index aa2297ce0a..8d5d716558 100644 --- a/src/daos/pgsql/Statements.php +++ b/src/daos/pgsql/Statements.php @@ -5,6 +5,7 @@ namespace daos\pgsql; use daos\DatabaseInterface; +use Generator; /** * PostgreSQL specific statements @@ -74,17 +75,15 @@ public static function csvRowMatches(string $column, string $value): string { } /** - * Ensure row values have the appropriate PHP type. This assumes we are - * using buffered queries (sql results are in PHP memory). + * Ensure row values have the appropriate PHP type. * - * @param array $rows array of associative array representing row results + * @param iterable $rows array of associative array representing row results * @param array $expectedRowTypes associative array mapping columns to PDO types * - * @return array of associative array representing row results having - * expected types + * @return Generator of associative array representing row results having expected types */ - public static function ensureRowTypes(array $rows, array $expectedRowTypes): array { - foreach ($rows as $rowIndex => $row) { + public static function ensureRowTypes(iterable $rows, array $expectedRowTypes): Generator { + foreach ($rows as $row) { foreach ($expectedRowTypes as $columnIndex => $type) { if (array_key_exists($columnIndex, $row)) { if ($type & DatabaseInterface::PARAM_NULL) { @@ -110,13 +109,13 @@ public static function ensureRowTypes(array $rows, array $expectedRowTypes): arr $value = null; } if ($value !== null) { - $rows[$rowIndex][$columnIndex] = $value; + $row[$columnIndex] = $value; } } } - } - return $rows; + yield $row; + } } /**