home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 13.img / SAMPLE / DBLIB / EXAMPLE7.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  4.4 KB  |  211 lines

  1. /*    example7.c */
  2. /*
  3. ** This example illustrates the use of browse mode functions to
  4. ** determine the source of result columns from ad hoc queries.
  5. */
  6.  
  7. #define DBMSOS2
  8. #include <stdio.h>
  9. #include <sqlfront.h>
  10. #include <sqldb.h>
  11.  
  12. #define STDEXIT 0
  13. void    examine_results();
  14. void    send_command();
  15.  
  16. /* Forward declarations of the error-handling and message-handling
  17. functions. */
  18. int    err_handler();
  19. int    msg_handler();
  20.  
  21. main()
  22. {
  23.     LOGINREC    *login;
  24.     DBPROCESS    *dbproc;
  25.  
  26.     int    command_count = 0;
  27.     RETCODE    retcode;
  28.  
  29.     /* Install the user-supplied error-handling and message-handling
  30.     * functions. They are defined at the bottom of this source file.
  31.     */
  32.     dberrhandle(err_handler);
  33.     dbmsghandle(msg_handler);
  34.  
  35.     /* Allocate and initialize the LOGINREC structure to be used
  36.     * to open a connection to SQL Server.
  37.     */
  38.  
  39.     login = dblogin();
  40.     DBSETLUSER(login, "user");
  41.     DBSETLPWD(login, "my_passwd");
  42.     DBSETLAPP(login, "example7");
  43.  
  44.     dbproc = dbopen(login, "my_server");
  45.  
  46.     /* Allow the user to type in a series of queries. This program
  47.     * is terminated by the word "quit" appearing at the
  48.     * beginning of the line.
  49.     */
  50.     while (1)
  51.     {
  52.         /* Send a user-generated query to SQL Server. */
  53.         send_command(dbproc);
  54.  
  55.         /* Now, examine the results of any queries the user has
  56.         * typed in.
  57.         */
  58.  
  59.         command_count = 1;
  60.         while ((retcode = dbresults(dbproc)) != NO_MORE_RESULTS)
  61.         {
  62.             command_count++ ;
  63.             if (retcode == FAIL)
  64.                 printf("Command %d failed.\n", command_count);
  65.             else
  66.             {
  67.                 if (!(DBCMDROW(dbproc)))
  68.                     printf
  69.                 ("Command %d returned no rows.\n",
  70.                 command_count);
  71.                 else
  72.                 {
  73.                 /* This is a command that can return
  74.                 * rows. Let's take a closer look at it.
  75.                 */
  76.                 printf("Command %d:\n", command_count);
  77.                 examine_results(dbproc);
  78.  
  79.                 /* Throw away all data rows. */
  80.                 dbcanquery(dbproc);
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. void examine_results(dbproc)
  88. DBPROCESS    *dbproc;
  89. {
  90.     int    colcount;
  91.     int    colnum;
  92.     char    fullsource[128];
  93.     char    *sourcecolname;
  94.     int    tabcount;
  95.     char    *tabname;
  96.     int    tabnum;
  97.  
  98.     /* Determine which tables were used to generate the query results.*/
  99.  
  100.     tabcount = dbtabcount(dbproc);
  101.     printf
  102.     ("The following tables were used to generate these query results:\n");
  103.  
  104.     for (tabnum = 1; tabnum <= tabcount; tabnum++)
  105.     {
  106.     if ((tabname = dbtabname(dbproc, tabnum)) != NULL)
  107.         printf
  108.         ("\t%s (%s)\n", tabname,
  109.         (dbtabbrowse(dbproc, tabnum)
  110.         ? "browsable" : "not browsable"));
  111.     }
  112.  
  113.     /* Determine which tables were used to generate each result column.*/
  114.  
  115.     colcount = dbnumcols(dbproc);
  116.     printf("Here are the columns of the target list and their sources:\n");
  117.  
  118.     printf
  119.     ("\t%-20s    %-30s    %s\n\n",
  120.     "Result column:", "Source:", "Browsable?");
  121.     for (colnum = 1; colnum <= colcount; colnum++)
  122.     {
  123.     tabname = dbtabsource(dbproc, colnum, NULL);
  124.     sourcecolname = dbcolsource(dbproc, colnum);
  125.  
  126.     if (tabname == NULL)
  127.         strcpy(fullsource, "(result of expression)");
  128.     else
  129.         sprintf(fullsource, "%s.%s", tabname, sourcecolname);
  130.  
  131.     printf
  132.     ("\t%-20s    %-30s    %s\n",
  133.     dbcolname(dbproc, colnum),
  134.     fullsource,
  135.     (dbcolbrowse(dbproc, colnum) ? "yes" : "no"));
  136.     }
  137. }
  138.  
  139.  
  140. void send_command(dbproc)
  141. DBPROCESS    *dbproc;
  142. {
  143.  
  144.     static char    cmdbuf[2048];
  145.  
  146.     /* Allow the user to type in an ad hoc query. This query
  147.     * is terminated by the word "go" appearing at the
  148.     * beginning of the line.
  149.     *
  150.     * If the user types the word "quit" at the beginning of a line,
  151.     * we'll quit the program.
  152.     */
  153.     printf("Enter SQL query:\n");
  154.     while (1)
  155.     {
  156.         printf("> ");
  157.         gets(cmdbuf);
  158.  
  159.         if (strcmp(cmdbuf, "go") == 0)
  160.         {
  161.             dbsqlexec(dbproc);
  162.             break;
  163.         }
  164.         else if (strcmp(cmdbuf, "quit") == 0)
  165.         {
  166.             exit(STDEXIT);
  167.         }
  168.         else
  169.         {
  170.             /* Keep reading SQL commands. */
  171.             dbcmd(dbproc, cmdbuf);
  172.         }
  173.     }
  174. }
  175.  
  176. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  177. DBPROCESS    *dbproc;
  178. int    severity;
  179. int    dberr;
  180. int    oserr;
  181. char    *dberrstr;
  182. char    *oserrstr;
  183. {
  184.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  185.         return(INT_EXIT);
  186.     else
  187.     {
  188.         printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  189.  
  190.         if (oserr != DBNOERR)
  191.             printf("Operating-system error:\n\t%s\n", oserrstr);
  192.  
  193.         return(INT_CANCEL);
  194.     }
  195. }
  196.  
  197. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  198. DBPROCESS    *dbproc;
  199. DBINT    msgno;
  200. int    msgstate;
  201. int    severity;
  202. char    *msgtext;
  203. {
  204.     printf
  205.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  206.     msgno, msgstate, severity, msgtext);
  207.     return(0);
  208. }
  209.  
  210.  
  211.