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

SQL Server

开发平台:

Unix_Linux

  1. %{
  2. /*
  3.  *  monitor.l - DB line monitor
  4.  *            
  5.  *  $Id: monitor.l,v 1.248 1997/04/03 14:36:10 kml Exp $
  6.  *
  7.  *  This file is a part of GNU SQL Server
  8.  *
  9.  *  Copyright (c) 1996, 1997, Free Software Foundation, Inc
  10.  *  Developed at the Institute of System Programming, Russia
  11.  *  This file is written by Michael Kimelman.
  12.  * 
  13.  *  This program is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  This program is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License
  24.  *  along with this program; if not, write to the Free Software
  25.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26.  *
  27.  *  Contact:  gss@ispras.ru
  28.  *
  29.  */
  30.   
  31. #include "xmem.h"
  32. #include "sql_decl.h"
  33. #include "sql.h"
  34. #include "dyn_funcs.h"
  35. #include <assert.h>
  36.   
  37. #define SWITCH_COND(cond)  { save_condition();BEGIN(cond);}
  38. #define REST_COND            rest_condition();
  39. #define SCANNER_NAME      yylex_extractor
  40. #ifdef YY_DECL
  41. #  undef YY_DECL
  42. #endif
  43. #define YY_DECL           i4_t SCANNER_NAME(void)
  44. YY_DECL;
  45. #define SQL_PROC(code)  if(code){return code;}
  46. static str_buf c_scanner_buffer={0,0,NULL};
  47. #define sql_str(s,l)  buffer_string(&c_scanner_buffer,s,l)
  48. static void  save_condition(YY_PROTO(void));
  49. static void  rest_condition(YY_PROTO(void));
  50. static char *statement_scanner_buffer=NULL;
  51. #define APPEND_BUFFER { sql_str(yytext,yyleng);} 
  52. #define POP_BUFFER pop_cl_buffer()
  53. #define NEW_LINE        {
  54.   if (smth_done) fprintf(stderr,"(:%d)>",++line_num);
  55.   else           fprintf(stderr,"FreeSQL(:%d)>",++line_num); }
  56. void pop_cl_buffer(void)
  57. {
  58.   register char *b=sql_str(NULL,0);
  59.   statement_scanner_buffer= (b?savestring(b):NULL);
  60. }
  61. %}
  62. %x SQL_str SQL_comment
  63. %s Start
  64. WS              [ tf]+
  65. NNS             [^A-Za-z0-9_]
  66. OPTWS           [ tf]*
  67. NOT_WS          [^ tfn]
  68. LET             [A-Za-z]
  69. SQL_STRT        ((EXEC[ t]+SQL)|("$"))
  70. %%
  71.     i4_t sql_str_d_quote=0;
  72.     static i4_t line_num = 0;
  73.     i4_t smth_done = 0;
  74.                          NEW_LINE;
  75.   /*********************************************
  76.    *        lexics of embedded SQL in C        *
  77.    *********************************************/
  78. ;                       {
  79.    POP_BUFFER;
  80.    SQL_PROC(1); 
  81.  }
  82. {WS}                  { if(smth_done) { APPEND_BUFFER; } }
  83. '                  {
  84.                            APPEND_BUFFER;
  85.                            smth_done = 1;
  86.    SWITCH_COND(SQL_str);
  87.  }
  88. "                  {
  89.                            APPEND_BUFFER;
  90.                            smth_done = 1;
  91.    sql_str_d_quote=1;
  92.    SWITCH_COND(SQL_str);
  93.  }
  94. "-"{2}                  {
  95.                            SWITCH_COND(SQL_comment);  
  96.  }
  97. n                  {
  98.                            if (smth_done)
  99.                              {
  100.                                APPEND_BUFFER;
  101.                              }
  102.                            NEW_LINE;
  103.  }
  104. .          {
  105.                            APPEND_BUFFER;
  106.                            smth_done = 1;
  107.  }
  108.   /*********************************************
  109.    *             SQL string recognizer         *
  110.    *********************************************/
  111. <SQL_str>'{2}           { APPEND_BUFFER; }
  112. <SQL_str>n              {
  113.                            yyerror(" Unterminated SQL string ");
  114.                            NEW_LINE;
  115.    REST_COND;
  116.          }
  117. <SQL_str>[^'"n]+      { APPEND_BUFFER; }
  118. <SQL_str>"{2}           { APPEND_BUFFER; }
  119. <SQL_str>"              {
  120.    APPEND_BUFFER ;
  121.            if(sql_str_d_quote) 
  122.                              {
  123.        sql_str_d_quote=0;
  124.        REST_COND;
  125.      }
  126.  }
  127. <SQL_str>'              {
  128.                            APPEND_BUFFER;
  129.    if(!sql_str_d_quote) 
  130.      {
  131.        REST_COND;
  132.      }
  133.  }
  134.   /*********************************************
  135.    *             SQL comment recognizer        *
  136.    *********************************************/
  137. <SQL_comment>.      /* do nothing */;
  138. <SQL_comment>n          {
  139.                            NEW_LINE;
  140.    REST_COND;
  141.  }
  142. %%
  143. typedef struct save_yy_cond {
  144.       i4_t yy_cond;
  145.       struct save_yy_cond *nxt;
  146.     } *sv_ys_ptr;
  147. static sv_ys_ptr save_yy_start=NULL;
  148. static void
  149. save_condition(YY_PROTO(void))
  150. {
  151.   register sv_ys_ptr nptr=(sv_ys_ptr)xmalloc(sizeof(struct save_yy_cond));
  152.   nptr->yy_cond=yy_start;
  153.   nptr->nxt=save_yy_start;
  154.   save_yy_start=nptr;
  155. #ifdef FLEX_DEBUG
  156.   if(yy_flex_debug)
  157.     fprintf(stderr,"n Lex: save condition :%d => (%d)n",
  158.             yy_start,nptr->yy_cond);
  159. #endif
  160. }
  161. static void
  162. rest_condition(YY_PROTO(void))
  163. {
  164.   register sv_ys_ptr nptr=save_yy_start;
  165.   if(nptr==NULL)
  166.     fatal("Lex: Impossible restore conditions before save it !",NULL);
  167.   yy_start=nptr->yy_cond;
  168.   save_yy_start=nptr->nxt;
  169. #ifdef FLEX_DEBUG
  170.   if(yy_flex_debug)
  171.     fprintf(stderr,"n Lex: restore condition :%d <=(%d)n",
  172.             yy_start,nptr->yy_cond);
  173. #endif
  174.   xfree(nptr);
  175. }
  176. int
  177. print_width(dyn_sql_type_code_t t,int len)
  178. {
  179.   switch(t)
  180.     {
  181.     case SQL__Long  : 
  182.     case SQL__Real  : return 10;
  183.     case SQL__Short : return  5;
  184.     case SQL__Double: return 15;
  185.     default:                   ;
  186.     }
  187.   return len; 
  188. }
  189. void
  190. error_rprt(char *st,int rc, char *stmt)
  191. {
  192.   fprintf(stderr,"n#### Error occured in '%s'n%snat "%s"n",
  193.           stmt,gsqlca.errmsg,st);
  194. }
  195. int
  196. main(int argc, char *argv[])
  197. {
  198.   static char buffer[4096*4];
  199.   int stmt = 0;
  200.   int rc;
  201.   int i,j;
  202.   static char *sps =
  203.     "                                                                       "
  204.     "                                                                       "
  205.     "                                                                       "
  206.     "                                                                       "
  207.     "                                                                       "
  208.     ;
  209.   yyin = stdin;
  210.   yyout = stderr;
  211. #if 0 
  212.   if (argc > 1)
  213.     {
  214.       char *host , *user, *passwd;
  215.       host   = argv[1];
  216.       user   = (argc > 2 ? argv[2]: getlogin() );
  217.       passwd = (argc > 3 ? argv[3]: NULL);
  218.       if(SQL__session(host,user,passwd)!=0)       /* Not implemented yet */
  219.         yyfatal("Can't connect to given host");
  220.     }
  221. #endif
  222.   
  223. #define CHECK(st) if((rc=SQL__##st)!=0){ error_rprt(#st,rc,statement_scanner_buffer); goto error; }
  224.   while(SCANNER_NAME())
  225.     {
  226.       char *b;
  227.       int   l = strlen(statement_scanner_buffer);
  228.       int  *clms = NULL;
  229.       
  230.       SQL_DESCR in,out;
  231.       
  232.       if(statement_scanner_buffer==NULL)
  233.         continue;
  234.       if(strncmp(statement_scanner_buffer,"select",strlen("select")))
  235.         {
  236.           /* if not select statement */
  237.           CHECK(execute_immediate(statement_scanner_buffer));
  238.           stmt++;
  239.           continue;
  240.         }
  241.       /* put select in parenthesys */
  242.       b = xmalloc(l+3);
  243.       b[0] = '(';
  244.       strcpy(b+1,statement_scanner_buffer);
  245.       strcat(b+1+l,")");
  246.       xfree(statement_scanner_buffer);
  247.       statement_scanner_buffer = b;
  248.           
  249.       b = xmalloc(20);
  250.       sprintf(b,"%dth Stmt",stmt++);
  251.       CHECK(prepare(b,statement_scanner_buffer));
  252.       CHECK(allocate_cursor(b,"CURSOR"));
  253.       in  = SQL__allocate_descr("IN",0);
  254.       out = SQL__allocate_descr("OUT",0);
  255.       CHECK(describe(b,1,in));
  256.       CHECK(describe(b,0,out));
  257.       clms = (int*)xmalloc(out->count*sizeof(int));
  258.       
  259.       /* open cursor and write headers */
  260.       j=1;
  261.       fprintf(stderr,"n|");
  262.       for( i=0; i < out->count; i++)
  263.         {
  264.           int jj, k = 0;
  265.           sql_descr_element_t *e = &(out->values[i]);
  266.           if (e->unnamed)
  267.             jj=fprintf(stderr," col_%03d ",i);
  268.           else
  269.             jj=fprintf(stderr," %s ",e->name);
  270.           k = print_width(e->type,e->length) - jj;
  271.           jj += fprintf(stderr,"%s|",sps+strlen(sps)- (k>0?k:0) );
  272.           clms [i] = jj - 1;
  273.           j += jj;
  274.         }
  275.       /* -------------------------------------------------*/
  276.       fprintf(stderr,"n");
  277.       for(i=j;i--;)
  278.         fprintf(stderr,"-");
  279.       if(in->count)
  280.         {
  281.           CHECK(open_cursor("CURSOR",in));
  282.         }
  283.       else
  284.         {
  285.           CHECK(open_cursor("CURSOR",NULL));
  286.         }
  287.         
  288.       while(1)
  289.         {
  290.           char oline[1024];
  291.           for( i=0; i < out->count; i++)
  292.             out->values[i].indicator = 0;
  293.           CHECK(fetch("CURSOR",out));
  294.           
  295.           if(SQLCODE!=0)
  296.             break;
  297.           
  298.           sprintf(oline,"n|");
  299.           for (i=0; i < out->count; i++)
  300.             {
  301.               int k ;
  302.               int kk = 0 ;
  303.               char buf[4096] ;
  304.               sql_descr_element_t *e = &(out->values[i]) ;
  305.               
  306.               if (e->indicator < 0)
  307.                 {
  308.                   if (!e->nullable)
  309.                     if (e->name)
  310.                       fprintf(stderr,"nindicator < 0 for 'not null' field '%s'",e->name);
  311.                     else
  312.                       fprintf(stderr,"nindicator < 0 for 'not null' field '%d'",i);
  313.                   sprintf(buf,"null");
  314.                 }
  315.               else switch(e->type)
  316.                 {
  317.                 case SQL__Char:
  318.                 case SQL__CharVar:
  319.                   sprintf(buf,"%s",(char*)e->data);
  320.                   break;
  321.                 case SQL__Double: /* DOUBLE */
  322.                 case SQL__Float: /* FLOAT  */
  323.                   sprintf(buf,"%g",*((double*)(e->data)));
  324.                   break;
  325.                 case SQL__Real: /* REAL   */
  326.                   sprintf(buf,"%g",*((float*)(e->data)));
  327.                   break;
  328.                 case SQL__Int: /* int */
  329.                   sprintf(buf,"%X",*((int*)(e->data)));
  330.                   break;
  331.                 case SQL__Short: /* small */
  332.                   sprintf(buf,"%d",(int)*((short*)(e->data)));
  333.                   break;
  334.                 default:
  335.                   sprintf(buf," **%d** ",e->type);
  336.                 }
  337.               assert(kk==0);
  338.               k = (clms[i] - strlen(buf))/2; 
  339.               if (k > 4*clms[i])
  340.                 {
  341.                   strcat(oline,"!!");
  342.                   k = 0;
  343.                 }
  344.               k = sprintf(oline+strlen(oline),"%s%s",
  345.                           sps + strlen(sps) - (k>0?k:0),buf);
  346.               k = clms[i] - k;
  347.               sprintf(oline+strlen(oline),"%s|",
  348.                       sps + strlen(sps) - (k>0?k:0));
  349.             }
  350.           fprintf(stderr,"%s",oline);
  351.         }
  352.       
  353.       /* -------------------------------------------------*/
  354.       fprintf(stderr,"n");
  355.       for(i=j;i--;)
  356.         fprintf(stderr,"-");
  357.       fprintf(stderr,"nn");
  358.       CHECK(close_cursor("CURSOR"));
  359.       CHECK(deallocate_prepare(b));
  360.     error:
  361.       SQL__deallocate_descr(&in);
  362.       SQL__deallocate_descr(&out);
  363.       xfree(clms);
  364.       xfree(b);
  365.       b = NULL;
  366.     }
  367.       
  368.   _SQL_commit();
  369.   return 0;
  370. }