Completed
Push — develop ( 00578d...d00dfa )
by Michael
03:51
created

Query::whereIn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\Query;
12
13
/**
14
 * The query class.
15
 *
16
 * @since 1.0.0
17
 */
18
class Query implements QueryInterface
19
{
20
	/* @var \PDO The PDO. */
21
	private $pdo;
22
23
	/* @var array The bindings. */
24
	private $bindings;
25
26
	/* @var QueryBuilder The query builder. */
27
	private $queryBuilder;
28
29
	/**
30
	 * Construct a query object with the given pdo and table.
31
	 *
32
	 * @param \PDO $pdo
33
	 * @param string $table
34
	 */
35 16
	public function __construct(\PDO $pdo, $table)
36
	{
37 16
		$this->pdo = $pdo;
38 16
		$this->bindings = [];
39 16
		$this->queryBuilder = new QueryBuilder($table);
40 16
	}
41
42
	/**
43
	 * Returns a string representation of the query object.
44
	 *
45
	 * @return string a string representation of the query object.
46
	 */
47 16
	public function __toString()
48
	{
49 16
		return $this->queryBuilder->__toString();
50
	}
51
52
	/**
53
	 * {@inheritdoc}
54
	 */
55 12
	public function select($columns = ['*'])
56
	{
57 12
		$this->queryBuilder->select($columns);
58
59 12
		return $this;
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65 1
	public function insert(array $values)
66
	{
67 1
		$this->queryBuilder->insert($this->replaceValuesWithBindings($values, 'insert'));
68
69 1
		return $this;
70
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75 1
	public function update(array $values)
76
	{
77 1
		$this->queryBuilder->update($this->replaceValuesWithBindings($values, 'update'));
78
79 1
		return $this;
80
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85 1
	public function delete()
86
	{
87 1
		$this->queryBuilder->delete();
88
89 1
		return $this;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95 6
	public function where($column, $operator, $value)
96
	{
97 6
		if ($operator == 'IN') {
98 1
			$this->whereIn($column, $value);
99 1
		} else {
100 5
			$this->bindings['where'][] = $value;
101 5
			$this->queryBuilder->where($column, $operator, sprintf(':where%s', count($this->bindings['where'])));
102
		}
103
104 6
		return $this;
105
	}
106
107
	/**
108
	 * Set an additional where in condition.
109
	 *
110
	 * @param string $column
111
	 * @param mixed $values
112
	 * @return $this
113
	 */
114 1
	private function whereIn($column, $values)
115
	{
116 1
		$bindings = [];
117
118 1
		foreach ($values as $value) {
119 1
			$this->bindings['where'][] = $value;
120
121 1
			$bindings[] = sprintf(':where%s', count($this->bindings['where']));
122 1
		}
123
124 1
		$this->queryBuilder->where($column, 'IN', $bindings);
125
126 1
		return $this;
127
	}
128
129
	/**
130
	 * {@inheritdoc}
131
	 */
132 1
	public function groupBy($column)
133
	{
134 1
		$this->queryBuilder->groupBy($column);
135
136 1
		return $this;
137
	}
138
139
	/**
140
	 * {@inheritdoc}
141
	 */
142 1
	public function orderBy($column, $order = null)
143
	{
144 1
		$this->queryBuilder->orderBy($column, $order);
145
146 1
		return $this;
147
	}
148
149
	/**
150
	 * {@inheritdoc}
151
	 */
152 2
	public function limit($limit)
153
	{
154 2
		$this->bindings['limit'][] = (int) $limit;
155 2
		$this->queryBuilder->limit(':limit1');
156
157 2
		return $this;
158
	}
159
160
	/**
161
	 * {@inheritdoc}
162
	 */
163 1
	public function offset($offset)
164
	{
165 1
		$this->bindings['offset'][] = (int) $offset;
166 1
		$this->queryBuilder->offset(':offset1');
167
168 1
		return $this;
169
	}
170
171
	/**
172
	 * Returns the result of the executed prepared query.
173
	 *
174
	 * @return QueryResult the result of the executed prepared query.
175
	 */
176 4
	public function execute()
177
	{
178 4
		$pdoStatement = $this->pdo->prepare((string) $this);
179
180 4
		foreach ($this->bindings as $clause => $predicate) {
181 1
			foreach ($predicate as $key => $value) {
182 1
				if (is_bool($value)) {
183 1
					$type = \PDO::PARAM_BOOL;
184 1
				} elseif (is_null($value)) {
185 1
					$type = \PDO::PARAM_NULL;
186 1
				} elseif (is_int($value)) {
187 1
					$type = \PDO::PARAM_INT;
188 1
				} else {
189 1
					$type = \PDO::PARAM_STR;
190
				}
191
192 1
				$pdoStatement->bindValue(sprintf(':%s%s', $clause, $key + 1), $value, $type);
193 1
			}
194 4
		}
195
196 4
		$pdoStatement->execute();
197
198 4
		return new QueryResult($pdoStatement);
199
	}
200
201
	/**
202
	 * Returns the values array with bindings instead of values.
203
	 *
204
	 * @param array $values
205
	 * @return array the values array with bindings instead of values.
206
	 */
207 2
	private function replaceValuesWithBindings(array $values, $clause)
208
	{
209 2
		$result = [];
210
211 2
		foreach ($values as $key => $value) {
212 2
			$this->bindings[$clause][] = $value;
213
214 2
			$result[$key] = sprintf(':%s%s', $clause, count($this->bindings[$clause]));
215 2
		}
216
217 2
		return $result;
218
	}
219
}
220