The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::DataModel::Schema::Generator - automatically generate a schema for DBIx::DataModel

SYNOPSIS

Command-line API

  perl -MDBIx::DataModel::Schema::Generator      \
       -e "fromDBI('dbi:connection:string')" --  \
       -schema My::New::Schema > My/New/Schema.pm

  perl -MDBIx::DataModel::Schema::Generator      \
       -e "fromDBIxClass('Some::DBIC::Schema')" -- \
       -schema My::New::Schema > My/New/Schema.pm

If SQL::Translator is installed

  sqlt -f <parser> -t DBIx::DataModel::Schema::Generator <parser_input>

Object-oriented API

  use DBIx::DataModel::Schema::Generator;
  my $generator 
    = DBIx::DataModel::Schema::Generator->new(schema => "My::New::Schema");

  $generator->parse_DBI($connection_string, $user, $passwd, \%options);
  $generator->parse_DBI($dbh);

  $generator->parse_DBIx_Class($class_name);

  $generator->parse_SQL_Translator($translator);

  my $perl_code = $generator->perl_code;

  $generator->load();

DESCRIPTION

Generates schema, table and association declarations for DBIx::DataModel, either from a DBI connection, or from an existing DBIx::Class schema. The result is written on standard output and can be redirected to a .pm file.

The module can be called easily from the perl command line, as demonstrated in the synopsis above. Command-line arguments after -- are passed to method new.

Alternatively, if SQL::Translator is installed, you can use DBIx::DataModel::Schema::Generator as a producer, translating from any available SQL::Translator parser.

Associations are derived from foreign key constraints declared in the database. If clause ON DELETE CASCADE is present, this is interpreted as a composition; otherwise as an association.

The generated code is a skeleton that most probably will need some manual additions or modifications to get a fully functional datamodel, because part of the information cannot be inferred automatically. In particular, you should inspect the names and multiplicities of the generated associations, and decide which of these associations should rather be compositions; and you should declare the column types for columns that need automatic inflation/deflation.

METHODS

new

  my $generator = DBIx::DataModel::Schema::Generator->new(@args);

Creates a new instance of a schema generator. Functions fromDBI and fromDBIxClass automatically call new if necessary, so usually you do not need to call it yourself. Arguments are :

-schema

Name of the DBIx::DataModel::Schema subclass that will be generated (default is My::Schema).

fromDBI

  $generator->fromDBI(@dbi_connection_args, $catalog, $schema, $type);
  # or
  fromDBI(@dbi_connection_args, $catalog, $schema, $type);

Connects to a DBI data source, gathers information from the database about tables, primary and foreign keys, and generates a DBIx::DataModel schema on standard output.

This can be used either as a regular method, or as a function (this function is exported by default). In the latter case, a generator is automatically created by calling new with arguments @ARGV.

The DBI connection arguments are as in "connect" in DBI. Alternatively, an already connected $dbh can also be passed as first argument to fromDBI().

The remaining arguments $catalog, $schema and $type are optional; they will be passed as arguments to "table_info" in DBI. The default values are undef, undef and 'TABLE'.

fromDBIxClass

  $generator->fromDBIxClass('Some::DBIC::Schema');
  # or
  fromDBIxClass('Some::DBIC::Schema');

Loads an existing DBIx::Class schema, and translates its declarations into a DBIx::DataModel schema printed on standard output.

This can be used either as a regular method, or as a function (this function is exported by default). In the latter case, a generator is automatically created by calling new with arguments @ARGV.

produce

Implementation of SQL::Translator::Producer.

parse_DBI

First step of "FromDBI" : gather data from a DBI connection and populate internal datastructures.

parse_DBIx_Class

First step of "FromDBIxClass" : gather data from a DBIx::Class schema and populate internal datastructures.

parse_SQL_Translator

First step of "produce" : gather data from a SQL::Translator instance and populate internal datastructures.

perl_code

Emits perl code from the internal datastructures parsed by one of the methods above.

load();

Immediately evals the generated perl code.

AUTHOR

Laurent Dami, <laurent.dami AT etat ge ch>

COPYRIGHT & LICENSE

Copyright 2008, 2012 Laurent Dami.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.