packages/sql-semantics-mysql/src/Lowering/Dml/FormatRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Lowering\Dml;
6
7use SqlParser\Parser\Node;
8use SqlSemantics\Platform\MySql\Lowering\Lowering;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\ImplementationGap;
10use SqlSemantics\Platform\MySql\Statement\Dml\Load\FieldOption;
11use SqlSemantics\Platform\MySql\Statement\Dml\Load\FieldOptionKind;
12use SqlSemantics\Platform\MySql\Statement\Dml\Load\LineOption;
13use SqlSemantics\Platform\MySql\Statement\Dml\Load\LineOptionKind;
14use SqlSemantics\Platform\MySql\Statement\Name\CharsetName;
15
16/**
17 * Lowers the text file format of LOAD DATA and SELECT ... INTO OUTFILE.
18 *
19 * Rule: MYSQL-DML-FORMAT-001. Scope: opt_load_data_charset, opt_field_term,
20 * field_term_list, field_term, opt_line_term, line_term_list, line_term.
21 * The options are kept in written order (FieldOption, LineOption); the
22 * keyword FIELDS is the lexer's COLUMNS. Terminates: lists are flattened by
23 * MYSQL-DML-LIST-001. Source: https://dev.mysql.com/doc/refman/8.4/en/load-data.html.
24 * Status: Implemented.
25 *
26 * @visibility SqlSemantics\Platform\MySql\Lowering\Dml
27 */
28final class FormatRule
29{
30    /**
31     * The field options by production.
32     */
33    private const FIELDS = [
34        'field_term: TERMINATED BY text_string' => FieldOptionKind::Terminated, 'field_term: OPTIONALLY ENCLOSED BY text_string' => FieldOptionKind::OptionallyEnclosed,
35        'field_term: ENCLOSED BY text_string' => FieldOptionKind::Enclosed, 'field_term: ESCAPED BY text_string' => FieldOptionKind::Escaped,
36    ];
37
38    /**
39     * The line options by production.
40     */
41    private const LINES = ['line_term: TERMINATED BY text_string' => LineOptionKind::Terminated, 'line_term: STARTING BY text_string' => LineOptionKind::Starting];
42
43    /**
44     * @param Lowering $lowering The lowering this rule belongs to
45     */
46    public function __construct(private readonly Lowering $lowering)
47    {
48    }
49
50    /**
51     * Lowers the character set clause: a node of `opt_load_data_charset`; an absent clause is null.
52     *
53     * @throws ImplementationGap When a production has no rule
54     */
55    public function charset(Node $charset): ?CharsetName
56    {
57        $form = $this->lowering->form($charset);
58        if ($form->signature === 'opt_load_data_charset:') {
59            return null;
60        }
61        if ($form->signature !== 'opt_load_data_charset: charset charset_name_or_default' && $form->signature !== 'opt_load_data_charset: character_set charset_name') {
62            throw ImplementationGap::production($form);
63        }
64        $this->lowering->options->skip($form->node(0));
65
66        return $this->lowering->charsets->charset($form->node(1));
67    }
68
69    /**
70     * Lowers the FIELDS clause: a node of `opt_field_term`; an absent clause is empty.
71     *
72     * @return list<FieldOption>
73     * @throws ImplementationGap When a production has no rule
74     */
75    public function fields(Node $clause): array
76    {
77        $form = $this->lowering->form($clause);
78        if ($form->signature === 'opt_field_term:') {
79            return [];
80        }
81        if ($form->signature !== 'opt_field_term: COLUMNS field_term_list') {
82            throw ImplementationGap::production($form);
83        }
84        $options = [];
85        foreach ((new ListRule($this->lowering))->items($form->node(1), ['field_term_list: field_term_list field_term', 'field_term_list: field_term']) as $item) {
86            $term = $this->lowering->form($item);
87            $kind = self::FIELDS[$term->signature] ?? throw ImplementationGap::production($term);
88            $options[] = new FieldOption($kind, $this->lowering->literals->text($term->node(count($term->node->children) - 1)));
89        }
90
91        return $options;
92    }
93
94    /**
95     * Lowers the LINES clause: a node of `opt_line_term`; an absent clause is empty.
96     *
97     * @return list<LineOption>
98     * @throws ImplementationGap When a production has no rule
99     */
100    public function lines(Node $clause): array
101    {
102        $form = $this->lowering->form($clause);
103        if ($form->signature === 'opt_line_term:') {
104            return [];
105        }
106        if ($form->signature !== 'opt_line_term: LINES line_term_list') {
107            throw ImplementationGap::production($form);
108        }
109        $options = [];
110        foreach ((new ListRule($this->lowering))->items($form->node(1), ['line_term_list: line_term_list line_term', 'line_term_list: line_term']) as $item) {
111            $term = $this->lowering->form($item);
112            $kind = self::LINES[$term->signature] ?? throw ImplementationGap::production($term);
113            $options[] = new LineOption($kind, $this->lowering->literals->text($term->node(2)));
114        }
115
116        return $options;
117    }
118}
119