1import sqlalchemy as sa
2from sqlalchemy.dialects.postgresql import BIT
3
4
5class BitType(sa.types.TypeDecorator):
6 """
7 BitType offers way of saving BITs into database.
8 """
9
10 impl = sa.types.BINARY
11
12 cache_ok = True
13
14 def __init__(self, length=1, **kwargs):
15 self.length = length
16 sa.types.TypeDecorator.__init__(self, **kwargs)
17
18 def load_dialect_impl(self, dialect):
19 # Use the native BIT type for drivers that has it.
20 if dialect.name == 'postgresql':
21 return dialect.type_descriptor(BIT(self.length))
22 elif dialect.name == 'sqlite':
23 return dialect.type_descriptor(sa.String(self.length))
24 else:
25 return dialect.type_descriptor(type(self.impl)(self.length))