Refactor storage layer.

Avoid repetition by better use of interfaces. Especially simplify SQL
query code.