seo-by-rank-math
/
vendor
/
mythemeshop
/
wordpress-helpers
/
src
/
database
/
class-query-builder.php
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-query-builder.php
289 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Query Builder. |
| 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 | * Query_Builder class. |
| 15 | */ |
| 16 | class Query_Builder { |
| 17 | |
| 18 | use Escape; |
| 19 | use Select; |
| 20 | use Where; |
| 21 | use Joins; |
| 22 | use GroupBy; |
| 23 | use OrderBy; |
| 24 | use Clauses; |
| 25 | use Translate; |
| 26 | |
| 27 | /** |
| 28 | * Table name. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | public $table = ''; |
| 33 | |
| 34 | /** |
| 35 | * Save last query. |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $last_query = ''; |
| 40 | |
| 41 | /** |
| 42 | * Make a distinct selection |
| 43 | * |
| 44 | * @var bool |
| 45 | */ |
| 46 | protected $distinct = false; |
| 47 | |
| 48 | /** |
| 49 | * Make SQL_CALC_FOUND_ROWS in selection |
| 50 | * |
| 51 | * @var bool |
| 52 | */ |
| 53 | protected $found_rows = false; |
| 54 | |
| 55 | /** |
| 56 | * Data store context used to pass to filters. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | protected $context; |
| 61 | |
| 62 | /** |
| 63 | * Constructor |
| 64 | * |
| 65 | * @param string $table The table name. |
| 66 | * @param string $context Optional context passed to filters. Default empty string. |
| 67 | */ |
| 68 | public function __construct( $table, $context = '' ) { |
| 69 | $this->table = $table; |
| 70 | $this->context = $context; |
| 71 | $this->reset(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Translate the given query object and return the results |
| 76 | * |
| 77 | * @param string $output (Optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
| 78 | * |
| 79 | * @return mixed |
| 80 | */ |
| 81 | public function get( $output = \OBJECT ) { |
| 82 | global $wpdb; |
| 83 | |
| 84 | $this->last_query = $this->translateSelect(); |
| 85 | $this->reset(); |
| 86 | |
| 87 | return $wpdb->get_results( $this->last_query, $output ); // phpcs:ignore |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Translate the given query object and return the results |
| 92 | * |
| 93 | * @param string $output (Optional) Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
| 94 | * |
| 95 | * @return mixed |
| 96 | */ |
| 97 | public function one( $output = \OBJECT ) { |
| 98 | global $wpdb; |
| 99 | |
| 100 | $this->limit( 1 ); |
| 101 | $this->last_query = $this->translateSelect(); |
| 102 | $this->reset(); |
| 103 | |
| 104 | return $wpdb->get_row( $this->last_query, $output ); // phpcs:ignore |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Translate the given query object and return one variable from the database |
| 109 | * |
| 110 | * @return mixed |
| 111 | */ |
| 112 | public function getVar() { // @codingStandardsIgnoreLine |
| 113 | $row = $this->one( \ARRAY_A ); |
| 114 | |
| 115 | return is_null( $row ) ? false : current( $row ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Insert a row into a table |
| 120 | * |
| 121 | * @codeCoverageIgnore |
| 122 | * @see wpdb::insert() |
| 123 | * |
| 124 | * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
| 125 | * @param array $format (Optional) An array of formats to be mapped to each of the value in $data. |
| 126 | * |
| 127 | * @return mixed |
| 128 | */ |
| 129 | public function insert( $data, $format = null ) { |
| 130 | global $wpdb; |
| 131 | |
| 132 | $wpdb->insert( $this->table, $data, $format ); |
| 133 | |
| 134 | return $wpdb->insert_id; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Update a row into a table |
| 139 | * |
| 140 | * @codeCoverageIgnore |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | public function update() { |
| 145 | |
| 146 | $query = $this->translateUpdate(); |
| 147 | $this->reset(); |
| 148 | |
| 149 | return $this->query( $query ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Delete data from table |
| 154 | * |
| 155 | * @codeCoverageIgnore |
| 156 | * |
| 157 | * @return mixed |
| 158 | */ |
| 159 | public function delete() { |
| 160 | |
| 161 | $query = $this->translateDelete(); |
| 162 | $this->reset(); |
| 163 | |
| 164 | return $this->query( $query ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Truncate table. |
| 169 | * |
| 170 | * @codeCoverageIgnore |
| 171 | * |
| 172 | * @return mixed |
| 173 | */ |
| 174 | public function truncate() { |
| 175 | return $this->query( "truncate table {$this->table};" ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get found rows. |
| 180 | * |
| 181 | * @return int |
| 182 | */ |
| 183 | public function get_found_rows() { |
| 184 | global $wpdb; |
| 185 | |
| 186 | return $wpdb->get_var( 'SELECT FOUND_ROWS();' ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Perform a MySQL database query, using current database connection. |
| 191 | * |
| 192 | * @codeCoverageIgnore |
| 193 | * |
| 194 | * @see wpdb::query |
| 195 | * |
| 196 | * @param string $query Database query. |
| 197 | * |
| 198 | * @return int|false Number of rows affected|selected or false on error. |
| 199 | */ |
| 200 | public function query( $query ) { |
| 201 | global $wpdb; |
| 202 | $this->last_query = $query; |
| 203 | |
| 204 | return $wpdb->query( $query ); // phpcs:ignore |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Set the limit clause. |
| 209 | * |
| 210 | * @param int $limit Limit size. |
| 211 | * @param int $offset Offeset. |
| 212 | * |
| 213 | * @return self The current query builder. |
| 214 | */ |
| 215 | public function limit( $limit, $offset = 0 ) { |
| 216 | global $wpdb; |
| 217 | $limit = \absint( $limit ); |
| 218 | $offset = \absint( $offset ); |
| 219 | |
| 220 | $this->clear_sql_clause( 'limit' ); |
| 221 | $this->add_sql_clause( 'limit', $wpdb->prepare( 'LIMIT %d, %d', $offset, $limit ) ); |
| 222 | |
| 223 | return $this; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Create an query limit based on a page and a page size |
| 228 | * |
| 229 | * @param int $page Page number. |
| 230 | * @param int $size Page size. |
| 231 | * |
| 232 | * @return self The current query builder. |
| 233 | */ |
| 234 | public function page( $page, $size = 25 ) { |
| 235 | $size = \absint( $size ); |
| 236 | $offset = $size * \absint( $page ); |
| 237 | |
| 238 | $this->limit( $size, $offset ); |
| 239 | |
| 240 | return $this; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Set values for insert/update |
| 245 | * |
| 246 | * @param string|array $name Key of pair. |
| 247 | * @param string|array $value Value of pair. |
| 248 | * |
| 249 | * @return self The current query builder. |
| 250 | */ |
| 251 | public function set( $name, $value = null ) { |
| 252 | if ( is_array( $name ) ) { |
| 253 | $this->sql_clauses['values'] = $this->sql_clauses['values'] + $name; |
| 254 | |
| 255 | return $this; |
| 256 | } |
| 257 | |
| 258 | $this->sql_clauses['values'][ $name ] = $value; |
| 259 | |
| 260 | return $this; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Reset all vaiables. |
| 265 | * |
| 266 | * @return self The current query builder. |
| 267 | */ |
| 268 | private function reset() { |
| 269 | $this->distinct = false; |
| 270 | $this->found_rows = false; |
| 271 | $this->sql_clauses = array( |
| 272 | 'select' => array(), |
| 273 | 'from' => array(), |
| 274 | 'left_join' => array(), |
| 275 | 'join' => array(), |
| 276 | 'right_join' => array(), |
| 277 | 'where' => array(), |
| 278 | 'where_time' => array(), |
| 279 | 'group_by' => array(), |
| 280 | 'having' => array(), |
| 281 | 'limit' => array(), |
| 282 | 'order_by' => array(), |
| 283 | 'values' => array(), |
| 284 | ); |
| 285 | |
| 286 | return $this; |
| 287 | } |
| 288 | } |
| 289 |