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

Query builder for building SQL migration queries. More...

#include <Migrate.hpp>

Public Member Functions

 SqlMigrationQueryBuilder (SqlQueryFormatter const &formatter)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderWithSchema (std::string schemaName)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderCreateDatabase (std::string_view databaseName)
 Creates a new database.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderDropDatabase (std::string_view databaseName)
 Drops a database.
 
LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTable (std::string_view tableName)
 Creates a new table.
 
LIGHTWEIGHT_API SqlCreateTableQueryBuilder CreateTableIfNotExists (std::string_view tableName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable (std::string_view tableName)
 Alters an existing table.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderDropTable (std::string_view tableName)
 Drops a table.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderDropTableIfExists (std::string_view tableName)
 Drops a table if it exists.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderDropTableCascade (std::string_view tableName)
 
template<typename Record >
SqlCreateTableQueryBuilder CreateTable ()
 Creates a new table for the given record type.
 
template<typename Record >
SqlAlterTableQueryBuilder AlterTable ()
 Alters an existing table.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderRawSql (std::string_view sql)
 Executes raw SQL.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderCreateIndex (std::string indexName, std::string tableName, std::vector< std::string > columns, bool unique=false)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderCreateUniqueIndex (std::string indexName, std::string tableName, std::vector< std::string > columns)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderCreateIndex (std::string tableName, std::vector< std::string > columns, IndexType type=IndexType::NonUnique)
 
LIGHTWEIGHT_API SqlMigrationInsertBuilder Insert (std::string_view tableName)
 Creates an INSERT statement for the migration.
 
LIGHTWEIGHT_API SqlMigrationUpdateBuilder Update (std::string_view tableName)
 Creates an UPDATE statement for the migration.
 
LIGHTWEIGHT_API SqlMigrationDeleteBuilder Delete (std::string_view tableName)
 Creates a DELETE statement for the migration.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderNative (std::function< std::string(SqlConnection &)> callback)
 Executes SQL interactively via a callback.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderBeginTransaction ()
 Starts a transaction.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilderCommitTransaction ()
 Commits a transaction.
 
LIGHTWEIGHT_API SqlMigrationPlan const & GetPlan () const &
 Gets the migration plan.
 
LIGHTWEIGHT_API SqlMigrationPlan GetPlan () &&
 

Detailed Description

Query builder for building SQL migration queries.

Definition at line 429 of file Migrate.hpp.

Constructor & Destructor Documentation

◆ SqlMigrationQueryBuilder()

Lightweight::SqlMigrationQueryBuilder::SqlMigrationQueryBuilder ( SqlQueryFormatter const &  formatter)
inlineexplicit

Definition at line 432 of file Migrate.hpp.

Member Function Documentation

◆ CreateTableIfNotExists()

LIGHTWEIGHT_API SqlCreateTableQueryBuilder Lightweight::SqlMigrationQueryBuilder::CreateTableIfNotExists ( std::string_view  tableName)

Creates a new table only if it does not already exist.

Note
Database support:
  • SQLite: Native CREATE TABLE IF NOT EXISTS
  • PostgreSQL: Native CREATE TABLE IF NOT EXISTS
  • SQL Server: Uses conditional IF NOT EXISTS block

◆ DropTableCascade()

LIGHTWEIGHT_API SqlMigrationQueryBuilder & Lightweight::SqlMigrationQueryBuilder::DropTableCascade ( std::string_view  tableName)

Drops a table and all foreign key constraints referencing it. On PostgreSQL, uses CASCADE. On MS SQL, drops FK constraints first.

◆ CreateTable()

template<typename Record >
SqlCreateTableQueryBuilder Lightweight::SqlMigrationQueryBuilder::CreateTable ( )
inline

Creates a new table for the given record type.

Definition at line 472 of file Migrate.hpp.

◆ AlterTable()

template<typename Record >
SqlAlterTableQueryBuilder Lightweight::SqlMigrationQueryBuilder::AlterTable ( )
inline

Alters an existing table.

Definition at line 483 of file Migrate.hpp.

◆ CreateIndex() [1/2]

LIGHTWEIGHT_API SqlMigrationQueryBuilder & Lightweight::SqlMigrationQueryBuilder::CreateIndex ( std::string  indexName,
std::string  tableName,
std::vector< std::string >  columns,
bool  unique = false 
)

Creates an index on a table.

Parameters
indexNameThe name of the index to create.
tableNameThe name of the table to create the index on.
columnsThe columns to include in the index.
uniqueIf true, creates a UNIQUE index.
q.Migration().CreateIndex("idx_user_email", "users", {"email"});
// Will execute CREATE INDEX "idx_user_email" ON "users" ("email");
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateIndex(std::string indexName, std::string tableName, std::vector< std::string > columns, bool unique=false)
API Entry point for building SQL queries.
Definition SqlQuery.hpp:29
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration()
Initiates query for building database migrations.

◆ CreateUniqueIndex()

LIGHTWEIGHT_API SqlMigrationQueryBuilder & Lightweight::SqlMigrationQueryBuilder::CreateUniqueIndex ( std::string  indexName,
std::string  tableName,
std::vector< std::string >  columns 
)

Creates a unique index on a table.

Parameters
indexNameThe name of the index to create.
tableNameThe name of the table to create the index on.
columnsThe columns to include in the index.

◆ CreateIndex() [2/2]

LIGHTWEIGHT_API SqlMigrationQueryBuilder & Lightweight::SqlMigrationQueryBuilder::CreateIndex ( std::string  tableName,
std::vector< std::string >  columns,
IndexType  type = IndexType::NonUnique 
)

Creates an index on a table with auto-generated name.

The index name is automatically generated as idx_{tableName}_{col1}_{col2}_...

Parameters
tableNameThe name of the table to create the index on.
columnsThe columns to include in the index.
typeThe type of index (NonUnique or Unique).
q.Migration().CreateIndex("users", {"email"}, IndexType::Unique);
// Will execute CREATE UNIQUE INDEX "idx_users_email" ON "users" ("email");

◆ GetPlan()

LIGHTWEIGHT_API SqlMigrationPlan Lightweight::SqlMigrationQueryBuilder::GetPlan ( ) &&

Gets the migration plan.

Note
This method is destructive and will invalidate the current builder.

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