SQT
A C++ ORM framework for SQLite
Loading...
Searching...
No Matches

◆ SQT_INDEX

#define SQT_INDEX ( ...)

Defines an index with auto-generated names based on the specified columns.

Parameters
...The names of the columns that form the index. An index can consist of up to 8 columns.

This macro must be used between the SQT_TABLE_BEGIN and SQT_TABLE_END macros and must be placed after all definitions of the columns that are part of the index. There can be multiple index definitions in a table type.

The index name in the database is generated using the following format:

SQTIndex_<TableName>_<Column1>_<Column2>_...

Example usage:

struct MyEntity {
int id{};
std::string name;
};
SQT_TABLE_BEGIN(MyEntityTable, MyEntity)
SQT_COLUMN_FIELD(Name, name)
// Define an index with the ID and Name columns.
SQT_INDEX(ID, Name)
#define SQT_COLUMN_FIELD(COLUMN_NAME, FIELD)
Defines a column that binds to the specified field of the entity type.
Definition table_definition.h:273
#define SQT_TABLE_BEGIN(TABLE_NAME, ENTITY_TYPE)
Begins the definition of a table type for the specified entity type.
Definition table_definition.h:141
#define SQT_TABLE_END
Ends the definition of a table type.
Definition table_definition.h:854
#define SQT_INDEX(...)
Defines an index with auto-generated names based on the specified columns.
Definition table_definition.h:733

The following code demonstrates the generated definition of the index in the table type:

class TableType {
public:
// Column types.
class IDType;
class NameType;
// The class for the index type, whose name is generated by joining the column names with
// an underscore ('_') as the delimiter and appending the result to the prefix
// "IndexType_".
class IndexType_ID_Name :
public sqt::CompositeColumn<IDType, NameType>,
public:
//Forbids copying.
IndexType_IDName(const IndexType_IDName&) = delete;
IndexType_IDName& operator=(const IndexType_IDName&) = delete;
//Forbids moving.
IndexType_IDName(IndexType_IDName&&) = delete;
IndexType_IDName& operator=(IndexType_IDName&&) = delete;
// Assignment operator that generates an assignment expression.
constexpr auto operator=(const ValueType&) const noexcept;
// Ordering operator that generates an ordering term expression.
constexpr auto Asc() const noexcept;
constexpr auto Desc() const noexcept;
// Comparison operators that generate predicate expressions.
// Here shows only the == operator. Other operators (!=, <, <=, >, >=) are omitted.
friend constexpr auto operator==(const IndexType_IDName&, const ValueType&) const noexcept;
friend constexpr auto operator==(const IndexType_IDName&, sqt::Placeholder) const noexcept;
friend constexpr auto operator==(const ValueType&, const IndexType_IDName&) const noexcept;
friend constexpr auto operator==(sqt::Placeholder, const IndexType_IDName&) const noexcept;
};
// The instance of the index, which is defined as a public member variable of the table
// type. The name of the variable is generated by joining the column names with an
// underscore ('_') as the delimiter and appending the result to the prefix "Index_".
IndexType_ID_Name Index_ID_Name;
};
An interface that provides access to information about an index.
Definition abstract_index.h:26
The primary template for defining composite column types for various number of columns.
Definition composite_column.h:37

The following alternative macros can also be used to define indexes:

  • SQT_INDEX_2: Defines an index with custom names.
  • SQT_INDEX_UNIQUE: Defines an unique index with auto-generated names.
  • SQT_INDEX_UNIQUE_2: Defines an unique index with custom names.
See also
sqt::AbstractIndex
sqt::CompositeColumn<>
SQT_INDEX_2
SQT_INDEX_UNIQUE
SQT_INDEX_UNIQUE_2
SQT_TABLE_BEGIN