use warnings;
use vars qw[ $DEBUG $WARN $VERSION %used_names ];
$VERSION = sprintf "%d.%02d", q$Revision: 1.29 $ =~ /(\d+)\.(\d+)/;
-$DEBUG = 1 unless defined $DEBUG;
+$DEBUG = 0 unless defined $DEBUG;
use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(header_comment);
+use SQL::Translator::Utils qw(debug header_comment);
use Data::Dumper;
my %translate;
# -------------------------------------------------------------------
sub produce {
my $translator = shift;
- $DEBUG = $translator->debug;
- $WARN = $translator->show_warnings;
+ local $DEBUG = $translator->debug;
+ local $WARN = $translator->show_warnings;
my $no_comments = $translator->no_comments;
my $add_drop_table = $translator->add_drop_table;
my $schema = $translator->schema;
}
+ for my $view ( $schema->get_views ) {
+ push @table_defs, create_view($view, {
+ add_drop_view => $add_drop_table,
+ quote_table_names => $qt,
+ quote_field_names => $qf,
+ no_comments => $no_comments,
+ });
+ }
+
$output = join("\n\n", @table_defs);
if ( @fks ) {
$output .= "--\n-- Foreign Key Definitions\n--\n\n" unless $no_comments;
return $create_statement, \@fks;
}
+sub create_view {
+ my ($view, $options) = @_;
+ my $qt = $options->{quote_table_names} || '';
+ my $qf = $options->{quote_field_names} || '';
+ my $add_drop_view = $options->{add_drop_view};
+
+ my $view_name = $view->name;
+ debug("PKG: Looking at view '${view_name}'\n");
+
+ my $create = '';
+ $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n"
+ unless $options->{no_comments};
+ $create .= "DROP VIEW ${qt}${view_name}${qt};\n" if $add_drop_view;
+ $create .= 'CREATE';
+
+ my $extra = $view->extra;
+ $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
+ $create .= " VIEW ${qt}${view_name}${qt}";
+
+ if ( my @fields = $view->fields ) {
+ my $field_list = join ', ', map { "${qf}${_}${qf}" } @fields;
+ $create .= " ( ${field_list} )";
+ }
+
+ if ( my $sql = $view->sql ) {
+ $create .= " AS (\n ${sql}\n )";
+ }
+
+ if ( $extra->{check_option} ) {
+ $create .= ' WITH ' . uc $extra->{check_option} . ' CHECK OPTION';
+ }
+
+ $create .= ";\n\n";
+ return $create;
+}
+
{
my %field_name_scope;