Issues (83)

src/Builder/RunnableInsert.php (1 issue)

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