blib/script/xml_repo.pl untested

filter: Statementstmt 0.0% 0 / 619 Branchbran 0.0% 0 / 350 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 1072 CC 18 SCAR 47.5
CC
18
Cov
0%
argv_parser
240.0
get_dbconn_cs
20.0
BEGIN
2.0
CRAP 115.8
1 #!/usr/local/bin/perl
2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
3 if 0; # not running under some shell
4 use strict; # use strict is for PBP
5 use Filter::Include;
6 include blx::xsdsql::include;
7 #line 8
8  
9 use Getopt::Std;
10 use File::Basename;
11 use DBI;
12 use XML::Parser;
13 use XML::Writer;
14  
15 use blx::xsdsql::ut::ut qw(nvl ev);
16 use blx::xsdsql::xsd_parser;
17 use blx::xsdsql::schema_repository;
18 use blx::xsdsql::connection;
19 use blx::xsdsql::xml;
20  
21 my %Opt=();
22  
23  
24 use constant {
25 COMMON_HELP1 =>
26 q(
27 -d - set the debug mode
28 -c <connstr> - connect string to database - the default is the value of the env var DB_CONNECT_STRING
29 otherwise is an error
30 the form is [<output_namespace>::]<dbtype>:<user>/<password>@<dbname>[:hostname[:port]][;<attribute>[,<attribute>...]
31 <output_namespace>::<dbtype> - see the output of namespaces command
32 the default for <output_namespace> is 'sql'
33 <dbname> - database name
34 <hostname> - remote host name or ip address
35 <port> - remote host port
36 <attribute> - extra attribute
37 Examples:
38 sql::pg:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,pg_enable_utf8 => 1
39 sql::mysql:user/pwd@mydb:127.0.0.1;RaiseError => 1,AutoCommit => 0,mysql_enable_utf8 => 1
40 sql::oracle:user/pwd@orcl:neutrino:1522;RaiseError => 1,AutoCommit => 0
41 sql::DBM:dbm_mldbm=Storable;RaiseError => 1,f_dir=> q(/tmp)
42 -t <c|r> - issue a commit or rollback at the end (default commit)
43 -i ignore errors (return 0 if exists errors)
44 )
45 ,COMMON_OPTIONS1 => 'hdc:t:i'
46 , HELP_ORDER => [qw(
47 instruction
48 namespaces
49 catalog_names
50 xml_names
51 drop_repository
52 create_repository
53 drop_catalog
54 create_catalog
55 drop_catalog_views
56 create_catalog_views
57 store_xml
58 put_xml
59 exec
60 )]
61 };
62  
63 my %Help=(
64 instruction => {
65 options => ''
66 ,s =>
67 q[
68 instruction
69 execute perl instruction
70
71
72 ]
73
74 }
75 ,namespaces => {
76 options => ''
77 ,s =>
78 q[
79 namespaces
80 display on stdout the namespaces (type+db) implemented (Ex: sql::pg)
81 ]
82 }
83 ,catalog_names => {
84 options => ''
85 ,s =>
86 q(
87 catalog_names <options>
88 display on stdout the schema catalogs stored into repository
89 <options>
90 %opt
91 )
92 }
93 ,xml_names => {
94 options => ''
95 ,s =>
96 q(
97 xml_names <options> [<catalog_name>..]
98 display on stdout the xml names stored into repository
99 <options>
100 %opt
101 <arguments>
102 <catalog_name> - a name unique into the repository
103 if is not specify display the objects names of all catalogs
104 )
105
106 }
107 ,drop_catalog => {
108 options => 'S'
109 ,s =>
110 q(
111 drop_catalog <options> <catalog_name>....
112 drop the objects associated to a catalog
113 <options>
114 %opt
115 -S - not drop objects but emit on stdout the sql commands
116 <arguments>
117 <catalog_name> - a name unique into the repository
118 )
119 }
120 ,drop_repository => {
121 options => 'S'
122 ,s =>
123 q[
124 drop_repository <options>
125 drop the entire repository (dictionary and catalog objects)
126 <options>
127 %opt
128 -S - not drop objects but emit on stdout the sql commands
129 ]
130 }
131 ,drop_catalog_views => {
132 options => 'S'
133 ,s =>
134 q(
135 drop_catalog_views <options> <catalog_name>...
136 drop the views associated to <catalog_name>
137 <options>
138 %opt
139 -S - not drop objects but emit on stdout the sql commands
140 <arguments>
141 <catalog_name> - a name unique into the repository
142 )
143 }
144 ,create_repository => {
145 options => 'S'
146 ,s =>
147 q(
148 create_repository <options>
149 create repository objects (dictionary)
150 <options>
151 %opt
152 -S - not create objects but emit on stdout the sql commands
153 )
154 }
155 ,create_catalog => {
156 options => 'So:'
157 ,s =>
158 q(
159 create_catalog <options> <catalog_name> <schema_file>
160 create the objects associated to a catalog and populate the dictionaries with the <schema_file>
161 <options>
162 %opt
163 -S - not create objects but emit on stdout the sql commands
164 -o <name>=<value>[,<name>=<value>...]
165 set extra params - valid names are:
166 TABLE_PREFIX - set the prefix for the tables name (default T<auto sequence number>_)
167 VIEW_PREFIX - set the prefix for views name (default V<auto sequence_number>_)
168 NO_FLAT_GROUPS - no flat the columns of table groups with maxoccurs <= 1 into the ref table
169 FORCE_NAMESPACE - force the namespace of the schema (valid only if the schema is in the global namespace)
170 <arguments>
171 <catalog_name> - a name unique into the repository
172 <schema_file> - a file name with contains an xsd schema
173 )
174 }
175 ,create_catalog_views => {
176 options => 'Sg:o:'
177 ,s =>
178 q(
179 create_catalog_views <options> <catalog_name>
180 create the views associated to <catalog_name>
181 <options>
182 %opt
183 -S - not create objects but emit on stdout the sql commands
184 -g <table_name>|<xpath_name>[,<table_name>|<xpath_name>...] - generate view starting only from <table_name> (default all tables)
185 if the first <table_name>|<xpath_name> begin with comma then <table_name>|<xpath_name> (without comma) is a file name containing a list of <table_name>|<xpath_name>
186 the xpath_name must be absolute (starting with /)
187 -o <name>=<value>[,<name>=<value>...]
188 set extra params - valid names are:
189 VIEWS_FROM_LEVEL - set the start level for generate create/drop the views (the start level is 0 and is the default)
190 MAX_VIEW_COLUMNS - produce view code only for views with columns number <= <max_view_columns>
191 -1 is a system limit (database depend)
192 0 is no limit (the default)
193 MAX_VIEW_JOINS - produce view code only for views with join number <= <max_view_joins>
194 -1 is a system limit (database depend)
195 0 is no limit (the default)
196  
197 <arguments>
198 <catalog_name> - a name unique into the repository
199 )
200 }
201 ,store_xml => {
202 options => 'b:a:'
203 ,s => q[
204 store_xml <options> <catalog_name> <name>|<xml_file> <name> ...
205 store xml_file into repository
206 <options>
207 %opt
208 -b - set the execute prefix for db objects (Ex. 'scott.' in oracle)
209 -a - set the execute suffix for db objects (Ex: '@dblink' in oracle)
210 <arguments>
211 <catalog_name> - a name unique into the repository
212 <xml_file> - a valid xml_file to schemas correspondig <catalog_name>
213 if is not specify the <xml_file> is the standard input
214 <name> - a unique name into catalog associated to <xml_file>
215 ]
216 }
217 ,put_xml => {
218 options => 'rb:a:1x:Ho:'
219 ,s => q[
220 put_xml <options> <catalog_name> <name>...
221 emit to stdout an xml associated to <name>
222 if <name> is specify many the result is an xml concatenation
223 <options>
224 %opt
225 -r - delete <name> after the write
226 -b - set the execute prefix for db objects (Ex. 'scott.' in oracle)
227 -a - set the execute suffix for db objects (Ex: '@dblink' in oracle)
228 -H - not write header and footer of the xml file
229 -1 - test tables row count after a delete
230 this option can be set only if 'r' option is set
231 -x - force the root_tag params in form name=value,...
232 -o <filename> - not emit on stdout but on <file_name>
233 <arguments>
234 <catalog_name> - a name unique into the repository
235 <name> - a unique name into catalog
236 ]
237 }
238 ,exec => {
239 options => COMMON_OPTIONS1.'e:'
240 ,s => q(
241 exec <options> [<file_name>]
242 execute commands from <file_name>
243 <options>
244 %opt
245 e <prompt> - echo on stderr the <prompt> and command before the execution
246 <arguments>
247 <file_name> - command file - if it's not specify stdin is assumed
248 )
249 }
250 );
251  
252 sub argv_parser;
253  
254 my %CMD=();
255 %CMD=(
256 instruction => sub {
257 my ($opt,@args)=@_;
258 my $cmd=join(" ",@args);
259 print STDERR "execute perl instruction '$cmd'\n" if $opt->{d};
260 local $@;
261 eval($cmd); ## no critic
262 if ($@) {
263 print STDERR "$@\n";
264 return $opt->{i} ? 0 : 1;
265 }
266 return 0;
267 }
268 ,namespaces => sub {
269 my ($opt,@args)=@_;
270 print STDERR "(W) namespaces: the argument of this commmad are ignored\n" if scalar(@args);
271 print join("\n",blx::xsdsql::schema_repository::get_namespaces),"\n";
272 return 0;
273 }
274 ,catalog_names => sub {
275 my ($opt,@args)=@_;
276 print STDERR "(W) catalog_names: the argument of this commmad are ignored\n" if scalar(@args);
277 my $cats=$opt->{repo}->get_catalog_names;
278 unless(defined $cats) {
279 print STDERR "the repository is not installed\n";
280 return $opt->{i} ? 0 : 1;
281 }
282 for my $name(@$cats) {
283 print $name,"\n";
284 }
285 return 0;
286 }
287 ,xml_names => sub {
288 my ($opt,@args)=@_;
289 unless ($opt->{repo}->is_repository_installed) {
290 print STDERR "the repository is not installed\n";
291 return $opt->{i} ? 0 : 1;
292 }
293  
294 if (scalar(@args)) {
295 for my $cat(@args) {
296 if (defined (my $catalog=$opt->{repo}->get_catalog($cat))) {
297 for my $r($catalog->get_xml_stored) {
298 print join(',',@$r),"\n";
299 }
300 }
301 else {
302 print STDERR "$cat: catalog not existent into repository\n";
303 }
304 }
305 }
306 else {
307 for my $cat($opt->{repo}->get_all_catalogs) {
308 for my $r($cat->get_xml_names) {
309 print join(',',@$r),"\n";
310 }
311 }
312 }
313 return 0;
314 }
315 ,drop_repository => sub {
316 my ($opt,@args)=@_;
317 print STDERR "(W) drop_repository: the argument of this commmad are ignored\n" if scalar(@args);
318 if (!$opt->{S} && !$opt->{repo}->is_repository_installed) {
319 print STDERR "the repository is not installed\n";
320 return $opt->{i} ? 0 : 1;
321 }
322 my $fd=$opt->{S} ? *STDOUT : undef;
323 $opt->{repo}->drop_repository(FD => $fd);
324 return 0;
325 }
326 ,create_repository => sub {
327 my ($opt,@args)=@_;
328 print STDERR "(W) create_repository: the argument of this commmad are ignored\n" if scalar(@args);
329 if (!$opt->{S} && $opt->{repo}->is_repository_installed) {
330 print STDERR "the repository is already installed\n";
331 return $opt->{i} ? 0 : 1;
332 }
333 my $fd=$opt->{S} ? *STDOUT : undef;
334 $opt->{repo}->create_repository(FD => $fd);
335 return 0;
336 }
337 ,drop_catalog => sub {
338 my ($opt,@args)=@_;
339 my $cats=$opt->{repo}->get_catalog_names;
340 unless (defined $cats) {
341 print STDERR "the repository is not installed\n";
342 return $opt->{i} ? 0 : 1;
343 }
344 unless (scalar(@args)) {
345 print STDERR "no catalog names specify\n";
346 return 0;
347 }
348 my $msgerr=": catalog name not found into repository\n";
349 my %cat=map { ($_,undef) } @$cats;
350 my @cats=grep(defined $_,map { exists $cat{$_} ? $_ : do { print STDERR "$_$msgerr";undef}} @args);
351 my $fd=$opt->{S} ? *STDOUT : undef;
352 for my $c(@cats) {
353 unless (defined $opt->{repo}->drop_catalog($c,FD => $fd)) {
354 if (scalar(keys %cat) == 1) {
355 return $opt->{i} ? 0 : 1;
356 }
357 }
358 }
359 return 0;
360 }
361 ,create_catalog => sub {
362 my ($opt,@args)=@_;
363 my $cats=$opt->{repo}->get_catalog_names;
364 unless(defined $cats) {
365 print STDERR "the repository is not installed\n";
366 return $opt->{i} ? 0 : 1;
367 }
368 if (scalar(@args) == 0) {
369 print STDERR "missing <catalog_name> and <schema_file> arguments\n";
370 return 1;
371 }
372 if (scalar(@args) == 1) {
373 print STDERR "missing <schema_file> argument\n";
374 return 1;
375 }
376
377 if (scalar(@args) > 3) {
378 print STDERR "too many arguments\n";
379 return 1;
380 }
381
382 my ($catalog_name,$schema_file)=@args;
383 unless (length($catalog_name)) {
384 print STDERR "<catalog_name> must be at length > 0\n";
385 return 1;
386 }
387 unless (length($schema_file)) {
388 print STDERR "<schema_file> must be at length > 0\n";
389 return 1;
390 }
391
392 my %extra_params=();
393
394 if (defined $opt->{o}) {
395 my @o=split(',',$opt->{o});
396 for my $o(@o) {
397 my($k,$v)=$o=~/^([^=]+)=(.+)$/;
398 if (defined $k) {
399 if (grep($k eq $_,(qw(TABLE_PREFIX VIEW_PREFIX NO_FLAT_GROUPS FORCE_NAMESPACE)))) {
400 $extra_params{$k}=$v;
401 }
402 else {
403 print STDERR $opt->{o}.": the key $k in option 'o' is not know\n";
404 return 1;
405 }
406 }
407 else {
408 print STDERR $opt->{o}.": invalid value for 'o' options\n";
409 return 1;
410 }
411 }
412 }
413
414 $extra_params{TABLE_PREFIX}='T'.sprintf("%03d",scalar(@$cats) + 1)
415 unless defined $extra_params{TABLE_PREFIX};
416 $extra_params{VIEW_PREFIX}='V'.sprintf("%03d",scalar(@$cats) + 1)
417 unless defined $extra_params{VIEW_PREFIX};
418 $extra_params{NO_FLAT_GROUPS}=0 unless defined $extra_params{NO_FLAT_GROUPS};
419 local $_;
420 SWITCH:
421 for ($extra_params{NO_FLAT_GROUPS}) {
422 /^(yes|y|1)$/ && do { $extra_params{NO_FLAT_GROUPS}=1; last SWITCH};
423 /^(no|n|0)$/ && do { $extra_params{NO_FLAT_GROUPS}=0; last SWITCH};
424 print STDERR $extra_params{NO_FLAT_GROUPS}.": the value of the key NO_FLAT_GROUPS into 'o' option is not valid\n";
425 return 1;
426 }
427 my $parser= blx::xsdsql::xsd_parser->new(
428 OUTPUT_NAMESPACE => $opt->{output_namespace}
429 ,DB_NAMESPACE => $opt->{db_namespace}
430 );
431 my $schema=$parser->parsefile($schema_file,%extra_params);
432 my $fd=$opt->{S} ? *STDOUT : undef;
433 unless($opt->{repo}->create_catalog($catalog_name,$schema,FD => $fd)) {
434 print STDERR "$catalog_name: already exist in the repository\n";
435 return $opt->{i} ? 0 : 1;
436 }
437 return 0;
438 }
439 ,drop_catalog_views => sub {
440 my ($opt,@args)=@_;
441 my $cats=$opt->{repo}->get_catalog_names;
442 unless(defined $cats) {
443 print STDERR "the repository is not installed\n";
444 return $opt->{i} ? 0 : 1;
445 }
446 unless (scalar(@args)) {
447 print STDERR "no catalog names specify\n";
448 return 0;
449 }
450 my $msgerr=": catalog name not found into repository\n";
451  
452 my %cat=map { ($_,undef) } @$cats;
453 my @cats=grep(defined $_,map { exists $cat{$_} ? $_ : do { print STDERR "$_$msgerr";undef}} @args);
454 my $fd=$opt->{S} ? *STDOUT : undef;
455 for my $c(@cats) {
456 my $catalog=$opt->{repo}->get_catalog($c);
457 unless (defined $catalog) {
458 print STDERR $c,$msgerr;
459 if (scalar(keys %cat) == 1) {
460 return $opt->{i} ? 0 : 1;
461 }
462 }
463 unless (defined $catalog->drop_views(FD => $fd)) {
464 print STDERR $c,$msgerr;
465 if (scalar(keys %cat) == 1) {
466 return $opt->{i} ? 0 : 1;
467 }
468 }
469 }
470 return 0;
471 }
472 ,create_catalog_views => sub {
473 my ($opt,@args)=@_;
474 my $cats=$opt->{repo}->get_catalog_names;
475 unless(defined $cats) {
476 print STDERR "the repository is not installed\n";
477 return 1;
478 }
479 unless ($opt->{repo}->is_support_views) {
480 print STDERR "database not support complex views\n";
481 return $opt->{i} ? 0 : 1;
482 }
483
484 unless (scalar(@args)) {
485 print STDERR "no catalog names specify\n";
486 return 0;
487 }
488 my $msgerr=": catalog name not found into repository\n";
489 my %cat=map { ($_,undef) } @$cats;
490 my @cats=grep(defined $_,map { exists $cat{$_} ? $_ : do { print STDERR "$_$msgerr";undef}} @args);
491  
492 if (defined (my $t=$opt->{g})) {
493 if ($t=~/^,(.*)$/) { # is a file name
494 if (open(my $fd,'<',$1)) {
495 my @lines=grep(!/^\s*$/,
496 map {
497 my $v=$_;
498 $v=~s/^\s*//;
499 $v=~s/\s*$//;
500 my $l=$v=~/^\s*#/ ? '' : $v;
501 $l;
502 } <$fd>);
503 $Opt{g}=\@lines;
504 close $fd;
505 }
506 else {
507 print STDERR "$1: $!\n";
508 exit 1;
509 }
510 }
511 else {
512 $opt->{g}=[split(",",$t)];
513 }
514 }
515
516 if (defined $opt->{o}) {
517 my %h=();
518 for my $e(split(",",$opt->{o})) {
519 my ($name,$value)=$e=~/^([^=]+)=(.*)$/;
520 unless (defined $name) {
521 print STDERR $Opt{o},": option o is invalid - valid is <name>=<value>[,<name>=<value>...]\n";
522 exit 1;
523 }
524 $h{$name}=$value;
525 }
526 my $o_keys= [ qw(MAX_VIEW_COLUMNS MAX_VIEW_JOINS VIEWS_FROM_LEVEL) ];
527 for my $k(keys %h) {
528 unless (grep($_ eq $k,@$o_keys)) {
529 print STDERR "$k: key on 'o' option is not valid - valid keys are ",join(',',@$o_keys),"\n";
530 exit 1;
531 }
532 }
533 $opt->{o}=\%h;
534 }
535 else {
536 $opt->{o}={};
537 }
538
539
540 my $fd=$opt->{S} ? *STDOUT : undef;
541 for my $c(@cats) {
542 my $catalog=$opt->{repo}->get_catalog($c);
543 unless (defined $catalog) {
544 print STDERR "$c: catalog not exist in the repository\n";
545 return $opt->{i} ? 0 : 1;
546 }
547 unless($catalog->create_views(
548 FD => $fd
549 ,TABLES_FILTER => $opt->{g}
550 ,%{$opt->{o}}
551 )) {
552 print STDERR "$c: catalog not exist in the repository\n";
553 return $opt->{i} ? 0 : 1;
554 }
555 }
556 return 0;
557 }
558 ,store_xml => sub {
559 my ($opt,@args)=@_;
560
561 my $cats=$opt->{repo}->get_catalog_names;
562 unless(defined $cats) {
563 print STDERR "the repository is not installed\n";
564 return $opt->{i} ? 0 : 1;
565 }
566
567 if (scalar(@args) == 0) {
568 print STDERR "missing <catalog_name> argument\n";
569 return 1;
570 }
571 my $catalog_name=shift @args;
572 if (scalar(@args) == 0) {
573 print STDERR "missing <name> or <xml_file> <name> argument\n";
574 return 1;
575 }
576 unshift @args,'-' if scalar(@args) == 1;
577 if (scalar(@args) % 2 != 0) {
578 print STDERR "argument number is not pair\n";
579 return 1;
580 }
581
582 my %args=@args;
583 my %names=();
584 my %fd=();
585 my $err=0;
586 my $stdin=undef;
587 for my $file_name(keys %args) {
588 my $name=$args{$file_name};
589 if (length($file_name) == 0) {
590 print STDERR "file_name at 0 length not allowed\n";
591 $err++;
592 }
593 if (length($name) == 0) {
594 print STDERR "name associated at file_name '$file_name' at 0 length not allowed\n";
595 $err++;
596 }
597 if (defined (my $f=$names{$name})) {
598 print STDERR "$name: this name is already associated at file_name '$f'\n";
599 $err++;
600 }
601 else {
602 $names{$name}=$file_name;
603 }
604 if ($file_name eq '-') {
605 if (defined $stdin) {
606 print STDERR "multiply stdin are not allowed\n";
607 $err++;
608 }
609 else {
610 $stdin=$file_name;
611 $fd{$name}=*STDIN;
612 }
613 }
614 else {
615 unless (-e $file_name) {
616 print STDERR "$file_name: no such file_name\n";
617 $err++;
618 }
619 if (-d $file_name) {
620 print STDERR "$file_name: is a directory\n";
621 $err++;
622 }
623 unless (-r $file_name) {
624 print STDERR "$file_name: is not readable\n";
625 $err++;
626 }
627 if (open(my $fd,'<',$file_name)) {
628 $fd{$name}=$fd;
629 }
630 else {
631 print STDERR "$file_name: $!\n";
632 $err++;
633 }
634 }
635 }
636
637 if ($err) {
638 for my $fd(values %fd) {
639 close $fd unless $fd eq *STDERR;
640 }
641 return 1;
642 }
643
644 my $catalog=$opt->{repo}->get_catalog($catalog_name);
645
646 unless (defined $catalog) {
647 print STDERR "$catalog_name: catalog not exist in the repository\n";
648 for my $fd(values %fd) {
649 close $fd unless $fd eq *STDERR;
650 }
651 return $opt->{i} ? 0 : 1;
652 }
653  
654 my $catalog_xml=$catalog->get_catalog_xml(
655 EXECUTE_OBJECTS_PREFIX => $opt->{b}
656 ,EXECUTE_OBJECTS_SUFFIX => $opt->{a}
657 );
658
659 $err=0;
660 while(scalar(@args)) {
661 my $file_name=shift @args;
662 my $name=shift @args;
663 my $fd=$fd{$name};
664
665 my $id=$catalog_xml->store_xml(
666 XML_NAME => $name
667 ,FD => $fd
668 );
669 delete $fd{$name};
670 close $fd unless $fd eq *STDIN;
671 unless (defined $id) {
672 print STDERR "$name: name already existent\n";
673 $err++;
674 }
675 }
676 if ($err) {
677 return $opt->{i} ? 0 : 1;
678 }
679 return 0;
680 }
681 ,put_xml => sub {
682 my ($opt,@args)=@_;
683 if ($opt->{1} && !$opt->{r}) {
684 print STDERR "for test the delete the option 'r' must be set\n";
685 return 1;
686 }
687 if (scalar(@args) == 0) {
688 print STDERR "missing <catalog_name> argument\n";
689 return 1;
690 }
691 unless($opt->{repo}->is_repository_installed) {
692 print STDERR "the repository is not installed\n";
693 return $opt->{i} ? 0 : 1;
694 }
695 my $catalog_name=shift @args;
696
697 my $catalog=$opt->{repo}->get_catalog($catalog_name);
698
699 unless (defined $catalog) {
700 print STDERR "$catalog_name: catalog not exist in the repository\n";
701 return 1;
702 }
703 if (scalar(@args) == 0) {
704 print STDERR "missing <name> argument\n";
705 return 1;
706 }
707 if (defined $opt->{x}) {
708 my @h=();
709 for my $e(split(",",$opt->{x})) {
710 my ($name,$value)=$e=~/^([^=]+)=(.*)$/;
711 unless (defined $name) {
712 print STDERR $Opt{x},": option x is invalid - valid is <name>=<value>[,<name>=<value>...]\n";
713 return 1;
714 }
715 $value=~s/^"([^"]+)"$/$1/;
716 push @h,($name,$value);
717 }
718 $opt->{x}=\@h;
719 }
720  
721 my $catalog_xml=$catalog->get_catalog_xml(
722 WRITER => XML::Writer->new(
723 DATA_INDENT => 4
724 ,DATA_MODE => 1
725 ,NAMESPACES => 0
726 ,UNSAFE => 1
727 )
728 ,EXECUTE_OBJECTS_PREFIX => $opt->{b}
729 ,EXECUTE_OBJECTS_SUFFIX => $opt->{a}
730 );
731
732 my ($count_rows,$loop_table_childs,$test_delete)=();
733 if ($opt->{1}) { # test delete
734 $count_rows=sub {
735 my ($t,$pcount,%params)=@_;
736 my $table_name=$t->get_sql_name;
737 my $sql="select count(*) from $table_name";
738 my $sth=$params{CONN}->prepare($sql) || die "prepare failed\n";
739 $sth->execute || die "execute failed\n";
740 my $row=$sth->fetchrow_arrayref || die "fetchrow_arrayref failed\n";
741 my $c=$row->[0];
742 $sth->finish;
743 if ($c > 0) {
744 print STDERR "table '$table_name' contains $c rows\n";
745 $$pcount++;
746 }
747 return;
748 };
749  
750 $loop_table_childs=sub {
751 my ($t,$pcount,%params)=@_;
752 $count_rows->($t,$pcount,%params);
753 for my $child($t->get_child_tables) {
754 $loop_table_childs->($child,$pcount,%params);
755 }
756 return;
757 };
758  
759 $test_delete=sub {
760 my ($schema,$pcount,%params)=@_;
761 for my $t($opt->{repo}->get_dtd_tables) {
762 $loop_table_childs->($t,$pcount,%params);
763 }
764 for my $k(qw(XML_CATALOG XML_ENCODING)) {
765 my $t=$opt->{repo}->get_table_from_type(TYPE => $k);
766 $loop_table_childs->($t,$pcount,%params);
767 }
768 my %types=$schema->get_types_name;
769 for my $t(values %types) {
770 $loop_table_childs->($t,$pcount,%params);
771 }
772 my $root_table=$schema->get_root_table;
773 $loop_table_childs->($root_table,$pcount,%params);
774 for my $child($schema->get_childs_schema) {
775 $test_delete->($child->{SCHEMA},$pcount,%params);
776 }
777 return;
778 };
779 }
780  
781 if (defined (my $outfile=delete $opt->{o})) {
782 if ($outfile eq '-') {
783 $opt->{o}=*STDOUT;
784 }
785 else {
786 unless(open($opt->{o},'>',$outfile)) {
787 print STDERR $opt->{o}." open error: $!\n";
788 return 1;
789 }
790 }
791 }
792 else {
793 $opt->{o}=*STDOUT;
794 }
795
796 for my $i(0..scalar(@args) - 1) {
797 my $name=$args[$i];
798 my $id=$catalog_xml->put_xml(
799 XML_NAME => $name
800 ,NO_WRITE_HEADER => $opt->{H} ? 1 : $i ? 1 : 0
801 ,NO_WRITE_FOOTER => $opt->{H} ? 1 : $i == scalar(@args) - 1 ? 0 : 1
802 ,FD => $opt->{o}
803 ,ROOT_TAG_PARAMS => $opt->{x}
804 ,DELETE => $opt->{r}
805 );
806 unless (defined $id) {
807 print STDERR "$name: not found into repository\n";
808 close $opt->{o} unless $opt->{o} eq *STDOUT;
809 return $opt->{i} ? 0 : 1;
810 }
811
812 if ($opt->{1}) {
813 print STDERR "test delete... \n" if $opt->{d};
814 my $count=0;
815 my $schema=$catalog->get_attrs_value(qw(SCHEMA));
816 $test_delete->($schema,\$count,CONN => $opt->{conn});
817 if ($count > 0) {
818 print STDERR "delete test failed - $count tables has rows\n";
819 close $opt->{o} unless $opt->{o} eq *STDOUT;
820 return 1;
821 }
822 }
823 }
824 if ($opt->{r}) {
825 for my $i(0..scalar(@args) - 1) {
826 my $name=$args[$i];
827 print STDERR "$name: delete from the repository\n";
828 }
829 }
830 close $opt->{o} unless $opt->{o} eq *STDOUT;
831 return 0;
832 }
833  
834 ,exec => sub {
835 my ($opt,@args)=@_;
836 my $file_name=shift @args;
837 if (scalar(@args)) {
838 print STDERR "(W) extra arguments are ignored\n";
839 }
840 $file_name=undef if defined $file_name && $file_name eq '-';
841 my $fd=undef;
842 if (defined $file_name) {
843 unless (open($fd,'<',$file_name)) {
844 print STDERR "$file_name: $!\n";
845 return 1;
846 }
847 }
848 else {
849 $fd=*STDIN;
850 }
851
852 my @cmds=grep($_ ne 'exec',@{&HELP_ORDER});
853 while(<$fd>) {
854 @ARGV=argv_parser($_);
855 my $cmd_str=defined $opt->{e} ? join(' ',@ARGV) : undef;
856 my $cmd=shift @ARGV;
857 unless (defined $cmd) {
858 print STDERR "no command specify\n";
859 print STDERR "\tvalid <cmd> is any of ".join("|",@cmds),"\n";
860 return 1 unless $opt->{i};
861 next;
862 }
863 if (!grep($cmd eq $_,@cmds)) {
864 print STDERR "$cmd: invalid command\n";
865 print STDERR "\tvalid <cmd> is any of ".join("|",@cmds),"\n";
866 return 1 unless $opt->{i};
867 next;
868 }
869 my $pcmd=$CMD{$cmd};
870 unless (defined $pcmd) {
871 print STDERR "$cmd: the command is not implemented\n";
872 return 1 unless $opt->{i};
873 next;
874 }
875 my $stroptions=$cmd eq 'namespaces' ? '' : COMMON_OPTIONS1.($Help{$cmd}->{options});
876 my %opt=();
877 unless (getopts ($stroptions,\%opt)) {
878 print STDERR "invalid option or option not set - use h options for help\n";
879 return 1;
880 }
881 if ($opt{h}) {
882 my $text=$Help{$cmd}->{s};
883 my $ch=COMMON_HELP1;
884 $text=~s/\%opt/$ch/;
885 print STDOUT join("\n",map {
886 my $v=$_;
887 $v=~s/^\t{6}//;
888 $v=~s/\t/ /g;
889 $v} grep(!/^\s*$/,split("\n",$text))),"\n";
890 return 0;
891 }
892 for my $k(keys %$opt) { #merge opt
893 $opt{$k}=$opt->{$k};
894 }
895 print STDERR $opt->{e},$cmd_str,"\n" if $opt->{e};
896 my $rc=$pcmd->(\%opt,@ARGV);
897 if ($rc) {
898 return $rc unless $opt->{i};
899 next;
900 }
901 }
902 return 0;
903 }
904 );
905  
906 sub argv_parser {
907 my $line=join('',grep(defined $_,@_));
908 my @args=();
909 my $quote=undef;
910 my $str='';
911 my @l=split('',$line);
912 for my $i(0..scalar(@l) - 1) {
913 my $c=$l[$i];
914 if (defined $quote) {
915 if ($c eq $quote) {
916 my $succ=$i == scalar(@l) - 1 ? undef : $l[$i + 1];
917 $quote=undef;
918 $c='' if defined $succ && $succ!~/^\s$/;
919 }
920 $str.=$c;
921 }
922 else {
923 if ($c eq "'" || $c eq '"') {
924 my $pred=$i == 0 ? undef : $l[$i - 1];
925 $quote=$c;
926 $c='' if defined $pred && $pred!~/^\s$/;
927 $str.=$c;
928 }
929 elsif ($c =~/^\s$/) {
930 if (length($str)) {
931 my $ch=substr($str,0,1);
932 if ($ch eq "'" || $ch eq '"') {
933 $str=substr($str,1);
934 $str=substr($str,0,length($str) - 1) if length($str) && substr($str,-1,1) eq $ch;
935 }
936 push @args,$str;
937 $str='';
938 }
939 }
940 else {
941 $str.=$c;
942 }
943 }
944 }
945 if (length($str)) {
946 my $ch=substr($str,0,1);
947 if ($ch eq "'" || $ch eq '"') {
948 $str=substr($str,1);
949 $str=substr($str,0,length($str) - 1) if length($str) && substr($str,-1,1) eq $ch;
950 }
951 push @args,$str;
952 }
953 return @args;
954 }
955  
956 sub get_dbconn_cs {
957 my ($connstr,%params)=@_;
958 $connstr=$ENV{DB_CONNECT_STRING} unless defined $connstr;
959 unless (defined $connstr) {
960 print STDERR "no connection string specify - set the option 'c' or define the env var DB_CONNECT_STRING\n";
961 return ();
962 }
963  
964 my $connection=blx::xsdsql::connection->new;
965 unless ($connection->do_connection_list($connstr)) {
966 print STDERR $connection->get_last_error,"\n";
967 return ();
968 }
969 my @a=$connection->get_connection_list;
970 return ( {
971 OUTPUT_NAMESPACE => $connection->get_output_namespace
972 ,DB_NAMESPACE => $connection->get_db_namespace
973 },@a
974 );
975 }
976  
977  
978 #### main #####
979  
980  
981 unless (getopts ('h',\%Opt)) {
982 print STDERR "invalid option or option not set - user h options for help\n";
983 exit 1;
984 }
985  
986  
987 if ($Opt{h}) {
988 my $pn=basename($0);
989 for my $cmd(@{&HELP_ORDER}) {
990 my $text=$Help{$cmd}->{s};
991 my $ch=COMMON_HELP1;
992 $text=~s/\%opt/$ch/;
993 print STDOUT $pn.' '.join("\n",map {
994 my $v=$_;
995 $v=~s/^\t{6}//;
996 $v=~s/\t/ /g;
997 $v } grep(!/^\s*$/,split("\n",$text)));
998 print STDOUT "\n\n";
999 }
1000 exit 0;
1001 }
1002
1003 unless (scalar(@ARGV)) {
1004 my $pn=basename($0);
1005 print STDERR "use $pn -h or $pn <command> -h for help\n";
1006 print STDERR "\t<cmd> is any of ".join("|",@{&HELP_ORDER}),"\n";
1007 exit 1;
1008 }
1009  
1010 my $cmd=shift @ARGV;
1011 unless (defined $cmd) {
1012 print STDERR "no command specify\n";
1013 print STDERR "\tvalid <cmd> is any of ".join("|",@{&HELP_ORDER}),"\n";
1014 exit 1;
1015 }
1016  
1017 if (!grep($cmd eq $_,@{&HELP_ORDER})) {
1018 print STDERR "$cmd: invalid command\n";
1019 print STDERR "\tvalid <cmd> is any of ".join("|",@{&HELP_ORDER}),"\n";
1020 exit 1;
1021 }
1022  
1023 my $pcmd=$CMD{$cmd};
1024 unless (defined $pcmd) {
1025 print STDERR "$cmd: the command is not know\n";
1026 exit 1;
1027 }
1028 my $stroptions=$cmd eq 'namespaces' ? '' : COMMON_OPTIONS1.($Help{$cmd}->{options});
1029 unless (getopts ($stroptions,\%Opt)) {
1030 print STDERR "invalid option or option not set - use h options for help\n";
1031 exit 1;
1032 }
1033 if ($Opt{h}) {
1034 my $text=$Help{$cmd}->{s};
1035 my $ch=COMMON_HELP1;
1036 $text=~s/\%opt/$ch/;
1037 print STDOUT join("\n",map {
1038 my $v=$_;
1039 $v=~s/^\t{6}//;
1040 $v=~s/\t/ /g;
1041 $v } grep(!/^\s*$/,split("\n",$text))),"\n";
1042 exit 0;
1043 }
1044  
1045 if (grep($cmd eq $_,qw(namespaces instruction))) {
1046 exit $pcmd->(\%Opt,@ARGV);
1047 }
1048  
1049 $Opt{t}='c' unless defined $Opt{t};
1050 if ($Opt{t} ne 'c' && $Opt{t} ne 'r') {
1051 print STDERR $Opt{t}.": invalid value for option 't' - corrected values are 'c' or 'r'\n";
1052 exit 1;
1053 }
1054 my ($ns,@conn)=get_dbconn_cs($Opt{c});
1055 exit 1 unless defined $ns;
1056 $Opt{output_namespace}=$ns->{OUTPUT_NAMESPACE};
1057 $Opt{db_namespace}=$ns->{DB_NAMESPACE};
1058  
1059  
1060 $Opt{conn}=eval { DBI->connect(@conn) };
1061 if ($@ || !defined $Opt{conn}) {
1062 print STDERR $@ if $@;
1063 print STDERR "connection failed\n";
1064 exit 1;
1065 }
1066  
1067 $Opt{repo}=blx::xsdsql::schema_repository->new(
1068 DB_CONN => $Opt{conn}
1069 ,OUTPUT_NAMESPACE => $Opt{output_namespace}
1070 ,DB_NAMESPACE => $Opt{db_namespace}
1071 ,DEBUG => $Opt{d}
1072 );
1073  
1074 my $rc=eval { $pcmd->(\%Opt,@ARGV); };
1075 if ($@) {
1076 print STDERR $@;
1077 $rc=127;
1078 }
1079  
1080 unless ($Opt{conn}->{AutoCommit}) {
1081 $Opt{t}='r' if $rc;
1082  
1083 if ($Opt{t} eq 'c') {
1084 $Opt{conn}->commit
1085 }
1086 else {
1087 print STDERR "(W) ROOLBACK issued\n";
1088 $Opt{conn}->rollback;
1089 }
1090 }
1091  
1092 $Opt{conn}->disconnect;
1093 exit $rc;
1094 __END__