packages/sql-semantics-mysql/src/Statement/Type/Floating.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Type;
6
7use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
8use SqlSemantics\Platform\MySql\Model\Rendering\Output;
9use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
10use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
11use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
12
13/**
14 * An approximate numeric type: FLOAT, REAL or DOUBLE with its optional precision and scale.
15 *
16 * `DOUBLE PRECISION` is `DOUBLE`. `FLOAT(p)` selects single or double
17 * precision by `p`; `FLOAT(M,D)`, `REAL(M,D)` and `DOUBLE(M,D)` are the
18 * nonstandard display forms. REAL is kept as its own kind because the
19 * session mode REAL_AS_FLOAT, which the language profile does not fix,
20 * decides whether it is DOUBLE or FLOAT.
21 * Source: https://dev.mysql.com/doc/refman/8.4/en/floating-point-types.html.
22 *
23 * @visibility public
24 * @example Reading an approximate type
25 * $type = new \SqlSemantics\Platform\MySql\Statement\Type\Floating(\SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind::Float, '7', '3');
26 * [$type->name(), $type->precision, $type->scale] // => ['FLOAT', '7', '3']
27 */
28final class Floating implements TypeName
29{
30 use Snapshot;
31
32 /**
33 * @var list<NumericModifier> The attributes in the order written; an attribute may repeat
34 */
35 public readonly array $modifiers;
36
37 /**
38 * @param FloatingKind $kind The approximate type
39 * @param string|null $precision The precision or display length exactly as written
40 * @param string|null $scale The number of decimals exactly as written; requires a precision
41 * @param list<NumericModifier> $modifiers The attributes in the order written
42 */
43 public function __construct(public readonly FloatingKind $kind, public readonly ?string $precision = null, public readonly ?string $scale = null, array $modifiers = [])
44 {
45 Check::input($precision === null || preg_match('/\A(?:[0-9]+\.?[0-9]*|\.[0-9]+)\z/', $precision) === 1, 'A precision is an unsigned number.');
46 Check::input($scale === null || ($precision !== null && preg_match('/\A[0-9]+\z/', $scale) === 1), 'A scale is an unsigned integer written after a precision.');
47 $this->modifiers = Check::listOf($modifiers, NumericModifier::class, 'Numeric attributes are SIGNED, UNSIGNED and ZEROFILL.');
48 }
49
50 /**
51 * Names the type by its keyword.
52 */
53 public function name(): string
54 {
55 return $this->kind->value;
56 }
57
58 /**
59 * Writes the keyword, the precision and scale, and the attributes.
60 */
61 public function render(Output $out): void
62 {
63 $out->keyword($this->kind->value);
64 if ($this->precision !== null) {
65 $out->glue()->symbol('(')->spelled($this->precision);
66 if ($this->scale !== null) {
67 $out->symbol(',')->spelled($this->scale);
68 }
69 $out->symbol(')');
70 }
71 foreach ($this->modifiers as $modifier) {
72 $out->keyword($modifier->value);
73 }
74 }
75}
76