def validate_recipient(self, cursor, address):
        if not self.config.query_validate_recipient_enabled:
            return

        try:
            result = self.query(cursor, self.config.query_validate_recipient,
                                {'address': address})
            if result != 1:
                raise exception.DatabaseQueryError(
                    '%s: Unexpected number of rows returned' % self.LABEL)
            elif cursor.fetchall()[0][0] != 1:
                raise exception.DatabaseQueryError(
                    '%s: Unable to find valid recipient entry' % self.LABEL)
        except Exception, e:
            self._log(log.info,
                      'Unable to validate recipient %s - %s' % (address, e))
            raise
 def record_response(self, cursor, sender, recipient):
     try:
         result = self.query(cursor, self.config.query_record_response, {
             'sender': sender,
             'recipient': recipient,
             'date': datetime.now(),
         })
         if result <= 0:
             raise exception.DatabaseQueryError(
                 '%s: Query affected no rows' % self.LABEL)
     except Exception, e:
         self._log(
             log.info, 'Unable to record autoresponse: %s -> %s - %s' %
             (sender, recipient, e))
         raise
    def query(self, cursor, sql, sql_params={}):
        if sql_params:
            # XXX normalize params? Not really... we quoted strings in the
            # query (see backend configuration) and e-mail addresses must not
            # contain any dangerous characters. Hint: MySQLdb can do that on
            # it's own if we pass it the params. Let's keep it specialized for
            # now.
            sql = sql % sql_params

        self._log(log.debug, 'Executing query: %s' % sql)
        try:
            return cursor.execute(sql)
        except self._mysql_exceptions.ProgrammingError, e:
            raise exception.DatabaseQueryError(
                '%s: Error executing query: %s' % (self.LABEL, e), sql)
        if sql_params:
            # XXX normalize params? Not really... we quoted strings in the
            # query (see backend configuration) and e-mail addresses must not
            # contain any dangerous characters. Hint: MySQLdb can do that on
            # it's own if we pass it the params. Let's keep it specialized for
            # now.
            sql = sql % sql_params

        self._log(log.debug, 'Executing query: %s' % sql)
        try:
            return cursor.execute(sql)
        except self._mysql_exceptions.ProgrammingError, e:
            raise exception.DatabaseQueryError(
                '%s: Error executing query: %s' % (self.LABEL, e), sql)
        except self._mysql_exceptions.IntegrityError, e:
            raise exception.DatabaseQueryError(
                '%s: Integrity error: %s' % (self.LABEL, e), sql)

    def fetchrow(self, cursor):
        return cursor.fetchone()

    def validate_recipient(self, cursor, address):
        if not self.config.query_validate_recipient_enabled:
            return

        try:
            result = self.query(cursor, self.config.query_validate_recipient,
                                {'address': address})
            if result != 1:
                raise exception.DatabaseQueryError(
                    '%s: Unexpected number of rows returned' % self.LABEL)
            elif cursor.fetchall()[0][0] != 1: