tabext.cpp 20.3 KB
Newer Older
Olivier Bertrand's avatar
Olivier Bertrand committed
1
/************* Tabext C++ Functions Source Code File (.CPP) ************/
2
/*  Name: TABEXT.CPP  Version 1.1                                      */
Olivier Bertrand's avatar
Olivier Bertrand committed
3
/*                                                                     */
4
/*  (C) Copyright to the author Olivier BERTRAND          2017 - 2019  */
Olivier Bertrand's avatar
Olivier Bertrand committed
5 6 7 8 9 10 11 12 13 14 15 16
/*                                                                     */
/*  This file contains the TBX, TDB and OPJOIN classes functions.      */
/***********************************************************************/

/***********************************************************************/
/*  Include relevant MariaDB header file.                              */
/***********************************************************************/
#define MYSQL_SERVER 1
#include "my_global.h"
#include "sql_class.h"
#include "sql_servers.h"
#include "sql_string.h"
17
#if !defined(_WIN32)
Olivier Bertrand's avatar
Olivier Bertrand committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "osutil.h"
#endif

/***********************************************************************/
/*  Include required application header files                          */
/*  global.h    is header containing all global Plug declarations.     */
/*  plgdbsem.h  is header containing the DB applic. declarations.      */
/*  xobject.h   is header containing XOBJECT derived classes declares. */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"
#include "xtable.h"
#include "tabext.h"
#include "ha_connect.h"

/* -------------------------- Class CONDFIL -------------------------- */

/***********************************************************************/
/*  CONDFIL Constructor.                                               */
/***********************************************************************/
38
CONDFIL::CONDFIL(uint idx, AMT type)
Olivier Bertrand's avatar
Olivier Bertrand committed
39
{
40
//Cond = cond; 
Olivier Bertrand's avatar
Olivier Bertrand committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	Idx = idx; 
	Type = type;
	Op = OP_XX;
	Cmds = NULL;
	Alist = NULL;
	All = true;
	Bd = false;
	Hv = false;
	Body = NULL, 
	Having = NULL;
}	// end of CONDFIL constructor

/***********************************************************************/
/*  Make and allocate the alias list.                                  */
/***********************************************************************/
int CONDFIL::Init(PGLOBAL g, PHC hc)
{
	PTOS  options = hc->GetTableOptionStruct();
	char *p, *cn, *cal, *alt = NULL;
	int   rc = RC_OK;
	bool  h;

	if (options)
64
    alt = (char*)GetListOption(g, "Alias", options->oplist, NULL);
Olivier Bertrand's avatar
Olivier Bertrand committed
65 66 67 68 69 70 71 72 73 74 75 76 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 119 120 121 122 123 124 125 126 127

	while (alt) {
		if (!(p = strchr(alt, '='))) {
			strcpy(g->Message, "Invalid alias list");
			rc = RC_FX;
			break;
		}	// endif !p

		cal = alt;				 // Alias
		*p++ = 0;

		if ((h = *p == '*')) {
			rc = RC_INFO;
			p++;
		}	// endif h

		cn = p;						// Remote column name

		if ((alt = strchr(p, ';')))
			*alt++ = 0;

		if (*cn == 0)
			cn = alt;

		Alist = new(g) ALIAS(Alist, cn, cal, h);
	}	// endwhile alt

	return rc;
}	// end of Init

/***********************************************************************/
/*  Make and allocate the alias list.                                  */
/***********************************************************************/
const char *CONDFIL::Chk(const char *fln, bool *h)
{
	for (PAL pal = Alist; pal; pal = pal->Next)
		if (!stricmp(fln, pal->Alias)) {
			*h = pal->Having;
			return pal->Name;
		}	// endif fln

	*h = false;
	return fln;
}	// end of Chk

/* --------------------------- Class EXTDEF -------------------------- */

/***********************************************************************/
/*  EXTDEF Constructor.                                                */
/***********************************************************************/
EXTDEF::EXTDEF(void)
{
	Tabname = Tabschema = Username = Password = Tabcat = Tabtyp = NULL;
	Colpat = Srcdef = Qchar = Qrystr = Sep = Phpos = NULL;
	Options = Cto = Qto = Quoted = Maxerr = Maxres = Memory = 0;
	Scrollable = Xsrc = false;
} // end of EXTDEF constructor

/***********************************************************************/
/*  DefineAM: define specific AM block values from XDB file.           */
/***********************************************************************/
bool EXTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
{
128 129 130 131 132 133
	if (g->Createas) {
		strcpy(g->Message,
			"Multiple-table UPDATE/DELETE commands are not supported");
		return true;
	}	// endif multi

Olivier Bertrand's avatar
Olivier Bertrand committed
134 135 136 137 138 139 140 141 142 143 144
	Desc = NULL;
	Tabname = GetStringCatInfo(g, "Name",
		(Catfunc & (FNC_TABLE | FNC_COL)) ? NULL : Name);
	Tabname = GetStringCatInfo(g, "Tabname", Tabname);
	Tabschema = GetStringCatInfo(g, "Dbname", NULL);
	Tabschema = GetStringCatInfo(g, "Schema", Tabschema);
	Tabcat = GetStringCatInfo(g, "Qualifier", NULL);
	Tabcat = GetStringCatInfo(g, "Catalog", Tabcat);
	Username = GetStringCatInfo(g, "User", NULL);
	Password = GetStringCatInfo(g, "Password", NULL);

145 146 147 148 149
	// Memory was Boolean, it is now integer
	if (!(Memory = GetIntCatInfo("Memory", 0)))
		Memory = GetBoolCatInfo("Memory", false) ? 1 : 0;

	if ((Srcdef = GetStringCatInfo(g, "Srcdef", NULL))) {
Olivier Bertrand's avatar
Olivier Bertrand committed
150
		Read_Only = true;
151 152
		if (Memory == 2) Memory = 1;
	}	// endif Srcdef
Olivier Bertrand's avatar
Olivier Bertrand committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	Qrystr = GetStringCatInfo(g, "Query_String", "?");
	Sep = GetStringCatInfo(g, "Separator", NULL);
//Alias = GetStringCatInfo(g, "Alias", NULL);
	Phpos = GetStringCatInfo(g, "Phpos", NULL);
	Xsrc = GetBoolCatInfo("Execsrc", FALSE);
	Maxerr = GetIntCatInfo("Maxerr", 0);
	Maxres = GetIntCatInfo("Maxres", 0);
	Quoted = GetIntCatInfo("Quoted", 0);
	Options = 0;
	Cto = 0;
	Qto = 0;

	if ((Scrollable = GetBoolCatInfo("Scrollable", false)) && !Elemt)
		Elemt = 1;     // Cannot merge SQLFetch and SQLExtendedFetch

	if (Catfunc == FNC_COL)
		Colpat = GetStringCatInfo(g, "Colpat", NULL);

	if (Catfunc == FNC_TABLE)
		Tabtyp = GetStringCatInfo(g, "Tabtype", NULL);

	Pseudo = 2;    // FILID is Ok but not ROWID
	return false;
} // end of DefineAM

/* ---------------------------TDBEXT class --------------------------- */

