Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Lightweight::SqlAlterTableQueryBuilder Class Referencefinal

Query builder for building ALTER TABLE queries. More...

#include <Migrate.hpp>

Public Member Functions

 SqlAlterTableQueryBuilder (SqlAlterTablePlan &plan)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderRenameTo (std::string_view newTableName)
 Renames the table.
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddColumn (std::string columnName, SqlColumnTypeDefinition columnType)
 Adds a new column to the table that is non-nullable.
 
template<auto MemberPointer>
SqlAlterTableQueryBuilderAddColumn ()
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddNotRequiredColumn (std::string columnName, SqlColumnTypeDefinition columnType)
 Adds a new column to the table that is nullable.
 
template<auto MemberPointer>
SqlAlterTableQueryBuilderAddNotRequiredColumn ()
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAlterColumn (std::string columnName, SqlColumnTypeDefinition columnType, SqlNullable nullable)
 Alters the column to have a new non-nullable type.
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderRenameColumn (std::string_view oldColumnName, std::string_view newColumnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderDropColumn (std::string_view columnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddColumnIfNotExists (std::string columnName, SqlColumnTypeDefinition columnType)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddNotRequiredColumnIfNotExists (std::string columnName, SqlColumnTypeDefinition columnType)
 Adds a new nullable column to the table only if it does not already exist.
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderDropColumnIfExists (std::string_view columnName)
 Drops a column from the table only if it exists.
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddIndex (std::string_view columnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddUniqueIndex (std::string_view columnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderDropIndex (std::string_view columnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderDropIndexIfExists (std::string_view columnName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddForeignKey (std::string columnName, SqlForeignKeyReferenceDefinition referencedColumn)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddForeignKeyColumn (std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddNotRequiredForeignKeyColumn (std::string columnName, SqlColumnTypeDefinition columnType, SqlForeignKeyReferenceDefinition referencedColumn)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderDropForeignKey (std::string columnName)
 Drops a foreign key for the column columnName from the table.
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilderAddCompositeForeignKey (std::vector< std::string > columns, std::string referencedTableName, std::vector< std::string > referencedColumns)
 

Detailed Description

Query builder for building ALTER TABLE queries.

See also
SqlQueryBuilder

Definition at line 88 of file Migrate.hpp.

Constructor & Destructor Documentation

◆ SqlAlterTableQueryBuilder()

Lightweight::SqlAlterTableQueryBuilder::SqlAlterTableQueryBuilder ( SqlAlterTablePlan plan)
inlineexplicit

Definition at line 91 of file Migrate.hpp.

Member Function Documentation

◆ AddColumn()

template<auto MemberPointer>
SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddColumn ( )
inline

Adds a new column to the table that is non-nullable.

Template Parameters
MemberPointerThe pointer to the member field in the record.
See also
AddColumn(std::string, SqlColumnTypeDefinition)

Definition at line 108 of file Migrate.hpp.

◆ AddNotRequiredColumn()

template<auto MemberPointer>
SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddNotRequiredColumn ( )
inline

Adds a new column to the table that is nullable.

Template Parameters
MemberPointerThe pointer to the member field in the record.
See also
AddNotRequiredColumn(std::string, SqlColumnTypeDefinition)

Definition at line 124 of file Migrate.hpp.

◆ AlterColumn()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AlterColumn ( std::string  columnName,
SqlColumnTypeDefinition  columnType,
SqlNullable  nullable 
)

Alters the column to have a new non-nullable type.

Parameters
columnNameThe name of the column to alter.
columnTypeThe new type of the column.
nullableThe new nullable state of the column.
Returns
The current query builder for chaining.
See also
SqlColumnTypeDefinition
auto stmt = SqlStatement();
auto sqlMigration = stmt.Migration()
.AlterTable("Table")
.AlterColumn("column", Integer {}, SqlNullable::NotNull)
.GetPlan().ToSql();
for (auto const& sql: sqlMigration)
stmt.ExecuteDirect(sql);
High level API for (prepared) raw SQL statements.

◆ RenameColumn()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::RenameColumn ( std::string_view  oldColumnName,
std::string_view  newColumnName 
)

Renames a column.

Parameters
oldColumnNameThe old column name.
newColumnNameThe new column name.

◆ DropColumn()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::DropColumn ( std::string_view  columnName)

Drops a column from the table.

Parameters
columnNameThe name of the column to drop.

◆ AddColumnIfNotExists()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddColumnIfNotExists ( std::string  columnName,
SqlColumnTypeDefinition  columnType 
)

Adds a new column to the table only if it does not already exist.

Note
Database support varies:
  • PostgreSQL: Native IF NOT EXISTS support
  • SQL Server: Uses conditional IF NOT EXISTS query
  • SQLite: Limited support (may require raw SQL)

◆ AddIndex()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddIndex ( std::string_view  columnName)

Add an index to the table for the specified column.

Parameters
columnNameThe name of the column to index.
q.Migration().AlterTable("Table").AddIndex("column");
// Will execute CREATE INDEX "Table_column_index" ON "Table" ("column");
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddIndex(std::string_view columnName)
LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable(std::string_view tableName)
Alters an existing table.
API Entry point for building SQL queries.
Definition SqlQuery.hpp:29
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration()
Initiates query for building database migrations.

◆ AddUniqueIndex()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddUniqueIndex ( std::string_view  columnName)

Add an index to the table for the specified column that is unique.

Parameters
columnNameThe name of the column to index.
q.Migration().AlterTable("Table").AddUniqueIndex("column");
// Will execute CREATE UNIQUE INDEX "Table_column_index" ON "Table" ("column");
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & AddUniqueIndex(std::string_view columnName)

◆ DropIndex()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::DropIndex ( std::string_view  columnName)

Drop an index from the table for the specified column.

Parameters
columnNameThe name of the column to drop the index from.
q.Migration().AlterTable("Table").DropIndex("column");
// Will execute DROP INDEX "Table_column_index";
LIGHTWEIGHT_API SqlAlterTableQueryBuilder & DropIndex(std::string_view columnName)

◆ DropIndexIfExists()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::DropIndexIfExists ( std::string_view  columnName)

Drop an index from the table only if it exists.

Parameters
columnNameThe name of the column to drop the index from.

◆ AddForeignKey()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddForeignKey ( std::string  columnName,
SqlForeignKeyReferenceDefinition  referencedColumn 
)

Adds a foreign key column columnName to referencedColumn to an existing column.

Parameters
columnNameThe name of the column to add.
referencedColumnThe column to reference.

◆ AddForeignKeyColumn()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddForeignKeyColumn ( std::string  columnName,
SqlColumnTypeDefinition  columnType,
SqlForeignKeyReferenceDefinition  referencedColumn 
)

Adds a foreign key column columnName of type columnType to referencedColumn.

Parameters
columnNameThe name of the column to add.
columnTypeThe type of the column to add.
referencedColumnThe column to reference.

◆ AddNotRequiredForeignKeyColumn()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddNotRequiredForeignKeyColumn ( std::string  columnName,
SqlColumnTypeDefinition  columnType,
SqlForeignKeyReferenceDefinition  referencedColumn 
)

Adds a nullable foreign key column columnName of type columnType to referencedColumn.

Parameters
columnNameThe name of the column to add.
columnTypeThe type of the column to add.
referencedColumnThe column to reference.

◆ AddCompositeForeignKey()

LIGHTWEIGHT_API SqlAlterTableQueryBuilder & Lightweight::SqlAlterTableQueryBuilder::AddCompositeForeignKey ( std::vector< std::string >  columns,
std::string  referencedTableName,
std::vector< std::string >  referencedColumns 
)

Adds a composite foreign key constraint.

Parameters
columnsThe columns in the current table.
referencedTableNameThe referenced table name.
referencedColumnsThe referenced columns in the referenced table.

The documentation for this class was generated from the following file: