|
ORM C++
|
A model is a reflected C++ struct whose fields can be mapped to SQLite columns.
By default the table name is derived from the C++ type name and :: is replaced with _.
SQLite models currently support these scalar C++ field types:
These types are mapped to SQLite storage classes by the SQLite backend. Types outside this list are not supported as scalar columns.
Wrap a supported scalar type in std::optional<T> to make the column nullable.
Non-optional fields are generated as NOT NULL. Optional fields are generated without NOT NULL, and std::nullopt is stored and read back as SQL NULL.
Override the generated table name with a static table_name field:
Override database column names with a static columns_names mapping. The mapping uses C++ field names as keys. Fields omitted from the mapping keep their C++ field names.
Query and update builders still use C++ field names such as col("displayName"); the renderer maps them to database column names.
If a model has an id field, it is used as the default primary key.
Override the primary key with id_columns. Values are C++ field names, not database column names.
To create a model without a primary key, omit id or define an empty id_columns collection.
SQLite integer primary keys can be generated by the database. To enable this, define auto_increment_columns with the C++ field name of the primary-key field:
This creates id INTEGER PRIMARY KEY AUTOINCREMENT and omits the field from generated INSERT statements. Existing models are unchanged unless they define auto_increment_columns.
Auto-increment support has these limitations:
If columns_names maps the field name, auto_increment_columns still uses the C++ field name:
This creates user_id INTEGER PRIMARY KEY AUTOINCREMENT.
A field whose type is another model with a primary key is treated as a one-to-one relation. The owning table stores the related model primary key as local foreign key column(s).
For this model, User stores profile_id and references Profile(id). Selecting a model joins one-to-one relations by default. Use Query<T>::disableJoining() to read only related primary-key values.
Relations can also be nullable:
Nullable relations generate nullable foreign-key columns, store std::nullopt as SQL NULL, and read SQL NULL back as std::nullopt.
Model metadata currently supports one level of one-to-one relations. It does not yet support OneToMany, ManyToMany, nested relation paths beyond one relation field, custom converters, date/time fields, UUID fields, or boost::optional.