Apply some optimizations and minor bug fixes

Hello,

I'm still exploring using this project, but noticed a few things while perusing the source.

The first changeset (8d9f839) fixes some missing declarations and is a bug fix.  

The next two are performance-related refactorings.  They should not change the end results in any way.  The reasoning behind them is in the commit notes.   If you don't like these, you should still add the local declarations here:
https://github.com/neomantra/gin/commit/53faf23fc9241d4ccbe152f86576ec51619a011f#diff-1012035152e305fc68031a182e055881R77

I think these pass unit tests -- I ran them, but am still getting used to luarocks and busted so may have been running them on stale code.   I was getting 4 migration test errors on either set because I don't have DB set up (just the client libs needed to build/run it).   Apologies ahead of time if I did it wrong and you find problems.  I tried to thoroughly inspect too.