@param Connection   $connection
@param QueryBuilder $parentQueryBuilder

@return QueryBuilder
335165
private function createBelongingToOtherEntitiesQueryBuilder(
        Connection $connection,
        QueryBuilder $parentQueryBuilder
    ) {
        $revisionFieldName = $this->configuration->getRevisionFieldName();
        $identifierColumnNames = $this->metadata->getIdentifierColumnNames();

        $queryBuilder = $connection->createQueryBuilder()
            ->select('1')
            ->from($this->configuration->getTableName($this->metadata), 'st')
            ->andWhere(sprintf('st.%1$s > t.%1$s', $revisionFieldName));

        $queryBuilder->andWhere(sprintf(
            'st.%s <= %s',
            $revisionFieldName,
            $parentQueryBuilder->createPositionalParameter($this->revision)
        ));

        //ids
        foreach ($identifierColumnNames as $name) {
            $queryBuilder->andWhere(sprintf('st.%1$s = t.%1$s', $name));
        }

        //master entity query, not equals
        $notEqualParts = $nullParts = array();
        foreach ($this->foreignKeys as $column => $value) {
            $notEqualParts[] = $column . ' <> ' . $parentQueryBuilder->createPositionalParameter($value);
            $nullParts[] = $column . ' IS NULL';
        }

        $expr = $queryBuilder->expr();
        $queryBuilder->andWhere(
            $expr->orX(
                $expr->andX(...$notEqualParts),
                $expr->andX(...$nullParts)
            )
        );

        return $queryBuilder;
    }
964596
static public function getSchemaBuilder($connection)
    {

        if (get_class($connection) === 'Illuminate\Database\MySqlConnection') {
            /** @var \Illuminate\Database\MySqlConnection $connection */
            $MySqlGrammar = $connection->withTablePrefix(new MySqlGrammar);
            $connection->setSchemaGrammar($MySqlGrammar);
            return new MySqlBuilder($connection);
        }

        return $connection->getSchemaBuilder();
    }
1327038
public function getOnlineChildrenQueryBuilder(Node $parent, $lang = null)
    {
        return $this->getChildrenQueryBuilder($parent, $lang)
            ->andWhere('nt.online = true');
    }
846557
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey)
        );
    }
838810
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->select($columns)->from(
            $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
        );

        $query->getModel()->setTable($hash);

        return $query->whereColumn(
            $hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName()
        );
    }
832192
public function queryBuilder(ConnectionInterface $connection, String $connectionId=null) : QueryBuilderProvider
	{
		return new QueryBuilder($connection, $connectionId);
	}
949644
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        $modelTable = $query->getModel()
            ->getTable();

        return $query->select($columns)
            ->whereColumn(
                $this->getQualifiedForeignKey(), '=', is_array($this->ownerKey) ? //Check for multi-columns relationship
                array_map(function ($k) use ($modelTable) {
                    return $modelTable.'.'.$k;
                }, $this->ownerKey) : $modelTable.'.'.$this->ownerKey
            );
    }
1023285
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName()
        );
    }
559534
public static function createQueryBuilder($queryBuilder)
    {
        if ($queryBuilder instanceof ORMBuilder) {
            return new ORMQueryBuilder($queryBuilder);
        }
        if ($queryBuilder instanceof DBALBuilder) {
            return new DBALQueryBuilder($queryBuilder);
        }

        throw new QueryBuilderException(
            sprintf('Invalid parameter "queryBuilder". Use %s or %s', ORMBuilder::class, DBALBuilder::class)
        );
    }
624291
protected function prepare(Connection $connection)
	{
		$connection->setFetchMode($this->app['config']['database.fetch']);

		if ($this->app->bound('events'))
		{
			$connection->setEventDispatcher($this->app['events']);
		}

		// The database connection can also utilize a cache manager instance when cache
		// functionality is used on queries, which provides an expressive interface
		// to caching both fluent queries and FlyORM queries that are executed.
		$app = $this->app;

		$connection->setCacheManager(function() use ($app)
		{
			return $app['cache'];
		});

		// We will setup a Closure to resolve the paginator instance on the connection
		// since the Paginator isn't sued on every request and needs quite a few of
		// our dependencies. It'll be more efficient to lazily resolve instances.
		$connection->setPaginator(function() use ($app)
		{
			return $app['paginator'];
		});

		return $connection;
	}
442446
protected function createClientsTable(Connection $connection, DatabaseSchemaInterface $schema): void
    {
        $manager = $connection->getSchemaManager();

        $table = new Table($schema->getClientsTable());
        $table->addColumn($schema->getClientsIdentityColumn(), Type::STRING)->setNotnull(true);
        $table->addColumn($schema->getClientsNameColumn(), Type::STRING)->setNotnull(true);
        $table->addColumn($schema->getClientsDescriptionColumn(), Type::STRING)->setNotnull(false);
        $table->addColumn($schema->getClientsCredentialsColumn(), Type::STRING)->setNotnull(false);
        $table->addColumn($schema->getClientsIsConfidentialColumn(), Type::BOOLEAN)->setDefault(true);
        $table->addColumn($schema->getClientsIsScopeExcessAllowedColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsIsUseDefaultScopeColumn(), Type::BOOLEAN)->setDefault(true);
        $table->addColumn($schema->getClientsIsCodeGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsIsImplicitGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsIsPasswordGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsIsClientGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsIsRefreshGrantEnabledColumn(), Type::BOOLEAN)->setDefault(false);
        $table->addColumn($schema->getClientsCreatedAtColumn(), Type::DATETIME)->setNotnull(true);
        $table->addColumn($schema->getClientsUpdatedAtColumn(), Type::DATETIME)->setNotnull(false);
        $table->setPrimaryKey([$schema->getClientsIdentityColumn()]);
        $manager->dropAndCreateTable($table);
    }
1025571
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);

        if (is_array($this->foreignKeys[0])) {
            $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);

            $query->where($column, '=', $this->farParent->getMorphClass());
        }

        return $query;
    }
360148
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $this->performJoin($query);

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $parentQuery->getQuery()->from.'.'.$query->getModel()->getKeyName(), '=', $this->getQualifiedFirstKeyName()
        );
    }
758751
public function getRelationCountQueryForSelfJoin(Builder $query, Builder $parent)
	{
		$query->select(new \Fly\Database\Query\Expression('count(*)'));

		$query->from($this->table.' as '.$hash = $this->getRelationCountHash());

		$key = $this->wrap($this->getQualifiedParentKeyName());

		return $query->where($hash.'.'.$this->foreignKey, '=', new \Fly\Database\Query\Expression($key));
	}
292676
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        $this->performJoin($query);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKeyName(), '=', $this->getQualifiedSecondOwnerKeyName()
        );
    }
701254
public function queryBuilder(ConnectionInterface $connection, String $connectionId=null) : QueryBuilderProvider
	{
		return new PdoQueryBuilder($connection, $connectionId);
	}
1233789
final protected function foreignKeyJoin(Query $newQuery, Query $parentQuery, Column $foreignKeyToParentColumn, Column $parentKeyColumn) : \Dms\Core\Persistence\Db\Query\Query
    {
        $parentTable = $parentQuery->getTable();
        $tableAlias  = $newQuery->generateUniqueAliasFor($parentTable->getName());

        return $newQuery->prependJoin(Join::inner($parentTable, $tableAlias, [
                Expr::equal(
                        Expr::column($newQuery->getTableAlias(), $foreignKeyToParentColumn),
                        Expr::column($tableAlias, $parentKeyColumn)
                )
        ]));
    }