'This is the "main" method for this class, the one that
consumes the commands which are sent to the Queue. The idea is to have
the following architecture features:
* Other parts of the framework which want to insert into the DB
simply add an item to our input Queue and "forget about it" since
it will be processed in another thread.
* Only one thread accesses the sqlite3 object, which avoids many
issues because of sqlite\'s non thread-safeness
The Queue.get() will make sure we don\'t have 100% CPU usage in the loop'

def run(self):

    OP_CODES = {SETUP: self._setup_handler, QUERY: self._query_handler, SELECT: self._select_handler, COMMIT: self._commit_handler, POISON: POISON}
    while True:
        (op_code, args, kwds, future) = self._in_queue.get()
        args = (args or ())
        kwds = (kwds or {})
        if self.DEBUG:
            print(('%s    %s    %s' % (op_code, args, kwds)))
        handler = OP_CODES.get(op_code, None)
        if (handler is None):
            continue
        elif (handler == POISON):
            break
        else:
            if (not future.set_running_or_notify_cancel()):
                return
            try:
                result = handler(*args, **kwds)
            except sqlite3.OperationalError as e:
                if ('no    such    table' in str(e)):
                    dbe = NoSuchTableException(str(e))
                elif ('malformed' in str(e)):
                    print(DB_MALFORMED_ERROR)
                    dbe = MalformedDBException(DB_MALFORMED_ERROR)
                else:
                    dbe = DBException(str(e))
                future.set_exception(dbe)
            except Exception as e:
                dbe = DBException(str(e))
                future.set_exception(dbe)
            else:
                future.set_result(result)
