State::getSetString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7.1
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2018 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query;
16
17
/**
18
 * Query builder state
19
 */
20
class State {
21
	// --------------------------------------------------------------------------
22
	// ! SQL Clause Strings
23
	// --------------------------------------------------------------------------
24
25
	/**
26
	 * Compiled 'select' clause
27
	 * @var string
28
	 */
29
	protected $selectString = '';
30
31
	/**
32
	 * Compiled 'from' clause
33
	 * @var string
34
	 */
35
	protected $fromString = '';
36
37
	/**
38
	 * Compiled arguments for insert / update
39
	 * @var string
40
	 */
41
	protected $setString = '';
42
43
	/**
44
	 * Order by clause
45
	 * @var string
46
	 */
47
	protected $orderString = '';
48
49
	/**
50
	 * Group by clause
51
	 * @var string
52
	 */
53
	protected $groupString = '';
54
55
	// --------------------------------------------------------------------------
56
	// ! SQL Clause Arrays
57
	// --------------------------------------------------------------------------
58
59
	/**
60
	 * Keys for insert/update statement
61
	 * @var array
62
	 */
63
	protected $setArrayKeys = [];
64
65
	/**
66
	 * Key/val pairs for order by clause
67
	 * @var array
68
	 */
69
	protected $orderArray = [];
70
71
	/**
72
	 * Key/val pairs for group by clause
73
	 * @var array
74
	 */
75
	protected $groupArray = [];
76
77
	// --------------------------------------------------------------------------
78
	// ! Other Class vars
79
	// --------------------------------------------------------------------------
80
81
	/**
82
	 * Values to apply to prepared statements
83
	 * @var array
84
	 */
85
	protected $values = [];
86
87
	/**
88
	 * Values to apply to where clauses in prepared statements
89
	 * @var array
90
	 */
91
	protected $whereValues = [];
92
93
	/**
94
	 * Value for limit string
95
	 * @var integer
96
	 */
97
	protected $limit;
98
99
	/**
100
	 * Value for offset in limit string
101
	 * @var string|false
102
	 */
103
	protected $offset = FALSE;
104
105
	/**
106
	 * Query component order mapping
107
	 * for complex select queries
108
	 *
109
	 * Format:
110
	 * [
111
	 *		'type' => 'where',
112
	 *		'conjunction' => ' AND ',
113
	 *		'string' => 'k=?'
114
	 * ]
115
	 *
116
	 * @var array
117
	 */
118
	protected $queryMap = [];
119
120
	/**
121
	 * Map for having clause
122
	 * @var array
123
	 */
124
	protected $havingMap = [];
125
126
	/**
127
	 * @param string $str
128
	 * @return State
129
	 */
130
	public function setSelectString(string $str): self
131
	{
132
		$this->selectString = $str;
133
		return $this;
134
	}
135
136
	/**
137
	 * @return string
138
	 */
139
	public function getSelectString(): string
140
	{
141
		return $this->selectString;
142
	}
143
144
	/**
145
	 * @param string $str
146
	 * @return State
147
	 */
148
	public function appendSelectString(string $str): self
149
	{
150
		$this->selectString .= $str;
151
		return $this;
152
	}
153
154
	/**
155
	 * @return string
156
	 */
157
	public function getFromString(): string
158
	{
159
		return $this->fromString;
160
	}
161
162
	/**
163
	 * @param string $fromString
164
	 * @return State
165
	 */
166
	public function setFromString(string $fromString): self
167
	{
168
		$this->fromString = $fromString;
169
		return $this;
170
	}
171
172
	/**
173
	 * @return string
174
	 */
175
	public function getSetString(): string
176
	{
177
		return $this->setString;
178
	}
179
180
	/**
181
	 * @param string $setString
182
	 * @return State
183
	 */
184
	public function setSetString(string $setString): self
185
	{
186
		$this->setString = $setString;
187
		return $this;
188
	}
189
190
	/**
191
	 * @return string
192
	 */
193
	public function getOrderString(): string
194
	{
195
		return $this->orderString;
196
	}
197
198
	/**
199
	 * @param string $orderString
200
	 * @return State
201
	 */
202
	public function setOrderString(string $orderString): self
203
	{
204
		$this->orderString = $orderString;
205
		return $this;
206
	}
207
208
	/**
209
	 * @return string
210
	 */
211
	public function getGroupString(): string
212
	{
213
		return $this->groupString;
214
	}
215
216
	/**
217
	 * @param string $groupString
218
	 * @return State
219
	 */
220
	public function setGroupString(string $groupString): self
221
	{
222
		$this->groupString = $groupString;
223
		return $this;
224
	}
225
226
	/**
227
	 * @return array
228
	 */
229
	public function getSetArrayKeys(): array
230
	{
231
		return $this->setArrayKeys;
232
	}
233
234
	/**
235
	 * @param array $setArrayKeys
236
	 * @return State
237
	 */
238
	public function appendSetArrayKeys(array $setArrayKeys): self
239
	{
240
		$this->setArrayKeys = array_merge($this->setArrayKeys, $setArrayKeys);
241
		return $this;
242
	}
243
244
	/**
245
	 * @param array $setArrayKeys
246
	 * @return State
247
	 */
248
	public function setSetArrayKeys(array $setArrayKeys): self
249
	{
250
		$this->setArrayKeys = $setArrayKeys;
251
		return $this;
252
	}
253
254
	/**
255
	 * @return array
256
	 */
257
	public function getOrderArray(): array
258
	{
259
		return $this->orderArray;
260
	}
261
262
	/**
263
	 * @param string $key
264
	 * @param mixed $orderArray
265
	 * @return State
266
	 */
267
	public function setOrderArray(string $key, $orderArray): self
268
	{
269
		$this->orderArray[$key] = $orderArray;
270
		return $this;
271
	}
272
273
	/**
274
	 * @return array
275
	 */
276
	public function getGroupArray(): array
277
	{
278
		return $this->groupArray;
279
	}
280
281
	/**
282
	 * @param array $groupArray
283
	 * @return State
284
	 */
285
	public function setGroupArray(array $groupArray): self
286
	{
287
		$this->groupArray = $groupArray;
288
		return $this;
289
	}
290
291
	/**
292
	 * @param string $groupArray
293
	 * @return State
294
	 */
295
	public function appendGroupArray(string $groupArray): self
296
	{
297
		$this->groupArray[] = $groupArray;
298
		return $this;
299
	}
300
301
	/**
302
	 * @return array
303
	 */
304
	public function getValues(): array
305
	{
306
		return $this->values;
307
	}
308
309
	/**
310
	 * @param array $values
311
	 * @return State
312
	 */
313
	public function appendValues(array $values): self
314
	{
315
		$this->values = array_merge($this->values, $values);
316
		return $this;
317
	}
318
319
	/**
320
	 * @return array
321
	 */
322
	public function getWhereValues(): array
323
	{
324
		return $this->whereValues;
325
	}
326
327
	/**
328
	 * @param mixed $val
329
	 * @return State
330
	 */
331
	public function appendWhereValues($val): self
332
	{
333
		if (\is_array($val))
334
		{
335
			foreach($val as $v)
336
			{
337
				$this->whereValues[] = $v;
338
			}
339
340
			return $this;
341
		}
342
343
		$this->whereValues[] = $val;
344
		return $this;
345
	}
346
347
	/**
348
	 * @return int
349
	 */
350
	public function getLimit(): ?int
351
	{
352
		return $this->limit;
353
	}
354
355
	/**
356
	 * @param int $limit
357
	 * @return State
358
	 */
359
	public function setLimit(int $limit): self
360
	{
361
		$this->limit = $limit;
362
		return $this;
363
	}
364
365
	/**
366
	 * @return string|false
367
	 */
368
	public function getOffset()
369
	{
370
		return $this->offset;
371
	}
372
373
	/**
374
	 * @param string|false $offset
375
	 * @return State
376
	 */
377
	public function setOffset($offset): self
378
	{
379
		$this->offset = $offset;
380
		return $this;
381
	}
382
383
	/**
384
	 * @return array
385
	 */
386
	public function getQueryMap(): array
387
	{
388
		return $this->queryMap;
389
	}
390
391
	/**
392
	 * Add an additional set of mapping pairs to a internal map
393
	 *
394
	 * @param string $conjunction
395
	 * @param string $string
396
	 * @param string $type
397
	 * @return State
398
	 */
399
	public function appendMap(string $conjunction = '', string $string = '', string $type = ''): self
400
	{
401
		$this->queryMap[] = [
402
			'type' => $type,
403
			'conjunction' => $conjunction,
404
			'string' => $string
405
		];
406
		return $this;
407
	}
408
409
	/**
410
	 * @return array
411
	 */
412
	public function getHavingMap(): array
413
	{
414
		return $this->havingMap;
415
	}
416
417
	/**
418
	 * @param array $item
419
	 * @return State
420
	 */
421
	public function appendHavingMap(array $item): self
422
	{
423
		$this->havingMap[] = $item;
424
		return $this;
425
	}
426
}
427