📄 multthrd.c
字号:
break;
default:
/* ignore */
break;
}
}
/*
** Deallocate the allocated structures, close the connection,
** and exit Client-Library.
*/
if (connection != NULL)
{
retcode = ex_con_cleanup(connection, retcode);
}
/* signal waiter */
(void)ex_release_sem(Ex_threadcompsem);
/* Exit the thread */
return(retcode);
}
/*
** DoCursor(connection, task)
**
** Type of function:
** sample program internal api
**
** Purpose:
** This function is the core of the cursor example. It declares
** and opens a cursor. It then processes the results in the
** standard ct_results() loop, calling ex_fetch_data()
** when the type of result is CS_CURSOR_RESULT.
**
** Parameters:
** connection - Pointer to connection structure
** task - pointer to EX_TASK entry with sql query and db name
**
** Return:
** CS_SUCCEED if cursor result set was processed correctly
** Otherwise a Client-Library failure code.
**
*/
CS_STATIC CS_RETCODE
DoCursor(connection, task)
CS_CONNECTION *connection;
EX_TASK *task;
{
CS_RETCODE retcode;
CS_COMMAND *cmd;
CS_CHAR msg[512];
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);
/*
** NOTE: Add capability check here
*/
/*
** Use the specified database
*/
if ((retcode = ex_use_db(connection, task->dbname)) != CS_SUCCEED)
{
sprintf(msg, "DoCursor: ex_use_db(%s) failed", task->dbname);
ex_error(threadnum, msg);
return retcode;
}
/*
** Allocate a command handle to declare the cursor on.
*/
if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cmd_alloc() failed");
return retcode;
}
/*
** Declare the cursor
*/
retcode = ct_cursor(cmd, CS_CURSOR_DECLARE, "cursor_a", CS_NULLTERM,
task->query, CS_NULLTERM, CS_READ_ONLY);
if (retcode != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cursor(declare) failed");
return retcode;
}
/*
** Set cursor rows to 10
*/
retcode = ct_cursor(cmd, CS_CURSOR_ROWS, NULL, CS_UNUSED, NULL,
CS_UNUSED, (CS_INT)10);
if (retcode != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cursor(currows) failed");
return retcode;
}
/*
** Open the cursor.
*/
retcode = ct_cursor(cmd, CS_CURSOR_OPEN, NULL, CS_UNUSED, NULL,
CS_UNUSED, CS_UNUSED);
if (retcode != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cursor() failed");
return retcode;
}
/*
** Send (batch) the last 3 cursor commands to the server,
** and check results. It will generate cursor result set.
*/
if ((retcode = SendCmdAndCheckResults(threadnum, cmd,
CS_CURSOR_RESULT)) != CS_SUCCEED)
{
/* error already reported */
return retcode;
}
/*
** Close and deallocate the cursor. Note that we don't have to do
** this, since it is done automatically when the connection is
** closed.
*/
retcode = ct_cursor(cmd, CS_CURSOR_CLOSE, NULL, CS_UNUSED, NULL,
CS_UNUSED, CS_DEALLOC);
if (retcode != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cursor(dealloc) failed");
return retcode;
}
/*
** Send the cursor command to the server and check its results.
** The command won't generate a result set.
*/
if ((retcode = SendCmdAndCheckResults(threadnum, cmd, CS_CMD_SUCCEED))
!= CS_SUCCEED)
{
/* error already reported */
return retcode;
}
/*
** Drop the cursor's command structure
*/
if ((retcode = ct_cmd_drop(cmd)) != CS_SUCCEED)
{
ex_error(threadnum, "DoCursor: ct_cmd_drop() failed");
return retcode;
}
return retcode;
}
/*
** DoQuery(connection, task)
**
** Type of function:
** sample program internal api
**
** Purpose:
** Synchronously sends a query to server and processes results
** in a standard ct_results loop, calling ex_fetch_data()
** when the type of result is CS_ROW_RESULT.
**
** Parameters:
** connection - Pointer to connection structure
** task - pointer to EX_TASK entry with sql query and db name
**
** Return:
** CS_SUCCEED if regular row result set was processed correctly
** Otherwise a Client-Library failure code.
**
*/
CS_STATIC CS_RETCODE
DoQuery(connection, task)
CS_CONNECTION *connection;
EX_TASK *task;
{
CS_RETCODE retcode;
CS_COMMAND *cmd;
CS_CHAR msg[512];
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);
/*
** Use the specified database
*/
if ((retcode = ex_use_db(connection, task->dbname)) != CS_SUCCEED)
{
sprintf(msg, "DoQuery: ex_use_db(%s) failed", task->dbname);
ex_error(threadnum, msg);
return retcode;
}
/*
** Allocate a command handle to send the query
*/
if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED)
{
ex_error(threadnum, "DoQuery: ct_cmd_alloc() failed");
return retcode;
}
/*
** Initiate the command with the specified query
*/
retcode = ct_command(cmd, CS_LANG_CMD, task->query,
CS_NULLTERM, CS_UNUSED);
if (retcode != CS_SUCCEED)
{
sprintf(msg, "DoQuery: ct_command(CS_LANG_CMD:%s) failed",
task->query);
ex_error(threadnum, msg);
return retcode;
}
/*
** Send the command to the server and check its results.
** It will generate regular row result set.
*/
if ((retcode = SendCmdAndCheckResults(threadnum, cmd, CS_ROW_RESULT))
!= CS_SUCCEED)
{
/* error already reported */
return retcode;
}
/*
** Drop our command structure
*/
if ((retcode = ct_cmd_drop(cmd)) != CS_SUCCEED)
{
ex_error(threadnum, "DoQuery: ct_cmd_drop() failed");
return retcode;
}
return retcode;
}
/*
** SendCmdAndCheckResults(thrdnum, cmd, restype)
**
** Type of function:
** sample program internal api
**
** Purpose:
** It sends a command to server and processes the results in the
** standard ct_results() loop, calling ex_fetch_data() when the
** type of result is CS_CURSOR_RESULT or CS_ROW_RESULT.
**
** Parameters:
** threadnum - current thread number (logical)
** cmd - pointer to command structure
** restype - restype expected
**
** Return:
** CS_SUCCEED if result set was processed correctly
** Otherwise a Client-Library failure code.
**
*/
CS_STATIC CS_RETCODE
SendCmdAndCheckResults(threadnum, cmd, restype)
CS_INT threadnum;
CS_COMMAND *cmd;
CS_INT restype;
{
CS_INT res_type;
CS_RETCODE retcode;
/*
** Send the command to the server
*/
if ((retcode = ct_send(cmd)) != CS_SUCCEED)
{
ex_error(threadnum, "SendCmdAndCheckResults: ct_send() failed");
return retcode;
}
/*
** Process the results. Loop while ct_results() returns CS_SUCCEED.
*/
while((retcode = ct_results(cmd, &res_type)) == CS_SUCCEED)
{
switch ((int)res_type)
{
case CS_CMD_SUCCEED:
/*
** This means no rows were returned.
*/
break;
case CS_CMD_DONE:
/*
** This means we're done with one result set.
*/
break;
case CS_CMD_FAIL:
/*
** This means that the server encountered an error while
** processing our command.
*/
ex_error(threadnum,
"SendCmdAndCheckResults: ct_results() returned CMD_FAIL");
break;
case CS_CURSOR_RESULT:
case CS_ROW_RESULT:
/* Did we get expected result type? */
if (restype == res_type)
{
retcode = ex_fetch_data(cmd);
if (retcode != CS_SUCCEED)
{
ex_error(threadnum,
"SendCmdAndCheckResults: ex_fetch_data() failed");
return retcode;
}
break;
}
/* otherwise fall through to default */
default:
/*
** We got an unexpected result type.
*/
ex_error(threadnum,
"SendCmdAndCheckResults: ct_results() returned unexpected result type");
return CS_FAIL;
}
}
/*
** We're done processing results. Let's check the
** return value of ct_results() to see if everything
** went ok.
*/
switch ((int)retcode)
{
case CS_END_RESULTS:
/*
** Everything went fine.
*/
break;
case CS_FAIL:
/*
** Something terrible happened.
*/
ex_error(threadnum,
"SendCmdAndCheckResults: ct_results() failed");
return retcode;
default:
/*
** We got an unexpected return value.
*/
ex_error(threadnum,
"SendCmdAndCheckResults: ct_results() returned unexpected result code");
return retcode;
}
/* return success */
return CS_SUCCEED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -