Added documentation

This commit is contained in:
Dr. Patrick Urbanke
2025-05-07 04:18:10 +02:00
parent 831ca01d43
commit b73e2a512d
7 changed files with 438 additions and 48 deletions

View File

@@ -1,32 +1,45 @@
# `sqlgen::PrimaryKey`
# `sqlgen::PrimaryKey`
Sometimes you would like to set primary keys on your
tables. This is possible as well:
`sqlgen::PrimaryKey` is used to indicate which key should be a primary key.
## Usage
### Basic Definition
Define a primary key field in your struct:
```cpp
struct People {
rfl::PrimaryKey<std::string> first_name;
sqlgen::PrimaryKey<std::string> first_name;
std::string last_name;
uint age;
};
```
Now, the generated SQL code will look something like this:
This generates the following SQL schema:
```sql
CREATE TABLE IF NOT EXISTS "People"("first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"age" INTEGER NOT NULL,
PRIMARY_KEY("first_name"));
CREATE TABLE IF NOT EXISTS "People"(
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"age" INTEGER NOT NULL,
PRIMARY_KEY("first_name")
);
```
`rfl::PrimaryKey<...>` is a simple wrapper. You can simply assign to it as follows:
### Assignment and Access
Assign values to primary key fields:
```cpp
const auto person = People{.first_name = "Homer", ...};
const auto person = People{
.first_name = "Homer",
.last_name = "Simpson",
.age = 45
};
```
And you can retrieve the underlying value using any of the following:
Access the underlying value using any of these methods:
```cpp
person.first_name();
@@ -34,3 +47,14 @@ person.first_name.get();
person.first_name.value();
```
## Notes
- The template parameter specifies the type of the primary key field
- Primary key fields are automatically marked as NOT NULL in the generated SQL
- The class supports:
- Direct value assignment
- Multiple access methods for the underlying value
- Reflection for SQL operations
- Move and copy semantics
- Primary keys can be used with any supported SQL data type