Oracle.pm untested

filter: Statementstmt 0.0% 0 / 1624 Branchbran 0.0% 0 / 650 Conditioncond 0.0% 0 / 128 MC/DCmcdc 0.0% 0 / 75 Subroutinesub 0.0% 0 / 84 Podpod 0.0% 0 / 82 totaltotal 0.0% 0 / 2643 CC 326 SCAR 50.4
CC
326
Cov
0%
_create_schema
870.0
_create_mview
306.0
_create_tablespace
272.0
CRAP 154.3
1 # $Id: Oracle.pm,v 1.51 2002/05/22 15:26:01 rvsutherland Exp $
2 #
3 # Copyright (c) 2000, 2001 Richard Sutherland - United States of America
4 #
5 # See COPYRIGHT section in pod text below for usage and distribution rights.
6 #
7  
8 require 5.004;
9  
10 BEGIN
11 {
12 $DDL::Oracle::VERSION = "1.11"; # Also update version in pod text below!
13 }
14  
15 package DDL::Oracle;
16  
17 use strict;
18  
19 my $block_size;
20 my $dbh;
21 my $ddl;
22 my $host;
23 my $instance;
24 my $isasnapindx;
25 my $isasnaptabl;
26 my $oracle_major;
27 my $oracle_minor;
28 my $oracle_release;
29 my $sth;
30  
31 my @size_arr;
32  
33 my %attr;
34  
35 my %compile =
36 (
37 'function' => \&_compile,
38 'package' => \&_compile_package,
39 'procedure' => \&_compile,
40 'trigger' => \&_compile,
41 'view' => \&_compile,
42 );
43  
44 my %create =
45 (
46 'comments' => \&_create_comments,
47 'components' => \&_create_components,
48 'constraint' => \&_create_constraint,
49 'database link' => \&_create_db_link,
50 'exchange index' => \&_create_exchange_index,
51 'exchange table' => \&_create_exchange_table,
52 'function' => \&_create_function,
53 'index' => \&_create_index,
54 'materialized view' => \&_create_materialized_view,
55 'materialized view log' => \&_create_materialized_view_log,
56 'package' => \&_create_package,
57 'package body' => \&_create_package_body,
58 'procedure' => \&_create_procedure,
59 'profile' => \&_create_profile,
60 'role' => \&_create_role,
61 'rollback segment' => \&_create_rollback_segment,
62 'schema' => \&_create_schema,
63 'sequence' => \&_create_sequence,
64 'snapshot' => \&_create_snapshot,
65 'snapshot log' => \&_create_snapshot_log,
66 'synonym' => \&_create_synonym,
67 'table' => \&_create_table,
68 'table family' => \&_create_table_family,
69 'tablespace' => \&_create_tablespace,
70 'trigger' => \&_create_trigger,
71 'type' => \&_create_type,
72 'user' => \&_create_user,
73 'view' => \&_create_view,
74 );
75  
76 my %drop =
77 (
78 'constraint' => \&_drop_constraint,
79 'database link' => \&_drop_database_link,
80 'dimension' => \&_drop_schema_object,
81 'directory' => \&_drop_object,
82 'function' => \&_drop_schema_object,
83 'index' => \&_drop_schema_object,
84 'library' => \&_drop_object,
85 'materialized view' => \&_drop_schema_object,
86 'materialized view log' => \&_drop_materialized_view_log,
87 'package' => \&_drop_schema_object,
88 'procedure' => \&_drop_schema_object,
89 'profile' => \&_drop_profile,
90 'role' => \&_drop_object,
91 'rollback segment' => \&_drop_object,
92 'sequence' => \&_drop_schema_object,
93 'snapshot' => \&_drop_schema_object,
94 'snapshot log' => \&_drop_snapshot_log,
95 'synonym' => \&_drop_synonym,
96 'table' => \&_drop_table,
97 'tablespace' => \&_drop_tablespace,
98 'trigger' => \&_drop_schema_object,
99 'type' => \&_drop_schema_object,
100 'user' => \&_drop_user,
101 'view' => \&_drop_schema_object,
102 );
103  
104 my %show_space =
105 (
106 'index' => \&_show_free_space,
107 'table' => \&_show_free_space,
108 'cluster' => \&_show_free_space,
109 );
110  
111 my %resize =
112 (
113 'index' => \&_resize_index,
114 'table' => \&_resize_table,
115 );
116  
117 ############################# Class Methods ############################
118  
119 sub configure
120 {
121 my ( $class, %args ) = @_;
122  
123 # Turn warnings off
124 $^W = 0;
125  
126 $dbh = $args{ 'dbh' };
127 $attr{ 'view' } = ( "\U$args{ 'view' }" eq 'USER' ) ? 'USER' : 'DBA';
128 $attr{ 'schema' } = ( exists $args{ 'schema' } ) ? $args{ 'schema' } : 1;
129 $attr{ 'resize' } = ( exists $args{ 'resize' } ) ? $args{ 'resize' } : 1;
130 $attr{ 'prompt' } = ( exists $args{ 'prompt' } ) ? $args{ 'prompt' } : 1;
131 $attr{ 'grants' } = ( exists $args{ 'grants' } ) ? $args{ 'grants' } : 0;
132 $attr{ 'heading' } = ( exists $args{ 'heading' } ) ? $args{ 'heading' } : 1;
133 $attr{ 'storage' } = ( exists $args{ 'storage' } ) ? $args{ 'storage' } : 1;
134 $attr{ 'tblspace'} = ( exists $args{ 'tblspace'} ) ? $args{ 'tblspace'} : 1;
135  
136 _set_sizing();
137 _get_oracle_release();
138 }
139  
140 sub new
141 {
142 my ( $class, %args ) = @_;
143  
144 my $self = {};
145  
146 $self->{ type } = $args{ type } || die
147 "\nAttribute 'type' is required " .
148 "in call to method 'new'.\n\n";;
149 $self->{ list } = $args{ list } || die
150 "\nAttribute 'list' is required " .
151 "in call to method 'new'.\n\n";;
152  
153 return bless $self, $class;
154 }
155  
156 ########################### Instance Methods ###########################
157  
158 sub compile
159 {
160 my $self = shift;
161 my $type = lc( $self->{ type } );
162  
163 die "\nObject type '$type' is invalid for 'compile' method.\n\n"
164 unless $compile{ $type };
165  
166 my $list = $self->{ list };
167 my $class = ref( $self );
168 _generate_heading( $class, 'COMPILE', $type, $list );
169  
170 foreach my $row ( @$list )
171 {
172 my ( $owner, $name ) = @$row;
173 my $schema = _set_schema( $owner );
174  
175 $ddl .= $compile{ $type }->(
176 $schema,
177 $owner,
178 $name,
179 $attr{ view },
180 $type,
181 );
182 }
183  
184 _scratch_prompts() unless $attr{ prompt };
185 return $ddl;
186 }
187  
188 sub create
189 {
190 my $self = shift;
191 my $type = lc( $self->{ type } );
192  
193 die "\nObject type '$type' is invalid for 'create' method.\n\n"
194 unless $create{ $type };
195  
196 my $list = $self->{ list };
197 my $class = ref( $self );
198 _generate_heading( $class, 'CREATE', $type, $list );
199  
200 foreach my $row ( @$list )
201 {
202 my ( $owner, $name ) = @$row;
203 my $schema = _set_schema( $owner );
204  
205 $ddl .= $create{ $type }->( $schema, $owner, $name, $attr{ view } );
206 }
207  
208 _scratch_prompts() unless $attr{ prompt };
209 return $ddl;
210 }
211  
212 sub drop
213 {
214 my $self = shift;
215 my $type = lc( $self->{ type } );
216  
217 die "\nObject type '$type' is invalid for 'drop' method.\n\n"
218 unless $drop{ $type };
219  
220 my $list = $self->{ list };
221 my $class = ref( $self );
222 _generate_heading( $class, 'DROP', $type, $list );
223  
224 foreach my $row ( @{ $self->{ list } } )
225 {
226 my ( $owner, $name ) = @$row;
227 my $schema = _set_schema( $owner );
228  
229 $ddl .= $drop{ $type }->( $schema, $name, $type, $owner, $attr{ view } );
230 }
231  
232 _scratch_prompts() unless $attr{ prompt };
233 return $ddl;
234 }
235  
236 sub show_space
237 {
238 my $self = shift;
239 my $type = lc( $self->{ type } );
240  
241 die "\nObject type '$type' is invalid for 'show_space' method.\n\n"
242 unless $show_space{ $type };
243  
244 my $list = $self->{ list };
245 my $class = ref( $self );
246 _generate_heading( $class, 'FREE SPACE', $type, $list );
247  
248 foreach my $row ( @{ $self->{ list } } )
249 {
250 my ( $owner, $name ) = @$row;
251  
252 $ddl .= $show_space{ $type }->( $owner, $name, $type, $attr{ view } );
253 }
254  
255 _scratch_prompts() unless $attr{ prompt };
256 return $ddl;
257 }
258  
259 sub resize
260 {
261 my $self = shift;
262 my $type = lc( $self->{ type } );
263  
264 die "\nObject type '$type' is invalid for 'resize' method.\n\n"
265 unless $resize{ $type };
266  
267 my $list = $self->{ list };
268 my $class = ref( $self );
269 _generate_heading( $class, 'ALTER', $type, $list );
270  
271 foreach my $row ( @{ $self->{ list } } )
272 {
273 my ( $owner, $name ) = @$row;
274 my $schema = _set_schema( $owner );
275  
276 $ddl .= $resize{ $type }->( $schema, $owner, $name, $attr{ view } );
277 }
278  
279 _scratch_prompts() unless $attr{ prompt };
280 return $ddl;
281 }
282  
283 ############################ Private Methods ###########################
284  
285 # sub _compile
286 #
287 # Returns DDL to compile the named object in the form of:
288 #
289 # ALTER <type> [schema.]<name> COMPILE [PACKAGE|BODY]
290 #
291 sub _compile
292 {
293 my ( $schema, $owner, $name, $view, $type ) = @_;
294  
295 $type = uc( $type );
296 my $type1 = ( $type eq 'PACKAGE BODY' ) ? 'PACKAGE' : $type;
297 my $type2 = ( $type eq 'PACKAGE BODY' ) ? ' BODY' :
298 ( $type eq 'PACKAGE' ) ? ' PACKAGE' : undef;
299  
300 my $stmt =
301 "
302 SELECT
303 'Standing tall with my boots on!'
304 FROM
305 ${view}_objects
306 WHERE
307 object_name = UPPER( ? )
308 AND object_type = ?
309 ";
310  
311 if ( $view eq 'DBA' )
312 {
313 $stmt .=
314 "
315 AND owner = UPPER('$owner')
316 ";
317 }
318  
319 $sth = $dbh->prepare( $stmt );
320 $sth->execute( $name, $type );
321 my @row = $sth->fetchrow_array;
322 die "\u\L$type \U$name \Ldoes not exist.\n\n" unless @row;
323  
324 return "PROMPT " .
325 "ALTER $type1 \L$schema$name \UCOMPILE$type2 \n\n" .
326 "ALTER $type1 \L$schema$name \UCOMPILE$type2 ;\n\n";
327 }
328  
329 # sub _compile_package
330 #
331 # Returns DDL to compile the named object in the form of:
332 #
333 # ALTER <type> [schema.]<name> COMPILE [PACKAGE|BODY]
334 #
335 sub _compile_package
336 {
337 my ( $schema, $owner, $name, $view, $type ) = @_;
338  
339 my $sql;
340 my $stmt =
341 "
342 SELECT
343 'Standing around with only my socks on!'
344 FROM
345 ${view}_objects
346 WHERE
347 object_name = UPPER( ? )
348 AND object_type = ?
349 ";
350  
351 if ( $view eq 'DBA' )
352 {
353 $stmt .=
354 "
355 AND owner = UPPER('$owner')
356 ";
357 }
358  
359 $sth = $dbh->prepare( $stmt );
360 $sth->execute( $name, 'PACKAGE' );
361 my @row = $sth->fetchrow_array;
362 die "Package \U$name \Ldoes not exist.\n\n" unless @row;
363  
364 $sql = _compile( @_ );
365  
366 $sth->execute( $name, 'PACKAGE BODY' );
367 @row = $sth->fetchrow_array;
368 $sql .= _compile( $schema, $owner, $name, $view, 'PACKAGE BODY' ) if @row;
369  
370 return $sql;
371 }
372  
373 #sub _constraint_columns
374 #
375 # Returns a formatted string containing the constraint columns.
376 #
377 sub _constraint_columns
378 {
379 my ( $owner, $name, $view, ) = @_;
380  
381 my $stmt =
382 "
383 SELECT
384 LOWER(column_name)
385 FROM
386 ${view}_cons_columns
387 WHERE
388 owner = UPPER( ? )
389 AND constraint_name = UPPER( ? )
390 ORDER
391 BY
392 position
393 ";
394  
395 $sth = $dbh->prepare( $stmt );
396 $sth->execute( $owner, $name );
397 my $aref = $sth->fetchall_arrayref;
398  
399 my @cols;
400 foreach my $row ( @$aref )
401 {
402 push @cols, $row->[0];
403 }
404  
405 return "(\n " .
406 join ( "\n , ", @cols ) .
407 "\n)\n";
408 }
409  
410 # sub _create_comments
411 #
412 # Returns DDL to create the comments on the named table and its columns
413 # in the form of:
414 #
415 # COMMENT ON TABLE [schema.]<name> IS '<text>'
416 # COMMENT ON COLUMN [schema.]<name>.<column> IS '<text>'
417 #
418 sub _create_comments
419 {
420 my ( $schema, $owner, $name, $view ) = @_;
421  
422 my $sql;
423 my $stmt =
424 "
425 SELECT
426 comments
427 FROM
428 ${view}_tab_comments
429 WHERE
430 table_name = UPPER( ? )
431 AND comments IS NOT null
432 ";
433  
434 if ( $view eq 'DBA' )
435 {
436 $stmt .=
437 "
438 AND owner = UPPER('$owner')
439 ";
440 }
441  
442 $sth = $dbh->prepare( $stmt );
443 $sth->execute( $name );
444 my $aref = $sth->fetchall_arrayref;
445  
446 foreach my $row ( @$aref )
447 {
448 @$row->[0] =~ s/'/''/g;
449  
450 $sql .= "PROMPT " .
451 "COMMENT ON TABLE \L$schema$name \UIS \E'@$row->[0]' \n\n" .
452 "COMMENT ON TABLE \L$schema$name \UIS \E'@$row->[0]' ;\n\n";
453 }
454  
455 $stmt =
456 "
457 SELECT
458 column_name
459 , comments
460 FROM
461 ${view}_col_comments
462 WHERE
463 table_name = UPPER( ? )
464 AND comments IS NOT null
465 ";
466  
467 if ( $view eq 'DBA' )
468 {
469 $stmt .=
470 "
471 AND owner = UPPER('$owner')
472 ";
473 }
474  
475 $sth = $dbh->prepare( $stmt );
476 $sth->execute( $name );
477 $aref = $sth->fetchall_arrayref;
478  
479 foreach my $row ( @$aref )
480 {
481 @$row->[1] =~ s/'/''/g;
482  
483 $sql .= "PROMPT " .
484 "COMMENT ON COLUMN \L$schema$name.@$row->[0] " .
485 "IS '@$row->[1]' \n\n" .
486 "COMMENT ON COLUMN \L$schema$name.@$row->[0] " .
487 "IS '@$row->[1]' ;\n\n";
488 }
489  
490 return $sql;
491 }
492  
493 # sub _create_components
494 #
495 # Returns DDL to create ALL objects of the following object types:
496 #
497 # TABLESPACE
498 # ROLLBACK SEGMENT
499 # PUBLIC DATABASE LINK
500 # PUBLIC SYNONYM
501 # PROFILE
502 # ROLE
503 # USER
504 #
505 sub _create_components
506 {
507 ( undef, undef, undef, my $view ) = @_;
508  
509 die "\nYou must use the DBA views in order to CREATE COMPONENTS\n\n"
510 unless $view eq 'DBA';
511  
512 my $sql;
513 my $stmt;
514 my $aref;
515 my $row;
516  
517 # Get tablespaces
518 $stmt =
519 "
520 SELECT
521 tablespace_name
522 FROM
523 dba_tablespaces
524 ORDER
525 BY
526 tablespace_name
527 ";
528  
529 $sth = $dbh->prepare( $stmt );
530 $sth->execute;
531 $aref = $sth->fetchall_arrayref;
532  
533 foreach $row ( @$aref )
534 {
535 $sql .= _create_tablespace( undef, undef, @$row->[0], $view );
536 }
537  
538 # Get rollback segments
539 $stmt =
540 "
541 SELECT
542 segment_name
543 FROM
544 dba_rollback_segs
545 ORDER
546 BY
547 segment_name
548 ";
549  
550 $sth = $dbh->prepare( $stmt );
551 $sth->execute;
552 $aref = $sth->fetchall_arrayref;
553  
554 foreach $row ( @$aref )
555 {
556 $sql .= _create_rollback_segment( undef, undef, @$row->[0], $view );
557 }
558  
559 # Get PUBLIC database links
560 $stmt =
561 "
562 SELECT
563 db_link
564 FROM
565 dba_db_links
566 WHERE
567 owner = 'PUBLIC'
568 ORDER
569 BY
570 db_link
571 ";
572  
573 $sth = $dbh->prepare( $stmt );
574 $sth->execute;
575 $aref = $sth->fetchall_arrayref;
576  
577 foreach $row ( @$aref )
578 {
579 $sql .= _create_db_link( 'PUBLIC ', 'PUBLIC', @$row->[0], $view );
580 }
581  
582 # Get PUBLIC synonyms
583 $stmt =
584 "
585 SELECT
586 owner
587 , synonym_name
588 FROM
589 dba_synonyms
590 WHERE
591 owner = 'PUBLIC'
592 ORDER
593 BY
594 owner
595 , synonym_name
596 ";
597  
598 $sth = $dbh->prepare( $stmt );
599 $sth->execute;
600 $aref = $sth->fetchall_arrayref;
601  
602 foreach $row ( @$aref )
603 {
604 my ( $owner, $name ) = @$row;
605 my $schema = _set_schema( $owner );
606  
607 $sql .= _create_synonym( $schema, $owner, $name, $view );
608 }
609  
610 # Get profiles
611 $stmt =
612 "
613 SELECT DISTINCT
614 profile
615 FROM
616 dba_profiles
617 ORDER
618 BY
619 profile
620 ";
621  
622 $sth = $dbh->prepare( $stmt );
623 $sth->execute;
624 $aref = $sth->fetchall_arrayref;
625  
626 foreach $row ( @$aref )
627 {
628 $sql .= _create_profile( undef, undef, @$row->[0], $view );
629 }
630  
631 # Get roles
632 $stmt =
633 "
634 SELECT
635 role
636 FROM
637 dba_roles
638 ORDER
639 BY
640 role
641 ";
642  
643 $sth = $dbh->prepare( $stmt );
644 $sth->execute;
645 $aref = $sth->fetchall_arrayref;
646  
647 foreach $row ( @$aref )
648 {
649 $sql .= _create_role( undef, undef, @$row->[0], $view );
650 }
651  
652 # Get users
653 $stmt =
654 "
655 SELECT
656 username
657 FROM
658 dba_users
659 WHERE
660 username NOT IN (
661 'NEXT_USER'
662 , 'PUBLIC'
663 , 'SYS'
664 , 'SYSTEM'
665 )
666 ORDER
667 BY
668 username
669 ";
670  
671 $sth = $dbh->prepare( $stmt );
672 $sth->execute;
673 $aref = $sth->fetchall_arrayref;
674  
675 foreach $row ( @$aref )
676 {
677 $sql .= _create_user( undef, undef, @$row->[0], $view );
678 }
679  
680 return $sql;
681 }
682  
683 #
684 #
685 # sub _create_constraint
686 #
687 # Returns DDL to create the named constraint in the form of:
688 #
689 # ALTER TABLE [schema.]<name> ADD CONSTRAINT <name> <TYPE>
690 # [<column list>
691 #
692 sub _create_constraint
693 {
694 my ( $schema, $owner, $name, $view ) = @_;
695  
696 my $sql;
697 my $stmt;
698  
699 if ( $oracle_major == 7 )
700 {
701 $stmt =
702 "
703 SELECT
704 table_name
705 , constraint_type
706 , search_condition
707 , r_owner
708 , r_constraint_name
709 , delete_rule
710 , DECODE(
711 status
712 ,'ENABLED','ENABLE'
713 , 'DISABLE'
714 ) as enagle
715 FROM
716 ${view}_constraints cn
717 WHERE
718 owner = UPPER( ? )
719 AND constraint_name = UPPER( ? )
720 ";
721 }
722 else
723 {
724 $stmt =
725 "
726 SELECT
727 table_name
728 , constraint_type
729 , search_condition
730 , r_owner
731 , r_constraint_name
732 , delete_rule
733 , DECODE(
734 status
735 ,'ENABLED','ENABLE'
736 , 'DISABLE'
737 ) as enagle
738 , deferrable
739 , deferred
740 FROM
741 ${view}_constraints cn
742 WHERE
743 owner = UPPER( ? )
744 AND constraint_name = UPPER( ? )
745 ";
746 }
747  
748 $dbh->{ LongReadLen } = 8192; # Allows SEARCH_CONDITION length of 8K
749 $dbh->{ LongTruncOk } = 1;
750  
751 $sth = $dbh->prepare( $stmt );
752 $sth->execute( $owner, $name );
753 my @row = $sth->fetchrow_array;
754  
755 my (
756 $table,
757 $cons_type,
758 $condition,
759 $r_owner,
760 $r_cons_name,
761 $delete_rule,
762 $enable,
763 $deferrable,
764 $deferred,
765 ) = @row;
766  
767 $sql = "PROMPT " .
768 "ALTER TABLE \L$schema$table \UADD CONSTRAINT \L$name ";
769 $sql .= ( $cons_type eq 'P' ) ? "PRIMARY KEY\n\n" :
770 ( $cons_type eq 'U' ) ? "UNIQUE\n\n" :
771 ( $cons_type eq 'R' ) ? "FOREIGN KEY\n\n" :
772 "CHECK\n\n";
773  
774 $sql .= "ALTER TABLE \L$schema$table \UADD CONSTRAINT \L$name " ;
775 $sql .= ( $cons_type eq 'P' ) ? "PRIMARY KEY\n" :
776 ( $cons_type eq 'U' ) ? "UNIQUE\n" :
777 ( $cons_type eq 'R' ) ? "FOREIGN KEY\n" :
778 "\nCHECK ($condition)\n";
779  
780 if ( $cons_type ne 'C' )
781 {
782 $sql .= _constraint_columns( $owner, $name, $view, );
783 }
784  
785 if ( $cons_type eq 'R' )
786 {
787 $stmt =
788 "
789 SELECT
790 table_name
791 FROM
792 ${view}_constraints
793 WHERE
794 constraint_name = UPPER( ? )
795 AND owner = UPPER( ? )
796 ";
797  
798 $sth = $dbh->prepare( $stmt );
799 $sth->execute( $r_cons_name, $r_owner );
800 my ( $table_name ) = $sth->fetchrow_array;
801  
802 $sql .= "REFERENCES \L$r_owner\.$table_name\n" .
803 _constraint_columns( $r_owner, $r_cons_name, $view );
804  
805 if ( $delete_rule eq 'CASCADE' )
806 {
807 $sql .= "ON DELETE CASCADE\n";
808 }
809 }
810  
811 if ( $oracle_major < 8 )
812 {
813 # Syntax for Oracle7 doesn't like the ENABLE clause
814 $sql .= "$enable\n" unless $enable eq 'ENABLE';
815 }
816 else
817 {
818 $sql .= "$deferrable\n" .
819 "INITIALLY $deferred\n";
820  
821 if ( $enable eq 'ENABLE' )
822 {
823 $sql .= "ENABLE NOVALIDATE\n";
824 }
825 else
826 {
827 $sql .= "DISABLE\n";
828 }
829 }
830  
831 return $sql .
832 ";\n\n";
833 }
834  
835 # sub _create_db_link
836 #
837 # Returns DDL to create the named database link in the form of:
838 #
839 # CREATE [PUBLIC] DATABASE LINK <name>
840 # CONNECT TO <user> IDENTIFIED BY <password>
841 # USING '<connect string>'
842 #
843 sub _create_db_link
844 {
845 my ( $schema, $owner, $name, $view ) = @_;
846  
847 my $msg = "\nYou must use the DBA views in order to " .
848 "CREATE a PUBLIC DATABASE LINK\n\n";
849 if ( "\U$owner" eq 'PUBLIC' and $view ne 'DBA' )
850 {
851 die $msg;
852 }
853  
854 my $sql;
855 my $stmt;
856  
857 if ( $view eq 'DBA' )
858 {
859 $stmt =
860 "
861 SELECT
862 l.userid
863 , l.password
864 , l.host
865 FROM
866 sys.link\$ l
867 , sys.user\$ u
868 WHERE
869 u.name = UPPER('$owner')
870 AND l.owner\# = u.user\#
871 AND l.name LIKE UPPER('${name}%')
872 ";
873 }
874 else # view is USER
875 {
876 $stmt =
877 "
878 SELECT
879 username
880 , password
881 , host
882 FROM
883 user_db_links
884 WHERE
885 db_link LIKE UPPER('${name}%')
886 ";
887 }
888  
889 $sth = $dbh->prepare( $stmt );
890 $sth->execute;
891 my @row = $sth->fetchrow_array;
892 die "Database Link \U$name \Ldoes not exist.\n\n" unless @row;
893  
894 my ( $user, $password, $host ) = @row;
895  
896 my $is_public = ( "\U$owner" eq 'PUBLIC' ) ? ' PUBLIC' : '';
897  
898 return "PROMPT " .
899 "CREATE$is_public DATABASE LINK \L$name\n\n" .
900 "CREATE$is_public DATABASE LINK \L$name\n" .
901 "CONNECT TO \L$user \UIDENTIFIED BY \L$password\n" .
902 "USING '$host'\n" .
903 ";\n\n";
904 }
905  
906 # sub _create_exchange_index
907 #
908 # Returns DDL to create a temporary table as a mirror of the named partition.
909 # See sub _create_table for the format. Physical attributes come from the
910 # partition.
911 #
912 sub _create_exchange_index
913 {
914 my ( $schema, $owner, $name, $view ) = @_;
915  
916 ( $name, my $partition ) = split /:/, $name;
917  
918 my $sql;
919 my $stmt =
920 "
921 SELECT
922 SUBSTR(segment_type,7) AS type
923 , blocks
924 FROM
925 ${view}_segments
926 WHERE
927 segment_name = UPPER( ? )
928 AND partition_name = UPPER( ? )
929 ";
930  
931 if ( $view eq 'DBA' )
932 {
933 $stmt .=
934 "
935 AND owner = UPPER('$owner')
936 ";
937 }
938  
939 $sth = $dbh->prepare( $stmt );
940 $sth->execute( $name, $partition );
941 my @row = $sth->fetchrow_array;
942 die "Partition \U$partition \Lof \EIndex \U$name \Ldoes not exist,\n"
943 unless @row;
944  
945 my (
946 $type,
947 $blocks,
948 ) = @row;
949  
950 $stmt =
951 "
952 SELECT
953 LTRIM(i.degree)
954 , LTRIM(i.instances)
955 , i.table_name
956 , DECODE(
957 i.uniqueness
958 ,'UNIQUE',' UNIQUE'
959 ,null
960 ) AS uniqueness
961 , DECODE(
962 i.index_type
963 ,'BITMAP',' BITMAP'
964 ,null
965 ) AS index_type
966 -- Physical Properties
967 , 'INDEX' AS organization
968 -- Segment Attributes
969 , 'N/A' AS cache
970 , 'N/A' AS pct_used
971 , p.pct_free
972 , DECODE(
973 p.ini_trans
974 ,0,1
975 ,null,1
976 ,p.ini_trans
977 ) AS ini_trans
978 , DECODE(
979 p.max_trans
980 ,0,255
981 ,null,255
982 ,p.max_trans
983 ) AS max_trans
984 -- Storage Clause
985 , p.initial_extent
986 , p.next_extent
987 , p.min_extent
988 , DECODE(
989 p.max_extent
990 ,2147483645,'unlimited'
991 , p.max_extent
992 ) AS max_extent
993 , p.pct_increase
994 , NVL(p.freelists,1)
995 , NVL(p.freelist_groups,1)
996 , LOWER(p.buffer_pool) AS buffer_pool
997 , DECODE(
998 p.logging
999 ,'NO','NOLOGGING'
1000 , 'LOGGING'
1001 ) AS logging
1002 , LOWER(p.tablespace_name) AS tablespace_name
1003 , $blocks AS blocks
1004 FROM
1005 ${view}_indexes i
1006 , ${view}_ind_${type}s p
1007 WHERE
1008 p.index_name = UPPER( ? )
1009 AND p.${type}_name = UPPER( ? )
1010 AND i.index_name = p.index_name
1011 ";
1012  
1013 if ( $view eq 'DBA' )
1014 {
1015 $stmt .=
1016 "
1017 AND p.index_owner = UPPER('$owner')
1018 AND i.owner = p.index_owner
1019 ";
1020 }
1021  
1022 $sth = $dbh->prepare( $stmt );
1023 $sth->execute( $name, $partition );
1024 @row = $sth->fetchrow_array;
1025  
1026 my $degree = shift @row;
1027 my $instances = shift @row;
1028 my $table = shift @row;
1029 my $unique = shift @row;
1030 my $bitmap = shift @row;
1031  
1032 $sql = "PROMPT " .
1033 "CREATE$unique$bitmap INDEX \L$schema$name \UON \L$schema$table\n\n" .
1034 "CREATE$unique$bitmap INDEX \L$schema$name \UON \L$schema$table\n" .
1035 _index_columns( '', $owner, $name, $view, ) .
1036 "PARALLEL\n" .
1037 "(\n" .
1038 " DEGREE $degree\n" .
1039 " INSTANCES $instances\n" .
1040 ")\n";
1041  
1042 unshift @row, ( '' ); # Indent (none)
1043  
1044 $sql .= _segment_attributes( \@row ) .
1045 ";\n\n";
1046  
1047 return $sql;
1048 }
1049  
1050 # sub _create_exchange_table
1051 #
1052 # Returns DDL to create a temporary table as a mirror of the named partition.
1053 # See sub _create_table for the format. Physical attributes come from the
1054 # partition.
1055 #
1056 sub _create_exchange_table
1057 {
1058 my ( $schema, $owner, $name, $view ) = @_;
1059  
1060 ( $name, my $partition ) = split /:/, $name;
1061  
1062 my $stmt =
1063 "
1064 SELECT
1065 SUBSTR(segment_type,7) AS type
1066 , blocks
1067 FROM
1068 ${view}_segments
1069 WHERE
1070 segment_name = UPPER( '$name' )
1071 AND partition_name = UPPER( ? )
1072 ";
1073  
1074 if ( $view eq 'DBA' )
1075 {
1076 $stmt .=
1077 "
1078 AND owner = UPPER('$owner')
1079 ";
1080 }
1081  
1082 $sth = $dbh->prepare( $stmt );
1083 $sth->execute( $partition );
1084 my @row = $sth->fetchrow_array;
1085 die "Partition \U$partition \Lof \ETable \U$name \Ldoes not exist,\n"
1086 unless @row;
1087  
1088 my (
1089 $type,
1090 $blocks,
1091 ) = @row;
1092  
1093 $stmt =
1094 "
1095 SELECT
1096 DECODE(
1097 t.monitoring
1098 ,'NO','NOMONITORING'
1099 , 'MONITORING'
1100 ) AS monitoring
1101 , t.table_name
1102 , LTRIM(t.degree) AS degree
1103 , LTRIM(t.instances) AS instances
1104 , 'HEAP' AS organization
1105 , DECODE(
1106 t.cache
1107 ,'y','CACHE'
1108 , 'NOCACHE'
1109 ) AS cache
1110 , p.pct_used
1111 , p.pct_free
1112 , p.ini_trans
1113 , p.max_trans
1114 , p.initial_extent
1115 , p.next_extent
1116 , p.min_extent
1117 , DECODE(
1118 p.max_extent
1119 ,2147483645,'unlimited'
1120 ,p.max_extent
1121 ) AS max_extents
1122 , p.pct_increase
1123 , p.freelists
1124 , p.freelist_groups
1125 , LOWER(p.buffer_pool) AS buffer_pool
1126 , DECODE(
1127 p.logging
1128 ,'NO','NOLOGGING'
1129 , 'LOGGING'
1130 ) AS logging
1131 , LOWER(p.tablespace_name) AS tablespace_name
1132 , $blocks - NVL(p.empty_blocks,0) AS blocks
1133 FROM
1134 dba_tables t
1135 , dba_tab_${type}s p
1136 WHERE
1137 p.table_name = UPPER('$name')
1138 AND p.${type}_name = UPPER('$partition')
1139 AND t.table_name = p.table_name
1140 ";
1141  
1142 if ( $view eq 'DBA' )
1143 {
1144 $stmt .=
1145 "
1146 AND p.table_owner = UPPER('$owner')
1147 AND t.owner = p.table_owner
1148 ";
1149 }
1150  
1151 return _create_table_text( $stmt, $schema, $owner, $name, $view ) .
1152 ";\n\n";
1153 }
1154  
1155 # sub _create_function
1156 #
1157 # Returns DDL to create the named procedure in the form of:
1158 #
1159 # CREATE OR REPLACE FUNCTION [schema.]<name>
1160 # AS
1161 # <source>
1162 #
1163 # by calling _display_source
1164 #
1165 sub _create_function
1166 {
1167 my ( $schema, $owner, $name, $view ) = @_;
1168  
1169 my $sql = _display_source( $schema, $owner, $name, $view, 'FUNCTION' );
1170  
1171 if ( $attr{ 'grants' } )
1172 {
1173 $sql .= _object_privs( $schema, $owner, $name, $view );
1174 }
1175  
1176 return $sql ;
1177 }
1178  
1179 # sub _create_index
1180 #
1181 # Returns DDL to create the named index and its partition(s) in the form of:
1182 #
1183 # CREATE INDEX [schema1.]<name> ON [schema2.]<table>
1184 # (
1185 # <column list>
1186 # )
1187 # INDEXTYPE IS <value> -- for domain indexes
1188 # PARAMETERS <value> -- for domain indexes
1189 # [NO]MONIOTORING
1190 # PARALLEL
1191 # (
1192 # DEGREE <value>
1193 # INSTANCES <value>
1194 # )
1195 # PCTFREE <value>
1196 # INITRANS <value>
1197 # MAXTRANS <value>
1198 # STORAGE
1199 # (
1200 # <storage clause>
1201 # )
1202 # [NO]LOGING
1203 # TABLESPACE <name>
1204 # [<partitioning clause>]
1205 #
1206 sub _create_index
1207 {
1208 my ( $schema, $owner, $name, $view ) = @_;
1209  
1210 my $sql;
1211 my $stmt;
1212  
1213 if ( $oracle_major == 7 )
1214 {
1215 $stmt =
1216 "
1217 SELECT
1218 'N/A' AS partitioned
1219 , table_name
1220 , table_owner
1221 , DECODE(
1222 uniqueness
1223 ,'UNIQUE',' UNIQUE'
1224 ,null
1225 )
1226 , null AS bitmap
1227 , null AS domain
1228 FROM
1229 ${view}_indexes
1230 WHERE
1231 index_name = UPPER( ? )
1232 ";
1233 }
1234 else # We're Oracle8 or newer
1235 {
1236 $stmt =
1237 "
1238 SELECT
1239 partitioned
1240 , table_name
1241 , table_owner
1242 , DECODE(
1243 uniqueness
1244 ,'UNIQUE',' UNIQUE'
1245 ,null
1246 )
1247 , DECODE(
1248 index_type
1249 ,'BITMAP',' BITMAP'
1250 ,null
1251 )
1252 , DECODE(
1253 index_type
1254 ,'DOMAIN','DOMAIN'
1255 ,null
1256 )
1257 FROM
1258 ${view}_indexes
1259 WHERE
1260 index_name = UPPER( ? )
1261 ";
1262 }
1263  
1264 if ( $view eq 'DBA' )
1265 {
1266 $stmt .=
1267 "
1268 AND owner = UPPER('$owner')
1269 ";
1270 }
1271  
1272 $sth = $dbh->prepare( $stmt );
1273 $sth->execute( $name );
1274 my @row = $sth->fetchrow_array;
1275 die "Index \U$name \Ldoes not exist.\n\n" unless @row;
1276  
1277 my (
1278 $partitioned,
1279 $table,
1280 $table_owner,
1281 $unique,
1282 $bitmap,
1283 $domain,
1284 ) = @row;
1285  
1286 my (
1287 $dom_owner,
1288 $dom_name,
1289 $dom_param
1290 );
1291  
1292 if ( $domain eq 'DOMAIN')
1293 {
1294 $stmt =
1295 "
1296 SELECT
1297 ityp_owner
1298 , ityp_name
1299 , parameters
1300 FROM
1301 ${view}_indexes
1302 WHERE
1303 index_name = UPPER( ? )
1304 ";
1305  
1306 if ( $view eq 'DBA' )
1307 {
1308 $stmt .=
1309 "
1310 AND owner = UPPER('$owner')
1311 ";
1312 }
1313  
1314 $sth = $dbh->prepare( $stmt );
1315 $sth->execute( $name );
1316 my @row = $sth->fetchrow_array;
1317  
1318 (
1319 $dom_owner,
1320 $dom_name,
1321 $dom_param
1322 ) = @row;
1323 }
1324  
1325 if ( $oracle_major == 7 )
1326 {
1327 $stmt =
1328 "
1329 SELECT
1330 'N/A' AS degree
1331 , 'N/A' AS instances
1332 , 0 AS compressed
1333 -- Physical Properties
1334 , 'INDEX' AS organization
1335 -- Segment Attributes
1336 , 'N/A' AS cache
1337 , 'N/A' AS pct_used
1338 , i.pct_free
1339 , DECODE(
1340 i.ini_trans
1341 ,0,1
1342 ,null,1
1343 ,i.ini_trans
1344 ) AS ini_trans
1345 , DECODE(
1346 i.max_trans
1347 ,0,255
1348 ,null,255
1349 ,i.max_trans
1350 ) AS max_trans
1351 -- Storage Clause
1352 , i.initial_extent
1353 , i.next_extent
1354 , i.min_extents
1355 , DECODE(
1356 i.max_extents
1357 ,2147483645,'unlimited'
1358 , i.max_extents
1359 ) AS max_extents
1360 , i.pct_increase
1361 , NVL(i.freelists,1)
1362 , NVL(i.freelist_groups,1)
1363 , 'N/A' AS buffer_pool
1364 , 'N/A' AS logging
1365 , LOWER(i.tablespace_name) AS tablespace_name
1366 , s.blocks
1367 FROM
1368 ${view}_indexes i
1369 , ${view}_segments s
1370 WHERE
1371 i.index_name = UPPER( ? )
1372 AND s.segment_name = i.index_name
1373 ";
1374 }
1375 elsif ( $oracle_major == 8 and $oracle_minor == 0 )
1376 {
1377 $stmt =
1378 "
1379 SELECT
1380 LTRIM(i.degree)
1381 , LTRIM(i.instances)
1382 , 0 AS compressed
1383 -- Physical Properties
1384 , 'INDEX' AS organization
1385 -- Segment Attributes
1386 , 'N/A' AS cache
1387 , 'N/A' AS pct_used
1388 , i.pct_free
1389 , DECODE(
1390 i.ini_trans
1391 ,0,1
1392 ,null,1
1393 ,i.ini_trans
1394 ) AS ini_trans
1395 , DECODE(
1396 i.max_trans
1397 ,0,255
1398 ,null,255
1399 ,i.max_trans
1400 ) AS max_trans
1401 -- Storage Clause
1402 , i.initial_extent
1403 , i.next_extent
1404 , i.min_extents
1405 , DECODE(
1406 i.max_extents
1407 ,2147483645,'unlimited'
1408 , i.max_extents
1409 ) AS max_extents
1410 , i.pct_increase
1411 , NVL(i.freelists,1)
1412 , NVL(i.freelist_groups,1)
1413 , LOWER(i.buffer_pool) AS buffer_pool
1414 , DECODE(
1415 i.logging
1416 ,'NO','NOLOGGING'
1417 , 'LOGGING'
1418 ) AS logging
1419 , LOWER(i.tablespace_name) AS tablespace_name
1420 , s.blocks
1421 FROM
1422 ${view}_indexes i
1423 , ${view}_segments s
1424 WHERE
1425 i.index_name = UPPER( ? )
1426 AND s.segment_name = i.index_name
1427 ";
1428 }
1429 else # We're Oracle8i or newer
1430 {
1431 $stmt =
1432 "
1433 SELECT
1434 LTRIM(i.degree)
1435 , LTRIM(i.instances)
1436 , DECODE(
1437 i.compression
1438 ,'ENABLED',i.prefix_length
1439 ,0
1440 ) AS compressed
1441 -- Physical Properties
1442 , 'INDEX' AS organization
1443 -- Segment Attributes
1444 , 'N/A' AS cache
1445 , 'N/A' AS pct_used
1446 , i.pct_free
1447 , DECODE(
1448 i.ini_trans
1449 ,0,1
1450 ,null,1
1451 ,i.ini_trans
1452 ) AS ini_trans
1453 , DECODE(
1454 i.max_trans
1455 ,0,255
1456 ,null,255
1457 ,i.max_trans
1458 ) AS max_trans
1459 -- Storage Clause
1460 , i.initial_extent
1461 , i.next_extent
1462 , i.min_extents
1463 , DECODE(
1464 i.max_extents
1465 ,2147483645,'unlimited'
1466 , i.max_extents
1467 ) AS max_extents
1468 , i.pct_increase
1469 , NVL(i.freelists,1)
1470 , NVL(i.freelist_groups,1)
1471 , LOWER(i.buffer_pool) AS buffer_pool
1472 , DECODE(
1473 i.logging
1474 ,'NO','NOLOGGING'
1475 , 'LOGGING'
1476 ) AS logging
1477 , LOWER(i.tablespace_name) AS tablespace_name
1478 , s.blocks
1479 FROM
1480 ${view}_indexes i
1481 , ${view}_segments s
1482 WHERE
1483 i.index_name = UPPER( ? )
1484 AND s.segment_name = i.index_name
1485 ";
1486 }
1487  
1488 if ( $view eq 'DBA' )
1489 {
1490 $stmt .=
1491 "
1492 AND i.owner = UPPER('$owner')
1493 AND s.owner = i.owner
1494 ";
1495 }
1496  
1497 $sth = $dbh->prepare( $stmt );
1498 $sth->execute( $name );
1499 @row = $sth->fetchrow_array;
1500  
1501 my $degree = shift @row;
1502 my $instances = shift @row;
1503 my $compressed = shift @row;
1504  
1505 my $schema2 = _set_schema( $table_owner );
1506  
1507 $sql = "PROMPT " .
1508 "CREATE$unique$bitmap INDEX \L$schema$name \UON \L$schema2$table\n\n" .
1509 "CREATE$unique$bitmap INDEX \L$schema$name \UON \L$schema2$table\n" .
1510 _index_columns( '', $owner, $name, $view, );
1511  
1512 if ( $domain eq 'DOMAIN' )
1513 {
1514 return $sql .
1515 qq!INDEXTYPE IS "$dom_owner"."$dom_name"\nPARAMETERS ('$dom_param') ;\n\n!;
1516 }
1517  
1518 if ( $oracle_major > 7 )
1519 {
1520 $sql .= "PARALLEL\n" .
1521 "(\n" .
1522 " DEGREE $degree\n" .
1523 " INSTANCES $instances\n" .
1524 ")\n";
1525 }
1526  
1527 if ( $partitioned eq 'YES' )
1528 {
1529 return _create_partitioned_index( $schema, $owner, $name, $view, $sql )
1530 }
1531 else # Plain ol' non-partitioned index
1532 {
1533 unshift @row, ( '' ); # Indent (none)
1534  
1535 $sql .= _segment_attributes( \@row );
1536  
1537 if ( $compressed )
1538 {
1539 $sql .= "COMPRESS $compressed\n";
1540 }
1541  
1542 return $sql .= ";\n\n";
1543 }
1544 }
1545  
1546 # sub _create_iot
1547 #
1548 # Returns DDL to create the index organized table and its partition(s).
1549 # See _create_table for format.
1550 #
1551 sub _create_iot
1552 {
1553 my ( $schema, $owner, $name, $view ) = @_;
1554  
1555 my $stmt;
1556  
1557 if ( $oracle_major == 8 and $oracle_minor == 0 )
1558 {
1559 $stmt =
1560 "
1561 SELECT
1562 'N/A' AS monitoring
1563 , logging
1564 , blocks - NVL(empty_blocks,0)
1565 FROM
1566 ${view}_all_tables
1567 WHERE
1568 table_name = UPPER( ? )
1569 ";
1570 }
1571 else
1572 {
1573 $stmt =
1574 "
1575 SELECT
1576 DECODE(
1577 monitoring
1578 ,'NO','NOMONITORING'
1579 , 'MONITORING'
1580 ) AS monitoring
1581 , logging
1582 , blocks - NVL(empty_blocks,0)
1583 FROM
1584 ${view}_all_tables
1585 WHERE
1586 table_name = UPPER( ? )
1587 ";
1588 }
1589  
1590 if ( $view eq 'DBA' )
1591 {
1592 $stmt .=
1593 "
1594 AND owner = UPPER('$owner')
1595 ";
1596 }
1597  
1598 my $sth = $dbh->prepare( $stmt );
1599 $sth->execute( $name );
1600 my (
1601 $monitoring,
1602 $logging,
1603 $blocks,
1604 ) = $sth->fetchrow_array;
1605  
1606 if ( $oracle_major == 8 and $oracle_minor == 0 )
1607 {
1608 $stmt =
1609 "
1610 SELECT
1611 -- Table Properties
1612 'N/A' AS monitoring
1613 , 'N/A' AS table_name
1614 -- Parallel Clause
1615 , LTRIM(degree)
1616 , LTRIM(instances)
1617 -- Physical Properties
1618 , 'INDEX' AS organization
1619 -- Segment Attributes
1620 , 'N/A' AS cache
1621 , 'N/A' AS pct_used
1622 , pct_free
1623 , DECODE(
1624 ini_trans
1625 ,0,1
1626 ,null,1
1627 ,ini_trans
1628 ) AS ini_trans
1629 , DECODE(
1630 max_trans
1631 ,0,255
1632 ,null,255
1633 ,max_trans
1634 ) AS max_trans
1635 -- Storage Clause
1636 , initial_extent
1637 , next_extent
1638 , min_extents
1639 , DECODE(
1640 max_extents
1641 ,2147483645,'unlimited'
1642 , max_extents
1643 ) AS max_extents
1644 , pct_increase
1645 , NVL(freelists,1)
1646 , NVL(freelist_groups,1)
1647 , LOWER(buffer_pool) AS buffer_pool
1648 , DECODE(
1649 '$logging'
1650 ,'NO','NOLOGGING'
1651 , 'LOGGING'
1652 ) AS logging
1653 , LOWER(tablespace_name) AS tablespace_name
1654 , DECODE(
1655 '$blocks'
1656 ,null,GREATEST(initial_extent,next_extent)
1657 / ($block_size * 1024)
1658 ,'0' ,GREATEST(initial_extent,next_extent)
1659 / ($block_size * 1024)
1660 ,'$blocks'
1661 ) AS blocks
1662 FROM
1663 ${view}_indexes
1664 WHERE
1665 table_name = UPPER('$name')
1666 ";
1667 }
1668 else
1669 {
1670 $stmt =
1671 "
1672 SELECT
1673 -- Table Properties
1674 DECODE(
1675 '$monitoring'
1676 ,'NO','NOMONITORING'
1677 , 'MONITORING'
1678 )
1679 , 'N/A' AS table_name
1680 -- Parallel Clause
1681 , LTRIM(degree)
1682 , LTRIM(instances)
1683 -- Physical Properties
1684 , 'INDEX' AS organization
1685 -- Segment Attributes
1686 , 'N/A' AS cache
1687 , 'N/A' AS pct_used
1688 , pct_free
1689 , DECODE(
1690 ini_trans
1691 ,0,1
1692 ,null,1
1693 ,ini_trans
1694 ) AS ini_trans
1695 , DECODE(
1696 max_trans
1697 ,0,255
1698 ,null,255
1699 ,max_trans
1700 ) AS max_trans
1701 -- Storage Clause
1702 , initial_extent
1703 , next_extent
1704 , min_extents
1705 , DECODE(
1706 max_extents
1707 ,2147483645,'unlimited'
1708 , max_extents
1709 ) AS max_extents
1710 , pct_increase
1711 , NVL(freelists,1)
1712 , NVL(freelist_groups,1)
1713 , LOWER(buffer_pool) AS buffer_pool
1714 , DECODE(
1715 '$logging'
1716 ,'NO','NOLOGGING'
1717 , 'LOGGING'
1718 ) AS logging
1719 , LOWER(tablespace_name) AS tablespace_name
1720 , DECODE(
1721 '$blocks'
1722 ,null,GREATEST(initial_extent,next_extent)
1723 / ($block_size * 1024)
1724 ,'0' ,GREATEST(initial_extent,next_extent)
1725 / ($block_size * 1024)
1726 ,'$blocks'
1727 ) AS blocks
1728 FROM
1729 ${view}_indexes
1730 WHERE
1731 table_name = UPPER('$name')
1732 ";
1733 }
1734  
1735 if ( $view eq 'DBA' )
1736 {
1737 $stmt .=
1738 "
1739 AND table_owner = UPPER('$owner')
1740 ";
1741 }
1742  
1743 my $sql = _create_table_text( $stmt, $schema, $owner, $name, $view ) .
1744 "; \n\n" .
1745 _create_comments( $schema, $owner, $name, $view );
1746  
1747 if ( $attr{ 'grants' } )
1748 {
1749 $sql .= _object_privs( $schema, $owner, $name, $view );
1750 }
1751  
1752 return $sql;
1753 }
1754  
1755 # sub _create_materialized_view
1756 #
1757 # Returns DDL to create the named materialized view
1758 # by calling _create_mview (which is shared with
1759 # _create_snapshot)
1760 #
1761 sub _create_materialized_view
1762 {
1763 _create_mview( @_, 'MATERIALIZED VIEW' );
1764 }
1765  
1766 # sub _create_materialized_view_log
1767 #
1768 # Returns DDL to create the named materialized view log
1769 # by calling _create_mview_log (which is shared with
1770 # _create_snapshot_log)
1771 #
1772 sub _create_materialized_view_log
1773 {
1774 _create_mview_log( @_, 'MATERIALIZED VIEW' );
1775 }
1776  
1777 # sub _create_mview
1778 #
1779 # Returns DDL to create the named snapshot or materialized view
1780 # in the form of:
1781 #
1782 # CREATE {MATERIALIZED VIEW|SNAPSHOT} [schema.]<name>
1783 # <table properties>
1784 #
1785 sub _create_mview
1786 {
1787 my ( $schema, $owner, $name, $view, $type ) = @_;
1788  
1789 my $sql;
1790 my $stmt;
1791  
1792 if ( $oracle_major == 7 )
1793 {
1794 $stmt =
1795 "
1796 SELECT
1797 table_name
1798 , null AS build_mode
1799 , null AS refresh_method
1800 ,DECODE(
1801 type
1802 ,null,null
1803 ,'REFRESH ' || type || CHR(10)
1804 ) AS refresh_mode
1805 , TO_CHAR(start_with, 'DD-MON-YYYY HH24:MI:SS')
1806 AS start_with
1807 , next
1808 , null AS using_pk
1809 , null AS master_rollback_seg
1810 , DECODE(
1811 updatable
1812 ,'TRUE','FOR UPDATE'
1813 ,null
1814 ) AS updatable
1815 , query
1816 FROM
1817 ${view}_snapshots
1818 WHERE
1819 name = UPPER( ? )
1820 ";
1821  
1822 if ( $view eq 'DBA' )
1823 {
1824 $stmt .=
1825 "
1826 AND owner = UPPER('$owner')
1827 "
1828 }
1829 }
1830 elsif ( $oracle_major == 8 and $oracle_minor == 0 )
1831 {
1832 $stmt =
1833 "
1834 SELECT
1835 table_name
1836 , null AS build_mode
1837 , null AS refresh_method
1838 ,DECODE(
1839 type
1840 ,null,null
1841 ,'REFRESH ' || type || CHR(10)
1842 ) AS refresh_mode
1843 , TO_CHAR(start_with, 'DD-MON-YYYY HH24:MI:SS')
1844 AS start_with
1845 , next
1846 , DECODE(
1847 refresh_method
1848 ,'PRIMARY KEY','WITH PRIMARY KEY'
1849 ,'ROWID' ,'WITH ROWID'
1850 ,null
1851 ) AS using_pk
1852 , master_rollback_seg
1853 , DECODE(
1854 updatable
1855 ,'YES','FOR UPDATE'
1856 ,null
1857 ) AS updatable
1858 , query
1859 FROM
1860 ${view}_snapshots
1861 WHERE
1862 name = UPPER( ? )
1863 ";
1864  
1865 if ( $view eq 'DBA' )
1866 {
1867 $stmt .=
1868 "
1869 AND owner = UPPER('$owner')
1870 "
1871 }
1872 }
1873 else
1874 {
1875 $stmt =
1876 "
1877 SELECT
1878 m.container_name
1879 , DECODE(
1880 m.build_mode
1881 ,'YES','USING PREBUILT TABLE'
1882 ,DECODE(
1883 m.last_refresh_date
1884 ,null,'BUILD DEFERRED'
1885 ,'BUILD IMMEDIATE'
1886 )
1887 ) || CHR(10) AS build_mode
1888 , DECODE(
1889 m.refresh_method
1890 ,'NEVER','NEVER REFRESH'
1891 ,'REFRESH ' || m.refresh_method
1892 ) AS refresh_method
1893 , DECODE(
1894 m.refresh_mode
1895 ,'NEVER',CHR(10)
1896 ,'ON ' || m.refresh_mode || CHR(10)
1897 ) AS refresh_mode
1898 , TO_CHAR(s.start_with, 'DD-MON-YYYY HH24:MI:SS')
1899 AS start_with
1900 , s.next
1901 , DECODE(
1902 s.refresh_method
1903 ,'PRIMARY KEY','WITH PRIMARY KEY'
1904 ,'ROWID' ,'WITH ROWID'
1905 ,null
1906 ) AS using_pk
1907 , s.master_rollback_seg
1908 , DECODE(
1909 m.updatable
1910 ,'N',null
1911 ,DECODE(
1912 m.rewrite_enabled
1913 ,'Y','FOR UPDATE ENABLE QUERY REWRITE'
1914 ,'N','FOR UPDATE DISABLE QUERY REWRITE'
1915 )
1916 ) AS updatable
1917 , s.query
1918 FROM
1919 ${view}_mviews m
1920 , ${view}_snapshots s
1921 WHERE
1922 m.mview_name = UPPER( ? )
1923 AND s.name = m.mview_name
1924 ";
1925  
1926 if ( $view eq 'DBA' )
1927 {
1928 $stmt .=
1929 "
1930 AND m.owner = UPPER('$owner')
1931 AND s.owner = m.owner
1932 "
1933 }
1934 }
1935  
1936 $dbh->{ LongReadLen } = 65536; # Allows Query to be 64K
1937 $dbh->{ LongTruncOk } = 1;
1938  
1939 $sth = $dbh->prepare( $stmt );
1940 $sth->execute( $name );
1941 my @row = $sth->fetchrow_array;
1942 my $lctype = ( $type eq 'SNAPSHOT' ) ? 'Snapshpt' : 'Materialized View';
1943 die "\n$lctype \U$name \Ldoes not exist.\n\n" unless @row;
1944  
1945 my (
1946 $table,
1947 $build_mode,
1948 $refresh_method,
1949 $refresh_mode,
1950 $start_with,
1951 $next,
1952 $using_pk,
1953 $master_rb_seg,
1954 $updatable,
1955 $query,
1956 ) = @row;
1957  
1958 $stmt =
1959 "
1960 SELECT
1961 index_name
1962 FROM
1963 ${view}_indexes
1964 WHERE
1965 table_name = UPPER( ? )
1966 ";
1967  
1968 if ( $view eq 'DBA' )
1969 {
1970 $stmt .=
1971 "
1972 AND owner = UPPER('$owner')
1973 "; }
1974  
1975 $sth = $dbh->prepare( $stmt );
1976 $sth->execute( $table );
1977 @row = $sth->fetchrow_array;
1978  
1979 my ( $index ) = @row;
1980  
1981 $sql = "PROMPT " .
1982 "CREATE $type \L$schema$name\n\n" .
1983 "CREATE $type \L$schema$name \n" .
1984 _create_mview_table( $owner, $owner, $table, $view ) .
1985 "$build_mode" .
1986 "USING INDEX\n" .
1987 _create_mview_index( $schema, $owner, $index, $view );
1988  
1989 $sql .= "$refresh_method " if $refresh_method;
1990  
1991 $sql .= $refresh_mode;
1992  
1993 if ( $refresh_method ne 'NEVER REFRESH' )
1994 {
1995 $sql .= "START WITH TO_DATE('$start_with','DD-MON-YYYY HH24:MI:SS')\n"
1996 if $start_with;
1997  
1998 $sql .= "NEXT $next\n" if $next;
1999  
2000 $sql .= "$using_pk\n" if $using_pk;
2001  
2002 $sql .= "USING MASTER ROLLBACK SEGMENT \L$master_rb_seg\n"
2003 if $master_rb_seg;
2004 }
2005  
2006 $sql .= "$updatable\n" if $updatable;
2007  
2008 $sql .= "AS\n" . $query;
2009  
2010 $sql =~ s/\n+\Z//;
2011  
2012 $sql .= " ;\n\n";
2013  
2014 if ( $attr{ 'grants' } )
2015 {
2016 $sql .= _object_privs( $schema, $owner, $name, $view );
2017 }
2018  
2019 return $sql;
2020 }
2021  
2022 # sub _create_mview_index
2023 #
2024 # Returns DDL for the USING INDEX definition part of:
2025 #
2026 # CREATE MATERIALIZED VIEW
2027 # CREATE SNAPSHOT
2028 #
2029 # statements. This is created by calling _create_index, and
2030 # then stripping off the PROMPT and CREATE INDEX portions and the
2031 # column list, leaving just the physical attributes and partitioning clauses
2032 #
2033 sub _create_mview_index
2034 {
2035 # Snapshots don't use attributes PCTUSED and PCTFREE.
2036 # This will prevent sub _segment_attributes from including them.
2037 $isasnapindx = 1;
2038  
2039 my $done;
2040 my $started;
2041 my @lines_in = split /\n/, _create_index( @_ );
2042 my @lines_out;
2043  
2044 LINE:
2045 foreach my $line ( @lines_in )
2046 {
2047 # Ignore everything before the INITRANS clause.
2048 # This includes REMs, CREATE INDEX, columns, etc.
2049 $started++ if $line =~ /^INITRANS/;
2050 next LINE if not $started;
2051  
2052 # Set $done when we hit a semicolon
2053 $done = $line =~ s/\;$//;
2054  
2055 # But keep everything in between except blank lines
2056 # and any [NO]LOGGINING clauses
2057 push @lines_out, $line unless $line =~ /^$|LOGGING/;
2058  
2059 # Exit when we get to the semicolon and ignore the rest.
2060 # This eliminates the ';' and any COMMENTs.
2061 last LINE if $done;
2062 }
2063  
2064 my $sql = join "\n", @lines_out;
2065
2066 $isasnapindx = 0;
2067  
2068 return $sql . "\n";
2069 }
2070  
2071 # sub _create_mview_log
2072 #
2073 # Returns DDL to create the named log (snapshot or materialized view)
2074 # in the form of:
2075 #
2076 # CREATE {MATERIALIZED VIEW|SNAPSHOT} LOG [schema.]<name>
2077 # <table properties>
2078 # WITH {PRIMARY KEY|ROWID|PRIMARY KEY, ROWID}
2079 # [<filter columns>]
2080 #
2081 sub _create_mview_log
2082 {
2083 my ( $schema, $owner, $name, $view, $type ) = @_;
2084  
2085 my $sql;
2086 my $stmt =
2087 "
2088 SELECT
2089 log_table
2090 , rowids
2091 , primary_key
2092 , filter_columns
2093 FROM
2094 ${view}_snapshot_logs
2095 WHERE
2096 master = UPPER( ? )
2097 ";
2098  
2099 if ( $view eq 'DBA' )
2100 {
2101 $stmt .=
2102 "
2103 AND log_owner = UPPER('$owner')
2104 "; }
2105  
2106 $sth = $dbh->prepare( $stmt );
2107 $sth->execute( $name );
2108 my @row = $sth->fetchrow_array;
2109 my $lctype = ( $type eq 'SNAPSHOT' ) ? 'Snapshpt' : 'Materialized View';
2110 die "\n$lctype Log on \U$name \Ldoes not exist.\n\n" unless @row;
2111  
2112 my ( $table, $rowids, $primary_key, $filter_columns, $refreshable ) = @row;
2113  
2114 $sql = "PROMPT " .
2115 "CREATE $type LOG ON \L$schema$name\n\n" .
2116 "CREATE $type LOG ON \L$schema$name \n" .
2117 _create_mview_table( $schema, $owner, $table, $view );
2118  
2119 if ( $rowids eq 'YES' and $primary_key eq 'YES' )
2120 {
2121 $sql .= "WITH PRIMARY KEY, ROWID "
2122 }
2123 elsif ( $rowids eq 'YES' )
2124 {
2125 $sql .= "WITH ROWID "
2126 }
2127 elsif ( $primary_key eq 'YES' )
2128 {
2129 $sql .= "WITH PRIMARY KEY "
2130 }
2131  
2132 $stmt =
2133 "
2134 SELECT
2135 column_name
2136 FROM
2137 dba_snapshot_log_filter_cols
2138 WHERE
2139 name = UPPER( ? )
2140 AND owner = UPPER( ? )
2141 MINUS
2142 SELECT
2143 column_name
2144 FROM
2145 ${view}_cons_columns c
2146 , ${view}_constraints d
2147 WHERE
2148 d.table_name = UPPER( ? )
2149 AND d.constraint_type = 'P'
2150 AND c.table_name = d.table_name
2151 AND c.constraint_name = d.constraint_name
2152 ";
2153  
2154 if ( $view eq 'DBA' )
2155 {
2156 $stmt .=
2157 "
2158 AND d.owner = UPPER('$owner')
2159 AND c.owner = d.owner
2160 "; }
2161  
2162 $sth = $dbh->prepare( $stmt );
2163 $sth->execute( $name, $owner, $name );
2164 my $aref = $sth->fetchall_arrayref;
2165  
2166 if ( @$aref )
2167 {
2168  
2169 my $comma = ' ';
2170 $sql .= "\n(\n";
2171  
2172 foreach my $row ( @$aref )
2173 {
2174 $sql .= "$comma \L$row->[0]\n";
2175 $comma = ' ,'
2176 }
2177  
2178 $sql .= ") ";
2179 }
2180  
2181 return $sql .
2182 ";\n\n";
2183 }
2184  
2185 # sub _create_mview_table
2186 #
2187 # Returns DDL for the table definition part of:
2188 #
2189 # CREATE MATERIALIZED VIEW
2190 # CREATE MATERIALIZED VIEW LOG
2191 # CREATE SNAPSHOT
2192 # CREATE SNAPSHOT LOG
2193 #
2194 # statements. This is created by calling _create_table, and
2195 # then stripping off the PROMPT and CREATE TABLE portions and the
2196 # column list, leaving just the physical attributes and partitioning clauses
2197 #
2198 sub _create_mview_table
2199 {
2200 # Snapshots and their logs don't use attribute INITRANS.
2201 # Prior to 8i, they don't use FREELISTS and FREELIST GROUPS, either.
2202 # This will prevent sub _segment_attributes from including it.
2203 $isasnaptabl = 1;
2204  
2205 my $done;
2206 my $started;
2207 my @lines_in = split /\n/, _create_table( @_ );
2208 my @lines_out;
2209  
2210 LINE:
2211 foreach my $line( @lines_in )
2212 {
2213 # Ignore everything before the PARALLEL clause.
2214 # This includes REMs, CREATE TABLE, column definitions, etc.
2215 $started++ if $line =~ /^PARALLEL/;
2216 next LINE if not $started;
2217  
2218 # Set $done when we hit a semicolon
2219 $done = $line =~ s/\;$//;
2220  
2221 # But keep everything in between
2222 push @lines_out, $line unless $line =~ /^$/;
2223  
2224 # Exit when we get to the semicolon and ignore the rest.
2225 # This eliminates the ';' and any COMMENTs.
2226 last LINE if $done;
2227 }
2228  
2229 my $sql = join "\n", @lines_out;
2230
2231 $isasnaptabl = 0;
2232  
2233 return $sql . "\n";
2234 }
2235  
2236 # sub _create_package
2237 #
2238 # Returns DDL to create the named package in the form of:
2239 #
2240 # CREATE OR REPLACE PACKAGE [schema.]<name>
2241 # AS
2242 # <source>
2243 #
2244 # by calling _display_source
2245 #
2246 sub _create_package
2247 {
2248 my ( $schema, $owner, $name, $view ) = @_;
2249  
2250 my $sql = _display_source( $schema, $owner, $name, $view, 'PACKAGE' );
2251  
2252 if ( $attr{ 'grants' } )
2253 {
2254 $sql .= _object_privs( $schema, $owner, $name, $view );
2255 }
2256  
2257 return $sql ;
2258 }
2259  
2260 # sub _create_package_body
2261 #
2262 # Returns DDL to create the named procedure in the form of:
2263 #
2264 # CREATE OR REPLACE PACKAGE BODY [schema.]<name>
2265 # AS
2266 # <source>
2267 #
2268 # by calling _display_source
2269 #
2270 sub _create_package_body
2271 {
2272 return _display_source( @_, 'PACKAGE BODY' );
2273 }
2274  
2275 # sub _create_partitioned_index
2276 #
2277 # Creates the GLOBAL/LOCAL partition syntax part of a CREATE INDEX statement.
2278 #
2279 sub _create_partitioned_index
2280 {
2281 my ( $schema, $owner, $name, $view, $sql ) = @_;
2282  
2283 my $stmt;
2284  
2285 if ( $oracle_major == 8 and $oracle_minor == 0 )
2286 {
2287 $stmt =
2288 "
2289 SELECT
2290 -- 8.0 Indexes may partition only by RANGE
2291 i.partitioning_type
2292 , 'N/A' AS subpartitioning_type
2293 , i.locality
2294 , 0 AS compressed
2295 -- Physical Properties
2296 , 'INDEX' AS organization
2297 -- Segment Attributes
2298 , 'N/A' AS cache
2299 , 'N/A' AS pct_used
2300 , i.def_pct_free
2301 , DECODE(
2302 i.def_ini_trans
2303 ,0,1
2304 ,null,1
2305 ,i.def_ini_trans
2306 ) AS ini_trans
2307 , DECODE(
2308 i.def_max_trans
2309 ,0,255
2310 ,null,255
2311 ,i.def_max_trans
2312 ) AS max_trans
2313 -- Storage Clause
2314 ,DECODE(
2315 i.def_initial_extent
2316 ,'DEFAULT',s.initial_extent
2317 ,i.def_initial_extent * $block_size * 1024
2318 ) AS initial_extent
2319 ,DECODE(
2320 i.def_next_extent
2321 ,'DEFAULT',s.next_extent
2322 ,i.def_next_extent * $block_size * 1024
2323 ) AS next_extent
2324 , DECODE(
2325 i.def_min_extents
2326 ,'DEFAULT',s.min_extents
2327 ,i.def_min_extents
2328 ) AS min_extents
2329 , DECODE(
2330 i.def_max_extents
2331 ,'DEFAULT',DECODE(
2332 s.max_extents
2333 ,2147483645,'unlimited'
2334 ,s.max_extents
2335 )
2336 ,2147483645,'unlimited'
2337 ,i.def_max_extents
2338 ) AS max_extents
2339 , DECODE(
2340 i.def_pct_increase
2341 ,'DEFAULT',s.pct_increase
2342 ,i.def_pct_increase
2343 ) AS pct_increase
2344 , DECODE(
2345 i.def_freelists
2346 ,0,1
2347 ,null,1
2348 ,i.def_freelists
2349 ) AS freelists
2350 , DECODE(
2351 i.def_freelist_groups
2352 ,0,1
2353 ,null,1
2354 ,i.def_freelist_groups
2355 ) AS freelist_groups
2356 , 'N/A' AS buffer_pool
2357 , DECODE(
2358 i.def_logging
2359 ,'NO','NOLOGGING'
2360 , 'LOGGING'
2361 ) AS logging
2362 , LOWER(NVL(i.def_tablespace_name,s.tablespace_name))
2363 -- Don't have default blocks, so use larger of initial/next
2364 , GREATEST(
2365 DECODE(
2366 i.def_initial_extent
2367 ,'DEFAULT',s.initial_extent / $block_size / 1024
2368 ,i.def_initial_extent
2369 )
2370 ,DECODE(
2371 i.def_next_extent
2372 ,'DEFAULT',s.next_extent / $block_size / 1024
2373 ,i.def_next_extent
2374 )
2375 ) AS blocks
2376 FROM
2377 ${view}_part_indexes i
2378 , ${view}_indexes n
2379 , ${view}_tablespaces s
2380 , ${view}_part_tables t
2381 WHERE
2382 -- def_tablspace is sometimes NULL in PART_INDEXES,
2383 -- we'll have to go over to the table for the defaults
2384 i.index_name = UPPER( ? )
2385 AND n.index_name = UPPER( ? )
2386 AND t.table_name = n.table_name
2387 AND s.tablespace_name = t.def_tablespace_name
2388 ";
2389 }
2390 else # We're Oracle8i or newer
2391 {
2392 $stmt =
2393 "
2394 SELECT
2395 -- Indexes may partition only by RANGE or RANGE/HASH
2396 i.partitioning_type
2397 , i.subpartitioning_type
2398 , i.locality
2399 , DECODE(
2400 n.compression
2401 ,'ENABLED',n.prefix_length
2402 ,0
2403 ) AS compressed
2404 -- Physical Properties
2405 , 'INDEX' AS organization
2406 -- Segment Attributes
2407 , 'N/A' AS cache
2408 , 'N/A' AS pct_used
2409 , i.def_pct_free
2410 , DECODE(
2411 i.def_ini_trans
2412 ,0,1
2413 ,null,1
2414 ,i.def_ini_trans
2415 ) AS ini_trans
2416 , DECODE(
2417 i.def_max_trans
2418 ,0,255
2419 ,null,255
2420 ,i.def_max_trans
2421 ) AS max_trans
2422 -- Storage Clause
2423 ,DECODE(
2424 i.def_initial_extent
2425 ,'DEFAULT',s.initial_extent
2426 ,i.def_initial_extent * $block_size * 1024
2427 ) AS initial_extent
2428 ,DECODE(
2429 i.def_next_extent
2430 ,'DEFAULT',s.next_extent
2431 ,i.def_next_extent * $block_size * 1024
2432 ) AS next_extent
2433 , DECODE(
2434 i.def_min_extents
2435 ,'DEFAULT',s.min_extents
2436 ,i.def_min_extents
2437 ) AS min_extents
2438 , DECODE(
2439 i.def_max_extents
2440 ,'DEFAULT',DECODE(
2441 s.max_extents
2442 ,2147483645,'unlimited'
2443 ,s.max_extents
2444 )
2445 ,2147483645,'unlimited'
2446 ,i.def_max_extents
2447 ) AS max_extents
2448 , DECODE(
2449 i.def_pct_increase
2450 ,'DEFAULT',s.pct_increase
2451 ,i.def_pct_increase
2452 ) AS pct_increase
2453 , DECODE(
2454 i.def_freelists
2455 ,0,1
2456 ,null,1
2457 ,i.def_freelists
2458 ) AS freelists
2459 , DECODE(
2460 i.def_freelist_groups
2461 ,0,1
2462 ,null,1
2463 ,i.def_freelist_groups
2464 ) AS freelist_groups
2465 , LOWER(i.def_buffer_pool) AS buffer_pool
2466 , DECODE(
2467 i.def_logging
2468 ,'NO','NOLOGGING'
2469 , 'LOGGING'
2470 ) AS logging
2471 , LOWER(NVL(i.def_tablespace_name,s.tablespace_name))
2472 -- Don't have default blocks, so use larger of initial/next
2473 , GREATEST(
2474 DECODE(
2475 i.def_initial_extent
2476 ,'DEFAULT',s.initial_extent / $block_size / 1024
2477 ,i.def_initial_extent
2478 )
2479 ,DECODE(
2480 i.def_next_extent
2481 ,'DEFAULT',s.next_extent / $block_size / 1024
2482 ,i.def_next_extent
2483 )
2484 ) AS blocks
2485 FROM
2486 ${view}_part_indexes i
2487 , ${view}_indexes n
2488 , ${view}_tablespaces s
2489 , ${view}_part_tables t
2490 WHERE
2491 -- def_tablspace is sometimes NULL in PART_INDEXES,
2492 -- we'll have to go over to the table for the defaults
2493 i.index_name = UPPER( ? )
2494 AND n.index_name = UPPER( ? )
2495 AND t.table_name = i.table_name
2496 AND s.tablespace_name = t.def_tablespace_name
2497 ";
2498 }
2499  
2500 if ( $view eq 'DBA' )
2501 {
2502 $stmt .=
2503 "
2504 AND i.owner = UPPER('$owner')
2505 AND n.owner = UPPER('$owner')
2506 AND t.owner = UPPER('$owner')
2507 ";
2508 }
2509  
2510 $sth = $dbh->prepare( $stmt );
2511 $sth->execute( $name, $name );
2512 my @row = $sth->fetchrow_array;
2513  
2514 my $partitioning_type = shift @row;
2515 my $subpartitioning_type = shift @row;
2516 my $locality = shift @row;
2517 my $compressed = shift @row;
2518  
2519 unshift @row, ( '' ); #indent (none)
2520  
2521 $sql .= _segment_attributes( \@row );
2522  
2523 if ( $compressed )
2524 {
2525 $sql .= "COMPRESS $compressed\n";
2526 }
2527  
2528 if ( $locality eq 'GLOBAL' )
2529 {
2530 $sql .= "GLOBAL PARTITION BY RANGE\n" . # Only RANGE on global indexes
2531 "(\n " .
2532 _partition_key_columns( $owner, $name, 'INDEX', $view ) .
2533 "\n)\n" .
2534 _range_partitions($owner, $name, $view,
2535 $subpartitioning_type, 'GLOBAL' );
2536 }
2537 else # Must be partitione by RANGE or RANGE/HASH
2538 {
2539 $sql .= "LOCAL\n";
2540  
2541 if ( $partitioning_type eq 'RANGE' )
2542 {
2543 $sql .= _range_partitions( $owner, $name, $view,
2544 $subpartitioning_type, 'LOCAL' );
2545 }
2546 }
2547  
2548 return $sql;
2549 }
2550  
2551 # sub _create_partitioned_iot
2552 #
2553 # Returns DDL to create the partitioned index organized table
2554 # and its partition(s).
2555 # See _create_table for format.
2556 #
2557 sub _create_partitioned_iot
2558 {
2559 my ( $schema, $owner, $name, $view ) = @_;
2560  
2561 my $stmt;
2562  
2563 if ( $oracle_major == 8 and $oracle_minor == 0 )
2564 {
2565 $stmt =
2566 "
2567 SELECT
2568 -- Table Properties
2569 'N/A' AS monitoring
2570 , t.table_name
2571 -- Parallel Clause
2572 , LTRIM(t.degree) AS degree
2573 , LTRIM(t.instances) AS instances
2574 -- Physical Properties
2575 , 'INDEX' AS organization
2576 -- Segment Attributes
2577 , DECODE(
2578 LTRIM(t.cache)
2579 ,'Y','CACHE'
2580 , 'NOCACHE'
2581 ) AS cache
2582 , 'N/A' AS pct_used
2583 , p.def_pct_free AS pct_free
2584 , p.def_ini_trans AS ini_trans
2585 , p.def_max_trans AS max_trans
2586 -- Storage Clause
2587 ,DECODE(
2588 p.def_initial_extent
2589 ,'DEFAULT',s.initial_extent
2590 ,p.def_initial_extent * $block_size * 1024
2591 ) AS initial_extent
2592 ,DECODE(
2593 p.def_next_extent
2594 ,'DEFAULT',s.next_extent
2595 ,p.def_next_extent * $block_size * 1024
2596 ) AS next_extent
2597 , DECODE(
2598 p.def_min_extents
2599 ,'DEFAULT',s.min_extents
2600 ,p.def_min_extents
2601 ) AS min_extents
2602 , DECODE(
2603 p.def_max_extents
2604 ,'DEFAULT',DECODE(
2605 s.max_extents
2606 ,2147483645,'unlimited'
2607 ,s.max_extents
2608 )
2609 ,2147483645,'unlimited'
2610 ,p.def_max_extents
2611 ) AS max_extents
2612 , DECODE(
2613 p.def_pct_increase
2614 ,'DEFAULT',s.pct_increase
2615 ,p.def_pct_increase
2616 ) AS pct_increase
2617 , DECODE(
2618 p.def_freelists
2619 ,0,1
2620 ,NVL(p.def_freelists,1)
2621 ) AS freelists
2622 , DECODE(
2623 p.def_freelist_groups
2624 ,0,1
2625 ,NVL(p.def_freelist_groups,1)
2626 ) AS freelist_groups
2627 , 'N/A' AS buffer_pool
2628 , DECODE(
2629 p.def_logging
2630 ,'NO','NOLOGGING'
2631 , 'LOGGING'
2632 ) AS logging
2633 , LOWER(p.def_tablespace_name) AS tablespace_name
2634 , t.blocks - NVL(t.empty_blocks,0)
2635 FROM
2636 ${view}_all_tables t
2637 , ${view}_part_indexes p
2638 , ${view}_tablespaces s
2639 WHERE
2640 t.table_name = UPPER('$name')
2641 AND p.table_name = t.table_name
2642 AND s.tablespace_name = p.def_tablespace_name
2643 ";
2644 }
2645 else # We're Oracle8i or newer
2646 {
2647 $stmt =
2648 "
2649 SELECT
2650 -- Table Properties
2651 DECODE(
2652 t.monitoring
2653 ,'NO','NOMONITORING'
2654 , 'MONITORING'
2655 ) AS monitoring
2656 , t.table_name
2657 -- Parallel Clause
2658 , LTRIM(t.degree) AS degree
2659 , LTRIM(t.instances) AS instances
2660 -- Physical Properties
2661 , 'INDEX' AS organization
2662 -- Segment Attributes
2663 , DECODE(
2664 LTRIM(t.cache)
2665 ,'Y','CACHE'
2666 , 'NOCACHE'
2667 ) AS cache
2668 , 'N/A' AS pct_used
2669 , p.def_pct_free AS pct_free
2670 , p.def_ini_trans AS ini_trans
2671 , p.def_max_trans AS max_trans
2672 -- Storage Clause
2673 ,DECODE(
2674 p.def_initial_extent
2675 ,'DEFAULT',s.initial_extent
2676 ,p.def_initial_extent * $block_size * 1024
2677 ) AS initial_extent
2678 ,DECODE(
2679 p.def_next_extent
2680 ,'DEFAULT',s.next_extent
2681 ,p.def_next_extent * $block_size * 1024
2682 ) AS next_extent
2683 , DECODE(
2684 p.def_min_extents
2685 ,'DEFAULT',s.min_extents
2686 ,p.def_min_extents
2687 ) AS min_extents
2688 , DECODE(
2689 p.def_max_extents
2690 ,'DEFAULT',DECODE(
2691 s.max_extents
2692 ,2147483645,'unlimited'
2693 ,s.max_extents
2694 )
2695 ,2147483645,'unlimited'
2696 ,p.def_max_extents
2697 ) AS max_extents
2698 , DECODE(
2699 p.def_pct_increase
2700 ,'DEFAULT',s.pct_increase
2701 ,p.def_pct_increase
2702 ) AS pct_increase
2703 , DECODE(
2704 p.def_freelists
2705 ,0,1
2706 ,NVL(p.def_freelists,1)
2707 ) AS freelists
2708 , DECODE(
2709 p.def_freelist_groups
2710 ,0,1
2711 ,NVL(p.def_freelist_groups,1)
2712 ) AS freelist_groups
2713 , LOWER(p.def_buffer_pool) AS buffer_pool
2714 , DECODE(
2715 p.def_logging
2716 ,'NO','NOLOGGING'
2717 , 'LOGGING'
2718 ) AS logging
2719 , LOWER(p.def_tablespace_name) AS tablespace_name
2720 , t.blocks - NVL(t.empty_blocks,0)
2721 FROM
2722 ${view}_all_tables t
2723 , ${view}_part_indexes p
2724 , ${view}_tablespaces s
2725 WHERE
2726 t.table_name = UPPER('$name')
2727 AND p.table_name = t.table_name
2728 AND s.tablespace_name = p.def_tablespace_name
2729 ";
2730 }
2731  
2732 if ( $view eq 'DBA' )
2733 {
2734 $stmt .=
2735 "
2736 AND t.owner = UPPER('$owner')
2737 AND p.owner = t.owner
2738 ";
2739 }
2740  
2741 my $sql = _create_table_text( $stmt, $schema, $owner, $name, $view );
2742  
2743 $stmt =
2744 "
2745 SELECT
2746 constraint_name
2747 FROM
2748 ${view}_constraints
2749 WHERE
2750 table_name = UPPER( ? )
2751 AND constraint_type = 'P'
2752 ";
2753  
2754 if ( $view eq 'DBA' )
2755 {
2756 $stmt .=
2757 "
2758 AND owner = UPPER('$owner')
2759 ";
2760 }
2761  
2762 my $sth = $dbh->prepare( $stmt );
2763 $sth->execute( $name );
2764 my ( $index ) = $sth->fetchrow_array;
2765  
2766 $sql .= "PARTITION BY RANGE\n" . # Only RANGE allowed on IOT's
2767 "(\n " .
2768 _partition_key_columns( $owner, $name, 'TABLE', $view ) .
2769 "\n)\n" .
2770 _range_partitions( $owner, $index, $view, 'NONE', 'IOT' ) .
2771 _create_comments( $schema, $owner, $name, $view );
2772  
2773 if ( $attr{ 'grants' } )
2774 {
2775 $sql .= _object_privs( $schema, $owner, $name, $view );
2776 }
2777  
2778 return $sql;
2779 }
2780  
2781 # sub _create_partitioned_table
2782 #
2783 # Returns DDL to create the partitioned table and its partition(s).
2784 # See _create_table for format.
2785 #
2786 sub _create_partitioned_table
2787 {
2788 my ( $schema, $owner, $name, $view ) = @_;
2789  
2790 my $stmt;
2791  
2792 if ( $oracle_major == 8 and $oracle_minor == 0 )
2793 {
2794 $stmt =
2795 "
2796 SELECT
2797 -- Table Properties
2798 'N/A' AS monitoring
2799 , t.table_name
2800 -- Parallel Clause
2801 , LTRIM(t.degree) AS degree
2802 , LTRIM(t.instances) AS instances
2803 -- Physical Properties
2804 , DECODE(
2805 t.iot_type
2806 ,'IOT','INDEX'
2807 , 'HEAP'
2808 ) AS organization
2809 -- Segment Attributes
2810 , DECODE(
2811 LTRIM(t.cache)
2812 ,'Y','CACHE'
2813 , 'NOCACHE'
2814 ) AS cache
2815 , p.def_pct_used
2816 , p.def_pct_free AS pct_free
2817 , p.def_ini_trans AS ini_trans
2818 , p.def_max_trans AS max_trans
2819 -- Storage Clause
2820 ,DECODE(
2821 p.def_initial_extent
2822 ,'DEFAULT',s.initial_extent
2823 ,p.def_initial_extent * $block_size * 1024
2824 ) AS initial_extent
2825 ,DECODE(
2826 p.def_next_extent
2827 ,'DEFAULT',s.next_extent
2828 ,p.def_next_extent * $block_size * 1024
2829 ) AS next_extent
2830 , DECODE(
2831 p.def_min_extents
2832 ,'DEFAULT',s.min_extents
2833 ,p.def_min_extents
2834 ) AS min_extents
2835 , DECODE(
2836 p.def_max_extents
2837 ,'DEFAULT',DECODE(
2838 s.max_extents
2839 ,2147483645,'unlimited'
2840 ,s.max_extents
2841 )
2842 ,2147483645,'unlimited'
2843 ,p.def_max_extents
2844 ) AS max_extents
2845 , DECODE(
2846 p.def_pct_increase
2847 ,'DEFAULT',s.pct_increase
2848 ,p.def_pct_increase
2849 ) AS pct_increase
2850 , DECODE(
2851 p.def_freelists
2852 ,0,1
2853 ,NVL(p.def_freelists,1)
2854 ) AS freelists
2855 , DECODE(
2856 p.def_freelist_groups
2857 ,0,1
2858 ,NVL(p.def_freelist_groups,1)
2859 ) AS freelist_groups
2860 , 'N/A' AS buffer_pool
2861 , DECODE(
2862 p.def_logging
2863 ,'NO','NOLOGGING'
2864 , 'LOGGING'
2865 ) AS logging
2866 , LOWER(p.def_tablespace_name) AS tablespace_name
2867 , t.blocks - NVL(t.empty_blocks,0)
2868 FROM
2869 ${view}_all_tables t
2870 , ${view}_part_tables p
2871 , ${view}_tablespaces s
2872 WHERE
2873 t.table_name = UPPER('$name')
2874 AND p.table_name = t.table_name
2875 AND s.tablespace_name = p.def_tablespace_name
2876 ";
2877 }
2878 else # We're Oracle8i or newer
2879 {
2880 $stmt =
2881 "
2882 SELECT
2883 -- Table Properties
2884 DECODE(
2885 t.monitoring
2886 ,'NO','NOMONITORING'
2887 , 'MONITORING'
2888 ) AS monitoring
2889 , t.table_name
2890 -- Parallel Clause
2891 , LTRIM(t.degree) AS degree
2892 , LTRIM(t.instances) AS instances
2893 -- Physical Properties
2894 , DECODE(
2895 t.iot_type
2896 ,'IOT','INDEX'
2897 , 'HEAP'
2898 ) AS organization
2899 -- Segment Attributes
2900 , DECODE(
2901 LTRIM(t.cache)
2902 ,'Y','CACHE'
2903 , 'NOCACHE'
2904 ) AS cache
2905 , p.def_pct_used
2906 , p.def_pct_free AS pct_free
2907 , p.def_ini_trans AS ini_trans
2908 , p.def_max_trans AS max_trans
2909 -- Storage Clause
2910 ,DECODE(
2911 p.def_initial_extent
2912 ,'DEFAULT',s.initial_extent
2913 ,p.def_initial_extent * $block_size * 1024
2914 ) AS initial_extent
2915 ,DECODE(
2916 p.def_next_extent
2917 ,'DEFAULT',s.next_extent
2918 ,p.def_next_extent * $block_size * 1024
2919 ) AS next_extent
2920 , DECODE(
2921 p.def_min_extents
2922 ,'DEFAULT',s.min_extents
2923 ,p.def_min_extents
2924 ) AS min_extents
2925 , DECODE(
2926 p.def_max_extents
2927 ,'DEFAULT',DECODE(
2928 s.max_extents
2929 ,2147483645,'unlimited'
2930 ,s.max_extents
2931 )
2932 ,2147483645,'unlimited'
2933 ,p.def_max_extents
2934 ) AS max_extents
2935 , DECODE(
2936 p.def_pct_increase
2937 ,'DEFAULT',s.pct_increase
2938 ,p.def_pct_increase
2939 ) AS pct_increase
2940 , DECODE(
2941 p.def_freelists
2942 ,0,1
2943 ,NVL(p.def_freelists,1)
2944 ) AS freelists
2945 , DECODE(
2946 p.def_freelist_groups
2947 ,0,1
2948 ,NVL(p.def_freelist_groups,1)
2949 ) AS freelist_groups
2950 , LOWER(p.def_buffer_pool) AS buffer_pool
2951 , DECODE(
2952 p.def_logging
2953 ,'NO','NOLOGGING'
2954 , 'LOGGING'
2955 ) AS logging
2956 , LOWER(p.def_tablespace_name) AS tablespace_name
2957 , t.blocks - NVL(t.empty_blocks,0)
2958 FROM
2959 ${view}_all_tables t
2960 , ${view}_part_tables p
2961 , ${view}_tablespaces s
2962 WHERE
2963 t.table_name = UPPER('$name')
2964 AND p.table_name = t.table_name
2965 AND s.tablespace_name = p.def_tablespace_name
2966 ";
2967 }
2968  
2969 if ( $view eq 'DBA' )
2970 {
2971 $stmt .=
2972 "
2973 AND t.owner = UPPER('$owner')
2974 AND p.owner = t.owner
2975 ";
2976 }
2977  
2978 my $sql = _create_table_text( $stmt, $schema, $owner, $name, $view );
2979  
2980 $sql =~ /ORGANIZATION\s+(\w+)/gm;
2981 my $organization = $1;
2982  
2983 if ( $oracle_major == 8 and $oracle_minor == 0 )
2984 {
2985 $stmt =
2986 "
2987 SELECT
2988 partitioning_type
2989 , partition_count
2990 , 'N/A' AS subpartitioning_type
2991 , 'N/A' AS def_subpartition_count
2992 FROM
2993 ${view}_part_tables
2994 WHERE
2995 table_name = UPPER( ? )
2996 ";
2997 }
2998 else # We're Oracle8i or newer
2999 {
3000 $stmt =
3001 "
3002 SELECT
3003 partitioning_type
3004 , partition_count
3005 , subpartitioning_type
3006 , def_subpartition_count
3007 FROM
3008 ${view}_part_tables
3009 WHERE
3010 table_name = UPPER( ? )
3011 ";
3012 }
3013  
3014 if ( $view eq 'DBA' )
3015 {
3016 $stmt .=
3017 "
3018 AND owner = UPPER('$owner')
3019 ";
3020 }
3021  
3022 my $sth = $dbh->prepare( $stmt );
3023 $sth->execute( $name );
3024 my (
3025 $partitioning_type,
3026 $partition_count,
3027 $subpartitioning_type,
3028 $subpartition_count
3029 ) = $sth->fetchrow_array;
3030  
3031 $sql .= "PARTITION BY $partitioning_type\n" .
3032 "(\n " .
3033 _partition_key_columns( $owner, $name, 'TABLE', $view ) .
3034 "\n)\n";
3035  
3036 if ( $partitioning_type eq 'RANGE' )
3037 {
3038 if ( $subpartitioning_type eq 'HASH' )
3039 {
3040 $sql .= "SUBPARTITION BY HASH\n" .
3041 "(\n " .
3042 _subpartition_key_columns( $owner, $name, 'TABLE', $view ) .
3043 "\n)\n" .
3044 "SUBPARTITIONS $subpartition_count" .
3045 "\n";
3046 }
3047  
3048 $sql .= "(\n";
3049  
3050 if ( $oracle_major == 8 and $oracle_minor == 0 )
3051 {
3052 $stmt =
3053 "
3054 SELECT
3055 partition_name
3056 , high_value
3057 , 'N/A'
3058 , pct_used
3059 , pct_free
3060 , ini_trans
3061 , max_trans
3062 -- Storage Clause
3063 , initial_extent
3064 , next_extent
3065 , min_extent
3066 , DECODE(
3067 max_extent
3068 ,2147483645,'unlimited'
3069 , max_extent
3070 ) AS max_extents
3071 , pct_increase
3072 , NVL(freelists,1)
3073 , NVL(freelist_groups,1)
3074 , 'N/A' AS buffer_pool
3075 , DECODE(
3076 logging
3077 ,'NO','NOLOGGING'
3078 , 'LOGGING'
3079 ) AS logging
3080 , LOWER(tablespace_name)
3081 , blocks - NVL(empty_blocks,0)
3082 FROM
3083 ${view}_tab_partitions
3084 WHERE
3085 table_name = UPPER( ? )
3086 ";
3087 }
3088 else # We're Oracle8i or newer
3089 {
3090 $stmt =
3091 "
3092 SELECT
3093 partition_name
3094 , high_value
3095 , 'N/A'
3096 , pct_used
3097 , pct_free
3098 , ini_trans
3099 , max_trans
3100 -- Storage Clause
3101 , initial_extent
3102 , next_extent
3103 , min_extent
3104 , DECODE(
3105 max_extent
3106 ,2147483645,'unlimited'
3107 , max_extent
3108 ) AS max_extents
3109 , pct_increase
3110 , NVL(freelists,1)
3111 , NVL(freelist_groups,1)
3112 , LOWER(buffer_pool)
3113 , DECODE(
3114 logging
3115 ,'NO','NOLOGGING'
3116 , 'LOGGING'
3117 ) AS logging
3118 , LOWER(tablespace_name)
3119 , blocks - NVL(empty_blocks,0)
3120 FROM
3121 ${view}_tab_partitions
3122 WHERE
3123 table_name = UPPER( ? )
3124 ";
3125 }
3126  
3127 if ( $view eq 'DBA' )
3128 {
3129 $stmt .=
3130 "
3131 AND table_owner = UPPER('$owner')
3132 ";
3133 }
3134  
3135 $stmt .=
3136 "
3137 ORDER
3138 BY
3139 partition_name
3140 ";
3141  
3142 $dbh->{ LongReadLen } = 8192; # Allows HIGH_VALUE length of 8K
3143 $dbh->{ LongTruncOk } = 1;
3144  
3145 $sth = $dbh->prepare( $stmt );
3146 $sth->execute( $name );
3147 my $aref = $sth->fetchall_arrayref;
3148  
3149 my $comma = ' ';
3150  
3151 foreach my $row ( @$aref )
3152 {
3153 my $partition = shift @$row;
3154 my $high_value = shift @$row;
3155  
3156 $sql .= "${comma}PARTITION \L$partition \UVALUES LESS THAN\n" .
3157 " (\n" .
3158 " $high_value\n" .
3159 " )\n";
3160  
3161 unshift @$row, ( ' ', $organization );
3162  
3163 $sql .= _segment_attributes( $row );
3164  
3165 $comma = ' , ';
3166  
3167 if ( $subpartitioning_type eq 'HASH' )
3168 {
3169 $stmt =
3170 "
3171 SELECT
3172 subpartition_name
3173 , tablespace_name
3174 FROM
3175 ${view}_tab_subpartitions
3176 WHERE
3177 table_name = UPPER( ? )
3178 AND partition_name = '$partition'
3179 ";
3180  
3181 if ( $view eq 'DBA' )
3182 {
3183 $stmt .=
3184 "
3185 AND table_owner = UPPER('$owner')
3186 ";
3187 }
3188  
3189 $stmt .=
3190 "
3191 ORDER
3192 BY
3193 subpartition_name
3194 ";
3195  
3196 $sth = $dbh->prepare( $stmt );
3197 $sth->execute( $name );
3198 my $aref = $sth->fetchall_arrayref;
3199  
3200 $sql .= " (\n ";
3201  
3202 my @cols;
3203 foreach my $row ( @$aref )
3204 {
3205 push @cols, "SUBPARTITION \L$row->[0] \UTABLESPACE \L$row->[1]";
3206 }
3207 $sql .= join ( "\n , ", @cols );
3208  
3209 $sql .= "\n )\n";
3210 }
3211 }
3212 $sql .= ");\n\n";
3213 }
3214 else # It's HASH partitioning
3215 {
3216 $sql .= "(\n ";
3217  
3218 $stmt =
3219 "
3220 SELECT
3221 partition_name
3222 , tablespace_name
3223 FROM
3224 ${view}_tab_partitions
3225 WHERE
3226 table_name = UPPER( ? )
3227 ";
3228  
3229 if ( $view eq 'DBA' )
3230 {
3231 $stmt .=
3232 "
3233 AND table_owner= UPPER('$owner')
3234 ";
3235 }
3236  
3237 $stmt .=
3238 "
3239 ORDER
3240 BY
3241 partition_name
3242 ";
3243  
3244 $sth = $dbh->prepare( $stmt );
3245 $sth->execute( $name );
3246 my $aref = $sth->fetchall_arrayref;
3247  
3248 my @cols;
3249 foreach my $row ( @$aref )
3250 {
3251 push @cols, "PARTITION \L$row->[0] \UTABLESPACE \L$row->[1]";
3252 }
3253 $sql .= join ( "\n , ", @cols );
3254  
3255 $sql .= "\n) ;\n\n";
3256 }
3257  
3258 $sql .= _create_comments( $schema, $owner, $name, $view );
3259  
3260 if ( $attr{ 'grants' } )
3261 {
3262 $sql .= _object_privs( $schema, $owner, $name, $view );
3263 }
3264  
3265 return $sql ;
3266 }
3267  
3268 # sub _create_procedure
3269 #
3270 # Returns DDL to create the named procedure in the form of:
3271 #
3272 # CREATE OR REPLACE PROCEDURE [schema.]<name>
3273 # AS
3274 # <source>
3275 #
3276 # by calling _display_source
3277 #
3278 sub _create_procedure
3279 {
3280 my ( $schema, $owner, $name, $view ) = @_;
3281  
3282 my $sql = _display_source( $schema, $owner, $name, $view, 'PROCEDURE' );
3283  
3284 if ( $attr{ 'grants' } )
3285 {
3286 $sql .= _object_privs( $schema, $owner, $name, $view );
3287 }
3288  
3289 return $sql ;
3290 }
3291  
3292 # sub _create_profile
3293 #
3294 # Returns DDL to create the named profile in the form of:
3295 #
3296 # CREATE PROFILE <name>
3297 # LIMIT
3298 # SESSIONS_PER_USER <value>
3299 # CPU_PER_SESSION <value>
3300 # CPU_PER_CALL <value>
3301 # etc
3302 #
3303 sub _create_profile
3304 {
3305 my ( $schema, $owner, $name, $view ) = @_;
3306  
3307 $sth = $dbh->prepare(
3308 "
3309 SELECT
3310 RPAD(resource_name,27)
3311 , DECODE(
3312 RESOURCE_NAME
3313 ,'PASSWORD_VERIFY_FUNCTION',DECODE(
3314 limit
3315 ,'UNLIMITED','null'
3316 ,LOWER(limit)
3317 )
3318 , LOWER(limit)
3319 )
3320 FROM
3321 dba_profiles
3322 WHERE
3323 profile = UPPER( ? )
3324 ORDER
3325 BY
3326 DECODE(
3327 SUBSTR(resource_name,1,8)
3328 ,'FAILED_L',2
3329 ,'PASSWORD',2
3330 ,1
3331 )
3332 , resource_name
3333 ");
3334  
3335 $sth->execute( $name );
3336 my $aref = $sth->fetchall_arrayref;
3337 die "\nProfile '\U$name' \Ldoes not exist.\n\n" unless @$aref;
3338  
3339 my $sql = "PROMPT " .
3340 "CREATE PROFILE \L$name\n\n" .
3341 "CREATE PROFILE \L$name\n" .
3342 "LIMIT\n";
3343  
3344 foreach my $row ( @$aref )
3345 {
3346 $sql .= " $row->[0]$row->[1]\n";
3347 }
3348  
3349 $sql .= ";\n\n";
3350  
3351 return $sql;
3352 }
3353  
3354 # sub _create_role
3355 #
3356 # Returns DDL to create the named role in the form of:
3357 #
3358 # CREATE ROLE <name> IDENTIFIED {EXTERNALLY|BY VALUES '<values>'}
3359 # or
3360 # CREATE ROLE <name> NOT IDENTIFIED
3361 #
3362 sub _create_role
3363 {
3364 my ( $schema, $owner, $name, $view ) = @_;
3365  
3366 die "\nYou must use the DBA views in order to CREATE ROLE\n\n"
3367 unless $view eq 'DBA';
3368  
3369 my $stmt =
3370 "
3371 SELECT
3372 DECODE(
3373 r.password_required
3374 ,'YES', DECODE(
3375 u.password
3376 ,'EXTERNAL','IDENTIFIED EXTERNALLY'
3377 ,'IDENTIFIED BY VALUES '''
3378 || u.password || ''''
3379 )
3380 ,'NOT IDENTIFIED'
3381 ) AS password
3382 FROM
3383 dba_roles r
3384 , sys.user\$ u
3385 WHERE
3386 r.role = UPPER( ? )
3387 AND u.name = UPPER( ? )
3388 ";
3389  
3390 $sth = $dbh->prepare( $stmt );
3391 $sth->execute( $name, $name );
3392 my @row = $sth->fetchrow_array;
3393 die "\nRole \U$name \Ldoes not exist.\n\n" unless @row;
3394  
3395 my ( $password ) = @row;
3396  
3397 my $sql =
3398 "PROMPT " .
3399 "CREATE ROLE \L$name\n\n" .
3400 "CREATE ROLE \L$name \U$password;\n\n";
3401  
3402 $sql .= _granted_privs( $name );
3403  
3404 return $sql;
3405 }
3406  
3407 # sub _create_rollback_segment
3408 #
3409 # Returns DDL to create the named rollback segment in the form of:
3410 #
3411 # CREATE [PUBLIC] ROLLBACK SEGMENT <name>
3412 # STORAGE
3413 # (
3414 # storage clause
3415 # )
3416 # TABLESPACE tablespace
3417 #
3418 sub _create_rollback_segment
3419 {
3420 my ( $schema, $owner, $name, $view ) = @_;
3421  
3422 $sth = $dbh->prepare(
3423 "
3424 SELECT
3425 DECODE(
3426 r.owner
3427 ,'PUBLIC',' PUBLIC '
3428 , ' '
3429 ) AS is_public
3430 , r.tablespace_name
3431 , NVL(r.initial_extent,t.initial_extent) AS initial_extent
3432 , NVL(r.next_extent,t.next_extent) AS next_extent
3433 , r.min_extents
3434 , DECODE(
3435 r.max_extents
3436 ,2147483645,'unlimited'
3437 , r.max_extents
3438 ) AS max_extents
3439 FROM
3440 dba_rollback_segs r
3441 , ${view}_tablespaces t
3442 WHERE
3443 r.segment_name = UPPER( ? )
3444 AND t.tablespace_name = r.tablespace_name
3445 ");
3446  
3447 $sth->execute( $name );
3448 my @row = $sth->fetchrow_array;
3449 die "\nRollback Segment '\U$name' \Ldoes not exist.\n\n" unless @row;
3450  
3451 my (
3452 $is_public,
3453 $tablespace_name,
3454 $initial_extent,
3455 $next_extent,
3456 $min_extents,
3457 $max_extents,
3458 ) = @row;
3459  
3460 return "PROMPT " .
3461 "CREATE${is_public}ROLLBACK SEGMENT \L$name\n\n" .
3462 "CREATE${is_public}ROLLBACK SEGMENT \L$name\n" .
3463 "STORAGE\n" .
3464 "(\n" .
3465 " INITIAL $initial_extent\n" .
3466 " NEXT $next_extent\n" .
3467 " MINEXTENTS $min_extents\n" .
3468 " MAXEXTENTS $max_extents\n" .
3469 ")\n" .
3470 "TABLESPACE \L$tablespace_name\n" .
3471 ";\n\n" ;
3472 }
3473  
3474 # sub _create_schema
3475 #
3476 # Returns DDL to create all objects of the following type:
3477 #
3478 # TYPE
3479 # TABLE
3480 # INDEX
3481 # CONSTRAINT
3482 # TRIGGER
3483 # VIEW
3484 # SNAPSHOT
3485 # SNAPSHOT LOG
3486 # SYNONYM
3487 # DATABASE LINK
3488 # FUNCTION
3489 # PROCEDURE
3490 # PACKAGE
3491 # PACKAGE BODY
3492 # SEQUENCE
3493 #
3494 sub _create_schema
3495 {
3496 my ( $schema, $owner, $name, $view ) = @_;
3497  
3498 my $sql;
3499 my $aref;
3500 my $stmt;
3501  
3502 #
3503 # Start with schema's types (they may be a data type in CREATE TABLE)
3504 #
3505 $stmt =
3506 "
3507 SELECT
3508 object_name
3509 FROM
3510 ${view}_objects
3511 WHERE
3512 object_type = 'TYPE'
3513 ";
3514  
3515 if ( $view eq 'DBA' )
3516 {
3517 $stmt .=
3518 "
3519 AND owner = UPPER('$owner')
3520 ";
3521 }
3522  
3523 $stmt .=
3524 "
3525 ORDER
3526 BY
3527 object_name
3528 ";
3529  
3530 $sth = $dbh->prepare( $stmt );
3531 $sth->execute;
3532 $aref = $sth->fetchall_arrayref;
3533  
3534 foreach my $row ( @$aref )
3535 {
3536 $sql .= _create_type( $schema, $owner, @$row->[0], $view );
3537 }
3538  
3539 #
3540 # Add schema's tables
3541 #
3542 $stmt =
3543 "
3544 SELECT
3545 table_name
3546 FROM
3547 ${view}_tables
3548 ";
3549  
3550 if ( $view eq 'DBA' )
3551 {
3552 $stmt .=
3553 "
3554 WHERE
3555 owner = UPPER('$owner')
3556 ";
3557 }
3558  
3559 if ( $oracle_major > 7 )
3560 {
3561 $stmt .=
3562 "
3563 MINUS
3564 SELECT
3565 table_name
3566 FROM
3567 ${view}_tables
3568 WHERE
3569 iot_type = 'IOT_OVERFLOW'
3570 ";
3571  
3572 if ( $view eq 'DBA' )
3573 {
3574 $stmt .=
3575 "
3576 AND owner = UPPER('$owner')
3577 ";
3578 }
3579 }
3580  
3581 $stmt .=
3582 "
3583 MINUS
3584 SELECT
3585 log_table
3586 FROM
3587 ${view}_snapshot_logs
3588 ";
3589  
3590 if ( $view eq 'DBA' )
3591 {
3592 $stmt .=
3593 "
3594 WHERE log_owner = UPPER('$owner')
3595 ";
3596 }
3597  
3598 $stmt .=
3599 "
3600 MINUS
3601 SELECT
3602 table_name
3603 FROM
3604 ${view}_snapshots
3605 ";
3606  
3607 if ( $view eq 'DBA' )
3608 {
3609 $stmt .=
3610 "
3611 WHERE
3612 owner = UPPER('$owner')
3613 ";
3614 }
3615  
3616 $stmt .=
3617 "
3618 ORDER
3619 BY
3620 1
3621 ";
3622  
3623 $sth = $dbh->prepare( $stmt );
3624 $sth->execute;
3625 $aref = $sth->fetchall_arrayref;
3626  
3627 foreach my $row ( @$aref )
3628 {
3629 $sql .= _create_table( $schema, $owner, @$row->[0], $view );
3630 }
3631  
3632 #
3633 # Add schema's indexes
3634 #
3635  
3636 # If we're an IOT (Oracle8 or newer), skip the PK index
3637 # because it was in the CREATE TABLE
3638  
3639 if ( $oracle_major == 7 )
3640 {
3641 $stmt =
3642 "
3643 SELECT
3644 index_name
3645 FROM
3646 ${view}_indexes i
3647 ";
3648  
3649 if ( $view eq 'DBA' )
3650 {
3651 $stmt .=
3652 "
3653 WHERE
3654 i.table_owner = UPPER('$owner')
3655 ";
3656 }
3657 }
3658 else # we're Oracle8 or newer
3659 {
3660 $stmt =
3661 "
3662 SELECT
3663 index_name
3664 FROM
3665 ${view}_indexes i
3666 , ${view}_tables t
3667 WHERE
3668 i.table_owner = UPPER('$owner')
3669 AND t.table_name = i.table_name
3670 AND (
3671 t.iot_type <> 'IOT'
3672 OR (
3673 NOT EXISTS (
3674 SELECT
3675 null
3676 FROM
3677 ${view}_constraints
3678 WHERE
3679 constraint_type = 'P'
3680 AND constraint_name =
3681 i.index_name
3682 AND owner =
3683 i.table_owner
3684 )
3685 )
3686 )
3687 ";
3688  
3689 if ( $view eq 'DBA' )
3690 {
3691 $stmt .=
3692 "
3693 AND t.owner = i.table_owner
3694 ";
3695 }
3696 }
3697  
3698 $stmt .=
3699 "
3700 ORDER
3701 BY
3702 i.table_name
3703 , i.index_name
3704 ";
3705  
3706 $sth = $dbh->prepare( $stmt );
3707 $sth->execute;
3708 $aref = $sth->fetchall_arrayref;
3709  
3710 foreach my $row ( @$aref )
3711 {
3712 $sql .= _create_index( $schema, $owner, @$row->[0], $view );
3713 }
3714  
3715 #
3716 # Add schema's constraints
3717 #
3718  
3719 # If we're an IOT (Oracle8 or newer), skip the PK constraint
3720 # because it was in the CREATE TABLE
3721  
3722 if ( $oracle_major == 7 )
3723 {
3724 $stmt =
3725 "
3726 SELECT
3727 c.constraint_name
3728 , c.constraint_type
3729 , c.search_condition
3730 FROM
3731 ${view}_constraints c
3732 WHERE
3733 constraint_type IN('P','U','R','C')
3734 ";
3735  
3736 if ( $view eq 'DBA' )
3737 {
3738 $stmt .=
3739 "
3740 AND c.owner = UPPER('$owner')
3741 ";
3742 }
3743 }
3744 else # we're Oracle8 or newer
3745 {
3746 $stmt =
3747 "
3748 SELECT
3749 c.constraint_name
3750 , c.constraint_type
3751 , c.search_condition
3752 FROM
3753 ${view}_constraints c
3754 , ${view}_tables t
3755 WHERE
3756 t.table_name = c.table_name
3757 AND (
3758 (
3759 (
3760 t.iot_type <> 'IOT'
3761 OR t.iot_type IS NULL
3762 )
3763 AND c.constraint_type IN('P','U','R','C')
3764 )
3765 OR (
3766 t.iot_type = 'IOT'
3767 AND c.constraint_type IN ('U','R','C')
3768 )
3769 )
3770 AND NOT EXISTS (
3771 SELECT
3772 null
3773 FROM
3774 ${view}_snapshot_logs
3775 WHERE
3776 log_table = c.table_name
3777 ";
3778  
3779 if ( $view eq 'DBA' )
3780 {
3781 $stmt .=
3782 "
3783 AND log_owner = c.owner
3784 ";
3785 }
3786  
3787 $stmt .=
3788 "
3789 )
3790 AND NOT EXISTS (
3791 SELECT
3792 null
3793 FROM
3794 ${view}_snapshots
3795 WHERE
3796 table_name = c.table_name
3797 ";
3798  
3799 if ( $view eq 'DBA' )
3800 {
3801 $stmt .=
3802 "
3803 AND owner = c.owner
3804 ";
3805 }
3806  
3807 $stmt .=
3808 "
3809 )
3810 ";
3811  
3812 if ( $view eq 'DBA' )
3813 {
3814 $stmt .=
3815 "
3816 AND c.owner = UPPER('$owner')
3817 AND t.owner = c.owner
3818 ";
3819 }
3820 }
3821  
3822 $stmt .=
3823 "
3824 ORDER
3825 BY
3826 DECODE(
3827 c.constraint_type
3828 ,'P',1
3829 ,'U',2
3830 ,'R',3
3831 ,'C',4
3832 )
3833 , c.table_name
3834 , c.constraint_name
3835 ";
3836  
3837 $dbh->{ LongReadLen } = 8192; # Allows SEARCH_CONDITION length of 8K
3838 $dbh->{ LongTruncOk } = 1;
3839  
3840 $sth = $dbh->prepare( $stmt );
3841 $sth->execute;
3842 $aref = $sth->fetchall_arrayref;
3843  
3844 foreach my $row ( @$aref )
3845 {
3846 my ( $cons_name, $cons_type, $condition ) = @$row;
3847  
3848 if ( $cons_type ne 'C' )
3849 {
3850 $sql .= _create_constraint( $schema, $owner, $cons_name, $view );
3851 }
3852 elsif ( $condition !~ /IS NOT NULL/ )
3853 {
3854 $sql .= _create_constraint( $schema, $owner, $cons_name, $view );
3855 }
3856 }
3857  
3858 #
3859 # Add schema's triggers
3860 #
3861 $stmt =
3862 "
3863 SELECT
3864 trigger_name
3865 FROM
3866 ${view}_triggers
3867 ";
3868  
3869 if ( $view eq 'DBA' )
3870 {
3871 $stmt .=
3872 "
3873 WHERE
3874 table_owner = UPPER('$owner')
3875 ";
3876 }
3877  
3878 $stmt .=
3879 "
3880 ORDER
3881 BY
3882 table_name
3883 , trigger_name
3884 ";
3885  
3886 $sth = $dbh->prepare( $stmt );
3887 $sth->execute;
3888 $aref = $sth->fetchall_arrayref;
3889  
3890 foreach my $row ( @$aref )
3891 {
3892 $sql .= _create_trigger( $schema, $owner, @$row->[0], $view );
3893 }
3894  
3895 #
3896 # Add schema's views
3897 #
3898 $stmt =
3899 "
3900 SELECT
3901 view_name
3902 FROM
3903 ${view}_views
3904 ";
3905  
3906 if ( $view eq 'DBA' )
3907 {
3908 $stmt .=
3909 "
3910 WHERE
3911 owner = UPPER('$owner')
3912 ";
3913 }
3914  
3915 $stmt .=
3916 "
3917 ORDER
3918 BY
3919 view_name
3920 ";
3921  
3922 $sth = $dbh->prepare( $stmt );
3923 $sth->execute;
3924 $aref = $sth->fetchall_arrayref;
3925  
3926 foreach my $row ( @$aref )
3927 {
3928 $sql .= _create_view( $schema, $owner, @$row->[0], $view );
3929 }
3930  
3931 #
3932 # Add schema's snapshot logs
3933 #
3934 $stmt =
3935 "
3936 SELECT
3937 master
3938 FROM
3939 ${view}_snapshot_logs
3940 ";
3941  
3942 if ( $view eq 'DBA' )
3943 {
3944 $stmt .=
3945 "
3946 WHERE log_owner = UPPER('$owner')
3947 ";
3948 }
3949  
3950 $stmt .=
3951 "
3952 ORDER
3953 BY
3954 master
3955 ";
3956  
3957 $sth = $dbh->prepare( $stmt );
3958 $sth->execute;
3959 $aref = $sth->fetchall_arrayref;
3960  
3961 foreach my $row ( @$aref )
3962 {
3963 $sql .= _create_snapshot_log( $schema, $owner, @$row->[0], $view );
3964 }
3965  
3966 #
3967 # Add schema's snapshots
3968 #
3969 $stmt =
3970 "
3971 SELECT
3972 name
3973 FROM
3974 ${view}_snapshots
3975 ";
3976  
3977 if ( $view eq 'DBA' )
3978 {
3979 $stmt .=
3980 "
3981 WHERE
3982 owner = UPPER('$owner')
3983 ";
3984 }
3985  
3986 $stmt .=
3987 "
3988 ORDER
3989 BY
3990 name
3991 ";
3992  
3993 $sth = $dbh->prepare( $stmt );
3994 $sth->execute;
3995 $aref = $sth->fetchall_arrayref;
3996  
3997 foreach my $row ( @$aref )
3998 {
3999 $sql .= _create_snapshot( $schema, $owner, @$row->[0], $view );
4000 }
4001  
4002 #
4003 # Add schema's synonyms
4004 #
4005 $stmt =
4006 "
4007 SELECT
4008 synonym_name
4009 FROM
4010 ${view}_synonyms
4011 ";
4012  
4013 if ( $view eq 'DBA' )
4014 {
4015 $stmt .=
4016 "
4017 WHERE
4018 owner = UPPER('$owner')
4019 ";
4020 }
4021  
4022 $stmt .=
4023 "
4024 ORDER
4025 BY
4026 synonym_name
4027 ";
4028  
4029 $sth = $dbh->prepare( $stmt );
4030 $sth->execute;
4031 $aref = $sth->fetchall_arrayref;
4032  
4033 foreach my $row ( @$aref )
4034 {
4035 $sql .= _create_synonym( $schema, $owner, @$row->[0], $view );
4036 }
4037  
4038 #
4039 # Add schema's database links
4040 #
4041 $stmt =
4042 "
4043 SELECT
4044 db_link
4045 FROM
4046 ${view}_db_links
4047 ";
4048  
4049 if ( $view eq 'DBA' )
4050 {
4051 $stmt .=
4052 "
4053 WHERE
4054 owner = UPPER('$owner')
4055 ";
4056 }
4057  
4058 $stmt .=
4059 "
4060 ORDER
4061 BY
4062 db_link
4063 ";
4064  
4065 $sth = $dbh->prepare( $stmt );
4066 $sth->execute;
4067 $aref = $sth->fetchall_arrayref;
4068  
4069 foreach my $row ( @$aref )
4070 {
4071 $sql .= _create_db_link( $schema, $owner, @$row->[0], $view );
4072 }
4073  
4074 #
4075 # Add schema's functions
4076 #
4077 $stmt =
4078 "
4079 SELECT
4080 object_name
4081 FROM
4082 ${view}_objects
4083 WHERE
4084 object_type = 'FUNCTION'
4085 ";
4086  
4087 if ( $view eq 'DBA' )
4088 {
4089 $stmt .=
4090 "
4091 AND owner = UPPER('$owner')
4092 ";
4093 }
4094  
4095 $stmt .=
4096 "
4097 ORDER
4098 BY
4099 object_name
4100 ";
4101  
4102 $sth = $dbh->prepare( $stmt );
4103 $sth->execute;
4104 $aref = $sth->fetchall_arrayref;
4105  
4106 foreach my $row ( @$aref )
4107 {
4108 $sql .= _create_function( $schema, $owner, @$row->[0], $view );
4109 }
4110  
4111 #
4112 # Add schema's procedures
4113 #
4114 $stmt =
4115 "
4116 SELECT
4117 object_name
4118 FROM
4119 ${view}_objects
4120 WHERE
4121 object_type = 'PROCEDURE'
4122 ";
4123  
4124 if ( $view eq 'DBA' )
4125 {
4126 $stmt .=
4127 "
4128 AND owner = UPPER('$owner')
4129 ";
4130 }
4131  
4132 $stmt .=
4133 "
4134 ORDER
4135 BY
4136 object_name
4137 ";
4138  
4139 $sth = $dbh->prepare( $stmt );
4140 $sth->execute;
4141 $aref = $sth->fetchall_arrayref;
4142  
4143 foreach my $row ( @$aref )
4144 {
4145 $sql .= _create_procedure( $schema, $owner, @$row->[0], $view );
4146 }
4147  
4148 #
4149 # Add schema's packages
4150 #
4151 $stmt =
4152 "
4153 SELECT
4154 object_name
4155 FROM
4156 ${view}_objects
4157 WHERE
4158 object_type = 'PACKAGE'
4159 ";
4160  
4161 if ( $view eq 'DBA' )
4162 {
4163 $stmt .=
4164 "
4165 AND owner = UPPER('$owner')
4166 ";
4167 }
4168  
4169 $stmt .=
4170 "
4171 ORDER
4172 BY
4173 object_name
4174 ";
4175  
4176 $sth = $dbh->prepare( $stmt );
4177 $sth->execute;
4178 $aref = $sth->fetchall_arrayref;
4179  
4180 foreach my $row ( @$aref )
4181 {
4182 $sql .= _create_package( $schema, $owner, @$row->[0], $view );
4183 }
4184  
4185 #
4186 # Add schema's package bodies
4187 #
4188 $stmt =
4189 "
4190 SELECT
4191 object_name
4192 FROM
4193 ${view}_objects
4194 WHERE
4195 object_type = 'PACKAGE BODY'
4196 ";
4197  
4198 if ( $view eq 'DBA' )
4199 {
4200 $stmt .=
4201 "
4202 AND owner = UPPER('$owner')
4203 ";
4204 }
4205  
4206 $stmt .=
4207 "
4208 ORDER
4209 BY
4210 object_name
4211 ";
4212  
4213 $sth = $dbh->prepare( $stmt );
4214 $sth->execute;
4215 $aref = $sth->fetchall_arrayref;
4216  
4217 foreach my $row ( @$aref )
4218 {
4219 $sql .= _create_package_body( $schema, $owner, @$row->[0], $view );
4220 }
4221  
4222 #
4223 # Add schema's sequences
4224 #
4225 $stmt =
4226 "
4227 SELECT
4228 sequence_name
4229 FROM
4230 ${view}_sequences
4231 ";
4232  
4233 if ( $view eq 'DBA' )
4234 {
4235 $stmt .=
4236 "
4237 WHERE
4238 sequence_owner = UPPER('$owner')
4239 ";
4240 }
4241  
4242 $stmt .=
4243 "
4244 ORDER
4245 BY
4246 sequence_name
4247 ";
4248  
4249 $sth = $dbh->prepare( $stmt );
4250 $sth->execute;
4251 $aref = $sth->fetchall_arrayref;
4252  
4253 foreach my $row ( @$aref )
4254 {
4255 $sql .= _create_sequence( $schema, $owner, @$row->[0], $view );
4256 }
4257  
4258 $sql .= "PROMPT Recompile schema \U$owner\n\n" .
4259 "BEGIN dbms_utility.compile_schema('\U$owner') ; END ;\n/\n\n"
4260 unless $schema eq 'PUBLIC';
4261  
4262 return $sql;
4263 }
4264  
4265 # sub _create_sequence
4266 #
4267 # Returns DDL to create the named sequence in the form of:
4268 #
4269 # CREATE SEQUENCE [schema.]<name>
4270 # START WITH <integer>
4271 # INCREMENT BY <integer>
4272 # [NO]MINVALUE <integer>
4273 # [NO]MAXVALUE <integer>
4274 # [NO]CACHE <integer>
4275 # [NO]CYCLE
4276 # [NO]ORDER
4277 #
4278 sub _create_sequence
4279 {
4280 my ( $schema, $owner, $name, $view ) = @_;
4281  
4282 my $stmt =
4283 "
4284 SELECT
4285 'START WITH '
4286 || LTRIM(TO_CHAR(last_number + cache_size,'fm999999999'))
4287 AS start_with
4288 , 'INCREMENT BY '
4289 || LTRIM(TO_CHAR(increment_by,'fm999999999')) AS imcrement_by
4290 , DECODE(
4291 min_value
4292 ,0,'NOMINVALUE'
4293 ,'MINVALUE ' || TO_CHAR(min_value)
4294 ) AS min_value
4295 , DECODE(
4296 TO_CHAR(max_value,'fm999999999999999999999999999')
4297 ,'999999999999999999999999999','NOMAXVALUE'
4298 ,'MAXVALUE ' || TO_CHAR(max_value)
4299 ) AS max_value
4300 , DECODE(
4301 cache_size
4302 ,0,'NOCACHE'
4303 ,'CACHE ' || TO_CHAR(cache_size)
4304 ) AS cache_size
4305 , DECODE(
4306 cycle_flag
4307 ,'Y','CYCLE'
4308 ,'N', 'NOCYCLE'
4309 ) AS cycle_flag
4310 , DECODE(
4311 order_flag
4312 ,'Y','ORDER'
4313 ,'N', 'NOORDER'
4314 ) AS order_flag
4315 FROM
4316 ${view}_sequences
4317 WHERE
4318 sequence_name = UPPER( ? )
4319 ";
4320  
4321 if ( $view eq 'DBA' )
4322 {
4323 $stmt .=
4324 "
4325 AND sequence_owner = UPPER('$owner')
4326 "; }
4327  
4328 $sth = $dbh->prepare( $stmt );
4329 $sth->execute( $name );
4330 my @row = $sth->fetchrow_array;
4331 die "\nSequence \U$name \Ldoes not exist.\n\n" unless @row;
4332  
4333 my (
4334 $start_with,
4335 $increment_by,
4336 $min_value,
4337 $max_value,
4338 $cache_size,
4339 $cycle_flag,
4340 $order_flag,
4341 ) = @row;
4342  
4343 my $sql = "PROMPT " .
4344 "CREATE SEQUENCE \L$schema$name\n\n" .
4345 "CREATE SEQUENCE \L$schema$name\n" .
4346 " $start_with\n" .
4347 " $increment_by\n" .
4348 " $min_value\n" .
4349 " $max_value\n" .
4350 " $cache_size\n" .
4351 " $cycle_flag\n" .
4352 " $order_flag\n" .
4353 ";\n\n";
4354  
4355 if ( $attr{ 'grants' } )
4356 {
4357 $sql .= _object_privs( $schema, $owner, $name, $view );
4358 }
4359  
4360 return $sql;
4361 }
4362  
4363 # sub _create_snapshot
4364 #
4365 # Returns DDL to create the named materialized view
4366 # by calling _create_mview (which is shared with
4367 # _create_materialized_view)
4368 #
4369 sub _create_snapshot
4370 {
4371 _create_mview( @_, 'SNAPSHOT' );
4372 }
4373  
4374 # sub _create_snapshot_log
4375 #
4376 # Returns DDL to create the named snapshot log
4377 # by calling _create_mview (which is shared with
4378 # _create_materialized_log)
4379 #
4380 sub _create_snapshot_log
4381 {
4382 _create_mview_log( @_, 'SNAPSHOT' );
4383 }
4384  
4385 # sub _create_synonym
4386 #
4387 # Returns DDL to create the named synonym in the form of:
4388 #
4389 # CREATE [PUBLIC] SYNONYM <name> FOR [schema.]<object>[@dblink]
4390 #
4391 sub _create_synonym
4392 {
4393 my ( $schema, $owner, $name, $view ) = @_;
4394  
4395 my $stmt =
4396 "
4397 SELECT
4398 table_owner
4399 , table_name
4400 , NVL(db_link,'NULL')
4401 FROM
4402 ${view}_synonyms
4403 WHERE
4404 synonym_name = UPPER( ? )
4405 ";
4406  
4407 if ( $view eq 'DBA' )
4408 {
4409 $stmt .=
4410 "
4411 AND owner = UPPER('$owner')
4412 ";
4413 }
4414  
4415 $sth = $dbh->prepare( $stmt );
4416 $sth->execute( $name );
4417 my @row = $sth->fetchrow_array;
4418 die "Synonym \U$name \Ldoes not exist.\n\n" unless @row;
4419  
4420 my ( $table_owner, $table_name, $db_link ) = @row;
4421  
4422 $db_link = ( $db_link eq 'NULL' ) ? '' : "\@$db_link";
4423 $schema = ( $schema eq 'PUBLIC' ) ? '' : $schema;
4424 my $is_public = ( "\U$owner" eq 'PUBLIC' ) ? ' PUBLIC' : '';
4425 my $table_schema = _set_schema( $table_owner );
4426  
4427 my $sql = "PROMPT " .
4428 "CREATE$is_public SYNONYM \L$schema$name " .
4429 "FOR \L$table_schema$table_name$db_link \n\n" .
4430 "CREATE$is_public SYNONYM \L$schema$name " .
4431 "FOR \L$table_schema$table_name$db_link;\n\n";
4432  
4433 if ( $attr{ 'grants' } )
4434 {
4435 $sql .= _object_privs( $schema, $table_owner, $table_name, $view );
4436 }
4437  
4438 return $sql;
4439 }
4440  
4441 # sub _create_table
4442 #
4443 # Returns DDL to create the named table and its comments
4444 # and its partition(s) in the form of:
4445 #
4446 # CREATE TABLE [schema.]<name>
4447 # (
4448 # <column list>
4449 # )
4450 # ORGANIZATION {HEAP|INDEX}
4451 # [NO]MONIOTORING
4452 # PARALLEL
4453 # (
4454 # DEGREE <value>
4455 # INSTANCES <value>
4456 # )
4457 # [NO]CACHE
4458 # [PCTUSED] <value>
4459 # PCTFREE <value>
4460 # INITRANS <value>
4461 # MAXTRANS <value>
4462 # STORAGE
4463 # (
4464 # <storage clause>
4465 # )
4466 # [NO]LOGING
4467 # TABLESPACE <name>
4468 # [<partitioning clause>]
4469 #
4470 sub _create_table
4471 {
4472 my ( $schema, $owner, $name, $view ) = @_;
4473  
4474 my $sql;
4475 my $stmt;
4476  
4477 if ( $oracle_major == 7 )
4478 {
4479 $stmt =
4480 "
4481 SELECT
4482 'NO' AS partitioned
4483 , 'NOT IOT' AS iot_type
4484 FROM
4485 ${view}_tables
4486 WHERE
4487 table_name = UPPER( ? )
4488 ";
4489 }
4490 else
4491 {
4492 $stmt =
4493 "
4494 SELECT
4495 partitioned
4496 , iot_type
4497 FROM
4498 ${view}_tables
4499 WHERE
4500 table_name = UPPER( ? )
4501 ";
4502 }
4503  
4504 if ( $view eq 'DBA' )
4505 {
4506 $stmt .=
4507 "
4508 AND owner = UPPER('$owner')
4509 ";
4510 }
4511  
4512 $sth = $dbh->prepare( $stmt );
4513 $sth->execute( $name );
4514 my @row = $sth->fetchrow_array;
4515 die "Table \U$name \Ldoes not exist.\n\n" unless @row;
4516  
4517 my ( $partitioned, $iot_type ) = @row;
4518  
4519 if ( $iot_type eq 'IOT' )
4520 {
4521 if ( $partitioned eq 'YES' )
4522 {
4523 return _create_partitioned_iot( $schema, $owner, $name, $view )
4524 }
4525 else
4526 {
4527 return _create_iot( $schema, $owner, $name, $view )
4528 }
4529 }
4530 elsif ( $partitioned eq 'YES' )
4531 {
4532 return _create_partitioned_table( $schema, $owner, $name, $view )
4533 }
4534  
4535 # We must be a plain, vanilla, non-partitioned, relational table.
4536  
4537 if ( $oracle_major == 7 )
4538 {
4539 $stmt =
4540 "
4541 SELECT
4542 -- Table Properties
4543 'N/A' AS monitoring
4544 , 'N/A' AS table_name
4545 -- Parallel Clause
4546 , LTRIM(t.degree)
4547 , LTRIM(t.instances)
4548 -- Physical Properties
4549 , 'N/A' AS organization
4550 -- Segment Attributes
4551 , DECODE(
4552 LTRIM(t.cache)
4553 ,'Y','CACHE'
4554 , 'NOCACHE'
4555 )
4556 , t.pct_used
4557 , t.pct_free
4558 , DECODE(
4559 t.ini_trans
4560 ,0,1
4561 ,null,1
4562 ,t.ini_trans
4563 ) AS ini_trans
4564 , DECODE(
4565 t.max_trans
4566 ,0,255
4567 ,null,255
4568 ,t.max_trans
4569 ) AS max_trans
4570 -- Storage Clause
4571 , t.initial_extent
4572 , t.next_extent
4573 , t.min_extents
4574 , DECODE(
4575 t.max_extents
4576 ,2147483645,'unlimited'
4577 , t.max_extents
4578 ) AS max_extents
4579 , NVL(t.pct_increase,0)
4580 , NVL(t.freelists,1)
4581 , NVL(t.freelist_groups,1)
4582 , 'N/A' AS buffer_pool
4583 , 'N/A' AS logging
4584 , LOWER(t.tablespace_name) AS tablespace_name
4585 , s.blocks - NVL(t.empty_blocks,0)
4586 FROM
4587 ${view}_tables t
4588 , ${view}_segments s
4589 WHERE
4590 t.table_name = UPPER('$name')
4591 AND t.table_name = s.segment_name
4592 ";
4593 }
4594 elsif ( $oracle_major == 8 and $oracle_minor == 0 )
4595 {
4596 $stmt =
4597 "
4598 SELECT
4599 -- Table Properties
4600 'N/A' AS monitoring
4601 , 'N/A' AS table_name
4602 -- Parallel Clause
4603 , LTRIM(t.degree)
4604 , LTRIM(t.instances)
4605 -- Physical Properties
4606 , DECODE(
4607 t.iot_type
4608 ,'IOT','INDEX'
4609 , 'HEAP'
4610 ) AS organization
4611 -- Segment Attributes
4612 , DECODE(
4613 LTRIM(t.cache)
4614 ,'Y','CACHE'
4615 , 'NOCACHE'
4616 )
4617 , t.pct_used
4618 , t.pct_free
4619 , DECODE(
4620 t.ini_trans
4621 ,0,1
4622 ,null,1
4623 ,t.ini_trans
4624 ) AS ini_trans
4625 , DECODE(
4626 t.max_trans
4627 ,0,255
4628 ,null,255
4629 ,t.max_trans
4630 ) AS max_trans
4631 -- Storage Clause
4632 , t.initial_extent
4633 , t.next_extent
4634 , t.min_extents
4635 , DECODE(
4636 t.max_extents
4637 ,2147483645,'unlimited'
4638 , t.max_extents
4639 ) AS max_extents
4640 , NVL(t.pct_increase,0)
4641 , NVL(t.freelists,1)
4642 , NVL(t.freelist_groups,1)
4643 , 'N/A' AS buffer_pool
4644 , DECODE(
4645 t.logging
4646 ,'NO','NOLOGGING'
4647 , 'LOGGING'
4648 ) AS logging
4649 , LOWER(t.tablespace_name) AS tablespace_name
4650 , s.blocks - NVL(t.empty_blocks,0)
4651 FROM
4652 ${view}_tables t
4653 , ${view}_segments s
4654 WHERE
4655 t.table_name = UPPER('$name')
4656 AND t.table_name = s.segment_name
4657 ";
4658 }
4659 else # We're Oracle8i or newer
4660 {
4661 $stmt =
4662 "
4663 SELECT
4664 -- Table Properties
4665 DECODE(
4666 t.monitoring
4667 ,'NO','NOMONITORING'
4668 , 'MONITORING'
4669 ) AS monitoring
4670 , 'N/A' AS table_name
4671 -- Parallel Clause
4672 , LTRIM(t.degree)
4673 , LTRIM(t.instances)
4674 -- Physical Properties
4675 , DECODE(
4676 t.iot_type
4677 ,'IOT','INDEX'
4678 , 'HEAP'
4679 ) AS organization
4680 -- Segment Attributes
4681 , DECODE(
4682 LTRIM(t.cache)
4683 ,'Y','CACHE'
4684 , 'NOCACHE'
4685 )
4686 , t.pct_used
4687 , t.pct_free
4688 , DECODE(
4689 t.ini_trans
4690 ,0,1
4691 ,null,1
4692 ,t.ini_trans
4693 ) AS ini_trans
4694 , DECODE(
4695 t.max_trans
4696 ,0,255
4697 ,null,255
4698 ,t.max_trans
4699 ) AS max_trans
4700 -- Storage Clause
4701 , t.initial_extent
4702 , t.next_extent
4703 , t.min_extents
4704 , DECODE(
4705 t.max_extents
4706 ,2147483645,'unlimited'
4707 , t.max_extents
4708 ) AS max_extents
4709 , NVL(t.pct_increase,0)
4710 , NVL(t.freelists,1)
4711 , NVL(t.freelist_groups,1)
4712 , LOWER(t.buffer_pool) AS buffer_pool
4713 , DECODE(
4714 t.logging
4715 ,'NO','NOLOGGING'
4716 , 'LOGGING'
4717 ) AS logging
4718 , LOWER(t.tablespace_name) AS tablespace_name
4719 , s.blocks - NVL(t.empty_blocks,0)
4720 FROM
4721 ${view}_tables t
4722 , ${view}_segments s
4723 WHERE
4724 t.table_name = UPPER('$name')
4725 AND t.table_name = s.segment_name
4726 ";
4727 }
4728  
4729 if ( $view eq 'DBA' )
4730 {
4731 $stmt .=
4732 "
4733 AND s.owner = UPPER('$owner')
4734 AND t.owner = s.owner
4735 ";
4736 }
4737  
4738 $sql = _create_table_text( $stmt, $schema, $owner, $name, $view ) .
4739 ";\n\n" .
4740 _create_comments( $schema, $owner, $name, $view );
4741  
4742 if ( $attr{ 'grants' } )
4743 {
4744 $sql .= _object_privs( $schema, $owner, $name, $view );
4745 }
4746  
4747 return $sql;
4748 }
4749  
4750 # sub _create_table_family
4751 #
4752 # Combines the CREATE TABLE statement with its "family" -- Comments,
4753 # Triggers and Constraints
4754 #
4755 sub _create_table_family
4756 {
4757 my ( $schema, $owner, $name, $view ) = @_;
4758  
4759 my $stmt;
4760 my $sql = _create_table( @_ );
4761  
4762 # Add table's indexes
4763 if ( $oracle_major == 7 )
4764 {
4765 $stmt =
4766 "
4767 SELECT
4768 'NOT IOT' AS iot_type
4769 FROM
4770 ${view}_tables
4771 WHERE
4772 table_name = UPPER( ? )
4773 ";
4774 }
4775 else
4776 {
4777 $stmt =
4778 "
4779 SELECT
4780 iot_type
4781 FROM
4782 ${view}_tables
4783 WHERE
4784 table_name = UPPER( ? )
4785 ";
4786 }
4787  
4788 if ( $view eq 'DBA' )
4789 {
4790 $stmt .=
4791 "
4792 AND owner = UPPER('$owner')
4793 ";
4794 }
4795  
4796 $sth = $dbh->prepare( $stmt );
4797 $sth->execute( $name );
4798 my ( $iot ) = $sth->fetchrow_array;
4799  
4800 $stmt =
4801 "
4802 SELECT
4803 index_name
4804 FROM
4805 ${view}_indexes
4806 WHERE
4807 table_name = UPPER( ? )
4808 ";
4809  
4810 if ( $view eq 'DBA' )
4811 {
4812 $stmt .=
4813 "
4814 AND table_owner = UPPER('$owner')
4815 ";
4816 }
4817  
4818 if ( $iot eq 'IOT' )
4819 {
4820 # Omit the PK, it's in the CREATE TABLE on an IOT
4821 $stmt .=
4822 "
4823 MINUS
4824 SELECT
4825 constraint_name
4826 FROM
4827 ${view}_constraints
4828 WHERE
4829 table_name = UPPER( '$name' )
4830 AND constraint_type = 'P'
4831 ";
4832  
4833 if ( $view eq 'DBA' )
4834 {
4835 $stmt .=
4836 "
4837 AND owner = UPPER('$owner')
4838 ";
4839 }
4840 }
4841  
4842 $stmt .=
4843 "
4844 ORDER
4845 BY
4846 1
4847 ";
4848  
4849 $sth = $dbh->prepare( $stmt );
4850 $sth->execute( $name );
4851 my $aref = $sth->fetchall_arrayref;
4852  
4853 foreach my $row ( @$aref )
4854 {
4855 $sql .= _create_index( $schema, $owner, @$row->[0], $view );
4856 }
4857  
4858 # Add table's constraints
4859 $stmt =
4860 "
4861 SELECT
4862 constraint_name
4863 , constraint_type
4864 , search_condition
4865 FROM
4866 ${view}_constraints cn
4867 WHERE
4868 owner = UPPER( ? )
4869 AND table_name = UPPER( ? )
4870 ";
4871  
4872 if ( $iot eq 'IOT' )
4873 {
4874 $stmt .=
4875 "
4876 AND constraint_type IN('U','R','C')
4877 ";
4878 }
4879 else
4880 {
4881 $stmt .=
4882 "
4883 AND constraint_type IN('P','U','R','C')
4884 ";
4885 }
4886  
4887 $stmt .=
4888 "
4889 ORDER
4890 BY
4891 DECODE(
4892 constraint_type
4893 ,'P',1
4894 ,'U',2
4895 ,'R',3
4896 ,'C',4
4897 )
4898 , constraint_name
4899 ";
4900  
4901 $dbh->{ LongReadLen } = 8192; # Allows SEARCH_CONDITION length of 8K
4902 $dbh->{ LongTruncOk } = 1;
4903  
4904 $sth = $dbh->prepare( $stmt );
4905 $sth->execute( $owner, $name );
4906 $aref = $sth->fetchall_arrayref;
4907  
4908 foreach my $row ( @$aref )
4909 {
4910 my ( $cons_name, $cons_type, $condition ) = @$row;
4911  
4912 if ( $cons_type ne 'C' )
4913 {
4914 $sql .= _create_constraint( $schema, $owner, $cons_name, $view );
4915 }
4916 elsif ( $condition !~ /IS NOT NULL/ )
4917 {
4918 $sql .= _create_constraint( $schema, $owner, $cons_name, $view );
4919 }
4920 }
4921  
4922  
4923 # Add table's triggers
4924 $stmt =
4925 "
4926 SELECT
4927 trigger_name
4928 FROM
4929 ${view}_triggers
4930 WHERE
4931 table_name = UPPER( ? )
4932 ";
4933  
4934 if ( $view eq 'DBA' )
4935 {
4936 $stmt .=
4937 "
4938 AND table_owner = UPPER('$owner')
4939 ";
4940 }
4941  
4942 $stmt .=
4943 "
4944 ORDER
4945 BY
4946 trigger_name
4947 ";
4948  
4949 $sth = $dbh->prepare( $stmt );
4950 $sth->execute( $name );
4951 $aref = $sth->fetchall_arrayref;
4952  
4953 foreach my $row ( @$aref )
4954 {
4955 $sql .= _create_trigger( $schema, $owner, @$row->[0], $view );
4956 }
4957  
4958 return $sql;
4959 }
4960  
4961 # sub _create_table_text
4962 #
4963 # Formats the CREATE TABLE statement
4964 #
4965 sub _create_table_text
4966 {
4967 my ( $stmt, $schema, $owner, $name, $view ) = @_;
4968  
4969 $sth = $dbh->prepare( $stmt );
4970 $sth->execute;
4971 my @row = $sth->fetchrow_array;
4972  
4973 # Turn warnings off
4974 $^W = 0;
4975  
4976 my $monitoring = shift @row;
4977 my $table = shift @row;
4978 my $degree = shift @row;
4979 my $instances = shift @row;
4980 my $organization = shift @row;
4981  
4982 my (
4983 # Segment Attributes
4984 $cache,
4985 $pct_used,
4986 $pct_free,
4987 $ini_trans,
4988 $max_trans,
4989 $initial,
4990 $next,
4991 $min_extents,
4992 $max_extents,
4993 $pct_increase,
4994 $freelists,
4995 $freelist_groups,
4996 $buffer_pool,
4997 $logging,
4998 $tablespace,
4999 $blocks,
5000 ) = @row;
5001  
5002 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
5003  
5004 my $sql =
5005 "PROMPT " .
5006 "CREATE TABLE \L$schema$name\n\n" .
5007 "CREATE TABLE \L$schema$name\n" .
5008 "(\n " .
5009 _table_columns( $owner, $name, $view );
5010  
5011 if ( $organization eq 'INDEX' )
5012 {
5013 $stmt =
5014 "
5015 SELECT
5016 constraint_name
5017 FROM
5018 ${view}_constraints
5019 WHERE
5020 table_name = UPPER( ? )
5021 AND constraint_type = 'P'
5022 ";
5023  
5024 if ( $view eq 'DBA' )
5025 {
5026 $stmt .=
5027 "
5028 AND owner = UPPER('$owner')
5029 ";
5030 }
5031  
5032 $sth = $dbh->prepare( $stmt );
5033 $sth->execute( $name );
5034 my ( $index ) = $sth->fetchrow_array;
5035  
5036 $sql .= " , CONSTRAINT \L$index \UPRIMARY KEY\n" .
5037 _index_columns( ' ', $owner, $index, $view, );
5038 }
5039  
5040 $sql .= ")\n";
5041  
5042 $sql .= "ORGANIZATION $organization\n" if $oracle_major > 7;
5043  
5044 if (
5045 $oracle_major > 8
5046 or ( $oracle_major == 8 and $oracle_minor > 0 )
5047 )
5048 {
5049 $sql .= "$monitoring\n";
5050 }
5051  
5052 unless ( $organization eq 'INDEX'
5053 and $oracle_major == 8
5054 and $oracle_minor == 0
5055 )
5056 {
5057 $sql .= "PARALLEL\n" .
5058 "(\n" .
5059 " DEGREE $degree\n" .
5060 " INSTANCES $instances\n" .
5061 ")\n";
5062 }
5063  
5064 unshift @row, ( '', $organization );
5065  
5066 $sql .= _segment_attributes( \@row );
5067  
5068 if ( $organization eq 'INDEX' )
5069 {
5070 if ( $oracle_major == 8 and $oracle_minor == 0 )
5071 {
5072 $stmt =
5073 "
5074 SELECT
5075 c.column_name
5076 , i.pct_threshold
5077 , 'HEAP' AS organization
5078 -- Segment Attributes
5079 , 'N/A' AS cache
5080 , t.pct_used
5081 , t.pct_free
5082 , DECODE(
5083 t.ini_trans
5084 ,0,1
5085 ,null,1
5086 ,t.ini_trans
5087 ) AS ini_trans
5088 , DECODE(
5089 t.max_trans
5090 ,0,255
5091 ,null,255
5092 ,t.max_trans
5093 ) AS max_trans
5094 -- Storage Clause
5095 , t.initial_extent
5096 , t.next_extent
5097 , t.min_extents
5098 , DECODE(
5099 t.max_extents
5100 ,2147483645,'unlimited'
5101 , t.max_extents
5102 ) AS max_extents
5103 , NVL(t.pct_increase,0)
5104 , NVL(t.freelists,1)
5105 , NVL(t.freelist_groups,1)
5106 , 'N/A' AS buffer_pool
5107 , DECODE(
5108 t.logging
5109 ,'NO','NOLOGGING'
5110 , 'LOGGING'
5111 ) AS logging
5112 , LOWER(t.tablespace_name) AS tablespace_name
5113 , s.blocks - NVL(t.empty_blocks,0)
5114 FROM
5115 ${view}_tables t
5116 , ${view}_indexes i
5117 , ${view}_segments s
5118 , ${view}_tab_columns c
5119 WHERE
5120 t.iot_name = UPPER('$name')
5121 AND i.table_name = t.iot_name
5122 AND c.table_name (+) = i.table_name
5123 AND c.column_id (+) = i.include_column
5124 AND s.segment_name = t.table_name
5125 ";
5126 }
5127 else # We're Oracle8i or newer
5128 {
5129 $stmt =
5130 "
5131 SELECT
5132 c.column_name
5133 , i.pct_threshold
5134 , 'HEAP' AS organization
5135 -- Segment Attributes
5136 , 'N/A' AS cache
5137 , t.pct_used
5138 , t.pct_free
5139 , DECODE(
5140 t.ini_trans
5141 ,0,1
5142 ,null,1
5143 ,t.ini_trans
5144 ) AS ini_trans
5145 , DECODE(
5146 t.max_trans
5147 ,0,255
5148 ,null,255
5149 ,t.max_trans
5150 ) AS max_trans
5151 -- Storage Clause
5152 , t.initial_extent
5153 , t.next_extent
5154 , t.min_extents
5155 , DECODE(
5156 t.max_extents
5157 ,2147483645,'unlimited'
5158 , t.max_extents
5159 ) AS max_extents
5160 , NVL(t.pct_increase,0)
5161 , NVL(t.freelists,1)
5162 , NVL(t.freelist_groups,1)
5163 , LOWER(t.buffer_pool) AS buffer_pool
5164 , DECODE(
5165 t.logging
5166 ,'NO','NOLOGGING'
5167 , 'LOGGING'
5168 ) AS logging
5169 , LOWER(t.tablespace_name) AS tablespace_name
5170 , s.blocks - NVL(t.empty_blocks,0)
5171 FROM
5172 ${view}_tables t
5173 , ${view}_indexes i
5174 , ${view}_segments s
5175 , ${view}_tab_columns c
5176 WHERE
5177 t.iot_name = UPPER('$name')
5178 AND i.table_name = t.iot_name
5179 AND c.table_name (+) = i.table_name
5180 AND c.column_id (+) = i.include_column
5181 AND s.segment_name = t.table_name
5182 ";
5183 }
5184  
5185 if ( $view eq 'DBA' )
5186 {
5187 $stmt .=
5188 "
5189 AND t.owner = UPPER('$owner')
5190 AND s.owner = t.owner
5191 AND c.owner (+) = i.table_owner
5192 AND i.table_owner = t.owner
5193 ";
5194 }
5195  
5196 $sth = $dbh->prepare( $stmt );
5197 $sth->execute;
5198 my @row = $sth->fetchrow_array;
5199  
5200 if ( @row )
5201 {
5202 my $column = shift @row;
5203 my $pct_threshold = shift @row;
5204  
5205 $sql .= "PCTTHRESHOLD $pct_threshold\n";
5206 $sql .= "INCLUDING $column\n" if $column;
5207 $sql .= "OVERFLOW\n";
5208 }
5209 }
5210  
5211 return $sql;
5212 }
5213  
5214 # sub _create_tablespace
5215 #
5216 # Returns DDL to create the named tablespace in the form of:
5217 #
5218 # CREATE [TEMPORARY] TABLESPACE <name>
5219 # {DATA|TEMP}FILE
5220 # '<filespec>'
5221 # , '<filespec>'
5222 # DEFAULT STORAGE
5223 # (
5224 # <storage clause>
5225 # )
5226 # [MINIMUM EXTENT <bytes>]
5227 # ]PERMANENT|TEMPORARY]
5228 # [EXTENT MANAGEMENT {DICTIONARY|LOCAL <extent spec>}]
5229 # [[NO]LOGGING]
5230 #
5231 sub _create_tablespace
5232 {
5233 my ( $schema, $owner, $name, $view ) = @_;
5234  
5235 die "\nYou must use the DBA views in order to CREATE TABLESPACE\n\n"
5236 unless $view eq 'DBA';
5237  
5238 my $sql;
5239 my $stmt;
5240 my $file_type;
5241  
5242 if ( $oracle_major == 7 )
5243 {
5244 $stmt =
5245 "
5246 SELECT
5247 initial_extent
5248 , next_extent
5249 , min_extents
5250 , DECODE(
5251 max_extents
5252 ,2147483645,'unlimited'
5253 ,null,DECODE(
5254 $block_size
5255 , 1, 57
5256 , 2, 121
5257 , 4, 249
5258 , 8, 505
5259 ,16,1017
5260 ,32,2041
5261 ,'???'
5262 )
5263 ,max_extents
5264 ) AS max_extents
5265 , pct_increase
5266 , 0 AS min_extlen
5267 , contents
5268 , 'N/A' AS logging
5269 , 'N/A' AS extent_management
5270 , 'N/A' AS allocation_type
5271 FROM
5272 dba_tablespaces
5273 WHERE
5274 tablespace_name = UPPER( ? )
5275 ";
5276 }
5277 elsif ( $oracle_major == 8 and $oracle_minor == 0 )
5278 {
5279 $stmt =
5280 "
5281 SELECT
5282 initial_extent
5283 , next_extent
5284 , min_extents
5285 , DECODE(
5286 max_extents
5287 ,2147483645,'unlimited'
5288 ,null,DECODE(
5289 $block_size
5290 , 1, 57
5291 , 2, 121
5292 , 4, 249
5293 , 8, 505
5294 ,16,1017
5295 ,32,2041
5296 ,'???'
5297 )
5298 ,max_extents
5299 ) AS max_extents
5300 , pct_increase
5301 , min_extlen
5302 , contents
5303 , DECODE(
5304 logging
5305 ,'NO','NOLOGGING'
5306 , 'LOGGING'
5307 ) AS logging
5308 , 'N/A' AS extent_management
5309 , 'N/A' AS allocation_type
5310 FROM
5311 dba_tablespaces
5312 WHERE
5313 tablespace_name = UPPER( ? )
5314 ";
5315 }
5316 else # We're newer than Oracle 8.0
5317 {
5318 $stmt =
5319 "
5320 SELECT
5321 initial_extent
5322 , next_extent
5323 , min_extents
5324 , DECODE(
5325 max_extents
5326 ,2147483645,'unlimited'
5327 ,null,DECODE(
5328 $block_size
5329 , 1, 57
5330 , 2, 121
5331 , 4, 249
5332 , 8, 505
5333 ,16,1017
5334 ,32,2041
5335 ,'???'
5336 )
5337 ,max_extents
5338 ) AS max_extents
5339 , pct_increase
5340 , min_extlen
5341 , contents
5342 , logging
5343 , extent_management
5344 , allocation_type
5345 FROM
5346 dba_tablespaces
5347 WHERE
5348 tablespace_name = UPPER( ? )
5349 ";
5350 }
5351  
5352 $sth = $dbh->prepare( $stmt );
5353 $sth->execute( $name );
5354 my @row = $sth->fetchrow_array;
5355 die "Tablespace \U$name \Ldoes not exist.\n\n" unless @row;
5356  
5357 my (
5358 $initial,
5359 $next,
5360 $min_extents,
5361 $max_extents,
5362 $pct_increase,
5363 $min_extlen,
5364 $contents,
5365 $logging,
5366 $extent_management,
5367 $allocation_type,
5368 ) = @row;
5369  
5370 if ( $extent_management eq 'LOCAL' and $contents eq 'TEMPORARY' )
5371 {
5372 $sql = "PROMPT " .
5373 "CREATE TEMPORARY TABLESPACE \L$name\n\n" .
5374 "CREATE TEMPORARY TABLESPACE \L$name\n";
5375 }
5376 else
5377 {
5378 $sql = "PROMPT " .
5379 "CREATE TABLESPACE \L$name\n\n" .
5380 "CREATE TABLESPACE \L$name\n";
5381 }
5382  
5383 if ( $oracle_major == 7 )
5384 {
5385 $file_type = 'DATA';
5386  
5387 $stmt =
5388 "
5389 SELECT
5390 file_name
5391 , bytes
5392 , 'N/A' AS autoextensible
5393 , 'N/A' AS maxbytes
5394 , 'N/A' AS increment_by
5395 FROM
5396 dba_data_files
5397 WHERE
5398 tablespace_name = UPPER( ? )
5399 ORDER
5400 BY
5401 file_name
5402 ";
5403 }
5404 else # We're newer than Oracle7
5405 {
5406 $stmt =
5407 "
5408 SELECT
5409 count(*)
5410 FROM
5411 dba_data_files
5412 WHERE
5413 tablespace_name = UPPER( ? )
5414 ";
5415  
5416 $sth = $dbh->prepare( $stmt );
5417 $sth->execute( $name );
5418 my ( $cnt ) = $sth->fetchrow_array;
5419 $file_type = ( $cnt == 0 ) ? 'TEMP' : 'DATA';
5420  
5421 $stmt =
5422 "
5423 SELECT
5424 file_name
5425 , bytes
5426 , autoextensible
5427 , DECODE(
5428 SIGN(2147483645 - maxbytes)
5429 ,-1,'unlimited'
5430 ,maxbytes
5431 ) AS maxbytes
5432 , increment_by * $block_size * 1024 AS increment_by
5433 FROM
5434 dba_${file_type}_files
5435 WHERE
5436 tablespace_name = UPPER( ? )
5437 ORDER
5438 BY
5439 file_name
5440 ";
5441 }
5442  
5443 $sql .= "${file_type}FILE\n";
5444  
5445 $sth = $dbh->prepare( $stmt );
5446 $sth->execute( $name );
5447 my $aref = $sth->fetchall_arrayref;
5448  
5449 my $comma = ' ';
5450 foreach my $row ( @$aref )
5451 {
5452 my (
5453 $file_name,
5454 $bytes,
5455 $autoextensible,
5456 $maxbytes,
5457 $increment_by,
5458 ) = @$row;
5459  
5460 $sql .= "$comma '$file_name' SIZE $bytes REUSE\n";
5461  
5462 if ( $oracle_major > 7 )
5463 {
5464 $sql .= " AUTOEXTEND ";
5465  
5466 if ( $autoextensible eq 'YES' )
5467 {
5468 $sql .= "ON NEXT $increment_by MAXSIZE $maxbytes\n";
5469 }
5470 else
5471 {
5472 $sql .= "OFF\n";
5473 }
5474 }
5475  
5476 $comma = ' ,';
5477 }
5478  
5479 if ( $extent_management eq 'LOCAL' )
5480 {
5481 $sql .= "EXTENT MANAGEMENT LOCAL ";
5482  
5483 if ( $allocation_type eq 'SYSTEM' )
5484 {
5485 $sql .= "AUTOALLOCATE\n";
5486 }
5487 else
5488 {
5489 $sql .= "UNIFORM SIZE $next\n";
5490 }
5491 }
5492 else # It's Dictionary Managed, Oracle8.0 or Oracle7
5493 {
5494 $sql .= "DEFAULT STORAGE\n" .
5495 "(\n" .
5496 " INITIAL $initial\n" .
5497 " NEXT $next\n" .
5498 " MINEXTENTS $min_extents\n" .
5499 " MAXEXTENTS $max_extents\n" .
5500 " PCTINCREASE $pct_increase\n" .
5501 ")\n" .
5502 "$contents\n";;
5503  
5504 if ( $min_extlen > 0 )
5505 {
5506 $sql .= "MINUMUM EXTENT $min_extlen\n";
5507 }
5508  
5509 if (
5510 $oracle_major > 8
5511 or ( $oracle_major == 8 and $oracle_minor > 0 )
5512 )
5513 {
5514 $sql .= "EXTENT MANAGEMENT DICTIONARY\n";
5515 }
5516 }
5517  
5518 if ( $oracle_major > 7 )
5519 {
5520 $sql .= "$logging\n" unless (
5521 $contents eq 'TEMPORARY'
5522 and $extent_management eq 'LOCAL'
5523 );
5524 }
5525  
5526 $sql .= ";\n\n";
5527  
5528 return $sql;
5529 }
5530  
5531 # sub _create_trigger
5532 #
5533 # Returns DDL to create the named trigger in the form of:
5534 #
5535 # CREATE OR REPLACE TRIGGER [schema.]<name>
5536 # {BEFORE|AFTER|INSTEAD OF} <triggering event>
5537 # [OF <column list ]ON {[schema.]<table>|DATABASE|SCHEMA}
5538 # REFERENCING <new> AS NEW <old> AS OLD
5539 # [WHEN <whatever>]
5540 # [FOR EACH ROW]
5541 # <code>
5542 #
5543 sub _create_trigger
5544 {
5545 my ( $schema, $owner, $name, $view ) = @_;
5546  
5547 my $stmt;
5548 if (
5549 $oracle_major > 8
5550 or ( $oracle_major == 8 and $oracle_minor > 0 )
5551 )
5552 {
5553 $stmt =
5554 "
5555 SELECT
5556 trigger_type
5557 , RTRIM(triggering_event)
5558 , table_owner
5559 , table_name
5560 , base_object_type
5561 , referencing_names
5562 , DECODE(
5563 when_clause
5564 ,null,null
5565 ,'WHEN (' || when_clause || ')' || CHR(10)
5566 )
5567 , trigger_body
5568 FROM
5569 ${view}_triggers
5570 WHERE
5571 trigger_name = UPPER( ? )
5572 ";
5573 }
5574 else
5575 {
5576 $stmt =
5577 "
5578 SELECT
5579 trigger_type
5580 , RTRIM(triggering_event)
5581 , table_owner
5582 , table_name
5583 -- Only table triggers before 8i
5584 , 'TABLE' AS base_object_type
5585 , referencing_names
5586 , DECODE(
5587 when_clause
5588 ,null,null
5589 ,'WHEN (' || when_clause || ')' || CHR(10)
5590 )
5591 , trigger_body
5592 FROM
5593 ${view}_triggers
5594 WHERE
5595 trigger_name = UPPER( ? )
5596 ";
5597 }
5598  
5599 if ( $view eq 'DBA' )
5600 {
5601 $stmt .=
5602 "
5603 AND table_owner = UPPER('$owner')
5604 ";
5605 }
5606  
5607 $dbh->{ LongReadLen } = 65536; # Allows TRIGGER_BODY length of 64K
5608 $dbh->{ LongTruncOk } = 1;
5609  
5610 $sth = $dbh->prepare( $stmt );
5611 $sth->execute( $name );
5612 my @row = $sth->fetchrow_array;
5613 die "\nTrigger \U$schema$name \Ldoes not exist.\n\n" unless @row;
5614  
5615 my (
5616 $trigger_type,
5617 $event,
5618 $table_owner,
5619 $table,
5620 $base_type,
5621 $ref_names,
5622 $when,
5623 $body,
5624 ) = @row;
5625  
5626 my ( $trg_type ) = $trigger_type =~ /(BEFORE|AFTER|INSTEAD OF)/;
5627 my $schema2 = _set_schema( $table_owner );
5628 my $object = ( $base_type eq 'TABLE' ) ? $schema2 . $table :
5629 ( $base_type eq 'SCHEMA' ) ? $schema . 'SCHEMA' : $base_type;
5630 $ref_names =~ s/ING (\w+) AS NEW (\w+)/ING \L$1 \UAS NEW \L$2/;
5631  
5632 # When clause sometimes ends in a null
5633 $when =~ s/\c@//g;
5634  
5635 # Body sometimes ends in a null
5636 $body =~ s/\c@//g;
5637  
5638 my $columns;
5639 if ( $event =~ /UPDATE/ )
5640 {
5641 $stmt =
5642 "
5643 SELECT
5644 LOWER(column_name)
5645 FROM
5646 ${view}_trigger_cols
5647 WHERE
5648 trigger_name = UPPER( ? )
5649 AND column_list = 'YES'
5650 ";
5651  
5652 if ( $view eq 'DBA' )
5653 {
5654 $stmt .=
5655 "
5656 AND table_owner = UPPER('$owner')
5657 ";
5658 }
5659  
5660 $sth = $dbh->prepare( $stmt );
5661 $sth->execute( $name );
5662  
5663 my (@columns) = map { $_->[0] } @{ $sth->fetchall_arrayref };
5664  
5665 $columns = "\nOF\n " . join ("\n , ", @columns) . "\n" if ( @columns );
5666 }
5667  
5668  
5669 my $sql = "PROMPT " .
5670 "CREATE OR REPLACE TRIGGER \L$schema$name\n\n" .
5671 "CREATE OR REPLACE TRIGGER \L$schema$name\n" .
5672 "$trg_type $event ${columns}ON \L$object\n";
5673  
5674 $sql .= "$ref_names\n" if $base_type =~ /TABLE|VIEW/;
5675 $sql .= "FOR EACH ROW\n" if $trigger_type =~ /EACH ROW/;
5676 $sql .= $when if $when;
5677  
5678 $sql .= $body;
5679 $sql .= "\n" unless $sql =~ /\Z\n/;
5680 $sql .= "/\n\n";
5681  
5682 return $sql;
5683 }
5684  
5685 # sub _create_type
5686 #
5687 # Returns DDL to create the named procedure in the form of:
5688 #
5689 # CREATE OR REPLACE TYPE [schema.]<name>
5690 # AS
5691 # <source>
5692 #
5693 # by calling _display_source
5694 #
5695 sub _create_type
5696 {
5697 my ( $schema, $owner, $name, $view ) = @_;
5698  
5699 my $sql = _display_source( $schema, $owner, $name, $view, 'TYPE' );
5700  
5701 if ( $attr{ 'grants' } )
5702 {
5703 $sql .= _object_privs( $schema, $owner, $name, $view );
5704 }
5705  
5706 return $sql;
5707 }
5708  
5709 # sub _create_user
5710 #
5711 # Returns DDL to create the named user in the form of:
5712 #
5713 # CREATE USER <name> IDENTIFIED {EXTERNALLY|BY VALUES '<values>'}
5714 # PROFILE <profile>
5715 # DEFAULT TABLESPACE <tablespace>
5716 # TEMPORARY TABLESPACE <tablespace>
5717 # [QUOTA {UNLIMITED|<bytes>} ON TABLESPACE <tablespace1>]
5718 # [QUOTA {UNLIMITED|<bytes>} ON TABLESPACE <tablespace2>]
5719 #
5720 # [GRANT <role > TO <name> [WITH ADMIN OPTION]]
5721 # [GRANT <system privilege> TO <name> [WITH ADMIN OPTION]]
5722 # [GRANT <privilege> ON <object> TO <name> [WITH GRANT OPTION]]
5723 #
5724 sub _create_user
5725 {
5726 my ( $schema, $owner, $name, $view ) = @_;
5727  
5728 die "\nYou must use the DBA views in order to CREATE USER\n\n"
5729 unless $view eq 'DBA';
5730  
5731 my $stmt =
5732 "
5733 SELECT
5734 DECODE(
5735 password
5736 ,'EXTERNAL','EXTERNALLY'
5737 ,'BY VALUES ''' || password || ''''
5738 ) AS password
5739 , profile
5740 , default_tablespace
5741 , temporary_tablespace
5742 FROM
5743 dba_users
5744 WHERE
5745 username = UPPER( ? )
5746 ";
5747  
5748 $sth = $dbh->prepare( $stmt );
5749 $sth->execute( $name );
5750 my @row = $sth->fetchrow_array;
5751 die "\nUser \U$name \Ldoes not exist.\n\n" unless @row;
5752  
5753 my (
5754 $password,
5755 $profile,
5756 $default_tablespace,
5757 $temporary_tablespace,
5758 ) = @row;
5759  
5760 my $sql =
5761 "PROMPT " .
5762 "CREATE USER \L$name\n\n" .
5763 "CREATE USER \L$name \UIDENTIFIED $password\n" .
5764 " \UPROFILE \L$profile\n" .
5765 " \UDEFAULT TABLESPACE \L$default_tablespace\n" .
5766 " \UTEMPORARY TABLESPACE \L$temporary_tablespace\n";
5767  
5768 # Add tablespace quotas
5769 $stmt =
5770 "
5771 SELECT
5772 DECODE(
5773 max_bytes
5774 ,-1,'unlimited'
5775 ,TO_CHAR(max_bytes,'99999999')
5776 ) AS max_bytes
5777 , tablespace_name
5778 FROM
5779 dba_ts_quotas
5780 WHERE
5781 username = UPPER( ? )
5782 ORDER
5783 BY
5784 tablespace_name
5785 ";
5786  
5787 $sth = $dbh->prepare( $stmt );
5788 $sth->execute( $name );
5789 my $aref = $sth->fetchall_arrayref;
5790  
5791 foreach my $row ( @$aref )
5792 {
5793 $sql .= " QUOTA @$row->[0] ON \L@$row->[1]\n";
5794 }
5795  
5796 $sql .= ";\n\n";
5797  
5798 $sql .= _granted_privs( $name );
5799  
5800 return $sql;
5801 }
5802  
5803 # sub _create_view
5804 #
5805 # Returns DDL to create the named view in the form of:
5806 #
5807 # CREATE OR REPLACE VIEW [schema.]<name>
5808 # (
5809 # <column aliases>
5810 # )
5811 # AS
5812 # <query>
5813 #
5814 sub _create_view
5815 {
5816 my ( $schema, $owner, $name, $view ) = @_;
5817  
5818 my $stmt =
5819 "
5820 SELECT
5821 text
5822 FROM
5823 ${view}_views
5824 WHERE
5825 view_name = UPPER( ? )
5826 ";
5827  
5828 if ( $view eq 'DBA' )
5829 {
5830 $stmt .=
5831 "
5832 AND owner = UPPER('$owner')
5833 ";
5834 }
5835  
5836 $dbh->{ LongReadLen } = 65536; # Allows View TEXT length of 64K
5837 $dbh->{ LongTruncOk } = 1;
5838  
5839 $sth = $dbh->prepare( $stmt );
5840 $sth->execute( $name );
5841 my ( $text ) = $sth->fetchrow_array;
5842 die "\nView \U$name \Ldoes not exist.\n\n" unless $text;
5843  
5844 $stmt =
5845 "
5846 SELECT
5847 LOWER(column_name)
5848 FROM
5849 ${view}_tab_columns
5850 WHERE
5851 table_name = UPPER( ? )
5852 ";
5853
5854 if ( $view eq 'DBA' )
5855 {
5856 $stmt .=
5857 "
5858 AND owner = UPPER('$owner')
5859 ";
5860 }
5861
5862 $stmt .=
5863 "
5864 ORDER
5865 BY
5866 column_id
5867 ";
5868
5869 $sth = $dbh->prepare( $stmt );
5870 $sth->execute( $name );
5871 my (@columns) = map { $_->[0] } @{ $sth->fetchall_arrayref };
5872  
5873 return "PROMPT " .
5874 "CREATE OR REPLACE VIEW \L$schema$name\n\n" .
5875 "CREATE OR REPLACE VIEW \L$schema$name\n" .
5876 "(\n " .
5877 join ("\n , ", @columns) .
5878 "\n)\n" .
5879 "AS\n" .
5880 "$text" .
5881 ";\n\n";
5882 }
5883  
5884 # sub _display_source
5885 #
5886 # Returns DDL to create the named stored item in the form of:
5887 #
5888 # CREATE OR REPLACE <TYPE> [schema.]<name>
5889 # <source>
5890 #
5891 # where TYPE is one of: PROCEDURE, FUNCTION, PACKAGE, PACKAGE BODY
5892 #
5893 sub _display_source
5894 {
5895 my ( $schema, $owner, $name, $view, $type ) = @_;
5896  
5897 my $sql;
5898 my $stmt =
5899 "
5900 SELECT
5901 text
5902 FROM
5903 ${view}_source
5904 WHERE
5905 type = '$type'
5906 AND name = UPPER( ? )
5907 ";
5908  
5909 if ( $view eq 'DBA' )
5910 {
5911 $stmt .=
5912 "
5913 AND owner = UPPER('$owner')
5914 ";
5915 }
5916  
5917 $stmt .=
5918 "
5919 ORDER
5920 BY
5921 line
5922 ";
5923  
5924 $sth = $dbh->prepare( $stmt );
5925 $sth->execute( $name );
5926 my $aref = $sth->fetchall_arrayref;
5927 die "\n\u\L$type \U$name \Ldoes not exist.\n\n" unless @$aref;
5928  
5929 $sql = "PROMPT " .
5930 "CREATE OR REPLACE $type \L$schema$name\n\n" .
5931 "CREATE OR REPLACE ";
5932  
5933 my $i = 1;
5934 foreach my $row ( @$aref )
5935 {
5936 if ( $i++ == 1 )
5937 {
5938 # source.text already includes <TYPE> <name>
5939 # We want to insert the schema right before the name
5940  
5941 # @$row->[0] =~ s/$type\s+\S+/$type \L$schema$name/i;
5942 # The following line was submitted by Sandor Toth, to fix the condition
5943 # where parameters immediately followed the name, as in
5944 # CREATE OR REPLACE PROCEDURE foo( var1 IN VARCHAR2)
5945 # ^
5946 # |
5947 # observe, no space between 'foo' and '('
5948  
5949 @$row->[0] =~ s/($type)\s+($name)\s*(.*)$/$1 \L$schema$2 $3/i;
5950 }
5951  
5952 $sql .= "@$row->[0]";
5953 }
5954  
5955 $sql .= "\n" unless $sql =~ /\Z\n/;
5956 $sql .= "/\n\n";
5957  
5958 return $sql;
5959 }
5960  
5961 # sub _drop_constraint
5962 #
5963 # Returns DDL to drop the named constraint in the form of:
5964 #
5965 # ALTER TABLE [schema.]<name> DROP CONSTRAINT <name>
5966 #
5967 sub _drop_constraint
5968 {
5969 my ( $schema, $name, $type, $owner, $view ) = @_;
5970  
5971 my $stmt =
5972 "
5973 SELECT
5974 table_name
5975 FROM
5976 ${view}_constraints
5977 WHERE
5978 owner = UPPER( ? )
5979 AND constraint_name = UPPER( ? )
5980 ";
5981  
5982 $sth = $dbh->prepare( $stmt );
5983 $sth->execute( $owner, $name );
5984 my ( $table_name ) = $sth->fetchrow_array;
5985  
5986 return "PROMPT " .
5987 "ALTER TABLE \L$schema$table_name \UDROP CONSTRAINT \L$name \n\n" .
5988 "ALTER TABLE \L$schema$table_name \UDROP CONSTRAINT \L$name;\n\n";
5989 }
5990  
5991 # sub _drop_database_link
5992 #
5993 # Returns DDL to drop the named database link in the form of:
5994 #
5995 # DROP [PUBLIC] DATABASE LINK <name>
5996 #
5997 sub _drop_database_link
5998 {
5999 my ( $schema, $name, $type ) = @_;
6000  
6001 my $public = ( $schema eq 'PUBLIC' ) ? ' PUBLIC' : '';
6002  
6003 return "PROMPT " .
6004 "DROP$public DATABASE LINK \L$name \n\n" .
6005 "DROP$public DATABASE LINK \L$name ;\n\n" ;
6006 }
6007  
6008 # sub _drop_materialized_view_log
6009 #
6010 # Returns DDL to drop the named materialized view
6011 # by calling _drop_mview_log (which is shared with
6012 # _drop_snapshot_log)
6013 #
6014 sub _drop_materialized_view_log
6015 {
6016 my ( $schema, $name, $type ) = @_;
6017  
6018 _drop_mview_log( $schema, $name, 'MATERIALIZED VIEW' );
6019 }
6020  
6021 # sub _drop_mview_log
6022 #
6023 # Returns DDL to drop the named database link in the form of:
6024 #
6025 # DROP MATERIALIZED VIEW LOG ON [schema.]<name>
6026 # or
6027 # DROP SNAPSHOT LOG ON [schema.]<name>
6028 #
6029 sub _drop_mview_log
6030 {
6031 my ( $schema, $name, $type ) = @_;
6032  
6033 return "PROMPT " .
6034 "DROP $type LOG ON \L$schema$name \n\n" .
6035 "DROP $type LOG ON \L$schema$name ;\n\n" ;
6036 }
6037  
6038 # sub _drop_object
6039 #
6040 # Returns generic DDL to drop the named object in the form of:
6041 #
6042 # DROP <TYPE> <name>
6043 #
6044 sub _drop_object
6045 {
6046 my ( $schema, $name, $type ) = @_;
6047  
6048 return "PROMPT " .
6049 "DROP \U$type \L$name \n\n" .
6050 "DROP \U$type \L$name ;\n\n";
6051 }
6052  
6053 # sub _drop_profile
6054 #
6055 # Returns DDL to drop the named profile in the form of:
6056 #
6057 # DROP PROFILE <name> CASCADE
6058 #
6059 sub _drop_profile
6060 {
6061 my ( $schema, $name, $type ) = @_;
6062  
6063 my $public = ( $schema eq 'PUBLIC' ) ? ' PUBLIC' : '';
6064  
6065 return "PROMPT " .
6066 "DROP PROFILE \L$name \UCASCADE \n\n" .
6067 "DROP PROFILE \L$name \UCASCADE ; \n\n" ;
6068 }
6069  
6070 # sub _drop_schema_object
6071 #
6072 # Returns generic DDL to drop the named object in the form of:
6073 #
6074 # DROP <TYPE> [schema.]<name>
6075 #
6076 sub _drop_schema_object
6077 {
6078 my ( $schema, $name, $type ) = @_;
6079  
6080 return "PROMPT " .
6081 "DROP \U$type \L$schema$name \n\n" .
6082 "DROP \U$type \L$schema$name ;\n\n";
6083 }
6084  
6085 # sub _drop_snapshot_log
6086 #
6087 # Returns DDL to drop the named snapshot log
6088 # by calling _drop_mview_log (which is shared with
6089 # _drop_materialized_view_log)
6090 #
6091 sub _drop_snapshot_log
6092 {
6093 my ( $schema, $name, $type ) = @_;
6094  
6095 _drop_mview_log( $schema, $name, 'SNAPSHOT' );
6096 }
6097  
6098 # sub _drop_synonym
6099 #
6100 # Returns DDL to drop the named object in the form of:
6101 #
6102 # DROP PUBLIC SYNONYM <name>
6103 # or
6104 # DROP SYNONYM [schema.]<name>
6105 #
6106 sub _drop_synonym
6107 {
6108 my ( $schema, $name, $type ) = @_;
6109  
6110 my $public = ( $schema eq 'PUBLIC' ) ? ' PUBLIC' : '';
6111  
6112 if ( $public )
6113 {
6114 return "PROMPT " .
6115 "DROP PUBLIC SYNONYM \L$name \n\n" .
6116 "DROP PUBLIC SYNONYM \L$name ;\n\n" ;
6117 } else
6118 {
6119 return "PROMPT " .
6120 "DROP SYNONYM \L$schema$name \n\n" .
6121 "DROP SYNONYM \L$schema$name ;\n\n";
6122 }
6123 }
6124  
6125 # sub _drop_table
6126 #
6127 # Returns DDL to drop the named table in the form of:
6128 #
6129 # DROP TABLE [schema.]<name> CASCADE CONSTRAINTS
6130 #
6131 sub _drop_table
6132 {
6133 my ( $schema, $name, $type ) = @_;
6134  
6135 return "PROMPT " .
6136 "DROP TABLE \L$schema$name \UCASCADE CONSTRAINTS \n\n" .
6137 "DROP TABLE \L$schema$name \UCASCADE CONSTRAINTS ;\n\n";
6138 }
6139  
6140 # sub _drop_tablespace
6141 #
6142 # Returns DDL to drop the named tablespace in the form of:
6143 #
6144 # DROP TABLESPACE <name> INCLUDING CONTENTS CASCADE CONSTRAINTS
6145 #
6146 sub _drop_tablespace
6147 {
6148 my ( $schema, $name, $type ) = @_;
6149  
6150 return "PROMPT " .
6151 "DROP TABLESPACE \L$name " .
6152 "\UINCLUDING CONTENTS CASCADE CONSTRAINTS \n\n" .
6153 "DROP TABLESPACE \L$name " .
6154 "\UINCLUDING CONTENTS CASCADE CONSTRAINTS ;\n\n";
6155 }
6156  
6157 # sub _drop_user
6158 #
6159 # Returns DDL to drop the named user in the form of:
6160 #
6161 # DROP USER <name> CASCADE
6162 #
6163 sub _drop_user
6164 {
6165 my ( $schema, $name, $type ) = @_;
6166  
6167 return "PROMPT " .
6168 "DROP USER \L$name \UCASCADE \n\n" .
6169 "DROP USER \L$name \UCASCADE ;\n\n";
6170 }
6171  
6172 #sub _generate_heading
6173 #
6174 # Initializes $ddl
6175 #
6176 sub _generate_heading
6177 {
6178 $ddl = "";
6179 return unless $attr{ heading };
6180  
6181 my ( $module, $action, $type, $list ) = @_;
6182  
6183 $ddl = "REM This DDL was reverse engineered by\n" .
6184 "REM Perl module $module, Version $DDL::Oracle::VERSION\n" .
6185 "REM\n" .
6186 "REM at: $host\n" .
6187 "REM from: $instance, an Oracle Release $oracle_release instance\n" .
6188 "REM\n" .
6189 "REM on: " . scalar ( localtime ) . "\n" .
6190 "REM\n";
6191  
6192 if ( $action eq 'FREE SPACE' )
6193 {
6194 $ddl .= "REM Generating $action \Lreport";
6195 }
6196 elsif ( "\U$type" eq 'COMPONENTS' )
6197 {
6198 $ddl .= "REM Generating CREATE <component> statements\n\n";
6199 return;
6200 }
6201 elsif ( "\U$type" eq 'SCHEMA' )
6202 {
6203 $ddl .= "REM Generating CREATE statements for all Objects in schema";
6204 }
6205 else
6206 {
6207 $ddl .= "REM Generating $action \U$type \Lstatement";
6208 }
6209  
6210 $ddl .= ( @$list == 1 ) ? '' : "s";
6211  
6212 if ( "\U$type" eq 'SCHEMA' )
6213 {
6214 $ddl .= "\n";
6215 }
6216 else
6217 {
6218 $ddl .= " for:\n";
6219 }
6220  
6221 $ddl .= "REM\n";
6222  
6223 # Only include the schema if the Type has such a beast.
6224 foreach my $row ( @$list )
6225 {
6226 # These don't
6227 if (
6228 "\L$type" eq 'directory'
6229 or "\L$type" eq 'library'
6230 or "\L$type" eq 'profile'
6231 or "\L$type" eq 'role'
6232 or "\L$type" eq 'rollback segment'
6233 or "\L$type" eq 'tablespace'
6234 or "\L$type" eq 'user'
6235 or (
6236 "\L@$row->[0]" ne 'public'
6237 and (
6238 "\L$type" eq 'database link'
6239 or "\L$type" eq 'synonym'
6240 )
6241 )
6242 )
6243 {
6244 $ddl .= "REM\t\U@$row->[1]\n";
6245 }
6246 # Schema's don't name individual objects
6247 elsif ( "\L$type" eq 'schema' )
6248 {
6249 $ddl .= "REM\t\U@$row->[0]\n";
6250 }
6251 # The rest do.
6252 else
6253 {
6254 $ddl .= "REM\t\U@$row->[0].@$row->[1]\n";
6255 }
6256 }
6257  
6258 return $ddl .= "\n";
6259 };
6260  
6261 # sub _get_oracle_release
6262 #
6263 # Determines Oracle Release number
6264 #
6265 sub _get_oracle_release
6266 {
6267 $sth = $dbh->prepare(
6268 "
6269 SELECT
6270 version
6271 FROM
6272 product_component_version
6273 WHERE
6274 product LIKE 'Oracle%'
6275 ");
6276  
6277 $sth->execute;
6278 my $version = $sth->fetchrow_array;
6279  
6280 (
6281 $oracle_release,
6282 $oracle_major,
6283 $oracle_minor
6284 ) = $version =~ /((\d+)\.(\d+)\S+)/;
6285  
6286 if ( $attr{ heading } )
6287 {
6288 $sth = $dbh->prepare(
6289 "
6290 SELECT
6291 LOWER(name)
6292 FROM
6293 v\$database
6294 ");
6295  
6296 $sth->execute;
6297 ( $instance ) = $sth->fetchrow_array;
6298  
6299 $host = `hostname`;
6300 chomp( $host );
6301 }
6302 }
6303  
6304 # sub _granted_privs
6305 #
6306 # Returns DDL to create GRANT statements to the named grantee
6307 # in the form of:
6308 #
6309 # [GRANT <role > TO <name> {WITH ADMIN OPTION]]
6310 # [GRANT <system privilege> TO <name> {WITH ADMIN OPTION]]
6311 #
6312 sub _granted_privs
6313 {
6314 my ( $name ) = @_;
6315  
6316 my $sql;
6317  
6318 # Add role privileges
6319 my $stmt =
6320 "
6321 SELECT
6322 granted_role
6323 , DECODE(
6324 admin_option
6325 ,'YES','WITH ADMIN OPTION '
6326 ,null
6327 ) AS admin_option
6328 FROM
6329 dba_role_privs
6330 WHERE
6331 grantee = UPPER( ? )
6332 ORDER
6333 BY
6334 granted_role
6335 ";
6336  
6337 $sth = $dbh->prepare( $stmt );
6338 $sth->execute( $name );
6339 my $aref = $sth->fetchall_arrayref;
6340  
6341 foreach my $row ( @$aref )
6342 {
6343 $sql .= "PROMPT " .
6344 "GRANT \L@$row->[0] \UTO \L$name \U@$row->[1] \n\n" .
6345 "GRANT \L@$row->[0] \UTO \L$name \U@$row->[1];\n\n";
6346 }
6347  
6348 # Add system privileges
6349 $stmt =
6350 "
6351 SELECT
6352 privilege
6353 , DECODE(
6354 admin_option
6355 ,'YES','WITH ADMIN OPTION '
6356 ,null
6357 ) AS admin_option
6358 FROM
6359 dba_sys_privs
6360 WHERE
6361 grantee = UPPER( ? )
6362 ORDER
6363 BY
6364 privilege
6365 ";
6366  
6367 $sth = $dbh->prepare( $stmt );
6368 $sth->execute( $name );
6369 $aref = $sth->fetchall_arrayref;
6370  
6371 foreach my $row ( @$aref )
6372 {
6373 $sql .= "PROMPT " .
6374 "GRANT \L@$row->[0] \UTO \L$name \U@$row->[1] \n\n" .
6375 "GRANT \L@$row->[0] \UTO \L$name \U@$row->[1];\n\n";
6376 }
6377  
6378 return $sql;
6379 }
6380  
6381 #sub _index_columns
6382 #
6383 # Returns a formatted string containing the index columns.
6384 # Starting with Oracle8i, columns may be DESCending.
6385 #
6386 sub _index_columns
6387 {
6388 my ( $indent, $owner, $name, $view, ) = @_;
6389  
6390 my $stmt;
6391 if (
6392 $oracle_major > 8
6393 or ( $oracle_major == 8 and $oracle_minor > 0 )
6394 )
6395 {
6396 $stmt =
6397 "
6398 SELECT
6399 LOWER(column_name)
6400 , descend
6401 FROM
6402 ${view}_ind_columns
6403 WHERE
6404 index_name = UPPER( ? )
6405 ";
6406 }
6407 else
6408 {
6409 $stmt =
6410 "
6411 SELECT
6412 LOWER(column_name)
6413 , 'ASC'
6414 FROM
6415 ${view}_ind_columns
6416 WHERE
6417 index_name = UPPER( ? )
6418 ";
6419 }
6420  
6421 if ( $view eq 'DBA' )
6422 {
6423 $stmt .=
6424 "
6425 AND index_owner = UPPER('$owner')
6426 ";
6427 }
6428  
6429 $stmt .=
6430 "
6431 ORDER
6432 BY
6433 column_position
6434 ";
6435  
6436 $sth = $dbh->prepare( $stmt );
6437 $sth->execute( $name );
6438 my $aref = $sth->fetchall_arrayref;
6439  
6440 my @cols;
6441 foreach my $row ( @$aref )
6442 {
6443 my ( $column, $direction ) = @$row;
6444  
6445 if ( $column =~ /^sys_nc\d+\$$/ )
6446 {
6447 $stmt =
6448 "
6449 SELECT
6450 c.default\$
6451 FROM
6452 sys.col\$ c -- User needs SELECT on this
6453 , ${view}_indexes i
6454 , ${view}_objects o
6455 WHERE
6456 i.index_name = UPPER( ? )
6457 AND o.object_name = i.table_name
6458 AND c.obj# = o.object_id
6459 AND c.name = UPPER( ? )
6460 ";
6461  
6462 if ( $view eq 'DBA' )
6463 {
6464 $stmt .=
6465 "
6466 AND i.owner = UPPER('$owner')
6467 AND o.owner = i.table_owner
6468 ";
6469 }
6470  
6471 $dbh->{ LongReadLen } = 1024;
6472 $dbh->{ LongTruncOk } = 1;
6473  
6474 $sth = $dbh->prepare( $stmt );
6475 $sth->execute( $name, $column );
6476 my ( $real_column ) = $sth->fetchrow_array;
6477 # Get rid of double quotes
6478 $real_column =~ s|\"||g;
6479  
6480 if ( $direction eq 'DESC' )
6481 {
6482 push @cols, "\L$real_column" .
6483 ' ' .
6484 ' ' x ( 30 - length( $real_column ) ) .
6485 "DESC";
6486 }
6487 else # Must be a function-based index
6488 {
6489 push @cols, $real_column;
6490 }
6491 }
6492 else
6493 {
6494 push @cols, $column;
6495 }
6496 }
6497  
6498 return "${indent}(\n$indent " .
6499 join ( "\n$indent , ", @cols ) .
6500 "\n${indent})\n";
6501 }
6502  
6503 # sub _initial_next
6504 #
6505 # Given the number of blocks in a object, returns the smallest
6506 # INITIAL/NEXT values appropriate for an object of this size.
6507 #
6508 sub _initial_next
6509 {
6510 my $blocks = shift;
6511  
6512 # Turn warnings off
6513 $^W = 0;
6514  
6515 my $i = 0;
6516 my $initial;
6517 my $next;
6518  
6519 until ( $initial )
6520 {
6521 $initial = ( $size_arr[$i][0] eq "UNLIMITED" ) ? $size_arr[$i][1] :
6522 ( $size_arr[$i][0] > $blocks ) ? $size_arr[$i][1] :
6523 undef;
6524  
6525 $next = ( $size_arr[$i][0] eq "UNLIMITED" ) ? $size_arr[$i][1] :
6526 ( $size_arr[$i][0] > $blocks ) ? $size_arr[$i][1] :
6527 undef;
6528 $i++;
6529 }
6530 return $initial, $next;
6531 }
6532  
6533 #sub _key_columns
6534 #
6535 # Returns a formatted string containing the partitioning key columns.
6536 # Called from _partition_key_columns and _subpartition_key_columns,
6537 # which merely control which key columns table to query.
6538 #
6539 sub _key_columns
6540 {
6541 my ( $owner, $name, $object_type, $view, $table ) = @_;
6542  
6543 my $stmt =
6544 "
6545 SELECT
6546 LOWER(column_name)
6547 FROM
6548 ${view}_${table}_key_columns
6549 WHERE
6550 name = UPPER( ? )
6551 AND object_type LIKE UPPER('$object_type%')
6552 ";
6553  
6554 if ( $view eq 'DBA' )
6555 {
6556 $stmt .=
6557 "
6558 AND owner = UPPER('$owner')
6559 ";
6560 }
6561  
6562 $stmt .=
6563 "
6564 ORDER
6565 BY
6566 column_position
6567 ";
6568  
6569 $sth = $dbh->prepare( $stmt );
6570 $sth->execute( $name );
6571 my $aref = $sth->fetchall_arrayref;
6572  
6573 my @cols;
6574 foreach my $row ( @$aref )
6575 {
6576 push @cols, $row->[0];
6577 }
6578  
6579 return join ( "\n , ", @cols );
6580 }
6581  
6582 # sub _object_privs
6583 #
6584 # Returns DDL to create GRANT statements in the form of:
6585 #
6586 # GRANT <privilege> ON <object> TO <name> [WITH GRANT OPTION]
6587 #
6588 # and/or
6589 #
6590 # GRANT <privilege>
6591 # (
6592 # column_name
6593 # , column_name
6594 # )
6595 # ON <object> TO <name> [WITH GRANT OPTION]
6596 #
6597 sub _object_privs
6598 {
6599 my ( $schema, $owner, $name, $view ) = @_;
6600  
6601 my $sql;
6602  
6603 my $stmt =
6604 "
6605 SELECT
6606 grantee
6607 , privilege
6608 , DECODE(
6609 grantable
6610 ,'YES','WITH GRANT OPTION '
6611 ,null
6612 ) AS grantable
6613 FROM
6614 ${view}_tab_privs
6615 WHERE
6616 grantor = UPPER( ? )
6617 AND table_name = UPPER( ? )
6618 ";
6619  
6620 if ( $view eq 'DBA' )
6621 {
6622 $stmt .=
6623 "
6624 AND owner = UPPER('$owner')
6625 ";
6626 }
6627  
6628 $stmt .=
6629 "
6630 ORDER
6631 BY
6632 grantee
6633 , privilege
6634 ";
6635  
6636 $sth = $dbh->prepare( $stmt );
6637 $sth->execute( $owner, $name );
6638 my $aref = $sth->fetchall_arrayref;
6639  
6640 foreach my $row ( @$aref )
6641 {
6642 my (
6643 $grantee,
6644 $privilege,
6645 $grantable,,
6646 ) = @$row;
6647  
6648 $sql .= "PROMPT " .
6649 "GRANT \L$privilege \UON \L$schema$name \UTO \L$grantee " .
6650 "\U$grantable \n\n" .
6651 "GRANT \L$privilege \UON \L$schema$name \UTO \L$grantee " .
6652 "\U$grantable;\n\n";
6653 }
6654  
6655 $stmt =
6656 "
6657 SELECT DISTINCT
6658 grantee
6659 , privilege
6660 , DECODE(
6661 grantable
6662 ,'YES','WITH GRANT OPTION '
6663 ,null
6664 ) AS grantable
6665 FROM
6666 ${view}_col_privs
6667 WHERE
6668 grantor = UPPER( ? )
6669 AND table_name = UPPER( ? )
6670 ";
6671  
6672 if ( $view eq 'DBA' )
6673 {
6674 $stmt .=
6675 "
6676 AND owner = UPPER('$owner')
6677 ";
6678 }
6679  
6680 $stmt .=
6681 "
6682 ORDER
6683 BY
6684 grantee
6685 , privilege
6686 ";
6687  
6688 $sth = $dbh->prepare( $stmt );
6689 $sth->execute( $owner, $name );
6690 $aref = $sth->fetchall_arrayref;
6691  
6692 foreach my $row ( @$aref )
6693 {
6694 my (
6695 $grantee,
6696 $privilege,
6697 $grantable,,
6698 ) = @$row;
6699  
6700 $sql .= "PROMPT " .
6701 "GRANT \L$privilege \UON \L$schema$name \UTO \L$grantee " .
6702 "\U$grantable\n\n" .
6703 "GRANT \L$privilege \n(\n ";
6704  
6705 $stmt =
6706 "
6707 SELECT
6708 LOWER(column_name)
6709 FROM
6710 ${view}_col_privs
6711 WHERE
6712 grantor = UPPER( ? )
6713 AND privilege = UPPER( ? )
6714 AND table_name = UPPER( ? )
6715 ";
6716  
6717 if ( $view eq 'DBA' )
6718 {
6719 $stmt .=
6720 "
6721 AND owner = UPPER('$owner')
6722 ";
6723 }
6724  
6725 $stmt .=
6726 "
6727 ORDER
6728 BY
6729 grantee
6730 , privilege
6731 ";
6732  
6733 $sth = $dbh->prepare( $stmt );
6734 $sth->execute( $owner, $privilege, $name );
6735 my $aref = $sth->fetchall_arrayref;
6736  
6737 my @cols;
6738 foreach my $row ( @$aref )
6739 {
6740 push @cols, $row->[0];
6741 }
6742  
6743 $sql .= join ( "\n , ", @cols );
6744  
6745 $sql .= "\n)\n\UON \L$schema$name \UTO \L$grantee \U$grantable;\n\n";
6746 }
6747  
6748 return $sql;
6749 }
6750  
6751 # sub _partition_key_columns
6752 #
6753 # Returns a formatted string containing the partitioning key columns
6754 #
6755 sub _partition_key_columns
6756 {
6757 return _key_columns( @_, 'PART' );
6758 }
6759  
6760 # sub _print_space
6761 #
6762 # Returns a formatted string containing the space analysis report.
6763 #
6764 sub _print_space
6765 {
6766 my ( $owner, $name, $type, $partition ) = @_;
6767  
6768 my (
6769 $total_blocks,
6770 $total_bytes,
6771 $unused_blocks,
6772 $unused_bytes,
6773 $last_file_id,
6774 $last_block_id,
6775 $last_block,
6776 $free_blocks,
6777 );
6778  
6779 my $max_length = 20;
6780 my $freelist_group_id = 0;
6781  
6782 if ( $partition )
6783 {
6784 $sth = $dbh->prepare(
6785 "
6786 BEGIN
6787 dbms_space.unused_space (
6788 ?,?,?,?,?,?,?,?,?,?,?
6789 ) ;
6790 END ;
6791 "
6792 );
6793 $sth->bind_param( 1, "\U$owner" );
6794 $sth->bind_param( 2, "\U$name" );
6795 $sth->bind_param( 3, "\U$type" );
6796 $sth->bind_param_inout( 4 , \$total_blocks , $max_length );
6797 $sth->bind_param_inout( 5 , \$total_bytes , $max_length );
6798 $sth->bind_param_inout( 6 , \$unused_blocks, $max_length );
6799 $sth->bind_param_inout( 7 , \$unused_bytes , $max_length );
6800 $sth->bind_param_inout( 8 , \$last_file_id , $max_length );
6801 $sth->bind_param_inout( 9 , \$last_block_id, $max_length );
6802 $sth->bind_param_inout( 10, \$last_block , $max_length );
6803 $sth->bind_param( 11, $partition );
6804 $sth->execute;
6805  
6806 $sth = $dbh->prepare(
6807 "
6808 BEGIN
6809 dbms_space.free_blocks(
6810 ?,?,?,?,?,?,?
6811 ) ;
6812 END ;
6813 "
6814 );
6815 $sth->bind_param( 1, "\U$owner" );
6816 $sth->bind_param( 2, "\U$name" );
6817 $sth->bind_param( 3, "\U$type" );
6818 $sth->bind_param( 4, $freelist_group_id );
6819 $sth->bind_param_inout( 5 , \$free_blocks, $max_length );
6820 $sth->bind_param( 6, '' );
6821 $sth->bind_param( 7, $partition );
6822 $sth->execute;
6823 }
6824 else
6825 {
6826 $sth = $dbh->prepare(
6827 "
6828 BEGIN
6829 dbms_space.unused_space (
6830 ?,?,?,?,?,?,?,?,?,?
6831 ) ;
6832 END ;
6833 "
6834 );
6835 $sth->bind_param( 1, "\U$owner" );
6836 $sth->bind_param( 2, "\U$name" );
6837 $sth->bind_param( 3, "\U$type" );
6838 $sth->bind_param_inout( 4 , \$total_blocks , $max_length );
6839 $sth->bind_param_inout( 5 , \$total_bytes , $max_length );
6840 $sth->bind_param_inout( 6 , \$unused_blocks, $max_length );
6841 $sth->bind_param_inout( 7 , \$unused_bytes , $max_length );
6842 $sth->bind_param_inout( 8 , \$last_file_id , $max_length );
6843 $sth->bind_param_inout( 9 , \$last_block_id, $max_length );
6844 $sth->bind_param_inout( 10, \$last_block , $max_length );
6845 $sth->execute;
6846  
6847 $sth = $dbh->prepare(
6848 "
6849 BEGIN
6850 dbms_space.free_blocks(
6851 ?,?,?,?,?
6852 ) ;
6853 END ;
6854 "
6855 );
6856 $sth->bind_param( 1, "\U$owner" );
6857 $sth->bind_param( 2, "\U$name" );
6858 $sth->bind_param( 3, "\U$type" );
6859 $sth->bind_param( 4, $freelist_group_id );
6860 $sth->bind_param_inout( 5 , \$free_blocks, $max_length );
6861 $sth->execute;
6862 }
6863  
6864 my $text;
6865  
6866 if ( $partition )
6867 {
6868 $text = "Partition $partition BYTES";
6869 }
6870 else
6871 {
6872 $text = " BYTES";
6873 }
6874  
6875 $text .= " BLOCKS\n" .
6876 " ============" .
6877 " =========\n" .
6878 sprintf(
6879 "Used BELOW the high water mark %12d %9d\n",
6880 $total_bytes - $unused_bytes
6881 - $free_blocks * $block_size * 1024,
6882 $total_blocks - $unused_blocks - $free_blocks
6883 ) .
6884 sprintf(
6885 "Free ABOVE the high water mark %12d %9d\n",
6886 $unused_bytes, $unused_blocks
6887 ) .
6888 sprintf(
6889 "Free BELOW the high water mark %12d %9d\n",
6890 $free_blocks * $block_size * 1024, $free_blocks
6891 ) .
6892 " ------------" .
6893 " ---------\n" .
6894 sprintf(
6895 " TOTAL in segment %12d %9d\n\n",
6896 $total_bytes, $total_blocks
6897 ) .
6898 " FILE_ID BLOCK_ID" .
6899 " BLOCK_NBR\n" .
6900 " ======= ========" .
6901 " =========\n" .
6902 sprintf(
6903 "Last extent having data %7d %8d %9d\n\n",
6904 $last_file_id, $last_block_id, $last_block
6905 );
6906  
6907 return (
6908 $text,
6909 $total_blocks,
6910 $total_bytes,
6911 $unused_blocks,
6912 $unused_bytes,
6913 $free_blocks,
6914 );
6915 }
6916  
6917 # sub _range_partitions
6918 #
6919 # Returns the ordered list of index range partitions with segment attributes
6920 #
6921 sub _range_partitions
6922 {
6923 my ( $owner, $index, $view, $subpartitioning_type, $caller ) = @_;
6924  
6925 my $sql .= "(\n";
6926 my $stmt;
6927  
6928 if ( $oracle_major == 8 and $oracle_minor == 0 )
6929 {
6930 $stmt =
6931 "
6932 SELECT
6933 partition_name
6934 , high_value
6935 , 'N/A' AS cache
6936 , 'N/A' AS pct_used
6937 , pct_free
6938 , ini_trans
6939 , max_trans
6940 -- Storage Clause
6941 , initial_extent
6942 , next_extent
6943 , min_extent
6944 , DECODE(
6945 max_extent
6946 ,2147483645,'unlimited'
6947 , max_extent
6948 ) AS max_extents
6949 , pct_increase
6950 , NVL(freelists,1)
6951 , NVL(freelist_groups,1)
6952 , 'N/A' AS buffer_pool
6953 , DECODE(
6954 logging
6955 ,'NO','NOLOGGING'
6956 , 'LOGGING'
6957 ) AS logging
6958 , tablespace_name
6959 , leaf_blocks AS blocks
6960 FROM
6961 ${view}_ind_partitions
6962 WHERE
6963 index_name = UPPER( ? )
6964 ";
6965 }
6966 else # We're Oracle8i or newer
6967 {
6968 $stmt =
6969 "
6970 SELECT
6971 partition_name
6972 , high_value
6973 , 'N/A' AS cache
6974 , 'N/A' AS pct_used
6975 , pct_free
6976 , ini_trans
6977 , max_trans
6978 -- Storage Clause
6979 , initial_extent
6980 , next_extent
6981 , min_extent
6982 , DECODE(
6983 max_extent
6984 ,2147483645,'unlimited'
6985 , max_extent
6986 ) AS max_extents
6987 , pct_increase
6988 , NVL(freelists,1)
6989 , NVL(freelist_groups,1)
6990 , LOWER(buffer_pool)
6991 , DECODE(
6992 logging
6993 ,'NO','NOLOGGING'
6994 , 'LOGGING'
6995 ) AS logging
6996 , tablespace_name
6997 , leaf_blocks AS blocks
6998 FROM
6999 ${view}_ind_partitions
7000 WHERE
7001 index_name = UPPER( ? )
7002 ";
7003 }
7004  
7005 if ( $view eq 'DBA' )
7006 {
7007 $stmt .=
7008 "
7009 AND index_owner = UPPER('$owner')
7010 ";
7011 }
7012  
7013 $stmt .=
7014 "
7015 ORDER
7016 BY
7017 partition_name
7018 ";
7019  
7020 $dbh->{ LongReadLen } = 8192; # Allows HIGH_VALUE length of 8K
7021 $dbh->{ LongTruncOk } = 1;
7022  
7023 $sth = $dbh->prepare( $stmt );
7024 $sth->execute( $index );
7025 my $aref = $sth->fetchall_arrayref;
7026  
7027 my $comma = ' ';
7028  
7029 foreach my $row ( @$aref )
7030 {
7031 my $partition = shift @$row;
7032 my $high_value = shift @$row;
7033  
7034 $sql .= "${comma}PARTITION \L$partition";
7035  
7036 if ($caller ne 'LOCAL' )
7037 {
7038 $sql .= " VALUES LESS THAN\n" .
7039 " (\n" .
7040 " $high_value\n" .
7041 " )\n";
7042 }
7043 else
7044 {
7045 $sql .= "\n";
7046 }
7047  
7048 unshift @$row, ( ' ', 'INDEX' );
7049  
7050 $sql .= _segment_attributes( $row );
7051  
7052 if ( $subpartitioning_type eq 'HASH' )
7053 {
7054 $stmt =
7055 "
7056 SELECT
7057 subpartition_name
7058 , tablespace_name
7059 FROM
7060 ${view}_ind_subpartitions
7061 WHERE
7062 index_name = UPPER( ? )
7063 AND partition_name = '$partition'
7064 ";
7065  
7066 if ( $view eq 'DBA' )
7067 {
7068 $stmt .=
7069 "
7070 AND index_owner = UPPER('$owner')
7071 ";
7072 }
7073  
7074 $stmt .=
7075 "
7076 ORDER
7077 BY
7078 subpartition_name
7079 ";
7080  
7081 $sth = $dbh->prepare( $stmt );
7082 $sth->execute( $index );
7083 my $aref = $sth->fetchall_arrayref;
7084  
7085 $sql .= " (\n ";
7086  
7087 my @cols;
7088 foreach my $row ( @$aref )
7089 {
7090 push @cols, "SUBPARTITION \L$row->[0] \UTABLESPACE \L$row->[1]";
7091 }
7092 $sql .= join ( "\n , ", @cols );
7093  
7094 $sql .= "\n )\n";
7095 }
7096  
7097 $comma = ' , ';
7098 }
7099 $sql .= ");\n\n";
7100  
7101 return $sql;
7102 }
7103  
7104 # sub _resize_index
7105 #
7106 # Returns DDL to rebuild the named index or its partition(s) in the form of:
7107 #
7108 # ALTER INDEX [schema.]<name> REBUILD
7109 # [PARTITION <partition>]
7110 # STORAGE
7111 # (
7112 # INITIAL <bytes>
7113 # NEXT <bytes>
7114 # )
7115 #
7116 sub _resize_index
7117 {
7118 my ( $schema, $owner, $name, $view ) = @_;
7119  
7120 my $cnt;
7121 my $sql;
7122 ( $name, my $partition ) = split /:/, $name;
7123  
7124 my $stmt =
7125 "
7126 SELECT
7127 count(*) AS cnt
7128 FROM
7129 ${view}_indexes
7130 WHERE
7131 index_name = UPPER( ? )
7132 ";
7133  
7134 if ( $view eq 'DBA' )
7135 {
7136 $stmt .=
7137 "
7138 AND owner = UPPER('$owner')
7139 ";
7140 }
7141  
7142 $sth = $dbh->prepare( $stmt );
7143 $sth->execute( $name );
7144 $cnt = $sth->fetchrow_array;
7145 die "Index \U$name \Ldoes not exist.\n\n" unless $cnt;
7146  
7147 if ( $partition ) # User wants one partition resized
7148 {
7149 $stmt =
7150 "
7151 SELECT
7152 SUBSTR(segment_type,7) -- PARTITION or SUBPARTITION
7153 FROM
7154 ${view}_segments s
7155 WHERE
7156 s.segment_name = UPPER( ? )
7157 AND s.partition_name = UPPER( ? )
7158 ";
7159 if ( $view eq 'DBA' )
7160 {
7161 $stmt .=
7162 "
7163 AND s.owner = UPPER('$owner')
7164 ";
7165 }
7166  
7167 $sth = $dbh->prepare( $stmt );
7168 $sth->execute( $name, $partition );
7169 my ( $seq_type, $partitioning_type ) = $sth->fetchrow_array;
7170 die "Partition \U$partition \Lof \UI\Lndex \U$name \Ldoes not exist,\n",
7171 " OR it is the parent of Hash subpartition(s)\n",
7172 " (i.e., it is not a segment and has no size).\n\n"
7173 unless $seq_type;
7174  
7175 $sql .= _resize_index_partition(
7176 $schema,
7177 $owner,
7178 $name,
7179 $partition,
7180 $seq_type,
7181 $view
7182 );
7183  
7184 return $sql;
7185 }
7186  
7187 # Find out if the object is partitioned
7188  
7189 $stmt =
7190 "
7191 SELECT
7192 partitioned
7193 FROM
7194 ${view}_indexes
7195 WHERE
7196 index_name = UPPER( ? )
7197 ";
7198  
7199 if ( $view eq 'DBA' )
7200 {
7201 $stmt .=
7202 "
7203 AND owner = UPPER('$owner')
7204 ";
7205 }
7206  
7207 $sth = $dbh->prepare( $stmt );
7208 $sth->execute( $name );
7209 my $partitioned = $sth->fetchrow_array;
7210  
7211 if ( $partitioned eq 'NO' )
7212 {
7213 $stmt =
7214 "
7215 SELECT
7216 s.blocks
7217 , s.initial_extent
7218 , s.next_extent
7219 FROM
7220 ${view}_segments s
7221 WHERE
7222 s.segment_name = UPPER( ? )
7223 AND s.segment_type = 'INDEX'
7224 ";
7225 if ( $view eq 'DBA' )
7226 {
7227 $stmt .=
7228 "
7229 AND s.owner = UPPER('$owner')
7230 ";
7231 }
7232 $sth = $dbh->prepare( $stmt );
7233 $sth->execute( $name );
7234 my ( $blocks, $initial, $next ) = $sth->fetchrow_array;
7235  
7236 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
7237  
7238 $sql .= "PROMPT " .
7239 "ALTER INDEX \L$schema$name \UREBUILD\n\n" .
7240 "ALTER INDEX \L$schema$name \UREBUILD\n" .
7241 "STORAGE\n" .
7242 "(\n" .
7243 " INITIAL $initial\n" .
7244 " NEXT $next\n" .
7245 ") ;\n\n";
7246  
7247 return $sql;
7248 }
7249 else
7250 {
7251 # It's partitioned -- get list of partitions
7252 # and call _resize_index_partition for each one
7253 $stmt =
7254 "
7255 SELECT
7256 partition_name
7257 , SUBSTR(segment_type,7) -- PARTITION or SUBPARTITION
7258 FROM
7259 ${view}_segments
7260 WHERE
7261 segment_name = UPPER( ? )
7262 ";
7263 if ( $view eq 'DBA' )
7264 {
7265 $stmt .=
7266 "
7267 AND owner = UPPER('$owner')
7268 ";
7269 }
7270 $stmt .= "
7271 ORDER
7272 BY
7273 partition_name
7274 ";
7275  
7276 $sth = $dbh->prepare( $stmt );
7277 $sth->execute( $name );
7278 my $aref = $sth->fetchall_arrayref;
7279  
7280 foreach my $row ( @$aref )
7281 {
7282 my ( $partition, $seg_type ) = @$row;
7283  
7284 $sql .= _resize_index_partition(
7285 $schema,
7286 $owner,
7287 $name,
7288 $partition,
7289 $seg_type,
7290 $view
7291 );
7292 }
7293  
7294 return $sql;
7295 }
7296 }
7297  
7298 # sub _resize_index_partition
7299 #
7300 # Returns DDL to rebuild one partition of the named object in the form of:
7301 #
7302 # ALTER INDEX [schema.]<name> REBUILD [SUB]PARTITION <partition>
7303 # STORAGE
7304 # (
7305 # INITIAL <bytes>
7306 # NEXT <bytes>
7307 # )
7308 #
7309 sub _resize_index_partition
7310 {
7311 my(
7312 $schema, $owner, $name, $partition, $seg_type, $view ) = @_;
7313  
7314 my $sql;
7315 my $stmt =
7316 "
7317 SELECT
7318 s.blocks
7319 , s.initial_extent
7320 , s.next_extent
7321 , p.partitioning_type
7322 FROM
7323 ${view}_segments s
7324 , ${view}_part_indexes p
7325 WHERE
7326 s.segment_name = UPPER( ? )
7327 AND s.partition_name = UPPER( ? )
7328 AND p.index_name = UPPER( ? )
7329 ";
7330 if ( $view eq 'DBA' )
7331 {
7332 $stmt .=
7333 "
7334 AND s.owner = UPPER('$owner')
7335 AND p.owner = UPPER('$owner')
7336 ";
7337 }
7338  
7339 $sth = $dbh->prepare( $stmt );
7340 $sth->execute( $name, $partition, $name );
7341 my ( $blocks, $initial, $next, $partitioning_type ) = $sth->fetchrow_array;
7342  
7343 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
7344  
7345 $sql .= "PROMPT " .
7346 "ALTER INDEX \L$schema$name \UREBUILD $seg_type \L$partition\n\n" .
7347 "ALTER INDEX \L$schema$name \UREBUILD $seg_type \L$partition ";
7348  
7349 # Cannot specify storage parameters for a HASH [SUB]PARTITION
7350 if ( $seg_type eq 'PARTITION' and $partitioning_type eq 'RANGE' )
7351 {
7352 $sql .= "\nSTORAGE\n" .
7353 "(\n" .
7354 " INITIAL $initial\n" .
7355 " NEXT $next\n" .
7356 ") ";
7357 }
7358  
7359 $sql .= ";\n\n";
7360  
7361 return $sql;
7362 }
7363  
7364 # sub _resize_table
7365 #
7366 # Returns DDL to rebuild the named table or its partition(s) in the form of:
7367 #
7368 # ALTER TABLE [schema.]<name> MOVE [[SUB]PARTITION <partition>]
7369 # STORAGE
7370 # (
7371 # INITIAL <bytes>
7372 # NEXT <bytes>
7373 # )
7374 #
7375 sub _resize_table
7376 {
7377 my ( $schema, $owner, $name, $view ) = @_;
7378  
7379 my $cnt;
7380 my $sql;
7381 ( $name, my $partition ) = split /:/, $name;
7382  
7383 my $stmt =
7384 "
7385 SELECT
7386 count(*) AS cnt
7387 FROM
7388 ${view}_tables
7389 WHERE
7390 table_name = UPPER( ? )
7391 ";
7392  
7393 if ( $view eq 'DBA' )
7394 {
7395 $stmt .=
7396 "
7397 AND owner = UPPER('$owner')
7398 ";
7399 }
7400  
7401 $sth = $dbh->prepare( $stmt );
7402 $sth->execute( $name );
7403 $cnt = $sth->fetchrow_array;
7404 die "Table \U$name \Ldoes not exist.\n\n" unless $cnt;
7405  
7406 if ( $partition ) # User wants one partition resized
7407 {
7408 $stmt =
7409 "
7410 SELECT
7411 SUBSTR(segment_type,7) -- PARTITION or SUBPARTITION
7412 FROM
7413 ${view}_segments
7414 WHERE
7415 segment_name = UPPER( ? )
7416 AND partition_name = UPPER( ? )
7417 ";
7418 if ( $view eq 'DBA' )
7419 {
7420 $stmt .=
7421 "
7422 AND owner = UPPER('$owner')
7423 ";
7424 }
7425  
7426 $sth = $dbh->prepare( $stmt );
7427 $sth->execute( $name, $ partition );
7428 my $type = $sth->fetchrow_array;
7429 die "Partition \U$partition \Lof \UT\Lable \U$name \Ldoes not exist,\n",
7430 " OR it is the parent of Hash subpartition(s)\n",
7431 " (i.e., it is not a segment and has no size).\n\n"
7432 unless $type;
7433  
7434 $sql .= _resize_table_partition(
7435 $schema,
7436 $owner,
7437 $name,
7438 $partition,
7439 $type,
7440 $view
7441 );
7442  
7443 # Rebuild this partition on all LOCAL indexes
7444 if ( $view eq 'DBA' )
7445 {
7446 $stmt =
7447 "
7448 SELECT
7449 owner
7450 , index_name
7451 FROM
7452 dba_part_indexes
7453 WHERE
7454 table_name = UPPER( ? )
7455 AND owner = UPPER('$owner')
7456 AND locality = 'LOCAL'
7457 ";
7458 }
7459 else # We're a mortal USER
7460 {
7461 $stmt =
7462 "
7463 SELECT
7464 '$owner'
7465 , index_name
7466 FROM
7467 user_part_indexes
7468 WHERE
7469 table_name = UPPER( ? )
7470 AND locality = 'LOCAL'
7471 ";
7472 }
7473
7474 $sth = $dbh->prepare( $stmt );
7475 $sth->execute( $name );
7476 my $aref = $sth->fetchall_arrayref;
7477  
7478 foreach my $row ( @$aref )
7479 {
7480 my ( $owner, $index ) = @$row;
7481 my $schema = _set_schema( $owner );
7482  
7483 $sql .= _resize_index(
7484 $schema,
7485 $owner,
7486 "$index:$partition",
7487 $view
7488 );
7489 }
7490 }
7491 else
7492 # Didn't want single partition, so move entire table.
7493 # First, find out if the object is partitioned
7494 {
7495 $stmt =
7496 "
7497 SELECT
7498 partitioned
7499 FROM
7500 ${view}_tables
7501 WHERE
7502 table_name = UPPER( ? )
7503 ";
7504
7505 if ( $view eq 'DBA' )
7506 {
7507 $stmt .=
7508 "
7509 AND owner = UPPER('$owner')
7510 ";
7511
7512 }
7513
7514 $sth = $dbh->prepare( $stmt );
7515 $sth->execute( $name );
7516 my $partitioned = $sth->fetchrow_array;
7517
7518 if ( $partitioned eq 'NO' )
7519 {
7520 $stmt =
7521 "
7522 SELECT
7523 s.blocks - NVL(t.empty_blocks,0)
7524 , s.initial_extent
7525 , s.next_extent
7526 FROM
7527 ${view}_segments s
7528 , ${view}_tables t
7529 WHERE
7530 s.segment_name = UPPER( ? )
7531 AND s.segment_type = 'TABLE'
7532 AND t.table_name = s.segment_name
7533 ";
7534 if ( $view eq 'DBA' )
7535 {
7536 $stmt .=
7537 "
7538 AND s.owner = UPPER('$owner')
7539 AND t.owner = s.owner
7540 ";
7541 }
7542 $sth = $dbh->prepare( $stmt );
7543 $sth->execute( $name );
7544 my ( $blocks, $initial, $next ) = $sth->fetchrow_array;
7545
7546 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
7547
7548 $sql .= "PROMPT " .
7549 "ALTER TABLE \L$schema$name \UMOVE\n\n" .
7550 "ALTER TABLE \L$schema$name \UMOVE\n" .
7551 "STORAGE\n" .
7552 "(\n" .
7553 " INITIAL $initial\n" .
7554 " NEXT $next\n" .
7555 ") ;\n\n";
7556
7557 return $sql;
7558 }
7559 else
7560 {
7561 # It's partitioned -- get list of partitions
7562 # and call _resize_table_partition for each one
7563 $stmt =
7564 "
7565 SELECT
7566 partition_name
7567 , SUBSTR(segment_type,7) -- PARTITION or SUBPARTITION
7568 FROM
7569 ${view}_segments
7570 WHERE
7571 segment_name = UPPER( ? )
7572 ";
7573 if ( $view eq 'DBA' )
7574 {
7575 $stmt .=
7576 "
7577 AND owner = UPPER('$owner')
7578 ";
7579 }
7580 $stmt .=
7581 "
7582 ORDER
7583 BY
7584 partition_name
7585 ";
7586
7587 $sth = $dbh->prepare( $stmt );
7588 $sth->execute( $name );
7589 my $aref = $sth->fetchall_arrayref;
7590
7591 foreach my $row ( @$aref )
7592 {
7593 my ( $partition, $type ) = @$row;
7594
7595 $sql .= _resize_table_partition(
7596 $schema,
7597 $owner,
7598 $name,
7599 $partition,
7600 $type,
7601 $view
7602 );
7603 }
7604 }
7605  
7606 # Rebuild all indexes (partitioned or not)
7607 if ( $view eq 'DBA' )
7608 {
7609 $stmt =
7610 "
7611 SELECT
7612 owner
7613 , index_name
7614 FROM
7615 dba_part_indexes
7616 WHERE
7617 table_name = UPPER( ? )
7618 AND owner = UPPER('$owner')
7619 ";
7620 }
7621 else # We're a mortal USER
7622 {
7623 $stmt =
7624 "
7625 SELECT
7626 '$owner'
7627 , index_name
7628 FROM
7629 user_part_indexes
7630 WHERE
7631 table_name = UPPER( ? )
7632 ";
7633 }
7634
7635 $sth = $dbh->prepare( $stmt );
7636 $sth->execute( $name );
7637 my $aref = $sth->fetchall_arrayref;
7638  
7639 foreach my $row ( @$aref )
7640 {
7641 my ( $owner, $index ) = @$row;
7642 my $schema = _set_schema( $owner );
7643  
7644 $sql .= _resize_index(
7645 $schema,
7646 $owner,
7647 $index,
7648 $view
7649 );
7650 }
7651 }
7652  
7653 return $sql;
7654 }
7655  
7656 # sub _resize_table_partition
7657 #
7658 # Returns DDL to rebuild one partition of the named object in the form of:
7659 #
7660 # ALTER TABLE [schema.]<name> MOVE [SUB]PARTITION <partition>
7661 # STORAGE
7662 # (
7663 # INITIAL <bytes>
7664 # NEXT <bytes>
7665 # )
7666 #
7667 sub _resize_table_partition
7668 {
7669 my( $schema, $owner, $name, $partition, $seg_type, $view ) = @_;
7670  
7671 my $sql;
7672 my $stmt =
7673 "
7674 SELECT
7675 s.blocks - NVL(t.empty_blocks,0)
7676 , s.initial_extent
7677 , s.next_extent
7678 , p.partitioning_type
7679 FROM
7680 ${view}_segments s
7681 , ${view}_tab_${seg_type}s t
7682 , ${view}_part_tables p
7683 WHERE
7684 s.segment_name = UPPER( ? )
7685 AND s.partition_name = UPPER( ? )
7686 AND t.table_name = UPPER( ? )
7687 AND t.partition_name = UPPER( ? )
7688 AND p.table_name = UPPER( ? )
7689 ";
7690 if ( $view eq 'DBA' )
7691 {
7692 $stmt .=
7693 "
7694 AND s.owner = UPPER('$owner')
7695 AND p.owner = UPPER('$owner')
7696 AND t.table_owner = UPPER('$owner')
7697 ";
7698 }
7699  
7700 $sth = $dbh->prepare( $stmt );
7701 $sth->execute( $name, $partition, $name, $partition, $name );
7702 my ( $blocks, $initial, $next, $partitioning_type ) = $sth->fetchrow_array;
7703  
7704 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
7705  
7706 $sql .= "PROMPT " .
7707 "ALTER TABLE \L$schema$name \UMOVE $seg_type \L$partition\n\n" .
7708 "ALTER TABLE \L$schema$name \UMOVE $seg_type \L$partition ";
7709  
7710 # Cannot specify storage parameters for a HASH [SUB]PARTITION
7711 if ( $seg_type eq 'PARTITION' and $partitioning_type eq 'RANGE' )
7712 {
7713 $sql .= "\nSTORAGE\n" .
7714 "(\n" .
7715 " INITIAL $initial\n" .
7716 " NEXT $next\n" .
7717 ") ";
7718 }
7719  
7720 $sql .= ";\n\n";
7721  
7722 return $sql;
7723 }
7724  
7725 # sub _scratch_prompts
7726 #
7727 # Eliminates all PROMPT statements
7728 #
7729 sub _scratch_prompts
7730 {
7731 # Drop all lines beginning with PROMPT
7732 $ddl = ( join "\n",grep !/^PROMPT/,split /\n/,$ddl );
7733  
7734 # This would have left the first line blank, so drop it
7735 $ddl =~ s|\A\n||;
7736  
7737 # Get rid of double blank lines
7738 $ddl =~ s|\n\n+|\n\n|g;
7739 }
7740  
7741 # sub _segments_attributes
7742 #
7743 # Formats the segment attributes portion of CREATE TABLE and CREATE INDEX
7744 # statements
7745 #
7746 sub _segment_attributes
7747 {
7748 my ( $arrayref ) = @_;
7749  
7750 my $sql;
7751 my (
7752 $indent,
7753 # Physical Properties
7754 $organization,
7755 # Segment Attributes
7756 $cache,
7757 $pct_used,
7758 $pct_free,
7759 $ini_trans,
7760 $max_trans,
7761 $initial,
7762 $next,
7763 $min_extents,
7764 $max_extents,
7765 $pct_increase,
7766 $freelists,
7767 $freelist_groups,
7768 $buffer_pool,
7769 $logging,
7770 $tablespace,
7771 $blocks,
7772 ) = @$arrayref;
7773  
7774 unless ( $attr{ 'storage' } )
7775 {
7776 if ( $attr{ 'tblspace' } )
7777 {
7778 $sql .= "${indent}TABLESPACE \L$tablespace\n";
7779 }
7780  
7781 return $sql;
7782 }
7783  
7784 ( $initial, $next ) = _initial_next( $blocks ) if $attr{ 'resize' };
7785  
7786 if ( $organization eq 'HEAP' )
7787 {
7788 $sql = "${indent}$cache\n" unless $cache eq 'N/A';
7789 $sql .="${indent}PCTUSED $pct_used\n" unless $isasnapindx;
7790 }
7791  
7792 $sql .= "${indent}PCTFREE $pct_free\n" unless $isasnapindx;
7793
7794 $sql .= "${indent}INITRANS $ini_trans\n" unless $isasnaptabl;
7795  
7796 $sql .= "${indent}MAXTRANS $max_trans\n" .
7797 "${indent}STORAGE\n" .
7798 "${indent}(\n" .
7799 "${indent} INITIAL $initial\n" .
7800 "${indent} NEXT $next\n" .
7801 "${indent} MINEXTENTS $min_extents\n" .
7802 "${indent} MAXEXTENTS $max_extents\n" .
7803 "${indent} PCTINCREASE $pct_increase\n";
7804  
7805 if (
7806 $oracle_major < 8
7807 or ( $oracle_major == 8 and $oracle_minor == 0 )
7808 )
7809 {
7810 unless (
7811 $isasnaptabl
7812 or $isasnapindx
7813 )
7814 {
7815 $sql .= "${indent} FREELISTS $freelists\n" .
7816 "${indent} FREELIST GROUPS $freelist_groups\n";
7817 }
7818 }
7819 else
7820 {
7821 $sql .= "${indent} FREELISTS $freelists\n" .
7822 "${indent} FREELIST GROUPS $freelist_groups\n";
7823 }
7824  
7825  
7826 if (
7827 $oracle_major > 8
7828 or ( $oracle_major == 8 and $oracle_minor > 0 )
7829 )
7830 {
7831 $sql .= "${indent} BUFFER_POOL $buffer_pool\n";
7832 }
7833  
7834 $sql .= "${indent})\n";
7835  
7836 $sql .= "${indent}$logging\n" if $oracle_major > 7;
7837  
7838 if ( $attr{ 'tblspace' } )
7839 {
7840 $sql .= "${indent}TABLESPACE \L$tablespace\n";
7841 }
7842  
7843 return $sql;
7844 }
7845  
7846 sub _set_schema
7847 {
7848 my $owner = shift;
7849  
7850 my $schema = ( "\U$owner" eq 'PUBLIC' ) ? 'PUBLIC' :
7851 ( $attr{ schema } == 1 ) ? $owner . '.' :
7852 ( $attr{ schema } ) ? $attr{ schema } . '.' : '';
7853  
7854 return $schema;
7855 }
7856  
7857 # sub _set_sizing
7858 #
7859 # If %attr has an entry for "resize" == l, generates an arbitrary sizing
7860 # algorithm wherein the database block size is used to create an array such
7861 # that each object will have no more than 8 extents. The INITIAL and NEXT
7862 # sizes of Tables and Indexes are set to the calculated value.
7863 #
7864 # If %attr DOES contain an entry for "resize", it is parsed and stored in the
7865 # array called @size_arr.
7866 #
7867 sub _set_sizing
7868 {
7869 $sth = $dbh->prepare(
7870 "
7871 SELECT
7872 block_size
7873 FROM
7874 (
7875 SELECT
7876 bytes / blocks AS block_size
7877 FROM
7878 user_segments
7879 WHERE
7880 bytes IS NOT NULL
7881 AND blocks IS NOT NULL
7882 AND rownum < 2
7883 UNION
7884 SELECT
7885 bytes / blocks AS block_size
7886 FROM
7887 user_free_space
7888 WHERE
7889 bytes IS NOT NULL
7890 AND blocks IS NOT NULL
7891 AND rownum < 2
7892 )
7893 WHERE
7894 rownum < 2
7895 ");
7896  
7897 $sth->execute;
7898 $block_size = $sth->fetchrow_array / 1024;
7899  
7900 if ( $attr{ 'resize' } == 1 )
7901 {
7902 # Create default array
7903 for my $i ( 0 .. 4 )
7904 {
7905 my $limit = ( 4 * ( 10 ** ( $i + 1 ) ) + 1 );
7906 my $initial = my $next = ( 5 * $block_size ) * ( 10 ** $i ) . "K";
7907 push @size_arr, [$limit, $initial, $next];
7908 }
7909 # Force upper limit bound
7910 $size_arr[$#size_arr][0] = 'UNLIMITED';
7911 }
7912 elsif ( $attr{ 'resize' } )
7913 {
7914 # parse user supplied string into @size_arr
7915 my $remainder = $attr{ 'resize' };
7916 while ( $remainder )
7917 {
7918 ( my ($limit,$initial,$next),$remainder ) = split /:/, $remainder, 4;
7919  
7920 die "\nSupplied resize string is malformed.\n\n" unless $initial;
7921 die "\nSupplied resize string is malformed.\n\n" unless $next;
7922  
7923 push @size_arr, [$limit, $initial, $next];
7924 }
7925 # Force upper limit bound
7926 $size_arr[$#size_arr][0] = 'UNLIMITED';
7927 }
7928 }
7929  
7930 #sub _show_free_space
7931 #
7932 # Reutrns a report in the form of:
7933 #
7934 # Space analysis for: [schema.]<object name>
7935 #
7936 # BYTES BLOCKS
7937 # ============ =========
7938 # Used BELOW the high water mark 1325056 647
7939 # Free ABOVE the high water mark 716800 350
7940 # Free BELOW the high water mark 6144 3
7941 # ------------ ---------
7942 # TOTAL in segment 2048000 1000
7943 #
7944 # FILE_ID BLOCK_ID BLOCK_NBR
7945 # ======= ======== =========
7946 # Last extent having data 9 10287 150
7947 #
7948 sub _show_free_space
7949 {
7950 my ( $owner, $name, $type, $view ) = @_;
7951  
7952 my $stmt =
7953 "
7954 SELECT
7955 'Yes, I can execute package DBMS_SPACE'
7956 FROM
7957 all_tab_privs
7958 WHERE
7959 privilege = 'EXECUTE'
7960 AND table_name = 'DBMS_SPACE'
7961 AND table_schema = 'SYS'
7962 ";
7963  
7964 $sth = $dbh->prepare( $stmt );
7965 $sth->execute;
7966 my ( $can_execute ) = $sth->fetchrow_array;
7967 die "Either you or PUBLIC must have EXECUTE privilege on package\n",
7968 "sys.DBMS_SPACE in order to produce the FREE SPACE report.\n\n"
7969 unless $can_execute;
7970  
7971 if ( $oracle_major == 7 or "\U$type" eq 'CLUSTER' )
7972 {
7973 $stmt =
7974 "
7975 SELECT
7976 'NO' AS partitioned
7977 FROM
7978 ${view}_segments
7979 WHERE
7980 segment_name = UPPER( ? )
7981 AND segment_type = UPPER( '$type' )
7982 ";
7983 }
7984 else
7985 {
7986 my $plural = ( "\U$type" eq 'INDEX' ) ? 'ES' : 'S';
7987  
7988 $stmt =
7989 "
7990 SELECT
7991 partitioned
7992 FROM
7993 ${view}_$type$plural
7994 WHERE
7995 ${type}_name = UPPER( ? )
7996 ";
7997 }
7998  
7999 if ( $view eq 'DBA' )
8000 {
8001 $stmt .=
8002 "
8003 AND owner = UPPER('$owner')
8004 ";
8005 }
8006  
8007 $sth = $dbh->prepare( $stmt );
8008 $sth->execute( $name );
8009 my ( $partitioned ) = $sth->fetchrow_array;
8010 die "\u\L$type \U$name \Ldoes not exist.\n\n" unless $partitioned;
8011  
8012 my $text = "Space analysis for: \U$owner.$name\n\n";
8013  
8014 if ( $partitioned eq 'YES' )
8015 {
8016 my (
8017 $section,
8018 $total_blocks,
8019 $total_bytes,
8020 $unused_blocks,
8021 $unused_bytes,
8022 $free_blocks,
8023 $sum_total_blocks,
8024 $sum_total_bytes,
8025 $sum_unused_blocks,
8026 $sum_unused_bytes,
8027 $sum_free_blocks,
8028 );
8029  
8030 $stmt =
8031 "
8032 SELECT
8033 RPAD(partition_name,30)
8034 , segment_type
8035 FROM
8036 ${view}_segments
8037 WHERE
8038 segment_name = UPPER( ? )
8039 ";
8040  
8041 if ( $view eq 'DBA' )
8042 {
8043 $stmt .=
8044 "
8045 AND owner = UPPER('$owner')
8046 ";
8047 }
8048  
8049 $stmt .=
8050 "
8051 ORDER
8052 BY
8053 partition_name
8054 ";
8055  
8056 $sth = $dbh->prepare( $stmt );
8057 $sth->execute( $name );
8058 my $aref = $sth->fetchall_arrayref;
8059  
8060 foreach my $row (@$aref )
8061 {
8062 my ( $partition, $seg_type ) = @$row;
8063  
8064 (
8065 $section,
8066 $total_blocks,
8067 $total_bytes,
8068 $unused_blocks,
8069 $unused_bytes,
8070 $free_blocks,
8071 ) = _print_space( $owner, $name, $seg_type, $partition );
8072  
8073 $text .= $section;
8074  
8075 $sum_total_blocks += $total_blocks;
8076 $sum_total_bytes += $total_bytes;
8077 $sum_unused_blocks += $unused_blocks;
8078 $sum_unused_bytes += $unused_bytes;
8079 $sum_free_blocks += $free_blocks;
8080 }
8081  
8082 $text .= "TOTAL segment BYTES" .
8083 " BLOCKS\n" .
8084 " ============" .
8085 " =========\n" .
8086 sprintf(
8087 "Used BELOW the high water mark %12d %9d\n",
8088 $sum_total_bytes - $sum_unused_bytes
8089 - $sum_free_blocks * $block_size * 1024,
8090 $sum_total_blocks - $sum_unused_blocks - $sum_free_blocks
8091 ) .
8092 sprintf(
8093 "Free ABOVE the high water mark %12d %9d\n",
8094 $sum_unused_bytes, $sum_unused_blocks
8095 ) .
8096 sprintf(
8097 "Free BELOW the high water mark %12d %9d\n",
8098 $sum_free_blocks * $block_size * 1024, $sum_free_blocks
8099 ) .
8100 " ------------" .
8101 " ---------\n" .
8102 sprintf(
8103 " GRAND TOTAL in segment %12d %9d\n\n",
8104 $sum_total_bytes, $sum_total_blocks
8105 );
8106  
8107 return $text;
8108 }
8109 else # Not partitioned
8110 {
8111 ( my $section ) = _print_space( $owner, $name, $type );
8112 return $text . $section;
8113 }
8114 }
8115  
8116 #sub _subpartition_key_columns
8117 #
8118 # Returns a formatted string containing the subpartitioning key columns
8119 #
8120 sub _subpartition_key_columns
8121 {
8122 return _key_columns( @_, 'SUBPART' );
8123 }
8124  
8125 # sub _table_columns
8126 #
8127 # Returns a formatted string containing the column names, datatype and
8128 # length, and NOT NULL (if appropriate) for use in a CREATE TABLE
8129 # statement.
8130 #
8131 sub _table_columns
8132 {
8133 my ( $owner, $name, $view ) = @_;
8134  
8135 my $stmt;
8136  
8137 if ( $oracle_major == 7 )
8138 {
8139 $stmt =
8140 "
8141 SELECT
8142 RPAD(LOWER(column_name),32)
8143 || RPAD(
8144 DECODE(
8145 data_type
8146 ,'NUMBER',DECODE(
8147 data_precision
8148 ,null,DECODE(
8149 data_scale
8150 ,0,'INTEGER'
8151 , 'NUMBER '
8152 )
8153 ,'NUMBER '
8154 )
8155 ,'RAW' ,'RAW '
8156 ,'CHAR' ,'CHAR '
8157 ,'NCHAR' ,'NCHAR '
8158 ,'UROWID' ,'UROWID '
8159 ,'VARCHAR2','VARCHAR2 '
8160 ,data_type
8161 )
8162 || DECODE(
8163 data_type
8164 ,'DATE',null
8165 ,'LONG',null
8166 ,'NUMBER',DECODE(
8167 data_precision
8168 ,null,null
8169 ,'('
8170 )
8171 ,'RAW' ,'('
8172 ,'CHAR' ,'('
8173 ,'NCHAR' ,'('
8174 ,'UROWID' ,'('
8175 ,'VARCHAR2' ,'('
8176 ,'NVARCHAR2','('
8177 ,null
8178 )
8179 || DECODE(
8180 data_type
8181 ,'RAW' ,data_length
8182 ,'CHAR' ,data_length
8183 ,'NCHAR' ,data_length
8184 ,'UROWID' ,data_length
8185 ,'VARCHAR2' ,data_length
8186 ,'NVARCHAR2',data_length
8187 ,'NUMBER' ,data_precision
8188 , null
8189 )
8190 || DECODE(
8191 data_type
8192 ,'NUMBER',DECODE(
8193 data_precision
8194 ,null,null
8195 ,DECODE(
8196 data_scale
8197 ,null,null
8198 ,0 ,null
8199 ,',' || data_scale
8200 )
8201 )
8202 )
8203 || DECODE(
8204 data_type
8205 ,'DATE',null
8206 ,'LONG',null
8207 ,'NUMBER',DECODE(
8208 data_precision
8209 ,null,null
8210 ,')'
8211 )
8212 ,'RAW' ,')'
8213 ,'CHAR' ,')'
8214 ,'NCHAR' ,')'
8215 ,'UROWID' ,')'
8216 ,'VARCHAR2' ,')'
8217 ,'NVARCHAR2',')'
8218 ,null
8219 )
8220 ,33
8221 )
8222 , nullable
8223 , data_default
8224 FROM
8225 ${view}_tab_columns
8226 WHERE
8227 table_name = UPPER( ? )
8228 ";
8229 }
8230 else # We're newer than Oracle7
8231 {
8232 $stmt =
8233 "
8234 SELECT
8235 RPAD(LOWER(column_name),32)
8236 || RPAD(
8237 DECODE(
8238 data_type
8239 ,'NUMBER',DECODE(
8240 data_precision
8241 ,null,DECODE(
8242 data_scale
8243 ,0,'INTEGER'
8244 , 'NUMBER '
8245 )
8246 ,'NUMBER '
8247 )
8248 ,'RAW' ,'RAW '
8249 ,'CHAR' ,'CHAR '
8250 ,'NCHAR' ,'NCHAR '
8251 ,'UROWID' ,'UROWID '
8252 ,'VARCHAR2','VARCHAR2 '
8253 ,data_type
8254 )
8255 || DECODE(
8256 data_type
8257 ,'DATE',null
8258 ,'LONG',null
8259 ,'NUMBER',DECODE(
8260 data_precision
8261 ,null,null
8262 ,'('
8263 )
8264 ,'RAW' ,'('
8265 ,'CHAR' ,'('
8266 ,'NCHAR' ,'('
8267 ,'UROWID' ,'('
8268 ,'VARCHAR2' ,'('
8269 ,'NVARCHAR2','('
8270 ,null
8271 )
8272 || DECODE(
8273 data_type
8274 ,'RAW' ,data_length
8275 ,'CHAR' ,data_length
8276 ,'NCHAR' ,data_length
8277 ,'UROWID' ,data_length
8278 ,'VARCHAR2' ,data_length
8279 ,'NVARCHAR2',data_length
8280 ,'NUMBER' ,data_precision
8281 , null
8282 )
8283 || DECODE(
8284 data_type
8285 ,'NUMBER',DECODE(
8286 data_precision
8287 ,null,null
8288 ,DECODE(
8289 data_scale
8290 ,null,null
8291 ,0 ,null
8292 ,',' || data_scale
8293 )
8294 )
8295 )
8296 || DECODE(
8297 data_type
8298 ,'DATE',null
8299 ,'LONG',null
8300 ,'NUMBER',DECODE(
8301 data_precision
8302 ,null,null
8303 ,')'
8304 )
8305 ,'RAW' ,')'
8306 ,'CHAR' ,')'
8307 ,'NCHAR' ,')'
8308 ,'UROWID' ,')'
8309 ,'VARCHAR2' ,')'
8310 ,'NVARCHAR2',')'
8311 ,null
8312 )
8313 ,32
8314 )
8315 , nullable
8316 , data_default
8317 FROM
8318 ${view}_tab_columns
8319 WHERE
8320 table_name = UPPER( ? )
8321 ";
8322 }
8323  
8324 if ( $view eq 'DBA' )
8325 {
8326 $stmt .=
8327 "
8328 AND owner = UPPER('$owner')
8329 ";
8330  
8331 }
8332  
8333 $stmt .=
8334 "
8335 ORDER
8336 BY
8337 column_id
8338 ";
8339  
8340 $dbh->{ LongReadLen } = 1024; # Allows Default to be 1K
8341 $dbh->{ LongTruncOk } = 1;
8342  
8343 $sth = $dbh->prepare( $stmt );
8344 $sth->execute( $name );
8345 my $aref = $sth->fetchall_arrayref;
8346  
8347 my @cols;
8348 foreach my $row ( @$aref )
8349 {
8350 my ( $column, $nullable, $default ) = @$row;
8351  
8352 # Knock off any trailing newlines
8353 chomp $default;
8354  
8355 $column .= "DEFAULT $default " if defined $default;
8356 $column .= 'NOT NULL' if $nullable eq 'N';
8357  
8358 push @cols, $column;
8359 }
8360  
8361 return join ( "\n , ", @cols ) . "\n";
8362 }
8363  
8364 1;
8365  
8366 __END__