source: trunk/pg.py @ 956

Last change on this file since 956 was 956, checked in by cito, 10 months ago

Make name in query_prepared a keyword-only parameter

  • Property svn:keywords set to Id
File size: 96.4 KB
Line 
1#! /usr/bin/python
2#
3# pg.py
4#
5# $Id: pg.py 956 2019-01-04 11:29:11Z cito $
6#
7
8"""PyGreSQL classic interface.
9
10This pg module implements some basic database management stuff.
11It includes the _pg module and builds on it, providing the higher
12level wrapper class named DB with additional functionality.
13This is known as the "classic" ("old style") PyGreSQL interface.
14For a DB-API 2 compliant interface use the newer pgdb module.
15"""
16
17# Copyright (c) 1997-2019 by D'Arcy J.M. Cain.
18#
19# Contributions made by Ch. Zwerschke and others.
20#
21# The notification handler is based on pgnotify which is
22# Copyright (c) 2001 Ng Pheng Siong. All rights reserved.
23#
24# Permission to use, copy, modify, and distribute this software and its
25# documentation for any purpose and without fee is hereby granted,
26# provided that the above copyright notice appear in all copies and that
27# both that copyright notice and this permission notice appear in
28# supporting documentation.
29
30from __future__ import print_function, division
31
32from _pg import *
33
34__version__ = version
35
36import select
37import warnings
38import weakref
39
40from datetime import date, time, datetime, timedelta, tzinfo
41from decimal import Decimal
42from math import isnan, isinf
43from collections import namedtuple
44from keyword import iskeyword
45from operator import itemgetter
46from functools import partial
47from re import compile as regex
48from json import loads as jsondecode, dumps as jsonencode
49from uuid import UUID
50
51try:
52    long
53except NameError:  # Python >= 3.0
54    long = int
55
56try:
57    basestring
58except NameError:  # Python >= 3.0
59    basestring = (str, bytes)
60
61try:
62    from functools import lru_cache
63except ImportError:  # Python < 3.2
64    from functools import update_wrapper
65    try:
66        from _thread import RLock
67    except ImportError:
68        class RLock:  # for builds without threads
69            def __enter__(self): pass
70
71            def __exit__(self, exctype, excinst, exctb): pass
72
73    def lru_cache(maxsize=128):
74        """Simplified functools.lru_cache decorator for one argument."""
75
76        def decorator(function):
77            sentinel = object()
78            cache = {}
79            get = cache.get
80            lock = RLock()
81            root = []
82            root_full = [root, False]
83            root[:] = [root, root, None, None]
84
85            if maxsize == 0:
86
87                def wrapper(arg):
88                    res = function(arg)
89                    return res
90
91            elif maxsize is None:
92
93                def wrapper(arg):
94                    res = get(arg, sentinel)
95                    if res is not sentinel:
96                        return res
97                    res = function(arg)
98                    cache[arg] = res
99                    return res
100
101            else:
102
103                def wrapper(arg):
104                    with lock:
105                        link = get(arg)
106                        if link is not None:
107                            root = root_full[0]
108                            prev, next, _arg, res = link
109                            prev[1] = next
110                            next[0] = prev
111                            last = root[0]
112                            last[1] = root[0] = link
113                            link[0] = last
114                            link[1] = root
115                            return res
116                    res = function(arg)
117                    with lock:
118                        root, full = root_full
119                        if arg in cache:
120                            pass
121                        elif full:
122                            oldroot = root
123                            oldroot[2] = arg
124                            oldroot[3] = res
125                            root = root_full[0] = oldroot[1]
126                            oldarg = root[2]
127                            oldres = root[3]  # keep reference
128                            root[2] = root[3] = None
129                            del cache[oldarg]
130                            cache[arg] = oldroot
131                        else:
132                            last = root[0]
133                            link = [last, root, arg, res]
134                            last[1] = root[0] = cache[arg] = link
135                            if len(cache) >= maxsize:
136                                root_full[1] = True
137                    return res
138
139            wrapper.__wrapped__ = function
140            return update_wrapper(wrapper, function)
141
142        return decorator
143
144
145# Auxiliary classes and functions that are independent from a DB connection:
146
147try:
148    from collections import OrderedDict
149except ImportError:  # Python 2.6 or 3.0
150    OrderedDict = dict
151
152
153    class AttrDict(dict):
154        """Simple read-only ordered dictionary for storing attribute names."""
155
156        def __init__(self, *args, **kw):
157            if len(args) > 1 or kw:
158                raise TypeError
159            items = args[0] if args else []
160            if isinstance(items, dict):
161                raise TypeError
162            items = list(items)
163            self._keys = [item[0] for item in items]
164            dict.__init__(self, items)
165            self._read_only = True
166            error = self._read_only_error
167            self.clear = self.update = error
168            self.pop = self.setdefault = self.popitem = error
169
170        def __setitem__(self, key, value):
171            if self._read_only:
172                self._read_only_error()
173            dict.__setitem__(self, key, value)
174
175        def __delitem__(self, key):
176            if self._read_only:
177                self._read_only_error()
178            dict.__delitem__(self, key)
179
180        def __iter__(self):
181            return iter(self._keys)
182
183        def keys(self):
184            return list(self._keys)
185
186        def values(self):
187            return [self[key] for key in self]
188
189        def items(self):
190            return [(key, self[key]) for key in self]
191
192        def iterkeys(self):
193            return self.__iter__()
194
195        def itervalues(self):
196            return iter(self.values())
197
198        def iteritems(self):
199            return iter(self.items())
200
201        @staticmethod
202        def _read_only_error(*args, **kw):
203            raise TypeError('This object is read-only')
204
205else:
206
207     class AttrDict(OrderedDict):
208        """Simple read-only ordered dictionary for storing attribute names."""
209
210        def __init__(self, *args, **kw):
211            self._read_only = False
212            OrderedDict.__init__(self, *args, **kw)
213            self._read_only = True
214            error = self._read_only_error
215            self.clear = self.update = error
216            self.pop = self.setdefault = self.popitem = error
217
218        def __setitem__(self, key, value):
219            if self._read_only:
220                self._read_only_error()
221            OrderedDict.__setitem__(self, key, value)
222
223        def __delitem__(self, key):
224            if self._read_only:
225                self._read_only_error()
226            OrderedDict.__delitem__(self, key)
227
228        @staticmethod
229        def _read_only_error(*args, **kw):
230            raise TypeError('This object is read-only')
231
232try:
233    from inspect import signature
234except ImportError:  # Python < 3.3
235    from inspect import getargspec
236
237    def get_args(func):
238        return getargspec(func).args
239else:
240
241    def get_args(func):
242        return list(signature(func).parameters)
243
244try:
245    from datetime import timezone
246except ImportError:  # Python < 3.2
247
248    class timezone(tzinfo):
249        """Simple timezone implementation."""
250
251        def __init__(self, offset, name=None):
252            self.offset = offset
253            if not name:
254                minutes = self.offset.days * 1440 + self.offset.seconds // 60
255                if minutes < 0:
256                    hours, minutes = divmod(-minutes, 60)
257                    hours = -hours
258                else:
259                    hours, minutes = divmod(minutes, 60)
260                name = 'UTC%+03d:%02d' % (hours, minutes)
261            self.name = name
262
263        def utcoffset(self, dt):
264            return self.offset
265
266        def tzname(self, dt):
267            return self.name
268
269        def dst(self, dt):
270            return None
271
272    timezone.utc = timezone(timedelta(0), 'UTC')
273
274    _has_timezone = False
275else:
276    _has_timezone = True
277
278# time zones used in Postgres timestamptz output
279_timezones = dict(CET='+0100', EET='+0200', EST='-0500',
280    GMT='+0000', HST='-1000', MET='+0100', MST='-0700',
281    UCT='+0000', UTC='+0000', WET='+0000')
282
283
284def _timezone_as_offset(tz):
285    if tz.startswith(('+', '-')):
286        if len(tz) < 5:
287            return tz + '00'
288        return tz.replace(':', '')
289    return _timezones.get(tz, '+0000')
290
291
292def _get_timezone(tz):
293    tz = _timezone_as_offset(tz)
294    minutes = 60 * int(tz[1:3]) + int(tz[3:5])
295    if tz[0] == '-':
296        minutes = -minutes
297    return timezone(timedelta(minutes=minutes), tz)
298
299
300def _oid_key(table):
301    """Build oid key from a table name."""
302    return 'oid(%s)' % table
303
304
305class _SimpleTypes(dict):
306    """Dictionary mapping pg_type names to simple type names."""
307
308    _types = {'bool': 'bool',
309        'bytea': 'bytea',
310        'date': 'date interval time timetz timestamp timestamptz'
311            ' abstime reltime',  # these are very old
312        'float': 'float4 float8',
313        'int': 'cid int2 int4 int8 oid xid',
314        'hstore': 'hstore', 'json': 'json jsonb', 'uuid': 'uuid',
315        'num': 'numeric', 'money': 'money',
316        'text': 'bpchar char name text varchar'}
317
318    def __init__(self):
319        for typ, keys in self._types.items():
320            for key in keys.split():
321                self[key] = typ
322                self['_%s' % key] = '%s[]' % typ
323
324    # this could be a static method in Python > 2.6
325    def __missing__(self, key):
326        return 'text'
327
328_simpletypes = _SimpleTypes()
329
330
331def _quote_if_unqualified(param, name):
332    """Quote parameter representing a qualified name.
333
334    Puts a quote_ident() call around the give parameter unless
335    the name contains a dot, in which case the name is ambiguous
336    (could be a qualified name or just a name with a dot in it)
337    and must be quoted manually by the caller.
338    """
339    if isinstance(name, basestring) and '.' not in name:
340        return 'quote_ident(%s)' % (param,)
341    return param
342
343
344class _ParameterList(list):
345    """Helper class for building typed parameter lists."""
346
347    def add(self, value, typ=None):
348        """Typecast value with known database type and build parameter list.
349
350        If this is a literal value, it will be returned as is.  Otherwise, a
351        placeholder will be returned and the parameter list will be augmented.
352        """
353        value = self.adapt(value, typ)
354        if isinstance(value, Literal):
355            return value
356        self.append(value)
357        return '$%d' % len(self)
358
359
360class Bytea(bytes):
361    """Wrapper class for marking Bytea values."""
362
363
364class Hstore(dict):
365    """Wrapper class for marking hstore values."""
366
367    _re_quote = regex('^[Nn][Uu][Ll][Ll]$|[ ,=>]')
368
369    @classmethod
370    def _quote(cls, s):
371        if s is None:
372            return 'NULL'
373        if not s:
374            return '""'
375        s = s.replace('"', '\\"')
376        if cls._re_quote.search(s):
377            s = '"%s"' % s
378        return s
379
380    def __str__(self):
381        q = self._quote
382        return ','.join('%s=>%s' % (q(k), q(v)) for k, v in self.items())
383
384
385class Json:
386    """Wrapper class for marking Json values."""
387
388    def __init__(self, obj):
389        self.obj = obj
390
391
392class Literal(str):
393    """Wrapper class for marking literal SQL values."""
394
395
396class Adapter:
397    """Class providing methods for adapting parameters to the database."""
398
399    _bool_true_values = frozenset('t true 1 y yes on'.split())
400
401    _date_literals = frozenset('current_date current_time'
402        ' current_timestamp localtime localtimestamp'.split())
403
404    _re_array_quote = regex(r'[{},"\\\s]|^[Nn][Uu][Ll][Ll]$')
405    _re_record_quote = regex(r'[(,"\\]')
406    _re_array_escape = _re_record_escape = regex(r'(["\\])')
407
408    def __init__(self, db):
409        self.db = weakref.proxy(db)
410
411    @classmethod
412    def _adapt_bool(cls, v):
413        """Adapt a boolean parameter."""
414        if isinstance(v, basestring):
415            if not v:
416                return None
417            v = v.lower() in cls._bool_true_values
418        return 't' if v else 'f'
419
420    @classmethod
421    def _adapt_date(cls, v):
422        """Adapt a date parameter."""
423        if not v:
424            return None
425        if isinstance(v, basestring) and v.lower() in cls._date_literals:
426            return Literal(v)
427        return v
428
429    @staticmethod
430    def _adapt_num(v):
431        """Adapt a numeric parameter."""
432        if not v and v != 0:
433            return None
434        return v
435
436    _adapt_int = _adapt_float = _adapt_money = _adapt_num
437
438    def _adapt_bytea(self, v):
439        """Adapt a bytea parameter."""
440        return self.db.escape_bytea(v)
441
442    def _adapt_json(self, v):
443        """Adapt a json parameter."""
444        if not v:
445            return None
446        if isinstance(v, basestring):
447            return v
448        return self.db.encode_json(v)
449
450    @classmethod
451    def _adapt_text_array(cls, v):
452        """Adapt a text type array parameter."""
453        if isinstance(v, list):
454            adapt = cls._adapt_text_array
455            return '{%s}' % ','.join(adapt(v) for v in v)
456        if v is None:
457            return 'null'
458        if not v:
459            return '""'
460        v = str(v)
461        if cls._re_array_quote.search(v):
462            v = '"%s"' % cls._re_array_escape.sub(r'\\\1', v)
463        return v
464
465    _adapt_date_array = _adapt_text_array
466
467    @classmethod
468    def _adapt_bool_array(cls, v):
469        """Adapt a boolean array parameter."""
470        if isinstance(v, list):
471            adapt = cls._adapt_bool_array
472            return '{%s}' % ','.join(adapt(v) for v in v)
473        if v is None:
474            return 'null'
475        if isinstance(v, basestring):
476            if not v:
477                return 'null'
478            v = v.lower() in cls._bool_true_values
479        return 't' if v else 'f'
480
481    @classmethod
482    def _adapt_num_array(cls, v):
483        """Adapt a numeric array parameter."""
484        if isinstance(v, list):
485            adapt = cls._adapt_num_array
486            return '{%s}' % ','.join(adapt(v) for v in v)
487        if not v and v != 0:
488            return 'null'
489        return str(v)
490
491    _adapt_int_array = _adapt_float_array = _adapt_money_array = \
492            _adapt_num_array
493
494    def _adapt_bytea_array(self, v):
495        """Adapt a bytea array parameter."""
496        if isinstance(v, list):
497            return b'{' + b','.join(
498                self._adapt_bytea_array(v) for v in v) + b'}'
499        if v is None:
500            return b'null'
501        return self.db.escape_bytea(v).replace(b'\\', b'\\\\')
502
503    def _adapt_json_array(self, v):
504        """Adapt a json array parameter."""
505        if isinstance(v, list):
506            adapt = self._adapt_json_array
507            return '{%s}' % ','.join(adapt(v) for v in v)
508        if not v:
509            return 'null'
510        if not isinstance(v, basestring):
511            v = self.db.encode_json(v)
512        if self._re_array_quote.search(v):
513            v = '"%s"' % self._re_array_escape.sub(r'\\\1', v)
514        return v
515
516    def _adapt_record(self, v, typ):
517        """Adapt a record parameter with given type."""
518        typ = self.get_attnames(typ).values()
519        if len(typ) != len(v):
520            raise TypeError('Record parameter %s has wrong size' % v)
521        adapt = self.adapt
522        value = []
523        for v, t in zip(v, typ):
524            v = adapt(v, t)
525            if v is None:
526                v = ''
527            elif not v:
528                v = '""'
529            else:
530                if isinstance(v, bytes):
531                    if str is not bytes:
532                        v = v.decode('ascii')
533                else:
534                    v = str(v)
535                if self._re_record_quote.search(v):
536                    v = '"%s"' % self._re_record_escape.sub(r'\\\1', v)
537            value.append(v)
538        return '(%s)' % ','.join(value)
539
540    def adapt(self, value, typ=None):
541        """Adapt a value with known database type."""
542        if value is not None and not isinstance(value, Literal):
543            if typ:
544                simple = self.get_simple_name(typ)
545            else:
546                typ = simple = self.guess_simple_type(value) or 'text'
547            try:
548                value = value.__pg_str__(typ)
549            except AttributeError:
550                pass
551            if simple == 'text':
552                pass
553            elif simple == 'record':
554                if isinstance(value, tuple):
555                    value = self._adapt_record(value, typ)
556            elif simple.endswith('[]'):
557                if isinstance(value, list):
558                    adapt = getattr(self, '_adapt_%s_array' % simple[:-2])
559                    value = adapt(value)
560            else:
561                adapt = getattr(self, '_adapt_%s' % simple)
562                value = adapt(value)
563        return value
564
565    @staticmethod
566    def simple_type(name):
567        """Create a simple database type with given attribute names."""
568        typ = DbType(name)
569        typ.simple = name
570        return typ
571
572    @staticmethod
573    def get_simple_name(typ):
574        """Get the simple name of a database type."""
575        if isinstance(typ, DbType):
576            return typ.simple
577        return _simpletypes[typ]
578
579    @staticmethod
580    def get_attnames(typ):
581        """Get the attribute names of a composite database type."""
582        if isinstance(typ, DbType):
583            return typ.attnames
584        return {}
585
586    @classmethod
587    def guess_simple_type(cls, value):
588        """Try to guess which database type the given value has."""
589        if isinstance(value, Bytea):
590            return 'bytea'
591        if isinstance(value, basestring):
592            return 'text'
593        if isinstance(value, bool):
594            return 'bool'
595        if isinstance(value, (int, long)):
596            return 'int'
597        if isinstance(value, float):
598            return 'float'
599        if isinstance(value, Decimal):
600            return 'num'
601        if isinstance(value, (date, time, datetime, timedelta)):
602            return 'date'
603        if isinstance(value, list):
604            return '%s[]' % (cls.guess_simple_base_type(value) or 'text',)
605        if isinstance(value, tuple):
606            simple_type = cls.simple_type
607            typ = simple_type('record')
608            guess = cls.guess_simple_type
609            def get_attnames(self):
610                return AttrDict((str(n + 1), simple_type(guess(v)))
611                    for n, v in enumerate(value))
612            typ._get_attnames = get_attnames
613            return typ
614
615    @classmethod
616    def guess_simple_base_type(cls, value):
617        """Try to guess the base type of a given array."""
618        for v in value:
619            if isinstance(v, list):
620                typ = cls.guess_simple_base_type(v)
621            else:
622                typ = cls.guess_simple_type(v)
623            if typ:
624                return typ
625
626    def adapt_inline(self, value, nested=False):
627        """Adapt a value that is put into the SQL and needs to be quoted."""
628        if value is None:
629            return 'NULL'
630        if isinstance(value, Literal):
631            return value
632        if isinstance(value, Bytea):
633            value = self.db.escape_bytea(value)
634            if bytes is not str:  # Python >= 3.0
635                value = value.decode('ascii')
636        elif isinstance(value, Json):
637            if value.encode:
638                return value.encode()
639            value = self.db.encode_json(value)
640        elif isinstance(value, (datetime, date, time, timedelta)):
641            value = str(value)
642        if isinstance(value, basestring):
643            value = self.db.escape_string(value)
644            return "'%s'" % value
645        if isinstance(value, bool):
646            return 'true' if value else 'false'
647        if isinstance(value, float):
648            if isinf(value):
649                return "'-Infinity'" if value < 0 else "'Infinity'"
650            if isnan(value):
651                return "'NaN'"
652            return value
653        if isinstance(value, (int, long, Decimal)):
654            return value
655        if isinstance(value, list):
656            q = self.adapt_inline
657            s = '[%s]' if nested else 'ARRAY[%s]'
658            return s % ','.join(str(q(v, nested=True)) for v in value)
659        if isinstance(value, tuple):
660            q = self.adapt_inline
661            return '(%s)' % ','.join(str(q(v)) for v in value)
662        try:
663            value = value.__pg_repr__()
664        except AttributeError:
665            raise InterfaceError(
666                'Do not know how to adapt type %s' % type(value))
667        if isinstance(value, (tuple, list)):
668            value = self.adapt_inline(value)
669        return value
670
671    def parameter_list(self):
672        """Return a parameter list for parameters with known database types.
673
674        The list has an add(value, typ) method that will build up the
675        list and return either the literal value or a placeholder.
676        """
677        params = _ParameterList()
678        params.adapt = self.adapt
679        return params
680
681    def format_query(self, command, values=None, types=None, inline=False):
682        """Format a database query using the given values and types."""
683        if not values:
684            return command, []
685        if inline and types:
686            raise ValueError('Typed parameters must be sent separately')
687        params = self.parameter_list()
688        if isinstance(values, (list, tuple)):
689            if inline:
690                adapt = self.adapt_inline
691                literals = [adapt(value) for value in values]
692            else:
693                add = params.add
694                literals = []
695                append = literals.append
696                if types:
697                    if (not isinstance(types, (list, tuple)) or
698                            len(types) != len(values)):
699                        raise TypeError('The values and types do not match')
700                    for value, typ in zip(values, types):
701                        append(add(value, typ))
702                else:
703                    for value in values:
704                        append(add(value))
705            command %= tuple(literals)
706        elif isinstance(values, dict):
707            # we want to allow extra keys in the dictionary,
708            # so we first must find the values actually used in the command
709            used_values = {}
710            literals = dict.fromkeys(values, '')
711            for key in values:
712                del literals[key]
713                try:
714                    command % literals
715                except KeyError:
716                    used_values[key] = values[key]
717                literals[key] = ''
718            values = used_values
719            if inline:
720                adapt = self.adapt_inline
721                literals = dict((key, adapt(value))
722                    for key, value in values.items())
723            else:
724                add = params.add
725                literals = {}
726                if types:
727                    if not isinstance(types, dict):
728                        raise TypeError('The values and types do not match')
729                    for key in sorted(values):
730                        literals[key] = add(values[key], types.get(key))
731                else:
732                    for key in sorted(values):
733                        literals[key] = add(values[key])
734            command %= literals
735        else:
736            raise TypeError('The values must be passed as tuple, list or dict')
737        return command, params
738
739
740def cast_bool(value):
741    """Cast a boolean value."""
742    if not get_bool():
743        return value
744    return value[0] == 't'
745
746
747def cast_json(value):
748    """Cast a JSON value."""
749    cast = get_jsondecode()
750    if not cast:
751        return value
752    return cast(value)
753
754
755def cast_num(value):
756    """Cast a numeric value."""
757    return (get_decimal() or float)(value)
758
759
760def cast_money(value):
761    """Cast a money value."""
762    point = get_decimal_point()
763    if not point:
764        return value
765    if point != '.':
766        value = value.replace(point, '.')
767    value = value.replace('(', '-')
768    value = ''.join(c for c in value if c.isdigit() or c in '.-')
769    return (get_decimal() or float)(value)
770
771
772def cast_int2vector(value):
773    """Cast an int2vector value."""
774    return [int(v) for v in value.split()]
775
776
777def cast_date(value, connection):
778    """Cast a date value."""
779    # The output format depends on the server setting DateStyle.  The default
780    # setting ISO and the setting for German are actually unambiguous.  The
781    # order of days and months in the other two settings is however ambiguous,
782    # so at least here we need to consult the setting to properly parse values.
783    if value == '-infinity':
784        return date.min
785    if value == 'infinity':
786        return date.max
787    value = value.split()
788    if value[-1] == 'BC':
789        return date.min
790    value = value[0]
791    if len(value) > 10:
792        return date.max
793    fmt = connection.date_format()
794    return datetime.strptime(value, fmt).date()
795
796
797def cast_time(value):
798    """Cast a time value."""
799    fmt = '%H:%M:%S.%f' if len(value) > 8 else '%H:%M:%S'
800    return datetime.strptime(value, fmt).time()
801
802
803_re_timezone = regex('(.*)([+-].*)')
804
805
806def cast_timetz(value):
807    """Cast a timetz value."""
808    tz = _re_timezone.match(value)
809    if tz:
810        value, tz = tz.groups()
811    else:
812        tz = '+0000'
813    fmt = '%H:%M:%S.%f' if len(value) > 8 else '%H:%M:%S'
814    if _has_timezone:
815        value += _timezone_as_offset(tz)
816        fmt += '%z'
817        return datetime.strptime(value, fmt).timetz()
818    return datetime.strptime(value, fmt).timetz().replace(
819        tzinfo=_get_timezone(tz))
820
821
822def cast_timestamp(value, connection):
823    """Cast a timestamp value."""
824    if value == '-infinity':
825        return datetime.min
826    if value == 'infinity':
827        return datetime.max
828    value = value.split()
829    if value[-1] == 'BC':
830        return datetime.min
831    fmt = connection.date_format()
832    if fmt.endswith('-%Y') and len(value) > 2:
833        value = value[1:5]
834        if len(value[3]) > 4:
835            return datetime.max
836        fmt = ['%d %b' if fmt.startswith('%d') else '%b %d',
837            '%H:%M:%S.%f' if len(value[2]) > 8 else '%H:%M:%S', '%Y']
838    else:
839        if len(value[0]) > 10:
840            return datetime.max
841        fmt = [fmt, '%H:%M:%S.%f' if len(value[1]) > 8 else '%H:%M:%S']
842    return datetime.strptime(' '.join(value), ' '.join(fmt))
843
844
845def cast_timestamptz(value, connection):
846    """Cast a timestamptz value."""
847    if value == '-infinity':
848        return datetime.min
849    if value == 'infinity':
850        return datetime.max
851    value = value.split()
852    if value[-1] == 'BC':
853        return datetime.min
854    fmt = connection.date_format()
855    if fmt.endswith('-%Y') and len(value) > 2:
856        value = value[1:]
857        if len(value[3]) > 4:
858            return datetime.max
859        fmt = ['%d %b' if fmt.startswith('%d') else '%b %d',
860            '%H:%M:%S.%f' if len(value[2]) > 8 else '%H:%M:%S', '%Y']
861        value, tz = value[:-1], value[-1]
862    else:
863        if fmt.startswith('%Y-'):
864            tz = _re_timezone.match(value[1])
865            if tz:
866                value[1], tz = tz.groups()
867            else:
868                tz = '+0000'
869        else:
870            value, tz = value[:-1], value[-1]
871        if len(value[0]) > 10:
872            return datetime.max
873        fmt = [fmt, '%H:%M:%S.%f' if len(value[1]) > 8 else '%H:%M:%S']
874    if _has_timezone:
875        value.append(_timezone_as_offset(tz))
876        fmt.append('%z')
877        return datetime.strptime(' '.join(value), ' '.join(fmt))
878    return datetime.strptime(' '.join(value), ' '.join(fmt)).replace(
879        tzinfo=_get_timezone(tz))
880
881
882_re_interval_sql_standard = regex(
883    '(?:([+-])?([0-9]+)-([0-9]+) ?)?'
884    '(?:([+-]?[0-9]+)(?!:) ?)?'
885    '(?:([+-])?([0-9]+):([0-9]+):([0-9]+)(?:\\.([0-9]+))?)?')
886
887_re_interval_postgres = regex(
888    '(?:([+-]?[0-9]+) ?years? ?)?'
889    '(?:([+-]?[0-9]+) ?mons? ?)?'
890    '(?:([+-]?[0-9]+) ?days? ?)?'
891    '(?:([+-])?([0-9]+):([0-9]+):([0-9]+)(?:\\.([0-9]+))?)?')
892
893_re_interval_postgres_verbose = regex(
894    '@ ?(?:([+-]?[0-9]+) ?years? ?)?'
895    '(?:([+-]?[0-9]+) ?mons? ?)?'
896    '(?:([+-]?[0-9]+) ?days? ?)?'
897    '(?:([+-]?[0-9]+) ?hours? ?)?'
898    '(?:([+-]?[0-9]+) ?mins? ?)?'
899    '(?:([+-])?([0-9]+)(?:\\.([0-9]+))? ?secs?)? ?(ago)?')
900
901_re_interval_iso_8601 = regex(
902    'P(?:([+-]?[0-9]+)Y)?'
903    '(?:([+-]?[0-9]+)M)?'
904    '(?:([+-]?[0-9]+)D)?'
905    '(?:T(?:([+-]?[0-9]+)H)?'
906    '(?:([+-]?[0-9]+)M)?'
907    '(?:([+-])?([0-9]+)(?:\\.([0-9]+))?S)?)?')
908
909
910def cast_interval(value):
911    """Cast an interval value."""
912    # The output format depends on the server setting IntervalStyle, but it's
913    # not necessary to consult this setting to parse it.  It's faster to just
914    # check all possible formats, and there is no ambiguity here.
915    m = _re_interval_iso_8601.match(value)
916    if m:
917        m = [d or '0' for d in m.groups()]
918        secs_ago = m.pop(5) == '-'
919        m = [int(d) for d in m]
920        years, mons, days, hours, mins, secs, usecs = m
921        if secs_ago:
922            secs = -secs
923            usecs = -usecs
924    else:
925        m = _re_interval_postgres_verbose.match(value)
926        if m:
927            m, ago = [d or '0' for d in m.groups()[:8]], m.group(9)
928            secs_ago = m.pop(5) == '-'
929            m = [-int(d) for d in m] if ago else [int(d) for d in m]
930            years, mons, days, hours, mins, secs, usecs = m
931            if secs_ago:
932                secs = - secs
933                usecs = -usecs
934        else:
935            m = _re_interval_postgres.match(value)
936            if m and any(m.groups()):
937                m = [d or '0' for d in m.groups()]
938                hours_ago = m.pop(3) == '-'
939                m = [int(d) for d in m]
940                years, mons, days, hours, mins, secs, usecs = m
941                if hours_ago:
942                    hours = -hours
943                    mins = -mins
944                    secs = -secs
945                    usecs = -usecs
946            else:
947                m = _re_interval_sql_standard.match(value)
948                if m and any(m.groups()):
949                    m = [d or '0' for d in m.groups()]
950                    years_ago = m.pop(0) == '-'
951                    hours_ago = m.pop(3) == '-'
952                    m = [int(d) for d in m]
953                    years, mons, days, hours, mins, secs, usecs = m
954                    if years_ago:
955                        years = -years
956                        mons = -mons
957                    if hours_ago:
958                        hours = -hours
959                        mins = -mins
960                        secs = -secs
961                        usecs = -usecs
962                else:
963                    raise ValueError('Cannot parse interval: %s' % value)
964    days += 365 * years + 30 * mons
965    return timedelta(days=days, hours=hours, minutes=mins,
966        seconds=secs, microseconds=usecs)
967
968
969class Typecasts(dict):
970    """Dictionary mapping database types to typecast functions.
971
972    The cast functions get passed the string representation of a value in
973    the database which they need to convert to a Python object.  The
974    passed string will never be None since NULL values are already
975    handled before the cast function is called.
976
977    Note that the basic types are already handled by the C extension.
978    They only need to be handled here as record or array components.
979    """
980
981    # the default cast functions
982    # (str functions are ignored but have been added for faster access)
983    defaults = {'char': str, 'bpchar': str, 'name': str,
984        'text': str, 'varchar': str,
985        'bool': cast_bool, 'bytea': unescape_bytea,
986        'int2': int, 'int4': int, 'serial': int, 'int8': long, 'oid': int,
987        'hstore': cast_hstore, 'json': cast_json, 'jsonb': cast_json,
988        'float4': float, 'float8': float,
989        'numeric': cast_num, 'money': cast_money,
990        'date': cast_date, 'interval': cast_interval,
991        'time': cast_time, 'timetz': cast_timetz,
992        'timestamp': cast_timestamp, 'timestamptz': cast_timestamptz,
993        'int2vector': cast_int2vector, 'uuid': UUID,
994        'anyarray': cast_array, 'record': cast_record}
995
996    connection = None  # will be set in a connection specific instance
997
998    def __missing__(self, typ):
999        """Create a cast function if it is not cached.
1000
1001        Note that this class never raises a KeyError,
1002        but returns None when no special cast function exists.
1003        """
1004        if not isinstance(typ, str):
1005            raise TypeError('Invalid type: %s' % typ)
1006        cast = self.defaults.get(typ)
1007        if cast:
1008            # store default for faster access
1009            cast = self._add_connection(cast)
1010            self[typ] = cast
1011        elif typ.startswith('_'):
1012            base_cast = self[typ[1:]]
1013            cast = self.create_array_cast(base_cast)
1014            if base_cast:
1015                self[typ] = cast
1016        else:
1017            attnames = self.get_attnames(typ)
1018            if attnames:
1019                casts = [self[v.pgtype] for v in attnames.values()]
1020                cast = self.create_record_cast(typ, attnames, casts)
1021                self[typ] = cast
1022        return cast
1023
1024    @staticmethod
1025    def _needs_connection(func):
1026        """Check if a typecast function needs a connection argument."""
1027        try:
1028            args = get_args(func)
1029        except (TypeError, ValueError):
1030            return False
1031        else:
1032            return 'connection' in args[1:]
1033
1034    def _add_connection(self, cast):
1035        """Add a connection argument to the typecast function if necessary."""
1036        if not self.connection or not self._needs_connection(cast):
1037            return cast
1038        return partial(cast, connection=self.connection)
1039
1040    def get(self, typ, default=None):
1041        """Get the typecast function for the given database type."""
1042        return self[typ] or default
1043
1044    def set(self, typ, cast):
1045        """Set a typecast function for the specified database type(s)."""
1046        if isinstance(typ, basestring):
1047            typ = [typ]
1048        if cast is None:
1049            for t in typ:
1050                self.pop(t, None)
1051                self.pop('_%s' % t, None)
1052        else:
1053            if not callable(cast):
1054                raise TypeError("Cast parameter must be callable")
1055            for t in typ:
1056                self[t] = self._add_connection(cast)
1057                self.pop('_%s' % t, None)
1058
1059    def reset(self, typ=None):
1060        """Reset the typecasts for the specified type(s) to their defaults.
1061
1062        When no type is specified, all typecasts will be reset.
1063        """
1064        if typ is None:
1065            self.clear()
1066        else:
1067            if isinstance(typ, basestring):
1068                typ = [typ]
1069            for t in typ:
1070                self.pop(t, None)
1071
1072    @classmethod
1073    def get_default(cls, typ):
1074        """Get the default typecast function for the given database type."""
1075        return cls.defaults.get(typ)
1076
1077    @classmethod
1078    def set_default(cls, typ, cast):
1079        """Set a default typecast function for the given database type(s)."""
1080        if isinstance(typ, basestring):
1081            typ = [typ]
1082        defaults = cls.defaults
1083        if cast is None:
1084            for t in typ:
1085                defaults.pop(t, None)
1086                defaults.pop('_%s' % t, None)
1087        else:
1088            if not callable(cast):
1089                raise TypeError("Cast parameter must be callable")
1090            for t in typ:
1091                defaults[t] = cast
1092                defaults.pop('_%s' % t, None)
1093
1094    def get_attnames(self, typ):
1095        """Return the fields for the given record type.
1096
1097        This method will be replaced with the get_attnames() method of DbTypes.
1098        """
1099        return {}
1100
1101    def dateformat(self):
1102        """Return the current date format.
1103
1104        This method will be replaced with the dateformat() method of DbTypes.
1105        """
1106        return '%Y-%m-%d'
1107
1108    def create_array_cast(self, basecast):
1109        """Create an array typecast for the given base cast."""
1110        cast_array = self['anyarray']
1111        def cast(v):
1112            return cast_array(v, basecast)
1113        return cast
1114
1115    def create_record_cast(self, name, fields, casts):
1116        """Create a named record typecast for the given fields and casts."""
1117        cast_record = self['record']
1118        record = namedtuple(name, fields)
1119        def cast(v):
1120            return record(*cast_record(v, casts))
1121        return cast
1122
1123
1124def get_typecast(typ):
1125    """Get the global typecast function for the given database type(s)."""
1126    return Typecasts.get_default(typ)
1127
1128
1129def set_typecast(typ, cast):
1130    """Set a global typecast function for the given database type(s).
1131
1132    Note that connections cache cast functions. To be sure a global change
1133    is picked up by a running connection, call db.db_types.reset_typecast().
1134    """
1135    Typecasts.set_default(typ, cast)
1136
1137
1138class DbType(str):
1139    """Class augmenting the simple type name with additional info.
1140
1141    The following additional information is provided:
1142
1143        oid: the PostgreSQL type OID
1144        pgtype: the PostgreSQL type name
1145        regtype: the regular type name
1146        simple: the simple PyGreSQL type name
1147        typtype: b = base type, c = composite type etc.
1148        category: A = Array, b = Boolean, C = Composite etc.
1149        delim: delimiter for array types
1150        relid: corresponding table for composite types
1151        attnames: attributes for composite types
1152    """
1153
1154    @property
1155    def attnames(self):
1156        """Get names and types of the fields of a composite type."""
1157        return self._get_attnames(self)
1158
1159
1160class DbTypes(dict):
1161    """Cache for PostgreSQL data types.
1162
1163    This cache maps type OIDs and names to DbType objects containing
1164    information on the associated database type.
1165    """
1166
1167    _num_types = frozenset('int float num money'
1168        ' int2 int4 int8 float4 float8 numeric money'.split())
1169
1170    def __init__(self, db):
1171        """Initialize type cache for connection."""
1172        super(DbTypes, self).__init__()
1173        self._db = weakref.proxy(db)
1174        self._regtypes = False
1175        self._typecasts = Typecasts()
1176        self._typecasts.get_attnames = self.get_attnames
1177        self._typecasts.connection = self._db
1178        if db.server_version < 80400:
1179            # older remote databases (not officially supported)
1180            self._query_pg_type = (
1181                "SELECT oid, typname, typname::text::regtype,"
1182                " typtype, null as typcategory, typdelim, typrelid"
1183                " FROM pg_type WHERE oid=%s::regtype")
1184        else:
1185            self._query_pg_type = (
1186                "SELECT oid, typname, typname::regtype,"
1187                " typtype, typcategory, typdelim, typrelid"
1188                " FROM pg_type WHERE oid=%s::regtype")
1189
1190    def add(self, oid, pgtype, regtype,
1191               typtype, category, delim, relid):
1192        """Create a PostgreSQL type name with additional info."""
1193        if oid in self:
1194            return self[oid]
1195        simple = 'record' if relid else _simpletypes[pgtype]
1196        typ = DbType(regtype if self._regtypes else simple)
1197        typ.oid = oid
1198        typ.simple = simple
1199        typ.pgtype = pgtype
1200        typ.regtype = regtype
1201        typ.typtype = typtype
1202        typ.category = category
1203        typ.delim = delim
1204        typ.relid = relid
1205        typ._get_attnames = self.get_attnames
1206        return typ
1207
1208    def __missing__(self, key):
1209        """Get the type info from the database if it is not cached."""
1210        try:
1211            q = self._query_pg_type % (_quote_if_unqualified('$1', key),)
1212            res = self._db.query(q, (key,)).getresult()
1213        except ProgrammingError:
1214            res = None
1215        if not res:
1216            raise KeyError('Type %s could not be found' % key)
1217        res = res[0]
1218        typ = self.add(*res)
1219        self[typ.oid] = self[typ.pgtype] = typ
1220        return typ
1221
1222    def get(self, key, default=None):
1223        """Get the type even if it is not cached."""
1224        try:
1225            return self[key]
1226        except KeyError:
1227            return default
1228
1229    def get_attnames(self, typ):
1230        """Get names and types of the fields of a composite type."""
1231        if not isinstance(typ, DbType):
1232            typ = self.get(typ)
1233            if not typ:
1234                return None
1235        if not typ.relid:
1236            return None
1237        return self._db.get_attnames(typ.relid, with_oid=False)
1238
1239    def get_typecast(self, typ):
1240        """Get the typecast function for the given database type."""
1241        return self._typecasts.get(typ)
1242
1243    def set_typecast(self, typ, cast):
1244        """Set a typecast function for the specified database type(s)."""
1245        self._typecasts.set(typ, cast)
1246
1247    def reset_typecast(self, typ=None):
1248        """Reset the typecast function for the specified database type(s)."""
1249        self._typecasts.reset(typ)
1250
1251    def typecast(self, value, typ):
1252        """Cast the given value according to the given database type."""
1253        if value is None:
1254            # for NULL values, no typecast is necessary
1255            return None
1256        if not isinstance(typ, DbType):
1257            typ = self.get(typ)
1258            if typ:
1259                typ = typ.pgtype
1260        cast = self.get_typecast(typ) if typ else None
1261        if not cast or cast is str:
1262            # no typecast is necessary
1263            return value
1264        return cast(value)
1265
1266
1267_re_fieldname = regex('^[A-Za-z][_a-zA-Z0-9]*$')
1268
1269# The result rows for database operations are returned as named tuples
1270# by default. Since creating namedtuple classes is a somewhat expensive
1271# operation, we cache up to 1024 of these classes by default.
1272
1273@lru_cache(maxsize=1024)
1274def _row_factory(names):
1275    """Get a namedtuple factory for row results with the given names."""
1276    try:
1277        try:
1278            return namedtuple('Row', names, rename=True)._make
1279        except TypeError:  # Python 2.6 and 3.0 do not support rename
1280            names = [v if _re_fieldname.match(v) and not iskeyword(v)
1281                        else 'column_%d' % (n,)
1282                     for n, v in enumerate(names)]
1283            return namedtuple('Row', names)._make
1284    except ValueError:  # there is still a problem with the field names
1285        names = ['column_%d' % (n,) for n in range(len(names))]
1286        return namedtuple('Row', names)._make
1287
1288
1289def set_row_factory_size(maxsize):
1290    """Change the size of the namedtuple factory cache.
1291
1292    If maxsize is set to None, the cache can grow without bound.
1293    """
1294    global _row_factory
1295    _row_factory = lru_cache(maxsize)(_row_factory.__wrapped__)
1296
1297
1298def _namedresult(q):
1299    """Get query result as named tuples."""
1300    row = _row_factory(q.listfields())
1301    return [row(r) for r in q.getresult()]
1302
1303
1304class _MemoryQuery:
1305    """Class that embodies a given query result."""
1306
1307    def __init__(self, result, fields):
1308        """Create query from given result rows and field names."""
1309        self.result = result
1310        self.fields = tuple(fields)
1311
1312    def listfields(self):
1313        """Return the stored field names of this query."""
1314        return self.fields
1315
1316    def getresult(self):
1317        """Return the stored result of this query."""
1318        return self.result
1319
1320
1321def _db_error(msg, cls=DatabaseError):
1322    """Return DatabaseError with empty sqlstate attribute."""
1323    error = cls(msg)
1324    error.sqlstate = None
1325    return error
1326
1327
1328def _int_error(msg):
1329    """Return InternalError."""
1330    return _db_error(msg, InternalError)
1331
1332
1333def _prg_error(msg):
1334    """Return ProgrammingError."""
1335    return _db_error(msg, ProgrammingError)
1336
1337
1338# Initialize the C module
1339
1340set_namedresult(_namedresult)
1341set_decimal(Decimal)
1342set_jsondecode(jsondecode)
1343
1344
1345# The notification handler
1346
1347class NotificationHandler(object):
1348    """A PostgreSQL client-side asynchronous notification handler."""
1349
1350    def __init__(self, db, event, callback=None,
1351            arg_dict=None, timeout=None, stop_event=None):
1352        """Initialize the notification handler.
1353
1354        You must pass a PyGreSQL database connection, the name of an
1355        event (notification channel) to listen for and a callback function.
1356
1357        You can also specify a dictionary arg_dict that will be passed as
1358        the single argument to the callback function, and a timeout value
1359        in seconds (a floating point number denotes fractions of seconds).
1360        If it is absent or None, the callers will never time out.  If the
1361        timeout is reached, the callback function will be called with a
1362        single argument that is None.  If you set the timeout to zero,
1363        the handler will poll notifications synchronously and return.
1364
1365        You can specify the name of the event that will be used to signal
1366        the handler to stop listening as stop_event. By default, it will
1367        be the event name prefixed with 'stop_'.
1368        """
1369        self.db = db
1370        self.event = event
1371        self.stop_event = stop_event or 'stop_%s' % event
1372        self.listening = False
1373        self.callback = callback
1374        if arg_dict is None:
1375            arg_dict = {}
1376        self.arg_dict = arg_dict
1377        self.timeout = timeout
1378
1379    def __del__(self):
1380        self.unlisten()
1381
1382    def close(self):
1383        """Stop listening and close the connection."""
1384        if self.db:
1385            self.unlisten()
1386            self.db.close()
1387            self.db = None
1388
1389    def listen(self):
1390        """Start listening for the event and the stop event."""
1391        if not self.listening:
1392            self.db.query('listen "%s"' % self.event)
1393            self.db.query('listen "%s"' % self.stop_event)
1394            self.listening = True
1395
1396    def unlisten(self):
1397        """Stop listening for the event and the stop event."""
1398        if self.listening:
1399            self.db.query('unlisten "%s"' % self.event)
1400            self.db.query('unlisten "%s"' % self.stop_event)
1401            self.listening = False
1402
1403    def notify(self, db=None, stop=False, payload=None):
1404        """Generate a notification.
1405
1406        Optionally, you can pass a payload with the notification.
1407
1408        If you set the stop flag, a stop notification will be sent that
1409        will cause the handler to stop listening.
1410
1411        Note: If the notification handler is running in another thread, you
1412        must pass a different database connection since PyGreSQL database
1413        connections are not thread-safe.
1414        """
1415        if self.listening:
1416            if not db:
1417                db = self.db
1418            q = 'notify "%s"' % (self.stop_event if stop else self.event)
1419            if payload:
1420                q += ", '%s'" % payload
1421            return db.query(q)
1422
1423    def __call__(self):
1424        """Invoke the notification handler.
1425
1426        The handler is a loop that listens for notifications on the event
1427        and stop event channels.  When either of these notifications are
1428        received, its associated 'pid', 'event' and 'extra' (the payload
1429        passed with the notification) are inserted into its arg_dict
1430        dictionary and the callback is invoked with this dictionary as
1431        a single argument.  When the handler receives a stop event, it
1432        stops listening to both events and return.
1433
1434        In the special case that the timeout of the handler has been set
1435        to zero, the handler will poll all events synchronously and return.
1436        If will keep listening until it receives a stop event.
1437
1438        Note: If you run this loop in another thread, don't use the same
1439        database connection for database operations in the main thread.
1440        """
1441        self.listen()
1442        poll = self.timeout == 0
1443        if not poll:
1444            rlist = [self.db.fileno()]
1445        while self.listening:
1446            if poll or select.select(rlist, [], [], self.timeout)[0]:
1447                while self.listening:
1448                    notice = self.db.getnotify()
1449                    if not notice:  # no more messages
1450                        break
1451                    event, pid, extra = notice
1452                    if event not in (self.event, self.stop_event):
1453                        self.unlisten()
1454                        raise _db_error(
1455                            'Listening for "%s" and "%s", but notified of "%s"'
1456                            % (self.event, self.stop_event, event))
1457                    if event == self.stop_event:
1458                        self.unlisten()
1459                    self.arg_dict.update(pid=pid, event=event, extra=extra)
1460                    self.callback(self.arg_dict)
1461                if poll:
1462                    break
1463            else:   # we timed out
1464                self.unlisten()
1465                self.callback(None)
1466
1467
1468def pgnotify(*args, **kw):
1469    """Same as NotificationHandler, under the traditional name."""
1470    warnings.warn("pgnotify is deprecated, use NotificationHandler instead",
1471        DeprecationWarning, stacklevel=2)
1472    return NotificationHandler(*args, **kw)
1473
1474
1475# The actual PostGreSQL database connection interface:
1476
1477class DB:
1478    """Wrapper class for the _pg connection type."""
1479
1480    db = None  # invalid fallback for underlying connection
1481
1482    def __init__(self, *args, **kw):
1483        """Create a new connection
1484
1485        You can pass either the connection parameters or an existing
1486        _pg or pgdb connection. This allows you to use the methods
1487        of the classic pg interface with a DB-API 2 pgdb connection.
1488        """
1489        if not args and len(kw) == 1:
1490            db = kw.get('db')
1491        elif not kw and len(args) == 1:
1492            db = args[0]
1493        else:
1494            db = None
1495        if db:
1496            if isinstance(db, DB):
1497                db = db.db
1498            else:
1499                try:
1500                    db = db._cnx
1501                except AttributeError:
1502                    pass
1503        if not db or not hasattr(db, 'db') or not hasattr(db, 'query'):
1504            db = connect(*args, **kw)
1505            self._closeable = True
1506        else:
1507            self._closeable = False
1508        self.db = db
1509        self.dbname = db.db
1510        self._regtypes = False
1511        self._attnames = {}
1512        self._pkeys = {}
1513        self._privileges = {}
1514        self._args = args, kw
1515        self.adapter = Adapter(self)
1516        self.dbtypes = DbTypes(self)
1517        if db.server_version < 80400:
1518            # support older remote data bases
1519            self._query_attnames = (
1520                "SELECT a.attname, t.oid, t.typname, t.typname::text::regtype,"
1521                " t.typtype, null as typcategory, t.typdelim, t.typrelid"
1522                " FROM pg_attribute a"
1523                " JOIN pg_type t ON t.oid = a.atttypid"
1524                " WHERE a.attrelid = %s::regclass AND %s"
1525                " AND NOT a.attisdropped ORDER BY a.attnum")
1526        else:
1527            self._query_attnames = (
1528                "SELECT a.attname, t.oid, t.typname, t.typname::regtype,"
1529                " t.typtype, t.typcategory, t.typdelim, t.typrelid"
1530                " FROM pg_attribute a"
1531                " JOIN pg_type t ON t.oid = a.atttypid"
1532                " WHERE a.attrelid = %s::regclass AND %s"
1533                " AND NOT a.attisdropped ORDER BY a.attnum")
1534        db.set_cast_hook(self.dbtypes.typecast)
1535        self.debug = None  # For debugging scripts, this can be set
1536            # * to a string format specification (e.g. in CGI set to "%s<BR>"),
1537            # * to a file object to write debug statements or
1538            # * to a callable object which takes a string argument
1539            # * to any other true value to just print debug statements
1540
1541    def __getattr__(self, name):
1542        # All undefined members are same as in underlying connection:
1543        if self.db:
1544            return getattr(self.db, name)
1545        else:
1546            raise _int_error('Connection is not valid')
1547
1548    def __dir__(self):
1549        # Custom dir function including the attributes of the connection:
1550        attrs = set(self.__class__.__dict__)
1551        attrs.update(self.__dict__)
1552        attrs.update(dir(self.db))
1553        return sorted(attrs)
1554
1555    # Context manager methods
1556
1557    def __enter__(self):
1558        """Enter the runtime context. This will start a transaction."""
1559        self.begin()
1560        return self
1561
1562    def __exit__(self, et, ev, tb):
1563        """Exit the runtime context. This will end the transaction."""
1564        if et is None and ev is None and tb is None:
1565            self.commit()
1566        else:
1567            self.rollback()
1568
1569    def __del__(self):
1570        try:
1571            db = self.db
1572        except AttributeError:
1573            db = None
1574        if db:
1575            db.set_cast_hook(None)
1576            if self._closeable:
1577                db.close()
1578
1579    # Auxiliary methods
1580
1581    def _do_debug(self, *args):
1582        """Print a debug message"""
1583        if self.debug:
1584            s = '\n'.join(str(arg) for arg in args)
1585            if isinstance(self.debug, basestring):
1586                print(self.debug % s)
1587            elif hasattr(self.debug, 'write'):
1588                self.debug.write(s + '\n')
1589            elif callable(self.debug):
1590                self.debug(s)
1591            else:
1592                print(s)
1593
1594    def _escape_qualified_name(self, s):
1595        """Escape a qualified name.
1596
1597        Escapes the name for use as an SQL identifier, unless the
1598        name contains a dot, in which case the name is ambiguous
1599        (could be a qualified name or just a name with a dot in it)
1600        and must be quoted manually by the caller.
1601        """
1602        if '.' not in s:
1603            s = self.escape_identifier(s)
1604        return s
1605
1606    @staticmethod
1607    def _make_bool(d):
1608        """Get boolean value corresponding to d."""
1609        return bool(d) if get_bool() else ('t' if d else 'f')
1610
1611    def _list_params(self, params):
1612        """Create a human readable parameter list."""
1613        return ', '.join('$%d=%r' % (n, v) for n, v in enumerate(params, 1))
1614
1615    # Public methods
1616
1617    # escape_string and escape_bytea exist as methods,
1618    # so we define unescape_bytea as a method as well
1619    unescape_bytea = staticmethod(unescape_bytea)
1620
1621    def decode_json(self, s):
1622        """Decode a JSON string coming from the database."""
1623        return (get_jsondecode() or jsondecode)(s)
1624
1625    def encode_json(self, d):
1626        """Encode a JSON string for use within SQL."""
1627        return jsonencode(d)
1628
1629    def close(self):
1630        """Close the database connection."""
1631        # Wraps shared library function so we can track state.
1632        if self._closeable:
1633            if self.db:
1634                self.db.set_cast_hook(None)
1635                self.db.close()
1636                self.db = None
1637            else:
1638                raise _int_error('Connection already closed')
1639
1640    def reset(self):
1641        """Reset connection with current parameters.
1642
1643        All derived queries and large objects derived from this connection
1644        will not be usable after this call.
1645
1646        """
1647        if self.db:
1648            self.db.reset()
1649        else:
1650            raise _int_error('Connection already closed')
1651
1652    def reopen(self):
1653        """Reopen connection to the database.
1654
1655        Used in case we need another connection to the same database.
1656        Note that we can still reopen a database that we have closed.
1657
1658        """
1659        # There is no such shared library function.
1660        if self._closeable:
1661            db = connect(*self._args[0], **self._args[1])
1662            if self.db:
1663                self.db.set_cast_hook(None)
1664                self.db.close()
1665            self.db = db
1666
1667    def begin(self, mode=None):
1668        """Begin a transaction."""
1669        qstr = 'BEGIN'
1670        if mode:
1671            qstr += ' ' + mode
1672        return self.query(qstr)
1673
1674    start = begin
1675
1676    def commit(self):
1677        """Commit the current transaction."""
1678        return self.query('COMMIT')
1679
1680    end = commit
1681
1682    def rollback(self, name=None):
1683        """Roll back the current transaction."""
1684        qstr = 'ROLLBACK'
1685        if name:
1686            qstr += ' TO ' + name
1687        return self.query(qstr)
1688
1689    abort = rollback
1690
1691    def savepoint(self, name):
1692        """Define a new savepoint within the current transaction."""
1693        return self.query('SAVEPOINT ' + name)
1694
1695    def release(self, name):
1696        """Destroy a previously defined savepoint."""
1697        return self.query('RELEASE ' + name)
1698
1699    def get_parameter(self, parameter):
1700        """Get the value of a run-time parameter.
1701
1702        If the parameter is a string, the return value will also be a string
1703        that is the current setting of the run-time parameter with that name.
1704
1705        You can get several parameters at once by passing a list, set or dict.
1706        When passing a list of parameter names, the return value will be a
1707        corresponding list of parameter settings.  When passing a set of
1708        parameter names, a new dict will be returned, mapping these parameter
1709        names to their settings.  Finally, if you pass a dict as parameter,
1710        its values will be set to the current parameter settings corresponding
1711        to its keys.
1712
1713        By passing the special name 'all' as the parameter, you can get a dict
1714        of all existing configuration parameters.
1715        """
1716        if isinstance(parameter, basestring):
1717            parameter = [parameter]
1718            values = None
1719        elif isinstance(parameter, (list, tuple)):
1720            values = []
1721        elif isinstance(parameter, (set, frozenset)):
1722            values = {}
1723        elif isinstance(parameter, dict):
1724            values = parameter
1725        else:
1726            raise TypeError(
1727                'The parameter must be a string, list, set or dict')
1728        if not parameter:
1729            raise TypeError('No parameter has been specified')
1730        params = {} if isinstance(values, dict) else []
1731        for key in parameter:
1732            param = key.strip().lower() if isinstance(
1733                key, basestring) else None
1734            if not param:
1735                raise TypeError('Invalid parameter')
1736            if param == 'all':
1737                q = 'SHOW ALL'
1738                values = self.db.query(q).getresult()
1739                values = dict(value[:2] for value in values)
1740                break
1741            if isinstance(values, dict):
1742                params[param] = key
1743            else:
1744                params.append(param)
1745        else:
1746            for param in params:
1747                q = 'SHOW %s' % (param,)
1748                value = self.db.query(q).getresult()[0][0]
1749                if values is None:
1750                    values = value
1751                elif isinstance(values, list):
1752                    values.append(value)
1753                else:
1754                    values[params[param]] = value
1755        return values
1756
1757    def set_parameter(self, parameter, value=None, local=False):
1758        """Set the value of a run-time parameter.
1759
1760        If the parameter and the value are strings, the run-time parameter
1761        will be set to that value.  If no value or None is passed as a value,
1762        then the run-time parameter will be restored to its default value.
1763
1764        You can set several parameters at once by passing a list of parameter
1765        names, together with a single value that all parameters should be
1766        set to or with a corresponding list of values.  You can also pass
1767        the parameters as a set if you only provide a single value.
1768        Finally, you can pass a dict with parameter names as keys.  In this
1769        case, you should not pass a value, since the values for the parameters
1770        will be taken from the dict.
1771
1772        By passing the special name 'all' as the parameter, you can reset
1773        all existing settable run-time parameters to their default values.
1774
1775        If you set local to True, then the command takes effect for only the
1776        current transaction.  After commit() or rollback(), the session-level
1777        setting takes effect again.  Setting local to True will appear to
1778        have no effect if it is executed outside a transaction, since the
1779        transaction will end immediately.
1780        """
1781        if isinstance(parameter, basestring):
1782            parameter = {parameter: value}
1783        elif isinstance(parameter, (list, tuple)):
1784            if isinstance(value, (list, tuple)):
1785                parameter = dict(zip(parameter, value))
1786            else:
1787                parameter = dict.fromkeys(parameter, value)
1788        elif isinstance(parameter, (set, frozenset)):
1789            if isinstance(value, (list, tuple, set, frozenset)):
1790                value = set(value)
1791                if len(value) == 1:
1792                    value = value.pop()
1793            if not(value is None or isinstance(value, basestring)):
1794                raise ValueError('A single value must be specified'
1795                    ' when parameter is a set')
1796            parameter = dict.fromkeys(parameter, value)
1797        elif isinstance(parameter, dict):
1798            if value is not None:
1799                raise ValueError('A value must not be specified'
1800                    ' when parameter is a dictionary')
1801        else:
1802            raise TypeError(
1803                'The parameter must be a string, list, set or dict')
1804        if not parameter:
1805            raise TypeError('No parameter has been specified')
1806        params = {}
1807        for key, value in parameter.items():
1808            param = key.strip().lower() if isinstance(
1809                key, basestring) else None
1810            if not param:
1811                raise TypeError('Invalid parameter')
1812            if param == 'all':
1813                if value is not None:
1814                    raise ValueError('A value must ot be specified'
1815                        " when parameter is 'all'")
1816                params = {'all': None}
1817                break
1818            params[param] = value
1819        local = ' LOCAL' if local else ''
1820        for param, value in params.items():
1821            if value is None:
1822                q = 'RESET%s %s' % (local, param)
1823            else:
1824                q = 'SET%s %s TO %s' % (local, param, value)
1825            self._do_debug(q)
1826            self.db.query(q)
1827
1828    def query(self, command, *args):
1829        """Execute a SQL command string.
1830
1831        This method simply sends a SQL query to the database.  If the query is
1832        an insert statement that inserted exactly one row into a table that
1833        has OIDs, the return value is the OID of the newly inserted row.
1834        If the query is an update or delete statement, or an insert statement
1835        that did not insert exactly one row in a table with OIDs, then the
1836        number of rows affected is returned as a string.  If it is a statement
1837        that returns rows as a result (usually a select statement, but maybe
1838        also an "insert/update ... returning" statement), this method returns
1839        a Query object that can be accessed via getresult() or dictresult()
1840        or simply printed.  Otherwise, it returns `None`.
1841
1842        The query can contain numbered parameters of the form $1 in place
1843        of any data constant.  Arguments given after the query string will
1844        be substituted for the corresponding numbered parameter.  Parameter
1845        values can also be given as a single list or tuple argument.
1846        """
1847        # Wraps shared library function for debugging.
1848        if not self.db:
1849            raise _int_error('Connection is not valid')
1850        if args:
1851            self._do_debug(command, args)
1852            return self.db.query(command, args)
1853        self._do_debug(command)
1854        return self.db.query(command)
1855
1856    def query_formatted(self, command,
1857            parameters=None, types=None, inline=False):
1858        """Execute a formatted SQL command string.
1859
1860        Similar to query, but using Python format placeholders of the form
1861        %s or %(names)s instead of PostgreSQL placeholders of the form $1.
1862        The parameters must be passed as a tuple, list or dict.  You can
1863        also pass a corresponding tuple, list or dict of database types in
1864        order to format the parameters properly in case there is ambiguity.
1865
1866        If you set inline to True, the parameters will be sent to the database
1867        embedded in the SQL command, otherwise they will be sent separately.
1868        """
1869        return self.query(*self.adapter.format_query(
1870            command, parameters, types, inline))
1871
1872    def query_prepared(self, *args, **kwargs):
1873        """Execute a prepared SQL statement.
1874
1875        This works like the query() method, except that instead of passing
1876        the SQL command, you pass the name of a prepared statement via
1877        the keyword-only argument `name`.  If you don't pass a name, the
1878        unnamed statement will be executed, if you created one before.
1879        """
1880        if not self.db:
1881            raise _int_error('Connection is not valid')
1882        # use kwargs because Python 2 does not support keyword-only arguments
1883        name = kwargs.get('name')
1884        if name is None:
1885            name = ''
1886        if args:
1887            self._do_debug('EXECUTE', name, args)
1888            return self.db.query_prepared(name, args)
1889        self._do_debug('EXECUTE', name)
1890        return self.db.query_prepared(name)
1891
1892    def prepare(self, command, name=None):
1893        """Create a prepared SQL statement.
1894
1895        This creates a prepared statement for the given command with the
1896        the given name for later execution with the query_prepared() method.
1897        The name can be empty or left out to create an unnamed statement,
1898        in which case any pre-existing unnamed statement is automatically
1899        replaced; otherwise it is an error if the statement name is already
1900        defined in the current database session.
1901
1902        If any parameters are used, they can be referred to in the query as
1903        numbered parameters of the form $1.
1904        """
1905        if not self.db:
1906            raise _int_error('Connection is not valid')
1907        if name is None:
1908            name = ''
1909        self._do_debug('prepare', name, command)
1910        return self.db.prepare(name, command)
1911
1912    def describe_prepared(self, name=None):
1913        """Describe a prepared SQL statement.
1914
1915        This method returns a Query object describing the result columns of
1916        the prepared statement with the given name. If you omit the name,
1917        the unnamed statement will be described if you created one before.
1918        """
1919        if name is None:
1920            name = ''
1921        return self.db.describe_prepared(name)
1922
1923    def delete_prepared(self, name=None):
1924        """Delete a prepared SQL statement
1925
1926        This deallocates a previously prepared SQL statement with the given
1927        name, or deallocates all prepared statements if you do not specify a
1928        name. Note that prepared statements are also deallocated automatically
1929        when the current session ends.
1930        """
1931        q = "DEALLOCATE %s" % (name or 'ALL',)
1932        self._do_debug(q)
1933        return self.db.query(q)
1934
1935    def pkey(self, table, composite=False, flush=False):
1936        """Get or set the primary key of a table.
1937
1938        Single primary keys are returned as strings unless you
1939        set the composite flag.  Composite primary keys are always
1940        represented as tuples.  Note that this raises a KeyError
1941        if the table does not have a primary key.
1942
1943        If flush is set then the internal cache for primary keys will
1944        be flushed.  This may be necessary after the database schema or
1945        the search path has been changed.
1946        """
1947        pkeys = self._pkeys
1948        if flush:
1949            pkeys.clear()
1950            self._do_debug('The pkey cache has been flushed')
1951        try:  # cache lookup
1952            pkey = pkeys[table]
1953        except KeyError:  # cache miss, check the database
1954            q = ("SELECT a.attname, a.attnum, i.indkey FROM pg_index i"
1955                " JOIN pg_attribute a ON a.attrelid = i.indrelid"
1956                " AND a.attnum = ANY(i.indkey)"
1957                " AND NOT a.attisdropped"
1958                " WHERE i.indrelid=%s::regclass"
1959                " AND i.indisprimary ORDER BY a.attnum") % (
1960                    _quote_if_unqualified('$1', table),)
1961            pkey = self.db.query(q, (table,)).getresult()
1962            if not pkey:
1963                raise KeyError('Table %s has no primary key' % table)
1964            # we want to use the order defined in the primary key index here,
1965            # not the order as defined by the columns in the table
1966            if len(pkey) > 1:
1967                indkey = pkey[0][2]
1968                pkey = sorted(pkey, key=lambda row: indkey.index(row[1]))
1969                pkey = tuple(row[0] for row in pkey)
1970            else:
1971                pkey = pkey[0][0]
1972            pkeys[table] = pkey  # cache it
1973        if composite and not isinstance(pkey, tuple):
1974            pkey = (pkey,)
1975        return pkey
1976
1977    def get_databases(self):
1978        """Get list of databases in the system."""
1979        return [s[0] for s in
1980            self.db.query('SELECT datname FROM pg_database').getresult()]
1981
1982    def get_relations(self, kinds=None, system=False):
1983        """Get list of relations in connected database of specified kinds.
1984
1985        If kinds is None or empty, all kinds of relations are returned.
1986        Otherwise kinds can be a string or sequence of type letters
1987        specifying which kind of relations you want to list.
1988
1989        Set the system flag if you want to get the system relations as well.
1990        """
1991        where = []
1992        if kinds:
1993            where.append("r.relkind IN (%s)" %
1994                ','.join("'%s'" % k for k in kinds))
1995        if not system:
1996            where.append("s.nspname NOT SIMILAR"
1997                " TO 'pg/_%|information/_schema' ESCAPE '/'")
1998        where = " WHERE %s" % ' AND '.join(where) if where else ''
1999        q = ("SELECT quote_ident(s.nspname)||'.'||quote_ident(r.relname)"
2000            " FROM pg_class r"
2001            " JOIN pg_namespace s ON s.oid = r.relnamespace%s"
2002            " ORDER BY s.nspname, r.relname") % where
2003        return [r[0] for r in self.db.query(q).getresult()]
2004
2005    def get_tables(self, system=False):
2006        """Return list of tables in connected database.
2007
2008        Set the system flag if you want to get the system tables as well.
2009        """
2010        return self.get_relations('r', system)
2011
2012    def get_attnames(self, table, with_oid=True, flush=False):
2013        """Given the name of a table, dig out the set of attribute names.
2014
2015        Returns a read-only dictionary of attribute names (the names are
2016        the keys, the values are the names of the attributes' types)
2017        with the column names in the proper order if you iterate over it.
2018
2019        If flush is set, then the internal cache for attribute names will
2020        be flushed. This may be necessary after the database schema or
2021        the search path has been changed.
2022
2023        By default, only a limited number of simple types will be returned.
2024        You can get the regular types after calling use_regtypes(True).
2025        """
2026        attnames = self._attnames
2027        if flush:
2028            attnames.clear()
2029            self._do_debug('The attnames cache has been flushed')
2030        try:  # cache lookup
2031            names = attnames[table]
2032        except KeyError:  # cache miss, check the database
2033            q = "a.attnum > 0"
2034            if with_oid:
2035                q = "(%s OR a.attname = 'oid')" % q
2036            q = self._query_attnames % (_quote_if_unqualified('$1', table), q)
2037            names = self.db.query(q, (table,)).getresult()
2038            types = self.dbtypes
2039            names = ((name[0], types.add(*name[1:])) for name in names)
2040            names = AttrDict(names)
2041            attnames[table] = names  # cache it
2042        return names
2043
2044    def use_regtypes(self, regtypes=None):
2045        """Use regular type names instead of simplified type names."""
2046        if regtypes is None:
2047            return self.dbtypes._regtypes
2048        else:
2049            regtypes = bool(regtypes)
2050            if regtypes != self.dbtypes._regtypes:
2051                self.dbtypes._regtypes = regtypes
2052                self._attnames.clear()
2053                self.dbtypes.clear()
2054            return regtypes
2055
2056    def has_table_privilege(self, table, privilege='select', flush=False):
2057        """Check whether current user has specified table privilege.
2058
2059        If flush is set, then the internal cache for table privileges will
2060        be flushed. This may be necessary after privileges have been changed.
2061        """
2062        privileges = self._privileges
2063        if flush:
2064            privileges.clear()
2065            self._do_debug('The privileges cache has been flushed')
2066        privilege = privilege.lower()
2067        try:  # ask cache
2068            ret = privileges[table, privilege]
2069        except KeyError:  # cache miss, ask the database
2070            q = "SELECT has_table_privilege(%s, $2)" % (
2071                _quote_if_unqualified('$1', table),)
2072            q = self.db.query(q, (table, privilege))
2073            ret = q.getresult()[0][0] == self._make_bool(True)
2074            privileges[table, privilege] = ret  # cache it
2075        return ret
2076
2077    def get(self, table, row, keyname=None):
2078        """Get a row from a database table or view.
2079
2080        This method is the basic mechanism to get a single row.  It assumes
2081        that the keyname specifies a unique row.  It must be the name of a
2082        single column or a tuple of column names.  If the keyname is not
2083        specified, then the primary key for the table is used.
2084
2085        If row is a dictionary, then the value for the key is taken from it.
2086        Otherwise, the row must be a single value or a tuple of values
2087        corresponding to the passed keyname or primary key.  The fetched row
2088        from the table will be returned as a new dictionary or used to replace
2089        the existing values when row was passed as a dictionary.
2090
2091        The OID is also put into the dictionary if the table has one, but
2092        in order to allow the caller to work with multiple tables, it is
2093        munged as "oid(table)" using the actual name of the table.
2094        """
2095        if table.endswith('*'):  # hint for descendant tables can be ignored
2096            table = table[:-1].rstrip()
2097        attnames = self.get_attnames(table)
2098        qoid = _oid_key(table) if 'oid' in attnames else None
2099        if keyname and isinstance(keyname, basestring):
2100            keyname = (keyname,)
2101        if qoid and isinstance(row, dict) and qoid in row and 'oid' not in row:
2102            row['oid'] = row[qoid]
2103        if not keyname:
2104            try:  # if keyname is not specified, try using the primary key
2105                keyname = self.pkey(table, True)
2106            except KeyError:  # the table has no primary key
2107                # try using the oid instead
2108                if qoid and isinstance(row, dict) and 'oid' in row:
2109                    keyname = ('oid',)
2110                else:
2111                    raise _prg_error('Table %s has no primary key' % table)
2112            else:  # the table has a primary key
2113                # check whether all key columns have values
2114                if isinstance(row, dict) and not set(keyname).issubset(row):
2115                    # try using the oid instead
2116                    if qoid and 'oid' in row:
2117                        keyname = ('oid',)
2118                    else:
2119                        raise KeyError(
2120                            'Missing value in row for specified keyname')
2121        if not isinstance(row, dict):
2122            if not isinstance(row, (tuple, list)):
2123                row = [row]
2124            if len(keyname) != len(row):
2125                raise KeyError(
2126                    'Differing number of items in keyname and row')
2127            row = dict(zip(keyname, row))
2128        params = self.adapter.parameter_list()
2129        adapt = params.add
2130        col = self.escape_identifier
2131        what = 'oid, *' if qoid else '*'
2132        where = ' AND '.join('%s = %s' % (
2133            col(k), adapt(row[k], attnames[k])) for k in keyname)
2134        if 'oid' in row:
2135            if qoid:
2136                row[qoid] = row['oid']
2137            del row['oid']
2138        q = 'SELECT %s FROM %s WHERE %s LIMIT 1' % (
2139            what, self._escape_qualified_name(table), where)
2140        self._do_debug(q, params)
2141        q = self.db.query(q, params)
2142        res = q.dictresult()
2143        if not res:
2144            raise _db_error('No such record in %s\nwhere %s\nwith %s' % (
2145                table, where, self._list_params(params)))
2146        for n, value in res[0].items():
2147            if qoid and n == 'oid':
2148                n = qoid
2149            row[n] = value
2150        return row
2151
2152    def insert(self, table, row=None, **kw):
2153        """Insert a row into a database table.
2154
2155        This method inserts a row into a table.  The name of the table must
2156        be passed as the first parameter.  The other parameters are used for
2157        providing the data of the row that shall be inserted into the table.
2158        If a dictionary is supplied as the second parameter, it starts with
2159        that.  Otherwise it uses a blank dictionary. Either way the dictionary
2160        is updated from the keywords.
2161
2162        The dictionary is then reloaded with the values actually inserted in
2163        order to pick up values modified by rules, triggers, etc.
2164        """
2165        if table.endswith('*'):  # hint for descendant tables can be ignored
2166            table = table[:-1].rstrip()
2167        if row is None:
2168            row = {}
2169        row.update(kw)
2170        if 'oid' in row:
2171            del row['oid']  # do not insert oid
2172        attnames = self.get_attnames(table)
2173        qoid = _oid_key(table) if 'oid' in attnames else None
2174        params = self.adapter.parameter_list()
2175        adapt = params.add
2176        col = self.escape_identifier
2177        names, values = [], []
2178        for n in attnames:
2179            if n in row:
2180                names.append(col(n))
2181                values.append(adapt(row[n], attnames[n]))
2182        if not names:
2183            raise _prg_error('No column found that can be inserted')
2184        names, values = ', '.join(names), ', '.join(values)
2185        ret = 'oid, *' if qoid else '*'
2186        q = 'INSERT INTO %s (%s) VALUES (%s) RETURNING %s' % (
2187            self._escape_qualified_name(table), names, values, ret)
2188        self._do_debug(q, params)
2189        q = self.db.query(q, params)
2190        res = q.dictresult()
2191        if res:  # this should always be true
2192            for n, value in res[0].items():
2193                if qoid and n == 'oid':
2194                    n = qoid
2195                row[n] = value
2196        return row
2197
2198    def update(self, table, row=None, **kw):
2199        """Update an existing row in a database table.
2200
2201        Similar to insert, but updates an existing row.  The update is based
2202        on the primary key of the table or the OID value as munged by get()
2203        or passed as keyword.  The OID will take precedence if provided, so
2204        that it is possible to update the primary key itself.
2205
2206        The dictionary is then modified to reflect any changes caused by the
2207        update due to triggers, rules, default values, etc.
2208        """
2209        if table.endswith('*'):
2210            table = table[:-1].rstrip()  # need parent table name
2211        attnames = self.get_attnames(table)
2212        qoid = _oid_key(table) if 'oid' in attnames else None
2213        if row is None:
2214            row = {}
2215        elif 'oid' in row:
2216            del row['oid']  # only accept oid key from named args for safety
2217        row.update(kw)
2218        if qoid and qoid in row and 'oid' not in row:
2219            row['oid'] = row[qoid]
2220        if qoid and 'oid' in row:  # try using the oid
2221            keyname = ('oid',)
2222        else:  # try using the primary key
2223            try:
2224                keyname = self.pkey(table, True)
2225            except KeyError:  # the table has no primary key
2226                raise _prg_error('Table %s has no primary key' % table)
2227            # check whether all key columns have values
2228            if not set(keyname).issubset(row):
2229                raise KeyError('Missing value for primary key in row')
2230        params = self.adapter.parameter_list()
2231        adapt = params.add
2232        col = self.escape_identifier
2233        where = ' AND '.join('%s = %s' % (
2234            col(k), adapt(row[k], attnames[k])) for k in keyname)
2235        if 'oid' in row:
2236            if qoid:
2237                row[qoid] = row['oid']
2238            del row['oid']
2239        values = []
2240        keyname = set(keyname)
2241        for n in attnames:
2242            if n in row and n not in keyname:
2243                values.append('%s = %s' % (col(n), adapt(row[n], attnames[n])))
2244        if not values:
2245            return row
2246        values = ', '.join(values)
2247        ret = 'oid, *' if qoid else '*'
2248        q = 'UPDATE %s SET %s WHERE %s RETURNING %s' % (
2249            self._escape_qualified_name(table), values, where, ret)
2250        self._do_debug(q, params)
2251        q = self.db.query(q, params)
2252        res = q.dictresult()
2253        if res:  # may be empty when row does not exist
2254            for n, value in res[0].items():
2255                if qoid and n == 'oid':
2256                    n = qoid
2257                row[n] = value
2258        return row
2259
2260    def upsert(self, table, row=None, **kw):
2261        """Insert a row into a database table with conflict resolution
2262
2263        This method inserts a row into a table, but instead of raising a
2264        ProgrammingError exception in case a row with the same primary key
2265        already exists, an update will be executed instead.  This will be
2266        performed as a single atomic operation on the database, so race
2267        conditions can be avoided.
2268
2269        Like the insert method, the first parameter is the name of the
2270        table and the second parameter can be used to pass the values to
2271        be inserted as a dictionary.
2272
2273        Unlike the insert und update statement, keyword parameters are not
2274        used to modify the dictionary, but to specify which columns shall
2275        be updated in case of a conflict, and in which way:
2276
2277        A value of False or None means the column shall not be updated,
2278        a value of True means the column shall be updated with the value
2279        that has been proposed for insertion, i.e. has been passed as value
2280        in the dictionary.  Columns that are not specified by keywords but
2281        appear as keys in the dictionary are also updated like in the case
2282        keywords had been passed with the value True.
2283
2284        So if in the case of a conflict you want to update every column that
2285        has been passed in the dictionary row, you would call upsert(table, row).
2286        If you don't want to do anything in case of a conflict, i.e. leave
2287        the existing row as it is, call upsert(table, row, **dict.fromkeys(row)).
2288
2289        If you need more fine-grained control of what gets updated, you can
2290        also pass strings in the keyword parameters.  These strings will
2291        be used as SQL expressions for the update columns.  In these
2292        expressions you can refer to the value that already exists in
2293        the table by prefixing the column name with "included.", and to
2294        the value that has been proposed for insertion by prefixing the
2295        column name with the "excluded."
2296
2297        The dictionary is modified in any case to reflect the values in
2298        the database after the operation has completed.
2299
2300        Note: The method uses the PostgreSQL "upsert" feature which is
2301        only available since PostgreSQL 9.5.
2302        """
2303        if table.endswith('*'):  # hint for descendant tables can be ignored
2304            table = table[:-1].rstrip()
2305        if row is None:
2306            row = {}
2307        if 'oid' in row:
2308            del row['oid']  # do not insert oid
2309        if 'oid' in kw:
2310            del kw['oid']  # do not update oid
2311        attnames = self.get_attnames(table)
2312        qoid = _oid_key(table) if 'oid' in attnames else None
2313        params = self.adapter.parameter_list()
2314        adapt = params.add
2315        col = self.escape_identifier
2316        names, values, updates = [], [], []
2317        for n in attnames:
2318            if n in row:
2319                names.append(col(n))
2320                values.append(adapt(row[n], attnames[n]))
2321        names, values = ', '.join(names), ', '.join(values)
2322        try:
2323            keyname = self.pkey(table, True)
2324        except KeyError:
2325            raise _prg_error('Table %s has no primary key' % table)
2326        target = ', '.join(col(k) for k in keyname)
2327        update = []
2328        keyname = set(keyname)
2329        keyname.add('oid')
2330        for n in attnames:
2331            if n not in keyname:
2332                value = kw.get(n, True)
2333                if value:
2334                    if not isinstance(value, basestring):
2335                        value = 'excluded.%s' % col(n)
2336                    update.append('%s = %s' % (col(n), value))
2337        if not values:
2338            return row
2339        do = 'update set %s' % ', '.join(update) if update else 'nothing'
2340        ret = 'oid, *' if qoid else '*'
2341        q = ('INSERT INTO %s AS included (%s) VALUES (%s)'
2342            ' ON CONFLICT (%s) DO %s RETURNING %s') % (
2343                self._escape_qualified_name(table), names, values,
2344                target, do, ret)
2345        self._do_debug(q, params)
2346        try:
2347            q = self.db.query(q, params)
2348        except ProgrammingError:
2349            if self.server_version < 90500:
2350                raise _prg_error(
2351                    'Upsert operation is not supported by PostgreSQL version')
2352            raise  # re-raise original error
2353        res = q.dictresult()
2354        if res:  # may be empty with "do nothing"
2355            for n, value in res[0].items():
2356                if qoid and n == 'oid':
2357                    n = qoid
2358                row[n] = value
2359        else:
2360            self.get(table, row)
2361        return row
2362
2363    def clear(self, table, row=None):
2364        """Clear all the attributes to values determined by the types.
2365
2366        Numeric types are set to 0, Booleans are set to false, and everything
2367        else is set to the empty string.  If the row argument is present,
2368        it is used as the row dictionary and any entries matching attribute
2369        names are cleared with everything else left unchanged.
2370        """
2371        # At some point we will need a way to get defaults from a table.
2372        if row is None:
2373            row = {}  # empty if argument is not present
2374        attnames = self.get_attnames(table)
2375        for n, t in attnames.items():
2376            if n == 'oid':
2377                continue
2378            t = t.simple
2379            if t in DbTypes._num_types:
2380                row[n] = 0
2381            elif t == 'bool':
2382                row[n] = self._make_bool(False)
2383            else:
2384                row[n] = ''
2385        return row
2386
2387    def delete(self, table, row=None, **kw):
2388        """Delete an existing row in a database table.
2389
2390        This method deletes the row from a table.  It deletes based on the
2391        primary key of the table or the OID value as munged by get() or
2392        passed as keyword.  The OID will take precedence if provided.
2393
2394        The return value is the number of deleted rows (i.e. 0 if the row
2395        did not exist and 1 if the row was deleted).
2396
2397        Note that if the row cannot be deleted because e.g. it is still
2398        referenced by another table, this method raises a ProgrammingError.
2399        """
2400        if table.endswith('*'):  # hint for descendant tables can be ignored
2401            table = table[:-1].rstrip()
2402        attnames = self.get_attnames(table)
2403        qoid = _oid_key(table) if 'oid' in attnames else None
2404        if row is None:
2405            row = {}
2406        elif 'oid' in row:
2407            del row['oid']  # only accept oid key from named args for safety
2408        row.update(kw)
2409        if qoid and qoid in row and 'oid' not in row:
2410            row['oid'] = row[qoid]
2411        if qoid and 'oid' in row:  # try using the oid
2412            keyname = ('oid',)
2413        else:  # try using the primary key
2414            try:
2415                keyname = self.pkey(table, True)
2416            except KeyError:  # the table has no primary key
2417                raise _prg_error('Table %s has no primary key' % table)
2418            # check whether all key columns have values
2419            if not set(keyname).issubset(row):
2420                raise KeyError('Missing value for primary key in row')
2421        params = self.adapter.parameter_list()
2422        adapt = params.add
2423        col = self.escape_identifier
2424        where = ' AND '.join('%s = %s' % (
2425            col(k), adapt(row[k], attnames[k])) for k in keyname)
2426        if 'oid' in row:
2427            if qoid:
2428                row[qoid] = row['oid']
2429            del row['oid']
2430        q = 'DELETE FROM %s WHERE %s' % (
2431            self._escape_qualified_name(table), where)
2432        self._do_debug(q, params)
2433        res = self.db.query(q, params)
2434        return int(res)
2435
2436    def truncate(self, table, restart=False, cascade=False, only=False):
2437        """Empty a table or set of tables.
2438
2439        This method quickly removes all rows from the given table or set
2440        of tables.  It has the same effect as an unqualified DELETE on each
2441        table, but since it does not actually scan the tables it is faster.
2442        Furthermore, it reclaims disk space immediately, rather than requiring
2443        a subsequent VACUUM operation. This is most useful on large tables.
2444
2445        If restart is set to True, sequences owned by columns of the truncated
2446        table(s) are automatically restarted.  If cascade is set to True, it
2447        also truncates all tables that have foreign-key references to any of
2448        the named tables.  If the parameter only is not set to True, all the
2449        descendant tables (if any) will also be truncated. Optionally, a '*'
2450        can be specified after the table name to explicitly indicate that
2451        descendant tables are included.
2452        """
2453        if isinstance(table, basestring):
2454            only = {table: only}
2455            table = [table]
2456        elif isinstance(table, (list, tuple)):
2457            if isinstance(only, (list, tuple)):
2458                only = dict(zip(table, only))
2459            else:
2460                only = dict.fromkeys(table, only)
2461        elif isinstance(table, (set, frozenset)):
2462            only = dict.fromkeys(table, only)
2463        else:
2464            raise TypeError('The table must be a string, list or set')
2465        if not (restart is None or isinstance(restart, (bool, int))):
2466            raise TypeError('Invalid type for the restart option')
2467        if not (cascade is None or isinstance(cascade, (bool, int))):
2468            raise TypeError('Invalid type for the cascade option')
2469        tables = []
2470        for t in table:
2471            u = only.get(t)
2472            if not (u is None or isinstance(u, (bool, int))):
2473                raise TypeError('Invalid type for the only option')
2474            if t.endswith('*'):
2475                if u:
2476                    raise ValueError(
2477                        'Contradictory table name and only options')
2478                t = t[:-1].rstrip()
2479            t = self._escape_qualified_name(t)
2480            if u:
2481                t = 'ONLY %s' % t
2482            tables.append(t)
2483        q = ['TRUNCATE', ', '.join(tables)]
2484        if restart:
2485            q.append('RESTART IDENTITY')
2486        if cascade:
2487            q.append('CASCADE')
2488        q = ' '.join(q)
2489        self._do_debug(q)
2490        return self.db.query(q)
2491
2492    def get_as_list(self, table, what=None, where=None,
2493            order=None, limit=None, offset=None, scalar=False):
2494        """Get a table as a list.
2495
2496        This gets a convenient representation of the table as a list
2497        of named tuples in Python.  You only need to pass the name of
2498        the table (or any other SQL expression returning rows).  Note that
2499        by default this will return the full content of the table which
2500        can be huge and overflow your memory.  However, you can control
2501        the amount of data returned using the other optional parameters.
2502
2503        The parameter 'what' can restrict the query to only return a
2504        subset of the table columns.  It can be a string, list or a tuple.
2505        The parameter 'where' can restrict the query to only return a
2506        subset of the table rows.  It can be a string, list or a tuple
2507        of SQL expressions that all need to be fulfilled.  The parameter
2508        'order' specifies the ordering of the rows.  It can also be a
2509        other string, list or a tuple.  If no ordering is specified,
2510        the result will be ordered by the primary key(s) or all columns
2511        if no primary key exists.  You can set 'order' to False if you
2512        don't care about the ordering.  The parameters 'limit' and 'offset'
2513        can be integers specifying the maximum number of rows returned
2514        and a number of rows skipped over.
2515
2516        If you set the 'scalar' option to True, then instead of the
2517        named tuples you will get the first items of these tuples.
2518        This is useful if the result has only one column anyway.
2519        """
2520        if not table:
2521            raise TypeError('The table name is missing')
2522        if what:
2523            if isinstance(what, (list, tuple)):
2524                what = ', '.join(map(str, what))
2525            if order is None:
2526                order = what
2527        else:
2528            what = '*'
2529        q = ['SELECT', what, 'FROM', table]
2530        if where:
2531            if isinstance(where, (list, tuple)):
2532                where = ' AND '.join(map(str, where))
2533            q.extend(['WHERE', where])
2534        if order is None:
2535            try:
2536                order = self.pkey(table, True)
2537            except (KeyError, ProgrammingError):
2538                try:
2539                    order = list(self.get_attnames(table))
2540                except (KeyError, ProgrammingError):
2541                    pass
2542        if order:
2543            if isinstance(order, (list, tuple)):
2544                order = ', '.join(map(str, order))
2545            q.extend(['ORDER BY', order])
2546        if limit:
2547            q.append('LIMIT %d' % limit)
2548        if offset:
2549            q.append('OFFSET %d' % offset)
2550        q = ' '.join(q)
2551        self._do_debug(q)
2552        q = self.db.query(q)
2553        res = q.namedresult()
2554        if res and scalar:
2555            res = [row[0] for row in res]
2556        return res
2557
2558    def get_as_dict(self, table, keyname=None, what=None, where=None,
2559            order=None, limit=None, offset=None, scalar=False):
2560        """Get a table as a dictionary.
2561
2562        This method is similar to get_as_list(), but returns the table
2563        as a Python dict instead of a Python list, which can be even
2564        more convenient. The primary key column(s) of the table will
2565        be used as the keys of the dictionary, while the other column(s)
2566        will be the corresponding values.  The keys will be named tuples
2567        if the table has a composite primary key.  The rows will be also
2568        named tuples unless the 'scalar' option has been set to True.
2569        With the optional parameter 'keyname' you can specify an alternative
2570        set of columns to be used as the keys of the dictionary.  It must
2571        be set as a string, list or a tuple.
2572
2573        If the Python version supports it, the dictionary will be an
2574        OrderedDict using the order specified with the 'order' parameter
2575        or the key column(s) if not specified.  You can set 'order' to False
2576        if you don't care about the ordering.  In this case the returned
2577        dictionary will be an ordinary one.
2578        """
2579        if not table:
2580            raise TypeError('The table name is missing')
2581        if not keyname:
2582            try:
2583                keyname = self.pkey(table, True)
2584            except (KeyError, ProgrammingError):
2585                raise _prg_error('Table %s has no primary key' % table)
2586        if isinstance(keyname, basestring):
2587            keyname = [keyname]
2588        elif not isinstance(keyname, (list, tuple)):
2589            raise KeyError('The keyname must be a string, list or tuple')
2590        if what:
2591            if isinstance(what, (list, tuple)):
2592                what = ', '.join(map(str, what))
2593            if order is None:
2594                order = what
2595        else:
2596            what = '*'
2597        q = ['SELECT', what, 'FROM', table]
2598        if where:
2599            if isinstance(where, (list, tuple)):
2600                where = ' AND '.join(map(str, where))
2601            q.extend(['WHERE', where])
2602        if order is None:
2603            order = keyname
2604        if order:
2605            if isinstance(order, (list, tuple)):
2606                order = ', '.join(map(str, order))
2607            q.extend(['ORDER BY', order])
2608        if limit:
2609            q.append('LIMIT %d' % limit)
2610        if offset:
2611            q.append('OFFSET %d' % offset)
2612        q = ' '.join(q)
2613        self._do_debug(q)
2614        q = self.db.query(q)
2615        res = q.getresult()
2616        cls = OrderedDict if order else dict
2617        if not res:
2618            return cls()
2619        keyset = set(keyname)
2620        fields = q.listfields()
2621        if not keyset.issubset(fields):
2622            raise KeyError('Missing keyname in row')
2623        keyind, rowind = [], []
2624        for i, f in enumerate(fields):
2625            (keyind if f in keyset else rowind).append(i)
2626        keytuple = len(keyind) > 1
2627        getkey = itemgetter(*keyind)
2628        keys = map(getkey, res)
2629        if scalar:
2630            rowind = rowind[:1]
2631            rowtuple = False
2632        else:
2633            rowtuple = len(rowind) > 1
2634        if scalar or rowtuple:
2635            getrow = itemgetter(*rowind)
2636        else:
2637            rowind = rowind[0]
2638            getrow = lambda row: (row[rowind],)
2639            rowtuple = True
2640        rows = map(getrow, res)
2641        if keytuple or rowtuple:
2642            namedresult = get_namedresult()
2643            if namedresult:
2644                if keytuple:
2645                    keys = namedresult(_MemoryQuery(keys, keyname))
2646                if rowtuple:
2647                    fields = [f for f in fields if f not in keyset]
2648                    rows = namedresult(_MemoryQuery(rows, fields))
2649        return cls(zip(keys, rows))
2650
2651    def notification_handler(self,
2652            event, callback, arg_dict=None, timeout=None, stop_event=None):
2653        """Get notification handler that will run the given callback."""
2654        return NotificationHandler(self,
2655            event, callback, arg_dict, timeout, stop_event)
2656
2657
2658# if run as script, print some information
2659
2660if __name__ == '__main__':
2661    print('PyGreSQL version' + version)
2662    print('')
2663    print(__doc__)
Note: See TracBrowser for help on using the repository browser.