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

📄 thrdutil.c

📁 sybase数据库ct library的开发,使用了所以有函数
💻 C
📖 第 1 页 / 共 3 页
字号:
	CS_INT			num_rows;	
				/* number of rows to get in a single fetch */
	CS_INT			row_count = 0;

	connection =(CS_CONNECTION *)NULL;
	rowsize = 0;
	threadnum = 0;

	/*
	** Get parent connection 
	*/
	retcode = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE, &connection,
				CS_UNUSED, NULL);
	if (retcode != CS_SUCCEED)
	{
		ex_error(threadnum, 
			"ex_fetch_data: ct_cmd_props(CS_PARENT_HANDLE) failed");
		return retcode;
	}
				 
        /*               
        ** Get Connection specific data - get thread number from the
        ** user data stored in connection structure
        */
        (void)ex_get_threadnum(NULL, connection, &threadnum); 

	/*
	** Find out how many columns there are in this result set.
	*/
	retcode = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
	if (retcode != CS_SUCCEED)
	{
		ex_error(threadnum, "ex_fetch_data: ct_res_info() failed");
		return retcode;
	}

	/*
	** Make sure we have at least one column
	*/
	if (num_cols <= 0)
	{
		ex_error(threadnum, 
			"ex_fetch_data: ct_res_info() returned zero columns");
		return CS_FAIL;
	}

	/*
	** Our program variable, called 'coldata', is an array of 
	** EX_COLUMN_DATA structures. Each array element represents
	** one column.  Each array element will re-used for each row set.
	** In coldata, we allocate an array of values and thus use
	** array binding.
	**
	** First, allocate memory for the data element to process.
	*/
	coldata = (EX_COLUMN_DATA *)malloc(num_cols * sizeof (EX_COLUMN_DATA));
	if (coldata == NULL)
	{
		ex_error(threadnum, "ex_fetch_data: malloc() failed");
		return CS_MEM_ERROR;
	}

	datafmt = (CS_DATAFMT *)malloc(num_cols * sizeof (CS_DATAFMT));
	if (datafmt == NULL)
	{
		ex_error(threadnum, "ex_fetch_data: malloc() failed");
		free(coldata);
		return CS_MEM_ERROR;
	}

	/*
	** Loop through the columns getting a description of each one
	** and binding each one to a program variable.
	**
	** We're going to bind each column to a character string; 
	** this will show how conversions from server native datatypes
	** to strings can occur via bind.
	**
	** We're going to use the same datafmt structure for both the describe
	** and the subsequent bind.
	**
	** If an error occurs within the for loop, a break is used to get out
	** of the loop and the data that was allocated is free'd before
	** returning.
	*/
	for (i = 0; i < num_cols; i++)
	{
		/*
		** Get the column description.  ct_describe() fills the
		** datafmt parameter with a description of the column.
		*/
		retcode = ct_describe(cmd, (i + 1), &datafmt[i]);
		if (retcode != CS_SUCCEED)
		{
			ex_error(threadnum,
				"ex_fetch_data: ct_describe() failed");
			break;
		}

		/*
		** update the datafmt structure to indicate that we want the
		** results in a null terminated character string.
		**
		** First, update datafmt.maxlength to contain the maximum
		** possible length of the column. To do this, call
		** ex_display_len() to determine the number of bytes needed
		** for the character string representation, given the
		** datatype described above.  Add one for the null
		** termination character.
		*/
		datafmt[i].maxlength = ex_display_dlen(&datafmt[i]) + 1;
		
		/*
		** Set datatype and format to tell bind we want things
		** converted to null terminated strings
		*/
		datafmt[i].datatype = CS_CHAR_TYPE;
		datafmt[i].format   = CS_FMT_NULLTERM;

		rowsize += MEM_ALIGN_SIZE(datafmt[i].maxlength);
	}

        if (retcode != CS_SUCCEED)
        {
                free(coldata);
                free(datafmt);
                return retcode;
        }

	rowsize += (MEM_ALIGN_SIZE(sizeof(CS_INT)) + 
			MEM_ALIGN_SIZE(sizeof(CS_SMALLINT))) * num_cols; 

	/* 
	** Find max. number of rows that we want to obtain in a single
	** fetch (Why? - just to limit memory being used).
	*/
	num_rows = MAX_MEM_BLOCK_SIZE / rowsize;

	for (i = 0; i < num_cols; i++)
	{
		coldata[i].value = (CS_CHAR *)NULL;
		coldata[i].valuelen = (CS_INT *)NULL;
		coldata[i].indicator = (CS_SMALLINT *)NULL;

                /*
                ** Allocate memory for the column string
                */
                coldata[i].value = (CS_CHAR *)malloc(
					datafmt[i].maxlength * num_rows);
                if (coldata[i].value == NULL)
                {
                        ex_error(threadnum, "ex_fetch_data: malloc() failed");
                        retcode = CS_MEM_ERROR;
                        break;
                }

                /*
                ** Allocate memory for the valuelen 
                */
                coldata[i].valuelen = (CS_INT *)malloc(
					sizeof(CS_INT) * num_rows);
                if (coldata[i].valuelen == NULL)
                {
                        ex_error(threadnum, "ex_fetch_data: malloc() failed");
                        retcode = CS_MEM_ERROR;
                        break;
                }

                /*
                ** Allocate memory for the indicator
                */
                coldata[i].indicator = (CS_SMALLINT *)malloc(
					sizeof(CS_SMALLINT) * num_rows);
                if (coldata[i].indicator == NULL)
                {
                        ex_error(threadnum, "ex_fetch_data: malloc() failed");
                        retcode = CS_MEM_ERROR;
                        break;
                }


		/*
		** Now do an array bind.
		*/
		datafmt[i].count = num_rows;
		retcode = ct_bind(cmd, (i + 1), &datafmt[i],
				coldata[i].value, coldata[i].valuelen,
				coldata[i].indicator);
		if (retcode != CS_SUCCEED)
		{
			ex_error(threadnum, "ex_fetch_data: ct_bind() failed");
			break;
		}
	}
	if (retcode != CS_SUCCEED)
	{
		for (j = 0; j <= i; j++)
		{
			free(coldata[j].value);
                        free(coldata[j].valuelen);
                        free(coldata[j].indicator);
		}
		free(coldata);
		free(datafmt);
		return retcode;
	}

	/*
	** Fetch the rows.  Loop while ct_fetch() returns CS_SUCCEED or 
	** CS_ROW_FAIL
	*/
	while (((retcode = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED,
			&rows_read)) == CS_SUCCEED) || (retcode == CS_ROW_FAIL))
	{
		/*
		** Increment our row count by the number of rows just fetched.
		*/
		row_count = row_count + rows_read;

		/*
		** Check if we hit a recoverable error.
		*/
		if (retcode == CS_ROW_FAIL)
		{
			EX_GLOBAL_PROTECT(fprintf(stdout, 
					"Thread_%d:Error on row %d.\n",
					threadnum, row_count));
			fflush(stdout);
		}

		ex_display_rows(threadnum, rows_read, num_cols, datafmt, 
				coldata);
	}

	/*
	** Free allocated space.
	*/
	for (i = 0; i < num_cols; i++)
	{
		free(coldata[i].value);
                free(coldata[i].valuelen);
                free(coldata[i].indicator);
	}
	free(coldata);
	free(datafmt);

	/*
	** We're done processing rows.  Let's check the final return
	** value of ct_fetch().
	*/
	switch ((int)retcode)
	{
		case CS_END_DATA:
			/*
			** Everything went fine.
			*/
			EX_GLOBAL_PROTECT(fprintf(stdout, 
			"Thread_%d:All done processing rows - total %d.\n",
				threadnum, row_count));
			fflush(stdout);
			retcode = CS_SUCCEED;
			break;

		case CS_FAIL:
			/*
			** Something terrible happened.
			*/
			ex_error(threadnum, "ex_fetch_data: ct_fetch() failed");
			return retcode;
			/*NOTREACHED*/
			break;

		default:
			/*
			** We got an unexpected return value.
			*/
			ex_error(threadnum, 
		"ex_fetch_data: ct_fetch() returned an unexpected retcode");
			return retcode;
			/*NOTREACHED*/
			break;

	}

	return retcode;
}

/*****************************************************************************
** 
** sql based functions 
** 
*****************************************************************************/

/*
** ex_create_db()
**
** Type of function:
**      example program utility api
**
** Purpose:
**      This routine creates a database and opens it. It first checks
**      that the database does not already exists. If it does exist
**      the database is dropped before creating a new one.
**
** Parameters:
**      connection      - Pointer to CS_CONNECTION structure.
**      dbname          - The name to be used for the created database.
**
** Return:
**      Result of functions called in CT-Lib.
**
*/

CS_RETCODE CS_PUBLIC
ex_create_db(connection, dbname)
CS_CONNECTION   *connection;
char            *dbname;
{
        CS_RETCODE      retcode;
        CS_CHAR         *cmdbuf;
        CS_INT  	threadnum = 0;
 
        /*               
        ** Get Connection specific data - get thread number from the
        ** user data stored in connection structure
        */
        (void)ex_get_threadnum(NULL, connection, &threadnum); 

        /*
        ** If the database already exists, drop it.
        */
        if ((retcode = ex_remove_db(connection, dbname)) != CS_SUCCEED)
        {
                ex_error(threadnum, "ex_create_db: ex_remove_db() failed");
        }

        /*
        ** Allocate the buffer for the command string.
        */
        cmdbuf = (CS_CHAR *) malloc(EX_BUFSIZE);
        if (cmdbuf == (CS_CHAR *)NULL)
        {
                ex_error(threadnum, "ex_create_db: malloc() failed");
                return CS_FAIL;
        }

        /*
        ** Set up and send the command to create the database.
        */
        sprintf(cmdbuf, "create database %s", dbname);
        if ((retcode = ex_execute_cmd(connection, cmdbuf)) != CS_SUCCEED)
        {
                ex_error(threadnum, 
			"ex_create_db: ex_execute_cmd(create db) failed");
        }
	free(cmdbuf);
	return retcode;
}

/*
** ex_remove_db()
**
** Type of function:
**      example program utility api
**
** Purpose:
**      This routine removes a database. It first checks that
**      the database exists, and if so, removes it.
**
** Parameters:
**      connection      - Pointer to CS_CONNECTION structure.
**      dbname          - The name of the database to remove.
**
** Return:
**      Result of functions called in CT-Lib or CS_FAIL if a malloc failure
**      occurred.
*/

CS_RETCODE
ex_remove_db(connection, dbname)
CS_CONNECTION   *connection;
char            *dbname;
{
        CS_RETCODE      retcode;
        CS_CHAR         *cmdbuf;
        CS_INT  	threadnum = 0;
 
        /*               
        ** Get Connection specific data - get thread number from the
        ** user data stored in connection structure
        */
        (void)ex_get_threadnum(NULL, connection, &threadnum); 

        /*
        ** Connect to the master database in order to
        ** remove the specified database.
        */
        if ((retcode = ex_use_db(connection, "master")) != CS_SUCCEED)
        {
                ex_error(threadnum, "ex_remove_db: ex_use_db(master) failed");
                return retcode;
        }

        /*
        ** Allocate the buffer for the command string.
        */
        cmdbuf = (CS_CHAR *) malloc(EX_BUFSIZE);
        if (cmdbuf == (CS_CHAR *)NULL)
        {
                ex_error(threadnum, "ex_remove_db: malloc() failed");
                return CS_FAIL;
        }

        /*
        ** Set up and send the command to check for and drop the
        ** database if it exists.
        */
        sprintf(cmdbuf,
                "if exists (select name from sysdatabases where name = \"%s\") \
                drop database %s", dbname, dbname);
        if ((retcode = ex_execute_cmd(connection, cmdbuf)) != CS_SUCCEED)
        {
                ex_error(threadnum, 
			"ex_remove_db: ex_execute_cmd(drop db) failed");
        }
        free(cmdbuf);
        return retcode;
}

/*
** ex_use_db()
**
** Type of function:
**      example program utility api
**
** Purpose:
**      This routine changes the current database to the named db passed in.
**
** Parameters:
**      connection      - Pointer to CS_CONNECTION structure.
**      dbname          - The name of the database to use.
**
** Return:
**      Result of functions called in CT-Lib or CS_FAIL if a malloc failure
**      occured.
*/

CS_RETCODE
ex_use_db(connection, dbname)
CS_CONNECTION   *connection;
char            *dbname;
{
        CS_RETCODE      retcode;
        CS_CHAR         *cmdbuf;
        CS_INT  threadnum = 0;
 
        /*               
        ** Get Connection specific data - get thread number from the
        ** user data stored in connection structure
        */
        (void)ex_get_threadnum(NULL, connection, &threadnum); 

        /*
        ** Allocate the buffer for the command string.
        */
        cmdbuf = (CS_CHAR *) malloc(EX_BUFSIZE);
        if (cmdbuf == (CS_CHAR *)NULL)
        {
                ex_error(threadnum, "ex_use_db: malloc() failed");
                return CS_FAIL;
        }

        /*
        ** Set up and send the command to use the database
        */
	sprintf(cmdbuf, "use %s\n", dbname);
        if ((retcode = ex_execute_cmd(connection, cmdbuf)) != CS_SUCCEED)
        {
                ex_error(threadnum, "ex_use_db: ex_execute_cmd(use db) failed");
        }
        free(cmdbuf);
        return retcode;
}

/*
** ex_get_threadnum()
**
** Type of function:
**      example program utility api
**
** Purpose:
**	Retrieve thread number from connection or context level user data.
**
**	NOTE that, application can store thread level data into connection or
**	context user data (as in this sample) for use in callbacks. For
**	other purposes, Operating System provided primitives for thread 
**	specific data (also known as thread local data) may also be used. 
**
** Parameters:
**	context		- Pointer to CS_CONTEXT structure
**      connection      - Pointer to CS_CONNECTION structure.
**	threadnum	- Pointer to CS_INT - that receives the user data
**
** Return:
**      Result of functions called in CT-Lib 
*/
 
CS_RETCODE
ex_get_threadnum(context, connection, threadnum)
CS_CONTEXT	*context;
CS_CONNECTION   *connection;
CS_INT		*threadnum;
{
        CS_RETCODE      retcode;
 
        if (connection != (CS_CONNECTION *)NULL)
        {
                retcode = ct_con_props(connection, CS_GET, CS_USERDATA,
				threadnum, CS_SIZEOF(CS_INT), (CS_INT *)NULL);
		if (retcode != CS_SUCCEED)
		{
			ex_error(0,
		    "Thread_unknown:ct_con_props(CS_GET CS_USERDATA) failed\n");
		}
        }
        else
        {
                retcode = cs_config(context, CS_GET, CS_USERDATA, threadnum,
                                        CS_SIZEOF(CS_INT), (CS_INT *)NULL);
		if (retcode != CS_SUCCEED)
		{
			ex_error(0,
		    "Thread_unknown:cs_config(CS_GET CS_USERDATA) failed\n");
		}
        }
	return(retcode);
}

⌨️ 快捷键说明

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