packages/sql-semantics-mysql/src/Statement/Expression/Tuple.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Expression;
6
7use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
9use SqlSemantics\Platform\MySql\Model\Statement\Type\TypeDescriptor;
10
11/**
12 * The type of a row of several values: a row constructor or a subquery of several columns (`ROW_RESULT`).
13 *
14 * Only the number of columns is part of the type: comparisons, IN and row
15 * subqueries require rows of equal width.
16 * Source: https://dev.mysql.com/doc/refman/8.4/en/row-subqueries.html.
17 *
18 * @visibility public
19 * @example Reading the width of a row
20 *     (new \SqlSemantics\Platform\MySql\Semantics())->analyze('SELECT a FROM t WHERE (a, b) IS NULL') // throws \SqlSemantics\Platform\MySql\Model\Diagnostic\SemanticException: Operand should contain 1 column(s), not 2.
21 */
22final class Tuple implements TypeDescriptor
23{
24    use Snapshot;
25
26    /**
27     * @param int $width The number of columns; at least two
28     */
29    public function __construct(public readonly int $width)
30    {
31        Check::input($width >= 2, 'A row has at least two columns.');
32    }
33
34    /**
35     * Names the type as the server's result type.
36     */
37    public function name(): string
38    {
39        return 'ROW';
40    }
41}
42