⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example7.c

📁 在SCO UNIX5.05
💻 C
字号:
/*
** Confidential property of Sybase, Inc.
** (c) Copyright Sybase, Inc. 1992 to ???
** All rights reserved.
*/

/*
** %M%:      %I%      %G%      %U%
**  
**  
**  
*/
#if USE_SCCSID
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* USE_SCCSID */

/*
**	example7.c
**
** This example illustrates the use of browse-mode routines to
** determine the source of result columns from ad hoc queries.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sybfront.h>
#include <sybdb.h>
#include "sybdbex.h"

/* Forward references
*/
void     examine_results PROTOTYPE((DBPROCESS *));
void		send_command PROTOTYPE((DBPROCESS *));

main()
{
	LOGINREC              *login;
	DBPROCESS             *dbproc;

	int                   command_count;
	RETCODE               retcode;

	printf("Demo of the use of browse-mode routines\n\n");
	fflush(stdout);

	/* 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();
	DBSETLUSER(login, USER);
	DBSETLPWD(login, 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 cannot return 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);
				}
			}
			command_count++;
		}
	}
}


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);

	/* dbtabcount returns -0 if the query didn't include the */
	/* words FOR BROWSE.                                     */
	if (tabcount > 0)
	{
		printf ("The following tables were used");
		printf (" 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"));
		}
	}
	/* dbtabcount must have returned 0: */
	else
	{
		printf ("Your query was not a browse-mode select.\n");
		printf ("For this reason, dbtabcount()\n");
                printf ("and dbtabname() cannot be used to print\n");
                printf ("the names of the tables involved in the query.\n");
	}

	/* 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)
		{
			if (tabcount == 0)
				strcpy(fullsource, "(not browse-mode SELECT)");
			else
			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 == (DBPROCESS *)NULL) || (DBDEAD(dbproc)))
        return(INT_EXIT);
    else 
    {
        fprintf (ERR_CH, "DB-Library error:\n\t%s\n", dberrstr);

        if (oserr != DBNOERR)
            fprintf (ERR_CH, "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;

{
	fprintf (ERR_CH, "Msg %ld, Level %d, State %d\n", 
	        msgno, severity, msgstate);

	if (strlen(srvname) > 0)
		fprintf (ERR_CH, "Server '%s', ", srvname);
	if (strlen(procname) > 0)
		fprintf (ERR_CH, "Procedure '%s', ", procname);
	if (line > 0)
		fprintf (ERR_CH, "Line %d", line);

	fprintf (ERR_CH, "\n\t%s\n", msgtext);

	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -