convert.c 41.3 KB
Newer Older
B
Bruce Momjian 已提交
1
/* Module:		   convert.c
2
 *
B
Bruce Momjian 已提交
3 4 5 6 7 8
 * Description:    This module contains routines related to
 *				   converting parameters and columns into requested data types.
 *				   Parameters are converted from their SQL_C data types into
 *				   the appropriate postgres type.  Columns are converted from
 *				   their postgres type (SQL type) into the appropriate SQL_C
 *				   data type.
9
 *
B
Bruce Momjian 已提交
10
 * Classes:		   n/a
11 12 13
 *
 * API functions:  none
 *
B
Bruce Momjian 已提交
14
 * Comments:	   See "notice.txt" for copyright and license information.
15 16
 *
 */
H
Hiroshi Inoue 已提交
17
/* Multibyte support  Eiji Tokuya	2001-03-15	*/
18

B
Byron Nikolaidis 已提交
19
#ifdef HAVE_CONFIG_H
20
#include "config.h"
B
Byron Nikolaidis 已提交
21 22
#endif

23 24
#include <stdio.h>
#include <string.h>
25
#include <ctype.h>
B
Byron Nikolaidis 已提交
26

B
 
Byron Nikolaidis 已提交
27 28
#include "psqlodbc.h"

H
Hiroshi Inoue 已提交
29 30 31 32
#ifdef MULTIBYTE
#include "multibyte.h"
#endif

33
#ifndef WIN32
B
Byron Nikolaidis 已提交
34 35 36 37
#include "iodbc.h"
#include "isql.h"
#include "isqlext.h"
#else
38
#include <windows.h>
39
#include <sql.h>
40
#include <sqlext.h>
B
Byron Nikolaidis 已提交
41 42
#endif

43
#include <time.h>
44 45 46
#include <math.h>
#include "convert.h"
#include "statement.h"
B
Byron Nikolaidis 已提交
47
#include "qresult.h"
48 49
#include "bind.h"
#include "pgtypes.h"
50 51 52
#include "lobj.h"
#include "connection.h"

53 54
#ifndef WIN32
#ifndef HAVE_STRICMP
B
Byron Nikolaidis 已提交
55 56 57 58 59
#define stricmp(s1,s2) strcasecmp(s1,s2)
#define strnicmp(s1,s2,n) strncasecmp(s1,s2,n)
#endif
#ifndef SCHAR
typedef signed char SCHAR;
B
Bruce Momjian 已提交
60

B
Byron Nikolaidis 已提交
61 62 63
#endif
#endif

64 65
extern GLOBAL_VALUES globals;

66 67 68 69 70 71
/*	How to map ODBC scalar functions {fn func(args)} to Postgres
 *	This is just a simple substitution
 *	List augmented from
 *	 http://www.merant.com/datadirect/download/docs/odbc16/Odbcref/rappc.htm
 * - thomas 2000-04-03
 */
B
Bruce Momjian 已提交
72 73 74 75
char	   *mapFuncs[][2] = {
/*	{ "ASCII",		 "ascii"	  }, */
	{"CHAR", "chr"},
	{"CONCAT", "textcat"},
B
Bruce Momjian 已提交
76
/*	{ "DIFFERENCE",  "difference" }, */
B
Bruce Momjian 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*	{ "INSERT",		 "insert"	  }, */
	{"LCASE", "lower"},
	{"LEFT", "ltrunc"},
	{"LOCATE", "strpos"},
	{"LENGTH", "char_length"},
/*	{ "LTRIM",		 "ltrim"	  }, */
	{"RIGHT", "rtrunc"},
/*	{ "REPEAT",		 "repeat"	  }, */
/*	{ "REPLACE",	 "replace"	  }, */
/*	{ "RTRIM",		 "rtrim"	  }, */
/*	{ "SOUNDEX",	 "soundex"	  }, */
	{"SUBSTRING", "substr"},
	{"UCASE", "upper"},

/*	{ "ABS",		 "abs"		  }, */
/*	{ "ACOS",		 "acos"		  }, */
/*	{ "ASIN",		 "asin"		  }, */
/*	{ "ATAN",		 "atan"		  }, */
/*	{ "ATAN2",		 "atan2"	  }, */
	{"CEILING", "ceil"},
/*	{ "COS",		 "cos"		  }, */
/*	{ "COT",		 "cot"		  }, */
/*	{ "DEGREES",	 "degrees"	  }, */
/*	{ "EXP",		 "exp"		  }, */
/*	{ "FLOOR",		 "floor"	  }, */
	{"LOG", "ln"},
	{"LOG10", "log"},
/*	{ "MOD",		 "mod"		  }, */
/*	{ "PI",			 "pi"		  }, */
	{"POWER", "pow"},
/*	{ "RADIANS",	 "radians"	  }, */
	{"RAND", "random"},
/*	{ "ROUND",		 "round"	  }, */
/*	{ "SIGN",		 "sign"		  }, */
/*	{ "SIN",		 "sin"		  }, */
/*	{ "SQRT",		 "sqrt"		  }, */
/*	{ "TAN",		 "tan"		  }, */
	{"TRUNCATE", "trunc"},

/*	{ "CURDATE",	 "curdate"	  }, */
/*	{ "CURTIME",	 "curtime"	  }, */
/*	{ "DAYNAME",	 "dayname"	  }, */
B
Bruce Momjian 已提交
119
/*	{ "DAYOFMONTH",  "dayofmonth" }, */
B
Bruce Momjian 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/*	{ "DAYOFWEEK",	 "dayofweek"  }, */
/*	{ "DAYOFYEAR",	 "dayofyear"  }, */
/*	{ "HOUR",		 "hour"		  }, */
/*	{ "MINUTE",		 "minute"	  }, */
/*	{ "MONTH",		 "month"	  }, */
/*	{ "MONTHNAME",	 "monthname"  }, */
/*	{ "NOW",		 "now"		  }, */
/*	{ "QUARTER",	 "quarter"	  }, */
/*	{ "SECOND",		 "second"	  }, */
/*	{ "WEEK",		 "week"		  }, */
/*	{ "YEAR",		 "year"		  }, */

/*	{ "DATABASE",	 "database"   }, */
	{"IFNULL", "coalesce"},
	{"USER", "odbc_user"},
	{0, 0}
136 137
};

B
Bruce Momjian 已提交
138
char	   *mapFunction(char *func);
139 140
unsigned int conv_from_octal(unsigned char *s);
unsigned int conv_from_hex(unsigned char *s);
B
Bruce Momjian 已提交
141
char	   *conv_to_octal(unsigned char val);
142 143 144 145 146 147 148 149 150 151 152

/********		A Guide for date/time/timestamp conversions    **************

			field_type		fCType				Output
			----------		------				----------
			PG_TYPE_DATE	SQL_C_DEFAULT		SQL_C_DATE
			PG_TYPE_DATE	SQL_C_DATE			SQL_C_DATE
			PG_TYPE_DATE	SQL_C_TIMESTAMP		SQL_C_TIMESTAMP		(time = 0 (midnight))
			PG_TYPE_TIME	SQL_C_DEFAULT		SQL_C_TIME
			PG_TYPE_TIME	SQL_C_TIME			SQL_C_TIME
			PG_TYPE_TIME	SQL_C_TIMESTAMP		SQL_C_TIMESTAMP		(date = current date)
B
Bruce Momjian 已提交
153 154 155 156
			PG_TYPE_ABSTIME SQL_C_DEFAULT		SQL_C_TIMESTAMP
			PG_TYPE_ABSTIME SQL_C_DATE			SQL_C_DATE			(time is truncated)
			PG_TYPE_ABSTIME SQL_C_TIME			SQL_C_TIME			(date is truncated)
			PG_TYPE_ABSTIME SQL_C_TIMESTAMP		SQL_C_TIMESTAMP
157 158 159 160 161
******************************************************************************/


/*	This is called by SQLFetch() */
int
B
Bruce Momjian 已提交
162
copy_and_convert_field_bindinfo(StatementClass *stmt, Int4 field_type, void *value, int col)
163
{
B
Bruce Momjian 已提交
164
	BindInfoClass *bic = &(stmt->bindings[col]);
165

B
Bruce Momjian 已提交
166 167
	return copy_and_convert_field(stmt, field_type, value, (Int2) bic->returntype, (PTR) bic->buffer,
							 (SDWORD) bic->buflen, (SDWORD *) bic->used);
168 169 170 171
}

/*	This is called by SQLGetData() */
int
B
Bruce Momjian 已提交
172
copy_and_convert_field(StatementClass *stmt, Int4 field_type, void *value, Int2 fCType,
B
Bruce Momjian 已提交
173
					   PTR rgbValue, SDWORD cbValueMax, SDWORD *pcbValue)
174
{
B
Bruce Momjian 已提交
175 176
	Int4		len = 0,
				copy_len = 0;
177
	SIMPLE_TIME st;
B
Bruce Momjian 已提交
178 179 180 181 182 183 184 185 186 187
	time_t		t = time(NULL);
	struct tm  *tim;
	int			pcbValueOffset,
				rgbValueOffset;
	char	   *rgbValueBindRow,
			   *ptr;
	int			bind_row = stmt->bind_row;
	int			bind_size = stmt->options.bind_size;
	int			result = COPY_OK;
	char		tempBuf[TEXT_FIELD_SIZE + 5];
188 189 190 191

/*	rgbValueOffset is *ONLY* for character and binary data */
/*	pcbValueOffset is for computing any pcbValue location */

B
Bruce Momjian 已提交
192 193
	if (bind_size > 0)
	{
194

195
		pcbValueOffset = rgbValueOffset = (bind_size * bind_row);
196
	}
B
Bruce Momjian 已提交
197 198
	else
	{
199

200 201
		pcbValueOffset = bind_row * sizeof(SDWORD);
		rgbValueOffset = bind_row * cbValueMax;
202

203
	}
204 205 206

	memset(&st, 0, sizeof(SIMPLE_TIME));

B
Bruce Momjian 已提交
207
	/* Initialize current date */
208 209 210 211 212
	tim = localtime(&t);
	st.m = tim->tm_mon + 1;
	st.d = tim->tm_mday;
	st.y = tim->tm_year + 1900;

B
Bruce Momjian 已提交
213
	mylog("copy_and_convert: field_type = %d, fctype = %d, value = '%s', cbValueMax=%d\n", field_type, fCType, (value == NULL) ? "<NULL>" : value, cbValueMax);
214

B
Bruce Momjian 已提交
215 216 217 218 219
	if (!value)
	{
		/* handle a null just by returning SQL_NULL_DATA in pcbValue, */
		/* and doing nothing to the buffer.							  */
		if (pcbValue)
220
			*(SDWORD *) ((char *) pcbValue + pcbValueOffset) = SQL_NULL_DATA;
B
Byron Nikolaidis 已提交
221 222 223
		return COPY_OK;
	}

224

B
Bruce Momjian 已提交
225 226 227 228 229 230 231 232 233
	if (stmt->hdbc->DataSourceToDriver != NULL)
	{
		int			length = strlen(value);

		stmt->hdbc->DataSourceToDriver(stmt->hdbc->translation_option,
									   SQL_CHAR,
									   value, length,
									   value, length, NULL,
									   NULL, 0, NULL);
B
Byron Nikolaidis 已提交
234 235
	}

236

B
Byron Nikolaidis 已提交
237 238 239 240
	/********************************************************************
		First convert any specific postgres types into more
		useable data.

B
Bruce Momjian 已提交
241 242
		NOTE: Conversions from PG char/varchar of a date/time/timestamp
		value to SQL_C_DATE,SQL_C_TIME, SQL_C_TIMESTAMP not supported
B
Byron Nikolaidis 已提交
243
	*********************************************************************/
B
Bruce Momjian 已提交
244 245
	switch (field_type)
	{
246

B
Bruce Momjian 已提交
247 248 249 250 251 252 253
			/*
			 * $$$ need to add parsing for date/time/timestamp strings in
			 * PG_TYPE_CHAR,VARCHAR $$$
			 */
		case PG_TYPE_DATE:
			sscanf(value, "%4d-%2d-%2d", &st.y, &st.m, &st.d);
			break;
254

B
Bruce Momjian 已提交
255 256 257
		case PG_TYPE_TIME:
			sscanf(value, "%2d:%2d:%2d", &st.hh, &st.mm, &st.ss);
			break;
258

B
Bruce Momjian 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		case PG_TYPE_ABSTIME:
		case PG_TYPE_DATETIME:
		case PG_TYPE_TIMESTAMP:
			if (strnicmp(value, "invalid", 7) != 0)
			{
				sscanf(value, "%4d-%2d-%2d %2d:%2d:%2d", &st.y, &st.m, &st.d, &st.hh, &st.mm, &st.ss);

			}
			else
			{					/* The timestamp is invalid so set
								 * something conspicuous, like the epoch */
				t = 0;
				tim = localtime(&t);
				st.m = tim->tm_mon + 1;
				st.d = tim->tm_mday;
				st.y = tim->tm_year + 1900;
				st.hh = tim->tm_hour;
				st.mm = tim->tm_min;
				st.ss = tim->tm_sec;
			}
			break;

		case PG_TYPE_BOOL:
			{					/* change T/F to 1/0 */
				char	   *s = (char *) value;

				if (s[0] == 'T' || s[0] == 't')
					s[0] = '1';
				else
					s[0] = '0';
			}
			break;

			/* This is for internal use by SQLStatistics() */
		case PG_TYPE_INT2VECTOR:
			{
				int			nval,
							i;
				char	   *vp;

				/* this is an array of eight integers */
				short	   *short_array = (short *) ((char *) rgbValue + rgbValueOffset);

				len = 32;
				vp = value;
				nval = 0;
				mylog("index=(");
				for (i = 0; i < 16; i++)
				{
					if (sscanf(vp, "%hd", &short_array[i]) != 1)
						break;

					mylog(" %d", short_array[i]);
					nval++;

					/* skip the current token */
					while ((*vp != '\0') && (!isspace((unsigned char) *vp)))
						vp++;
					/* and skip the space to the next token */
					while ((*vp != '\0') && (isspace((unsigned char) *vp)))
						vp++;
					if (*vp == '\0')
						break;
				}
				mylog(") nval = %d\n", nval);

				for (i = nval; i < 16; i++)
					short_array[i] = 0;
B
Byron Nikolaidis 已提交
327

328
#if 0
B
Bruce Momjian 已提交
329 330 331 332 333 334 335 336 337
				sscanf(value, "%hd %hd %hd %hd %hd %hd %hd %hd",
					   &short_array[0],
					   &short_array[1],
					   &short_array[2],
					   &short_array[3],
					   &short_array[4],
					   &short_array[5],
					   &short_array[6],
					   &short_array[7]);
338
#endif
B
Byron Nikolaidis 已提交
339

B
Bruce Momjian 已提交
340 341 342
				/* There is no corresponding fCType for this. */
				if (pcbValue)
					*(SDWORD *) ((char *) pcbValue + pcbValueOffset) = len;
B
Byron Nikolaidis 已提交
343

B
Bruce Momjian 已提交
344 345 346
				return COPY_OK; /* dont go any further or the data will be
								 * trashed */
			}
B
Byron Nikolaidis 已提交
347

B
Bruce Momjian 已提交
348 349 350 351 352
			/*
			 * This is a large object OID, which is used to store
			 * LONGVARBINARY objects.
			 */
		case PG_TYPE_LO:
B
Byron Nikolaidis 已提交
353

B
Bruce Momjian 已提交
354
			return convert_lo(stmt, value, fCType, ((char *) rgbValue + rgbValueOffset), cbValueMax, (SDWORD *) ((char *) pcbValue + pcbValueOffset));
B
Byron Nikolaidis 已提交
355

B
Bruce Momjian 已提交
356
		default:
B
Byron Nikolaidis 已提交
357

B
Bruce Momjian 已提交
358 359 360
			if (field_type == stmt->hdbc->lobj_type)	/* hack until permanent
														 * type available */
				return convert_lo(stmt, value, fCType, ((char *) rgbValue + rgbValueOffset), cbValueMax, (SDWORD *) ((char *) pcbValue + pcbValueOffset));
B
Byron Nikolaidis 已提交
361 362
	}

B
Bruce Momjian 已提交
363 364 365
	/* Change default into something useable */
	if (fCType == SQL_C_DEFAULT)
	{
B
Byron Nikolaidis 已提交
366 367 368 369 370
		fCType = pgtype_to_ctype(stmt, field_type);

		mylog("copy_and_convert, SQL_C_DEFAULT: fCType = %d\n", fCType);
	}

371

372 373
	rgbValueBindRow = (char *) rgbValue + rgbValueOffset;

B
Bruce Momjian 已提交
374 375
	if (fCType == SQL_C_CHAR)
	{
376 377


B
Bruce Momjian 已提交
378
		/* Special character formatting as required */
379

B
Bruce Momjian 已提交
380 381 382 383 384 385 386 387 388 389 390
		/*
		 * These really should return error if cbValueMax is not big
		 * enough.
		 */
		switch (field_type)
		{
			case PG_TYPE_DATE:
				len = 10;
				if (cbValueMax > len)
					sprintf(rgbValueBindRow, "%.4d-%.2d-%.2d", st.y, st.m, st.d);
				break;
391

B
Bruce Momjian 已提交
392 393 394 395 396
			case PG_TYPE_TIME:
				len = 8;
				if (cbValueMax > len)
					sprintf(rgbValueBindRow, "%.2d:%.2d:%.2d", st.hh, st.mm, st.ss);
				break;
397

B
Bruce Momjian 已提交
398 399 400 401 402 403 404 405
			case PG_TYPE_ABSTIME:
			case PG_TYPE_DATETIME:
			case PG_TYPE_TIMESTAMP:
				len = 19;
				if (cbValueMax > len)
					sprintf(rgbValueBindRow, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
							st.y, st.m, st.d, st.hh, st.mm, st.ss);
				break;
406

B
Bruce Momjian 已提交
407 408 409 410 411 412 413 414
			case PG_TYPE_BOOL:
				len = 1;
				if (cbValueMax > len)
				{
					strcpy(rgbValueBindRow, value);
					mylog("PG_TYPE_BOOL: rgbValueBindRow = '%s'\n", rgbValueBindRow);
				}
				break;
415

B
Bruce Momjian 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
				/*
				 * Currently, data is SILENTLY TRUNCATED for BYTEA and
				 * character data types if there is not enough room in
				 * cbValueMax because the driver can't handle multiple
				 * calls to SQLGetData for these, yet.	Most likely, the
				 * buffer passed in will be big enough to handle the
				 * maximum limit of postgres, anyway.
				 *
				 * LongVarBinary types are handled correctly above, observing
				 * truncation and all that stuff since there is
				 * essentially no limit on the large object used to store
				 * those.
				 */
			case PG_TYPE_BYTEA:/* convert binary data to hex strings
								 * (i.e, 255 = "FF") */
				len = convert_pgbinary_to_char(value, rgbValueBindRow, cbValueMax);

				/***** THIS IS NOT PROPERLY IMPLEMENTED *****/
				break;
435

B
Bruce Momjian 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
			default:
				/* convert linefeeds to carriage-return/linefeed */
				len = convert_linefeeds(value, tempBuf, sizeof(tempBuf));
				ptr = tempBuf;

				mylog("DEFAULT: len = %d, ptr = '%s'\n", len, ptr);

				if (stmt->current_col >= 0)
				{
					if (stmt->bindings[stmt->current_col].data_left == 0)
						return COPY_NO_DATA_FOUND;
					else if (stmt->bindings[stmt->current_col].data_left > 0)
					{
						ptr += len - stmt->bindings[stmt->current_col].data_left;
						len = stmt->bindings[stmt->current_col].data_left;
					}
					else
						stmt->bindings[stmt->current_col].data_left = strlen(ptr);
454 455
				}

B
Bruce Momjian 已提交
456 457 458 459
				if (cbValueMax > 0)
				{

					copy_len = (len >= cbValueMax) ? cbValueMax - 1 : len;
460

B
Bruce Momjian 已提交
461 462
					/* Copy the data */
					strncpy_null(rgbValueBindRow, ptr, copy_len + 1);
463

B
Bruce Momjian 已提交
464 465 466
					/* Adjust data_left for next time */
					if (stmt->current_col >= 0)
						stmt->bindings[stmt->current_col].data_left -= copy_len;
467 468
				}

B
Bruce Momjian 已提交
469 470 471 472 473 474
				/*
				 * Finally, check for truncation so that proper status can
				 * be returned
				 */
				if (len >= cbValueMax)
					result = COPY_RESULT_TRUNCATED;
475 476


B
Bruce Momjian 已提交
477 478
				mylog("    SQL_C_CHAR, default: len = %d, cbValueMax = %d, rgbValueBindRow = '%s'\n", len, cbValueMax, rgbValueBindRow);
				break;
B
Byron Nikolaidis 已提交
479
		}
480 481


B
Bruce Momjian 已提交
482 483 484
	}
	else
	{
B
Bruce Momjian 已提交
485

B
Bruce Momjian 已提交
486 487 488 489 490
		/*
		 * for SQL_C_CHAR, it's probably ok to leave currency symbols in.
		 * But to convert to numeric types, it is necessary to get rid of
		 * those.
		 */
B
Byron Nikolaidis 已提交
491 492 493
		if (field_type == PG_TYPE_MONEY)
			convert_money(value);

B
Bruce Momjian 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
		switch (fCType)
		{
			case SQL_C_DATE:
				len = 6;
				{
					DATE_STRUCT *ds;

					if (bind_size > 0)
						ds = (DATE_STRUCT *) ((char *) rgbValue + (bind_row * bind_size));
					else
						ds = (DATE_STRUCT *) rgbValue + bind_row;
					ds->year = st.y;
					ds->month = st.m;
					ds->day = st.d;
				}
				break;
510

B
Bruce Momjian 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524
			case SQL_C_TIME:
				len = 6;
				{
					TIME_STRUCT *ts;

					if (bind_size > 0)
						ts = (TIME_STRUCT *) ((char *) rgbValue + (bind_row * bind_size));
					else
						ts = (TIME_STRUCT *) rgbValue + bind_row;
					ts->hour = st.hh;
					ts->minute = st.mm;
					ts->second = st.ss;
				}
				break;
B
Byron Nikolaidis 已提交
525

B
Bruce Momjian 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
			case SQL_C_TIMESTAMP:
				len = 16;
				{
					TIMESTAMP_STRUCT *ts;

					if (bind_size > 0)
						ts = (TIMESTAMP_STRUCT *) ((char *) rgbValue + (bind_row * bind_size));
					else
						ts = (TIMESTAMP_STRUCT *) rgbValue + bind_row;
					ts->year = st.y;
					ts->month = st.m;
					ts->day = st.d;
					ts->hour = st.hh;
					ts->minute = st.mm;
					ts->second = st.ss;
					ts->fraction = 0;
				}
				break;
544

B
Bruce Momjian 已提交
545 546 547 548 549 550
			case SQL_C_BIT:
				len = 1;
				if (bind_size > 0)
					*(UCHAR *) ((char *) rgbValue + (bind_row * bind_size)) = atoi(value);
				else
					*((UCHAR *) rgbValue + bind_row) = atoi(value);
551

B
Bruce Momjian 已提交
552 553 554 555 556
				/*
				 * mylog("SQL_C_BIT: val = %d, cb = %d, rgb=%d\n",
				 * atoi(value), cbValueMax, *((UCHAR *)rgbValue));
				 */
				break;
557

B
Bruce Momjian 已提交
558 559 560 561 562 563 564 565
			case SQL_C_STINYINT:
			case SQL_C_TINYINT:
				len = 1;
				if (bind_size > 0)
					*(SCHAR *) ((char *) rgbValue + (bind_row * bind_size)) = atoi(value);
				else
					*((SCHAR *) rgbValue + bind_row) = atoi(value);
				break;
566

B
Bruce Momjian 已提交
567 568 569 570 571 572 573
			case SQL_C_UTINYINT:
				len = 1;
				if (bind_size > 0)
					*(UCHAR *) ((char *) rgbValue + (bind_row * bind_size)) = atoi(value);
				else
					*((UCHAR *) rgbValue + bind_row) = atoi(value);
				break;
574

B
Bruce Momjian 已提交
575 576 577 578 579 580 581
			case SQL_C_FLOAT:
				len = 4;
				if (bind_size > 0)
					*(SFLOAT *) ((char *) rgbValue + (bind_row * bind_size)) = (float) atof(value);
				else
					*((SFLOAT *) rgbValue + bind_row) = (float) atof(value);
				break;
582

B
Bruce Momjian 已提交
583 584 585 586 587 588 589
			case SQL_C_DOUBLE:
				len = 8;
				if (bind_size > 0)
					*(SDOUBLE *) ((char *) rgbValue + (bind_row * bind_size)) = atof(value);
				else
					*((SDOUBLE *) rgbValue + bind_row) = atof(value);
				break;
590

B
Bruce Momjian 已提交
591 592 593 594 595 596 597 598
			case SQL_C_SSHORT:
			case SQL_C_SHORT:
				len = 2;
				if (bind_size > 0)
					*(SWORD *) ((char *) rgbValue + (bind_row * bind_size)) = atoi(value);
				else
					*((SWORD *) rgbValue + bind_row) = atoi(value);
				break;
599

B
Bruce Momjian 已提交
600 601 602 603 604 605 606
			case SQL_C_USHORT:
				len = 2;
				if (bind_size > 0)
					*(UWORD *) ((char *) rgbValue + (bind_row * bind_size)) = atoi(value);
				else
					*((UWORD *) rgbValue + bind_row) = atoi(value);
				break;
607

B
Bruce Momjian 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
			case SQL_C_SLONG:
			case SQL_C_LONG:
				len = 4;
				if (bind_size > 0)
					*(SDWORD *) ((char *) rgbValue + (bind_row * bind_size)) = atol(value);
				else
					*((SDWORD *) rgbValue + bind_row) = atol(value);
				break;

			case SQL_C_ULONG:
				len = 4;
				if (bind_size > 0)
					*(UDWORD *) ((char *) rgbValue + (bind_row * bind_size)) = atol(value);
				else
					*((UDWORD *) rgbValue + bind_row) = atol(value);
				break;
624

B
Bruce Momjian 已提交
625
			case SQL_C_BINARY:
626

B
Bruce Momjian 已提交
627 628
				/* truncate if necessary */
				/* convert octal escapes to bytes */
629

B
Bruce Momjian 已提交
630 631
				len = convert_from_pgbinary(value, tempBuf, sizeof(tempBuf));
				ptr = tempBuf;
632

B
Bruce Momjian 已提交
633 634
				if (stmt->current_col >= 0)
				{
635

B
Bruce Momjian 已提交
636 637 638
					/* No more data left for this column */
					if (stmt->bindings[stmt->current_col].data_left == 0)
						return COPY_NO_DATA_FOUND;
639

B
Bruce Momjian 已提交
640 641 642 643 644 645 646 647 648
					/*
					 * Second (or more) call to SQLGetData so move the
					 * pointer
					 */
					else if (stmt->bindings[stmt->current_col].data_left > 0)
					{
						ptr += len - stmt->bindings[stmt->current_col].data_left;
						len = stmt->bindings[stmt->current_col].data_left;
					}
649

B
Bruce Momjian 已提交
650 651 652
					/* First call to SQLGetData so initialize data_left */
					else
						stmt->bindings[stmt->current_col].data_left = len;
653

B
Bruce Momjian 已提交
654
				}
655

B
Bruce Momjian 已提交
656 657 658
				if (cbValueMax > 0)
				{
					copy_len = (len > cbValueMax) ? cbValueMax : len;
659

B
Bruce Momjian 已提交
660 661
					/* Copy the data */
					memcpy(rgbValueBindRow, ptr, copy_len);
662

B
Bruce Momjian 已提交
663 664 665
					/* Adjust data_left for next time */
					if (stmt->current_col >= 0)
						stmt->bindings[stmt->current_col].data_left -= copy_len;
666
				}
667

B
Bruce Momjian 已提交
668 669 670 671 672 673
				/*
				 * Finally, check for truncation so that proper status can
				 * be returned
				 */
				if (len > cbValueMax)
					result = COPY_RESULT_TRUNCATED;
674

B
Bruce Momjian 已提交
675 676 677 678 679
				mylog("SQL_C_BINARY: len = %d, copy_len = %d\n", len, copy_len);
				break;

			default:
				return COPY_UNSUPPORTED_TYPE;
B
Byron Nikolaidis 已提交
680 681
		}
	}
682

B
Bruce Momjian 已提交
683 684 685
	/* store the length of what was copied, if there's a place for it */
	if (pcbValue)
		*(SDWORD *) ((char *) pcbValue + pcbValueOffset) = len;
686

687
	return result;
688

689 690
}

