Insert::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of Yolk - Gamer Network's PHP Framework.
4
 *
5
 * Copyright (c) 2015 Gamer Network Ltd.
6
 *
7
 * Distributed under the MIT License, a copy of which is available in the
8
 * LICENSE file that was bundled with this package, or online at:
9
 * https://github.com/gamernetwork/yolk-database
10
 */
11
12
namespace yolk\database\query;
13
14
use yolk\contracts\database\DatabaseConnection;
15
16
/**
17
 * Generic insert query.
18
 */
19
class Insert extends BaseQuery {
20
21
	protected $ignore;
22
	protected $into;
23
	protected $columns;
24
	protected $values;
25
26
	public function __construct( DatabaseConnection $db ) {
27
		parent::__construct($db);
28
		$this->ignore  = false;
29
		$this->into    = '';
30
		$this->columns = [];
31
		$this->values  = [];
32
	}
33
34
	public function ignore( $ignore = true ) {
35
		$this->ignore = (bool) $ignore;
36
		return $this;
37
	}
38
39
	public function into( $table ) {
40
		$this->into = $this->quoteIdentifier($table);
41
		return $this;
42
	}
43
44
	public function cols( array $columns ) {
45
		$this->columns = [];
46
		foreach( $columns as $column ) {
47
			$this->columns[] = $this->quoteIdentifier($column);
48
		}
49
	}
50
51
	public function item( array $item ) {
52
53
		if( !$this->columns )
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
			$this->cols(array_keys($item));
55
56
		$values = [];
57
		$index  = count($this->values) + 1;
58
59
		foreach( $item as $column => $value ) {
60
			$column = "{$column}_{$index}";
61
			$values[] = ":{$column}";
62
			$this->params[$column] = $value;
63
		}
64
65
		$this->values[] = $values;
66
67
		return $this;
68
69
	}
70
71
	public function execute( $return_insert_id = true ) {
72
73
		$result = $this->db->execute(
74
			$this->__toString(),
75
			$this->params
76
		);
77
78
		if( $return_insert_id )
79
			$result = $this->db->insertId();
80
81
		return $result;
82
83
	}
84
85
	protected function compile() {
86
87
		$sql = [
88
			($this->ignore ? 'INSERT IGNORE' : 'INSERT'). ' '. $this->into,
89
			'('. implode(', ', $this->columns). ')',
90
			'VALUES',
91
		];
92
93
		foreach( $this->values as $list ) {
94
			$sql[] = sprintf('(%s),', implode(', ', $list));
95
		}
96
97
		// remove comma from last values item
98
		$tmp = substr(array_pop($sql), 0, -1);
99
		array_push($sql, $tmp);
100
101
		return $sql;
102
103
	}
104
105
}
106
107
// EOF