in reply to DBI quoting when I don't want it to

Here is some code from a very old version of a CGI/Mysql timesheet system I wrote. I used both Date::Manip and SQL date calculation commands. Maybe it will help.

The program basically lets you record how many hours a day you spend on any of your clients, in a view which covers all clients for a 1 week period. Due to user requests, a function was later added which let them log in to past weeks (to make up for unrecorded hours) by selecting a pulldown menu at login time which had items such as "2 weeks ago". So I think your idea was completely valid.

I can tell you that Date::Manip is great, but is also one of the two or three biggest modules on CPAN, if memory matters. As for quoting, maybe DATE_ADD will solve it for you?

Some Date::Manip (mainly used for day of the week type stuff): $companystartdate = &ParseDate("today"); # returns 2000082415:40:44 +format $companystartdate =~ s/^(....)(..).+/$1-$2-01/; # build 2000-08-01 Some SQL: my $statement = "select * from charges where (UserId='$userhash{'UserId'}' AND (Date BETWEEN '$start' AND DATE_ADD('$start', interval 6 day) ) ) order by ClientGroupId,ClientId,Date"; and my $monthstart = substr($sqlstartdate,0,8) . "01"; # 2000-08-01 my $monthend = "DATE_SUB( ( DATE_ADD('$monthstart',interval 1 month) ), Interval 1 day)"; # 1mo less 1dy. too many parens? my $statement="select sum(Hours) from charges where (UserId='$userhash{'UserId'}' and ClientId='$client' and FeeType='$fee' and Date between '$monthstart' and $monthend ) group by UserId";

By the way regarding list values, why the "world of pain"? Are you creating columns all over the place? You probably know it, but CGI.pm can freeze into a string joined by nulls. Not that DBI might like it any.

Hope this helps.

Replies are listed 'Best First'.
Re: Re: DBI quoting when I don't want it to
by Tardis (Pilgrim) on Aug 22, 2002 at 05:58 UTC
    I can tell you that Date::Manip is great, but is also one of the two or three biggest modules on CPAN, if memory matters. As for quoting, maybe DATE_ADD will solve it for you?
    It's the outer quotes that hurt, not so much the inner ones. I simply can't do this unless I stop using placeholders. So I need DBI to do:

    INSERT INTO table (thisdate) VALUES (DATE(NOW() - INTERVAL '+1 month'));

    Rather than:

    INSERT INTO table (thisdate) VALUES ('DATE(NOW() - INTERVAL '+1 month')');

    At least I think that's what you are hinting at being the problem that DATE_ADD might fix.

    Not to mention that (AFAIK) DATE_ADD is a MySQL-specific function (I use Pg).

    By the way regarding list values, why the "world of pain"?
    Because before they came into the picture, the world was a simpler place. I assign arbitary attributes to arbitary table columns. The unique id for each row was the object id.

    Suddenly this doesn't work for list values.

    My approach has been to break any list values into a seperate table of it's own, with a non-unique key.

    When you consider that in a CGI object, anything can have a list value, you can see my pain. In my new backend world I'll be putting my foot down (hard) and deciding which ones can be lists and which ones can't.

    Migration is going to be ..... fun

      Ah. Tres harsh. I can see your pain.

      Override dbh->quote() mentioned in DBI::DBD.pm pod? Stop using placeholders? (eek)