| 1 |
<?php |
| 2 |
/** |
| 3 |
* Table Index. |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace SeQura\WC\Dto; |
| 9 |
|
| 10 |
/** |
| 11 |
* Table Index. |
| 12 |
*/ |
| 13 |
class Table_Index extends Dto { |
| 14 |
|
| 15 |
/** |
| 16 |
* Unique identifier |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public $name; |
| 21 |
|
| 22 |
/** |
| 23 |
* Columns to be indexed |
| 24 |
* |
| 25 |
* @var Table_Index_Column[] |
| 26 |
*/ |
| 27 |
public $columns; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor |
| 31 |
* |
| 32 |
* @param string $name The name of the index. |
| 33 |
* @param Table_Index_Column[] $columns The columns to be indexed. |
| 34 |
*/ |
| 35 |
public function __construct( string $name, array $columns ) { |
| 36 |
$this->name = $name; |
| 37 |
$this->columns = $columns; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generate SQL definition for the index. |
| 42 |
* |
| 43 |
* @return string SQL statement to create the index. |
| 44 |
*/ |
| 45 |
public function to_sql() { |
| 46 |
$index_name = \sanitize_key( $this->name ); |
| 47 |
$columns = array(); |
| 48 |
foreach ( $this->columns as $column ) { |
| 49 |
$columns[] = '`' . \sanitize_key( $column->name ) . '`' . ( null !== $column->char_limit ? "({$column->char_limit})" : '' ); |
| 50 |
} |
| 51 |
$columns = implode( ', ', $columns ); |
| 52 |
return "INDEX `{$index_name}` ({$columns})"; |
| 53 |
} |
| 54 |
} |
| 55 |
|