/***********************************************************************/
/*  Implementation of the TDBEXT class.                                */
/***********************************************************************/
TDBEXT::TDBEXT(EXTDEF *tdp) : TDB(tdp)
{
	Qrp = NULL;

	if (tdp) {
		TableName = tdp->Tabname;
		Schema = tdp->Tabschema;
		User = tdp->Username;
		Pwd = tdp->Password;
		Catalog = tdp->Tabcat;
		Srcdef = tdp->Srcdef;
		Qrystr = tdp->Qrystr;
		Sep = tdp->GetSep();
		Options = tdp->Options;
		Cto = tdp->Cto;
		Qto = tdp->Qto;
		Quoted = MY_MAX(0, tdp->GetQuoted());
		Rows = tdp->GetElemt();
		Memory = tdp->Memory;
		Scrollable = tdp->Scrollable;
	} else {
		TableName = NULL;
		Schema = NULL;
		User = NULL;
		Pwd = NULL;
		Catalog = NULL;
		Srcdef = NULL;
		Qrystr = NULL;
		Sep = 0;
		Options = 0;
		Cto = 0;
		Qto = 0;
		Quoted = 0;
		Rows = 0;
		Memory = 0;
		Scrollable = false;
	} // endif tdp

	Quote = NULL;
	Query = NULL;
	Count = NULL;
	//Where = NULL;
	MulConn = NULL;
	DBQ = NULL;
	Qrp = NULL;
	Fpos = 0;
	Curpos = 0;
	AftRows = 0;
	CurNum = 0;
	Rbuf = 0;
	BufSize = 0;
	Nparm = 0;
	Ncol = 0;
	Placed = false;
} // end of TDBEXT constructor

TDBEXT::TDBEXT(PTDBEXT tdbp) : TDB(tdbp)
{
	Qrp = tdbp->Qrp;
	TableName = tdbp->TableName;
	Schema = tdbp->Schema;
	User = tdbp->User;
	Pwd = tdbp->Pwd;
	Catalog = tdbp->Catalog;
	Srcdef = tdbp->Srcdef;
	Qrystr = tdbp->Qrystr;
	Sep = tdbp->Sep;
	Options = tdbp->Options;
	Cto = tdbp->Cto;
	Qto = tdbp->Qto;
	Quoted = tdbp->Quoted;
	Rows = tdbp->Rows;
	Memory = tdbp->Memory;
	Scrollable = tdbp->Scrollable;
	Quote = tdbp->Quote;
	Query = tdbp->Query;
	Count = tdbp->Count;
	//Where = tdbp->Where;
	MulConn = tdbp->MulConn;
	DBQ = tdbp->DBQ;
	Fpos = 0;
	Curpos = 0;
	AftRows = 0;
	CurNum = 0;
	Rbuf = 0;
	BufSize = tdbp->BufSize;
	Nparm = tdbp->Nparm;
	Ncol = tdbp->Ncol;
	Placed = false;
} // end of TDBEXT copy constructor

/******************************************************************/
/*  Convert an UTF-8 string to latin characters.                  */
/******************************************************************/
278
int TDBEXT::Decode(PCSZ txt, char *buf, size_t n)
Olivier Bertrand's avatar
Olivier Bertrand committed
279 280 281 282 283 284 285 286 287 288 289
{
	uint   dummy_errors;
	uint32 len = copy_and_convert(buf, n, &my_charset_latin1,
		txt, strlen(txt),
		&my_charset_utf8_general_ci,
		&dummy_errors);
	buf[len] = '\0';
	return 0;
} // end of Decode

/***********************************************************************/
290
/*  MakeSrcdef: make the SQL statement from SRDEF option.              */
Olivier Bertrand's avatar
Olivier Bertrand committed
291
/***********************************************************************/
292 293 294 295 296
bool TDBEXT::MakeSrcdef(PGLOBAL g)
{
	char *catp = strstr(Srcdef, "%s");

	if (catp) {
297
		char *fil1 = 0, *fil2;
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 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		PCSZ  ph = ((EXTDEF*)To_Def)->Phpos;

		if (!ph)
			ph = (strstr(catp + 2, "%s")) ? "WH" : "W";

		if (stricmp(ph, "H")) {
			fil1 = (To_CondFil && *To_CondFil->Body)
				? To_CondFil->Body : PlugDup(g, "1=1");
		} // endif ph

		if (stricmp(ph, "W")) {
			fil2 = (To_CondFil && To_CondFil->Having && *To_CondFil->Having)
				? To_CondFil->Having : PlugDup(g, "1=1");
		} // endif ph

		if (!stricmp(ph, "W")) {
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil1));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil1));
		} else if (!stricmp(ph, "WH")) {
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil1) + strlen(fil2));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil1, fil2));
		} else if (!stricmp(ph, "H")) {
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil2));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil2));
		} else if (!stricmp(ph, "HW")) {
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil1) + strlen(fil2));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil2, fil1));
		} else {
			strcpy(g->Message, "MakeSQL: Wrong place holders specification");
			return true;
		} // endif's ph

	} else
		Query = new(g)STRING(g, 0, Srcdef);

	return false;
} // end of MakeSrcdef

	/***********************************************************************/
	/*  MakeSQL: make the SQL statement use with remote connection.        */
	/*  TODO: when implementing remote filtering, column only used in      */
	/*  local filter should be removed from column list.                   */
	/***********************************************************************/
Olivier Bertrand's avatar
Olivier Bertrand committed
341 342
bool TDBEXT::MakeSQL(PGLOBAL g, bool cnt)
{
343 344
	PCSZ   schmp = NULL;
	char  *catp = NULL, buf[NAM_LEN * 3];
Olivier Bertrand's avatar
Olivier Bertrand committed
345
	int    len;
346
	bool   first = true;
Olivier Bertrand's avatar
Olivier Bertrand committed
347 348 349
	PTABLE tablep = To_Table;
	PCOL   colp;

350 351
	if (Srcdef)
		return MakeSrcdef(g);
Olivier Bertrand's avatar
Olivier Bertrand committed
352 353 354 355 356 357 358 359 360 361

	// Allocate the string used to contain the Query
	Query = new(g)STRING(g, 1023, "SELECT ");

	if (!cnt) {
		if (Columns) {
			// Normal SQL statement to retrieve results
			for (colp = Columns; colp; colp = colp->GetNext())
				if (!colp->IsSpecial()) {
					if (!first)
362
						Query->Append(", ");
Olivier Bertrand's avatar
Olivier Bertrand committed
363 364 365 366 367 368 369 370
					else
						first = false;

					// Column name can be encoded in UTF-8
					Decode(colp->GetName(), buf, sizeof(buf));

					if (Quote) {
						// Put column name between identifier quotes in case in contains blanks
371 372 373
						Query->Append(Quote);
						Query->Append(buf);
						Query->Append(Quote);
Olivier Bertrand's avatar
Olivier Bertrand committed
374
					} else
375
						Query->Append(buf);
Olivier Bertrand's avatar
Olivier Bertrand committed
376 377 378 379 380 381 382

					((PEXTCOL)colp)->SetRank(++Ncol);
				} // endif colp

		} else
			// !Columns can occur for queries such that sql count(*) from...
			// for which we will count the rows from sql * from...
383
			Query->Append('*');
Olivier Bertrand's avatar
Olivier Bertrand committed
384 385 386

	} else
		// SQL statement used to retrieve the size of the result
387
		Query->Append("count(*)");
Olivier Bertrand's avatar
Olivier Bertrand committed
388

389
	Query->Append(" FROM ");
Olivier Bertrand's avatar
Olivier Bertrand committed
390 391 392 393 394 395 396 397 398 399 400

	if (Catalog && *Catalog)
		catp = Catalog;

	//if (tablep->GetSchema())
	//	schmp = (char*)tablep->GetSchema();
	//else 
	if (Schema && *Schema)
		schmp = Schema;

	if (catp) {
401
		Query->Append(catp);
Olivier Bertrand's avatar
Olivier Bertrand committed
402 403

		if (schmp) {
404 405
			Query->Append('.');
			Query->Append(schmp);
Olivier Bertrand's avatar
Olivier Bertrand committed
406 407
		} // endif schmp

408
		Query->Append('.');
Olivier Bertrand's avatar
Olivier Bertrand committed
409
	} else if (schmp) {
410 411
		Query->Append(schmp);
		Query->Append('.');
Olivier Bertrand's avatar
Olivier Bertrand committed
412 413 414 415 416 417 418
	} // endif schmp

	// Table name can be encoded in UTF-8
	Decode(TableName, buf, sizeof(buf));

	if (Quote) {
		// Put table name between identifier quotes in case in contains blanks
419 420 421
		Query->Append(Quote);
		Query->Append(buf);
		Query->Append(Quote);
Olivier Bertrand's avatar
Olivier Bertrand committed
422
	} else
423
		Query->Append(buf);
Olivier Bertrand's avatar
Olivier Bertrand committed
424 425 426 427 428

	len = Query->GetLength();

	if (To_CondFil) {
		if (Mode == MODE_READ) {
429 430
			Query->Append(" WHERE ");
			Query->Append(To_CondFil->Body);
Olivier Bertrand's avatar
Olivier Bertrand committed
431 432 433 434 435 436 437
			len = Query->GetLength() + 1;
		} else
			len += (strlen(To_CondFil->Body) + 256);

	} else
		len += ((Mode == MODE_READX) ? 256 : 1);

438
	if (Query->IsTruncated()) {
Olivier Bertrand's avatar
Olivier Bertrand committed
439 440
		strcpy(g->Message, "MakeSQL: Out of memory");
		return true;
441 442
	} else
		Query->Resize(len);
Olivier Bertrand's avatar
Olivier Bertrand committed
443

444
	if (trace(33))
Olivier Bertrand's avatar
Olivier Bertrand committed
445 446 447 448 449
		htrc("Query=%s\n", Query->GetStr());

	return false;
} // end of MakeSQL

450 451 452 453 454 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
/***********************************************************************/
/*  Remove the NAME_CONST functions that are added by procedures.      */
/***********************************************************************/
void TDBEXT::RemoveConst(PGLOBAL g, char *stmt)
{
	char *p, *p2;
	char  val[1025], nval[1025];
	int   n, nc;

	while ((p = strstr(stmt, "NAME_CONST")))
		if ((n = sscanf(p, "%*[^,],%1024[^)])%n", val, &nc))) {
			if (trace(33))
				htrc("p=%s\nn=%d val=%s nc=%d\n", p, n, val, nc);

			*p = 0;

			if ((p2 = strstr(val, "'"))) {
				if ((n = sscanf(p2, "%*['\\]%1024[^'\\]", nval))) {
					if (trace(33))
						htrc("p2=%s\nn=%d nval=%s\n", p2, n, nval);

					strcat(strcat(strcat(strcat(stmt, "'"), nval), "'"), p + nc);
				} else
					break;

			} else
				strcat(strcat(strcat(strcat(stmt, "("), val), ")"), p + nc);

			if (trace(33))
				htrc("stmt=%s\n", stmt);

		} else
			break;

		return;
} // end of RemoveConst

Olivier Bertrand's avatar
Olivier Bertrand committed
487 488 489 490 491 492
/***********************************************************************/
/*  MakeCommand: make the Update or Delete statement to send to the    */
/*  MySQL server. Limited to remote values and filtering.              */
/***********************************************************************/
bool TDBEXT::MakeCommand(PGLOBAL g)
{
493 494
	PCSZ  schmp = NULL;
	char *p, *stmt, name[132], *body = NULL;
Olivier Bertrand's avatar
Olivier Bertrand committed
495 496
	char *qrystr = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 1);
	bool  qtd = Quoted > 0;
497
	char  q = qtd ? *Quote : ' ';
Olivier Bertrand's avatar
Olivier Bertrand committed
498 499 500 501 502
	int   i = 0, k = 0;

	// Make a lower case copy of the originale query and change
	// back ticks to the data source identifier quoting character
	do {
503
		qrystr[i] = (Qrystr[i] == '`') ? q : tolower(Qrystr[i]);
Olivier Bertrand's avatar
Olivier Bertrand committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	} while (Qrystr[i++]);

	if (To_CondFil && (p = strstr(qrystr, " where "))) {
		p[7] = 0;           // Remove where clause
		Qrystr[(p - qrystr) + 7] = 0;
		body = To_CondFil->Body;
		stmt = (char*)PlugSubAlloc(g, NULL, strlen(qrystr)
			+ strlen(body) + 64);
	} else
		stmt = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);

	// Check whether the table name is equal to a keyword
	// If so, it must be quoted in the original query
	strlwr(strcat(strcat(strcpy(name, " "), Name), " "));

	if (strstr(" update delete low_priority ignore quick from ", name)) {
520 521 522 523 524 525 526 527
		if (Quote) {
			strlwr(strcat(strcat(strcpy(name, Quote), Name), Quote));
			k += 2;
		} else {
			strcpy(g->Message, "Quoted must be specified");
			return true;
		}	// endif Quote

Olivier Bertrand's avatar
Olivier Bertrand committed
528 529 530 531 532
	} else
		strlwr(strcpy(name, Name));     // Not a keyword

	if ((p = strstr(qrystr, name))) {
		for (i = 0; i < p - qrystr; i++)
533
			stmt[i] = (Qrystr[i] == '`') ? q : Qrystr[i];
Olivier Bertrand's avatar
Olivier Bertrand committed
534 535

		stmt[i] = 0;
536

Olivier Bertrand's avatar
Olivier Bertrand committed
537 538
		k += i + (int)strlen(Name);

539 540 541 542 543 544 545
		if (Schema && *Schema)
			schmp = Schema;

		if (qtd && *(p - 1) == ' ') {
			if (schmp)
				strcat(strcat(stmt, schmp), ".");

Olivier Bertrand's avatar
Olivier Bertrand committed
546
			strcat(strcat(strcat(stmt, Quote), TableName), Quote);
547 548 549 550 551 552 553 554 555 556
		} else {
			if (schmp) {
				if (qtd && *(p - 1) != ' ') {
					stmt[i - 1] = 0;
					strcat(strcat(strcat(stmt, schmp), "."), Quote);
				} else
					strcat(strcat(stmt, schmp), ".");

			}	// endif schmp

Olivier Bertrand's avatar
Olivier Bertrand committed
557
			strcat(stmt, TableName);
558
		} // endif's
Olivier Bertrand's avatar
Olivier Bertrand committed
559 560 561 562

		i = (int)strlen(stmt);

		do {
563
			stmt[i++] = (Qrystr[k] == '`') ? q : Qrystr[k];
Olivier Bertrand's avatar
Olivier Bertrand committed
564 565
		} while (Qrystr[k++]);

566 567
		RemoveConst(g, stmt);

Olivier Bertrand's avatar
Olivier Bertrand committed
568 569 570 571 572 573 574 575 576
		if (body)
			strcat(stmt, body);

	} else {
		sprintf(g->Message, "Cannot use this %s command",
			(Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
		return true;
	} // endif p

577
	if (trace(33))
Olivier Bertrand's avatar
Olivier Bertrand committed
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
		htrc("Command=%s\n", stmt);

	Query = new(g)STRING(g, 0, stmt);
	return (!Query->GetSize());
} // end of MakeCommand

/***********************************************************************/
/*  GetRecpos: return the position of last read record.                */
/***********************************************************************/
int TDBEXT::GetRecpos(void)
{
	return Fpos;
} // end of GetRecpos

/***********************************************************************/
/*  ODBC GetMaxSize: returns table size estimate in number of lines.   */
/***********************************************************************/
int TDBEXT::GetMaxSize(PGLOBAL g)
{
	if (MaxSize < 0) {
		if (Mode == MODE_DELETE)
			// Return 0 in mode DELETE in case of delete all.
			MaxSize = 0;
		else if (!Cardinality(NULL))
			MaxSize = 10;   // To make MySQL happy
		else if ((MaxSize = Cardinality(g)) < 0)
			MaxSize = 12;   // So we can see an error occurred

	} // endif MaxSize

	return MaxSize;
} // end of GetMaxSize

/***********************************************************************/
/*  Return max size value.                                             */
/***********************************************************************/
int TDBEXT::GetProgMax(PGLOBAL g)
{
	return GetMaxSize(g);
} // end of GetProgMax

/* ---------------------------EXTCOL class --------------------------- */

/***********************************************************************/
/*  EXTCOL public constructor.                                         */
/***********************************************************************/
624
EXTCOL::EXTCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PCSZ am)
Olivier Bertrand's avatar
Olivier Bertrand committed
625 626 627 628 629 630 631 632 633 634
	: COLBLK(cdp, tdbp, i)
{
	if (cprec) {
		Next = cprec->GetNext();
		cprec->SetNext(this);
	} else {
		Next = tdbp->GetColumns();
		tdbp->SetColumns(this);
	} // endif cprec

635
	if (trace(1))
Olivier Bertrand's avatar
Olivier Bertrand committed
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 669 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 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);

	// Set additional remote access method information for column.
	Crp = NULL;
	Long = Precision;
	To_Val = NULL;
	Bufp = NULL;
	Blkp = NULL;
	Rank = 0;           // Not known yet
} // end of JDBCCOL constructor

