Add checks for query passed to setCachedStatement

There is a chance to crash if pass query is nil, which might be a case having complex sql builder.