Obtains DBMS specific SQL code portion needed to set an index
declaration to be used in statements like CREATE TABLE.

@param mixed[]|Index $columnsOrIndex array declaration is deprecated, prefer passing Index to this method
763082
public function getIndexFieldDeclarationListSQL($columnsOrIndex) : string
    {
        if ($columnsOrIndex instanceof Index) {
            return implode(', ', $columnsOrIndex->getQuotedColumns($this));
        }

        if (! is_array($columnsOrIndex)) {
            throw new InvalidArgumentException('Fields argument should be an Index or array.');
        }

        $ret = [];

        foreach ($columnsOrIndex as $column => $definition) {
            if (is_array($definition)) {
                $ret[] = $column;
            } else {
                $ret[] = $definition;
            }
        }

        return implode(', ', $ret);
    }
885319
protected function makeIndexedColumnsDDL()
    {
        $columnDDLs = [];

        foreach ($this->indexedColumns as $indexedColumn) {
            $columnDDLs[] = '`' . $indexedColumn->getColumnName() . '`'
                . ($indexedColumn->hasLength() ? '(' . $indexedColumn->getLength() . ')' : null)
                . ($indexedColumn->hasCollation() ? ' ' . strtoupper($indexedColumn->getCollation()) : null);
        }

        return '(' . implode(',', $columnDDLs) . ')';
    }
643735
final public function setSqlType(string $sqlType = null) : ColumnInterface
    {
        if ($sqlType) {
            $available = [
                self::SQL_TYPE_BOOLEAN,
                self::SQL_TYPE_DOUBLE,
                self::SQL_TYPE_FLOAT,
                self::SQL_TYPE_INT,
                self::SQL_TYPE_INTEGER,
                self::SQL_TYPE_STRING,
            ];

            if (!in_array($sqlType, $available)) {
                if (!class_exists($sqlType)) {
                    throw new \InvalidArgumentException('Invalid SQL type: '.$sqlType);
                } else {
                    $reflection = new \ReflectionClass($sqlType);
                    if (!$reflection->isSubclassOf(Behavior::class)) {
                        throw new \InvalidArgumentException('SQL type '.$sqlType.' must be instance of '.Behavior::class);
                    }
                }
            }
        }

        $this->sqlType = $sqlType;

        return $this;
    }
85713
function adjustColumnIndex( $columnIndex, $newColumnIndex )
    {
        $matrix = $this->attribute( 'matrix' );
        $columnDefinition = $matrix['columns']['sequential'][$columnIndex];
        if ( $columnDefinition['index'] != $newColumnIndex )
        {
            $this->setColumnIndex( $columnIndex, $newColumnIndex );
            return true;
        }
        return false;
    }
63566
public function getAddIndexDDL(Index $index)
    {
        $pattern = "
CREATE %sINDEX %s ON %s (%s);
";

        return sprintf($pattern,
            $this->getIndexType($index),
            $this->quoteIdentifier($index->getName()),
            $this->quoteIdentifier($index->getTable()->getName()),
            $this->getColumnListDDL($index->getColumns())
        );
    }
867763
public function getCreateIndexSQLQueries(Index $index, $table)
    {
        if ($index->isUnique()) {
            return array($this->getAlterTableSQLQuery($table, 'ADD', $this->getIndexSQLDeclaration($index)));
        }

        if (!$this->supportIndexes()) {
            throw PlatformException::methodNotSupported(__METHOD__);
        }

        return array('CREATE INDEX '.$index->getName().' ON '.$table.' ('.implode(', ', $index->getColumnNames()).')');
    }
115575
public static function createTable(
        $name,
        $columns,
        $pks = [],
        $keys = [],
        $inherits = null,
        $ifNotExists = true,
        $tablespace = null
    ) {
        $query = static::getQuery();
        $cols = [];

        foreach ($columns as $cName => $details) {
            $details = (array) $details;

            array_unshift($details, $query->quoteName($cName));

            $cols[] = call_user_func_array([get_called_class(), 'build'], $details);
        }

        if (!is_array($keys)) {
            throw new \InvalidArgumentException('Keys should be an array');
        }

        if ($pks) {
            $cols[] = 'PRIMARY KEY ' . static::buildIndexDeclare(null, (array) $pks, null);
        }

        $indexes = [];

        foreach ($keys as $key) {
            $define = [
                'type' => 'INDEX',
                'name' => null,
                'columns' => [],
                'comment' => '',
            ];

            if (!is_array($key)) {
                throw new \InvalidArgumentException('Every key data should be an array with "type", "name", "columns"');
            }

            $define = array_merge($define, $key);

            $indexes[] = 'CREATE ' . $define['type'] . ' ' . static::buildIndexDeclare(
                $define['name'],
                $define['columns'],
                $name
            );
        }

        $indexes = implode(";\n", $indexes);

        $cols = "(\n" . implode(",\n", $cols) . "\n)";

        return static::build(
            'CREATE TABLE',
            $ifNotExists ? 'IF NOT EXISTS' : null,
            $query->quoteName($name),
            $cols,
            $inherits ? 'INHERITS (' . implode(',', $query->quoteName((array) $inherits)) . ')' : null,
            $tablespace ? 'TABLESPACE ' . $query->quoteName($tablespace) : null,
            ";\n",
            $indexes ? $indexes : null
        );
    }
