CDBI::DATA::Schema idea

[prev] [thread] [next] [Date index for 2004/05/22]

From: Simon Cozens
Subject: CDBI::DATA::Schema idea
Date: 14:17 on 22 May 2004
Since not everyone uses the same RDBMS, and not all RDBMSes use the same SQL,
CDBI::DATA::Schema helpfully has an undocumented "translate" option which
rewrites the schema for a different database.

Unfortunately, someone writing frameworks based on CDBI may not know at
program-time what database the end-user will be using. This makes it tricky
to know what to translate to.

However, CDBI itself will know what database is being used at runtime, once
the set_db call has been done, and so should be able to do the translation
automatically.

Here's what I use to do this, which may be of more general use:

my $schema = "mysql"; # The stuff in my DATA sections is written for mysql

sub import {
    my ($self, @params) = @_;
    if (@params) {
        $self->set_db(Main => @params);
        my ($dbd) = $params[0] =~ /dbi:(\w+):/;
        $self->translate_schema($schema => $dbd);
    }
}

my %map = ( # Why SQL::Translator doesn't provide this I don't know
    mysql => "MySQL",
    Pg    => "PostgresQL"
);

sub translate_schema {
    my ($self, $from, $to) = @_;
    $from = exists $map{$from} ? $map{$from} : $from;
    $to   = exists $map{$to}   ? $map{$to}   : $to;
    Class::DBI::DATA::Schema->import(
        ($from eq $to) ? () :
            (translate => [$from => $to ])
    );
}


        -- 
        Building translators is good clean fun.
		-- T. Cheatham

CDBI::DATA::Schema idea
Simon Cozens 14:17 on 22 May 2004

Re: CDBI::DATA::Schema idea
Tony Bowden 15:02 on 22 May 2004

Re: CDBI::DATA::Schema idea
Tim Bunce 11:16 on 24 May 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52