| 1 | package blx::xsdsql::generator::sql::generic::handle::create_view; | ||
| 2 | |||
| 3 | use strict; # use strict is for PBP | ||
| 4 | use Filter::Include; | ||
| 5 | include blx::xsdsql::include; | ||
| 6 | #line 7 | ||
| 7 | use base(qw(blx::xsdsql::generator::sql::generic::handle)); | ||
| 8 | |||
| 9 | sub _get_create_prefix { | ||
| 10 | my ($self,%params)=@_; | ||
| 11 | return "create or replace view"; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub get_binding_objects { | ||
| 15 | my ($self,$schema,%params)=@_; | ||
| 16 | my $table=$schema->get_root_table; | ||
| 17 | return wantarray ? ( $table ) : [ $table ]; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _alias_table { | ||
| 21 | my ($self,%params)=@_; | ||
| 22 | return " as "; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub table_header { | ||
| 26 | my ($self,$table,%params)=@_; | ||
| 27 | return $self->_table_header($table,%params,STREAMER => $self->{STREAMER}); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _table_header { | ||
| 31 | my ($self,$table,%params)=@_; | ||
| 32 | my $path=$table->get_attrs_value(qw(PATH)); | ||
| 33 | my $comm=defined $path && !$params{NO_EMIT_COMMENTS} ? $table->comment('ROOTPATH: '.$path) : ''; | ||
| 34 | $params{STREAMER}->put_line($self->_get_create_prefix,' ',$table->get_view_sql_name," as select $comm"); | ||
| 35 | my @cols=$self->_get_columns($table,%params); | ||
| 36 | my $colseq=0; | ||
| 37 | for my $col(@cols) { | ||
| 38 | next unless $col->get_attrs_value(qw(VIEWABLE)); | ||
| 39 | next if $col->is_attribute; | ||
| 40 | next if $col->is_sys_attributes; | ||
| 41 | my $t=$col->get_attrs_value(qw(TABLE)); | ||
| 42 | my $sqlcomm=sub { | ||
| 43 | return '' if $params{NO_EMIT_COMMENTS}; | ||
| 44 | my $path=$col->get_attrs_value(qw(PATH)); | ||
| 45 | my $comm=defined $path ? 'PATH: '.$path : ''; | ||
| 46 | my $ref=$col->get_attrs_value(qw(PATH_REFERENCE)); | ||
| 47 | $comm.=defined $ref ? ' REF: '.$ref : ''; | ||
| 48 | $comm=~s/^(\s+|\s+)$//; | ||
| 49 | return length($comm) ? $col->comment($comm) : ''; | ||
| 50 | }->(); | ||
| 51 | my $table_alias=sprintf("A_%0".length(scalar(@cols))."d",$t->get_attrs_value(qw(ALIAS_COUNT))); | ||
| 52 | my $column_alias=$col->get_attrs_value(qw(ALIAS_NAME)); | ||
| 53 | $params{STREAMER}->put_line("\t".($colseq == 0 ? '' : ',').$table_alias,'.',$col->get_sql_name,' as ',$column_alias,' ',$sqlcomm); | ||
| 54 | ++$colseq; | ||
| 55 | } | ||
| 56 | $params{STREAMER}->put_line(' from '); | ||
| 57 | for my $col(@cols) { | ||
| 58 | my $t=$col->get_attrs_value(qw(TABLE)); | ||
| 59 | my $alias=sprintf("A_%0".length(scalar(@cols))."d",$col->get_attrs_value(qw(TABLE))->get_attrs_value(qw(ALIAS_COUNT))); | ||
| 60 | |||
| 61 | if ($t->get_sql_name eq $table->get_sql_name) { #the start table | ||
| 62 | $params{STREAMER}->put_line("\t",$t->get_sql_name,$self->_alias_table,$alias) if $col->is_pk && $col->get_pk_seq == 0; | ||
| 63 | } | ||
| 64 | my $table_ref=$col->get_attrs_value(qw(JOIN_TABLE)); | ||
| 65 | next unless defined $table_ref; | ||
| 66 | my $alias_ref=sprintf("A_%0".length(scalar(@cols))."d",$table_ref->get_attrs_value(qw(ALIAS_COUNT))); | ||
| 67 | $params{STREAMER}->put_chars("\t","left join ",$table_ref->get_sql_name,$self->_alias_table,$alias_ref,' on '); | ||
| 68 | my @pk=$table_ref->get_pk_columns; | ||
| 69 | $params{STREAMER}->put_chars("\t",$alias,'.',$col->get_sql_name,'=',$alias_ref,'.',$pk[0]->get_sql_name); | ||
| 70 | $params{STREAMER}->put_chars("\t\tand ",$alias_ref,'.',$pk[1]->get_sql_name,'=0') if scalar(@pk) > 1; | ||
| 71 | $params{STREAMER}->put_line; | ||
| 72 | } | ||
| 73 | $params{STREAMER}->put_line($table->command_terminator); | ||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub table_footer { | ||
| 78 | my ($self,$table,%params)=@_; | ||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | |||
| 86 | __END__ |