[freetds] ***POTENTIAL SPAM*** More tsql options

Chris.Fernandes at barclayscapital.com Chris.Fernandes at barclayscapital.com
Tue Jun 20 12:59:01 EDT 2006


>Did you know about DBD::Sybase or DBD::ODBC? Both work with the FreeTDS
No, I did not (I feel I must apologize for not knowing:)

In any case, I do often use for pipelined commandline queries, so would
hope that following diffs are accepted.
Regards,
Chris


84a85,100
> typedef char*	strptr;
> typedef strptr*	strptrarr;
> static int splitMultilineString(char* instr, const char* sepstr, int*
numout, strptrarr* arr);
> 
> static strptrarr	global_query_arr	= 0;
> static char* global_query_sep	= 0;
> static int   global_query_num	= 0;
> static char* global_database	= 0;
> static char* global_sep		= 0;
> static char* global_resultsep	= 0;
> static char* global_null	= 0;
> static int   global_null_quote	= 1;
> static int   global_header_quote= 1;
> static char* global_quote_pre	= 0;
> static char* global_quote_post	= 0;
> 
122a139,188
> static char * getStatement(char* prompt);
> 
> static char *
> getStatement(char* prompt)
> {
>   if(global_query_num <= 0)
>     return readline(prompt);
>   static int	numDone = 0;
>   char*		stmt	= 0;
>   if(numDone == 0)
>     {
>       if(global_database)
> 	{
> 	  stmt = (char *) malloc(strlen("use ") +
strlen(global_database)+1);	strcpy(stmt, global_database);
> 	  strcpy(stmt, "use ");
> 	  strcpy(stmt + strlen("use "), global_database);
> 	}
>       else numDone == 2;	/* use default database */
>     }
>   if(numDone == 1)
>     {
>       stmt = (char *) malloc(strlen("go")+1);	strcpy(stmt, "go");
>     }
>   if(numDone >= 2)
>     {
>       int querynum	= (int)((numDone-2)/2);
>       if(querynum < global_query_num)
> 	{
> 	  if(numDone == (2*((int)(numDone/2))))
> 	    {
> 	      char*	querystr	= global_query_arr[querynum];
> 	      if(strlen(querystr) > 0)
> 		{
> 		  stmt = (char *) malloc(strlen(querystr)+1);
strcpy(stmt, querystr);
> 		}
> 	    }
> 	  else
> 	    {
> 	      stmt = (char *) malloc(strlen("go")+1);	strcpy(stmt,
"go");
> 	    }
> 	}
>       else
> 	{
> 	  stmt = (char *) malloc(strlen("quit")+1);	strcpy(stmt,
"quit");
> 	}
>     }
>   numDone++;
>   return stmt;
> }
> 
155c221,225
< 					fprintf(stdout, "%s\t",
tds->res_info->columns[i]->column_name);
---
> 				  if(i) fprintf(stdout, "%s",
global_sep);
> 				  if(global_header_quote)
> 				    fprintf(stdout, "%s%s%s",
global_quote_pre, tds->res_info->columns[i]->column_name,
global_quote_post);
> 				  else
> 				    fprintf(stdout, "%s",
tds->res_info->columns[i]->column_name);
171c241,247
< 							fprintf(stdout,
"NULL\t");
---
> 						  {
> 						    if(i)
fprintf(stdout, "%s", global_sep);
>
if(global_null_quote)
> 						      fprintf(stdout,
"%s%s%s", global_quote_pre, global_null, global_quote_post);
> 						    else
> 						      fprintf(stdout,
"%s", global_null);
> 						  }
186c262,265
< 						fprintf(stdout, "%s\t",
dres.c);
---
> 					  {
> 					    if(i) fprintf(stdout, "%s",
global_sep);
> 					    fprintf(stdout, "%s%s%s",
global_quote_pre, dres.c, global_quote_post);
> 					  }
190,191c269
< 					fprintf(stdout, "\n");
< 
---
> 				    fprintf(stdout, "\n");
225c303
< 		"Usage:\t%s [-S <server> | -H <hostname> -p <port>] -U
<username> [ -P <password> ] [ -I <config file> ]\n\t%s -C\n",
---
> 		"Usage:\t%s [-S <server> | -H <hostname> -p <port>] -U
<username> [ -P <password> ] [ -I <config file> ] [ -s <sepstr> ] [ -a
<quotestr> ] [ -d <database> ] [ -Q <query> ] [ -n <nullstr> ] [-A] [-b]
[-R resultsepstr] [-B querysep]\n\t%s -C\n",
226a305,307
> 		fprintf(stderr, "	-A	# dont quote NULL\n");
> 		fprintf(stderr, "	-b	# dont quote column
headers\n");
> 	fprintf(stderr, "	NOTE : -d,-Q,-R,-B options are intended
non-interactive use\n");
272a354,393
> static int splitMultilineString(char* instr, const char* sepstr, int*
numout, strptrarr* arr)
> {
>   if(!instr)  return 1;
>   if(!sepstr) return 2;
>   if(!numout) return 3;
> 
>   const char* pstr	= instr;
>   const char* str	= instr;
>   while(1)
>     {
>       str	= strstr(str, sepstr);
>       ++global_query_num;
>       if(!str) break;
>       str		+= strlen(sepstr);
>       pstr		= str;
>     }
>   *arr		= (strptrarr) malloc(global_query_num *
sizeof(strptr));
>   int	i;
> 
>   for(i = 0; i < global_query_num; i++)
>     {
>       (*arr)[i]	= 0;
>     }
> 
>   pstr		= instr;
>   str		= instr;
>   i		= 0;
>   while(1)
>     {
>       str		= strstr(str, sepstr);
>       int	len	= str? (str - pstr) : strlen(pstr);
>       (*arr)[i]	= (char*) malloc(len + 1);  strncpy((*arr)[i],
pstr, len); (*arr)[i][len]	= '\0';
>       if(!str) break;
>       str	+= strlen(sepstr);
>       pstr	= str;
>       i++;
>     }
>   return 0;
> }
> 
286a408
> 	char*	opt_query_str = 0;
300c422
< 	while ((opt = getopt(argc, argv, "H:S:I:V::P:U:p:vCo:")) != -1)
{
---
> 	while ((opt = getopt(argc, argv,
"H:S:I:V::P:U:p:Co:s:d:a:Q:n:AR:B:b")) != -1) {
327a450,501
> 		case 'n':
> 		  if(optarg)
> 		    {
> 		      global_null = (char *) malloc(strlen(optarg) + 1);
strcpy(global_null, optarg);
> 		    }
> 			break;
> 		case 's':
> 		  if(optarg)
> 		    {
> 		      global_sep = (char *) malloc(strlen(optarg) + 1);
strcpy(global_sep, optarg);
> 		    }
> 			break;
> 		case 'R':
> 		  if(optarg)
> 		    {
> 		      global_resultsep = (char *) malloc(strlen(optarg)
+ 1);		strcpy(global_resultsep, optarg);
> 		    }
> 			break;
> 		case 'd':
> 		  if(optarg)
> 		    {
> 		      global_database = (char *) malloc(strlen(optarg) +
1);	strcpy(global_database, optarg);
> 		    }
> 			break;
> 		case 'B':
> 		  if(optarg)
> 		    {
> 		      global_query_sep = (char *) malloc(strlen(optarg)
+ 1);	strcpy(global_query_sep, optarg);
> 		    }
> 			break;
> 		case 'Q':
> 		  if(optarg)
> 		    {
> 		      opt_query_str = (char *) malloc(strlen(optarg) +
1);	strcpy(opt_query_str, optarg);
> 		    }
> 			break;
> 		case 'a':
> 		  if(optarg)
> 		    {
> 		      global_quote_pre = (char *) malloc(strlen(optarg)
+ 1);	      strcpy(global_quote_pre, optarg);
> 		      global_quote_post= (char *) malloc(strlen(optarg)
+ 1);	      strcpy(global_quote_post, optarg);
> 		    }
> 			break;
> 		case 'A':
> 		  global_null_quote = 0;
> 			break;
> 		case 'b':
> 		  global_header_quote = 0;
> 			break;
> 		case 'h':
> 		  global_null_quote = 0;
> 			break;
349a524,550
> 	if(opt_query_str)
> 	  {
> 	    if(!global_query_sep)
> 	      {
> 		global_query_sep	= (char *) malloc(2);
strcpy(global_query_sep, ",");
> 	      }
> 	    splitMultilineString(opt_query_str, global_query_sep,
&global_query_num, &global_query_arr);
> 	    free(opt_query_str);
> 	  }
> 
> 	if(!global_null)	/* enforce legacy */
> 	  {
> 	    global_null		= (char *) malloc(strlen("NULL")+1);
strcpy(global_null, "NULL");
> 	  }
> 
> 	if(!global_sep)		/* enforce legacy - trailing tab and no
quotes */
> 	  {
> 	    global_sep		= (char *) malloc(1);
strcpy(global_sep, "");
> 	    global_quote_pre	= (char *) malloc(1);
strcpy(global_quote_pre, "");
> 	    global_quote_post	= (char *) malloc(2);
strcpy(global_quote_post, "\t");
> 	  }
> 	else if(!global_quote_pre)	/* no trailing, no quoting */
> 	  {
> 	    global_quote_pre	= (char *) malloc(1);
strcpy(global_quote_pre, "");
> 	    global_quote_post	= (char *) malloc(1);
strcpy(global_quote_post, "");
> 	  }
> 
527c728
< 		s = readline(prompt);
---
> 		s = getStatement(prompt);
551a753,760
> 			/* Print resultset separator - except for any
implicit "use <database>" which is the first sql statement*/
> 			if(global_resultsep)
> 			  {
> 			    static int firstTimeHere	= 1;
> 			    if(global_database && firstTimeHere)
> 			      firstTimeHere = 0;
> 			    else fprintf(stdout, "%s\n",
global_resultsep);
> 			  }
------------------------------------------------------------------------
For more information about Barclays Capital, please visit our web site at http://www.barcap.com.

Internet communications are not secure and therefore the Barclays Group does not accept legal responsibility for the contents of this message.  Although the Barclays Group operates anti-virus programmes, it does not accept responsibility for any damage whatsoever that is caused by viruses being passed.  Any views or opinions presented are solely those of the author and do not necessarily represent those of the Barclays Group.  Replies to this email may be monitored by the Barclays Group for operational or business reasons.
------------------------------------------------------------------------


More information about the FreeTDS mailing list