242989
public function addIndex($type, ...$columns) {
        if (empty($columns)) {
            throw new \InvalidArgumentException("An index must contain at least one column.", 500);
        }

        $type = strtolower($type);

        // Look for a current index row.
        $currentIndex = null;
        foreach ($this->indexes as $i => $index) {
            if ($type !== $index['type']) {
                continue;
            }

            if ($type === Db::INDEX_PK || array_diff($index['columns'], $columns) == []) {
                $currentIndex =& $this->indexes[$i];
                break;
            }
        }

        if ($currentIndex) {
            $currentIndex['columns'] = $columns;
        } else {
            $indexDef = [
                'type' => $type,
                'columns' => $columns,
            ];
            $this->indexes[] = $indexDef;
        }

        return $this;
    }
622456
public function getColumnByIndex($index)
    {
        $keys = array_keys($this->columns);
        if (empty($keys[$index]) || empty($column = $this->columns[$keys[$index]])) {
            throw new \InvalidArgumentException('Table don\'t have a column index : ' . $index);
        }

        return $column;
    }
1133438
public function index(string $indexName): IndexColumnsDefiner
    {
        return new IndexColumnsDefiner(function (array $columnNames) use ($indexName) {
            $this->indexes[] = new Index($this->orm->getNamespace() . $indexName, false, $columnNames);
        });
    }
202224
public function createIndex(array $columnNames, $unique = false, $name = null)
    {
        $index = new Index($name, $columnNames, $unique);
        $this->addIndex($index);

        return $index;
    }
891898
protected function getColumnNameByIndex($index)
    {
        $name = (isset($this->columns[$index]) && $this->columns[$index] != '*')
            ? $this->columns[$index] : $this->getPrimaryKeyName();

        return in_array($name, $this->extraColumns, true) ? $this->getPrimaryKeyName() : $name;
    }
268187
public function getParameter($nameOrIndex): PhpParameter {
		if (is_int($nameOrIndex)) {
			$this->checkPosition($nameOrIndex);
			return $this->parameters[$nameOrIndex];
		}

		foreach ($this->parameters as $param) {
			if ($param->getName() === $nameOrIndex) {
				return $param;
			}
		}

		throw new \InvalidArgumentException(sprintf('There is no parameter named "%s".', $nameOrIndex));
	}
390127
protected function getColumnsSQLDeclaration(array $columns)
    {
        $columnsDeclaration = array();

        foreach ($columns as $column) {
            $columnsDeclaration[] = $this->getColumnSQLDeclaration($column);
        }

        return implode(', ', $columnsDeclaration);
    }
832008
protected function _getFinalSql($tablePrefix, $repeat = false)
    {
        $db = \Zend_Registry::getInstance()->get('db');

        $sqlSuffix = $db->quoteIdentifier($tablePrefix . '_changed') . " timestamp NOT NULL,\n"
            . $db->quoteIdentifier($tablePrefix . '_changed_by') . " bigint(20) NOT NULL,\n"
            . $db->quoteIdentifier($tablePrefix . '_created') . " timestamp NOT NULL,\n"
            . $db->quoteIdentifier($tablePrefix . '_created_by') . " bigint(20) NOT NULL,\n";
        if ($repeat) {
            $sqlSuffix .= 'PRIMARY KEY  (`' . $tablePrefix . '_id`, `' . $tablePrefix . '_response_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
        } else {
            $sqlSuffix .= 'PRIMARY KEY  (`' . $tablePrefix . '_id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
        }

        return $sqlSuffix;
    }
