Gets foreign key constraints info from the information schema.

@return array
778295
protected function getConstraints()
    {
        if (is_null($this->constrains)) {
            $this->constrains = DB::select(
                'SELECT
                    r.referenced_table_name AS `references`
                   ,r.CONSTRAINT_NAME AS `name`
                   ,r.UPDATE_RULE AS `onUpdate`
                   ,r.DELETE_RULE AS `onDelete`
                   ,u.referenced_column_name AS `on`
                   ,u.column_name AS `foreign`
                   ,CASE WHEN u.TABLE_NAME = r.referenced_table_name THEN 1 ELSE 0 END selfReferences
                   FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS r
                   INNER JOIN information_schema.key_column_usage AS u ON u.CONSTRAINT_NAME = r.CONSTRAINT_NAME
                                                                       AND u.table_schema = r.constraint_schema
                                                                       AND u.table_name = r.table_name
                   WHERE u.table_name = ? AND u.constraint_schema = ?;',
                [$this->tableName, $this->databaseName]
            );
        }

        return $this->constrains;
    }
279740
public function getRawAttributes(): array
    {
        $attributes = $this->wrappedEntity->getEntityAttributes();

        foreach ($this->entityMap->getNonEmbeddedRelationships() as $relation) {
            unset($attributes[$relation]);
        }

        if ($this->entityMap->getInheritanceType() == 'single_table') {
            $attributes = $this->addDiscriminatorColumn($attributes);
        }

        $attributes = $this->entityMap->getColumnNamesFromAttributes($attributes);

        $attributes = $this->flattenEmbeddables($attributes);

        $foreignKeys = $this->getForeignKeyAttributes();

        return $this->mergeForeignKeysWithAttributes($foreignKeys, $attributes);
    }
850613
public function getDefAsArray()
    {
        return array_replace(
            [
                'name'              => $this->getName(),
                'type'              => $this->getType(),
                'exposedName'       => $this->getName(),
                'doctrineType'      => $this->getTypeDoctrine(),
                'serializerType'    => $this->getTypeSerializer(),
                'relType'           => self::REL_TYPE_EMBED,
                'isClassType'       => true,
                'constraints'       => [],
                'required'          => false,
                'searchable'        => 0,
            ],
            $this->definition === null ? [
                'required'          => $this->isRequired()
            ] : [
                'exposedName'       => $this->definition->getExposeAs() ?: $this->getName(),
                'title'             => $this->definition->getTitle(),
                'description'       => $this->definition->getDescription(),
                'readOnly'          => $this->definition->getReadOnly(),
                'required'          => $this->definition->getRequired(),
                'searchable'        => $this->definition->getSearchable(),
                'constraints'       => array_map(
                    [Utils\ConstraintNormalizer::class, 'normalize'],
                    $this->definition->getConstraints()
                ),
            ]
        );
    }
467878
private function getForeignKeyColumns()
    {
        $foreignKeyColumns = [];
        foreach ($this->getForeignKeys() as $foreignKey) {
            $foreignKeyColumns = array_merge($foreignKeyColumns, $foreignKey->getColumns());
        }

        return $this->filterColumns($foreignKeyColumns);
    }
319688
protected function get_indexing_restrictions() {
        global $DB;

        // This includes completely empty configdata, and also three other values that are
        // equivalent to empty:
        // - A serialized completely empty object.
        // - A serialized object with one field called '0' (string not int) set to boolean false
        //   (this can happen after backup and restore, at least historically).
        // - A serialized null.
        $stupidobject = (object)[];
        $zero = '0';
        $stupidobject->{$zero} = false;
        return [$DB->sql_compare_text('bi.configdata') . " != ? AND " .
                $DB->sql_compare_text('bi.configdata') . " != ? AND " .
                $DB->sql_compare_text('bi.configdata') . " != ? AND " .
                $DB->sql_compare_text('bi.configdata') . " != ?",
                ['', base64_encode(serialize((object)[])), base64_encode(serialize($stupidobject)),
                base64_encode(serialize(null))]];
    }
1120735
public function getSchemaDataDefaults()
    {
        $data = parent::getSchemaDataDefaults();

        $sourceRecord = $this->getSourceRecord();

        $data['data'] = array_merge($data['data'], [
            'recordId' => $sourceRecord ? $sourceRecord->ID : null,
            'recordClass' => $sourceRecord ? $sourceRecord->ClassName : null,
            'contextKey' => $this->getContextKey(),
            'isPreviewable' => $this->getPreviewEnabled(),
            'limit' => $this->config()->get('default_page_size'),
            'offset' => 0,
            'page' => 0,
        ]);

        return $data;
    }
115409
public function readableInfo() {
        $o = '';
        // type
        $o .= $this->getXMLDBKeyName($this->type);
        // fields
        $o .= ' (' . implode(', ', $this->fields) . ')';
        // foreign key
        if ($this->type == XMLDB_KEY_FOREIGN ||
            $this->type == XMLDB_KEY_FOREIGN_UNIQUE) {
            $o .= ' references ' . $this->reftable . ' (' . implode(', ', $this->reffields) . ')';
        }

        return $o;
    }
706286
public function getAttributes()
    {
        return [
            Attributes\IdAttribute::make(),
            Attributes\TextAttribute::make('name')
                ->setRequired(true)
                ->setUnique(true),
            Attributes\LongTextAttribute::make('description'),
            Attributes\BelongsToAttribute::make('data_builder_id')
                ->setRelationName('data_builder')
                ->setRelationManager(DataBuilderManager::class),
            Attributes\YamlAttribute::make('payload')->setDefault(function (EntityContract $entity) {
                return file_get_contents(__DIR__.'/../../resources/schema/default/payload.yaml');
            }),
            Attributes\YamlAttribute::make('data')->setDefault(function (EntityContract $entity) {
                return file_get_contents(__DIR__.'/../../resources/schema/default/data.yaml');
            }),
            Attributes\CreatedAtAttribute::make(),
            Attributes\UpdatedAtAttribute::make(),
            Attributes\DeletedAtAttribute::make(),
        ];
    }
