コード例 #1
0
ファイル: cmd.c プロジェクト: fbessho/python-sybase
static PyObject *CS_COMMAND_ct_describe(CS_COMMANDObj *self, PyObject *args)
{
    CS_INT num;
    CS_DATAFMT datafmt;
    PyObject *fmt;
    CS_RETCODE status;

    if (!PyArg_ParseTuple(args, "i", &num))
	return NULL;

    if (self->cmd == NULL) {
	PyErr_SetString(PyExc_TypeError, "CS_COMMAND has been dropped");
	return NULL;
    }

    memset(&datafmt, 0, sizeof(datafmt));

    /* PyErr_Clear(); */

    SY_CONN_BEGIN_THREADS(self->conn);
    status = ct_describe(self->cmd, num, &datafmt);
    SY_CONN_END_THREADS(self->conn);

    if (self->debug)
	debug_msg("ct_describe(cmd%d, %d, &fmt) -> %s",
		  self->serial, (int)num, value_str(VAL_STATUS, status));
    if (PyErr_Occurred()) {
	if (self->debug)
	    debug_msg("\n");
	return NULL;
    }

    if (status != CS_SUCCEED) {
	if (self->debug)
	    debug_msg(", None\n");
	return Py_BuildValue("iO", status, Py_None);
    }

    fmt = datafmt_alloc(&datafmt, self->strip);
    if (fmt == NULL) {
	if (self->debug)
	    debug_msg("\n");
	return NULL;
    }

    if (self->debug) {
	debug_msg(", datafmt%d=", ((CS_DATAFMTObj*)fmt)->serial);
	datafmt_debug(&datafmt);
	debug_msg("\n");
    }
    return Py_BuildValue("iN", status, fmt);
}
コード例 #2
0
ファイル: ct_dynamic.c プロジェクト: hanky6/freetds
int
main(int argc, char *argv[])
{
    int errCode = 1;

    CS_RETCODE ret;
    CS_RETCODE results_ret;
    CS_CHAR cmdbuf[4096];
    CS_CHAR name[257];
    CS_INT datalength;
    CS_SMALLINT ind;
    CS_INT count;
    CS_INT num_cols;
    CS_INT row_count = 0;
    CS_DATAFMT datafmt;
    CS_DATAFMT descfmt;
    CS_INT intvar;
    CS_INT intvarsize;
    CS_SMALLINT intvarind;
    CS_INT res_type;

    int i;

    if (argc > 1 && (0 == strcmp(argv[1], "-v")))
        verbose = 1;

    printf("%s: use ct_dynamic to prepare and execute  a statement\n", __FILE__);
    if (verbose) {
        printf("Trying login\n");
    }
    ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
    if (ret != CS_SUCCEED) {
        fprintf(stderr, "Login failed\n");
        return 1;
    }

    ct_callback(ctx, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *) ex_clientmsg_cb);

    ct_callback(ctx, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *) ex_servermsg_cb);

    ret = ct_cmd_alloc(conn, &cmd2);
    chk(ret == CS_SUCCEED, "cmd2_alloc failed\n");

    /* do not test error */
    ret = run_command(cmd, "IF OBJECT_ID('tempdb..#ct_dynamic') IS NOT NULL DROP table #ct_dynamic");

    strcpy(cmdbuf, "create table #ct_dynamic (id numeric identity not null, \
        name varchar(30), age int, cost money, bdate datetime, fval float) ");

    ret = run_command(cmd, cmdbuf);
    chk(ret == CS_SUCCEED, "create table failed\n");

    strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
    strcat(cmdbuf, "values ('Bill', 44, 2000.00, 'May 21 1960', 60.97 ) ");

    ret = run_command(cmd, cmdbuf);
    chk(ret == CS_SUCCEED, "insert table failed\n");

    strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
    strcat(cmdbuf, "values ('Freddy', 32, 1000.00, 'Jan 21 1972', 70.97 ) ");

    ret = run_command(cmd, cmdbuf);
    chk(ret == CS_SUCCEED, "insert table failed\n");

    strcpy(cmdbuf, "insert into #ct_dynamic ( name , age , cost , bdate , fval ) ");
    strcat(cmdbuf, "values ('James', 42, 5000.00, 'May 21 1962', 80.97 ) ");

    ret = run_command(cmd, cmdbuf);
    chk(ret == CS_SUCCEED, "insert table failed\n");

    strcpy(cmdbuf, "select name from #ct_dynamic where age = ?");

    ret = ct_dynamic(cmd, CS_PREPARE, "age", CS_NULLTERM, cmdbuf, CS_NULLTERM);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    chk(ct_send(cmd) == CS_SUCCEED, "ct_send(CS_PREPARE) failed\n");

    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
        switch ((int) res_type) {

        case CS_CMD_SUCCEED:
        case CS_CMD_DONE:
            break;

        case CS_CMD_FAIL:
            break;

        default:
            goto ERR;
        }
    }
    chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);

    ret = ct_dynamic(cmd, CS_DESCRIBE_INPUT, "age", CS_NULLTERM, NULL, CS_UNUSED);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    chk(ct_send(cmd) == CS_SUCCEED, "ct_send(CS_DESCRIBE_INPUT) failed\n");

    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
        switch ((int) res_type) {

        case CS_DESCRIBE_RESULT:
            ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
            chk(ret == CS_SUCCEED, "ct_res_info() failed");

            for (i = 1; i <= num_cols; i++) {
                ret = ct_describe(cmd, i, &descfmt);
                chk(ret == CS_SUCCEED, "ct_describe() failed");
                fprintf(stderr, "CS_DESCRIBE_INPUT parameter %d :\n", i);
                if (descfmt.namelen == 0)
                    fprintf(stderr, "\t\tNo name...\n");
                else
                    fprintf(stderr, "\t\tName = %*.*s\n", descfmt.namelen, descfmt.namelen, descfmt.name);
                fprintf(stderr, "\t\tType   = %d\n", descfmt.datatype);
                fprintf(stderr, "\t\tLength = %d\n", descfmt.maxlength);
            }
            break;

        case CS_CMD_SUCCEED:
        case CS_CMD_DONE:
            break;

        case CS_CMD_FAIL:
            break;

        default:
            goto ERR;
        }
    }

    ret = ct_dynamic(cmd, CS_DESCRIBE_OUTPUT, "age", CS_NULLTERM, NULL, CS_UNUSED);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    chk(ct_send(cmd) == CS_SUCCEED, "ct_send(CS_DESCRIBE_OUTPUT) failed\n");

    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
        switch ((int) res_type) {

        case CS_DESCRIBE_RESULT:
            ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
            chk(ret == CS_SUCCEED, "ct_res_info() failed");
            chk(num_cols == 1, "CS_DESCRIBE_OUTPUT showed %d columns , expected 1\n", num_cols);

            for (i = 1; i <= num_cols; i++) {
                ret = ct_describe(cmd, i, &descfmt);
                chk(ret == CS_SUCCEED, "ct_describe() failed");

                if (descfmt.namelen == 0)
                    fprintf(stderr, "\t\tNo name...\n");
                else
                    fprintf(stderr, "\t\tName = %*.*s\n", descfmt.namelen, descfmt.namelen, descfmt.name);
                fprintf(stderr, "\t\tType   = %d\n", descfmt.datatype);
                fprintf(stderr, "\t\tLength = %d\n", descfmt.maxlength);
            }
            break;

        case CS_CMD_SUCCEED:
        case CS_CMD_DONE:
            break;

        case CS_CMD_FAIL:
            break;

        default:
            goto ERR;
        }
    }

    /* execute dynamic on a second command to check it still works */
    ret = ct_dynamic(cmd2, CS_EXECUTE, "age", CS_NULLTERM, NULL, CS_UNUSED);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    intvar = 44;
    intvarsize = 4;
    intvarind = 0;

    datafmt.name[0] = 0;
    datafmt.namelen = 0;
    datafmt.datatype = CS_INT_TYPE;
    datafmt.status = CS_INPUTVALUE;

    ret = ct_setparam(cmd2, &datafmt, (CS_VOID *) & intvar, &intvarsize, &intvarind);
    chk(ret == CS_SUCCEED, "ct_setparam(int) failed\n");

    chk(ct_send(cmd2) == CS_SUCCEED, "ct_send(CS_EXECUTE) failed\n");

    while ((results_ret = ct_results(cmd2, &res_type)) == CS_SUCCEED) {
        chk(res_type != CS_CMD_FAIL, "1: ct_results() result_type CS_CMD_FAIL.\n");
        chk(res_type != CS_COMPUTE_RESULT, "ct_results() unexpected CS_COMPUTE_RESULT.\n");

        switch ((int) res_type) {
        case CS_CMD_SUCCEED:
            break;
        case CS_CMD_DONE:
            break;
        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(cmd2, 1, &datafmt, name, &datalength, &ind);
            chk(ret == CS_SUCCEED, "ct_bind() failed\n");

            while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
                    || (ret == CS_ROW_FAIL)) {
                row_count += count;
                chk(ret != CS_ROW_FAIL, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
                if (ret == CS_SUCCEED) {
                    chk(!strcmp(name, "Bill"), "fetched value '%s' expected 'Bill'\n", name);
                } else {
                    break;
                }
            }
            chk(ret == CS_END_DATA, "ct_fetch() unexpected return %d.\n", (int) ret);
            break;
        default:
            fprintf(stderr, "ct_results() unexpected result_type.\n");
            return 1;
        }
    }
    chk(results_ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) results_ret);

    intvar = 32;

    chk(ct_send(cmd2) == CS_SUCCEED, "ct_send(CS_EXECUTE) failed\n");

    while ((results_ret = ct_results(cmd2, &res_type)) == CS_SUCCEED) {
        chk(res_type != CS_CMD_FAIL, "2: ct_results() result_type CS_CMD_FAIL.\n");
        chk(res_type != CS_COMPUTE_RESULT, "ct_results() unexpected CS_COMPUTE_RESULT.\n");

        switch ((int) res_type) {
        case CS_CMD_SUCCEED:
            break;
        case CS_CMD_DONE:
            break;
        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(cmd2, 1, &datafmt, name, &datalength, &ind);
            chk(ret == CS_SUCCEED, "ct_bind() failed\n");

            while (((ret = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
                    || (ret == CS_ROW_FAIL)) {
                row_count += count;
                chk(ret != CS_ROW_FAIL, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);

                if (ret == CS_SUCCEED) {
                    chk(!strcmp(name, "Freddy"), "fetched value '%s' expected 'Freddy'\n", name);
                } else {
                    break;
                }
            }
            chk(ret == CS_END_DATA, "ct_fetch() unexpected return %d.\n", (int) ret);
            break;
        default:
            fprintf(stderr, "ct_results() unexpected result_type.\n");
            return 1;
        }
    }
    chk(results_ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) results_ret);

    ret = ct_dynamic(cmd, CS_DEALLOC, "age", CS_NULLTERM, NULL, CS_UNUSED);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    chk(ct_send(cmd) == CS_SUCCEED, "ct_send(CS_DEALLOC) failed\n");

    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
        switch ((int) res_type) {

        case CS_CMD_SUCCEED:
        case CS_CMD_DONE:
            break;

        case CS_CMD_FAIL:
            break;

        default:
            goto ERR;
        }
    }
    chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);

    /*
     * check we can prepare again dynamic with same name after deallocation
     */
    strcpy(cmdbuf, "select name from #ct_dynamic where age = ?");
    ret = ct_dynamic(cmd, CS_PREPARE, "age", CS_NULLTERM, cmdbuf, CS_NULLTERM);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    chk(ct_send(cmd) == CS_SUCCEED, "ct_send(CS_PREPARE) failed\n");

    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
        switch ((int) res_type) {

        case CS_CMD_SUCCEED:
        case CS_CMD_DONE:
            break;

        case CS_CMD_FAIL:
            break;

        default:
            goto ERR;
        }
    }
    chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);

    ct_dynamic(cmd, CS_DEALLOC, "invalid", CS_NULLTERM, NULL, CS_UNUSED);
    ct_send(cmd);
    while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED)
        chk(res_type != CS_ROW_RESULT, "Rows not expected\n");
    chk(ret == CS_END_RESULTS, "ct_results() unexpected return.\n", (int) ret);

    ret = ct_dynamic(cmd2, CS_EXECUTE, "age", CS_NULLTERM, NULL, CS_UNUSED);
    chk(ret == CS_SUCCEED, "ct_dynamic failed\n");

    intvar = 32;
    intvarsize = 4;
    intvarind = 0;

    datafmt.name[0] = 0;
    datafmt.namelen = 0;
    datafmt.datatype = CS_INT_TYPE;
    datafmt.status = CS_INPUTVALUE;

    ret = ct_setparam(cmd2, &datafmt, (CS_VOID *) & intvar, &intvarsize, &intvarind);
    chk(ct_send(cmd2) == CS_SUCCEED, "ct_send(CS_EXECUTE) failed\n");

    /* all tests succeeded */
    errCode = 0;

