SQT
A C++ ORM framework for SQLite
Loading...
Searching...
No Matches
SQT - Structural Query Templates

Introduction

SQT (Structural Query Templates) is a C++ ORM (Object-Relational Mapping) framework for interacting with SQLite databases.

SQT is currently developed and tested on Windows with MSVC. While it is theoretically compatible with other compilers and platforms, it has not yet been tested outside this environment.

Quick Start

1. Define the entity type

Define a struct or class for your entity, where each field corresponds to a column in the database table.

struct MyEntity {
int id;
std::string name;
};

2. Define the table type

Use SQT macros to define the table type, which provides metadata for mapping the entity type to a corresponding database table.

SQT_TABLE_BEGIN(MyEntityTable, MyEntity)
// Define the ID column that binds to the id field in MyEntity.
// Define the Name column that binds to the name field in MyEntity.
SQT_COLUMN_FIELD(Name, name)
// Define the primary key with the ID column.
#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

3. Register the table type

Register the table type to enable ORM functionality for the entity type.

SQT_REGISTER(MyEntityTable)
#define SQT_REGISTER(QUALIFIED_TABLE_NAME)
Registers the specified table type in the framework, enabling ORM capabilities for the associated ent...
Definition table_definition.h:935

4. Use the data context

Create a data context for database operations. This provides the ability to insert, update, delete, and query rows.

// Open a database and create a data context instance for the entity type.
auto database = std::make_shared<sqt::Database>(sqt::Database::Open("MyDatabase.db"));
sqt::DataContext<MyEntity> data_context{ database };
// Insert rows into the database.
data_context.Insert(MyEntity{ 1, "The first entity" });
data_context.Insert(MyEntity{ 2, "The second entity" });
data_context.Insert(MyEntity{ 3, "The third entity" });
// Update an existing row.
data_context.Update(MyEntity{ 3, "THE THIRD ENTITY" });
// Delete a row.
data_context.Delete(2);
// Select a row by primary key.
std::optional<MyEntity> entity = data_context.Select(1);
if (entity) {
std::cout << "Select() result: " << entity->id << ',' << entity->name << "\n";
}
// Select all rows.
std::vector<MyEntity> all_entities = data_context.SelectAll();
std::cout << "SelectAll() result:\n";
for (const auto& each_entity : all_entities) {
std::cout << each_entity.id << ',' << each_entity.name << "\n";
}
std::int64_t Insert(const ENTITY &entity)
Inserts the specified entity into the database table.
Definition data_context.h:678
bool Update(const ENTITY &entity)
Updates the specified entity in the database table.
Definition data_context.h:817
bool Delete(const typename TableType< E >::PrimaryKeyType::ValueType &primary_key)
Deletes a row by the specified primary key value from the database table.
Definition data_context.h:879
std::vector< ENTITY > SelectAll()
Retrieves all entities from the database table.
Definition data_context.h:911
std::optional< E > Select(const typename TableType< E >::PrimaryKeyType::ValueType &primary_key)
Retrieves a single entity by the specified primary key value from the database table.
Definition data_context.h:952
Provides a set of operations for performing CRUD (Create, Read, Update, Delete) operations on a datab...
Definition data_context.h:96
static Database Open(const std::filesystem::path &path)
Opens a SQLite database at the specified file path.
Definition database.cpp:7

Documentation

For more information about using SQT, please refer to the SQT Documentation.