'Prepares and runs an SQL query for the history database.
Parameters
sql : str
Any filtering expressions to go after SELECT ... FROM ...
params : tuple
Parameters passed to the SQL query (to replace "?")
raw, output : bool
See :meth:`get_range`
Returns
Tuples as :meth:`get_range`'

def _run_sql(self, sql, params, raw=True, output=False):

    toget = ('source_raw' if raw else 'source')
    sqlfrom = 'history'
    if output:
        sqlfrom = 'history    LEFT    JOIN    output_history    USING    (session,    line)'
        toget = ('history.%s,    output_history.output' % toget)
    cur = self.db.execute((('SELECT    session,    line,    %s    FROM    %s    ' % (toget, sqlfrom)) + sql), params)
    if output:
        return ((ses, lin, (inp, out)) for (ses, lin, inp, out) in cur)
    return cur
