1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package DBIShell::dr::MSSQL_A;
# dbishell: A generic database shell based on the Perl DBI layer
# Copyright (C) 2000 Vivek Dasmohapatra (vivek@etla.org)
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
use strict;
use DBI;
use Exporter ();
use DBIShell::dr::Sybase qw();
use DBIShell::UTIL qw/:context _NULLS/;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA @INHERIT);
$VERSION = 0.01_07;
@EXPORT = ();
@EXPORT_OK = ();
%EXPORT_TAGS = ();
@ISA = qw(DBIShell::dr::Sybase);
@INHERIT = qw(_is_table _is_describable _needs_comment);
sub _init ()
{
my $target = $::{'DBIShell::'}{'dr::'}{'MSSQL_A::'};
my $source = $::{'DBIShell::'}{'dr::'}{'Sybase::'};
foreach my $sym (@INHERIT)
{
#warn("aliasing ",$target->{$sym}," to ",$source->{$sym},"\n");
$target->{$sym} = $source->{$sym};
}
@INHERIT = ();
};
use constant STATUS_NULLABLE => DBIShell::dr::Sybase::STATUS_NULLABLE;
use constant CS_ROW_RESULT => DBD::Sybase::CS_ROW_RESULT;
use constant CS_CURSOR_RESULT => DBD::Sybase::CS_CURSOR_RESULT;
use constant CS_PARAM_RESULT => DBD::Sybase::CS_PARAM_RESULT;
use constant CS_STATUS_RESULT => DBD::Sybase::CS_STATUS_RESULT;
use constant CS_MSG_RESULT => DBD::Sybase::CS_MSG_RESULT;
use constant CS_COMPUTE_RESULT => DBD::Sybase::CS_COMPUTE_RESULT;
use constant TYPEQ => <<TypeQuery;
select type from sysobjects where id = object_id('%s')
TypeQuery
use constant COMMENTQ => <<CommentQuery;
select text from syscomments
where id = object_id('%s')
order by colid asc
CommentQuery
use constant DESCQ => sprintf(<<DescTable, STATUS_NULLABLE);
select c.name, t.name as type, c.length, c.prec, c.scale, c.status & %d
from syscolumns c,
systypes t
where c.id = object_id('%%s')
and t.usertype = c.usertype
order by c.colid
DescTable
use constant TABLES_QUERY => <<TableQuery;
select rtrim(user_name(uid) + '.' + name)
from sysobjects
where type in ('U ','S ','V ')
TableQuery
use constant PROCS_QUERY => <<ProcsQuery;
select rtrim(user_name(uid) + '.' + name)
from sysobjects
where type = 'P '
ProcsQuery
use constant D_NAME => 0;
use constant D_TYPE => 1;
use constant D_SIZE => 2;
use constant D_PREC => 3;
use constant D_SCALE => 4;
use constant D_NULL => 5;
sub new ($$$)
{
_init();
my $package = ref($_[0]) ? ref(shift()) : shift();
my $driver = shift();
(!$driver || ($driver eq 'MSSQL_A')) && ($driver = 'Sybase');
return $package->SUPER::new($driver, @_);
}
sub _resolve_type ($$)
{
my $engine = $_[0];
my $target = $_[1];
my($type);
my $dbh = $engine->dbh();
eval
{
my $sth;
my $TYPEQ = sprintf(TYPEQ, $target);
$sth = $dbh->prepare($TYPEQ)
|| die($dbh->errstr,"\n");
$sth->{ChopBlanks} = 0;
$sth->execute()
|| die($sth->errstr,"\n");
$sth->bind_columns(undef, \$type);
$sth->fetchrow_arrayref();
$sth->finish();
};
if ($@) { chomp($engine->{ERROR} = $@) }
elsif (!$type)
{
$engine->{ERROR} = sprintf("No such object %s found",$target);
}
return $type;
}
sub describe ($$$)
{
my @l;
my $engine = shift(@_);
my $sh = shift(@_);
my $target = shift(@_);
my $csep;
$csep = $sh->getvar('FIELD_SEPARATOR');
$csep = defined($csep) ? $csep : '|';
my $dbh = $engine->dbh();
my $type = $engine->_resolve_type($target);
if(!$type) { return 0 }
if(_is_describable($type))
{
my($sth,$dqd);
eval
{
my $DESCQ = sprintf(DESCQ, $target);
$sth = $dbh->prepare($DESCQ)
|| die($dbh->errstr,"\n");
$sth->execute()
|| die($sth->errstr,"\n");
$dqd = $sth->fetchall_arrayref()
|| die($sth->errstr,"\n");
};
if ($@)
{
$engine->{ERRNO} = $!;
$engine->{ERROR} = $@;
$sth && $sth->finish();
return 0;
}
unless (@$dqd)
{
$engine->{ERRNO} = 0;
$engine->{ERROR} = "No such object [$target] found to describe";
$sth && $sth->finish();
return 0;
}
foreach my $r (@$dqd)
{
my $type_decl =
($$r[D_TYPE] =~ /date|time/) ? $$r[D_TYPE] :
$$r[D_SCALE] ?
sprintf('%s(%d,%d)',$$r[D_TYPE], $$r[D_PREC],$$r[D_SCALE]) :
sprintf('%s(%d)', $$r[D_TYPE],
$$r[D_PREC] ? $$r[D_PREC]: $$r[D_SIZE]);
$$r[D_NULL] = $$r[D_NULL] ? 'NOT NULL' : '';
splice(@$r, D_TYPE, 4, $type_decl);
for (my $p = 0; $p <= $#$r; $p++)
{
my $l = defined($$r[$p]) ? length($$r[$p]) : 4;
($l > $l[$p]) && ($l[$p] = $l);
}
}
my $format = join('',join($csep,(map { " %-$_.${_}s " } @l)),"\n");
$sh->errputf(CONTEXT_NIL, " \n");
$sh->start_pager($#{$dqd}+2);
$sh->outputf(CONTEXT_NIL, $format, qw(NAME TYPE NULLABLE));
foreach (@$dqd) { $sh->outputf(CONTEXT_NIL, $format, _NULLS(@$_)) }
$sh->stop_pager();
$sth->finish();
}
else
{
$engine->{ERROR} =
sprintf("%s cannot describe type '%s' objects", __PACKAGE__, $type);
return 0;
}
return 1;
}
sub dump_def ($$$)
{
my $engine = shift(@_);
my $sh = shift(@_);
my $target = shift(@_);
my $dbh = $engine->dbh();
my $type = $engine->_resolve_type($target);
if(!$type) { return 0 }
if(_needs_comment($type) || !_is_table($type))
{
eval
{
my $sth;
my $comment;
my $COMMENTQ = sprintf(COMMENTQ, $target);
$sth = $dbh->prepare($COMMENTQ)
|| die($dbh->errstr,"\n");
$sth->execute()
|| die($sth->errstr,"\n");
$sth->bind_columns(undef, \$comment)
|| die($sth->errstr,"\n");
$sh->errputf(CONTEXT_NIL, " \n");
if($sh->getvar('PRESCAN_ROWS',0))
{
my $nl = 0;
my $data = $sth->fetchall_arrayref();
foreach my $t (@$data)
{
chomp($t->[0]);
$nl += ($t->[0] =~ tr/\n/\n/);
}
$sh->start_pager($nl);
foreach my $row (@$data)
{
$sh->outputf(CONTEXT_NIL, '%s', $row->[0]);
}
$sh->stop_pager();
}
else
{
$sh->start_pager($sth->rows());
while($sth->fetchrow_arrayref())
{
chomp($comment);
$sh->outputf(CONTEXT_NIL, "%s", $comment);
}
$sh->stop_pager();
}
$sth->finish();
};
if ($@)
{
$engine->{ERRNO} = $!;
$engine->{ERROR} = $@;
return 0;
}
return 1;
}
else
{
$engine->{ERROR} =
sprintf("%s cannot show definition of %s type objects",
__PACKAGE__, $type
);
return 0;
}
}
__END__
# TLF: Nikola Tesla died for you....
|