File: dbapi20.py

package info (click to toggle)
mariadb-connector-python 1.1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 812 kB
  • sloc: python: 6,099; ansic: 4,896; sh: 23; makefile: 14
file content (111 lines) | stat: -rw-r--r-- 3,311 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from mariadb.constants import FIELD_TYPE
import time
import datetime

apilevel = '2.0'

paramstyle = 'qmark'

threadsafety = True


class DbApiType(frozenset):
    """
    Immutable set for type checking

    By default the following sets are defined:

    - BINARY: for binary field types
    - NUMBER: for numeric field types
    - STRING: for character based (string) field types
    - DATE: for date field type(s)
    - DATETIME: for datetime and timestamp field type(s)
    - TIME: for time field type(s)
    - TIMESTAMP: for datetime and timestamp field type(s)


    Example:
    >>> FIELD_TYPE.GEOMETRY == mariadb.BINARY
    True
    >>> FIELD_TYPE.FLOAT == mariadb.BINARY
    False
    """

    def __eq__(self, field_type):
        if (isinstance(field_type, DbApiType)):
            return not self.difference(field_type)
        return field_type in self


BINARY = DbApiType([FIELD_TYPE.GEOMETRY,
                    FIELD_TYPE.LONG_BLOB,
                    FIELD_TYPE.MEDIUM_BLOB,
                    FIELD_TYPE.TINY_BLOB,
                    FIELD_TYPE.BLOB])

STRING = DbApiType([FIELD_TYPE.ENUM,
                    FIELD_TYPE.JSON,
                    FIELD_TYPE.STRING,
                    FIELD_TYPE.VARCHAR,
                    FIELD_TYPE.VAR_STRING])

NUMBER = DbApiType([FIELD_TYPE.DECIMAL,
                    FIELD_TYPE.DOUBLE,
                    FIELD_TYPE.FLOAT,
                    FIELD_TYPE.INT24,
                    FIELD_TYPE.LONG,
                    FIELD_TYPE.LONGLONG,
                    FIELD_TYPE.NEWDECIMAL,
                    FIELD_TYPE.SHORT,
                    FIELD_TYPE.TINY,
                    FIELD_TYPE.YEAR])

DATE = DbApiType([FIELD_TYPE.DATE])
TIME = DbApiType([FIELD_TYPE.TIME])
DATETIME = TIMESTAMP = DbApiType([FIELD_TYPE.DATETIME,
                                 FIELD_TYPE.TIMESTAMP])
ROWID = DbApiType()


def Binary(object):
    """Constructs an object capable of holding a binary value."""
    return bytes(object)


def Date(year, month, day):
    """Constructs an object holding a date value."""
    return datetime.date(year, month, day)


def Time(hour, minute, second):
    """Constructs an object holding a time value."""
    return datetime.time(hour, minute, second)


def Timestamp(year, month, day, hour, minute, second):
    """Constructs an object holding a datetime value."""
    return datetime.datetime(year, month, day, hour, minute, second)


def DateFromTicks(ticks):
    """Constructs an object holding a date value from the given ticks value
       (number of seconds since the epoch).
       For more information see the documentation of the standard Python
       time module."""
    return Date(*time.localtime(ticks)[:3])


def TimeFromTicks(ticks):
    """Constructs an object holding a time value from the given ticks value
       (number of seconds since the epoch).
       For more information see the documentation of the standard Python
       time module."""
    return Time(*time.localtime(ticks)[3:6])


def TimestampFromTicks(ticks):
    """Constructs an object holding a datetime value from the given ticks value
       (number of seconds since the epoch).
       For more information see the documentation of the standard Python
       time module."""
    return datetime.datetime(*time.localtime(ticks)[:6])