Passed
Pull Request — master (#24)
by Wanderson
06:44
created

Sql::setValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Win\Repositories;
4
5
/**
6
 * Cria comandos SQL
7
 */
8
class Sql
9
{
10
	public ?array $columns = null;
11
12
	private string $table;
13
	private array $values;
14
	private array $join;
15
	private array $joinValues;
16
	private array $where;
17
	private array $whereValues;
18
	private array $orderBy;
19
	private string $limit;
20
21
	/**
22
	 * Prepara a query
23
	 * @param string $table
24
	 */
25
	public function __construct(&$table)
26
	{
27
		$this->table = &$table;
28
		$this->values = [];
29
		$this->join = [];
30
		$this->joinValues = [];
31
		$this->where = [];
32
		$this->whereValues = [];
33
		$this->orderBy = [];
34
		$this->limit = '';
35
	}
36
37
	/** @param mixed[] */
38
	public function setValues($values)
39
	{
40
		return $this->values = $values;
41
	}
42
43
	/** @return mixed[] */
44
	public function values()
45
	{
46
		return array_merge($this->values, $this->joinValues, $this->whereValues);
47
	}
48
49
	/**
50
	 * SELECT * FROM ...
51
	 * @return string
52
	 */
53
	public function select()
54
	{
55
		return 'SELECT ' . implode(', ', $this->columns ?? ['*'])
56
			. ' FROM ' . $this->table
57
			. $this->join()
58
			. $this->where()
59
			. $this->orderBy()
60
			. $this->limit();
61
	}
62
63
	/**
64
	 * SELECT COUNT(*) FROM
65
	 * @return string
66
	 */
67
	public function selectCount()
68
	{
69
		return 'SELECT COUNT(*) FROM ' . $this->table
70
			. $this->join()
71
			. $this->where();
72
	}
73
74
	/**
75
	 * INSERT INTO ... VALUES
76
	 * @return string
77
	 */
78
	public function insert()
79
	{
80
		$params = str_split(str_repeat('?', count($this->values)));
81
		return 'INSERT INTO ' . $this->table
82
			. ' (' . implode(',', array_keys($this->values)) . ')'
83
			. ' VALUES (' . implode(', ', $params) . ')';
84
	}
85
86
	/**
87
	 * UPDATE ... SET
88
	 * @return string
89
	 */
90
	public function update()
91
	{
92
		$keys = array_keys($this->values);
93
		$columns = array_map(fn ($column) => $column . ' = ?', $keys);
94
95
		return 'UPDATE ' . $this->table
96
			. ' SET ' . implode(', ', $columns)
97
			. $this->where()
98
			. $this->limit();
99
	}
100
101
	/**
102
	 * DELETE FROM ...
103
	 * @return string
104
	 */
105
	public function delete()
106
	{
107
		return 'DELETE FROM ' . $this->table
108
			. $this->where()
109
			. $this->limit();
110
	}
111
112
	/**
113
	 * WHERE ...
114
	 * @param string $comparator
115
	 * @param mixed $values
116
	 */
117
	public function addWhere($comparator, $values)
118
	{
119
		$hasBindParams = preg_match('/[:\?]/', $comparator);
120
		if ($values && !$hasBindParams) {
121
			$comparator .= ' = ?';
122
		}
123
		$this->where[] = '(' . $comparator . ')';
124
		$this->whereValues = array_merge($this->whereValues, $values);
125
	}
126
127
	/**
128
	 * JOIN, LEFT JOIN ...
129
	 * @param string $join
130
	 * @param array $values
131
	 */
132
	public function addJoin($join, $values)
133
	{
134
		$this->join[] = $join;
135
		$this->joinValues = array_merge($this->joinValues, $values);
136
	}
137
138
	/**
139
	 * Define o limit
140
	 * @param int $offset
141
	 * @param int $limit
142
	 */
143
	public function setLimit($offset, $limit)
144
	{
145
		$this->limit = $offset . ',' . $limit;
146
	}
147
148
	private function join()
149
	{
150
		if (empty($this->join)) {
151
			return '';
152
		}
153
154
		return ' ' . implode(' ', $this->join);
155
	}
156
157
	/**
158
	 * Retorna o SQL
159
	 * @return string
160
	 */
161
	private function where()
162
	{
163
		if (empty($this->where)) {
164
			return '';
165
		}
166
167
		return ' WHERE ' . implode(' AND ', $this->where);
168
	}
169
170
	/**
171
	 * Define a ordenação principal
172
	 * @param string $orderBy
173
	 */
174
	public function setOrderBy($orderBy)
175
	{
176
		$this->orderBy = [$orderBy];
177
	}
178
179
	/**
180
	 * Adiciona uma ordenação
181
	 * @param string $orderBy
182
	 * @param int $priority
183
	 */
184
	public function addOrderBy($orderBy, $priority = 0)
185
	{
186
		$this->orderBy[$priority] = $orderBy;
187
	}
188
189
	/**
190
	 * LIMIT ...
191
	 * @return string
192
	 */
193
	private function limit()
194
	{
195
		if (empty($this->limit)) {
196
			return '';
197
		}
198
199
		return ' LIMIT ' . $this->limit;
200
	}
201
202
203
	/**
204
	 * ORDER BY...
205
	 * @return string
206
	 */
207
	private function orderBy()
208
	{
209
		if (empty($this->orderBy)) {
210
			return '';
211
		}
212
213
		ksort($this->orderBy);
214
		return ' ORDER BY ' . implode(', ', $this->orderBy);
215
	}
216
}
217