Skip to Content.
Sympa Menu

freetds - Re: [freetds] FreeTDS always setting empty VARCHAR's to NULL

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Andrew Victor <avictor.za AT gmail.com>
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Subject: Re: [freetds] FreeTDS always setting empty VARCHAR's to NULL
  • Date: Tue, 23 Jun 2009 20:08:00 +0200

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



Archive powered by MHonArc 2.6.24.

Top of Page