Completed
Push — master ( 3097a5...bdf786 )
by Ron
03:10
created

RunnableInsert::insertRows()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
c 1
b 0
f 1
dl 0
loc 13
rs 9.9332
cc 4
nc 3
nop 1
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
use Kir\MySQL\Databases\MySQL;
9
use Traversable;
10
11
class RunnableInsert extends Insert implements DDLPreparable {
12
	use CreateDDLRunnable;
13
14
	/**
15
	 * @param array|Traversable|mixed $rows
16
	 * @return int[] Insert IDs
17
	 */
18
	public function insertRows($rows) {
19
		if (!(is_array($rows) || $rows instanceof Traversable)) {
20
			throw new BadMethodCallException('Expected $rows to by an array or an instance of \\Traversable');
21
		}
22
		$result = [];
23
		$query = $this->__toString();
24
		$stmt = $this->db()->prepare($query);
25
		foreach ($rows as $row) {
26
			$stmt->execute($row);
27
			$result[] = (int) $this->db()->getLastInsertId();
28
		}
29
		$stmt->closeCursor();
30
		return $result;
31
	}
32
33
	/**
34
	 * @return DDLRunnable
35
	 */
36
	public function prepare() {
37
		return $this->createPreparable($this->db()->prepare($this), function() {
38
			return (int) $this->db()->getLastInsertId();
39
		});
40
	}
41
42
	/**
43
	 * @param array $params
44
	 * @return int
45
	 */
46
	public function run(array $params = []) {
47
		return $this->prepare()->run($params);
48
	}
49
}
50