| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Db_Queries; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Traits\With_View; |
| 7 |
use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 8 |
use Jet_Form_Builder\Exceptions\Handler_Exception; |
| 9 |
use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'WPINC' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* @method Query_Builder set_view( View_Base $view ) |
| 18 |
* |
| 19 |
* Class Query_Builder |
| 20 |
* @package Jet_Form_Builder\Db_Queries |
| 21 |
*/ |
| 22 |
class Query_Builder { |
| 23 |
|
| 24 |
use With_View; |
| 25 |
|
| 26 |
public $sql; |
| 27 |
public $select = ''; |
| 28 |
public $join = ''; |
| 29 |
public $where = ''; |
| 30 |
public $limit = ''; |
| 31 |
public $order_by = ''; |
| 32 |
|
| 33 |
public $debug = false; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array $columns |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public static function build_set( array $columns ): string { |
| 41 |
$pairs = array(); |
| 42 |
|
| 43 |
foreach ( $columns as $name => $value ) { |
| 44 |
if ( is_numeric( $name ) ) { |
| 45 |
$pairs[] = $value; |
| 46 |
} else { |
| 47 |
$pairs[] = sprintf( '%s = %s', $name, $value ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return 'SET ' . implode( ', ', $pairs ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return array[] |
| 56 |
* @throws Query_Builder_Exception |
| 57 |
*/ |
| 58 |
public function prepare_parts_callbacks(): array { |
| 59 |
return array( |
| 60 |
'select' => array( $this, 'prepare_select' ), |
| 61 |
'join' => array( $this->view(), 'get_prepared_join' ), |
| 62 |
'where' => array( $this, 'prepare_where' ), |
| 63 |
'order_by' => array( $this, 'prepare_order_by' ), |
| 64 |
'limit' => array( $this, 'prepare_limit' ), |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return $this |
| 70 |
* @throws Query_Builder_Exception |
| 71 |
*/ |
| 72 |
public function prepare_select(): Query_Builder { |
| 73 |
$this->select = 'SELECT'; |
| 74 |
$columns = array(); |
| 75 |
|
| 76 |
foreach ( $this->view()->select_columns() as $column ) { |
| 77 |
$columns[] = $this->view()->column( $column ); |
| 78 |
} |
| 79 |
|
| 80 |
$this->select .= " \r\n\t" . implode( ", \r\n\t", $columns ); |
| 81 |
$this->select .= "\r\nFROM `{$this->view()->table()}`"; |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* @return $this |
| 89 |
* @throws Query_Builder_Exception |
| 90 |
*/ |
| 91 |
public function prepare_where(): Query_Builder { |
| 92 |
$custom = $this->view()->custom_where(); |
| 93 |
|
| 94 |
if ( $custom ) { |
| 95 |
$this->where = $custom; |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
$this->where = ( new Query_Conditions_Builder() ) |
| 101 |
->set_condition( |
| 102 |
array( |
| 103 |
'type' => Query_Conditions_Builder::TYPE_EQUAL_STATIC, |
| 104 |
'values' => array( 1, 1 ), |
| 105 |
) |
| 106 |
) |
| 107 |
->set_view( $this->view() ) |
| 108 |
->result(); |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @throws Query_Builder_Exception |
| 115 |
*/ |
| 116 |
public function prepare_limit(): Query_Builder { |
| 117 |
if ( ! $this->view()->limit() ) { |
| 118 |
return $this; |
| 119 |
} |
| 120 |
$this->limit = 'LIMIT ' . implode( ', ', $this->view()->limit() ); |
| 121 |
|
| 122 |
return $this; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @return Query_Builder |
| 127 |
* @throws Query_Builder_Exception |
| 128 |
*/ |
| 129 |
public function prepare_order_by(): Query_Builder { |
| 130 |
if ( ! $this->view()->order_by() ) { |
| 131 |
return $this; |
| 132 |
} |
| 133 |
$columns = array(); |
| 134 |
|
| 135 |
foreach ( $this->view()->order_by() as $column ) { |
| 136 |
$name = $this->view()->column( $column ); |
| 137 |
$sort = $column['sort'] ?? ''; |
| 138 |
|
| 139 |
$columns[] = trim( "$name $sort" ); |
| 140 |
} |
| 141 |
|
| 142 |
$this->order_by = 'ORDER BY ' . " \r\n\t" . implode( ", \r\n\t", $columns ); |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @throws Query_Builder_Exception |
| 149 |
*/ |
| 150 |
public function maybe_prepare_parts() { |
| 151 |
foreach ( $this->prepare_parts_callbacks() as $part_name => $callback ) { |
| 152 |
if ( ! empty( $this->$part_name ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
call_user_func( $callback, $this ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @return array |
| 162 |
* @throws Query_Builder_Exception |
| 163 |
*/ |
| 164 |
public function get_parts(): array { |
| 165 |
$parts = array(); |
| 166 |
|
| 167 |
foreach ( $this->prepare_parts_callbacks() as $part_name => $callback ) { |
| 168 |
if ( empty( $this->$part_name ) ) { |
| 169 |
continue; |
| 170 |
} |
| 171 |
$parts[] = $this->$part_name; |
| 172 |
} |
| 173 |
|
| 174 |
return $parts; |
| 175 |
} |
| 176 |
|
| 177 |
public function debug(): Query_Builder { |
| 178 |
$this->debug = ! $this->debug; |
| 179 |
|
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
private function log( ...$something ) { |
| 184 |
if ( ! $this->debug ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
if ( 1 === count( $something ) ) { |
| 188 |
do_action( 'qm/debug', $something[0] ); |
| 189 |
|
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
do_action( 'qm/debug', $something ); |
| 194 |
} |
| 195 |
|
| 196 |
public function prepare_sql(): Query_Builder { |
| 197 |
if ( ! empty( $this->sql ) ) { |
| 198 |
return $this; |
| 199 |
} |
| 200 |
|
| 201 |
$this->maybe_prepare_parts(); |
| 202 |
|
| 203 |
$this->sql = implode( "\r\n", $this->get_parts() ) . ';'; |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* @return \QM_DB|\wpdb |
| 211 |
*/ |
| 212 |
public function db() { |
| 213 |
global $wpdb; |
| 214 |
|
| 215 |
return $wpdb; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
public function sql(): string { |
| 222 |
$this->prepare_sql(); |
| 223 |
|
| 224 |
$this->log( $this->sql ); |
| 225 |
|
| 226 |
return $this->sql; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @throws Query_Builder_Exception |
| 231 |
*/ |
| 232 |
public function query_all(): array { |
| 233 |
// phpcs:ignore WordPress.DB |
| 234 |
$rows = $this->db()->get_results( $this->sql(), ARRAY_A ); |
| 235 |
|
| 236 |
if ( empty( $rows ) ) { |
| 237 |
throw new Query_Builder_Exception( esc_html( "Empty {$this->view()->table()} rows" ) ); |
| 238 |
} |
| 239 |
|
| 240 |
$response = array(); |
| 241 |
|
| 242 |
foreach ( $rows as $row ) { |
| 243 |
$response[] = $this->view()->get_prepared_row( $row ); |
| 244 |
} |
| 245 |
|
| 246 |
return $response; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* @param string $format |
| 251 |
* |
| 252 |
* @return \Generator |
| 253 |
* @throws Query_Builder_Exception |
| 254 |
*/ |
| 255 |
public function generate_all( string $format = OBJECT ): \Generator { |
| 256 |
// phpcs:ignore WordPress.DB |
| 257 |
$this->db()->query( $this->sql() ); |
| 258 |
|
| 259 |
if ( empty( $this->db()->last_result ) ) { |
| 260 |
throw new Query_Builder_Exception( esc_html( "Empty {$this->view()->table()} rows" ) ); |
| 261 |
} |
| 262 |
|
| 263 |
foreach ( (array) $this->db()->last_result as $row ) { |
| 264 |
yield ( OBJECT === $format ? $row : (array) $row ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* @throws Query_Builder_Exception |
| 270 |
*/ |
| 271 |
public function query_one(): array { |
| 272 |
// phpcs:ignore WordPress.DB |
| 273 |
$row = $this->db()->get_row( $this->sql(), ARRAY_A ); |
| 274 |
|
| 275 |
return $this->view()->get_prepared_row( $row ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @param $column_offset |
| 280 |
* @param $row_offset |
| 281 |
* |
| 282 |
* @return array |
| 283 |
* @throws Query_Builder_Exception |
| 284 |
*/ |
| 285 |
public function query_var( $column_offset = 0, $row_offset = 0 ) { |
| 286 |
// phpcs:ignore WordPress.DB |
| 287 |
$row = $this->db()->get_var( $this->sql(), $column_offset, $row_offset ); |
| 288 |
|
| 289 |
return $this->view()->get_prepared_row( $row ); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* @param int $index |
| 294 |
* |
| 295 |
* @return array |
| 296 |
* @throws Query_Builder_Exception |
| 297 |
*/ |
| 298 |
public function query_col( $index = 0 ) { |
| 299 |
$values = $this->db()->get_col( $this->sql(), $index ); |
| 300 |
|
| 301 |
return $this->view()->get_prepared_values( $values ); |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|