-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.py
67 lines (43 loc) · 1.44 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
import time
from sqlalchemy import types
from sqlalchemy.sql import expression
__all__ = ['Float', 'String', 'Integer', 'Timestamp', 'Multi', 'Boolean']
class _TypeDecorator(types.TypeDecorator):
default = None
def process_bind_param(self, value, dialect):
return self.default if value is None else value
class Float(_TypeDecorator):
impl = types.Float
default = 0.0
class String(_TypeDecorator):
impl = types.String
default = u''
class Integer(_TypeDecorator):
impl = types.Integer
default = 0
class Timestamp(types.TypeDecorator):
impl = types.Integer
def process_bind_param(self, value, dialect):
if value is not None:
return int(time.mktime(value.timetuple()))
return 0
def process_result_value(self, value, dialect):
if value:
return value.fromtimestamp(value) if value else None
return None
class Boolean(types.TypeDecorator):
impl = types.Boolean
def process_bind_param(self, value, dialect):
return int(value)
def process_result_value(self, value, dialect):
return bool(value)
class Multi(types.UserDefinedType):
type = types.Integer
class MultiExpression(expression.Tuple):
__visit_name__ = 'multi'
type = Multi
def __init__(self, clauses, **kw):
super(MultiExpression, self).__init__(*clauses, **kw)
def self_group(self, against=None):
return self