MantisBase v0.3.4
Loading...
Searching...
No Matches
mb::EntitySchemaField Class Reference

Represents a single field in a database table schema. More...

#include <entity_schema_field.h>

Public Member Functions

 EntitySchemaField (std::string field_name, std::string field_type)
 Construct field with name and type.
 
 EntitySchemaField (const nlohmann::json &field_schema)
 Construct field from JSON schema object.
 
 EntitySchemaField (const EntitySchemaField &other)
 
bool operator== (const EntitySchemaField &other) const
 
std::string id () const
 Get field unique identifier.
 
std::string name () const
 Get field name.
 
EntitySchemaFieldsetName (const std::string &name)
 Set field name (fluent interface).
 
std::string type () const
 Get field type.
 
EntitySchemaFieldsetType (const std::string &type)
 Set field type (fluent interface).
 
bool required () const
 Check if field is required.
 
EntitySchemaFieldsetRequired (bool required)
 Set required flag (fluent interface).
 
bool isPrimaryKey () const
 Check if field is primary key.
 
EntitySchemaFieldsetIsPrimaryKey (bool pk)
 Set primary key flag (fluent interface).
 
bool isSystem () const
 Check if field is system field.
 
EntitySchemaFieldsetIsSystem (bool system)
 Set system field flag (fluent interface).
 
bool isUnique () const
 Check if field has unique constraint.
 
EntitySchemaFieldsetIsUnique (bool unique)
 Set unique constraint (fluent interface).
 
bool isForeignKey () const
 Check if field is a foreign key.
 
std::string foreignKeyTable () const
 Get foreign key reference table name.
 
std::string foreignKeyColumn () const
 Get foreign key reference column name.
 
std::string foreignKeyOnUpdate () const
 Get foreign key update policy.
 
std::string foreignKeyOnDelete () const
 Get foreign key delete policy.
 
EntitySchemaFieldsetForeignKey (const std::string &table, const std::string &column="id", const std::string &onUpdate="RESTRICT", const std::string &onDelete="RESTRICT")
 Set foreign key reference (fluent interface).
 
EntitySchemaFieldremoveForeignKey ()
 Remove foreign key constraint (fluent interface).
 
nlohmann::json constraints () const
 Get all field constraints.
 
nlohmann::json constraint (const std::string &key) const
 Get specific constraint by key.
 
EntitySchemaFieldsetConstraints (const nlohmann::json &opts)
 Set field constraints (fluent interface).
 
EntitySchemaFieldupdateWith (const nlohmann::json &field_schema)
 Update field with new JSON data.
 
nlohmann::json toJSON () const
 Convert field to JSON representation.
 
soci::db_type toSociType () const
 Convert field type to SOCI database type.
 
std::optional< std::string > validate () const
 Validate field definition.
 

Static Public Member Functions

static const std::vector< std::string > & defaultBaseFields ()
 
static const std::vector< std::string > & defaultAuthFields ()
 
static const std::vector< std::string > & defaultEntityFieldTypes ()
 
static soci::db_type toSociType (const std::string &type)
 Convert type string to SOCI database type (static).
 
static bool isValidFieldType (const std::string &type)
 Check if field type is valid.
 
static std::string genFieldId (const std::string &id)
 Generate unique field ID from name.
 
static const nlohmann::json & defaultConstraints ()
 Get default constraints JSON.
 

Detailed Description

Represents a single field in a database table schema.

Defines field properties including name, type, constraints, and validation rules. Supports fluent interface for building field definitions.

// Create a required string field
EntitySchemaField nameField("name", "string");
nameField.setRequired(true);
// Create a unique email field
EntitySchemaField emailField("email", "string");
emailField.setIsUnique(true).setRequired(true);
Represents a single field in a database table schema.
Definition entity_schema_field.h:36

Constructor & Destructor Documentation

◆ EntitySchemaField() [1/3]

mb::EntitySchemaField::EntitySchemaField ( std::string  field_name,
std::string  field_type 
)

Construct field with name and type.

Parameters
field_nameField name
field_typeField type (e.g., "string", "int32", "bool", "date")

◆ EntitySchemaField() [2/3]

mb::EntitySchemaField::EntitySchemaField ( const nlohmann::json &  field_schema)
explicit

Construct field from JSON schema object.

Parameters
field_schemaJSON object with field definition

◆ EntitySchemaField() [3/3]

mb::EntitySchemaField::EntitySchemaField ( const EntitySchemaField other)
default

Member Function Documentation

◆ constraint()

nlohmann::json mb::EntitySchemaField::constraint ( const std::string &  key) const

Get specific constraint by key.

Parameters
keyConstraint key (e.g., "min", "max", "validator")
Returns
Constraint value JSON

◆ constraints()

nlohmann::json mb::EntitySchemaField::constraints ( ) const

Get all field constraints.

Returns
JSON object with constraints (min, max, validator, etc.)

◆ defaultAuthFields()

const std::vector< std::string > & mb::EntitySchemaField::defaultAuthFields ( )
static

◆ defaultBaseFields()

const std::vector< std::string > & mb::EntitySchemaField::defaultBaseFields ( )
static

◆ defaultConstraints()

const nlohmann::json & mb::EntitySchemaField::defaultConstraints ( )
static

Get default constraints JSON.

Returns
Reference to default constraints object

◆ defaultEntityFieldTypes()

const std::vector< std::string > & mb::EntitySchemaField::defaultEntityFieldTypes ( )
static

◆ foreignKeyColumn()

std::string mb::EntitySchemaField::foreignKeyColumn ( ) const

Get foreign key reference column name.

