freetds AT lists.ibiblio.org
Subject: FreeTDS Development Group
List archive
- From: christos AT zoulas.com (Christos Zoulas)
- To: freetds AT lists.ibiblio.org
- Subject: [freetds] Yet another bcp fix
- Date: Fri, 5 Mar 2004 16:18:25 -0500
Hello,
The following fixes:
1. end of file on the first fixed length column produces spurious complaints
2. nullable fields [in MSSQL2k] are handled by always prefixing them with a
length; this true for all types, not only varchar as the previous patch
fixed. nullable fields of fixed length, are prefixed by a single byte of
length.
With these two fixes, I've been able to bcp back and forth pretty complex
tables, and I've compared the binary files produces by M$'s bcp and freetds's
and are identical.
I have not tested any other database but MSSQL2k.
christos
Index: bcp.c
===================================================================
RCS file: /src/twosigma/cvsroot/pub/devel/freetds/src/dblib/bcp.c,v
retrieving revision 1.3
diff -u -u -r1.3 bcp.c
--- bcp.c 5 Mar 2004 20:10:34 -0000 1.3
+++ bcp.c 5 Mar 2004 21:13:53 -0000
@@ -491,6 +491,7 @@
int srctype;
int buflen;
int destlen;
+ int plen;
BYTE *outbuf;
TDS_INT rowtype;
@@ -765,12 +766,16 @@
/* FIX ME -- does not handle prefix_len == -1
*/
/* The prefix */
- switch (hostcol->prefix_len) {
- case -1:
- if
(!(is_fixed_type(hostcol->datatype))) {
- si = buflen;
- fwrite(&si, sizeof(si), 1,
hostfile);
- }
+ if ((plen = hostcol->prefix_len) == -1) {
+ if
(!(is_fixed_type(hostcol->datatype)))
+ plen = 2;
+ else if (curcol->column_nullable)
+ plen = 1;
+ else
+ plen = 0;
+ }
+ switch (plen) {
+ case 0:
break;
case 1:
ti = buflen;
@@ -984,16 +989,26 @@
*/
} else { /* unterminated field */
bcpcol = dbproc->bcp.db_columns[hostcol->tab_colnum -
1];
- if (collen == 0 && (is_nullable_type(bcpcol->db_type)
- || bcpcol->db_nullable)) {
- TDS_SMALLINT len;
+ if (collen == 0 || bcpcol->db_nullable) {
+ if (collen != 0) {
+ /* A fixed length type */
+ TDS_TINYINT len;
+ if (fread(&len, sizeof(len), 1,
hostfile) != 1) {
+ if (i != 0)
+
_bcp_err_handler(dbproc, SYBEBCRE);
+ return (FAIL);
+ }
+ collen = len == 255 ? -1 : len;
+ } else {
+ TDS_SMALLINT len;
- if (fread(&len, sizeof(len), 1, hostfile) !=
1) {
- if (i != 0)
- _bcp_err_handler(dbproc,
SYBEBCRE);
- return (FAIL);
+ if (fread(&len, sizeof(len), 1,
hostfile) != 1) {
+ if (i != 0)
+
_bcp_err_handler(dbproc, SYBEBCRE);
+ return (FAIL);
+ }
+ collen = len;
}
- collen = len;
if (collen == -1) {
collen = 0;
data_is_null = 1;
@@ -1017,8 +1032,11 @@
* call and test it.
*/
if (fread(coldata, collen, 1, hostfile) != 1)
{
- _bcp_err_handler(dbproc, SYBEBCRE);
free(coldata);
+ if (i == 0 && feof(hostfile))
+ tdsdump_log(TDS_DBG_FUNC, "%L
Normal end-of-file reached while loading bcp data file.\n");
+ else
+ _bcp_err_handler(dbproc,
SYBEBCRE);
return (FAIL);
}
}
- [freetds] Yet another bcp fix, Christos Zoulas, 03/05/2004
Archive powered by MHonArc 2.6.24.