framework/db/sqlite/Schema.php
private function loadTableConstraints($tableName, $returnType)
    {
        $indexes = $this->db->createCommand('PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')')->queryAll();
        $indexes = $this->normalizePdoRowKeyCase($indexes, true);
        $tableColumns = null;
        if (!empty($indexes) && !isset($indexes[0]['origin'])) {
            /*
             * SQLite may not have an "origin" column in INDEX_LIST
             * See https://www.sqlite.org/src/info/2743846cdba572f6
             */
            $tableColumns = $this->loadTableColumnsInfo($tableName);
        }
        $result = [
            'primaryKey' => null,
            'indexes' => [],
            'uniques' => [],
        ];
        foreach ($indexes as $index) {
            $columns = $this->db->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($index['name']) . ')')->queryAll();
            $columns = $this->normalizePdoRowKeyCase($columns, true);
            ArrayHelper::multisort($columns, 'seqno', SORT_ASC, SORT_NUMERIC);
            if ($tableColumns !== null) {
                // SQLite may not have an "origin" column in INDEX_LIST
                $index['origin'] = 'c';
                if (!empty($columns) && $tableColumns[$columns[0]['cid']]['pk'] > 0) {
                    $index['origin'] = 'pk';
                } elseif ($index['unique'] && $this->isSystemIdentifier($index['name'])) {
                    $index['origin'] = 'u';
                }
            }
            $result['indexes'][] = new IndexConstraint([
                'isPrimary' => $index['origin'] === 'pk',
                'isUnique' => (bool) $index['unique'],
                'name' => $index['name'],
                'columnNames' => ArrayHelper::getColumn($columns, 'name'),
            ]);
            if ($index['origin'] === 'u') {
                $result['uniques'][] = new Constraint([
                    'name' => $index['name'],
                    'columnNames' => ArrayHelper::getColumn($columns, 'name'),
                ]);
            } elseif ($index['origin'] === 'pk') {
                $result['primaryKey'] = new Constraint([
                    'columnNames' => ArrayHelper::getColumn($columns, 'name'),
                ]);
            }
        }

        if ($result['primaryKey'] === null) {
            /*
             * Additional check for PK in case of INTEGER PRIMARY KEY with ROWID
             * See https://www.sqlite.org/lang_createtable.html#primkeyconst
             */
            if ($tableColumns === null) {
                $tableColumns = $this->loadTableColumnsInfo($tableName);
            }
            foreach ($tableColumns as $tableColumn) {
                if ($tableColumn['pk'] > 0) {
                    $result['primaryKey'] = new Constraint([
                        'columnNames' => [$tableColumn['name']],
                    ]);
                    break;
                }
            }
        }

        foreach ($result as $type => $data) {
            $this->setTableMetadata($tableName, $type, $data);
        }

        return $result[$returnType];
    }
private function loadTableConstraints($tableName, $returnType)
    {
        $indexes = $this->db->createCommand('PRAGMA INDEX_LIST (' . $this->quoteValue($tableName) . ')')->queryAll();
        $indexes = $this->normalizePdoRowKeyCase($indexes, true);
        $tableColumns = null;
        if (!empty($indexes) && !isset($indexes[0]['origin'])) {
            /*
             * SQLite may not have an "origin" column in INDEX_LIST
             * See https://www.sqlite.org/src/info/2743846cdba572f6
             */
            $tableColumns = $this->loadTableColumnsInfo($tableName);
        }
        $result = [
            'primaryKey' => null,
            'indexes' => [],
            'uniques' => [],
        ];
        foreach ($indexes as $index) {
            $columns = $this->db->createCommand('PRAGMA INDEX_INFO (' . $this->quoteValue($index['name']) . ')')->queryAll();
            $columns = $this->normalizePdoRowKeyCase($columns, true);
            ArrayHelper::multisort($columns, 'seqno', SORT_ASC, SORT_NUMERIC);
            if ($tableColumns !== null) {
                // SQLite may not have an "origin" column in INDEX_LIST
                $index['origin'] = 'c';
                if (!empty($columns) && $tableColumns[$columns[0]['cid']]['pk'] > 0) {
                    $index['origin'] = 'pk';
                } elseif ($index['unique'] && $this->isSystemIdentifier($index['name'])) {
                    $index['origin'] = 'u';
                }
            }
            $result['indexes'][] = new IndexConstraint([
                'isPrimary' => $index['origin'] === 'pk',
                'isUnique' => (bool) $index['unique'],
                'name' => $index['name'],
                'columnNames' => ArrayHelper::getColumn($columns, 'name'),
            ]);
            if ($index['origin'] === 'u') {
                $result['uniques'][] = new Constraint([
                    'name' => $index['name'],
                    'columnNames' => ArrayHelper::getColumn($columns, 'name'),
                ]);
            } elseif ($index['origin'] === 'pk') {
                $result['primaryKey'] = new Constraint([
                    'columnNames' => ArrayHelper::getColumn($columns, 'name'),
                ]);
            }
        }

        if ($result['primaryKey'] === null) {
            /*
             * Additional check for PK in case of INTEGER PRIMARY KEY with ROWID
             * See https://www.sqlite.org/lang_createtable.html#primkeyconst
             */
            if ($tableColumns === null) {
                $tableColumns = $this->loadTableColumnsInfo($tableName);
            }
            foreach ($tableColumns as $tableColumn) {
                if ($tableColumn['pk'] > 0) {
                    $result['primaryKey'] = new Constraint([
                        'columnNames' => [$tableColumn['name']],
                    ]);
                    break;
                }
            }
        }

        foreach ($result as $type => $data) {
            $this->setTableMetadata($tableName, $type, $data);
        }

        return $result[$returnType];
    }
