home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume28 / sybperl / part01 / t / sbex.pl
Encoding:
Perl Script  |  1992-02-10  |  4.9 KB  |  193 lines

  1. #!../sybperl
  2.  
  3.  
  4. @nul = ('not null','null');
  5. @sysdb = ('master', 'model', 'tempdb');
  6.  
  7. require "../lib/sybperl.pl";
  8. require "../lib/sybdb.ph";
  9.  
  10. print "Sybperl version $SybperlVer\n\n";
  11.  
  12. print "This script tests some of sybperl's functions, and prints out\n";
  13. print "description of the databases that are defined in your Sybase\n";
  14. print "dataserver.\n\n";
  15.  
  16.  
  17. $dbproc = &dblogin("sa");    # Login to sybase
  18. &dbmsghandle ("message_handler"); # Some user defined error handlers
  19. &dberrhandle ("error_handler");
  20.  
  21. $dbproc2 = &dbopen;        # Get a second dbprocess, so that we can select from several
  22.                                 # chanels simultaneously. We could code things so that this
  23.                 # feature is unnecessary, but it's good to exercise it.
  24.  
  25.                 # First, find out what databases exist:
  26. &dbcmd($dbproc, "select name from sysdatabases order by crdate\n");
  27. &dbsqlexec($dbproc);
  28. &dbresults($dbproc);
  29.  
  30. database: while((@db = &dbnextrow($dbproc)))
  31. {
  32.     foreach $nm (@sysdb)
  33.     {
  34.     if($db[0] =~ /$nm/)
  35.     {
  36.         print "'$db[0]' is a system database\n";
  37.         next database;
  38.     }
  39.     }
  40.     print "Finding user tables in user database $db[0]...";
  41.  
  42.     &dbcmd($dbproc2, "select o.name, u.name, o.id\n"); # 
  43.     &dbcmd($dbproc2, "from $db[0].dbo.sysobjects o, $db[0].dbo.sysusers u\n");
  44.     &dbcmd($dbproc2, "where o.type = 'U' and u.uid = o.uid\n");
  45.     &dbcmd($dbproc2, "order by o.name\n");
  46.  
  47.     &dbsqlexec($dbproc2);
  48.     &dbresults($dbproc2);
  49.  
  50.     while((@dat = &dbnextrow($dbproc2)))
  51.     {
  52.     $tab = join('@', @dat);    # Save the information
  53.     push(@tables, $tab);    # for later use...
  54.     }
  55.     print "Done.\n";
  56.  
  57.     print "Finding user defined datatypes in database $db[0]...\n";
  58.  
  59.     &dbcmd($dbproc2, "select s.length,substring(s.name,1,30),substring(st.name,1,30)\n");
  60.     &dbcmd($dbproc2, "from $db[0].dbo.systypes s, $db[0].dbo.systypes st\n");
  61.     &dbcmd($dbproc2, "where  st.type = s.type\n");
  62.     &dbcmd($dbproc2, "and s.usertype > 100 and st.usertype < 100 and st.usertype != 18\n");
  63.     &dbsqlexec($dbproc2);
  64.     &dbresults($dbproc2);
  65.  
  66.     while((@dat = &dbnextrow($dbproc2)))
  67.     {
  68.     print "sp_addtype $dat[1],";
  69.     if ($dat[2] =~ /char|binary/)
  70.     {
  71.         print "'$dat[2]($dat[0])'";
  72.     }
  73.     else
  74.     {
  75.         print "$dat[2]";
  76.     }
  77.     print "\n";
  78.  
  79.     }
  80.     print "Done.\n";
  81.  
  82.     print "Now we find the table definition for each user table\nin database $db[0]...\n";
  83.  
  84.     foreach $ln (@tables)        # For each line in the list
  85.     {
  86.     @tab = split('@',$ln);
  87.  
  88.     &dbcmd($dbproc2, "select Column_name = c.name, \n");
  89.     &dbcmd($dbproc2, "       Type = t.name, \n");
  90.     &dbcmd($dbproc2, "       Length = c.length, \n");
  91.     &dbcmd($dbproc2, "       Nulls = convert(bit, (c.status & 8))\n");
  92.     &dbcmd($dbproc2, "from   $db[0].dbo.syscolumns c, $db[0].dbo.systypes t\n");
  93.     &dbcmd($dbproc2, "where  c.id = $tab[2]\n");
  94.     &dbcmd($dbproc2, "and    c.usertype *= t.usertype\n");
  95.     
  96.     &dbsqlexec($dbproc2);
  97.     &dbresults($dbproc2);
  98.  
  99.     print "\nTABLE $db[0].$tab[1].$tab[0]\n ("; 
  100.     $first = 1;
  101.     while((@field = &dbnextrow($dbproc2)))
  102.     {
  103.         print ",\n" if !$first;        # add a , and a \n if not first field in table
  104.         
  105.         print "\t$field[0] \t$field[1]";
  106.         print "($field[2])" if $field[1] =~ /char|bin/;
  107.         print " $nul[$field[3]]";
  108.  
  109.         $first = 0 if $first;
  110.     }
  111.     print " )\n";
  112.  
  113. # now get the indexes...
  114. #
  115.     print "\nIndexes on $db[0].$tab[0].$tab[1]...\n\n";
  116.     &dbuse($dbproc2, $db[0]);
  117.     &dbcmd($dbproc2, "sp_helpindex '$tab[1].$tab[0]'\n");
  118.  
  119.     &dbsqlexec($dbproc2);
  120.     &dbresults($dbproc2);
  121.  
  122.     while((@field = &dbnextrow($dbproc2)))
  123.     {
  124.         print "unique " if $field[1] =~ /unique/;
  125.         print "clustered " if $field[1] =~ /^clust/;
  126.         print "index $field[0]\n";
  127.         @col = split(/,/,$field[2]);
  128.         print "on $db[0].$tab[1].$tab[0] (";
  129.         $first = 1;
  130.         foreach $ln1 (@col)
  131.         {
  132.         print ", " if !$first;
  133.         $first = 0;
  134.         print "$ln1";
  135.         }
  136.         print ")\n";
  137.     }
  138.     print "\nDone.\n";
  139.     }
  140.     &dbuse($dbproc2, "master");
  141.     @tables = ();
  142. }
  143.  
  144. &dbexit;
  145.  
  146.  
  147. # Message and error handlers.
  148.  
  149. sub message_handler
  150. {
  151.     local ($db, $message, $state, $severity, $text, $server, $procedure, $line)
  152.     = @_;
  153.  
  154.     if ($severity > 0)
  155.     {
  156.     print ("Sybase message ", $message, ", Severity ", $severity,
  157.            ", state ", $state);
  158.     print ("\nServer `", $server, "'") if defined ($server);
  159.     print ("\nProcedure `", $procedure, "'") if defined ($procedure);
  160.     print ("\nLine ", $line) if defined ($line);
  161.     print ("\n    ", $text, "\n\n");
  162.  
  163. # &dbstrcpy returns the command buffer.
  164.  
  165.     local ($lineno) = 1;    # 
  166.     foreach $row (split (/\n/, &dbstrcpy ($db)))
  167.     {
  168.         print (sprintf ("%5d", $lineno ++), "> ", $row, "\n");
  169.     }
  170.     }
  171.     elsif ($message == 0)
  172.     {
  173.     print ($text, "\n");
  174.     }
  175.     
  176.     0;
  177. }
  178.  
  179. sub error_handler {
  180.     # Check the error code to see if we should report this.
  181.     if ($_[2] != &SYBESMSG) {
  182.     local ($db, $severity, $error, $os_error, $error_msg, $os_error_msg)
  183.         = @_;
  184.     print ("Sybase error: ", $error_msg, "\n");
  185.     print ("OS Error: ", $os_error_msg, "\n") if defined ($os_error_msg);
  186.     }
  187.  
  188.     &INT_CANCEL;
  189. }
  190.  
  191.  
  192.  
  193.