(FR #917) Convert dangerous queries to strictly use the Civi db connection

Change-Id: Ie4f2271ca42dd5fe81fd553dd29140e320e3ce04
Merge "(FR #917) Convert dangerous queries to strictly use the Civi db connection"