| 1 | <?php |
||
| 2 | |||
| 3 | namespace Kir\MySQL\Builder; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use Kir\MySQL\Builder\Internal\DDLPreparable; |
||
| 7 | use Kir\MySQL\Builder\Internal\DDLRunnable; |
||
| 8 | use Kir\MySQL\Builder\Traits\CreateDDLRunnable; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @implements DDLPreparable<int> |
||
| 12 | */ |
||
| 13 | class RunnableInsert extends Insert implements DDLPreparable { |
||
| 14 | /** @use CreateDDLRunnable<int> */ |
||
| 15 | use CreateDDLRunnable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @inheritDoc |
||
| 19 | */ |
||
| 20 | public function insertRows(iterable $rows) { |
||
| 21 | if(!is_iterable($rows)) { |
||
| 22 | throw new BadMethodCallException('Expected $rows to by an iterable'); |
||
| 23 | } |
||
| 24 | $result = []; |
||
| 25 | $query = $this->__toString(); |
||
| 26 | $stmt = $this->db()->prepare($query); |
||
| 27 | foreach($rows as $row) { |
||
| 28 | $stmt->execute($row); |
||
| 29 | $result[] = (int) $this->db()->getLastInsertId(); |
||
| 30 | } |
||
| 31 | $stmt->closeCursor(); |
||
| 32 | |||
| 33 | return $result; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritDoc |
||
| 38 | */ |
||
| 39 | public function run(array $params = []): int { |
||
| 40 | return $this->prepare()->run($params); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return DDLRunnable<int> |
||
| 45 | */ |
||
| 46 | public function prepare(): DDLRunnable { |
||
| 47 | return $this->createPreparable( |
||
| 48 | $this->db()->prepare($this), |
||
| 49 | fn() => (int) $this->db()->getLastInsertId() |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 |