class-clauses.php
2 years ago
class-database.php
2 years ago
class-escape.php
2 years ago
class-groupby.php
2 years ago
class-joins.php
2 years ago
class-orderby.php
2 years ago
class-query-builder.php
2 years ago
class-select.php
2 years ago
class-translate.php
2 years ago
class-where.php
2 years ago
index.php
2 years ago
class-where.php
367 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The where functions. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Database |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Database; |
| 12 | |
| 13 | /** |
| 14 | * Where class. |
| 15 | */ |
| 16 | trait Where { |
| 17 | |
| 18 | /** |
| 19 | * Create a where statement |
| 20 | * |
| 21 | * ->where('name', 'ladina') |
| 22 | * ->where('age', '>', 18) |
| 23 | * ->where('name', 'in', ['charles', 'john', 'jeffry']) |
| 24 | * |
| 25 | * @throws \Exception If $type is not 'AND', 'OR', 'WHERE'. |
| 26 | * |
| 27 | * @param mixed $column The SQL column. |
| 28 | * @param mixed $param1 Operator or value depending if $param2 isset. |
| 29 | * @param mixed $param2 The value if $param1 is an operator. |
| 30 | * @param string $type the where type ( AND, OR ). |
| 31 | * |
| 32 | * @return self The current query builder. |
| 33 | */ |
| 34 | public function where( $column, $param1 = null, $param2 = null, $type = 'AND' ) { |
| 35 | |
| 36 | $this->is_valid_type( $type ); |
| 37 | |
| 38 | $sub_type = is_null( $param1 ) ? $type : $param1; |
| 39 | if ( ! $this->has_sql_clause( 'where' ) ) { |
| 40 | $type = 'WHERE'; |
| 41 | } |
| 42 | |
| 43 | // When column is an array we assume to make a bulk and where. |
| 44 | if ( is_array( $column ) ) { |
| 45 | $this->bulk_where( $column, $type, $sub_type ); |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | $this->add_sql_clause( 'where', $this->generateWhere( $column, $param1, $param2, $type ) ); |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create an or where statement |
| 56 | * |
| 57 | * @param string $column The SQL column. |
| 58 | * @param mixed $param1 Operator or value depending if $param2 isset. |
| 59 | * @param mixed $param2 The value if $param1 is an operator. |
| 60 | * |
| 61 | * @return self The current query builder. |
| 62 | */ |
| 63 | public function orWhere( $column, $param1 = null, $param2 = null ) { // @codingStandardsIgnoreLine |
| 64 | return $this->where( $column, $param1, $param2, 'OR' ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a where in statement |
| 69 | * |
| 70 | * ->whereIn('id', [42, 38, 12]) |
| 71 | * |
| 72 | * @param string $column The SQL column. |
| 73 | * @param array $options Array of values for in statement. |
| 74 | * |
| 75 | * @return self The current query builder. |
| 76 | */ |
| 77 | public function whereIn( $column, $options ) { // @codingStandardsIgnoreLine |
| 78 | return $this->where( $column, 'IN', $options ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Creates a where in statement |
| 83 | * |
| 84 | * ->orWhereIn('id', [42, 38, 12]) |
| 85 | * |
| 86 | * @param string $column The SQL column. |
| 87 | * @param array $options Array of values for in statement. |
| 88 | * |
| 89 | * @return self The current query builder. |
| 90 | */ |
| 91 | public function orWhereIn( $column, $options ) { // @codingStandardsIgnoreLine |
| 92 | return $this->where( $column, 'IN', $options, 'OR' ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a where not in statement |
| 97 | * |
| 98 | * ->whereNotIn('id', [42, 38, 12]) |
| 99 | * |
| 100 | * @param string $column The SQL column. |
| 101 | * @param array $options Array of values for in statement. |
| 102 | * |
| 103 | * @return self The current query builder. |
| 104 | */ |
| 105 | public function whereNotIn( $column, $options ) { // @codingStandardsIgnoreLine |
| 106 | return $this->where( $column, 'NOT IN', $options ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Creates a where not in statement |
| 111 | * |
| 112 | * ->orWhereNotIn('id', [42, 38, 12]) |
| 113 | * |
| 114 | * @param string $column The SQL column. |
| 115 | * @param array $options Array of values for in statement. |
| 116 | * |
| 117 | * @return self The current query builder. |
| 118 | */ |
| 119 | public function orWhereNotIn( $column, $options ) { // @codingStandardsIgnoreLine |
| 120 | return $this->where( $column, 'NOT IN', $options, 'OR' ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a where between statement |
| 125 | * |
| 126 | * ->whereBetween('id', [10, 100]) |
| 127 | * |
| 128 | * @param string $column The SQL column. |
| 129 | * @param array $options Array of values for in statement. |
| 130 | * |
| 131 | * @return self The current query builder. |
| 132 | */ |
| 133 | public function whereBetween( $column, $options ) { // @codingStandardsIgnoreLine |
| 134 | return $this->where( $column, 'BETWEEN', $options ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Creates a where between statement |
| 139 | * |
| 140 | * ->orWhereBetween('id', [10, 100]) |
| 141 | * |
| 142 | * @param string $column The SQL column. |
| 143 | * @param array $options Array of values for in statement. |
| 144 | * |
| 145 | * @return self The current query builder. |
| 146 | */ |
| 147 | public function orWhereBetween( $column, $options ) { // @codingStandardsIgnoreLine |
| 148 | return $this->where( $column, 'BETWEEN', $options, 'OR' ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Creates a where not between statement |
| 153 | * |
| 154 | * ->whereNotBetween('id', [10, 100]) |
| 155 | * |
| 156 | * @param string $column The SQL column. |
| 157 | * @param array $options Array of values for in statement. |
| 158 | * |
| 159 | * @return self The current query builder. |
| 160 | */ |
| 161 | public function whereNotBetween( $column, $options ) { // @codingStandardsIgnoreLine |
| 162 | return $this->where( $column, 'NOT BETWEEN', $options ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Creates a where not between statement |
| 167 | * |
| 168 | * ->orWhereNotBetween('id', [10, 100]) |
| 169 | * |
| 170 | * @param string $column The SQL column. |
| 171 | * @param array $options Array of values for in statement. |
| 172 | * |
| 173 | * @return self The current query builder. |
| 174 | */ |
| 175 | public function orWhereNotBetween( $column, $options ) { // @codingStandardsIgnoreLine |
| 176 | return $this->where( $column, 'NOT BETWEEN', $options, 'OR' ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Creates a where like statement |
| 181 | * |
| 182 | * ->whereLike('id', 'value') |
| 183 | * |
| 184 | * @codeCoverageIgnore |
| 185 | * |
| 186 | * @param string $column The SQL column. |
| 187 | * @param string $value Value for like statement. |
| 188 | * @param string $start (Optional) The start of like query. |
| 189 | * @param string $end (Optional) The end of like query. |
| 190 | * |
| 191 | * @return self The current query builder. |
| 192 | */ |
| 193 | public function whereLike( $column, $value, $start = '%', $end = '%' ) { // @codingStandardsIgnoreLine |
| 194 | return $this->where( $column, 'LIKE', $this->esc_like( $value, $start, $end ) ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Creates a where like statement |
| 199 | * |
| 200 | * ->orWhereLike('id', 'value') |
| 201 | * |
| 202 | * @codeCoverageIgnore |
| 203 | * |
| 204 | * @param string $column The SQL column. |
| 205 | * @param string $value Value for like statement. |
| 206 | * @param string $start (Optional) The start of like query. |
| 207 | * @param string $end (Optional) The end of like query. |
| 208 | * |
| 209 | * @return self The current query builder. |
| 210 | */ |
| 211 | public function orWhereLike( $column, $value, $start = '%', $end = '%' ) { // @codingStandardsIgnoreLine |
| 212 | return $this->where( $column, 'LIKE', $this->esc_like( $value, $start, $end ), 'OR' ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Creates a where not like statement |
| 217 | * |
| 218 | * ->whereNotLike('id', 'value' ) |
| 219 | * |
| 220 | * @codeCoverageIgnore |
| 221 | * |
| 222 | * @param string $column The SQL column. |
| 223 | * @param mixed $value Value for like statement. |
| 224 | * @param string $start (Optional) The start of like query. |
| 225 | * @param string $end (Optional) The end of like query. |
| 226 | * |
| 227 | * @return self The current query builder. |
| 228 | */ |
| 229 | public function whereNotLike( $column, $value, $start = '%', $end = '%' ) { // @codingStandardsIgnoreLine |
| 230 | return $this->where( $column, 'NOT LIKE', $this->esc_like( $value, $start, $end ) ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Creates a where not like statement |
| 235 | * |
| 236 | * ->orWhereNotLike('id', 'value' ) |
| 237 | * |
| 238 | * @codeCoverageIgnore |
| 239 | * |
| 240 | * @param string $column The SQL column. |
| 241 | * @param mixed $value Value for like statement. |
| 242 | * @param string $start (Optional) The start of like query. |
| 243 | * @param string $end (Optional) The end of like query. |
| 244 | * |
| 245 | * @return self The current query builder. |
| 246 | */ |
| 247 | public function orWhereNotLike( $column, $value, $start = '%', $end = '%' ) { // @codingStandardsIgnoreLine |
| 248 | return $this->where( $column, 'NOT LIKE', $this->esc_like( $value, $start, $end ), 'OR' ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Creates a where is null statement |
| 253 | * |
| 254 | * ->whereNull( 'name' ) |
| 255 | * |
| 256 | * @param string $column The SQL column. |
| 257 | * |
| 258 | * @return self The current query builder. |
| 259 | */ |
| 260 | public function whereNull( $column ) { // @codingStandardsIgnoreLine |
| 261 | return $this->where( $column, 'IS', 'NULL' ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Creates a where is null statement |
| 266 | * |
| 267 | * ->orWhereNull( 'name' ) |
| 268 | * |
| 269 | * @param string $column The SQL column. |
| 270 | * |
| 271 | * @return self The current query builder. |
| 272 | */ |
| 273 | public function orWhereNull( $column ) { // @codingStandardsIgnoreLine |
| 274 | return $this->where( $column, 'IS', 'NULL', 'OR' ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Creates a where is not null statement |
| 279 | * |
| 280 | * ->whereNotNull( 'name' ) |
| 281 | * |
| 282 | * @param string $column The SQL column. |
| 283 | * |
| 284 | * @return self The current query builder. |
| 285 | */ |
| 286 | public function whereNotNull( $column ) { // @codingStandardsIgnoreLine |
| 287 | return $this->where( $column, 'IS NOT', 'NULL' ); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Creates a where is not null statement |
| 292 | * |
| 293 | * ->orWhereNotNull( 'name' ) |
| 294 | * |
| 295 | * @param string $column The SQL column. |
| 296 | * |
| 297 | * @return self The current query builder. |
| 298 | */ |
| 299 | public function orWhereNotNull( $column ) { // @codingStandardsIgnoreLine |
| 300 | return $this->where( $column, 'IS NOT', 'NULL', 'OR' ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Generate Where clause |
| 305 | * |
| 306 | * @param string $column The SQL column. |
| 307 | * @param mixed $param1 Operator or value depending if $param2 isset. |
| 308 | * @param mixed $param2 The value if $param1 is an operator. |
| 309 | * @param string $type the where type ( AND, or ). |
| 310 | * |
| 311 | * @return string |
| 312 | */ |
| 313 | protected function generateWhere( $column, $param1 = null, $param2 = null, $type = 'AND' ) { // @codingStandardsIgnoreLine |
| 314 | |
| 315 | // when param2 is null we replace param2 with param one as the |
| 316 | // value holder and make param1 to the = operator. |
| 317 | if ( is_null( $param2 ) ) { |
| 318 | $param2 = $param1; |
| 319 | $param1 = '='; |
| 320 | } |
| 321 | |
| 322 | // When param2 is an array we probably |
| 323 | // have an "in" or "between" statement which has no need for duplicates. |
| 324 | if ( is_array( $param2 ) ) { |
| 325 | $param2 = $this->esc_array( array_unique( $param2 ) ); |
| 326 | $param2 = in_array( $param1, array( 'BETWEEN', 'NOT BETWEEN' ), true ) ? join( ' AND ', $param2 ) : '(' . join( ', ', $param2 ) . ')'; |
| 327 | } elseif ( is_scalar( $param2 ) ) { |
| 328 | $param2 = $this->esc_value( $param2 ); |
| 329 | } |
| 330 | |
| 331 | return join( ' ', array( $type, $column, $param1, $param2 ) ); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Check if the where type is valid. |
| 336 | * |
| 337 | * @param string $type Value to check. |
| 338 | * |
| 339 | * @throws \Exception If not a valid type. |
| 340 | */ |
| 341 | private function is_valid_type( $type ) { |
| 342 | if ( ! in_array( $type, array( 'AND', 'OR', 'WHERE' ), true ) ) { |
| 343 | throw new \Exception( 'Invalid where type "' . $type . '"' ); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Create bulk where statement. |
| 349 | * |
| 350 | * @param array $where Array of statments. |
| 351 | * @param string $type Statement type. |
| 352 | * @param string $sub_type Statement sub-type. |
| 353 | */ |
| 354 | private function bulk_where( $where, $type, $sub_type ) { |
| 355 | $subquery = array(); |
| 356 | foreach ( $where as $value ) { |
| 357 | if ( ! isset( $value[2] ) ) { |
| 358 | $value[2] = $value[1]; |
| 359 | $value[1] = '='; |
| 360 | } |
| 361 | $subquery[] = $this->generateWhere( $value[0], $value[1], $value[2], empty( $subquery ) ? '' : $sub_type ); |
| 362 | } |
| 363 | |
| 364 | $this->add_sql_clause( 'where', $type . ' ( ' . trim( join( ' ', $subquery ) ) . ' )' ); |
| 365 | } |
| 366 | } |
| 367 |