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<bool>
34 static constexpr auto value = SqlColumnTypeDefinitions::Bool {};
38 struct SqlColumnTypeDefinitionOf<char>
40 static constexpr auto value = SqlColumnTypeDefinitions::Char { 1 };
44 struct SqlColumnTypeDefinitionOf<SqlDate>
46 static constexpr auto value = SqlColumnTypeDefinitions::Date {};
50 struct SqlColumnTypeDefinitionOf<SqlDateTime>
52 static constexpr auto value = SqlColumnTypeDefinitions::DateTime {};
56 struct SqlColumnTypeDefinitionOf<SqlTime>
58 static constexpr auto value = SqlColumnTypeDefinitions::Time {};
61 template <
size_t Precision,
size_t Scale>
62 struct SqlColumnTypeDefinitionOf<SqlNumeric<Precision, Scale>>
64 static constexpr auto value = SqlColumnTypeDefinitions::Decimal { .precision = Precision, .scale = Scale };
68 struct SqlColumnTypeDefinitionOf<SqlGuid>
70 static constexpr auto value = SqlColumnTypeDefinitions::Guid {};
74 requires(detail::OneOf<T, int16_t, uint16_t>)
75 struct SqlColumnTypeDefinitionOf<T>
77 static constexpr auto value = SqlColumnTypeDefinitions::Smallint {};
81 requires(detail::OneOf<T, int32_t, uint32_t>)
82 struct SqlColumnTypeDefinitionOf<T>
84 static constexpr auto value = SqlColumnTypeDefinitions::Integer {};
88 requires(detail::OneOf<T, int64_t, uint64_t>)
89 struct SqlColumnTypeDefinitionOf<T>
91 static constexpr auto value = SqlColumnTypeDefinitions::Bigint {};
95 requires(detail::OneOf<T, float, double>)
96 struct SqlColumnTypeDefinitionOf<T>
98 static constexpr auto value = SqlColumnTypeDefinitions::Real {};
101 template <
size_t N,
typename CharT>
102 requires(detail::OneOf<CharT, char>)
103 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
105 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
108 template <
size_t N,
typename CharT>
109 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
110 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::VARIABLE_SIZE>>
112 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
115 template <
size_t N,
typename CharT>
116 requires(detail::OneOf<CharT, char>)
117 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
119 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
122 template <
size_t N,
typename CharT>
123 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
124 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE>>
126 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
129 template <
size_t N,
typename CharT>
130 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
132 static constexpr auto value = SqlColumnTypeDefinitions::Char { N };
135 template <
size_t N,
typename CharT>
136 requires(detail::OneOf<CharT, char16_t, char32_t, wchar_t>)
137 struct SqlColumnTypeDefinitionOf<SqlFixedString<N, CharT, SqlFixedStringMode::FIXED_SIZE_RIGHT_TRIMMED>>
139 static constexpr auto value = SqlColumnTypeDefinitions::NChar { N };
142 template <
size_t N,
typename CharT>
143 requires(detail::OneOf<CharT, char>)
144 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
146 static constexpr auto value = SqlColumnTypeDefinitions::Varchar { N };
149 template <
size_t N,
typename CharT>
150 requires(detail::OneOf<CharT, char8_t, char16_t, char32_t, wchar_t>)
151 struct SqlColumnTypeDefinitionOf<SqlDynamicString<N, CharT>>
153 static constexpr auto value = SqlColumnTypeDefinitions::NVarchar { N };
156 template <
typename T>
157 struct SqlColumnTypeDefinitionOf<std::optional<T>>
159 static constexpr auto value = SqlColumnTypeDefinitionOf<T>::value;
168constexpr auto SqlColumnTypeDefinitionOf = detail::SqlColumnTypeDefinitionOf<T>::value;
175enum class SqlPrimaryKeyType : uint8_t
210 std::optional<SqlForeignKeyReferenceDefinition>
foreignKey {};
222struct SqlCreateTablePlan
224 std::string tableName;
225 std::vector<SqlColumnDeclaration> columns;
228enum class SqlNullable : uint8_t
234namespace SqlAlterTableCommands
239 std::string_view newTableName;
244 std::string columnName;
245 SqlColumnTypeDefinition columnType;
246 SqlNullable nullable = SqlNullable::Null;
251 std::string columnName;
252 SqlColumnTypeDefinition columnType;
253 SqlNullable nullable = SqlNullable::Null;
258 std::string_view columnName;
264 std::string_view oldColumnName;
265 std::string_view newColumnName;
270 std::string_view columnName;
275 std::string_view columnName;
280 std::string columnName;
281 SqlForeignKeyReferenceDefinition referencedColumn;
284 struct DropForeignKey
286 std::string columnName;
294using SqlAlterTableCommand = std::variant<SqlAlterTableCommands::RenameTable,
295 SqlAlterTableCommands::AddColumn,
296 SqlAlterTableCommands::AlterColumn,
297 SqlAlterTableCommands::AddIndex,
298 SqlAlterTableCommands::RenameColumn,
299 SqlAlterTableCommands::DropColumn,
300 SqlAlterTableCommands::DropIndex,
301 SqlAlterTableCommands::AddForeignKey,
302 SqlAlterTableCommands::DropForeignKey>;
332using SqlMigrationPlanElement = std::variant<
348[[nodiscard]] LIGHTWEIGHT_API std::vector<std::string> ToSql(
SqlQueryFormatter const& formatter,
349 SqlMigrationPlanElement
const& element);
359 std::vector<SqlMigrationPlanElement> steps {};
361 [[nodiscard]] LIGHTWEIGHT_API std::vector<std::string> ToSql()
const;
Represents a SQL ALTER TABLE plan on a given table.
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.
bool unique
Indicates if the column is unique.
Represents a SQL DROP TABLE plan.
std::string_view tableName
The name of the table to drop.
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 migration plan.