| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Settings | |
| 3 | + * Queue item repository. | |
| 4 | 4 | * |
| 5 | 5 | * @package SeQura/WC |
| 6 | 6 | * @subpackage SeQura/WC/Repositories |
| 7 | 7 | */ |
| @@ -8,9 +8,8 @@ | ||
| 8 | 8 | |
| 9 | 9 | namespace SeQura\WC\Repositories; |
| 10 | 10 | |
| 11 | 11 | use SeQura\Core\Infrastructure\ORM\Entity; |
| 12 | -use SeQura\Core\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException; | |
| 13 | 12 | use SeQura\Core\Infrastructure\ORM\Interfaces\QueueItemRepository; |
| 14 | 13 | use SeQura\Core\Infrastructure\ORM\QueryFilter\QueryFilter; |
| 15 | 14 | use SeQura\Core\Infrastructure\ORM\Utility\IndexHelper; |
| 16 | 15 | use SeQura\Core\Infrastructure\TaskExecution\Exceptions\QueueItemSaveException; |
| @@ -15,11 +14,13 @@ | ||
| 15 | 14 | use SeQura\Core\Infrastructure\ORM\Utility\IndexHelper; |
| 16 | 15 | use SeQura\Core\Infrastructure\TaskExecution\Exceptions\QueueItemSaveException; |
| 17 | 16 | use SeQura\Core\Infrastructure\TaskExecution\Interfaces\Priority; |
| 18 | 17 | use SeQura\Core\Infrastructure\TaskExecution\QueueItem; |
| 18 | +use SeQura\WC\Dto\Table_Index; | |
| 19 | +use SeQura\WC\Dto\Table_Index_Column; | |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | - * Class Base_Repository | |
| 22 | + * Queue item repository. | |
| 22 | 23 | */ |
| 23 | 24 | class Queue_Item_Repository extends Repository implements QueueItemRepository { |
| 24 | 25 | |
| 25 | 26 | /** |
| @@ -70,11 +71,23 @@ | ||
| 70 | 71 | ON queueView.id = queueTable.id"; |
| 71 | 72 | |
| 72 | 73 | $result = $this->db->get_results( $sql, ARRAY_A ); |
| 73 | 74 | if ( ! is_array( $result ) ) { |
| 74 | - return array(); | |
| 75 | + $result = array(); | |
| 75 | 76 | } |
| 77 | + $pending_items = $limit - count( $result ); | |
| 76 | 78 | |
| 79 | + if ( $pending_items > 0 && $this->table_exists( true ) ) { | |
| 80 | + $legacy_result = $this->db->get_results( str_replace( $this->get_table_name(), $this->get_legacy_table_name(), $sql ), ARRAY_A ); | |
| 81 | + if ( is_array( $legacy_result ) ) { | |
| 82 | + $length = count( $legacy_result ); | |
| 83 | + for ( $i = 0; $i < $length && $pending_items > 0; $i++ ) { | |
| 84 | + $result[] = $legacy_result[ $i ]; | |
| 85 | + --$pending_items; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 77 | 90 | return $this->translateToEntities( $result ); |
| 78 | 91 | } |
| 79 | 92 | |
| 80 | 93 | /** |
| @@ -96,10 +109,16 @@ | ||
| 96 | 109 | $queue_item_id = $queue_item->getId(); |
| 97 | 110 | if ( null === $queue_item_id || $queue_item_id <= 0 ) { |
| 98 | 111 | $item_id = $this->save( $queue_item ); |
| 99 | 112 | } else { |
| 100 | - $this->update_queue_item( $queue_item, $additional_where ); | |
| 101 | - $item_id = $queue_item_id; | |
| 113 | + $filter = $this->build_query_filter( | |
| 114 | + array_merge( $additional_where, array( 'id' => $queue_item->getId() ) ) | |
| 115 | + ); | |
| 116 | + | |
| 117 | + if ( null === $this->selectOne( $filter ) ) { | |
| 118 | + throw new QueueItemSaveException( \esc_html( 'Failed to save queue item, update condition(s) not met.' ) ); | |
| 119 | + } | |
| 120 | + $item_id = $this->save( $queue_item ); | |
| 102 | 121 | } |
| 103 | 122 | } catch ( \Exception $exception ) { |
| 104 | 123 | throw new QueueItemSaveException( |
| 105 | 124 | \esc_html( 'Failed to save queue item with id: ' . $item_id ), |
| @@ -123,68 +142,8 @@ | ||
| 123 | 142 | // Not used in this implementation. |
| 124 | 143 | } |
| 125 | 144 | |
| 126 | 145 | /** |
| 127 | - * Updates database record with data from provided $queueItem. | |
| 128 | - * | |
| 129 | - * @param QueueItem $queue_item Queue item. | |
| 130 | - * @param mixed[] $conditions Array of update conditions. | |
| 131 | - * | |
| 132 | - * @throws QueueItemSaveException Queue item save exception. | |
| 133 | - * @throws QueryFilterInvalidParamException If filter condition is invalid. | |
| 134 | - */ | |
| 135 | - private function update_queue_item( QueueItem $queue_item, array $conditions = array() ): void { | |
| 136 | - $conditions = array_merge( $conditions, array( 'id' => $queue_item->getId() ) ); | |
| 137 | - | |
| 138 | - $item = $this->select_for_update( $conditions ); | |
| 139 | - $this->check_if_record_exists( $item ); | |
| 140 | - | |
| 141 | - if ( null !== $item ) { | |
| 142 | - $this->update_with_condition( $queue_item, $conditions ); | |
| 143 | - } | |
| 144 | - } | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * Executes select query for update. | |
| 148 | - * | |
| 149 | - * @param mixed[]$conditions Array of update conditions. | |
| 150 | - * | |
| 151 | - * @return QueueItem|null First found entity or NULL. | |
| 152 | - * @throws QueryFilterInvalidParamException If filter condition is invalid. | |
| 153 | - */ | |
| 154 | - private function select_for_update( array $conditions ) { | |
| 155 | - /** | |
| 156 | - * Entity object. | |
| 157 | - * | |
| 158 | - * @var Entity $entity | |
| 159 | - */ | |
| 160 | - $entity = new $this->entity_class(); | |
| 161 | - $type = $entity->getConfig()->getType(); | |
| 162 | - $field_index_map = IndexHelper::mapFieldsToIndexes( $entity ); | |
| 163 | - | |
| 164 | - $filter = $this->build_query_filter( $conditions ); | |
| 165 | - | |
| 166 | - $query = "SELECT * FROM {$this->get_table_name()} WHERE type = '$type' "; | |
| 167 | - $query .= $this->apply_query_filter( $filter, $field_index_map ); | |
| 168 | - $query .= ' FOR UPDATE'; | |
| 169 | - | |
| 170 | - $raw_results = $this->db->get_results( $query, ARRAY_A ); | |
| 171 | - | |
| 172 | - if ( ! is_array( $raw_results ) ) { | |
| 173 | - return null; | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Entities | |
| 178 | - * | |
| 179 | - * @var QueueItem[] $entities | |
| 180 | - */ | |
| 181 | - $entities = $this->translateToEntities( $raw_results ); | |
| 182 | - | |
| 183 | - return ! empty( $entities ) ? $entities[0] : null; | |
| 184 | - } | |
| 185 | - | |
| 186 | - /** | |
| 187 | 146 | * Builds query filter from conditions array. |
| 188 | 147 | * |
| 189 | 148 | * @noinspection PhpDocMissingThrowsInspection |
| 190 | 149 | * |
| @@ -207,53 +166,60 @@ | ||
| 207 | 166 | return $filter; |
| 208 | 167 | } |
| 209 | 168 | |
| 210 | 169 | /** |
| 211 | - * Validates if item exists. | |
| 212 | - * | |
| 213 | - * @param QueueItem $item Queue item. | |
| 214 | - * | |
| 215 | - * @throws QueueItemSaveException Queue item save exception. | |
| 170 | + * Get the index column name that stores the store ID. | |
| 171 | + * | |
| 172 | + * @return string Index column name or empty string if not applicable. | |
| 216 | 173 | */ |
| 217 | - private function check_if_record_exists( QueueItem $item = null ): void { | |
| 218 | - if ( null === $item ) { | |
| 219 | - $message = 'Failed to save queue item, update condition(s) not met.'; | |
| 220 | - throw new QueueItemSaveException( esc_html( $message ) ); | |
| 221 | - } | |
| 174 | + protected function get_store_id_index_column(): string { | |
| 175 | + return ''; | |
| 222 | 176 | } |
| 223 | 177 | |
| 224 | 178 | /** |
| 225 | - * Updates single record. | |
| 226 | - * | |
| 227 | - * @param QueueItem $item Queue item. | |
| 228 | - * @param mixed[] $conditions List of simple search filters as key-value pair to find records to update. | |
| 229 | - * | |
| 230 | - * @return bool TRUE if operation succeeded; otherwise, FALSE. | |
| 231 | - * | |
| 232 | - * @throws \InvalidArgumentException Invalid argument. | |
| 179 | + * Get a list of indexes that are required for the table. | |
| 180 | + * | |
| 181 | + * @return Table_Index[] The list of indexes. | |
| 233 | 182 | */ |
| 234 | - private function update_with_condition( QueueItem $item, array $conditions ) { | |
| 235 | - $field_index_map = IndexHelper::mapFieldsToIndexes( $item ); | |
| 236 | - $prepared = $this->prepare_entity_for_storage( $item ); | |
| 183 | + public function get_required_indexes() { | |
| 237 | 184 | |
| 238 | - $indexed_conditions = array(); | |
| 239 | - foreach ( $conditions as $key => $value ) { | |
| 240 | - if ( 'id' === $key ) { | |
| 241 | - $indexed_conditions[ $key ] = intval( $value ); | |
| 242 | - } else { | |
| 243 | - $indexed_conditions[ 'index_' . $field_index_map[ $key ] ] = IndexHelper::castFieldValue( $value, gettype( $value ) ); | |
| 244 | - } | |
| 245 | - } | |
| 185 | + $type_col = new Table_Index_Column( 'type', 64 ); | |
| 186 | + $index_1_col = new Table_Index_Column( 'index_1', 64 ); | |
| 187 | + $index_2_col = new Table_Index_Column( 'index_2', 64 ); | |
| 188 | + $index_3_col = new Table_Index_Column( 'index_3', 64 ); | |
| 189 | + $index_4_col = new Table_Index_Column( 'index_4', 64 ); | |
| 246 | 190 | |
| 247 | - // Only one record should be updated. | |
| 248 | - return 1 === $this->db->update( $this->get_table_name(), $prepared, $indexed_conditions ); | |
| 191 | + return array_merge( | |
| 192 | + parent::get_required_indexes(), | |
| 193 | + array( | |
| 194 | + new Table_Index( $this->get_table_name() . '_type_index_1', array( $type_col, $index_1_col ) ), | |
| 195 | + new Table_Index( $this->get_table_name() . '_type_index_2', array( $type_col, $index_2_col ) ), | |
| 196 | + new Table_Index( $this->get_table_name() . '_type_index_3', array( $type_col, $index_3_col ) ), | |
| 197 | + new Table_Index( $this->get_table_name() . '_type_index_4', array( $type_col, $index_4_col ) ), | |
| 198 | + ) | |
| 199 | + ); | |
| 249 | 200 | } |
| 250 | 201 | |
| 251 | 202 | /** |
| 252 | - * Get the index column name that stores the store ID. | |
| 203 | + * Get the SQL statement to create the table without the indexes definition. | |
| 204 | + * Resulting string should include an additional %s placeholder for the indexes. | |
| 253 | 205 | * |
| 254 | - * @return string Index column name or empty string if not applicable. | |
| 206 | + * @return string The SQL statement to create the table. | |
| 255 | 207 | */ |
| 256 | - protected function get_store_id_index_column(): string { | |
| 257 | - return ''; | |
| 208 | + protected function get_create_table_sql() { | |
| 209 | + $charset_collate = $this->db->get_charset_collate(); | |
| 210 | + return "CREATE TABLE {$this->get_table_name()} ( | |
| 211 | + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | |
| 212 | + `type` VARCHAR(255), | |
| 213 | + `index_1` VARCHAR(127), | |
| 214 | + `index_2` VARCHAR(127), | |
| 215 | + `index_3` VARCHAR(127), | |
| 216 | + `index_4` VARCHAR(127), | |
| 217 | + `index_5` VARCHAR(127), | |
| 218 | + `index_6` BIGINT UNSIGNED, | |
| 219 | + `index_7` BIGINT UNSIGNED, | |
| 220 | + `index_8` BIGINT UNSIGNED, | |
| 221 | + `index_9` BIGINT UNSIGNED, | |
| 222 | + `data` LONGTEXT, | |
| 223 | + PRIMARY KEY (id) %s) $charset_collate;"; | |
| 258 | 224 | } |
| 259 | 225 | } |