tabtbl.cpp 25.1 KB
Newer Older
Alexander Barkov's avatar
Alexander Barkov committed
1 2 3
/************* TabTbl C++ Program Source Code File (.CPP) **************/
/* PROGRAM NAME: TABTBL                                                */
/* -------------                                                       */
4
/*  Version 1.6                                                        */
Alexander Barkov's avatar
Alexander Barkov committed
5 6 7
/*                                                                     */
/* COPYRIGHT:                                                          */
/* ----------                                                          */
8
/*  (C) Copyright to PlugDB Software Development          2008-2013    */
Alexander Barkov's avatar
Alexander Barkov committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*  Author: Olivier BERTRAND                                           */
/*                                                                     */
/* WHAT THIS PROGRAM DOES:                                             */
/* -----------------------                                             */
/*  This program are the TDBTBL class DB routines.                     */
/*                                                                     */
/* WHAT YOU NEED TO COMPILE THIS PROGRAM:                              */
/* --------------------------------------                              */
/*                                                                     */
/*  REQUIRED FILES:                                                    */
/*  ---------------                                                    */
/*    TABTBL.CPP     - Source code                                     */
/*    PLGDBSEM.H     - DB application declaration file                 */
/*    TABDOS.H       - TABDOS classes declaration file                 */
/*    TABTBL.H       - TABTBL classes declaration file                 */
/*    GLOBAL.H       - Global declaration file                         */
/*                                                                     */
/*  REQUIRED LIBRARIES:                                                */
/*  -------------------                                                */
/*    Large model C library                                            */
/*                                                                     */
/*  REQUIRED PROGRAMS:                                                 */
/*  ------------------                                                 */
/*    IBM, Borland, GNU or Microsoft C++ Compiler and Linker           */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/*  Include relevant section of system dependant header files.         */
/***********************************************************************/
//#include "sql_base.h"
#include "my_global.h"
#if defined(WIN32)
#include <stdlib.h>
#include <stdio.h>
#if defined(__BORLANDC__)
#define __MFC_COMPAT__                   // To define min/max as macro
#endif
//#include <windows.h>
#else
#if defined(UNIX)
#include <fnmatch.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "osutil.h"
#else
//#include <io.h>
#endif
//#include <fcntl.h>
#endif

/***********************************************************************/
/*  Include application header files:                                  */
/***********************************************************************/
#include "table.h"       // MySQL table definitions
#include "global.h"      // global declarations
#include "plgdbsem.h"    // DB application declarations
#include "reldef.h"      // DB definition declares
//#include "filter.h"      // FILTER classes dcls
#include "filamtxt.h"
#include "tabcol.h"
#include "tabdos.h"      // TDBDOS and DOSCOL class dcls
73
#include "tabtbl.h"
74 75 76
#if defined(MYSQL_SUPPORT)
#include "tabmysql.h"
#endif   // MYSQL_SUPPORT
Alexander Barkov's avatar
Alexander Barkov committed
77 78 79
#include "ha_connect.h"
#include "mycat.h"       // For GetHandler

80 81 82 83 84 85 86 87 88
#if defined(WIN32)
#if defined(__BORLANDC__)
#define SYSEXIT void _USERENTRY
#else
#define SYSEXIT void
#endif
#else   // !WIN32
#define SYSEXIT void *
#endif  // !WIN32
89

Alexander Barkov's avatar
Alexander Barkov committed
90 91 92 93 94 95 96 97 98
extern "C" int trace;

/* ---------------------------- Class TBLDEF ---------------------------- */

/**************************************************************************/
/*  Constructor.                                                          */
/**************************************************************************/
TBLDEF::TBLDEF(void)
  {
99
//To_Tables = NULL;
100 101 102
  Accept = false;
  Thread = false;
  Maxerr = 0;
Alexander Barkov's avatar
Alexander Barkov committed
103 104 105 106 107 108 109 110 111 112 113 114
  Ntables = 0;
  Pseudo = 3;
  } // end of TBLDEF constructor

/**************************************************************************/
/*  DefineAM: define specific AM block values from XDB file.              */
/**************************************************************************/
bool TBLDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
  {
  char   *tablist, *dbname;

  Desc = "Table list table";
115
  tablist = Cat->GetStringCatInfo(g, "Tablist", "");
116
  dbname = Cat->GetStringCatInfo(g, "Dbname", "*");
Alexander Barkov's avatar
Alexander Barkov committed
117 118 119
  Ntables = 0;

  if (*tablist) {
120 121
    char  *p, *pn, *pdb;
    PTABLE tbl;
Alexander Barkov's avatar
Alexander Barkov committed
122 123 124 125 126

    for (pdb = tablist; ;) {
      if ((p = strchr(pdb, ',')))
        *p = 0;

127
      // Analyze the table name, it may have the format:
Alexander Barkov's avatar
Alexander Barkov committed
128 129 130 131 132 133 134 135 136
      // [dbname.]tabname
      if ((pn = strchr(pdb, '.'))) {
        *pn++ = 0;
      } else {
        pn = pdb;
        pdb = dbname;
      } // endif p

      // Allocate the TBLIST block for that table
137 138
      tbl = new(g) XTAB(pn);
      tbl->SetQualifier(pdb);
Alexander Barkov's avatar
Alexander Barkov committed
139 140
      
      if (trace)
141
        htrc("TBL: Name=%s db=%s\n", tbl->GetName(), tbl->GetQualifier());
Alexander Barkov's avatar
Alexander Barkov committed
142 143

      // Link the blocks
144 145 146 147 148
      if (Tablep)
        Tablep->Link(tbl);
      else
        Tablep = tbl;

Alexander Barkov's avatar
Alexander Barkov committed
149 150 151 152 153 154 155 156 157
      Ntables++;

      if (p)
        pdb = pn + strlen(pn) + 1;
      else
        break;

      } // endfor pdb

158
    Maxerr = Cat->GetIntCatInfo("Maxerr", 0);
159 160 161
    Accept = Cat->GetBoolCatInfo("Accept", false);
    Thread = Cat->GetBoolCatInfo("Thread", false);
    } // endif tablist
Alexander Barkov's avatar
Alexander Barkov committed
162 163 164 165 166 167 168 169 170

  return FALSE;
  } // end of DefineAM

/***********************************************************************/
/*  GetTable: makes a new Table Description Block.                     */
/***********************************************************************/
PTDB TBLDEF::GetTable(PGLOBAL g, MODE m)
  {
171 172
  if (Catfunc == FNC_COL)
    return new(g) TDBTBC(this);
173 174
  else if (Thread)
    return new(g) TDBTBM(this);
175 176
  else
    return new(g) TDBTBL(this);
Alexander Barkov's avatar
Alexander Barkov committed
177 178 179 180 181 182 183 184

  } // end of GetTable

/* ------------------------- Class TDBTBL ---------------------------- */

/***********************************************************************/
/*  TDBTBL constructors.                                               */
/***********************************************************************/
185
TDBTBL::TDBTBL(PTBLDEF tdp) : TDBPRX(tdp)
Alexander Barkov's avatar
Alexander Barkov committed
186 187 188
  {
  Tablist = NULL;
  CurTable = NULL;
189
//Tdbp = NULL;
Alexander Barkov's avatar
Alexander Barkov committed
190 191
  Accept = tdp->Accept;
  Maxerr = tdp->Maxerr;
192
  Nbc = 0;
Alexander Barkov's avatar
Alexander Barkov committed
193 194 195 196 197 198 199 200 201 202 203
  Rows = 0;
  Crp = 0;
//  NTables = 0;
//  iTable = 0;
  } // end of TDBTBL standard constructor

/***********************************************************************/
/*  Allocate TBL column description block.                             */
/***********************************************************************/
PCOL TDBTBL::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
  {
204
  return new(g) PRXCOL(cdp, this, cprec, n);
Alexander Barkov's avatar
Alexander Barkov committed
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
  } // end of MakeCol

