[freetds] Re: tsql & libtds status

Chris Sharman chris.sharman at ccagroup.co.uk
Tue Apr 5 10:37:29 EDT 2005


FreeTDS Development Group wrote:
> If you want to send a patch for csv we can incorporate in main distro.

Thanks - I've got some site & vms-specific mods, which I won't bother 
posting, but csv may be of more general interest, appended below. Hope 
that's ok - I don't have any standard difference-applying tools ...

static char separator[20] = "\t", *endsep = 0;
/* separator contains "\t", ",", "\",\"" or whatever, endsep contains 
"\"" or "'" if appropriate. */

In do_query():
case TDS_ROWFMT_RESULT:
	if ((!(opt_flags & OPT_NOHEADER)) && tds->res_info) {
		if (endsep && *endsep) fprintf(stdout, endsep);
		for (i = 0; i < tds->res_info->num_cols; i++) {
			fprintf(stdout, "%s%s", (i>0)?separator:"", 
tds->res_info->columns[i]->column_name);
		}
		if (endsep && *endsep) fprintf(stdout, endsep);
		fprintf(stdout, "\n");
	}


Similar changes go into TDS_ROW_RESULT

In main(), to allow commas (or whatever) and/or quoted values:

	char *sep;

	if ((0!=(sep=getenv("COLUMN-SEPARATOR"))) && *sep) {
	    strcpy (separator, sep);
	    if (((*sep=='"') || (*sep=='\'')) && sep[1] && 
(*sep==sep[strlen(sep)-1])) endsep = separator+(strlen(sep)-1);
	};

All this enables output to be generated as tab-separated by default, or 
comma separated or whatever (via environment variable, or DCL logical), 
and optionally quoted. It could be done via a command line switch 
instead, I suppose, but VMS struggles with switch values, particularly 
when they contain special characters (like comma and quote).
I haven't done anything to check the output to ensure it's correct (no 
embedded separators and/or quotes), mainly because I don't know a 
universally acceptable way of escaping them if I find them. Universal 
for me means Excel + MS Navision dataports. This would be required to 
make it truly production quality.

Splitting the output csv's would be a nice feature, but I've done it in 
a fairly VMSish way (freopen), so you probably don't want that. In any 
case, it wouldn't be at all compatible with previous versions.

Thanks
Chris Sharman


-----------------------------------------------------------------------

Any views expressed in this message are those of the sender and not
necessarily those of CCA Group.  The unauthorized use, disclosure,
copying or alteration of this message is forbidden.  The contents of
this message may be confidential and/or privileged, copyright CCA Group
and are intended solely for the use of the individual or entity to whom
they are addressed.  Whilst this message has been scanned, CCA Group
cannot guarantee that it is virus free or compatible with your systems
and accepts no responsibility for any loss or damage arising from its
use. The recipient is advised to run their own anti-virus software. If
you receive this message in error please contact
postmaster at ccagroup.co.uk immediately, destroy any copies and delete it
from your computer systems.


More information about the FreeTDS mailing list