home *** CD-ROM | disk | FTP | other *** search
- /*
- ** example7.c
- **
- ** This example illustrates the use of browse-mode routines to
- ** determine the source of result columns from ad hoc queries.
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- #include <sybfront.h>
- #include <sybdb.h>
-
- /* Function Prototypes */
- int err_handler(DBPROCESS *, int, int, int, char *, char *);
- int msg_handler(DBPROCESS *, DBINT, int, int, char *, char *,
- char *, DBUSMALLINT);
- int main();
- void examine_results(DBPROCESS *);
- void send_command(DBPROCESS *);
-
-
- main()
- {
- LOGINREC *login;
- DBPROCESS *dbproc;
-
- int command_count;
- RETCODE retcode;
-
- /* Initialize DB-Library. */
- if (dbinit() == FAIL)
- exit(ERREXIT);
-
- /* Install the user-supplied error-handling and message-handling
- * routines. They are defined at the bottom of this source file.
- */
- dberrhandle(err_handler);
- dbmsghandle(msg_handler);
-
- /* Allocate and initialize the LOGINREC structure to be used
- * to open a connection to SQL Server.
- */
-
- login = dblogin();
- DBSETLPWD(login, "server_password");
- DBSETLAPP(login, "example7");
-
- dbproc = dbopen(login, NULL);
-
- /* Allow the user to type in a series of queries. This program
- * will be terminated by the word "quit", appearing at the
- * beginning of the line.
- */
- while (1)
- {
- /* Send a user-generated query to SQL Server. */
- send_command(dbproc);
-
- /* Now, examine the results of any queries the user has
- * typed in.
- */
-
- command_count = 1;
- while ((retcode = dbresults(dbproc)) != NO_MORE_RESULTS)
- {
- if (retcode == FAIL)
- printf("Command %d failed.\n", command_count);
- else
- {
- if (!(DBCMDROW(dbproc)))
- printf
- ("Command %d returned no rows.\n",
- command_count);
- else
- {
- /* This is a command that can return
- * rows. Let's take a closer look at it.
- */
- printf("Command %d:\n", command_count);
- examine_results(dbproc);
-
- /* Throw away all data rows. */
- dbcanquery(dbproc);
- }
- }
- }
- }
- }
-
-
- void examine_results(dbproc)
- DBPROCESS *dbproc;
- {
- int colcount;
- int colnum;
- char fullsource[128];
- char *sourcecolname;
- int tabcount;
- char *tabname;
- int tabnum;
-
- /* Determine which tables were used to generate the query results. */
-
- tabcount = dbtabcount(dbproc);
- printf
- ("The following tables were used to generate these query results:\n");
-
- for (tabnum = 1; tabnum <= tabcount; tabnum++)
- {
- if ((tabname = dbtabname(dbproc, tabnum)) != NULL)
- printf
- ("\t%s (%s)\n", tabname,
- (dbtabbrowse(dbproc, tabnum)
- ? "browsable" : "not browsable"));
- }
-
- /* Determine which tables were used to generate each result column. */
-
- colcount = dbnumcols(dbproc);
- printf("Here are the columns of the target list and their sources:\n");
-
- printf
- ("\t%-20s %-30s %s\n\n",
- "Result column:", "Source:", "Browsable?");
- for (colnum = 1; colnum <= colcount; colnum++)
- {
- tabname = dbtabsource(dbproc, colnum, NULL);
- sourcecolname = dbcolsource(dbproc, colnum);
-
- if (tabname == NULL)
- strcpy(fullsource, "(result of expression)");
- else
- sprintf(fullsource, "%s.%s", tabname, sourcecolname);
-
- printf
- ("\t%-20s %-30s %s\n",
- dbcolname(dbproc, colnum),
- fullsource,
- (dbcolbrowse(dbproc, colnum) ? "yes" : "no"));
- }
- }
-
-
-
- void send_command(dbproc)
- DBPROCESS *dbproc;
- {
- char cmdbuf[2048];
-
- /* Allow the user to type in an ad hoc query. This query
- * will be terminated by the word "go" appearing at the
- * beginning of the line.
- *
- * If the user types the word "quit" at the beginning of a line,
- * we'll quit the program.
- */
- printf("Enter a SQL query:\n");
- while (1)
- {
- printf("> ");
- gets(cmdbuf);
-
- if (strcmp(cmdbuf, "go") == 0)
- {
- dbsqlexec(dbproc);
- break;
- }
- else if (strcmp(cmdbuf, "quit") == 0)
- {
-
- dbexit();
- exit(STDEXIT);
- }
- else
- {
- /* Keep reading SQL commands. */
- dbcmd(dbproc, cmdbuf);
- }
- }
- }
-
-
-
- int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
- DBPROCESS *dbproc;
- int severity;
- int dberr;
- int oserr;
- char *dberrstr;
- char *oserrstr;
- {
- if ((dbproc == NULL) || (DBDEAD(dbproc)))
- return(INT_EXIT);
- else
- {
- printf("DB-Library error:\n\t%s\n", dberrstr);
-
- if (oserr != DBNOERR)
- printf("Operating-system error:\n\t%s\n", oserrstr);
-
- return(INT_CANCEL);
- }
- }
-
-
- int msg_handler(dbproc, msgno, msgstate, severity, msgtext,
- srvname, procname, line)
-
- DBPROCESS *dbproc;
- DBINT msgno;
- int msgstate;
- int severity;
- char *msgtext;
- char *srvname;
- char *procname;
- DBUSMALLINT line;
-
- {
- printf ("Msg %ld, Level %d, State %d\n",
- msgno, severity, msgstate);
-
- if (strlen(srvname) > 0)
- printf ("Server '%s', ", srvname);
- if (strlen(procname) > 0)
- printf ("Procedure '%s', ", procname);
- if (line > 0)
- printf ("Line %d", line);
-
- printf("\n\t%s\n", msgtext);
-
- return(0);
- }
-