~launchpad-pqm/launchpad/devel

8687.15.20 by Karl Fogel
Add the copyright header block to two stragglers under lib/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
5821.2.4 by James Henstridge
* Remove psycopgda, sqlobject and sqlos from lib/ directory.
3
4
"""Expose the Storm SQLObject compatibility layer."""
5
6
__metaclass__ = type
7
14612.2.4 by William Grant
skip sqlobject when format-importsing, as it does some sys mangling that relies on order.
8
# SKIP this file when reformatting, due to the sys mangling.
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
9
import datetime
10
5821.5.36 by James Henstridge
* Fix doc/gina.txt test.
11
from storm.exceptions import NotOneError as SQLObjectMoreThanOneResultError
5821.2.57 by James Henstridge
more changes to resolve failing tests.
12
from storm.expr import SQL
5821.2.4 by James Henstridge
* Remove psycopgda, sqlobject and sqlos from lib/ directory.
13
from storm.sqlobject import *
14
15
# Provide the same interface from these other locations.
16
import sys
17
sys.modules['sqlobject.joins'] = sys.modules['sqlobject']
18
sys.modules['sqlobject.sqlbuilder'] = sys.modules['sqlobject']
19
del sys
20
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
21
_sqlStringReplace = [
22
    ('\\', '\\\\'),
23
    ("'", "''"),
24
    ('\000', '\\0'),
25
    ('\b', '\\b'),
26
    ('\n', '\\n'),
27
    ('\r', '\\r'),
28
    ('\t', '\\t'),
29
    ]
30
5821.2.12 by James Henstridge
Changes necessary to run pagests/openid tests successfully.
31
# XXX 2007-03-07 jamesh:
32
# This is a cut down version of sqlobject's sqlrepr() method.  Ideally
33
# we can get rid of this as code is converted to use store.execute().
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
34
def sqlrepr(value, dbname=None):
5821.2.4 by James Henstridge
* Remove psycopgda, sqlobject and sqlos from lib/ directory.
35
    assert dbname in [None, 'postgres']
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
36
    if hasattr(value, '__sqlrepr__'):
37
        return value.__sqlrepr__(dbname)
5821.2.12 by James Henstridge
Changes necessary to run pagests/openid tests successfully.
38
    elif hasattr(value, 'getquoted'):
39
        return value.getquoted()
5821.2.57 by James Henstridge
more changes to resolve failing tests.
40
    elif isinstance(value, SQL):
41
        return value.expr
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
42
    elif isinstance(value, (str, unicode)):
43
        for orig, repl in _sqlStringReplace:
44
            value = value.replace(orig, repl)
45
        return "'%s'" % value
46
    elif isinstance(value, bool):
47
        if value:
48
            return "'t'"
49
        else:
50
            return "'f'"
5821.2.54 by James Henstridge
Handle bool before int in sqlrepr() compat wrapper.
51
    elif isinstance(value, int):
52
        return repr(int(value))
53
    elif isinstance(value, long):
54
        return str(value)
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
55
    elif isinstance(value, float):
56
        return repr(value)
57
    elif value is None:
58
        return "NULL"
59
    elif isinstance(value, (list, set, tuple)):
60
        return "(%s)" % ", ".join(sqlrepr(v, dbname) for v in value)
61
    elif isinstance(value, datetime.datetime):
5821.4.13 by James Henstridge
Make sqlrepr() output ISO formatted time, to match what sqlobject was
62
        return value.strftime("'%Y-%m-%dT%H:%M:%S'")
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
63
    elif isinstance(value, datetime.time):
64
        return value.strftime("'%H:%M:%S'")
65
    elif isinstance(value, datetime.date):
66
        return value.strftime("'%Y-%m-%d'")
67
    elif isinstance(value, datetime.timedelta):
68
        return "INTERVAL '%d DAYS %d SECONDS %d MICROSECONDS'" % (
69
            value.days, value.seconds, value.microseconds)
70
    else:
71
        raise AssertionError("Unhandled type: %r" % type(value))