source: branches/4.x/pg.py @ 745

Last change on this file since 745 was 745, checked in by cito, 4 years ago

Add methods get/set_parameter to DB wrapper class

These methods can be used to get/set/reset run-time parameters,
even several at once.

Since this is pretty useful and will not break anything, I have
also back ported these additions to the 4.x branch.

Everything is well documented and tested, of course.

  • Property svn:keywords set to Id
File size: 39.2 KB
Line 
1#! /usr/bin/python
2#
3# pg.py
4#
5# $Id: pg.py 745 2016-01-15 01:16:23Z cito $
6#
7
8"""PyGreSQL classic interface.
9
10This pg module implements some basic database management stuff.
11It includes the _pg module and builds on it, providing the higher
12level wrapper class named DB with additional functionality.
13This is known as the "classic" ("old style") PyGreSQL interface.
14For a DB-API 2 compliant interface use the newer pgdb module.
15
16"""
17
18# Copyright (c) 1997-2016 by D'Arcy J.M. Cain.
19#
20# Contributions made by Ch. Zwerschke and others.
21#
22# The notification handler is based on pgnotify which is
23# Copyright (c) 2001 Ng Pheng Siong. All rights reserved.
24#
25# Permission to use, copy, modify, and distribute this software and its
26# documentation for any purpose and without fee is hereby granted,
27# provided that the above copyright notice appear in all copies and that
28# both that copyright notice and this permission notice appear in
29# supporting documentation.
30
31from _pg import *
32
33import select
34import warnings
35try:
36    frozenset
37except NameError:  # Python < 2.4, unsupported
38    from sets import ImmutableSet as frozenset
39try:
40    from decimal import Decimal
41    set_decimal(Decimal)
42except ImportError:  # Python < 2.4, unsupported
43    Decimal = float
44try:
45    from collections import namedtuple
46except ImportError:  # Python < 2.6
47    namedtuple = None
48
49
50# Auxiliary functions that are independent from a DB connection:
51
52def _is_quoted(s):
53    """Check whether this string is a quoted identifier."""
54    s = s.replace('_', 'a')
55    return not s.isalnum() or s[:1].isdigit() or s != s.lower()
56
57
58def _is_unquoted(s):
59    """Check whether this string is an unquoted identifier."""
60    s = s.replace('_', 'a')
61    return s.isalnum() and not s[:1].isdigit()
62
63
64def _split_first_part(s):
65    """Split the first part of a dot separated string."""
66    s = s.lstrip()
67    if s[:1] == '"':
68        p = []
69        s = s.split('"', 3)[1:]
70        p.append(s[0])
71        while len(s) == 3 and s[1] == '':
72            p.append('"')
73            s = s[2].split('"', 2)
74            p.append(s[0])
75        p = [''.join(p)]
76        s = '"'.join(s[1:]).lstrip()
77        if s:
78            if s[:0] == '.':
79                p.append(s[1:])
80            else:
81                s = _split_first_part(s)
82                p[0] += s[0]
83                if len(s) > 1:
84                    p.append(s[1])
85    else:
86        p = s.split('.', 1)
87        s = p[0].rstrip()
88        if _is_unquoted(s):
89            s = s.lower()
90        p[0] = s
91    return p
92
93
94def _split_parts(s):
95    """Split all parts of a dot separated string."""
96    q = []
97    while s:
98        s = _split_first_part(s)
99        q.append(s[0])
100        if len(s) < 2:
101            break
102        s = s[1]
103    return q
104
105
106def _join_parts(s):
107    """Join all parts of a dot separated string."""
108    return '.'.join([_is_quoted(p) and '"%s"' % p or p for p in s])
109
110
111def _oid_key(qcl):
112    """Build oid key from qualified class name."""
113    return 'oid(%s)' % qcl
114
115
116if namedtuple:
117
118    def _namedresult(q):
119        """Get query result as named tuples."""
120        row = namedtuple('Row', q.listfields())
121        return [row(*r) for r in q.getresult()]
122
123    set_namedresult(_namedresult)
124
125
126def _db_error(msg, cls=DatabaseError):
127    """Returns DatabaseError with empty sqlstate attribute."""
128    error = cls(msg)
129    error.sqlstate = None
130    return error
131
132
133def _int_error(msg):
134    """Returns InternalError."""
135    return _db_error(msg, InternalError)
136
137
138def _prg_error(msg):
139    """Returns ProgrammingError."""
140    return _db_error(msg, ProgrammingError)
141
142
143class NotificationHandler(object):
144    """A PostgreSQL client-side asynchronous notification handler."""
145
146    def __init__(self, db, event, callback, arg_dict=None, timeout=None):
147        """Initialize the notification handler.
148
149        db       - PostgreSQL connection object.
150        event    - Event (notification channel) to LISTEN for.
151        callback - Event callback function.
152        arg_dict - A dictionary passed as the argument to the callback.
153        timeout  - Timeout in seconds; a floating point number denotes
154                   fractions of seconds. If it is absent or None, the
155                   callers will never time out.
156
157        """
158        if isinstance(db, DB):
159            db = db.db
160        self.db = db
161        self.event = event
162        self.stop_event = 'stop_%s' % event
163        self.listening = False
164        self.callback = callback
165        if arg_dict is None:
166            arg_dict = {}
167        self.arg_dict = arg_dict
168        self.timeout = timeout
169
170    def __del__(self):
171        self.close()
172
173    def close(self):
174        """Stop listening and close the connection."""
175        if self.db:
176            self.unlisten()
177            self.db.close()
178            self.db = None
179
180    def listen(self):
181        """Start listening for the event and the stop event."""
182        if not self.listening:
183            self.db.query('listen "%s"' % self.event)
184            self.db.query('listen "%s"' % self.stop_event)
185            self.listening = True
186
187    def unlisten(self):
188        """Stop listening for the event and the stop event."""
189        if self.listening:
190            self.db.query('unlisten "%s"' % self.event)
191            self.db.query('unlisten "%s"' % self.stop_event)
192            self.listening = False
193
194    def notify(self, db=None, stop=False, payload=None):
195        """Generate a notification.
196
197        Note: If the main loop is running in another thread, you must pass
198        a different database connection to avoid a collision.
199
200        The payload parameter is only supported in PostgreSQL >= 9.0.
201
202        """
203        if not db:
204            db = self.db
205        if self.listening:
206            q = 'notify "%s"' % (stop and self.stop_event or self.event)
207            if payload:
208                q += ", '%s'" % payload
209            return db.query(q)
210
211    def __call__(self, close=False):
212        """Invoke the notification handler.
213
214        The handler is a loop that actually LISTENs for two NOTIFY messages:
215
216        <event> and stop_<event>.
217
218        When either of these NOTIFY messages are received, its associated
219        'pid' and 'event' are inserted into <arg_dict>, and the callback is
220        invoked with <arg_dict>. If the NOTIFY message is stop_<event>, the
221        handler UNLISTENs both <event> and stop_<event> and exits.
222
223        Note: If you run this loop in another thread, don't use the same
224        database connection for database operations in the main thread.
225
226        """
227        self.listen()
228        _ilist = [self.db.fileno()]
229
230        while self.listening:
231            ilist, _olist, _elist = select.select(_ilist, [], [], self.timeout)
232            if ilist:
233                while self.listening:
234                    notice = self.db.getnotify()
235                    if not notice:  # no more messages
236                        break
237                    event, pid, extra = notice
238                    if event not in (self.event, self.stop_event):
239                        self.unlisten()
240                        raise _db_error(
241                            'listening for "%s" and "%s", but notified of "%s"'
242                            % (self.event, self.stop_event, event))
243                    if event == self.stop_event:
244                        self.unlisten()
245                    self.arg_dict['pid'] = pid
246                    self.arg_dict['event'] = event
247                    self.arg_dict['extra'] = extra
248                    self.callback(self.arg_dict)
249            else:   # we timed out
250                self.unlisten()
251                self.callback(None)
252
253
254def pgnotify(*args, **kw):
255    """Same as NotificationHandler, under the traditional name."""
256    warnings.warn("pgnotify is deprecated, use NotificationHandler instead.",
257        DeprecationWarning, stacklevel=2)
258    return NotificationHandler(*args, **kw)
259
260
261# The actual PostGreSQL database connection interface:
262
263class DB(object):
264    """Wrapper class for the _pg connection type."""
265
266    def __init__(self, *args, **kw):
267        """Create a new connection.
268
269        You can pass either the connection parameters or an existing
270        _pg or pgdb connection. This allows you to use the methods
271        of the classic pg interface with a DB-API 2 pgdb connection.
272
273        """
274        if not args and len(kw) == 1:
275            db = kw.get('db')
276        elif not kw and len(args) == 1:
277            db = args[0]
278        else:
279            db = None
280        if db:
281            if isinstance(db, DB):
282                db = db.db
283            else:
284                try:
285                    db = db._cnx
286                except AttributeError:
287                    pass
288        if not db or not hasattr(db, 'db') or not hasattr(db, 'query'):
289            db = connect(*args, **kw)
290            self._closeable = True
291        else:
292            self._closeable = False
293        self.db = db
294        self.dbname = db.db
295        self._regtypes = False
296        self._attnames = {}
297        self._pkeys = {}
298        self._privileges = {}
299        self._args = args, kw
300        self.debug = None  # For debugging scripts, this can be set
301            # * to a string format specification (e.g. in CGI set to "%s<BR>"),
302            # * to a file object to write debug statements or
303            # * to a callable object which takes a string argument
304            # * to any other true value to just print debug statements
305
306    def __getattr__(self, name):
307        # All undefined members are same as in underlying connection:
308        if self.db:
309            return getattr(self.db, name)
310        else:
311            raise _int_error('Connection is not valid')
312
313    # Context manager methods
314
315    def __enter__(self):
316        """Enter the runtime context. This will start a transaction."""
317        self.begin()
318        return self
319
320    def __exit__(self, et, ev, tb):
321        """Exit the runtime context. This will end the transaction."""
322        if et is None and ev is None and tb is None:
323            self.commit()
324        else:
325            self.rollback()
326
327    # Auxiliary methods
328
329    def _do_debug(self, s):
330        """Print a debug message."""
331        if self.debug:
332            if isinstance(self.debug, basestring):
333                print(self.debug % s)
334            elif isinstance(self.debug, file):
335                self.debug.write(s + '\n')
336            elif callable(self.debug):
337                self.debug(s)
338            else:
339                print(s)
340
341    def _make_bool(d):
342        """Get boolean value corresponding to d."""
343        if get_bool():
344            return bool(d)
345        return d and 't' or 'f'
346    _make_bool = staticmethod(_make_bool)
347
348    def _quote_text(self, d):
349        """Quote text value."""
350        if not isinstance(d, basestring):
351            d = str(d)
352        return "'%s'" % self.escape_string(d)
353
354    _bool_true = frozenset('t true 1 y yes on'.split())
355
356    def _quote_bool(self, d):
357        """Quote boolean value."""
358        if isinstance(d, basestring):
359            if not d:
360                return 'NULL'
361            d = d.lower() in self._bool_true
362        return d and "'t'" or "'f'"
363
364    _date_literals = frozenset('current_date current_time'
365        ' current_timestamp localtime localtimestamp'.split())
366
367    def _quote_date(self, d):
368        """Quote date value."""
369        if not d:
370            return 'NULL'
371        if isinstance(d, basestring) and d.lower() in self._date_literals:
372            return d
373        return self._quote_text(d)
374
375    def _quote_num(self, d):
376        """Quote numeric value."""
377        if not d and d != 0:
378            return 'NULL'
379        return str(d)
380
381    def _quote_money(self, d):
382        """Quote money value."""
383        if d is None or d == '':
384            return 'NULL'
385        if not isinstance(d, basestring):
386            d = str(d)
387        return d
388
389    _quote_funcs = dict(  # quote methods for each type
390        text=_quote_text, bool=_quote_bool, date=_quote_date,
391        int=_quote_num, num=_quote_num, float=_quote_num,
392        money=_quote_money)
393
394    def _quote(self, d, t):
395        """Return quotes if needed."""
396        if d is None:
397            return 'NULL'
398        try:
399            quote_func = self._quote_funcs[t]
400        except KeyError:
401            quote_func = self._quote_funcs['text']
402        return quote_func(self, d)
403
404    def _split_schema(self, cl):
405        """Return schema and name of object separately.
406
407        This auxiliary function splits off the namespace (schema)
408        belonging to the class with the name cl. If the class name
409        is not qualified, the function is able to determine the schema
410        of the class, taking into account the current search path.
411
412        """
413        s = _split_parts(cl)
414        if len(s) > 1:  # name already qualified?
415            # should be database.schema.table or schema.table
416            if len(s) > 3:
417                raise _prg_error('Too many dots in class name %s' % cl)
418            schema, cl = s[-2:]
419        else:
420            cl = s[0]
421            # determine search path
422            q = 'SELECT current_schemas(TRUE)'
423            schemas = self.db.query(q).getresult()[0][0][1:-1].split(',')
424            if schemas:  # non-empty path
425                # search schema for this object in the current search path
426                # (we could also use unnest with ordinality here to spare
427                # one query, but this is only possible since PostgreSQL 9.4)
428                q = ' UNION '.join(
429                    ["SELECT %d::integer AS n, '%s'::name AS nspname"
430                        % s for s in enumerate(schemas)])
431                q = ("SELECT nspname FROM pg_class r"
432                    " JOIN pg_namespace s ON r.relnamespace = s.oid"
433                    " JOIN (%s) AS p USING (nspname)"
434                    " WHERE r.relname = $1 ORDER BY n LIMIT 1" % q)
435                schema = self.db.query(q, (cl,)).getresult()
436                if schema:  # schema found
437                    schema = schema[0][0]
438                else:  # object not found in current search path
439                    schema = 'public'
440            else:  # empty path
441                schema = 'public'
442        return schema, cl
443
444    def _add_schema(self, cl):
445        """Ensure that the class name is prefixed with a schema name."""
446        return _join_parts(self._split_schema(cl))
447
448    # Public methods
449
450    # escape_string and escape_bytea exist as methods,
451    # so we define unescape_bytea as a method as well
452    unescape_bytea = staticmethod(unescape_bytea)
453
454    def close(self):
455        """Close the database connection."""
456        # Wraps shared library function so we can track state.
457        if self._closeable:
458            if self.db:
459                self.db.close()
460                self.db = None
461            else:
462                raise _int_error('Connection already closed')
463
464    def reset(self):
465        """Reset connection with current parameters.
466
467        All derived queries and large objects derived from this connection
468        will not be usable after this call.
469
470        """
471        if self.db:
472            self.db.reset()
473        else:
474            raise _int_error('Connection already closed')
475
476    def reopen(self):
477        """Reopen connection to the database.
478
479        Used in case we need another connection to the same database.
480        Note that we can still reopen a database that we have closed.
481
482        """
483        # There is no such shared library function.
484        if self._closeable:
485            db = connect(*self._args[0], **self._args[1])
486            if self.db:
487                self.db.close()
488            self.db = db
489
490    def begin(self, mode=None):
491        """Begin a transaction."""
492        qstr = 'BEGIN'
493        if mode:
494            qstr += ' ' + mode
495        return self.query(qstr)
496
497    start = begin
498
499    def commit(self):
500        """Commit the current transaction."""
501        return self.query('COMMIT')
502
503    end = commit
504
505    def rollback(self, name=None):
506        """Roll back the current transaction."""
507        qstr = 'ROLLBACK'
508        if name:
509            qstr += ' TO ' + name
510        return self.query(qstr)
511
512    def savepoint(self, name):
513        """Define a new savepoint within the current transaction."""
514        return self.query('SAVEPOINT ' + name)
515
516    def release(self, name):
517        """Destroy a previously defined savepoint."""
518        return self.query('RELEASE ' + name)
519
520    def get_parameter(self, parameter):
521        """Get the value of a run-time parameter.
522
523        If the parameter is a string, the return value will also be a string
524        that is the current setting of the run-time parameter with that name.
525
526        You can get several parameters at once by passing a list or tuple of
527        parameter names.  The return value will then be a corresponding list
528        of parameter settings.  If you pass a dict as parameter instead, its
529        values will be set to the parameter settings corresponding to its keys.
530
531        By passing the special name 'all' as the parameter, you can get a dict
532        of all existing configuration parameters.
533        """
534        if isinstance(parameter, basestring):
535            parameter = [parameter]
536            values = None
537        elif isinstance(parameter, (list, tuple)):
538            values = []
539        elif isinstance(parameter, dict):
540            values = parameter
541        else:
542            raise TypeError('The parameter must be a dict, list or string')
543        if not parameter:
544            raise TypeError('No parameter has been specified')
545        if isinstance(values, dict):
546            params = {}
547        else:
548            params = []
549        for key in parameter:
550            if isinstance(key, basestring):
551                param = key.strip().lower()
552            else:
553                param = None
554            if not param:
555                raise TypeError('Invalid parameter')
556            if param == 'all':
557                q = 'SHOW ALL'
558                values = self.db.query(q).getresult()
559                values = dict(value[:2] for value in values)
560                break
561            if isinstance(values, dict):
562                params[param] = key
563            else:
564                params.append(param)
565        else:
566            for param in params:
567                q = 'SHOW %s' % (param,)
568                value = self.db.query(q).getresult()[0][0]
569                if values is None:
570                    values = value
571                elif isinstance(values, list):
572                    values.append(value)
573                else:
574                    values[params[param]] = value
575        return values
576
577    def set_parameter(self, parameter, value=None, local=False):
578        """Set the value of a run-time parameter.
579
580        If the parameter and the value are strings, the run-time parameter
581        will be set to that value.  If no value or None is passed as a value,
582        then the run-time parameter will be restored to its default value.
583
584        You can set several parameters at once by passing a list or tuple
585        of parameter names, with a single value that all parameters should
586        be set to or with a corresponding list or tuple of values.
587
588        You can also pass a dict as parameters.  In this case, you should
589        not pass a value, since the values will be taken from the dict.
590
591        By passing the special name 'all' as the parameter, you can reset
592        all existing settable run-time parameters to their default values.
593
594        If you set local to True, then the command takes effect for only the
595        current transaction.  After commit() or rollback(), the session-level
596        setting takes effect again.  Setting local to True will appear to
597        have no effect if it is executed outside a transaction, since the
598        transaction will end immediately.
599        """
600        if isinstance(parameter, basestring):
601            parameter = {parameter: value}
602        elif isinstance(parameter, (list, tuple)):
603            if isinstance(value, (list, tuple)):
604                parameter = dict(zip(parameter, value))
605            else:
606                parameter = dict.fromkeys(parameter, value)
607        elif isinstance(parameter, dict):
608            if value is not None:
609                raise ValueError(
610                    'A value must not be set when parameter is a dictionary')
611        else:
612            raise TypeError('The parameter must be a dict, list or string')
613        if not parameter:
614            raise TypeError('No parameter has been specified')
615        params = {}
616        for key, value in parameter.items():
617            if isinstance(key, basestring):
618                param = key.strip().lower()
619            else:
620                param = None
621            if not param:
622                raise TypeError('Invalid parameter')
623            if param == 'all':
624                if value is not None:
625                    raise ValueError(
626                        "A value must ot be set when parameter is 'all'")
627                params = {'all': None}
628                break
629            params[param] = value
630        local = local and ' LOCAL' or ''
631        for param, value in params.items():
632            if value is None:
633                q = 'RESET%s %s' % (local, param)
634            else:
635                q = 'SET%s %s TO %s' % (local, param, value)
636            self._do_debug(q)
637            self.db.query(q)
638
639    def query(self, qstr, *args):
640        """Executes a SQL command string.
641
642        This method simply sends a SQL query to the database. If the query is
643        an insert statement that inserted exactly one row into a table that
644        has OIDs, the return value is the OID of the newly inserted row.
645        If the query is an update or delete statement, or an insert statement
646        that did not insert exactly one row in a table with OIDs, then the
647        number of rows affected is returned as a string. If it is a statement
648        that returns rows as a result (usually a select statement, but maybe
649        also an "insert/update ... returning" statement), this method returns
650        a pgqueryobject that can be accessed via getresult() or dictresult()
651        or simply printed. Otherwise, it returns `None`.
652
653        The query can contain numbered parameters of the form $1 in place
654        of any data constant. Arguments given after the query string will
655        be substituted for the corresponding numbered parameter. Parameter
656        values can also be given as a single list or tuple argument.
657
658        Note that the query string must not be passed as a unicode value,
659        but you can pass arguments as unicode values if they can be decoded
660        using the current client encoding.
661
662        """
663        # Wraps shared library function for debugging.
664        if not self.db:
665            raise _int_error('Connection is not valid')
666        self._do_debug(qstr)
667        return self.db.query(qstr, args)
668
669    def pkey(self, cl, newpkey=None):
670        """This method gets or sets the primary key of a class.
671
672        Composite primary keys are represented as frozensets. Note that
673        this raises a KeyError if the table does not have a primary key.
674
675        If newpkey is set and is not a dictionary then set that
676        value as the primary key of the class.  If it is a dictionary
677        then replace the internal cache of primary keys with a copy of it.
678
679        """
680        # First see if the caller is supplying a dictionary
681        if isinstance(newpkey, dict):
682            # make sure that all classes have a namespace
683            self._pkeys = dict([
684                ('.' in cl and cl or 'public.' + cl, pkey)
685                for cl, pkey in newpkey.items()])
686            return self._pkeys
687
688        qcl = self._add_schema(cl)  # build fully qualified class name
689        # Check if the caller is supplying a new primary key for the class
690        if newpkey:
691            self._pkeys[qcl] = newpkey
692            return newpkey
693
694        # Get all the primary keys at once
695        if qcl not in self._pkeys:
696            # if not found, check again in case it was added after we started
697            self._pkeys = {}
698            if self.server_version >= 80200:
699                # the ANY syntax works correctly only with PostgreSQL >= 8.2
700                any_indkey = "= ANY (i.indkey)"
701            else:
702                any_indkey = "IN (%s)" % ', '.join(
703                    ['i.indkey[%d]' % i for i in range(16)])
704            q = ("SELECT s.nspname, r.relname, a.attname"
705                " FROM pg_class r"
706                " JOIN pg_namespace s ON s.oid = r.relnamespace"
707                " AND s.nspname NOT SIMILAR"
708                " TO 'pg/_%|information/_schema' ESCAPE '/'"
709                " JOIN pg_attribute a ON a.attrelid = r.oid"
710                " AND NOT a.attisdropped"
711                " JOIN pg_index i ON i.indrelid = r.oid"
712                " AND i.indisprimary AND a.attnum " + any_indkey)
713            for r in self.db.query(q).getresult():
714                cl, pkey = _join_parts(r[:2]), r[2]
715                self._pkeys.setdefault(cl, []).append(pkey)
716            # (only) for composite primary keys, the values will be frozensets
717            for cl, pkey in self._pkeys.items():
718                self._pkeys[cl] = len(pkey) > 1 and frozenset(pkey) or pkey[0]
719            self._do_debug(self._pkeys)
720
721        # will raise an exception if primary key doesn't exist
722        return self._pkeys[qcl]
723
724    def get_databases(self):
725        """Get list of databases in the system."""
726        return [s[0] for s in
727            self.db.query('SELECT datname FROM pg_database').getresult()]
728
729    def get_relations(self, kinds=None):
730        """Get list of relations in connected database of specified kinds.
731
732        If kinds is None or empty, all kinds of relations are returned.
733        Otherwise kinds can be a string or sequence of type letters
734        specifying which kind of relations you want to list.
735
736        """
737        where = kinds and " AND r.relkind IN (%s)" % ','.join(
738            ["'%s'" % k for k in kinds]) or ''
739        q = ("SELECT s.nspname, r.relname"
740            " FROM pg_class r"
741            " JOIN pg_namespace s ON s.oid = r.relnamespace"
742            " WHERE s.nspname NOT SIMILAR"
743            " TO 'pg/_%%|information/_schema' ESCAPE '/' %s"
744            " ORDER BY 1, 2") % where
745        return [_join_parts(r) for r in self.db.query(q).getresult()]
746
747    def get_tables(self):
748        """Return list of tables in connected database."""
749        return self.get_relations('r')
750
751    def get_attnames(self, cl, newattnames=None):
752        """Given the name of a table, digs out the set of attribute names.
753
754        Returns a dictionary of attribute names (the names are the keys,
755        the values are the names of the attributes' types).
756        If the optional newattnames exists, it must be a dictionary and
757        will become the new attribute names dictionary.
758
759        By default, only a limited number of simple types will be returned.
760        You can get the regular types after calling use_regtypes(True).
761
762        """
763        if isinstance(newattnames, dict):
764            self._attnames = newattnames
765            return
766        elif newattnames:
767            raise _prg_error('If supplied, newattnames must be a dictionary')
768        cl = self._split_schema(cl)  # split into schema and class
769        qcl = _join_parts(cl)  # build fully qualified name
770        # May as well cache them:
771        if qcl in self._attnames:
772            return self._attnames[qcl]
773        if qcl not in self.get_relations('rv'):
774            raise _prg_error('Class %s does not exist' % qcl)
775
776        q = ("SELECT a.attname, t.typname%s"
777            " FROM pg_class r"
778            " JOIN pg_namespace s ON r.relnamespace = s.oid"
779            " JOIN pg_attribute a ON a.attrelid = r.oid"
780            " JOIN pg_type t ON t.oid = a.atttypid"
781            " WHERE s.nspname = $1 AND r.relname = $2"
782            " AND (a.attnum > 0 OR a.attname = 'oid')"
783            " AND NOT a.attisdropped") % (
784                self._regtypes and '::regtype' or '',)
785        q = self.db.query(q, cl).getresult()
786
787        if self._regtypes:
788            t = dict(q)
789        else:
790            t = {}
791            for att, typ in q:
792                if typ.startswith('bool'):
793                    typ = 'bool'
794                elif typ.startswith('abstime'):
795                    typ = 'date'
796                elif typ.startswith('date'):
797                    typ = 'date'
798                elif typ.startswith('interval'):
799                    typ = 'date'
800                elif typ.startswith('timestamp'):
801                    typ = 'date'
802                elif typ.startswith('oid'):
803                    typ = 'int'
804                elif typ.startswith('int'):
805                    typ = 'int'
806                elif typ.startswith('float'):
807                    typ = 'float'
808                elif typ.startswith('numeric'):
809                    typ = 'num'
810                elif typ.startswith('money'):
811                    typ = 'money'
812                else:
813                    typ = 'text'
814                t[att] = typ
815
816        self._attnames[qcl] = t  # cache it
817        return self._attnames[qcl]
818
819    def use_regtypes(self, regtypes=None):
820        """Use regular type names instead of simplified type names."""
821        if regtypes is None:
822            return self._regtypes
823        else:
824            regtypes = bool(regtypes)
825            if regtypes != self._regtypes:
826                self._regtypes = regtypes
827                self._attnames.clear()
828            return regtypes
829
830    def has_table_privilege(self, cl, privilege='select'):
831        """Check whether current user has specified table privilege."""
832        qcl = self._add_schema(cl)
833        privilege = privilege.lower()
834        try:
835            return self._privileges[(qcl, privilege)]
836        except KeyError:
837            q = "SELECT has_table_privilege($1, $2)"
838            q = self.db.query(q, (qcl, privilege))
839            ret = q.getresult()[0][0] == self._make_bool(True)
840            self._privileges[(qcl, privilege)] = ret
841            return ret
842
843    def get(self, cl, arg, keyname=None):
844        """Get a row from a database table or view.
845
846        This method is the basic mechanism to get a single row.  The keyname
847        that the key specifies a unique row.  If keyname is not specified
848        then the primary key for the table is used.  If arg is a dictionary
849        then the value for the key is taken from it and it is modified to
850        include the new values, replacing existing values where necessary.
851        For a composite key, keyname can also be a sequence of key names.
852        The OID is also put into the dictionary if the table has one, but
853        in order to allow the caller to work with multiple tables, it is
854        munged as oid(schema.table).
855
856        """
857        if cl.endswith('*'):  # scan descendant tables?
858            cl = cl[:-1].rstrip()  # need parent table name
859        # build qualified class name
860        qcl = self._add_schema(cl)
861        # To allow users to work with multiple tables,
862        # we munge the name of the "oid" key
863        qoid = _oid_key(qcl)
864        if not keyname:
865            # use the primary key by default
866            try:
867                keyname = self.pkey(qcl)
868            except KeyError:
869                raise _prg_error('Class %s has no primary key' % qcl)
870        # We want the oid for later updates if that isn't the key
871        if keyname == 'oid':
872            if isinstance(arg, dict):
873                if qoid not in arg:
874                    raise _db_error('%s not in arg' % qoid)
875            else:
876                arg = {qoid: arg}
877            where = 'oid = %s' % arg[qoid]
878            attnames = '*'
879        else:
880            attnames = self.get_attnames(qcl)
881            if isinstance(keyname, basestring):
882                keyname = (keyname,)
883            if not isinstance(arg, dict):
884                if len(keyname) > 1:
885                    raise _prg_error('Composite key needs dict as arg')
886                arg = dict([(k, arg) for k in keyname])
887            where = ' AND '.join(['%s = %s'
888                % (k, self._quote(arg[k], attnames[k])) for k in keyname])
889            attnames = ', '.join(attnames)
890        q = 'SELECT %s FROM %s WHERE %s LIMIT 1' % (attnames, qcl, where)
891        self._do_debug(q)
892        res = self.db.query(q).dictresult()
893        if not res:
894            raise _db_error('No such record in %s where %s' % (qcl, where))
895        for att, value in res[0].items():
896            arg[att == 'oid' and qoid or att] = value
897        return arg
898
899    def insert(self, cl, d=None, **kw):
900        """Insert a row into a database table.
901
902        This method inserts a row into a table.  The name of the table must
903        be passed as the first parameter.  The other parameters are used for
904        providing the data of the row that shall be inserted into the table.
905        If a dictionary is supplied as the second parameter, it starts with
906        that.  Otherwise it uses a blank dictionary. Either way the dictionary
907        is updated from the keywords.
908
909        The dictionary is then, if possible, reloaded with the values actually
910        inserted in order to pick up values modified by rules, triggers, etc.
911
912        Note: The method currently doesn't support insert into views
913        although PostgreSQL does.
914
915        """
916        qcl = self._add_schema(cl)
917        qoid = _oid_key(qcl)
918        if d is None:
919            d = {}
920        d.update(kw)
921        attnames = self.get_attnames(qcl)
922        names, values = [], []
923        for n in attnames:
924            if n != 'oid' and n in d:
925                names.append('"%s"' % n)
926                values.append(self._quote(d[n], attnames[n]))
927        names, values = ', '.join(names), ', '.join(values)
928        selectable = self.has_table_privilege(qcl)
929        if selectable and self.server_version >= 80200:
930            ret = ' RETURNING %s*' % ('oid' in attnames and 'oid, ' or '')
931        else:
932            ret = ''
933        q = 'INSERT INTO %s (%s) VALUES (%s)%s' % (qcl, names, values, ret)
934        self._do_debug(q)
935        res = self.db.query(q)
936        if ret:
937            res = res.dictresult()
938            for att, value in res[0].items():
939                d[att == 'oid' and qoid or att] = value
940        elif isinstance(res, int):
941            d[qoid] = res
942            if selectable:
943                self.get(qcl, d, 'oid')
944        elif selectable:
945            if qoid in d:
946                self.get(qcl, d, 'oid')
947            else:
948                try:
949                    self.get(qcl, d)
950                except ProgrammingError:
951                    pass  # table has no primary key
952        return d
953
954    def update(self, cl, d=None, **kw):
955        """Update an existing row in a database table.
956
957        Similar to insert but updates an existing row.  The update is based
958        on the OID value as munged by get or passed as keyword, or on the
959        primary key of the table.  The dictionary is modified, if possible,
960        to reflect any changes caused by the update due to triggers, rules,
961        default values, etc.
962
963        """
964        # Update always works on the oid which get() returns if available,
965        # otherwise use the primary key.  Fail if neither.
966        # Note that we only accept oid key from named args for safety.
967        qcl = self._add_schema(cl)
968        qoid = _oid_key(qcl)
969        if 'oid' in kw:
970            kw[qoid] = kw['oid']
971            del kw['oid']
972        if d is None:
973            d = {}
974        d.update(kw)
975        attnames = self.get_attnames(qcl)
976        if qoid in d:
977            where = 'oid = %s' % d[qoid]
978            keyname = ()
979        else:
980            try:
981                keyname = self.pkey(qcl)
982            except KeyError:
983                raise _prg_error('Class %s has no primary key' % qcl)
984            if isinstance(keyname, basestring):
985                keyname = (keyname,)
986            try:
987                where = ' AND '.join(['%s = %s'
988                    % (k, self._quote(d[k], attnames[k])) for k in keyname])
989            except KeyError:
990                raise _prg_error('Update needs primary key or oid.')
991        values = []
992        for n in attnames:
993            if n in d and n not in keyname:
994                values.append('%s = %s' % (n, self._quote(d[n], attnames[n])))
995        if not values:
996            return d
997        values = ', '.join(values)
998        selectable = self.has_table_privilege(qcl)
999        if selectable and self.server_version >= 80200:
1000            ret = ' RETURNING %s*' % ('oid' in attnames and 'oid, ' or '')
1001        else:
1002            ret = ''
1003        q = 'UPDATE %s SET %s WHERE %s%s' % (qcl, values, where, ret)
1004        self._do_debug(q)
1005        res = self.db.query(q)
1006        if ret:
1007            res = res.dictresult()[0]
1008            for att, value in res.items():
1009                d[att == 'oid' and qoid or att] = value
1010        else:
1011            if selectable:
1012                if qoid in d:
1013                    self.get(qcl, d, 'oid')
1014                else:
1015                    self.get(qcl, d)
1016        return d
1017
1018    def clear(self, cl, d=None):
1019        """Clear all the attributes to values determined by the types.
1020
1021        Numeric types are set to 0, Booleans are set to false, and everything
1022        else is set to the empty string.  If the second argument is present,
1023        it is used as the row dictionary and any entries matching attribute
1024        names are cleared with everything else left unchanged.
1025
1026        """
1027        # At some point we will need a way to get defaults from a table.
1028        qcl = self._add_schema(cl)
1029        if d is None:
1030            d = {}  # empty if argument is not present
1031        attnames = self.get_attnames(qcl)
1032        for n, t in attnames.items():
1033            if n == 'oid':
1034                continue
1035            if t in ('int', 'integer', 'smallint', 'bigint',
1036                    'float', 'real', 'double precision',
1037                    'num', 'numeric', 'money'):
1038                d[n] = 0
1039            elif t in ('bool', 'boolean'):
1040                d[n] = self._make_bool(False)
1041            else:
1042                d[n] = ''
1043        return d
1044
1045    def delete(self, cl, d=None, **kw):
1046        """Delete an existing row in a database table.
1047
1048        This method deletes the row from a table.  It deletes based on the
1049        OID value as munged by get or passed as keyword, or on the primary
1050        key of the table.  The return value is the number of deleted rows
1051        (i.e. 0 if the row did not exist and 1 if the row was deleted).
1052
1053        """
1054        # Like update, delete works on the oid.
1055        # One day we will be testing that the record to be deleted
1056        # isn't referenced somewhere (or else PostgreSQL will).
1057        # Note that we only accept oid key from named args for safety
1058        qcl = self._add_schema(cl)
1059        qoid = _oid_key(qcl)
1060        if 'oid' in kw:
1061            kw[qoid] = kw['oid']
1062            del kw['oid']
1063        if d is None:
1064            d = {}
1065        d.update(kw)
1066        if qoid in d:
1067            where = 'oid = %s' % d[qoid]
1068        else:
1069            try:
1070                keyname = self.pkey(qcl)
1071            except KeyError:
1072                raise _prg_error('Class %s has no primary key' % qcl)
1073            if isinstance(keyname, basestring):
1074                keyname = (keyname,)
1075            attnames = self.get_attnames(qcl)
1076            try:
1077                where = ' AND '.join(['%s = %s'
1078                    % (k, self._quote(d[k], attnames[k])) for k in keyname])
1079            except KeyError:
1080                raise _prg_error('Delete needs primary key or oid.')
1081        q = 'DELETE FROM %s WHERE %s' % (qcl, where)
1082        self._do_debug(q)
1083        return int(self.db.query(q))
1084
1085    def notification_handler(self, event, callback, arg_dict={}, timeout=None):
1086        """Get notification handler that will run the given callback."""
1087        return NotificationHandler(self.db, event, callback, arg_dict, timeout)
1088
1089
1090# if run as script, print some information
1091
1092if __name__ == '__main__':
1093    print('PyGreSQL version' + version)
1094    print('')
1095    print(__doc__)
Note: See TracBrowser for help on using the repository browser.