home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / rsql.zip / RSQL.SQC < prev   
Text File  |  1994-01-13  |  52KB  |  1,224 lines

  1. /*****************************************************************************/
  2. /*  PROGRAM NAME:  RSQL.SQC          -- Run SQL Program                      */
  3. /*                                                                           */
  4. /*  DESCRIPTION:  This program allows a user to run a batch file consisting  */
  5. /*    of SQL and Database Administrator commands against an OS/2 database.   */
  6. /*    The script is submitted to RSQL in a command file and the results      */
  7. /*    appear in a response file.  RSQL runs as an OS/2 task and does not     */
  8. /*    require the services of Query Manager.                                 */
  9. /*                                                                           */
  10. /*  PROGRAM INVOCATION:                                                      */
  11. /*                                                                           */
  12. /*    rsql cmd_file resp_file                                                */
  13. /*                                                                           */
  14. /*         cmd_file  = run file that contains the user script                */
  15. /*         resp_file = response file that contains the output                */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18. #include <process.h>                        /* For exit()                    */
  19. #include <stdio.h>                          /* for printf() and NULL         */
  20. #include <stdlib.h>                         /* for printf()                  */
  21. #include <string.h>                         /* for printf()                  */
  22. #include <sys\types.h>                      /* for _ftime()                  */
  23. #include <sys\timeb.h>                      /* for _ftime()                  */
  24. #include <io.h>                             /* for file I/O                  */
  25. #include <conio.h>                          /* for getch and kbhit           */
  26. #include <fcntl.h>                          /* for file I/O                  */
  27.  
  28. #define INCL_DOSMEMMGR
  29. #define INCL_DOSPROCESS
  30. #include <os2.h>
  31.  
  32. #include <sqlca.h>                          /* for sqlca                     */
  33. #include <sql.h>                            /* for error msg retrieval       */
  34. #include <sqlcodes.h>                       /* for SQL return code constants */
  35. #include <sqlenv.h>                         /* for environment functions     */
  36. #include <sqlutil.h>                        /* for utility functions         */
  37. #include <upm.h>                            /* for logon/logoff functions    */
  38. struct sqlca sqlca;                         /* for DB mgr return codes       */
  39.  
  40. #include "rsql.h"
  41. #pragma stack16(8192)
  42.  
  43. /*___________________________________________________________________________*/
  44. /* Main function                                                             */
  45. /*___________________________________________________________________________*/
  46. int main(int argc, char **argv)
  47. {
  48.    SHORT cmd_type;
  49.    SHORT ch;
  50.  
  51.    initialize_rsql();
  52.    check_args_and_prepare_files(argc, argv);
  53.  
  54.    /*_____________________________________________*/
  55.    /* Process commands one at a time              */
  56.    /*_____________________________________________*/
  57.    while (!feof(cmd_stream))
  58.     /*_____________________________________________*/
  59.     /* get next command                            */
  60.     /*_____________________________________________*/
  61.     {
  62.      cmd_type = get_command(parm_buf);
  63.      /*_____________________________________________*/
  64.      /* Initialize timer                            */
  65.      /*_____________________________________________*/
  66.      reset_timer();
  67.      start_timer();
  68.      /*_____________________________________________*/
  69.      /* Execute the command                         */
  70.      /*_____________________________________________*/
  71.      switch (cmd_type) {
  72.                        /*_____________________________________________*/
  73.                        /* Dynamic SQL commands                        */
  74.                        /*_____________________________________________*/
  75.      case CMD_SQL_IMMEDIATE         : rcd = sql_immediate(parm_buf);     break;
  76.      case CMD_SQL_SELECT            : rcd = sql_select(parm_buf);        break;
  77.                        /*_____________________________________________*/
  78.                        /* Execute an OS/2 program                     */
  79.                        /*_____________________________________________*/
  80.      case CMD_DOS                   : rcd = dos_exec(parm_buf);          break;
  81.                        /*_____________________________________________*/
  82.                        /* Database Administration commands            */
  83.                        /*_____________________________________________*/
  84.      case CMD_DBA                   : rcd = dba_exec(parm_buf);          break;
  85.                        /*_____________________________________________*/
  86.                        /* RSQL execution controls                     */
  87.                        /*_____________________________________________*/
  88.      case CMD_DBA_CONTINUE_ON_ERROR : rcd = RECORD_NOTHING;
  89.                                       continue_on_error = TRUE;          break;
  90.      case CMD_DBA_STOP_ON_ERROR     : rcd = RECORD_NOTHING;
  91.                                       continue_on_error = FALSE;         break;
  92.      case CMD_DBA_PAUSE             : rcd = RECORD_NOTHING;
  93.                                       printf("\nPausing! Press any key "
  94.                                              "to continue...");
  95.                                       while (!kbhit());
  96.                                       ch = getch();
  97.                                       break;
  98.      case CMD_SYNTAX_ERROR          : rcd = RECORD_ERR_MSG;              break;
  99.      case CMD_NO_CMD                : rcd = RECORD_NOTHING;              break;
  100.      default: break;
  101.      } /* endswitch */
  102.      /*_____________________________________________*/
  103.      /* Stop the timer, record the results,         */
  104.      /* terminate command processing if error       */
  105.      /*_____________________________________________*/
  106.      stop_timer();
  107.      if (record_command_results(rcd) == TERMINATE)
  108.         break;
  109.  
  110.    } /* endwhile */
  111.  
  112.    terminate_program();
  113.  
  114. }  /* end main */
  115.  
  116.  
  117. /*___________________________________________________________________________*/
  118. /* Perform required program initialization                                   */
  119. /*___________________________________________________________________________*/
  120. VOID initialize_rsql(VOID)
  121. {
  122.      /*_____________________________________________*/
  123.      /* Allocate an SQLDA                           */
  124.      /*_____________________________________________*/
  125.      sqlda = (struct sqlda *)malloc(SQLDASIZE(255));
  126.  
  127.      /*_____________________________________________*/
  128.      /* Enable the exit list procedure              */
  129.      /*_____________________________________________*/
  130.      DosExitList(1,                       /* 1 = Add to exit list */
  131.                 (PFNEXITLIST) exit_proc);  /* pointer to procedure */
  132.  
  133. } /* end initialize_rsql */
  134.  
  135.  
  136. /*___________________________________________________________________________*/
  137. /* Check for the right number of program arguments and open files            */
  138. /*___________________________________________________________________________*/
  139. VOID check_args_and_prepare_files(USHORT argc, PCHAR argv[])
  140. {
  141.      /*_____________________________________________*/
  142.      /* Are the required parameters there?          */
  143.      /*_____________________________________________*/
  144.      if (argc < 3) {
  145.         fprintf(stderr,"\nCalling syntax is: rsql run_file resp_file");
  146.         exit(1);
  147.      } /* endif */
  148.      /*_____________________________________________*/
  149.      /* Open the RUN and RESPONSE files             */
  150.      /*_____________________________________________*/
  151.      if (NULL == (cmd_stream = fopen(argv[1], "r"))) {
  152.         printf("\nError opening the RUN file : %s", argv[1]);
  153.         exit(1);
  154.      } /* endif */
  155.      if (NULL == (out_stream = fopen(argv[2], "w"))) {
  156.         printf("\nError opening the RESPONSE file : %s", argv[2]);
  157.         exit(1);
  158.      } /* endif */
  159.  
  160. } /* end check_args_and_prepare_file */
  161.  
  162.  
  163.  
  164. /*___________________________________________________________________________*/
  165. /* Cleanup and terminate the program                                         */
  166. /*___________________________________________________________________________*/
  167. VOID terminate_program(VOID)
  168. {
  169.    free(sqlda);
  170.    fclose(cmd_stream);
  171.    fclose(out_stream);
  172. } /* end terminate_program */
  173.  
  174.  
  175.  
  176. /*___________________________________________________________________________*/
  177. /* This procedure displays how the program terminated and performs cleanup   */
  178. /* if abnormal termination.                                                  */
  179. /*___________________________________________________________________________*/
  180. VOID exit_proc(ULONG term_code)
  181. {
  182.    /*_____________________________________________*/
  183.    /* Determine termination cause                 */
  184.    /*_____________________________________________*/
  185.    switch (term_code) {
  186.    case  0:
  187.       printf("\n**program terminated normally**\n");
  188.       break;
  189.    case  1:
  190.       printf("\n**program terminated by hard error**\n");
  191.       break;
  192.    case  2:
  193.       fclose(cmd_stream);
  194.       fclose(out_stream);
  195.       printf("\n**program terminated by a trap**\n");
  196.       break;
  197.    case  3:
  198.       fclose(cmd_stream);
  199.       fclose(out_stream);
  200.       printf("\n**program terminated by a DOSKillProcess**\n");
  201.       break;
  202.    default:
  203.       printf("\n**unknown program termination**\n");
  204.    } /* endswitch */
  205.    /*_____________________________________________*/
  206.    /* Allow next exit list                        */
  207.    /*_____________________________________________*/
  208.    DosExitList(3,                                  /* 3 allows next exit to run */
  209.                (void far *) 0);                    /* null pointer              */
  210. } /* end exit_proc */
  211.  
  212.  
  213.  
  214. /*___________________________________________________________________________*/
  215. /* This procedure determines the command type                                */
  216. /*___________________________________________________________________________*/
  217. SHORT get_command(PCHAR parm_str)
  218. {
  219.   char cmd[MAX_CMD_SIZE];          /* temp buffer for cmd processing */
  220.   char token1[5];                  /* token to receive EXEC          */
  221.   char token2[5];                  /* token to receive DBA: or SQL:  */
  222.   char token3[25];                 /* token to receive cmd name      */
  223.   char token4[MAX_CMD_SIZE];       /* token to receive parms         */
  224.   char comment_line[MAX_CMD_SIZE]; /* comment line temp variable     */
  225.   char delim1,delim2;              /* variables for sscanf           */
  226.  
  227.   /*_____________________________________________*/
  228.   /* Initialize cmds table                       */
  229.   /*_____________________________________________*/
  230.   typedef struct
  231.   {
  232.     CHAR    cmd_name[40];            /*  cmd name                   */
  233.     CHAR    cmd_num;                 /* numerical representation    */
  234.     USHORT  num_parms;               /* no. of parms for this cmd   */
  235.   }   CMD_TYPE;
  236.  
  237.   static CMD_TYPE cmds[] =
  238.   {/*<=== cmd_name ---><-- cmd_num -----------------><-- num_parms -->*/
  239.    {"BACKUP",           CMD_DBA_BACKUP             ,  3               },
  240.    {"BIND",             CMD_DBA_BIND               ,  6               },
  241.    {"CATALOG_DB",       CMD_DBA_CATALOG_DB         ,  6               },
  242.    {"CATALOG_NODE",     CMD_DBA_CATALOG_NODE       ,  4               },
  243.    {"COLLECT_STATUS",   CMD_DBA_COLLECT_STATUS     ,  1               },
  244.    {"CONTINUE_ON_ERROR",CMD_DBA_CONTINUE_ON_ERROR  ,  0               },
  245.    {"CREATE_DB",        CMD_DBA_CREATE_DB          ,  3               },
  246.    {"DROP_DB",          CMD_DBA_DROP_DB            ,  1               },
  247.    {"EXPORT",           CMD_DBA_EXPORT             ,  4               },
  248.    {"GET_ADMIN",        CMD_DBA_GET_ADMIN          ,  0               },
  249.    {"IMPORT",           CMD_DBA_IMPORT             ,  5               },
  250.    {"LOGOFF",           CMD_DBA_LOGOFF             ,  3               },
  251.    {"LOGON",            CMD_DBA_LOGON              ,  5               },
  252.    {"PAUSE",            CMD_DBA_PAUSE              ,  0               },
  253.    {"REORG",            CMD_DBA_REORG              ,  3               },
  254.    {"RESET_DM_CFG",     CMD_DBA_RESET_DM_CFG       ,  0               },
  255.    {"RESET_DB_CFG",     CMD_DBA_RESET_DB_CFG       ,  1               },
  256.    {"RESTORE",          CMD_DBA_RESTORE            ,  3               },
  257.    {"ROLL_FORWARD",     CMD_DBA_ROLL_FORWARD       ,  3               },
  258.    {"RUNSTATS",         CMD_DBA_RUNSTATS           ,  1               },
  259.    {"SHOW_DB_CFG",      CMD_DBA_SHOW_DB_CFG        ,  1               },
  260.    {"SHOW_DB_DIR",      CMD_DBA_SHOW_DB_DIR        ,  1               },
  261.    {"SHOW_DM_CFG",      CMD_DBA_SHOW_DM_CFG        ,  0               },
  262.    {"SHOW_NODE_DIR",    CMD_DBA_SHOW_NODE_DIR      ,  0               },
  263.    {"STARTDM",          CMD_DBA_STARTDM            ,  0               },
  264.    {"STARTUSE",         CMD_DBA_STARTUSE           ,  2               },
  265.    {"STOPDM",           CMD_DBA_STOPDM             ,  0               },
  266.    {"STOP_ON_ERROR",    CMD_DBA_STOP_ON_ERROR      ,  0               },
  267.    {"STOPUSE",          CMD_DBA_STOPUSE            ,  0               },
  268.    {"UNCATALOG_DB",     CMD_DBA_UNCATALOG_DB       ,  1               },
  269.    {"UNCATALOG_NODE",   CMD_DBA_UNCATALOG_NODE     ,  1               },
  270.    {"UPDATE_DB_CFG",    CMD_DBA_UPDATE_DB_CFG      ,  2               },
  271.    {"UPDATE_DM_CFG",    CMD_DBA_UPDATE_DM_CFG      ,  0               },
  272.   };
  273.   USHORT      cmd_not_found;     /* True if a match is found          */
  274.   USHORT      i;                 /* Loop index variable               */
  275.   PCHAR       char_ptr;          /* used to remove CRLF from SQL stmts*/
  276.   /*_____________________________________________*/
  277.   /* Read a command from the RUN file            */
  278.   /*_____________________________________________*/
  279.    num_parms = fscanf(cmd_stream," %[^;]%*c ", cmd);
  280.    if (num_parms == EOF) {
  281.       return(CMD_NO_CMD);                       /* empty file          */
  282.    strcat(cmd,";");                             /* maintain cmd delim. */
  283.    } /* endif */
  284.    /*_____________________________________________*/
  285.    /* Echo command to the response file           */
  286.    /*_____________________________________________*/
  287.    fprintf(out_stream, "%s\n", cmd);
  288.  
  289.    /*_____________________________________________*/
  290.    /* Remove any comment lines                    */
  291.    /*_____________________________________________*/
  292.    while ((cmd[0] == '-') && (cmd[1] == '-')) {
  293.       num_parms = sscanf(cmd,"%[^\n;]%c %[^;]%c ",
  294.                   comment_line, &delim1, cmd, &delim2);  /* remove comment line */
  295.       if (delim1 == ';') {
  296.          return (CMD_NO_CMD);                            /* maintain cmd delim */
  297.       } /* endif */
  298.    } /* endwhile */
  299.  
  300.    /*_____________________________________________*/
  301.    /* Parse the command and make some validity    */
  302.    /* checks                                      */
  303.    /*_____________________________________________*/
  304.    num_parms = sscanf(cmd, " %4s %4s %24s %[^;]",
  305.                          token1, token2, token3, token4);
  306.  
  307.    if (num_parms == 3) {                                 /* no parms, parse accordingly */
  308.       num_parms = sscanf(cmd, " %4s %4s %24[^;]",
  309.                          token1, token2, token3);
  310.       strcpy(token4,"");
  311.    } /* endif */
  312.  
  313.    if ((num_parms <3)                      ||
  314.        (strcmp(token1,"EXEC") != 0)        ||
  315.        ((strcmp(token2,"DBA:") != 0)       &&
  316.         (strcmp(token2,"DOS:") != 0)       &&
  317.         (strcmp(token2,"SQL:") != 0)))     {
  318.       sprintf(err_msg,
  319.       "\nSYNTAX ERROR: Expected EXEC (SQL or DBA etc...) to begin a command.");
  320.       return(CMD_SYNTAX_ERROR);
  321.    } /* endif */
  322.  
  323.    /*_____________________________________________*/
  324.    /* SQL CMD, return command type,               */
  325.    /* remove EXEC SQL                             */
  326.    /*_____________________________________________*/
  327.      if (strcmp(token2,"SQL:") == 0) {
  328.         char_ptr = strpbrk(token4,"\n\r");              /* remove CRLF chars */
  329.         while (char_ptr != NULL) {
  330.            char_ptr[0] = ' ';
  331.            char_ptr = strpbrk(char_ptr,"\n\r");
  332.         } /* endwhile */
  333.         sprintf(parm_str, "%s %s", token3, token4);     /* return the cmd for */
  334.         if (strcmp(strupr(token3),"SELECT") == 0) {     /* for processing     */
  335.            return(CMD_SQL_SELECT);
  336.         } else {
  337.            return(CMD_SQL_IMMEDIATE);
  338.         } /* endif */
  339.      } /* endif */
  340.  
  341.    /*_____________________________________________*/
  342.    /* DOS CMD, return command type,               */
  343.    /* remove EXEC DOS                             */
  344.    /*_____________________________________________*/
  345.    if (strcmp(token2,"DOS:") == 0) {
  346.       sprintf(parm_str, "%s%c%s", token3, 0, token4);   /* return the cmd     */
  347.                                                         /* in the form for    */
  348.                                                         /* DOSExecPgm         */
  349.       return(CMD_DOS);
  350.    } /* endif */
  351.  
  352.    /*_____________________________________________*/
  353.    /* DBA CMD, return command type,               */
  354.    /* remove EXEC DBA: cmd_name"                  */
  355.    /*_____________________________________________*/
  356.    i=0;
  357.    cmd_not_found = TRUE;
  358.  
  359.    while (cmd_not_found && (i < CMD_LAST_CMD)) {
  360.       cmd_not_found = strcmp(cmds[i].cmd_name,token3);
  361.       if (!cmd_not_found) {
  362.          break;
  363.       } else {
  364.          i++;
  365.       } /* endif */
  366.    } /* endwhile */
  367.  
  368.    if (cmd_not_found) {
  369.       sprintf(err_msg,
  370.       "\nSYNTAX ERROR: Unknown Command. ");
  371.       return(CMD_SYNTAX_ERROR);
  372.    } /* endif */
  373.  
  374.    if ((cmds[i].cmd_num == CMD_DBA_CONTINUE_ON_ERROR) ||
  375.        (cmds[i].cmd_num == CMD_DBA_STOP_ON_ERROR) ||
  376.        (cmds[i].cmd_num == CMD_DBA_PAUSE))  {
  377.       return(cmds[i].cmd_num);
  378.    } /* endif */
  379.  
  380.    sprintf(parm_str, "%s", token4);
  381.    strcpy(parms.cmd_name,cmds[i].cmd_name);
  382.    parms.cmd_num = cmds[i].cmd_num;
  383.    parms.num_parms = cmds[i].num_parms;
  384.    return(CMD_DBA);
  385.  
  386. } /* end get_command */
  387.  
  388.  
  389.  
  390. /*___________________________________________________________________________*/
  391. /* This procedure initializes the time variable                              */
  392. /*___________________________________________________________________________*/
  393. VOID  reset_timer(VOID)
  394. {
  395.    event_time = 0.0;
  396.    start_time = 0.0;
  397.    end_time   = 0.0;
  398. } /* end reset_timer */
  399.  
  400.  
  401.  
  402. /*___________________________________________________________________________*/
  403. /* This procedure records the time at the start of an interval               */
  404. /*___________________________________________________________________________*/
  405. VOID  start_timer(VOID)
  406. {
  407.    _ftime(&timebuff);
  408.    start_time=(double)timebuff.time+((double)timebuff.millitm)/(double)1000;
  409. } /* end start_timer */
  410.  
  411.  
  412.  
  413. /*___________________________________________________________________________*/
  414. /* This procedure records the time at the end of an interval                 */
  415. /* and calculates the elapsed time for an event.                             */
  416. /*___________________________________________________________________________*/
  417. VOID  stop_timer(VOID)
  418. {
  419.    _ftime(&timebuff);
  420.    end_time=(double)timebuff.time+((double)timebuff.millitm)/(double)1000;
  421.    event_time = event_time + (end_time - start_time);
  422.  
  423. } /* end stop_timer */
  424.  
  425.  
  426.  
  427.  
  428. /*___________________________________________________________________________*/
  429. /* This procedure executes an SQL command dynamically (non SELECT)           */
  430. /*___________________________________________________________________________*/
  431. SHORT sql_immediate(PCHAR cmd_sql)
  432. {
  433.    /*_____________________________________________*/
  434.    /* Declare cmd to DB2/2                        */
  435.    /*_____________________________________________*/
  436.    EXEC SQL BEGIN DECLARE SECTION;
  437.       char     *  cmd;
  438.    EXEC SQL END DECLARE SECTION;
  439.    cmd = cmd_sql;
  440.    /*_____________________________________________*/
  441.    /* Execute cmd                                 */
  442.    /*_____________________________________________*/
  443.    EXEC SQL EXECUTE IMMEDIATE :cmd;
  444.  
  445.    return (RECORD_SQLCA);
  446. } /* end sql_immediate */
  447.  
  448.  
  449.  
  450.  
  451. /*___________________________________________________________________________*/
  452. /* This procedure executes an SQL command dynamically (SELECT)               */
  453. /*___________________________________________________________________________*/
  454. SHORT sql_select(PCHAR cmd)
  455. {
  456.    SHORT    index, i;                           /* for counting                  */
  457.    SHORT    num_of_rows;                        /* number of rows selected       */
  458.    SHORT    num_brk;                            /* number of rows since break    */
  459.    SHORT    byte_count;                         /* number of bytes to allocate   */
  460.    /*_____________________________________________*/
  461.    /* Declare cmd to DB2/2                        */
  462.    /*_____________________________________________*/
  463.    EXEC SQL BEGIN DECLARE SECTION;
  464.       char     *  cmd_sql;                      /* command buffer                */
  465.    EXEC SQL END DECLARE SECTION;
  466.  
  467.    cmd_sql     = cmd;
  468.    num_of_rows = 0;
  469.    num_brk     = 0;
  470.  
  471.    /*_____________________________________________*/
  472.    /* Initialize the SQLDA                        */
  473.    /*_____________________________________________*/
  474.    strncpy(sqlda->sqldaid,"SQLDA   ",8);
  475.    sqlda->sqldabc = (long) SQLDASIZE(255);
  476.    sqlda->sqln = SQLVAR_NUM;
  477.    sqlda->sqld = 0;
  478.  
  479.    /*_____________________________________________*/
  480.    /* Prepare cmd                                 */
  481.    /*_____________________________________________*/
  482.    EXEC SQL PREPARE st into :*sqlda FROM :cmd_sql;
  483.    if (SQLCODE != 0) {
  484.       return(RECORD_SQLCA);
  485.    } /* endif */
  486.  
  487.    /*_____________________________________________*/
  488.    /* Stop timer                                  */
  489.    /*_____________________________________________*/
  490.    stop_timer();
  491.  
  492.    /*_____________________________________________*/
  493.    /* Allocate space for select variables         */
  494.    /*_____________________________________________*/
  495.    for (index=0;index<sqlda->sqld ;index++ ) {
  496.       if ((sqlda->sqlvar[index].sqltype == SQL_TYP_DECIMAL) ||
  497.           (sqlda->sqlvar[index].sqltype == SQL_TYP_NDECIMAL))  {
  498.          byte_count = (LOBYTE(sqlda->sqlvar[index].sqllen) + 2)/2;
  499.       } else {
  500.          byte_count = sqlda->sqlvar[index].sqllen;
  501.       } /* endif */
  502.  
  503.       sqlda->sqlvar[index].sqldata = (unsigned char *)malloc(byte_count);
  504.       if (sqlda->sqlvar[index].sqldata == NULL) {
  505.          fprintf(out_stream,
  506.          "\nOut of dynamic memory while space for select column %d",index+1);
  507.          return(RECORD_ERR_MSG);
  508.       } /* endif */
  509.  
  510.       /*_____________________________________________*/
  511.       /* Allocate space for null indicators          */
  512.       /*_____________________________________________*/
  513.       if (sqlda->sqlvar[index].sqltype & 1) {         /* odd   */
  514.          sqlda->sqlvar[index].sqlind = (short *)malloc(2);
  515.          if (sqlda->sqlvar[index].sqldata == NULL) {
  516.             fprintf(out_stream,
  517.             "\nOut of dynamic memory while space for NULL column %d", index+1);
  518.             return(RECORD_ERR_MSG);
  519.          } /* endif */
  520.       } /* endif */
  521.    } /* endfor */
  522.  
  523.    rcd = print_format_row(SET_TABS_WIDTH);
  524.    if (rcd != 0) {
  525.       return(RECORD_ERR_MSG);
  526.    } /* endif */
  527.  
  528.    rcd = print_format_row(PRINT_COLUMNS);
  529.    if (rcd != 0) {
  530.       return(RECORD_ERR_MSG);
  531.    } /* endif */
  532.  
  533.    /*_____________________________________________*/
  534.    /* Start timer                                 */
  535.    /*_____________________________________________*/
  536.  
  537.    start_timer();
  538.  
  539.    EXEC SQL DECLARE cur CURSOR for st;
  540.    if (SQLCODE != 0) {
  541.       return(RECORD_SQLCA);
  542.    } /* endif */
  543.  
  544.    EXEC SQL OPEN cur;
  545.    if (SQLCODE != 0) {
  546.       return(RECORD_SQLCA);
  547.    } /* endif */
  548.  
  549.    /*_____________________________________________*/
  550.    /* Handle data one row at a time               */
  551.    /*_____________________________________________*/
  552.    EXEC SQL FETCH cur USING DESCRIPTOR :*sqlda;
  553.    if ((SQLCODE !=0) && (SQLCODE != SQL_RC_W100)) {
  554.       return(RECORD_SQLCA);
  555.    } /* endif */
  556.  
  557.    while (SQLCODE == 0l) {
  558.       stop_timer();
  559.       num_of_rows++;
  560.       if (num_brk++ > BREAK_SEP) {
  561.          for (i=0;i<BREAK_ROWS ;i++ ) {
  562.             fputs("\n",out_stream);
  563.          } /* endfor */
  564.          print_format_row(PRINT_COLUMNS);
  565.          num_brk = 0;
  566.       } /* endif */
  567.       rcd=print_format_row(PRINT_SELECTED_ROW);
  568.       if (rcd != 0) {
  569.          return(RECORD_ERR_MSG);
  570.       } else {
  571.          start_timer();
  572.          EXEC SQL FETCH cur USING DESCRIPTOR :*sqlda;
  573.          if ((SQLCODE !=0) && (SQLCODE != SQL_RC_W100)) {
  574.             return(RECORD_SQLCA);
  575.          } /* endif */
  576.       } /* endif */
  577.    } /* endwhile */
  578.  
  579.    EXEC SQL CLOSE cur;
  580.    if (SQLCODE != 0) {
  581.       return(RECORD_SQLCA);
  582.    } /* endif */
  583.  
  584.    /*_____________________________________________*/
  585.    /* Stop timer                                  */
  586.    /*_____________________________________________*/
  587.  
  588.    stop_timer();
  589.    fprintf(out_stream,"\n\n Total Number of Rows SELECTED = %d\n",num_of_rows);
  590.  
  591.    /*_____________________________________________*/
  592.    /* Free space for select variables             */
  593.    /*_____________________________________________*/
  594.    for (index=0;index < sqlda->sqld ;index++ ) {
  595.       free(sqlda->sqlvar[index].sqldata);
  596.  
  597.    /*_____________________________________________*/
  598.    /* Free space for null indicators              */
  599.    /*_____________________________________________*/
  600.       if (sqlda->sqlvar[index].sqltype & 1) {                  /*   odd    */
  601.          free(sqlda->sqlvar[index].sqlind);
  602.       } /* endif */
  603.    } /* endfor */
  604.  
  605.    return(RECORD_SQLCA);
  606.  
  607. } /* end sql_select */
  608.  
  609.  
  610.  
  611.  
  612. /*___________________________________________________________________________*/
  613. /*  This procedure performs the requested format actions:                    */
  614. /*                                                                           */
  615. /*  SET_TABS_WIDTH calculates the tab position for each field in a query     */
  616. /*  PRINT_COLUMNS  formats one row of columns using the tabs and sqlda       */
  617. /*  PRINT_SELECTED_ROW                                                       */
  618. /*                 formats one SELECT row using the tabs and sqlda           */
  619. /*___________________________________________________________________________*/
  620. SHORT print_format_row(USHORT action)
  621. {
  622.    #define      MAX_FLDS     50              /* for this exercise             */
  623.    SHORT        index, i, y;                 /*  for counting                 */
  624.    CHAR         row[MAX_LINE_SIZE+1];        /* row to be printed             */
  625.    CHAR         temp[MAX_LINE_SIZE+10];      /* temp buffer                   */
  626.    CHAR         temp1[100];                  /* 2nd temp buffer               */
  627.    typedef struct
  628.    {
  629.       SHORT    tab_pos;                      /* column position               */
  630.       SHORT    max_width;                    /* max_width str width of type   */
  631.    }  FLD_TYPE;
  632.    static FLD_TYPE   field[MAX_FLDS];        /* contains field descriptors    */
  633.    SHORT          col_name_width;            /* width of col name             */
  634.    SHORT          col_fld_width;             /* width of col fld              */
  635.    static SHORT   max_num_flds;              /* number of printable columns   */
  636.    SHORT          null_not_found;            /* a flag                        */
  637.    USHORT         top, precision, scale;     /* for decimal fields            */
  638.    SHORT          bottom, point;             /* for decimal fields            */
  639.    SHORT          len;                       /* for decimal fields            */
  640.    UCHAR          *ptr;                      /* for decimal fields            */
  641.    BOOL           output_blank;              /* for decimal fields            */
  642.  
  643.    /*_____________________________________________*/
  644.    /* Initialize                                  */
  645.    /*_____________________________________________*/
  646.    memset(row,' ',sizeof(row));
  647.    row[sizeof(row) - 1] = '\0';
  648.    i = 0;
  649.    rcd = GOOD;
  650.  
  651.    /*_____________________________________________*/
  652.    /* Do action                                   */
  653.    /*_____________________________________________*/
  654.    switch (action) {
  655.    case  SET_TABS_WIDTH:
  656.       field[0].tab_pos = 1;                  /* Start with column 1           */
  657.       for (index=0;index < sqlda->sqld ;index++ ) {
  658.          col_name_width = sqlda->sqlvar[index].sqlname.length;
  659.          col_fld_width  = sqlda->sqlvar[index].sqllen;
  660.  
  661.          switch (sqlda->sqlvar[index].sqltype) {
  662.          case SQL_TYP_INTEGER:
  663.          case SQL_TYP_NINTEGER:
  664.          case SQL_TYP_FLOAT:
  665.          case SQL_TYP_NFLOAT:
  666.             col_fld_width = 11;
  667.             break;
  668.          case SQL_TYP_SMALL:
  669.          case SQL_TYP_NSMALL:
  670.             col_fld_width = 6;
  671.             break;
  672.          case SQL_TYP_DECIMAL:
  673.          case SQL_TYP_NDECIMAL:
  674.             col_fld_width = LOBYTE(sqlda->sqlvar[index].sqllen) + 1;
  675.                                              /* precision + decimal point     */
  676.             break;
  677.          case SQL_TYP_DATE:
  678.          case SQL_TYP_NDATE:
  679.             col_fld_width=10;
  680.             break;
  681.          case SQL_TYP_TIME:
  682.          case SQL_TYP_NTIME:
  683.             col_fld_width=8;
  684.             break;
  685.          case SQL_TYP_STAMP:
  686.          case SQL_TYP_NSTAMP:
  687.             col_fld_width=26;
  688.             break;
  689.          default:
  690.             break;
  691.          } /* endswitch */
  692.          /*_____________________________________________*/
  693.          /* Use the larger of col_name or col_fld       */
  694.          /*_____________________________________________*/
  695.          if (col_name_width > col_fld_width) {
  696.             field[index].max_width = col_name_width;
  697.          } else {
  698.             field[index].max_width = col_fld_width;
  699.          } /* endif */
  700.          /*_____________________________________________*/
  701.          /* Update tab field 1 space between columns    */
  702.          /*_____________________________________________*/
  703.          field[index+1].tab_pos = field[index].tab_pos +
  704.                                     field[index].max_width +
  705.                                     COL_SEP;
  706.          /*_____________________________________________*/
  707.          /* Check Limits                                */
  708.          /*_____________________________________________*/
  709.          if (field[index+1].tab_pos > MAX_LINE_SIZE) {
  710.             max_num_flds = index;
  711.             break;                           /*  out of for loop              */
  712.          } else {
  713.             max_num_flds = index + 1;
  714.          } /* endif */
  715.  
  716.          if (max_num_flds > MAX_FLDS) {
  717.             break;                           /*  out of for loop              */
  718.          } /* endif */
  719.  
  720.       } /* endfor */
  721.  
  722.       break;                                 /* end of action SET_TABS_WIDTH  */
  723.  
  724.    case PRINT_COLUMNS:
  725.       /*_____________________________________________*/
  726.       /* Print column names                          */
  727.       /*_____________________________________________*/
  728.       for (index=0;index < max_num_flds ;index++ ) {
  729.          memcpy(&row[field[index].tab_pos],
  730.                 sqlda->sqlvar[index].sqlname.data,
  731.                 sqlda->sqlvar[index].sqlname.length);
  732.       } /* endfor */
  733.       fprintf(out_stream,"\n%s",row);
  734.  
  735.       /*_____________________________________________*/
  736.       /* Clear row buffer                            */
  737.       /*_____________________________________________*/
  738.       memset(row,' ',sizeof(row));
  739.       row[sizeof(row) +1] = '\0';
  740.  
  741.       /*_____________________________________________*/
  742.       /* Underline names                             */
  743.       /*_____________________________________________*/
  744.       row[sizeof(row) -1] = '\0';
  745.       for (index=0;index<max_num_flds ;index++ ) {
  746.          memset(temp,'-',field[index].max_width);
  747.          memcpy(&row[field[index].tab_pos], temp, field[index].max_width);
  748.       } /* endfor */
  749.       fprintf(out_stream,"\n%s",row);
  750.  
  751.       break;                                 /* end of action PRINT_COLUMNS   */
  752.  
  753.    case PRINT_SELECTED_ROW:
  754.       for (index=0;index<max_num_flds ;index++ ) {
  755.          /*_____________________________________________*/
  756.          /* Initialize                                  */
  757.          /*_____________________________________________*/
  758.          null_not_found = TRUE;
  759.          /*_____________________________________________*/
  760.          /* Is it null?                                 */
  761.          /*_____________________________________________*/
  762.          if (sqlda->sqlvar[index].sqltype & 1) {
  763.             if (* (sqlda->sqlvar[index].sqlind) == -1) {
  764.                sprintf(temp,"NULL");
  765.                null_not_found = FALSE;
  766.             } else {
  767.                if (* (sqlda->sqlvar[index].sqlind) != 0) {
  768.                   sprintf(err_msg,"\n Truncated field col = %d",index+1);
  769.                   rcd=FAILED;
  770.                   break;
  771.                } /* endif */
  772.             } /* endif */
  773.          } /* endif */
  774.          /*_____________________________________________*/
  775.          /* Is not null field                           */
  776.          /*_____________________________________________*/
  777.          if (null_not_found) {
  778.             switch (sqlda->sqlvar[index].sqltype) {
  779.             case SQL_TYP_INTEGER:
  780.             case SQL_TYP_NINTEGER:
  781.                sprintf(temp,"%ld", *(PLONG)sqlda->sqlvar[index].sqldata);
  782.                break;
  783.             case SQL_TYP_SMALL:
  784.             case SQL_TYP_NSMALL:
  785.                sprintf(temp,"%d", *(PSHORT)sqlda->sqlvar[index].sqldata);
  786.                break;
  787.             case SQL_TYP_DECIMAL:
  788.             case SQL_TYP_NDECIMAL:
  789.                len = sqlda->sqlvar[index].sqllen;
  790.                ptr = sqlda->sqlvar[index].sqldata;
  791.                sprintf(temp,"");
  792.                scale     = HIBYTE(len);
  793.                precision = LOBYTE(len);
  794.                y = (precision+2)/2;          /* total number of bytes         */
  795.                point = precision - scale;
  796.                bottom = *(ptr + y - 1) & 0x000F;    /* sign                    */
  797.                if ((bottom == 0x000D) || (bottom == 0x000B)) {
  798.                   strcat(temp,"-");
  799.                } /* endif */
  800.                output_blank = TRUE;
  801.                for (i=0;i<y ;i++ ) {
  802.                   top = *(ptr + i) & 0x00F0;
  803.                   top = (top >> 4);
  804.                   bottom = *(ptr + i) &0x000F;
  805.                   if (point-- == 0) {
  806.                      strcat(temp,".");
  807.                   } /* endif */
  808.  
  809.                   if (((output_blank) && (top != 0)) || (point <= 1)) {
  810.                      output_blank = FALSE;
  811.                   } /* endif */
  812.                   if (output_blank) {
  813.                      strcat(temp," ");
  814.                   } else {
  815.                      sprintf(temp1,"%d",top);
  816.                      strcat(temp,temp1);
  817.                   } /* endif */
  818.                   /*_____________________________________________*/
  819.                   /* Ignore bottom half of last half-byte        */
  820.                   /* because it's the sign.                      */
  821.                   /*_____________________________________________*/
  822.                   if (i < (y-1)) {
  823.                      /* sign half byte? */
  824.                      if (point-- == 0) {
  825.                         strcat(temp,".");
  826.                      } /* endif */
  827.                      if ((output_blank) && (bottom != 0) || (point <= 1)) {
  828.                         output_blank = FALSE;
  829.                      } /* endif */
  830.                      if (output_blank) {
  831.                         strcat(temp," ");
  832.                      } else {
  833.                         sprintf(temp1,"%d",bottom);
  834.                         strcat(temp,temp1);
  835.                      } /* endif */
  836.                   } /* endif */
  837.                } /* endfor */
  838.                if (scale == 0) {
  839.                   strcat(temp,".");
  840.                } /* endif */
  841.                break;
  842.             case SQL_TYP_FLOAT:
  843.             case SQL_TYP_NFLOAT:
  844.                sprintf(temp,"%e",*(float *)sqlda->sqlvar[index].sqldata);
  845.                break;
  846.             case SQL_TYP_CHAR:
  847.             case SQL_TYP_NCHAR:
  848.                strncpy(temp,
  849.                        sqlda->sqlvar[index].sqldata,
  850.                        sqlda->sqlvar[index].sqllen);
  851.                temp[sqlda->sqlvar[index].sqllen] = '\0';       /* terminate   */
  852.                break;
  853.             case SQL_TYP_VARCHAR:
  854.             case SQL_TYP_NVARCHAR:
  855.             case SQL_TYP_LONG:
  856.             case SQL_TYP_NLONG:
  857.                strncpy(temp,
  858.                        sqlda->sqlvar[index].sqldata + 2,
  859.                        (SHORT)*sqlda->sqlvar[index].sqldata);
  860.                temp[(SHORT)*sqlda->sqlvar[index].sqldata] = '\0'; /* terminate */
  861.                break;
  862.             case SQL_TYP_DATE:
  863.             case SQL_TYP_NDATE:
  864.                strncpy(temp,
  865.                        sqlda->sqlvar[index].sqldata,10);
  866.                temp[10] = '\0';                              /* terminate   */
  867.                break;
  868.             case SQL_TYP_TIME:
  869.             case SQL_TYP_NTIME:
  870.                strncpy(temp,
  871.                        sqlda->sqlvar[index].sqldata,8);
  872.                temp[8] = '\0';                              /* terminate   */
  873.                break;
  874.             case SQL_TYP_STAMP:
  875.             case SQL_TYP_NSTAMP:
  876.                strncpy(temp,
  877.                        sqlda->sqlvar[index].sqldata,26);
  878.                temp[26] = '\0';                             /* terminate   */
  879.                break;
  880.             case SQL_TYP_LSTR:
  881.             case SQL_TYP_NLSTR:
  882.                strncpy(temp,
  883.                        sqlda->sqlvar[index].sqldata + 1,
  884.                        sqlda->sqlvar[index].sqllen);
  885.                temp[sqlda->sqlvar[index].sqllen] = '\0';         /* terminate */
  886.                break;
  887.             case SQL_TYP_CSTR:   /* null-terminated varying length string     */
  888.             case SQL_TYP_NCSTR:
  889.                sprintf(temp,"%s",sqlda->sqlvar[index].sqldata + 2);
  890.                break;
  891.             default:
  892.                sprintf(err_msg,
  893.                "\n Unknown type col = %d type = %d",
  894.                index+1,sqlda->sqlvar[index].sqltype);
  895.                rcd = FAILED;
  896.                break;
  897.             } /* endswitch */
  898.          } /* endif */
  899.  
  900.          if (rcd == FAILED) {          /* break out of for loop               */
  901.             break;
  902.          } /* endif */
  903.          if (strlen(temp) <= field[index].max_width) {
  904.             memcpy(&row[field[index].tab_pos],temp,strlen(temp));
  905.          } else {
  906.             sprintf(err_msg,
  907.             "\n Field too long col = %d value = %s",(index+1),temp);
  908.             rcd = FAILED;
  909.             break;
  910.          } /* endif */
  911.       } /* endfor */
  912.  
  913.       /*_____________________________________________*/
  914.       /* Print selected row                          */
  915.       /*_____________________________________________*/
  916.       fputs("\n",out_stream);
  917.       fputs(row,out_stream);
  918.       break;
  919.    } /* endswitch */
  920.  
  921.    return(rcd);
  922.  
  923. } /* end print_format_row */
  924.  
  925.  
  926.  
  927.  
  928. /*___________________________________________________________________________*/
  929. /* This procedure invokes an OS/2 program and passes it parameters           */
  930. /*___________________________________________________________________________*/
  931. SHORT dos_exec(PCHAR cmd_dos)
  932. {
  933.       /*_____________________________________________*/
  934.       /* Execute the program                         */
  935.       /*_____________________________________________*/
  936.    api_rcd = DosExecPgm(object_name,      /* failing object if program fails  */
  937.                   sizeof(object_name),    /* the object size                  */
  938.                   0,                      /* sync. program execution          */
  939.                   cmd_dos,                /* program name and parameters      */
  940.                   NULL,                   /* inherit environment              */
  941.                   &ExecResult,            /* termination reason code          */
  942.                   cmd_dos);               /* program name                     */
  943.    if (api_rcd) {
  944.       sprintf(err_msg,
  945.                "\n ERROR: DosExecPgm API call failed with %d return code",
  946.                api_rcd);
  947.       return(RECORD_DOS_EXEC_RESULTS);
  948.    } /* endif */
  949.       /*_____________________________________________*/
  950.       /* Format the return codes                     */
  951.       /*_____________________________________________*/
  952.  
  953.    switch (ExecResult.codeTerminate) {
  954.    case 0:
  955.       sprintf(err_msg,"\n Program completed with a return code of %d.",
  956.                ExecResult.codeResult);
  957.       api_rcd = ExecResult.codeResult;
  958.       break;
  959.    case 1:
  960.       sprintf(err_msg,"\n Program terminated by hard error.");
  961.       api_rcd = ExecResult.codeTerminate;
  962.       break;
  963.    case 2:
  964.       sprintf(err_msg,"\n Program terminated by a TRAP.");
  965.       api_rcd = ExecResult.codeTerminate;
  966.       break;
  967.    case 3:
  968.       sprintf(err_msg,"\n Program terminated by a DosKillProcess.");
  969.       api_rcd = ExecResult.codeTerminate;
  970.       break;
  971.    default:
  972.       sprintf(err_msg,"\n Unknown Program termination code.");
  973.       api_rcd = ExecResult.codeTerminate;
  974.    } /* endswitch */
  975.  
  976.    return(RECORD_DOS_EXEC_RESULTS);
  977.  
  978. } /* end dos_exec */
  979.  
  980.  
  981.  
  982. /*___________________________________________________________________________*/
  983. /* This procedure records the command results along with the elapsed         */
  984. /* time of execution of a command.                                           */
  985. /*___________________________________________________________________________*/
  986. SHORT record_command_results(SHORT rcd)
  987. {
  988.    CHAR           display_msg[1000];         /* temporary buffer              */
  989.    CHAR           msgbuf[512];               /* rcd explanation               */
  990.    SHORT          index;                     /* for counting                  */
  991.    CHAR           temp[20];                  /* char buffer                   */
  992.    CHAR           *err_line;                 /* a line of error info.         */
  993.  
  994.    switch (rcd) {
  995.    case RECORD_NOTHING:
  996.       fprintf(out_stream,"\n\n");
  997.       return(CONTINUE);
  998.       break;
  999.    case RECORD_DOS_EXEC_RESULTS:
  1000.       if (api_rcd == 0) {
  1001.          fprintf(out_stream,"%s",err_msg);
  1002.          fprintf(out_stream,
  1003.                   "\n OK.                       "
  1004.                   "Execution time = %10.4lf secs\n\n\n",
  1005.                   (double)event_time);
  1006.          return(CONTINUE);
  1007.       } else {
  1008.          fprintf(out_stream,"%s",err_msg);
  1009.          fprintf(out_stream,
  1010.                   "\n ERROR.                    "
  1011.                   "Execution time = %10.4lf secs\n\n\n",
  1012.                   (double)event_time);
  1013.          if (continue_on_error) {
  1014.             return(CONTINUE);
  1015.          } else {
  1016.             return(TERMINATE);
  1017.          } /* endif */
  1018.       } /* endif */
  1019.       break;
  1020.    case RECORD_SQLCA:
  1021.       if (SQLCODE == 0) {
  1022.          fprintf(out_stream,"%s",err_msg);
  1023.          fprintf(out_stream,
  1024.                   "\n OK. sql_rcd = %ld         "
  1025.                   "Execution time = %10.4lf secs\n\n\n",
  1026.                   SQLCODE,(double)event_time);
  1027.          return(CONTINUE);
  1028.       } else {
  1029.          if (SQLCODE > 0) {
  1030.             sprintf(display_msg,
  1031.             "\n WARNING, sql_code =%ld,    Execution time = %10.4lf secs",
  1032.             SQLCODE,(double)event_time);
  1033.          } else {
  1034.             sprintf(display_msg,
  1035.             "\n ERROR, sql_code = %ld\n",
  1036.             SQLCODE);
  1037.          } /* endif */
  1038.          /*_____________________________________________*/
  1039.          /* Retrieve error message                      */
  1040.          /*_____________________________________________*/
  1041.          rcd = sqlaintp(msgbuf,              /* buffer for msg text           */
  1042.                         512,                 /* buffer size                   */
  1043.                         79,                  /* line width                    */
  1044.                         &sqlca);             /* SQLCA                         */
  1045.  
  1046.          if (rcd < 0) {                      /* message retrieve error        */
  1047.             sprintf(msgbuf,"\n SQLAINTP ERROR, Return code = %d \n",rcd);
  1048.          } /* endif */
  1049.  
  1050.          err_line = strtok(msgbuf,"\n");
  1051.          while (err_line != NULL) {
  1052.             sprintf(display_msg,"%s\n %s",display_msg,err_line);
  1053.             err_line = strtok(NULL,"\n");
  1054.          } /* endwhile */
  1055.          strcat(display_msg,"\n\n ------ ADDITIONAL SQLCA Info --------");
  1056.          fputs(display_msg,out_stream);
  1057.  
  1058.          /*_____________________________________________*/
  1059.          /* Display SQLCA sqlerrmc                      */
  1060.          /*_____________________________________________*/
  1061.          if (sqlca.sqlerrml) {
  1062.             sqlca.sqlerrmc[sqlca.sqlerrml] = '\0';    /* NULL terminate       */
  1063.             while (sqlca.sqlerrml--) {
  1064.                if (sqlca.sqlerrmc[sqlca.sqlerrml] == 0xFF) {   /* remove FFs  */
  1065.                   sqlca.sqlerrmc[sqlca.sqlerrml] = ' ';
  1066.                } /* endif */
  1067.             } /* endwhile */
  1068.             fprintf(out_stream,"\n SQLERRMC's:  %s",sqlca.sqlerrmc);
  1069.          } /* endif */
  1070.  
  1071.          /*_____________________________________________*/
  1072.          /* Display SQLCA sqlerrp                       */
  1073.          /*_____________________________________________*/
  1074.          sprintf(display_msg,"\n SQLERRP: ");
  1075.          for (index=0;index<8 ;index++ ) {
  1076.             sprintf(temp,"%c",sqlca.sqlerrp[index]);
  1077.             strcat(display_msg,temp);
  1078.          } /* endfor */
  1079.          fputs(display_msg,out_stream);
  1080.  
  1081.          /*_____________________________________________*/
  1082.          /* Display SQLCA sqlerrd                       */
  1083.          /*_____________________________________________*/
  1084.          sprintf(display_msg,"\n SQLERRD: ");
  1085.          for (index=0;index<6 ;index++ ) {
  1086.             sprintf(temp,"%10ld",sqlca.sqlerrd[index]);
  1087.             strcat(display_msg,temp);
  1088.          } /* endfor */
  1089.          fputs(display_msg,out_stream);
  1090.  
  1091.          /*_____________________________________________*/
  1092.          /* Display SQLCA warnings                      */
  1093.          /*_____________________________________________*/
  1094.          sprintf(display_msg,"\n Warning Indicators: ");
  1095.          for (index=0;index<5 ;index++ ) {
  1096.             sprintf(temp,"%c",sqlca.sqlwarn[index]);
  1097.             strcat(display_msg,temp);
  1098.          } /* endfor */
  1099.          fprintf(out_stream,"%s\n\n\n",temp);
  1100.  
  1101.          if (continue_on_error) {
  1102.             return(CONTINUE);
  1103.          } else {
  1104.             return(TERMINATE);
  1105.          } /* endif */
  1106.  
  1107.       } /* endif */
  1108.       break;
  1109.    case RECORD_API_RCD:
  1110.       if (api_rcd == 0) {
  1111.          fprintf(out_stream,
  1112.          "\n OF, api_rcd = %ld,         "
  1113.          " Execution time = %10.4lf secs\n\n\n",
  1114.          api_rcd,(double)event_time);
  1115.          return(CONTINUE);
  1116.       } else {
  1117.          fprintf(out_stream,
  1118.          "\n API for command %s failed with "
  1119.          "a return code of %d.\n\n\n",
  1120.          parms.cmd_name,api_rcd);
  1121.       } /* endif */
  1122.       if (continue_on_error) {
  1123.          return(CONTINUE);
  1124.       } else {
  1125.          return(TERMINATE);
  1126.       } /* endif */
  1127.       break;
  1128.    case RECORD_ERR_MSG:
  1129.       fprintf(out_stream," %s\n\n\n", err_msg);
  1130.       return(TERMINATE);
  1131.       break;
  1132.    } /* endswitch */
  1133. } /* end record_command_results */
  1134.  
  1135.  
  1136.  
  1137. /*___________________________________________________________________________*/
  1138. /* Dispatch Database Administration Command Handler                          */
  1139. /*___________________________________________________________________________*/
  1140. SHORT dba_exec(PCHAR parm_buf)
  1141. {
  1142.    /*_____________________________________________*/
  1143.    /* Display SQLCA sqlerrd                       */
  1144.    /*_____________________________________________*/
  1145.    switch (parms.cmd_num) {
  1146.          /*_____________________________________________*/
  1147.          /* Environment Services                        */
  1148.          /*_____________________________________________*/
  1149.    case CMD_DBA_STARTUSE        : rcd = dba_startuse(parm_buf);      break;
  1150.    case CMD_DBA_STOPUSE         : rcd = dba_stopuse();               break;
  1151.    default: RECORD_NOTHING;                                          break;
  1152.    } /* endswitch */
  1153.  
  1154.    return(rcd);
  1155.  
  1156. } /* end dba_exec */
  1157.  
  1158.  
  1159.  
  1160. /*___________________________________________________________________________*/
  1161. /* execute the STARTUSE DBA function                                         */
  1162. /* FORMAT: EXEC DBA: STARTUSE database_name [S/X]                            */
  1163. /*___________________________________________________________________________*/
  1164. SHORT dba_startuse(PCHAR parm_str)
  1165. {
  1166.    BYTE  database_name[9];                   /* Database name,null terminated */
  1167.    CHAR  share_mode;                         /* Share mode for STARTUSE       */
  1168.    /*_____________________________________________*/
  1169.    /* Get the parm strings                        */
  1170.    /*_____________________________________________*/
  1171.    if (sscanf(parm_str, " %8s %c ", database_name, &share_mode)
  1172.             != parms.num_parms) {
  1173.       sprintf(err_msg, "\nSYNTAX ERROR: invalid parameters.");
  1174.       return(RECORD_ERR_MSG);
  1175.    } /* endif */
  1176.  
  1177.    /*_____________________________________________*/
  1178.    /* call the START USING DB API                 */
  1179.    /*_____________________________________________*/
  1180.    sqlestrd(database_name,share_mode,&sqlca);
  1181.  
  1182.    if (SQLCODE == RESTART) {
  1183.    /*_____________________________________________*/
  1184.    /* call the RESTART DB API                     */
  1185.    /*_____________________________________________*/
  1186.    sqlerest(database_name,&sqlca);
  1187.    } /* endif */
  1188.  
  1189.    if (SQLCODE != 0) {
  1190.       return(RECORD_SQLCA);
  1191.    } else {
  1192.       /*_____________________________________________*/
  1193.       /* call the START USING DB API (again)         */
  1194.       /*_____________________________________________*/
  1195.       sqlestrd(database_name,share_mode,&sqlca);
  1196.    } /* endif */
  1197.  
  1198.    if (SQLCODE != 0) {
  1199.       return(RECORD_SQLCA);
  1200.    } /* endif */
  1201.  
  1202.    sqleisig(&sqlca);                      /* handle CTRL-BRK signal handler */
  1203.  
  1204.    return(RECORD_SQLCA);
  1205.  
  1206. } /* end dba_startuse */
  1207.  
  1208.  
  1209.  
  1210. /*___________________________________________________________________________*/
  1211. /* execute the STOPUSE DBA function                                          */
  1212. /* FORMAT: EXEC DBA: STOPUSE                                                 */
  1213. /*___________________________________________________________________________*/
  1214. SHORT dba_stopuse(VOID)
  1215. {
  1216.    /*_____________________________________________*/
  1217.    /* Call the STOP USING DB API                  */
  1218.    /*_____________________________________________*/
  1219.    sqlestpd(&sqlca);
  1220.    return(RECORD_SQLCA);
  1221.  
  1222. } /* end dba_stopuse */
  1223.  
  1224.