Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Migrate.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../DataMapper/BelongsTo.hpp"
6#include "../DataMapper/Field.hpp"
7#include "../DataMapper/Record.hpp"
8#include "../Utils.hpp"
9#include "Core.hpp"
10#include "MigrationPlan.hpp"
11
12#include <reflection-cpp/reflection.hpp>
13
14namespace Lightweight
15{
16
17/// @brief Index type for simplified CreateIndex API.
18/// @ingroup QueryBuilder
19enum class IndexType : std::uint8_t
20{
21 NonUnique, ///< Regular (non-unique) index
22 Unique ///< Unique index
23};
24
25/// @brief Query builder for building CREATE TABLE queries.
26///
27/// @see SqlQueryBuilder
28/// @ingroup QueryBuilder
29class [[nodiscard]] SqlCreateTableQueryBuilder final
30{
31 public:
32 explicit SqlCreateTableQueryBuilder(SqlCreateTablePlan& plan):
33 _plan { plan }
34 {
35 }
36
37 /// Adds a new column to the table.
39
40 /// Creates a new nullable column.
41 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& Column(std::string columnName, SqlColumnTypeDefinition columnType);
42
43 /// Creates a new column that is non-nullable.
44 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& RequiredColumn(std::string columnName, SqlColumnTypeDefinition columnType);
45
46 /// Adds the created_at and updated_at columns to the table.
48
49 /// Creates a new primary key column.
50 /// Primary keys are always required, unique, have an index, and are non-nullable.
51 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& PrimaryKey(std::string columnName, SqlColumnTypeDefinition columnType);
52
53 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& PrimaryKeyWithAutoIncrement(
54 std::string columnName, SqlColumnTypeDefinition columnType = SqlColumnTypeDefinitions::Bigint {});
55
56 /// Creates a new nullable foreign key column.
57 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& ForeignKey(std::string columnName,
58 SqlColumnTypeDefinition columnType,
60
61 /// Creates a new non-nullable foreign key column.
62 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& RequiredForeignKey(std::string columnName,
63 SqlColumnTypeDefinition columnType,
65
66 /// Adds a composite foreign key constraint.
67 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& ForeignKey(std::vector<std::string> columns,
68 std::string referencedTableName,
69 std::vector<std::string> referencedColumns);
70
71 /// Enables the UNIQUE constraint on the last declared column.
73
74 /// Enables the INDEX constraint on the last declared column.
76
77 /// Enables the UNIQUE and INDEX constraint on the last declared column and makes it an index.
79
80 private:
81 SqlCreateTablePlan& _plan;
82};
83
84/// @brief Query builder for building ALTER TABLE queries.
85///
86/// @see SqlQueryBuilder
87/// @ingroup QueryBuilder
88class [[nodiscard]] SqlAlterTableQueryBuilder final
89{
90 public:
92 _plan { plan }
93 {
94 }
95
96 /// Renames the table.
97 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& RenameTo(std::string_view newTableName);
98
99 /// Adds a new column to the table that is non-nullable.
100 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddColumn(std::string columnName, SqlColumnTypeDefinition columnType);
101
102 /// Adds a new column to the table that is non-nullable.
103 ///
104 /// @tparam MemberPointer The pointer to the member field in the record.
105 ///
106 /// @see AddColumn(std::string, SqlColumnTypeDefinition)
107 template <auto MemberPointer>
109 {
110 return AddColumn(std::string(FieldNameOf<MemberPointer>),
111 SqlColumnTypeDefinitionOf<ReferencedFieldTypeOf<MemberPointer>>);
112 }
113
114 /// Adds a new column to the table that is nullable.
115 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddNotRequiredColumn(std::string columnName,
116 SqlColumnTypeDefinition columnType);
117
118 /// Adds a new column to the table that is nullable.
119 ///
120 /// @tparam MemberPointer The pointer to the member field in the record.
121 ///
122 /// @see AddNotRequiredColumn(std::string, SqlColumnTypeDefinition)
123 template <auto MemberPointer>
125 {
126 return AddNotRequiredColumn(std::string(FieldNameOf<MemberPointer>),
127 SqlColumnTypeDefinitionOf<ReferencedFieldTypeOf<MemberPointer>>);
128 }
129
130 /// @brief Alters the column to have a new non-nullable type.
131 ///
132 /// @param columnName The name of the column to alter.
133 /// @param columnType The new type of the column.
134 /// @param nullable The new nullable state of the column.
135 ///
136 /// @return The current query builder for chaining.
137 ///
138 /// @see SqlColumnTypeDefinition
139 ///
140 /// @code
141 /// auto stmt = SqlStatement();
142 /// auto sqlMigration = stmt.Migration()
143 /// .AlterTable("Table")
144 /// .AlterColumn("column", Integer {}, SqlNullable::NotNull)
145 /// .GetPlan().ToSql();
146 /// for (auto const& sql: sqlMigration)
147 /// stmt.ExecuteDirect(sql);
148 /// @endcode
149 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AlterColumn(std::string columnName,
150 SqlColumnTypeDefinition columnType,
151 SqlNullable nullable);
152
153 /// Renames a column.
154 /// @param oldColumnName The old column name.
155 /// @param newColumnName The new column name.
156 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& RenameColumn(std::string_view oldColumnName, std::string_view newColumnName);
157
158 /// Drops a column from the table.
159 /// @param columnName The name of the column to drop.
160 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropColumn(std::string_view columnName);
161
162 /// Adds a new column to the table only if it does not already exist.
163 ///
164 /// @note Database support varies:
165 /// - PostgreSQL: Native IF NOT EXISTS support
166 /// - SQL Server: Uses conditional IF NOT EXISTS query
167 /// - SQLite: Limited support (may require raw SQL)
168 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddColumnIfNotExists(std::string columnName,
169 SqlColumnTypeDefinition columnType);
170
171 /// Adds a new nullable column to the table only if it does not already exist.
172 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddNotRequiredColumnIfNotExists(std::string columnName,
173 SqlColumnTypeDefinition columnType);
174
175 /// Drops a column from the table only if it exists.
176 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropColumnIfExists(std::string_view columnName);
177
178 /// Add an index to the table for the specified column.
179 /// @param columnName The name of the column to index.
180 ///
181 /// @code
182 /// SqlQueryBuilder q;
183 /// q.Migration().AlterTable("Table").AddIndex("column");
184 /// // Will execute CREATE INDEX "Table_column_index" ON "Table" ("column");
185 /// @endcode
186 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddIndex(std::string_view columnName);
187
188 /// Add an index to the table for the specified column that is unique.
189 /// @param columnName The name of the column to index.
190 ///
191 /// @code
192 /// SqlQueryBuilder q;
193 /// q.Migration().AlterTable("Table").AddUniqueIndex("column");
194 /// // Will execute CREATE UNIQUE INDEX "Table_column_index" ON "Table" ("column");
195 /// @endcode
196 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddUniqueIndex(std::string_view columnName);
197
198 /// Drop an index from the table for the specified column.
199 /// @param columnName The name of the column to drop the index from.
200 ///
201 /// @code
202 /// SqlQueryBuilder q;
203 /// q.Migration().AlterTable("Table").DropIndex("column");
204 /// // Will execute DROP INDEX "Table_column_index";
205 /// @endcode
206 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropIndex(std::string_view columnName);
207
208 /// Drop an index from the table only if it exists.
209 /// @param columnName The name of the column to drop the index from.
210 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropIndexIfExists(std::string_view columnName);
211
212 /// Adds a foreign key column @p columnName to @p referencedColumn to an existing column.
213 ///
214 /// @param columnName The name of the column to add.
215 /// @param referencedColumn The column to reference.
216 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddForeignKey(std::string columnName,
217 SqlForeignKeyReferenceDefinition referencedColumn);
218
219 /// Adds a foreign key column @p columnName of type @p columnType to @p referencedColumn.
220 ///
221 /// @param columnName The name of the column to add.
222 /// @param columnType The type of the column to add.
223 /// @param referencedColumn The column to reference.
224 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddForeignKeyColumn(std::string columnName,
225 SqlColumnTypeDefinition columnType,
226 SqlForeignKeyReferenceDefinition referencedColumn);
227
228 /// Adds a nullable foreign key column @p columnName of type @p columnType to @p referencedColumn.
229 ///
230 /// @param columnName The name of the column to add.
231 /// @param columnType The type of the column to add.
232 /// @param referencedColumn The column to reference.
234 std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn);
235
236 /// Drops a foreign key for the column @p columnName from the table.
237 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropForeignKey(std::string columnName);
238
239 /// Adds a composite foreign key constraint.
240 ///
241 /// @param columns The columns in the current table.
242 /// @param referencedTableName The referenced table name.
243 /// @param referencedColumns The referenced columns in the referenced table.
244 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddCompositeForeignKey(std::vector<std::string> columns,
245 std::string referencedTableName,
246 std::vector<std::string> referencedColumns);
247
248 private:
249 SqlAlterTablePlan& _plan;
250};
251
252namespace detail
253{
254 template <typename Record>
255 void PopulateCreateTableBuilder(SqlCreateTableQueryBuilder& builder)
256 {
257 static_assert(DataMapperRecord<Record>, "Record must satisfy DataMapperRecord");
258
259#if defined(LIGHTWEIGHT_CXX26_REFLECTION)
260 constexpr auto ctx = std::meta::access_context::current();
261 template for (constexpr auto el: define_static_array(nonstatic_data_members_of(^^Record, ctx)))
262 {
263 using FieldType = typename[:std::meta::type_of(el):];
264 if constexpr (FieldWithStorage<FieldType>)
265 {
266 if constexpr (IsAutoIncrementPrimaryKey<FieldType>)
267 builder.PrimaryKeyWithAutoIncrement(
268 std::string(FieldNameOf<el>),
269 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
270 else if constexpr (FieldType::IsPrimaryKey)
271 builder.PrimaryKey(std::string(FieldNameOf<el>),
272 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
273 else if constexpr (IsBelongsTo<FieldType>)
274 {
275 constexpr size_t referencedFieldIndex = []() constexpr -> size_t {
276 auto index = size_t(-1);
277 Reflection::EnumerateMembers<typename FieldType::ReferencedRecord>(
278 [&index]<size_t J, typename ReferencedFieldType>() constexpr -> void {
279 if constexpr (IsField<ReferencedFieldType>)
280 if constexpr (ReferencedFieldType::IsPrimaryKey)
281 index = J;
282 });
283 return index;
284 }();
285 builder.ForeignKey(
286 std::string(FieldNameOf<el>),
287 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value,
288 SqlForeignKeyReferenceDefinition {
289 .tableName = std::string { RecordTableName<typename FieldType::ReferencedRecord> },
290 .columnName = std::string { FieldNameOf<FieldType::ReferencedField> } });
291 }
292 else if constexpr (FieldType::IsMandatory)
293 builder.RequiredColumn(std::string(FieldNameOf<el>),
294 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
295 else
296 builder.Column(std::string(FieldNameOf<el>),
297 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
298 }
299 }
300#else
301 Reflection::EnumerateMembers<Record>([&builder]<size_t I, typename FieldType>() {
302 if constexpr (FieldWithStorage<FieldType>)
303 {
304 if constexpr (IsAutoIncrementPrimaryKey<FieldType>)
305 builder.PrimaryKeyWithAutoIncrement(
306 std::string(FieldNameAt<I, Record>()),
307 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
308 else if constexpr (FieldType::IsPrimaryKey)
309 builder.PrimaryKey(std::string(FieldNameAt<I, Record>()),
310 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
311 else if constexpr (IsBelongsTo<FieldType>)
312 {
313 constexpr size_t referencedFieldIndex = []() constexpr -> size_t {
314 auto index = size_t(-1);
315 Reflection::EnumerateMembers<typename FieldType::ReferencedRecord>(
316 [&index]<size_t J, typename ReferencedFieldType>() constexpr -> void {
317 if constexpr (IsField<ReferencedFieldType>)
318 if constexpr (ReferencedFieldType::IsPrimaryKey)
319 index = J;
320 });
321 return index;
322 }();
323 builder.ForeignKey(
324 std::string(FieldNameAt<I, Record>()),
325 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value,
326 SqlForeignKeyReferenceDefinition {
327 .tableName = std::string { RecordTableName<typename FieldType::ReferencedRecord> },
328 .columnName =
329 std::string { FieldNameAt<referencedFieldIndex, typename FieldType::ReferencedRecord>() } });
330 }
331 else if constexpr (FieldType::IsMandatory)
332 builder.RequiredColumn(std::string(FieldNameAt<I, Record>()),
333 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
334 else
335 builder.Column(std::string(FieldNameAt<I, Record>()),
336 detail::SqlColumnTypeDefinitionOf<typename FieldType::ValueType>::value);
337 }
338 });
339#endif
340 }
341} // namespace detail
342
343/// @brief Query builder for building INSERT queries in migrations.
344///
345/// @see SqlMigrationQueryBuilder
346/// @ingroup QueryBuilder
347class [[nodiscard]] SqlMigrationInsertBuilder final
348{
349 public:
351 _plan { plan }
352 {
353 }
354
355 /// Sets a column value for the INSERT.
356 template <typename T>
357 SqlMigrationInsertBuilder& Set(std::string columnName, T const& value)
358 {
359 _plan.columns.emplace_back(std::move(columnName), SqlVariant(value));
360 return *this;
361 }
362
363 private:
364 SqlInsertDataPlan& _plan;
365};
366
367/// @brief Query builder for building UPDATE queries in migrations.
368///
369/// @see SqlMigrationQueryBuilder
370/// @ingroup QueryBuilder
371class [[nodiscard]] SqlMigrationUpdateBuilder final
372{
373 public:
375 _plan { plan }
376 {
377 }
378
379 /// Sets a column value for the UPDATE.
380 template <typename T>
381 SqlMigrationUpdateBuilder& Set(std::string columnName, T const& value)
382 {
383 _plan.setColumns.emplace_back(std::move(columnName), SqlVariant(value));
384 return *this;
385 }
386
387 /// Adds a WHERE condition to the UPDATE.
388 template <typename T>
389 SqlMigrationUpdateBuilder& Where(std::string columnName, std::string op, T const& value)
390 {
391 _plan.whereColumn = std::move(columnName);
392 _plan.whereOp = std::move(op);
393 _plan.whereValue = SqlVariant(value);
394 return *this;
395 }
396
397 private:
398 SqlUpdateDataPlan& _plan;
399};
400
401/// @brief Query builder for building DELETE queries in migrations.
402///
403/// @see SqlMigrationQueryBuilder
404/// @ingroup QueryBuilder
405class [[nodiscard]] SqlMigrationDeleteBuilder final
406{
407 public:
409 _plan { plan }
410 {
411 }
412
413 /// Adds a WHERE condition to the DELETE.
414 template <typename T>
415 SqlMigrationDeleteBuilder& Where(std::string columnName, std::string op, T const& value)
416 {
417 _plan.whereColumn = std::move(columnName);
418 _plan.whereOp = std::move(op);
419 _plan.whereValue = SqlVariant(value);
420 return *this;
421 }
422
423 private:
424 SqlDeleteDataPlan& _plan;
425};
426
427/// @brief Query builder for building SQL migration queries.
428/// @ingroup QueryBuilder
429class [[nodiscard]] SqlMigrationQueryBuilder final
430{
431 public:
432 explicit SqlMigrationQueryBuilder(SqlQueryFormatter const& formatter):
433 _formatter { formatter },
434 _migrationPlan { .formatter = formatter }
435 {
436 }
437
438 LIGHTWEIGHT_API SqlMigrationQueryBuilder& WithSchema(std::string schemaName);
439
440 /// Creates a new database.
441 LIGHTWEIGHT_API SqlMigrationQueryBuilder& CreateDatabase(std::string_view databaseName);
442
443 /// Drops a database.
444 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropDatabase(std::string_view databaseName);
445
446 /// Creates a new table.
447 LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTable(std::string_view tableName);
448
449 /// Creates a new table only if it does not already exist.
450 ///
451 /// @note Database support:
452 /// - SQLite: Native CREATE TABLE IF NOT EXISTS
453 /// - PostgreSQL: Native CREATE TABLE IF NOT EXISTS
454 /// - SQL Server: Uses conditional IF NOT EXISTS block
455 LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTableIfNotExists(std::string_view tableName);
456
457 /// Alters an existing table.
458 LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable(std::string_view tableName);
459
460 /// Drops a table.
461 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropTable(std::string_view tableName);
462
463 /// Drops a table if it exists.
464 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropTableIfExists(std::string_view tableName);
465
466 /// Drops a table and all foreign key constraints referencing it.
467 /// On PostgreSQL, uses CASCADE. On MS SQL, drops FK constraints first.
468 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropTableCascade(std::string_view tableName);
469
470 /// Creates a new table for the given record type.
471 template <typename Record>
473 {
474 static_assert(DataMapperRecord<Record>, "Record must satisfy DataMapperRecord");
475
476 auto builder = CreateTable(RecordTableName<Record>);
477 detail::PopulateCreateTableBuilder<Record>(builder);
478 return builder;
479 }
480
481 /// Alters an existing table.
482 template <typename Record>
484 {
485 static_assert(DataMapperRecord<Record>, "Record must satisfy DataMapperRecord");
486 return AlterTable(RecordTableName<Record>);
487 }
488
489 /// Executes raw SQL.
490 LIGHTWEIGHT_API SqlMigrationQueryBuilder& RawSql(std::string_view sql);
491
492 /// Creates an index on a table.
493 ///
494 /// @param indexName The name of the index to create.
495 /// @param tableName The name of the table to create the index on.
496 /// @param columns The columns to include in the index.
497 /// @param unique If true, creates a UNIQUE index.
498 ///
499 /// @code
500 /// SqlQueryBuilder q;
501 /// q.Migration().CreateIndex("idx_user_email", "users", {"email"});
502 /// // Will execute CREATE INDEX "idx_user_email" ON "users" ("email");
503 /// @endcode
504 LIGHTWEIGHT_API SqlMigrationQueryBuilder& CreateIndex(std::string indexName,
505 std::string tableName,
506 std::vector<std::string> columns,
507 bool unique = false);
508
509 /// Creates a unique index on a table.
510 ///
511 /// @param indexName The name of the index to create.
512 /// @param tableName The name of the table to create the index on.
513 /// @param columns The columns to include in the index.
514 LIGHTWEIGHT_API SqlMigrationQueryBuilder& CreateUniqueIndex(std::string indexName,
515 std::string tableName,
516 std::vector<std::string> columns);
517
518 /// Creates an index on a table with auto-generated name.
519 ///
520 /// The index name is automatically generated as `idx_{tableName}_{col1}_{col2}_...`
521 ///
522 /// @param tableName The name of the table to create the index on.
523 /// @param columns The columns to include in the index.
524 /// @param type The type of index (NonUnique or Unique).
525 ///
526 /// @code
527 /// SqlQueryBuilder q;
528 /// q.Migration().CreateIndex("users", {"email"}, IndexType::Unique);
529 /// // Will execute CREATE UNIQUE INDEX "idx_users_email" ON "users" ("email");
530 /// @endcode
531 LIGHTWEIGHT_API SqlMigrationQueryBuilder& CreateIndex(std::string tableName,
532 std::vector<std::string> columns,
533 IndexType type = IndexType::NonUnique);
534
535 /// Creates an INSERT statement for the migration.
536 LIGHTWEIGHT_API SqlMigrationInsertBuilder Insert(std::string_view tableName);
537
538 /// Creates an UPDATE statement for the migration.
539 LIGHTWEIGHT_API SqlMigrationUpdateBuilder Update(std::string_view tableName);
540
541 /// Creates a DELETE statement for the migration.
542 LIGHTWEIGHT_API SqlMigrationDeleteBuilder Delete(std::string_view tableName);
543
544 /// Executes SQL interactively via a callback.
545 LIGHTWEIGHT_API SqlMigrationQueryBuilder& Native(std::function<std::string(SqlConnection&)> callback);
546
547 /// Starts a transaction.
549
550 /// Commits a transaction.
552
553 /// Gets the migration plan.
554 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationPlan const& GetPlan() const&;
555
556 /// Gets the migration plan.
557 ///
558 /// @note This method is destructive and will invalidate the current builder.
559 LIGHTWEIGHT_API SqlMigrationPlan GetPlan() &&;
560
561 private:
562 SqlQueryFormatter const& _formatter;
563 std::string _schemaName;
564 SqlMigrationPlan _migrationPlan;
565};
566
567} // namespace Lightweight
Query builder for building ALTER TABLE queries.
Definition Migrate.hpp:89
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropIndex(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropIndexIfExists(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropColumn(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddColumn(std::string columnName, SqlColumnTypeDefinition columnType)
Adds a new column to the table that is non-nullable.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & RenameColumn(std::string_view oldColumnName, std::string_view newColumnName)
SqlAlterTableQueryBuilder & AddColumn()
Definition Migrate.hpp:108
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & RenameTo(std::string_view newTableName)
Renames the table.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddIndex(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddNotRequiredForeignKeyColumn(std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddNotRequiredColumn(std::string columnName, SqlColumnTypeDefinition columnType)
Adds a new column to the table that is nullable.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddUniqueIndex(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropColumnIfExists(std::string_view columnName)
Drops a column from the table only if it exists.
SqlAlterTableQueryBuilder & AddNotRequiredColumn()
Definition Migrate.hpp:124
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropForeignKey(std::string columnName)
Drops a foreign key for the column columnName from the table.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AlterColumn(std::string columnName, SqlColumnTypeDefinition columnType, SqlNullable nullable)
Alters the column to have a new non-nullable type.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddForeignKey(std::string columnName, SqlForeignKeyReferenceDefinition referencedColumn)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddColumnIfNotExists(std::string columnName, SqlColumnTypeDefinition columnType)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddNotRequiredColumnIfNotExists(std::string columnName, SqlColumnTypeDefinition columnType)
Adds a new nullable column to the table only if it does not already exist.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddForeignKeyColumn(std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddCompositeForeignKey(std::vector< std::string > columns, std::string referencedTableName, std::vector< std::string > referencedColumns)
Represents a connection to a SQL database.
Query builder for building CREATE TABLE queries.
Definition Migrate.hpp:30
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & ForeignKey(std::vector< std::string > columns, std::string referencedTableName, std::vector< std::string > referencedColumns)
Adds a composite foreign key constraint.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & Timestamps()
Adds the created_at and updated_at columns to the table.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & RequiredColumn(std::string columnName, SqlColumnTypeDefinition columnType)
Creates a new column that is non-nullable.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & RequiredForeignKey(std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition foreignKey)
Creates a new non-nullable foreign key column.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & ForeignKey(std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition foreignKey)
Creates a new nullable foreign key column.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & Index()
Enables the INDEX constraint on the last declared column.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & UniqueIndex()
Enables the UNIQUE and INDEX constraint on the last declared column and makes it an index.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & PrimaryKey(std::string columnName, SqlColumnTypeDefinition columnType)
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & Column(SqlColumnDeclaration column)
Adds a new column to the table.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & Column(std::string columnName, SqlColumnTypeDefinition columnType)
Creates a new nullable column.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder & Unique()
Enables the UNIQUE constraint on the last declared column.
Query builder for building DELETE queries in migrations.
Definition Migrate.hpp:406
SqlMigrationDeleteBuilder & Where(std::string columnName, std::string op, T const &value)
Adds a WHERE condition to the DELETE.
Definition Migrate.hpp:415
Query builder for building INSERT queries in migrations.
Definition Migrate.hpp:348
SqlMigrationInsertBuilder & Set(std::string columnName, T const &value)
Sets a column value for the INSERT.
Definition Migrate.hpp:357
Query builder for building SQL migration queries.
Definition Migrate.hpp:430
LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTable(std::string_view tableName)
Creates a new table.
SqlCreateTableQueryBuilder CreateTable()
Creates a new table for the given record type.
Definition Migrate.hpp:472
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CommitTransaction()
Commits a transaction.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & RawSql(std::string_view sql)
Executes raw SQL.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateIndex(std::string indexName, std::string tableName, std::vector< std::string > columns, bool unique=false)
LIGHTWEIGHT_API SqlMigrationDeleteBuilder Delete(std::string_view tableName)
Creates a DELETE statement for the migration.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable(std::string_view tableName)
Alters an existing table.
LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTableIfNotExists(std::string_view tableName)
LIGHTWEIGHT_API SqlMigrationPlan const & GetPlan() const &
Gets the migration plan.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTable(std::string_view tableName)
Drops a table.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateIndex(std::string tableName, std::vector< std::string > columns, IndexType type=IndexType::NonUnique)
LIGHTWEIGHT_API SqlMigrationUpdateBuilder Update(std::string_view tableName)
Creates an UPDATE statement for the migration.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateUniqueIndex(std::string indexName, std::string tableName, std::vector< std::string > columns)
LIGHTWEIGHT_API SqlMigrationInsertBuilder Insert(std::string_view tableName)
Creates an INSERT statement for the migration.
SqlAlterTableQueryBuilder AlterTable()
Alters an existing table.
Definition Migrate.hpp:483
LIGHTWEIGHT_API SqlMigrationQueryBuilder & BeginTransaction()
Starts a transaction.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTableCascade(std::string_view tableName)
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropDatabase(std::string_view databaseName)
Drops a database.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & Native(std::function< std::string(SqlConnection &)> callback)
Executes SQL interactively via a callback.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTableIfExists(std::string_view tableName)
Drops a table if it exists.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateDatabase(std::string_view databaseName)
Creates a new database.
Query builder for building UPDATE queries in migrations.
Definition Migrate.hpp:372
SqlMigrationUpdateBuilder & Where(std::string columnName, std::string op, T const &value)
Adds a WHERE condition to the UPDATE.
Definition Migrate.hpp:389
SqlMigrationUpdateBuilder & Set(std::string columnName, T const &value)
Sets a column value for the UPDATE.
Definition Migrate.hpp:381
API to format SQL queries for different SQL dialects.
Represents a record type that can be used with the DataMapper.
Definition Record.hpp:47
std::remove_cvref_t< decltype(std::declval< MemberClassType< decltype(Field)> >().*Field)>::ValueType ReferencedFieldTypeOf
Retrieves the type of a member field in a record.
Definition Field.hpp:381
Represents a SQL ALTER TABLE plan on a given table.
Represents a SQL column declaration.
Represents a SQL DELETE data plan for migrations.
Represents a foreign key reference definition.
Represents a SQL INSERT data plan for migrations.
Represents a SQL migration plan.
Represents a SQL UPDATE data plan for migrations.
Represents a value that can be any of the supported SQL data types.