query.pl untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 72 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #! /usr/bin/perl -w
2  
3 # $Id: query.pl,v 1.4 2001/03/31 18:28:34 rvsutherland Exp $
4  
5 use strict;
6  
7 use DBI;
8 use DDL::Oracle;
9  
10 my $aref;
11 my $sql;
12 my $sth;
13 my $stmt;
14  
15 my $dbh = DBI->connect(
16 "dbi:Oracle:",
17 "",
18 "",
19 {
20 PrintError => 0,
21 RaiseError => 1
22 }
23 );
24  
25 DDL::Oracle->configure(
26 dbh => $dbh,
27 # resize => 0,
28 # view => 'user',
29 );
30  
31 print STDERR "Enter Action [CREATE]: ";
32 chomp( my $action = <STDIN> );
33 $action = "create" unless $action;
34  
35 print STDERR "Enter Type [TABLE]: ";
36 chomp( my $type = <STDIN> );
37 $type = "TABLE" unless $type;
38  
39 print STDERR "Enter Name of File : ";
40 chomp( my $file = <STDIN> );
41  
42 die "\nYou must specify a File.\n" unless $file;
43 die "\nFile $file does not exist.\n" unless -e $file;
44 die "\nFile $file is not readable.\n" unless -r $file;
45  
46 open FILE, "< $file" or die "\nCan't open $file: $!\n";
47  
48 print STDERR "\n";
49  
50 my @lines = <FILE>;
51  
52 # Create statement, eliminating lines containing only a slash
53 # and eliminating any semi-colons
54 $stmt = ( join "", grep !/^\/$/, @lines );
55 $stmt =~ s/\;//g;
56 $sth = $dbh->prepare( $stmt );
57 $sth->execute;
58 $aref = $sth->fetchall_arrayref;
59  
60 my $obj = DDL::Oracle->new(
61 type => $type,
62 list => $aref,
63 );
64  
65 if ( $action eq "drop" ){
66 $sql = $obj->drop;
67 }
68 elsif ( $action eq "create" ){
69 $sql = $obj->create;
70 }
71 elsif ( $action eq "resize" ){
72 $sql = $obj->resize;
73 }
74 elsif ( $action eq "compile" ){
75 $sql = $obj->compile;
76 }
77 elsif ( $action eq "show_space" ){
78 $sql = $obj->show_space;
79 }
80 else{
81 die "\nDon't know how to '$action'.\n";
82 } ;
83  
84 print $sql;
85  
86 # $Log: query.pl,v $
87 # Revision 1.4 2001/03/31 18:28:34 rvsutherland
88 # Facilitated new method 'show_space'.
89 #
90 # Revision 1.3 2001/01/27 16:21:44 rvsutherland
91 # Added NAME section to pod.
92 #
93 # Revision 1.2 2001/01/14 16:47:55 rvsutherland
94 # Nominal changes for version 0.32
95 #
96 # Revision 1.1 2001/01/07 16:42:45 rvsutherland
97 # Initial Revision
98 #
99  
100 =head1 NAME
101  
102 query.pl - Generates DDL for a specified list of objects.
103  
104 =head1 DESCRIPTION
105  
106 Uses DDL::Oracle to generate the DDL for a query provided in a file.
107 The query should select owner, name for a list of objects of the same
108 type (e.g., TABLE, INDEX, TABLESPACE, etc.). The FROM and WHERE clauses
109 may be anything of the user's choice.
110  
111 For example, the file might contain:
112  
113 SELECT
114 owner
115 , table_name
116 FROM
117 dba_tables
118 WHERE
119 tablespace_name = 'MY_TBLSP' -- your mileage may vary
120  
121 The file may contain SQL*Plus's traditional '/', and/or may contain a ';'
122  
123 =head1 AUTHOR
124  
125 Richard V. Sutherland
126 rvsutherland@yahoo.com
127  
128 =head1 COPYRIGHT
129  
130 Copyright (c) 2000, 2001 Richard V. Sutherland. All rights reserved.
131 This module is free software. It may be used, redistributed, and/or
132 modified under the same terms as Perl itself. See:
133  
134 http://www.perl.com/perl/misc/Artistic.html
135  
136 =cut
137