ecpglib.c 35.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright comment */
/*
 * The aim is to get a simpler inteface to the database routines.
 * All the tidieous messing around with tuples is supposed to be hidden
 * by this function.
 */
/* Author: Linus Tolke
   (actually most if the code is "borrowed" from the distribution and just
   slightly modified)
 */

/* Taken over as part of PostgreSQL by Michael Meskes <meskes@debian.org>
   on Feb. 5th, 1998 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>

21 22
#include <libpq-fe.h>
#include <libpq/pqcomm.h>
23 24 25 26
#include <ecpgtype.h>
#include <ecpglib.h>
#include <sqlca.h>

27 28
extern int no_auto_trans;

29 30 31 32 33 34 35
static struct connection
{
	char *name;
	PGconn *connection;
	struct connection *next;
} *all_connections = NULL, *actual_connection = NULL;

36 37 38 39 40
static int	simple_debug = 0;
static FILE *debugstream = NULL;
static int	committed = true;

static void
41
register_error(long code, char *fmt,...)
42 43 44 45 46 47 48 49 50 51 52 53 54 55
{
	va_list		args;

	sqlca.sqlcode = code;
	va_start(args, fmt);
	vsprintf(sqlca.sqlerrm.sqlerrmc, fmt, args);
	va_end(args);
	sqlca.sqlerrm.sqlerrml = strlen(sqlca.sqlerrm.sqlerrmc);
}

/* This function returns a newly malloced string that has the ' and \
   in the argument quoted with \.
 */
static
56
char *
57 58 59 60 61 62
quote_postgres(char *arg)
{
	char	   *res = (char *) malloc(2 * strlen(arg) + 1);
	int			i,
				ri;

63 64 65
	if (!res)
		return(res);
		
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	for (i = 0, ri = 0; arg[i]; i++, ri++)
	{
		switch (arg[i])
		{
			case '\'':
			case '\\':
				res[ri++] = '\\';
			default:
				;
		}

		res[ri] = arg[i];
	}
	res[ri] = '\0';

	return res;
}


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
static void
ECPGfinish(struct connection *act)
{
	if (act != NULL)
	{
		ECPGlog("ECPGfinish: finishing %s.\n", act->name);
		PQfinish(act->connection);
		/* remove act from the list */
		if (act == all_connections)
		{
			all_connections = act->next;
			free(act->name);
			free(act);
		}
		else
		{
			struct connection *con;
			
			for (con = all_connections; con->next && con->next !=  act; con = con->next);
			if (con->next)
			{
				con->next = act->next;
				free(act->name);
				free(act);
			}
		}
		
		if (actual_connection == act)
			actual_connection = all_connections;
	}
	else
		ECPGlog("ECPGfinish: called an extra time.\n");
}

