#!/bin/perl -w

use strict;

use DBI;
use SQL::Schema;

for (qw(DBI_DATA_SOURCE DBI_USERNAME DBI_AUTH)) {
  die "Please set environment $_ as documented.\n"
    . "For more information please have a look at the\n"
    . "export_schema(1) man page.\n"
    . "If you can't display this man page, please try\n"
    . "  perldoc export_schema\n"
    unless exists $ENV{$_};
}

print
  SQL::Schema->new(
    DBI->connect( @ENV{qw(DBI_DATA_SOURCE DBI_USERNAME DBI_AUTH)} )
  )->string;

exit(0);

__END__

=pod

=head1 NAME

export_schema  - Prints a database schema as plain SQL

=head1 SYNOPSIS

  $ DBI_DATA_SOURCE='dbi:Oracle:hobbit'
  $ DBI_USERNAME='frodo'
  $ DBI_AUTH='bilbo'
  $ export DBI_DATA_SOURCE DBI_USERNAME DBI_AUTH
  $ export_schema

or more sophisticated:

  $ DBI_DATA_SOURCE='dbi:Oracle:hobbit' \
  > DBI_USERNAME='frodo' \
  > DBI_AUTH='bilbo' \
  > export_schema

=head1 DESCRIPTION

This short script reads the data dictionary of an Oracle database
and prints corresponding plain SQL create statements to stdout.
These create statements can be used as input for sqlplus to duplicate
the schema on another database.

=head1 REQUIRED ENVIRONMENT

The script takes the information necessary to establish a database
connection from the environment. It needs three environment variables
to be set as shown above at SYNOPSIS. These variables correspond to
the first three arguments handed over to DBI's C<connect()> method.
For more information about possible values please have a look at
L<DBI(3)>.

The script does not accept command line parameters with the
connect info as this is the major security whole with some
database scripts (incluing Oracle's sqlplus) if you invoke them
automatically (e.g. from cron). Anybody who is able to use the
command C<ps> can explore your password then. Ok, it makes the
script hard to use within an interactive environment. The real
reason for environment variables was: It makes the script this short. :-)

=head1 CAVEAT

This software is still under development. Not all tweaks are supported
by now. This means: The create statements produced on stdout might
not create exactly the same schema if you execute them.
For more information, please have a look at the TODO file that comes
with this perl module.

=head1 AUTHOR AND COPYRIGHT

  SQL::Schema is Copyright (C) 2000, Torsten Hentschel
                                     Windmuehlenweg 47
                                     44141 Dortmund
                                     Germany

                                     Email: todd@bayleys.ping.de

  All rights reserved.

  You may distribute this package under the terms of either the GNU
  General Public License or the Artistic License, as specified in the
  Perl README file.

=head1 SEE ALSO

L<SQL::Schema(3)>, L<DBI(3)>

=cut
