Hi all,

This may or may not be of use to anyone.  One thing I've done is create
a 'datatype generator' which is shorthand for many data types and it's
legible, too.  For example, instead of this:

  reason => { 
    type     => 'scalar',
    length   => 255, 
    default  => '', 
    not_null => 1,
    lazy     => 1,
   },

I can write this:

  reason => SCALAR( 255, DEFAULT '', NOT_NULL, LAZY ),

Here's the beginnings of the package (minus POD and a couple of other
business specific things):

  package My::Rose::Datatypes;

  use strict;
  use warnings;
  
  use Exporter::NoWork;
  use My::Exceptions 'throw_invalid';
  use Scalar::Util qw/looks_like_number reftype/;
  
  BEGIN {
      my @types = qw(
        date
        integer
        text
        timestamp
      );
      my %types = ( tinytext => 'text', map { $_ => $_ } @types );
  
      while ( my ( $name, $type ) = each %types ) {
          my $function = uc $name;
          no strict 'refs';
          *$function = sub {
              if ( looks_like_number( $_[0] ) ) {
                  throw_invalid
                    "Datatype '$name' does not require a numeric length
'$_[0]'";
              }
              return { type => $type, @_ };
          };
      }
      my @types_with_lengths = qw(
        character
        scalar
        tinyint
        varchar
      );
      my %types_with_lengths = (
          char => 'character',
          map { $_ => $_ } @types_with_lengths );
      while ( my ( $name, $type ) = each %types_with_lengths ) {
          my $function = uc $name;
          no strict 'refs';
          *$function = sub {
              my $length = shift;
              unless ( looks_like_number($length) ) {
                  throw_invalid
                    "Datatype '$name' requires a numeric length, not
'$length'";
              }
              return { type => $type, length => $length, @_ };
          };
      }
  }
  
  sub ENUM {
      my $values = shift;
      throw_invalid "First argument to _ENUM must be an array
reference"
        unless 'ARRAY' eq reftype $values;
      return { type => 'enum', values => $values, @_ };
  }
  
  sub DEFAULT($)    { return default     => shift }
  sub LAZY()        { return lazy        => 1 }
  sub NOT_NULL()    { return not_null    => 1 }
  sub PRIMARY_KEY() { return primary_key => 1 }

  1;

Anyone see problems with this approach?

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to