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