SQL Join Checker, Making Sure Your Joins Are Right

Recently on a project we came across the need to generate a bunch of different reports from the database.  Due to different requirements we weren’t able to use the ORM (Doctrine2 on the specific project), so we wrote the queries by hand.  As we continued to build the different reports we noticed sometimes we’d typo a join, for example join something on `id` versus `user_id`.  These small typos would cause the reports to still run fine, however have the incorrect data, often it was difficult to pinpoint the exact issue in the given report, as only certain conditions could reproduce the results.  After a while Ashish said it’d be great if we had some sort of sanity checker to make sure the queries we were writing were going across the proper joins.  To me, this was:

challenge-accepted

 

At first I thought about just using Regular Expressions to parse out the join parts of the SQL queries.  However, I found http://code.google.com/p/php-sql-parser/ which appears to do the job.  I downloaded it and wrote a class which uses it and some expressions to discover FK’s in the database.  I ended up with something which, albeit not the most elegant, gets the job done.  Here is an example output of it:

Basically it will run through whatever query you give it, and make sure that the columns you are joining on are defined in the DB. If you are trying to join on a column that is defined as a constraint, it will output the part of the join that failed the check as well as what FK’s currently do exist. Another issue this may help with, is if your database is missing a constraint (FK) that should be defined it will point it out.

I wrote this really quickly, so let me know (or make a pull request) if you find any bugs. I’ve put the code up on Github. Let me know if it helps out!

End of Movembend-of-movemberer,
Just Under 2k Raised

November 30th marked the end of Movember for us.  We raised a total of $1,848 dollars for men’s health!  I think also over the month, those of us that participated and their significant others, have learned that we just aren’t built for mustaches.   Here are some before and after shots of each of us from the month.  Thanks for donating!

ashish ashish

jaredjared

daum
daum

Let us know if you have any suggestions for our next run at it! Until next Movember, stay healthy.

Movember: End of Week #1 and a Client Launch

Well, it’s one week into Movember and three of our engineers, including myself, have joined the team.   It’s too late to join our team, however if you want you can still donate.  We’ll continue to provide an update each week.

Here we are this week.  Let us know which week you think will be the best mug shot and who has the best ‘stache:

daum jared ashish

On a side note, we’d like to congratulate DiscoverE on a successful launch earlier this week.  We helped the DiscoverE team build their entire site which aggregated a number of old sites they had.