/***********************************************************************/
/*  InsertSpecialColumn: Put a special column ahead of the column list.*/
/***********************************************************************/
PCOL TDBTBL::InsertSpecialColumn(PGLOBAL g, PCOL scp)
  {
  PCOL colp;

  if (!scp->IsSpecial())
    return NULL;

  if (scp->GetAmType() == TYPE_AM_TABID)
    // This special column is handled locally
    colp = new((TIDBLK*)scp) TBTBLK(scp->GetValue());
  else  // Other special columns are treated normally
    colp = scp;

  colp->SetNext(Columns);
  Columns = colp;
  return colp;
  } // end of InsertSpecialColumn

/***********************************************************************/
/*  Initializes the table table list.                                  */
/***********************************************************************/
bool TDBTBL::InitTableList(PGLOBAL g)
  {
Olivier Bertrand's avatar
Olivier Bertrand committed
233
  int     n;
234
  PTABLE  tp, tabp;
Alexander Barkov's avatar
Alexander Barkov committed
235 236 237 238 239
  PCOL    colp;
  PTBLDEF tdp = (PTBLDEF)To_Def;

//  PlugSetPath(filename, Tdbp->GetFile(g), Tdbp->GetPath());

240 241 242
  for (n = 0, tp = tdp->Tablep; tp; tp = tp->GetNext()) {
    if (TestFil(g, To_Filter, tp)) {
      tabp = new(g) XTAB(tp);
Alexander Barkov's avatar
Alexander Barkov committed
243 244

      // Get the table description block of this table
245
      if (!(Tdbp = GetSubTable(g, tabp))) {
246
        if (++Nbc > Maxerr)
Alexander Barkov's avatar
Alexander Barkov committed
247 248 249 250
          return TRUE;               // Error return
        else
          continue;                  // Skip this table

251 252
      } else
        RemoveNext(tabp);            // To avoid looping
Alexander Barkov's avatar
Alexander Barkov committed
253 254 255 256

      // We must allocate subtable columns before GetMaxSize is called
      // because some (PLG, ODBC?) need to have their columns attached.
      // Real initialization will be done later.
257 258 259
      for (colp = Columns; colp; colp = colp->GetNext())
        if (!colp->IsSpecial())
          if (((PPRXCOL)colp)->Init(g) && !Accept)
Olivier Bertrand's avatar
Olivier Bertrand committed
260
            return TRUE;
Alexander Barkov's avatar
Alexander Barkov committed
261 262 263 264 265 266 267 268 269

      if (Tablist)
        Tablist->Link(tabp);
      else
        Tablist = tabp;

      n++;
      } // endif filp

270
    } // endfor tp
Alexander Barkov's avatar
Alexander Barkov committed
271 272 273 274 275 276 277 278 279

//NumTables = n;
  To_Filter = NULL;        // To avoid doing it several times
  return FALSE;
  } // end of InitTableList

/***********************************************************************/
/*  Test the tablename against the pseudo "local" filter.              */
/***********************************************************************/
280
bool TDBTBL::TestFil(PGLOBAL g, PFIL filp, PTABLE tabp)
Alexander Barkov's avatar
Alexander Barkov committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  {
  char *fil, op[8], tn[NAME_LEN];
  bool  neg;

  if (!filp)
    return TRUE;
  else if (strstr(filp, " OR ") || strstr(filp, " AND "))
    return TRUE;               // Not handled yet
  else
    fil = filp + (*filp == '(' ? 1 : 0);

  if (sscanf(fil, "TABID %s", op) != 1)
    return TRUE;               // ignore invalid filter

  if ((neg = !strcmp(op, "NOT")))
    strcpy(op, "IN");

  if (!strcmp(op, "=")) {
    // Temporarily, filter must be "TABID = 'value'" only
    if (sscanf(fil, "TABID = '%[^']'", tn) != 1)
      return TRUE;             // ignore invalid filter

303
    return !stricmp(tn, tabp->GetName());
Alexander Barkov's avatar
Alexander Barkov committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  } else if (!strcmp(op, "IN")) {
    char *p, *tnl = (char*)PlugSubAlloc(g, NULL, strlen(fil) - 10);
    int   n;

    if (neg)
      n = sscanf(fil, "TABID NOT IN (%[^)])", tnl);
    else
      n = sscanf(fil, "TABID IN (%[^)])", tnl);

    if (n != 1)
      return TRUE;             // ignore invalid filter

    while (tnl) {
      if ((p = strchr(tnl, ',')))
        *p++ = 0;

      if (sscanf(tnl, "'%[^']'", tn) != 1)
        return TRUE;           // ignore invalid filter
322
      else if (!stricmp(tn, tabp->GetName()))
Alexander Barkov's avatar
Alexander Barkov committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        return !neg;           // Found

      tnl = p;
      } // endwhile

    return neg;                // Not found
  } // endif op

  return TRUE;                 // invalid operator
  } // end of TestFil