119 120 121 122 123 124 125 126 127
bool
ECPGdo(int lineno, char *query,...)
{
	va_list		ap;
	bool		status = false;
	char	   *copiedquery;
	PGresult   *results;
	PGnotify   *notify;
	enum ECPGttype type;
128 129 130 131 132
	void	   *value = NULL, *ind_value;
	long		varcharsize, ind_varcharsize;
	long		arrsize, ind_arrsize;
	long		offset, ind_offset;
	enum ECPGttype ind_type;
133

134
	memset((char *) &sqlca, 0, sizeof (sqlca));
135 136 137 138 139 140 141
	va_start(ap, query);

	copiedquery = strdup(query);

	type = va_arg(ap, enum ECPGttype);

	/*
142 143 144 145
	 * Now, if the type is one of the fill in types then we take the
	 * argument and enter that in the string at the first %s position.
	 * Then if there are any more fill in types we fill in at the next and
	 * so on.
146 147 148 149 150 151 152 153 154
	 */
	while (type != ECPGt_EOIT)
	{
		char	   *newcopy;
		char	   *mallocedval = NULL;
		char	   *tobeinserted = NULL;
		char	   *p;
		char		buff[20];

155 156 157 158 159
		/*
		 * Some special treatment is needed for records since we want
		 * their contents to arrive in a comma-separated list on insert (I
		 * think).
		 */
160 161

		value = va_arg(ap, void *);
162 163
		varcharsize = va_arg(ap, long);
		arrsize = va_arg(ap, long);
164
		offset = va_arg(ap, long);
Marc G. Fournier's avatar
Marc G. Fournier committed
165 166 167 168
		ind_type = va_arg(ap, enum ECPGttype);
		ind_value = va_arg(ap, void *);
		ind_varcharsize = va_arg(ap, long);
		ind_arrsize = va_arg(ap, long);
169
		ind_offset = va_arg(ap, long);
Marc G. Fournier's avatar
Marc G. Fournier committed
170 171 172 173 174

		buff[0] = '\0';
		
		/* check for null value and set input buffer accordingly */
		switch (ind_type)
175
		{
Marc G. Fournier's avatar
Marc G. Fournier committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
			case ECPGt_short:
			case ECPGt_unsigned_short:
				if (*(short *) ind_value < 0)
					strcpy(buff, "null");
				break;
			case ECPGt_int:
			case ECPGt_unsigned_int:
				if (*(int *) ind_value < 0)
					strcpy(buff, "null");
				break;
			case ECPGt_long:
			case ECPGt_unsigned_long:
				if (*(long *) ind_value < 0L)
					strcpy(buff, "null");
				break;
			default:
				break;
		}
		
		if (*buff == '\0')
		{
		   switch (type)
		   {
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
			case ECPGt_short:
			case ECPGt_int:
				sprintf(buff, "%d", *(int *) value);
				tobeinserted = buff;
				break;

			case ECPGt_unsigned_short:
			case ECPGt_unsigned_int:
				sprintf(buff, "%d", *(unsigned int *) value);
				tobeinserted = buff;
				break;

			case ECPGt_long:
				sprintf(buff, "%ld", *(long *) value);
				tobeinserted = buff;
				break;

			case ECPGt_unsigned_long:
				sprintf(buff, "%ld", *(unsigned long *) value);
				tobeinserted = buff;
				break;

			case ECPGt_float:
				sprintf(buff, "%.14g", *(float *) value);
				tobeinserted = buff;
				break;

			case ECPGt_double:
				sprintf(buff, "%.14g", *(double *) value);
				tobeinserted = buff;
				break;

			case ECPGt_bool:
				sprintf(buff, "'%c'", (*(char *) value ? 't' : 'f'));
				tobeinserted = buff;
				break;

236 237 238 239
			case ECPGt_char:
			case ECPGt_unsigned_char:
				{
					/* set slen to string length if type is char * */
240
					int			slen = (varcharsize == 0) ? strlen((char *) value) : varcharsize;
241
					char * tmp;
242

243
					newcopy = (char *) malloc(slen + 1);
244 245 246 247 248 249 250 251
					if (!newcopy)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
						
252 253 254 255
					strncpy(newcopy, (char *) value, slen);
					newcopy[slen] = '\0';

					mallocedval = (char *) malloc(2 * strlen(newcopy) + 3);
256 257 258 259 260 261 262 263
					if (!mallocedval)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
						
264
					strcpy(mallocedval, "'");
265 266 267 268 269 270 271 272 273 274
					tmp = quote_postgres(newcopy);
					if (!tmp)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
						
					strcat(mallocedval, tmp);
275 276 277 278 279 280 281 282
					strcat(mallocedval, "'");

					free(newcopy);

					tobeinserted = mallocedval;
				}
				break;

283 284 285 286
			case ECPGt_varchar:
				{
					struct ECPGgeneric_varchar *var =
					(struct ECPGgeneric_varchar *) value;
287
					char *tmp;
288 289

					newcopy = (char *) malloc(var->len + 1);
290 291 292 293 294 295 296 297
					if (!newcopy)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
						
298 299 300 301
					strncpy(newcopy, var->arr, var->len);
					newcopy[var->len] = '\0';

					mallocedval = (char *) malloc(2 * strlen(newcopy) + 3);
302 303 304 305 306 307 308 309
					if (!mallocedval)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
					
310
					strcpy(mallocedval, "'");
311 312 313 314 315 316 317 318 319 320
					tmp = quote_postgres(newcopy);
					if (!tmp)
					{
						ECPGfinish(actual_connection);
						ECPGlog("out of memory\n");
				                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
						return false;
					}
					                                                
					strcat(mallocedval, tmp);
321 322 323 324 325 326 327 328 329 330
					strcat(mallocedval, "'");

					free(newcopy);

					tobeinserted = mallocedval;
				}
				break;

			default:
				/* Not implemented yet */
331
				register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.",
332 333 334
							   ECPGtype_name(type), lineno);
				return false;
				break;
Marc G. Fournier's avatar
Marc G. Fournier committed
335
		   }
336
		}
Marc G. Fournier's avatar
Marc G. Fournier committed
337 338
		else
		   tobeinserted = buff;
339

340 341 342
		/*
		 * Now tobeinserted points to an area that is to be inserted at
		 * the first %s
343 344 345 346
		 */
		newcopy = (char *) malloc(strlen(copiedquery)
								  + strlen(tobeinserted)
								  + 1);
347 348 349 350 351 352 353 354
		if (!newcopy)
		{
			ECPGfinish(actual_connection);
			ECPGlog("out of memory\n");
	                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
			return false;
		}
		 	
355 356 357
		strcpy(newcopy, copiedquery);
		if ((p = strstr(newcopy, ";;")) == NULL)
		{
358 359 360 361

			/*
			 * We have an argument but we dont have the matched up string
			 * in the string
362
			 */
363
			register_error(ECPG_TOO_MANY_ARGUMENTS, "Too many arguments line %d.", lineno);
364 365 366 367 368
			return false;
		}
		else
		{
			strcpy(p, tobeinserted);
369 370 371 372 373

			/*
			 * The strange thing in the second argument is the rest of the
			 * string from the old string
			 */
374 375 376 377 378 379
			strcat(newcopy,
				   copiedquery
				   + (p - newcopy)
				   + 2 /* Length of ;; */ );
		}

380 381 382
		/*
		 * Now everything is safely copied to the newcopy. Lets free the
		 * oldcopy and let the copiedquery get the value from the newcopy.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		 */
		if (mallocedval != NULL)
		{
			free(mallocedval);
			mallocedval = NULL;
		}

		free(copiedquery);
		copiedquery = newcopy;

		type = va_arg(ap, enum ECPGttype);
	}

	/* Check if there are unmatched things left. */
	if (strstr(copiedquery, ";;") != NULL)
	{
399
		register_error(ECPG_TOO_FEW_ARGUMENTS, "Too few arguments line %d.", lineno);
400 401 402
		return false;
	}