ERR:
    cleanup();
    return errCode;
}
コード例 #3
0
ファイル: get_send_data.c プロジェクト: RQZeng/freetds
/* 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;
}
コード例 #4
0
ファイル: ct_diagclient.c プロジェクト: smalinin/FreeTDS
/* 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;
}
コード例 #5
0
ファイル: freetds.c プロジェクト: ebryn/freetds4ruby
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;	
}
コード例 #6
0
ファイル: t0009.c プロジェクト: smalinin/FreeTDS
/* 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;
}
コード例 #7
0
ファイル: array_bind.c プロジェクト: hanky6/freetds
/* 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;
}
コード例 #8
0
ファイル: sybase.c プロジェクト: ajinkya007/pike-1
/*
 * Must be called in a MT-safe fashion
 */
static INT32 guess_column_length (CS_COMMAND *cmd, int column_number) {
  CS_DATAFMT description;

  ct_describe(cmd,column_number,&description);
  return (INT32)description.maxlength;
}
コード例 #9
0
ファイル: datafmt.c プロジェクト: Distrotech/freetds
/* 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;
}
コード例 #10
0
ファイル: t0002.c プロジェクト: DavidRueter/freetds
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;
}
コード例 #11
0
ファイル: ct_cursors.c プロジェクト: smalinin/FreeTDS
int
main(int argc, char **argv)
{
	CS_CONTEXT *ctx;
	CS_CONNECTION *conn;
	CS_COMMAND *cmd;
	CS_COMMAND *cmd2;
	CS_COMMAND *cmd3;
	CS_RETCODE ret;
	CS_RETCODE ret2;
	CS_RETCODE results_ret;
	CS_INT result_type;
	CS_INT count, count2, row_count = 0;
	CS_DATAFMT datafmt;
	CS_DATAFMT datafmt2;
	CS_SMALLINT ind;
	int verbose = 1;
	CS_CHAR *name = "c1";
	CS_CHAR *name2 = "c2";
	CS_CHAR col1[6];
	CS_CHAR col2[6];
	CS_INT datalength;
	CS_CHAR text[128];
	CS_INT num_cols, i;
	int is_return_status = 0;

	fprintf(stdout, "%s: use multiple cursors on the same connection\n", __FILE__);

	if (verbose) {
		fprintf(stdout, "Trying login\n");
	}
	ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
	if (ret != CS_SUCCEED) {
		fprintf(stderr, "Login failed\n");
		return 1;
	}

	ret = ct_cmd_alloc(conn, &cmd2);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Command Alloc failed!\n");
		}
		return ret;
	}

	ret = ct_cmd_alloc(conn, &cmd3);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Command Alloc failed!\n");
		}
		return ret;
	}

	ret = run_command(cmd, "CREATE TABLE #test_table (col1 char(4))");
	if (ret != CS_SUCCEED)
		return 1;

	ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('AAA')");
	if (ret != CS_SUCCEED)
		return 1;
	ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('BBB')");
	if (ret != CS_SUCCEED)
		return 1;
	ret = run_command(cmd, "INSERT #test_table (col1) VALUES ('CCC')");
	if (ret != CS_SUCCEED)
		return 1;

	ret = run_command(cmd, "CREATE TABLE #test_table2 (col1 char(4))");
	if (ret != CS_SUCCEED)
		return 1;

	ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('XXX')");
	if (ret != CS_SUCCEED)
		return 1;

	ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('YYY')");
	if (ret != CS_SUCCEED)
		return 1;

	ret = run_command(cmd, "INSERT #test_table2 (col1) VALUES ('ZZZ')");


	if (ret != CS_SUCCEED)
		return 1;


	if (verbose) {
		fprintf(stdout, "opening first cursor on connection\n");
	}

	strcpy(text, "select col1 from #test_table order by col1");

	ret = ct_cursor(cmd, CS_CURSOR_DECLARE, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor declare failed\n");
		return 1;
	}

	ret = ct_cursor(cmd, CS_CURSOR_ROWS, name, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1);
	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor set cursor rows failed");
		return 1;
	}

	ret = ct_cursor(cmd, CS_CURSOR_OPEN, name, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor open failed\n");
		return 1;
	}

	ret = ct_send(cmd);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_send failed\n");
	}

	while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
		switch ((int) result_type) {

		case CS_CMD_SUCCEED:
		case CS_CMD_DONE:
		case CS_CMD_FAIL:
		case CS_STATUS_RESULT:
			break;

		case CS_CURSOR_RESULT:
			ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);

			if (ret != CS_SUCCEED) {
				fprintf(stderr, "ct_res_info() failed");
				return 1;
			}

			if (num_cols != 1) {
				fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
				return 1;
			}

			for (i = 0; i < num_cols; i++) {

				/* here we can finally test for the return status column */
				ret = ct_describe(cmd, i + 1, &datafmt);

				if (ret != CS_SUCCEED) {
					fprintf(stderr, "ct_describe() failed for column %d\n", i);
					return 1;
				}

				if (datafmt.status & CS_RETURN) {
					fprintf(stdout, "ct_describe() column %d \n", i);
					is_return_status = i + 1;
				}

				datafmt.datatype = CS_CHAR_TYPE;
				datafmt.format = CS_FMT_NULLTERM;
				datafmt.maxlength = 6;
				datafmt.count = 1;
				datafmt.locale = NULL;
				ret = ct_bind(cmd, 1, &datafmt, col1, &datalength, &ind);
				if (ret != CS_SUCCEED) {
					fprintf(stderr, "ct_bind() failed\n");
					return 1;
				}

			}
			break;

		case CS_COMPUTE_RESULT:
			fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
			return 1;
		default:
			fprintf(stderr, "ct_results() unexpected result_type.\n");
			return 1;
		}
	}

	if (verbose) {
		fprintf(stdout, "opening second cursor on connection\n");
	}

	strcpy(text, "select col1 from #test_table2 order by col1");

	ret = ct_cursor(cmd2, CS_CURSOR_DECLARE, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor declare failed\n");
		return 1;
	}

	ret = ct_cursor(cmd2, CS_CURSOR_ROWS, name2, CS_NULLTERM, NULL, CS_UNUSED, (CS_INT) 1);
	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor set cursor rows failed");
		return 1;
	}

	ret = ct_cursor(cmd2, CS_CURSOR_OPEN, name2, CS_NULLTERM, text, CS_NULLTERM, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor open failed\n");
		return 1;
	}

	ret = ct_send(cmd2);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_send failed\n");
		return 1;
	}

	while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
		switch ((int) result_type) {

		case CS_CMD_SUCCEED:
		case CS_CMD_DONE:
		case CS_CMD_FAIL:
		case CS_STATUS_RESULT:
			break;

		case CS_CURSOR_RESULT:
			ret = ct_res_info(cmd2, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);

			if (ret != CS_SUCCEED) {
				fprintf(stderr, "ct_res_info() failed");
				return 1;
			}

			if (num_cols != 1) {
				fprintf(stderr, "unexpected num of columns =  %d \n", num_cols);
				return 1;
			}

			for (i = 0; i < num_cols; i++) {

				/* here we can finally test for the return status column */
				ret = ct_describe(cmd2, i + 1, &datafmt2);

				if (ret != CS_SUCCEED) {
					fprintf(stderr, "ct_describe() failed for column %d\n", i);
					return 1;
				}

				if (datafmt2.status & CS_RETURN) {
					fprintf(stdout, "ct_describe() column %d \n", i);
					is_return_status = i + 1;
				}

				datafmt2.datatype = CS_CHAR_TYPE;
				datafmt2.format = CS_FMT_NULLTERM;
				datafmt2.maxlength = 6;
				datafmt2.count = 1;
				datafmt2.locale = NULL;
				ret = ct_bind(cmd2, 1, &datafmt2, col2, &datalength, &ind);
				if (ret != CS_SUCCEED) {
					fprintf(stderr, "ct_bind() failed\n");
					return 1;
				}

			}
			break;

		case CS_COMPUTE_RESULT:
			fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
			return 1;
		default:
			fprintf(stderr, "ct_results() unexpected result_type.\n");
			return 1;
		}
	}

	row_count = 0;

	while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
	       || (ret == CS_ROW_FAIL)) {

		ret2 = ct_fetch(cmd2, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count2);
		if (ret == CS_SUCCEED && ret2 == CS_SUCCEED) {
			printf("%s\t\t\t%s\n", col1, col2);
		}
	}

	switch ((int) ret) {
	case CS_END_DATA:
		break;
	case CS_ROW_FAIL:
		fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
		return 1;
	case CS_FAIL:
		fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
		return 1;
	default:
		fprintf(stderr, "ct_fetch() unexpected return.\n");
		return 1;
	}

	if (verbose) {
		fprintf(stdout, "closing first cursor on connection\n");
	}

	ret = ct_cursor(cmd, CS_CURSOR_CLOSE, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor(close) failed\n");
		return ret;
	}

	if ((ret = ct_send(cmd)) != CS_SUCCEED) {
		fprintf(stderr, "BILL ct_send() failed\n");
		return ret;
	}

	while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
		if (result_type == CS_CMD_FAIL) {
			fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
			return 1;
		}
	}
	if (results_ret != CS_END_RESULTS) {
		fprintf(stderr, "ct_results() returned BAD.\n");
		return 1;
	}

	ret = ct_cursor(cmd, CS_CURSOR_DEALLOC, name, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor(dealloc) failed\n");
		return ret;
	}

	if ((ret = ct_send(cmd)) != CS_SUCCEED) {
		fprintf(stderr, "ct_send() failed\n");
		return ret;
	}

	while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
		if (result_type == CS_CMD_FAIL) {
			fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
			return 1;
		}
	}
	if (results_ret != CS_END_RESULTS) {
		fprintf(stderr, "ct_results() returned BAD.\n");
		return 1;
	}

	if (verbose) {
		fprintf(stdout, "closing second cursor on connection\n");
	}

	ret = ct_cursor(cmd2, CS_CURSOR_CLOSE, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor(close) failed\n");
		return ret;
	}

	if ((ret = ct_send(cmd2)) != CS_SUCCEED) {
		fprintf(stderr, "BILL ct_send() failed\n");
		return ret;
	}

	while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
		if (result_type == CS_CMD_FAIL) {
			fprintf(stderr, "ct_results(2) result_type CS_CMD_FAIL.\n");
			return 1;
		}
	}
	if (results_ret != CS_END_RESULTS) {
		fprintf(stderr, "ct_results() returned BAD.\n");
		return 1;
	}

	ret = ct_cursor(cmd2, CS_CURSOR_DEALLOC, name2, CS_NULLTERM, NULL, CS_UNUSED, CS_UNUSED);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "ct_cursor(dealloc) failed\n");
		return ret;
	}

	if ((ret = ct_send(cmd2)) != CS_SUCCEED) {
		fprintf(stderr, "ct_send() failed\n");
		return ret;
	}

	while ((results_ret = ct_results(cmd2, &result_type)) == CS_SUCCEED) {
		if (result_type == CS_CMD_FAIL) {
			fprintf(stderr, "ct_results(3) result_type CS_CMD_FAIL.\n");
			return 1;
		}
	}
	if (results_ret != CS_END_RESULTS) {
		fprintf(stderr, "ct_results() returned BAD.\n");
		return 1;
	}

	ct_cmd_drop(cmd2);
	ct_cmd_drop(cmd3);

	if (verbose) {
		fprintf(stdout, "Trying logout\n");
	}
	ret = try_ctlogout(ctx, conn, cmd, verbose);
	if (ret != CS_SUCCEED) {
		fprintf(stderr, "Logout failed\n");
		return 2;
	}

	if (verbose) {
		fprintf(stdout, "Test succeded\n");
	}
	return 0;
}
コード例 #12
0
CTL_RowResult::CTL_RowResult(CS_COMMAND* cmd, CTL_Connection& conn) :
    m_Connect(&conn),
    m_Cmd(cmd),
    m_CurrItem(-1),
    m_EOR(false),
    m_BindedCols(0)
{
    CheckIsDead();

    CS_INT outlen;

    CS_INT nof_cols;
    bool rc = (Check(ct_res_info(x_GetSybaseCmd(),
                                 CS_NUMDATA,
                                 &nof_cols,
                                 CS_UNUSED,
                                 &outlen))
               != CS_SUCCEED);
    CHECK_DRIVER_ERROR( rc, "ct_res_info(CS_NUMDATA) failed." + GetDbgInfo(), 130001 );

    CS_INT bind_len = 0;
    m_BindedCols = 0;
    bool buff_is_full = false;

    m_ColFmt = AutoArray<CS_DATAFMT>(nof_cols);
    m_NullValue = AutoArray<ENullValue>(nof_cols);

    for (unsigned int nof_item = 0;  nof_item < (unsigned int) nof_cols;  nof_item++) {
        rc = (Check(ct_describe(x_GetSybaseCmd(),
                                (CS_INT) nof_item + 1,
                                &m_ColFmt[nof_item]))
            != CS_SUCCEED);
        CHECK_DRIVER_ERROR( rc, "ct_describe failed." + GetDbgInfo(), 130002 );

        m_NullValue[nof_item] = eNullUnknown;

#ifdef FTDS_IN_USE
        // Seems like FreeTDS reports wrong maxlength in
        // ct_describe() - fix this when binding to a buffer.
        if (m_ColFmt[nof_item].datatype == CS_NUMERIC_TYPE
            || m_ColFmt[nof_item].datatype == CS_DECIMAL_TYPE
            ) {
            m_ColFmt[nof_item].maxlength = sizeof(CS_NUMERIC);
        }
#endif

        m_CachedRowInfo.Add(
                string(m_ColFmt[nof_item].name, m_ColFmt[nof_item].namelen),
                m_ColFmt[nof_item].maxlength,
                ConvDataType_Ctlib2DBAPI(m_ColFmt[nof_item])
                );

        if (!buff_is_full) {
            if (m_ColFmt[nof_item].maxlength > 2048
                ||  m_ColFmt[nof_item].datatype == CS_IMAGE_TYPE)
            {
                buff_is_full = true;
            } else {
                bind_len += m_ColFmt[nof_item].maxlength;
                if (bind_len <= 2048) {
                    m_BindedCols++;
                } else {
                    buff_is_full = true;
                }
            }
        }
    }



    if(m_BindedCols) {
        m_BindItem = AutoArray<CS_VOID*>(m_BindedCols);
        m_Copied = AutoArray<CS_INT>(m_BindedCols);
        m_Indicator = AutoArray<CS_SMALLINT>(m_BindedCols);

        for(int i= 0; i < m_BindedCols; i++) {
          m_BindItem[i] = (i ? ((unsigned char*)(m_BindItem[i-1])) + m_ColFmt[i-1].maxlength : m_BindBuff);
          rc = (Check(ct_bind(x_GetSybaseCmd(),
                              i+1,
                              &m_ColFmt[i],
                              m_BindItem[i],
                              &m_Copied[i],
                              &m_Indicator[i]) )
                    != CS_SUCCEED);

            CHECK_DRIVER_ERROR( rc, "ct_bind failed." + GetDbgInfo(), 130042 );
        }
    }
}
コード例 #13
0
// returns 0=OK, -1=error (exception raised)
int command::get_row_description(row_result_t &result, unsigned column_count, ExceptionSink* xsink) {
   for (unsigned i = 0; i < column_count; ++i) {
      CS_DATAFMT_EX datafmt;
      memset(&datafmt, 0, sizeof(datafmt));

      CS_RETCODE err = ct_describe(m_cmd, i + 1, &datafmt);
      if (err != CS_SUCCEED) {
	 m_conn.do_exception(xsink, "DBI_SYBASE:EXEC-ERROR", "ct_describe() failed with error %d", (int)err);
	 return -1;
      }
      datafmt.count = 1; // fetch just single row per every ct_fetch()
      bool is_multi_byte = m_conn.getEncoding()->isMultiByte();

      printd(5, "command::get_row_description(): name=%s type=%d usertype=%d\n",
	     datafmt.name, datafmt.datatype, datafmt.usertype);

      datafmt.origin_datatype = datafmt.datatype;
      switch (datafmt.datatype) {
	 // we map DECIMAL types to strings so we have no conversion to do
	 case CS_DECIMAL_TYPE:
	 case CS_NUMERIC_TYPE:
	    datafmt.maxlength = 50;
	    datafmt.datatype = CS_CHAR_TYPE;
	    datafmt.format = CS_FMT_PADBLANK;
	    break;

	 case CS_UNICHAR_TYPE:
	    datafmt.datatype = CS_TEXT_TYPE;
	    datafmt.format = CS_FMT_NULLTERM;
	    break;

	    // freetds only works with CS_FMT_PADBLANK with CS_CHAR columns it seems
	    // however this is also compatible with Sybase's ct-lib
	 case CS_CHAR_TYPE:
	    datafmt.format = CS_FMT_PADBLANK;
	    break;

	 case CS_LONGCHAR_TYPE:
	 case CS_VARCHAR_TYPE:
	 case CS_TEXT_TYPE:
	    // if it's a multi-byte encoding, double the buffer size
	    if (is_multi_byte)
	       datafmt.maxlength *= 2;
	    datafmt.format = CS_FMT_NULLTERM;
	    break;

#ifdef FREETDS
	    // FreeTDS seems to return DECIMAL types as FLOAT for some reason
	 case CS_FLOAT_TYPE:
	    // can't find a defined USER_TYPE_* for 26
	    if (datafmt.usertype == 26) {
	       datafmt.maxlength = 50;
	       datafmt.datatype = CS_CHAR_TYPE;
	       datafmt.format = CS_FMT_NULLTERM;
	       break;
	    }
#endif

	 case CS_MONEY_TYPE:
	 case CS_MONEY4_TYPE:
	    datafmt.datatype = CS_FLOAT_TYPE;

	 default:
	    datafmt.format = CS_FMT_UNUSED;
	    break;
      }

      printd(5, "command::get_row_description(): name=%s type=%d usertype=%d maxlength=%d\n", datafmt.name, datafmt.datatype, datafmt.usertype, datafmt.maxlength);

      result.push_back(datafmt);
   }
   return 0;
}
コード例 #14
0
ファイル: ct_cursor.c プロジェクト: Distrotech/freetds
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;
}
コード例 #15
0
ファイル: t0003.c プロジェクト: hankinsoft/freetds_framework
/* 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;
}
コード例 #16
0
ファイル: ct_c.c プロジェクト: Chris00/ocaml-freetds
CAMLprim value mltds_ct_bind( value cmd, value maxlen, value index )
{
    CAMLparam3(cmd, maxlen, index);
    CS_DATAFMT fmt = {{0,0,0,0,0,0,0,0,0,0,0}};
    struct binding_buffer* buf = malloc(sizeof(struct binding_buffer));
    buf->fmt = fmt;

    retval_inspect( "ct_describe",
                    ct_describe( command_ptr(cmd), Int_val(index), &(buf->fmt) ) );

    /* NOTE: the datatype in dataformat with coerce the ct library to
       convert the data to the appropriate type, so anything we don't want to
       cast to caml we let FreeTDS cast for us */
    buf->real_type = buf->fmt.datatype;

    switch(buf->fmt.datatype)
    {
    case CS_BIT_TYPE:
        /* No change */
        break;

    case CS_TINYINT_TYPE:
    case CS_SMALLINT_TYPE:
    case CS_INT_TYPE:
        /* All become 32 bit ints, even though ocamldbi only uses 31 bit ints */
        buf->fmt.datatype = CS_INT_TYPE;
        break;

    case CS_REAL_TYPE:
    case CS_FLOAT_TYPE:
        buf->fmt.datatype = CS_FLOAT_TYPE;
        break;

    case CS_DATETIME_TYPE:
    case CS_DATETIME4_TYPE:
    case CS_MONEY_TYPE:
    case CS_MONEY4_TYPE:
    case CS_BIGINT_TYPE:
    case CS_NUMERIC_TYPE:
    case CS_DECIMAL_TYPE:
        /* I don't really understand the DECIMAL type so I let FreetDS cast to a string
           and then load in the ocamldbi Decimal.t type */
    case CS_TEXT_TYPE:
    case CS_CHAR_TYPE:
    case CS_VARCHAR_TYPE:
        buf->fmt.datatype = CS_CHAR_TYPE;
        buf->fmt.maxlength = Int_val(maxlen);
        break;

    case CS_IMAGE_TYPE:
    case CS_BINARY_TYPE:
    case CS_VARBINARY_TYPE:
        buf->fmt.datatype = CS_BINARY_TYPE;
        buf->fmt.maxlength = Int_val(maxlen);
        break;
    }

    buf->data = malloc(maxlen);

    retval_inspect( "ct_bind",
                    ct_bind(command_ptr(cmd),
                            Int_val(index),
                            &(buf->fmt),
                            (buf->data),
                            &(buf->copied),
                            &(buf->indicator)));

    CAMLreturn(column_of_buffer(buf));
}