Returns Phinx type by SQL type

@param string $sqlTypeDef SQL type
@throws UnsupportedColumnTypeException
@return array
506735
public function getPhinxType($sqlTypeDef)
    {
        if (!preg_match('/^([\w]+)(\(([\d]+)*(,([\d]+))*\))*$/', $sqlTypeDef, $matches)) {
            throw new UnsupportedColumnTypeException('Column type "' . $sqlTypeDef . '" is not supported by SQLite.');
        } else {
            $limit = null;
            $scale = null;
            $type = $matches[1];
            if (count($matches) > 2) {
                $limit = $matches[3] ?: null;
            }
            if (count($matches) > 4) {
                $scale = $matches[5];
            }
            switch ($type) {
                case 'varchar':
                    $type = static::PHINX_TYPE_STRING;
                    if ($limit === 255) {
                        $limit = null;
                    }
                    break;
                case 'char':
                    $type = static::PHINX_TYPE_CHAR;
                    if ($limit === 255) {
                        $limit = null;
                    }
                    if ($limit === 36) {
                        $type = static::PHINX_TYPE_UUID;
                    }
                    break;
                case 'smallint':
                    $type = static::PHINX_TYPE_SMALL_INTEGER;
                    if ($limit === 11) {
                        $limit = null;
                    }
                    break;
                case 'int':
                    $type = static::PHINX_TYPE_INTEGER;
                    if ($limit === 11) {
                        $limit = null;
                    }
                    break;
                case 'bigint':
                    if ($limit === 11) {
                        $limit = null;
                    }
                    $type = static::PHINX_TYPE_BIG_INTEGER;
                    break;
                case 'blob':
                    $type = static::PHINX_TYPE_BINARY;
                    break;
            }
            if ($type === 'tinyint') {
                if ($matches[3] === 1) {
                    $type = static::PHINX_TYPE_BOOLEAN;
                    $limit = null;
                }
            }

            try {
                // Call this to check if parsed type is supported.
                $this->getSqlType($type);
            } catch (UnsupportedColumnTypeException $e) {
                $type = Literal::from($type);
            }

            return [
                'name' => $type,
                'limit' => $limit,
                'scale' => $scale
            ];
        }
    }
641568
public static int toSQLType(String strType) throws DatabaseException {
	strType = strType.toUpperCase().trim();
	if (strType.startsWith("CF_SQL_")) strType = strType.substring(7);
	if (strType.startsWith("SQL_")) strType = strType.substring(4);

	if (strType.length() > 2) {
	    char first = strType.charAt(0);
	    if (first == 'A') {
		if (strType.equals("ARRAY")) return Types.ARRAY;
	    }
	    else if (first == 'B') {
		if (strType.equals("BIGINT")) return Types.BIGINT;
		else if (strType.equals("BINARY")) return Types.BINARY;
		else if (strType.equals("BIT")) return Types.BIT;
		else if (strType.equals("BLOB")) return Types.BLOB;
		else if (strType.equals("BOOLEAN")) return Types.BOOLEAN;
		else if (strType.equals("BOOL")) return Types.BOOLEAN;
	    }
	    else if (first == 'C') {
		if (strType.equals("CLOB")) return Types.CLOB;
		else if (strType.equals("CHAR")) return Types.CHAR;
		else if (strType.equals("CLOB")) return Types.CLOB;
		else if (strType.equals("CURSOR")) return CFTypes.CURSOR;
	    }
	    else if (first == 'D') {
		if (strType.equals("DATALINK")) return Types.DATALINK;
		else if (strType.equals("DATE")) return Types.DATE;
		else if (strType.equals("DISTINCT")) return Types.DISTINCT;
		else if (strType.equals("DECIMAL")) return Types.DECIMAL;
		else if (strType.equals("DOUBLE")) return Types.DOUBLE;
	    }
	    else if (first == 'F') {
		if (strType.equals("FLOAT")) return Types.FLOAT;
	    }
	    else if (first == 'I') {
		if (strType.equals("IDSTAMP")) return CFTypes.IDSTAMP;
		else if (strType.equals("INTEGER")) return Types.INTEGER;
		else if (strType.equals("INT")) return Types.INTEGER;
	    }
	    else if (first == 'L') {
		// if(strType.equals("LONG"))return Types.INTEGER;
		if (strType.equals("LONGVARBINARY")) return Types.LONGVARBINARY;
		else if (strType.equals("LONGVARCHAR")) return Types.LONGVARCHAR;
	    }
	    else if (first == 'M') {
		if (strType.equals("MONEY")) return Types.DOUBLE;
		else if (strType.equals("MONEY4")) return Types.DOUBLE;
	    }
	    else if (first == 'N') {
		if (strType.equals("NUMERIC")) return Types.NUMERIC;
		else if (strType.equals("NUMBER")) return Types.NUMERIC;
		else if (strType.equals("NULL")) return Types.NULL;
		else if (strType.equals("NCHAR")) return Types.NCHAR;
		else if (strType.equals("NCLOB")) return Types.NCLOB;
		else if (strType.equals("NVARCHAR")) return Types.NVARCHAR;

	    }
	    else if (first == 'O') {
		if (strType.equals("OTHER")) return Types.OTHER;
		else if ("OBJECT".equals(strType)) return Types.OTHER;
	    }
	    else if (first == 'R') {
		if (strType.equals("REAL")) return Types.REAL;
		else if (strType.equals("REF")) return Types.REF;
		else if (strType.equals("REFCURSOR")) return CFTypes.CURSOR;
	    }
	    else if (first == 'S') {
		if (strType.equals("SMALLINT")) return Types.SMALLINT;
		else if (strType.equals("STRUCT")) return Types.STRUCT;
		else if (strType.equals("STRING")) return Types.VARCHAR;
		else if (strType.equals("SQLXML")) return Types.SQLXML;
	    }
	    else if (first == 'T') {
		if (strType.equals("TIME")) return Types.TIME;
		else if (strType.equals("TIMESTAMP")) return Types.TIMESTAMP;
		else if (strType.equals("TINYINT")) return Types.TINYINT;
	    }
	    else if (first == 'V') {
		if (strType.equals("VARBINARY")) return Types.VARBINARY;
		else if (strType.equals("VARCHAR")) return Types.VARCHAR;
		else if (strType.equals("VARCHAR2")) return CFTypes.VARCHAR2;
	    }
	}

	throw new DatabaseException("invalid CF SQL Type [" + strType + "]", null, null, null);
    }
