File: util.py

package info (click to toggle)
twextpy 1%3A0.1~git20161216.0.b90293c-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,724 kB
  • sloc: python: 20,458; sh: 742; makefile: 5
file content (79 lines) | stat: -rw-r--r-- 2,870 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
# -*- test-case-name: twext.enterprise.test.test_util -*-
##
# Copyright (c) 2010-2016 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Utilities for dealing with different databases.
"""

from datetime import datetime

SQL_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S.%f"


def parseSQLTimestamp(ts):
    """
    Parse an SQL timestamp string.
    """
    # Handle case where fraction seconds may not be present
    if not isinstance(ts, datetime):
        if len(ts) < len(SQL_TIMESTAMP_FORMAT):
            ts += ".0"
        return datetime.strptime(ts, SQL_TIMESTAMP_FORMAT)
    else:
        return ts


def mapOracleOutputType(column):
    """
    Map a single output value from cx_Oracle based on some rules and
    expectations that we have based on the pgdb bindings.

    @param column: a single value from a column.

    @return: a converted value based on the type of the input; oracle CLOBs and
        datetime timestamps will be converted to strings, unicode values will
        be converted to UTF-8 encoded byte sequences (C{str}s), and floating
        point numbers will be converted to integer types if they are integers.
        Any other types will be left alone.
    """
    if hasattr(column, "read"):
        # Try to detect large objects and format convert them to
        # strings on the fly.  We need to do this as we read each
        # row, due to the issue described here -
        # http://cx-oracle.sourceforge.net/html/lob.html - in
        # particular, the part where it says "In particular, do not
        # use the fetchall() method".
        column = column.read()

    elif isinstance(column, float):
        # cx_Oracle maps _all_ numbers to float types, which is more
        # consistent, but we expect the database to be able to store integers
        # as integers (in fact almost all the values in our schema are
        # integers), so we map those values which exactly match back into
        # integers.
        if int(column) == column:
            return int(column)
        else:
            return column

    if isinstance(column, unicode):
        # Finally, we process all data as UTF-8 byte strings in order to reduce
        # memory consumption.  Pass any unicode string values back to the
        # application as unicode.
        column = column.encode("utf-8")

    return column