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