コード例 #1
0
CS_RETCODE SybConnection::init_()
{
    CS_RETCODE retcode;
    CS_INT netio_type = CS_SYNC_IO;

    // Get a context handle to use.
    if ((retcode = cs_ctx_alloc(EX_CTLIB_VERSION, &sContext_)) != CS_SUCCEED) {
        SysLogger::error("init_: cs_ctx_alloc() failed");
        return retcode;
    }

    // Initialize Open Client.
    if ((retcode = ct_init(sContext_, EX_CTLIB_VERSION)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_init() failed");
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Install client message handlers.
    if ((retcode = ct_callback(sContext_, NULL, CS_SET, CS_CLIENTMSG_CB,
                               (CS_VOID *)clientmsg_cb)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_callback(CS_CLIENTMSG_CB) failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Install server message handlers.
    if ((retcode = ct_callback(sContext_, NULL, CS_SET, CS_SERVERMSG_CB,
                               (CS_VOID *)servermsg_cb)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_callback(CS_SERVERMSG_CB) failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    // Set the input/output type to synchronous.
    if ((retcode = ct_config(sContext_, CS_SET, CS_NETIO, &netio_type,
                             CS_UNUSED, NULL)) != CS_SUCCEED) {
        SysLogger::error("init_: ct_config() failed");
        ct_exit(sContext_, CS_FORCE_EXIT);
        cs_ctx_drop(sContext_);
        sContext_ = NULL;
        return retcode;
    }

    return CS_SUCCEED;
}
コード例 #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
ファイル: common.c プロジェクト: smalinin/FreeTDS
CS_RETCODE
continue_logging_in(CS_CONTEXT ** ctx, CS_CONNECTION ** conn, CS_COMMAND ** cmd, int verbose)
{
	CS_RETCODE ret;
	char query[30];
#ifdef TDS_STATIC_CAST
	TDSCONTEXT *tds_ctx;
#endif

	ret = cs_ctx_alloc(CS_VERSION_100, ctx);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Context Alloc failed!\n");
		}
		return ret;
	}

#ifdef TDS_STATIC_CAST
	/* Force default date format, some tests rely on it */
	tds_ctx = (TDSCONTEXT *) (*ctx)->tds_ctx;
	if (tds_ctx && tds_ctx->locale && tds_ctx->locale->date_fmt) {
		free(tds_ctx->locale->date_fmt);
		tds_ctx->locale->date_fmt = strdup("%b %d %Y %I:%M%p");
	}
#endif

	ret = ct_init(*ctx, CS_VERSION_100);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Library Init failed!\n");
		}
		return ret;
	}
	if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_CLIENTMSG_CB, 
			       (CS_VOID*) clientmsg_cb)) != CS_SUCCEED) {
		fprintf(stderr, "ct_callback() failed\n");
		return ret;
	}
	if ((ret = ct_callback(*ctx, NULL, CS_SET, CS_SERVERMSG_CB, servermsg_cb)) != CS_SUCCEED) {
		fprintf(stderr, "ct_callback() failed\n");
		return ret;
	}
	ret = ct_con_alloc(*ctx, conn);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connect Alloc failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_USERNAME, USER, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET USERNAME failed!\n");
		}
		return ret;
	}
	ret = ct_con_props(*conn, CS_SET, CS_PASSWORD, PASSWORD, CS_NULLTERM, NULL);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n");
		}
		return ret;
	}
	
	printf("connecting as %s to %s.%s\n", USER, SERVER, DATABASE);
	
	ret = ct_connect(*conn, SERVER, CS_NULLTERM);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Connection failed!\n");
		}
		return ret;
	}
	ret = ct_cmd_alloc(*conn, cmd);
	if (ret != CS_SUCCEED) {
		if (verbose) {
			fprintf(stderr, "Command Alloc failed!\n");
		}
		return ret;
	}

	strcpy(query, "use ");
	strncat(query, DATABASE, 20);

	ret = run_command(*cmd, query);
	if (ret != CS_SUCCEED)
		return ret;

	return CS_SUCCEED;
}
コード例 #4
0
ファイル: lang_ct_param.c プロジェクト: DmitrySigaev/ncbi
/* Testing: binding of data via ct_param */
int
main(int argc, char *argv[])
{
	int errCode = 0;
	
	CS_CONTEXT *ctx;
	CS_CONNECTION *conn;
	CS_COMMAND *cmd;
	int verbose = 0;

	CS_RETCODE ret;
	CS_CHAR cmdbuf[4096];

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

	fprintf(stdout, "%s: submit language query with variables using ct_param \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;
	}

	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);

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

	ret = run_command(cmd, cmdbuf);

	if (ret != CS_SUCCEED) {
		fprintf(stderr, "create table failed\n");
		errCode = 1;
		goto ERR;
	}

	/* test by name */
	errCode = insert_test(conn, cmd, 1);
	/* if worked, test by position */
	if (0 == errCode)
		errCode = insert_test(conn, cmd, 0);
	query = "insert into #ctparam_lang (name,name2,age,cost,bdate,fval) values (?, ?, ?, ?, ?, ?)";
	if (0 == errCode)
		errCode = insert_test(conn, cmd, 0);

	if (verbose && (0 == errCode))
		fprintf(stdout, "lang_ct_param tests successful\n");

ERR:
	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 errCode;
}