/** * Obtains the size of the given cell * * @param string $last_cumulative_size Last cumulative column size * @param int $last_cumulative_type Last cumulative column type * (NONE or VARCHAR or DECIMAL or INT or BIGINT) * @param int $curr_type Type of the current cell * (NONE or VARCHAR or DECIMAL or INT or BIGINT) * @param string $cell The current cell * * @return string Size of the given cell in the type-appropriate format * @access public * * @todo Handle the error cases more elegantly */ function PMA_detectSize($last_cumulative_size, $last_cumulative_type, $curr_type, $cell) { $curr_size = mb_strlen((string) $cell); /** * If the cell is NULL, don't treat it as a varchar */ if (!strcmp('NULL', $cell)) { return $last_cumulative_size; } elseif ($curr_type == VARCHAR) { /** * What to do if the current cell is of type VARCHAR */ /** * The last cumulative type was VARCHAR */ if ($last_cumulative_type == VARCHAR) { if ($curr_size >= $last_cumulative_size) { return $curr_size; } else { return $last_cumulative_size; } } elseif ($last_cumulative_type == DECIMAL) { /** * The last cumulative type was DECIMAL */ $oldM = PMA_getDecimalPrecision($last_cumulative_size); if ($curr_size >= $oldM) { return $curr_size; } else { return $oldM; } } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { /** * The last cumulative type was BIGINT or INT */ if ($curr_size >= $last_cumulative_size) { return $curr_size; } else { return $last_cumulative_size; } } elseif (!isset($last_cumulative_type) || $last_cumulative_type == NONE) { /** * This is the first row to be analyzed */ return $curr_size; } else { /** * An error has DEFINITELY occurred */ /** * TODO: Handle this MUCH more elegantly */ return -1; } } elseif ($curr_type == DECIMAL) { /** * What to do if the current cell is of type DECIMAL */ /** * The last cumulative type was VARCHAR */ if ($last_cumulative_type == VARCHAR) { /* Convert $last_cumulative_size from varchar to decimal format */ $size = PMA_getDecimalSize($cell); if ($size[M] >= $last_cumulative_size) { return $size[M]; } else { return $last_cumulative_size; } } elseif ($last_cumulative_type == DECIMAL) { /** * The last cumulative type was DECIMAL */ $size = PMA_getDecimalSize($cell); $oldM = PMA_getDecimalPrecision($last_cumulative_size); $oldD = PMA_getDecimalScale($last_cumulative_size); /* New val if M or D is greater than current largest */ if ($size[M] > $oldM || $size[D] > $oldD) { /* Take the largest of both types */ return (string) (($size[M] > $oldM ? $size[M] : $oldM) . "," . ($size[D] > $oldD ? $size[D] : $oldD)); } else { return $last_cumulative_size; } } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { /** * The last cumulative type was BIGINT or INT */ /* Convert $last_cumulative_size from int to decimal format */ $size = PMA_getDecimalSize($cell); if ($size[M] >= $last_cumulative_size) { return $size[FULL]; } else { return $last_cumulative_size . "," . $size[D]; } } elseif (!isset($last_cumulative_type) || $last_cumulative_type == NONE) { /** * This is the first row to be analyzed */ /* First row of the column */ $size = PMA_getDecimalSize($cell); return $size[FULL]; } else { /** * An error has DEFINITELY occurred */ /** * TODO: Handle this MUCH more elegantly */ return -1; } } elseif ($curr_type == BIGINT || $curr_type == INT) { /** * What to do if the current cell is of type BIGINT or INT */ /** * The last cumulative type was VARCHAR */ if ($last_cumulative_type == VARCHAR) { if ($curr_size >= $last_cumulative_size) { return $curr_size; } else { return $last_cumulative_size; } } elseif ($last_cumulative_type == DECIMAL) { /** * The last cumulative type was DECIMAL */ $oldM = PMA_getDecimalPrecision($last_cumulative_size); $oldD = PMA_getDecimalScale($last_cumulative_size); $oldInt = $oldM - $oldD; $newInt = mb_strlen((string) $cell); /* See which has the larger integer length */ if ($oldInt >= $newInt) { /* Use old decimal size */ return $last_cumulative_size; } else { /* Use $newInt + $oldD as new M */ return $newInt + $oldD . "," . $oldD; } } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) { /** * The last cumulative type was BIGINT or INT */ if ($curr_size >= $last_cumulative_size) { return $curr_size; } else { return $last_cumulative_size; } } elseif (!isset($last_cumulative_type) || $last_cumulative_type == NONE) { /** * This is the first row to be analyzed */ return $curr_size; } else { /** * An error has DEFINITELY occurred */ /** * TODO: Handle this MUCH more elegantly */ return -1; } } else { /** * An error has DEFINITELY occurred */ /** * TODO: Handle this MUCH more elegantly */ return -1; } }
/** * Test for PMA_getDecimalPrecision * * @param int $expected Expected result of the function * @param string|null $size Size of field * * @return void * * @dataProvider provGetDecimalPrecision */ function testGetDecimalPrecision($expected, $size) { $this->assertEquals($expected, PMA_getDecimalPrecision($size)); }