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