470935
protected function _parseConstraints()
    {
        $parts = [];
        foreach ($this->_sql->getConstraints() as $constraint) {
            $cons = $this->_parseConstraint($constraint);
            if ($cons) {
                $parts[] = $cons;
            }
        }
        return implode(', ', $parts);
    }
725376
protected function _droppedConstraintsStatement()
    {
        $columns = $this->_sql->getDroppedConstraints();
        if (empty($columns)) {
            return null;
        }

        $commands = [];
        foreach ($columns as $constraint) {
            if ($constraint instanceof Primary) {
                $commands[] = 'DROP PRIMARY KEY';
            }
            if ($constraint instanceof ForeignKey) {
                $commands[] = 'DROP FOREIGN KEY '. $constraint->getName();
            }
            if ($constraint instanceof Unique) {
                $commands[] = 'DROP INDEX '. $constraint->getName();
            }
        }

        return implode(', ', $commands);
    }
792680
private function fetchReferences() {
        $qry = "SELECT i.CONSTRAINT_NAME as constraintName, i.CONSTRAINT_TYPE as constraintType,
			j.COLUMN_NAME as columnName, j.REFERENCED_TABLE_SCHEMA as refDB, j.REFERENCED_TABLE_NAME as refTable,
			j.REFERENCED_COLUMN_NAME as refColumn, k.UPDATE_RULE as onUpdate, k.DELETE_RULE as onDelete" .
                " FROM information_schema.TABLE_CONSTRAINTS i" .
                " LEFT JOIN information_schema.KEY_COLUMN_USAGE j
                    ON i.CONSTRAINT_NAME = j.CONSTRAINT_NAME AND j.TABLE_SCHEMA = '" . $this->connection->getDBName() . "'
			AND j.TABLE_NAME = '" . $this->name . "'" .
                " LEFT JOIN information_schema.REFERENTIAL_CONSTRAINTS k
                    ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME AND j.CONSTRAINT_SCHEMA = k.CONSTRAINT_SCHEMA
			AND k.TABLE_NAME = '" . $this->name . "'" .
                " WHERE i.TABLE_SCHEMA = '" . $this->connection->getDBName() . "'
								AND i.TABLE_NAME = '" . $this->name . "'";

        $define = $this->connection->doPrepare($qry);
        if (is_bool($define)) return;

        foreach ($define as $info) {
            if (isset($info['constraintType']) && $info['constraintType'] === 'PRIMARY KEY') {
                if (isset($info['columnName']))
                        $this->primaryKey = $info['columnName'];
            } else if ($info['refTable']) {
                if (isset($info['constraintType']))
                        unset($info['constraintType']);
                if (isset($info['columnName'])) {
                    $name = $info['columnName'];
                    unset($info['columnName']);
                }
                $this->references[$name] = $info;
            }
        }
    }
697643
protected function getAllColumnsForSelect() {
        $with = is_array($this->with) ? $this->with : ($this->with ? explode(',', $this->with) : array());
        if (!count($with))
            return ['*'];
        $model = $this->model;
        $db = $model::getDb();
        $columns = $this->_columnsForModel($model::getTableName(), $db);
        foreach ($this->relationsParser->getListOfSelectedRelations() as $name=>$relation){
            $cols = $this->_columnsForRelation($relation, $db, $name);
            foreach ($cols as $column) {
                $columns[] = $column;
            }
        }
        return $columns;
    }
359783
public function getExtraFormConstraints()
    {
        if (null === $this->extraFormType) {
            return null;
        }

        $configurationArray = json_decode($this->configuration, true);

        return $configurationArray['extra_form_constraints'];
    }
1068547
public function getCreates()
    {
        if ($this->tablesCreate === null) {
            return [];
        }

        $platform = $this->connection->getDatabasePlatform();
        $flags = AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS;
        $queries = [];

        foreach ($this->tablesCreate as $tableName => $table) {
            $queries[$tableName] = $platform->getCreateTableSQL($table, $flags);
        }

        return $queries;
    }
684172
protected function getTableIndexes(): array
    {
        $data = [];

        $idxs = $this->db->schema->getTableIndexes($this->tableName, true);

        /* @var $idx IndexConstraint */
        foreach ($idxs as $idx) {
            if (!$idx->isPrimary) {
                $data[$idx->name] = new TableIndex([
                    'name' => $idx->name,
                    'unique' => $idx->isUnique,
                    'columns' => $idx->columnNames
                ]);
            }
        }

        return $data;
    }
892663
public function getRelatedIds()
    {
        $related = $this->getRelated();

        $fullKey = $related->getQualifiedKeyName();

        return $this->getQuery()->select($fullKey)->lists($related->getKeyName());
    }
767280
private function getPropertiesInfo()
    {
        if (!$this->_automatedBehaviorInitialized) {
            $classInfo = Reader::getClassInformation($this);

            $this->_accessProperties = $classInfo['accessProperties'];
            $this->_collectionsItemNames = $classInfo['collectionsItemNames'];
            $this->_associationsList = $classInfo['associationsList'];
            $this->_initialPropertiesValues = $classInfo['initialPropertiesValues'];
            $this->_initializationNeededArguments = $classInfo['initializationNeededArguments'];

            if ($this->_constraintsValidationEnabled === null) {
                $this->_constraintsValidationEnabled = $classInfo['constraintsValidationEnabled'];
            }

            $this->_automatedBehaviorInitialized = true;
        }
    }