📄 ctlibadapter.cpp
字号:
{
sprintf(rowsql, "set rowcount %d", row);
if(execute(s->stmt, rowsql, timeout) < 0)
m_pPool->PrintLog("set rowcount failed.");
}
row_num = execute(s->stmt, sql, timeout);
if(row > 0)
{
if(execute(s->stmt, "set rowcount 0", timeout) < 0)
m_pPool->PrintLog("reset rowcount failed.");
}
return row_num;
}
int CCTLIBAdapter::Procedure(void *stmt, char *procname, DbProc_t *params, int nparam, int timeout)
{
int i, iVal, ins;
CS_INT result, cols;
CS_RETCODE ret;
float fVal;
CTL_Statement_t *s = (CTL_Statement_t*)stmt;
CS_DATAFMT tformat;
char rstatus[10];
if(stmt==NULL || procname==NULL || nparam<0 || nparam>0 && params==NULL || m_pContext==NULL)
return -1;
ct_cancel(NULL, s->stmt, CS_CANCEL_ALL);
if(ct_command(s->stmt, CS_RPC_CMD, procname, CS_NULLTERM, CS_RECOMPILE) != CS_SUCCEED)
{
m_pPool->PrintLog("set procedure name failed.");
return -1;
}
ins = 0;
if(params != NULL)
{
for(i=0; i<nparam; i++)
{
memset(&tformat, 0, sizeof(tformat));
tformat.format = CS_FMT_NULLTERM;
tformat.datatype = DbType(params[i].type);
switch(params[i].io)
{
case DBIO_IN:
ins++;
tformat.status = CS_INPUTVALUE;
if(params[i].null)
{
tformat.maxlength = CS_UNUSED;
if(ct_param(s->stmt, &tformat, NULL, 0, CS_UNUSED) != CS_SUCCEED)
{
m_pPool->PrintLog("bind null parameter failed.");
return -1;
}
}
else
{
switch(params[i].type)
{
case DBTYPE_INT:
tformat.maxlength = CS_UNUSED;
iVal = atoi(params[i].value);
ret = ct_param(s->stmt, &tformat, &iVal, sizeof(iVal), CS_UNUSED);
break;
case DBTYPE_STRING:
case DBTYPE_DATETIME:
tformat.datatype = CS_CHAR_TYPE;
tformat.maxlength = MAX_VALUE_LENGTH;
ret = ct_param(s->stmt, &tformat, params[i].value, strlen(params[i].value), CS_UNUSED);
break;
case DBTYPE_FLOAT:
tformat.maxlength = CS_UNUSED;
fVal = (float)atof(params[i].value);
ret = ct_param(s->stmt, &tformat, &fVal, sizeof(fVal), CS_UNUSED);
break;
default:
m_pPool->PrintLog("DB data type is invalid.");
return -1;
}
break;
case DBIO_OUT:
tformat.status = CS_RETURN;
tformat.maxlength = MAX_VALUE_LENGTH;
ret = ct_param(s->stmt, &tformat, params[i].value, 0, CS_UNUSED);
break;
default:
return -1;
}
if(ret != CS_SUCCEED)
{
m_pPool->PrintLog("set procedure parameter value failed.");
return -1;
}
}
}
}
if(ct_send(s->stmt) != CS_SUCCEED)
{
m_pPool->PrintLog("send statement failed.");
return -1;
}
if(timeout > 0)
{
if(ct_config(m_pContext, CS_SET, CS_TIMEOUT, &timeout, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("set timeout failed.");
return -1;
}
}
if(ct_results(s->stmt, &result)!=CS_SUCCEED || result!=CS_STATUS_RESULT)
{
m_pPool->PrintLog("get procedure result failed.");
return -1;
}
if(ct_res_info(s->stmt, CS_NUMDATA, &cols, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("get procedure result columns failed.");
return -1;
}
if(cols != 1)
{
m_pPool->PrintLog("call procedure error.");
return -1;
}
memset(&tformat, 0, sizeof(tformat));
tformat.datatype = CS_CHAR_TYPE;
tformat.maxlength = MAX_VALUE_LENGTH;
tformat.count = 1;
tformat.format = CS_FMT_NULLTERM;
if(ct_bind(s->stmt, 1, &tformat, rstatus, NULL, &s->indicator[0]) != CS_SUCCEED)
{
m_pPool->PrintLog("bind return status buffer failed.");
return -1;
}
if(ct_fetch(s->stmt, CS_UNUSED, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("fetch return status failed.");
return -1;
}
if(atoi(rstatus) != 0)
{
m_pPool->PrintLog("return status is incorrect.");
return -1;
}
if(ct_fetch(s->stmt, CS_UNUSED, CS_UNUSED, CS_UNUSED, NULL) != CS_END_DATA)
{
m_pPool->PrintLog("finish return status failed.");
return -1;
}
if(nparam-ins > 0)
{
if(ct_results(s->stmt, &result)!=CS_SUCCEED || result!=CS_PARAM_RESULT)
{
m_pPool->PrintLog("get procedure result failed.");
return -1;
}
if(ct_res_info(s->stmt, CS_NUMDATA, &cols, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("get procedure result columns failed.");
return -1;
}
if(cols <= 0)
{
m_pPool->PrintLog("get output parameter number failed.");
return -1;
}
for(i=0; i<cols; i++)
{
if(ct_bind(s->stmt, i+1, &tformat, params[i+ins].value, NULL, &s->indicator[i]) != CS_SUCCEED)
{
m_pPool->PrintLog("bind procedure output buffer failed.");
return -1;
}
}
if(ct_fetch(s->stmt, CS_UNUSED, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("get procedure param failed.");
return -1;
}
for(i=ins; i<nparam; i++)
{
if(s->indicator[i] == -1)
params[i].null = true;
else
params[i].null = false;
}
}
return ins;
}
DB_TYPE CCTLIBAdapter::DbType(int type)
{
DB_TYPE res;
switch(type)
{
case CS_VARCHAR_TYPE:
case CS_CHAR_TYPE:
res = DBTYPE_STRING;
break;
case CS_DATETIME4_TYPE:
case CS_DATETIME_TYPE:
res = DBTYPE_DATETIME;
break;
case CS_DECIMAL_TYPE:
case CS_NUMERIC_TYPE:
case CS_TINYINT_TYPE:
case CS_SMALLINT_TYPE:
case CS_INT_TYPE:
res = DBTYPE_INT;
break;
case CS_REAL_TYPE:
case CS_FLOAT_TYPE:
res = DBTYPE_FLOAT;
break;
default:
res = DBTYPE_STRING;
break;
}
return res;
}
int CCTLIBAdapter::DbType(DB_TYPE type)
{
int res;
switch(type)
{
case DBTYPE_STRING:
res = CS_CHAR_TYPE;
break;
case DBTYPE_INT:
res = CS_INT_TYPE;
break;
case DBTYPE_FLOAT:
res = CS_REAL_TYPE;
break;
case DBTYPE_DATETIME:
res = CS_DATETIME_TYPE;
break;
default:
res = CS_CHAR_TYPE;
break;
}
return res;
}
/**************************************************************/
void CCTLIBAdapter::release()
{
if(m_pContext != NULL)
{
ct_exit(m_pContext, CS_FORCE_EXIT);
cs_ctx_drop(m_pContext);
m_pContext = NULL;
}
}
int CCTLIBAdapter::execute(void *stmt, char *sql, int timeout)
{
CS_INT result;
CS_INT num_rows;
char buf[1024];
ct_cancel(NULL, (CS_COMMAND*)stmt, CS_CANCEL_ALL);
if(ct_command((CS_COMMAND*)stmt, CS_LANG_CMD, sql, CS_NULLTERM, CS_UNUSED) != CS_SUCCEED)
{
m_pPool->PrintLog("set execute sql failed.");
return -1;
}
if(ct_send((CS_COMMAND*)stmt) != CS_SUCCEED)
{
m_pPool->PrintLog("send execute failed.");
return -1;
}
if(timeout > 0)
{
if(ct_config(m_pContext, CS_SET, CS_TIMEOUT, &timeout, CS_UNUSED, NULL) != CS_SUCCEED)
m_pPool->PrintLog("set execute timeout failed.");
}
if(ct_results((CS_COMMAND*)stmt, &result) != CS_SUCCEED)
{
m_pPool->PrintLog("get execute results failed.");
return false;
}
if(result== CS_CMD_SUCCEED || result == CS_CMD_DONE)
{
if(ct_res_info((CS_COMMAND*)stmt, CS_ROW_COUNT, &num_rows, CS_UNUSED, NULL) != CS_SUCCEED)
{
m_pPool->PrintLog("get execute rows failed.");
return -1;
}
}
else
{
sprintf(buf, "execute result type %d is error.", result);
m_pPool->PrintLog(buf);
return -1;
}
if(num_rows < 0)
return 0;
else
return num_rows;
}
/****************************************************************/
CS_RETCODE CS_PUBLIC myctlib_server_msg_handler( CS_CONTEXT *ctx, CS_CONNECTION *conn, CS_SERVERMSG *srvr_msgtxt)
{
printf(srvr_msgtxt->text);
printf("\n");
return CS_SUCCEED;
}
CS_RETCODE CS_PUBLIC myctlib_client_msg_handler( CS_CONTEXT *ctx, CS_CONNECTION *conn, CS_CLIENTMSG *client_msgtxt)
{
printf(client_msgtxt->msgstring);
printf("\n");
return CS_SUCCEED;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -