nysus has asked for the wisdom of the Perl Monks concerning the following question:
As a hobbyist Perl programmer, SQL syntax makes my head hurt. I don't use it a lot and every time I do I have to relearn it. It makes it hurt so much I even tried writing a program that used store to free complex, nested variables in files. That was probably a big mistake. So I'm going to rewrite it properly this time using a database.
So I'm wondering what Monks out there think of SQL query builders. Do they actually save any work? I've taken a quick peek at some out there like SQL::Maker.pm but they don't seem to save much time or thought I'm guessing they are probably kind of inflexible. Plus, aren't I just learning one arcane syntax for another?
Over the weekend, I was dabbling with rolling my own SQL builder to query some data I need out of a database. It's not professional in any sense. Here's an example:
sub number_members_for_year_for_state { my $self = shift; my $year = shift; my $state = shift; my $col = 'members'; my $table = 'lm_data'; my $criteria = {state => $state, year => $year, and => {or => {desig +_name => ['LU', 'LLG', 'CH', 'BR', 'LG', 'SLG', 'UNIT', 'LDIV']}}}; my $result = $self->data->sum_column($col, $table, $criteria); return $result->[0][0]; }
My builder is arcane and functions are highly specialized like the sum_column function used here but at least I know how it works and after I build up a big enough library up, it will save me a lot of typing and researching how to make the different kinds of queries. I can't tell you how many times I've looked up they syntax to take a SUM a column and have forgotten. With my sum_column function I never will have to again. Also, I can customize my queries just so. For example, my database object will automatically look up the column type to see if an argument needs apostrophes around it. And it also makes every insert a multiple row insert to speed things up. However, of course, it will be a lot of work up front. The danger is, I think, I will have to write more and more complicated queries for all the different types of SQL queries. In that case, I guess I just bite the bullet and agonize for an hour trying to figure out how to write the proper SQL query without my builder.
Anyway, I'd appreciate some tips and advice from the pros for making database queries less painful.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|