|
Lightweight 0.20261105.1
|
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 |
| 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 std::string_view | ReadUncommittedTableHint () const noexcept |
| The per-table hint that asks for a dirty read (READ UNCOMMITTED) of one table in a SELECT. | |
| 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 bool | DistinguishesNarrowAndWideText () const noexcept |
Whether the dialect treats narrow (CHAR/VARCHAR) and wide (NCHAR/NVARCHAR) text as distinct types — distinct enough that a foreign key between them is rejected and changing a column between them needs its dependent constraints and indexes dropped first. | |
| 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 StringList | SqliteSettingsStatements (SqliteConnectionSettings const &settings) const |
Returns the statements a connection executes after connecting to realise settings. | |
| 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 |
| virtual bool | IdentifiersAreCaseInsensitive () const noexcept |
Whether the dialect resolves table and column names without regard to case, even when they are quoted — so a declared ITEM_NAME names the live column Item_Name. | |
| virtual bool | SupportsSchemas () const noexcept |
| Whether the dialect has schemas that tables can be created in. | |
| virtual std::string | CreateSchemaIfNotExists (std::string_view schema) const |
Returns the statement that creates schema unless it already exists. | |
| virtual std::string_view | ReportRowCountsStatement () const noexcept |
| Returns the statement that makes the session report affected-row counts again. | |
| virtual std::string_view | TableRowCountEstimateQuery () const noexcept |
| A query estimating a table's row count from the catalog's statistics, without reading the table. It takes two parameters, the schema name and then the table name, and yields one integer. | |
| virtual bool | HasNativeGuidType () const noexcept |
| Whether the dialect has a native GUID/UUID column type (as opposed to storing GUIDs as text). | |
| virtual bool | ComparesFixedCharWithPadSemantics () const noexcept |
| Whether comparing a fixed-width CHAR(n) column pads the shorter operand with spaces. | |
| virtual std::string | FoldUnquotedIdentifier (std::string_view identifier) const |
| Returns the name under which the dialect stores an unquoted identifier. | |
| virtual bool | ParameterStatusIdentifiesFailedRows () const noexcept |
| Whether the driver's parameter status array names exactly the parameter sets that failed when a batch (parameter array) execution fails. | |
| virtual bool | BoundsBulkLoadTransactionSize () const noexcept |
| Whether a bulk load should commit periodically rather than hold all its rows in one transaction. | |
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. | |
| static std::string | DoubleQuoteCharacter (std::string_view text, char quote) |
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 139 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 all rows.
In all Select* functions groupBy is the GROUP BY clause followed by the HAVING clause, if the query has one, and has to be emitted as one piece between the WHERE and the ORDER BY clause.
|
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 200 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
The per-table hint that asks for a dirty read (READ UNCOMMITTED) of one table in a SELECT.
Rendered right after a table reference (and its alias) in the FROM clause and in each JOIN of a query built with SqlSelectQueryBuilder::ReadUncommitted. Only that statement is affected; the connection's transaction isolation level is left alone.
Defaults to an empty string, meaning the dialect has no per-table hint and renders the statement unchanged. SQL Server returns " WITH (READUNCOMMITTED)".
Definition at line 216 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 231 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether the dialect treats narrow (CHAR/VARCHAR) and wide (NCHAR/NVARCHAR) text as distinct types — distinct enough that a foreign key between them is rejected and changing a column between them needs its dependent constraints and indexes dropped first.
Defaults to false (PostgreSQL and SQLite store both the same way). SQL Server returns true.
true if narrow and wide text are distinct types in this dialect. Definition at line 245 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 262 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 278 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 299 of file SqlQueryFormatter.hpp.
|
inlinevirtual |
Returns the statements a connection executes after connecting to realise settings.
Only the SQLite dialect has these settings, and emits one PRAGMA per setting; every other dialect returns an empty list, which leaves the settings inert there.
| settings | The connection's SQLite settings. |
Definition at line 312 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.
|
inlinevirtualnoexcept |
Whether the dialect resolves table and column names without regard to case, even when they are quoted — so a declared ITEM_NAME names the live column Item_Name.
Defaults to false. SQLite returns true. SQL Server returns true as well, which assumes a case-insensitive database collation (the default); it is not probed per database. PostgreSQL keeps quoted identifiers case-sensitive and returns false.
New hooks are appended at the end of the class: that keeps the slots of the earlier ones for callers, but a subclass outside this library must still be rebuilt.
true if identifiers differing only in ASCII letter case name the same object. Definition at line 347 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether the dialect has schemas that tables can be created in.
Defaults to true. SQLite returns false: it has no schemas, only attached databases.
true if a table can be created in a named schema. Definition at line 357 of file SqlQueryFormatter.hpp.
|
inlinevirtual |
Returns the statement that creates schema unless it already exists.
Returns an empty string when the dialect has no schemas (see SupportsSchemas()).
| schema | The schema to create. |
Definition at line 368 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Returns the statement that makes the session report affected-row counts again.
Defaults to an empty string, meaning the dialect always reports them. SQL Server returns SET NOCOUNT OFF: under NOCOUNT ON the driver may report 0 rows for a statement that changed rows. The setting stays in effect for the session after the statement ran.
Definition at line 381 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
A query estimating a table's row count from the catalog's statistics, without reading the table. It takes two parameters, the schema name and then the table name, and yields one integer.
SELECT COUNT(*), which reads the whole table. Definition at line 391 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether the dialect has a native GUID/UUID column type (as opposed to storing GUIDs as text).
Defaults to false. SQL Server (UNIQUEIDENTIFIER) and PostgreSQL (UUID) return true.
true if GUID columns are stored in a dedicated binary type. Definition at line 401 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether comparing a fixed-width CHAR(n) column pads the shorter operand with spaces.
Defaults to true. SQLite returns false: it stores CHAR(n) as plain text and compares it verbatim.
true if trailing spaces of CHAR(n) values are insignificant in comparisons. Definition at line 411 of file SqlQueryFormatter.hpp.
|
inlinevirtual |
Returns the name under which the dialect stores an unquoted identifier.
Defaults to returning identifier unchanged. PostgreSQL folds unquoted identifiers to lower case and returns the ASCII-lower-cased name.
| identifier | The identifier as written without quotes. |
Definition at line 423 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether the driver's parameter status array names exactly the parameter sets that failed when a batch (parameter array) execution fails.
Defaults to false. SQL Server returns true: ODBC Driver 18 executes every parameter set and marks only the failing ones SQL_PARAM_ERROR. PostgreSQL's psqlodbc marks every parameter set of a failed array SQL_PARAM_ERROR, and the SQLite ODBC driver does not fill the array at all, so their failing rows are found by bisection instead.
true if the rows marked SQL_PARAM_ERROR may be trusted to be the failing ones. Definition at line 437 of file SqlQueryFormatter.hpp.
|
inlinevirtualnoexcept |
Whether a bulk load should commit periodically rather than hold all its rows in one transaction.
Defaults to false. SQLite returns true: its write-ahead log grows with the open transaction, so a restore commits every RestoreSettings::maxRowsPerCommit rows of a chunk.
true if a bulk load bounds the rows per transaction. Definition at line 449 of file SqlQueryFormatter.hpp.
|
staticprotected |
Doubles every quote in text, so text can be embedded between two such quotes.
| text | The text to embed. |
| quote | The quote character that delimits the embedded text. |
text with each quote doubled.