'Return a possible completion for readline
This function is called with state starting at zero to get the
first completion, then one/two/three etc until you return None.  The best
implementation is to generate the list when state==0, save it,
and provide members on each increase.
The default implementation extracts the current full input
from readline and then calls :meth:`complete_command` or
:meth:`complete_sql` as appropriate saving the results for
subsequent calls.'

def complete(self, token, state):

    if (state == 0):
        import readline
        line = readline.get_line_buffer()
        beg = readline.get_begidx()
        end = readline.get_endidx()
        try:
            if (self._completion_first and line.startswith('.')):
                self.completions = self.complete_command(line, token, beg, end)
            else:
                self.completions = self.complete_sql(line, token, beg, end)
        except:
            import traceback
            traceback.print_exc()
            raise
    if (state > len(self.completions)):
        return None
    return self.completions[state]