/***********************************************************************/
/*  Sum up the sizes of all sub-tables.                                */
/***********************************************************************/
int TDBTBL::GetMaxSize(PGLOBAL g)
  {
  if (MaxSize < 0) {
340
    int mxsz;
Alexander Barkov's avatar
Alexander Barkov committed
341 342 343 344

    if (!Tablist && InitTableList(g))
      return 0;               // Cannot be calculated at this stage

345
    MaxSize = 0;
Alexander Barkov's avatar
Alexander Barkov committed
346

347 348
    for (PTABLE tabp = Tablist; tabp; tabp = tabp->GetNext()) {
      if ((mxsz = tabp->GetTo_Tdb()->GetMaxSize(g)) < 0) {
Alexander Barkov's avatar
Alexander Barkov committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        MaxSize = -1;
        return mxsz;
        } // endif mxsz

      MaxSize += mxsz;
      } // endfor i

    } // endif MaxSize

  return MaxSize;
  } // end of GetMaxSize

/***********************************************************************/
/*  Reset read/write position values.                                  */
/***********************************************************************/
void TDBTBL::ResetDB(void)
  {
  for (PCOL colp = Columns; colp; colp = colp->GetNext())
    if (colp->GetAmType() == TYPE_AM_TABID)
      colp->COLBLK::Reset();

370 371
  for (PTABLE tabp = Tablist; tabp; tabp = tabp->GetNext())
    ((PTDBASE)tabp->GetTo_Tdb())->ResetDB();
Alexander Barkov's avatar
Alexander Barkov committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  Tdbp = (PTDBASE)Tablist->GetTo_Tdb();
  Crp = 0;
  } // end of ResetDB

/***********************************************************************/
/*  Returns RowId if b is false or Rownum if b is true.                */
/***********************************************************************/
int TDBTBL::RowNumber(PGLOBAL g, bool b)
  {
  return Tdbp->RowNumber(g) + ((b) ? 0 : Rows);
  } // end of RowNumber

/***********************************************************************/
/*  TBL Access Method opening routine.                                 */
/*  Open first file, other will be opened sequencially when reading.   */
/***********************************************************************/
bool TDBTBL::OpenDB(PGLOBAL g)
  {
  if (trace)
    htrc("TBL OpenDB: tdbp=%p tdb=R%d use=%d key=%p mode=%d\n",
                      this, Tdb_No, Use, To_Key_Col, Mode);

  if (Use == USE_OPEN) {
    /*******************************************************************/
    /*  Table already open, replace it at its beginning.               */
    /*******************************************************************/
    ResetDB();
    return Tdbp->OpenDB(g);  // Re-open fist table
    } // endif use

  /*********************************************************************/
  /*  When GetMaxsize was called, To_Filter was not set yet.           */
  /*********************************************************************/
  if (To_Filter && Tablist) {
    Tablist = NULL;
408
    Nbc = 0;
Alexander Barkov's avatar
Alexander Barkov committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    } // endif To_Filter

  /*********************************************************************/
  /*  Open the first table of the list.                                */
  /*********************************************************************/
  if (!Tablist && InitTableList(g))     //  done in GetMaxSize
    return TRUE;

  if ((CurTable = Tablist)) {
    Tdbp = (PTDBASE)CurTable->GetTo_Tdb();
    Tdbp->SetMode(Mode);
//  Tdbp->ResetDB();
//  Tdbp->ResetSize();

    // Check and initialize the subtable columns
    for (PCOL cp = Columns; cp; cp = cp->GetNext())
      if (cp->GetAmType() == TYPE_AM_TABID)
        cp->COLBLK::Reset();
Olivier Bertrand's avatar
Olivier Bertrand committed
427
      else if (((PPRXCOL)cp)->Init(g) && !Accept)
Alexander Barkov's avatar
Alexander Barkov committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 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
        return TRUE;
        
    if (trace)
      htrc("Opening subtable %s\n", Tdbp->GetName());

    // Now we can safely open the table
    if (Tdbp->OpenDB(g))
      return TRUE;

    } // endif *Tablist

  Use = USE_OPEN;
  return FALSE;
  } // end of OpenDB