Returns
Reference column name, defaults to "id" if not specified

◆ foreignKeyOnDelete()

std::string mb::EntitySchemaField::foreignKeyOnDelete ( ) const

Get foreign key delete policy.

Returns
Delete policy ("CASCADE", "SET NULL", "RESTRICT", "NO ACTION", "SET DEFAULT")

◆ foreignKeyOnUpdate()

std::string mb::EntitySchemaField::foreignKeyOnUpdate ( ) const

Get foreign key update policy.

Returns
Update policy ("CASCADE", "SET NULL", "RESTRICT", "NO ACTION", "SET DEFAULT")

◆ foreignKeyTable()

std::string mb::EntitySchemaField::foreignKeyTable ( ) const

Get foreign key reference table name.

Returns
Reference table name, empty if not a foreign key

◆ genFieldId()

std::string mb::EntitySchemaField::genFieldId ( const std::string &  id)
static

Generate unique field ID from name.

Parameters
idField name or identifier
Returns
Generated field ID string

◆ id()

std::string mb::EntitySchemaField::id ( ) const

Get field unique identifier.

Returns
Field ID string

◆ isForeignKey()

bool mb::EntitySchemaField::isForeignKey ( ) const

Check if field is a foreign key.

Returns
true if foreign key

◆ isPrimaryKey()

bool mb::EntitySchemaField::isPrimaryKey ( ) const

Check if field is primary key.

Returns
true if primary key

◆ isSystem()

bool mb::EntitySchemaField::isSystem ( ) const

Check if field is system field.

Returns
true if system field

◆ isUnique()

bool mb::EntitySchemaField::isUnique ( ) const

Check if field has unique constraint.

Returns
true if unique

◆ isValidFieldType()

bool mb::EntitySchemaField::isValidFieldType ( const std::string &  type)
static

Check if field type is valid.

Parameters
typeType string to validate
Returns
true if valid type

◆ name()

std::string mb::EntitySchemaField::name ( ) const

Get field name.

Returns
Field name

◆ operator==()

bool mb::EntitySchemaField::operator== ( const EntitySchemaField other) const

◆ removeForeignKey()

EntitySchemaField & mb::EntitySchemaField::removeForeignKey ( )

Remove foreign key constraint (fluent interface).

Returns
Reference to self for chaining

◆ required()

bool mb::EntitySchemaField::required ( ) const

Check if field is required.

Returns
true if required

◆ setConstraints()

EntitySchemaField & mb::EntitySchemaField::setConstraints ( const nlohmann::json &  opts)

Set field constraints (fluent interface).

Parameters
optsJSON object with constraint values
Returns
Reference to self for chaining

◆ setForeignKey()

EntitySchemaField & mb::EntitySchemaField::setForeignKey ( const std::string &  table,
const std::string &  column = "id",
const std::string &  onUpdate = "RESTRICT",
const std::string &  onDelete = "RESTRICT" 
)

Set foreign key reference (fluent interface).

Parameters
tableReference table name
columnReference column name (defaults to "id")
onUpdateUpdate policy (defaults to "RESTRICT")
onDeleteDelete policy (defaults to "RESTRICT")
Returns
Reference to self for chaining

◆ setIsPrimaryKey()

EntitySchemaField & mb::EntitySchemaField::setIsPrimaryKey ( bool  pk)

Set primary key flag (fluent interface).

Parameters
pkPrimary key flag
Returns
Reference to self for chaining

◆ setIsSystem()

EntitySchemaField & mb::EntitySchemaField::setIsSystem ( bool  system)

Set system field flag (fluent interface).

Parameters
systemSystem field flag
Returns
Reference to self for chaining

◆ setIsUnique()

EntitySchemaField & mb::EntitySchemaField::setIsUnique ( bool  unique)

Set unique constraint (fluent interface).

Parameters
uniqueUnique flag
Returns
Reference to self for chaining

◆ setName()

EntitySchemaField & mb::EntitySchemaField::setName ( const std::string &  name)

Set field name (fluent interface).

Parameters
nameNew field name
Returns
Reference to self for chaining

◆ setRequired()

EntitySchemaField & mb::EntitySchemaField::setRequired ( bool  required)

Set required flag (fluent interface).

Parameters
requiredRequired flag
Returns
Reference to self for chaining

◆ setType()

EntitySchemaField & mb::EntitySchemaField::setType ( const std::string &  type)

Set field type (fluent interface).

Parameters
typeField type (e.g., "string", "int32", "bool")
Returns
Reference to self for chaining

◆ toJSON()

nlohmann::json mb::EntitySchemaField::toJSON ( ) const

Convert field to JSON representation.

Returns
JSON object with field definition

◆ toSociType() [1/2]

soci::db_type mb::EntitySchemaField::toSociType ( ) const

Convert field type to SOCI database type.

Returns
SOCI db_type enum value

◆ toSociType() [2/2]

soci::db_type mb::EntitySchemaField::toSociType ( const std::string &  type)
static

Convert type string to SOCI database type (static).

Parameters
typeField type string
Returns
SOCI db_type enum value

◆ type()

std::string mb::EntitySchemaField::type ( ) const

Get field type.

Returns
Field type string

◆ updateWith()

EntitySchemaField & mb::EntitySchemaField::updateWith ( const nlohmann::json &  field_schema)

Update field with new JSON data.

Parameters
field_schemaJSON object with field updates
Returns
Reference to self for chaining

◆ validate()

std::optional< std::string > mb::EntitySchemaField::validate ( ) const

Validate field definition.

Returns
Optional error message if validation fails

The documentation for this class was generated from the following files: