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