Changeset 745 for trunk/pg.py


Ignore:
Timestamp:
Jan 14, 2016, 8:16:23 PM (4 years ago)
Author:
cito
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r743 r745  
    462462        """Destroy a previously defined savepoint."""
    463463        return self.query('RELEASE ' + name)
     464
     465    def get_parameter(self, parameter):
     466        """Get the value of a run-time parameter.
     467
     468        If the parameter is a string, the return value will also be a string
     469        that is the current setting of the run-time parameter with that name.
     470
     471        You can get several parameters at once by passing a list or tuple of
     472        parameter names.  The return value will then be a corresponding list
     473        of parameter settings.  If you pass a dict as parameter instead, its
     474        values will be set to the parameter settings corresponding to its keys.
     475
     476        By passing the special name 'all' as the parameter, you can get a dict
     477        of all existing configuration parameters.
     478        """
     479        if isinstance(parameter, basestring):
     480            parameter = [parameter]
     481            values = None
     482        elif isinstance(parameter, (list, tuple)):
     483            values = []
     484        elif isinstance(parameter, dict):
     485            values = parameter
     486        else:
     487            raise TypeError('The parameter must be a dict, list or string')
     488        if not parameter:
     489            raise TypeError('No parameter has been specified')
     490        params = {} if isinstance(values, dict) else []
     491        for key in parameter:
     492            param = key.strip().lower() if isinstance(
     493                key, basestring) else None
     494            if not param:
     495                raise TypeError('Invalid parameter')
     496            if param == 'all':
     497                q = 'SHOW ALL'
     498                values = self.db.query(q).getresult()
     499                values = dict(value[:2] for value in values)
     500                break
     501            if isinstance(values, dict):
     502                params[param] = key
     503            else:
     504                params.append(param)
     505        else:
     506            for param in params:
     507                q = 'SHOW %s' % (param,)
     508                value = self.db.query(q).getresult()[0][0]
     509                if values is None:
     510                    values = value
     511                elif isinstance(values, list):
     512                    values.append(value)
     513                else:
     514                    values[params[param]] = value
     515        return values
     516
     517    def set_parameter(self, parameter, value=None, local=False):
     518        """Set the value of a run-time parameter.
     519
     520        If the parameter and the value are strings, the run-time parameter
     521        will be set to that value.  If no value or None is passed as a value,
     522        then the run-time parameter will be restored to its default value.
     523
     524        You can set several parameters at once by passing a list or tuple
     525        of parameter names, with a single value that all parameters should
     526        be set to or with a corresponding list or tuple of values.
     527
     528        You can also pass a dict as parameters.  In this case, you should
     529        not pass a value, since the values will be taken from the dict.
     530
     531        By passing the special name 'all' as the parameter, you can reset
     532        all existing settable run-time parameters to their default values.
     533
     534        If you set local to True, then the command takes effect for only the
     535        current transaction.  After commit() or rollback(), the session-level
     536        setting takes effect again.  Setting local to True will appear to
     537        have no effect if it is executed outside a transaction, since the
     538        transaction will end immediately.
     539        """
     540        if isinstance(parameter, basestring):
     541            parameter = {parameter: value}
     542        elif isinstance(parameter, (list, tuple)):
     543            if isinstance(value, (list, tuple)):
     544                parameter = dict(zip(parameter, value))
     545            else:
     546                parameter = dict.fromkeys(parameter, value)
     547        elif isinstance(parameter, dict):
     548            if value is not None:
     549                raise ValueError(
     550                    'A value must not be set when parameter is a dictionary')
     551        else:
     552            raise TypeError('The parameter must be a dict, list or string')
     553        if not parameter:
     554            raise TypeError('No parameter has been specified')
     555        params = {}
     556        for key, value in parameter.items():
     557            param = key.strip().lower() if isinstance(
     558                key, basestring) else None
     559            if not param:
     560                raise TypeError('Invalid parameter')
     561            if param == 'all':
     562                if value is not None:
     563                    raise ValueError(
     564                        "A value must ot be set when parameter is 'all'")
     565                params = {'all': None}
     566                break
     567            params[param] = value
     568        local = ' LOCAL' if local else ''
     569        for param, value in params.items():
     570            if value is None:
     571                q = 'RESET%s %s' % (local, param)
     572            else:
     573                q = 'SET%s %s TO %s' % (local, param, value)
     574            self._do_debug(q)
     575            self.db.query(q)
    464576
    465577    def query(self, qstr, *args):
Note: See TracChangeset for help on using the changeset viewer.