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(name, schema, description, owner, privileges)

A user-defined type, such as a composite, domain or enum

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(name, schema, description, owner, privileges, input, output, receive=None, send=None, typmod_in=None, typmod_out=None, analyze=None, internallength=1, alignment=None, storage=None, delimiter=', ', category=None, preferred=False, oid=None)

A base type

BaseType.to_map(db, no_owner, no_privs)

Convert a type to a YAML-suitable format

Parameters:no_owner – exclude type owner information
Returns:dictionary
BaseType.to_map(db, no_owner, no_privs)

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()

Generate SQL to drop the type

Returns:list of SQL statements

The CASCADE thing is mandatory to drop the functions too. There is a cyclic dependency so the dependency graph cannot be used. The functions will not be explicitly dropped.

Composite

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

class pyrseas.dbobject.dbtype.Composite(name, schema, description, owner, privileges, oid=None)

A composite type

static Composite.from_map(name, schema, inobj)

Initialize a Composite instance from a YAML map

Parameters:
  • name – Composite name
  • schema – schema map
  • inobj – YAML map of the Composite
Returns:

Composite instance

Composite.to_map(db, no_owner, no_privs)

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.alter(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(name, schema, description, owner, privileges, labels, oid=None)

An enumerated type definition

static Enum.from_map(name, schema, inobj)

Initialize an Enum instance from a YAML map

Parameters:
  • name – Enum name
  • schema – schema map
  • inobj – YAML map of the Enum
Returns:

Enum instance

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(name, schema, description, owner, privileges, type, not_null=False, default=None, oid=None)

A domain definition

static Domain.from_map(name, schema, inobj)

Initialize an Domain instance from a YAML map

Parameters:
  • name – Domain name
  • schema – schema map
  • inobj – YAML map of the Domain
Returns:

Domain instance

Domain.to_map(db, no_owner, no_privs)

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 user-defined types 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