void mltds_ct_con_finalize(value connection) { CS_CONNECTION* conn = connection_ptr(connection); ct_close(conn, CS_FORCE_CLOSE); ct_con_drop(conn); }
int main(int argc, char **argv) { CS_CONTEXT *ctx; CS_CONNECTION *conn; int ret = 1; read_login_info(); if (cs_ctx_alloc(CS_VERSION_100, &ctx) != CS_SUCCEED) { fprintf(stderr, "Context Alloc failed!\n"); return ret; } if (ct_init(ctx, CS_VERSION_100) != CS_SUCCEED) { fprintf(stderr, "Library Init failed!\n"); return ret; } if (ct_con_alloc(ctx, &conn) != CS_SUCCEED) { fprintf(stderr, "Connect Alloc failed!\n"); return ret; } if (ct_con_props(conn, CS_SET, CS_USERNAME, (CS_VOID*) "sa", CS_NULLTERM, NULL) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() SET USERNAME failed!\n"); return ret; } if (ct_con_props(conn, CS_SET, CS_PASSWORD, (CS_VOID*) "invalid", CS_NULLTERM, NULL) != CS_SUCCEED) { fprintf(stderr, "ct_con_props() SET PASSWORD failed!\n"); return ret; } if (ct_connect(conn, SERVER, CS_NULLTERM) != CS_FAIL) { fprintf(stderr, "Connection succeeded??\n"); return ret; } if (ct_cancel(conn, NULL, CS_CANCEL_ALL) != CS_SUCCEED) { fprintf(stderr, "ct_cancel() failed!\n"); return ret; } if (ct_close(conn, CS_UNUSED) != CS_SUCCEED) { fprintf(stderr, "ct_close() failed!\n"); return ret; } if (ct_con_drop(conn) != CS_SUCCEED) { fprintf(stderr, "ct_con_drop() failed!\n"); return ret; } if (ct_exit(ctx, CS_UNUSED) != CS_SUCCEED) { fprintf(stderr, "ct_exit() failed!\n"); return ret; } if (cs_ctx_drop(ctx) != CS_SUCCEED) { fprintf(stderr, "cs_ctx_drop() failed!\n"); return ret; } fprintf(stdout, "Test succeeded\n"); return 0; }
CS_RETCODE try_ctlogout(CS_CONTEXT * ctx, CS_CONNECTION * conn, CS_COMMAND * cmd, int verbose) { CS_RETCODE ret; ret = ct_cancel(conn, NULL, CS_CANCEL_ALL); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "ct_cancel() failed!\n"); } return ret; } ct_cmd_drop(cmd); ct_close(conn, CS_UNUSED); ct_con_drop(conn); ct_exit(ctx, CS_UNUSED); cs_ctx_drop(ctx); return CS_SUCCEED; }
CS_RETCODE SybConnection::con_cleanup_(CS_RETCODE status) { if (conn_) { CS_RETCODE retcode; CS_INT close_option; close_option = (status != CS_SUCCEED) ? CS_FORCE_CLOSE : CS_UNUSED; retcode = ct_close(conn_, close_option); if (retcode != CS_SUCCEED) { SysLogger::error("con_cleanup_: ct_close() failed"); return retcode; } retcode = ct_con_drop(conn_); if (retcode != CS_SUCCEED) { SysLogger::error("con_cleanup_: ct_con_drop() failed"); return retcode; } return retcode; } else { return CS_SUCCEED; } }
CS_RETCODE SybConnection::connect_(const char *app, const char *addr, const char *user, const char *pwd, const char *db) { CS_RETCODE retcode; CS_BOOL hafailover = CS_TRUE; // Allocate a connection structure. retcode = ct_con_alloc(sContext_, &conn_); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_alloc() failed"); return retcode; } // If a appname is defined, set the CS_APPNAME property. if (app != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_APPNAME, (CS_VOID*)app, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_APPNAME) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a servername is defined, set the CS_SERVERNAME property. if (addr != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_SERVERADDR, (CS_VOID*)addr, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_SERVERADDR) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a username is defined, set the CS_USERNAME property. if (user != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_USERNAME, (CS_VOID*)user, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_USERNAME) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // If a password is defined, set the CS_PASSWORD property. if (pwd != NULL) { retcode = ct_con_props(conn_, CS_SET, CS_PASSWORD, (CS_VOID*)pwd, CS_NULLTERM, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_PASSWORD) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } } // Set the CS_HAFAILOVER property. retcode = ct_con_props(conn_, CS_SET, CS_HAFAILOVER, &hafailover, CS_UNUSED, NULL); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_con_props(CS_HAFAILOVER) failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } // Connect to the server. retcode = ct_connect(conn_, NULL, CS_UNUSED); if (retcode != CS_SUCCEED) { SysLogger::error("connect_: ct_connect() failed"); ct_con_drop(conn_); conn_ = NULL; return retcode; } return CS_SUCCEED; }
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"); } ct_con_drop(*conn); *conn = NULL; cs_ctx_drop(*ctx); *ctx = NULL; return ret; } ret = ct_cmd_alloc(*conn, cmd); if (ret != CS_SUCCEED) { if (verbose) { fprintf(stderr, "Command Alloc failed!\n"); } ct_con_drop(*conn); *conn = NULL; cs_ctx_drop(*ctx); *ctx = NULL; return ret; } strcpy(query, "use "); strncat(query, DATABASE, 20); ret = run_command(*cmd, query); if (ret != CS_SUCCEED) return ret; return CS_SUCCEED; }