Re: how to find out column types?

[prev] [thread] [next] [Date index for 2004/12/28]

From: Ted Zlatanov
Subject: Re: how to find out column types?
Date: 19:38 on 28 Dec 2004
Here's my version of Class::DBI::Plugin::Type's import() function.
This is pretty much the whole of the module minus the docs.  I just
add a $full parameter, which gets you the whole information hash
back.  Without $full, you get the current behavior.

Simon: sorry for ruining your nice map() call.  Feel free to
reassemble it if you prefer.

I haven't been able to figure out how to use primary_key(); if anyone
can show me how to incorporate that I would appreciate it greatly.  I
think I am using it correctly but I get an empty list every time, so I
must be missing something.  Getting the primary key would be really
useful to me.

Ted


sub import {
    no strict 'refs';
    my $caller = caller();
    #if ($caller->isa("Class::DBI::mysql") and
    #    $caller->can("column_type")) {
    #    return; # My work here is done
    #}

    return if $caller->can("sql_dummy");
    $caller->set_sql(dummy => <<'');
        SELECT *
        FROM __TABLE__
        WHERE 1=0

    $caller->mk_classdata("_types");
    *{$caller."::column_type"} = sub {
        my ($self, $column, $full) = @_;

        if (!$self->_types) {
            my $sth = $self->sql_dummy;
            $sth->execute;
            my %hash;
	    my @names = @{$sth->{NAME}};
	    my @types = @{$sth->{TYPE}};
	    foreach my $i (0 .. (scalar @names)-1)
	    {
	     my $name = $names[$i];
	     my $type = $types[$i];
	     
	     my $info = scalar $self->db_Main->type_info($type);

	     if ($info)
	     {
	      $hash{$name} = [ $info->{TYPE_NAME} => $info ];
	     }
	     else
	     {
	      $hash{$name} = [ $_ => $_ ]; # Typeless databases (SQLite)
	     }
	    }
            $sth->finish;
            $self->_types(\%hash);
        }
        return (defined $full) ? $self->_types->{$column}->[1] : $self->_types->{$column}->[0];
    }   
}

(message missing)

how to find out column types?
Ted Zlatanov 18:54 on 21 Dec 2004

Re: how to find out column types?
Tony Bowden 18:58 on 21 Dec 2004

Re: how to find out column types?
Ted Zlatanov 19:28 on 21 Dec 2004

Re: how to find out column types?
Peter Speltz 08:05 on 22 Dec 2004

Re: how to find out column types?
Tony Bowden 08:58 on 22 Dec 2004

Re: how to find out column types?
Ted Zlatanov 15:34 on 22 Dec 2004

Re: how to find out column types?
Tony Bowden 17:04 on 22 Dec 2004

Re: how to find out column types?
Ted Zlatanov 17:11 on 22 Dec 2004

Re: how to find out column types?
Tony Bowden 01:08 on 23 Dec 2004

Re: how to find out column types?
Stephen Quinney 07:51 on 23 Dec 2004

Re: how to find out column types?
Simon Flack 13:17 on 23 Dec 2004

Re: how to find out column types?
Ted Zlatanov 16:05 on 23 Dec 2004

Re: how to find out column types?
Peter Speltz 07:55 on 24 Dec 2004

Re: how to find out column types?
Ted Zlatanov 19:38 on 28 Dec 2004

Re: how to find out column types?
Simon Flack 21:29 on 28 Dec 2004

Re: how to find out column types?
Tony Bowden 03:55 on 29 Dec 2004

Re: how to find out column types?
Stephen Quinney 12:02 on 29 Dec 2004

Generated at 12:15 on 16 Jan 2005 by mariachi v0.52