6#include "../SqlColumnTypeDefinitions.hpp"
7#include "../SqlDataBinder.hpp"
10#include <reflection-cpp/reflection.hpp>
20class SqlQueryFormatter;
28 static_assert(AlwaysFalse<T>,
"Unsupported type for SQL column definition.");
34 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
40 static constexpr auto value = SqlColumnTypeDefinitions::Bool {};
46 static constexpr auto value = SqlColumnTypeDefinitions::Char { 1 };
52 static constexpr auto value = SqlColumnTypeDefinitions::Date {};
58 static constexpr auto value = SqlColumnTypeDefinitions::DateTime {};
64 static constexpr auto value = SqlColumnTypeDefinitions::Time {};
67 template <
size_t Precision,
size_t Scale>
70 static constexpr auto value = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
76 static constexpr auto value = SqlColumnTypeDefinitions::Guid {};
80 requires(detail::OneOf<T, int16_t, uint16_t>)
81 struct SqlColumnTypeDefinitionOf<T>
83 static constexpr auto value = SqlColumnTypeDefinitions::Smallint {};
87 requires(detail::OneOf<T, int32_t, uint32_t>)
88 struct SqlColumnTypeDefinitionOf<T>
90 static constexpr auto value = SqlColumnTypeDefinitions::Integer {};
94 requires(detail::OneOf<T, int64_t, uint64_t>)
95 struct SqlColumnTypeDefinitionOf<T>
97 static constexpr auto value = SqlColumnTypeDefinitions::Bigint {};
100 template <
typename T>
101 requires(detail::OneOf<T, float, double>)
102 struct SqlColumnTypeDefinitionOf<T>
104 static constexpr auto value = SqlColumnTypeDefinitions::Real {};
107 template <
size_t N,
typename CharT>
108 requires(detail::OneOf<CharT, char>)
109 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
111 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
114 template <
size_t N,
typename CharT>
115 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
116 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
118 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
121 template <
size_t N,
typename CharT>
122 requires(detail::OneOf<CharT, char>)
123 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
125 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
128 template <
size_t N,
typename CharT>
129 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
130 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
132 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
135 template <
size_t N,
typename CharT>
138 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
141 template <
size_t N,
typename CharT>
142 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
143 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
145 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
148 template <
size_t N,
typename CharT>
149 requires(detail::OneOf<CharT, char>)
150 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
152 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
155 template <
size_t N,
typename CharT>
156 requires(detail::OneOf<CharT, char8_t, char16_t, char32_t, wchar_t>)
157 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
159 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
162 template <
typename T>
165 static constexpr auto value = SqlColumnTypeDefinitionOf<T>::value;
171 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
177 static constexpr auto value = SqlColumnTypeDefinitions::VarBinary { N };
228 std::optional<SqlForeignKeyReferenceDefinition>
foreignKey {};
261struct SqlCreateTablePlan
263 std::string schemaName;
264 std::string tableName;
265 std::vector<SqlColumnDeclaration> columns;
266 std::vector<SqlCompositeForeignKeyConstraint> foreignKeys;
267 bool ifNotExists {
false };
270namespace SqlAlterTableCommands
275 std::string_view newTableName;
280 std::string columnName;
281 SqlColumnTypeDefinition columnType;
282 SqlNullable nullable = SqlNullable::Null;
287 std::string columnName;
288 SqlColumnTypeDefinition columnType;
289 SqlNullable nullable = SqlNullable::Null;
294 std::string_view columnName;
300 std::string_view oldColumnName;
301 std::string_view newColumnName;
306 std::string_view columnName;
311 std::string_view columnName;
316 std::string columnName;
317 SqlForeignKeyReferenceDefinition referencedColumn;
320 struct AddCompositeForeignKey
322 std::vector<std::string> columns;
323 std::string referencedTableName;
324 std::vector<std::string> referencedColumns;
327 struct DropForeignKey
329 std::string columnName;
363 SqlAlterTableCommands::AddColumn,
365 SqlAlterTableCommands::AlterColumn,
366 SqlAlterTableCommands::AddIndex,
367 SqlAlterTableCommands::RenameColumn,
368 SqlAlterTableCommands::DropColumn,
370 SqlAlterTableCommands::DropIndex,
372 SqlAlterTableCommands::AddForeignKey,
373 SqlAlterTableCommands::AddCompositeForeignKey,
374 SqlAlterTableCommands::DropForeignKey>;
433 std::vector<std::pair<std::string, SqlVariant>>
columns;
552 std::vector<SqlMigrationPlanElement> steps {};
555 [[nodiscard]] LIGHTWEIGHT_API std::vector<std::string>
ToSql()
const;
567 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.
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 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::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::string whereColumn
The column name for the WHERE clause.
Represents a value that can be any of the supported SQL data types.