private function loadTableConstraints ( $ tableName , $ returnType ) { $ indexes = $ this -> db -> createCommand ( 'PRAGMA INDEX_LIST (' . $ this -> quoteValue ( $ tableName ) . ')' ) -> queryAll ( ) ; $ indexes = $ this -> normalizePdoRowKeyCase ( $ indexes , true ) ; $ tableColumns = null ; if ( ! empty ( $ indexes ) && ! isset ( $ indexes [ 0 ] [ 'origin' ] ) ) { /*
             * SQLite may not have an "origin" column in INDEX_LIST
             * See https://www.sqlite.org/src/info/2743846cdba572f6
             */ $ tableColumns = $ this -> loadTableColumnsInfo ( $ tableName ) ; } $ result = [ 'primaryKey' => null , 'indexes' => [ ] , 'uniques' => [ ] , ] ; foreach ( $ indexes as $ index ) { $ columns = $ this -> db -> createCommand ( 'PRAGMA INDEX_INFO (' . $ this -> quoteValue ( $ index [ 'name' ] ) . ')' ) -> queryAll ( ) ; $ columns = $ this -> normalizePdoRowKeyCase ( $ columns , true ) ; ArrayHelper :: multisort ( $ columns , 'seqno' , SORT_ASC , SORT_NUMERIC ) ; if ( $ tableColumns !== null ) { // SQLite may not have an "origin" column in INDEX_LIST $ index [ 'origin' ] = 'c' ; if ( ! empty ( $ columns ) && $ tableColumns [ $ columns [ 0 ] [ 'cid' ] ] [ 'pk' ] > 0 ) { $ index [ 'origin' ] = 'pk' ; } elseif ( $ index [ 'unique' ] && $ this -> isSystemIdentifier ( $ index [ 'name' ] ) ) { $ index [ 'origin' ] = 'u' ; } } $ result [ 'indexes' ] [ ] = new IndexConstraint ( [ 'isPrimary' => $ index [ 'origin' ] === 'pk' , 'isUnique' => ( bool ) $ index [ 'unique' ] , 'name' => $ index [ 'name' ] , 'columnNames' => ArrayHelper :: getColumn ( $ columns , 'name' ) , ] ) ; if ( $ index [ 'origin' ] === 'u' ) { $ result [ 'uniques' ] [ ] = new Constraint ( [ 'name' => $ index [ 'name' ] , 'columnNames' => ArrayHelper :: getColumn ( $ columns , 'name' ) , ] ) ; } elseif ( $ index [ 'origin' ] === 'pk' ) { $ result [ 'primaryKey' ] = new Constraint ( [ 'columnNames' => ArrayHelper :: getColumn ( $ columns , 'name' ) , ] ) ; } } if ( $ result [ 'primaryKey' ] === null ) { /*
             * Additional check for PK in case of INTEGER PRIMARY KEY with ROWID
             * See https://www.sqlite.org/lang_createtable.html#primkeyconst
             */ if ( $ tableColumns === null ) { $ tableColumns = $ this -> loadTableColumnsInfo ( $ tableName ) ; } foreach ( $ tableColumns as $ tableColumn ) { if ( $ tableColumn [ 'pk' ] > 0 ) { $ result [ 'primaryKey' ] = new Constraint ( [ 'columnNames' => [ $ tableColumn [ 'name' ] ] , ] ) ; break ; } } } foreach ( $ result as $ type => $ data ) { $ this -> setTableMetadata ( $ tableName , $ type , $ data ) ; } return $ result [ $ returnType ] ; }
Loads multiple types of constraints and returns the specified ones.
@param string $tableName table name.
@param string $returnType return type:
- primaryKey
- indexes
- uniques
@return mixed constraints.
Loads multiple types of constraints and returns the specified ones .