Schema

Schema definition classes for sqbooster table system.

Provides Column and TableSchema classes for defining database table structure.

class sqbooster.schema.Column(name, col_type, primary_key=False, nullable=True, unique=False, default=None, autoincrement=False)[source]

Bases: object

Defines a database column with type and constraints.

Parameters:
  • name – Column name.

  • col_type – Column type instance (e.g., Integer(), Text(), Float()).

  • primary_key – Whether this column is the primary key.

  • nullable – Whether NULL values are allowed.

  • unique – Whether values must be unique.

  • default – Default value for the column.

  • autoincrement – Whether the value auto-increments (primary key only).

Example

Column(“id”, Integer(), primary_key=True, autoincrement=True) Column(“name”, Text(), nullable=False) Column(“age”, Integer(), default=0)

property sql_type

Return the SQL type string, accounting for VARCHAR length.

to_sql_definition(backend='sqlite')[source]

Generate the SQL column definition fragment.

Parameters:

backend – ‘sqlite’ or ‘postgresql’ to handle dialect differences.

to_python(value)[source]

Convert a SQL value to Python type using the column’s type.

to_sql(value)[source]

Convert a Python value to SQL format using the column’s type.

class sqbooster.schema.TableSchema(name, columns)[source]

Bases: object

Defines a complete table schema with columns and metadata.

Parameters:
  • name – Table name.

  • columns – List of Column instances.

property primary_key

Return the primary key column, or None.

get_column(name)[source]

Get a column by name.

Raises:

KeyError – If column doesn’t exist.

has_column(name)[source]

Check if a column exists.

to_sql_create(backend='sqlite')[source]

Generate the CREATE TABLE SQL statement.

Parameters:

backend – ‘sqlite’ or ‘postgresql’.

validate_row(data)[source]

Validate a data dict against this schema.

Parameters:

data – Dictionary of column_name -> value.

Raises:

ValueError – If validation fails.

prepare_insert(data)[source]

Prepare data for INSERT: validate and convert Python values to SQL.

Returns:

Tuple of (column_names, sql_values).

row_to_dict(row, column_names=None)[source]

Convert a SQL row (tuple) back to a Python dict.

Parameters:
  • row – Tuple of values.

  • column_names – List of column names matching the row values.