| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SeQura\WC\Repositories; |
| 10 | 10 | |
| 11 | +use Exception; | |
| 11 | 12 | use SeQura\Core\Infrastructure\ORM\Entity; |
| 12 | 13 | use SeQura\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException; |
| 13 | 14 | use SeQura\Core\Infrastructure\ORM\Interfaces\RepositoryInterface; |
| 14 | 15 | use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryCondition; |
| @@ -15,8 +16,9 @@ | ||
| 15 | 16 | use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; |
| 16 | 17 | use SeQura\Core\Infrastructure\ORM\Utility\IndexHelper; |
| 17 | 18 | use SeQura\Core\Infrastructure\ServiceRegister; |
| 18 | 19 | use SeQura\WC\Dto\Table_Index; |
| 20 | +use SeQura\WC\Dto\Table_Index_Column; | |
| 19 | 21 | use wpdb; |
| 20 | 22 | |
| 21 | 23 | /** |
| 22 | 24 | * Shared repository functionality. |
| @@ -94,14 +96,14 @@ | ||
| 94 | 96 | |
| 95 | 97 | /** |
| 96 | 98 | * Executes select query. |
| 97 | 99 | * |
| 98 | - * @param QueryFilter $filter Filter for query. | |
| 100 | + * @param QueryFilter|null $filter Filter for query. | |
| 99 | 101 | * |
| 100 | 102 | * @return Entity[] A list of found entities ot empty array. |
| 101 | 103 | * @throws QueryFilterInvalidParamException If filter condition is invalid. |
| 102 | 104 | */ |
| 103 | - public function select( QueryFilter $filter = null ) { | |
| 105 | + public function select( ?QueryFilter $filter = null ) { | |
| 104 | 106 | /** |
| 105 | 107 | * Entity object. |
| 106 | 108 | * |
| 107 | 109 | * @var Entity $entity |
| @@ -136,14 +138,14 @@ | ||
| 136 | 138 | |
| 137 | 139 | /** |
| 138 | 140 | * Executes select query and returns first result. |
| 139 | 141 | * |
| 140 | - * @param QueryFilter $filter Filter for query. | |
| 142 | + * @param QueryFilter|null $filter Filter for query. | |
| 141 | 143 | * |
| 142 | 144 | * @return Entity|null First found entity or NULL. |
| 143 | 145 | * @throws QueryFilterInvalidParamException If filter condition is invalid. |
| 144 | 146 | */ |
| 145 | - public function selectOne( QueryFilter $filter = null ) { | |
| 147 | + public function selectOne( ?QueryFilter $filter = null ) { | |
| 146 | 148 | if ( ! $filter ) { |
| 147 | 149 | $filter = new QueryFilter(); |
| 148 | 150 | } |
| 149 | 151 | |
| @@ -239,14 +241,14 @@ | ||
| 239 | 241 | |
| 240 | 242 | /** |
| 241 | 243 | * Counts records that match filter criteria. |
| 242 | 244 | * |
| 243 | - * @param QueryFilter $filter Filter for query. | |
| 245 | + * @param QueryFilter|null $filter Filter for query. | |
| 244 | 246 | * |
| 245 | 247 | * @return int Number of records that match filter criteria. |
| 246 | 248 | * @throws QueryFilterInvalidParamException If filter condition is invalid. |
| 247 | 249 | */ |
| 248 | - public function count( QueryFilter $filter = null ) { | |
| 250 | + public function count( ?QueryFilter $filter = null ) { | |
| 249 | 251 | /** |
| 250 | 252 | * Entity object. |
| 251 | 253 | * |
| 252 | 254 | * @var Entity $entity |
| @@ -579,11 +581,11 @@ | ||
| 579 | 581 | if ( $this->index_exists( $index ) ) { |
| 580 | 582 | return true; |
| 581 | 583 | } |
| 582 | 584 | $index_name = \sanitize_key( $index->name ); |
| 583 | - $columns = $index->columns; | |
| 584 | - foreach ( $columns as &$column ) { | |
| 585 | - $column = '`' . \sanitize_key( $column ) . '`'; | |
| 585 | + $columns = array(); | |
| 586 | + foreach ( $index->columns as $column ) { | |
| 587 | + $columns[] = '`' . \sanitize_key( $column->name ) . '`' . ( null !== $column->char_limit ? "({$column->char_limit})" : '' ); | |
| 586 | 588 | } |
| 587 | 589 | $columns = implode( ',', $columns ); |
| 588 | 590 | return false !== $this->db->query( "ALTER TABLE `{$this->get_table_name()}` ADD INDEX `{$index_name}` ({$columns})" ); |
| 589 | 591 | } |
| @@ -663,20 +665,14 @@ | ||
| 663 | 665 | |
| 664 | 666 | /** |
| 665 | 667 | * Create the table if it doesn't exist. |
| 666 | 668 | * |
| 667 | - * @return bool True if the table was created successfully, false otherwise. | |
| 669 | + * @throws Exception If the table creation fails. | |
| 668 | 670 | */ |
| 669 | 671 | public function create_table() { |
| 670 | 672 | $indexes = array(); |
| 671 | 673 | foreach ( $this->get_required_indexes() as $index ) { |
| 672 | - $index_name = $index->name; | |
| 673 | - $columns = $index->columns; | |
| 674 | - foreach ( $columns as &$column ) { | |
| 675 | - $column = '`' . \sanitize_key( $column ) . '`'; | |
| 676 | - } | |
| 677 | - $columns = implode( ',', $columns ); | |
| 678 | - $indexes[] = "KEY `{$index_name}` ({$columns})"; | |
| 674 | + $indexes[] = $index->to_sql(); | |
| 679 | 675 | } |
| 680 | 676 | $indexes = implode( ', ', $indexes ); |
| 681 | 677 | if ( ! empty( $indexes ) ) { |
| 682 | 678 | $indexes = ', ' . $indexes; |
| @@ -683,39 +679,36 @@ | ||
| 683 | 679 | } |
| 684 | 680 | |
| 685 | 681 | $sql = sprintf( $this->get_create_table_sql(), $indexes ); |
| 686 | 682 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 687 | - \dbDelta( $sql ); | |
| 688 | - return $this->table_exists(); | |
| 683 | + $result = \dbDelta( $sql ); | |
| 684 | + if ( ! $this->table_exists() ) { | |
| 685 | + throw new Exception( \esc_html( "SQL: $sql\nResult: " . implode( '. ', $result ) ) ); | |
| 686 | + } | |
| 689 | 687 | } |
| 690 | 688 | |
| 691 | 689 | /** |
| 692 | 690 | * Make sure that the required tables for the migration are created. |
| 693 | 691 | * |
| 694 | - * @return bool | |
| 692 | + * @throws Exception If cannot prepare tables for migration. | |
| 695 | 693 | */ |
| 696 | 694 | public function prepare_tables_for_migration() { |
| 697 | - if ( $this->table_exists( true ) ) { | |
| 698 | - // If the legacy table already exists, we don't need to do anything. | |
| 699 | - return true; | |
| 695 | + // Rename the table to legacy table if it doesn't exist. | |
| 696 | + if ( ! $this->table_exists( true ) && false === $this->db->query( "RENAME TABLE {$this->get_table_name()} TO {$this->get_legacy_table_name()};" ) ) { | |
| 697 | + throw new Exception( \esc_html( "Could not rename table {$this->get_table_name()} to {$this->get_legacy_table_name()}" ) ); | |
| 700 | 698 | } |
| 701 | - // Rename the table to legacy table. | |
| 702 | - if ( false === $this->db->query( "RENAME TABLE {$this->get_table_name()} TO {$this->get_legacy_table_name()};" ) ) { | |
| 703 | - return false; | |
| 704 | - } | |
| 705 | 699 | |
| 706 | - // Create the table if not exists. | |
| 707 | - if ( ! $this->create_table() ) { | |
| 708 | - return false; | |
| 700 | + if ( ! $this->table_exists() ) { | |
| 701 | + // Create the table if not exists. | |
| 702 | + $this->create_table(); | |
| 703 | + | |
| 704 | + // Add the auto-increment next value to the new table. | |
| 705 | + $raw_id = $this->db->get_var( "SELECT MAX(id) FROM {$this->get_legacy_table_name()};" ); | |
| 706 | + $auto_increment = null !== $raw_id && is_numeric( $raw_id ) ? (int) $raw_id + 1 : 1; | |
| 707 | + if ( false === $this->db->query( "ALTER TABLE {$this->get_table_name()} AUTO_INCREMENT = {$auto_increment};" ) ) { | |
| 708 | + throw new Exception( \esc_html( "Could not set auto-increment value for table {$this->get_table_name()} to {$auto_increment}" ) ); | |
| 709 | + } | |
| 709 | 710 | } |
| 710 | - // Add the auto-increment next value to the new table. | |
| 711 | - $raw_id = $this->db->get_var( "SELECT MAX(id) FROM {$this->get_legacy_table_name()};" ); | |
| 712 | - $auto_increment = null !== $raw_id && is_numeric( $raw_id ) ? (int) $raw_id + 1 : 1; | |
| 713 | - if ( false === $this->db->query( "ALTER TABLE {$this->get_table_name()} AUTO_INCREMENT = {$auto_increment};" ) ) { | |
| 714 | - return false; | |
| 715 | - } | |
| 716 | - | |
| 717 | - return true; | |
| 718 | 711 | } |
| 719 | 712 | |
| 720 | 713 | /** |
| 721 | 714 | * Evaluates if the legacy table should be removed and if so, removes it. |
| @@ -740,9 +733,9 @@ | ||
| 740 | 733 | * @return Table_Index[] The list of indexes. |
| 741 | 734 | */ |
| 742 | 735 | public function get_required_indexes() { |
| 743 | 736 | return array( |
| 744 | - new Table_Index( $this->get_table_name() . '_type', array( 'type' ) ), | |
| 737 | + new Table_Index( $this->get_table_name() . '_type', array( new Table_Index_Column( 'type', 64 ) ) ), | |
| 745 | 738 | ); |
| 746 | 739 | } |
| 747 | 740 | |
| 748 | 741 | /** |