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