home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / DBDEMO.ZIP / DEMOFLS.ZIP / STAT_DYN.SQC < prev    next >
Text File  |  1991-06-30  |  8KB  |  240 lines

  1. /************************************************************************/
  2. /* DATABASE PERFORMANCE CONCEPTS AND TECHNIQUES DEMONSTRATION PROGRAM   */
  3. /* MODULE:  STATIC VS. DYNAMIC                                          */
  4. /* source file: STAT_DYN.SQC                                            */
  5. /************************************************************************/
  6.  
  7. /************************************************************************/
  8. /* HEADER FILES - USER DEFINED                                          */
  9. /************************************************************************/
  10. #include "db.h"
  11.  
  12. /************************************************************************/
  13. /*     FUNCTION PROTOTYPES                                              */
  14. /************************************************************************/
  15.  
  16. short far stat_dyn(void);
  17. short stat(void);
  18. short dyn(void);
  19.  
  20.  double q1time, q2time;
  21.  struct timeb dbeg, dend;
  22.  
  23. /***************************************************************************/
  24. /*           static query                                               */
  25. /***************************************************************************/
  26. /*************************************************/
  27. /*  SQL HOST VARIABLE DECLARATIONS               */
  28. /*************************************************/
  29. EXEC SQL BEGIN DECLARE SECTION;
  30. char st_cu_ssn[12];
  31. char st_cu_name[31];
  32. double s_bal;
  33. char c_accnumb[9];
  34. char st_cu_addr1[26];
  35. char st_cu_addr2[26];
  36. char st_cu_zip[6];
  37. EXEC SQL END DECLARE SECTION;
  38.  
  39. short num_rows;
  40. /************************************************************************/
  41. /* FUNCTION  stat()                                                     */
  42. /*  Runs static query                                                   */
  43. /************************************************************************/
  44. short stat(void)
  45. {
  46.  
  47.      q1time = 0.0;
  48.      num_rows = 0;
  49.  
  50. /*** SQL error handling ***/
  51.   EXEC SQL WHENEVER SQLERROR GO TO error;
  52.   EXEC SQL WHENEVER NOT FOUND GO TO ext;
  53.  
  54. /****** DECLARE CURSOR *********/
  55.      ftime (&dbeg);
  56.  
  57.      EXEC SQL DECLARE CSTAT CURSOR FOR
  58.         SELECT  CU_NAME, CU_SSN, S_BAL, C_ACCNUMB
  59.         FROM BMCHUGH.CUSTOMER, BMCHUGH.SAVINGS, BMCHUGH.CHECKING
  60.         WHERE CU_SSN = S_SSN AND CU_SSN > '000-00-0000'
  61.            AND  CU_SSN < '999-99-9999' AND  S_BAL > 499
  62.            AND  S_BAL < 20000 AND  C_STARTBAL > 0
  63.            AND C_STARTBAL < 10000 AND C_ENDBAL > 0
  64.            AND C_ENDBAL < 20000 AND C_STARTBAL <= C_ENDBAL
  65.            AND CU_ZIP  BETWEEN  '11111'  AND  '99999'
  66.            AND CU_NAME LIKE 'B%Vald%'
  67.            AND (CU_SSN = C_SSN1 OR CU_SSN = C_SSN2);
  68.  
  69.      ftime(&dend);
  70.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  71.  
  72. //     get_error( &sqlca, " declare cursor");
  73.  
  74. /****** OPEN CURSOR *********/
  75.      ftime (&dbeg);
  76.     EXEC SQL OPEN CSTAT;
  77.      ftime(&dend);
  78.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  79.  
  80. //     get_error( &sqlca, " open cursor");
  81.  
  82. /****** FETCH TILL NO MORE ROWS LEFT *********/
  83.     while (sqlca.sqlcode == 0) {
  84.  
  85.      ftime (&dbeg);
  86.        EXEC SQL FETCH CSTAT
  87.        INTO :st_cu_ssn, :st_cu_name, :s_bal, :c_accnumb;
  88.      ftime(&dend);
  89.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  90.  
  91.      if ((sqlca.sqlcode == 0) ) num_rows++;
  92.     }/*end while more rows */
  93.  
  94.  ext:
  95. /****** CLOSE CURSOR *********/
  96.      ftime (&dbeg);
  97.    EXEC SQL CLOSE CSTAT;
  98.      ftime(&dend);
  99.      q1time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  100.  
  101. /*     printf("\nstat time = %7.3f", q1time);
  102. */
  103.     return(0);
  104.  
  105.  error:
  106.  error_sql = sqlca.sqlcode; // copy the sql code into external variable
  107.                             // to be printed on user screen by dbdemo
  108.  return(-1);
  109. }/* end stat */
  110.  
  111. /************************************************************************/
  112. /* FUNCTION dyn()                                                       */
  113. /* runs the same query as stat() dynamically.  It uses the simplest form*/
  114. /* of dynamic query, assuming that                                      */
  115. /*   1. The query is a SELECT                                           */
  116. /*   2. The table name and the columns fetched are known                */
  117. /************************************************************************/
  118. short dyn()
  119. {
  120. /* the same queries executed dynamically */
  121. /*************************************************/
  122. /*  SQL HOST VARIABLE DECLARATIONS               */
  123. /*************************************************/
  124. EXEC SQL BEGIN DECLARE SECTION;
  125.   char qstr[600];
  126. EXEC SQL END DECLARE SECTION;
  127.  
  128. /*** SQL error handling ***/
  129.   EXEC SQL WHENEVER SQLERROR GO TO error;
  130.   EXEC SQL WHENEVER NOT FOUND GO TO ext;
  131.  
  132. /****** DECLARE CURSOR *********/
  133.   EXEC SQL DECLARE CDYN CURSOR FOR SDYN;
  134.  
  135.   q2time= 0.0;
  136.      num_rows = 0;
  137.   strcpy(qstr, "SELECT  CU_NAME, CU_SSN, S_BAL, C_ACCNUMB" );
  138.   strcat(qstr , "  FROM BMCHUGH.CUSTOMER, BMCHUGH.SAVINGS, BMCHUGH.CHECKING");
  139.   strcat(qstr ,    "  WHERE CU_SSN = S_SSN AND CU_SSN > '000-00-0000'"      );
  140.   strcat(qstr ,       " AND  CU_SSN < '999-99-9999' AND  S_BAL > 499"       );
  141.   strcat(qstr ,       " AND  S_BAL < 20000 AND  C_STARTBAL > 0"            );
  142.   strcat(qstr ,       " AND C_STARTBAL < 10000 AND C_ENDBAL > 0"           );
  143.   strcat(qstr ,       " AND C_ENDBAL < 20000 AND C_STARTBAL <= C_ENDBAL"   );
  144.   strcat(qstr ,       " AND CU_ZIP  BETWEEN  '11111'  AND  '99999'"        );
  145.   strcat(qstr ,       " AND CU_NAME LIKE 'B%Vald%'"                        );
  146.   strcat(qstr ,       " AND (CU_SSN = C_SSN1 OR CU_SSN = C_SSN2)"          );
  147.  
  148. /****** PREPARE CURSOR SINCE DYNAMIC QUERY *********/
  149. // in more complex situation DESCRIBE and SQLDA may have to be used
  150.  
  151.      ftime (&dbeg);
  152.    EXEC SQL PREPARE SDYN FROM :qstr ;
  153.      ftime(&dend);
  154.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  155. //   get_error( &sqlca, " prepare cursor");
  156.  
  157. /****** OPEN CURSOR *********/
  158.      ftime (&dbeg);
  159.     EXEC SQL OPEN CDYN;
  160.      ftime(&dend);
  161.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  162.  //   get_error( &sqlca, " open cursor");
  163.  
  164. /****** FETCH TILL NO MORE ROWS LEFT *********/
  165.     while (sqlca.sqlcode == 0) {
  166.  
  167.      ftime (&dbeg);
  168.        EXEC SQL FETCH CDYN
  169.           INTO :st_cu_ssn, :st_cu_name, :s_bal, :c_accnumb;
  170.      ftime(&dend);
  171.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  172.      if ((sqlca.sqlcode == 0) ) num_rows++;
  173.     }/*end while more rows */
  174.  
  175.  ext:
  176. /****** CLOSE CURSOR *********/
  177.      ftime (&dbeg);
  178.    EXEC SQL CLOSE CDYN;
  179.      ftime(&dend);
  180.      q2time += delta(dbeg.time, dend.time, dbeg.millitm, dend.millitm);
  181. /*     printf("\ndyn time = %7.3f", q2time);
  182. */
  183.  
  184.     return(0);
  185.  
  186.  error:
  187.  error_sql = sqlca.sqlcode; // copy the sql code into external variable
  188.                             // to be printed on user screen by dbdemo
  189.  return(-1);
  190. }/* end dyn */
  191.  
  192. /************************************************************************/
  193. /* FUNCTION  stat_dyn()                                                 */
  194. /************************************************************************/
  195.  
  196.  
  197. short far stat_dyn()
  198. {
  199.  short i;
  200.   q1avg=0.0;
  201.   q2avg=0.0;
  202.  
  203.     // SET THE NUMBER OF DEMOS FOR THIS SECTION 1 OR 2.. 2 IS MAX!!!!
  204.        Timetext[0].test_num = 2;
  205.     for  (i=1; i<=num_runs; i++)
  206.         {
  207.             if (stat())
  208.                return(3);
  209.             if (i!=1)
  210.                q1avg =q1avg + q1time;
  211.         }/*end for */
  212.     if (num_runs==1)
  213.        q1avg = q1time;
  214.     else
  215.        q1avg = q1avg/(num_runs-1);
  216.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 1
  217.        strcpy(Timetext[0].demoname,"Static SQL    ");
  218.        Timetext[0].demotime = q1avg;
  219.        Timetext[0].rows = num_rows;
  220.  
  221.     for  (i=1; i<=num_runs; i++)
  222.         {
  223.             if (dyn())
  224.                return(3);
  225.         if (i!=1)
  226.                q2avg = q2avg+ q2time;
  227.         }/*end for */
  228.     if (num_runs==1)
  229.        q2avg = q2time;
  230.     else
  231.        q2avg = q2avg/(num_runs-1);
  232.  
  233.     // FILL UP TIMETEXT STRUCTURE FOR DEMO 2
  234.        strcpy(Timetext[1].demoname,"Dynamic SQL   ");
  235.        Timetext[1].demotime = q2avg;
  236.        Timetext[1].rows = num_rows;
  237.  
  238.  return(0);
  239. }/* end main */
  240.