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 |