Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
I get really tired of seeing SELECT * FROM foo. Who knows the hell you're getting? It's real fun having to get into MySQL every time and figure out what's in the table. SELECT * is for lazy programmers -- and not a good lazy. By explicitly naming the fields we get a host of benefits.
So why do I bring this up? Because of the following bug caused by SELECT *.
my $thing = $dbh->selectrow_hashref(q[
SELECT *
FROM things
LEFT JOIN dead_things USING (thing)
WHERE things.thing = ?
], undef, $thing_num) or die "$thing_num not found\n";
See the bug? Had the fields been explicitly named instead of using an asterisk, this bug would not have occurred.
USING is bad anyway (Score:1)
I prefer to name my primary key column `id` and use `foo_id` as the name for foreign key columns. Then I use `ON` explicitly.
Overall the query becomes more verbose, but I prefer it that way. It makes my schemata much more easily readable when columns immediately reveal themselves for what they are by their name.
Re: (Score:2)
That still doesn't fix the bug :)
Re: (Score:2)
I don't actually see the bug, yet, but I'm still a little groggy this AM. I do agree with your principle. But you may have to help me along to see this specific bug. :)
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re: (Score:2)
Major Hint: what are we joining on and what data structure is Perl using?
Re: (Score:2)
You know, this might be a MySQL-specific bug. I don't have other databases handy on which to test this. I tried it in SQLite and the problem doesn't appear:
Re: (Score:2)
I think it's definitely MySQL specific ... it sounds like MySQL doesn't actually know how to do that type of join properly?
But I'm confused about something else ... I thought selectrow_hashref should give you a hash where the keys are the fieldnames and the values are the values in the record. You seem to be indicating a problem where one of the values is NULL and that gets used as a key in the hash? But I can't see how that would happen, unless I'm misunderstanding the expected results of selectrow_has
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re: (Score:2)
You seem to be indicating a problem where one of the values is NULL and that gets used as a key in the hash?
No, I misunderstood. Never mind. :)
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re: (Score:1)
I’m saying that you got bitten this easily because you combined two bad ideas.
Although now that I think of it, the naming scheme I use means that any join would lead to duplicate
idcolumns.Good thing I avoid
SELECT *like the pest, then. Hmm, looking at some of my own source code, I find I do use the star quite a lot in the older parts. However, it’s always qualified with a table name, which explains why I never ran into your bug.Bug depends on the DB (I think) (Score:2)
I agree on the principle though - SELECT * should probably only be used in the SQL-Shell for testing stuff out.
Re: (Score:2)
Ah, I can see how that would be a problem, if you wound up with two fields in different tables with the same name. But I'm used to the syntax of NATURAL JOIN and didn't look up the syntax of LEFT JOIN to see if it removes the duplicated column from the output or not.
J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
"SELECT *" encouraged in some environments (Score:1)
In the 4gl I used, when you had to explicitly declare variables, you could declare a record 'like' a table, then 'select *' into the record. This was extremely convenient on tables with many columns. Unfortunately, that meant that you had to recompile the program if the table changed.
And Matt is correct in that some databases return 'table.column' as the column name (as the hash key or in
$sth->{NAME}), which is annoying when switching between databases.