|
Lightweight 0.20260921.0
|
API to format SQL queries for different SQL dialects. More...
#include <SqlQueryFormatter.hpp>
Inherited by Lightweight::SQLiteQueryFormatter.
Public Types | |
| using | StringList = std::vector< std::string > |
| Alias for a list of SQL statement strings. | |
Public Member Functions | |
| SqlQueryFormatter ()=default | |
| Default constructor. | |
| SqlQueryFormatter (SqlQueryFormatter &&)=default | |
| Default move constructor. | |
| SqlQueryFormatter (SqlQueryFormatter const &)=default | |
| Default copy constructor. | |
| SqlQueryFormatter & | operator= (SqlQueryFormatter &&)=default |
| Default move assignment operator. | |
| SqlQueryFormatter & | operator= (SqlQueryFormatter const &)=default |
| Default copy assignment operator. | |
| virtual std::string_view | BooleanLiteral (bool value) const noexcept=0 |
| Converts a boolean value to a string literal. | |
| virtual std::string_view | DateFunction () const noexcept=0 |
| Returns the SQL function name used to retrieve the current date. | |
| virtual std::string | StringLiteral (std::string_view value) const noexcept=0 |
| Converts a string value to a string literal. | |
| virtual std::string | StringLiteral (char value) const noexcept=0 |
| Converts a character value to a string literal. | |
| virtual std::string | BinaryLiteral (std::span< uint8_t const > data) const =0 |
| Converts a binary value to a hex-encoded string literal. | |
| virtual std::string | QualifiedTableName (std::string_view schema, std::string_view table) const =0 |
| virtual std::string | Insert (std::string_view intoTable, std::string_view fields, std::string_view values) const =0 |
| virtual std::string | Insert (std::string_view schema, std::string_view intoTable, std::string_view fields, std::string_view values) const =0 |
| Constructs an SQL INSERT query with a schema prefix. | |
| virtual std::string | QueryLastInsertId (std::string_view tableName) const =0 |
| Retrieves the last insert ID of the given table. | |
| virtual std::string | SelectAll (bool distinct, std::string_view fields, std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition, std::string_view orderBy, std::string_view groupBy) const =0 |
| Constructs an SQL SELECT query for all rows. | |
| virtual std::string | SelectFirst (bool distinct, std::string_view fields, std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition, std::string_view orderBy, std::string_view groupBy, size_t count) const =0 |
| virtual std::string | SelectRange (bool distinct, std::string_view fields, std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition, std::string_view orderBy, std::string_view groupBy, std::size_t offset, std::size_t limit) const =0 |
| Constructs an SQL SELECT query for a range of rows. | |
| virtual std::string | SelectCount (bool distinct, std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition, std::string_view groupBy) const =0 |
| virtual std::string | Update (std::string_view table, std::string_view tableAlias, std::string_view setFields, std::string_view whereCondition) const =0 |
| Constructs an SQL UPDATE query. | |
| virtual std::string | Delete (std::string_view fromTable, std::string_view fromTableAlias, std::string_view tableJoins, std::string_view whereCondition) const =0 |
| Constructs an SQL DELETE query. | |
| virtual std::string | ColumnType (SqlColumnTypeDefinition const &type) const =0 |
| Convert the given column type definition to the SQL type. | |
| virtual StringList | CreateTable (std::string_view schema, std::string_view tableName, std::vector< SqlColumnDeclaration > const &columns, std::vector< SqlCompositeForeignKeyConstraint > const &foreignKeys, bool ifNotExists=false) const =0 |
| virtual StringList | AlterTable (std::string_view schema, std::string_view tableName, std::vector< SqlAlterTableCommand > const &commands) const =0 |
| Constructs an SQL ALTER TABLE query. | |
| virtual StringList | DropTable (std::string_view schema, std::string_view const &tableName, bool ifExists=false, bool cascade=false) const =0 |
| virtual std::string | QueryServerVersion () const =0 |
| virtual bool | RequiresTableRebuildForSchemaChange () const noexcept |
Whether the dialect must rebuild a table to apply an ALTER TABLE schema change that it cannot express in place — adding/dropping a foreign-key constraint, or altering a column's type/nullability (ALTER TABLE … ADD/DROP CONSTRAINT / … ALTER COLUMN). | |
| virtual bool | SupportsBatchedSchemaIntrospection () const noexcept |
Whether the dialect provides a batched, whole-database schema-introspection fast path that SqlSchema::ReadAllTables can use instead of the per-table ODBC catalog loop. | |
| virtual size_t | MaxInPredicateValues () const noexcept |
Largest number of values this dialect should be handed in a single WHERE ... IN (...) predicate. | |
| virtual std::string | SetDefaultSchemaStatement (std::string_view schema) const |
Returns the SQL statement to execute after connect to make schema the connection-level default for unqualified DDL/DML. | |
| virtual SqlAdvisoryLockHandler const & | AdvisoryLockOps () const =0 |
Returns the dialect-specific handler used by SqlScopedLock to acquire and release named cross-process advisory locks. | |
| virtual SqlRetryClassifier const & | RetryOps () const noexcept=0 |
Static Public Member Functions | |
| static SqlQueryFormatter const & | Sqlite () |
| Retrieves the SQL query formatter for SQLite. | |
| static SqlQueryFormatter const & | SqlServer () |
| Retrieves the SQL query formatter for Microsoft SQL server. | |
| static SqlQueryFormatter const & | PostgrSQL () |
| Retrieves the SQL query formatter for PostgreSQL. | |
| static SqlQueryFormatter const * | Get (SqlServerType serverType) noexcept |
| Retrieves the SQL query formatter for the given SqlServerType. | |
| template<typename Range > | |
| static std::string | BuildForeignKeyConstraintName (std::string_view tableName, Range const &columns) |
| Builds the canonical foreign-key constraint name for a set of columns. | |
Static Protected Member Functions | |
| static std::string | FormatTableName (std::string_view schema, std::string_view table) |
| Formats a table name with optional schema prefix. | |
API to format SQL queries for different SQL dialects.
Definition at line 20 of file SqlQueryFormatter.hpp.
| using Lightweight::SqlQueryFormatter::StringList = std::vector<std::string> |
Alias for a list of SQL statement strings.
Definition at line 136 of file SqlQueryFormatter.hpp.
|
pure virtual |
Formats a qualified table name with proper quoting for this database.
| schema | The schema name (can be empty for default schema) |
| table | The table name |
|
pure virtual |
Constructs an SQL INSERT query.
| intoTable | The table to insert into. |
| fields | The fields to insert into. |
| values | The values to insert. |
The fields and values must be in the same order.
Referenced by Lightweight::SqlInsertQueryBuilder::ToSql().
|
pure virtual |
Constructs an SQL SELECT query for the first row.
When groupBy is non-empty it is emitted between the WHERE and the ORDER BY clause, so the row limit given by count applies to the grouped result set.
|
pure virtual |
Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
When groupBy is non-empty the query counts the rows of each group, i.e. it yields one row per group rather than a single total.
|
pure virtual |
Constructs an SQL CREATE TABLE query.
| schema | The schema name of the table to create. |
| tableName | The name of the table to create. |
| columns | The columns of the table. |
| foreignKeys | The foreign key constraints of the table. |
| ifNotExists | If true, generates CREATE TABLE IF NOT EXISTS instead of CREATE TABLE. |
|
pure virtual |
Constructs an SQL DROP TABLE query.
| schema | The schema name of the table to drop. |
| tableName | The name of the table to drop. |
| ifExists | If true, generates DROP TABLE IF EXISTS instead of DROP TABLE. |
| cascade | If true, drops all foreign key constraints referencing this table first. |
|
pure virtual |
Returns the SQL query to retrieve the full server version string.
This query returns detailed version information specific to each database:
|
inlinevirtualnoexcept |
Whether the dialect must rebuild a table to apply an ALTER TABLE schema change that it cannot express in place — adding/dropping a foreign-key constraint, or altering a column's type/nullability (ALTER TABLE … ADD/DROP CONSTRAINT / … ALTER COLUMN).
SQLite returns true; every other backend defaults to false. The migration executor consults this (via SqlConnection::RequiresTableRebuildForSchemaChange) to decide whether to take the table-rebuild path for the -- LIGHTWEIGHT_SQLITE_GUARD: sentinels these dialects emit.
Definition at line 197 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether the dialect provides a batched, whole-database schema-introspection fast path that SqlSchema::ReadAllTables can use instead of the per-table ODBC catalog loop.
Defaults to false, meaning the generic per-table catalog reader is used. SQL Server returns true: it can answer the entire schema with a handful of sys.* queries, collapsing thousands of per-table round-trips into a few. The batched path must produce byte-identical schema metadata to the legacy path.
true if the dialect supports the batched fast path, false otherwise. Definition at line 212 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Largest number of values this dialect should be handed in a single WHERE ... IN (...) predicate.
The batched relation loading behind Query<Record>().With<&Record::relation>() resolves a whole result set through IN predicates over the collected keys. Those keys are rendered as literals, so a large batch would otherwise produce one enormous expression — SQL Server raises "an expression services limit has been reached", and SQLite has its own parser limits. Splitting the batch into chunks of this size keeps every generated statement inside what the dialect accepts; the cost is one extra round-trip per chunk, still a constant number of queries per relation instead of one per record.
IN predicate. Defaults to 1000 for every dialect. Definition at line 229 of file SqlQueryFormatter.hpp.
|
inlinestatic |
Builds the canonical foreign-key constraint name for a set of columns.
Produces FK_<table>_<col1>[_<col2>…]. A single-column FK collapses to FK_<table>_<col>; a composite FK includes every column so that a composite FK whose first column matches an existing single-column FK doesn't collide on the constraint name (which MSSQL enforces as globally unique per DB).
Consumers include the SQL Server, PostgreSQL and SQLite formatters — centralising the convention here keeps CREATE/ALTER emission in sync with DROP CONSTRAINT lookup (e.g. SQLiteRebuildDropForeignKey).
Definition at line 245 of file SqlQueryFormatter.hpp.
|
inlinevirtual |
Returns the SQL statement to execute after connect to make schema the connection-level default for unqualified DDL/DML.
Returns an empty string when the DBMS has no session-level concept of a default schema (SQL Server, SQLite). Callers must skip emission for empty results. PostgreSQL implements this via SET search_path TO ....
The schema value is interpolated into the statement; callers must validate it (e.g. whitelist [A-Za-z0-9_]) before invoking.
Definition at line 266 of file SqlQueryFormatter.hpp.
|
pure virtual |
Returns the dialect-specific handler used by SqlScopedLock to acquire and release named cross-process advisory locks.
The returned reference is to a process-singleton, valid for the lifetime of the program. Each backend implements its own primitive — SQL Server uses sp_getapplock, PostgreSQL uses pg_advisory_lock, SQLite uses a lock table — and the implementation lives in the backend's formatter translation unit, so adding a new dialect only touches that unit and SqlScopedLock itself stays dialect-agnostic.
|
pure virtualnoexcept |
Returns the dialect's transient-error classifier, used by SqlRetryPolicy to decide whether a failed statement is worth another attempt.
Routing the classification through the formatter is what keeps per-DBMS error-code knowledge out of business logic — the same reason AdvisoryLockOps() exists.