~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
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
8
import datetime
9
5821.5.36 by James Henstridge
* Fix doc/gina.txt test.
10
from storm.exceptions import NotOneError as SQLObjectMoreThanOneResultError
5821.2.57 by James Henstridge
more changes to resolve failing tests.
11
from storm.expr import SQL
5821.2.4 by James Henstridge
* Remove psycopgda, sqlobject and sqlos from lib/ directory.
12
from storm.sqlobject import *
13
14
# Provide the same interface from these other locations.
15
import sys
16
sys.modules['sqlobject.joins'] = sys.modules['sqlobject']
17
sys.modules['sqlobject.sqlbuilder'] = sys.modules['sqlobject']
18
del sys
19
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
20
_sqlStringReplace = [
21
    ('\\', '\\\\'),
22
    ("'", "''"),
23
    ('\000', '\\0'),
24
    ('\b', '\\b'),
25
    ('\n', '\\n'),
26
    ('\r', '\\r'),
27
    ('\t', '\\t'),
28
    ]
29
5821.2.12 by James Henstridge
Changes necessary to run pagests/openid tests successfully.
30
# XXX 2007-03-07 jamesh:
31
# This is a cut down version of sqlobject's sqlrepr() method.  Ideally
32
# 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.
33
def sqlrepr(value, dbname=None):
5821.2.4 by James Henstridge
* Remove psycopgda, sqlobject and sqlos from lib/ directory.
34
    assert dbname in [None, 'postgres']
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
35
    if hasattr(value, '__sqlrepr__'):
36
        return value.__sqlrepr__(dbname)
5821.2.12 by James Henstridge
Changes necessary to run pagests/openid tests successfully.
37
    elif hasattr(value, 'getquoted'):
38
        return value.getquoted()
5821.2.57 by James Henstridge
more changes to resolve failing tests.
39
    elif isinstance(value, SQL):
40
        return value.expr
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
41
    elif isinstance(value, (str, unicode)):
42
        for orig, repl in _sqlStringReplace:
43
            value = value.replace(orig, repl)
44
        return "'%s'" % value
45
    elif isinstance(value, bool):
46
        if value:
47
            return "'t'"
48
        else:
49
            return "'f'"
5821.2.54 by James Henstridge
Handle bool before int in sqlrepr() compat wrapper.
50
    elif isinstance(value, int):
51
        return repr(int(value))
52
    elif isinstance(value, long):
53
        return str(value)
5821.2.8 by James Henstridge
Fix a few more issues uncovered by clicking around a bit.
54
    elif isinstance(value, float):
55
        return repr(value)
56
    elif value is None:
57
        return "NULL"
58
    elif isinstance(value, (list, set, tuple)):
59
        return "(%s)" % ", ".join(sqlrepr(v, dbname) for v in value)
60
    elif isinstance(value, datetime.datetime):
5821.4.13 by James Henstridge
Make sqlrepr() output ISO formatted time, to match what sqlobject was
61
        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.
62
    elif isinstance(value, datetime.time):
63
        return value.strftime("'%H:%M:%S'")
64
    elif isinstance(value, datetime.date):
65
        return value.strftime("'%Y-%m-%d'")
66
    elif isinstance(value, datetime.timedelta):
67
        return "INTERVAL '%d DAYS %d SECONDS %d MICROSECONDS'" % (
68
            value.days, value.seconds, value.microseconds)
69
    else:
70
        raise AssertionError("Unhandled type: %r" % type(value))