constraints
2 years ago
exceptions
2 years ago
models
2 years ago
traits
2 years ago
views
2 years ago
base-db-constraint.php
2 years ago
base-db-model.php
2 years ago
execution-builder.php
2 years ago
query-builder.php
2 years ago
query-cache-builder.php
2 years ago
query-conditions-builder.php
2 years ago
execution-builder.php
355 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Db_Queries; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 7 | use Jet_Form_Builder\Db_Queries\Exceptions\Skip_Exception; |
| 8 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 9 | use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 10 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 11 | |
| 12 | // If this file is called directly, abort. |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * @method static Execution_Builder instance() |
| 19 | * |
| 20 | * Class Execution_Builder |
| 21 | * @package Jet_Form_Builder\Db_Queries |
| 22 | */ |
| 23 | class Execution_Builder { |
| 24 | |
| 25 | public $existed_tables = array(); |
| 26 | |
| 27 | use Instance_Trait; |
| 28 | |
| 29 | /** |
| 30 | * @param Base_Db_Model $model |
| 31 | * |
| 32 | * @return Execution_Builder |
| 33 | */ |
| 34 | public function create( Base_Db_Model $model ): Execution_Builder { |
| 35 | global $wpdb; |
| 36 | $model->before_create(); |
| 37 | |
| 38 | $sql = $this->create_table_schema( $model ); |
| 39 | |
| 40 | // phpcs:ignore WordPress.DB |
| 41 | $wpdb->query( $sql ); |
| 42 | |
| 43 | $model->after_create(); |
| 44 | $this->add_foreign_relations( $model ); |
| 45 | |
| 46 | return $this->save_to_existed( $model ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param Base_Db_Model $model |
| 51 | * |
| 52 | * @return Execution_Builder |
| 53 | */ |
| 54 | public function create_foreign_tables( Base_Db_Model $model ): Execution_Builder { |
| 55 | foreach ( $model->foreign_relations() as $constraint ) { |
| 56 | // Fool protection |
| 57 | if ( get_class( $constraint->get_model() ) === get_class( $model ) ) { |
| 58 | _doing_it_wrong( |
| 59 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 60 | get_class( $model ) . '::foreign_relations', |
| 61 | 'You have a logical error. A model cannot be dependent on itself.', |
| 62 | '2.0.0' |
| 63 | ); |
| 64 | continue; |
| 65 | } |
| 66 | $constraint->get_model()->create(); |
| 67 | } |
| 68 | |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * @param Base_Db_Model $model |
| 75 | * @param array $columns |
| 76 | * |
| 77 | * @param null $format |
| 78 | * |
| 79 | * @return int |
| 80 | * @throws Sql_Exception |
| 81 | */ |
| 82 | public function insert( Base_Db_Model $model, $columns = array(), $format = null ): int { |
| 83 | global $wpdb; |
| 84 | |
| 85 | $model->before_insert(); |
| 86 | |
| 87 | $insert_columns = array_merge( $model->get_defaults(), $columns ); |
| 88 | |
| 89 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 90 | $wpdb->insert( $model::table(), $insert_columns, $format ); |
| 91 | |
| 92 | if ( $wpdb->rows_affected ) { |
| 93 | $model->after_insert( $insert_columns ); |
| 94 | |
| 95 | return $wpdb->insert_id; |
| 96 | } |
| 97 | |
| 98 | if ( 0 === strpos( $model::table_name(), 'records' ) ) { |
| 99 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions |
| 100 | error_log( |
| 101 | sprintf( |
| 102 | '[jetformbuilder]: %s', |
| 103 | $wpdb->last_error |
| 104 | ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | $prefix = $model::DB_TABLE_PREFIX; |
| 109 | $table = $model::table_name(); |
| 110 | |
| 111 | throw new Sql_Exception( |
| 112 | esc_html( |
| 113 | 'Something went wrong on insert into: ' . $prefix . $table |
| 114 | ), |
| 115 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 116 | $wpdb->last_error, |
| 117 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 118 | $insert_columns |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param Base_Db_Model $model |
| 124 | * @param array $columns |
| 125 | * @param $where |
| 126 | * @param $format |
| 127 | * @param $where_format |
| 128 | * |
| 129 | * @return int |
| 130 | * @throws Sql_Exception |
| 131 | */ |
| 132 | public function update( Base_Db_Model $model, $columns, $where, $format = null, $where_format = null ): int { |
| 133 | global $wpdb; |
| 134 | |
| 135 | $model->before_update(); |
| 136 | |
| 137 | $update_columns = $model->update_columns( $columns ); |
| 138 | |
| 139 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 140 | $wpdb->update( $model->table(), $update_columns, $where, $format, $where_format ); |
| 141 | |
| 142 | if ( ! $wpdb->rows_affected ) { |
| 143 | throw new Sql_Exception( |
| 144 | esc_html( "Something went wrong on update rows in: {$model::table()}" ), |
| 145 | $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 146 | $columns, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 147 | $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | return $wpdb->rows_affected; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @param Base_Db_Model $model |
| 156 | * @param $where |
| 157 | * @param $where_format |
| 158 | * |
| 159 | * @return int |
| 160 | * @throws Sql_Exception |
| 161 | */ |
| 162 | public function delete( Base_Db_Model $model, $where, $where_format ): int { |
| 163 | global $wpdb; |
| 164 | |
| 165 | $model->before_delete(); |
| 166 | |
| 167 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 168 | $result = (int) $wpdb->delete( $model->table(), $where, $where_format ); |
| 169 | |
| 170 | if ( ! $result ) { |
| 171 | throw new Sql_Exception( |
| 172 | esc_html( "Something went wrong on delete rows in: {$model->table()}" ), |
| 173 | $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 174 | $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | return $result; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @param View_Base $view |
| 183 | * |
| 184 | * @return int |
| 185 | * @throws Query_Builder_Exception |
| 186 | */ |
| 187 | public function view_delete( View_Base $view ): int { |
| 188 | global $wpdb; |
| 189 | |
| 190 | $where = ( new Query_Conditions_Builder() ) |
| 191 | ->set_view( $view ) |
| 192 | ->result(); |
| 193 | |
| 194 | // phpcs:ignore WordPress.DB |
| 195 | $wpdb->query( "DELETE FROM {$view->table()} {$where}" ); |
| 196 | |
| 197 | if ( ! $wpdb->rows_affected ) { |
| 198 | throw new Query_Builder_Exception( |
| 199 | esc_html( "Something went wrong on delete rows in: {$view->table()}" ), |
| 200 | $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 201 | $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | return $wpdb->rows_affected; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param array $columns |
| 210 | * @param View_Base $view |
| 211 | * |
| 212 | * @return int |
| 213 | * @throws Sql_Exception |
| 214 | */ |
| 215 | public function view_update( array $columns, View_Base $view ): int { |
| 216 | global $wpdb; |
| 217 | |
| 218 | $where = ( new Query_Conditions_Builder() ) |
| 219 | ->set_view( $view ) |
| 220 | ->result(); |
| 221 | |
| 222 | $set = $view->build_set( $columns ); |
| 223 | $query = "UPDATE `{$view->table()}` {$set} {$where};"; |
| 224 | |
| 225 | // phpcs:ignore WordPress.DB |
| 226 | (int) $wpdb->query( $query ); |
| 227 | |
| 228 | if ( ! $wpdb->rows_affected ) { |
| 229 | throw new Sql_Exception( |
| 230 | esc_html( "Something went wrong on update rows in: {$view->table()}" ), |
| 231 | $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 232 | $query // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | return $wpdb->rows_affected; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @param Base_Db_Model $model |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | |
| 245 | public function create_table_schema( Base_Db_Model $model ) { |
| 246 | $schema_keys = $model->schema_keys(); |
| 247 | $table = $model->table(); |
| 248 | $columns = $model->schema(); |
| 249 | $charset_collate = $model->schema_charset_collate(); |
| 250 | $engine = apply_filters( 'jet-form-builder/table-engine', $model->schema_engine(), $model ); |
| 251 | |
| 252 | if ( ! empty( $engine ) ) { |
| 253 | $engine = "ENGINE={$engine}"; |
| 254 | } |
| 255 | |
| 256 | $ready_columns = ''; |
| 257 | foreach ( $columns as $column => $desc ) { |
| 258 | $ready_columns .= $column . ' ' . $desc . ", \n"; |
| 259 | } |
| 260 | |
| 261 | $keys = array(); |
| 262 | foreach ( $schema_keys as $column_name => $key ) { |
| 263 | $keys[] = "$key ($column_name)"; |
| 264 | } |
| 265 | $ready_keys = implode( ", \n", $keys ); |
| 266 | |
| 267 | return "CREATE TABLE $table ( |
| 268 | $ready_columns |
| 269 | $ready_keys |
| 270 | ) {$engine} {$charset_collate};"; |
| 271 | } |
| 272 | |
| 273 | protected function add_foreign_relations( Base_Db_Model $model ) { |
| 274 | global $wpdb; |
| 275 | |
| 276 | foreach ( $model->foreign_relations() as $constraint ) { |
| 277 | $constraint->set_foreign_table( $model::table_name() ); |
| 278 | try { |
| 279 | $constraint->before_create(); |
| 280 | } catch ( Skip_Exception $exception ) { |
| 281 | continue; |
| 282 | } |
| 283 | // phpcs:ignore WordPress.DB |
| 284 | $wpdb->query( "ALTER TABLE `{$model::table()}` ADD {$constraint->build()}" ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | public function is_exist( Base_Db_Model $model ): bool { |
| 289 | global $wpdb; |
| 290 | |
| 291 | $table = $model::table(); |
| 292 | |
| 293 | if ( ! isset( $this->existed_tables[ $table ] ) ) { |
| 294 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 295 | $find = $wpdb->get_var( |
| 296 | $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) |
| 297 | ); |
| 298 | |
| 299 | $this->existed_tables[ $table ] = ( $table === $find ); |
| 300 | } |
| 301 | |
| 302 | return $this->existed_tables[ $table ]; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @param Base_Db_Model $model |
| 307 | * |
| 308 | * @return Execution_Builder |
| 309 | */ |
| 310 | public function safe_create( Base_Db_Model $model ): Execution_Builder { |
| 311 | if ( $this->is_exist( $model ) ) { |
| 312 | return $this; |
| 313 | } |
| 314 | |
| 315 | return $this->create( $model ); |
| 316 | } |
| 317 | |
| 318 | public function save_to_existed( Base_Db_Model $model ): Execution_Builder { |
| 319 | $this->existed_tables[ $model->table() ] = true; |
| 320 | |
| 321 | return $this; |
| 322 | } |
| 323 | |
| 324 | public function transaction_start() { |
| 325 | global $wpdb; |
| 326 | |
| 327 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 328 | return $wpdb->query( 'START TRANSACTION' ); |
| 329 | } |
| 330 | |
| 331 | public function transaction_commit() { |
| 332 | global $wpdb; |
| 333 | |
| 334 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 335 | return $wpdb->query( 'COMMIT' ); |
| 336 | } |
| 337 | |
| 338 | public function transaction_rollback() { |
| 339 | global $wpdb; |
| 340 | |
| 341 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 342 | return $wpdb->query( 'ROLLBACK' ); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | public function delta( $sql ) { |
| 347 | if ( ! function_exists( 'dbDelta' ) ) { |
| 348 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 349 | } |
| 350 | |
| 351 | dbDelta( $sql ); |
| 352 | } |
| 353 | |
| 354 | } |
| 355 |