Lightweight 0.20250904.0
Loading...
Searching...
No Matches
Migrate.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "Core.hpp"
6#include "MigrationPlan.hpp"
7
8#include <reflection-cpp/reflection.hpp>
9
10namespace Lightweight
11{
12
13/// @brief Query builder for building CREATE TABLE queries.
14///
15/// @see SqlQueryBuilder
16/// @ingroup QueryBuilder
17class [[nodiscard]] SqlCreateTableQueryBuilder final
18{
19 public:
20 explicit SqlCreateTableQueryBuilder(SqlCreateTablePlan& plan):
21 _plan { plan }
22 {
23 }
24
25 /// Adds a new column to the table.
27
28 /// Creates a new nullable column.
29 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& Column(std::string columnName, SqlColumnTypeDefinition columnType);
30
31 /// Creates a new column that is non-nullable.
32 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& RequiredColumn(std::string columnName, SqlColumnTypeDefinition columnType);
33
34 /// Adds the created_at and updated_at columns to the table.
36
37 /// Creates a new primary key column.
38 /// Primary keys are always required, unique, have an index, and are non-nullable.
39 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& PrimaryKey(std::string columnName, SqlColumnTypeDefinition columnType);
40
41 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& PrimaryKeyWithAutoIncrement(
42 std::string columnName, SqlColumnTypeDefinition columnType = SqlColumnTypeDefinitions::Bigint {});
43
44 /// Creates a new nullable foreign key column.
45 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& ForeignKey(std::string columnName,
46 SqlColumnTypeDefinition columnType,
48
49 /// Creates a new non-nullable foreign key column.
50 LIGHTWEIGHT_API SqlCreateTableQueryBuilder& RequiredForeignKey(std::string columnName,
51 SqlColumnTypeDefinition columnType,
53
54 /// Enables the UNIQUE constraint on the last declared column.
56
57 /// Enables the INDEX constraint on the last declared column.
59
60 /// Enables the UNIQUE and INDEX constraint on the last declared column and makes it an index.
62
63 private:
64 SqlCreateTablePlan& _plan;
65};
66
67/// @brief Query builder for building ALTER TABLE queries.
68///
69/// @see SqlQueryBuilder
70/// @ingroup QueryBuilder
71class [[nodiscard]] SqlAlterTableQueryBuilder final
72{
73 public:
75 _plan { plan }
76 {
77 }
78
79 /// Renames the table.
80 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& RenameTo(std::string_view newTableName);
81
82 /// Adds a new column to the table that is non-nullable.
83 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddColumn(std::string columnName, SqlColumnTypeDefinition columnType);
84
85 /// Adds a new column to the table that is nullable.
86 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddNotRequiredColumn(std::string columnName,
87 SqlColumnTypeDefinition columnType);
88
89 /// @brief Alters the column to have a new non-nullable type.
90 ///
91 /// @param columnName The name of the column to alter.
92 /// @param columnType The new type of the column.
93 /// @param nullable The new nullable state of the column.
94 ///
95 /// @return The current query builder for chaining.
96 ///
97 /// @see SqlColumnTypeDefinition
98 ///
99 /// @code
100 /// auto stmt = SqlStatement();
101 /// auto sqlMigration = stmt.Migration()
102 /// .AlterTable("Table")
103 /// .AlterColumn("column", Integer {}, SqlNullable::NotNull)
104 /// .GetPlan().ToSql();
105 /// for (auto const& sql: sqlMigration)
106 /// stmt.ExecuteDirect(sql);
107 /// @endcode
108 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AlterColumn(std::string columnName,
109 SqlColumnTypeDefinition columnType,
110 SqlNullable nullable);
111
112 /// Renames a column.
113 /// @param oldColumnName The old column name.
114 /// @param newColumnName The new column name.
115 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& RenameColumn(std::string_view oldColumnName, std::string_view newColumnName);
116
117 /// Drops a column from the table.
118 /// @param columnName The name of the column to drop.
119 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropColumn(std::string_view columnName);
120
121 /// Add an index to the table for the specified column.
122 /// @param columnName The name of the column to index.
123 ///
124 /// @code
125 /// SqlQueryBuilder q;
126 /// q.Migration().AlterTable("Table").AddIndex("column");
127 /// // Will execute CREATE INDEX "Table_column_index" ON "Table"("column");
128 /// @endcode
129 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddIndex(std::string_view columnName);
130
131 /// Add an index to the table for the specified column that is unique.
132 /// @param columnName The name of the column to index.
133 ///
134 /// @code
135 /// SqlQueryBuilder q;
136 /// q.Migration().AlterTable("Table").AddUniqueIndex("column");
137 /// // Will execute CREATE UNIQUE INDEX "Table_column_index" ON "Table"("column");
138 /// @endcode
139 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddUniqueIndex(std::string_view columnName);
140
141 /// Drop an index from the table for the specified column.
142 /// @param columnName The name of the column to drop the index from.
143 ///
144 /// @code
145 /// SqlQueryBuilder q;
146 /// q.Migration().AlterTable("Table").DropIndex("column");
147 /// // Will execute DROP INDEX "Table_column_index";
148 /// @endcode
149 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropIndex(std::string_view columnName);
150
151 /// Adds a foreign key column @p columnName to @p referencedColumn to an existing column.
152 ///
153 /// @param columnName The name of the column to add.
154 /// @param referencedColumn The column to reference.
155 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddForeignKey(std::string columnName,
156 SqlForeignKeyReferenceDefinition referencedColumn);
157
158 /// Adds a foreign key column @p columnName of type @p columnType to @p referencedColumn.
159 ///
160 /// @param columnName The name of the column to add.
161 /// @param columnType The type of the column to add.
162 /// @param referencedColumn The column to reference.
163 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& AddForeignKeyColumn(std::string columnName,
164 SqlColumnTypeDefinition columnType,
165 SqlForeignKeyReferenceDefinition referencedColumn);
166
167 /// Adds a nullable foreign key column @p columnName of type @p columnType to @p referencedColumn.
168 ///
169 /// @param columnName The name of the column to add.
170 /// @param columnType The type of the column to add.
171 /// @param referencedColumn The column to reference.
173 std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn);
174
175 /// Drops a foreign key for the column @p columnName from the table.
176 LIGHTWEIGHT_API SqlAlterTableQueryBuilder& DropForeignKey(std::string columnName);
177
178 private:
179 SqlAlterTablePlan& _plan;
180};
181
182/// @brief Query builder for building SQL migration queries.
183/// @ingroup QueryBuilder
184class [[nodiscard]] SqlMigrationQueryBuilder final
185{
186 public:
187 explicit SqlMigrationQueryBuilder(SqlQueryFormatter const& formatter):
188 _formatter { formatter },
189 _migrationPlan { .formatter = formatter }
190 {
191 }
192
193 /// Creates a new database.
194 LIGHTWEIGHT_API SqlMigrationQueryBuilder& CreateDatabase(std::string_view databaseName);
195
196 /// Drops a database.
197 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropDatabase(std::string_view databaseName);
198
199 /// Creates a new table.
200 LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTable(std::string_view tableName);
201
202 /// Alters an existing table.
203 LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable(std::string_view tableName);
204
205 /// Drops a table.
206 LIGHTWEIGHT_API SqlMigrationQueryBuilder& DropTable(std::string_view tableName);
207
208 /// Executes raw SQL.
209 LIGHTWEIGHT_API SqlMigrationQueryBuilder& RawSql(std::string_view sql);
210
211 /// Executes SQL interactively via a callback.
212 LIGHTWEIGHT_API SqlMigrationQueryBuilder& Native(std::function<std::string(SqlConnection&)> callback);
213
214 /// Starts a transaction.
216
217 /// Commits a transaction.
219
220 /// Gets the migration plan.
221 [[nodiscard]] LIGHTWEIGHT_API SqlMigrationPlan const& GetPlan() const&;
222
223 /// Gets the migration plan.
224 ///
225 /// @note This method is destructive and will invalidate the current builder.
226 LIGHTWEIGHT_API SqlMigrationPlan GetPlan() &&;
227
228 private:
229 SqlQueryFormatter const& _formatter;
230 SqlMigrationPlan _migrationPlan;
231};
232
233} // namespace Lightweight
Query builder for building ALTER TABLE queries.
Definition Migrate.hpp:72
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropIndex(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)
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 & 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 & AddForeignKeyColumn(std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn)
Represents a connection to a SQL database.
Query builder for building CREATE TABLE queries.
Definition Migrate.hpp:18
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 SQL migration queries.
Definition Migrate.hpp:185
LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTable(std::string_view tableName)
Creates a new table.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CommitTransaction()
Commits a transaction.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & RawSql(std::string_view sql)
Executes raw SQL.
LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable(std::string_view tableName)
Alters an existing table.
LIGHTWEIGHT_API SqlMigrationPlan const & GetPlan() const &
Gets the migration plan.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTable(std::string_view tableName)
Drops a table.
LIGHTWEIGHT_API SqlMigrationQueryBuilder & BeginTransaction()
Starts a transaction.
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 & CreateDatabase(std::string_view databaseName)
Creates a new database.
API to format SQL queries for different SQL dialects.
Represents a SQL ALTER TABLE plan on a given table.
Represents a SQL column declaration.
Represents a foreign key reference definition.
Represents a SQL migration plan.