403
	/* Now the request is built. */
404

405
	if (committed && !no_auto_trans)
406
	{
407
		if ((results = PQexec(actual_connection->connection, "begin transaction")) == NULL)
408
		{
409
			register_error(ECPG_TRANS, "Error starting transaction line %d.", lineno);
410 411 412 413 414 415 416
			return false;
		}
		PQclear(results);
		committed = 0;
	}

	ECPGlog("ECPGdo line %d: QUERY: %s\n", lineno, copiedquery);
417
	results = PQexec(actual_connection->connection, copiedquery);
418 419 420 421 422
	free(copiedquery);

	if (results == NULL)
	{
		ECPGlog("ECPGdo line %d: error: %s", lineno,
423
				PQerrorMessage(actual_connection->connection));
424
		register_error(ECPG_PGSQL, "Postgres error: %s line %d.",
425
					   PQerrorMessage(actual_connection->connection), lineno);
426 427
	}
	else
428 429
	{
		sqlca.sqlerrd[2] = 0;
430 431
		switch (PQresultStatus(results))
		{
432
				int			nfields, ntuples, act_tuple, act_field;
433 434

			case PGRES_TUPLES_OK:
435 436 437 438 439

				/*
				 * XXX Cheap Hack. For now, we see only the last group of
				 * tuples.	This is clearly not the right way to do things
				 * !!
440 441
				 */

442 443 444 445 446
				nfields = PQnfields(results);
				sqlca.sqlerrd[2] = ntuples = PQntuples(results);
				status = true;
				
				if (ntuples < 1)
447
				{
448
					ECPGlog("ECPGdo line %d: Incorrect number of matches: %d\n",
449 450 451
							lineno, ntuples);
					register_error(ECPG_NOT_FOUND, "Data not found line %d.", lineno);
					status = false;
452 453 454
					break;
				}

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
                                for (act_field = 0; act_field < nfields && status; act_field++)
                                {
                                     char	   *pval;
                                     char	   *scan_length;
                                             
                                     type = va_arg(ap, enum ECPGttype);
                                     value = va_arg(ap, void *);
                                     varcharsize = va_arg(ap, long);
                                     arrsize = va_arg(ap, long);
                                     offset = va_arg(ap, long);
                                     ind_type = va_arg(ap, enum ECPGttype);
                                     ind_value = va_arg(ap, void *);
                                     ind_varcharsize = va_arg(ap, long);
                                     ind_arrsize = va_arg(ap, long);
                                     ind_offset = va_arg(ap, long);

                                     /* if we don't have enough space, we cannot read all tuples */
                                     if ((arrsize > 0 && ntuples > arrsize) || (ind_arrsize > 0 && ntuples > ind_arrsize))
                                     {
                                             ECPGlog("ECPGdo line %d: Incorrect number of matches: %d don't fit into array of %d\n",
                                                             lineno, ntuples, arrsize);
                                             register_error(ECPG_TOO_MANY_MATCHES, "Too many matches line %d.", lineno);
                                             status = false;
                                             break;
                                     }
                                     for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
                                     {
                                         pval = PQgetvalue(results, act_tuple, act_field);

                                         ECPGlog("ECPGdo line %d: RESULT: %s\n", lineno, pval ? pval : "");

                                         /* Now the pval is a pointer to the value. */
                                         /* We will have to decode the value */
                                     
                                         /* check for null value and set indicator accordingly */
                                         switch (ind_type)
                                         {
                                                 case ECPGt_short:
                                                 case ECPGt_unsigned_short:
                                                         ((short *) ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field);
                                                         break;
                                                 case ECPGt_int:
                                                 case ECPGt_unsigned_int:
                                                         ((int *) ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field);
                                                         break;
                                                 case ECPGt_long:
                                                 case ECPGt_unsigned_long:
                                                         ((long *) ind_value)[act_tuple] = -PQgetisnull(results, act_tuple, act_field);
                                                         break;
                                                 default:
                                                         break;
                                         }
                                                                                 
                                         switch (type)
                                         {
                                                         long		res;
                                                         unsigned long ures;
                                                         double		dres;
    
                                                 case ECPGt_short:
                                                 case ECPGt_int:
                                                 case ECPGt_long:
                                                         if (pval)
                                                         {
                                                                 res = strtol(pval, &scan_length, 10);
                                                                 if (*scan_length != '\0')		/* Garbage left */
                                                                 {
                                                                         register_error(ECPG_INT_FORMAT, "Not correctly formatted int type: %s line %d.",
                                                                                                    pval, lineno);
                                                                         status = false;
                                                                         res = 0L;
                                                                 }
                                                         }
                                                         else
                                                                 res = 0L;
    
                                                         /* Again?! Yes */
                                                         switch (type)
                                                         {
                                                                 case ECPGt_short:
                                                                         ((short *) value)[act_tuple] = (short) res;
                                                                         break;
                                                                 case ECPGt_int:
                                                                         ((int *) value)[act_tuple] = (int) res;
                                                                         break;
                                                                 case ECPGt_long:
                                                                         ((long *) value)[act_tuple] = res;
                                                                         break;
                                                                 default:
                                                                         /* Cannot happen */
                                                                         break;
                                                         }
                                                         break;
    
                                                 case ECPGt_unsigned_short:
                                                 case ECPGt_unsigned_int:
                                                 case ECPGt_unsigned_long:
                                                         if (pval)
                                                         {
                                                                 ures = strtoul(pval, &scan_length, 10);
                                                                 if (*scan_length != '\0')		/* Garbage left */
                                                                 {
                                                                         register_error(ECPG_UINT_FORMAT, "Not correctly formatted unsigned type: %s line %d.",
                                                                                                    pval, lineno);
                                                                         status = false;
                                                                         ures = 0L;
                                                                 }
                                                         }
                                                         else
                                                                 ures = 0L;
    
                                                         /* Again?! Yes */
                                                         switch (type)
                                                         {
                                                                 case ECPGt_unsigned_short:
                                                                         ((unsigned short *) value)[act_tuple] = (unsigned short) ures;
                                                                         break;
                                                                 case ECPGt_unsigned_int:
                                                                         ((unsigned int *) value)[act_tuple] = (unsigned int) ures;
                                                                         break;
                                                                 case ECPGt_unsigned_long:
                                                                         ((unsigned long *) value)[act_tuple] = ures;
                                                                         break;
                                                                 default:
                                                                         /* Cannot happen */
                                                                         break;
                                                         }
                                                         break;
    
    
                                                 case ECPGt_float:
                                                 case ECPGt_double:
                                                         if (pval)
                                                         {
                                                                 dres = strtod(pval, &scan_length);
                                                                 if (*scan_length != '\0')		/* Garbage left */
                                                                 {
                                                                         register_error(ECPG_FLOAT_FORMAT, "Not correctly formatted floating point type: %s line %d.",
                                                                                                    pval, lineno);
                                                                         status = false;
                                                                         dres = 0.0;
                                                                 }
                                                         }
                                                         else
                                                                 dres = 0.0;
    
                                                         /* Again?! Yes */
                                                         switch (type)
                                                         {
                                                                 case ECPGt_float:
                                                                         ((float *) value)[act_tuple] = dres;
                                                                         break;
                                                                 case ECPGt_double:
                                                                         ((double *) value)[act_tuple] = dres;
                                                                         break;
                                                                 default:
                                                                         /* Cannot happen */
                                                                         break;
                                                         }
                                                         break;
    
                                                 case ECPGt_bool:
                                                         if (pval)
                                                         {
                                                                 if (pval[0] == 'f' && pval[1] == '\0')
                                                                 {
                                                                         ((char *) value)[act_tuple] = false;
                                                                         break;
                                                                 }
                                                                 else if (pval[0] == 't' && pval[1] == '\0')
                                                                 {
                                                                         ((char *) value)[act_tuple] = true;
                                                                         break;
                                                                 }
                                                         }
    
                                                         register_error(ECPG_CONVERT_BOOL, "Unable to convert %s to bool on line %d.",
                                                                                    (pval ? pval : "NULL"),
                                                                                    lineno);
                                                         status = false;
                                                         break;
    
                                                 case ECPGt_char:
                                                 case ECPGt_unsigned_char:
                                                         {
                                                                 if (varcharsize == 0)
                                                                 {
                                                                         /* char* */
                                                                         strncpy(((char **) value)[act_tuple], pval, strlen(pval));
                                                                         (((char **) value)[act_tuple])[strlen(pval)] = '\0';
                                                                 }
                                                                 else
                                                                 {
                                                                         strncpy((char *) (value + offset * act_tuple), pval, varcharsize);
                                                                         if (varcharsize < strlen(pval))
                                                                         {
                                                                                 /* truncation */
                                                                                 switch (ind_type)
                                                                                 {
                                                                                         case ECPGt_short:
                                                                                         case ECPGt_unsigned_short:
                                                                                                 ((short *) ind_value)[act_tuple] = varcharsize;
                                                                                                 break;
                                                                                         case ECPGt_int:
                                                                                         case ECPGt_unsigned_int:
                                                                                                 ((int *) ind_value)[act_tuple] = varcharsize;
                                                                                                 break;
                                                                                         case ECPGt_long:
                                                                                         case ECPGt_unsigned_long:
                                                                                                 ((long *) ind_value)[act_tuple] = varcharsize;
                                                                                                 break;
                                                                                         default:
                                                                                                 break;
                                                                                 }
669
                                                                                 sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W';
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
                                                                         }
                                                                 }
                                                         }
                                                         break;
    
                                                 case ECPGt_varchar:
                                                         {
                                                                 struct ECPGgeneric_varchar *var =
								 (struct ECPGgeneric_varchar *) (value + offset * act_tuple);
    
								 if (varcharsize == 0)
								   strncpy(var->arr, pval, strlen(pval));
								 else
								   strncpy(var->arr, pval, varcharsize);

                                                                 var->len = strlen(pval);
                                                                 if (varcharsize > 0 && var->len > varcharsize)
                                                                 {
                                                                         /* truncation */
                                                                         switch (ind_type)
                                                                         {
                                                                                 case ECPGt_short:
                                                                                 case ECPGt_unsigned_short:
                                                                                         ((short *) ind_value)[act_tuple] = varcharsize;
                                                                                         break;
                                                                                 case ECPGt_int:
                                                                                 case ECPGt_unsigned_int:
                                                                                         ((int *) ind_value)[act_tuple] = varcharsize;
                                                                                         break;
                                                                                 case ECPGt_long:
                                                                                 case ECPGt_unsigned_long:
                                                                                         ((long *) ind_value)[act_tuple] = varcharsize;
                                                                                         break;
                                                                                 default:
                                                                                         break;
                                                                         }
706
                                                                         sqlca.sqlwarn[0] = sqlca.sqlwarn[1] = 'W';
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    
                                                                         var->len = varcharsize;
                                                                 }
                                                         }
                                                         break;
    
                                                 case ECPGt_EORT:
                                                         ECPGlog("ECPGdo line %d: Too few arguments.\n", lineno);
                                                         register_error(ECPG_TOO_FEW_ARGUMENTS, "Too few arguments line %d.", lineno);
                                                         status = false;
                                                         break;
    
                                                 default:
                                                         register_error(ECPG_UNSUPPORTED, "Unsupported type %s on line %d.",
                                                                                    ECPGtype_name(type), lineno);
                                                         status = false;
                                                         break;
                                         }
                                     }
726 727 728 729 730 731
				}

				type = va_arg(ap, enum ECPGttype);

				if (status && type != ECPGt_EORT)
				{
732 733
					register_error(ECPG_TOO_MANY_ARGUMENTS, "Too many arguments line %d.", lineno);
					status = false;
734 735 736 737 738 739
				}

				PQclear(results);
				break;
			case PGRES_EMPTY_QUERY:
				/* do nothing */
740
				register_error(ECPG_EMPTY, "Empty query line %d.", lineno);
741 742 743
				break;
			case PGRES_COMMAND_OK:
				status = true;
744 745
				sqlca.sqlerrd[2] = atol(PQcmdTuples(results));
				ECPGlog("TEST: %s\n", PQcmdTuples(results));
746 747 748 749 750 751
				ECPGlog("ECPGdo line %d Ok: %s\n", lineno, PQcmdStatus(results));
				break;
			case PGRES_NONFATAL_ERROR:
			case PGRES_FATAL_ERROR:
			case PGRES_BAD_RESPONSE:
				ECPGlog("ECPGdo line %d: Error: %s",
752
						lineno, PQerrorMessage(actual_connection->connection));
753
				register_error(ECPG_PGSQL, "Error: %s line %d.",
754
							   PQerrorMessage(actual_connection->connection), lineno);
755
				status = false;
756 757 758 759 760 761 762 763 764 765 766 767
				break;
			case PGRES_COPY_OUT:
				ECPGlog("ECPGdo line %d: Got PGRES_COPY_OUT ... tossing.\n", lineno);
				PQendcopy(results->conn);
				break;
			case PGRES_COPY_IN:
				ECPGlog("ECPGdo line %d: Got PGRES_COPY_IN ... tossing.\n", lineno);
				PQendcopy(results->conn);
				break;
			default:
				ECPGlog("ECPGdo line %d: Got something else, postgres error.\n",
						lineno);
768 769
				register_error(ECPG_PGSQL, "Postgres error line %d.", lineno);
				status = false;
770 771
				break;
		}
772
	}
