~ubuntu-branches/debian/sid/sqlalchemy/sid

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/dialects/postgresql/pg8000.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2014-06-27 20:17:13 UTC
  • mfrom: (1.4.28)
  • Revision ID: package-import@ubuntu.com-20140627201713-g6p1kq8q1qenztrv
Tags: 0.9.6-1
* New upstream release
* Remove Python 3.X build tag files, thanks to Matthias Urlichs for the
  patch (closes: #747852)
* python-fdb isn't in the Debian archive yet so default dialect for firebird://
  URLs is changed to obsolete kinterbasdb, thanks to Russell Stuart for the
  patch (closes: #752145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# postgresql/pg8000.py
2
 
# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS file>
 
2
# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS
 
3
# file>
3
4
#
4
5
# This module is part of SQLAlchemy and is released under
5
6
# the MIT License: http://www.opensource.org/licenses/mit-license.php
8
9
.. dialect:: postgresql+pg8000
9
10
    :name: pg8000
10
11
    :dbapi: pg8000
11
 
    :connectstring: postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...]
12
 
    :url: http://pybrary.net/pg8000/
 
12
    :connectstring: \
 
13
postgresql+pg8000://user:password@host:port/dbname[?key=value&key=value...]
 
14
    :url: https://pythonhosted.org/pg8000/
13
15
 
14
16
Unicode
15
17
-------
16
18
 
17
 
pg8000 requires that the postgresql client encoding be
18
 
configured in the postgresql.conf file in order to use encodings
19
 
other than ascii. Set this value to the same value as the
20
 
"encoding" parameter on create_engine(), usually "utf-8".
21
 
 
22
 
Interval
23
 
--------
24
 
 
25
 
Passing data from/to the Interval type is not supported as of
26
 
yet.
 
19
When communicating with the server, pg8000 uses the character set that the
 
20
server asks it to use (the client encoding). By default the client encoding is
 
21
the database's character set (chosen when the database is created), but the
 
22
client encoding can be changed in a number of ways (eg. setting CLIENT_ENCODING
 
23
in postgresql.conf).
 
24
 
 
25
Set the "encoding" parameter on create_engine(), to the same as the client
 
26
encoding, usually "utf-8".
 
27
 
 
28
.. _pg8000_isolation_level:
 
29
 
 
30
pg8000 Transaction Isolation Level
 
31
-------------------------------------
 
32
 
 
33
The pg8000 dialect offers the same isolation level settings as that
 
34
of the :ref:`psycopg2 <psycopg2_isolation_level>` dialect:
 
35
 
 
36
* ``READ COMMITTED``
 
37
* ``READ UNCOMMITTED``
 
38
* ``REPEATABLE READ``
 
39
* ``SERIALIZABLE``
 
40
* ``AUTOCOMMIT``
 
41
 
 
42
.. versionadded:: 0.9.5 support for AUTOCOMMIT isolation level when using
 
43
   pg8000.
 
44
 
 
45
.. seealso::
 
46
 
 
47
    :ref:`postgresql_isolation_level`
 
48
 
 
49
    :ref:`psycopg2_isolation_level`
 
50
 
27
51
 
28
52
"""
29
53
from ... import util, exc
30
54
import decimal
31
55
from ... import processors
32
56
from ... import types as sqltypes
33
 
from .base import PGDialect, \
34
 
                PGCompiler, PGIdentifierPreparer, PGExecutionContext,\
35
 
                _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES
 
57
from .base import (
 
58
    PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext,
 
59
    _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES)
36
60
 
37
61
 
38
62
class _PGNumeric(sqltypes.Numeric):
40
64
        if self.asdecimal:
41
65
            if coltype in _FLOAT_TYPES:
42
66
                return processors.to_decimal_processor_factory(
43
 
                                    decimal.Decimal,
44
 
                                    self._effective_decimal_return_scale)
 
67
                    decimal.Decimal, self._effective_decimal_return_scale)
45
68
            elif coltype in _DECIMAL_TYPES or coltype in _INT_TYPES:
46
69
                # pg8000 returns Decimal natively for 1700
47
70
                return None
48
71
            else:
49
72
                raise exc.InvalidRequestError(
50
 
                            "Unknown PG numeric type: %d" % coltype)
 
73
                    "Unknown PG numeric type: %d" % coltype)
51
74
        else:
52
75
            if coltype in _FLOAT_TYPES:
53
76
                # pg8000 returns float natively for 701
56
79
                return processors.to_float
57
80
            else:
58
81
                raise exc.InvalidRequestError(
59
 
                            "Unknown PG numeric type: %d" % coltype)
 
82
                    "Unknown PG numeric type: %d" % coltype)
60
83
 
61
84
 
62
85
class _PGNumericNoBind(_PGNumeric):
71
94
class PGCompiler_pg8000(PGCompiler):
72
95
    def visit_mod_binary(self, binary, operator, **kw):
73
96
        return self.process(binary.left, **kw) + " %% " + \
74
 
                        self.process(binary.right, **kw)
 
97
            self.process(binary.right, **kw)
75
98
 
76
99
    def post_process_text(self, text):
77
100
        if '%%' in text:
111
134
 
112
135
    @classmethod
113
136
    def dbapi(cls):
114
 
        return __import__('pg8000').dbapi
 
137
        return __import__('pg8000')
115
138
 
116
139
    def create_connect_args(self, url):
117
140
        opts = url.translate_connect_args(username='user')
123
146
    def is_disconnect(self, e, connection, cursor):
124
147
        return "connection is closed" in str(e)
125
148
 
 
149
    def set_isolation_level(self, connection, level):
 
150
        level = level.replace('_', ' ')
 
151
 
 
152
        if level == 'AUTOCOMMIT':
 
153
            connection.connection.autocommit = True
 
154
        elif level in self._isolation_lookup:
 
155
            connection.connection.autocommit = False
 
156
            cursor = connection.cursor()
 
157
            cursor.execute(
 
158
                "SET SESSION CHARACTERISTICS AS TRANSACTION "
 
159
                "ISOLATION LEVEL %s" % level)
 
160
            cursor.execute("COMMIT")
 
161
            cursor.close()
 
162
        else:
 
163
            raise exc.ArgumentError(
 
164
                "Invalid value '%s' for isolation_level. "
 
165
                "Valid isolation levels for %s are %s or AUTOCOMMIT" %
 
166
                (level, self.name, ", ".join(self._isolation_lookup))
 
167
                )
 
168
 
126
169
dialect = PGDialect_pg8000