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

◆ SQT_TABLE_BEGIN

#define SQT_TABLE_BEGIN ( TABLE_NAME,
ENTITY_TYPE )

Begins the definition of a table type for the specified entity type.

Parameters
TABLE_NAMEThe name of the table. It will be the table name in the database.
ENTITY_TYPEThe entity type for which the table type is being defined. If this macro is used in a different namespace from the entity type, the namespace must be explicitly specified.

This macro opens the definition of a table type, which must be closed using the SQT_TABLE_END macro. Within the definition, the following macros can be used:

  • Defining columns:
    • SQT_COLUMN_FIELD
    • SQT_COLUMN_FIELD_2
    • SQT_COLUMN_ACCESSOR
    • SQT_COLUMN_ACCESSOR_2
    • SQT_COLUMN_CUSTOM
    • SQT_COLUMN_CUSTOM_2
  • Defining primary key:
    • SQT_PRIMARY_KEY
    • SQT_PRIMARY_KEY_AUTO_INC
  • Defining indexes:
    • SQT_INDEX
    • SQT_INDEX_2
    • SQT_INDEX_UNIQUE
    • SQT_INDEX_UNIQUE_2

This macro can be used in any namespace, including the anonymous namespace. If the entity type is defined in a different namespace, ENTITY_TYPE must be prefixed with its namespace to ensure proper resolution.

Note
While identical table names can exist in different namespaces, this is discouraged as it may cause name conflicts when used within the same database.

Example usage:

namespace my_scope {
class MyEntity { };
// Define the table type in the same namespace as the entity type.
SQT_TABLE_BEGIN(MyEntityTable1, MyEntity)
}
// Define the table type in the different namespace from the entity type.
SQT_TABLE_BEGIN(MyEntityTable2, my_scope::MyEntity)
#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 class demonstrates the expected interface of a defined table type:

class TableType : public sqt::AbstractTable {
public:
// Type alias for the associated entity type.
using EntityType = ENTITY_TYPE;
// Retrieves the singleton instance of the table type.
static constexpr const TableType& GetInstance() noexcept;
// Retrieves all columns of the table.
constexpr sqt::ColumnsView<EntityType> GetColumns() const noexcept;
// Retrieves the columns that form the primary key.
// If no primary key is defined, an empty view is returned.
constexpr sqt::ColumnsView<EntityType> GetPrimaryKeyColumns() const noexcept;
// Retrieves the columns that are not part of the primary key.
// If no primary key is defined, this is equivalent to GetColumns().
constexpr sqt::ColumnsView<EntityType> GetNonPrimaryKeyColumns() const noexcept;
// Retrieves the column of the specified column type.
template<sqt::ColumnType COLUMN>
constexpr const sqt::Column<EntityType>* GetColumn() const noexcept;
// Forbids copying.
TableType(const TableType&) = delete;
TableType& operator=(const TableType&) = delete;
// Forbids moving.
TableType(TableType&&) = delete;
TableType& operator=(TableType&&) = delete;
private:
constexpr TableType() noexcept;
};
An interface that provides access to information about a table.
Definition abstract_table.h:24
Note
After defining a table type, it must be registered in the framework using the SQT_REGISTER macro.

After registration, the helper templates sqt::TableType<> and sqt::Table<> allow access to the table type and its singleton instance.

Note
While it is legal to define multiple table types for the same entity type, only one table type can be registered for each entity type. Attempting to register multiple table types for the same entity type will result in a compilation error.
See also
SQT_COLUMN_FIELD
SQT_COLUMN_ACCESSOR
SQT_COLUMN_CUSTOM
SQT_PRIMARY_KEY
SQT_PRIMARY_KEY_AUTO_INC
SQT_INDEX
SQT_INDEX_UNIQUE
SQT_TABLE_END
SQT_REGISTER
sqt::TableType<>
sqt::Table<>
sqt::AbstractTable