Lightweight 0.20261105.1
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)
 Constructs a migration query builder.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & WithSchema (std::string schemaName)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateDatabase (std::string_view databaseName)
 Creates a new database.
 
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 SqlCreateTableQueryBuilder CreateTableIfNotExists (std::string_view tableName)
 
LIGHTWEIGHT_API SqlAlterTableQueryBuilder AlterTable (std::string_view tableName)
 Alters an existing table.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTable (std::string_view tableName)
 Drops a table.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTableIfExists (std::string_view tableName)
 Drops a table if it exists.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & DropTableCascade (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 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 SqlMigrationQueryBuilder & CreateIndex (std::string indexName, std::string tableName, std::vector< std::string > columns, SqlCreateIndexOptions options)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateUniqueIndex (std::string indexName, std::string tableName, std::vector< std::string > columns)
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CreateIndex (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 SqlMigrationQueryBuilder & BeginTransaction ()
 Starts a transaction.
 
LIGHTWEIGHT_API SqlMigrationQueryBuilder & CommitTransaction ()
 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 520 of file Migrate.hpp.

Constructor & Destructor Documentation

◆ SqlMigrationQueryBuilder()

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

Constructs a migration query builder.

Definition at line 524 of file Migrate.hpp.

Member Function Documentation

◆ WithSchema()

LIGHTWEIGHT_API SqlMigrationQueryBuilder & Lightweight::SqlMigrationQueryBuilder::WithSchema ( std::string  schemaName)

Sets the schema name for the migration.

On SQL Server and PostgreSQL, CreateTable then creates the table in that schema, which must exist. On PostgreSQL this differs from earlier releases, which created it in the search_path schema, so the checksums of such migrations changed (see dbtool rewrite-checksums).

◆ 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 569 of file Migrate.hpp.

◆ AlterTable()

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

Alters an existing table.

Definition at line 580 of file Migrate.hpp.

◆ CreateIndex() [1/3]

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:32
LIGHTWEIGHT_API SqlMigrationQueryBuilder Migration()
Initiates query for building database migrations.

◆ CreateIndex() [2/3]

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

Creates an index on a table, optionally unique and/or partial.

A non-empty options.whereExpression renders a partial / filtered index, CREATE [UNIQUE] INDEX ... ON t (cols) WHERE <expression>, which SQLite, PostgreSQL and SQL Server all support.

Note
SQL Server creates a filtered index, and accepts writes to its table, only with ANSI_WARNINGS ON, i.e. on connections using SqlStringTruncationMode::Error; the default SqlStringTruncationMode::Truncate fails them with error 1934.
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.
optionsUniqueness and the optional index predicate.
q.Migration().CreateIndex("idx_queue_key", "queue", {"dedup_key"},
{.type = IndexType::Unique, .whereExpression = R"("dedup_key" <> '')"});
// Will execute CREATE UNIQUE INDEX "idx_queue_key" ON "queue" ("dedup_key") WHERE "dedup_key" <> ''
@ Unique
Unique index.

◆ 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() [3/3]

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: