packages/sql-semantics-mysql/tests/Unit/Rules/Dml/GeneratedWritesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Dml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\MySql\Rules\Dml\GeneratedWrites;
12use SqlSemantics\Platform\MySql\Semantics;
13use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertQuery;
14use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertRows;
15use SqlSemantics\Platform\MySql\Statement\Dml\Problem\GeneratedColumnWrite;
16use SqlSemantics\Platform\MySql\Statement\Query\ValuesQuery;
17
18#[CoversClass(GeneratedWrites::class)]
19#[Medium]
20final class GeneratedWritesTest extends TestCase
21{
22 public function testValueReportsAValueWrittenIntoAGeneratedColumn(): void
23 {
24 $semantics = new Semantics();
25 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1), s INT AS (a * 2) STORED, b INT DEFAULT 5)')->declarations();
26 $messages = static fn (string $sql): array => array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect($sql, $tables)->facts->diagnostics);
27
28 self::assertSame(["The value specified for generated column 'g' in table 't' is not allowed."], $messages('INSERT INTO t (a, G) VALUES (1, 2)'));
29 self::assertSame(["The value specified for generated column 's' in table 't' is not allowed."], $messages('REPLACE INTO t (a, s) VALUES (1, NULL)'));
30 self::assertSame(["The value specified for generated column 'g' in table 't' is not allowed."], $messages('INSERT IGNORE INTO t (a, g) VALUES (1, DEFAULT), (2, 3)'));
31 self::assertSame(["The value specified for generated column 'g' in table 't' is not allowed.", "The value specified for generated column 's' in table 't' is not allowed."], $messages('INSERT INTO t VALUES (1, 2, 3, 4)'));
32 }
33
34 public function testValueAcceptsDefaultValueItems(): void
35 {
36 $semantics = new Semantics();
37 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1), s INT AS (a * 2) STORED, b INT DEFAULT 5)')->declarations();
38
39 self::assertSame([], $semantics->inspect('INSERT INTO t VALUES (1, DEFAULT, DEFAULT(b), 4)', $tables)->facts->diagnostics);
40 self::assertSame([], $semantics->inspect('INSERT INTO t (a, g) VALUES (1, ((DEFAULT(b))))', $tables)->facts->diagnostics);
41 }
42
43 public function testValueLeavesARowWithAnotherCountToTheCountMismatch(): void
44 {
45 $semantics = new Semantics();
46 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1))')->declarations();
47
48 self::assertSame(["Column count doesn't match value count at row 1"], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('INSERT INTO t (a, g) VALUES (1, 2, 3)', $tables)->facts->diagnostics));
49 }
50
51 public function testAssignmentsReportsAssignedGeneratedColumns(): void
52 {
53 $semantics = new Semantics();
54 $tables = $semantics->inspect('CREATE TABLE t (a INT PRIMARY KEY, g INT AS (a + 1), b INT)')->declarations();
55 $messages = static fn (string $sql): array => array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect($sql, $tables)->facts->diagnostics);
56 $expected = ["The value specified for generated column 'g' in table 't' is not allowed."];
57
58 self::assertSame($expected, $messages('INSERT INTO t SET a = 1, g = 2'));
59 self::assertSame($expected, $messages('INSERT INTO t (a) VALUES (1) AS n ON DUPLICATE KEY UPDATE g = n.a'));
60 self::assertSame($expected, $messages('INSERT INTO t (a) VALUES (1) ON DUPLICATE KEY UPDATE g = VALUES(g)'));
61 self::assertSame($expected, $messages('UPDATE IGNORE t SET g = g'));
62 self::assertSame($expected, $messages('UPDATE t, t AS u SET u.b = 1, t.g = 1'));
63 self::assertSame([], $messages('UPDATE t, t AS u SET u.g = DEFAULT, t.g = DEFAULT(t.b)'));
64 self::assertSame([], $messages('INSERT INTO t SET a = 1, g = DEFAULT ON DUPLICATE KEY UPDATE g = DEFAULT'));
65 }
66
67 public function testAssignmentsIgnoresColumnsOutsideDeclaredTables(): void
68 {
69 $semantics = new Semantics();
70 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1))')->declarations();
71 $copy = $semantics->inspect('CREATE TABLE c SELECT * FROM t', $tables)->declarations();
72
73 self::assertSame([], $semantics->inspect('LOAD DATA INFILE \'f\' INTO TABLE t (a, g) SET g = 1', $tables)->facts->diagnostics);
74 self::assertSame([], $semantics->inspect('UPDATE c SET g = 1', $copy)->facts->diagnostics);
75 }
76
77 public function testQueryReportsTheFieldsOfAQuerySource(): void
78 {
79 $semantics = new Semantics();
80 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1), b INT DEFAULT 5)')->declarations();
81 $messages = static fn (string $sql): array => array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect($sql, $tables)->facts->diagnostics);
82 $expected = ["The value specified for generated column 'g' in table 't' is not allowed."];
83
84 self::assertSame($expected, $messages('INSERT INTO t (a, g) SELECT 1, 2'));
85 self::assertSame($expected, $messages('INSERT INTO t (a, g) SELECT 1, DEFAULT(b) FROM t UNION SELECT 2, DEFAULT(b) FROM t'));
86 self::assertSame($expected, $messages('INSERT INTO t (a, g) SELECT * FROM (SELECT 1, DEFAULT(b) FROM t) AS d'));
87 self::assertSame($expected, $messages('INSERT INTO t (a, g) VALUES ROW(1, 2)'));
88 self::assertSame([], $messages('INSERT INTO t (a, g) WITH c AS (SELECT 1) (SELECT DISTINCT 1, (DEFAULT(b)) FROM t ORDER BY 1 LIMIT 1)'));
89 }
90
91 public function testQueryChecksTheRowsOfAValuesSourceAsRows(): void
92 {
93 $semantics = new Semantics();
94 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1))')->declarations();
95 $diagnostics = $semantics->inspect('INSERT INTO t (a, g) VALUES ROW(1, DEFAULT), ROW(2, 3)', $tables)->facts->diagnostics;
96
97 self::assertCount(1, array_filter($diagnostics, static fn (Diagnostic $diagnostic): bool => $diagnostic instanceof GeneratedColumnWrite));
98 }
99
100 public function testValueRowsFindsAValuesStatementInsideItsClauses(): void
101 {
102 $semantics = new Semantics();
103 $values = $semantics->inspect('INSERT INTO t (a) WITH c AS (SELECT 1) (VALUES ROW(1)) ORDER BY 1 LIMIT 1')->statement;
104 $select = $semantics->inspect('INSERT INTO t (a) (SELECT 1)')->statement;
105 self::assertInstanceOf(InsertQuery::class, $values);
106 self::assertInstanceOf(InsertQuery::class, $select);
107
108 self::assertInstanceOf(ValuesQuery::class, (new GeneratedWrites())->valueRows($values->source));
109 self::assertNull((new GeneratedWrites())->valueRows($select->source));
110 }
111
112 public function testDefaultTellsDefaultValueItems(): void
113 {
114 $insert = (new Semantics())->inspect('INSERT INTO t (a, b, c, d) VALUES (DEFAULT, (DEFAULT(a)), DEFAULT(a) + 0, NULL)')->statement;
115 self::assertInstanceOf(InsertRows::class, $insert);
116 $values = $insert->rows[0]->values;
117
118 self::assertSame([true, true, false, false, false], [...array_map(static fn ($value): bool => (new GeneratedWrites())->default($value), $values), (new GeneratedWrites())->default(null)]);
119 }
120
121 public function testTableAnswersTheDeclaredNameOfTheTable(): void
122 {
123 $semantics = new Semantics();
124 $tables = $semantics->inspect('CREATE TABLE t (a INT, g INT AS (a + 1))')->declarations();
125
126 self::assertSame("The value specified for generated column 'g' in table 't' is not allowed.", $semantics->inspect('UPDATE t AS x SET x.g = 1', $tables)->facts->diagnostics[0]->message());
127 }
128}
129