-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.php
More file actions
205 lines (179 loc) · 7.27 KB
/
Query.php
File metadata and controls
205 lines (179 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\QueryBuilders;
use PDO;
/**
* Defines common functionality for query classes
*/
abstract class Query
{
/** @var string The name of the table we're querying */
protected $tableName = '';
/** @var string The alias of the table we're querying */
protected $tableAlias = '';
/** @var array The list of bound parameters */
protected $parameters = [];
/**
* True if we're using unnamed placeholders instead of named placeholders
* False if we're using named placeholders instead of unnamed placeholders
* Null if we haven't added any placeholders, and, therefore, don't know yet
*
* @var bool|null
*/
protected $usingUnnamedPlaceholders = null;
/**
* Gets the SQL statement as a string
*
* @return string The SQL statement
*/
abstract public function getSql() : string;
/**
* Adds a named placeholder's value
* Note that you cannot use a mix of named and unnamed placeholders in a query
*
* @param string $placeholderName The name of the placeholder (what comes after the ":")
* @param mixed $value The value of the placeholder
* @param int $dataType The PDO constant that indicates the type of data the value represents
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders
*/
public function addNamedPlaceholderValue(string $placeholderName, $value, int $dataType = PDO::PARAM_STR) : self
{
if ($this->usingUnnamedPlaceholders === true) {
throw new InvalidQueryException('Cannot mix unnamed placeholders with named placeholders');
}
$this->usingUnnamedPlaceholders = false;
$this->parameters[$placeholderName] = [$value, $dataType];
return $this;
}
/**
* Adds named placeholders' values
* Note that you cannot use a mix of named and unnamed placeholders in a query
*
* @param array $placeholderNamesToValues The mapping of placeholder names to their respective values
* Optionally, the names can map to an array whose first item is the value and whose second value is the
* PDO constant indicating the type of data the value represents
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders or
* if the value is an array that doesn't contain the correct number of items
*/
public function addNamedPlaceholderValues(array $placeholderNamesToValues) : self
{
foreach ($placeholderNamesToValues as $placeholderName => $value) {
if (is_array($value)) {
if (count($value) !== 2) {
throw new InvalidQueryException('Incorrect number of items in value array');
}
$this->addNamedPlaceholderValue($placeholderName, $value[0], $value[1]);
} else {
$this->addNamedPlaceholderValue($placeholderName, $value);
}
}
return $this;
}
/**
* Adds an unnamed placeholder's value
* Note that you cannot use a mix of named and unnamed placeholders in a query
*
* @param mixed $value
* @param int $dataType The PDO constant that indicates the type of data the value represents
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders
*/
public function addUnnamedPlaceholderValue($value, int $dataType = PDO::PARAM_STR) : self
{
if ($this->usingUnnamedPlaceholders === false) {
throw new InvalidQueryException('Cannot mix unnamed placeholders with named placeholders');
}
$this->usingUnnamedPlaceholders = true;
$this->parameters[] = [$value, $dataType];
return $this;
}
/**
* Adds multiple unnamed placeholders' values
* Note that you cannot use a mix of named and unnamed placeholders in a query
*
* @param array $placeholderValues The list of placeholder values
* Optionally, each value can be contained in an array whose first item is the value and whose second value is
* the PDO constant indicating the type of data the value represents
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders or
* if the value is an array that doesn't contain the correct number of items
*/
public function addUnnamedPlaceholderValues(array $placeholderValues) : self
{
foreach ($placeholderValues as $value) {
if ($value instanceof Expression) {
$this->addUnnamedPlaceholderValues($value->getParameters());
continue;
}
if (is_array($value)) {
if (count($value) !== 2) {
throw new InvalidQueryException('Incorrect number of items in value array');
}
$this->addUnnamedPlaceholderValue($value[0], $value[1]);
} else {
$this->addUnnamedPlaceholderValue($value);
}
}
return $this;
}
/**
* Gets the bound query parameters
*
* @return array The array of bound query parameters
*/
public function getParameters() : array
{
return $this->parameters;
}
/**
* Removes a named placeholder from the query
*
* @param string $placeholderName The name of the placeholder to remove
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders
*/
public function removeNamedPlaceHolder(string $placeholderName) : self
{
if ($this->usingUnnamedPlaceholders === true) {
throw new InvalidQueryException('Cannot mix unnamed placeholders with named placeholders');
}
unset($this->parameters[$placeholderName]);
return $this;
}
/**
* Removes an unnamed placeholder from the query
*
* @param int $placeholderIndex The index of the placeholder in the parameters to remove
* @return self For method chaining
* @throws InvalidQueryException Thrown if the user mixed unnamed placeholders with named placeholders
*/
public function removeUnnamedPlaceHolder(int $placeholderIndex) : self
{
if ($this->usingUnnamedPlaceholders === false) {
throw new InvalidQueryException('Cannot mix unnamed placeholders with named placeholders');
}
unset($this->parameters[$placeholderIndex]);
// Re-index the array
$this->parameters = array_values($this->parameters);
return $this;
}
/**
* Sets the table we're querying
*
* @param string $tableName The name of the table we're querying
* @param string $tableAlias The table alias
*/
protected function setTable(string $tableName, string $tableAlias = '')
{
$this->tableName = $tableName;
$this->tableAlias = $tableAlias;
}
}