/***********************************************************************/
/*  ReadDB: Data Base read routine for MUL access method.              */
/***********************************************************************/
int TDBTBL::ReadDB(PGLOBAL g)
  {
  int rc;

  if (!CurTable)
    return RC_EF;
  else if (To_Kindex) {
    /*******************************************************************/
    /*  Reading is by an index table.                                  */
    /*******************************************************************/
    strcpy(g->Message, MSG(NO_INDEX_READ));
    rc = RC_FX;
  } else {
    /*******************************************************************/
    /*  Now start the reading process.                                 */
    /*******************************************************************/
   retry:
    rc = Tdbp->ReadDB(g);

    if (rc == RC_EF) {
      // Total number of rows met so far
      Rows += Tdbp->RowNumber(g) - 1;
      Crp += Tdbp->GetProgMax(g);

      if ((CurTable = CurTable->GetNext())) {
        /***************************************************************/
        /*  Continue reading from next table file.                     */
        /***************************************************************/
        Tdbp->CloseDB(g);
        Tdbp = (PTDBASE)CurTable->GetTo_Tdb();

        // Check and initialize the subtable columns
        for (PCOL cp = Columns; cp; cp = cp->GetNext())
          if (cp->GetAmType() == TYPE_AM_TABID)
            cp->COLBLK::Reset();
Olivier Bertrand's avatar
Olivier Bertrand committed
481
          else if (((PPRXCOL)cp)->Init(g) && !Accept)
Alexander Barkov's avatar
Alexander Barkov committed
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
            return RC_FX;

        if (trace)
          htrc("Opening subtable %s\n", Tdbp->GetName());

        // Now we can safely open the table
        if (Tdbp->OpenDB(g))     // Open next table
          return RC_FX;

        goto retry;
        } // endif iFile

    } else if (rc == RC_FX)
      strcat(strcat(strcat(g->Message, " ("), Tdbp->GetName()), ")");

  } // endif To_Kindex

  return rc;
  } // end of ReadDB

/* ---------------------------- TBTBLK ------------------------------- */

/***********************************************************************/
/*  ReadColumn:                                                        */
/***********************************************************************/
void TBTBLK::ReadColumn(PGLOBAL g)
  {
  if (trace)
    htrc("TBT ReadColumn: name=%s\n", Name);

  Value->SetValue_psz((char*)((PTDBTBL)To_Tdb)->Tdbp->GetName());
513

Alexander Barkov's avatar
Alexander Barkov committed
514 515
  } // end of ReadColumn

516 517
/* ------------------------- Class TDBTBM ---------------------------- */

518 519 520 521 522 523 524 525
/***********************************************************************/
/*  Thread routine that check and open one remote connection.          */
/***********************************************************************/
pthread_handler_t ThreadOpen(void *p)
  {
  PTBMT cmp = (PTBMT)p;

  if (!my_thread_init()) {
526 527
    set_current_thd(cmp->Thd);

528 529 530 531 532 533 534 535 536 537 538 539 540
    // Try to open the connection
    if (!cmp->Tap->GetTo_Tdb()->OpenDB(cmp->G)) {
      cmp->Ready = true;
    } else
      cmp->Rc = RC_FX;

    my_thread_end();
  } else
    cmp->Rc = RC_FX;

  return NULL;
  } // end of ThreadOpen

