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