"Fossies" - the Fresh Open Source Software Archive

Member "phpMyAdmin-5.1.0-all-languages/libraries/classes/TablePartitionDefinition.php" (24 Feb 2021, 6664 Bytes) of package /linux/www/phpMyAdmin-5.1.0-all-languages.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "TablePartitionDefinition.php": 5.0.4-english_vs_5.1.0-english.

    1 <?php
    2 
    3 declare(strict_types=1);
    4 
    5 namespace PhpMyAdmin;
    6 
    7 use function array_intersect_key;
    8 use function array_merge;
    9 use function array_splice;
   10 use function min;
   11 
   12 class TablePartitionDefinition
   13 {
   14     /**
   15      * @param array|null $details Details that may be pre-filled
   16      *
   17      * @return array
   18      */
   19     public static function getDetails(?array $details = null): array
   20     {
   21         if (! isset($details)) {
   22             $details = self::generateDetails();
   23         }
   24 
   25         return $details;
   26     }
   27 
   28     /**
   29      * @return array
   30      */
   31     protected static function generateDetails(): array
   32     {
   33         $partitionDetails = self::extractDetailsFromRequest();
   34 
   35         // Only LIST and RANGE type parameters allow subpartitioning
   36         $partitionDetails['can_have_subpartitions'] = $partitionDetails['partition_count'] > 1
   37             && isset($partitionDetails['partition_by'])
   38             && ($partitionDetails['partition_by'] === 'RANGE'
   39                 || $partitionDetails['partition_by'] === 'RANGE COLUMNS'
   40                 || $partitionDetails['partition_by'] === 'LIST'
   41                 || $partitionDetails['partition_by'] === 'LIST COLUMNS');
   42 
   43         // Values are specified only for LIST and RANGE type partitions
   44         $partitionDetails['value_enabled'] = isset($partitionDetails['partition_by'])
   45             && ($partitionDetails['partition_by'] === 'RANGE'
   46                 || $partitionDetails['partition_by'] === 'RANGE COLUMNS'
   47                 || $partitionDetails['partition_by'] === 'LIST'
   48                 || $partitionDetails['partition_by'] === 'LIST COLUMNS');
   49 
   50         return self::extractPartitions($partitionDetails);
   51     }
   52 
   53     /**
   54      * Extract some partitioning and subpartitioning parameters from the request
   55      *
   56      * @return array
   57      */
   58     protected static function extractDetailsFromRequest(): array
   59     {
   60         $partitionParams = [
   61             'partition_by' => null,
   62             'partition_expr' => null,
   63             'subpartition_by' => null,
   64             'subpartition_expr' => null,
   65         ];
   66         //Initialize details with values to "null" if not in request
   67         $details = array_merge(
   68             $partitionParams,
   69             //Keep $_POST values, but only for keys that are in $partitionParams
   70             array_intersect_key($_POST, $partitionParams)
   71         );
   72 
   73         $details['partition_count'] = self::extractPartitionCount('partition_count') ?: '';
   74         $details['subpartition_count'] = self::extractPartitionCount('subpartition_count') ?: '';
   75 
   76         return $details;
   77     }
   78 
   79     /**
   80      * @param string $paramLabel Label searched in request
   81      */
   82     protected static function extractPartitionCount(string $paramLabel): int
   83     {
   84         if (Core::isValid($_POST[$paramLabel], 'numeric')) {
   85             // MySQL's limit is 8192, so do not allow more
   86             $count = min((int) $_POST[$paramLabel], 8192);
   87         } else {
   88             $count = 0;
   89         }
   90 
   91         return $count;
   92     }
   93 
   94     /**
   95      * @param array $partitionDetails Details of partitions
   96      *
   97      * @return array
   98      */
   99     protected static function extractPartitions(array $partitionDetails): array
  100     {
  101         $partitionCount = $partitionDetails['partition_count'];
  102         $subpartitionCount = $partitionDetails['subpartition_count'];
  103 
  104         // No partitions
  105         if ($partitionCount <= 1) {
  106             return $partitionDetails;
  107         }
  108 
  109         // Has partitions
  110         $partitions = $_POST['partitions'] ?? [];
  111 
  112         // Remove details of the additional partitions
  113         // when number of partitions have been reduced
  114         array_splice($partitions, $partitionCount);
  115 
  116         for ($i = 0; $i < $partitionCount; $i++) {
  117             if (! isset($partitions[$i])) { // Newly added partition
  118                 $partitions[$i] = [
  119                     'name' => 'p' . $i,
  120                     'value_type' => '',
  121                     'value' => '',
  122                     'engine' => '',
  123                     'comment' => '',
  124                     'data_directory' => '',
  125                     'index_directory' => '',
  126                     'max_rows' => '',
  127                     'min_rows' => '',
  128                     'tablespace' => '',
  129                     'node_group' => '',
  130                 ];
  131             }
  132 
  133             $partition =& $partitions[$i];
  134             $partition['prefix'] = 'partitions[' . $i . ']';
  135 
  136             // Changing from HASH/KEY to RANGE/LIST
  137             if (! isset($partition['value_type'])) {
  138                 $partition['value_type'] = '';
  139                 $partition['value'] = '';
  140             }
  141             if (! isset($partition['engine'])) { // When removing subpartitioning
  142                 $partition['engine'] = '';
  143                 $partition['comment'] = '';
  144                 $partition['data_directory'] = '';
  145                 $partition['index_directory'] = '';
  146                 $partition['max_rows'] = '';
  147                 $partition['min_rows'] = '';
  148                 $partition['tablespace'] = '';
  149                 $partition['node_group'] = '';
  150             }
  151 
  152             // No subpartitions
  153             if ($subpartitionCount <= 1 || $partitionDetails['can_have_subpartitions'] !== true) {
  154                 unset($partition['subpartitions'], $partition['subpartition_count']);
  155                 continue;
  156             }
  157 
  158             // Has subpartitions
  159             $partition['subpartition_count'] = $subpartitionCount;
  160 
  161             if (! isset($partition['subpartitions'])) {
  162                 $partition['subpartitions'] = [];
  163             }
  164             $subpartitions =& $partition['subpartitions'];
  165 
  166             // Remove details of the additional subpartitions
  167             // when number of subpartitions have been reduced
  168             array_splice($subpartitions, $subpartitionCount);
  169 
  170             for ($j = 0; $j < $subpartitionCount; $j++) {
  171                 if (! isset($subpartitions[$j])) { // Newly added subpartition
  172                     $subpartitions[$j] = [
  173                         'name' => $partition['name'] . '_s' . $j,
  174                         'engine' => '',
  175                         'comment' => '',
  176                         'data_directory' => '',
  177                         'index_directory' => '',
  178                         'max_rows' => '',
  179                         'min_rows' => '',
  180                         'tablespace' => '',
  181                         'node_group' => '',
  182                     ];
  183                 }
  184 
  185                 $subpartitions[$j]['prefix'] = 'partitions[' . $i . ']'
  186                     . '[subpartitions][' . $j . ']';
  187             }
  188         }
  189         $partitionDetails['partitions'] = $partitions;
  190 
  191         return $partitionDetails;
  192     }
  193 }