|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Data\Db; |
| 6 | + |
| 7 | +use Throwable; |
| 8 | +use Yiisoft\Data\Writer\DataWriterException; |
| 9 | +use Yiisoft\Data\Writer\DataWriterInterface; |
| 10 | +use Yiisoft\Db\Connection\ConnectionInterface; |
| 11 | + |
| 12 | +use function is_array; |
| 13 | + |
| 14 | +/** |
| 15 | + * QueryDataWriter allows writing (inserting/updating) and deleting data to/from a database table. |
| 16 | + * |
| 17 | + * @template TKey as array-key |
| 18 | + * @template TValue as array |
| 19 | + * |
| 20 | + * @implements DataWriterInterface<TKey, TValue> |
| 21 | + * |
| 22 | + * Example usage: |
| 23 | + * |
| 24 | + * ```php |
| 25 | + * $writer = new QueryDataWriter($db, 'customer'); |
| 26 | + * |
| 27 | + * // Write (insert or update) items |
| 28 | + * $writer->write([ |
| 29 | + * ['id' => 1, 'name' => 'John', 'email' => '[email protected]'], |
| 30 | + * ['id' => 2, 'name' => 'Jane', 'email' => '[email protected]'], |
| 31 | + * ]); |
| 32 | + * |
| 33 | + * // Delete items |
| 34 | + * $writer->delete([ |
| 35 | + * ['id' => 1], |
| 36 | + * ['id' => 2], |
| 37 | + * ]); |
| 38 | + * ``` |
| 39 | + * |
| 40 | + * @psalm-suppress ClassMustBeFinal This class can be extended. |
| 41 | + */ |
| 42 | +class QueryDataWriter implements DataWriterInterface |
| 43 | +{ |
| 44 | + /** |
| 45 | + * @param ConnectionInterface $db The database connection instance. |
| 46 | + * @param string $table The name of the table to write to or delete from. |
| 47 | + * @param array<string> $primaryKey The primary key column name(s). Defaults to ['id']. |
| 48 | + * @param bool $useUpsert Whether to use UPSERT (insert or update) instead of plain INSERT. |
| 49 | + * When true, existing records will be updated, otherwise only new records will be inserted. |
| 50 | + * Defaults to true. |
| 51 | + */ |
| 52 | + public function __construct( |
| 53 | + private readonly ConnectionInterface $db, |
| 54 | + private readonly string $table, |
| 55 | + private readonly array $primaryKey = ['id'], |
| 56 | + private readonly bool $useUpsert = true, |
| 57 | + ) { |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Write items to the database table. |
| 62 | + * |
| 63 | + * If `$useUpsert` is true (default), this will insert new records or update existing ones. |
| 64 | + * If `$useUpsert` is false, this will only insert new records. |
| 65 | + * |
| 66 | + * @param iterable $items Items to write. Each item must be an associative array of column => value. |
| 67 | + * |
| 68 | + * @throws DataWriterException If there is an error while writing items. |
| 69 | + */ |
| 70 | + public function write(iterable $items): void |
| 71 | + { |
| 72 | + try { |
| 73 | + foreach ($items as $item) { |
| 74 | + if (!is_array($item)) { |
| 75 | + throw new DataWriterException('Each item must be an array.'); |
| 76 | + } |
| 77 | + |
| 78 | + if (empty($item)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if ($this->useUpsert) { |
| 83 | + $this->db->createCommand()->upsert($this->table, $item)->execute(); |
| 84 | + } else { |
| 85 | + $this->db->createCommand()->insert($this->table, $item)->execute(); |
| 86 | + } |
| 87 | + } |
| 88 | + } catch (Throwable $e) { |
| 89 | + throw new DataWriterException( |
| 90 | + 'Failed to write items to table "' . $this->table . '": ' . $e->getMessage(), |
| 91 | + $e->getCode(), |
| 92 | + $e, |
| 93 | + ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Delete items from the database table. |
| 99 | + * |
| 100 | + * Each item should contain the primary key column(s) used to identify the record to delete. |
| 101 | + * |
| 102 | + * @param iterable $items Items to delete. Each item must be an associative array containing at least |
| 103 | + * the primary key column(s). |
| 104 | + * |
| 105 | + * @throws DataWriterException If there is an error deleting items. |
| 106 | + */ |
| 107 | + public function delete(iterable $items): void |
| 108 | + { |
| 109 | + try { |
| 110 | + foreach ($items as $item) { |
| 111 | + if (!is_array($item)) { |
| 112 | + throw new DataWriterException('Each item must be an array.'); |
| 113 | + } |
| 114 | + |
| 115 | + if (empty($item)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + $condition = []; |
| 120 | + foreach ($this->primaryKey as $key) { |
| 121 | + if (!isset($item[$key])) { |
| 122 | + throw new DataWriterException( |
| 123 | + 'Item must contain primary key column "' . $key . '" for deletion.', |
| 124 | + ); |
| 125 | + } |
| 126 | + $condition[$key] = $item[$key]; |
| 127 | + } |
| 128 | + |
| 129 | + $this->db->createCommand()->delete($this->table, $condition)->execute(); |
| 130 | + } |
| 131 | + } catch (DataWriterException $e) { |
| 132 | + throw $e; |
| 133 | + } catch (Throwable $e) { |
| 134 | + throw new DataWriterException( |
| 135 | + 'Failed to delete items from table "' . $this->table . '": ' . $e->getMessage(), |
| 136 | + $e->getCode(), |
| 137 | + $e, |
| 138 | + ); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments