source: trunk/pg.py

Last change on this file was 930, checked in by cito, 9 months ago

Update year, version number and supported Pg versions

  • Property svn:keywords set to Id
File size: 93.8 KB
Line 
1#! /usr/bin/python
2#
3# pg.py
4#
5# $Id: pg.py 930 2018-01-22 09:44:28Z darcy $
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-2018 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 be
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 pkey(self, table, composite=False, flush=False):
1873        """Get or set the primary key of a table.
1874
1875        Single primary keys are returned as strings unless you
1876        set the composite flag.  Composite primary keys are always
1877        represented as tuples.  Note that this raises a KeyError
1878        if the table does not have a primary key.
1879
1880        If flush is set then the internal cache for primary keys will
1881        be flushed.  This may be necessary after the database schema or
1882        the search path has been changed.
1883        """
1884        pkeys = self._pkeys
1885        if flush:
1886            pkeys.clear()
1887            self._do_debug('The pkey cache has been flushed')
1888        try:  # cache lookup
1889            pkey = pkeys[table]
1890        except KeyError:  # cache miss, check the database
1891            q = ("SELECT a.attname, a.attnum, i.indkey FROM pg_index i"
1892                " JOIN pg_attribute a ON a.attrelid = i.indrelid"
1893                " AND a.attnum = ANY(i.indkey)"
1894                " AND NOT a.attisdropped"
1895                " WHERE i.indrelid=%s::regclass"
1896                " AND i.indisprimary ORDER BY a.attnum") % (
1897                    _quote_if_unqualified('$1', table),)
1898            pkey = self.db.query(q, (table,)).getresult()
1899            if not pkey:
1900                raise KeyError('Table %s has no primary key' % table)
1901            # we want to use the order defined in the primary key index here,
1902            # not the order as defined by the columns in the table
1903            if len(pkey) > 1:
1904                indkey = pkey[0][2]
1905                pkey = sorted(pkey, key=lambda row: indkey.index(row[1]))
1906                pkey = tuple(row[0] for row in pkey)
1907            else:
1908                pkey = pkey[0][0]
1909            pkeys[table] = pkey  # cache it
1910        if composite and not isinstance(pkey, tuple):
1911            pkey = (pkey,)
1912        return pkey
1913
1914    def get_databases(self):
1915        """Get list of databases in the system."""
1916        return [s[0] for s in
1917            self.db.query('SELECT datname FROM pg_database').getresult()]
1918
1919    def get_relations(self, kinds=None, system=False):
1920        """Get list of relations in connected database of specified kinds.
1921
1922        If kinds is None or empty, all kinds of relations are returned.
1923        Otherwise kinds can be a string or sequence of type letters
1924        specifying which kind of relations you want to list.
1925
1926        Set the system flag if you want to get the system relations as well.
1927        """
1928        where = []
1929        if kinds:
1930            where.append("r.relkind IN (%s)" %
1931                ','.join("'%s'" % k for k in kinds))
1932        if not system:
1933            where.append("s.nspname NOT SIMILAR"
1934                " TO 'pg/_%|information/_schema' ESCAPE '/'")
1935        where = " WHERE %s" % ' AND '.join(where) if where else ''
1936        q = ("SELECT quote_ident(s.nspname)||'.'||quote_ident(r.relname)"
1937            " FROM pg_class r"
1938            " JOIN pg_namespace s ON s.oid = r.relnamespace%s"
1939            " ORDER BY s.nspname, r.relname") % where
1940        return [r[0] for r in self.db.query(q).getresult()]
1941
1942    def get_tables(self, system=False):
1943        """Return list of tables in connected database.
1944
1945        Set the system flag if you want to get the system tables as well.
1946        """
1947        return self.get_relations('r', system)
1948
1949    def get_attnames(self, table, with_oid=True, flush=False):
1950        """Given the name of a table, dig out the set of attribute names.
1951
1952        Returns a read-only dictionary of attribute names (the names are
1953        the keys, the values are the names of the attributes' types)
1954        with the column names in the proper order if you iterate over it.
1955
1956        If flush is set, then the internal cache for attribute names will
1957        be flushed. This may be necessary after the database schema or
1958        the search path has been changed.
1959
1960        By default, only a limited number of simple types will be returned.
1961        You can get the regular types after calling use_regtypes(True).
1962        """
1963        attnames = self._attnames
1964        if flush:
1965            attnames.clear()
1966            self._do_debug('The attnames cache has been flushed')
1967        try:  # cache lookup
1968            names = attnames[table]
1969        except KeyError:  # cache miss, check the database
1970            q = "a.attnum > 0"
1971            if with_oid:
1972                q = "(%s OR a.attname = 'oid')" % q
1973            q = self._query_attnames % (_quote_if_unqualified('$1', table), q)
1974            names = self.db.query(q, (table,)).getresult()
1975            types = self.dbtypes
1976            names = ((name[0], types.add(*name[1:])) for name in names)
1977            names = AttrDict(names)
1978            attnames[table] = names  # cache it
1979        return names
1980
1981    def use_regtypes(self, regtypes=None):
1982        """Use regular type names instead of simplified type names."""
1983        if regtypes is None:
1984            return self.dbtypes._regtypes
1985        else:
1986            regtypes = bool(regtypes)
1987            if regtypes != self.dbtypes._regtypes:
1988                self.dbtypes._regtypes = regtypes
1989                self._attnames.clear()
1990                self.dbtypes.clear()
1991            return regtypes
1992
1993    def has_table_privilege(self, table, privilege='select', flush=False):
1994        """Check whether current user has specified table privilege.
1995
1996        If flush is set, then the internal cache for table privileges will
1997        be flushed. This may be necessary after privileges have been changed.
1998        """
1999        privileges = self._privileges
2000        if flush:
2001            privileges.clear()
2002            self._do_debug('The privileges cache has been flushed')
2003        privilege = privilege.lower()
2004        try:  # ask cache
2005            ret = privileges[table, privilege]
2006        except KeyError:  # cache miss, ask the database
2007            q = "SELECT has_table_privilege(%s, $2)" % (
2008                _quote_if_unqualified('$1', table),)
2009            q = self.db.query(q, (table, privilege))
2010            ret = q.getresult()[0][0] == self._make_bool(True)
2011            privileges[table, privilege] = ret  # cache it
2012        return ret
2013
2014    def get(self, table, row, keyname=None):
2015        """Get a row from a database table or view.
2016
2017        This method is the basic mechanism to get a single row.  It assumes
2018        that the keyname specifies a unique row.  It must be the name of a
2019        single column or a tuple of column names.  If the keyname is not
2020        specified, then the primary key for the table is used.
2021
2022        If row is a dictionary, then the value for the key is taken from it.
2023        Otherwise, the row must be a single value or a tuple of values
2024        corresponding to the passed keyname or primary key.  The fetched row
2025        from the table will be returned as a new dictionary or used to replace
2026        the existing values when row was passed as a dictionary.
2027
2028        The OID is also put into the dictionary if the table has one, but
2029        in order to allow the caller to work with multiple tables, it is
2030        munged as "oid(table)" using the actual name of the table.
2031        """
2032        if table.endswith('*'):  # hint for descendant tables can be ignored
2033            table = table[:-1].rstrip()
2034        attnames = self.get_attnames(table)
2035        qoid = _oid_key(table) if 'oid' in attnames else None
2036        if keyname and isinstance(keyname, basestring):
2037            keyname = (keyname,)
2038        if qoid and isinstance(row, dict) and qoid in row and 'oid' not in row:
2039            row['oid'] = row[qoid]
2040        if not keyname:
2041            try:  # if keyname is not specified, try using the primary key
2042                keyname = self.pkey(table, True)
2043            except KeyError:  # the table has no primary key
2044                # try using the oid instead
2045                if qoid and isinstance(row, dict) and 'oid' in row:
2046                    keyname = ('oid',)
2047                else:
2048                    raise _prg_error('Table %s has no primary key' % table)
2049            else:  # the table has a primary key
2050                # check whether all key columns have values
2051                if isinstance(row, dict) and not set(keyname).issubset(row):
2052                    # try using the oid instead
2053                    if qoid and 'oid' in row:
2054                        keyname = ('oid',)
2055                    else:
2056                        raise KeyError(
2057                            'Missing value in row for specified keyname')
2058        if not isinstance(row, dict):
2059            if not isinstance(row, (tuple, list)):
2060                row = [row]
2061            if len(keyname) != len(row):
2062                raise KeyError(
2063                    'Differing number of items in keyname and row')
2064            row = dict(zip(keyname, row))
2065        params = self.adapter.parameter_list()
2066        adapt = params.add
2067        col = self.escape_identifier
2068        what = 'oid, *' if qoid else '*'
2069        where = ' AND '.join('%s = %s' % (
2070            col(k), adapt(row[k], attnames[k])) for k in keyname)
2071        if 'oid' in row:
2072            if qoid:
2073                row[qoid] = row['oid']
2074            del row['oid']
2075        q = 'SELECT %s FROM %s WHERE %s LIMIT 1' % (
2076            what, self._escape_qualified_name(table), where)
2077        self._do_debug(q, params)
2078        q = self.db.query(q, params)
2079        res = q.dictresult()
2080        if not res:
2081            raise _db_error('No such record in %s\nwhere %s\nwith %s' % (
2082                table, where, self._list_params(params)))
2083        for n, value in res[0].items():
2084            if qoid and n == 'oid':
2085                n = qoid
2086            row[n] = value
2087        return row
2088
2089    def insert(self, table, row=None, **kw):
2090        """Insert a row into a database table.
2091
2092        This method inserts a row into a table.  The name of the table must
2093        be passed as the first parameter.  The other parameters are used for
2094        providing the data of the row that shall be inserted into the table.
2095        If a dictionary is supplied as the second parameter, it starts with
2096        that.  Otherwise it uses a blank dictionary. Either way the dictionary
2097        is updated from the keywords.
2098
2099        The dictionary is then reloaded with the values actually inserted in
2100        order to pick up values modified by rules, triggers, etc.
2101        """
2102        if table.endswith('*'):  # hint for descendant tables can be ignored
2103            table = table[:-1].rstrip()
2104        if row is None:
2105            row = {}
2106        row.update(kw)
2107        if 'oid' in row:
2108            del row['oid']  # do not insert oid
2109        attnames = self.get_attnames(table)
2110        qoid = _oid_key(table) if 'oid' in attnames else None
2111        params = self.adapter.parameter_list()
2112        adapt = params.add
2113        col = self.escape_identifier
2114        names, values = [], []
2115        for n in attnames:
2116            if n in row:
2117                names.append(col(n))
2118                values.append(adapt(row[n], attnames[n]))
2119        if not names:
2120            raise _prg_error('No column found that can be inserted')
2121        names, values = ', '.join(names), ', '.join(values)
2122        ret = 'oid, *' if qoid else '*'
2123        q = 'INSERT INTO %s (%s) VALUES (%s) RETURNING %s' % (
2124            self._escape_qualified_name(table), names, values, ret)
2125        self._do_debug(q, params)
2126        q = self.db.query(q, params)
2127        res = q.dictresult()
2128        if res:  # this should always be true
2129            for n, value in res[0].items():
2130                if qoid and n == 'oid':
2131                    n = qoid
2132                row[n] = value
2133        return row
2134
2135    def update(self, table, row=None, **kw):
2136        """Update an existing row in a database table.
2137
2138        Similar to insert, but updates an existing row.  The update is based
2139        on the primary key of the table or the OID value as munged by get()
2140        or passed as keyword.  The OID will take precedence if provided, so
2141        that it is possible to update the primary key itself.
2142
2143        The dictionary is then modified to reflect any changes caused by the
2144        update due to triggers, rules, default values, etc.
2145        """
2146        if table.endswith('*'):
2147            table = table[:-1].rstrip()  # need parent table name
2148        attnames = self.get_attnames(table)
2149        qoid = _oid_key(table) if 'oid' in attnames else None
2150        if row is None:
2151            row = {}
2152        elif 'oid' in row:
2153            del row['oid']  # only accept oid key from named args for safety
2154        row.update(kw)
2155        if qoid and qoid in row and 'oid' not in row:
2156            row['oid'] = row[qoid]
2157        if qoid and 'oid' in row:  # try using the oid
2158            keyname = ('oid',)
2159        else:  # try using the primary key
2160            try:
2161                keyname = self.pkey(table, True)
2162            except KeyError:  # the table has no primary key
2163                raise _prg_error('Table %s has no primary key' % table)
2164            # check whether all key columns have values
2165            if not set(keyname).issubset(row):
2166                raise KeyError('Missing value for primary key in row')
2167        params = self.adapter.parameter_list()
2168        adapt = params.add
2169        col = self.escape_identifier
2170        where = ' AND '.join('%s = %s' % (
2171            col(k), adapt(row[k], attnames[k])) for k in keyname)
2172        if 'oid' in row:
2173            if qoid:
2174                row[qoid] = row['oid']
2175            del row['oid']
2176        values = []
2177        keyname = set(keyname)
2178        for n in attnames:
2179            if n in row and n not in keyname:
2180                values.append('%s = %s' % (col(n), adapt(row[n], attnames[n])))
2181        if not values:
2182            return row
2183        values = ', '.join(values)
2184        ret = 'oid, *' if qoid else '*'
2185        q = 'UPDATE %s SET %s WHERE %s RETURNING %s' % (
2186            self._escape_qualified_name(table), values, where, ret)
2187        self._do_debug(q, params)
2188        q = self.db.query(q, params)
2189        res = q.dictresult()
2190        if res:  # may be empty when row does not exist
2191            for n, value in res[0].items():
2192                if qoid and n == 'oid':
2193                    n = qoid
2194                row[n] = value
2195        return row
2196
2197    def upsert(self, table, row=None, **kw):
2198        """Insert a row into a database table with conflict resolution
2199
2200        This method inserts a row into a table, but instead of raising a
2201        ProgrammingError exception in case a row with the same primary key
2202        already exists, an update will be executed instead.  This will be
2203        performed as a single atomic operation on the database, so race
2204        conditions can be avoided.
2205
2206        Like the insert method, the first parameter is the name of the
2207        table and the second parameter can be used to pass the values to
2208        be inserted as a dictionary.
2209
2210        Unlike the insert und update statement, keyword parameters are not
2211        used to modify the dictionary, but to specify which columns shall
2212        be updated in case of a conflict, and in which way:
2213
2214        A value of False or None means the column shall not be updated,
2215        a value of True means the column shall be updated with the value
2216        that has been proposed for insertion, i.e. has been passed as value
2217        in the dictionary.  Columns that are not specified by keywords but
2218        appear as keys in the dictionary are also updated like in the case
2219        keywords had been passed with the value True.
2220
2221        So if in the case of a conflict you want to update every column that
2222        has been passed in the dictionary row, you would call upsert(table, row).
2223        If you don't want to do anything in case of a conflict, i.e. leave
2224        the existing row as it is, call upsert(table, row, **dict.fromkeys(row)).
2225
2226        If you need more fine-grained control of what gets updated, you can
2227        also pass strings in the keyword parameters.  These strings will
2228        be used as SQL expressions for the update columns.  In these
2229        expressions you can refer to the value that already exists in
2230        the table by prefixing the column name with "included.", and to
2231        the value that has been proposed for insertion by prefixing the
2232        column name with the "excluded."
2233
2234        The dictionary is modified in any case to reflect the values in
2235        the database after the operation has completed.
2236
2237        Note: The method uses the PostgreSQL "upsert" feature which is
2238        only available since PostgreSQL 9.5.
2239        """
2240        if table.endswith('*'):  # hint for descendant tables can be ignored
2241            table = table[:-1].rstrip()
2242        if row is None:
2243            row = {}
2244        if 'oid' in row:
2245            del row['oid']  # do not insert oid
2246        if 'oid' in kw:
2247            del kw['oid']  # do not update oid
2248        attnames = self.get_attnames(table)
2249        qoid = _oid_key(table) if 'oid' in attnames else None
2250        params = self.adapter.parameter_list()
2251        adapt = params.add
2252        col = self.escape_identifier
2253        names, values, updates = [], [], []
2254        for n in attnames:
2255            if n in row:
2256                names.append(col(n))
2257                values.append(adapt(row[n], attnames[n]))
2258        names, values = ', '.join(names), ', '.join(values)
2259        try:
2260            keyname = self.pkey(table, True)
2261        except KeyError:
2262            raise _prg_error('Table %s has no primary key' % table)
2263        target = ', '.join(col(k) for k in keyname)
2264        update = []
2265        keyname = set(keyname)
2266        keyname.add('oid')
2267        for n in attnames:
2268            if n not in keyname:
2269                value = kw.get(n, True)
2270                if value:
2271                    if not isinstance(value, basestring):
2272                        value = 'excluded.%s' % col(n)
2273                    update.append('%s = %s' % (col(n), value))
2274        if not values:
2275            return row
2276        do = 'update set %s' % ', '.join(update) if update else 'nothing'
2277        ret = 'oid, *' if qoid else '*'
2278        q = ('INSERT INTO %s AS included (%s) VALUES (%s)'
2279            ' ON CONFLICT (%s) DO %s RETURNING %s') % (
2280                self._escape_qualified_name(table), names, values,
2281                target, do, ret)
2282        self._do_debug(q, params)
2283        try:
2284            q = self.db.query(q, params)
2285        except ProgrammingError:
2286            if self.server_version < 90500:
2287                raise _prg_error(
2288                    'Upsert operation is not supported by PostgreSQL version')
2289            raise  # re-raise original error
2290        res = q.dictresult()
2291        if res:  # may be empty with "do nothing"
2292            for n, value in res[0].items():
2293                if qoid and n == 'oid':
2294                    n = qoid
2295                row[n] = value
2296        else:
2297            self.get(table, row)
2298        return row
2299
2300    def clear(self, table, row=None):
2301        """Clear all the attributes to values determined by the types.
2302
2303        Numeric types are set to 0, Booleans are set to false, and everything
2304        else is set to the empty string.  If the row argument is present,
2305        it is used as the row dictionary and any entries matching attribute
2306        names are cleared with everything else left unchanged.
2307        """
2308        # At some point we will need a way to get defaults from a table.
2309        if row is None:
2310            row = {}  # empty if argument is not present
2311        attnames = self.get_attnames(table)
2312        for n, t in attnames.items():
2313            if n == 'oid':
2314                continue
2315            t = t.simple
2316            if t in DbTypes._num_types:
2317                row[n] = 0
2318            elif t == 'bool':
2319                row[n] = self._make_bool(False)
2320            else:
2321                row[n] = ''
2322        return row
2323
2324    def delete(self, table, row=None, **kw):
2325        """Delete an existing row in a database table.
2326
2327        This method deletes the row from a table.  It deletes based on the
2328        primary key of the table or the OID value as munged by get() or
2329        passed as keyword.  The OID will take precedence if provided.
2330
2331        The return value is the number of deleted rows (i.e. 0 if the row
2332        did not exist and 1 if the row was deleted).
2333
2334        Note that if the row cannot be deleted because e.g. it is still
2335        referenced by another table, this method raises a ProgrammingError.
2336        """
2337        if table.endswith('*'):  # hint for descendant tables can be ignored
2338            table = table[:-1].rstrip()
2339        attnames = self.get_attnames(table)
2340        qoid = _oid_key(table) if 'oid' in attnames else None
2341        if row is None:
2342            row = {}
2343        elif 'oid' in row:
2344            del row['oid']  # only accept oid key from named args for safety
2345        row.update(kw)
2346        if qoid and qoid in row and 'oid' not in row:
2347            row['oid'] = row[qoid]
2348        if qoid and 'oid' in row:  # try using the oid
2349            keyname = ('oid',)
2350        else:  # try using the primary key
2351            try:
2352                keyname = self.pkey(table, True)
2353            except KeyError:  # the table has no primary key
2354                raise _prg_error('Table %s has no primary key' % table)
2355            # check whether all key columns have values
2356            if not set(keyname).issubset(row):
2357                raise KeyError('Missing value for primary key in row')
2358        params = self.adapter.parameter_list()
2359        adapt = params.add
2360        col = self.escape_identifier
2361        where = ' AND '.join('%s = %s' % (
2362            col(k), adapt(row[k], attnames[k])) for k in keyname)
2363        if 'oid' in row:
2364            if qoid:
2365                row[qoid] = row['oid']
2366            del row['oid']
2367        q = 'DELETE FROM %s WHERE %s' % (
2368            self._escape_qualified_name(table), where)
2369        self._do_debug(q, params)
2370        res = self.db.query(q, params)
2371        return int(res)
2372
2373    def truncate(self, table, restart=False, cascade=False, only=False):
2374        """Empty a table or set of tables.
2375
2376        This method quickly removes all rows from the given table or set
2377        of tables.  It has the same effect as an unqualified DELETE on each
2378        table, but since it does not actually scan the tables it is faster.
2379        Furthermore, it reclaims disk space immediately, rather than requiring
2380        a subsequent VACUUM operation. This is most useful on large tables.
2381
2382        If restart is set to True, sequences owned by columns of the truncated
2383        table(s) are automatically restarted.  If cascade is set to True, it
2384        also truncates all tables that have foreign-key references to any of
2385        the named tables.  If the parameter only is not set to True, all the
2386        descendant tables (if any) will also be truncated. Optionally, a '*'
2387        can be specified after the table name to explicitly indicate that
2388        descendant tables are included.
2389        """
2390        if isinstance(table, basestring):
2391            only = {table: only}
2392            table = [table]
2393        elif isinstance(table, (list, tuple)):
2394            if isinstance(only, (list, tuple)):
2395                only = dict(zip(table, only))
2396            else:
2397                only = dict.fromkeys(table, only)
2398        elif isinstance(table, (set, frozenset)):
2399            only = dict.fromkeys(table, only)
2400        else:
2401            raise TypeError('The table must be a string, list or set')
2402        if not (restart is None or isinstance(restart, (bool, int))):
2403            raise TypeError('Invalid type for the restart option')
2404        if not (cascade is None or isinstance(cascade, (bool, int))):
2405            raise TypeError('Invalid type for the cascade option')
2406        tables = []
2407        for t in table:
2408            u = only.get(t)
2409            if not (u is None or isinstance(u, (bool, int))):
2410                raise TypeError('Invalid type for the only option')
2411            if t.endswith('*'):
2412                if u:
2413                    raise ValueError(
2414                        'Contradictory table name and only options')
2415                t = t[:-1].rstrip()
2416            t = self._escape_qualified_name(t)
2417            if u:
2418                t = 'ONLY %s' % t
2419            tables.append(t)
2420        q = ['TRUNCATE', ', '.join(tables)]
2421        if restart:
2422            q.append('RESTART IDENTITY')
2423        if cascade:
2424            q.append('CASCADE')
2425        q = ' '.join(q)
2426        self._do_debug(q)
2427        return self.db.query(q)
2428
2429    def get_as_list(self, table, what=None, where=None,
2430            order=None, limit=None, offset=None, scalar=False):
2431        """Get a table as a list.
2432
2433        This gets a convenient representation of the table as a list
2434        of named tuples in Python.  You only need to pass the name of
2435        the table (or any other SQL expression returning rows).  Note that
2436        by default this will return the full content of the table which
2437        can be huge and overflow your memory.  However, you can control
2438        the amount of data returned using the other optional parameters.
2439
2440        The parameter 'what' can restrict the query to only return a
2441        subset of the table columns.  It can be a string, list or a tuple.
2442        The parameter 'where' can restrict the query to only return a
2443        subset of the table rows.  It can be a string, list or a tuple
2444        of SQL expressions that all need to be fulfilled.  The parameter
2445        'order' specifies the ordering of the rows.  It can also be a
2446        other string, list or a tuple.  If no ordering is specified,
2447        the result will be ordered by the primary key(s) or all columns
2448        if no primary key exists.  You can set 'order' to False if you
2449        don't care about the ordering.  The parameters 'limit' and 'offset'
2450        can be integers specifying the maximum number of rows returned
2451        and a number of rows skipped over.
2452
2453        If you set the 'scalar' option to True, then instead of the
2454        named tuples you will get the first items of these tuples.
2455        This is useful if the result has only one column anyway.
2456        """
2457        if not table:
2458            raise TypeError('The table name is missing')
2459        if what:
2460            if isinstance(what, (list, tuple)):
2461                what = ', '.join(map(str, what))
2462            if order is None:
2463                order = what
2464        else:
2465            what = '*'
2466        q = ['SELECT', what, 'FROM', table]
2467        if where:
2468            if isinstance(where, (list, tuple)):
2469                where = ' AND '.join(map(str, where))
2470            q.extend(['WHERE', where])
2471        if order is None:
2472            try:
2473                order = self.pkey(table, True)
2474            except (KeyError, ProgrammingError):
2475                try:
2476                    order = list(self.get_attnames(table))
2477                except (KeyError, ProgrammingError):
2478                    pass
2479        if order:
2480            if isinstance(order, (list, tuple)):
2481                order = ', '.join(map(str, order))
2482            q.extend(['ORDER BY', order])
2483        if limit:
2484            q.append('LIMIT %d' % limit)
2485        if offset:
2486            q.append('OFFSET %d' % offset)
2487        q = ' '.join(q)
2488        self._do_debug(q)
2489        q = self.db.query(q)
2490        res = q.namedresult()
2491        if res and scalar:
2492            res = [row[0] for row in res]
2493        return res
2494
2495    def get_as_dict(self, table, keyname=None, what=None, where=None,
2496            order=None, limit=None, offset=None, scalar=False):
2497        """Get a table as a dictionary.
2498
2499        This method is similar to get_as_list(), but returns the table
2500        as a Python dict instead of a Python list, which can be even
2501        more convenient. The primary key column(s) of the table will
2502        be used as the keys of the dictionary, while the other column(s)
2503        will be the corresponding values.  The keys will be named tuples
2504        if the table has a composite primary key.  The rows will be also
2505        named tuples unless the 'scalar' option has been set to True.
2506        With the optional parameter 'keyname' you can specify an alternative
2507        set of columns to be used as the keys of the dictionary.  It must
2508        be set as a string, list or a tuple.
2509
2510        If the Python version supports it, the dictionary will be an
2511        OrderedDict using the order specified with the 'order' parameter
2512        or the key column(s) if not specified.  You can set 'order' to False
2513        if you don't care about the ordering.  In this case the returned
2514        dictionary will be an ordinary one.
2515        """
2516        if not table:
2517            raise TypeError('The table name is missing')
2518        if not keyname:
2519            try:
2520                keyname = self.pkey(table, True)
2521            except (KeyError, ProgrammingError):
2522                raise _prg_error('Table %s has no primary key' % table)
2523        if isinstance(keyname, basestring):
2524            keyname = [keyname]
2525        elif not isinstance(keyname, (list, tuple)):
2526            raise KeyError('The keyname must be a string, list or tuple')
2527        if what:
2528            if isinstance(what, (list, tuple)):
2529                what = ', '.join(map(str, what))
2530            if order is None:
2531                order = what
2532        else:
2533            what = '*'
2534        q = ['SELECT', what, 'FROM', table]
2535        if where:
2536            if isinstance(where, (list, tuple)):
2537                where = ' AND '.join(map(str, where))
2538            q.extend(['WHERE', where])
2539        if order is None:
2540            order = keyname
2541        if order:
2542            if isinstance(order, (list, tuple)):
2543                order = ', '.join(map(str, order))
2544            q.extend(['ORDER BY', order])
2545        if limit:
2546            q.append('LIMIT %d' % limit)
2547        if offset:
2548            q.append('OFFSET %d' % offset)
2549        q = ' '.join(q)
2550        self._do_debug(q)
2551        q = self.db.query(q)
2552        res = q.getresult()
2553        cls = OrderedDict if order else dict
2554        if not res:
2555            return cls()
2556        keyset = set(keyname)
2557        fields = q.listfields()
2558        if not keyset.issubset(fields):
2559            raise KeyError('Missing keyname in row')
2560        keyind, rowind = [], []
2561        for i, f in enumerate(fields):
2562            (keyind if f in keyset else rowind).append(i)
2563        keytuple = len(keyind) > 1
2564        getkey = itemgetter(*keyind)
2565        keys = map(getkey, res)
2566        if scalar:
2567            rowind = rowind[:1]
2568            rowtuple = False
2569        else:
2570            rowtuple = len(rowind) > 1
2571        if scalar or rowtuple:
2572            getrow = itemgetter(*rowind)
2573        else:
2574            rowind = rowind[0]
2575            getrow = lambda row: (row[rowind],)
2576            rowtuple = True
2577        rows = map(getrow, res)
2578        if keytuple or rowtuple:
2579            namedresult = get_namedresult()
2580            if namedresult:
2581                if keytuple:
2582                    keys = namedresult(_MemoryQuery(keys, keyname))
2583                if rowtuple:
2584                    fields = [f for f in fields if f not in keyset]
2585                    rows = namedresult(_MemoryQuery(rows, fields))
2586        return cls(zip(keys, rows))
2587
2588    def notification_handler(self,
2589            event, callback, arg_dict=None, timeout=None, stop_event=None):
2590        """Get notification handler that will run the given callback."""
2591        return NotificationHandler(self,
2592            event, callback, arg_dict, timeout, stop_event)
2593
2594
2595# if run as script, print some information
2596
2597if __name__ == '__main__':
2598    print('PyGreSQL version' + version)
2599    print('')
2600    print(__doc__)
Note: See TracBrowser for help on using the repository browser.