977761
public function getColumnType()
    {
        if (!($type = $this->links->get('simpleType'))) {
            $type = $this->links->get('userType');
        }

        return $type;
    }
367334
public static String getSQLTypeString(int sqlType) {
        switch (sqlType) {
            case Types.ARRAY:
                return "ARRAY (" + sqlType + ')'; 

            case Types.BIGINT:
                return "BIGINT (" + sqlType + ')'; 

            case Types.BINARY:
                return "BINARY (" + sqlType + ')'; 

            case Types.BIT:
                return "BIT (" + sqlType + ')'; 

            case Types.BLOB:
                return "BLOB (" + sqlType + ')'; 

            case Types.BOOLEAN: 
                return "BOOLEAN (" + sqlType + ')'; 

            case Types.CHAR:
                return "CHAR (" + sqlType + ')'; 

            case Types.CLOB:
                return "CLOB (" + sqlType + ')'; 

            case Types.DATALINK: 
                return "DATALINK (" + sqlType + ')'; 

            case Types.DATE:
                return "DATE (" + sqlType + ')'; 

            case Types.DECIMAL:
                return "DECIMAL (" + sqlType + ')'; 

            case Types.DISTINCT:
                return "DISTINCT (" + sqlType + ')'; 

            case Types.DOUBLE:
                return "DOUBLE (" + sqlType + ')'; 

            case Types.FLOAT:
                return "FLOAT (" + sqlType + ')'; 

            case Types.INTEGER:
                return "INTEGER (" + sqlType + ')'; 

            case Types.JAVA_OBJECT:
                return "JAVA OBJECT (" + sqlType + ')'; 

            case Types.LONGNVARCHAR: 
                return "LONGNVARCHAR (" + sqlType + ')'; 

            case Types.LONGVARBINARY:
                return "LONGVARBINARY (" + sqlType + ')'; 

            case Types.LONGVARCHAR:
                return "LONGVARCHAR (" + sqlType + ')'; 

            case Types.NCHAR: 
                return "NCHAR (" + sqlType + ')'; 

            case Types.NCLOB: 
                return "NCLOB (" + sqlType + ')'; 

            case Types.NULL:
                return "NULL (" + sqlType + ')'; 

            case Types.NUMERIC:
                return "NUMERIC (" + sqlType + ')'; 

            case Types.NVARCHAR: 
                return "NVARCHAR (" + sqlType + ')'; 

            case Types.OTHER:
                return "OTHER (" + sqlType + ')'; 

            case Types.REAL:
                return "REAL (" + sqlType + ')'; 

            case Types.REF:
                return "REF (" + sqlType + ')'; 

            case Types.ROWID: 
                return "ROWID (" + sqlType + ')'; 

            case Types.SMALLINT:
                return "SMALLINT (" + sqlType + ')'; 

            case Types.SQLXML: 
                return "SQLXML (" + sqlType + ')'; 

            case Types.STRUCT:
                return "STRUCT (" + sqlType + ')'; 

            case Types.TIME:
                return "TIME (" + sqlType + ')'; 

            case Types.TIMESTAMP:
                return "TIMESTAMP (" + sqlType + ')'; 

            case Types.TINYINT:
                return "TINYINT (" + sqlType + ')'; 

            case Types.VARBINARY:
                return "VARBINARY (" + sqlType + ')'; 

            case Types.VARCHAR:
                return "VARCHAR (" + sqlType + ')'; 
        }

        return "UNKNOWN SQL TYPE (" + sqlType + ')'; 
    }
