View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Laurent Dami > DBIx-DataModel-2.46 > DBIx::DataModel::Schema::Generator



Annotate this POD


New  2
Open  3
View/Report Bugs
Source   Latest Release: DBIx-DataModel-3.0


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


Command-line API

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

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

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(schema => "My::New::Schema");

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



  my $perl_code = $generator->perl_code;



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.



  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 :


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


  $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'.


  # or

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.


Implementation of SQL::Translator::Producer.


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


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


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


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


Immediately evals the generated perl code.


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


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.

syntax highlighting: