tabext.cpp 22 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

	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);
162 163 164
	Qchar = GetStringCatInfo(g,"Qchar", NULL);
  if (Qchar && !Quoted)
    Quoted = 1;
Olivier Bertrand's avatar
Olivier Bertrand committed
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
	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());
204
		Quote = tdp->GetQchar();
Olivier Bertrand's avatar
Olivier Bertrand committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		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;
221
		Quote = NULL;
Olivier Bertrand's avatar
Olivier Bertrand committed
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
		Rows = 0;
		Memory = 0;
		Scrollable = false;
	} // endif tdp

	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;
259
	Quote = tdbp->Quote;
Olivier Bertrand's avatar
Olivier Bertrand committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	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.                  */
/******************************************************************/
283
int TDBEXT::Decode(PCSZ txt, char *buf, size_t n)
Olivier Bertrand's avatar
Olivier Bertrand committed
284 285 286 287
{
	uint   dummy_errors;
	uint32 len = copy_and_convert(buf, n, &my_charset_latin1,
		txt, strlen(txt),
288
		&my_charset_utf8mb3_general_ci,
Olivier Bertrand's avatar
Olivier Bertrand committed
289 290 291 292 293
		&dummy_errors);
	buf[len] = '\0';
	return 0;
} // end of Decode

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
/*
  Count number of %s placeholders in string.
  Returns -1 if other sprintf placeholders are found, .g %d
*/
static int count_placeholders(const char *fmt)
{
  int cnt= 0;
  for (const char *p=fmt; *p; p++)
  {
    if (*p == '%')
    {
      switch (p[1])
      {
      case 's':
        /* %s found */
        cnt++;
        p++;
        break;
      case '%':
        /* masking char for % found */
        p++;
        break;
      default:
        /* some other placeholder found */
        return -1;
      }
    }
  }
  return cnt;
}

