6#include "../SqlColumnTypeDefinitions.hpp"
7#include "../SqlDataBinder.hpp"
10#include <reflection-cpp/reflection.hpp>
25class SqlQueryFormatter;
33 static_assert(AlwaysFalse<T>,
"Unsupported type for SQL column definition.");
39 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
45 static constexpr auto value = SqlColumnTypeDefinitions::Bool {};
51 static constexpr auto value = SqlColumnTypeDefinitions::Char { 1 };
57 static constexpr auto value = SqlColumnTypeDefinitions::Date {};
63 static constexpr auto value = SqlColumnTypeDefinitions::DateTime {};
69 static constexpr auto value = SqlColumnTypeDefinitions::Time {};
72 template <
size_t Precision,
size_t Scale>
75 static constexpr auto value = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
81 static constexpr auto value = SqlColumnTypeDefinitions::Guid {};
85 requires(detail::OneOf<T, int16_t, uint16_t>)
86 struct SqlColumnTypeDefinitionOf<T>
88 static constexpr auto value = SqlColumnTypeDefinitions::Smallint {};
92 requires(detail::OneOf<T, int32_t, uint32_t>)
93 struct SqlColumnTypeDefinitionOf<T>
95 static constexpr auto value = SqlColumnTypeDefinitions::Integer {};
99 requires(detail::OneOf<T, int64_t, uint64_t>)
100 struct SqlColumnTypeDefinitionOf<T>
102 static constexpr auto value = SqlColumnTypeDefinitions::Bigint {};
105 template <
typename T>
106 requires(detail::OneOf<T, float, double>)
107 struct SqlColumnTypeDefinitionOf<T>
109 static constexpr auto value = SqlColumnTypeDefinitions::Real {};
112 template <
size_t N,
typename CharT>
113 requires(detail::OneOf<CharT, char>)
114 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
116 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
119 template <
size_t N,
typename CharT>
120 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
121 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
123 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
126 template <
size_t N,
typename CharT>
127 requires(detail::OneOf<CharT, char>)
128 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
130 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
133 template <
size_t N,
typename CharT>
134 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
135 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
137 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
140 template <
size_t N,
typename CharT>
143 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
146 template <
size_t N,
typename CharT>
147 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
148 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
150 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
153 template <
size_t N,
typename CharT>
154 requires(detail::OneOf<CharT, char>)
155 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
157 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
160 template <
size_t N,
typename CharT>
161 requires(detail::OneOf<CharT, char8_t, char16_t, char32_t, wchar_t>)
162 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
164 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
167 template <
typename T>
170 static constexpr auto value = SqlColumnTypeDefinitionOf<T>::value;
176 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
182 static constexpr auto value = SqlColumnTypeDefinitions::VarBinary { N };
233 std::optional<SqlForeignKeyReferenceDefinition>
foreignKey {};
266struct SqlCreateTablePlan
268 std::string schemaName;
269 std::string tableName;
270 std::vector<SqlColumnDeclaration> columns;
271 std::vector<SqlCompositeForeignKeyConstraint> foreignKeys;
272 bool ifNotExists {
false };
275namespace SqlAlterTableCommands
280 std::string_view newTableName;
285 std::string columnName;
286 SqlColumnTypeDefinition columnType;
287 SqlNullable nullable = SqlNullable::Null;
292 std::string columnName;
293 SqlColumnTypeDefinition columnType;
294 SqlNullable nullable = SqlNullable::Null;
299 std::string_view columnName;
305 std::string_view oldColumnName;
306 std::string_view newColumnName;
311 std::string_view columnName;
316 std::string_view columnName;
321 std::string columnName;
322 SqlForeignKeyReferenceDefinition referencedColumn;
325 struct AddCompositeForeignKey
327 std::vector<std::string> columns;
328 std::string referencedTableName;
329 std::vector<std::string> referencedColumns;
332 struct DropForeignKey
334 std::string columnName;
368 SqlAlterTableCommands::AddColumn,
370 SqlAlterTableCommands::AlterColumn,
371 SqlAlterTableCommands::AddIndex,
372 SqlAlterTableCommands::RenameColumn,
373 SqlAlterTableCommands::DropColumn,
375 SqlAlterTableCommands::DropIndex,
377 SqlAlterTableCommands::AddForeignKey,
378 SqlAlterTableCommands::AddCompositeForeignKey,
379 SqlAlterTableCommands::DropForeignKey>;
438 std::vector<std::pair<std::string, SqlVariant>>
columns;
557inline constexpr std::string_view CompatFlagLupTruncateName =
"lup-truncate";
579 bool lupTruncate =
false;
606 std::size_t value { 0 };
607 WidthUnit unit { WidthUnit::Characters };
644 std::uint64_t activeMigrationTimestamp = 0;
676 std::vector<SqlMigrationPlanElement> steps {};
679 [[nodiscard]] LIGHTWEIGHT_API std::vector<std::string>
ToSql()
const;
691 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)
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::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.
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 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::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.