blib/lib/blx/xsdsql/schema_repository/loader.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 200 CC 8 SCAR 35.0
CC
8
Cov
0%
load_schema_from_catalog
72.0
new
2.0
BEGIN
2.0
CRAP 33.1
1 package blx::xsdsql::schema_repository::loader;
2  
3 use strict; # use strict is for PBP
4 use Filter::Include;
5 include blx::xsdsql::include;
6 #line 7
7  
8 use blx::xsdsql::ut::ut qw(nvl ev);
9 use blx::xsdsql::xsd_parser::schema;
10 use blx::xsdsql::schema_repository::sql::generic::table;
11 use blx::xsdsql::schema_repository::sql::generic::column;
12 use blx::xsdsql::schema_repository::sql::generic::relation_schema;
13 use blx::xsdsql::schema_repository::sql::generic::relation_table;
14  
15 use base qw(blx::xsdsql::schema_repository::base);
16  
17  
18 sub new {
19 my ($class,%params)=@_;
20 return $class->SUPER::_new(%params);
21 }
22  
23 sub load_schema_from_catalog {
24 my ($self,$catalog_name,%params)=@_;
25 my $conn=$self->{DB_CONN};
26 my %schemas=(); #hash with key schema_code and value obj of class blx::xsdsql::xsd_parser::schema
27 my $root_schema=undef;
28 my %tables=(); #hash with key sql_name and value obj of class blx::xsdsql::<output_namespace>::<db_namespace>::table
29 my %root_tables=(); #hash with key schema_code and value obj of class blx::xsdsql::<output_namespace>::<db_namespace>::table
30 my %table_types=(); #hash with key schema_code and value array of tables
31
32 { ### load schema
33 my $table=$self->{_EXTRA_TABLES}->get_extra_table(qw(SCHEMA_DICTIONARY));
34 my @cols=grep(!$_->get_attrs_value(qw(PK_AUTOSEQUENCE)),$table->get_columns);
35 my $prep=$self->{_BINDING}->generic_query_rows(
36 $table
37 ,DISPLAY => \@cols
38 ,WHERE => [ { COL => 'catalog_name',VALUE => $catalog_name } ]
39 ,TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__}
40 );
41 while(my $r=$prep->fetchrow_arrayref) {
42 my $schema= blx::xsdsql::xsd_parser::schema::factory_from_dictionary_data($r,EXTRA_TABLES => $self->{_EXTRA_TABLES});
43 my $code=$schema->get_attrs_value(qw(SCHEMA_CODE));
44 affirm { defined $code } "attribute SCHEMA_CODE not set";
45 $schemas{$code}=$schema;
46 if ($schema->get_attrs_value(qw(ROOT_SCHEMA))) {
47 affirm { !defined $root_schema } "$catalog_name: many root_schema in this catalog";
48 $root_schema=$schema;
49 }
50 }
51 $self->{_BINDING}->finish(TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__});
52 affirm { defined $root_schema } "$catalog_name: no root schema in this catalog";
53 }
54
55 { # assign childs schema
56 my $table=$self->{_EXTRA_TABLES}->get_extra_table(qw(RELATION_SCHEMA_DICTIONARY));
57 my @cols=grep(!$_->get_attrs_value(qw(PK_AUTOSEQUENCE)),$table->get_columns);
58 my $prep=$self->{_BINDING}->generic_query_rows(
59 $table
60 ,DISPLAY => \@cols
61 ,WHERE => [ { COL => 'catalog_name',VALUE => $catalog_name } ]
62 ,ORDER => [ qw(catalog_name parent_schema_code child_sequence) ]
63 ,TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__}
64 );
65 while(my $r=$prep->fetchrow_arrayref) {
66 my $rs= blx::xsdsql::schema_repository::sql::generic::relation_schema::factory_from_dictionary_data($r,EXTRA_TABLES => $self->{_EXTRA_TABLES});
67 my $schema_code=$rs->get_attrs_value(qw(PARENT_SCHEMA_CODE));
68 affirm { defined $schema_code } "PARENT_SCHEMA_CODE not set";
69 my $parent_schema=$schemas{$schema_code};
70 affirm { defined $parent_schema } "no such schema with code '$schema_code'";
71 $schema_code=$rs->get_attrs_value(qw(CHILD_SCHEMA_CODE));
72 affirm { defined $schema_code } "CHILD_SCHEMA_CODE not set";
73 my $child_schema=$schemas{$schema_code};
74 affirm { defined $child_schema } "no such schema with code '$schema_code'";
75 my ($parent_namespace,$child_location)=$rs->get_attrs_value(qw(PARENT_NAMESPACE CHILD_LOCATION));
76 $self->{_LOGGER}->log(__LINE__,'add child_schema from location ',$child_location,' and namespace ',$parent_namespace);
77 $parent_schema->add_child_schema($child_schema,$parent_namespace,$child_location);
78 }
79 $self->{_BINDING}->finish(TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__});
80 }
81
82 { #load tables
83 my $table=$self->{_EXTRA_TABLES}->get_extra_table(qw(TABLE_DICTIONARY));
84 my @cols=grep(!$_->get_attrs_value(qw(PK_AUTOSEQUENCE)),$table->get_columns);
85 my $prep=$self->{_BINDING}->generic_query_rows(
86 $table
87 ,DISPLAY => \@cols
88 ,WHERE => [ { COL => 'catalog_name',VALUE => $catalog_name } ]
89 ,TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__}
90 );
91 while(my $r=$prep->fetchrow_arrayref) {
92 my $t= blx::xsdsql::schema_repository::sql::generic::table::factory_from_dictionary_data(
93 $r
94 ,EXTRA_TABLES => $self->{_EXTRA_TABLES}
95 );
96 $tables{$t->get_sql_name}=$t;
97 if ($t->is_root_table) {
98 my $schema_code=$t->get_attrs_value(qw(SCHEMA_CODE));
99 affirm { defined $schema_code } "attribute SCHEMA_CODE not set";
100 $root_tables{$schema_code}=$t;
101 }
102 if ($t->is_type) {
103 my $schema_code=$t->get_attrs_value(qw(SCHEMA_CODE));
104 affirm { defined $schema_code } "attribute SCHEMA_CODE not set";
105 $table_types{$schema_code}=[] unless defined $table_types{$schema_code};
106 push @{$table_types{$schema_code}},$t;
107 }
108 }
109 $self->{_BINDING}->finish(TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__});
110 }
111
112 { # relation tables
113 my $table=$self->{_EXTRA_TABLES}->get_extra_table(qw(RELATION_TABLE_DICTIONARY));
114 my @cols=grep(!$_->get_attrs_value(qw(PK_AUTOSEQUENCE)),$table->get_columns);
115 my $prep=$self->{_BINDING}->generic_query_rows(
116 $table
117 ,DISPLAY => \@cols
118 ,WHERE => [ { COL => 'catalog_name',VALUE => $catalog_name } ]
119 ,ORDER => [ qw(parent_table_name child_sequence) ]
120 ,TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__}
121 );
122 while(my $r=$prep->fetchrow_arrayref) {
123 my $t=blx::xsdsql::schema_repository::sql::generic::relation_table::factory_from_dictionary_data(
124 $r
125 ,EXTRA_TABLES => $self->{_EXTRA_TABLES}
126 );
127 my $table_name=$t->get_attrs_value(qw(PARENT_TABLE_NAME));
128 my $parent_table=$tables{$table_name};
129 affirm { defined $parent_table } "$table_name: no such table object with this sql_name";
130 $table_name=$t->get_attrs_value(qw(CHILD_TABLE_NAME));
131 my $child_table=$tables{$table_name};
132 affirm { defined $child_table } "$table_name: no such table object with this sql_name";
133 $self->{_LOGGER}->log({ PACKAGE => __PACKAGE__,LINE => __LINE__},"add table ",$child_table->get_sql_name," with child table to table ",$parent_table->get_sql_name);
134 $parent_table->add_child_tables($child_table);
135 }
136 $self->{_BINDING}->finish(TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__});
137 }
138
139 { # columns
140 my $table=$self->{_EXTRA_TABLES}->get_extra_table(qw(COLUMN_DICTIONARY));
141 my @cols=grep(!$_->get_attrs_value(qw(PK_AUTOSEQUENCE)),$table->get_columns);
142 my $prep=$self->{_BINDING}->generic_query_rows(
143 $table
144 ,DISPLAY => \@cols
145 ,WHERE => [ { COL => 'catalog_name',VALUE => $catalog_name } ]
146 ,ORDER => [ qw(table_name column_seq) ]
147 ,TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__}
148 );
149
150 my $symtable=ev('\\%'.ref($cols[0]).'::'); # use the class method factory_from_dictionary_data from the column class
151 # if exists otherwise from the generic column class
152 # $pf contain a pointer to sub
153 my $pf=$symtable->{factory_from_dictionary_data}
154 ? \&{ref($cols[0]).'::factory_from_dictionary_data'}
155 : \&blx::xsdsql::schema_repository::sql::generic::column::factory_from_dictionary_data
156 ;
157
158 while(my $r=$prep->fetchrow_arrayref) {
159
160 my $c=$pf->(
161 $r
162 ,EXTRA_TABLES => $self->{_EXTRA_TABLES}
163 );
164  
165 if (defined (my $sql_name=$c->get_table_reference)) {
166 my $t=$tables{$sql_name};
167 affirm { defined $t } "$sql_name: no such table with this name";
168 $c->set_attrs_value(TABLE_REFERENCE => $t);
169 $self->{_LOGGER}->log( { PACKAGE => __PACKAGE__,LINE => __LINE__},"bind table ",$t->get_sql_name," to column ".$c->get_full_name);
170 }
171 my $sql_name=$c->get_table_name;
172 my $t=$tables{$sql_name};
173 affirm { defined $t } "$sql_name: no such table with this name";
174 $t->add_columns({
175 NO_GENERATE_SEQUENCE => 1
176 ,NO_SET_TABLE_NAME => 1
177 ,NO_GENERATE_SQL_NAME => 1
178 ,ACCEPT_DUPLICATE_PATH => 1
179 ,IGNORE_ALREADY_EXIST => 1
180 },$c
181 );
182 $self->{_LOGGER}->log({ PACKAGE => __PACKAGE__,LINE => __LINE__},"added column ",$c->get_sql_name,' to table ',$t->get_sql_name);
183 }
184 $self->{_BINDING}->finish(TAG => { PACKAGE => __PACKAGE__,LINE => __LINE__});
185 }
186 for my $schema_code(keys %schemas) { # add types e mapping paths
187 my $schema=$schemas{$schema_code};
188 affirm { defined $schema } "$schema_code: not schema with this schema_code";
189 if (defined (my $types=$table_types{$schema_code})) {
190 affirm { ref($types) eq 'ARRAY' } ref($types).": must be ARRAY";
191 $schema->add_types(@$types);
192 }
193 my $root_table=$root_tables{$schema_code};
194 affirm { defined $root_table } "$schema_code: no root table for this schema_code";
195 $schema->set_attrs_value(TABLE => $root_table);
196 $schema->set_types;
197 my $type_table_paths=$schema->get_types_path;
198 affirm { ref($type_table_paths) eq 'HASH' } ref($type_table_paths)." not HASH";
199 $schema->mapping_paths($type_table_paths,DEBUG => $self->{DEBUG});
200 }
201
202 return $root_schema->set_attrs_value(CATALOG_NAME => $catalog_name);
203 }
204  
205  
206 1;
207  
208  
209  
210 __END__