packages/sql-semantics/src/Facade/Semantics.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Facade;
6
7use SqlSemantics\Core\Analysis\Analyzer;
8use SqlSemantics\Core\Dialect;
9use SqlSemantics\Statement\Statement;
10
11/**
12 * Structures every statement of a selected SQL language into independent values.
13 *
14 * This entry point needs no schema or database connection. Use Binder separately
15 * when schema-dependent name, type, and nullability facts are needed.
16 *
17 * @visibility public
18 * @example Reconstructing SQL with the SQLite database package
19 * $semantics = new \SqlSemantics\Facade\Semantics(\SqlSemantics\Platform\Sqlite\Dialect::Sqlite);
20 * $semantics->analyze('DROP TABLE example')->toString() // => 'DROP TABLE example'
21 */
22final class Semantics
23{
24 private readonly Analyzer $analyzer;
25
26 /**
27 * Selects the dialect and optionally one of its shipped grammar releases.
28 */
29 public function __construct(Dialect $dialect, ?string $grammarVersion = null)
30 {
31 $this->analyzer = new Analyzer($dialect, $grammarVersion);
32 }
33
34 /**
35 * Builds an immutable statement from SQL without keeping its original syntax.
36 *
37 * @throws \SqlSemantics\Core\AnalysisException When SQL is not in the selected language
38 */
39 public function analyze(string $sql): Statement
40 {
41 return $this->analyzer->analyze($sql);
42 }
43}
44