Re: FreeTDS always setting empty VARCHAR's to NULL

Andrew Victor <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
hi,

> It seems that calling ct_param() with an empty string (ie, data !=
> NULL and datalen = 0), a NULL gets passed to the database rather than
> an empty string.
>
> According to the Sybase documentation for ct_param:
>
>    "There are two ways to indicate a parameter with a null value:
>     - Pass indicator as -1. In this case, data and datalen are ignored.
>     - Pass data as NULL and datalen as 0 or CS_UNUSED"

Attached is a patch (against 0.82) which should fix this.


Regards,
  Andrew Victor

_______________________________________________
FreeTDS mailing list
[email protected]
http://lists.ibiblio.org/mailman/listinfo/freetds
empty_string.patch (text/x-patch, 1.7 KB)
diff -urN freetds-0.82.orig/src/ctlib/ct.c freetds-0.82/src/ctlib/ct.c
--- freetds-0.82.orig/src/ctlib/ct.c	2008-05-06 05:23:45.000000000 +0200
+++ freetds-0.82/src/ctlib/ct.c	2009-06-23 20:02:27.000000000 +0200
@@ -4082,12 +4082,15 @@
 	if (!row)
 		return NULL;
 
-	if (size > 0 && value) {
+	if (value) {
 		/* TODO check for BLOB and numeric */
 		if (size > curcol->column_size)
 			size = curcol->column_size;
 		/* TODO blobs */
-		if (!is_blob_type(curcol->column_type))
+		if (size == 0) {
+			/* data field is empty */
+		}
+		else if (!is_blob_type(curcol->column_type))
 			memcpy(curcol->column_data, value, size);
 		curcol->column_cur_size = size;
 	} else {
@@ -4375,23 +4378,24 @@
 					*(param->datalen) = (*datalen == CS_UNUSED) ? 0 : *datalen;
 				}
 
-				if (*(param->datalen) && data) {
+				if (data) {
 					if (*(param->datalen) == CS_NULLTERM) {
 						tdsdump_log(TDS_DBG_INFO1, " _ct_fill_param() about to strdup string %u bytes long\n",
 							    (unsigned int) strlen(data));
 						*(param->datalen) = strlen(data);
 					} else if (*(param->datalen) < 0) {
 						return CS_FAIL;
+					} else if (*(param)->datalen) {
+						param->value = malloc(*(param->datalen));
+						if (param->value == NULL)
+							return CS_FAIL;
+						memcpy(param->value, data, *(param->datalen));
+					} else {	/* data is empty */
+						param->value = malloc(1);		/* allocate dummy value */
+						if (param->value == NULL)
+							return CS_FAIL;
 					}
-					param->value = malloc(*(param->datalen));
-					if (param->value == NULL)
-						return CS_FAIL;
-					memcpy(param->value, data, *(param->datalen));
 					param->param_by_value = 1;
-				} else {
-					param->value = NULL;
-					*(param->datalen) = 0;
-					param_is_null = 1;
 				}
 			}
 		}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.