0 follower

Final Class Yiisoft\Db\Mssql\DQLQueryBuilder

InheritanceYiisoft\Db\Mssql\DQLQueryBuilder » Yiisoft\Db\QueryBuilder\AbstractDQLQueryBuilder

Implements a DQL (Data Query Language) SQL statements for MSSQL Server.

Protected Methods

Hide inherited methods

Method Description Defined By
defaultExpressionBuilders() Yiisoft\Db\Mssql\DQLQueryBuilder
extractAlias() Extracts table alias if there is one or returns false. Yiisoft\Db\Mssql\DQLQueryBuilder
newBuildOrderByAndLimit() Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer. Yiisoft\Db\Mssql\DQLQueryBuilder

Method Details

Hide inherited methods

buildOrderByAndLimit() public method

public buildOrderByAndLimit( string $sql, array $orderBy, \Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit, \Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset, array &$params = [] ): string
$sql string
$orderBy array
$limit \Yiisoft\Db\Expression\ExpressionInterface|integer|null
$offset \Yiisoft\Db\Expression\ExpressionInterface|integer|null
$params array

                public function buildOrderByAndLimit(
    string $sql,
    array $orderBy,
    ExpressionInterface|int|null $limit,
    ExpressionInterface|int|null $offset,
    array &$params = [],
): string {
    if (empty($offset) && $limit === null) {
        $orderByString = $this->buildOrderBy($orderBy, $params);
        return $orderByString === '' ? $sql : $sql . $this->separator . $orderByString;
    }
    return $this->newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset, $params);
}

            
buildWithQueries() public method

public buildWithQueries( array $withQueries, array &$params ): string
$withQueries array
$params array

                public function buildWithQueries(array $withQueries, array &$params): string
{
    $withQueries = array_map(
        static fn(WithQuery $withQuery) => new WithQuery(
            $withQuery->query,
            $withQuery->alias,
            false,
        ),
        $withQueries,
    );
    return parent::buildWithQueries($withQueries, $params);
}

            
defaultExpressionBuilders() protected method

protected defaultExpressionBuilders( ): array

                protected function defaultExpressionBuilders(): array
{
    return [
        ...parent::defaultExpressionBuilders(),
        In::class => InBuilder::class,
        NotIn::class => InBuilder::class,
        Like::class => LikeBuilder::class,
        NotLike::class => LikeBuilder::class,
        Length::class => LengthBuilder::class,
        ArrayMerge::class => ArrayMergeBuilder::class,
        Greatest::class => GreatestBuilder::class,
        Least::class => LeastBuilder::class,
        Longest::class => LongestBuilder::class,
        Shortest::class => ShortestBuilder::class,
    ];
}

            
extractAlias() protected method

Extracts table alias if there is one or returns false.

protected extractAlias( string $table ): array|boolean
$table string

                protected function extractAlias(string $table): array|bool
{
    if (preg_match('/^\[.*]$/', $table)) {
        return false;
    }
    return parent::extractAlias($table);
}

            
newBuildOrderByAndLimit() protected method

Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer.

protected newBuildOrderByAndLimit( string $sql, array $orderBy, \Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit, \Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset, array &$params = [] ): string
$sql string

The existing SQL (without ORDER BY/LIMIT/OFFSET).

$orderBy array

The order by columns. See \Yiisoft\Db\Query\Query::orderBy for more details on how to specify this parameter.

$limit \Yiisoft\Db\Expression\ExpressionInterface|integer|null

The limit number. See \Yiisoft\Db\Query\Query::limit for more details.

$offset \Yiisoft\Db\Expression\ExpressionInterface|integer|null

The offset number. See \Yiisoft\Db\Query\Query::offset for more details.

$params array

The binding parameters to populate.

return string

The SQL completed with ORDER BY/LIMIT/OFFSET (if any).

throws \Yiisoft\Db\Exception\Exception
throws InvalidArgumentException

                protected function newBuildOrderByAndLimit(
    string $sql,
    array $orderBy,
    ExpressionInterface|int|null $limit,
    ExpressionInterface|int|null $offset,
    array &$params = [],
): string {
    $orderByString = $this->buildOrderBy($orderBy, $params);
    if ($orderByString === '') {
        /** `ORDER BY` clause is required when `FETCH` and `OFFSET` are in the SQL */
        $orderByString = 'ORDER BY (SELECT NULL)';
    }
    $sql .= $this->separator . $orderByString;
    /**
     * @link https://technet.microsoft.com/en-us/library/gg699618.aspx
     */
    $offsetString = !empty($offset)
        ? ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string) $offset)
        : '0';
    $sql .= $this->separator . 'OFFSET ' . $offsetString . ' ROWS';
    if ($limit !== null) {
        $sql .= $this->separator . 'FETCH NEXT '
            . ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string) $limit)
            . ' ROWS ONLY';
    }
    return $sql;
}

            
selectExists() public method

public selectExists( string $rawSql ): string
$rawSql string

                public function selectExists(string $rawSql): string
{
    return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END AS [0]';
}