106282
protected function getSelectColumns( $columns = null )
    {
        if ( null === $columns )
        {
            $label = true;
        }
        elseif ( ( $index = array_search( 'label', $columns ) ) )
        {
            $label = true;
            unset( $columns[$index] );
        }
        else
        {
            $label = false;
        }

        if ( null === $columns )
        {
            $proxyData = true;
        }
        elseif ( ( $index = array_search( 'proxyData', $columns ) ) )
        {
            $proxyData = true;
            unset( $columns[$index] );
        }
        else
        {
            $proxyData = false;
        }

        if ( null === $columns )
        {
            $level = false;
        }
        elseif ( ( $index = array_search( 'level', $columns ) ) )
        {
            $level = true;
            unset( $columns[$index] );
        }
        else
        {
            $level = false;
        }

        $columns = parent::getSelectColumns( $columns );

        if ( $label )
        {
            $platform = $this->getDbAdapter()
                             ->getPlatform();

            $columns['label'] = new Sql\Expression( '(' .
                $this->sql( $this->getTableInSchema(
                        static::$labelTableName
                     ) )
                     ->select()
                     ->columns( array( 'label' ) )
                     ->where( array(
                         new Sql\Predicate\Expression(
                             $platform->quoteIdentifierChain( array(
                                 static::$labelTableName, 'menuId'
                             ) ) .
                             ' = ' .
                             $platform->quoteIdentifierChain( array(
                                 static::$tableName, 'id'
                             ) )
                         )
                     ) )
                     ->order( array(
                         new Sql\Expression(
                             'CASE ? ' .
                                 'WHEN ? THEN 1 ' .
                                 'WHEN ? THEN 2 ' .
                                 'WHEN ? THEN 3 ' .
                                 'WHEN ? THEN 4 ' .
                                 'ELSE 5 ' .
                             'END ASC',
                             array(
                                 'locale',
                                 $this->getLocale(),
                                 $this->getPrimaryLanguage(),
                                 $this->getDefaultLocale(),
                                 'en',
                             ),
                             array(
                                 Sql\Expression::TYPE_IDENTIFIER,
                                 Sql\Expression::TYPE_VALUE,
                                 Sql\Expression::TYPE_VALUE,
                                 Sql\Expression::TYPE_VALUE,
                                 Sql\Expression::TYPE_VALUE,
                             )
                         ),
                     ) )
                     ->limit( 1 )
                     ->getSqlString( $platform ) .
            ')' );
        }

        if ( $proxyData )
        {
            $platform = $this->getDbAdapter()
                             ->getPlatform();

            $columns['proxyData'] = new Sql\Expression( '(' .
                $this->sql( $this->getTableInSchema(
                        static::$propertyTableName
                     ) )
                     ->select()
                     ->columns( array(
                         new Sql\Expression( 'TEXT( ARRAY_TO_JSON(
                             ARRAY_AGG( ? ORDER BY ? ASC )
                         ) )', array(
                             static::$propertyTableName,
                             'name',
                         ), array(
                             Sql\Expression::TYPE_IDENTIFIER,
                             Sql\Expression::TYPE_IDENTIFIER,
                         ) )
                     ) )
                     ->where( array(
                         new Sql\Predicate\Expression(
                             $platform->quoteIdentifierChain( array(
                                 static::$propertyTableName, 'menuId'
                             ) ) .
                             ' = ' .
                             $platform->quoteIdentifierChain( array(
                                 static::$tableName, 'id'
                             ) )
                         )
                     ) )
                     ->getSqlString( $platform ) .
            ')' );
        }

        if ( $level )
        {
            $columns['level'] = new Sql\Expression( '(' .
                $this->sql( null )
                     ->select( array(
                         'ascendants' => static::$tableName,
                     ) )
                     ->columns( array( new Sql\Expression( 'COUNT(*) - 1' ) ) )
                     ->where( array(
                         new Sql\Predicate\Expression(
                             $platform->quoteIdentifierChain( array(
                                 static::$tableName, 'left'
                             ) ) .
                             ' BETWEEN ' .
                             $platform->quoteIdentifierChain( array(
                                 'ascendants', 'left'
                             ) ) .
                             ' AND ' .
                             $platform->quoteIdentifierChain( array(
                                 'ascendants', 'right'
                             ) )
                         )
                     ) )
                     ->getSqlString( $platform ) .
            ')' );
        }

        return $columns;
    }
659893
private function getColumnAlias($columnName, $mode, array $customRenameColumns)
    {
        switch ($mode) {
            case self::COLUMN_RENAMING_INCREMENT:
                return $columnName . $this->sqlCounter++;

            case self::COLUMN_RENAMING_CUSTOM:
                return isset($customRenameColumns[$columnName])
                    ? $customRenameColumns[$columnName] : $columnName;

            case self::COLUMN_RENAMING_NONE:
                return $columnName;

        }
    }