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