Esempio n. 1
0
            value = timezone.make_aware(value, default_timezone)
        value = value.astimezone(timezone.utc).replace(tzinfo=None)
    return escape_string(value.strftime("%Y-%m-%d %H:%M:%S"))

# MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like
# timedelta in terms of actual behavior as they are signed and include days --
# and Django expects time, so we still need to override that. We also need to
# add special handling for SafeText and SafeBytes as MySQLdb's type
# checking is too tight to catch those (see Django ticket #6052).
# Finally, MySQLdb always returns naive datetime objects. However, when
# timezone support is active, Django expects timezone-aware datetime objects.
def typecast_time(v):
    if isinstance(v, bytes):
        v = v.decode('ascii')
    return backend_utils.typecast_time(v)
django_conversions = decoders.copy()
django_conversions.update({
    FIELD_TYPE.TIME: typecast_time,
    FIELD_TYPE.DATETIME: parse_datetime_with_timezone_support,
    datetime.datetime: adapt_datetime_with_timezone_support,
})


# This should match the numerical portion of the version numbers (we can treat
# versions like 5.0.24 and 5.0.24a as the same). Based on the list of version
# at http://dev.mysql.com/doc/refman/4.1/en/news.html and
# http://dev.mysql.com/doc/refman/5.0/en/news.html .
server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')

# MySQLdb-1.2.1 and newer automatically makes use of SHOW WARNINGS on
# MySQL-4.1 and newer, so the MysqlDebugWrapper is unnecessary. Since the
Esempio n. 2
0
from .introspection import DatabaseIntrospection  # isort:skip
from django.db.backends.mysql.operations import DatabaseOperations  # isort:skip
from .schema import DatabaseSchemaEditor  # isort:skip
from django.db.backends.mysql.validation import DatabaseValidation  # isort:skip


# MySQLdb returns TIME columns as timedelta -- they are more like timedelta in
# terms of actual behavior as they are signed and include days -- and Django
# expects time.
def typecast_time(v):
    if isinstance(v, bytes):
        v = v.decode('ascii')
    return backend_utils.typecast_time(v)


django_conversions = decoders.copy()
django_conversions.update({
    FIELD_TYPE.TIME: typecast_time,
})

# This should match the numerical portion of the version numbers (we can treat
# versions like 5.0.24 and 5.0.24a as the same).
server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')


class CursorWrapper:
    """
    A thin wrapper around MySQLdb's normal cursor class that catches particular
    exception instances and reraises them with the correct types.

    Implemented as a wrapper, rather than a subclass, so that it isn't stuck