packages/sql-semantics-mysql/src/Statement/Server/Spatial/CreateSpatialReference.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Server\Spatial;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\InvalidConstruction;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
12use SqlSemantics\Platform\MySql\Model\Statement\Statement;
13use SqlSemantics\Platform\MySql\Rules\Server\SpatialChecks;
14use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
15
16/**
17 * `CREATE [OR REPLACE] SPATIAL REFERENCE SYSTEM [IF NOT EXISTS] srid attribute …`: a request to define a spatial reference system (MySQL 8.0 and later).
18 *
19 * Mirrors PT_create_srs (Sql_cmd_create_srs). Rule: MYSQL-CREATE-SRS-001.
20 * The attributes are kept in written order. The problems the server reports
21 * while it parses the statement are diagnostics (MYSQL-SRS-CHECK-001). A
22 * spatial reference system is no relation, so the statement declares none.
23 * Source: https://dev.mysql.com/doc/refman/8.4/en/create-spatial-reference-system.html.
24 * Status: Implemented.
25 *
26 * @visibility public
27 * @example Defining a spatial reference system
28 * $create = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("create or replace spatial reference system 4120 name 'Greek' organization 'EPSG' identified by 4120 definition 'GEOGCS[]'");
29 * [$create->toString(), $create->facts->diagnostics] // => ["CREATE OR REPLACE SPATIAL REFERENCE SYSTEM 4120 NAME 'Greek' ORGANIZATION 'EPSG' IDENTIFIED BY 4120 DEFINITION 'GEOGCS[]'", []]
30 */
31final class CreateSpatialReference implements Statement
32{
33 use Snapshot;
34
35 /**
36 * @var list<SpatialAttribute> The attributes in written order
37 */
38 public readonly array $attributes;
39
40 /**
41 * @param bool $orReplace Whether OR REPLACE is written
42 * @param bool $ifNotExists Whether IF NOT EXISTS is written
43 * @param Numeral $srid The spatial reference system identifier
44 * @param list<SpatialAttribute> $attributes The attributes in written order
45 * @throws InvalidConstruction When OR REPLACE and IF NOT EXISTS are both written
46 */
47 public function __construct(public readonly bool $orReplace, public readonly bool $ifNotExists, public readonly Numeral $srid, array $attributes)
48 {
49 Check::input(!$orReplace || !$ifNotExists, 'OR REPLACE and IF NOT EXISTS exclude each other.');
50 $this->attributes = Check::listOf($attributes, SpatialAttribute::class, 'CREATE SPATIAL REFERENCE SYSTEM takes a list of attributes.');
51 }
52
53 /**
54 * Reports the problems the server finds while it parses the statement.
55 */
56 public function deriveStatement(Derivation $derivation): void
57 {
58 $checks = new SpatialChecks();
59 $checks->srid($derivation, $this->srid);
60 $checks->attributes($derivation, $this->attributes);
61 }
62
63 /**
64 * Writes the request.
65 */
66 public function render(Output $out): void
67 {
68 $out->keyword('CREATE');
69 if ($this->orReplace) {
70 $out->keyword('OR', 'REPLACE');
71 }
72 $out->keyword('SPATIAL', 'REFERENCE', 'SYSTEM');
73 if ($this->ifNotExists) {
74 $out->keyword('IF', 'NOT', 'EXISTS');
75 }
76 $out->node($this->srid);
77 foreach ($this->attributes as $attribute) {
78 $out->node($attribute);
79 }
80 }
81}
82