541 542 543 544 545
/***********************************************************************/
/*  TDBTBM constructors.                                               */
/***********************************************************************/
TDBTBM::TDBTBM(PTBLDEF tdp) : TDBTBL(tdp)
  {
546 547 548 549 550 551
  Tmp = NULL;              // To data table TBMT structures
  Cmp = NULL;              // Current data table TBMT
  Bmp = NULL;              // To bad (unconnected) TBMT structures
  Done = false;            // TRUE after first GetAllResults
  Nrc = 0;                 // Number of remote connections
  Nlc = 0;                 // Number of local connections
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
  } // end of TDBTBL standard constructor

/***********************************************************************/
/*  Reset read/write position values.                                  */
/***********************************************************************/
void TDBTBM::ResetDB(void)
  {
  for (PCOL colp = Columns; colp; colp = colp->GetNext())
    if (colp->GetAmType() == TYPE_AM_TABID)
      colp->COLBLK::Reset();

  for (PTABLE tabp = Tablist; tabp; tabp = tabp->GetNext())
    ((PTDBASE)tabp->GetTo_Tdb())->ResetDB();

  Tdbp = (PTDBASE)Tablist->GetTo_Tdb();
  Crp = 0;
  } // end of ResetDB

/***********************************************************************/
/*  Returns RowId if b is false or Rownum if b is true.                */
/***********************************************************************/
int TDBTBM::RowNumber(PGLOBAL g, bool b)
  {
  return Tdbp->RowNumber(g) + ((b) ? 0 : Rows);
  } // end of RowNumber

/***********************************************************************/
/*  Initialyze table parallel processing.                              */
/***********************************************************************/
bool TDBTBM::OpenTables(PGLOBAL g)
  {
583 584
  int    k;
  THD   *thd = current_thd;
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
  PTABLE tabp, *ptabp = &Tablist;
  PTBMT  tp, *ptp = &Tmp;

  // Allocates the TBMT blocks for the tables
  for (tabp = Tablist; tabp; tabp = tabp->Next)
    if (tabp->GetTo_Tdb()->GetAmType() == TYPE_AM_MYSQL) {
      // Remove remote table from the local list
      *ptabp = tabp->Next;

      // Make the remote table block
      tp = (PTBMT)PlugSubAlloc(g, NULL, sizeof(TBMT));
      memset(tp, 0, sizeof(TBMT));
      tp->G = g;
      tp->Tap = tabp;
      tp->Thd = thd;

601
      // Create the thread that will do the table opening.
602 603 604
      pthread_attr_init(&tp->attr);
//    pthread_attr_setdetachstate(&tp->attr, PTHREAD_CREATE_JOINABLE);

605 606 607 608 609 610
      if ((k = pthread_create(&tp->Tid, &tp->attr, ThreadOpen, tp))) {
        sprintf(g->Message, "pthread_create error %d", k);
        Nbc++;
        continue;
        } // endif k

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 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 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 761 762 763 764 765 766 767 768 769 770 771 772 773
      // Add it to the remote list
      *ptp = tp;
      ptp = &tp->Next;
      Nrc++;         // Number of remote connections
    } else {
      ptabp = &tabp->Next;
      Nlc++;         // Number of local connections
    } // endif Type

  return false;
  } // end of OpenTables