/***********************************************************************/
/*  EXTCOL private constructor.                                        */
/***********************************************************************/
EXTCOL::EXTCOL(void) : COLBLK()
{
	Crp = NULL;
	Buf_Type = TYPE_INT;     // This is a count(*) column

	// Set additional Dos access method information for column.
	Long = sizeof(int);
	To_Val = NULL;
	Bufp = NULL;
	Blkp = NULL;
	Rank = 1;
} // end of EXTCOL constructor

/***********************************************************************/
/*  EXTCOL constructor used for copying columns.                       */
/*  tdbp is the pointer to the new table descriptor.                   */
/***********************************************************************/
EXTCOL::EXTCOL(PEXTCOL col1, PTDB tdbp) : COLBLK(col1, tdbp)
{
	Crp = col1->Crp;
	Long = col1->Long;
	To_Val = col1->To_Val;
	Bufp = col1->Bufp;
	Blkp = col1->Blkp;
	Rank = col1->Rank;
} // end of JDBCCOL copy constructor

/***********************************************************************/
/*  SetBuffer: prepare a column block for write operation.             */
/***********************************************************************/
bool EXTCOL::SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check)
{
	if (!(To_Val = value)) {
		sprintf(g->Message, MSG(VALUE_ERROR), Name);
		return true;
	} else if (Buf_Type == value->GetType()) {
		// Values are of the (good) column type
		if (Buf_Type == TYPE_DATE) {
			// If any of the date values is formatted
			// output format must be set for the receiving table
			if (GetDomain() || ((DTVAL *)value)->IsFormatted())
				goto newval;          // This will make a new value;

		} else if (Buf_Type == TYPE_DOUBLE)
			// Float values must be written with the correct (column) precision
			// Note: maybe this should be forced by ShowValue instead of this ?
			value->SetPrec(GetScale());

		Value = value;            // Directly access the external value
	} else {
		// Values are not of the (good) column type
		if (check) {
			sprintf(g->Message, MSG(TYPE_VALUE_ERR), Name,
				GetTypeName(Buf_Type), GetTypeName(value->GetType()));
			return true;
		} // endif check

	newval:
		if (InitValue(g))         // Allocate the matching value block
			return true;

	} // endif's Value, Buf_Type

	// Because Colblk's have been made from a copy of the original TDB in
	// case of Update, we must reset them to point to the original one.
	if (To_Tdb->GetOrig())
		To_Tdb = (PTDB)To_Tdb->GetOrig();

	// Set the Column
	Status = (ok) ? BUF_EMPTY : BUF_NO;
	return false;
} // end of SetBuffer