/* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */ int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int i, verbose = 0; CS_RETCODE ret; CS_RETCODE ret2; CS_RETCODE results_ret; CS_INT result_type; CS_INT num_cols; CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_INT count, row_count = 0; CS_INT id; CS_CHAR name[600]; CS_CHAR *nameptr; CS_INT getlen; char large_sql[1024]; char len600[601]; char len800[801]; char temp[11]; char *textptr; CS_IODESC iodesc; len600[0] = 0; name[0] = 0; for (i = 0; i < 60; i++) { sprintf(temp, "_abcde_%03d", (i + 1) * 10); strcat(len600, temp); } len600[600] = '\0'; len800[0] = 0; for (i = 0; i < 80; i++) { sprintf(temp, "_zzzzz_%03d", (i + 1) * 10); strcat(len800, temp); } len600[800] = '\0'; fprintf(stdout, "%s: Retrieve CS_TEXT_TYPE using ct_bind()\n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } /* do not test error */ ret = run_command(cmd, "DROP TABLE #test_table"); ret = run_command(cmd, "CREATE TABLE #test_table (id int, name text)"); if (ret != CS_SUCCEED) return 1; sprintf(large_sql, "INSERT #test_table (id, name) VALUES (2, '%s')", len600); ret = run_command(cmd, large_sql); if (ret != CS_SUCCEED) return 1; ret = ct_command(cmd, CS_LANG_CMD, "SELECT id, name FROM #test_table", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 2) { fprintf(stderr, "num_cols %d != 2", num_cols); return 1; } ret = ct_describe(cmd, 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.format = CS_FMT_UNUSED; if (datafmt.maxlength > 1024) { datafmt.maxlength = 1024; } ret = ct_bind(cmd, 1, &datafmt, &id, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else { /* ret == CS_SUCCEED */ if (verbose) { fprintf(stdout, "id = '%d'\n", id); } nameptr = name; while ((ret2 = ct_get_data(cmd, 2 , nameptr, 200, &getlen )) == CS_SUCCEED) { nameptr += getlen; } if (ret2 != CS_END_DATA) { fprintf(stderr, "ct_get_data() failed\n"); return 1; } if (memcmp(name, len600, 600)) { fprintf(stderr, "Bad return data\n"); return 1; } fprintf(stdout, "%s: Trying ct_data_info on text column\n", __FILE__); if (ct_data_info(cmd, CS_GET, 2, &iodesc) != CS_SUCCEED) { fprintf(stderr, "ct_data_info() failed\n"); return 1; } else { fprintf(stdout, "datatype = %d\n", iodesc.datatype); fprintf(stdout, "usertype = %d\n", iodesc.usertype); fprintf(stdout, "text len = %d\n", iodesc.total_txtlen); fprintf(stdout, "name = %*.*s\n", iodesc.namelen, iodesc.namelen, iodesc.name); } } } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } if ((ret = ct_command(cmd, CS_SEND_DATA_CMD, NULL, CS_UNUSED, CS_COLUMN_DATA)) != CS_SUCCEED) { fprintf(stderr, "ct_command(CS_SEND_DATA_CMD) failed.\n"); return 1; } iodesc.total_txtlen = 800; iodesc.log_on_update = CS_TRUE; if (ct_data_info(cmd, CS_SET, CS_UNUSED, &iodesc) != CS_SUCCEED) { fprintf(stderr, "ct_data_info() failed\n"); return 1; } for ( i = 0 ; i < 800 ; i += 200 ) { textptr = &len800[i]; ret = ct_send_data(cmd, textptr, (CS_INT) 200); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send_data failed\n"); return 1; } } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: break; case CS_PARAM_RESULT: break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } if (verbose) { fprintf(stdout, "Trying logout\n"); } ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 1; } return 0; }
int sp_who(CS_COMMAND *cmd) { enum {maxcol=10, colsize=260}; struct _col { CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_CHAR data[colsize]; } col[maxcol]; CS_INT num_cols; CS_INT count, row_count = 0; CS_INT result_type; CS_RETCODE ret; CS_RETCODE results_ret; int i; int is_status_result=0; ret = ct_command(cmd, CS_LANG_CMD, "exec sp_who", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command: \"exec sp_who\" failed with %d\n", ret); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send: \"exec sp_who\" failed with %d\n", ret); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { is_status_result = 0; switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_STATUS_RESULT: fprintf(stdout, "ct_results: CS_STATUS_RESULT detected for sp_who\n"); is_status_result = 1; /* fall through */ case CS_ROW_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED || num_cols > maxcol) { fprintf(stderr, "ct_res_info() failed\n"); return 1; } if (num_cols <= 0) { fprintf(stderr, "ct_res_info() return strange values\n"); return 1; } if (is_status_result && num_cols != 1) { fprintf(stderr, "CS_STATUS_RESULT return more than 1 column\n"); return 1; } for (i=0; i < num_cols; i++) { /* here we can finally test for the return status column */ ret = ct_describe(cmd, i+1, &col[i].datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed for column %d\n", i); return 1; } if (col[i].datafmt.status & CS_RETURN) { fprintf(stdout, "ct_describe() indicates a return code in column %d for sp_who\n", i); /* * other possible values: * CS_CANBENULL * CS_HIDDEN * CS_IDENTITY * CS_KEY * CS_VERSION_KEY * CS_TIMESTAMP * CS_UPDATABLE * CS_UPDATECOL */ } col[i].datafmt.datatype = CS_CHAR_TYPE; col[i].datafmt.format = CS_FMT_NULLTERM; col[i].datafmt.maxlength = colsize; col[i].datafmt.count = 1; col[i].datafmt.locale = NULL; ret = ct_bind(cmd, i+1, &col[i].datafmt, &col[i].data, &col[i].datalength, &col[i].ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } row_count = 0; while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) { if( row_count == 0) { /* titles */ for (i=0; i < num_cols; i++) { char fmt[40]; if (col[i].datafmt.namelen == 0) { printf("unnamed%d ",i+1); continue; } sprintf(fmt, "%%-%d.%ds ", col[i].datafmt.namelen, col[i].datafmt.maxlength); printf(fmt, col[i].datafmt.name); } printf("\n"); } for (i=0; i < num_cols; i++) { /* data */ char fmt[40]; if (col[i].ind) continue; sprintf(fmt, "%%-%d.%ds ", col[i].datalength, col[i].datafmt.maxlength); printf(fmt, col[i].data); if (is_status_result && strcmp(col[i].data,"0")) { fprintf(stderr, "CS_STATUS_RESULT should return 0 as result\n"); return 1; } } printf("\n"); row_count += count; } if (is_status_result && row_count != 1) { fprintf(stderr, "CS_STATUS_RESULT should return a row\n"); return 1; } switch ((int) ret) { case CS_END_DATA: fprintf(stdout, "ct_results fetched %d rows.\n", row_count); break; case CS_ROW_FAIL: fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } return 0; }
CTL_RowResult::CTL_RowResult(CS_COMMAND* cmd, CTL_Connection& conn) : m_Connect(&conn), m_Cmd(cmd), m_CurrItem(-1), m_EOR(false), m_BindedCols(0) { CheckIsDead(); CS_INT outlen; CS_INT nof_cols; bool rc = (Check(ct_res_info(x_GetSybaseCmd(), CS_NUMDATA, &nof_cols, CS_UNUSED, &outlen)) != CS_SUCCEED); CHECK_DRIVER_ERROR( rc, "ct_res_info(CS_NUMDATA) failed." + GetDbgInfo(), 130001 ); CS_INT bind_len = 0; m_BindedCols = 0; bool buff_is_full = false; m_ColFmt = AutoArray<CS_DATAFMT>(nof_cols); m_NullValue = AutoArray<ENullValue>(nof_cols); for (unsigned int nof_item = 0; nof_item < (unsigned int) nof_cols; nof_item++) { rc = (Check(ct_describe(x_GetSybaseCmd(), (CS_INT) nof_item + 1, &m_ColFmt[nof_item])) != CS_SUCCEED); CHECK_DRIVER_ERROR( rc, "ct_describe failed." + GetDbgInfo(), 130002 ); m_NullValue[nof_item] = eNullUnknown; #ifdef FTDS_IN_USE // Seems like FreeTDS reports wrong maxlength in // ct_describe() - fix this when binding to a buffer. if (m_ColFmt[nof_item].datatype == CS_NUMERIC_TYPE || m_ColFmt[nof_item].datatype == CS_DECIMAL_TYPE ) { m_ColFmt[nof_item].maxlength = sizeof(CS_NUMERIC); } #endif m_CachedRowInfo.Add( string(m_ColFmt[nof_item].name, m_ColFmt[nof_item].namelen), m_ColFmt[nof_item].maxlength, ConvDataType_Ctlib2DBAPI(m_ColFmt[nof_item]) ); if (!buff_is_full) { if (m_ColFmt[nof_item].maxlength > 2048 || m_ColFmt[nof_item].datatype == CS_IMAGE_TYPE) { buff_is_full = true; } else { bind_len += m_ColFmt[nof_item].maxlength; if (bind_len <= 2048) { m_BindedCols++; } else { buff_is_full = true; } } } } if(m_BindedCols) { m_BindItem = AutoArray<CS_VOID*>(m_BindedCols); m_Copied = AutoArray<CS_INT>(m_BindedCols); m_Indicator = AutoArray<CS_SMALLINT>(m_BindedCols); for(int i= 0; i < m_BindedCols; i++) { m_BindItem[i] = (i ? ((unsigned char*)(m_BindItem[i-1])) + m_ColFmt[i-1].maxlength : m_BindBuff); rc = (Check(ct_bind(x_GetSybaseCmd(), i+1, &m_ColFmt[i], m_BindItem[i], &m_Copied[i], &m_Indicator[i]) ) != CS_SUCCEED); CHECK_DRIVER_ERROR( rc, "ct_bind failed." + GetDbgInfo(), 130042 ); } } }
int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; CS_COMMAND *cmd2; CS_COMMAND *cmd3; CS_RETCODE ret; CS_RETCODE ret2; CS_RETCODE results_ret; CS_INT result_type; CS_INT count, count2, row_count = 0; CS_DATAFMT datafmt; CS_DATAFMT datafmt2; CS_SMALLINT ind; int verbose = 1; CS_CHAR *name = "c1"; CS_CHAR *name2 = "c2"; CS_CHAR col1[6]; CS_CHAR col2[6]; CS_INT datalength; CS_CHAR text[128]; CS_INT num_cols, i; int is_return_status = 0; fprintf(stdout, "%s: use multiple cursors on the same connection\n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } ret = ct_cmd_alloc(conn, &cmd2); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } return ret; } ret = ct_cmd_alloc(conn, &cmd3); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } return ret; } ret = run_command(cmd, "CREATE TABLE #test_table (col1 char(4))"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('AAA')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('BBB')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('CCC')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "CREATE TABLE #test_table2 (col1 char(4))"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('XXX')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('YYY')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('ZZZ')"); if (ret != CS_SUCCEED) return 1; if (verbose) { fprintf(stdout, "opening first cursor on connection\n"); } strcpy(text, "select col1 from #test_table order by col1"); ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor declare failed\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor set cursor rows failed"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor open failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: case CS_CMD_DONE: case CS_CMD_FAIL: case CS_STATUS_RESULT: break; case CS_CURSOR_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 1) { fprintf(stderr, "unexpected num of columns = %d \n", num_cols); return 1; } for (i = 0; i < num_cols; i++) { /* here we can finally test for the return status column */ ret = ct_describe(cmd, i + 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed for column %d\n", i); return 1; } if (datafmt.status & CS_RETURN) { fprintf(stdout, "ct_describe() column %d \n", i); is_return_status = i + 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 6; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } if (verbose) { fprintf(stdout, "opening second cursor on connection\n"); } strcpy(text, "select col1 from #test_table2 order by col1"); ret = ct_cursor(cmd2, CS_CURSOR_DECLARE, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor declare failed\n"); return 1; } ret = ct_cursor(cmd2, CS_CURSOR_ROWS, name2, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor set cursor rows failed"); return 1; } ret = ct_cursor(cmd2, CS_CURSOR_OPEN, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor open failed\n"); return 1; } ret = ct_send(cmd2); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); return 1; } while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: case CS_CMD_DONE: case CS_CMD_FAIL: case CS_STATUS_RESULT: break; case CS_CURSOR_RESULT: ret = ct_res_info(cmd2, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 1) { fprintf(stderr, "unexpected num of columns = %d \n", num_cols); return 1; } for (i = 0; i < num_cols; i++) { /* here we can finally test for the return status column */ ret = ct_describe(cmd2, i + 1, &datafmt2); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed for column %d\n", i); return 1; } if (datafmt2.status & CS_RETURN) { fprintf(stdout, "ct_describe() column %d \n", i); is_return_status = i + 1; } datafmt2.datatype = CS_CHAR_TYPE; datafmt2.format = CS_FMT_NULLTERM; datafmt2.maxlength = 6; datafmt2.count = 1; datafmt2.locale = NULL; ret = ct_bind(cmd2, 1, &datafmt2, col2, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } row_count = 0; while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { ret2 = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count2); if (ret == CS_SUCCEED && ret2 == CS_SUCCEED) { printf("%s\t\t\t%s\n", col1, col2); } } switch ((int) ret) { case CS_END_DATA: break; case CS_ROW_FAIL: fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } if (verbose) { fprintf(stdout, "closing first cursor on connection\n"); } ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(close) failed\n"); return ret; } if ((ret = ct_send(cmd)) != CS_SUCCEED) { fprintf(stderr, "BILL ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_DEALLOC, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(dealloc) failed\n"); return ret; } if ((ret = ct_send(cmd)) != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } if (verbose) { fprintf(stdout, "closing second cursor on connection\n"); } ret = ct_cursor(cmd2, CS_CURSOR_CLOSE, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(close) failed\n"); return ret; } if ((ret = ct_send(cmd2)) != CS_SUCCEED) { fprintf(stderr, "BILL ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cursor(cmd2, CS_CURSOR_DEALLOC, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(dealloc) failed\n"); return ret; } if ((ret = ct_send(cmd2)) != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ct_cmd_drop(cmd2); ct_cmd_drop(cmd3); if (verbose) { fprintf(stdout, "Trying logout\n"); } ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 2; } if (verbose) { fprintf(stdout, "Test succeded\n"); } return 0; }
/** * @brief read data from sybase ASE server TBL_DataUpdateFlag * * @return */ int CSybClient::read_DataUpdateFlag(int connId, char* tablename, int flagid, stDataUpdateTime* pData) { CS_CONNECTION *connection = getConnection(connId); if(NULL == connection || NULL == tablename) return -1; CLEAR_ALL_MESSAGE(connection); CS_RETCODE retcode; CS_COMMAND *cmd; CS_INT res_type; /* result type from ct_results */ CS_INT row_count = 0; /* ** Use the dbname database */ //CS_CHAR usetable[64] = {0}; //sprintf(usetable, "use %s\n", dbname); //if(execNoReplyCmd(connection, usetable) < 0) //{ // return -1; //} /* ** Allocate a command handle to send the query with */ if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); return -1; } /* ** Define a language command. */ CS_CHAR select[256] = {0}; sprintf(select, "select UpdateTime, UpdateFlag from %s where UpdateIndex=%d", tablename, flagid); retcode = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; } /* ** Send the command to the server */ if (ct_send(cmd) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; } /* ** 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. */ DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; case CS_ROW_RESULT: { #define FIELD_UpdateTime 1 //ÒÔselect Óï¾äΪ׼ #define FIELD_UpdateFlag 2 CS_DATETIME updateTime; CS_TINYINT updateFlag; CS_INT i; CS_INT rows_read; CS_DATAFMT dfmt; /* ** Bind to column */ dfmt.datatype = CS_DATETIME_TYPE; dfmt.maxlength = CS_SIZEOF(CS_DATETIME); dfmt.format = CS_FMT_UNUSED; dfmt.count = 0; dfmt.locale = NULL; retcode = ct_bind(cmd, FIELD_UpdateTime, &dfmt, &updateTime, NULL, NULL); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cmd_drop(cmd); return -1; } // dfmt.datatype = CS_TINYINT_TYPE; dfmt.maxlength= CS_SIZEOF(CS_TINYINT); retcode = ct_bind(cmd, FIELD_UpdateFlag, &dfmt, &updateFlag, NULL, NULL); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cmd_drop(cmd); return -1; } CS_DATEREC userdatetime; /* ** 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) { } /* ** We have a row. Loop through the columns displaying the ** column values. */ //printf("%ld rows read from TBL_DataUpdateFlag\n", (long)rows_read); for (i = 0; i < rows_read; i++) { if(cs_dt_crack(context, CS_DATETIME_TYPE, &updateTime, &userdatetime) == CS_SUCCEED) { sprintf(pData->UpdateTime, "%04d-%02d-%02d %02d:%02d:%02d", userdatetime.dateyear, userdatetime.datemonth+1, userdatetime.datedmonth, userdatetime.datehour, userdatetime.dateminute, userdatetime.datesecond); pData->UpdateIndex = flagid; pData->UpdateFlag = updateFlag; } } } /* ** 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. */ retcode = CS_SUCCEED; break; case CS_FAIL: /* ** Something terrible happened. */ break; default: /* ** We got an unexpected return value. */ break; } } /////////////////////// if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cmd_drop(cmd); return -1; } break; default: /* ** We got an unexpected result type. */ DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; } } /* ** 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 went wrong. */ DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cmd_drop(cmd); return -1; default: /* ** We got an unexpected return value. */ DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cmd_drop(cmd); return -1; } /* ** Drop our command structure */ if ((retcode = ct_cmd_drop(cmd)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); return -1; } return retcode == CS_SUCCEED ? row_count : -1; }
/* Testing: Client and server Messages */ int main(int argc, char *argv[]) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int verbose = 0; CS_RETCODE ret; CS_RETCODE results_ret; CS_INT result_type; CS_INT num_cols; CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_INT count, row_count = 0; CS_INT cv; int i; CS_CHAR select[1024]; CS_INT col1[2]; CS_CHAR col2[2][5]; CS_INT num_msgs, totmsgs; CS_CLIENTMSG client_message; CS_SERVERMSG server_message; fprintf(stdout, "%s: Retrieve data using array binding \n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } if (ct_diag(conn, CS_INIT, CS_UNUSED, CS_UNUSED, NULL) != CS_SUCCEED) { fprintf(stderr, "ct_diag(CS_INIT) failed\n"); return 1; } totmsgs = 5; if (ct_diag(conn, CS_MSGLIMIT, CS_ALLMSG_TYPE, CS_UNUSED, &totmsgs) != CS_SUCCEED) { fprintf(stderr, "ct_diag(CS_MSGLIMIT) failed\n"); return 1; } fprintf(stdout, "Maximum message limit is set to: %d\n", totmsgs); /* do not test error */ ret = run_command(cmd, "DROP TABLE #ctlibarray1"); ret = run_command(cmd, "DROP TABLE #ctlibarray2"); ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; strcpy(select, "select col1, col2 from #ctlibarray order by col1 "); ret = ct_command(cmd, CS_LANG_CMD, select, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command(%s) failed\n", select); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 2) { fprintf(stderr, "num_cols %d != 2", num_cols); return 1; } ret = ct_describe(cmd, 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.format = CS_FMT_UNUSED; if (datafmt.maxlength > 1024) { datafmt.maxlength = 1024; } datafmt.count = 2; ret = ct_bind(cmd, 1, &datafmt, &col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } ret = ct_describe(cmd, 2, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 5; datafmt.count = 4; ret = ct_bind(cmd, 2, &datafmt, col2[0], &datalength, &ind); if (ret != CS_SUCCEED) { datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 5; datafmt.count = 2; ret = ct_bind(cmd, 2, &datafmt, col2[0], &datalength, &ind); } if (ret != CS_SUCCEED) { return 1; } count = 0; while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else { /* ret == CS_SUCCEED */ fprintf(stdout, "ct_fetch returned %d rows\n", count); for (cv = 0; cv < count; cv++) fprintf(stdout, "col1 = %d col2= '%s'\n", col1[cv], col2[cv]); } count = 0; } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } break; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: tdsdump_log(TDS_DBG_FUNC, "%L ct_results() failed\n"); fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } ret = run_command(cmd, "DROP TABLE #ctlibarray3"); ret = run_command(cmd, "DROP TABLE #ctlibarray4"); ret = run_command(cmd, "DROP TABLE #ctlibarray5"); if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) { fprintf(stderr, "ct_diag(CS_STATUS) failed\n"); return 1; } fprintf(stdout, "Total number of client/server messages = %d \n", num_msgs); if (ct_diag(conn, CS_STATUS, CS_CLIENTMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) { fprintf(stderr, "ct_diag(CS_STATUS) failed\n"); return 1; } fprintf(stdout, "Number of client messages returned: %d\n", num_msgs); for (i = 0; i < num_msgs; i++) { if (ct_diag(conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message) != CS_SUCCEED) { fprintf(stderr, "cs_diag(CS_GET) failed\n"); return 1; } clientmsg_cb(conn->ctx, conn, &client_message); } if (ct_diag(conn, CS_STATUS, CS_SERVERMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) { fprintf(stderr, "ct_diag(CS_STATUS) failed\n"); return 1; } fprintf(stdout, "Number of server messages returned: %d\n", num_msgs); for (i = 0; i < num_msgs; i++) { if (ct_diag(conn, CS_GET, CS_SERVERMSG_TYPE, i + 1, &server_message) != CS_SUCCEED) { fprintf(stderr, "cs_diag(CS_GET) failed\n"); return 1; } servermsg_cb(conn, cmd, &server_message); } if (ct_diag(conn, CS_CLEAR, CS_ALLMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED) { fprintf(stderr, "cs_diag(CS_CLEAR) failed\n"); return 1; } if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) { fprintf(stderr, "cs_diag(CS_STATUS) failed\n"); return 1; } if (num_msgs != 0) { fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n", num_msgs); return 1; } if (verbose) { fprintf(stdout, "Trying logout\n"); } ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 1; } return 0; }
/* * ct_send SQL |select name = @@servername| * ct_bind variable * ct_fetch and print results */ int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int verbose = 0; CS_RETCODE ret; CS_RETCODE results_ret; CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_INT count, row_count = 0; CS_INT result_type; CS_CHAR name[256]; fprintf(stdout, "%s: Testing bind & select\n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } ret = ct_command(cmd, CS_LANG_CMD, "select name = @@servername", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 256; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd, 1, &datafmt, name, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else if (ret == CS_SUCCEED) { if (verbose) { fprintf(stdout, "server name = %s\n", name); } } else { break; } } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } /* * test parameter return code processing with sp_who */ sp_who(cmd); if (verbose) { fprintf(stdout, "Trying logout\n"); } ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 1; } return 0; }
static int update_second_table(CS_COMMAND * cmd2, char *value) { CS_RETCODE ret; CS_RETCODE results_ret; CS_INT result_type; CS_INT count, row_count = 0; CS_DATAFMT datafmt; CS_SMALLINT ind; CS_CHAR col1[6]; CS_INT datalength; CS_CHAR text[128]; sprintf(text, "update #test_table2 set col1 = '%s' ", value); ret = run_command(cmd2, text); if (ret != CS_SUCCEED) return 1; ret = ct_command(cmd2, CS_LANG_CMD, "select col1 from #test_table2", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return 1; } ret = ct_send(cmd2); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 6; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd2, 1, &datafmt, col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else if (ret == CS_SUCCEED) { ; } else { break; } } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } return 0; }
int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; CS_COMMAND *cmd2; CS_RETCODE ret; CS_RETCODE results_ret; CS_INT result_type; CS_INT count, row_count = 0; CS_DATAFMT datafmt; CS_SMALLINT ind; int verbose = 1; CS_CHAR name[3]; CS_CHAR col1[6]; CS_INT datalength; CS_CHAR text[128]; CS_INT num_cols, i, j; int is_return_status = 0; CS_INT props_value; fprintf(stdout, "%s: Testing ct_cursor()\n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } ret = ct_cmd_alloc(conn, &cmd2); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } return ret; } ret = run_command(cmd, "CREATE TABLE #test_table (col1 char(4))"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('AAA')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('BBB')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('CCC')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd2, "CREATE TABLE #test_table2 (col1 char(4))"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd2, "INSERT #test_table2 (col1) VALUES ('---')"); if (ret != CS_SUCCEED) return 1; if (verbose) { fprintf(stdout, "Trying declare, rows , open in one SEND\n"); } strcpy(text, "select col1 from #test_table where 1 = 1"); strcpy(name, "c1"); ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor declare failed\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor set cursor rows failed"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor open failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: case CS_CMD_DONE: case CS_CMD_FAIL: case CS_STATUS_RESULT: break; case CS_CURSOR_RESULT: ret = ct_cmd_props(cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cmd_props() failed\n"); return 1; } if (props_value & CS_CURSTAT_DECLARED) { fprintf(stderr, "ct_cmd_props claims cursor is in DECLARED state when it should be OPEN\n"); return 1; } if (!(props_value & CS_CURSTAT_OPEN)) { fprintf(stderr, "ct_cmd_props claims cursor is not in OPEN state when it should be \n"); return 1; } if (props_value & CS_CURSTAT_CLOSED) { fprintf(stderr, "ct_cmd_props claims cursor is in CLOSED state when it should be OPEN\n"); return 1; } ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 1) { fprintf(stderr, "unexpected num of columns = %d \n", num_cols); return 1; } for (i = 0; i < num_cols; i++) { /* here we can finally test for the return status column */ ret = ct_describe(cmd, i + 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed for column %d\n", i); return 1; } if (datafmt.status & CS_RETURN) { fprintf(stdout, "ct_describe() column %d \n", i); is_return_status = i + 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 6; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } row_count = 0; while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { if (row_count == 0) { for (j = 0; j < num_cols; j++) { fprintf(stdout, "\n%s\n", datafmt.name); } fprintf(stdout, "------\n\n"); } for (j = 0; j < num_cols; j++) { fprintf(stdout, "%s\n\n", col1); row_count++; } ret = update_second_table(cmd2, col1); if (ret) return ret; } switch ((int) ret) { case CS_END_DATA: break; case CS_ROW_FAIL: fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_DEALLOC); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(close) failed\n"); return ret; } if ((ret = ct_send(cmd)) != CS_SUCCEED) { fprintf(stderr, "BILL ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cmd_props(cmd, CS_GET, CS_CUR_STATUS, &props_value, sizeof(CS_INT), NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cmd_props() failed"); return 1; } if (props_value != CS_CURSTAT_NONE) { fprintf(stderr, "ct_cmd_props() CS_CUR_STATUS != CS_CURSTAT_NONE \n"); return 1; } if (verbose) { fprintf(stdout, "Trying declare, rows, open one at a time \n"); } strcpy(text, "select col1 from #test_table where 2 = 2"); ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, 3, text, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor declare failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(4) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, 3, NULL, CS_UNUSED, (CS_INT) 1); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor set cursor rows failed"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(5) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, 3, text, 26, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor open failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send failed\n"); } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results(6) result_type CS_CMD_FAIL.\n"); break; case CS_STATUS_RESULT: fprintf(stdout, "ct_results: CS_STATUS_RESULT detected for sp_who\n"); case CS_CURSOR_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 1) { fprintf(stderr, "unexpected num of columns = %d \n", num_cols); return 1; } for (i = 0; i < num_cols; i++) { /* here we can finally test for the return status column */ ret = ct_describe(cmd, i + 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed for column %d\n", i); return 1; } if (datafmt.status & CS_RETURN) { fprintf(stdout, "ct_describe() column %d \n", i); is_return_status = i + 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 6; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } row_count = 0; while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { if (row_count == 0) { for (j = 0; j < num_cols; j++) { fprintf(stdout, "\n%s\n", datafmt.name); } fprintf(stdout, "------\n\n"); } for (j = 0; j < num_cols; j++) { fprintf(stdout, "%s\n\n", col1); row_count++; } } switch ((int) ret) { case CS_END_DATA: break; case CS_ROW_FAIL: fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return. %d\n", ret); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, 3, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(close) failed\n"); return ret; } if ((ret = ct_send(cmd)) != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(7) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } ret = ct_cursor(cmd, CS_CURSOR_DEALLOC, name, 3, NULL, CS_UNUSED, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_cursor(dealloc) failed\n"); return ret; } if ((ret = ct_send(cmd)) != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return ret; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { if (result_type == CS_CMD_FAIL) { fprintf(stderr, "ct_results(8) result_type CS_CMD_FAIL.\n"); return 1; } } if (results_ret != CS_END_RESULTS) { fprintf(stderr, "ct_results() returned BAD.\n"); return 1; } if (verbose) { fprintf(stdout, "Running normal select command after cursor operations\n"); } ret = ct_command(cmd, CS_LANG_CMD, "select col1 from #test_table", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 6; datafmt.count = 1; datafmt.locale = NULL; ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else if (ret == CS_SUCCEED) { ; } else { break; } } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.%d\n", ret); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type. %d\n", result_type); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } if (verbose) { fprintf(stdout, "Trying logout\n"); } ct_cmd_drop(cmd2); ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 2; } if (verbose) { fprintf(stdout, "Test suceeded\n"); } return 0; }
/* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */ int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int i, verbose = 0; CS_RETCODE ret; CS_RETCODE results_ret; CS_INT result_type; CS_INT num_cols; CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_INT count, row_count = 0; CS_CHAR name[1024]; char large_sql[1024]; char len600[601]; char temp[11]; len600[0] = 0; name[0] = 0; for (i = 0; i < 60; i++) { sprintf(temp, "_abcde_%03d", (i + 1) * 10); strcat(len600, temp); } len600[600] = '\0'; fprintf(stdout, "%s: Retrieve CS_TEXT_TYPE using ct_bind()\n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin(&ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } ret = run_command(cmd, "CREATE TABLE #test_table (id int, name text)"); if (ret != CS_SUCCEED) return 1; /* ret = run_command(cmd, "INSERT #test_table (id, name) VALUES (1, 'name1')"); if (ret != CS_SUCCEED) return 1; */ sprintf(large_sql, "INSERT #test_table (id, name) VALUES (2, '%s')", len600); ret = run_command(cmd, large_sql); if (ret != CS_SUCCEED) return 1; ret = ct_command(cmd, CS_LANG_CMD, "SELECT name FROM #test_table", CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return 1; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return 1; } while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { switch ((int) result_type) { case CS_CMD_SUCCEED: break; case CS_CMD_DONE: break; case CS_CMD_FAIL: fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n"); return 1; case CS_ROW_RESULT: ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed"); return 1; } if (num_cols != 1) { fprintf(stderr, "num_cols %d != 1", num_cols); return 1; } ret = ct_describe(cmd, 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.format = CS_FMT_NULLTERM; if (datafmt.maxlength > 1024) { datafmt.maxlength = 1024; } ret = ct_bind(cmd, 1, &datafmt, name, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) || (ret == CS_ROW_FAIL)) { row_count += count; if (ret == CS_ROW_FAIL) { fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count); return 1; } else { /* ret == CS_SUCCEED */ if (verbose) { fprintf(stdout, "name = '%s'\n", name); } if (strcmp(name, len600)) { fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name, len600); return 1; } if (datalength != strlen(name) + 1) { fprintf(stderr, "Bad count:\n'%ld'\n! =\n'%d'\n", (long) strlen(name) + 1, count); return 1; } } } switch ((int) ret) { case CS_END_DATA: break; case CS_FAIL: fprintf(stderr, "ct_fetch() returned CS_FAIL.\n"); return 1; default: fprintf(stderr, "ct_fetch() unexpected return.\n"); return 1; } break; case CS_COMPUTE_RESULT: fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n"); return 1; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return 1; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return 1; } if (verbose) { fprintf(stdout, "Trying logout\n"); } ret = try_ctlogout(ctx, conn, cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Logout failed\n"); return 1; } return 0; }
CAMLprim value mltds_ct_bind( value cmd, value maxlen, value index ) { CAMLparam3(cmd, maxlen, index); CS_DATAFMT fmt = {{0,0,0,0,0,0,0,0,0,0,0}}; struct binding_buffer* buf = malloc(sizeof(struct binding_buffer)); buf->fmt = fmt; retval_inspect( "ct_describe", ct_describe( command_ptr(cmd), Int_val(index), &(buf->fmt) ) ); /* NOTE: the datatype in dataformat with coerce the ct library to convert the data to the appropriate type, so anything we don't want to cast to caml we let FreeTDS cast for us */ buf->real_type = buf->fmt.datatype; switch(buf->fmt.datatype) { case CS_BIT_TYPE: /* No change */ break; case CS_TINYINT_TYPE: case CS_SMALLINT_TYPE: case CS_INT_TYPE: /* All become 32 bit ints, even though ocamldbi only uses 31 bit ints */ buf->fmt.datatype = CS_INT_TYPE; break; case CS_REAL_TYPE: case CS_FLOAT_TYPE: buf->fmt.datatype = CS_FLOAT_TYPE; break; case CS_DATETIME_TYPE: case CS_DATETIME4_TYPE: case CS_MONEY_TYPE: case CS_MONEY4_TYPE: case CS_BIGINT_TYPE: case CS_NUMERIC_TYPE: case CS_DECIMAL_TYPE: /* I don't really understand the DECIMAL type so I let FreetDS cast to a string and then load in the ocamldbi Decimal.t type */ case CS_TEXT_TYPE: case CS_CHAR_TYPE: case CS_VARCHAR_TYPE: buf->fmt.datatype = CS_CHAR_TYPE; buf->fmt.maxlength = Int_val(maxlen); break; case CS_IMAGE_TYPE: case CS_BINARY_TYPE: case CS_VARBINARY_TYPE: buf->fmt.datatype = CS_BINARY_TYPE; buf->fmt.maxlength = Int_val(maxlen); break; } buf->data = malloc(maxlen); retval_inspect( "ct_bind", ct_bind(command_ptr(cmd), Int_val(index), &(buf->fmt), (buf->data), &(buf->copied), &(buf->indicator))); CAMLreturn(column_of_buffer(buf)); }