PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.5.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.5.2
3.6.5.2 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 All 130 releases
jetformbuilder / includes / db-queries / base-db-constraint.php
base-db-constraint.php
132 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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