On 21 Feb 2006, John Siracusa wrote:

> SQLite really has no concept of SQL data types.  Well, it has a very
> simplified concept, anyway.  All that detail you put in your CREATE
> TABLE statements is pretty much ignored by SQLite.  Instead, it stores
> everything as strings and integers (and maybe floats?).  SQLite will
> let you type almost any crazy thing in your CREATE TABLE statements
> and then blithely insert whatever you want.  Example:

Well it must have some sort of concept at least of integers -- 
otherwise I didn't have a problem to begin with. Or perhaps it is a 
problem of DBI?

> Not much to be done.  That's just the nature of the beast.

O.k. I will be more careful in the future.
 
> > One other question:
> > Is there an accessor method to get all the fields of a set in a
> > hash(ref)? I know I could just use the object but that is considered
> > bad practice.
> 
> I'm not sure what you're asking here.  Can you give me an example?

My new project is a web based application with many forms that 
represent more or less a database table each and I have to write many 
forms to edit or add records.
I use CGI-Application with a Plugin that makes it very easy to fill a 
form. This plugin usually takes two parameters: the HTML of a form 
(usually from a template) and a hashref with field names as keys. 
This makes writing an "edit table" form very easy. Here is the code I 
use at the moment to get a hashref I can feed to the form:

sub rose2hashref {
    my $self = shift;
    my $rose = shift;
    my $class = ref $rose;
    my @columns = @{$class->meta->columns};
    my %hash;
    foreach my $field (@columns) {
        if (ref $rose->$field eq 'DateTime') {
            my $date = $rose->$field;
            $date =~ s/^(\d{4})-(\d{2})-(\d{2}).*/$3.$2.$1/;
            $hash{$field} = $date;
        } else {
            $hash{$field} = $rose->$field;
            # $hash{$field} = ref $rose->$field;
        }
    }
    return \%hash;
}

It would be very handy if I could just say
my $hashref = $obj->as_hash_ref;
instead.
Well I stll had to do the date formating so it would be even better 
if I could define my own default for the stringification of DateTime 
objects (german date format in my case).

The same problem arises when the data comes back from the form. I 
have a CGI query object and CGI.pm has the Vars-function that gives 
me all the form data in a hashref. It would be very helpful if I 
could pass the result of $q->Vars or perhaps the whole object to my 
Rose-object, do a 'save' and everything is fine (again I would have 
to change the date fields to a format that is allowed, so again it 
would be helpful if I could define my default date format).

Here is the code I use at the moment to achieve this:
sub q2rose {
    my $self = shift;
    my $rose = shift;
    my %rose = %$rose;
    my $class = ref $rose;
    my @columns = @{$class->meta->columns};
    my %daten = %{$self->query->Vars};
    foreach my $field (@columns) {
        next unless exists $daten{$field};
        if ($daten{$field} eq '') {
            $daten{$field} = undef;
        } elsif ($daten{$field} =~ /(\d{1,2})\.(\d{1,2})\.(\d{4})/) {
            $daten{$field} = sprintf("$3-%02d-%02d", $2, $1);
        } 
        # $rose->{$field} = $daten{$field};
        $rose->$field($daten{$field});
    }
    return $rose;
}

It works but looks quite clumsy, so I wanted to ask if there is a 
more elegant shortcut to achieve this common task.

Michael




-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to