PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.5.4
JetFormBuilder — Dynamic Blocks Form Builder v3.5.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 / base-db-constraint.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 8 months ago
base-db-constraint.php
132 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Db_Queries;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 class Base_Db_Constraint {
12
13 const ACTION_CASCADE = 'CASCADE';
14 const ACTION_SET_DEFAULT = 'SET DEFAULT';
15 const ACTION_SET_NULL = 'SET NULL';
16 const ACTION_RESTRICT = 'RESTRICT';
17
18 protected $foreign_keys;
19 protected $foreign_table;
20
21 /** @var Base_Db_Model */
22 protected $parent_model;
23 protected $parent_keys = array( 'id' );
24
25 protected $actions = array();
26
27 /**
28 * Here we can check the engine of the table,
29 * if it is not InnoDB - throw \Jet_Form_Builder\Db_Queries\Exceptions\Skip_Exception
30 */
31 public function before_create() {
32 }
33
34 /**
35 * @param array $keys
36 *
37 * @return $this
38 */
39 public function set_foreign_keys( array $keys ): Base_Db_Constraint {
40 $this->foreign_keys = $keys;
41
42 return $this;
43 }
44
45 /**
46 * @param string $table
47 *
48 * @return $this
49 */
50 public function set_foreign_table( string $table ): Base_Db_Constraint {
51 $this->foreign_table = $table;
52
53 return $this;
54 }
55
56 public function set_model( Base_Db_Model $model ): Base_Db_Constraint {
57 $this->parent_model = $model;
58
59 return $this;
60 }
61
62 public function get_model(): Base_Db_Model {
63 return $this->parent_model;
64 }
65
66 public function get_table(): string {
67 return $this->parent_model::table();
68 }
69
70 public function get_table_name(): string {
71 return $this->parent_model::table_name();
72 }
73
74 public function set_parent_keys( array $keys ): Base_Db_Constraint {
75 $this->parent_keys = $keys;
76
77 return $this;
78 }
79
80 public function on_delete( string $action ): Base_Db_Constraint {
81 return $this->set_action( 'DELETE', $action );
82 }
83
84 public function on_update( string $action ): Base_Db_Constraint {
85 return $this->set_action( 'UPDATE', $action );
86 }
87
88 /**
89 * @param string $name
90 * @param string $action
91 *
92 * @return $this
93 */
94 public function set_action( string $name, string $action ): Base_Db_Constraint {
95 $this->actions[ $name ] = $action;
96
97 return $this;
98 }
99
100 public function get_name(): string {
101 $raw_name = "{$this->get_table_name()}__" . implode( '_', $this->foreign_keys ) . "__{$this->foreign_table}";
102
103 /**
104 * https://dev.mysql.com/doc/refman/5.6/en/identifier-length.html
105 */
106 $length_excess = strlen( $raw_name ) - 64;
107
108 if ( $length_excess < 0 ) {
109 return $raw_name;
110 }
111
112 // cut the string
113 return substr( $raw_name, 0, -$length_excess );
114 }
115
116 final public function build_part(): string {
117 $constraint = 'FOREIGN KEY (`' . implode( '` , `', $this->foreign_keys ) . '`) ';
118 $constraint .= "REFERENCES {$this->get_table()}(`" . implode( '` , `', $this->parent_keys ) . '`)';
119
120 foreach ( $this->actions as $name => $action ) {
121 $constraint .= " ON {$name} {$action}";
122 }
123
124 return $constraint;
125 }
126
127 final public function build(): string {
128 return "CONSTRAINT {$this->get_name()} {$this->build_part()}";
129 }
130
131 }
132