20868
protected function getDefaultValueDefinition($default, $columnType = null)
    {
        if (is_string($default) && 'CURRENT_TIMESTAMP' !== $default) {
            $default = $this->getConnection()->quote($default);
        } elseif (is_bool($default)) {
            $default = $this->castToBool($default);
        } elseif ($columnType === static::PHINX_TYPE_BOOLEAN) {
            $default = $this->castToBool((bool)$default);
        }

        return isset($default) ? 'DEFAULT ' . $default : '';
    }
626339
public function getPhinxType($sqlTypeDef)
    {
        $matches = [];
        if (!preg_match('/^([\w]+)(\(([\d]+)*(,([\d]+))*\))*(.+)*$/', $sqlTypeDef, $matches)) {
            throw new \RuntimeException('Column type ' . $sqlTypeDef . ' is not supported');
        }

        $limit = null;
        $precision = null;
        $type = $matches[1];
        if (count($matches) > 2) {
            $limit = $matches[3] ? (int) $matches[3] : null;
        }
        if (count($matches) > 4) {
            $precision = (int) $matches[5];
        }
        if ($type === 'tinyint' && $limit === 1) {
            $type = static::PHINX_TYPE_BOOLEAN;
            $limit = null;
        }
        switch ($type) {
            case 'varchar':
                $type = static::PHINX_TYPE_STRING;
                if ($limit === 255) {
                    $limit = null;
                }
                break;
            case 'char':
                $type = static::PHINX_TYPE_CHAR;
                if ($limit === 255) {
                    $limit = null;
                }
                if ($limit === 36) {
                    $type = static::PHINX_TYPE_UUID;
                }
                break;
            case 'tinyint':
                $type = static::PHINX_TYPE_INTEGER;
                $limit = static::INT_TINY;
                break;
            case 'smallint':
                $type = static::PHINX_TYPE_INTEGER;
                $limit = static::INT_SMALL;
                break;
            case 'mediumint':
                $type = static::PHINX_TYPE_INTEGER;
                $limit = static::INT_MEDIUM;
                break;
            case 'int':
                $type = static::PHINX_TYPE_INTEGER;
                if ($limit === 11) {
                    $limit = null;
                }
                break;
            case 'bigint':
                if ($limit === 20) {
                    $limit = null;
                }
                $type = static::PHINX_TYPE_BIG_INTEGER;
                break;
            case 'blob':
                $type = static::PHINX_TYPE_BINARY;
                break;
            case 'tinyblob':
                $type = static::PHINX_TYPE_BINARY;
                $limit = static::BLOB_TINY;
                break;
            case 'mediumblob':
                $type = static::PHINX_TYPE_BINARY;
                $limit = static::BLOB_MEDIUM;
                break;
            case 'longblob':
                $type = static::PHINX_TYPE_BINARY;
                $limit = static::BLOB_LONG;
                break;
            case 'tinytext':
                $type = static::PHINX_TYPE_TEXT;
                $limit = static::TEXT_TINY;
                break;
            case 'mediumtext':
                $type = static::PHINX_TYPE_TEXT;
                $limit = static::TEXT_MEDIUM;
                break;
            case 'longtext':
                $type = static::PHINX_TYPE_TEXT;
                $limit = static::TEXT_LONG;
                break;
        }

        $this->getSqlType($type, $limit);

        $phinxType = [
            'name' => $type,
            'limit' => $limit,
            'precision' => $precision,
        ];

        if (static::PHINX_TYPE_ENUM == $type) {
            $phinxType['values'] = explode("','", trim($matches[6], "()'"));
        }

        return $phinxType;
    }
