static int discard_result(TDSSOCKET * tds) { int rc; int result_type; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCEED) { switch (result_type) { case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: case TDS_DESCRIBE_RESULT: case TDS_STATUS_RESULT: case TDS_PARAM_RESULT: break; default: fprintf(stderr, "Error: query should not return results\n"); return TDS_FAIL; } } if (rc == TDS_FAIL) { fprintf(stderr, "tds_process_tokens() returned TDS_FAIL\n"); return TDS_FAIL; } else if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); return TDS_FAIL; } return TDS_SUCCEED; }
static void test_end(void) { TDS_INT result_type; int done_flags; if (tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS) != TDS_SUCCESS) fatal_error("tds_process_tokens() failed"); if (result_type != TDS_DONE_RESULT) fatal_error("expected done failed"); if (done_flags & TDS_DONE_ERROR) fatal_error("query failed"); if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_NO_MORE_RESULTS) fatal_error("tds_process_tokens() failed"); }
bool TdsResultSet::Next() { //fprintf(stderr, "In TdsResultSet::Next()\n"); int nResultType; int nReturn = tds_process_tokens(m_pDatabase, &nResultType, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW|TDS_RETURN_COMPUTE); //fprintf(stderr, "Result type: %d, Looking for %d\n", nResultType, TDS_ROWFMT_RESULT); if (nReturn == TDS_SUCCEED && nResultType == TDS_ROWFMT_RESULT) { PopulateFieldMap(); CreateResultSetMetaData(); // Go to the next row since the first call only returned the column names nReturn = tds_process_tokens(m_pDatabase, &nResultType, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW);//|TDS_RETURN_COMPUTE); } //fprintf(stderr, "tds_process_tokens returned %d (%d ?)\n", nReturn, TDS_SUCCEED); return (nReturn == TDS_SUCCEED); }
ResultSetMetaData* TdsResultSet::GetMetaData() { if (m_pResultSetMetaData == NULL) { // Just grab the result set info int nResultType; int nReturn = tds_process_tokens(m_pDatabase, &nResultType, NULL, TDS_RETURN_ROWFMT); if (nReturn == TDS_SUCCEED && nResultType == TDS_ROWFMT_RESULT) { PopulateFieldMap(); CreateResultSetMetaData(); } } return m_pResultSetMetaData; }
static void test_begin(const char *cmd) { TDS_INT result_type; fprintf(stdout, "%s: Testing query\n", cmd); if (tds_submit_query(tds, cmd) != TDS_SUCCESS) fatal_error("tds_submit_query() failed"); if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) fatal_error("tds_process_tokens() failed"); if (result_type != TDS_ROWFMT_RESULT) fatal_error("expected row fmt() failed"); /* test columns results */ if (tds->current_results != tds->res_info) fatal_error("wrong current_results"); }
/* Run query for which there should be no return results */ int run_query(TDSSOCKET * tds, const char *query) { int rc; int result_type; rc = tds_submit_query(tds, query); if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_submit_query() failed for query '%s'\n", query); return TDS_FAIL; } while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { switch (result_type) { case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: /* ignore possible spurious result (TDS7+ send it) */ case TDS_STATUS_RESULT: break; default: fprintf(stderr, "Error: query should not return results\n"); return TDS_FAIL; } } if (rc == TDS_FAIL) { fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for '%s'\n", query); return TDS_FAIL; } else if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); return TDS_FAIL; } return TDS_SUCCESS; }
int main(int argc, char **argv) { TDSLOGIN *login; TDSSOCKET *tds; int verbose = 0; int num_cols = 2; TDS_INT result_type; int rc; int i, done_flags; fprintf(stdout, "%s: Test basic submit query, results\n", __FILE__); rc = try_tds_login(&login, &tds, __FILE__, verbose); if (rc != TDS_SUCCESS) { fprintf(stderr, "try_tds_login() failed\n"); return 1; } rc = tds_submit_query(tds, "select db_name() dbname, user_name() username"); if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_submit_query() failed\n"); return 1; } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { switch (result_type) { case TDS_ROWFMT_RESULT: if (tds->res_info->num_cols != num_cols) { fprintf(stderr, "Error: num_cols != %d in %s\n", num_cols, __FILE__); return 1; } if (tds->res_info->columns[0]->column_type != SYBVARCHAR || tds->res_info->columns[1]->column_type != SYBVARCHAR) { fprintf(stderr, "Wrong column_type in %s\n", __FILE__); return 1; } if (strcmp(tds_dstr_cstr(&tds->res_info->columns[0]->column_name), "dbname") || strcmp(tds_dstr_cstr(&tds->res_info->columns[1]->column_name), "username")) { fprintf(stderr, "Wrong column_name in %s\n", __FILE__); return 1; } break; case TDS_ROW_RESULT: while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) { if (result_type != TDS_ROW_RESULT || result_type != TDS_COMPUTE_RESULT) break; if (verbose) { for (i = 0; i < num_cols; i++) { printf("col %i is %s\n", i, value_as_string(tds, i)); } } } if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); } break; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_process_tokens() unexpected result_type\n"); break; } } if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); } try_tds_logout(login, tds, verbose); return 0; }
SQLerror Query(SQLrequest* req) { if (!sock) return SQLerror(SQL_BAD_CONN, "Socket was NULL, check if SQL server is running."); /* Pointer to the buffer we screw around with substitution in */ char* query; /* Pointer to the current end of query, where we append new stuff */ char* queryend; /* Total length of the unescaped parameters */ unsigned long maxparamlen, paramcount; /* The length of the longest parameter */ maxparamlen = 0; for(ParamL::iterator i = req->query.p.begin(); i != req->query.p.end(); i++) { if (i->size() > maxparamlen) maxparamlen = i->size(); } /* How many params are there in the query? */ paramcount = count(req->query.q.c_str(), '?'); /* This stores copy of params to be inserted with using numbered params 1;3B*/ ParamL paramscopy(req->query.p); /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be. * sizeofquery + (maxtotalparamlength*2) + 1 * * The +1 is for null-terminating the string */ query = new char[req->query.q.length() + (maxparamlen*paramcount*2) + 1]; queryend = query; for(unsigned long i = 0; i < req->query.q.length(); i++) { if(req->query.q[i] == '?') { /* We found a place to substitute..what fun. * use mssql calls to escape and write the * escaped string onto the end of our query buffer, * then we "just" need to make sure queryend is * pointing at the right place. */ /* Is it numbered parameter? */ bool numbered; numbered = false; /* Numbered parameter number :| */ unsigned int paramnum; paramnum = 0; /* Let's check if it's a numbered param. And also calculate it's number. */ while ((i < req->query.q.length() - 1) && (req->query.q[i+1] >= '0') && (req->query.q[i+1] <= '9')) { numbered = true; ++i; paramnum = paramnum * 10 + req->query.q[i] - '0'; } if (paramnum > paramscopy.size() - 1) { /* index is out of range! */ numbered = false; } if (numbered) { /* Custom escaping for this one. converting ' to '' should make SQL Server happy. Ugly but fast :] */ char* escaped = new char[(paramscopy[paramnum].length() * 2) + 1]; char* escend = escaped; for (std::string::iterator p = paramscopy[paramnum].begin(); p < paramscopy[paramnum].end(); p++) { if (*p == '\'') { *escend = *p; escend++; *escend = *p; } *escend = *p; escend++; } *escend = 0; for (char* n = escaped; *n; n++) { *queryend = *n; queryend++; } delete[] escaped; } else if (req->query.p.size()) { /* Custom escaping for this one. converting ' to '' should make SQL Server happy. Ugly but fast :] */ char* escaped = new char[(req->query.p.front().length() * 2) + 1]; char* escend = escaped; for (std::string::iterator p = req->query.p.front().begin(); p < req->query.p.front().end(); p++) { if (*p == '\'') { *escend = *p; escend++; *escend = *p; } *escend = *p; escend++; } *escend = 0; for (char* n = escaped; *n; n++) { *queryend = *n; queryend++; } delete[] escaped; req->query.p.pop_front(); } else break; } else { *queryend = req->query.q[i]; queryend++; } } *queryend = 0; req->query.q = query; MsSQLResult* res = new MsSQLResult((Module*)mod, req->source, req->id); res->dbid = host.id; res->query = req->query.q; char* msquery = strdup(req->query.q.data()); LoggingMutex->Lock(); ServerInstance->Logs->Log("m_mssql",LOG_DEBUG,"doing Query: %s",msquery); LoggingMutex->Unlock(); if (tds_submit_query(sock, msquery) != TDS_SUCCEED) { std::string error("failed to execute: "+std::string(req->query.q.data())); delete[] query; delete res; free(msquery); return SQLerror(SQL_QSEND_FAIL, error); } delete[] query; free(msquery); int tds_res; while (tds_process_tokens(sock, &tds_res, NULL, TDS_TOKEN_RESULTS) == TDS_SUCCEED) { //ServerInstance->Logs->Log("m_mssql",LOG_DEBUG,"<******> result type: %d", tds_res); //ServerInstance->Logs->Log("m_mssql",LOG_DEBUG,"AFFECTED ROWS: %d", sock->rows_affected); switch (tds_res) { case TDS_ROWFMT_RESULT: break; case TDS_DONE_RESULT: if (sock->rows_affected > -1) { for (int c = 0; c < sock->rows_affected; c++) res->UpdateAffectedCount(); continue; } break; case TDS_ROW_RESULT: while (tds_process_tokens(sock, &tds_res, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW) == TDS_SUCCEED) { if (tds_res != TDS_ROW_RESULT) break; if (!sock->current_results) continue; if (sock->res_info->row_count > 0) { int cols = sock->res_info->num_cols; char** name = new char*[MAXBUF]; char** data = new char*[MAXBUF]; for (int j=0; j<cols; j++) { TDSCOLUMN* col = sock->current_results->columns[j]; name[j] = col->column_name; int ctype; int srclen; unsigned char* src; CONV_RESULT dres; ctype = tds_get_conversion_type(col->column_type, col->column_size); #if _TDSVER >= 82 src = col->column_data; #else src = &(sock->current_results->current_row[col->column_offset]); #endif srclen = col->column_cur_size; tds_convert(sock->tds_ctx, ctype, (TDS_CHAR *) src, srclen, SYBCHAR, &dres); data[j] = (char*)dres.ib; } ResultReady(res, cols, data, name); } } break; default: break; } } ResultsMutex->Lock(); results.push_back(res); ResultsMutex->Unlock(); return SQLerror(); }
static int do_query(TDSSOCKET * tds, char *buf, int opt_flags) { int rows = 0; TDSRET rc; int i; TDSCOLUMN *col; int ctype; CONV_RESULT dres; unsigned char *src; TDS_INT srclen; TDS_INT resulttype; struct timeval start, stop; int print_rows = 1; char message[128]; rc = tds_submit_query(tds, buf); if (TDS_FAILED(rc)) { fprintf(stderr, "tds_submit_query() failed\n"); return 1; } while ((rc = tds_process_tokens(tds, &resulttype, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { const int stop_mask = TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE; if (opt_flags & OPT_TIMER) { gettimeofday(&start, NULL); print_rows = 0; } switch (resulttype) { case TDS_ROWFMT_RESULT: if ((!(opt_flags & OPT_NOHEADER)) && tds->current_results) { for (i = 0; i < tds->current_results->num_cols; i++) { if (i) fputs(opt_col_term, stdout); fputs(tds_dstr_cstr(&tds->current_results->columns[i]->column_name), stdout); } fputs(opt_row_term, stdout); } break; case TDS_COMPUTE_RESULT: case TDS_ROW_RESULT: rows = 0; while ((rc = tds_process_tokens(tds, &resulttype, NULL, stop_mask)) == TDS_SUCCESS) { if (resulttype != TDS_ROW_RESULT && resulttype != TDS_COMPUTE_RESULT) break; rows++; if (!tds->current_results) continue; for (i = 0; i < tds->current_results->num_cols; i++) { col = tds->current_results->columns[i]; if (col->column_cur_size < 0) { if (print_rows) { if (i) fputs(opt_col_term, stdout); fputs("NULL", stdout); } continue; } ctype = tds_get_conversion_type(col->column_type, col->column_size); src = col->column_data; if (is_blob_col(col) && col->column_type != SYBVARIANT) src = (unsigned char *) ((TDSBLOB *) src)->textvalue; srclen = col->column_cur_size; if (tds_convert(tds_get_ctx(tds), ctype, (TDS_CHAR *) src, srclen, SYBVARCHAR, &dres) < 0) continue; if (print_rows) { if (i) fputs(opt_col_term, stdout); fputs(dres.c, stdout); } free(dres.c); } if (print_rows) fputs(opt_row_term, stdout); } if (!QUIET) fprintf(stdout, "(%d row%s affected)\n", rows, rows == 1 ? "" : "s"); break; case TDS_STATUS_RESULT: if (!QUIET) printf("(return status = %d)\n", tds->ret_status); break; default: break; } if (opt_flags & OPT_VERSION) { char version[64]; int line = 0; line = tds_version(tds->conn, version); if (line) { TDSMESSAGE msg; memset(&msg, 0, sizeof(TDSMESSAGE)); msg.server = "tsql"; sprintf(message, "using TDS version %s", version); msg.message = message; tsql_handle_message(tds_get_ctx(tds), tds, &msg); } } if (opt_flags & OPT_TIMER) { TDSMESSAGE msg; gettimeofday(&stop, NULL); sprintf(message, "Total time for processing %d rows: %ld msecs\n", rows, (long) ((stop.tv_sec - start.tv_sec) * 1000) + ((stop.tv_usec - start.tv_usec) / 1000)); memset(&msg, 0, sizeof(TDSMESSAGE)); msg.server = "tsql"; msg.message = message; tsql_handle_message(tds_get_ctx(tds), tds, &msg); } } return 0; }
/** * Initialize BCP information. * Query structure of the table to server. * \tds * \param bcpinfo BCP information to initialize. Structure should be allocate * and table name and direction should be already set. */ TDSRET tds_bcp_init(TDSSOCKET *tds, TDSBCPINFO *bcpinfo) { TDSRESULTINFO *resinfo; TDSRESULTINFO *bindinfo = NULL; TDSCOLUMN *curcol; TDS_INT result_type; int i; TDSRET rc; const char *fmt; /* FIXME don't leave state in processing state */ /* TODO quote tablename if needed */ if (bcpinfo->direction != TDS_BCP_QUERYOUT) fmt = "SET FMTONLY ON select * from %s SET FMTONLY OFF"; else fmt = "SET FMTONLY ON %s SET FMTONLY OFF"; if (TDS_FAILED(rc=tds_submit_queryf(tds, fmt, bcpinfo->tablename))) /* TODO return an error ?? */ /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */ return rc; /* TODO possibly stop at ROWFMT and copy before going to idle */ /* TODO check what happen if table is not present, cleanup on error */ while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) continue; if (TDS_FAILED(rc)) return rc; /* copy the results info from the TDS socket */ if (!tds->res_info) return TDS_FAIL; resinfo = tds->res_info; if ((bindinfo = tds_alloc_results(resinfo->num_cols)) == NULL) { rc = TDS_FAIL; goto cleanup; } bindinfo->row_size = resinfo->row_size; /* Copy the column metadata */ rc = TDS_FAIL; for (i = 0; i < bindinfo->num_cols; i++) { curcol = bindinfo->columns[i]; /* * TODO use memcpy ?? * curcol and resinfo->columns[i] are both TDSCOLUMN. * Why not "curcol = resinfo->columns[i];"? Because the rest of TDSCOLUMN (below column_timestamp) * isn't being used. Perhaps this "upper" part of TDSCOLUMN should be a substructure. * Or, see if the "lower" part is unused (and zeroed out) at this point, and just do one assignment. */ curcol->funcs = resinfo->columns[i]->funcs; curcol->column_type = resinfo->columns[i]->column_type; curcol->column_usertype = resinfo->columns[i]->column_usertype; curcol->column_flags = resinfo->columns[i]->column_flags; if (curcol->column_varint_size == 0) curcol->column_cur_size = resinfo->columns[i]->column_cur_size; else curcol->column_cur_size = -1; curcol->column_size = resinfo->columns[i]->column_size; curcol->column_varint_size = resinfo->columns[i]->column_varint_size; curcol->column_prec = resinfo->columns[i]->column_prec; curcol->column_scale = resinfo->columns[i]->column_scale; curcol->on_server.column_type = resinfo->columns[i]->on_server.column_type; curcol->on_server.column_size = resinfo->columns[i]->on_server.column_size; curcol->char_conv = resinfo->columns[i]->char_conv; if (!tds_dstr_dup(&curcol->column_name, &resinfo->columns[i]->column_name)) goto cleanup; if (!tds_dstr_dup(&curcol->table_column_name, &resinfo->columns[i]->table_column_name)) goto cleanup; curcol->column_nullable = resinfo->columns[i]->column_nullable; curcol->column_identity = resinfo->columns[i]->column_identity; curcol->column_timestamp = resinfo->columns[i]->column_timestamp; memcpy(curcol->column_collation, resinfo->columns[i]->column_collation, 5); if (is_numeric_type(curcol->column_type)) { curcol->bcp_column_data = tds_alloc_bcp_column_data(sizeof(TDS_NUMERIC)); ((TDS_NUMERIC *) curcol->bcp_column_data->data)->precision = curcol->column_prec; ((TDS_NUMERIC *) curcol->bcp_column_data->data)->scale = curcol->column_scale; } else if (bcpinfo->bind_count != 0 /* ctlib */ && is_blob_col(curcol)) { curcol->bcp_column_data = tds_alloc_bcp_column_data(0); } else { curcol->bcp_column_data = tds_alloc_bcp_column_data(MAX(curcol->column_size,curcol->on_server.column_size)); } if (!curcol->bcp_column_data) goto cleanup; } if (!IS_TDS7_PLUS(tds->conn)) { bindinfo->current_row = (unsigned char*) malloc(bindinfo->row_size); if (!bindinfo->current_row) goto cleanup; bindinfo->row_free = tds_bcp_row_free; } if (bcpinfo->identity_insert_on) { rc = tds_submit_queryf(tds, "set identity_insert %s on", bcpinfo->tablename); if (TDS_FAILED(rc)) goto cleanup; /* TODO use tds_process_simple_query */ while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { } if (rc != TDS_NO_MORE_RESULTS) goto cleanup; } bcpinfo->bindinfo = bindinfo; bcpinfo->bind_count = 0; return TDS_SUCCESS; cleanup: tds_free_results(bindinfo); return rc; }
int main(int argc, char **argv) { TDSLOGIN *login; TDSSOCKET *tds; int verbose = 0; int rc, i; int result_type; int rows_returned = 0; const char *len200 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"; char *long_query = (char*) malloc(5000); strcpy(long_query, "SELECT name FROM #longquerytest WHERE ("); for (i = 0; i < 20; ++i) sprintf(strchr(long_query, 0), "name = '%c%s' OR ", 'A'+i, len200); strcat(long_query, "name = 'correct')"); fprintf(stdout, "%s: Test large (>4096 bytes) queries\n", __FILE__); rc = try_tds_login(&login, &tds, __FILE__, verbose); if (rc != TDS_SUCCESS) { fprintf(stderr, "try_tds_login() failed\n"); return 1; } /* do not check error here, if TABLE is not create this give error */ rc = run_query(tds, "DROP TABLE #longquerytest"); rc = run_query(tds, "CREATE TABLE #longquerytest (name varchar(255))"); if (rc != TDS_SUCCESS) { return 1; } rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('incorrect')"); if (rc != TDS_SUCCESS) { return 1; } rc = run_query(tds, "INSERT #longquerytest (name) VALUES ('correct')"); if (rc != TDS_SUCCESS) { return 1; } /* * The heart of the test */ if (verbose) { fprintf(stdout, "block size %d\n", tds->conn->env.block_size); } rc = tds_submit_query(tds, long_query); while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW)) == TDS_SUCCESS) { switch (result_type) { case TDS_ROWFMT_RESULT: if (tds->res_info->columns[0]->column_type != SYBVARCHAR) { fprintf(stderr, "Wrong column_type in %s\n", __FILE__); return 1; } break; case TDS_ROW_RESULT: ++rows_returned; if (verbose) { printf("col 0 is %s\n", varchar_as_string(tds, 0)); } break; default: break; } } if (rc == TDS_FAIL) { fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for long query\n"); return 1; } else if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); } if (rows_returned != 1) { fprintf(stderr, "%d rows returned, 1 expected\n", rows_returned); return 1; } /* do not check error here, if TABLE is not create this give error */ rc = run_query(tds, "DROP TABLE #longquerytest"); try_tds_logout(login, tds, verbose); free(long_query); return 0; }
void TdsPreparedStatement::FreeAllocatedResultSets() { //fprintf(stderr, "In FreeAllocatedResultSets\n"); int rc; int result_type; while ((rc = tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCEED) { switch (result_type) { case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: /* ignore possible spurious result (TDS7+ send it) */ case TDS_STATUS_RESULT: break; case TDS_ROWFMT_RESULT: case TDS_COMPUTEFMT_RESULT: case TDS_DESCRIBE_RESULT: break; case TDS_ROW_RESULT: //fprintf(stderr, "Warning: TdsPreparedStatement query should not return results. Type: %d\n", result_type); if (m_pDatabase->current_results && m_pDatabase->current_results->num_cols > 0) { while (tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW) == TDS_SUCCEED) { //fprintf(stderr, "Warning: TdsPreparedStatement TDS_ROW_RESULT query should not return results. Type: %d\n", result_type); if (result_type != TDS_ROW_RESULT) break; if (!m_pDatabase->current_results) continue; } } return; break; default: //fprintf(stderr, "Error: TdsPreparedStatement query should not return results. Type: %d\n", result_type); return; //break; } } // Clean up after ourselves if (m_pDatabase != NULL) tds_free_all_results(m_pDatabase); if (rc == TDS_FAIL) { //fprintf(stderr, "tds_process_tokens() returned TDS_FAIL\n"); SetErrorInformationFromDatabaseLayer(); ThrowDatabaseException(); return; } else if (rc != TDS_NO_MORE_RESULTS) { //fprintf(stderr, "tds_process_tokens() unexpected return\n"); SetErrorInformationFromDatabaseLayer(); ThrowDatabaseException(); return; } }
static void test0(const char *type, ...) { char buf[512]; CONV_RESULT cr; int rc; TDS_INT result_type; int done_flags; va_list ap; struct { const char *value; const char *result; } data[10]; int num_data = 0, i_row; sprintf(buf, "CREATE TABLE #tmp(a %s)", type); exec_query(buf); va_start(ap, type); for (;;) { const char * value = va_arg(ap, const char *); const char * result; if (!value) break; result = va_arg(ap, const char *); if (!result) result = value; data[num_data].value = value; data[num_data].result = result; sprintf(buf, "INSERT INTO #tmp VALUES(CONVERT(%s,'%s'))", type, value); exec_query(buf); ++num_data; } va_end(ap); assert(num_data > 0); /* execute it */ rc = tds_submit_query(tds, "SELECT * FROM #tmp"); if (rc != TDS_SUCCEED) { fprintf(stderr, "tds_submit_query() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROWFMT_RESULT) { fprintf(stderr, "expected row fmt() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROW_RESULT) { fprintf(stderr, "expected row result() failed\n"); exit(1); } i_row = 0; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCEED && (result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT)) { TDSCOLUMN *curcol = tds->current_results->columns[0]; TDS_CHAR *src = (TDS_CHAR *) curcol->column_data; int conv_type = tds_get_conversion_type(curcol->column_type, curcol->column_size); assert(i_row < num_data); if (is_blob_type(curcol->column_type)) { TDSBLOB *blob = (TDSBLOB *) src; src = blob->textvalue; } if (tds_convert(test_context, conv_type, src, curcol->column_cur_size, SYBVARCHAR, &cr) < 0) { fprintf(stderr, "Error converting\n"); g_result = 1; } else { if (strcmp(data[i_row].result, cr.c) != 0) { fprintf(stderr, "Failed! Is \n%s\nShould be\n%s\n", cr.c, data[i_row].result); g_result = 1; } free(cr.c); } ++i_row; } if (rc != TDS_NO_MORE_RESULTS && rc != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); exit(1); } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCEED) { switch (result_type) { case TDS_NO_MORE_RESULTS: return; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_process_tokens() unexpected result_type\n"); exit(1); break; } } exec_query("DROP TABLE #tmp"); }
static void test(const char *buf) { char query[1024]; char tmp[129 * 3]; int i; int rc; TDS_INT result_type; int done_flags; to_utf8(buf, tmp); sprintf(query, "SELECT 1 AS [%s]", tmp); /* do a select and check all results */ rc = tds_submit_query(tds, query); if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_submit_query() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROWFMT_RESULT) { fprintf(stderr, "expected row fmt() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROW_RESULT) { fprintf(stderr, "expected row result() failed\n"); exit(1); } i = 0; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_STOPAT_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) { TDSCOLUMN *curcol; if (result_type != TDS_ROW_RESULT) break; curcol = tds->current_results->columns[0]; if (strcmp(tmp, tds_dstr_cstr(&curcol->column_name)) != 0) { strlcpy(query, tds_dstr_cstr(&curcol->column_name), sizeof(query)); fprintf(stderr, "Wrong result Got: '%s' len %u\n Expected: '%s' len %u\n", query, (unsigned) tds_dstr_len(&curcol->column_name), tmp, (unsigned int) strlen(tmp)); exit(1); } ++i; } if (rc != TDS_SUCCESS || result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); exit(1); } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { switch (result_type) { case TDS_NO_MORE_RESULTS: return; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_proces_tokens() unexpected result_type\n"); exit(1); break; } } }
static void test(int n, int type) { int rc; TDS_INT result_type; char buf[1024], tmp[1024]; TDSCOLUMN *curcol; char *src; int done_flags; int i; char prefix[32], suffix[32]; sprintf(test_name, "test %d len %d", type, n); /* do a select and check all results */ prefix[0] = 0; suffix[0] = 0; tmp[0] = 0; switch (type) { case 0: strcpy(suffix, "C280C290"); break; case 1: /* try two invalid in different part */ strcpy(prefix, "C480C290"); strcpy(suffix, "C480C290"); break; } for (i = 0; i < n; ++i) sprintf(strchr(tmp, 0), "%02X", 0x30 + (i % 10)); sprintf(buf, "select convert(varchar(255), 0x%s%s%s)", prefix, tmp, suffix); rc = tds_submit_query(tds, buf); if (rc != TDS_SUCCEED) { fprintf(stderr, "tds_submit_query() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROWFMT_RESULT) { fprintf(stderr, "expected row fmt() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROW_RESULT) { fprintf(stderr, "expected row result() failed\n"); exit(1); } /* force tds to convert from utf8 to iso8859-1 (even on Sybase) */ tds_srv_charset_changed(tds, "UTF-8"); tds->current_results->columns[0]->char_conv = tds->char_convs[client2server_chardata]; rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE); if (rc != TDS_SUCCEED) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } curcol = tds->current_results->columns[0]; src = (char*) curcol->column_data; if (is_blob_type(curcol->column_type)) { TDSBLOB *blob = (TDSBLOB *) src; src = blob->textvalue; } prefix[0] = 0; suffix[0] = 0; tmp[0] = 0; switch (type) { case 0: strcpy(suffix, "\x80\x90"); break; case 1: /* try two invalid in different part */ strcpy(prefix, "?\x90"); strcpy(suffix, "?\x90"); /* some platforms replace invalid sequence with a fixed char */ if (invalid_char < 0) invalid_char = (unsigned char) src[0]; prefix[0] = (char) invalid_char; suffix[0] = (char) invalid_char; break; } for (i = 0; i < n; ++i) sprintf(strchr(tmp, 0), "%c", "0123456789"[i % 10]); sprintf(buf, "%s%s%s", prefix, tmp, suffix); if (strlen(buf) != curcol->column_cur_size || strncmp(buf, src, curcol->column_cur_size) != 0) { int l = curcol->column_cur_size; if (l > 1000) l = 1000; strncpy(tmp, src, l); tmp[l] = 0; fprintf(stderr, "Wrong result in %s\n Got: '%s' len %d\n Expected: '%s' len %u\n", test_name, tmp, curcol->column_cur_size, buf, (unsigned int) strlen(buf)); exit(1); } rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE); if (rc != TDS_SUCCEED || result_type == TDS_ROW_RESULT) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); exit(1); } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCEED) { switch (result_type) { case TDS_NO_MORE_RESULTS: break; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_process_tokens() unexpected result_type\n"); exit(1); break; } } }
static void test(const char *type, const char *test_name) { char buf[256]; char tmp[256]; int i; const char **s; int rc; TDS_INT result_type; int done_flags; sprintf(buf, "CREATE TABLE #tmp (i INT, t %s)", type); query(buf); /* insert all test strings in table */ for (i = 0, s = strings; *s; ++s, ++i) { sprintf(buf, "insert into #tmp values(%d, N'%s')", i, to_utf8(*s, tmp)); query(buf); } /* do a select and check all results */ rc = tds_submit_query(tds, "select t from #tmp order by i"); if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_submit_query() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROWFMT_RESULT) { fprintf(stderr, "expected row fmt() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROW_RESULT) { fprintf(stderr, "expected row result() failed\n"); exit(1); } i = 0; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROWFMT|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) { switch (result_type) { case TDS_ROW_RESULT: { TDSCOLUMN *curcol = tds->current_results->columns[0]; char *src = (char *) curcol->column_data; if (is_blob_col(curcol)) { TDSBLOB *blob = (TDSBLOB *) src; src = blob->textvalue; } strcpy(buf, to_utf8(strings[i], tmp)); if (strlen(buf) != curcol->column_cur_size || strncmp(buf, src, curcol->column_cur_size) != 0) { int l = curcol->column_cur_size; if (l > 200) l = 200; strncpy(tmp, src, l); tmp[l] = 0; fprintf(stderr, "Wrong result in test %s\n Got: '%s' len %d\n Expected: '%s' len %u\n", test_name, tmp, curcol->column_cur_size, buf, (unsigned int) strlen(buf)); exit(1); } ++i; } break; default: fprintf(stderr, "Unexpected result\n"); exit(1); break; } } if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); exit(1); } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { switch (result_type) { case TDS_NO_MORE_RESULTS: return; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_process_tokens() unexpected result_type\n"); exit(1); break; } } query("DROP TABLE #tmp"); /* do sone select to test results */ /* * for (s = strings; *s; ++s) { * printf("%s\n", to_utf8(*s, tmp)); * } */ }
int main(int argc, char **argv) { TDSLOGIN *login; TDSSOCKET *tds; int verbose = 0; int rc; int row_count, i; /* variables for conversions */ TDSCOLUMN *curcol; TDSRESULTINFO *resinfo; unsigned char *src; CONV_RESULT cr; TDS_INT srctype, srclen; int src_id = 0; double src_val; double src_err; double tolerance = 0.000001; char sql[256]; int num_sybreal = 5; float sybreal[5]; int num_sybflt8 = 7; double sybflt8[7]; int result_type; memset(&ctx, 0, sizeof(ctx)); sybreal[0] = 1.1; sybreal[1] = 12345678; sybreal[2] = 0.012345678; sybreal[3] = 1.234567890e+20; sybreal[4] = 1.234567890e-20; sybflt8[0] = 1.1; sybflt8[1] = 1234567890123456.0; sybflt8[2] = 0.01234567890123456; sybflt8[3] = 1.234567890123456e+20; sybflt8[4] = 1.234567890123456e-20; sybflt8[5] = 1.234567890123456e+200; sybflt8[6] = 1.234567890123456e-200; printf("%s: Test SYBREAL, SYBFLT8 values\n", __FILE__); rc = try_tds_login(&login, &tds, __FILE__, verbose); if (rc != TDS_SUCCEED) { fprintf(stderr, "try_tds_login() failed\n"); return 1; } /* * SYBREAL tests */ if (verbose) printf("Starting SYBREAL tests\n"); rc = run_query(tds, "DROP TABLE #test_table"); if (rc != TDS_SUCCEED) { return 1; } rc = run_query(tds, "CREATE TABLE #test_table (id int, val real)"); if (rc != TDS_SUCCEED) { return 1; } for (i = 0; i < num_sybreal; i++) { sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.8g)", i, sybreal[i]); if (verbose) printf("%s\n", sql); rc = run_query(tds, sql); if (rc != TDS_SUCCEED) { return 1; } } rc = tds_submit_query(tds, "SELECT * FROM #test_table"); row_count = 0; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCEED) { switch (result_type) { case TDS_ROW_RESULT: resinfo = tds->res_info; for (i = 0; i < resinfo->num_cols; i++) { curcol = resinfo->columns[i]; src = curcol->column_data; if (verbose) { srctype = curcol->column_type; srclen = curcol->column_size; tds_convert(&ctx, srctype, (TDS_CHAR *) src, srclen, SYBCHAR, &cr); printf("col %i is %s\n", i, cr.c); } if (i == 0) { src_id = *(int *) src; } else { src_val = *(float *) src; src_err = src_val - sybreal[src_id]; if (src_err != 0.0) { src_err = src_err / src_val; } if (src_err < -tolerance || src_err > tolerance) { fprintf(stderr, "SYBREAL expected %.8g got %.8g\n", sybreal[src_id], src_val); fprintf(stderr, "Error was %.4g%%\n", 100 * src_err); return 1; } } } row_count++; case TDS_COMPUTE_RESULT: break; default: fprintf(stderr, "tds_process_tokens() unexpected result\n"); break; } } if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); } /* * SYBFLT8 tests */ if (verbose) printf("Starting SYBFLT8 tests\n"); rc = run_query(tds, "DROP TABLE #test_table"); if (rc != TDS_SUCCEED) { return 1; } rc = run_query(tds, "CREATE TABLE #test_table (id int, val float(48))"); if (rc != TDS_SUCCEED) { return 1; } for (i = 0; i < num_sybflt8; i++) { sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.15g)", i, sybflt8[i]); if (verbose) printf("%s\n", sql); rc = run_query(tds, sql); if (rc != TDS_SUCCEED) { return 1; } } rc = tds_submit_query(tds, "SELECT * FROM #test_table"); while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCEED) { switch (result_type) { case TDS_ROW_RESULT: resinfo = tds->res_info; for (i = 0; i < resinfo->num_cols; i++) { curcol = resinfo->columns[i]; src = curcol->column_data; if (verbose) { srctype = curcol->column_type; srclen = curcol->column_size; tds_convert(&ctx, srctype, (TDS_CHAR *) src, srclen, SYBCHAR, &cr); printf("col %i is %s\n", i, cr.c); } if (i == 0) { src_id = *(int *) src; } else { memcpy(&src_val, src, 8); src_err = src_val - sybflt8[src_id]; if (src_err != 0.0) { src_err = src_err / src_val; } if (src_err < -tolerance || src_err > tolerance) { fprintf(stderr, "SYBFLT8 expected %.16g got %.16g\n", sybflt8[src_id], src_val); fprintf(stderr, "Error was %.4g%%\n", 100 * src_err); return 1; } } } case TDS_COMPUTE_RESULT: break; default: fprintf(stderr, "tds_process_tokens() returned unexpected result\n"); break; } } if (rc != TDS_NO_MORE_RESULTS) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); } try_tds_logout(login, tds, verbose); return 0; }
void TdsDatabaseLayer::FreeAllocatedResultSets() { //fprintf(stderr, "In FreeAllocatedResultSets\n"); int rc; int result_type; /* */ //while ((rc = tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCEED) //while ((rc = tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_RETURN_ROW|TDS_TOKEN_RESULTS|TDS_RETURN_COMPUTE)) == TDS_SUCCEED) while ((rc = tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCEED) { switch (result_type) { case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: case TDS_STATUS_RESULT: break; case TDS_ROWFMT_RESULT: case TDS_COMPUTEFMT_RESULT: case TDS_DESCRIBE_RESULT: break; case TDS_ROW_RESULT: //fprintf(stderr, "Warning (%d): TdsDatabaseLayer query should not return results. Type: %d\n", result_type, result_type); if (m_pDatabase->current_results && m_pDatabase->current_results->num_cols > 0) { while (tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW) == TDS_SUCCEED) { //fprintf(stderr, "Warning: TdsDatabaseLayer TDS_ROW_RESULT query should not return results. Type: %d\n", result_type); if (result_type != TDS_ROW_RESULT) break; if (!m_pDatabase->current_results) continue; } } if (m_pDatabase != NULL) tds_free_all_results(m_pDatabase); return; break; default: //fprintf(stderr, "Error: TdsDatabaseLayer query should not return results. Type: %d\n", result_type); return; //break; } } /* while (tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_TOKEN_RESULTS) == TDS_SUCCEED) { switch (result_type) { case TDS_ROWFMT_RESULT: break; case TDS_ROW_RESULT: while (tds_process_tokens(m_pDatabase, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW) == TDS_SUCCEED) { if (result_type != TDS_ROW_RESULT) break; if (!m_pDatabase->current_results) continue; TDSCOLUMN* col = m_pDatabase->current_results->columns[0]; int ctype = tds_get_conversion_type(col->column_type, col->column_size); unsigned char* src = col->column_data; int srclen = col->column_cur_size; CONV_RESULT dres; tds_convert(m_pDatabase->tds_ctx, ctype, (TDS_CHAR *) src, srclen, SYBINT4, &dres); int optionval = dres.i; } break; default: break; } } */ // Make sure to clean up after ourselves if (m_pDatabase != NULL) tds_free_all_results(m_pDatabase); if (rc == TDS_FAIL) { ThrowDatabaseException(); //fprintf(stderr, "tds_process_tokens() returned TDS_FAIL\n"); return; } else if (rc != TDS_NO_MORE_RESULTS) { ThrowDatabaseException(); //fprintf(stderr, "tds_process_tokens() unexpected return\n"); return; } }