antirice has asked for the wisdom of the Perl Monks concerning the following question:

I have a question regarding verification of data format before trying to insert it into a database, specifically MySQL. Is there a module that I can just use with some nifty subs that would allow me to check is_safe_date($string) prior to insertion? I've used a number of subs previously in other projects. The only thing is that they've matured as I've continued to work on projects. I am aware of Regexp::Common and DateTime::Format::MySQL. I was just wondering if anyone ever put a module together that would verify the format of data before attempting insertion. If there isn't, would this be a good project to attempt as my first published module or no?

Thanks in advance :-)

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

  • Comment on Data format verification for insertion into a database

Replies are listed 'Best First'.
Re: Data format verification for insertion into a database
by dws (Chancellor) on Apr 25, 2003 at 05:47 UTC
    I was just wondering if anyone ever put a module together that would verify the format of data before attempting insertion.

    At one level, what constitutes correct format is application-specific.

    At another level, things like data with embedded quotes can be handled automatically by DBI if you use parameter markers in your queries and pass values to execute(), where they'll be automagically quoted.

    Could you be a little more specific about what you mean by "verification of data format"?

      For instance: make certain that a date is in the proper sequence, make certain that something which is supposed to be an integer is most certainly an integer, etc. There are also other checks such as proper e-mail address formatting that I would like to see rolled up into one friendly module (outside of Email::Valid so I don't have to import 500 modules to know that my data is properly formatted). Hmmm..

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

        There are also other checks such as proper e-mail address formatting that I would like to see rolled up into one friendly module ...

        As of yet, there is no such friendly module, at least as far as I know (though perhaps someone else knows better). You'll need to build up the verification you need from individual pieces.

Re: Data format verification for insertion into a database
by Abigail-II (Bishop) on Apr 25, 2003 at 07:44 UTC
    Well, MySQL is very capable of checking the format itself. Why not do something like:
    eval { local $dbh -> {RaiseError} = 1; local $dbh -> {PrintError} = 0; local $dbh -> {AutoCommit} = 0; my $sql = "INSERT INTO foo_table VALUES (?, ?)"; my $sth = $dbh -> prepare ($sql); $sth -> execute ($key, $date); }; if ($@) { # Some error occurred, for instance due to wrong dateformat. $dbh -> rollback; # Inspect $@ for error, take appropriate action. ... } else { $dbh -> commit; }

    Abigail

Re: Data format verification for insertion into a database
by PodMaster (Abbot) on Apr 25, 2003 at 07:46 UTC
    Now, your module has nothing to do with DBI nor MySQL. It has to do with data.

    I can't see any sense in rolling out a module which just consolidates various bits from other modules. It may make sense if you're doing this for some application (inlining only the patterns you're going to use from Regexp::Common for your app is a good idea), but as a module on cpan? Unless you're releasing that app on CPAN it doesn't make much sense to me.

    I say you contact Abigail-II, the author of Regexp::Common and see if you can contribute.

    update: Perhaps roll out Regexp::Common::MySQL, which would pull out patterns from Regexp::Common::date and Regexp::Common::sql ... or something


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

(Date) Data format verification for insertion into a database
by Your Mother (Archbishop) on Apr 25, 2003 at 08:06 UTC
    Not quite on point but something you might find some use in and I had lying around (tweaked it minorly for this). The following script parses dates, in all manner of formats, into something useable by mySQL. If it fails, you'd know you had nothing good.

    sample usage (readmore for code)

    user[29]~/bin>mysql-date-fixer next tuesday 2003-04-29 01:06:55 user[30]~/bin>mysql-date-fixer Jan 1 2021 2021-01-01 00:00:00 user[31]~/bin>mysql-date-fixer 982321156 2001-02-16 03:59:16
Re: Data format verification for insertion into a database
by TVSET (Chaplain) on Apr 25, 2003 at 13:32 UTC
    I usually do something like this (pseudo-code != Perl):

    if (data_submitted()) { @errors = validate_data(); if (@errors) { output_errors(@errors); output_form(); } else { process_data(); } } else { output_form(); }

    Forms appearence, data validation and processing are VERY application specific, so I don't think that you might cheat a lot there. On the other hand, the validate_data() sub can win a lot from simplified validations. I'd say that it would be very helpful if I could do something like this (pseudo-code again):

    push @errors, "Age not in range" if (!is_integer($age,5,100)); push @errors, "Ugly date format" if (!is_date($date,"YYYY-MM-DD")); push @errors, "Unknown image type" if (!is_file_type($uploaded_file, " +image/jpeg"));

    Leonid Mamtchenkov