refactor to_sql so it does not depend on the to_sql implementation of
the connection