/***********************************************************************/
/*  TBL Access Method opening routine.                                 */
/*  Open first file, other will be opened sequencially when reading.   */
/***********************************************************************/
bool TDBTBM::OpenDB(PGLOBAL g)
  {
  if (trace)
    htrc("TBM OpenDB: tdbp=%p tdb=R%d use=%d key=%p mode=%d\n",
                      this, Tdb_No, Use, To_Key_Col, Mode);

  if (Use == USE_OPEN) {
    /*******************************************************************/
    /*  Table already open, replace it at its beginning.               */
    /*******************************************************************/
    ResetDB();
    return Tdbp->OpenDB(g);  // Re-open fist table
    } // endif use

#if 0
  /*********************************************************************/
  /*  When GetMaxsize was called, To_Filter was not set yet.           */
  /*********************************************************************/
  if (To_Filter && Tablist) {
    Tablist = NULL;
    Nbc = 0;
    } // endif To_Filter
#endif // 0

  /*********************************************************************/
  /*  Make the table list.                                             */
  /*********************************************************************/
  if (/*!Tablist &&*/ InitTableList(g))
    return TRUE;

  /*********************************************************************/
  /*  Open all remote tables of the list.                              */
  /*********************************************************************/
  if (OpenTables(g))
    return TRUE;

  /*********************************************************************/
  /*  Proceed with local tables.                                       */
  /*********************************************************************/
  if ((CurTable = Tablist)) {
    Tdbp = (PTDBASE)CurTable->GetTo_Tdb();
    Tdbp->SetMode(Mode);

    // Check and initialize the subtable columns
    for (PCOL cp = Columns; cp; cp = cp->GetNext())
      if (cp->GetAmType() == TYPE_AM_TABID)
        cp->COLBLK::Reset();
      else if (((PPRXCOL)cp)->Init(g) && !Accept)
        return TRUE;
        
    if (trace)
      htrc("Opening subtable %s\n", Tdbp->GetName());

    // Now we can safely open the table
    if (Tdbp->OpenDB(g))
      return TRUE;

    } // endif *Tablist

  Use = USE_OPEN;
  return FALSE;
  } // end of OpenDB

/***********************************************************************/
/*  ReadDB: Data Base read routine for MUL access method.              */
/***********************************************************************/
int TDBTBM::ReadDB(PGLOBAL g)
  {
  int rc;

  if (!Done) {
    // Get result from local tables
    if ((rc = TDBTBL::ReadDB(g)) != RC_EF)
      return rc;
    else if ((rc = ReadNextRemote(g)) != RC_OK)
      return rc;

    Done = true;
    } // endif Done

  /*********************************************************************/
  /*  Now start the reading process of remote tables.                  */
  /*********************************************************************/
 retry:
  rc = Tdbp->ReadDB(g);

  if (rc == RC_EF) {
    // Total number of rows met so far
    Rows += Tdbp->RowNumber(g) - 1;
    Crp += Tdbp->GetProgMax(g);
    Cmp->Complete = true;

    if ((rc = ReadNextRemote(g)) == RC_OK)
      goto retry;

  } else if (rc == RC_FX)
    strcat(strcat(strcat(g->Message, " ("), Tdbp->GetName()), ")");

  return rc;
  } // end of ReadDB

/***********************************************************************/
/*  ReadNext: Continue reading from next table.                        */
/***********************************************************************/
int TDBTBM::ReadNextRemote(PGLOBAL g)
  {
  bool b = false;

  if (Tdbp)
    Tdbp->CloseDB(g);

  Cmp = NULL;

 retry:
  // Search for a remote table having its result set
  for (PTBMT  tp = Tmp; tp; tp = tp->Next)
    if (tp->Ready) {
      if (!tp->Complete)
        Cmp = tp;

    } else
      b = true;

  if (!Cmp) {
    if (b) {          // more result to come
//    sleep(20);
      goto retry;
    } else
      return RC_EF;

    } // endif Curtable

  Tdbp = (PTDBASE)Cmp->Tap->GetTo_Tdb();

  // Check and initialize the subtable columns
  for (PCOL cp = Columns; cp; cp = cp->GetNext())
    if (cp->GetAmType() == TYPE_AM_TABID)
      cp->COLBLK::Reset();
    else if (((PPRXCOL)cp)->Init(g) && !Accept)
      return RC_FX;

  if (trace)
    htrc("Reading subtable %s\n", Tdbp->GetName());

  return RC_OK;
  } // end of ReadNextRemote

Alexander Barkov's avatar
Alexander Barkov committed
774
/* ------------------------------------------------------------------- */