Fix SQLite result sets failing with nested queries or unions

I think this fixes the issues described in #3972. By 'parsing' the entire query string and iterating it we can find the first `FROM` clause more deterministically.