Skip to contents

The TableModel class represents a database table in the oRm framework. It manages table structure, fields, relationships, and provides methods for interacting with the database table.

Details

TableModel is a core component of the oRm framework, responsible for:

  • Defining table structure with columns and relationships

  • Creating and managing database tables

  • Providing an interface for CRUD operations on table records

  • Managing relationships between different tables

Key features:

  • Dynamic table creation and management

  • Support for various column types and constraints

  • Relationship definitions and querying

  • Record creation and retrieval

Methods

initialize(tablename, engine, ..., .data = list())

Constructor for creating a new TableModel instance.

get_connection()

Retrieve the active database connection from the engine.

generate_sql_fields()

Generate SQL field definitions for table creation.

create_table(if_not_exists = TRUE, overwrite = FALSE, verbose = FALSE)

Create the associated table in the database.

record(..., .data = list())

Create a new Record object associated with this model.

read(..., mode = c("all", "one_or_none", "get"), limit = NULL)

Read records from the table using dynamic filters.

relationship(rel_name, ...)

Query related records based on defined relationships.

print()

Print a formatted overview of the model, including its fields.

Methods


Method new()

Constructor for a new TableModel.

Usage

TableModel$new(tablename, engine, ..., .data = list())

Arguments

tablename

The name of the database table.

engine

The Engine object for database connection.

...

Column definitions.

.data

a list of Column defintions


Method get_connection()

Retrieve the active database connection from the engine.

Usage

TableModel$get_connection(...)


Method generate_sql_fields()

Generate DBI-compatible field definitions from Column and ForeignKey objects, including constraints and SQL clause generation.

Usage

TableModel$generate_sql_fields()


Method create_table()

Create the associated table in the database.

Create the associated table in the database.

Usage

TableModel$create_table(
  if_not_exists = TRUE,
  overwrite = FALSE,
  verbose = FALSE
)

Arguments

if_not_exists

Logical. If TRUE, only create the table if it doesn't exist. Default is TRUE.

overwrite

Logical. If TRUE, drop the table if it exists and recreate it. Default is FALSE.

verbose

Logical. If TRUE, return the SQL statement instead of executing it. Default is FALSE.


Method record()

Create a new Record object with this model.

Usage

TableModel$record(..., .data = list())

Arguments

...

Named values to initialize the record's data.

.data

a named list of field values.


Method read()

Read records using dynamic filters and return in the specified mode.

Usage

TableModel$read(..., mode = c("all", "one_or_none", "get"), limit = NULL)

Arguments

...

Unquoted expressions for filtering.

mode

One of "all", "one_or_none", or "get".

limit

Integer. Maximum number of records to return. NULL (default) means no limit. Positive values return the first N records, negative values return the last N records.


Method relationship()

Usage

TableModel$relationship(rel_name, ...)


Method print()

Print a formatted overview of the model, including its fields.

Usage

TableModel$print(...)


Method clone()

The objects of this class are cloneable with this method.

Usage

TableModel$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.