New enhanced database class, with query builder

Import from core competed (still untested)