home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE7.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  5.2 KB  |  235 lines

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