Types and Domains

The dbtype module defines six classes, DbType derived from DbSchemaObject, BaseType, Composite, Enum and Domain derived from DbType, and TypeDict derived from and DbObjectDict.

Database Type

Class DbType is derived from DbSchemaObject and represents a SQL type or domain as defined in the PostgreSQL pg_type catalog. Note: Only enumerated types are implemented currently.

class pyrseas.dbobject.dbtype.DbType(**attrs)

A composite, domain or enum type

Base Type

BaseType is derived from DbType and represents a PostgreSQL user-defined base type.

The map returned by to_map() and expected as argument by diff_map() has the following structure (not all fields need be present):

{'type t1':
    {'alignment': 'double',
     'analyze': 'analyze_func',
     'input': 'input_func',
     'internallength': 'variable',
     'output': 'output_func',
     'receive': 'receive_func',
     'send': 'send_func',
     'storage': 'plain'
     'typmod_in': 'typmod_in_func',
     'typmod_out': 'typmod_out_func'
    }
}
class pyrseas.dbobject.dbtype.BaseType(**attrs)

A composite type

BaseType.to_map(no_owner)

Convert a type to a YAML-suitable format

Parameters:no_owner – exclude type owner information
Returns:dictionary
BaseType.create(obj, *args, **kwargs)

Return SQL statements to CREATE the base type

Returns:SQL statements
BaseType.drop()

Return SQL statement to DROP the base type

Returns:SQL statement

We have to override the super method and add CASCADE to drop dependent functions.

Composite

Composite is derived from DbType and represents a standalone composite type.

class pyrseas.dbobject.dbtype.Composite(**attrs)

A composite type

Composite.to_map(no_owner)

Convert a type to a YAML-suitable format

Parameters:no_owner – exclude type owner information
Returns:dictionary
Composite.create(obj, *args, **kwargs)

Return SQL statements to CREATE the composite type

Returns:SQL statements
Composite.diff_map(intype)

Generate SQL to transform an existing composite type

Parameters:intype – the new composite type
Returns:list of SQL statements

Compares the type to an input type and generates SQL statements to transform it into the one represented by the input.

Enum

Enum is derived from DbType and represents an enumerated type.

class pyrseas.dbobject.dbtype.Enum(**attrs)

An enumerated type definition

Enum.create(obj, *args, **kwargs)

Return SQL statements to CREATE the enum

Returns:SQL statements

Domain

Domain is derived from DbType and represents a domain.

class pyrseas.dbobject.dbtype.Domain(**attrs)

A domain definition

Domain.to_map(no_owner)

Convert a domain to a YAML-suitable format

Parameters:no_owner – exclude domain owner information
Returns:dictionary
Domain.create(obj, *args, **kwargs)

Return SQL statements to CREATE the domain

Returns:SQL statements

Type Dictionary

TypeDict is derived from DbObjectDict. It is a dictionary that represents the collection of domains and enums in a database.

class pyrseas.dbobject.dbtype.TypeDict(dbconn=None)

The collection of domains and enums in a database

TypeDict.from_map(schema, inobjs, newdb)

Initalize the dictionary of types by converting the input map

Parameters:
  • schema – schema owning the types
  • inobjs – YAML map defining the schema objects
  • newdb – collection of dictionaries defining the database

Connect various objects to their corresponding types or domains

Parameters:
  • dbcolumns – dictionary of columns
  • dbconstrs – dictionary of constraints
  • dbfuncs – dictionary of functions

Fills the check_constraints dictionaries for each domain by traversing the dbconstrs dictionary. Fills the attributes list for composite types. Fills the dependent functions dictionary for base types.

TypeDict.diff_map(intypes)

Generate SQL to transform existing domains and types

Parameters:intypes – a YAML map defining the new domains/types
Returns:list of SQL statements

Compares the existing domain/type definitions, as fetched from the catalogs, to the input map and generates SQL statements to transform the domains/types accordingly.

Table Of Contents

Previous topic

Operator Classes

Next topic

Tables, Views and Sequences

This Page