Olivier Bertrand's avatar
Olivier Bertrand committed
325
/***********************************************************************/
326
/*  MakeSrcdef: make the SQL statement from SRDEF option.              */
Olivier Bertrand's avatar
Olivier Bertrand committed
327
/***********************************************************************/
328 329 330 331 332
bool TDBEXT::MakeSrcdef(PGLOBAL g)
{
	char *catp = strstr(Srcdef, "%s");

	if (catp) {
333
		char *fil1 = 0, *fil2;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
		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

349 350 351 352 353 354 355 356
		int n_placeholders = count_placeholders(Srcdef);
		if (n_placeholders < 0)
		{
			strcpy(g->Message, "MakeSQL: Wrong place holders specification");
			return true;
		}

		if (!stricmp(ph, "W") && n_placeholders <= 1) {
357 358
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil1));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil1));
359 360 361
		}
		else if (!stricmp(ph, "WH") && n_placeholders <= 2)
		{
362 363
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil1) + strlen(fil2));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil1, fil2));
364 365 366
		}
		else if (!stricmp(ph, "H") && n_placeholders <= 1)
		{
367 368
			Query = new(g)STRING(g, strlen(Srcdef) + strlen(fil2));
			Query->SetLength(sprintf(Query->GetStr(), Srcdef, fil2));
369 370 371
		}
		else if (!stricmp(ph, "HW") && n_placeholders <= 2)
		{
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
			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
390 391
bool TDBEXT::MakeSQL(PGLOBAL g, bool cnt)
{
392 393
	PCSZ   schmp = NULL;
	char  *catp = NULL, buf[NAM_LEN * 3];
Olivier Bertrand's avatar
Olivier Bertrand committed
394
	int    len;
395
	bool   first = true;
Olivier Bertrand's avatar
Olivier Bertrand committed
396
	PCOL   colp;
397 398
	char *res= NULL, *my_schema_table= NULL;
	size_t my_len= 0;
Olivier Bertrand's avatar
Olivier Bertrand committed
399

400 401
	if (Srcdef)
		return MakeSrcdef(g);
Olivier Bertrand's avatar
Olivier Bertrand committed
402 403 404 405 406 407 408 409 410 411

	// 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)
412
						Query->Append(", ");
Olivier Bertrand's avatar
Olivier Bertrand committed
413 414 415 416 417 418 419 420
					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
421 422 423
						Query->Append(Quote);
						Query->Append(buf);
						Query->Append(Quote);
Olivier Bertrand's avatar
Olivier Bertrand committed
424
					} else
425
						Query->Append(buf);
Olivier Bertrand's avatar
Olivier Bertrand committed
426 427 428 429 430 431 432

					((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...
433
			Query->Append('*');
Olivier Bertrand's avatar
Olivier Bertrand committed
434 435 436

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

439
	Query->Append(" FROM ");
Olivier Bertrand's avatar
Olivier Bertrand committed
440 441 442 443 444 445 446 447 448 449 450

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

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

	if (catp) {
451
		Query->Append(catp);
Olivier Bertrand's avatar
Olivier Bertrand committed
452 453

		if (schmp) {
454 455
			Query->Append('.');
			Query->Append(schmp);
Olivier Bertrand's avatar
Olivier Bertrand committed
456 457
		} // endif schmp

458
		Query->Append('.');
Olivier Bertrand's avatar
Olivier Bertrand committed
459
	} else if (schmp) {
460 461
		Query->Append(schmp);
		Query->Append('.');
Olivier Bertrand's avatar
Olivier Bertrand committed
462 463 464 465 466 467
	} // endif schmp

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

	if (Quote) {
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
		// Tabname can have both database and table identifiers, we need to parse
		if (res= strstr(buf, "."))
		{
			// Parse schema
			my_len= res - buf + 1;
			my_schema_table= (char *) malloc(my_len);
			memcpy(my_schema_table, buf, my_len - 1);
			my_schema_table[my_len] = 0;
			Query->Append(Quote);
			Query->Append(my_schema_table);
			Query->Append(Quote);
			free(my_schema_table);
			Query->Append(".");
			// Parse table
			my_len= strlen(buf) - my_len + 1;
			my_schema_table= (char *) malloc(my_len);
			memcpy(my_schema_table, ++res, my_len);
			my_schema_table[my_len] = 0;
			Query->Append(Quote);
			Query->Append(my_schema_table);
			Query->Append(Quote);
			free(my_schema_table);
		}
		else
		{
			// Put table name between identifier quotes in case in contains blanks
			Query->Append(Quote);
			Query->Append(buf);
			Query->Append(Quote);
		}

Olivier Bertrand's avatar
Olivier Bertrand committed
499
	} else
500
		Query->Append(buf);
Olivier Bertrand's avatar
Olivier Bertrand committed
501 502 503 504 505

	len = Query->GetLength();

	if (To_CondFil) {
		if (Mode == MODE_READ) {
506 507
			Query->Append(" WHERE ");
			Query->Append(To_CondFil->Body);
Olivier Bertrand's avatar
Olivier Bertrand committed
508 509 510 511 512 513 514
			len = Query->GetLength() + 1;
		} else
			len += (strlen(To_CondFil->Body) + 256);

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

515
	if (Query->IsTruncated()) {
Olivier Bertrand's avatar
Olivier Bertrand committed
516 517
		strcpy(g->Message, "MakeSQL: Out of memory");
		return true;
518 519
	} else
		Query->Resize(len);
Olivier Bertrand's avatar
Olivier Bertrand committed
520

521
	if (trace(33))
Olivier Bertrand's avatar
Olivier Bertrand committed
522 523 524 525 526
		htrc("Query=%s\n", Query->GetStr());

	return false;
} // end of MakeSQL

527 528 529 530 531 532 533 534 535 536
/***********************************************************************/
/*  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")))
537
        {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
		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;
561 562
        }
	return;
563 564
} // end of RemoveConst

Olivier Bertrand's avatar
Olivier Bertrand committed
565 566 567 568 569 570
/***********************************************************************/
/*  MakeCommand: make the Update or Delete statement to send to the    */
/*  MySQL server. Limited to remote values and filtering.              */
/***********************************************************************/
bool TDBEXT::MakeCommand(PGLOBAL g)
{
571 572
	PCSZ  schmp = NULL;
	char *p, *stmt, name[132], *body = NULL;
Olivier Bertrand's avatar
Olivier Bertrand committed
573 574
	char *qrystr = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 1);
	bool  qtd = Quoted > 0;
575
	char  q = qtd ? *Quote : ' ';
Olivier Bertrand's avatar
Olivier Bertrand committed
576 577 578 579 580
	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 {
581
		qrystr[i] = (Qrystr[i] == '`') ? q : tolower(Qrystr[i]);
Olivier Bertrand's avatar
Olivier Bertrand committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	} 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)) {
598 599 600 601 602 603 604 605
		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
606 607 608 609 610
	} else
		strlwr(strcpy(name, Name));     // Not a keyword

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

		stmt[i] = 0;
614

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

617 618 619 620 621 622 623
		if (Schema && *Schema)
			schmp = Schema;

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

Olivier Bertrand's avatar
Olivier Bertrand committed
624
			strcat(strcat(strcat(stmt, Quote), TableName), Quote);
625 626 627 628 629 630 631 632 633 634
		} 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
635
			strcat(stmt, TableName);
636
		} // endif's
Olivier Bertrand's avatar
Olivier Bertrand committed
637 638 639 640

		i = (int)strlen(stmt);

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

644 645
		RemoveConst(g, stmt);

Olivier Bertrand's avatar
Olivier Bertrand committed
646 647 648 649
		if (body)
			strcat(stmt, body);

	} else {
650
		snprintf(g->Message, sizeof(g->Message), "Cannot use this %s command",
Olivier Bertrand's avatar
Olivier Bertrand committed
651 652 653 654
			(Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
		return true;
	} // endif p

655
	if (trace(33))
Olivier Bertrand's avatar
Olivier Bertrand committed
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
		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.                                         */
/***********************************************************************/
702
EXTCOL::EXTCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PCSZ am)
Olivier Bertrand's avatar
Olivier Bertrand committed
703 704 705 706 707 708 709 710 711 712
	: COLBLK(cdp, tdbp, i)
{
	if (cprec) {
		Next = cprec->GetNext();
		cprec->SetNext(this);
	} else {
		Next = tdbp->GetColumns();
		tdbp->SetColumns(this);
	} // endif cprec

713
	if (trace(1))
Olivier Bertrand's avatar
Olivier Bertrand committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		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)) {
761
		snprintf(g->Message, sizeof(g->Message), MSG(VALUE_ERROR), Name);
Olivier Bertrand's avatar
Olivier Bertrand committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		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) {
780
			snprintf(g->Message, sizeof(g->Message), MSG(TYPE_VALUE_ERR), Name,
Olivier Bertrand's avatar
Olivier Bertrand committed
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
				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