Esempio n. 1
0
/**
 * Determines if the column types are int, decimal, or string
 *
 * @param array &$table array(string $table_name, array $col_names, array $rows)
 *
 * @return array    array(array $types, array $sizes)
 * @access  public
 *
 * @link http://wiki.phpmyadmin.net/pma/Import
 *
 * @todo    Handle the error case more elegantly
 */
function PMA_analyzeTable(&$table)
{
    /* Get number of rows in table */
    $numRows = count($table[ROWS]);
    /* Get number of columns */
    $numCols = count($table[COL_NAMES]);
    /* Current type for each column */
    $types = array();
    $sizes = array();
    /* Initialize $sizes to all 0's */
    for ($i = 0; $i < $numCols; ++$i) {
        $sizes[$i] = 0;
    }
    /* Initialize $types to NONE */
    for ($i = 0; $i < $numCols; ++$i) {
        $types[$i] = NONE;
    }
    /* If the passed array is not of the correct form, do not process it */
    if (!is_array($table) || is_array($table[TBL_NAME]) || !is_array($table[COL_NAMES]) || !is_array($table[ROWS])) {
        /**
         * TODO: Handle this better
         */
        return false;
    }
    /* Analyze each column */
    for ($i = 0; $i < $numCols; ++$i) {
        /* Analyze the column in each row */
        for ($j = 0; $j < $numRows; ++$j) {
            /* Determine type of the current cell */
            $curr_type = PMA_detectType($types[$i], $table[ROWS][$j][$i]);
            /* Determine size of the current cell */
            $sizes[$i] = PMA_detectSize($sizes[$i], $types[$i], $curr_type, $table[ROWS][$j][$i]);
            /**
             * If a type for this column has already been declared,
             * only alter it if it was a number and a varchar was found
             */
            if ($curr_type != NONE) {
                if ($curr_type == VARCHAR) {
                    $types[$i] = VARCHAR;
                } else {
                    if ($curr_type == DECIMAL) {
                        if ($types[$i] != VARCHAR) {
                            $types[$i] = DECIMAL;
                        }
                    } else {
                        if ($curr_type == BIGINT) {
                            if ($types[$i] != VARCHAR && $types[$i] != DECIMAL) {
                                $types[$i] = BIGINT;
                            }
                        } else {
                            if ($curr_type == INT) {
                                if ($types[$i] != VARCHAR && $types[$i] != DECIMAL && $types[$i] != BIGINT) {
                                    $types[$i] = INT;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /* Check to ensure that all types are valid */
    $len = count($types);
    for ($n = 0; $n < $len; ++$n) {
        if (!strcmp(NONE, $types[$n])) {
            $types[$n] = VARCHAR;
            $sizes[$n] = '10';
        }
    }
    return array($types, $sizes);
}
Esempio n. 2
0
 /**
  * Test for PMA_detectType
  *
  * @param int         $expected Expected result of the function
  * @param int|null    $type     Last cumulative column type (VARCHAR or INT or
  *                              BIGINT or DECIMAL or NONE)
  * @param string|null $cell     String representation of the cell for which a
  *                              best-fit type is to be determined
  *
  * @return void
  *
  * @dataProvider provDetectType
  */
 function testDetectType($expected, $type, $cell)
 {
     $this->assertEquals($expected, PMA_detectType($type, $cell));
 }