Cleaned up use of database & query objects

#### Changes
* Changed use of variable name $sql to $query
* Changed use of $db->quote() method to $bd->q()
* Changed use of $db->quoteName() method to $bd->qn()
* Changed adjacent single line $query statements to single multiline statements
* Changed $query->delete();$query->from('...'); statements to the $query->delete('...');
* Changed leftJoin(...) to join('LEFT', ...)

### Testing instructions
Seeing this touches many files, you will just have to test everything :)