691

692 693 694 695 696
/*	This function inserts parameters into an SQL statements.
	It will also modify a SELECT statement for use with declare/fetch cursors.
	This function no longer does any dynamic memory allocation!
*/
int
B
Bruce Momjian 已提交
697
copy_statement_with_parameters(StatementClass *stmt)
698
{
B
Bruce Momjian 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	static char *func = "copy_statement_with_parameters";
	unsigned int opos,
				npos,
				oldstmtlen;
	char		param_string[128],
				tmp[256],
				cbuf[TEXT_FIELD_SIZE + 5];
	int			param_number;
	Int2		param_ctype,
				param_sqltype;
	char	   *old_statement = stmt->statement;
	char	   *new_statement = stmt->stmt_with_params;
	SIMPLE_TIME st;
	time_t		t = time(NULL);
	struct tm  *tim;
	SDWORD		used;
	char	   *buffer,
			   *buf;
	char		in_quote = FALSE;
	Oid			lobj_oid;
	int			lobj_fd,
				retval;


	if (!old_statement)
	{
B
Byron Nikolaidis 已提交
725
		SC_log_error(func, "No statement string", stmt);
726
		return SQL_ERROR;
B
Byron Nikolaidis 已提交
727
	}
728

729 730
	memset(&st, 0, sizeof(SIMPLE_TIME));

B
Bruce Momjian 已提交
731
	/* Initialize current date */
732 733 734 735 736
	tim = localtime(&t);
	st.m = tim->tm_mon + 1;
	st.d = tim->tm_mday;
	st.y = tim->tm_year + 1900;

B
Bruce Momjian 已提交
737 738
	/* If the application hasn't set a cursor name, then generate one */
	if (stmt->cursor_name[0] == '\0')
739
		sprintf(stmt->cursor_name, "SQL_CUR%p", stmt);
740

B
Bruce Momjian 已提交
741 742 743
	/* For selects, prepend a declare cursor to the statement */
	if (stmt->statement_type == STMT_TYPE_SELECT && globals.use_declarefetch)
	{
744
		sprintf(new_statement, "declare %s cursor for ", stmt->cursor_name);
745 746
		npos = strlen(new_statement);
	}
B
Bruce Momjian 已提交
747 748
	else
	{
749 750 751 752
		new_statement[0] = '0';
		npos = 0;
	}

B
Bruce Momjian 已提交
753
	param_number = -1;
754

755
	oldstmtlen = strlen(old_statement);
H
Hiroshi Inoue 已提交
756
#ifdef MULTIBYTE
B
Bruce Momjian 已提交
757
	multibyte_init();
H
Hiroshi Inoue 已提交
758
#endif
759

B
Bruce Momjian 已提交
760 761
	for (opos = 0; opos < oldstmtlen; opos++)
	{
762

B
Bruce Momjian 已提交
763 764 765
		/* Squeeze carriage-return/linefeed pairs to linefeed only */
		if (old_statement[opos] == '\r' && opos + 1 < oldstmtlen &&
			old_statement[opos + 1] == '\n')
766 767
			continue;

B
Bruce Momjian 已提交
768 769 770 771
		/*
		 * Handle literals (date, time, timestamp) and ODBC scalar
		 * functions
		 */
H
Hiroshi Inoue 已提交
772
#ifdef MULTIBYTE
B
Bruce Momjian 已提交
773 774
		else if (multibyte_char_check(old_statement[opos]) == 0 && old_statement[opos] == '{')
		{
H
Hiroshi Inoue 已提交
775
#else
B
Bruce Momjian 已提交
776 777
		else if (old_statement[opos] == '{')
		{
H
Hiroshi Inoue 已提交
778
#endif
B
Bruce Momjian 已提交
779 780 781
			char	   *esc;
			char	   *begin = &old_statement[opos + 1];

H
Hiroshi Inoue 已提交
782
#ifdef MULTIBYTE
B
Bruce Momjian 已提交
783 784
			char	   *end = multibyte_strchr(begin, '}');

H
Hiroshi Inoue 已提交
785
#else
B
Bruce Momjian 已提交
786 787
			char	   *end = strchr(begin, '}');

H
Hiroshi Inoue 已提交
788
#endif
789

B
Bruce Momjian 已提交
790
			if (!end)
791 792 793 794 795
				continue;

			*end = '\0';

			esc = convert_escape(begin);
B
Bruce Momjian 已提交
796 797
			if (esc)
			{
798 799 800
				memcpy(&new_statement[npos], esc, strlen(esc));
				npos += strlen(esc);
			}
B
Bruce Momjian 已提交
801 802 803
			else
			{					/* it's not a valid literal so just copy */
				*end = '}';
804 805 806
				new_statement[npos++] = old_statement[opos];
				continue;
			}
807

808
			opos += end - begin + 1;
809 810 811 812 813 814

			*end = '}';

			continue;
		}

B
Bruce Momjian 已提交
815 816 817 818 819
		/*
		 * Can you have parameter markers inside of quotes?  I dont think
		 * so. All the queries I've seen expect the driver to put quotes
		 * if needed.
		 */
820
		else if (old_statement[opos] == '?' && !in_quote)
B
Bruce Momjian 已提交
821 822 823
			;					/* ok */
		else
		{
824 825 826
			if (old_statement[opos] == '\'')
				in_quote = (in_quote ? FALSE : TRUE);

827 828 829 830
			new_statement[npos++] = old_statement[opos];
			continue;
		}

831 832


833
		/****************************************************/
B
Bruce Momjian 已提交
834
		/* Its a '?' parameter alright				  */
835 836 837 838
		/****************************************************/

		param_number++;

B
Bruce Momjian 已提交
839
		if (param_number >= stmt->parameters_allocated)
840
			break;
841

B
Bruce Momjian 已提交
842 843 844
		/* Assign correct buffers based on data at exec param or not */
		if (stmt->parameters[param_number].data_at_exec)
		{
845 846 847
			used = stmt->parameters[param_number].EXEC_used ? *stmt->parameters[param_number].EXEC_used : SQL_NTS;
			buffer = stmt->parameters[param_number].EXEC_buffer;
		}
B
Bruce Momjian 已提交
848 849
		else
		{
850 851 852 853
			used = stmt->parameters[param_number].used ? *stmt->parameters[param_number].used : SQL_NTS;
			buffer = stmt->parameters[param_number].buffer;
		}

B
Bruce Momjian 已提交
854 855 856
		/* Handle NULL parameter data */
		if (used == SQL_NULL_DATA)
		{
857 858 859 860 861
			strcpy(&new_statement[npos], "NULL");
			npos += 4;
			continue;
		}

B
Bruce Momjian 已提交
862 863 864 865 866 867
		/*
		 * If no buffer, and it's not null, then what the hell is it? Just
		 * leave it alone then.
		 */
		if (!buffer)
		{
868 869
			new_statement[npos++] = '?';
			continue;
870
		}
871 872 873

		param_ctype = stmt->parameters[param_number].CType;
		param_sqltype = stmt->parameters[param_number].SQLType;
B
Bruce Momjian 已提交
874

875
		mylog("copy_statement_with_params: from(fcType)=%d, to(fSqlType)=%d\n", param_ctype, param_sqltype);
B
Bruce Momjian 已提交
876

B
Bruce Momjian 已提交
877
		/* replace DEFAULT with something we can use */
B
Bruce Momjian 已提交
878
		if (param_ctype == SQL_C_DEFAULT)
879 880 881 882 883 884 885
			param_ctype = sqltype_to_default_ctype(param_sqltype);

		buf = NULL;
		param_string[0] = '\0';
		cbuf[0] = '\0';


B
Bruce Momjian 已提交
886 887 888 889 890 891 892
		/* Convert input C type to a neutral format */
		switch (param_ctype)
		{
			case SQL_C_BINARY:
			case SQL_C_CHAR:
				buf = buffer;
				break;
893

B
Bruce Momjian 已提交
894 895 896 897
			case SQL_C_DOUBLE:
				sprintf(param_string, "%f",
						*((SDOUBLE *) buffer));
				break;
898

B
Bruce Momjian 已提交
899 900 901 902
			case SQL_C_FLOAT:
				sprintf(param_string, "%f",
						*((SFLOAT *) buffer));
				break;
903

B
Bruce Momjian 已提交
904 905 906 907 908
			case SQL_C_SLONG:
			case SQL_C_LONG:
				sprintf(param_string, "%ld",
						*((SDWORD *) buffer));
				break;
909

B
Bruce Momjian 已提交
910 911 912 913 914
			case SQL_C_SSHORT:
			case SQL_C_SHORT:
				sprintf(param_string, "%d",
						*((SWORD *) buffer));
				break;
915

B
Bruce Momjian 已提交
916 917 918 919 920
			case SQL_C_STINYINT:
			case SQL_C_TINYINT:
				sprintf(param_string, "%d",
						*((SCHAR *) buffer));
				break;
921

B
Bruce Momjian 已提交
922 923 924 925
			case SQL_C_ULONG:
				sprintf(param_string, "%lu",
						*((UDWORD *) buffer));
				break;
926

B
Bruce Momjian 已提交
927 928 929 930
			case SQL_C_USHORT:
				sprintf(param_string, "%u",
						*((UWORD *) buffer));
				break;
931

B
Bruce Momjian 已提交
932 933 934 935
			case SQL_C_UTINYINT:
				sprintf(param_string, "%u",
						*((UCHAR *) buffer));
				break;
936

B
Bruce Momjian 已提交
937 938 939
			case SQL_C_BIT:
				{
					int			i = *((UCHAR *) buffer);
940

B
Bruce Momjian 已提交
941 942 943
					sprintf(param_string, "%d", i ? 1 : 0);
					break;
				}
944

B
Bruce Momjian 已提交
945 946 947
			case SQL_C_DATE:
				{
					DATE_STRUCT *ds = (DATE_STRUCT *) buffer;
948

B
Bruce Momjian 已提交
949 950 951
					st.m = ds->month;
					st.d = ds->day;
					st.y = ds->year;
952

B
Bruce Momjian 已提交
953 954
					break;
				}
955

B
Bruce Momjian 已提交
956 957 958
			case SQL_C_TIME:
				{
					TIME_STRUCT *ts = (TIME_STRUCT *) buffer;
959

B
Bruce Momjian 已提交
960 961 962
					st.hh = ts->hour;
					st.mm = ts->minute;
					st.ss = ts->second;
963

B
Bruce Momjian 已提交
964 965
					break;
				}
966

B
Bruce Momjian 已提交
967 968 969
			case SQL_C_TIMESTAMP:
				{
					TIMESTAMP_STRUCT *tss = (TIMESTAMP_STRUCT *) buffer;
970

B
Bruce Momjian 已提交
971 972 973 974 975 976
					st.m = tss->month;
					st.d = tss->day;
					st.y = tss->year;
					st.hh = tss->hour;
					st.mm = tss->minute;
					st.ss = tss->second;
977

B
Bruce Momjian 已提交
978
					mylog("m=%d,d=%d,y=%d,hh=%d,mm=%d,ss=%d\n", st.m, st.d, st.y, st.hh, st.mm, st.ss);
979

B
Bruce Momjian 已提交
980
					break;
981

B
Bruce Momjian 已提交
982 983 984 985 986 987 988 989 990
				}
			default:
				/* error */
				stmt->errormsg = "Unrecognized C_parameter type in copy_statement_with_parameters";
				stmt->errornumber = STMT_NOT_IMPLEMENTED_ERROR;
				new_statement[npos] = '\0';		/* just in case */
				SC_log_error(func, "", stmt);
				return SQL_ERROR;
		}
991

B
Bruce Momjian 已提交
992 993 994 995
		/*
		 * Now that the input data is in a neutral format, convert it to
		 * the desired output format (sqltype)
		 */
996

B
Bruce Momjian 已提交
997 998 999 1000 1001
		switch (param_sqltype)
		{
			case SQL_CHAR:
			case SQL_VARCHAR:
			case SQL_LONGVARCHAR:
1002

B
Bruce Momjian 已提交
1003
				new_statement[npos++] = '\'';	/* Open Quote */
1004

B
Bruce Momjian 已提交
1005 1006 1007 1008 1009 1010
				/* it was a SQL_C_CHAR */
				if (buf)
				{
					convert_special_chars(buf, &new_statement[npos], used);
					npos += strlen(&new_statement[npos]);
				}
1011

B
Bruce Momjian 已提交
1012 1013 1014 1015 1016 1017
				/* it was a numeric type */
				else if (param_string[0] != '\0')
				{
					strcpy(&new_statement[npos], param_string);
					npos += strlen(param_string);
				}
1018

B
Bruce Momjian 已提交
1019 1020 1021 1022 1023
				/* it was date,time,timestamp -- use m,d,y,hh,mm,ss */
				else
				{
					sprintf(tmp, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
							st.y, st.m, st.d, st.hh, st.mm, st.ss);
1024

B
Bruce Momjian 已提交
1025 1026 1027
					strcpy(&new_statement[npos], tmp);
					npos += strlen(tmp);
				}
1028

B
Bruce Momjian 已提交
1029
				new_statement[npos++] = '\'';	/* Close Quote */
1030

B
Bruce Momjian 已提交
1031
				break;
1032

B
Bruce Momjian 已提交
1033 1034 1035 1036 1037 1038
			case SQL_DATE:
				if (buf)
				{				/* copy char data to time */
					my_strcpy(cbuf, sizeof(cbuf), buf, used);
					parse_datetime(cbuf, &st);
				}
1039

B
Bruce Momjian 已提交
1040
				sprintf(tmp, "'%.4d-%.2d-%.2d'", st.y, st.m, st.d);
1041

B
Bruce Momjian 已提交
1042 1043 1044
				strcpy(&new_statement[npos], tmp);
				npos += strlen(tmp);
				break;
1045

B
Bruce Momjian 已提交
1046 1047 1048 1049 1050 1051
			case SQL_TIME:
				if (buf)
				{				/* copy char data to time */
					my_strcpy(cbuf, sizeof(cbuf), buf, used);
					parse_datetime(cbuf, &st);
				}
1052

B
Bruce Momjian 已提交
1053
				sprintf(tmp, "'%.2d:%.2d:%.2d'", st.hh, st.mm, st.ss);
1054

B
Bruce Momjian 已提交
1055 1056 1057
				strcpy(&new_statement[npos], tmp);
				npos += strlen(tmp);
				break;
1058

B
Bruce Momjian 已提交
1059
			case SQL_TIMESTAMP:
1060

B
Bruce Momjian 已提交
1061 1062 1063 1064 1065
				if (buf)
				{
					my_strcpy(cbuf, sizeof(cbuf), buf, used);
					parse_datetime(cbuf, &st);
				}
1066

B
Bruce Momjian 已提交
1067 1068
				sprintf(tmp, "'%.4d-%.2d-%.2d %.2d:%.2d:%.2d'",
						st.y, st.m, st.d, st.hh, st.mm, st.ss);
1069

B
Bruce Momjian 已提交
1070 1071
				strcpy(&new_statement[npos], tmp);
				npos += strlen(tmp);
1072

B
Bruce Momjian 已提交
1073
				break;
1074

B
Bruce Momjian 已提交
1075 1076 1077 1078
			case SQL_BINARY:
			case SQL_VARBINARY:/* non-ascii characters should be
								 * converted to octal */
				new_statement[npos++] = '\'';	/* Open Quote */
1079

B
Bruce Momjian 已提交
1080
				mylog("SQL_VARBINARY: about to call convert_to_pgbinary, used = %d\n", used);
1081

B
Bruce Momjian 已提交
1082
				npos += convert_to_pgbinary(buf, &new_statement[npos], used);
1083

B
Bruce Momjian 已提交
1084
				new_statement[npos++] = '\'';	/* Close Quote */
B
Byron Nikolaidis 已提交
1085

B
Bruce Momjian 已提交
1086
				break;
1087

B
Bruce Momjian 已提交
1088 1089 1090 1091
			case SQL_LONGVARBINARY:

				if (stmt->parameters[param_number].data_at_exec)
				{
1092

B
Bruce Momjian 已提交
1093
					lobj_oid = stmt->parameters[param_number].lobj_oid;
1094

B
Bruce Momjian 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
				}
				else
				{

					/* begin transaction if needed */
					if (!CC_is_in_trans(stmt->hdbc))
					{
						QResultClass *res;
						char		ok;

						res = CC_send_query(stmt->hdbc, "BEGIN", NULL);
						if (!res)
						{
							stmt->errormsg = "Could not begin (in-line) a transaction";
							stmt->errornumber = STMT_EXEC_ERROR;
							SC_log_error(func, "", stmt);
							return SQL_ERROR;
						}
						ok = QR_command_successful(res);
						QR_Destructor(res);
						if (!ok)
						{
							stmt->errormsg = "Could not begin (in-line) a transaction";
							stmt->errornumber = STMT_EXEC_ERROR;
							SC_log_error(func, "", stmt);
							return SQL_ERROR;
						}
1122

B
Bruce Momjian 已提交
1123 1124
						CC_set_in_trans(stmt->hdbc);
					}
B
Byron Nikolaidis 已提交
1125

B
Bruce Momjian 已提交
1126 1127 1128 1129
					/* store the oid */
					lobj_oid = lo_creat(stmt->hdbc, INV_READ | INV_WRITE);
					if (lobj_oid == 0)
					{
B
Byron Nikolaidis 已提交
1130
						stmt->errornumber = STMT_EXEC_ERROR;
B
Bruce Momjian 已提交
1131
						stmt->errormsg = "Couldnt create (in-line) large object.";
B
Byron Nikolaidis 已提交
1132 1133 1134
						SC_log_error(func, "", stmt);
						return SQL_ERROR;
					}
B
Bruce Momjian 已提交
1135 1136 1137 1138 1139

					/* store the fd */
					lobj_fd = lo_open(stmt->hdbc, lobj_oid, INV_WRITE);
					if (lobj_fd < 0)
					{
B
Byron Nikolaidis 已提交
1140
						stmt->errornumber = STMT_EXEC_ERROR;
B
Bruce Momjian 已提交
1141
						stmt->errormsg = "Couldnt open (in-line) large object for writing.";
B
Byron Nikolaidis 已提交
1142 1143 1144 1145
						SC_log_error(func, "", stmt);
						return SQL_ERROR;
					}

B
Bruce Momjian 已提交
1146
					retval = lo_write(stmt->hdbc, lobj_fd, buffer, used);
1147

B
Bruce Momjian 已提交
1148
					lo_close(stmt->hdbc, lobj_fd);
1149

B
Bruce Momjian 已提交
1150 1151 1152 1153 1154
					/* commit transaction if needed */
					if (!globals.use_declarefetch && CC_is_in_autocommit(stmt->hdbc))
					{
						QResultClass *res;
						char		ok;
1155

B
Bruce Momjian 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
						res = CC_send_query(stmt->hdbc, "COMMIT", NULL);
						if (!res)
						{
							stmt->errormsg = "Could not commit (in-line) a transaction";
							stmt->errornumber = STMT_EXEC_ERROR;
							SC_log_error(func, "", stmt);
							return SQL_ERROR;
						}
						ok = QR_command_successful(res);
						QR_Destructor(res);
						if (!ok)
						{
							stmt->errormsg = "Could not commit (in-line) a transaction";
							stmt->errornumber = STMT_EXEC_ERROR;
							SC_log_error(func, "", stmt);
							return SQL_ERROR;
						}
1173

B
Bruce Momjian 已提交
1174 1175 1176
						CC_set_no_trans(stmt->hdbc);
					}
				}
1177

B
Bruce Momjian 已提交
1178 1179 1180 1181 1182 1183
				/*
				 * the oid of the large object -- just put that in for the
				 * parameter marker -- the data has already been sent to
				 * the large object
				 */
				sprintf(param_string, "'%d'", lobj_oid);
1184 1185 1186
				strcpy(&new_statement[npos], param_string);
				npos += strlen(param_string);

B
Bruce Momjian 已提交
1187 1188 1189 1190 1191 1192 1193
				break;

				/*
				 * because of no conversion operator for bool and int4,
				 * SQL_BIT
				 */
				/* must be quoted (0 or 1 is ok to use inside the quotes) */
1194

B
Bruce Momjian 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			case SQL_REAL:
				if (buf)
					my_strcpy(param_string, sizeof(param_string), buf, used);
				sprintf(tmp, "'%s'::float4", param_string);
				strcpy(&new_statement[npos], tmp);
				npos += strlen(tmp);
				break;
			case SQL_FLOAT:
			case SQL_DOUBLE:
				if (buf)
					my_strcpy(param_string, sizeof(param_string), buf, used);
				sprintf(tmp, "'%s'::float8", param_string);
				strcpy(&new_statement[npos], tmp);
				npos += strlen(tmp);
				break;
			case SQL_NUMERIC:
				if (buf)
				{
					cbuf[0] = '\'';
					my_strcpy(cbuf + 1, sizeof(cbuf) - 12, buf, used);	/* 12 = 1('\'') +
																		 * strlen("'::numeric")
																		 * + 1('\0') */
					strcat(cbuf, "'::numeric");
				}
				else
					sprintf(cbuf, "'%s'::numeric", param_string);
				my_strcpy(&new_statement[npos], sizeof(stmt->stmt_with_params) - npos - 1, cbuf, strlen(cbuf));
				npos += strlen(&new_statement[npos]);
				break;
			default:			/* a numeric type or SQL_BIT */
				if (param_sqltype == SQL_BIT)
					new_statement[npos++] = '\'';		/* Open Quote */

				if (buf)
				{
					my_strcpy(&new_statement[npos], sizeof(stmt->stmt_with_params) - npos, buf, used);
					npos += strlen(&new_statement[npos]);
				}
				else
				{
					strcpy(&new_statement[npos], param_string);
					npos += strlen(param_string);
				}

				if (param_sqltype == SQL_BIT)
					new_statement[npos++] = '\'';		/* Close Quote */

				break;
1243

1244
		}
1245

B
Bruce Momjian 已提交
1246
	}							/* end, for */
1247

B
Bruce Momjian 已提交
1248
	/* make sure new_statement is always null-terminated */
1249 1250
	new_statement[npos] = '\0';

1251

B
Bruce Momjian 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260
	if (stmt->hdbc->DriverToDataSource != NULL)
	{
		int			length = strlen(new_statement);

		stmt->hdbc->DriverToDataSource(stmt->hdbc->translation_option,
									   SQL_CHAR,
									   new_statement, length,
									   new_statement, length, NULL,
									   NULL, 0, NULL);
B
Byron Nikolaidis 已提交
1261 1262
	}

1263

1264 1265 1266
	return SQL_SUCCESS;
}

1267 1268 1269
char *
mapFunction(char *func)
{
B
Bruce Momjian 已提交
1270
	int			i;
1271 1272

	for (i = 0; mapFuncs[i][0]; i++)
B
Bruce Momjian 已提交
1273
		if (!stricmp(mapFuncs[i][0], func))
1274 1275 1276 1277
			return mapFuncs[i][1];

	return NULL;
}
1278

1279 1280 1281
/* convert_escape()
 * This function returns a pointer to static memory!
 */
1282 1283 1284
char *
convert_escape(char *value)
{
B
Bruce Momjian 已提交
1285 1286
	static char escape[1024];
	char		key[33];
1287

1288
	/* Separate off the key, skipping leading and trailing whitespace */
B
Bruce Momjian 已提交
1289 1290
	while ((*value != '\0') && isspace((unsigned char) *value))
		value++;
1291
	sscanf(value, "%32s", key);
B
Bruce Momjian 已提交
1292 1293 1294 1295
	while ((*value != '\0') && (!isspace((unsigned char) *value)))
		value++;
	while ((*value != '\0') && isspace((unsigned char) *value))
		value++;
1296

1297
	mylog("convert_escape: key='%s', val='%s'\n", key, value);
1298

B
Bruce Momjian 已提交
1299 1300 1301 1302
	if ((strcmp(key, "d") == 0) ||
		(strcmp(key, "t") == 0) ||
		(strcmp(key, "ts") == 0))
	{
1303
		/* Literal; return the escape part as-is */
B
Bruce Momjian 已提交
1304
		strncpy(escape, value, sizeof(escape) - 1);
1305
	}
B
Bruce Momjian 已提交
1306 1307 1308 1309 1310 1311
	else if (strcmp(key, "fn") == 0)
	{

		/*
		 * Function invocation Separate off the func name, skipping
		 * trailing whitespace.
1312
		 */
B
Bruce Momjian 已提交
1313 1314 1315
		char	   *funcEnd = value;
		char		svchar;
		char	   *mapFunc;
1316 1317

		while ((*funcEnd != '\0') && (*funcEnd != '(') &&
B
Bruce Momjian 已提交
1318
			   (!isspace((unsigned char) *funcEnd)))
1319
			funcEnd++;
1320 1321 1322 1323
		svchar = *funcEnd;
		*funcEnd = '\0';
		sscanf(value, "%32s", key);
		*funcEnd = svchar;
1324 1325
		while ((*funcEnd != '\0') && isspace((unsigned char) *funcEnd))
			funcEnd++;
1326

B
Bruce Momjian 已提交
1327 1328 1329
		/*
		 * We expect left parenthesis here, else return fn body as-is
		 * since it is one of those "function constants".
1330
		 */
B
Bruce Momjian 已提交
1331 1332 1333
		if (*funcEnd != '(')
		{
			strncpy(escape, value, sizeof(escape) - 1);
1334
			return escape;
1335
		}
1336
		mapFunc = mapFunction(key);
B
Bruce Momjian 已提交
1337 1338 1339 1340

		/*
		 * We could have mapFunction() return key if not in table... -
		 * thomas 2000-04-03
1341
		 */
B
Bruce Momjian 已提交
1342 1343
		if (mapFunc == NULL)
		{
1344
			/* If unrecognized function name, return fn body as-is */
B
Bruce Momjian 已提交
1345
			strncpy(escape, value, sizeof(escape) - 1);
1346 1347 1348 1349
			return escape;
		}
		/* copy mapped name and remaining input string */
		strcpy(escape, mapFunc);
B
Bruce Momjian 已提交
1350
		strncat(escape, funcEnd, sizeof(escape) - 1 - strlen(mapFunc));
1351
	}
B
Bruce Momjian 已提交
1352 1353
	else
	{
1354
		/* Bogus key, leave untranslated */
1355 1356 1357 1358
		return NULL;
	}

	return escape;
1359

1360 1361 1362 1363 1364 1365
}


char *
convert_money(char *s)
{
B
Bruce Momjian 已提交
1366 1367
	size_t		i = 0,
				out = 0;
1368

B
Bruce Momjian 已提交
1369 1370
	for (i = 0; i < strlen(s); i++)
	{
1371
		if (s[i] == '$' || s[i] == ',' || s[i] == ')')
B
Bruce Momjian 已提交
1372
			;					/* skip these characters */
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
		else if (s[i] == '(')
			s[out++] = '-';
		else
			s[out++] = s[i];
	}
	s[out] = '\0';
	return s;
}



/*	This function parses a character string for date/time info and fills in SIMPLE_TIME */
/*	It does not zero out SIMPLE_TIME in case it is desired to initialize it with a value */
char
B
Bruce Momjian 已提交
1387
parse_datetime(char *buf, SIMPLE_TIME *st)
1388
{
B
Bruce Momjian 已提交
1389 1390 1391 1392 1393 1394 1395 1396
	int			y,
				m,
				d,
				hh,
				mm,
				ss;
	int			nf;

1397 1398
	y = m = d = hh = mm = ss = 0;

B
Bruce Momjian 已提交
1399 1400
	if (buf[4] == '-')			/* year first */
		nf = sscanf(buf, "%4d-%2d-%2d %2d:%2d:%2d", &y, &m, &d, &hh, &mm, &ss);
1401
	else
B
Bruce Momjian 已提交
1402
		nf = sscanf(buf, "%2d-%2d-%4d %2d:%2d:%2d", &m, &d, &y, &hh, &mm, &ss);
1403

B
Bruce Momjian 已提交
1404 1405
	if (nf == 5 || nf == 6)
	{
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
		st->y = y;
		st->m = m;
		st->d = d;
		st->hh = hh;
		st->mm = mm;
		st->ss = ss;

		return TRUE;
	}

B
Bruce Momjian 已提交
1416
	if (buf[4] == '-')			/* year first */
1417 1418 1419 1420
		nf = sscanf(buf, "%4d-%2d-%2d", &y, &m, &d);
	else
		nf = sscanf(buf, "%2d-%2d-%4d", &m, &d, &y);

B
Bruce Momjian 已提交
1421 1422
	if (nf == 3)
	{
1423 1424 1425 1426 1427 1428 1429 1430
		st->y = y;
		st->m = m;
		st->d = d;

		return TRUE;
	}

	nf = sscanf(buf, "%2d:%2d:%2d", &hh, &mm, &ss);
B
Bruce Momjian 已提交
1431 1432
	if (nf == 2 || nf == 3)
	{
1433 1434 1435 1436 1437 1438 1439 1440 1441
		st->hh = hh;
		st->mm = mm;
		st->ss = ss;

		return TRUE;
	}

	return FALSE;
}
1442 1443

/*	Change linefeed to carriage-return/linefeed */
1444
int
1445 1446
convert_linefeeds(char *si, char *dst, size_t max)
{
B
Bruce Momjian 已提交
1447 1448
	size_t		i = 0,
				out = 0;
1449

B
Bruce Momjian 已提交
1450 1451 1452 1453 1454 1455 1456
	for (i = 0; i < strlen(si) && out < max - 1; i++)
	{
		if (si[i] == '\n')
		{
			/* Only add the carriage-return if needed */
			if (i > 0 && si[i - 1] == '\r')
			{
1457
				dst[out++] = si[i];
B
Byron Nikolaidis 已提交
1458 1459 1460
				continue;
			}

1461 1462
			dst[out++] = '\r';
			dst[out++] = '\n';
1463 1464
		}
		else
1465
			dst[out++] = si[i];
1466
	}
1467 1468
	dst[out] = '\0';
	return out;
1469 1470
}

B
Bruce Momjian 已提交
1471
/*	Change carriage-return/linefeed to just linefeed
1472 1473 1474 1475 1476
	Plus, escape any special characters.
*/
char *
convert_special_chars(char *si, char *dst, int used)
{
B
Bruce Momjian 已提交
1477 1478 1479 1480 1481
	size_t		i = 0,
				out = 0,
				max;
	static char sout[TEXT_FIELD_SIZE + 5];
	char	   *p;
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493

	if (dst)
		p = dst;
	else
		p = sout;

	p[0] = '\0';

	if (used == SQL_NTS)
		max = strlen(si);
	else
		max = used;
H
Hiroshi Inoue 已提交
1494 1495 1496
#ifdef MULTIBYTE
	multibyte_init();
#endif
1497

B
Bruce Momjian 已提交
1498 1499 1500
	for (i = 0; i < max; i++)
	{
		if (si[i] == '\r' && i + 1 < strlen(si) && si[i + 1] == '\n')
1501
			continue;
H
Hiroshi Inoue 已提交
1502 1503 1504
#ifdef MULTIBYTE
		else if (multibyte_char_check(si[i]) == 0 && (si[i] == '\'' || si[i] == '\\'))
#else
B
Byron Nikolaidis 已提交
1505
		else if (si[i] == '\'' || si[i] == '\\')
H
Hiroshi Inoue 已提交
1506
#endif
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
			p[out++] = '\\';

		p[out++] = si[i];
	}
	p[out] = '\0';
	return p;
}

/*	!!! Need to implement this function !!!  */
int
convert_pgbinary_to_char(char *value, char *rgbValue, int cbValueMax)
{
1519 1520 1521
	mylog("convert_pgbinary_to_char: value = '%s'\n", value);

	strncpy_null(rgbValue, value, cbValueMax);
1522 1523 1524 1525 1526 1527
	return 0;
}

unsigned int
conv_from_octal(unsigned char *s)
{
B
Bruce Momjian 已提交
1528 1529
	int			i,
				y = 0;
1530

B
Bruce Momjian 已提交
1531 1532
	for (i = 1; i <= 3; i++)
		y += (s[i] - 48) * (int) pow(8, 3 - i);
1533 1534

	return y;
1535

1536 1537
}

B
Byron Nikolaidis 已提交
1538 1539 1540
unsigned int
conv_from_hex(unsigned char *s)
{
B
Bruce Momjian 已提交
1541 1542 1543
	int			i,
				y = 0,
				val;
1544

B
Bruce Momjian 已提交
1545 1546
	for (i = 1; i <= 2; i++)
	{
1547

B
Bruce Momjian 已提交
1548 1549 1550 1551 1552 1553
		if (s[i] >= 'a' && s[i] <= 'f')
			val = s[i] - 'a' + 10;
		else if (s[i] >= 'A' && s[i] <= 'F')
			val = s[i] - 'A' + 10;
		else
			val = s[i] - '0';
B
Byron Nikolaidis 已提交
1554

B
Bruce Momjian 已提交
1555
		y += val * (int) pow(16, 2 - i);
B
Byron Nikolaidis 已提交
1556 1557 1558 1559 1560
	}

	return y;
}

B
Bruce Momjian 已提交
1561
/*	convert octal escapes to bytes */
1562 1563 1564
int
convert_from_pgbinary(unsigned char *value, unsigned char *rgbValue, int cbValueMax)
{
B
Bruce Momjian 已提交
1565 1566
	size_t		i;
	int			o = 0;
1567

B
Bruce Momjian 已提交
1568 1569 1570 1571 1572

	for (i = 0; i < strlen(value);)
	{
		if (value[i] == '\\')
		{
1573 1574 1575
			rgbValue[o] = conv_from_octal(&value[i]);
			i += 4;
		}
B
Bruce Momjian 已提交
1576
		else
1577 1578 1579 1580
			rgbValue[o] = value[i++];
		mylog("convert_from_pgbinary: i=%d, rgbValue[%d] = %d, %c\n", i, o, rgbValue[o], rgbValue[o]);
		o++;
	}
1581

B
Bruce Momjian 已提交
1582
	rgbValue[o] = '\0';			/* extra protection */
1583

1584 1585 1586 1587 1588 1589 1590
	return o;
}


char *
conv_to_octal(unsigned char val)
{
B
Bruce Momjian 已提交
1591 1592
	int			i;
	static char x[6];
1593 1594 1595 1596 1597

	x[0] = '\\';
	x[1] = '\\';
	x[5] = '\0';

B
Bruce Momjian 已提交
1598 1599
	for (i = 4; i > 1; i--)
	{
1600 1601 1602 1603 1604 1605 1606
		x[i] = (val & 7) + 48;
		val >>= 3;
	}

	return x;
}

B
Bruce Momjian 已提交
1607
/*	convert non-ascii bytes to octal escape sequences */
1608 1609 1610
int
convert_to_pgbinary(unsigned char *in, char *out, int len)
{
B
Bruce Momjian 已提交
1611 1612
	int			i,
				o = 0;
1613 1614


B
Bruce Momjian 已提交
1615 1616
	for (i = 0; i < len; i++)
	{
1617
		mylog("convert_to_pgbinary: in[%d] = %d, %c\n", i, in[i], in[i]);
B
Bruce Momjian 已提交
1618
		if (isalnum(in[i]) || in[i] == ' ')
1619
			out[o++] = in[i];
B
Bruce Momjian 已提交
1620 1621 1622
		else
		{
			strcpy(&out[o], conv_to_octal(in[i]));
1623 1624
			o += 5;
		}
1625

1626 1627 1628 1629 1630 1631 1632 1633
	}

	mylog("convert_to_pgbinary: returning %d, out='%.*s'\n", o, o, out);

	return o;
}


B
Byron Nikolaidis 已提交
1634 1635 1636
void
encode(char *in, char *out)
{
B
Bruce Momjian 已提交
1637 1638
	unsigned int i,
				o = 0;
B
Byron Nikolaidis 已提交
1639

B
Bruce Momjian 已提交
1640 1641 1642 1643
	for (i = 0; i < strlen(in); i++)
	{
		if (in[i] == '+')
		{
B
Byron Nikolaidis 已提交
1644 1645 1646
			sprintf(&out[o], "%%2B");
			o += 3;
		}
B
Bruce Momjian 已提交
1647
		else if (isspace((unsigned char) in[i]))
B
Byron Nikolaidis 已提交
1648
			out[o++] = '+';
B
Bruce Momjian 已提交
1649 1650
		else if (!isalnum((unsigned char) in[i]))
		{
1651
			sprintf(&out[o], "%%%02x", (unsigned char) in[i]);
B
Byron Nikolaidis 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
			o += 3;
		}
		else
			out[o++] = in[i];
	}
	out[o++] = '\0';
}


void
decode(char *in, char *out)
{
B
Bruce Momjian 已提交
1664 1665
	unsigned int i,
				o = 0;
B
Byron Nikolaidis 已提交
1666

B
Bruce Momjian 已提交
1667 1668
	for (i = 0; i < strlen(in); i++)
	{
B
Byron Nikolaidis 已提交
1669 1670
		if (in[i] == '+')
			out[o++] = ' ';
B
Bruce Momjian 已提交
1671 1672
		else if (in[i] == '%')
		{
B
Byron Nikolaidis 已提交
1673
			sprintf(&out[o++], "%c", conv_from_hex(&in[i]));
B
Bruce Momjian 已提交
1674
			i += 2;
B
Byron Nikolaidis 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683
		}
		else
			out[o++] = in[i];
	}
	out[o++] = '\0';
}



1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
/*	1. get oid (from 'value')
	2. open the large object
	3. read from the large object (handle multiple GetData)
	4. close when read less than requested?  -OR-
		lseek/read each time
		handle case where application receives truncated and
		decides not to continue reading.

	CURRENTLY, ONLY LONGVARBINARY is handled, since that is the only
	data type currently mapped to a PG_TYPE_LO.  But, if any other types
B
Bruce Momjian 已提交
1694
	are desired to map to a large object (PG_TYPE_LO), then that would
1695 1696 1697 1698
	need to be handled here.  For example, LONGVARCHAR could possibly be
	mapped to PG_TYPE_LO someday, instead of PG_TYPE_TEXT as it is now.
*/
int
B
Bruce Momjian 已提交
1699
convert_lo(StatementClass *stmt, void *value, Int2 fCType, PTR rgbValue,
B
Bruce Momjian 已提交
1700
		   SDWORD cbValueMax, SDWORD *pcbValue)
1701
{
B
Bruce Momjian 已提交
1702 1703 1704 1705
	Oid			oid;
	int			retval,
				result,
				left = -1;
T
Tom Lane 已提交
1706
	BindInfoClass *bindInfo = NULL;
1707 1708 1709


/*	If using SQLGetData, then current_col will be set */
B
Bruce Momjian 已提交
1710 1711
	if (stmt->current_col >= 0)
	{
1712 1713 1714
		bindInfo = &stmt->bindings[stmt->current_col];
		left = bindInfo->data_left;
	}
1715

B
Bruce Momjian 已提交
1716 1717 1718 1719
	/*
	 * if this is the first call for this column, open the large object
	 * for reading
	 */
1720

B
Bruce Momjian 已提交
1721 1722
	if (!bindInfo || bindInfo->data_left == -1)
	{
1723

B
Byron Nikolaidis 已提交
1724
		/* begin transaction if needed */
B
Bruce Momjian 已提交
1725 1726
		if (!CC_is_in_trans(stmt->hdbc))
		{
B
Byron Nikolaidis 已提交
1727
			QResultClass *res;
B
Bruce Momjian 已提交
1728
			char		ok;
B
Byron Nikolaidis 已提交
1729 1730

			res = CC_send_query(stmt->hdbc, "BEGIN", NULL);
B
Bruce Momjian 已提交
1731 1732
			if (!res)
			{
B
Byron Nikolaidis 已提交
1733 1734 1735 1736 1737 1738
				stmt->errormsg = "Could not begin (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}
			ok = QR_command_successful(res);
			QR_Destructor(res);
B
Bruce Momjian 已提交
1739 1740
			if (!ok)
			{
B
Byron Nikolaidis 已提交
1741 1742 1743 1744 1745 1746 1747 1748
				stmt->errormsg = "Could not begin (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}

			CC_set_in_trans(stmt->hdbc);
		}

1749 1750
		oid = atoi(value);
		stmt->lobj_fd = lo_open(stmt->hdbc, oid, INV_READ);
B
Bruce Momjian 已提交
1751 1752
		if (stmt->lobj_fd < 0)
		{
1753
			stmt->errornumber = STMT_EXEC_ERROR;
1754
			stmt->errormsg = "Couldnt open large object for reading.";
1755 1756
			return COPY_GENERAL_ERROR;
		}
1757

B
Bruce Momjian 已提交
1758
		/* Get the size */
1759
		retval = lo_lseek(stmt->hdbc, stmt->lobj_fd, 0L, SEEK_END);
B
Bruce Momjian 已提交
1760 1761
		if (retval >= 0)
		{
1762

1763 1764 1765 1766
			left = lo_tell(stmt->hdbc, stmt->lobj_fd);
			if (bindInfo)
				bindInfo->data_left = left;

B
Bruce Momjian 已提交
1767
			/* return to beginning */
1768 1769 1770 1771
			lo_lseek(stmt->hdbc, stmt->lobj_fd, 0L, SEEK_SET);
		}
	}

B
Bruce Momjian 已提交
1772
	if (left == 0)
1773
		return COPY_NO_DATA_FOUND;
1774

B
Bruce Momjian 已提交
1775 1776
	if (stmt->lobj_fd < 0)
	{
1777 1778 1779 1780
		stmt->errornumber = STMT_EXEC_ERROR;
		stmt->errormsg = "Large object FD undefined for multiple read.";
		return COPY_GENERAL_ERROR;
	}
1781

1782
	retval = lo_read(stmt->hdbc, stmt->lobj_fd, (char *) rgbValue, cbValueMax);
B
Bruce Momjian 已提交
1783 1784
	if (retval < 0)
	{
1785
		lo_close(stmt->hdbc, stmt->lobj_fd);
B
Byron Nikolaidis 已提交
1786 1787

		/* commit transaction if needed */
B
Bruce Momjian 已提交
1788 1789
		if (!globals.use_declarefetch && CC_is_in_autocommit(stmt->hdbc))
		{
B
Byron Nikolaidis 已提交
1790
			QResultClass *res;
B
Bruce Momjian 已提交
1791
			char		ok;
B
Byron Nikolaidis 已提交
1792 1793

			res = CC_send_query(stmt->hdbc, "COMMIT", NULL);
B
Bruce Momjian 已提交
1794 1795
			if (!res)
			{
B
Byron Nikolaidis 已提交
1796 1797 1798 1799 1800 1801
				stmt->errormsg = "Could not commit (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}
			ok = QR_command_successful(res);
			QR_Destructor(res);
B
Bruce Momjian 已提交
1802 1803
			if (!ok)
			{
B
Byron Nikolaidis 已提交
1804 1805 1806 1807 1808 1809 1810 1811
				stmt->errormsg = "Could not commit (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}

			CC_set_no_trans(stmt->hdbc);
		}

1812 1813 1814 1815 1816 1817 1818
		stmt->lobj_fd = -1;

		stmt->errornumber = STMT_EXEC_ERROR;
		stmt->errormsg = "Error reading from large object.";
		return COPY_GENERAL_ERROR;
	}

1819 1820 1821 1822 1823 1824 1825
	if (retval < left)
		result = COPY_RESULT_TRUNCATED;
	else
		result = COPY_OK;

	if (pcbValue)
		*pcbValue = left < 0 ? SQL_NO_TOTAL : left;
1826 1827


B
Bruce Momjian 已提交
1828
	if (bindInfo && bindInfo->data_left > 0)
1829
		bindInfo->data_left -= retval;
1830

1831

B
Bruce Momjian 已提交
1832 1833
	if (!bindInfo || bindInfo->data_left == 0)
	{
1834
		lo_close(stmt->hdbc, stmt->lobj_fd);
B
Byron Nikolaidis 已提交
1835 1836

		/* commit transaction if needed */
B
Bruce Momjian 已提交
1837 1838
		if (!globals.use_declarefetch && CC_is_in_autocommit(stmt->hdbc))
		{
B
Byron Nikolaidis 已提交
1839
			QResultClass *res;
B
Bruce Momjian 已提交
1840
			char		ok;
B
Byron Nikolaidis 已提交
1841 1842

			res = CC_send_query(stmt->hdbc, "COMMIT", NULL);
B
Bruce Momjian 已提交
1843 1844
			if (!res)
			{
B
Byron Nikolaidis 已提交
1845 1846 1847 1848 1849 1850
				stmt->errormsg = "Could not commit (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}
			ok = QR_command_successful(res);
			QR_Destructor(res);
B
Bruce Momjian 已提交
1851 1852
			if (!ok)
			{
B
Byron Nikolaidis 已提交
1853 1854 1855 1856 1857 1858 1859 1860
				stmt->errormsg = "Could not commit (in-line) a transaction";
				stmt->errornumber = STMT_EXEC_ERROR;
				return COPY_GENERAL_ERROR;
			}

			CC_set_no_trans(stmt->hdbc);
		}

B
Bruce Momjian 已提交
1861
		stmt->lobj_fd = -1;		/* prevent further reading */
1862
	}
1863 1864 1865


	return result;
1866

1867
}