ObjectInfo.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 86 CC 7 SCAR 33.7
CC
7
Cov
0%
grab
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 29.0
1 #
2 #extract name, type, len from a sybase database given a list of tables
3 #
4 # $Id: ObjectInfo.pm,v 1.6 2001/02/26 04:06:55 spragues Exp $
5 #
6 # (c) 1999, 2000 Morgan Stanley Dean Witter and Co.
7 # See ..../src/LICENSE for terms of distribution.
8 #
9 #
10 #pod documentation at the end
11 #
12 package Sybase::ObjectInfo;
13  
14 require 5.004;
15  
16 #set-up package
17 use strict;
18 no strict 'refs';
19 use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION/;
20 use Exporter;
21 use Carp;
22 use Sybase::DBlib;
23 $VERSION = 0.2;
24 @ISA = qw/Exporter/;
25 @EXPORT = qw/grab/;
26  
27  
28  
29 #----------------------------------------------------------
30 #Load Sybase table information into our data structure
31 #----------------------------------------------------------
32 sub grab {
33  
34  
35 my $class = shift;
36
37
38 #get args - db handle, database, and pointer to array of tables
39 my ($dbh, $db_name, $pt) = @_;
40 my %info;
41  
42 croak "param #1 must be a blessed Sybase::DBlib reference" if ref($dbh) ne "Sybase::DBlib";
43  
44  
45 #if table is not a ref the it must be a single table
46 $pt = [ $pt ] unless( ref $pt);
47
48 #check if db_name is specified, if not then it *must* be in the table
49
50 my @odata = ();
51 unless($db_name) {
52 for my $t (@$pt) {
53 my ($db, $own, $tab) = split(/\./,$t);
54 push @odata, "$db\t$tab";
55 }
56 } else {
57 for my $t (@$pt) { push @odata, "$db_name\t$t"; }
58 }
59  
60  
61  
62 #for each line in the table file
63 foreach my $tb (@odata) {
64 my ($db_name, $tab_name) = split(/\t/, $tb);
65 $db_name =~ s/\s+//g;
66 $tab_name =~ s/\s+//g;
67
68 #create the sql
69 my $sql_string=<<EOF;
70 select
71 col_name = c.name,
72 type_name = t.name,
73 col_len = c.length,
74 col_order = c.colid,
75 ctype = c.type,
76 utype = t.usertype,
77 allownulls = t.allownulls,
78 prec = c.prec
79 from
80 $db_name.dbo.syscolumns c,
81 $db_name.dbo.systypes t
82 where
83 c.id = object_id('$db_name..$tab_name')
84 and c.usertype *= t.usertype
85
86 order
87 by c.colid
88 EOF
89  
90
91 #run the sql
92 my @h_pointers = $dbh->nsql($sql_string, "HASH") ;
93 if($DB_ERROR) {carp "$DB_ERROR"}
94  
95 #re-organize
96 for my $col (@h_pointers) {
97 my $col_name= $col->{col_name};
98 $info{$db_name}->{$tab_name}->{$col_name}->{col_type} = $col->{type_name};
99 $info{$db_name}->{$tab_name}->{$col_name}->{col_len} = $col->{col_len};
100 $info{$db_name}->{$tab_name}->{$col_name}->{col_id} = $col->{col_order};
101 $info{$db_name}->{$tab_name}->{$col_name}->{col_allownulls} = $col->{allownulls};
102 $info{$db_name}->{$tab_name}->{$col_name}->{col_prec} = $col->{prec};
103  
104 #if user-defined type, get underlying
105 if($col->{utype} > 100 ) {
106  
107 #i pulled this sql from sp_help
108 my $sql = <<EOF;
109 select
110 name
111 from
112 $db_name..systypes
113 where
114 usertype < 100
115 and type=$col->{ctype}
116 and name not in ("sysname", "nchar", "nvarchar")
117 EOF
118 my @name = $dbh->nsql($sql,"HASH");
119 if($DB_ERROR) {carp "$DB_ERROR" }
120  
121 $info{$db_name}->{$tab_name}->{$col_name}->{col_type} = $name[0]->{name};
122 $info{$db_name}->{$tab_name}->{$col_name}->{col_usertype} = $col->{type_name};
123  
124 #not user-defined type
125 } else {
126 $info{$db_name}->{$tab_name}->{$col_name}->{col_usertype} = undef;
127  
128 }
129  
130 # $info{$db_name}->{$tab_name}->{$col_name}->{col_ctype} = $col->{ctype};
131 # $info{$db_name}->{$tab_name}->{$col_name}->{col_utype} = $col->{utype};
132  
133 }
134  
135 #add the list column names in the right order
136 @{ $info{$db_name}->{$tab_name}->{__COLNAMES__} } =
137 sort { $info{$db_name}->{$tab_name}->{$a}->{col_id} <=>
138 $info{$db_name}->{$tab_name}->{$b}->{col_id}
139 } keys %{ $info{$db_name}->{$tab_name} };
140  
141 }
142  
143  
144 # bless \%info;
145 return %info;
146 }
147 __END__