Completed
Push — master ( 353d0e...6c7828 )
by Ron
01:57
created

RunnableInsert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 MySQL $db
16
	 * @param array $options
17
	 */
18
	public function __construct(MySQL $db, array $options = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
		parent::__construct($db);
20
	}
21
22
	/**
23
	 * @param array|Traversable $rows
24
	 * @return int[] Insert IDs
25
	 */
26
	public function insertRows($rows) {
27
		if (!(is_array($rows) || $rows instanceof Traversable)) {
28
			throw new BadMethodCallException('Expected $rows to by an array or an instance of \\Traversable');
29
		}
30
		$result = [];
31
		$query = $this->__toString();
32
		$stmt = $this->db()->prepare($query);
33
		foreach ($rows as $row) {
34
			$stmt->execute($row);
35
			$result[] = (int) $this->db()->getLastInsertId();
36
		}
37
		$stmt->closeCursor();
38
		return $result;
39
	}
40
41
	/**
42
	 * @return DDLRunnable
43
	 */
44
	public function prepare() {
45
		return $this->createPreparable($this->db()->prepare($this), function() {
46
			return (int) $this->db()->getLastInsertId();
47
		});
48
	}
49
50
	/**
51
	 * @param array $params
52
	 * @return int
53
	 * @throws Exception
54
	 */
55
	public function run(array $params = array()) {
56
		return $this->prepare()->run($params);
57
	}
58
}
59