Refactoring and fixes for custom SQL code

An up-to-date version of changes proposed in pull request  https://github.com/nhibernate/nhibernate-core/pull/161.
I added the this. prefixes to get clearer distinction between local variable and instance fields, as in the original code the only difference between field and local variable names was the casing. I can revert that change to get a clearer view of the actual changes that this commit introduces, but it makes reading/understanding the code harder.