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

◆ SQT_PRIMARY_KEY

#define SQT_PRIMARY_KEY ( ...)
Value:
__SQT_PRIMARY_KEY(false, __VA_ARGS__)

Defines a primary key with the specified columns.

Parameters
...The names of the columns that form the primary key. A primary key can be composed of one or more 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 primary key. There can be only one primary key definition in a table type.

Columns of the primary key must not be nullable.

To define an autoincrement primary key, use the SQT_PRIMARY_KEY_AUTO_INC macro instead.

Example usage:

struct MyEntity {
int id{};
std::string name;
};
SQT_TABLE_BEGIN(MyEntityTable, MyEntity)
SQT_COLUMN_FIELD(Name, name)
// Define the primary key with the ID and Name columns.
SQT_PRIMARY_KEY(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_PRIMARY_KEY(...)
Defines a primary key with the specified columns.
Definition table_definition.h:608
#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

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

class TableType {
public:
// Column types.
class IDType;
class NameType;
// The class name of the primary key type is fixed.
class PrimaryKeyType : public sqt::PrimaryKey<IDType, NameType> {
public:
// Forbids copying.
PrimaryKeyType(const PrimaryKeyType&) = delete;
PrimaryKeyType& operator=(const PrimaryKeyType&) = delete;
// Forbids moving.
PrimaryKeyType(PrimaryKeyType&&) = delete;
PrimaryKeyType& operator=(PrimaryKeyType&&) = 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 PrimaryKeyType&, const ValueType&) const noexcept;
friend constexpr auto operator==(const PrimaryKeyType&, sqt::Placeholder) const noexcept;
friend constexpr auto operator==(const ValueType&, const PrimaryKeyType&) const noexcept;
friend constexpr auto operator==(sqt::Placeholder, const PrimaryKeyType&) const noexcept;
};
// The instance of the primary key, which is defined as a public member variable of the
// table type. The name of the variable is fixed.
PrimaryKeyType PrimaryKey;
};
Represents a primary key for a table, which is a composite of one or more columns.
Definition primary_key.h:29
See also
SQT_TABLE_BEGIN
SQT_COLUMN_FIELD
SQT_PRIMARY_KEY_AUTO_INC
sqt::PrimaryKey<>