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
335 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->insert_id ) { |
| 93 | throw new Sql_Exception( |
| 94 | esc_html( "Something went wrong on insert into: {$model::table()}" ), |
| 95 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 96 | $insert_columns |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $model->after_insert( $insert_columns ); |
| 101 | |
| 102 | return $wpdb->insert_id; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param Base_Db_Model $model |
| 107 | * @param array $columns |
| 108 | * @param $where |
| 109 | * @param $format |
| 110 | * @param $where_format |
| 111 | * |
| 112 | * @return int |
| 113 | * @throws Sql_Exception |
| 114 | */ |
| 115 | public function update( Base_Db_Model $model, $columns, $where, $format = null, $where_format = null ): int { |
| 116 | global $wpdb; |
| 117 | |
| 118 | $model->before_update(); |
| 119 | |
| 120 | $update_columns = $model->update_columns( $columns ); |
| 121 | |
| 122 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 123 | $wpdb->update( $model->table(), $update_columns, $where, $format, $where_format ); |
| 124 | |
| 125 | if ( ! $wpdb->rows_affected ) { |
| 126 | throw new Sql_Exception( |
| 127 | esc_html( "Something went wrong on update rows in: {$model::table()}" ), |
| 128 | $columns, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 129 | $where // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | return $wpdb->rows_affected; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @param Base_Db_Model $model |
| 138 | * @param $where |
| 139 | * @param $where_format |
| 140 | * |
| 141 | * @return int |
| 142 | * @throws Sql_Exception |
| 143 | */ |
| 144 | public function delete( Base_Db_Model $model, $where, $where_format ): int { |
| 145 | global $wpdb; |
| 146 | |
| 147 | $model->before_delete(); |
| 148 | |
| 149 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 150 | $result = (int) $wpdb->delete( $model->table(), $where, $where_format ); |
| 151 | |
| 152 | if ( ! $result ) { |
| 153 | throw new Sql_Exception( |
| 154 | esc_html( "Something went wrong on delete rows in: {$model->table()}" ), |
| 155 | $where // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | return $result; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param View_Base $view |
| 164 | * |
| 165 | * @return int |
| 166 | * @throws Query_Builder_Exception |
| 167 | */ |
| 168 | public function view_delete( View_Base $view ): int { |
| 169 | global $wpdb; |
| 170 | |
| 171 | $where = ( new Query_Conditions_Builder() ) |
| 172 | ->set_view( $view ) |
| 173 | ->result(); |
| 174 | |
| 175 | // phpcs:ignore WordPress.DB |
| 176 | $wpdb->query( "DELETE FROM {$view->table()} {$where}" ); |
| 177 | |
| 178 | if ( ! $wpdb->rows_affected ) { |
| 179 | throw new Query_Builder_Exception( |
| 180 | esc_html( "Something went wrong on delete rows in: {$view->table()}" ), |
| 181 | $where // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | return $wpdb->rows_affected; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @param array $columns |
| 190 | * @param View_Base $view |
| 191 | * |
| 192 | * @return int |
| 193 | * @throws Sql_Exception |
| 194 | */ |
| 195 | public function view_update( array $columns, View_Base $view ): int { |
| 196 | global $wpdb; |
| 197 | |
| 198 | $where = ( new Query_Conditions_Builder() ) |
| 199 | ->set_view( $view ) |
| 200 | ->result(); |
| 201 | |
| 202 | $set = $view->build_set( $columns ); |
| 203 | $query = "UPDATE `{$view->table()}` {$set} {$where};"; |
| 204 | |
| 205 | // phpcs:ignore WordPress.DB |
| 206 | (int) $wpdb->query( $query ); |
| 207 | |
| 208 | if ( ! $wpdb->rows_affected ) { |
| 209 | throw new Sql_Exception( |
| 210 | esc_html( "Something went wrong on update rows in: {$view->table()}" ), |
| 211 | $query, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 212 | $wpdb->rows_affected // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | return $wpdb->rows_affected; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @param Base_Db_Model $model |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | |
| 225 | public function create_table_schema( Base_Db_Model $model ) { |
| 226 | $schema_keys = $model->schema_keys(); |
| 227 | $table = $model->table(); |
| 228 | $columns = $model->schema(); |
| 229 | $charset_collate = $model->schema_charset_collate(); |
| 230 | $engine = apply_filters( 'jet-form-builder/table-engine', $model->schema_engine(), $model ); |
| 231 | |
| 232 | if ( ! empty( $engine ) ) { |
| 233 | $engine = "ENGINE={$engine}"; |
| 234 | } |
| 235 | |
| 236 | $ready_columns = ''; |
| 237 | foreach ( $columns as $column => $desc ) { |
| 238 | $ready_columns .= $column . ' ' . $desc . ", \n"; |
| 239 | } |
| 240 | |
| 241 | $keys = array(); |
| 242 | foreach ( $schema_keys as $column_name => $key ) { |
| 243 | $keys[] = "$key ($column_name)"; |
| 244 | } |
| 245 | $ready_keys = implode( ", \n", $keys ); |
| 246 | |
| 247 | return "CREATE TABLE $table ( |
| 248 | $ready_columns |
| 249 | $ready_keys |
| 250 | ) {$engine} {$charset_collate};"; |
| 251 | } |
| 252 | |
| 253 | protected function add_foreign_relations( Base_Db_Model $model ) { |
| 254 | global $wpdb; |
| 255 | |
| 256 | foreach ( $model->foreign_relations() as $constraint ) { |
| 257 | $constraint->set_foreign_table( $model::table_name() ); |
| 258 | try { |
| 259 | $constraint->before_create(); |
| 260 | } catch ( Skip_Exception $exception ) { |
| 261 | continue; |
| 262 | } |
| 263 | // phpcs:ignore WordPress.DB |
| 264 | $wpdb->query( "ALTER TABLE `{$model::table()}` ADD {$constraint->build()}" ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | public function is_exist( Base_Db_Model $model ): bool { |
| 269 | global $wpdb; |
| 270 | |
| 271 | $table = $model::table(); |
| 272 | |
| 273 | if ( ! isset( $this->existed_tables[ $table ] ) ) { |
| 274 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 275 | $find = $wpdb->get_var( |
| 276 | $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) |
| 277 | ); |
| 278 | |
| 279 | $this->existed_tables[ $table ] = ( $table === $find ); |
| 280 | } |
| 281 | |
| 282 | return $this->existed_tables[ $table ]; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * @param Base_Db_Model $model |
| 287 | * |
| 288 | * @return Execution_Builder |
| 289 | */ |
| 290 | public function safe_create( Base_Db_Model $model ): Execution_Builder { |
| 291 | if ( $this->is_exist( $model ) ) { |
| 292 | return $this; |
| 293 | } |
| 294 | |
| 295 | return $this->create( $model ); |
| 296 | } |
| 297 | |
| 298 | public function save_to_existed( Base_Db_Model $model ): Execution_Builder { |
| 299 | $this->existed_tables[ $model->table() ] = true; |
| 300 | |
| 301 | return $this; |
| 302 | } |
| 303 | |
| 304 | public function transaction_start() { |
| 305 | global $wpdb; |
| 306 | |
| 307 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 308 | return $wpdb->query( 'START TRANSACTION' ); |
| 309 | } |
| 310 | |
| 311 | public function transaction_commit() { |
| 312 | global $wpdb; |
| 313 | |
| 314 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 315 | return $wpdb->query( 'COMMIT' ); |
| 316 | } |
| 317 | |
| 318 | public function transaction_rollback() { |
| 319 | global $wpdb; |
| 320 | |
| 321 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 322 | return $wpdb->query( 'ROLLBACK' ); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | public function delta( $sql ) { |
| 327 | if ( ! function_exists( 'dbDelta' ) ) { |
| 328 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 329 | } |
| 330 | |
| 331 | dbDelta( $sql ); |
| 332 | } |
| 333 | |
| 334 | } |
| 335 |