6#include "../SqlColumnTypeDefinitions.hpp"
7#include "../SqlDataBinder.hpp"
10#include <reflection-cpp/reflection.hpp>
20class SqlQueryFormatter;
26 struct SqlColumnTypeDefinitionOf
28 static_assert(AlwaysFalse<T>,
"Unsupported type for SQL column definition.");
32 struct SqlColumnTypeDefinitionOf<std::string>
34 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
38 struct SqlColumnTypeDefinitionOf<bool>
40 static constexpr auto value = SqlColumnTypeDefinitions::Bool {};
44 struct SqlColumnTypeDefinitionOf<char>
46 static constexpr auto value = SqlColumnTypeDefinitions::Char { 1 };
50 struct SqlColumnTypeDefinitionOf<SqlDate>
52 static constexpr auto value = SqlColumnTypeDefinitions::Date {};
56 struct SqlColumnTypeDefinitionOf<SqlDateTime>
58 static constexpr auto value = SqlColumnTypeDefinitions::DateTime {};
62 struct SqlColumnTypeDefinitionOf<SqlTime>
64 static constexpr auto value = SqlColumnTypeDefinitions::Time {};
67 template <
size_t Precision,
size_t Scale>
68 struct SqlColumnTypeDefinitionOf<SqlNumeric<Precision, Scale>>
70 static constexpr auto value = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
74 struct SqlColumnTypeDefinitionOf<SqlGuid>
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>
136 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
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>
163 struct SqlColumnTypeDefinitionOf<std::optional<T>>
165 static constexpr auto value = SqlColumnTypeDefinitionOf<T>::value;
169 struct SqlColumnTypeDefinitionOf<SqlText>
171 static constexpr auto value = SqlColumnTypeDefinitions::Text {};
175 struct SqlColumnTypeDefinitionOf<SqlDynamicBinary<N>>
177 static constexpr auto value = SqlColumnTypeDefinitions::VarBinary { N };
186constexpr auto SqlColumnTypeDefinitionOf = detail::SqlColumnTypeDefinitionOf<T>::value;
193enum class SqlPrimaryKeyType : uint8_t
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;
335 std::string columnName;
336 SqlColumnTypeDefinition columnType;
337 SqlNullable nullable = SqlNullable::Null;
343 std::string columnName;
349 std::string columnName;
357using SqlAlterTableCommand = std::variant<SqlAlterTableCommands::RenameTable,
358 SqlAlterTableCommands::AddColumn,
360 SqlAlterTableCommands::AlterColumn,
361 SqlAlterTableCommands::AddIndex,
362 SqlAlterTableCommands::RenameColumn,
363 SqlAlterTableCommands::DropColumn,
365 SqlAlterTableCommands::DropIndex,
367 SqlAlterTableCommands::AddForeignKey,
368 SqlAlterTableCommands::AddCompositeForeignKey,
369 SqlAlterTableCommands::DropForeignKey>;
428 std::vector<std::pair<std::string, SqlVariant>>
columns;
513using SqlMigrationPlanElement = std::variant<
534[[nodiscard]] LIGHTWEIGHT_API std::vector<std::string> ToSql(SqlQueryFormatter
const& formatter,
535 SqlMigrationPlanElement
const& element);
545 std::vector<SqlMigrationPlanElement> steps {};
547 [[nodiscard]] LIGHTWEIGHT_API std::vector<std::string> ToSql()
const;
558[[nodiscard]] LIGHTWEIGHT_API std::vector<std::string> ToSql(
SqlQueryFormatter const& formatter,
559 std::vector<SqlMigrationPlan>
const& plans);
Adds a column only if it does not already exist.
Drops a column only if it exists.
Drops an index only if it exists.
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.
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.