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 |