testd1.ec
上传用户:dgyhgb
上传日期:2007-01-07
资源大小:676k
文件大小:7k
源码类别:

SQL Server

开发平台:

Unix_Linux

  1. /*
  2.  *  monitor.l - DB line monitor
  3.  *            
  4.  *  $Id: monitor.l,v 1.245 1997/03/31 03:46:38 kml Exp kml $
  5.  *
  6.  *  This file is a part of GNU SQL Server
  7.  *
  8.  *  Copyright (c) 1996, 1997, Free Software Foundation, Inc
  9.  *  Developed at the Institute of System Programming, Russia
  10.  *  This file is written by Michael Kimelman.
  11.  * 
  12.  *  This program is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2 of the License, or
  15.  *  (at your option) any later version.
  16.  *
  17.  *  This program is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with this program; if not, write to the Free Software
  24.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25.  *
  26.  *  Contact:  gss@ispras.ru
  27.  *
  28.  */
  29. /* $Id$ */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "sql.h"
  33. #include "dyn_funcs.h"
  34. #include <assert.h>
  35.   
  36. static char *statement_scanner_buffer=NULL;
  37. int ARGC;
  38. char **ARGV;
  39. int
  40. SCANNER_NAME(void)
  41. {
  42.   return 1;
  43. }
  44. int
  45. print_width(dyn_sql_type_code_t t,int len)
  46. {
  47.   switch(t)
  48.     {
  49.     case SQL__Long  : 
  50.     case SQL__Real  : return 10;
  51.     case SQL__Short : return  5;
  52.     case SQL__Double: return 15;
  53.     default:
  54.     }
  55.   return len; 
  56. }
  57. int
  58. SQL_connect(char *host,char *user,char *passwd)
  59. {
  60.   return 0;
  61. }
  62. void
  63. error_rprt(char *st,int rc, char *stmt)
  64. {
  65.   fprintf(stderr,"n#### Error occured in '%s'n%snat "%s"n",
  66.           stmt,gsqlca.errmsg,st);
  67. }
  68. int
  69. main(int argc, char *argv[])
  70. {
  71.   static char buffer[4096*4];
  72.   int stmt = 0;
  73.   int rc;
  74.   int i,j;
  75.   static char *sps =
  76.     "                                                                       "
  77.     "                                                                       "
  78.     "                                                                       "
  79.     "                                                                       "
  80.     "                                                                       "
  81.     ;
  82.   statement_scanner_buffer = buffer;
  83.   
  84. #define CHECK(st) if((rc=SQL__##st)!=0){ error_rprt(#st,rc,statement_scanner_buffer); goto error; }
  85.   sprintf(buffer,"select * from %s.%s",
  86.           (argc>2?argv[2]:"DEFINITION_SCHEMA"),
  87.           (argc>1?argv[1]:"SYSTABLES"));
  88.   while(stmt==0)
  89.     {
  90.       char *b;
  91.       int   l = strlen(statement_scanner_buffer);
  92.       int  *clms = NULL;
  93.       
  94.       SQL_DESCR in,out;
  95.       
  96.       if (statement_scanner_buffer == NULL)
  97.         continue;
  98.       if (strncmp (statement_scanner_buffer,"select",strlen("select")) &&
  99.           strncmp(statement_scanner_buffer,"SELECT",strlen("select")))
  100.         {
  101.           /* if not select statement */
  102.           CHECK(execute_immediate(statement_scanner_buffer));
  103.           stmt++;
  104.           continue;
  105.         }
  106.       /* put select in parenthesys */
  107.       b = malloc (l+3);
  108.       b[0] = '(';
  109.       strcpy(b+1,statement_scanner_buffer);
  110.       strcat(b+1+l,")");
  111.       statement_scanner_buffer = b;
  112.           
  113.       b = malloc(20);
  114.       sprintf(b,"%dth Stmt",stmt++);
  115.       CHECK(prepare(b,statement_scanner_buffer));
  116.       CHECK(allocate_cursor(b,"CURSOR"));
  117.       in  = SQL__allocate_descr("IN",0);
  118.       out = SQL__allocate_descr("OUT",0);
  119.       CHECK(describe(b,1,in));
  120.       CHECK(describe(b,0,out));
  121.       clms = (int*)malloc(out->count*sizeof(int));
  122.       
  123.       /* open cursor and write headers */
  124.       j=1;
  125.       fprintf(stderr,"n|");
  126.       for( i=0; i < out->count; i++)
  127.         {
  128.           int jj, k = 0;
  129.           sql_descr_element_t *e = &(out->values[i]);
  130.           if (e->unnamed)
  131.             jj=fprintf(stderr," col_%03d ",i);
  132.           else
  133.             jj=fprintf(stderr," %s ",e->name);
  134.           k = print_width(e->type,e->length) - jj;
  135.           jj += fprintf(stderr,"%s|",sps+strlen(sps)- (k>0?k:0) );
  136.           clms [i] = jj - 1;
  137.           j += jj;
  138.         }
  139.       /* -------------------------------------------------*/
  140.       fprintf(stderr,"n");
  141.       for(i=j;i--;)
  142.         fprintf(stderr,"-");
  143.       if(in->count)
  144.         {
  145.           CHECK(open_cursor("CURSOR",in));
  146.         }
  147.       else
  148.         {
  149.           CHECK(open_cursor("CURSOR",NULL));
  150.         }
  151.         
  152.       while(1)
  153.         {
  154.           char oline[1024];
  155.           for( i=0; i < out->count; i++)
  156.             out->values[i].indicator = 0;
  157.           CHECK(fetch("CURSOR",out));
  158.           
  159.           if(SQLCODE!=0)
  160.             break;
  161.           
  162.           sprintf(oline,"n|");
  163.           for (i=0; i < out->count; i++)
  164.             {
  165.               int k ;
  166.               int kk = 0 ;
  167.               char buf[4096] ;
  168.               sql_descr_element_t *e = &(out->values[i]) ;
  169.               
  170.               if (e->indicator < 0)
  171.                 {
  172.                   if (!e->nullable)
  173.                     if (e->name)
  174.                       fprintf(stderr,"nindicator < 0 for 'not null' field '%s'",e->name);
  175.                     else
  176.                       fprintf(stderr,"nindicator < 0 for 'not null' field '%d'",i);
  177.                   sprintf(buf,"null");
  178.                 }
  179.               else switch(e->type)
  180.                 {
  181.                 case SQL__Char:
  182.                 case SQL__CharVar:
  183.                   sprintf(buf,"%s",(char*)e->data);
  184.                   break;
  185.                 case SQL__Double: /* DOUBLE */
  186.                 case SQL__Float: /* FLOAT  */
  187.                   sprintf(buf,"%g",*((double*)(e->data)));
  188.                   break;
  189.                 case SQL__Real: /* REAL   */
  190.                   sprintf(buf,"%g",*((float*)(e->data)));
  191.                   break;
  192.                 case SQL__Int: /* int */
  193.                   sprintf(buf,"%X",*((int*)(e->data)));
  194.                   break;
  195.                 case SQL__Short: /* small */
  196.                   sprintf(buf,"%d",(int)*((short*)(e->data)));
  197.                   break;
  198.                 default:
  199.                   sprintf(buf," **%d** ",e->type);
  200.                 }
  201.               assert(kk==0);
  202.               k = (clms[i] - strlen(buf))/2; 
  203.               if (k > 4*clms[i])
  204.                 {
  205.                   strcat(oline,"!!");
  206.                   k = 0;
  207.                 }
  208.               k = sprintf(oline+strlen(oline),"%s%s",
  209.                           sps + strlen(sps) - (k>0?k:0),buf);
  210.               k = clms[i] - k;
  211.               sprintf(oline+strlen(oline),"%s|",
  212.                       sps + strlen(sps) - (k>0?k:0));
  213.             }
  214.           fprintf(stderr,"%s",oline);
  215.         }
  216.       
  217.       /* -------------------------------------------------*/
  218.       fprintf(stderr,"n");
  219.       for(i=j;i--;)
  220.         fprintf(stderr,"-");
  221.       fprintf(stderr,"nn");
  222.       CHECK(close_cursor("CURSOR"));
  223.       CHECK(deallocate_prepare(b));
  224.     error:
  225.       SQL__deallocate_descr(&in);
  226.       SQL__deallocate_descr(&out);
  227.       xfree(clms);
  228.       xfree(b);
  229.       b = NULL;
  230.     }
  231.   _SQL_commit();
  232.   return 0;
  233. }