BOOL DBConnect(char *szServer, char *szUser, char *szPswd) { // Initialize DB-Library if ( dbinit() == FAIL ) return FALSE; // Install user-supplied error-handling and message-handling dberrhandle((EHANDLEFUNC)err_handler); dbmsghandle((MHANDLEFUNC)msg_handler); // Allocate and init LOGINREC structure used to open a connection to SQL Server login = dblogin(); DBSETLUSER(login, szUser); // "sa" DBSETLPWD(login, szPswd); // "" DBSETLAPP(login, "cags_import_bcp"); // Enable bulk copy for this connection BCP_SETL(login, TRUE); // Get a connection to the database. if ((dbproc = dbopen(login, szServer)) == (DBPROCESS *) NULL) // "cgserver" { errfile << ERROR_PREFIX << "can't connect to server" << endl; return FALSE; } // Make cags the current database if ( dbuse(dbproc, "cags") == FAIL ) { errfile << ERROR_PREFIX << "can't make cags current database." << endl; return FALSE; } return TRUE; }
static int login_to_databases(BCPPARAMDATA * pdata, DBPROCESS ** dbsrc, DBPROCESS ** dbdest) { LOGINREC *slogin; LOGINREC *dlogin; /* Initialize DB-Library. */ if (dbinit() == FAIL) return FALSE; /* * Install the user-supplied error-handling and message-handling * routines. They are defined at the bottom of this source file. */ dberrhandle(err_handler); dbmsghandle(msg_handler); /* * Allocate and initialize the LOGINREC structure to be used * to open a connection to SQL Server. */ slogin = dblogin(); DBSETLUSER(slogin, pdata->suser); DBSETLPWD(slogin, pdata->spass); DBSETLAPP(slogin, "Migrate Data"); /* if packet size specified, set in login record */ if (pdata->pflag && pdata->packetsize > 0) { DBSETLPACKET(slogin, pdata->packetsize); } /* * Get a connection to the database. */ if ((*dbsrc = dbopen(slogin, pdata->sserver)) == (DBPROCESS *) NULL) { fprintf(stderr, "Can't connect to source server.\n"); return FALSE; } if (dbuse(*dbsrc, pdata->sdb) == FAIL) { fprintf(stderr, "Can't change database to %s .\n", pdata->sdb); return FALSE; } dlogin = dblogin(); DBSETLUSER(dlogin, pdata->duser); DBSETLPWD(dlogin, pdata->dpass); DBSETLAPP(dlogin, "Migrate Data"); /* Enable bulk copy for this connection. */ BCP_SETL(dlogin, TRUE); /* if packet size specified, set in login record */ if (pdata->pflag && pdata->packetsize > 0) { DBSETLPACKET(dlogin, pdata->packetsize); } /* * Get a connection to the database. */ if ((*dbdest = dbopen(dlogin, pdata->dserver)) == (DBPROCESS *) NULL) { fprintf(stderr, "Can't connect to destination server.\n"); return FALSE; } if (dbuse(*dbdest, pdata->ddb) == FAIL) { fprintf(stderr, "Can't change database to %s .\n", pdata->sdb); return FALSE; } return TRUE; }
int main(int argc, char **argv) { LOGINREC *login; DBPROCESS *dbproc; int i, rows_sent=0; int failed = 0; const char *s; const char *table_name = "all_types_bcp_unittest"; set_malloc_options(); read_login_info(argc, argv); fprintf(stdout, "Starting %s\n", argv[0]); dbinit(); dberrhandle(syb_err_handler); dbmsghandle(syb_msg_handler); fprintf(stdout, "About to logon\n"); login = dblogin(); DBSETLPWD(login, PASSWORD); DBSETLUSER(login, USER); DBSETLAPP(login, "bcp.c unit test"); BCP_SETL(login, 1); fprintf(stdout, "About to open %s.%s\n", SERVER, DATABASE); dbproc = dbopen(login, SERVER); if (strlen(DATABASE)) dbuse(dbproc, DATABASE); dbloginfree(login); if (init(dbproc, table_name)) exit(1); /* set up and send the bcp */ sprintf(cmd, "%s..%s", DATABASE, table_name); fprintf(stdout, "preparing to insert into %s ... ", cmd); if (bcp_init(dbproc, cmd, NULL, NULL, DB_IN) == FAIL) { fprintf(stdout, "failed\n"); exit(1); } fprintf(stdout, "OK\n"); test_bind(dbproc); fprintf(stdout, "Sending same row 10 times... \n"); for (i=0; i<10; i++) { if (bcp_sendrow(dbproc) == FAIL) { fprintf(stdout, "send failed\n"); exit(1); } } fprintf(stdout, "Sending 5 more rows ... \n"); for (i=15; i <= 27; i++) { int type = dbcoltype(dbproc, i); int len = (type == SYBCHAR || type == SYBVARCHAR)? dbcollen(dbproc, i) : -1; if (bcp_collen(dbproc, len, i) == FAIL) { fprintf(stdout, "bcp_collen failed for column %d\n", i); exit(1); } } for (i=0; i<5; i++) { if (bcp_sendrow(dbproc) == FAIL) { fprintf(stdout, "send failed\n"); exit(1); } } #if 1 rows_sent = bcp_batch(dbproc); if (rows_sent == -1) { fprintf(stdout, "batch failed\n"); exit(1); } #endif fprintf(stdout, "OK\n"); /* end bcp. */ if ((rows_sent += bcp_done(dbproc)) == -1) printf("Bulk copy unsuccessful.\n"); else printf("%d rows copied.\n", rows_sent); printf("done\n"); #if 1 sql_cmd(dbproc); dbsqlexec(dbproc); while ((i=dbresults(dbproc)) == SUCCEED) { dbprhead(dbproc); dbprrow(dbproc); while ((i=dbnextrow(dbproc)) == REG_ROW) { dbprrow(dbproc); } } #endif if ((s = getenv("BCP")) != NULL && 0 == strcmp(s, "nodrop")) { fprintf(stdout, "BCP=nodrop: '%s..%s' kept\n", DATABASE, table_name); } else { fprintf(stdout, "Dropping table %s\n", table_name); sql_cmd(dbproc); dbsqlexec(dbproc); while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } } dbexit(); failed = 0; fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK")); return failed ? 1 : 0; }
int main(int argc, char *argv[]) { LOGINREC *login; DBPROCESS *dbproc; int i; RETCODE ret; int big_endian = 1; char *out_file = "t0017.out"; static const char in_file_le[] = FREETDS_SRCDIR "/t0017.in"; static const char in_file_be[] = FREETDS_SRCDIR "/t0017.in.be"; const char *in_file = in_file_le; const char *err_file = "t0017.err"; DBINT rows_copied; int num_cols = 0; int col_type[256]; DBBOOL col_varylen[256]; int prefix_len; if (((char *) &big_endian)[0] == 1) big_endian = 0; if (big_endian) in_file = in_file_be; setbuf(stdout, NULL); setbuf(stderr, NULL); set_malloc_options(); read_login_info(argc, argv); printf("Starting %s\n", software_version); dbinit(); dberrhandle(syb_err_handler); dbmsghandle(syb_msg_handler); printf("About to logon ... "); login = dblogin(); assert(login); BCP_SETL(login, TRUE); DBSETLPWD(login, PASSWORD); DBSETLUSER(login, USER); DBSETLAPP(login, "t0017"); printf("done\n"); printf("Opening \"%s\" for \"%s\" ... ", SERVER, USER); dbproc = dbopen(login, SERVER); assert(dbproc); if (strlen(DATABASE)) { dbuse(dbproc, DATABASE); } dbloginfree(login); printf("done\n"); printf("Creating table ... "); dbcmd(dbproc, "create table #dblib0017 (c1 int null, c2 text)"); dbsqlexec(dbproc); while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } printf("done\n"); dbcmd(dbproc, "insert into #dblib0017(c1,c2) values(1144201745,'prova di testo questo testo dovrebbe andare a finire in un campo text')"); dbsqlexec(dbproc); while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } /* BCP out */ printf("bcp_init... "); ret = bcp_init(dbproc, "#dblib0017", out_file, err_file, DB_OUT); if (ret != SUCCEED) failed = 1; printf("done\n"); printf("Issuing SELECT ... "); dbcmd(dbproc, "select * from #dblib0017 where 0=1"); dbsqlexec(dbproc); printf("done\nFetching metadata ... "); if (dbresults(dbproc) != FAIL) { num_cols = dbnumcols(dbproc); for (i = 0; i < num_cols; ++i) { col_type[i] = dbcoltype(dbproc, i + 1); col_varylen[i] = dbvarylen(dbproc, i + 1); } while (dbnextrow(dbproc) != NO_MORE_ROWS) { } } printf("done\n"); printf("bcp_columns ... "); ret = bcp_columns(dbproc, num_cols); if (ret != SUCCEED) failed = 1; for (i = 0; i < num_cols; i++) { prefix_len = 0; if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) { prefix_len = 4; } else if (col_varylen[i]) { prefix_len = 1; } printf("bind %d prefix %d col_type %s\n", i, prefix_len, col_type[i] == SYBIMAGE ? "image" : "other"); ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1); if (ret == FAIL) { fprintf(stderr, "return from bcp_colfmt = %d\n", ret); failed = 1; } } printf("done\n"); rows_copied = -1; printf("bcp_exec ... "); ret = bcp_exec(dbproc, &rows_copied); if (ret != SUCCEED || rows_copied != 1) failed = 1; printf("%d rows copied\n", rows_copied); /* delete rows */ dbcmd(dbproc, "delete from #dblib0017"); dbsqlexec(dbproc); while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } /* * BCP in */ printf("bcp_init... "); ret = bcp_init(dbproc, "#dblib0017", in_file, err_file, DB_IN); if (ret != SUCCEED) failed = 1; printf("done\n"); printf("Issuing SELECT ... "); dbcmd(dbproc, "select * from #dblib0017 where 0=1"); dbsqlexec(dbproc); printf("done\nFetching metadata ... "); if (dbresults(dbproc) != FAIL) { num_cols = dbnumcols(dbproc); for (i = 0; i < num_cols; i++) { col_type[i] = dbcoltype(dbproc, i + 1); col_varylen[i] = dbvarylen(dbproc, i + 1); } while (dbnextrow(dbproc) != NO_MORE_ROWS) { } } while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } printf("done\n"); printf("bcp_columns ... "); ret = bcp_columns(dbproc, num_cols); if (ret != SUCCEED) failed = 1; for (i = 0; i < num_cols; i++) { prefix_len = 0; if (col_type[i] == SYBIMAGE || col_type[i] == SYBTEXT) { prefix_len = 4; } else if (col_varylen[i]) { prefix_len = 1; } ret = bcp_colfmt(dbproc, i + 1, col_type[i], prefix_len, -1, NULL, 0, i + 1); if (ret == FAIL) { fprintf(stderr, "return from bcp_colfmt = %d\n", ret); failed = 1; } } printf("done\n"); printf("bcp_exec ... "); rows_copied = -1; ret = bcp_exec(dbproc, &rows_copied); if (ret != SUCCEED || rows_copied != 1) failed = 1; printf("done\n"); /* test we inserted correctly row */ if (!failed) { dbcmd(dbproc, "SET NOCOUNT ON DECLARE @n INT SELECT @n = COUNT(*) FROM #dblib0017 WHERE c1=1144201745 AND c2 LIKE 'prova di testo questo testo dovrebbe andare a finire in un campo text' IF @n <> 1 SELECT 0"); dbsqlexec(dbproc); while (dbresults(dbproc) != NO_MORE_RESULTS) { while ((ret=dbnextrow(dbproc)) != NO_MORE_ROWS) { fprintf(stderr, "Invalid dbnextrow result %d executing query\n", ret); failed = 1; } } } printf("%d rows copied\n", rows_copied); dbclose(dbproc); dbexit(); printf("dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__); return failed ? 1 : 0; }
int main(int argc, char **argv) { LOGINREC *login; DBPROCESS *dbproc; int i, rows_sent=0; int failed = 0; const char *table_name = "all_types_bcp_unittest"; set_malloc_options(); read_login_info(); fprintf(stdout, "Start\n"); add_bread_crumb(); dbinit(); add_bread_crumb(); dberrhandle(syb_err_handler); dbmsghandle(syb_msg_handler); fprintf(stdout, "About to logon\n"); add_bread_crumb(); login = dblogin(); DBSETLPWD(login, PASSWORD); DBSETLUSER(login, USER); DBSETLAPP(login, "bcp.c unit test"); BCP_SETL(login, 1); fprintf(stdout, "About to open %s.%s\n", SERVER, DATABASE); add_bread_crumb(); dbproc = dbopen(login, SERVER); if (strlen(DATABASE)) dbuse(dbproc, DATABASE); add_bread_crumb(); add_bread_crumb(); if (init(dbproc, table_name)) exit(1); /* set up and send the bcp */ sprintf(cmd, "%s..%s", DATABASE, table_name); fprintf(stdout, "preparing to insert into %s ... ", cmd); if (bcp_init(dbproc, cmd, (BYTE *)NULL, (BYTE *)NULL, DB_IN) == FAIL) { fprintf(stdout, "failed\n"); exit(1); } fprintf(stdout, "OK\n"); bind(dbproc); fprintf(stdout, "Sending same row 10 times... \n"); for (i=0; i<10; i++) { if (bcp_sendrow(dbproc) == FAIL) { fprintf(stdout, "send failed\n"); exit(1); } } #if 1 rows_sent = bcp_batch(dbproc); if (rows_sent == -1) { fprintf(stdout, "batch failed\n"); exit(1); } #endif fprintf(stdout, "OK\n"); /* end bcp. */ if ((rows_sent += bcp_done(dbproc)) == -1) printf("Bulk copy unsuccessful.\n"); else printf("%d rows copied.\n", rows_sent); printf("done\n"); add_bread_crumb(); fprintf(stdout, "Dropping table %s\n", table_name); add_bread_crumb(); sprintf(cmd, "drop table %s", table_name); dbcmd(dbproc, cmd); add_bread_crumb(); dbsqlexec(dbproc); add_bread_crumb(); while (dbresults(dbproc) != NO_MORE_RESULTS) { /* nop */ } add_bread_crumb(); failed = 0; fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__); return failed ? 1 : 0; }
BEGIN_NCBI_SCOPE CDBL_Connection::CDBL_Connection(CDBLibContext& cntx, const CDBConnParams& params) : impl::CConnection(cntx, params, true), m_DBLibCtx(&cntx), m_Login(NULL), m_Link(NULL), m_ActiveCmd(NULL) { m_Login = CheckWhileOpening(dblogin()); _ASSERT(m_Login); const string err_str( "Cannot connect to the server '" + params.GetServerName() + "' as user '" + params.GetUserName() + "'" ); if (!GetCDriverContext().GetHostName().empty()) DBSETLHOST(m_Login, (char*) GetCDriverContext().GetHostName().c_str()); if (GetDBLibCtx().GetPacketSize() > 0) DBSETLPACKET(m_Login, GetDBLibCtx().GetPacketSize()); if (DBSETLAPP (m_Login, (char*) GetCDriverContext().GetApplicationName().c_str()) != SUCCEED || DBSETLUSER(m_Login, (char*) params.GetUserName().c_str()) != SUCCEED || DBSETLPWD (m_Login, (char*) params.GetPassword().c_str()) != SUCCEED) { DATABASE_DRIVER_ERROR( err_str, 200011 ); } DBSETLCHARSET( m_Login, const_cast<char*>( GetCDriverContext().GetClientCharset().c_str() ) ); BCP_SETL(m_Login, TRUE); if (params.GetParam("secure_login") == "true") DBSETLENCRYPT(m_Login, TRUE); string server_name; server_name = params.GetServerName(); m_Link = CheckWhileOpening(dbopen(m_Login, (char*) server_name.c_str())); /* Error: Could not open interface file. * This is a development code. DO NOT DELETE IT !!! if (params.GetHost()) { server_name = impl::ConvertN2A(params.GetHost()); // server_name = NStr::IntToString(params.GetHost()); string port_str = NStr::IntToString(params.GetPort()); RETCODE rc = dbsetconnect( "query", // "master", "tcp", "ether", // (char*)server_name.c_str(), //"130.14.24.38", "sybdev", // (char*)port_str.c_str() "2158" ); CHECK_DRIVER_ERROR(rc != SUCCEED, "dbsetconnect failed.", 200001); m_Link = CheckWhileOpening(dbopen(m_Login, NULL)); } else { server_name = params.GetServerName(); m_Link = CheckWhileOpening(dbopen(m_Login, (char*) server_name.c_str())); } */ // It doesn't work currently ... // if (dbprocess) { // CHECK_DRIVER_ERROR( // CheckWhileOpening(dbsetopt( // dbprocess, // DBBUFFER, // const_cast<char*>(NStr::UIntToString(GetBufferSize()).c_str()), // -1)) != SUCCEED, // "dbsetopt failed", // 200001 // ); // } if (!m_Link) { DATABASE_DRIVER_ERROR( err_str, 200011 ); } // Set user-data. That will let us get server and user names in case of // any problem. dbsetuserdata(GetDBLibConnection(), (BYTE*) this); CheckFunctCall(); }