6#include "../SqlColumnTypeDefinitions.hpp"
7#include "../SqlDataBinder.hpp"
10#include <reflection-cpp/reflection.hpp>
26class SqlQueryFormatter;
34 static_assert(AlwaysFalse<T>,
"Unsupported type for SQL column definition.");
40 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
46 static constexpr auto value = SqlColumnTypeDefinitions::Bool {};
52 static constexpr auto value = SqlColumnTypeDefinitions::Char { 1 };
58 static constexpr auto value = SqlColumnTypeDefinitions::Date {};
64 static constexpr auto value = SqlColumnTypeDefinitions::DateTime {};
70 static constexpr auto value = SqlColumnTypeDefinitions::Time {};
73 template <
size_t Precision,
size_t Scale>
76 static constexpr auto value = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
82 static constexpr auto value = SqlColumnTypeDefinitions::Guid {};
86 requires(detail::OneOf<T, int16_t, uint16_t>)
87 struct SqlColumnTypeDefinitionOf<T>
89 static constexpr auto value = SqlColumnTypeDefinitions::Smallint {};
93 requires(detail::OneOf<T, int32_t, uint32_t>)
94 struct SqlColumnTypeDefinitionOf<T>
96 static constexpr auto value = SqlColumnTypeDefinitions::Integer {};
100 requires(detail::OneOf<T, int64_t, uint64_t>)
101 struct SqlColumnTypeDefinitionOf<T>
103 static constexpr auto value = SqlColumnTypeDefinitions::Bigint {};
109 static constexpr auto value = SqlColumnTypeDefinitions::Real { .precision = 24 };
115 static constexpr auto value = SqlColumnTypeDefinitions::Real { .precision = 53 };
118 template <
size_t N,
typename CharT>
119 requires(detail::OneOf<CharT, char>)
120 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
122 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
125 template <
size_t N,
typename CharT>
126 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
127 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
129 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
132 template <
size_t N,
typename CharT>
133 requires(detail::OneOf<CharT, char>)
134 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
136 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
139 template <
size_t N,
typename CharT>
140 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
141 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
143 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
146 template <
size_t N,
typename CharT>
149 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
152 template <
size_t N,
typename CharT>
153 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
154 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
156 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
159 template <
size_t N,
typename CharT>
160 requires(detail::OneOf<CharT, char>)
161 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
163 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
166 template <
size_t N,
typename CharT>
167 requires(detail::OneOf<CharT, char8_t, char16_t, char32_t, wchar_t>)
168 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
170 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
173 template <
typename T>
176 static constexpr auto value = SqlColumnTypeDefinitionOf<T>::value;
182 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
188 static constexpr auto value = SqlColumnTypeDefinitions::VarBinary { N };
239 std::optional<SqlForeignKeyReferenceDefinition>
foreignKey {};
272struct SqlCreateTablePlan
274 std::string schemaName;
275 std::string tableName;
276 std::vector<SqlColumnDeclaration> columns;
277 std::vector<SqlCompositeForeignKeyConstraint> foreignKeys;
278 bool ifNotExists {
false };
281namespace SqlAlterTableCommands
286 std::string_view newTableName;
291 std::string columnName;
292 SqlColumnTypeDefinition columnType;
293 SqlNullable nullable = SqlNullable::Null;
298 std::string columnName;
299 SqlColumnTypeDefinition columnType;
300 SqlNullable nullable = SqlNullable::Null;
305 std::string_view columnName;
311 std::string_view oldColumnName;
312 std::string_view newColumnName;
317 std::string_view columnName;
322 std::string_view columnName;
327 std::string columnName;
328 SqlForeignKeyReferenceDefinition referencedColumn;
331 struct AddCompositeForeignKey
333 std::vector<std::string> columns;
334 std::string referencedTableName;
335 std::vector<std::string> referencedColumns;
337 std::string referencedSchemaName {};
340 struct DropForeignKey
342 std::string columnName;
376 SqlAlterTableCommands::AddColumn,
378 SqlAlterTableCommands::AlterColumn,
379 SqlAlterTableCommands::AddIndex,
380 SqlAlterTableCommands::RenameColumn,
381 SqlAlterTableCommands::DropColumn,
383 SqlAlterTableCommands::DropIndex,
385 SqlAlterTableCommands::AddForeignKey,
386 SqlAlterTableCommands::AddCompositeForeignKey,
387 SqlAlterTableCommands::DropForeignKey>;
446 std::vector<std::pair<std::string, SqlVariant>>
columns;
579inline constexpr std::string_view CompatFlagLupTruncateName =
"lup-truncate";
586inline constexpr std::string_view CompatFlagFkMatchReferencedTextName =
"fk-match-referenced-text";
608 bool lupTruncate =
false;
614 bool fkMatchReferencedText =
false;
641 std::size_t value { 0 };
642 WidthUnit unit { WidthUnit::Characters };
683 std::uint64_t activeMigrationTimestamp = 0;
760 std::vector<SqlMigrationPlanElement> steps {};
763 [[nodiscard]] LIGHTWEIGHT_API std::vector<std::string>
ToSql()
const;
775 std::vector<SqlMigrationPlan>
const& plans);
std::variant< SqlAlterTableCommands::RenameTable, SqlAlterTableCommands::AddColumn, SqlAlterTableCommands::AddColumnIfNotExists, SqlAlterTableCommands::AlterColumn, SqlAlterTableCommands::AddIndex, SqlAlterTableCommands::RenameColumn, SqlAlterTableCommands::DropColumn, SqlAlterTableCommands::DropColumnIfExists, SqlAlterTableCommands::DropIndex, SqlAlterTableCommands::DropIndexIfExists, SqlAlterTableCommands::AddForeignKey, SqlAlterTableCommands::AddCompositeForeignKey, SqlAlterTableCommands::DropForeignKey > SqlAlterTableCommand
Represents a single SQL ALTER TABLE command.
LIGHTWEIGHT_API std::vector< std::string > ToSql(SqlQueryFormatter const &formatter, SqlMigrationPlanElement const &element)
LIGHTWEIGHT_API std::optional< SqlBoundStatement > ToBoundSql(SqlMigrationPlanElement const &element)
Renders an Insert(...).IfNotExists(...) step for execution with bound parameters.
SqlPrimaryKeyType
Represents a primary key type.
std::variant< SqlCreateTablePlan, SqlAlterTablePlan, SqlDropTablePlan, SqlCreateIndexPlan, SqlRawSqlPlan, SqlInsertDataPlan, SqlUpdateDataPlan, SqlDeleteDataPlan > SqlMigrationPlanElement
Represents a single SQL migration plan element.
constexpr auto SqlColumnTypeDefinitionOf
Represents a SQL column type definition of T.
std::string schema
Schema label (empty for engines without schemas, e.g. SQLite).
std::string table
Table name.
auto operator<=>(ColumnKey const &) const =default
Total ordering on (schema, table, column) — defaulted three-way comparison.
Tables for which the lookup has already been attempted, so we don't re-query for every INSERT against...
std::string schema
Schema label (empty when the engine reports none).
auto operator<=>(TableKey const &) const =default
Total ordering on (schema, table) — defaulted three-way comparison.
Opt-in behavioural knobs that the ToSql migration-plan renderer honours.
std::string activeMigrationTitle
Identity of the migration currently being rendered. Set by MigrationManager::ApplySingleMigration bef...
std::map< ColumnKey, ColumnWidth > columnWidths
Cache of declared character widths, populated lazily as plan elements render.
WidthUnit
How a column's declared width is counted. The truncation layer must match the server's interpretation...
std::map< ColumnKey, SqlColumnTypeDefinition > textColumnTypes
std::function< void(MigrationRenderContext &, std::string_view, std::string_view)> widthLookup
Optional fallback that fetches column widths for a (schema, table) from the live database when the ca...
std::set< TableKey > lookupAttempted
Tables for which widthLookup has already been called; prevents repeat queries.
Adds a column only if it does not already exist.
SqlNullable nullable
Whether the column is nullable.
SqlColumnTypeDefinition columnType
The type of the column to add.
std::string columnName
The name of the column to add.
Drops a column only if it exists.
std::string columnName
The name of the column to drop.
Drops an index only if it exists.
std::string columnName
The name of the column whose index to drop.
Represents a SQL ALTER TABLE plan on a given table.
std::string_view schemaName
The schema name of the table to alter.
std::string_view tableName
The name of the table to alter.
std::vector< SqlAlterTableCommand > commands
The list of commands to execute on the table.
A statement whose values are bound as parameters when it runs.
std::vector< SqlVariant > parameters
The values bound to the placeholders, in order.
std::string sql
The statement text, with one ? placeholder per parameter.
Represents a SQL column declaration.
bool required
Indicates if the column is required (non-nullable).
bool index
Indicates if the column is indexed.
SqlColumnTypeDefinition type
The type of the column.
std::string name
The name of the column.
std::optional< SqlForeignKeyReferenceDefinition > foreignKey
The foreign key reference definition of the column.
SqlPrimaryKeyType primaryKey
The primary key type of the column.
std::string defaultValue
The default value of the column.
bool unique
Indicates if the column is unique.
uint16_t primaryKeyIndex
The 1-based index in the primary key (0 if not part of a specific order).
Represents a composite foreign key constraint.
std::vector< std::string > columns
The columns in the current table.
std::string referencedTableName
The referenced table name.
std::vector< std::string > referencedColumns
The referenced columns in the referenced table.
Represents a SQL CREATE INDEX plan for migrations.
std::vector< std::string > columns
The columns to include in the index.
std::string whereExpression
std::string schemaName
The schema name of the table to create the index on.
bool ifNotExists
If true, generates CREATE INDEX IF NOT EXISTS.
std::string indexName
The name of the index to create.
std::string tableName
The name of the table to create the index on.
bool unique
If true, creates a UNIQUE index.
Represents a SQL DELETE data plan for migrations.
std::string whereExpression
Pre-rendered WHERE-clause body. See SqlUpdateDataPlan::whereExpression.
std::string whereOp
The comparison operator for the WHERE clause (e.g., "=", "<>", etc.).
std::string tableName
The name of the table to delete from.
std::string whereColumn
The column name for the WHERE clause.
std::string schemaName
The schema name of the table to delete from.
SqlVariant whereValue
The value for the WHERE clause.
Represents a SQL DROP TABLE plan.
std::string_view schemaName
The schema name of the table to drop.
std::string_view tableName
The name of the table to drop.
bool ifExists
If true, generates DROP TABLE IF EXISTS instead of DROP TABLE.
Represents a foreign key reference definition.
std::string tableName
The table name that the foreign key references.
std::string columnName
The column name that the foreign key references.
Represents a SQL INSERT data plan for migrations.
std::vector< std::pair< std::string, SqlVariant > > columns
The columns and their values to insert.
std::string schemaName
The schema name of the table to insert into.
std::vector< std::string > keyColumns
Key columns that make the insert conditional, empty for a plain insert.
std::string tableName
The name of the table to insert into.
Represents a SQL migration plan.
SqlQueryFormatter const & formatter
The SQL query formatter to use.
LIGHTWEIGHT_API std::vector< std::string > ToSql() const
Converts the migration plan to a list of SQL statements.
Represents a raw SQL plan.
std::string_view sql
The raw SQL to execute.
Represents a SQL UPDATE data plan for migrations.
std::string schemaName
The schema name of the table to update.
SqlVariant whereValue
The value for the WHERE clause.
std::string whereExpression
Pre-rendered WHERE-clause body (the text after WHERE), used when the condition cannot be expressed wi...
std::vector< std::pair< std::string, SqlVariant > > setColumns
The columns and their values to set.
std::string tableName
The name of the table to update.
std::string whereOp
The comparison operator for the WHERE clause (e.g., "=", "<>", etc.).
std::vector< std::pair< std::string, std::string > > setExpressions
Column-to-expression assignments that cannot be represented as a literal value.
std::string whereColumn
The column name for the WHERE clause.
Represents a value that can be any of the supported SQL data types.