226082
private function getStandardTypes(): array
    {
        $standard = [
            // Scalars
            BooleanType::class,
            FloatType::class,
            IDType::class,
            IntType::class,
            StringType::class,
            AnyType::class,
            DateTimeType::class,

            // Directives
            Deprecation::class,
        ];

        return \array_merge($standard, $this->additionalTypes);
    }
977608
private function getTableColumnTypes($tableName)
    {
        $columnTypes = array();
        $columns = $this->dbHandler->query(
            $this->typeAdapter->show_columns($tableName)
        );
        $columns->setFetchMode(PDO::FETCH_ASSOC);

        foreach ($columns as $key => $col) {
            $types = $this->typeAdapter->parseColumnType($col);
            $columnTypes[$col['Field']] = array(
                'is_numeric'=> $types['is_numeric'],
                'is_blob' => $types['is_blob'],
                'type' => $types['type'],
                'type_sql' => $col['Type'],
                'is_virtual' => $types['is_virtual']
            );
        }

        return $columnTypes;
    }
607879
public function column( $type )
    {
        $classPrefix = __CLASS__ . '\\Type\\';

        if ( is_string( $type ) &&
             class_exists( $classPrefix . ucfirst( $type ) ) )
        {
            $type = ucfirst( $type );
            $args = func_get_args();
            array_shift( $args );
        }
        else if ( is_callable( $type ) )
        {
            $args = func_get_args();
            $type = static::CALLBACK;
        }
        else
        {
            $type = ucfirst( (string) $type );
            $args = func_get_args();
            array_shift( $args );
        }

        $class = $classPrefix . $type;

        if ( ! class_exists( $class ) )
        {
            throw new \LogicException( 'RowSet-type does not exists: ' . $class );
        }

        $reflection = new \ReflectionClass( $class );
        $result     = $reflection->newInstanceArgs( $args );

        if ( $result instanceof HelperInterface )
        {
            $result->setView( $this->getView() );
        }

        return $result;
    }
288186
protected function getColumnSqlDefinition(Column $column)
    {
        $buffer = [];
        if ($column->isIdentity()) {
            $buffer[] = $column->getType() === 'biginteger' ? 'BIGSERIAL' : 'SERIAL';
        } else {
            $sqlType = $this->getSqlType($column->getType(), $column->getLimit());
            $buffer[] = \strtoupper($sqlType['name']);
            // integers cant have limits in postgres
            if (static::PHINX_TYPE_DECIMAL === $sqlType['name'] && ($column->getPrecision() || $column->getScale())) {
                $buffer[] = \sprintf(
                    '(%s, %s)',
                    $column->getPrecision() ?: $sqlType['precision'],
                    $column->getScale() ?: $sqlType['scale']
                );
            } elseif ($sqlType['name'] === 'geography') {
                // geography type must be written with geometry type and srid, like this: geography(POLYGON,4326)
                $buffer[] = \sprintf(
                    '(%s,%s)',
                    \strtoupper($sqlType['type']),
                    $sqlType['srid']
                );
            } elseif (!\in_array($sqlType['name'], ['integer', 'smallint', 'bigint'], true)) {
                if ($column->getLimit() || isset($sqlType['limit'])) {
                    $buffer[] = sprintf('(%s)', $column->getLimit() ?: $sqlType['limit']);
                }
            }

            $timeTypes = [
                'time',
                'timestamp',
            ];
            if (in_array($sqlType['name'], $timeTypes) && $column->isTimezone()) {
                $buffer[] = strtoupper('with time zone');
            }
        }

        $buffer[] = $column->isNull() ? 'NULL' : 'NOT NULL';

        if (null !== $column->getDefault()) {
            $buffer[] = $this->getDefaultValueDefinition($column->getDefault());
        }

        return implode(' ', $buffer);
    }
335456
public function getColumnType($tableName, $columnName) {
        $columns = $this->getTableColumns($tableName);
        foreach ($columns as $column) {
            if ($column['Field'] != $columnName)
                continue;
            $type = explode('(', $column['Type'], 2);
            return $type[0];
        }
        throw new \Exception("Column `$columnName` not found in table `$tableName`!");
    }
20117
protected function _getSQLByType($iType)
    {
        $sSQLAdd = '';
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
        $sSQLType = " AND `oxtype` = " . $oDb->quote($iType);

        if ($iType == self::TYPE_CATEGORY_MENU) {
            $sSQLAdd = " AND `oxcatid` IS NOT NULL AND `oxsnippet` = '0'";
        }

        if ($iType == self::TYPE_SERVICE_LIST) {
            $sIdents = implode(", ", \OxidEsales\Eshop\Core\DatabaseProvider::getDb()->quoteArray($this->getServiceKeys()));
            $sSQLAdd = " AND OXLOADID IN (" . $sIdents . ")";
            $sSQLType = '';
        }
        $sViewName = $this->getBaseObject()->getViewName();
        $sSql = "SELECT * FROM {$sViewName} WHERE `oxactive` = '1' $sSQLType AND `oxshopid` = " . $oDb->quote($this->_sShopID) . " $sSQLAdd ORDER BY `oxloadid`";

        return $sSql;
    }
913586
function serializeTypeDef($type)
    {
        //print "in sTD() for type $type<br>";
        if ($typeDef = $this->getTypeDef($type)) {
            $str .= '<' . $type;
            if (is_array($typeDef['attrs'])) {
                foreach ($typeDef['attrs'] as $attName => $data) {
                    $str .= " $attName=\"{type = " . $data['type'] . "}\"";
                }
            }
            $str .= " xmlns=\"" . $this->schema['targetNamespace'] . "\"";
            if (count($typeDef['elements']) > 0) {
                $str .= ">";
                foreach ($typeDef['elements'] as $element => $eData) {
                    $str .= $this->serializeTypeDef($element);
                }
                $str .= "</$type>";
            } elseif ($typeDef['typeClass'] == 'element') {
                $str .= "></$type>";
            } else {
                $str .= "/>";
            }

            return $str;
        }

        return FALSE;
    }
1248272
public Class<?> getJavaType(int sqlType, String typeName, int size, int digits, String tableName, String columnName) {
        // table.column mapped class
        Type<?> type = javaTypeMapping.getType(tableName, columnName);
        if (type != null) {
            return type.getReturnedClass();
        } else if (typeName != null && !typeName.isEmpty()) {
            typeName = typeName.toLowerCase();
            // typename mapped class
            Class<?> clazz = typeToName.get(typeName);
            if (clazz != null) {
                return clazz;
            }
            if (sqlType == Types.ARRAY) {
                if (typeName.startsWith("_")) {
                    typeName = typeName.substring(1);
                } else if (typeName.endsWith(" array")) {
                    typeName = typeName.substring(0, typeName.length() - 6);
                }
                if (typeName.contains("[")) {
                    typeName = typeName.substring(0, typeName.indexOf("["));
                }
                if (typeName.contains("(")) {
                    typeName = typeName.substring(0, typeName.indexOf("("));
                }

                Integer sqlComponentType = templates.getCodeForTypeName(typeName);
                if (sqlComponentType == null) {
                    logger.warn("Found no JDBC type for " + typeName + " using OTHER instead");
                    sqlComponentType = Types.OTHER;
                }
                Class<?> componentType = jdbcTypeMapping.get(sqlComponentType, size, digits);
                return Array.newInstance(componentType, 0).getClass();
            }
        }
        // sql type mapped class
        return jdbcTypeMapping.get(sqlType, size, digits);
    }
1135753
public function getXMLDBFieldType($type) {

        $result = XMLDB_TYPE_INCORRECT;

        switch (strtolower($type)) {
            case 'int':
                $result = XMLDB_TYPE_INTEGER;
                break;
            case 'number':
                $result = XMLDB_TYPE_NUMBER;
                break;
            case 'float':
                $result = XMLDB_TYPE_FLOAT;
                break;
            case 'char':
                $result = XMLDB_TYPE_CHAR;
                break;
            case 'text':
                $result = XMLDB_TYPE_TEXT;
                break;
            case 'binary':
                $result = XMLDB_TYPE_BINARY;
                break;
            case 'datetime':
                $result = XMLDB_TYPE_DATETIME;
                break;
        }
        // Return the normalized XMLDB_TYPE
        return $result;
    }
276553
protected function getMySQLColumnType($columnData)
    {
        $type = $columnData['COLUMN_TYPE'];
        $pattern = '/^[a-z]+/';
        $match = null;
        preg_match($pattern, $type, $match);

        return $match[0];
    }
1267177
public function getColumnType($type)
    {
        if (array_key_exists($type, $this->typeMap))
        {
            return $this->typeMap[$type];
        }

        return $this->defaultType;
    }