SQL file structure for legacy / sql alchemy

First step for #4163, replacing my PR #4323 aimed at the master branch.