Example #1
0
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;
}
Example #2
0
int
main(int argc, char **argv)
{
	int verbose = 1;
	CS_CONTEXT *ctx;

	CS_CHAR string_in[16], string_out[16];
	CS_INT  int_in,        int_out;
	CS_INT ret_len;

	if (verbose) {
		fprintf(stdout, "Trying cs_config with CS_USERDATA\n\n");
	}

	if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) {
		fprintf(stderr, "cs_ctx_alloc() for first context failed\n");
	}
	if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) {
		fprintf(stderr, "ct_init() for first context failed\n");
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with char array\n");

	strcpy(string_in,"FreeTDS");

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)string_in,  CS_NULLTERM, NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}
	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)string_out, 16, &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (strcmp(string_in, string_out)) {
		fprintf(stdout, "returned value >%s< not as stored >%s<\n", (char *)string_out, (char *)string_in);
		return 1;
	}
    if (ret_len != (strlen(string_in) + 1)) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) (strlen(string_in) + 1));
		return 1;
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with char array\n");

	strcpy(string_in,"FreeTDS");

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)string_in,  CS_NULLTERM, NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}

	strcpy(string_out,"XXXXXXXXXXXXXXX");

	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)string_out, 4, &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (strcmp(string_out, "FreeXXXXXXXXXXX")) {
		fprintf(stdout, "returned value >%s< not as expected >%s<\n", (char *)string_out, "FreeXXXXXXXXXXX");
		return 1;
	}
    if (ret_len != (strlen(string_in) + 1)) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) (strlen(string_in) + 1));
		return 1;
	}

	fprintf(stdout, "Testing CS_SET/GET USERDATA with int\n");

	int_in = 255;

	if (cs_config(ctx, CS_SET, CS_USERDATA, (CS_VOID *)&int_in,  sizeof(int), NULL)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() set failed\n");
		return 1;
	}
	if (cs_config(ctx, CS_GET, CS_USERDATA, (CS_VOID *)&int_out, sizeof(int), &ret_len)
	    != CS_SUCCEED) {
		fprintf(stderr, "cs_config() get failed\n");
		return 1;
	}

	if (int_in != int_out) {
		fprintf(stdout, "returned value >%d< not as stored >%d<\n", int_out, int_in);
		return 1;
	}
	if (ret_len != (sizeof(int))) {
		fprintf(stdout, "returned length >%d< not as expected >%u<\n", ret_len, (unsigned int) sizeof(int));
		return 1;
	}

	cs_ctx_drop(ctx);

	return 0;
}