void command::initiate_language_command(const char *cmd_text, ExceptionSink *xsink) { assert(cmd_text && cmd_text[0]); CS_RETCODE err = ct_command(m_cmd, CS_LANG_CMD, (CS_CHAR*)cmd_text, CS_NULLTERM, CS_UNUSED); if (err != CS_SUCCEED) { m_conn.do_exception(xsink, "DBI:SYBASE:EXEC-ERROR", "ct_command(CS_LANG_CMD, '%s') failed with error %d", cmd_text, (int)err); } }
int main(int argc, char **argv) { int verbose = 0; CS_COMMAND *command; CS_CONNECTION *connection; CS_CONTEXT *context; unsigned char buffer[65536]; CS_INT buffer_len = 8192; CS_SMALLINT ind = 0; CS_DATAFMT datafmt; CS_INT ret; int i; printf("-- begin --\n"); check_ret("try_ctlogin", try_ctlogin(&context, &connection, &command, verbose)); check_ret("cs_config", cs_config(context, CS_SET, CS_MESSAGE_CB, (CS_VOID *) csmsg_callback, unused, 0)); execute_sql(command, "if object_id('mps_table') is not null drop table mps_table"); execute_sql(command, "if object_id('mps_rpc') is not null drop procedure mps_rpc"); /* if this query fails probably we are using wrong database vendor or version */ ret = execute_sql(command, "create procedure mps_rpc (@varbinary_param varbinary(max)) as " "insert mps_table values (@varbinary_param) " "select len(varbinary_data) from mps_table"); if (ret != 0) { try_ctlogout(context, connection, command, verbose); return 0; } execute_sql(command, "create table mps_table (varbinary_data varbinary(max))"); if (argc > 1) buffer_len = atoi(argv[1]); if (buffer_len < 0 || buffer_len > sizeof(buffer)) return 1; printf("sending %d bytes\n", buffer_len); for (i = 0; i < buffer_len; i++) buffer[i] = (rand() % 16); memset(&datafmt, 0, sizeof(datafmt)); strcpy(datafmt.name, "@varbinary_param"); datafmt.namelen = nullterm; datafmt.datatype = CS_IMAGE_TYPE; datafmt.status = CS_INPUTVALUE; check_ret("ct_command", ct_command(command, CS_RPC_CMD, "mps_rpc", nullterm, unused)); check_ret("ct_setparam", ct_setparam(command, &datafmt, buffer, &buffer_len, &ind)); check_ret("ct_send", ct_send(command)); fetch_results(command); execute_sql(command, "drop table mps_table"); execute_sql(command, "drop procedure mps_rpc"); try_ctlogout(context, connection, command, verbose); printf("-- end --\n"); return (result_len == buffer_len) ? 0 : 1; }
static int execute_sql(CS_COMMAND * command, const char *sql) { printf("executing sql: %s\n", sql); check_ret("ct_command", ct_command(command, CS_LANG_CMD, sql, nullterm, unused)); check_ret("ct_send", ct_send(command)); return fetch_results(command); }
CAMLprim value mltds_ct_command(value cmd, value cmdtype, value option, value text) { CAMLparam4(cmd, cmdtype, option, text); retval_inspect( "ct_command", ct_command(command_ptr(cmd), cmdtype_of_value(cmdtype), String_val(text), string_length(text), CS_UNUSED /*cmdoption_of_value(option)*/) ); CAMLreturn( Val_unit ); }
/* Run commands from which we expect no results returned */ CS_RETCODE run_command(CS_COMMAND * cmd, const char *sql) { CS_RETCODE ret, results_ret; CS_INT result_type; if (cmd == NULL) { return CS_FAIL; } ret = ct_command(cmd, CS_LANG_CMD, sql, CS_NULLTERM, CS_UNUSED); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_command() failed\n"); return ret; } ret = ct_send(cmd); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_send() failed\n"); return ret; } 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 CS_FAIL; */ break; default: fprintf(stderr, "ct_results() unexpected result_type.\n"); return CS_FAIL; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); return CS_FAIL; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); return CS_FAIL; } return CS_SUCCEED; }
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; }
static PyObject *CS_COMMAND_ct_command(CS_COMMANDObj *self, PyObject *args) { int type; char *databuf; CS_INT option = CS_UNUSED; CS_RETCODE status; char *type_str = NULL; if (!first_tuple_int(args, &type)) return NULL; if (self->cmd == NULL) { PyErr_SetString(PyExc_TypeError, "CS_COMMAND has been dropped"); return NULL; } switch (type) { case CS_LANG_CMD: /* ct_command(CS_LANG_CMD, sql [,option]) -> status */ type_str = "CS_LANG_CMD"; case CS_RPC_CMD: /* ct_command(CS_RPC_CMD, name [,option]) -> status */ if (type_str == NULL) type_str = "CS_RPC_CMD"; if (!PyArg_ParseTuple(args, "is|i", &type, &databuf, &option)) return NULL; /* PyErr_Clear(); */ SY_CONN_BEGIN_THREADS(self->conn); status = ct_command(self->cmd, type, databuf, CS_NULLTERM, option); SY_CONN_END_THREADS(self->conn); if (self->debug) debug_msg("ct_command(cmd%d, %s, \"%s\", CS_NULLTERM, %s) -> %s\n", self->serial, type_str, databuf, value_str(VAL_OPTION, option), value_str(VAL_STATUS, status)); if (PyErr_Occurred()) return NULL; return PyInt_FromLong(status); #ifdef CS_MSG_CMD case CS_MSG_CMD: /* ct_command(CS_MSG_CMD, int) -> status */ { CS_INT num; if (!PyArg_ParseTuple(args, "ii", &type, &num)) return NULL; /* PyErr_Clear(); */ SY_CONN_BEGIN_THREADS(self->conn); status = ct_command(self->cmd, type, (CS_VOID*)&num, CS_UNUSED, CS_UNUSED); SY_CONN_END_THREADS(self->conn); if (self->debug) debug_msg("ct_command(cmd%d, CS_MSG_CMD, %d, CS_UNUSED, CS_UNUSED)" " -> %s\n", self->serial, (int)num, value_str(VAL_STATUS, status)); if (PyErr_Occurred()) return NULL; return PyInt_FromLong(status); } #endif #ifdef CS_PACKAGE_CMD case CS_PACKAGE_CMD: /* ct_command(CS_PACKAGE_CMD, name) -> status */ if (!PyArg_ParseTuple(args, "is", &type, &databuf)) return NULL; /* PyErr_Clear(); */ SY_CONN_BEGIN_THREADS(self->conn); status = ct_command(self->cmd, type, databuf, CS_NULLTERM, CS_UNUSED); SY_CONN_END_THREADS(self->conn); if (self->debug) debug_msg("ct_command(cmd%d, CS_PACKAGE_CMD, \"%s\", CS_NULLTERM," " CS_UNUSED) -> %s\n", self->serial, databuf, value_str(VAL_STATUS, status)); if (PyErr_Occurred()) return NULL; return PyInt_FromLong(status); #endif case CS_SEND_DATA_CMD: /* ct_command(CS_SEND_DATA_CMD) -> status */ if (!PyArg_ParseTuple(args, "i", &type)) return NULL; /* PyErr_Clear(); */ SY_CONN_BEGIN_THREADS(self->conn); status = ct_command(self->cmd, type, NULL, CS_UNUSED, CS_COLUMN_DATA); SY_CONN_END_THREADS(self->conn); if (self->debug) debug_msg("ct_command(cmd%d, CS_SEND_DATA_CMD, NULL, CS_UNUSED," " CS_COLUMN_DATA) -> %s\n", self->serial, value_str(VAL_STATUS, status)); if (PyErr_Occurred()) return NULL; return PyInt_FromLong(status); default: PyErr_SetString(PyExc_TypeError, "unknown type"); return NULL; } }
/* Testing: Test asynchronous ct_cancel() */ int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; CS_COMMAND *cmd; int i, verbose = 0, cnt = 0; CS_RETCODE ret; CS_INT result_type; struct itimerval timer; char query[1024]; unsigned clock = 200000; fprintf(stdout, "%s: Check asynchronous called ct_cancel()\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; } /* Create needed tables */ ret = run_command(cmd, "CREATE TABLE #t0010 (id int, col1 varchar(255))"); if (ret != CS_SUCCEED) return 1; for (i = 0; i < 10; i++) { sprintf(query, "INSERT #t0010 (id, col1) values (%d, 'This is field no %d')", i, i); ret = run_command(cmd, query); if (ret != CS_SUCCEED) return 1; } /* Set SIGALRM signal handler */ signal(SIGALRM, catch_alrm); for (;;) { /* TODO better to use alarm AFTER ct_send ?? */ /* Set timer */ timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = clock; timer.it_value.tv_sec = 0; timer.it_value.tv_usec = clock; if (0 != setitimer(ITIMER_REAL, &timer, NULL)) { fprintf(stderr, "Could not set realtime timer.\n"); return 1; } /* Issue a command returning many rows */ ret = ct_command(cmd, CS_LANG_CMD, "SELECT * FROM #t0010 t1, #t0010 t2, #t0010 t3, #t0010 t4", 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, "first ct_send() failed.\n"); return 1; } /* Save a global reference for the interrupt handler */ g_cmd = cmd; while ((ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { printf("More results?...\n"); if (result_type == CS_STATUS_RESULT) continue; switch ((int) result_type) { case CS_ROW_RESULT: printf("do_fetch() returned: %d\n", do_fetch(cmd, &cnt)); break; } } /* We should not have received all rows, as the alarm signal cancelled it... */ if (cnt < 10000) break; if (clock <= 5000) { fprintf(stderr, "All rows read, this may not occur.\n"); return 1; } g_cmd = NULL; clock /= 2; } /* Remove timer */ timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; timer.it_value.tv_sec = 0; timer.it_value.tv_usec = 0; if (0 != setitimer(ITIMER_REAL, &timer, NULL)) { fprintf(stderr, "Could not remove realtime timer.\n"); return 1; } /* * Issue another command, this will be executed after a ct_cancel, * to test if wire state is consistent */ ret = ct_command(cmd, CS_LANG_CMD, "SELECT * FROM #t0010 t1, #t0010 t2, #t0010 t3", 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, "second ct_send() failed.\n"); return 1; } while ((ret = ct_results(cmd, &result_type)) == CS_SUCCEED) { printf("More results?...\n"); if (result_type == CS_STATUS_RESULT) continue; switch ((int) result_type) { case CS_ROW_RESULT: printf("do_fetch() returned: %d\n", do_fetch(cmd, &cnt)); break; } } if (1000 != cnt) { /* This time, all rows must have been received */ fprintf(stderr, "Incorrect number of rows read.\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; } printf("%s: asynchronous cancel test: PASSED\n", __FILE__); 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; }
/* Testing: Client 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[2]; 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; int result = 1; 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 = 1; if (ct_diag(conn, CS_MSGLIMIT, CS_CLIENTMSG_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); 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) { 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(ctx, conn, &client_message); } if (ct_diag(conn, CS_CLEAR, CS_CLIENTMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED) { fprintf(stderr, "cs_diag(CS_CLEAR) failed\n"); return 1; } if (ct_diag(conn, CS_STATUS, CS_CLIENTMSG_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; } /* we catch error, good */ result = 0; } else { fprintf(stderr, "ct_bind() succeeded while it shouldn't\n"); return 1; } datafmt.count = 2; ret = ct_bind(cmd, 2, &datafmt, col2[0], &datalength, ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); 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: 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 result; }
/* Testing: Retrieve compute 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_INT result_type; CS_INT num_cols, compute_id; CS_DATAFMT datafmt; CS_INT datalength; CS_SMALLINT ind; CS_INT count, row_count = 0; CS_CHAR select[1024]; CS_INT col1; CS_CHAR col2[2]; CS_CHAR col3[32]; CS_INT compute_col1; CS_CHAR compute_col3[32]; fprintf(stdout, "%s: Retrieve compute results processing\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 #ctlib0009 (col1 int not null, col2 char(1) not null, col3 datetime not null)"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlib0009 values (1, 'A', 'Jan 1 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlib0009 values (2, 'A', 'Jan 2 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlib0009 values (3, 'A', 'Jan 3 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlib0009 values (8, 'B', 'Jan 4 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlib0009 values (9, 'B', 'Jan 5 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; strcpy(select, "select col1, col2, col3 from #ctlib0009 order by col2 "); strcat(select, "compute sum(col1) by col2 "); strcat(select, "compute max(col3)"); 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 != 3) { fprintf(stderr, "num_cols %d != 3", 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, &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 = 2; ret = ct_bind(cmd, 2, &datafmt, col2, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } ret = ct_describe(cmd, 3, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 32; ret = ct_bind(cmd, 3, &datafmt, col3, &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 */ fprintf(stdout, "col1 = %d col2= '%s', col3 = '%s'\n", col1, col2, col3); } } 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: printf("testing compute_result\n"); ret = ct_compute_info(cmd, CS_COMP_ID, CS_UNUSED, &compute_id, CS_UNUSED, NULL); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_compute_info() failed"); return 1; } if (compute_id != 1 && compute_id != 2) { fprintf(stderr, "invalid compute_id value"); return 1; } if (compute_id == 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, "compute_id %d num_cols %d != 1", compute_id, 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; } compute_col1 = -1; ret = ct_bind(cmd, 1, &datafmt, &compute_col1, &datalength, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } } if (compute_id == 2) { 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, "compute_id %d num_cols %d != 1", compute_id, num_cols); return 1; } ret = ct_describe(cmd, 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 32; compute_col3[0] = 0; ret = ct_bind(cmd, 1, &datafmt, compute_col3, &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 (compute_id == 1) { fprintf(stdout, "compute_col1 = %d \n", compute_col1); if (compute_col1 != 6 && compute_col1 != 17) { fprintf(stderr, "(should be 6 or 17)\n"); return 1; } } if (compute_id == 2) { fprintf(stdout, "compute_col3 = '%s'\n", compute_col3); if (strcmp("Jan 5 2002 10:00:00AM", compute_col3) && strcmp("Jan 05 2002 10:00AM", compute_col3) && strcmp("Jan 5 2002 10:00AM", compute_col3)) { fprintf(stderr, "(should be \"Jan 5 2002 10:00:00AM\")\n"); 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; 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; }
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; }
static int insert_test(CS_CONNECTION *conn, CS_COMMAND *cmd, int useNames) { CS_CONTEXT *ctx; CS_RETCODE ret; CS_INT res_type; CS_DATAFMT datafmt; CS_DATAFMT srcfmt; CS_DATAFMT destfmt; CS_INT intvar; CS_FLOAT floatvar; CS_MONEY moneyvar; CS_DATEREC datevar; char moneystring[10]; char dummy_name[30]; char dummy_name2[20]; CS_INT destlen; /* clear table */ run_command(cmd, "delete #ctparam_lang"); /* * Assign values to the variables used for parameter passing. */ intvar = 2; floatvar = 0.12; strcpy(dummy_name, "joe blow"); strcpy(dummy_name2, ""); strcpy(moneystring, "300.90"); /* * Clear and setup the CS_DATAFMT structures used to convert datatypes. */ memset(&srcfmt, 0, sizeof(CS_DATAFMT)); srcfmt.datatype = CS_CHAR_TYPE; srcfmt.maxlength = (CS_INT)strlen(moneystring); srcfmt.precision = 5; srcfmt.scale = 2; srcfmt.locale = NULL; memset(&destfmt, 0, sizeof(CS_DATAFMT)); destfmt.datatype = CS_MONEY_TYPE; destfmt.maxlength = sizeof(CS_MONEY); destfmt.precision = 5; destfmt.scale = 2; destfmt.locale = NULL; /* * Convert the string representing the money value * to a CS_MONEY variable. Since this routine does not have the * context handle, we use the property functions to get it. */ if ((ret = ct_cmd_props(cmd, CS_GET, CS_PARENT_HANDLE, &conn, CS_UNUSED, NULL)) != CS_SUCCEED) { fprintf(stderr, "ct_cmd_props() failed\n"); return 1; } if ((ret = ct_con_props(conn, CS_GET, CS_PARENT_HANDLE, &ctx, CS_UNUSED, NULL)) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() failed\n"); return 1; } ret = cs_convert(ctx, &srcfmt, (CS_VOID *) moneystring, &destfmt, &moneyvar, &destlen); if (ret != CS_SUCCEED) { fprintf(stderr, "cs_convert() failed\n"); return 1; } /* * create the command */ if ((ret = ct_command(cmd, CS_LANG_CMD, query, (CS_INT)strlen(query), CS_UNUSED)) != CS_SUCCEED) { fprintf(stderr, "ct_command(CS_LANG_CMD) failed\n"); return 1; } /* * Clear and setup the CS_DATAFMT structure, then pass * each of the parameters for the query. */ memset(&datafmt, 0, sizeof(datafmt)); if (useNames) strcpy(datafmt.name, "@in1"); else datafmt.name[0] = 0; datafmt.maxlength = 255; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_CHAR_TYPE; datafmt.status = CS_INPUTVALUE; /* * The character string variable is filled in by the RPC so pass NULL * for the data 0 for data length, and -1 for the indicator arguments. */ ret = ct_param(cmd, &datafmt, dummy_name, (CS_INT)strlen(dummy_name), 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(char) failed\n"); return 1; } if (useNames) strcpy(datafmt.name, "@in2"); else datafmt.name[0] = 0; datafmt.maxlength = 255; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_CHAR_TYPE; datafmt.status = CS_INPUTVALUE; ret = ct_param(cmd, &datafmt, dummy_name2, (CS_INT)strlen(dummy_name2), 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(char) failed\n"); return 1; } if (useNames) strcpy(datafmt.name, "@in3"); else datafmt.name[0] = 0; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_INT_TYPE; datafmt.status = CS_INPUTVALUE; ret = ct_param(cmd, &datafmt, (CS_VOID *) & intvar, CS_SIZEOF(CS_INT), 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(int) failed\n"); return 1; } if (useNames) strcpy(datafmt.name, "@moneyval"); else datafmt.name[0] = 0; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_MONEY_TYPE; datafmt.status = CS_INPUTVALUE; ret = ct_param(cmd, &datafmt, (CS_VOID *) & moneyvar, CS_SIZEOF(CS_MONEY), 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(money) failed\n"); return 1; } if (useNames) strcpy(datafmt.name, "@dateval"); else datafmt.name[0] = 0; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_DATETIME_TYPE; datafmt.status = CS_INPUTVALUE; memset(&datevar, 0, sizeof(CS_DATEREC)); datevar.dateyear = 2003; datevar.datemonth = 2; datevar.datedmonth = 1; ret = ct_param(cmd, &datafmt, &datevar, 0, 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(datetime) failed"); return 1; } if (useNames) strcpy(datafmt.name, "@floatval"); else datafmt.name[0] = 0; datafmt.namelen = CS_NULLTERM; datafmt.datatype = CS_FLOAT_TYPE; datafmt.status = CS_INPUTVALUE; ret = ct_param(cmd, &datafmt, &floatvar, 0, 0); if (CS_SUCCEED != ret) { fprintf(stderr, "ct_param(float) failed"); return 1; } /* * Send the command to the server */ if (ct_send(cmd) != CS_SUCCEED) { fprintf(stderr, "ct_send(CS_LANG_CMD) failed\n"); return 1; } /* * Process the results. */ while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) { switch ((int) res_type) { case CS_CMD_SUCCEED: case CS_CMD_DONE: { CS_INT rowsAffected=0; ct_res_info(cmd, CS_ROW_COUNT, &rowsAffected, CS_UNUSED, NULL); if (1 != rowsAffected) fprintf(stderr, "insert touched %d rows instead of 1\n", rowsAffected); } break; case CS_CMD_FAIL: /* * The server encountered an error while * processing our command. */ fprintf(stderr, "ct_results returned CS_CMD_FAIL.\n"); break; case CS_STATUS_RESULT: /* * The server encountered an error while * processing our command. */ fprintf(stderr, "ct_results returned CS_STATUS_RESULT.\n"); break; default: /* * We got something unexpected. */ fprintf(stderr, "ct_results returned unexpected result type %d\n", res_type); return 1; } } if (ret != CS_END_RESULTS) fprintf(stderr, "ct_results returned unexpected result %d.\n", (int) ret); /* test row inserted */ ret = run_command(cmd, "if not exists(select * from #ctparam_lang where name = 'joe blow' and name2 is not null and age = 2) select 1"); if (ret != CS_SUCCEED) { fprintf(stderr, "check row inserted failed\n"); exit(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; }
/* Testing: data truncation behavior of ct_fetch */ 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 copied = 0; CS_SMALLINT ind = 0; CS_INT count, row_count = 0; CS_CHAR select[1024]; char *addr = NULL; fprintf(stdout, "%s: test data truncation behavior of ct_fetch \n", __FILE__); if (verbose) { fprintf(stdout, "Trying login\n"); } ret = try_ctlogin_with_options(argc, argv, &ctx, &conn, &cmd, verbose); if (ret != CS_SUCCEED) { fprintf(stderr, "Login failed\n"); return 1; } verbose += common_pwd.fverbose; strcpy(select, "select name from systypes where datalength(name) > 2*9 order by datalength(name)"); printf("%s\n", select); 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"); free(addr); 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"); free(addr); return 1; } fprintf(stderr, "%d column%s\n", num_cols, num_cols==1? "":"s"); ret = ct_describe(cmd, 1, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed\n"); free(addr); return 1; } fprintf(stderr, "type = %d\n", datafmt.datatype); fprintf(stderr, "maxlength = %d\n", datafmt.maxlength); if (datafmt.datatype == CS_CHAR_TYPE) { fprintf(stderr, "CS_CHAR_TYPE\n"); datafmt.format = CS_FMT_NULLTERM; addr = malloc(datafmt.maxlength); } fprintf(stderr, "binding column 1 (%s)\n", datafmt.name); /* set maxlength to something short to test truncation behavior */ if (common_pwd.maxlength) datafmt.maxlength = common_pwd.maxlength; ret = ct_bind(cmd, 1, &datafmt, addr, &copied, &ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); free(addr); return 1; } fprintf(stderr, "fetching rows with datafmt.maxlength = %d\n", datafmt.maxlength); while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) != CS_END_DATA) { fprintf(stderr, "ct_fetch() row %d returned %d.\n", row_count, (int) ret); addr[copied] = '\0'; fprintf(stderr, "copied %d bytes: [%s]\n", copied, addr); row_count += count; switch (ret) { case CS_SUCCEED: fprintf(stdout, "ct_fetch returned %d row%s\n", count, count==1? "":"s"); break; case CS_ROW_FAIL: fprintf(stderr, "error: ct_fetch() returned CS_ROW_FAIL on row %d.\n", row_count); free(addr); return 1; case CS_CANCELED: fprintf(stderr, "error: ct_fetch() returned CS_CANCELED??\n"); free(addr); return 1; case CS_FAIL: fprintf(stderr, "error: ct_fetch() returned CS_FAIL.\n"); free(addr); return 1; default: fprintf(stderr, "error: ct_fetch() unexpected return.\n"); free(addr); return 1; } } break; } } switch ((int) results_ret) { case CS_END_RESULTS: break; case CS_FAIL: fprintf(stderr, "ct_results() failed.\n"); free(addr); return 1; break; default: fprintf(stderr, "ct_results() unexpected return.\n"); free(addr); 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"); free(addr); return 1; } free(addr); return 0; }
/* Testing: Test order of ct_results() */ 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; char query[1024]; CS_INT insert_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE }; CS_INT update_results[] = { CS_CMD_SUCCEED, CS_CMD_DONE }; CS_INT select_results[] = { CS_ROW_RESULT, CS_CMD_DONE }; fprintf(stdout, "%s: Check ordering of returns from cs_results()\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 #t0004 (id int)"); if (ret != CS_SUCCEED) return 1; for (i = 0; i < NUMROWS; i++) { sprintf(query, "INSERT #t0004 (id) VALUES (%d)", i); ret = ct_command(cmd, CS_LANG_CMD, query, 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; } results_ret = do_results(cmd, insert_results); 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; } } ret = ct_command(cmd, CS_LANG_CMD, "UPDATE #t0004 SET id = id + 1", 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; } results_ret = do_results(cmd, update_results); 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; } /* single row select */ ret = ct_command(cmd, CS_LANG_CMD, "SELECT * FROM #t0004 WHERE id = 1", 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; } results_ret = do_results(cmd, select_results); 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; }
/* Testing: array binding of result set */ 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[2]; CS_SMALLINT ind[2]; CS_INT count, row_count = 0; CS_INT cv; CS_CHAR select[1024]; CS_INT col1[2]; CS_CHAR col2[2][5]; CS_CHAR col3[2][32]; 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; } 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; ret = run_command(cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan 3 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan 4 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; ret = run_command(cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan 5 2002 10:00:00AM')"); if (ret != CS_SUCCEED) return 1; strcpy(select, "select col1, col2, col3 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 != 3) { fprintf(stderr, "num_cols %d != 3", 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[0], 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 = 2; ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); return 1; } ret = ct_describe(cmd, 3, &datafmt); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_describe() failed"); return 1; } datafmt.datatype = CS_CHAR_TYPE; datafmt.format = CS_FMT_NULLTERM; datafmt.maxlength = 32; datafmt.count = 2; ret = ct_bind(cmd, 3, &datafmt, &col3[0], datalength, ind); if (ret != CS_SUCCEED) { fprintf(stderr, "ct_bind() failed\n"); 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', col3 = '%s'\n", col1[cv], col2[cv], col3[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: 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 CSybClient::delMsisdnImsiByMsisdn(int connId, char* tablename, stImsiMsisdn* pmsisdn) { CS_CONNECTION *connection = getConnection(connId); if(NULL == connection || NULL == tablename) return -1; if(NULL == pmsisdn) return -1; CLEAR_ALL_MESSAGE(connection); // CS_RETCODE retcode; CS_INT restype; CS_COMMAND *cmd = NULL; CS_RETCODE query_code; /* ** Get a command handle, store the command string in it, and ** send it to the server. */ if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); return -1; } // CS_CHAR cmdstring[1024] = {0}; sprintf(cmdstring, "delete from %s where MSISDN='%s'", tablename, pmsisdn->strMsisdn); if ((retcode = ct_command(cmd, CS_LANG_CMD, cmdstring, CS_NULLTERM, CS_UNUSED)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; } if ((retcode = 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; } /* ** Examine the results coming back. If any errors are seen, the query ** result code (which we will return from this function) will be ** set to FAIL. */ query_code = CS_SUCCEED; while ((retcode = ct_results(cmd, &restype)) == CS_SUCCEED) { switch((int)restype) { case CS_CMD_SUCCEED: case CS_CMD_DONE: break; case CS_CMD_FAIL: query_code = CS_FAIL; break; case CS_STATUS_RESULT: retcode = ct_cancel(NULL, cmd, CS_CANCEL_CURRENT); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); query_code = CS_FAIL; } break; default: /* ** Unexpected result type. */ query_code = CS_FAIL; break; } if (query_code == CS_FAIL) { /* ** Terminate results processing and break out of ** the results loop */ retcode = ct_cancel(NULL, cmd, CS_CANCEL_ALL); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); } break; } } /* ** Clean up the command handle used */ if (retcode == CS_END_RESULTS) { retcode = ct_cmd_drop(cmd); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); query_code = CS_FAIL; } } else { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); (void)ct_cmd_drop(cmd); query_code = CS_FAIL; } return query_code == CS_SUCCEED ? 0 : -1; }
static VALUE statement_Execute(VALUE self) { int i; CS_DATAFMT col; CS_DATAFMT *cols; EX_COLUMN_DATA *col_data; CS_INT rc; CS_INT resulttype; CS_INT num_cols; CS_INT col_len; CS_INT row_count = 0; CS_INT rows_read; CS_INT num_errors = 0; CS_SERVERMSG servermsg; VALUE err; char *error_msg; struct timeval start, stop; int print_rows = 1; char message[128]; char* buf; CS_DATEREC date_rec; char output[200]; CS_INT output_len; int tempInt; CS_BIGINT tempBigInt; double tempDouble; CS_NUMERIC tempNumeric; char* tempText; char* newTempText; int tempTextLen; CS_INT data_rc; int isNull = 0; CS_DATE tempDate; CS_DATETIME tempDateTime; TDS_Connection* conn; CS_COMMAND * cmd; VALUE connection; VALUE query; VALUE columns; VALUE rows; VALUE status; VALUE errors; VALUE date_parts[8]; VALUE column; VALUE row; VALUE column_name = rb_str_new2("name"); VALUE column_type = rb_str_new2("type"); VALUE column_size = rb_str_new2("size"); VALUE column_scale = rb_str_new2("scale"); VALUE column_precision = rb_str_new2("precision"); VALUE column_value; connection = rb_iv_get(self, "@connection"); query = rb_iv_get(self, "@query"); columns = rb_ary_new(); rb_iv_set(self, "@columns", columns); rows = rb_ary_new(); rb_iv_set(self, "@rows", rows); Data_Get_Struct(connection, TDS_Connection, conn); buf = value_to_cstr(query); rb_iv_set(self, "@status", Qnil); rb_iv_set(self, "@messages", rb_ary_new()); errors = rb_ary_new(); rb_iv_set(self, "@errors", errors); ct_diag(conn->connection, CS_INIT, CS_UNUSED, CS_UNUSED, NULL); // if ( ct_callback(conn->context, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)clientmsg_cb) != CS_SUCCEED ) { // error_message("ct_callback CS_CLIENTMSG_CB failed\n"); // } // if ( ct_callback(conn->context, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)servermsg_cb) != CS_SUCCEED ) { // error_message("ct_callback CS_SERVERMSG_CB failed\n"); // } ct_cmd_alloc(conn->connection, &cmd); ct_command(cmd, CS_LANG_CMD, buf, CS_NULLTERM, CS_UNUSED); ct_send(cmd); if ( ct_diag(conn->connection, CS_STATUS, CS_SERVERMSG_TYPE, CS_UNUSED, &num_errors) != CS_SUCCEED ) { error_message("ct_diag CS_STATUS CS_SERVERMSG_TYPE failed"); } if (num_errors > 0) { // fprintf(stderr, "%d errors found\n", num_errors); for (i = 0; i < num_errors; i++) { if ( ct_diag(conn->connection, CS_GET, CS_SERVERMSG_TYPE, i+1, &servermsg) != CS_SUCCEED ) { error_message("ct_diag CS_GET CS_SERVERMSG_TYPE failed"); } if (servermsg.severity > 0) { // error_message(servermsg.text); rb_ary_push(errors, rb_str_new2(servermsg.text)); } } if ( ct_diag(conn->connection, CS_CLEAR, CS_SERVERMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED ) { error_message("ct_diag CS_CLEAR CS_SERVERMSG_TYPE failed"); } } // Raise errors from ct_command/ct_send err = rb_funcall(errors, rb_intern("first"), 0); // FIXME: should probably display all errors instead of just first if(RTEST(err)) { error_msg = value_to_cstr(err); rb_raise(rb_eIOError, error_msg); ct_cmd_drop(cmd); return Qnil; } // TODO: // - We should have an array of malloc'd cols // - Then we bind / fetch to those // - Finish conversions... while ((rc = ct_results(cmd, &resulttype)) == CS_SUCCEED) { switch (resulttype) { case CS_ROW_RESULT: rc = ct_res_info(cmd, CS_NUMDATA, &num_cols, sizeof(num_cols), &col_len); if (rc != CS_SUCCEED) { fprintf(stderr, "ct_res_info() failed\n"); return 1; } col_data = (EX_COLUMN_DATA *)malloc(num_cols * sizeof (EX_COLUMN_DATA)); if (col_data == NULL) { fprintf(stderr, "ex_fetch_data: malloc() failed"); return CS_MEM_ERROR; } cols = (CS_DATAFMT *)malloc(num_cols * sizeof (CS_DATAFMT)); if (cols == NULL) { fprintf(stderr, "ex_fetch_data: malloc() failed"); free(col_data); return CS_MEM_ERROR; } // Get column information for (i = 0; i < num_cols; i++) { rc = ct_describe(cmd, (i+1), &cols[i]); if ( rc != CS_SUCCEED ) { fprintf(stderr, "ct_describe failed on col #%d", i+1); } column_value = rb_hash_new(); // fprintf(stderr, "%s\n", cols[i].name); if (cols[i].name) { rb_hash_aset(column_value, column_name, rb_str_new2(cols[i].name)); } else { rb_hash_aset(column_value, column_name, Qnil); } rb_hash_aset(column_value, column_type, rb_str_new2(column_type_name(cols[i]))); rb_hash_aset(column_value, column_size, INT2FIX(cols[i].maxlength)); rb_hash_aset(column_value, column_scale, INT2FIX(cols[i].scale)); rb_hash_aset(column_value, column_precision, INT2FIX(cols[i].precision)); rb_ary_push(columns, column_value); } // Fetch data while (((rc = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) == CS_SUCCEED) || (rc == CS_ROW_FAIL)) { row_count = row_count + rows_read; row = rb_hash_new(); rb_ary_push(rows, row); // Create Ruby objects for (i = 0; i < num_cols; i++) { // if (col_data[i].indicator == -1) { // rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); // continue; // } switch (cols[i].datatype) { case CS_TINYINT_TYPE: case CS_BIT_TYPE: data_rc = ct_get_data(cmd, (i + 1), &tempInt, sizeof(tempInt), &output_len); if (output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { if(tempInt == 1) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qtrue); } else { rb_hash_aset(row, rb_str_new2(cols[i].name), Qfalse); } } tempInt = -1; break; case CS_INT_TYPE: case CS_SMALLINT_TYPE: data_rc = ct_get_data(cmd, (i + 1), &tempInt, sizeof(tempInt), &output_len); if (output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { rb_hash_aset(row, rb_str_new2(cols[i].name), INT2FIX(tempInt)); } tempInt = -1; break; case CS_DATETIME_TYPE: case CS_DATETIME4_TYPE: data_rc = ct_get_data(cmd, (i + 1), &tempDateTime, sizeof(tempDateTime), &output_len); if (output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { if ( cs_dt_crack(conn->context, CS_DATETIME_TYPE, &tempDateTime, &date_rec) == CS_SUCCEED ) { if(date_rec.dateyear && date_rec.datemonth && date_rec.datedmonth) { date_parts[0] = INT2FIX(date_rec.dateyear); date_parts[1] = INT2FIX(date_rec.datemonth+1); date_parts[2] = INT2FIX(date_rec.datedmonth); date_parts[3] = INT2FIX(date_rec.datehour); date_parts[4] = INT2FIX(date_rec.dateminute); date_parts[5] = INT2FIX(date_rec.datesecond); date_parts[6] = INT2FIX(date_rec.datemsecond); date_parts[7] = INT2FIX(date_rec.datetzone); // String (fastest known so far, but pushes the burden to ActiveRecord for parsing) sprintf(output, "%d-%02d-%02d %02d:%02d:%02d.%03d", date_rec.dateyear, date_rec.datemonth+1, date_rec.datedmonth, date_rec.datehour, date_rec.dateminute, date_rec.datesecond, date_rec.datemsecond); rb_hash_aset(row, rb_str_new2(cols[i].name), rb_str_new2(output)); // DateTime - this is slow a f*ck //rb_hash_aset(row, rb_str_new2(cols[i].name), rb_funcall2(rb_DateTime, rb_intern("civil"), 6, &date_parts[0])); // Time - way faster than DateTime // FIXME: should we be assuming utc?! // rb_hash_aset(row, rb_str_new2(cols[i].name), rb_funcall2(rb_cTime, rb_intern("utc"), 6, &date_parts[0])); } else { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } } else { fprintf(stderr, "cs_dt_crack failed\n"); } } // tempDateTime = 0; // not sure how to clear this... break; // case CS_REAL_TYPE: case CS_FLOAT_TYPE: // case CS_MONEY_TYPE: // case CS_MONEY4_TYPE: data_rc = ct_get_data(cmd, (i + 1), &tempDouble, sizeof(tempDouble), &output_len); if (output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { rb_hash_aset(row, rb_str_new2(cols[i].name), rb_float_new(tempDouble)); } tempDouble = -1.0; break; // case CS_BIGINT_TYPE: // error_message("HELLO BIGINT!"); // break; case CS_DECIMAL_TYPE: case CS_NUMERIC_TYPE: // fprintf(stderr, "CS_NUMERIC_TYPE detected - name: %s\n", cols[i].name); data_rc = ct_get_data(cmd, (i + 1), &tempNumeric, sizeof(tempNumeric), &output_len); if (output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { // fprintf(stderr, "tempNumeric output_len: %d, precision: %d, scale: %d, array: %s\n", output_len, tempNumeric.precision, tempNumeric.scale, tempNumeric.array); col.datatype = CS_CHAR_TYPE; col.format = CS_FMT_NULLTERM; col.maxlength = 200; // col.maxlength = cols[i].precision + 1; data_rc = cs_convert(conn->context, &cols[i], &tempNumeric, &col, output, &output_len); if ( data_rc != CS_SUCCEED ) { error_message("CS_NUMERIC_TYPE conversion failed"); fprintf(stderr, "cs_convert returned: %d\n", data_rc); } // fprintf(stderr, "numeric output_len: %d, output: %s\n", output_len, output); rb_hash_aset(row, rb_str_new2(cols[i].name), LL2NUM(strtoll(output, NULL, 10))); } break; case CS_CHAR_TYPE: case CS_LONGCHAR_TYPE: case CS_TEXT_TYPE: case CS_VARCHAR_TYPE: case CS_UNICHAR_TYPE: case CS_UNIQUE_TYPE: // @todo should this one be handled differently? isNull = 0; tempTextLen = 1; // 1 for \0 do { newTempText = realloc((tempTextLen == 1 ? NULL : tempText), tempTextLen + (1000 * sizeof(char))); // allocate another 1000 chars if (newTempText != NULL) { tempText = newTempText; } else { fprintf(stderr, "realloc error\n"); } data_rc = ct_get_data(cmd, (i + 1), tempText + tempTextLen - 1, 1000, &output_len); if (tempTextLen == 1 && output_len == 0 && (data_rc == CS_END_DATA || data_rc == CS_END_ITEM)) { isNull = 1; } tempTextLen = tempTextLen + output_len; } while (data_rc == CS_SUCCEED); if (data_rc != CS_END_DATA && data_rc != CS_END_ITEM) { fprintf(stderr, "ct_get_data failed, data_rc = %d\n", data_rc); return data_rc; } tempText[tempTextLen-1] = '\0'; if (isNull == 1) { rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); } else { rb_hash_aset(row, rb_str_new2(cols[i].name), rb_str_new2(tempText)); } free(tempText); tempText = NULL; break; case CS_BINARY_TYPE: case CS_LONGBINARY_TYPE: case CS_VARBINARY_TYPE: case CS_IMAGE_TYPE: // rb_hash_aset(row, rb_str_new2(tds->res_info->columns[i]->column_name), rb_str_new((char *) ((TDSBLOB *) src)->textvalue, tds->res_info->columns[i]->column_cur_size)); rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); break; default: rb_hash_aset(row, rb_str_new2(cols[i].name), Qnil); printf("\nUnexpected datatype: %d\n", cols[i].datatype); } } } if( rc != CS_END_DATA ) { fprintf(stderr, "ct_fetch failed"); } free(cols); cols = NULL; free(col_data); col_data = NULL; break; case CS_CMD_SUCCEED: rb_iv_set(self, "@status", Qnil); break; case CS_CMD_FAIL: if ( ct_diag(conn->connection, CS_STATUS, CS_SERVERMSG_TYPE, CS_UNUSED, &num_errors) != CS_SUCCEED ) { error_message("ct_diag CS_STATUS CS_SERVERMSG_TYPE failed"); } if (num_errors > 0) { // fprintf(stderr, "%d errors found\n", num_errors); for (i = 0; i < num_errors; i++) { if ( ct_diag(conn->connection, CS_GET, CS_SERVERMSG_TYPE, i+1, &servermsg) != CS_SUCCEED ) { error_message("ct_diag CS_GET CS_SERVERMSG_TYPE failed"); } if (servermsg.severity > 0) { // error_message(servermsg.text); rb_ary_push(errors, rb_str_new2(servermsg.text)); } } if ( ct_diag(conn->connection, CS_CLEAR, CS_SERVERMSG_TYPE, CS_UNUSED, NULL) != CS_SUCCEED ) { error_message("ct_diag CS_CLEAR CS_SERVERMSG_TYPE failed"); } } err = rb_funcall(errors, rb_intern("first"), 0); // FIXME: should probably display all errors instead of just first if(RTEST(err)) { error_msg = value_to_cstr(err); rb_raise(rb_eIOError, error_msg); } else { rb_raise(rb_eIOError, "CS_CMD_FAIL without server error message"); } // rb_iv_set(self, "@status", INT2FIX(0)); break; case CS_CMD_DONE: rb_iv_set(self, "@status", Qnil); break; case CS_STATUS_RESULT: // FIXME: We should probably do something here, right? break; default: fprintf(stderr, "ct_results returned unexpected result type: %d\n", resulttype); break; } } ct_cmd_drop(cmd); return Qnil; }
int CSybClient::write_ImsiMsisdn(int connId, char* tablename, stImsiMsisdn* pNum) { CS_CONNECTION *connection = getConnection(connId); if(NULL == connection || NULL == tablename) return -1; if(NULL == pNum) return -1; CLEAR_ALL_MESSAGE(connection); // CS_RETCODE retcode; CS_INT restype; CS_COMMAND *cmd = NULL; CS_RETCODE query_code; /* ** Get a command handle, store the command string in it, and ** send it to the server. */ if ((retcode = ct_cmd_alloc(connection, &cmd)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); return -1; } // CS_CHAR cmdstring[1024] = {0}; char strimsi[32] = {0}; memcpy(strimsi, pNum->strImsi, MINIX(sizeof(strimsi) - 1, pNum->ImsiLen)); sprintf(cmdstring, "insert into %s (IMSI, MSISDN, UpdateTime, AreaCode, SARNumber, ExchangeHouse) values (\"%s\", \"%s\", getdate(), \"%s\", %hhu, %hhu)", tablename, strimsi, pNum->strMsisdn, pNum->strAreaCode, pNum->admCenter, pNum->switchCenter); if ((retcode = ct_command(cmd, CS_LANG_CMD, cmdstring, CS_NULLTERM, CS_UNUSED)) != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); ct_cancel(NULL, cmd, CS_CANCEL_ALL); ct_cmd_drop(cmd); return -1; } if ((retcode = 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; } /* ** Examine the results coming back. If any errors are seen, the query ** result code (which we will return from this function) will be ** set to FAIL. */ query_code = CS_SUCCEED; while ((retcode = ct_results(cmd, &restype)) == CS_SUCCEED) { switch((int)restype) { case CS_CMD_SUCCEED: case CS_CMD_DONE: break; case CS_CMD_FAIL: query_code = CS_FAIL; break; case CS_STATUS_RESULT: retcode = ct_cancel(NULL, cmd, CS_CANCEL_CURRENT); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); query_code = CS_FAIL; } break; default: /* ** Unexpected result type. */ query_code = CS_FAIL; break; } if (query_code == CS_FAIL) { /* ** Terminate results processing and break out of ** the results loop */ retcode = ct_cancel(NULL, cmd, CS_CANCEL_ALL); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); } break; } } /* ** Clean up the command handle used */ if (retcode == CS_END_RESULTS) { retcode = ct_cmd_drop(cmd); if (retcode != CS_SUCCEED) { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); query_code = CS_FAIL; } } else { DIAG_CLIENT_MESSAGE(connection); DIAG_SERVER_MESSAGE(connection); (void)ct_cmd_drop(cmd); query_code = CS_FAIL; } return query_code == CS_SUCCEED ? 0 : -1; }
/* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */ int main(int argc, char **argv) { CS_CONTEXT *ctx; 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; int tds_version; 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); } len800[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; } ret = ct_con_props(conn, CS_GET, CS_TDS_VERSION, &tds_version, CS_UNUSED, NULL); if (ret == CS_SUCCEED) { if (tds_version >= CS_TDS_72) { printf("Protocol TDS7.2+ detected, test not supported\n"); try_ctlogout(ctx, conn, cmd, verbose); return 0; } } 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; }
/** * @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; }