#!/usr/bin/perl { package MySchema; use base qw/Class::DBI::Schema::SQL::Translator/; sub dbh { DBI->connect_cached( ... ); # mine comes from a config module } { package MyTable; use base qw/Class::DBI::Schema::SQL::Translator::Table/; sub schema_class { "MySchema" } } { # this should really be a file package Foo::Schema; use base qw/MyTable/; use Class::DBI::Schema::SQL::Translator; # for CAPS_SUBS sub init { my $self = shift->SUPER::init(@_); $self->add_fields( # these guys create SQL::Translator::Schema::Field # objects, or transform them. Prototypes make them # sort of like SQL ID, STRING name => 30, UNIQUE INTEGER "blah", ); return $self; # SQL::Translator objects are Class::Base based } } # elsewhere use Foo::Schema; # import creates an instance # ... or # my $table = Foo::Schema->instance; # because ->isa("Class::Singleton") # creating the schema singleton will cause it to register with the global schema # and stub the CDBI class with an AUTOLOAD.. more on this several lines down # the fun part: # just use CDBI Foo->create({ name => "bob" }); # some explanation: # AUTOLOAD causes the schema object to create the CDBI class, # by calling $schema->make_cdbi_class. # make_cdbi_class will do some standard CDBI stuff. # it will also make a db_Main if Foo::Schema->schema_class (the global schema) # ->can("dbh") # Foo::Schema->cdbi_class == Foo, the ::Schema suffix is removed. # if Foo::Schema->cdbi_class->db_Main->tables doesn't contain # Foo::Schema->cdbi_class->moniker, it uses SQL::Translator to create the table # in Foo->db_Main # the Class::DBI::Schema::SQL::Translator::Table init checks for # ->can("SUB"), so you can say package Foo::Schema; sub FIELDS { ID, STRING blah => $length, }; # or even use constant FIELDS => [ ID, STRING blah => $length ]; # but i have to do this for relationships too.