Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Lightweight::SqlQueryFormatter Class Referenceabstract

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 >
 

Public Member Functions

 SqlQueryFormatter (SqlQueryFormatter &&)=default
 
 SqlQueryFormatter (SqlQueryFormatter const &)=default
 
SqlQueryFormatteroperator= (SqlQueryFormatter &&)=default
 
SqlQueryFormatteroperator= (SqlQueryFormatter const &)=default
 
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
 
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
 
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, size_t count) const =0
 Constructs an SQL SELECT query for the first row.
 
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) const =0
 Constructs an SQL SELECT query retrieve the count of rows matching the given condition.
 
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
 

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.
 

Static Protected Member Functions

static std::string FormatTableName (std::string_view schema, std::string_view table)
 

Detailed Description

API to format SQL queries for different SQL dialects.

Definition at line 19 of file SqlQueryFormatter.hpp.

Member Typedef Documentation

◆ StringList

using Lightweight::SqlQueryFormatter::StringList = std::vector<std::string>

Definition at line 120 of file SqlQueryFormatter.hpp.

Member Function Documentation

◆ QualifiedTableName()

virtual std::string Lightweight::SqlQueryFormatter::QualifiedTableName ( std::string_view  schema,
std::string_view  table 
) const
pure virtual

Formats a qualified table name with proper quoting for this database.

Parameters
schemaThe schema name (can be empty for default schema)
tableThe table name
Returns
The properly quoted qualified table name (e.g., "schema"."table" or [schema].[table])

◆ Insert()

virtual std::string Lightweight::SqlQueryFormatter::Insert ( std::string_view  intoTable,
std::string_view  fields,
std::string_view  values 
) const
pure virtual

Constructs an SQL INSERT query.

Parameters
intoTableThe table to insert into.
fieldsThe fields to insert into.
valuesThe values to insert.

The fields and values must be in the same order.

◆ CreateTable()

virtual StringList Lightweight::SqlQueryFormatter::CreateTable ( std::string_view  schema,
std::string_view  tableName,
std::vector< SqlColumnDeclaration > const &  columns,
std::vector< SqlCompositeForeignKeyConstraint > const &  foreignKeys,
bool  ifNotExists = false 
) const
pure virtual

Constructs an SQL CREATE TABLE query.

Parameters
schemaThe schema name of the table to create.
tableNameThe name of the table to create.
columnsThe columns of the table.
foreignKeysThe foreign key constraints of the table.
ifNotExistsIf true, generates CREATE TABLE IF NOT EXISTS instead of CREATE TABLE.

◆ DropTable()

virtual StringList Lightweight::SqlQueryFormatter::DropTable ( std::string_view  schema,
std::string_view const &  tableName,
bool  ifExists = false,
bool  cascade = false 
) const
pure virtual

Constructs an SQL DROP TABLE query.

Parameters
schemaThe schema name of the table to drop.
tableNameThe name of the table to drop.
ifExistsIf true, generates DROP TABLE IF EXISTS instead of DROP TABLE.
cascadeIf true, drops all foreign key constraints referencing this table first.

◆ QueryServerVersion()

virtual std::string Lightweight::SqlQueryFormatter::QueryServerVersion ( ) const
pure virtual

Returns the SQL query to retrieve the full server version string.

This query returns detailed version information specific to each database:

  • SQL Server: Returns result of SELECT @VERSION (includes build, edition, OS info)
  • PostgreSQL: Returns result of SELECT version() (includes build info)
  • SQLite: Returns result of SELECT sqlite_version()

The documentation for this class was generated from the following file: