Skip to content

pg

Postgres-specific column types.

BigInt

Bases: Column[int]

Big integer column type.

Source code in src/embar/column/pg.py
84
85
86
87
88
89
90
class BigInt(Column[int]):
    """
    Big integer column type.
    """

    _sql_type: str = "BIGINT"
    _py_type: Type = int

BigSerial

Bases: Column[int]

Auto-incrementing big integer column.

Source code in src/embar/column/pg.py
103
104
105
106
107
108
109
class BigSerial(Column[int]):
    """
    Auto-incrementing big integer column.
    """

    _sql_type: str = "BIGSERIAL"
    _py_type: Type = int

Boolean

Bases: Column[bool]

Boolean column type.

Source code in src/embar/column/pg.py
47
48
49
50
51
52
53
class Boolean(Column[bool]):
    """
    Boolean column type.
    """

    _sql_type: str = "BOOLEAN"
    _py_type: Type = bool

Char

Bases: Column[str]

Fixed-length character column type.

Source code in src/embar/column/pg.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class Char(Column[str]):
    """
    Fixed-length character column type.
    """

    _sql_type: str = "CHAR"
    _py_type: Type = str

    def __init__(
        self,
        name: str | None = None,
        default: str | None = None,
        primary: bool = False,
        not_null: bool = False,
        length: int | None = None,
    ):
        """
        Create a new Char instance.
        """
        self._extra_args: tuple[int] | tuple[int, int] | None = (length,) if length is not None else None
        super().__init__(name=name, default=default, primary=primary, not_null=not_null)

__init__(name=None, default=None, primary=False, not_null=False, length=None)

Create a new Char instance.

Source code in src/embar/column/pg.py
144
145
146
147
148
149
150
151
152
153
154
155
156
def __init__(
    self,
    name: str | None = None,
    default: str | None = None,
    primary: bool = False,
    not_null: bool = False,
    length: int | None = None,
):
    """
    Create a new Char instance.
    """
    self._extra_args: tuple[int] | tuple[int, int] | None = (length,) if length is not None else None
    super().__init__(name=name, default=default, primary=primary, not_null=not_null)

Date

Bases: Column[date]

Date column type.

Source code in src/embar/column/pg.py
254
255
256
257
258
259
260
class Date(Column[date]):
    """
    Date column type.
    """

    _sql_type: str = "DATE"
    _py_type: Type = date

DoublePrecision

Bases: Column[float]

Double precision floating point column type.

Source code in src/embar/column/pg.py
225
226
227
228
229
230
231
class DoublePrecision(Column[float]):
    """
    Double precision floating point column type.
    """

    _sql_type: str = "DOUBLE PRECISION"
    _py_type: Type = float

EmbarEnum

Bases: str, Enum

EmbarEnum is just a regular Enum but without having to set the right side.

from enum import auto
from embar.column.pg import EmbarEnum
class StatusEnum(EmbarEnum):
   PENDING = auto()
   DONE = auto()
Source code in src/embar/column/pg.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
class EmbarEnum(str, Enum):
    """
    `EmbarEnum` is just a regular Enum but without having to set the right side.

    ```python
    from enum import auto
    from embar.column.pg import EmbarEnum
    class StatusEnum(EmbarEnum):
       PENDING = auto()
       DONE = auto()
    ```
    """

    @staticmethod
    @override
    def _generate_next_value_(name: str, start: int, count: int, last_values: list[Any]) -> str:
        return name

EnumCol

Bases: Column[str]

Column type for Postgres enum values.

Source code in src/embar/column/pg.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
class EnumCol[E: EmbarEnum](Column[str]):
    """
    Column type for Postgres enum values.
    """

    _sql_type: str
    _py_type: Type = str

    def __init__(
        self,
        pg_enum: type[PgEnum[E]],
        name: str | None = None,
        default: E | None = None,
        primary: bool = False,
        not_null: bool = False,
    ):
        """
        Create a new EnumCol instance.
        """
        self._sql_type = pg_enum.name

        super().__init__(name=name, default=default, primary=primary, not_null=not_null)

__init__(pg_enum, name=None, default=None, primary=False, not_null=False)

Create a new EnumCol instance.

Source code in src/embar/column/pg.py
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def __init__(
    self,
    pg_enum: type[PgEnum[E]],
    name: str | None = None,
    default: E | None = None,
    primary: bool = False,
    not_null: bool = False,
):
    """
    Create a new EnumCol instance.
    """
    self._sql_type = pg_enum.name

    super().__init__(name=name, default=default, primary=primary, not_null=not_null)

Float

Bases: Column[float]

A floating point column type.

Source code in src/embar/column/common.py
153
154
155
156
157
158
159
class Float(Column[float]):
    """
    A floating point column type.
    """

    _sql_type: str = "REAL"
    _py_type: Type = float

Integer

Bases: Column[int]

An integer column type.

Source code in src/embar/column/common.py
144
145
146
147
148
149
150
class Integer(Column[int]):
    """
    An integer column type.
    """

    _sql_type: str = "INTEGER"
    _py_type: Type = int

Interval

Bases: Column[timedelta]

Interval column type for storing time intervals.

Source code in src/embar/column/pg.py
263
264
265
266
267
268
269
class Interval(Column[timedelta]):
    """
    Interval column type for storing time intervals.
    """

    _sql_type: str = "INTERVAL"
    _py_type: Type = timedelta

Json

Bases: Column[dict[str, Any]]

JSON column type for storing JSON data.

Source code in src/embar/column/pg.py
235
236
237
238
239
240
241
class Json(Column[dict[str, Any]]):
    """
    JSON column type for storing JSON data.
    """

    _sql_type: str = "JSON"
    _py_type: Type = dict[str, Any]

Jsonb

Bases: Column[dict[str, Any]]

JSONB column type for storing JSON data.

Source code in src/embar/column/pg.py
65
66
67
68
69
70
71
class Jsonb(Column[dict[str, Any]]):
    """
    JSONB column type for storing JSON data.
    """

    _sql_type: str = "JSONB"
    _py_type: Type = dict[str, Any]

Numeric

Bases: Column[Decimal]

Numeric column type with configurable precision and scale.

Source code in src/embar/column/pg.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class Numeric(Column[Decimal]):
    """
    Numeric column type with configurable precision and scale.
    """

    _sql_type: str = "NUMERIC"
    _py_type: Type = Decimal

    _extra_args: tuple[int] | tuple[int, int] | None

    def __init__(
        self,
        name: str | None = None,
        default: Decimal | None = None,
        primary: bool = False,
        not_null: bool = False,
        precision: int | None = None,
        scale: int | None = None,
    ):
        """
        Create a new Numeric instance.
        """
        if precision is None:
            if scale is not None:
                raise Exception("Numeric: 'precision' cannot be None if scale is set")
        elif scale is None:
            self._extra_args = (precision,)
        else:
            self._extra_args = (precision, scale)
        super().__init__(name=name, default=default, primary=primary, not_null=not_null)

__init__(name=None, default=None, primary=False, not_null=False, precision=None, scale=None)

Create a new Numeric instance.

Source code in src/embar/column/pg.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def __init__(
    self,
    name: str | None = None,
    default: Decimal | None = None,
    primary: bool = False,
    not_null: bool = False,
    precision: int | None = None,
    scale: int | None = None,
):
    """
    Create a new Numeric instance.
    """
    if precision is None:
        if scale is not None:
            raise Exception("Numeric: 'precision' cannot be None if scale is set")
    elif scale is None:
        self._extra_args = (precision,)
    else:
        self._extra_args = (precision, scale)
    super().__init__(name=name, default=default, primary=primary, not_null=not_null)

PgDecimal

Bases: Column[Decimal]

Decimal column type with configurable precision and scale.

Source code in src/embar/column/pg.py
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class PgDecimal(Column[Decimal]):
    """
    Decimal column type with configurable precision and scale.
    """

    # Note: DECIMAL is an alias for NUMERIC in PostgreSQL
    _sql_type: str = "DECIMAL"
    _py_type: Type = Decimal

    _extra_args: tuple[int] | tuple[int, int] | None

    def __init__(
        self,
        name: str | None = None,
        default: Decimal | None = None,
        primary: bool = False,
        not_null: bool = False,
        precision: int | None = None,
        scale: int | None = None,
    ):
        """
        Create a new PgDecimal instance.
        """
        if precision is None:
            if scale is not None:
                raise Exception("Numeric: 'precision' cannot be None if scale is set")
        elif scale is None:
            self._extra_args = (precision,)
        else:
            self._extra_args = (precision, scale)
        super().__init__(name=name, default=default, primary=primary, not_null=not_null)

__init__(name=None, default=None, primary=False, not_null=False, precision=None, scale=None)

Create a new PgDecimal instance.

Source code in src/embar/column/pg.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def __init__(
    self,
    name: str | None = None,
    default: Decimal | None = None,
    primary: bool = False,
    not_null: bool = False,
    precision: int | None = None,
    scale: int | None = None,
):
    """
    Create a new PgDecimal instance.
    """
    if precision is None:
        if scale is not None:
            raise Exception("Numeric: 'precision' cannot be None if scale is set")
    elif scale is None:
        self._extra_args = (precision,)
    else:
        self._extra_args = (precision, scale)
    super().__init__(name=name, default=default, primary=primary, not_null=not_null)

PgEnum

Bases: EnumBase

`PgEnum is used to create Postgres enum types.

Subclasses must always assign values to the two class variables!

from enum import auto
from embar.table import Table
from embar.column.pg import EmbarEnum, EnumCol, PgEnum
class StatusEnum(EmbarEnum):
   PENDING = auto()
   DONE = auto()
class StatusPgEnum(PgEnum[StatusEnum]):
    name: str = "status_enum"
    enum: type[StatusEnum] = StatusEnum
class TableWithStatus(Table):
    status: EnumCol[StatusEnum] = EnumCol(StatusPgEnum)
Source code in src/embar/column/pg.py
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
class PgEnum[E: EmbarEnum](EnumBase):
    """
    `PgEnum is used to create Postgres enum types.

    Subclasses must always assign values to the two class variables!

    ```python
    from enum import auto
    from embar.table import Table
    from embar.column.pg import EmbarEnum, EnumCol, PgEnum
    class StatusEnum(EmbarEnum):
       PENDING = auto()
       DONE = auto()
    class StatusPgEnum(PgEnum[StatusEnum]):
        name: str = "status_enum"
        enum: type[StatusEnum] = StatusEnum
    class TableWithStatus(Table):
        status: EnumCol[StatusEnum] = EnumCol(StatusPgEnum)
    ```
    """

    name: str
    enum: type[E]

    @override
    @classmethod
    def ddl(cls) -> str:
        quoted = [f"'{e.name}'" for e in cls.enum]
        values = ", ".join(quoted)
        sql = f"CREATE TYPE {cls.name} AS ENUM ({values});"
        return sql

Serial

Bases: Column[int]

Auto-incrementing integer column.

Source code in src/embar/column/pg.py
38
39
40
41
42
43
44
class Serial(Column[int]):
    """
    Auto-incrementing integer column.
    """

    _sql_type: str = "SERIAL"
    _py_type: Type = int

SmallInt

Bases: Column[int]

Small integer column type.

Source code in src/embar/column/pg.py
75
76
77
78
79
80
81
class SmallInt(Column[int]):
    """
    Small integer column type.
    """

    _sql_type: str = "SMALLINT"
    _py_type: Type = int

SmallSerial

Bases: Column[int]

Auto-incrementing small integer column.

Source code in src/embar/column/pg.py
 94
 95
 96
 97
 98
 99
100
class SmallSerial(Column[int]):
    """
    Auto-incrementing small integer column.
    """

    _sql_type: str = "SMALLSERIAL"
    _py_type: Type = int

Text

Bases: Column[str]

A text column type.

Source code in src/embar/column/common.py
135
136
137
138
139
140
141
class Text(Column[str]):
    """
    A text column type.
    """

    _sql_type: str = "TEXT"
    _py_type: Type = str

Time

Bases: Column[time]

Time column type.

Source code in src/embar/column/pg.py
245
246
247
248
249
250
251
class Time(Column[time]):
    """
    Time column type.
    """

    _sql_type: str = "TIME"
    _py_type: Type = time

Timestamp

Bases: Column[datetime]

Timestamp column type.

Source code in src/embar/column/pg.py
56
57
58
59
60
61
62
class Timestamp(Column[datetime]):
    """
    Timestamp column type.
    """

    _sql_type: str = "TIMESTAMP"
    _py_type: Type = str

Varchar

Bases: Column[str]

Variable-length character column type.

Source code in src/embar/column/pg.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Varchar(Column[str]):
    """
    Variable-length character column type.
    """

    _sql_type: str = "VARCHAR"
    _py_type: Type = str

    def __init__(
        self,
        name: str | None = None,
        default: str | None = None,
        primary: bool = False,
        not_null: bool = False,
        length: int | None = None,
    ):
        """
        Create a new Varchar instance.
        """
        self._extra_args: tuple[int] | tuple[int, int] | None = (length,) if length is not None else None
        super().__init__(name=name, default=default, primary=primary, not_null=not_null)

__init__(name=None, default=None, primary=False, not_null=False, length=None)

Create a new Varchar instance.

Source code in src/embar/column/pg.py
121
122
123
124
125
126
127
128
129
130
131
132
133
def __init__(
    self,
    name: str | None = None,
    default: str | None = None,
    primary: bool = False,
    not_null: bool = False,
    length: int | None = None,
):
    """
    Create a new Varchar instance.
    """
    self._extra_args: tuple[int] | tuple[int, int] | None = (length,) if length is not None else None
    super().__init__(name=name, default=default, primary=primary, not_null=not_null)