blib/lib/blx/xsdsql/generator/sql/generic/handle.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 206 CC 8 SCAR 14.8
CC
8
Cov
0%
new
12.0
header
6.0
footer
6.0
CRAP 4.4
1 package blx::xsdsql::generator::sql::generic::handle;
2 use strict; # use strict is for PBP
3 use Filter::Include;
4 include blx::xsdsql::include;
5 #line 6
6  
7 use blx::xsdsql::ut::ut qw(nvl ev);
8  
9 use constant {
10 STREAM_CLASS => 'blx::xsdsql::ios::ostream'
11 };
12  
13 use blx::xsdsql::schema_repository::sql::generic::column;
14 use base qw(blx::xsdsql::ios::debuglogger);
15  
16 sub header {
17 my ($self,$table,%params)=@_;
18 return $self if $params{NO_EMIT_COMMENTS};
19 $self->{STREAMER}->put_line;
20 $self->{STREAMER}->put_line($table->comment('generated by blx::xsd2sql'));
21 $self->{STREAMER}->put_line;
22 return $self;
23 }
24  
25  
26 sub first_pass {
27 my ($self,%params)=@_;
28 return $self;
29 }
30  
31 sub table_header {
32 my ($self,$table,%params)=@_;
33 return $self;
34 }
35  
36 sub table_footer {
37 my ($self,$table,%params)=@_;
38 return $self;
39 }
40  
41 sub column {
42 my ($self,$table,%params)=@_;
43 return $self;
44 }
45  
46 sub last_pass {
47 my ($self,%params)=@_;
48 return $self;
49 }
50  
51 sub footer {
52 my ($self,$table,%params)=@_;
53 return $self if $params{NO_EMIT_COMMENTS};
54 $self->{STREAMER}->put_line;
55 $self->{STREAMER}->put_line($table->comment('end of blx::xsd2sql'));
56 $self->{STREAMER}->put_line;
57 return $self;
58 }
59  
60 sub new {
61 my ($class,%params)=@_;
62 my $fd=nvl(delete $params{FD},*STDOUT);
63 my $self=bless \%params,$class;
64  
65 if (ref($fd) ne STREAM_CLASS) {
66 if (ref($self->{STREAMER}) eq STREAM_CLASS) {
67 $self->{STREAMER}->set_output_descriptor($fd);
68 }
69 else {
70 ev('use ',STREAM_CLASS);
71 $self->{STREAMER}=STREAM_CLASS->new(OUTPUT_STREAM => $fd);
72 }
73 }
74 else {
75 $self->{STREAMER}=$fd;
76 }
77 return $self;
78 }
79  
80  
81  
82 {
83 my $filter=undef;
84 $filter=sub { #recursive function
85 my ($col,$table,%params)=@_;
86 my $newcol=$col->clone(); #clone the column for add ALIAS_NAME attr
87 my $table_ref=$newcol->get_table_reference;
88 my $viewable=$newcol->is_pk && !$params{START_TABLE} || $newcol->is_sys_attributes || $newcol->is_attribute ? 0 : 1;
89 my $join_table=defined $table_ref && $table_ref->get_max_occurs <= 1 && $newcol->get_max_occurs <=1 ? $table_ref->shallow_clone : undef;
90  
91 $newcol->set_attrs_value(
92 VIEWABLE => $viewable
93 ,TABLE => $table
94 );
95 if ($viewable) { #set the alias for view
96 my $tmpcol=$col->clone(EXCLUDE_ATTR_KEYS => [ qw(SQL_NAME) ]); # clone without SQL_NAME attribute for generate uniq alias name
97 my $alias_name=$tmpcol->set_sql_name(%params); #create a unique alias name
98 $newcol->set_attrs_value(ALIAS_NAME => $alias_name);
99 }
100 my @ret=($newcol);
101 if (defined $join_table) {
102 $join_table->set_attrs_value(ALIAS_COUNT => ++${$params{ALIAS_COUNT}});
103 $newcol->set_attrs_value(JOIN_TABLE => $join_table);
104 push @ret,map { $filter->($_,$join_table,%params,START_TABLE => 0) } $join_table->get_columns;
105 }
106 return @ret;
107 };
108 sub _get_columns {
109 my ($self,$table,%params)=@_;
110 my $t=$table->shallow_clone;
111 my $alias_count=0;
112 $t->set_attrs_value(ALIAS_COUNT => $alias_count);
113 my $colname_list={};
114 my @cols=map { $filter->($_,$t,COLUMNNAME_LIST => $colname_list,ALIAS_COUNT => \$alias_count,START_TABLE => 1,SCHEMA => $params{SCHEMA})} $t->get_columns;
115 return @cols;
116 }
117  
118 }
119  
120  
121 sub get_view_columns {
122 my ($self,$table,%params)=@_;
123 my @cols=grep($_->{VIEWABLE},$self->_get_columns($table,%params));
124 return wantarray ? @cols : \@cols;
125 }
126  
127 sub get_join_columns {
128 my ($self,$table,%params)=@_;
129 my @cols=grep(defined $_->get_attrs_value(qw(JOIN_TABLE)),$self->_get_columns($table,%params));
130 return wantarray ? @cols : \@cols;
131 }
132  
133 sub put_comment {
134 my ($self,$catalog,$comment,%params)=@_;
135 $self->{STREAMER}->put_line($catalog->comment($comment));
136 return $self;
137 }
138  
139  
140 sub get_streamer { return $_[0]->{STREAMER}; }
141  
142  
143 sub get_binding_objects {
144 my ($self,$schema,%params)=@_;
145 my %t=$self->{EXTRA_TABLES}->factory_extra_tables;
146 my @t=values %t;
147 return wantarray ? @t : \@t;
148 }
149  
150 sub relation_schema {
151 my ($self,%params)=@_;
152 return $self;
153 }
154  
155  
156 sub _create_indexes {
157 my ($self,$table,%params)=@_;
158 my $table_name=$table->get_sql_name(%params);
159 my $pk_name=$table->get_constraint_name('pk');
160 my @cols=map { $_->get_sql_name } $table->get_pk_columns;
161 $self->{STREAMER}->put_line("alter table $table_name add constraint $pk_name primary key (".join(',',@cols).')',$table->command_terminator);
162 $self->{STREAMER}->put_line;
163
164 for my $idx($table->get_uk_index_names) {
165 my @cols=$table->get_index_columns($idx);
166 my @col_names=map {
167 my $col=$cols[$_];
168 affirm { defined $col } "the position $_ of index $idx not has an associated column";
169 $col->get_sql_name;
170 } (0..scalar(@cols) - 1);
171 $self->{STREAMER}->put_line("create unique index $idx on ",$table_name,"(",join(',',@col_names),')',$table->command_terminator);
172 $self->{STREAMER}->put_line;
173 }
174  
175 for my $idx($table->get_ix_index_names) {
176 my @cols=$table->get_index_columns($idx);
177 my @col_names=map {
178 my $col=$cols[$_];
179 affirm { defined $col } "the position $_ of index $idx not has an associated column";
180 $col->get_sql_name;
181 } (0..scalar(@cols) - 1);
182 $self->{STREAMER}->put_line("create index $idx on ",$table_name,"(",join(',',@col_names),')',$table->command_terminator);
183 $self->{STREAMER}->put_line;
184 }
185 return $self;
186 }
187  
188 1;
189  
190  
191  
192 __END__