773 774

	/* check for asynchronous returns */
775
	notify = PQnotifies(actual_connection->connection);
776 777 778 779 780 781 782 783 784 785 786 787 788
	if (notify)
	{
		ECPGlog("ECPGdo line %d: ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
				lineno, notify->relname, notify->be_pid);
		free(notify);
	}

	va_end(ap);
	return status;
}


bool
789
ECPGtrans(int lineno, const char * transaction)
790 791 792
{
	PGresult   *res;

793
	ECPGlog("ECPGtrans line %d action = %s\n", lineno, transaction);
794
	if ((res = PQexec(actual_connection->connection, transaction)) == NULL)
795
	{
796
		register_error(ECPG_TRANS, "Error in transaction processing line %d.", lineno);
797 798 799
		return (FALSE);
	}
	PQclear(res);
800 801
	if (strcmp(transaction, "commit") == 0 || strcmp(transaction, "rollback") == 0)
		committed = 1;
802 803 804 805
	return (TRUE);
}

bool
806
ECPGsetconn(int lineno, const char *connection_name)
807
{
808
	struct connection *con = all_connections;
809 810

	ECPGlog("ECPGsetconn: setting actual connection to %s\n", connection_name);	
811
	
812
	for (; con && strcmp(connection_name, con->name) != 0; con=con->next);
813 814 815 816 817 818 819 820 821 822
	if (con)
	{
		actual_connection = con;
		return true;
	}
	else
	{
		register_error(ECPG_NO_CONN, "No such connection %s in line %d", connection_name, lineno);
		return false;
	}
823 824 825
}

bool
826
ECPGconnect(int lineno, const char *dbname, const char *user, const char *passwd, const char * connection_name)
827
{
828
	struct connection *this = malloc(sizeof(struct connection));
829

830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	if (!this)
	{
		ECPGlog("out of memory\n");
                register_error(ECPG_OUT_OF_MEMORY, "out of memory in line %d", lineno);
		return false;
	}
				
	if (dbname == NULL && connection_name == NULL)
		connection_name = "DEFAULT";
	
	/* add connection to our list */
	if (connection_name != NULL)
		this->name = strdup(connection_name);
	else
		this->name = strdup(dbname);
	
	if (all_connections == NULL)
		this->next = NULL;
	else
		this->next = all_connections;
850

851 852 853
	actual_connection = all_connections = this;
		
	ECPGlog("ECPGconnect: opening database %s %s%s\n", dbname ? dbname : "NULL", user ? "for user ": "", user ? user : "");
854

855
	sqlca.sqlcode = 0;
856

857 858 859
	this->connection = PQsetdbLogin(NULL, NULL, NULL, NULL, dbname, user, passwd);
        
	if (PQstatus(this->connection) == CONNECTION_BAD)
860
	{
861 862 863 864
		ECPGfinish(this);
                ECPGlog("connect: could not open database %s %s%s in line %d\n", dbname ? dbname : "NULL", user ? "for user ": "", user ? user : "", lineno);
                
		register_error(ECPG_CONNECT, "connect: could not open database %s.", dbname ? dbname : "NULL");
865 866
		return false;
	}
867
	
868 869 870 871
	return true;
}

bool
872
ECPGdisconnect(int lineno, const char *connection_name)
873
{
874 875 876 877 878
	struct connection *con;
	
	if (strcmp(connection_name, "CURRENT") == 0)
		ECPGfinish(actual_connection);
	else if (strcmp(connection_name, "ALL") == 0)
879
	{
880 881 882 883 884 885 886
		for (con = all_connections; con;)
		{
			struct connection *f = con;
			
			con = con->next;
			ECPGfinish(f);
		}
887
	}
888
	else 
889
	{
890
		for (con = all_connections; con && strcmp(con->name, connection_name) != 0;con = con->next);
891 892 893 894 895 896 897
		if (con == NULL)
		{		
			ECPGlog("disconnect: not connected to connection %s\n", connection_name);
			register_error(ECPG_NO_CONN, "No such connection %s in line %d", connection_name, lineno);
			return false;
		}
		else
898
		{
899
			ECPGfinish(con);
900
		}
901
	}
902
	
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	return true;
}

void
ECPGdebug(int n, FILE *dbgs)
{
	simple_debug = n;
	debugstream = dbgs;
	ECPGlog("ECPGdebug: set to %d\n", simple_debug);
}

void
ECPGlog(const char *format,...)
{
	va_list		ap;

	if (simple_debug)
	{
		char	   *f = (char *) malloc(strlen(format) + 100);

923 924 925
		if (!f)
			return;
						
926 927 928 929 930 931 932 933 934
		sprintf(f, "[%d]: %s", getpid(), format);

		va_start(ap, format);
		vfprintf(debugstream, f, ap);
		va_end(ap);

		free(f);
	}
}
935 936

/* print out an error message */
937 938
void
sqlprint(void)
939 940
{
	sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
941
	printf("sql error %s\n", sqlca.sqlerrm.sqlerrmc);
942
}