home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06034a < prev    next >
Text File  |  1991-03-05  |  5KB  |  215 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "xqlm.h"
  6.  
  7. /*----------------------------------------------------
  8.     INTRPRET.C
  9.  
  10.     Compile using Turbo C version 2.0 or higher.
  11.     Requires Novell XQL API library (XQLMINTF.OBJ)
  12.  
  13.     A Novell SQL query interpreter.
  14.  
  15.     (c) Copyright 1991 Mark L. Pruett
  16. ----------------------------------------------------*/
  17.  
  18. #define FETCH_CURRENT 0
  19. #define FETCH_FIRST   1
  20. #define FETCH_NEXT    2
  21. #define FETCH_PREV    3
  22. #define FETCH_LAST    4
  23.  
  24. #define FORMAT_RAW    0
  25. #define FORMAT_BLANKS 1
  26. #define FORMAT_ZEROS  2
  27.  
  28. #define MAX_BUFFER    500
  29. #define MAX_TYPES     14
  30.  
  31. #define REC_COUNT     0
  32. #define STATUS_INFO   1
  33.  
  34. int cursorid,
  35.     SQLCODE;
  36.  
  37. char buffer[1000];
  38.  
  39. /*-------------------------------------------------------
  40.     Read SQL query from standard input.
  41. -------------------------------------------------------*/
  42. void get_SQL_query (char *query) {
  43. int ch = ' ',
  44.     i;
  45.  
  46.     for (i=0; (i < 99) && (ch != EOF); i++) {
  47.         ch = getchar ();
  48.         if ((ch == '\t') || (ch == '\n'))
  49.             ch = ' ';
  50.         query[i] = ch;
  51.     }
  52.     query[i-1] = '\0';
  53. }
  54.  
  55. /*-------------------------------------------------------
  56.     Check status code, print error message or return.
  57. -------------------------------------------------------*/
  58. void SQL_error (char *function, int status) {
  59.  
  60.     if (status > 0) {
  61.         printf ("Function %s has returned an "
  62.                 "error code of %d\n", function, status);
  63.  
  64.         if (strcmp(function, "XQLLogout"))
  65.             XQLLogout ();
  66.         exit(1);
  67.     }
  68. }
  69.  
  70. /*-------------------------------------------------------
  71.     Get extended information on an SQL call.
  72. -------------------------------------------------------*/
  73. void SQL_info (int ret_code) {
  74. int
  75.     status;
  76.  
  77. struct {
  78.     long selected;
  79.     long rejected;
  80. } stats;
  81.  
  82.     status = XQLStatus (cursorid, REC_COUNT,
  83.                        (char *) &stats);
  84.     if (status == 0) {
  85.         switch (ret_code) {
  86.             case -102: printf ("%ld record(s) INSERTED\n",
  87.                                stats.selected);
  88.                        break;
  89.             case -103: printf ("%ld record(s) UPDATED\n",
  90.                                stats.selected);
  91.                        break;
  92.             case -104: printf ("%ld record(s) DELETED\n",
  93.                                stats.selected);
  94.                        break;
  95.         }
  96.     }
  97. }
  98.  
  99. /*-------------------------------------------------------
  100.     Print the SQL table structure of the query.
  101. -------------------------------------------------------*/
  102. void print_structure (void) {
  103. int
  104.     status = 0,
  105.     datatype = 0,
  106.     size = 0,
  107.     decimal_places = 0,
  108.     display_len = 0,
  109.     name_len = 0,
  110.     field;
  111.  
  112. char
  113.     field_name[36],
  114.     *type[] =
  115.      { "String", "Integer", "Float",  "Date",
  116.        "Time",   "Decimal", "Money",  "Logical",
  117.        "Numeric","BFloat",  "LString","ZString",
  118.        "Note",   "LVar",    "UNKNOWN" };
  119.  
  120.  
  121.     puts ("RECORD STRUCTURE:\n");
  122.     puts (
  123.     "Field Name                 Type      Size  Dec");
  124.     puts (
  125.     "----- -------------------- --------- ----- ---");
  126.     for (field=1; status == 0; field++) {
  127.         name_len = sizeof (field_name);
  128.         status = XQLDescribe (cursorid, field, &datatype,
  129.                  &size, &decimal_places, &display_len,
  130.                  &name_len, field_name);
  131.         if (! status) {
  132.             field_name[name_len] = '\0';
  133.             if (datatype > MAX_TYPES)
  134.                 datatype = MAX_TYPES;
  135.             printf ("%5d %-20s %-9s %5d %3d\n",
  136.                    field, field_name, type[datatype],
  137.                    size, decimal_places);
  138.         }
  139.     }
  140.     printf ("\n");
  141. }
  142.  
  143. /*-----------------------------------------------------*/
  144. void main (void) {
  145. int
  146.     status,
  147.     iReserved = 0,
  148.     query_length = 0,
  149.     buf_len,
  150.     fetch_option,
  151.     spaces = 1;
  152.  
  153. long
  154.     rec_count;
  155.  
  156. char
  157.     user      [11] = "",
  158.     password  [11] = "",
  159.     DDpath    [31] = "",
  160.     datapath  [31] = "",
  161.     sReserved [1]  = "",
  162.     query     [200];
  163.  
  164.     /* Initialize an XQLM session */
  165.  
  166.     status = XQLLogin (user, password, DDpath, datapath,
  167.                        sReserved, iReserved);
  168.     SQL_error ("XQLLogin", status);
  169.  
  170.     /* Obtain a cursor from XQLM */
  171.  
  172.     status = XQLCursor (&cursorid);
  173.     SQL_error ("XQLCursor", status);
  174.  
  175.     /* Get SQL query from standard input */
  176.  
  177.     get_SQL_query (query);
  178.  
  179.     printf ("SQL QUERY:\n\n%s\n\n", query);
  180.  
  181.     query_length = strlen (query);
  182.     status = XQLCompile (cursorid, &query_length, query);
  183.     SQL_info (status);
  184.  
  185.     if (! status) {
  186.         SQL_error ("XQLCompile", status);
  187.  
  188.         /* Print record structure */
  189.         print_structure ();
  190.  
  191.         puts("RECORD DATA:\n");
  192.  
  193.         rec_count = 1;
  194.         fetch_option = FETCH_FIRST;
  195.         do {
  196.             buf_len = MAX_BUFFER;
  197.             status = XQLFetch (cursorid, fetch_option,
  198.                      &buf_len, buffer, &rec_count,
  199.                      FORMAT_BLANKS, spaces);
  200.             fetch_option = FETCH_NEXT;
  201.  
  202.             if (status == 0) {
  203.                 buffer[buf_len] = '\0';
  204.                 printf ("%s\n", buffer+2);
  205.             }
  206.  
  207.         } while (status == 0);
  208.     }
  209.  
  210.     /* Terminate an XQLM session */
  211.  
  212.     status = XQLLogout ();
  213.     SQL_error ("XQLLogout", status);
  214. }
  215.