[#1251] Refactor cli.py moving SQL queries to Resource model

I've also used SQLAlchemy instead of writing bare SQL.
WIP
[#1251] Remove useless imports at cli.py