final class Semantics
    implements Analyzer
Public API: explicitly declared with @visibility public.

Analyzes SQL of one fixed language profile into read-only operations.

An operation holds the concrete structure of the statement, the facts derived against an explicit declaration context, and SQL rendered from that structure. Nothing here changes an operation: a different statement is analyzed anew or built from explicit inputs.

Resolving a query against the declaration it depends ondoctest
$semantics = new \SqlSemantics\Platform\MySql\Semantics();
    $users = $semantics->analyze('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)');
    $query = $semantics->analyze('SELECT name FROM users WHERE id = 1', [$users]);
    $query->field('name')->column() === $users->declarations()[0]->columns[1] // => true
Rendering SQL from the analyzed structuredoctest
$semantics = new \SqlSemantics\Platform\MySql\Semantics();
    $semantics->analyze('select   1')->toString() // => 'SELECT 1'
Finding the statements of a scriptdoctest
$semantics = new \SqlSemantics\Platform\MySql\Semantics();
    $semantics->split("SELECT 1; SELECT ';'") // => ['SELECT 1;', " SELECT ';'"]

Methods§

public function __construct(
    string|null $grammarVersion = null,
    Mode|null $mode = null,
    ParameterStyle $parameters = ParameterStyle::Native,
    SearchPath|null $searchPath = null,
)

Fixes the language profile: database, grammar release, session mode and parameter style.

Parameters

$grammarVersionstring|nullA release tag the database package ships, or null for its default
$modeMode|nullThe session settings SQL is read under, or null for the defaults of the database
$parametersParameterStyleWhich parameter markers are read; the named style adds `:name`
$searchPathSearchPath|nullThe schemas an unqualified relation name is searched in, or null for the default of the database

Throws

InvalidArgumentException When the release, mode or search path does not belong to the database
Calls 5
public function profile(): LanguageProfile

Answers the fixed language profile.

Test cases 134
public function context(
    list<Table|Operation>|AnalysisContext|null $declarations = null,
    bool $complete = true,
): AnalysisContext

Prepares an immutable declaration context or verifies a supplied context.

Null declarations give an open context: relations that are not declared may exist. A list, even an empty one, gives a context that enumerates every relation unless $complete is false. An operation in the list contributes the declarations it provides; it is not executed, and ALTER, DROP and writes contribute nothing.

Parameters

$declarationslist<Table|Operation>|AnalysisContext|nullA declaration list, a prepared context, or null for an open context
$completeboolWhether a given list enumerates every relation

Throws

InvalidConstruction When a declaration belongs to another language profile
Test cases 69
Called from 2
Calls 4
public function analyze(
    string $sql,
    list<Table|Operation>|AnalysisContext|null $context = null,
): Operation

Analyzes SQL and returns a checked operation against the given declarations.

Parameters

$sqlstring
$contextlist<Table|Operation>|AnalysisContext|nullA declaration list, a prepared context, or null for an open context

Returns

Throws

AnalysisException When the grammar rejects the SQL
SemanticException When the analysis establishes a semantic problem
InvalidConstruction When the context belongs to another dialect or language profile
Test cases 1196
Called from 1
Calls 2
public function inspect(
    string $sql,
    list<Table|Operation>|AnalysisContext|null $context = null,
): Operation
Restricted visibility: declared "@visibility SqlSemantics". Code outside that scope must not name this declaration.

Checks the structure and derives facts, including diagnostics for invalid statements.

Parameters

$sqlstring
$contextlist<Table|Operation>|AnalysisContext|null

Returns

Throws

AnalysisException When the grammar rejects the SQL
InvalidConstruction When the context belongs to another dialect or language profile
Test cases 725
Called from 1
Calls 20
public function analyzeAll(
    string $sql,
    list<Table|Operation>|AnalysisContext|null $context = null,
): list<Operation>

Analyzes each statement of a script against the same explicit declaration context.

No statement is executed for the ones after it.

Parameters

$sqlstring
$contextlist<Table|Operation>|AnalysisContext|nullThe declarations, a prepared context, or null for an open context

Returns

list<Operation>

Throws

AnalysisException When a statement is outside the selected grammar
SemanticException When a statement has a semantic problem
Test cases 1
Calls 3
public function split(string $sql): list<string>

Finds the statement boundaries of a script, as the database finds them.

Each text ends with its own terminator; trailing whitespace and comments stay with the last statement. A semicolon inside a string, a comment, or a compound statement ends nothing.

Parameters

$sqlstring

Returns

list<string>

Throws

AnalysisException When a statement is outside the selected grammar
Test cases 1
Called from 1
Calls 3

Private surface 4§

Implementation details, listed for orientation only.

private Platform $platform
private LanguageProfile $profile
private SqlParser $parser
private list<string>|null $searchPath

Test cases 2020§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 10
Other tests reaching this symbol 2010