PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.1
JetFormBuilder — Dynamic Blocks Form Builder v3.3.1
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / db-queries / execution-builder.php
jetformbuilder / includes / db-queries Last commit date
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
340 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 throw new Sql_Exception(
94 esc_html( "Something went wrong on insert into: {$model::table()}" ),
95 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
96 $wpdb->last_error,
97 // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
98 $insert_columns
99 );
100 }
101
102 $model->after_insert( $insert_columns );
103
104 return $wpdb->insert_id;
105 }
106
107 /**
108 * @param Base_Db_Model $model
109 * @param array $columns
110 * @param $where
111 * @param $format
112 * @param $where_format
113 *
114 * @return int
115 * @throws Sql_Exception
116 */
117 public function update( Base_Db_Model $model, $columns, $where, $format = null, $where_format = null ): int {
118 global $wpdb;
119
120 $model->before_update();
121
122 $update_columns = $model->update_columns( $columns );
123
124 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
125 $wpdb->update( $model->table(), $update_columns, $where, $format, $where_format );
126
127 if ( ! $wpdb->rows_affected ) {
128 throw new Sql_Exception(
129 esc_html( "Something went wrong on update rows in: {$model::table()}" ),
130 $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
131 $columns, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
132 $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
133 );
134 }
135
136 return $wpdb->rows_affected;
137 }
138
139 /**
140 * @param Base_Db_Model $model
141 * @param $where
142 * @param $where_format
143 *
144 * @return int
145 * @throws Sql_Exception
146 */
147 public function delete( Base_Db_Model $model, $where, $where_format ): int {
148 global $wpdb;
149
150 $model->before_delete();
151
152 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
153 $result = (int) $wpdb->delete( $model->table(), $where, $where_format );
154
155 if ( ! $result ) {
156 throw new Sql_Exception(
157 esc_html( "Something went wrong on delete rows in: {$model->table()}" ),
158 $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
159 $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
160 );
161 }
162
163 return $result;
164 }
165
166 /**
167 * @param View_Base $view
168 *
169 * @return int
170 * @throws Query_Builder_Exception
171 */
172 public function view_delete( View_Base $view ): int {
173 global $wpdb;
174
175 $where = ( new Query_Conditions_Builder() )
176 ->set_view( $view )
177 ->result();
178
179 // phpcs:ignore WordPress.DB
180 $wpdb->query( "DELETE FROM {$view->table()} {$where}" );
181
182 if ( ! $wpdb->rows_affected ) {
183 throw new Query_Builder_Exception(
184 esc_html( "Something went wrong on delete rows in: {$view->table()}" ),
185 $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
186 $where // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
187 );
188 }
189
190 return $wpdb->rows_affected;
191 }
192
193 /**
194 * @param array $columns
195 * @param View_Base $view
196 *
197 * @return int
198 * @throws Sql_Exception
199 */
200 public function view_update( array $columns, View_Base $view ): int {
201 global $wpdb;
202
203 $where = ( new Query_Conditions_Builder() )
204 ->set_view( $view )
205 ->result();
206
207 $set = $view->build_set( $columns );
208 $query = "UPDATE `{$view->table()}` {$set} {$where};";
209
210 // phpcs:ignore WordPress.DB
211 (int) $wpdb->query( $query );
212
213 if ( ! $wpdb->rows_affected ) {
214 throw new Sql_Exception(
215 esc_html( "Something went wrong on update rows in: {$view->table()}" ),
216 $wpdb->last_error, // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
217 $query // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
218 );
219 }
220
221 return $wpdb->rows_affected;
222 }
223
224 /**
225 * @param Base_Db_Model $model
226 *
227 * @return string
228 */
229
230 public function create_table_schema( Base_Db_Model $model ) {
231 $schema_keys = $model->schema_keys();
232 $table = $model->table();
233 $columns = $model->schema();
234 $charset_collate = $model->schema_charset_collate();
235 $engine = apply_filters( 'jet-form-builder/table-engine', $model->schema_engine(), $model );
236
237 if ( ! empty( $engine ) ) {
238 $engine = "ENGINE={$engine}";
239 }
240
241 $ready_columns = '';
242 foreach ( $columns as $column => $desc ) {
243 $ready_columns .= $column . ' ' . $desc . ", \n";
244 }
245
246 $keys = array();
247 foreach ( $schema_keys as $column_name => $key ) {
248 $keys[] = "$key ($column_name)";
249 }
250 $ready_keys = implode( ", \n", $keys );
251
252 return "CREATE TABLE $table (
253 $ready_columns
254 $ready_keys
255 ) {$engine} {$charset_collate};";
256 }
257
258 protected function add_foreign_relations( Base_Db_Model $model ) {
259 global $wpdb;
260
261 foreach ( $model->foreign_relations() as $constraint ) {
262 $constraint->set_foreign_table( $model::table_name() );
263 try {
264 $constraint->before_create();
265 } catch ( Skip_Exception $exception ) {
266 continue;
267 }
268 // phpcs:ignore WordPress.DB
269 $wpdb->query( "ALTER TABLE `{$model::table()}` ADD {$constraint->build()}" );
270 }
271 }
272
273 public function is_exist( Base_Db_Model $model ): bool {
274 global $wpdb;
275
276 $table = $model::table();
277
278 if ( ! isset( $this->existed_tables[ $table ] ) ) {
279 // phpcs:ignore WordPress.DB.DirectDatabaseQuery
280 $find = $wpdb->get_var(
281 $wpdb->prepare( 'SHOW TABLES LIKE %s', $table )
282 );
283
284 $this->existed_tables[ $table ] = ( $table === $find );
285 }
286
287 return $this->existed_tables[ $table ];
288 }
289
290 /**
291 * @param Base_Db_Model $model
292 *
293 * @return Execution_Builder
294 */
295 public function safe_create( Base_Db_Model $model ): Execution_Builder {
296 if ( $this->is_exist( $model ) ) {
297 return $this;
298 }
299
300 return $this->create( $model );
301 }
302
303 public function save_to_existed( Base_Db_Model $model ): Execution_Builder {
304 $this->existed_tables[ $model->table() ] = true;
305
306 return $this;
307 }
308
309 public function transaction_start() {
310 global $wpdb;
311
312 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
313 return $wpdb->query( 'START TRANSACTION' );
314 }
315
316 public function transaction_commit() {
317 global $wpdb;
318
319 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
320 return $wpdb->query( 'COMMIT' );
321 }
322
323 public function transaction_rollback() {
324 global $wpdb;
325
326 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
327 return $wpdb->query( 'ROLLBACK' );
328 }
329
330
331 public function delta( $sql ) {
332 if ( ! function_exists( 'dbDelta' ) ) {
333 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
334 }
335
336 dbDelta( $sql );
337 }
338
339 }
340