Expand prefix specification to SQL.

def _expand_prefix_spec(self, spec, prefix = ''):
        """ Expand prefix specification to SQL.
        """

        # sanity checks
        if type(spec) is not dict:
            raise NipapInputError('invalid prefix specification')

        for key in spec.keys():
            if key not in _prefix_spec:
                raise NipapExtraneousInputError("Key '" + key + "' not allowed in prefix spec.")

        where = ""
        params = {}

        # if we have id, no other input is needed
        if 'id' in spec:
            if spec != {'id': spec['id']}:
                raise NipapExtraneousInputError("If 'id' specified, no other keys are allowed.")

        family = None
        if 'family' in spec:
            family = spec['family']
            del(spec['family'])

        # rename prefix columns
        spec2 = {}
        for k in spec:
            spec2[prefix + k] = spec[k]
        spec = spec2

        # handle keys which refer to external keys
        if prefix + 'vrf_id' in spec:
            # "translate" vrf id None to id = 0
            if spec[prefix + 'vrf_id'] is None:
                spec[prefix + 'vrf_id'] = 0

        if prefix + 'vrf_name' in spec:
            spec['vrf.name'] = spec[prefix + 'vrf_name']
            del(spec[prefix + 'vrf_name'])

        if prefix + 'vrf_rt' in spec:
            spec['vrf.rt'] = spec[prefix + 'vrf_rt']
            del(spec[prefix + 'vrf_rt'])

        if prefix + 'pool_name' in spec:
            spec['pool.name'] = spec[prefix + 'pool_name']
            del(spec[prefix + 'pool_name'])

        where, params = self._sql_expand_where(spec)

        # prefix family needs to be handled separately as it's not stored
        # explicitly in the database
        if family:
            if len(params) == 0:
                where = "family(" + prefix + "prefix) = %(family)s"
            else:
                where += " AND family(" + prefix + "prefix) = %(family)s"
            params['family'] = family

        self._logger.debug("_expand_prefix_spec; where: %s params: %s" % (where, unicode(params)))
        return where, params