summaryrefslogtreecommitdiff
path: root/storage/connect
diff options
context:
space:
mode:
authorOlivier Bertrand <bertrandop@gmail.com>2014-03-22 08:57:32 +0100
committerOlivier Bertrand <bertrandop@gmail.com>2014-03-22 08:57:32 +0100
commit81ce7da7a72e483b8f9d491c2d937d8ccc8c7b6e (patch)
tree6f076508d09cd50b09869ce00b5f5810133c3402 /storage/connect
parent7b400a088d049661b9a4dded385ac78923bb0017 (diff)
parent31560c448a62514f244b67d0925cd3837188e4c9 (diff)
downloadmariadb-git-81ce7da7a72e483b8f9d491c2d937d8ccc8c7b6e.tar.gz
- Resolving conflicts
modified: storage/connect/block.h storage/connect/colblk.cpp storage/connect/connect.cc storage/connect/csort.h storage/connect/filamap.cpp storage/connect/filamdbf.cpp storage/connect/filamfix.cpp storage/connect/filamtxt.cpp storage/connect/filamzip.cpp storage/connect/ha_connect.cc storage/connect/mycat.cc storage/connect/myconn.cpp storage/connect/myutil.cpp storage/connect/osutil.c storage/connect/plgdbsem.h storage/connect/plgdbutl.cpp storage/connect/plugutil.c storage/connect/reldef.cpp storage/connect/tabcol.cpp storage/connect/tabfmt.cpp storage/connect/tabmysql.cpp storage/connect/tabodbc.cpp storage/connect/tabpivot.cpp storage/connect/tabvct.cpp storage/connect/user_connect.cc storage/connect/valblk.cpp storage/connect/value.cpp storage/connect/xindex.cpp
Diffstat (limited to 'storage/connect')
-rw-r--r--storage/connect/block.h7
-rw-r--r--storage/connect/colblk.cpp26
-rw-r--r--storage/connect/connect.cc1794
-rw-r--r--storage/connect/csort.h4
-rw-r--r--storage/connect/filamap.cpp59
-rw-r--r--storage/connect/filamdbf.cpp19
-rw-r--r--storage/connect/filamfix.cpp213
-rw-r--r--storage/connect/filamtxt.cpp2886
-rw-r--r--storage/connect/filamzip.cpp2807
-rw-r--r--storage/connect/ha_connect.cc11374
-rw-r--r--storage/connect/mycat.cc12
-rw-r--r--storage/connect/myconn.cpp11
-rw-r--r--storage/connect/myutil.cpp32
-rw-r--r--storage/connect/osutil.c6
-rw-r--r--storage/connect/plgdbsem.h1
-rw-r--r--storage/connect/plgdbutl.cpp192
-rw-r--r--storage/connect/plugutil.c1097
-rw-r--r--storage/connect/reldef.cpp868
-rw-r--r--storage/connect/tabcol.cpp25
-rw-r--r--storage/connect/tabfmt.cpp2858
-rw-r--r--storage/connect/tabmysql.cpp3240
-rw-r--r--storage/connect/tabodbc.cpp2
-rw-r--r--storage/connect/tabpivot.cpp4
-rw-r--r--storage/connect/tabvct.cpp37
-rw-r--r--storage/connect/user_connect.cc321
-rw-r--r--storage/connect/valblk.cpp19
-rw-r--r--storage/connect/value.cpp4410
-rwxr-xr-xstorage/connect/xindex.cpp6038
28 files changed, 19075 insertions, 19287 deletions
diff --git a/storage/connect/block.h b/storage/connect/block.h
index 20db70351e4..d63a899d1f5 100644
--- a/storage/connect/block.h
+++ b/storage/connect/block.h
@@ -38,10 +38,9 @@ typedef class BLOCK *PBLOCK;
class DllExport BLOCK {
public:
void * operator new(size_t size, PGLOBAL g, void *p = NULL) {
-#ifdef DEBTRACE
-if (debug != NULL)
- htrc("New BLOCK: size=%d g=%p p=%p\n", size, g, p);
-#endif
+// if (trace > 2)
+// htrc("New BLOCK: size=%d g=%p p=%p\n", size, g, p);
+
return (PlugSubAlloc(g, p, size));
} // end of new
diff --git a/storage/connect/colblk.cpp b/storage/connect/colblk.cpp
index e890ad1ce44..a3fb5587ba9 100644
--- a/storage/connect/colblk.cpp
+++ b/storage/connect/colblk.cpp
@@ -23,6 +23,8 @@
#include "xindex.h"
#include "xtable.h"
+extern "C" int trace;
+
/***********************************************************************/
/* COLBLK protected constructor. */
/***********************************************************************/
@@ -76,9 +78,8 @@ COLBLK::COLBLK(PCOL col1, PTDB tdbp)
//To_Orig = col1;
To_Tdb = tdbp;
-#ifdef DEBTRACE
- htrc(" copying COLBLK %s from %p to %p\n", Name, col1, this);
-#endif
+ if (trace > 1)
+ htrc(" copying COLBLK %s from %p to %p\n", Name, col1, this);
if (tdbp)
// Attach the new column to the table block
@@ -116,10 +117,9 @@ bool COLBLK::SetFormat(PGLOBAL g, FORMAT& fmt)
{
fmt = Format;
-#ifdef DEBTRACE
- htrc("COLBLK: %p format=%c(%d,%d)\n",
- this, *fmt.Type, fmt.Length, fmt.Prec);
-#endif
+ if (trace > 1)
+ htrc("COLBLK: %p format=%c(%d,%d)\n",
+ this, *fmt.Type, fmt.Length, fmt.Prec);
return false;
} // end of SetFormat
@@ -130,9 +130,8 @@ bool COLBLK::SetFormat(PGLOBAL g, FORMAT& fmt)
/***********************************************************************/
bool COLBLK::Eval(PGLOBAL g)
{
-#ifdef DEBTRACE
- htrc("Col Eval: %s status=%.4X\n", Name, Status);
-#endif
+ if (trace > 1)
+ htrc("Col Eval: %s status=%.4X\n", Name, Status);
if (!GetStatus(BUF_READ)) {
// if (To_Tdb->IsNull())
@@ -168,10 +167,9 @@ bool COLBLK::InitValue(PGLOBAL g)
AddStatus(BUF_READY);
Value->SetNullable(Nullable);
-#ifdef DEBTRACE
- htrc(" colp=%p type=%d value=%p coluse=%.4X status=%.4X\n",
- this, Buf_Type, Value, ColUse, Status);
-#endif
+ if (trace > 1)
+ htrc(" colp=%p type=%d value=%p coluse=%.4X status=%.4X\n",
+ this, Buf_Type, Value, ColUse, Status);
return false;
} // end of InitValue
diff --git a/storage/connect/connect.cc b/storage/connect/connect.cc
index f4bc4119f36..cf11a22d32b 100644
--- a/storage/connect/connect.cc
+++ b/storage/connect/connect.cc
@@ -1,897 +1,897 @@
-/* Copyright (C) Olivier Bertrand 2004 - 2012
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
-
-/***********************************************************************/
-/* Author Olivier BERTRAND bertrandop@gmail.com 2004-2012 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are the CONNECT general purpose semantic routines. */
-/***********************************************************************/
-#ifdef USE_PRAGMA_IMPLEMENTATION
-#pragma implementation // gcc: Class implementation
-#endif
-
-/***********************************************************************/
-/* Include application header files */
-/* */
-/* global.h is header containing all global declarations. */
-/* plgdbsem.h is header containing the DB applic. declarations. */
-/***********************************************************************/
-#define DONT_DEFINE_VOID
-#include "handler.h"
-#undef OFFSET
-
-#include "global.h"
-#include "plgdbsem.h"
-#include "xobject.h"
-#include "connect.h"
-#include "tabcol.h"
-#include "catalog.h"
-#include "ha_connect.h"
-#include "mycat.h"
-
-#define my_strupr(p) my_caseup_str(default_charset_info, (p));
-#define my_strlwr(p) my_casedn_str(default_charset_info, (p));
-#define my_stricmp(a, b) my_strcasecmp(default_charset_info, (a), (b))
-
-/***********************************************************************/
-/* DB static variables. */
-/***********************************************************************/
-extern int xtrace;
-
-/***********************************************************************/
-/* Routines called internally by semantic routines. */
-/***********************************************************************/
-void CntEndDB(PGLOBAL);
-RCODE EvalColumns(PGLOBAL g, PTDB tdbp, bool mrr= false);
-
-/***********************************************************************/
-/* MySQL routines called externally by semantic routines. */
-/***********************************************************************/
-int rename_file_ext(const char *from, const char *to,const char *ext);
-
-/***********************************************************************/
-/* CntExit: CONNECT termination routine. */
-/***********************************************************************/
-PGLOBAL CntExit(PGLOBAL g)
- {
- if (g) {
- CntEndDB(g);
-
- if (g->Activityp)
- delete g->Activityp;
-
- PlugExit(g);
- g= NULL;
- } // endif g
-
- return g;
- } // end of CntExit
-
-/***********************************************************************/
-/* CntEndDB: DB termination semantic routine. */
-/***********************************************************************/
-void CntEndDB(PGLOBAL g)
- {
- PDBUSER dbuserp= PlgGetUser(g);
-
- if (dbuserp) {
- if (dbuserp->Catalog)
- delete dbuserp->Catalog;
-
- free(dbuserp);
- } // endif dbuserp
-
- } // end of CntEndDB
-
-/***********************************************************************/
-/* CntCheckDB: Initialize a DB application session. */
-/* Note: because MySQL does not call a storage handler when a user */
-/* executes a use db command, a check must be done before an SQL */
-/* command is executed to check whether we are still working on the */
-/* current database, and if not to load the newly used database. */
-/***********************************************************************/
-bool CntCheckDB(PGLOBAL g, PHC handler, const char *pathname)
- {
- bool rc= false;
- PDBUSER dbuserp= PlgGetUser(g);
-
- if (xtrace) {
- printf("CntCheckDB: dbuserp=%p\n", dbuserp);
- } // endif xtrace
-
- if (!dbuserp || !handler)
- return true;
-
- if (xtrace)
- printf("cat=%p oldhandler=%p newhandler=%p\n", dbuserp->Catalog,
- (dbuserp->Catalog) ? ((MYCAT*)dbuserp->Catalog)->GetHandler() : NULL,
- handler);
-
- if (dbuserp->Catalog) {
-// ((MYCAT *)dbuserp->Catalog)->SetHandler(handler); done later
- ((MYCAT *)dbuserp->Catalog)->SetDataPath(g, pathname);
- return false; // Nothing else to do
- } // endif Catalog
-
- // Copy new database name in dbuser block
- strncpy(dbuserp->Name, "???", sizeof(dbuserp->Name) - 1);
-
- dbuserp->Vtdbno= 0; // Init of TDB numbers
-
- /*********************************************************************/
- /* Now allocate and initialize the Database Catalog. */
- /*********************************************************************/
- dbuserp->Step= MSG(READY);
-
- if (!(dbuserp->Catalog= new MYCAT(handler)))
- return true;
-
- ((MYCAT *)dbuserp->Catalog)->SetDataPath(g, pathname);
- dbuserp->UseTemp= TMP_YES; // Must use temporary file
-
- /*********************************************************************/
- /* All is correct. */
- /*********************************************************************/
- sprintf(g->Message, MSG(DATABASE_LOADED), "???");
-
- if (xtrace)
- printf("msg=%s\n", g->Message);
-
- return rc;
- } // end of CntCheckDB
-
-/***********************************************************************/
-/* CntInfo: Get table info. */
-/* Returns valid: true if this is a table info. */
-/***********************************************************************/
-bool CntInfo(PGLOBAL g, PTDB tp, PXF info)
- {
- bool b;
- PTDBDOS tdbp= (PTDBDOS)tp;
-
- if (tdbp) {
- b= tdbp->GetFtype() != RECFM_NAF;
- info->data_file_length= (b) ? (ulonglong)tdbp->GetFileLength(g) : 0;
- info->records= (unsigned)tdbp->GetMaxSize(g);
-// info->mean_rec_length= tdbp->GetLrecl();
- info->mean_rec_length= 0;
- info->data_file_name= (b) ? tdbp->GetFile(g) : NULL;
- return true;
- } else {
- info->data_file_length= 0;
- info->records= 0;
- info->mean_rec_length= 0;
- info->data_file_name= NULL;
- return false;
- } // endif tdbp
-
- } // end of CntInfo
-
-/***********************************************************************/
-/* GetTDB: Get the table description block of a CONNECT table. */
-/***********************************************************************/
-PTDB CntGetTDB(PGLOBAL g, LPCSTR name, MODE mode, PHC h)
- {
- int rc;
- PTDB tdbp;
- PTABLE tabp;
- PDBUSER dup= PlgGetUser(g);
- PCATLG cat= (dup) ? dup->Catalog : NULL; // Safe over longjmp
-
- if (xtrace)
- printf("CntGetTDB: name=%s mode=%d cat=%p\n", name, mode, cat);
-
- if (!cat)
- return NULL;
-
- // Save stack and allocation environment and prepare error return
- if (g->jump_level == MAX_JUMP) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- return NULL;
- } // endif jump_level
-
- if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
- tdbp= NULL;
- goto err;
- } // endif rc
-
- // Get table object from the catalog
- tabp= new(g) XTAB(name);
-
- if (xtrace)
- printf("CntGetTDB: tabp=%p\n", tabp);
-
- // Perhaps this should be made thread safe
- ((MYCAT*)cat)->SetHandler(h);
-
- if (!(tdbp= cat->GetTable(g, tabp, mode)))
- printf("CntGetTDB: %s\n", g->Message);
-
- err:
- if (xtrace)
- printf("Returning tdbp=%p mode=%d\n", tdbp, mode);
-
- g->jump_level--;
- return tdbp;
- } // end of CntGetTDB
-
-/***********************************************************************/
-/* OPENTAB: Open a Table. */
-/***********************************************************************/
-bool CntOpenTable(PGLOBAL g, PTDB tdbp, MODE mode, char *c1, char *c2,
- bool del, PHC h)
- {
- char *p;
- int i, n, rc;
- bool rcop= true;
- PCOL colp;
-//PCOLUMN cp;
- PDBUSER dup= PlgGetUser(g);
-
- if (xtrace)
- printf("CntOpenTable: tdbp=%p mode=%d\n", tdbp, mode);
-
- if (!tdbp) {
- strcpy(g->Message, "Null tdbp");
- printf("CntOpenTable: %s\n", g->Message);
- return true;
- } // endif tdbp
-
- // Save stack and allocation environment and prepare error return
- if (g->jump_level == MAX_JUMP) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- return true;
- } // endif jump_level
-
- if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
- goto err;
- } // endif rc
-
- if (!c1) {
- if (mode == MODE_INSERT)
- // Allocate all column blocks for that table
- tdbp->ColDB(g, NULL, 0);
-
- } else for (p= c1; *p; p+= n) {
- // Allocate only used column blocks
- if (xtrace)
- printf("Allocating column %s\n", p);
-
-// if (*p == '*') {
-// // This is a special column
-// cp= new(g) COLUMN(p + 1);
-// cp->SetTo_Table(tdbp->GetTable());
-// colp= ((PTDBASE)tdbp)->InsertSpcBlk(g, cp);
-// } else
- colp= tdbp->ColDB(g, p, 0);
-
- if (!colp) {
- sprintf(g->Message, "Column %s not found in %s", p, tdbp->GetName());
- goto err;
- } // endif colp
-
- n= strlen(p) + 1;
- } // endfor p
-
- for (i= 0, colp= tdbp->GetColumns(); colp; i++, colp= colp->GetNext()) {
- if (colp->InitValue(g))
- goto err;
-
- if (mode == MODE_INSERT)
- // Allow type conversion
- if (colp->SetBuffer(g, colp->GetValue(), true, false))
- goto err;
-
- colp->AddColUse(U_P); // For PLG tables
- } // endfor colp
-
- /*********************************************************************/
- /* In Update mode, the updated column blocks must be distinct from */
- /* the read column blocks. So make a copy of the TDB and allocate */
- /* its column blocks in mode write (required by XML tables). */
- /*********************************************************************/
- if (mode == MODE_UPDATE) {
- PTDBASE utp;
-
- if (!(utp= (PTDBASE)tdbp->Duplicate(g))) {
- sprintf(g->Message, MSG(INV_UPDT_TABLE), tdbp->GetName());
- goto err;
- } // endif tp
-
- if (!c2)
- // Allocate all column blocks for that table
- utp->ColDB(g, NULL, 0);
- else for (p= c2; *p; p+= n) {
- // Allocate only used column blocks
- colp= utp->ColDB(g, p, 0);
- n= strlen(p) + 1;
- } // endfor p
-
- for (i= 0, colp= utp->GetColumns(); colp; i++, colp= colp->GetNext()) {
- if (colp->InitValue(g))
- goto err;
-
- if (colp->SetBuffer(g, colp->GetValue(), true, false))
- goto err;
-
- } // endfor colp
-
- // Attach the updated columns list to the main table
- ((PTDBASE)tdbp)->SetSetCols(utp->GetColumns());
- } else if (tdbp && mode == MODE_INSERT)
- ((PTDBASE)tdbp)->SetSetCols(tdbp->GetColumns());
-
- // Now do open the physical table
- if (xtrace)
- printf("Opening table %s in mode %d tdbp=%p\n",
- tdbp->GetName(), mode, tdbp);
-
-//tdbp->SetMode(mode);
-
- if (del && ((PTDBASE)tdbp)->GetFtype() != RECFM_NAF) {
- // To avoid erasing the table when doing a partial delete
- // make a fake Next
- PDOSDEF ddp= new(g) DOSDEF;
- PTDB tp= new(g) TDBDOS(ddp, NULL);
- tdbp->SetNext(tp);
- dup->Check &= ~CHK_DELETE;
- } // endif del
-
-
- if (xtrace)
- printf("About to open the table: tdbp=%p\n", tdbp);
-
- if (mode != MODE_ANY && mode != MODE_ALTER) {
- if (tdbp->OpenDB(g)) {
- printf("%s\n", g->Message);
- goto err;
- } else
- tdbp->SetNext(NULL);
-
- } // endif mode
-
- rcop= false;
-
- err:
- g->jump_level--;
- return rcop;
- } // end of CntOpenTable
-
-/***********************************************************************/
-/* Rewind a table by reopening it. */
-/***********************************************************************/
-bool CntRewindTable(PGLOBAL g, PTDB tdbp)
-{
- if (!tdbp)
- return true;
-
- tdbp->OpenDB(g);
- return false;
-} // end of CntRewindTable
-
-/***********************************************************************/
-/* Evaluate all columns after a record is read. */
-/***********************************************************************/
-RCODE EvalColumns(PGLOBAL g, PTDB tdbp, bool mrr)
- {
- RCODE rc= RC_OK;
- PCOL colp;
-
- // Save stack and allocation environment and prepare error return
- if (g->jump_level == MAX_JUMP) {
- if (xtrace) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- printf("EvalColumns: %s\n", g->Message);
- } // endif
-
- return RC_FX;
- } // endif jump_level
-
- if (setjmp(g->jumper[++g->jump_level]) != 0) {
- if (xtrace)
- printf("Error reading columns: %s\n", g->Message);
-
- rc= RC_FX;
- goto err;
- } // endif rc
-
- for (colp= tdbp->GetColumns(); rc == RC_OK && colp;
- colp= colp->GetNext()) {
- colp->Reset();
-
- // Virtual columns are computed by MariaDB
- if (!colp->GetColUse(U_VIRTUAL) && (!mrr || colp->GetKcol()))
- if (colp->Eval(g))
- rc= RC_FX;
-
- } // endfor colp
-
- err:
- g->jump_level--;
- return rc;
- } // end of EvalColumns
-
-/***********************************************************************/
-/* ReadNext: Read next record sequentially. */
-/***********************************************************************/
-RCODE CntReadNext(PGLOBAL g, PTDB tdbp)
- {
- RCODE rc;
-
- if (!tdbp)
- return RC_FX;
- else if (((PTDBASE)tdbp)->GetKindex()) {
- // Reading sequencially an indexed table. This happens after the
- // handler function records_in_range was called and MySQL decides
- // to quit using the index (!!!) Drop the index.
- for (PCOL colp= tdbp->GetColumns(); colp; colp= colp->GetNext())
- colp->SetKcol(NULL);
-
- ((PTDBASE)tdbp)->SetKindex(NULL);
- } // endif index
-
- // Save stack and allocation environment and prepare error return
- if (g->jump_level == MAX_JUMP) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- return RC_FX;
- } // endif jump_level
-
- if ((setjmp(g->jumper[++g->jump_level])) != 0) {
- rc= RC_FX;
- goto err;
- } // endif rc
-
- while ((rc= (RCODE)tdbp->ReadDB(g)) == RC_NF) ;
-
- err:
- g->jump_level--;
- return (rc != RC_OK) ? rc : EvalColumns(g, tdbp);
- } // end of CntReadNext
-
-/***********************************************************************/
-/* WriteRow: Insert a new row into a table. */
-/***********************************************************************/
-RCODE CntWriteRow(PGLOBAL g, PTDB tdbp)
- {
- RCODE rc;
- PCOL colp;
- PTDBASE tp= (PTDBASE)tdbp;
-
- if (!tdbp)
- return RC_FX;
-
- // Save stack and allocation environment and prepare error return
- if (g->jump_level == MAX_JUMP) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- return RC_FX;
- } // endif jump_level
-
- if (setjmp(g->jumper[++g->jump_level]) != 0) {
- printf("%s\n", g->Message);
- rc= RC_FX;
- goto err;
- } // endif rc
-
- // Store column values in table write buffer(s)
- for (colp= tp->GetSetCols(); colp; colp= colp->GetNext())
- if (!colp->GetColUse(U_VIRTUAL))
- colp->WriteColumn(g);
-
-// if (tdbp->GetMode() == MODE_INSERT)
-// tbxp->SetModified(true);
-
- // Return result code from write operation
- rc= (RCODE)tdbp->WriteDB(g);
-
- err:
- g->jump_level--;
- return rc;
- } // end of CntWriteRow
-
-/***********************************************************************/
-/* UpdateRow: Update a row into a table. */
-/***********************************************************************/
-RCODE CntUpdateRow(PGLOBAL g, PTDB tdbp)
- {
- if (!tdbp || tdbp->GetMode() != MODE_UPDATE)
- return RC_FX;
-
- // Return result code from write operation
- return CntWriteRow(g, tdbp);
- } // end of CntUpdateRow
-
-/***********************************************************************/
-/* DeleteRow: Delete a row from a table. */
-/***********************************************************************/
-RCODE CntDeleteRow(PGLOBAL g, PTDB tdbp, bool all)
- {
- RCODE rc;
-
- if (!tdbp || tdbp->GetMode() != MODE_DELETE)
- return RC_FX;
- else if (tdbp->IsReadOnly())
- return RC_NF;
-
- if (((PTDBASE)tdbp)->GetDef()->Indexable() && all)
- ((PTDBDOS)tdbp)->Cardinal= 0;
-
- // Return result code from delete operation
- // Note: if all, this call will be done when closing the table
- rc= (RCODE)tdbp->DeleteDB(g, (all) ? RC_FX : RC_OK);
- return rc;
- } // end of CntDeleteRow
-
-/***********************************************************************/
-/* CLOSETAB: Close a table. */
-/***********************************************************************/
-int CntCloseTable(PGLOBAL g, PTDB tdbp)
- {
- int rc= RC_OK;
- TDBDOX *tbxp= NULL;
-
- if (!tdbp || tdbp->GetUse() != USE_OPEN)
- return rc; // Nothing to do
-
- if (xtrace)
- printf("CntCloseTable: tdbp=%p mode=%d\n", tdbp, tdbp->GetMode());
-
- if (tdbp->GetMode() == MODE_DELETE && tdbp->GetUse() == USE_OPEN)
- rc= tdbp->DeleteDB(g, RC_EF); // Specific A.M. delete routine
-
- // Prepare error return
- if (g->jump_level == MAX_JUMP) {
- strcpy(g->Message, MSG(TOO_MANY_JUMPS));
- rc= RC_FX;
- goto err;
- } // endif
-
- if ((rc = setjmp(g->jumper[++g->jump_level])) != 0) {
- g->jump_level--;
- goto err;
- } // endif
-
- // This will close the table file(s) and also finalize write
- // operations such as Insert, Update, or Delete.
- tdbp->CloseDB(g);
-
- g->jump_level--;
-
- if (xtrace > 1)
- printf("Table %s closed\n", tdbp->GetName());
-
-//if (!((PTDBDOX)tdbp)->GetModified())
-// return 0;
-
- if (tdbp->GetMode() == MODE_READ || tdbp->GetMode() == MODE_ANY)
- return 0;
-
- if (xtrace > 1)
- printf("About to reset opt\n");
-
- // Make all the eventual indexes
- tbxp= (TDBDOX*)tdbp;
- tbxp->SetKindex(NULL);
- tbxp->To_Key_Col= NULL;
- rc= tbxp->ResetTableOpt(g, false, ((PTDBASE)tdbp)->GetDef()->Indexable());
-
- err:
- if (xtrace > 1)
- printf("Done rc=%d\n", rc);
-
- return (rc == RC_OK || rc == RC_INFO) ? 0 : rc;
- } // end of CntCloseTable
-
-/***********************************************************************/
-/* Load and initialize the use of an index. */
-/* This is the condition(s) for doing indexing. */
-/* Note: FIX table are not reset here to Nrec= 1. */
-/***********************************************************************/
-int CntIndexInit(PGLOBAL g, PTDB ptdb, int id)
- {
- int k;
- PCOL colp;
- PVAL valp;
- PKXBASE xp;
- PXLOAD pxp;
- PIXDEF xdp;
- XKPDEF *kdp;
- PTDBDOX tdbp;
- PCOLDEF cdp;
- DOXDEF *dfp;
-
- if (!ptdb)
- return -1;
- else if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
- sprintf(g->Message, "CntIndexInit: Table %s is not indexable", ptdb->GetName());
- return 0;
- } else
- tdbp= (PTDBDOX)ptdb;
-
- dfp= (DOXDEF*)tdbp->To_Def;
-
-//if (!(k= colp->GetKey()))
-// if (colp->GetOpt() >= 2) {
-// strcpy(g->Message, "Not a valid indexed column");
-// return -1;
-// } else
- // This is a pseudo indexed sorted block optimized column
-// return 0;
-
- if (tdbp->To_Kindex)
- if (((XXBASE*)tdbp->To_Kindex)->GetID() == id) {
- tdbp->To_Kindex->Reset(); // Same index
- return (tdbp->To_Kindex->IsMul()) ? 2 : 1;
- } else {
- tdbp->To_Kindex->Close();
- tdbp->To_Kindex= NULL;
- } // endif colp
-
- for (xdp= dfp->To_Indx; xdp; xdp= xdp->GetNext())
- if (xdp->GetID() == id)
- break;
-
- if (!xdp) {
- sprintf(g->Message, "Wrong index ID %d", id);
- return 0;
- } // endif xdp
-
- // Allocate the key columns definition block
- tdbp->Knum= xdp->GetNparts();
- tdbp->To_Key_Col= (PCOL*)PlugSubAlloc(g, NULL, tdbp->Knum * sizeof(PCOL));
-
- // Get the key column description list
- for (k= 0, kdp= (XKPDEF*)xdp->GetToKeyParts(); kdp; kdp= (XKPDEF*)kdp->Next)
- if (!(colp= tdbp->ColDB(g, kdp->Name, 0)) || colp->InitValue(g)) {
- sprintf(g->Message, "Wrong column %s", kdp->Name);
- return 0;
- } else
- tdbp->To_Key_Col[k++]= colp;
-
-#if defined(_DEBUG)
- if (k != tdbp->Knum) {
- sprintf(g->Message, "Key part number mismatch for %s",
- xdp->GetName());
- return 0;
- } // endif k
-#endif // _DEBUG
-
- // Allocate the pseudo constants that will contain the key values
- tdbp->To_Link= (PXOB*)PlugSubAlloc(g, NULL, tdbp->Knum * sizeof(PXOB));
-
- for (k= 0, kdp= (XKPDEF*)xdp->GetToKeyParts();
- kdp; k++, kdp= (XKPDEF*)kdp->Next) {
- cdp= tdbp->Key(k)->GetCdp();
- valp= AllocateValue(g, cdp->GetType(), cdp->GetLength());
- tdbp->To_Link[k]= new(g) CONSTANT(valp);
- } // endfor k
-
- // Make the index on xdp
- if (!xdp->IsAuto()) {
- if (dfp->Huge)
- pxp= new(g) XHUGE;
- else
- pxp= new(g) XFILE;
-
- if (tdbp->Knum == 1) // Single index
- xp= new(g) XINDXS(tdbp, xdp, pxp, tdbp->To_Key_Col, tdbp->To_Link);
- else // Multi-Column index
- xp= new(g) XINDEX(tdbp, xdp, pxp, tdbp->To_Key_Col, tdbp->To_Link);
-
- } else // Column contains same values as ROWID
- xp= new(g) XXROW(tdbp);
-
- if (xp->Init(g))
- return 0;
-
- tdbp->To_Kindex= xp;
- return (xp->IsMul()) ? 2 : 1;
- } // end of CntIndexInit
-
-/***********************************************************************/
-/* IndexRead: fetch a record having the index value. */
-/***********************************************************************/
-RCODE CntIndexRead(PGLOBAL g, PTDB ptdb, OPVAL op,
- const void *key, int len, bool mrr)
- {
- char *kp= (char*)key;
- int n;
- short lg;
- bool rcb;
- RCODE rc;
- PVAL valp;
- PCOL colp;
- XXBASE *xbp;
- PTDBDOX tdbp;
-
- if (!ptdb)
- return RC_FX;
- if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
- sprintf(g->Message, "CntIndexRead: Table %s is not indexable", ptdb->GetName());
- return RC_FX;
- } else
- tdbp= (PTDBDOX)ptdb;
-
- // Set reference values and index operator
- if (!tdbp->To_Link || !tdbp->To_Kindex) {
- sprintf(g->Message, "Index not initialized for table %s", tdbp->Name);
- return RC_FX;
- } else
- xbp= (XXBASE*)tdbp->To_Kindex;
-
- if (key) {
- for (n= 0; n < tdbp->Knum; n++) {
- colp= (PCOL)tdbp->To_Key_Col[n];
-
- if (colp->GetColUse(U_NULLS))
- kp++; // Skip null byte
-
- valp= tdbp->To_Link[n]->GetValue();
-
- if (!valp->IsTypeNum()) {
- if (colp->GetColUse(U_VAR)) {
- lg= *(short*)kp;
- kp+= sizeof(short);
- rcb= valp->SetValue_char(kp, (int)lg);
- } else
- rcb= valp->SetValue_char(kp, valp->GetClen());
-
- if (rcb) {
- if (tdbp->RowNumber(g))
- sprintf(g->Message, "Out of range value for column %s at row %d",
- colp->GetName(), tdbp->RowNumber(g));
- else
- sprintf(g->Message, "Out of range value for column %s",
- colp->GetName());
-
- PushWarning(g, tdbp);
- } // endif b
-
- } else
- valp->SetBinValue((void*)kp);
-
- kp+= valp->GetClen();
-
- if (len == kp - (char*)key) {
- n++;
- break;
- } else if (len < kp - (char*)key) {
- strcpy(g->Message, "Key buffer is too small");
- return RC_FX;
- } // endif len
-
- } // endfor n
-
- xbp->SetNval(n);
- } // endif key
-
- xbp->SetOp(op);
- xbp->SetNth(0);
-
- if ((rc= (RCODE)tdbp->ReadDB(g)) == RC_OK)
- rc= EvalColumns(g, tdbp, mrr);
-
- return rc;
- } // end of CntIndexRead
-
-/***********************************************************************/
-/* Return the number of rows matching given values. */
-/***********************************************************************/
-int CntIndexRange(PGLOBAL g, PTDB ptdb, const uchar* *key, uint *len,
- bool *incl, key_part_map *kmap)
- {
- const uchar *p, *kp;
- int i, n, k[2];
- short lg;
- bool b, rcb;
- PVAL valp;
- PCOL colp;
- PTDBDOX tdbp;
- XXBASE *xbp;
-
- if (!ptdb)
- return -1;
- else if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
- sprintf(g->Message, "CntIndexRange: Table %s is not indexable", ptdb->GetName());
- DBUG_PRINT("Range", ("%s", g->Message));
- return -1;
- } else
- tdbp= (PTDBDOX)ptdb;
-
- if (!tdbp->To_Link || !tdbp->To_Kindex) {
- sprintf(g->Message, "Index not initialized for table %s", tdbp->Name);
- DBUG_PRINT("Range", ("%s", g->Message));
- return -1;
- } else
- xbp= (XXBASE*)tdbp->To_Kindex;
-
- for (b= false, i= 0; i < 2; i++) {
- p= kp= key[i];
-
- if (kp) {
- for (n= 0; n < tdbp->Knum; n++) {
- if (kmap[i] & (key_part_map)(1 << n)) {
- if (b == true)
- // Cannot do indexing with missing intermediate key
- return -1;
-
- colp= (PCOL)tdbp->To_Key_Col[n];
-
- if (colp->GetColUse(U_NULLS))
- p++; // Skip null byte ???
-
- valp= tdbp->To_Link[n]->GetValue();
-
- if (!valp->IsTypeNum()) {
- if (colp->GetColUse(U_VAR)) {
- lg= *(short*)p;
- p+= sizeof(short);
- rcb= valp->SetValue_char((char*)p, (int)lg);
- } else
- rcb= valp->SetValue_char((char*)p, valp->GetClen());
-
- if (rcb) {
- if (tdbp->RowNumber(g))
- sprintf(g->Message,
- "Out of range value for column %s at row %d",
- colp->GetName(), tdbp->RowNumber(g));
- else
- sprintf(g->Message, "Out of range value for column %s",
- colp->GetName());
-
- PushWarning(g, tdbp);
- } // endif b
-
- } else
- valp->SetBinValue((void*)p);
-
- if (xtrace) {
- char bf[32];
- printf("i=%d n=%d key=%s\n", i, n, valp->GetCharString(bf));
- } // endif xtrace
-
- p+= valp->GetClen();
-
- if (len[i] == (unsigned)(p - kp)) {
- n++;
- break;
- } else if (len[i] < (unsigned)(p - kp)) {
- strcpy(g->Message, "Key buffer is too small");
- return -1;
- } // endif len
-
- } else
- b= true;
-
- } // endfor n
-
- xbp->SetNval(n);
-
- if (xtrace)
- printf("xbp=%p Nval=%d i=%d incl=%d\n", xbp, n, i, incl[i]);
-
- k[i]= xbp->Range(g, i + 1, incl[i]);
- } else
- k[i]= (i) ? xbp->GetNum_K() : 0;
-
- } // endfor i
-
- if (xtrace)
- printf("k1=%d k0=%d\n", k[1], k[0]);
-
- return k[1] - k[0];
- } // end of CntIndexRange
+/* Copyright (C) Olivier Bertrand 2004 - 2012
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+/***********************************************************************/
+/* Author Olivier BERTRAND bertrandop@gmail.com 2004-2012 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are the CONNECT general purpose semantic routines. */
+/***********************************************************************/
+#ifdef USE_PRAGMA_IMPLEMENTATION
+#pragma implementation // gcc: Class implementation
+#endif
+
+/***********************************************************************/
+/* Include application header files */
+/* */
+/* global.h is header containing all global declarations. */
+/* plgdbsem.h is header containing the DB applic. declarations. */
+/***********************************************************************/
+#define DONT_DEFINE_VOID
+#include "handler.h"
+#undef OFFSET
+
+#include "global.h"
+#include "plgdbsem.h"
+#include "xobject.h"
+#include "connect.h"
+#include "tabcol.h"
+#include "catalog.h"
+#include "ha_connect.h"
+#include "mycat.h"
+
+#define my_strupr(p) my_caseup_str(default_charset_info, (p));
+#define my_strlwr(p) my_casedn_str(default_charset_info, (p));
+#define my_stricmp(a, b) my_strcasecmp(default_charset_info, (a), (b))
+
+/***********************************************************************/
+/* DB static variables. */
+/***********************************************************************/
+extern "C" int trace;
+
+/***********************************************************************/
+/* Routines called internally by semantic routines. */
+/***********************************************************************/
+void CntEndDB(PGLOBAL);
+RCODE EvalColumns(PGLOBAL g, PTDB tdbp, bool mrr= false);
+
+/***********************************************************************/
+/* MySQL routines called externally by semantic routines. */
+/***********************************************************************/
+int rename_file_ext(const char *from, const char *to,const char *ext);
+
+/***********************************************************************/
+/* CntExit: CONNECT termination routine. */
+/***********************************************************************/
+PGLOBAL CntExit(PGLOBAL g)
+ {
+ if (g) {
+ CntEndDB(g);
+
+ if (g->Activityp)
+ delete g->Activityp;
+
+ PlugExit(g);
+ g= NULL;
+ } // endif g
+
+ return g;
+ } // end of CntExit
+
+/***********************************************************************/
+/* CntEndDB: DB termination semantic routine. */
+/***********************************************************************/
+void CntEndDB(PGLOBAL g)
+ {
+ PDBUSER dbuserp= PlgGetUser(g);
+
+ if (dbuserp) {
+ if (dbuserp->Catalog)
+ delete dbuserp->Catalog;
+
+ free(dbuserp);
+ } // endif dbuserp
+
+ } // end of CntEndDB
+
+/***********************************************************************/
+/* CntCheckDB: Initialize a DB application session. */
+/* Note: because MySQL does not call a storage handler when a user */
+/* executes a use db command, a check must be done before an SQL */
+/* command is executed to check whether we are still working on the */
+/* current database, and if not to load the newly used database. */
+/***********************************************************************/
+bool CntCheckDB(PGLOBAL g, PHC handler, const char *pathname)
+ {
+ bool rc= false;
+ PDBUSER dbuserp= PlgGetUser(g);
+
+ if (trace) {
+ printf("CntCheckDB: dbuserp=%p\n", dbuserp);
+ } // endif trace
+
+ if (!dbuserp || !handler)
+ return true;
+
+ if (trace)
+ printf("cat=%p oldhandler=%p newhandler=%p\n", dbuserp->Catalog,
+ (dbuserp->Catalog) ? ((MYCAT*)dbuserp->Catalog)->GetHandler() : NULL,
+ handler);
+
+ if (dbuserp->Catalog) {
+// ((MYCAT *)dbuserp->Catalog)->SetHandler(handler); done later
+ ((MYCAT *)dbuserp->Catalog)->SetDataPath(g, pathname);
+ return false; // Nothing else to do
+ } // endif Catalog
+
+ // Copy new database name in dbuser block
+ strncpy(dbuserp->Name, "???", sizeof(dbuserp->Name) - 1);
+
+ dbuserp->Vtdbno= 0; // Init of TDB numbers
+
+ /*********************************************************************/
+ /* Now allocate and initialize the Database Catalog. */
+ /*********************************************************************/
+ dbuserp->Step= MSG(READY);
+
+ if (!(dbuserp->Catalog= new MYCAT(handler)))
+ return true;
+
+ ((MYCAT *)dbuserp->Catalog)->SetDataPath(g, pathname);
+ dbuserp->UseTemp= TMP_YES; // Must use temporary file
+
+ /*********************************************************************/
+ /* All is correct. */
+ /*********************************************************************/
+ sprintf(g->Message, MSG(DATABASE_LOADED), "???");
+
+ if (trace)
+ printf("msg=%s\n", g->Message);
+
+ return rc;
+ } // end of CntCheckDB
+
+/***********************************************************************/
+/* CntInfo: Get table info. */
+/* Returns valid: true if this is a table info. */
+/***********************************************************************/
+bool CntInfo(PGLOBAL g, PTDB tp, PXF info)
+ {
+ bool b;
+ PTDBDOS tdbp= (PTDBDOS)tp;
+
+ if (tdbp) {
+ b= tdbp->GetFtype() != RECFM_NAF;
+ info->data_file_length= (b) ? (ulonglong)tdbp->GetFileLength(g) : 0;
+ info->records= (unsigned)tdbp->GetMaxSize(g);
+// info->mean_rec_length= tdbp->GetLrecl();
+ info->mean_rec_length= 0;
+ info->data_file_name= (b) ? tdbp->GetFile(g) : NULL;
+ return true;
+ } else {
+ info->data_file_length= 0;
+ info->records= 0;
+ info->mean_rec_length= 0;
+ info->data_file_name= NULL;
+ return false;
+ } // endif tdbp
+
+ } // end of CntInfo
+
+/***********************************************************************/
+/* GetTDB: Get the table description block of a CONNECT table. */
+/***********************************************************************/
+PTDB CntGetTDB(PGLOBAL g, LPCSTR name, MODE mode, PHC h)
+ {
+ int rc;
+ PTDB tdbp;
+ PTABLE tabp;
+ PDBUSER dup= PlgGetUser(g);
+ PCATLG cat= (dup) ? dup->Catalog : NULL; // Safe over longjmp
+
+ if (trace)
+ printf("CntGetTDB: name=%s mode=%d cat=%p\n", name, mode, cat);
+
+ if (!cat)
+ return NULL;
+
+ // Save stack and allocation environment and prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ return NULL;
+ } // endif jump_level
+
+ if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
+ tdbp= NULL;
+ goto err;
+ } // endif rc
+
+ // Get table object from the catalog
+ tabp= new(g) XTAB(name);
+
+ if (trace)
+ printf("CntGetTDB: tabp=%p\n", tabp);
+
+ // Perhaps this should be made thread safe
+ ((MYCAT*)cat)->SetHandler(h);
+
+ if (!(tdbp= cat->GetTable(g, tabp, mode)))
+ printf("CntGetTDB: %s\n", g->Message);
+
+ err:
+ if (trace)
+ printf("Returning tdbp=%p mode=%d\n", tdbp, mode);
+
+ g->jump_level--;
+ return tdbp;
+ } // end of CntGetTDB
+
+/***********************************************************************/
+/* OPENTAB: Open a Table. */
+/***********************************************************************/
+bool CntOpenTable(PGLOBAL g, PTDB tdbp, MODE mode, char *c1, char *c2,
+ bool del, PHC h)
+ {
+ char *p;
+ int i, n, rc;
+ bool rcop= true;
+ PCOL colp;
+//PCOLUMN cp;
+ PDBUSER dup= PlgGetUser(g);
+
+ if (trace)
+ printf("CntOpenTable: tdbp=%p mode=%d\n", tdbp, mode);
+
+ if (!tdbp) {
+ strcpy(g->Message, "Null tdbp");
+ printf("CntOpenTable: %s\n", g->Message);
+ return true;
+ } // endif tdbp
+
+ // Save stack and allocation environment and prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ return true;
+ } // endif jump_level
+
+ if ((rc= setjmp(g->jumper[++g->jump_level])) != 0) {
+ goto err;
+ } // endif rc
+
+ if (!c1) {
+ if (mode == MODE_INSERT)
+ // Allocate all column blocks for that table
+ tdbp->ColDB(g, NULL, 0);
+
+ } else for (p= c1; *p; p+= n) {
+ // Allocate only used column blocks
+ if (trace)
+ printf("Allocating column %s\n", p);
+
+// if (*p == '*') {
+// // This is a special column
+// cp= new(g) COLUMN(p + 1);
+// cp->SetTo_Table(tdbp->GetTable());
+// colp= ((PTDBASE)tdbp)->InsertSpcBlk(g, cp);
+// } else
+ colp= tdbp->ColDB(g, p, 0);
+
+ if (!colp) {
+ sprintf(g->Message, "Column %s not found in %s", p, tdbp->GetName());
+ goto err;
+ } // endif colp
+
+ n= strlen(p) + 1;
+ } // endfor p
+
+ for (i= 0, colp= tdbp->GetColumns(); colp; i++, colp= colp->GetNext()) {
+ if (colp->InitValue(g))
+ goto err;
+
+ if (mode == MODE_INSERT)
+ // Allow type conversion
+ if (colp->SetBuffer(g, colp->GetValue(), true, false))
+ goto err;
+
+ colp->AddColUse(U_P); // For PLG tables
+ } // endfor colp
+
+ /*********************************************************************/
+ /* In Update mode, the updated column blocks must be distinct from */
+ /* the read column blocks. So make a copy of the TDB and allocate */
+ /* its column blocks in mode write (required by XML tables). */
+ /*********************************************************************/
+ if (mode == MODE_UPDATE) {
+ PTDBASE utp;
+
+ if (!(utp= (PTDBASE)tdbp->Duplicate(g))) {
+ sprintf(g->Message, MSG(INV_UPDT_TABLE), tdbp->GetName());
+ goto err;
+ } // endif tp
+
+ if (!c2)
+ // Allocate all column blocks for that table
+ utp->ColDB(g, NULL, 0);
+ else for (p= c2; *p; p+= n) {
+ // Allocate only used column blocks
+ colp= utp->ColDB(g, p, 0);
+ n= strlen(p) + 1;
+ } // endfor p
+
+ for (i= 0, colp= utp->GetColumns(); colp; i++, colp= colp->GetNext()) {
+ if (colp->InitValue(g))
+ goto err;
+
+ if (colp->SetBuffer(g, colp->GetValue(), true, false))
+ goto err;
+
+ } // endfor colp
+
+ // Attach the updated columns list to the main table
+ ((PTDBASE)tdbp)->SetSetCols(utp->GetColumns());
+ } else if (tdbp && mode == MODE_INSERT)
+ ((PTDBASE)tdbp)->SetSetCols(tdbp->GetColumns());
+
+ // Now do open the physical table
+ if (trace)
+ printf("Opening table %s in mode %d tdbp=%p\n",
+ tdbp->GetName(), mode, tdbp);
+
+//tdbp->SetMode(mode);
+
+ if (del && ((PTDBASE)tdbp)->GetFtype() != RECFM_NAF) {
+ // To avoid erasing the table when doing a partial delete
+ // make a fake Next
+ PDOSDEF ddp= new(g) DOSDEF;
+ PTDB tp= new(g) TDBDOS(ddp, NULL);
+ tdbp->SetNext(tp);
+ dup->Check &= ~CHK_DELETE;
+ } // endif del
+
+
+ if (trace)
+ printf("About to open the table: tdbp=%p\n", tdbp);
+
+ if (mode != MODE_ANY && mode != MODE_ALTER) {
+ if (tdbp->OpenDB(g)) {
+ printf("%s\n", g->Message);
+ goto err;
+ } else
+ tdbp->SetNext(NULL);
+
+ } // endif mode
+
+ rcop= false;
+
+ err:
+ g->jump_level--;
+ return rcop;
+ } // end of CntOpenTable
+
+/***********************************************************************/
+/* Rewind a table by reopening it. */
+/***********************************************************************/
+bool CntRewindTable(PGLOBAL g, PTDB tdbp)
+{
+ if (!tdbp)
+ return true;
+
+ tdbp->OpenDB(g);
+ return false;
+} // end of CntRewindTable
+
+/***********************************************************************/
+/* Evaluate all columns after a record is read. */
+/***********************************************************************/
+RCODE EvalColumns(PGLOBAL g, PTDB tdbp, bool mrr)
+ {
+ RCODE rc= RC_OK;
+ PCOL colp;
+
+ // Save stack and allocation environment and prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ if (trace) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ printf("EvalColumns: %s\n", g->Message);
+ } // endif
+
+ return RC_FX;
+ } // endif jump_level
+
+ if (setjmp(g->jumper[++g->jump_level]) != 0) {
+ if (trace)
+ printf("Error reading columns: %s\n", g->Message);
+
+ rc= RC_FX;
+ goto err;
+ } // endif rc
+
+ for (colp= tdbp->GetColumns(); rc == RC_OK && colp;
+ colp= colp->GetNext()) {
+ colp->Reset();
+
+ // Virtual columns are computed by MariaDB
+ if (!colp->GetColUse(U_VIRTUAL) && (!mrr || colp->GetKcol()))
+ if (colp->Eval(g))
+ rc= RC_FX;
+
+ } // endfor colp
+
+ err:
+ g->jump_level--;
+ return rc;
+ } // end of EvalColumns
+
+/***********************************************************************/
+/* ReadNext: Read next record sequentially. */
+/***********************************************************************/
+RCODE CntReadNext(PGLOBAL g, PTDB tdbp)
+ {
+ RCODE rc;
+
+ if (!tdbp)
+ return RC_FX;
+ else if (((PTDBASE)tdbp)->GetKindex()) {
+ // Reading sequencially an indexed table. This happens after the
+ // handler function records_in_range was called and MySQL decides
+ // to quit using the index (!!!) Drop the index.
+ for (PCOL colp= tdbp->GetColumns(); colp; colp= colp->GetNext())
+ colp->SetKcol(NULL);
+
+ ((PTDBASE)tdbp)->SetKindex(NULL);
+ } // endif index
+
+ // Save stack and allocation environment and prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ return RC_FX;
+ } // endif jump_level
+
+ if ((setjmp(g->jumper[++g->jump_level])) != 0) {
+ rc= RC_FX;
+ goto err;
+ } // endif rc
+
+ while ((rc= (RCODE)tdbp->ReadDB(g)) == RC_NF) ;
+
+ err:
+ g->jump_level--;
+ return (rc != RC_OK) ? rc : EvalColumns(g, tdbp);
+ } // end of CntReadNext
+
+/***********************************************************************/
+/* WriteRow: Insert a new row into a table. */
+/***********************************************************************/
+RCODE CntWriteRow(PGLOBAL g, PTDB tdbp)
+ {
+ RCODE rc;
+ PCOL colp;
+ PTDBASE tp= (PTDBASE)tdbp;
+
+ if (!tdbp)
+ return RC_FX;
+
+ // Save stack and allocation environment and prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ return RC_FX;
+ } // endif jump_level
+
+ if (setjmp(g->jumper[++g->jump_level]) != 0) {
+ printf("%s\n", g->Message);
+ rc= RC_FX;
+ goto err;
+ } // endif rc
+
+ // Store column values in table write buffer(s)
+ for (colp= tp->GetSetCols(); colp; colp= colp->GetNext())
+ if (!colp->GetColUse(U_VIRTUAL))
+ colp->WriteColumn(g);
+
+// if (tdbp->GetMode() == MODE_INSERT)
+// tbxp->SetModified(true);
+
+ // Return result code from write operation
+ rc= (RCODE)tdbp->WriteDB(g);
+
+ err:
+ g->jump_level--;
+ return rc;
+ } // end of CntWriteRow
+
+/***********************************************************************/
+/* UpdateRow: Update a row into a table. */
+/***********************************************************************/
+RCODE CntUpdateRow(PGLOBAL g, PTDB tdbp)
+ {
+ if (!tdbp || tdbp->GetMode() != MODE_UPDATE)
+ return RC_FX;
+
+ // Return result code from write operation
+ return CntWriteRow(g, tdbp);
+ } // end of CntUpdateRow
+
+/***********************************************************************/
+/* DeleteRow: Delete a row from a table. */
+/***********************************************************************/
+RCODE CntDeleteRow(PGLOBAL g, PTDB tdbp, bool all)
+ {
+ RCODE rc;
+
+ if (!tdbp || tdbp->GetMode() != MODE_DELETE)
+ return RC_FX;
+ else if (tdbp->IsReadOnly())
+ return RC_NF;
+
+ if (((PTDBASE)tdbp)->GetDef()->Indexable() && all)
+ ((PTDBDOS)tdbp)->Cardinal= 0;
+
+ // Return result code from delete operation
+ // Note: if all, this call will be done when closing the table
+ rc= (RCODE)tdbp->DeleteDB(g, (all) ? RC_FX : RC_OK);
+ return rc;
+ } // end of CntDeleteRow
+
+/***********************************************************************/
+/* CLOSETAB: Close a table. */
+/***********************************************************************/
+int CntCloseTable(PGLOBAL g, PTDB tdbp)
+ {
+ int rc= RC_OK;
+ TDBDOX *tbxp= NULL;
+
+ if (!tdbp || tdbp->GetUse() != USE_OPEN)
+ return rc; // Nothing to do
+
+ if (trace)
+ printf("CntCloseTable: tdbp=%p mode=%d\n", tdbp, tdbp->GetMode());
+
+ if (tdbp->GetMode() == MODE_DELETE && tdbp->GetUse() == USE_OPEN)
+ rc= tdbp->DeleteDB(g, RC_EF); // Specific A.M. delete routine
+
+ // Prepare error return
+ if (g->jump_level == MAX_JUMP) {
+ strcpy(g->Message, MSG(TOO_MANY_JUMPS));
+ rc= RC_FX;
+ goto err;
+ } // endif
+
+ if ((rc = setjmp(g->jumper[++g->jump_level])) != 0) {
+ g->jump_level--;
+ goto err;
+ } // endif
+
+ // This will close the table file(s) and also finalize write
+ // operations such as Insert, Update, or Delete.
+ tdbp->CloseDB(g);
+
+ g->jump_level--;
+
+ if (trace > 1)
+ printf("Table %s closed\n", tdbp->GetName());
+
+//if (!((PTDBDOX)tdbp)->GetModified())
+// return 0;
+
+ if (tdbp->GetMode() == MODE_READ || tdbp->GetMode() == MODE_ANY)
+ return 0;
+
+ if (trace > 1)
+ printf("About to reset opt\n");
+
+ // Make all the eventual indexes
+ tbxp= (TDBDOX*)tdbp;
+ tbxp->SetKindex(NULL);
+ tbxp->To_Key_Col= NULL;
+ rc= tbxp->ResetTableOpt(g, false, ((PTDBASE)tdbp)->GetDef()->Indexable());
+
+ err:
+ if (trace > 1)
+ printf("Done rc=%d\n", rc);
+
+ return (rc == RC_OK || rc == RC_INFO) ? 0 : rc;
+ } // end of CntCloseTable
+
+/***********************************************************************/
+/* Load and initialize the use of an index. */
+/* This is the condition(s) for doing indexing. */
+/* Note: FIX table are not reset here to Nrec= 1. */
+/***********************************************************************/
+int CntIndexInit(PGLOBAL g, PTDB ptdb, int id)
+ {
+ int k;
+ PCOL colp;
+ PVAL valp;
+ PKXBASE xp;
+ PXLOAD pxp;
+ PIXDEF xdp;
+ XKPDEF *kdp;
+ PTDBDOX tdbp;
+ PCOLDEF cdp;
+ DOXDEF *dfp;
+
+ if (!ptdb)
+ return -1;
+ else if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
+ sprintf(g->Message, "CntIndexInit: Table %s is not indexable", ptdb->GetName());
+ return 0;
+ } else
+ tdbp= (PTDBDOX)ptdb;
+
+ dfp= (DOXDEF*)tdbp->To_Def;
+
+//if (!(k= colp->GetKey()))
+// if (colp->GetOpt() >= 2) {
+// strcpy(g->Message, "Not a valid indexed column");
+// return -1;
+// } else
+ // This is a pseudo indexed sorted block optimized column
+// return 0;
+
+ if (tdbp->To_Kindex)
+ if (((XXBASE*)tdbp->To_Kindex)->GetID() == id) {
+ tdbp->To_Kindex->Reset(); // Same index
+ return (tdbp->To_Kindex->IsMul()) ? 2 : 1;
+ } else {
+ tdbp->To_Kindex->Close();
+ tdbp->To_Kindex= NULL;
+ } // endif colp
+
+ for (xdp= dfp->To_Indx; xdp; xdp= xdp->GetNext())
+ if (xdp->GetID() == id)
+ break;
+
+ if (!xdp) {
+ sprintf(g->Message, "Wrong index ID %d", id);
+ return 0;
+ } // endif xdp
+
+ // Allocate the key columns definition block
+ tdbp->Knum= xdp->GetNparts();
+ tdbp->To_Key_Col= (PCOL*)PlugSubAlloc(g, NULL, tdbp->Knum * sizeof(PCOL));
+
+ // Get the key column description list
+ for (k= 0, kdp= (XKPDEF*)xdp->GetToKeyParts(); kdp; kdp= (XKPDEF*)kdp->Next)
+ if (!(colp= tdbp->ColDB(g, kdp->Name, 0)) || colp->InitValue(g)) {
+ sprintf(g->Message, "Wrong column %s", kdp->Name);
+ return 0;
+ } else
+ tdbp->To_Key_Col[k++]= colp;
+
+#if defined(_DEBUG)
+ if (k != tdbp->Knum) {
+ sprintf(g->Message, "Key part number mismatch for %s",
+ xdp->GetName());
+ return 0;
+ } // endif k
+#endif // _DEBUG
+
+ // Allocate the pseudo constants that will contain the key values
+ tdbp->To_Link= (PXOB*)PlugSubAlloc(g, NULL, tdbp->Knum * sizeof(PXOB));
+
+ for (k= 0, kdp= (XKPDEF*)xdp->GetToKeyParts();
+ kdp; k++, kdp= (XKPDEF*)kdp->Next) {
+ cdp= tdbp->Key(k)->GetCdp();
+ valp= AllocateValue(g, cdp->GetType(), cdp->GetLength());
+ tdbp->To_Link[k]= new(g) CONSTANT(valp);
+ } // endfor k
+
+ // Make the index on xdp
+ if (!xdp->IsAuto()) {
+ if (dfp->Huge)
+ pxp= new(g) XHUGE;
+ else
+ pxp= new(g) XFILE;
+
+ if (tdbp->Knum == 1) // Single index
+ xp= new(g) XINDXS(tdbp, xdp, pxp, tdbp->To_Key_Col, tdbp->To_Link);
+ else // Multi-Column index
+ xp= new(g) XINDEX(tdbp, xdp, pxp, tdbp->To_Key_Col, tdbp->To_Link);
+
+ } else // Column contains same values as ROWID
+ xp= new(g) XXROW(tdbp);
+
+ if (xp->Init(g))
+ return 0;
+
+ tdbp->To_Kindex= xp;
+ return (xp->IsMul()) ? 2 : 1;
+ } // end of CntIndexInit
+
+/***********************************************************************/
+/* IndexRead: fetch a record having the index value. */
+/***********************************************************************/
+RCODE CntIndexRead(PGLOBAL g, PTDB ptdb, OPVAL op,
+ const void *key, int len, bool mrr)
+ {
+ char *kp= (char*)key;
+ int n;
+ short lg;
+ bool rcb;
+ RCODE rc;
+ PVAL valp;
+ PCOL colp;
+ XXBASE *xbp;
+ PTDBDOX tdbp;
+
+ if (!ptdb)
+ return RC_FX;
+ if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
+ sprintf(g->Message, "CntIndexRead: Table %s is not indexable", ptdb->GetName());
+ return RC_FX;
+ } else
+ tdbp= (PTDBDOX)ptdb;
+
+ // Set reference values and index operator
+ if (!tdbp->To_Link || !tdbp->To_Kindex) {
+ sprintf(g->Message, "Index not initialized for table %s", tdbp->Name);
+ return RC_FX;
+ } else
+ xbp= (XXBASE*)tdbp->To_Kindex;
+
+ if (key) {
+ for (n= 0; n < tdbp->Knum; n++) {
+ colp= (PCOL)tdbp->To_Key_Col[n];
+
+ if (colp->GetColUse(U_NULLS))
+ kp++; // Skip null byte
+
+ valp= tdbp->To_Link[n]->GetValue();
+
+ if (!valp->IsTypeNum()) {
+ if (colp->GetColUse(U_VAR)) {
+ lg= *(short*)kp;
+ kp+= sizeof(short);
+ rcb= valp->SetValue_char(kp, (int)lg);
+ } else
+ rcb= valp->SetValue_char(kp, valp->GetClen());
+
+ if (rcb) {
+ if (tdbp->RowNumber(g))
+ sprintf(g->Message, "Out of range value for column %s at row %d",
+ colp->GetName(), tdbp->RowNumber(g));
+ else
+ sprintf(g->Message, "Out of range value for column %s",
+ colp->GetName());
+
+ PushWarning(g, tdbp);
+ } // endif b
+
+ } else
+ valp->SetBinValue((void*)kp);
+
+ kp+= valp->GetClen();
+
+ if (len == kp - (char*)key) {
+ n++;
+ break;
+ } else if (len < kp - (char*)key) {
+ strcpy(g->Message, "Key buffer is too small");
+ return RC_FX;
+ } // endif len
+
+ } // endfor n
+
+ xbp->SetNval(n);
+ } // endif key
+
+ xbp->SetOp(op);
+ xbp->SetNth(0);
+
+ if ((rc= (RCODE)tdbp->ReadDB(g)) == RC_OK)
+ rc= EvalColumns(g, tdbp, mrr);
+
+ return rc;
+ } // end of CntIndexRead
+
+/***********************************************************************/
+/* Return the number of rows matching given values. */
+/***********************************************************************/
+int CntIndexRange(PGLOBAL g, PTDB ptdb, const uchar* *key, uint *len,
+ bool *incl, key_part_map *kmap)
+ {
+ const uchar *p, *kp;
+ int i, n, k[2];
+ short lg;
+ bool b, rcb;
+ PVAL valp;
+ PCOL colp;
+ PTDBDOX tdbp;
+ XXBASE *xbp;
+
+ if (!ptdb)
+ return -1;
+ else if (!((PTDBASE)ptdb)->GetDef()->Indexable()) {
+ sprintf(g->Message, "CntIndexRange: Table %s is not indexable", ptdb->GetName());
+ DBUG_PRINT("Range", ("%s", g->Message));
+ return -1;
+ } else
+ tdbp= (PTDBDOX)ptdb;
+
+ if (!tdbp->To_Link || !tdbp->To_Kindex) {
+ sprintf(g->Message, "Index not initialized for table %s", tdbp->Name);
+ DBUG_PRINT("Range", ("%s", g->Message));
+ return -1;
+ } else
+ xbp= (XXBASE*)tdbp->To_Kindex;
+
+ for (b= false, i= 0; i < 2; i++) {
+ p= kp= key[i];
+
+ if (kp) {
+ for (n= 0; n < tdbp->Knum; n++) {
+ if (kmap[i] & (key_part_map)(1 << n)) {
+ if (b == true)
+ // Cannot do indexing with missing intermediate key
+ return -1;
+
+ colp= (PCOL)tdbp->To_Key_Col[n];
+
+ if (colp->GetColUse(U_NULLS))
+ p++; // Skip null byte ???
+
+ valp= tdbp->To_Link[n]->GetValue();
+
+ if (!valp->IsTypeNum()) {
+ if (colp->GetColUse(U_VAR)) {
+ lg= *(short*)p;
+ p+= sizeof(short);
+ rcb= valp->SetValue_char((char*)p, (int)lg);
+ } else
+ rcb= valp->SetValue_char((char*)p, valp->GetClen());
+
+ if (rcb) {
+ if (tdbp->RowNumber(g))
+ sprintf(g->Message,
+ "Out of range value for column %s at row %d",
+ colp->GetName(), tdbp->RowNumber(g));
+ else
+ sprintf(g->Message, "Out of range value for column %s",
+ colp->GetName());
+
+ PushWarning(g, tdbp);
+ } // endif b
+
+ } else
+ valp->SetBinValue((void*)p);
+
+ if (trace) {
+ char bf[32];
+ printf("i=%d n=%d key=%s\n", i, n, valp->GetCharString(bf));
+ } // endif trace
+
+ p+= valp->GetClen();
+
+ if (len[i] == (unsigned)(p - kp)) {
+ n++;
+ break;
+ } else if (len[i] < (unsigned)(p - kp)) {
+ strcpy(g->Message, "Key buffer is too small");
+ return -1;
+ } // endif len
+
+ } else
+ b= true;
+
+ } // endfor n
+
+ xbp->SetNval(n);
+
+ if (trace)
+ printf("xbp=%p Nval=%d i=%d incl=%d\n", xbp, n, i, incl[i]);
+
+ k[i]= xbp->Range(g, i + 1, incl[i]);
+ } else
+ k[i]= (i) ? xbp->GetNum_K() : 0;
+
+ } // endfor i
+
+ if (trace)
+ printf("k1=%d k0=%d\n", k[1], k[0]);
+
+ return k[1] - k[0];
+ } // end of CntIndexRange
diff --git a/storage/connect/csort.h b/storage/connect/csort.h
index 1808f640788..55ff6268a4b 100644
--- a/storage/connect/csort.h
+++ b/storage/connect/csort.h
@@ -22,9 +22,7 @@
#define THRESH 4 /* Threshold for insertion (was 4) */
#define MTHRESH 6 /* Threshold for median */
-#ifdef DEBTRACE
-extern FILE *debug; /* Debug file */
-#endif
+//extern FILE *debug; /* Debug file */
typedef int* const CPINT;
diff --git a/storage/connect/filamap.cpp b/storage/connect/filamap.cpp
index 0c68a599496..87cb2e83cfa 100644
--- a/storage/connect/filamap.cpp
+++ b/storage/connect/filamap.cpp
@@ -46,6 +46,8 @@
#include "filamap.h"
#include "tabdos.h"
+extern "C" int trace;
+
/* --------------------------- Class MAPFAM -------------------------- */
/***********************************************************************/
@@ -89,9 +91,8 @@ int MAPFAM::GetFileLength(PGLOBAL g)
len = (To_Fb) ? To_Fb->Length : TXTFAM::GetFileLength(g);
-#ifdef DEBTRACE
- htrc("Mapped file length=%d\n", len);
-#endif
+ if (trace)
+ htrc("Mapped file length=%d\n", len);
return len;
} // end of GetFileLength
@@ -166,9 +167,9 @@ bool MAPFAM::OpenTableFile(PGLOBAL g)
sprintf(g->Message, MSG(OPEN_MODE_ERROR),
"map", (int) rc, filename);
-#ifdef DEBTRACE
- htrc("%s\n", g->Message);
-#endif
+ if (trace)
+ htrc("CreateFileMap: %s\n", g->Message);
+
return (mode == MODE_READ && rc == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif hFile
@@ -228,10 +229,9 @@ bool MAPFAM::OpenTableFile(PGLOBAL g)
Fpos = Mempos = Memory;
Top = Memory + len;
-#ifdef DEBTRACE
- htrc("fp=%p count=%d MapView=%p len=%d Top=%p\n",
- fp, fp->Count, Memory, len, Top);
-#endif
+ if (trace)
+ htrc("fp=%p count=%d MapView=%p len=%d Top=%p\n",
+ fp, fp->Count, Memory, len, Top);
return AllocateBuffer(g); // Useful for DBF files
} // end of OpenTableFile
@@ -383,20 +383,19 @@ int MAPFAM::DeleteRecords(PGLOBAL g, int irc)
{
int n;
-#ifdef DEBTRACE
- fprintf(debug,
- "MAP DeleteDB: irc=%d mempos=%p tobuf=%p Tpos=%p Spos=%p\n",
- irc, Mempos, To_Buf, Tpos, Spos);
-#endif
+ if (trace)
+ htrc("MAP DeleteDB: irc=%d mempos=%p tobuf=%p Tpos=%p Spos=%p\n",
+ irc, Mempos, To_Buf, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the top of map position. */
/*******************************************************************/
Fpos = Top;
-#ifdef DEBTRACE
- htrc("Fpos placed at file top=%p\n", Fpos);
-#endif
+
+ if (trace)
+ htrc("Fpos placed at file top=%p\n", Fpos);
+
} // endif irc
if (Tpos == Spos)
@@ -412,17 +411,16 @@ int MAPFAM::DeleteRecords(PGLOBAL g, int irc)
memmove(Tpos, Spos, n);
Tpos += n;
-#ifdef DEBTRACE
- htrc("move %d bytes\n", n);
-#endif
+ if (trace)
+ htrc("move %d bytes\n", n);
+
} // endif n
if (irc == RC_OK) {
Spos = Mempos; // New start position
-#ifdef DEBTRACE
- htrc("after: Tpos=%p Spos=%p\n", Tpos, Spos);
-#endif
+ if (trace)
+ htrc("after: Tpos=%p Spos=%p\n", Tpos, Spos);
} else if (To_Fb) { // Can be NULL for deleted files
/*******************************************************************/
@@ -450,9 +448,8 @@ int MAPFAM::DeleteRecords(PGLOBAL g, int irc)
return RC_FX;
} // endif
-#ifdef DEBTRACE
- htrc("done, Tpos=%p newsize=%d drc=%d\n", Tpos, n, drc);
-#endif
+ if (trace)
+ htrc("done, Tpos=%p newsize=%d drc=%d\n", Tpos, n, drc);
if (!SetEndOfFile(fp->Handle)) {
sprintf(g->Message, MSG(FUNCTION_ERROR),
@@ -484,10 +481,10 @@ void MAPFAM::CloseTableFile(PGLOBAL g)
PlugCloseFile(g, To_Fb);
To_Fb = NULL; // To get correct file size in Cardinality
-#ifdef DEBTRACE
- htrc("MAP Close: closing %s count=%d\n",
- To_File, (To_Fb) ? To_Fb->Count : 0);
-#endif
+ if (trace)
+ htrc("MAP Close: closing %s count=%d\n",
+ To_File, (To_Fb) ? To_Fb->Count : 0);
+
} // end of CloseTableFile
/***********************************************************************/
diff --git a/storage/connect/filamdbf.cpp b/storage/connect/filamdbf.cpp
index 2b56e5c3d2f..7ac81117f35 100644
--- a/storage/connect/filamdbf.cpp
+++ b/storage/connect/filamdbf.cpp
@@ -485,16 +485,15 @@ bool DBFFAM::OpenTableFile(PGLOBAL g)
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (!(Stream = PlugOpenFile(g, filename, opmode))) {
-#ifdef DEBTRACE
- htrc("%s\n", g->Message);
-#endif
+ if (trace)
+ htrc("%s\n", g->Message);
+
return (mode == MODE_READ && errno == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif Stream
-#ifdef DEBTRACE
- htrc("File %s is open in mode %s\n", filename, opmode);
-#endif
+ if (trace)
+ htrc("File %s is open in mode %s\n", filename, opmode);
To_Fb = dbuserp->Openlist; // Keep track of File block
@@ -853,10 +852,10 @@ void DBFFAM::CloseTableFile(PGLOBAL g)
rc = PlugCloseFile(g, To_Fb);
fin:
-#ifdef DEBTRACE
- htrc("DBF CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
- To_File, mode, wrc, rc);
-#endif
+ if (trace)
+ htrc("DBF CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
+ To_File, mode, wrc, rc);
+
Stream = NULL; // So we can know whether table is open
} // end of CloseTableFile
diff --git a/storage/connect/filamfix.cpp b/storage/connect/filamfix.cpp
index daf133d4203..50c5dd9ede1 100644
--- a/storage/connect/filamfix.cpp
+++ b/storage/connect/filamfix.cpp
@@ -52,6 +52,7 @@
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
+extern "C" int trace;
extern int num_read, num_there, num_eq[2]; // Statistics
/* --------------------------- Class FIXFAM -------------------------- */
@@ -241,9 +242,8 @@ int FIXFAM::ReadBuffer(PGLOBAL g)
return RC_FX;
} // endif fseek
-#ifdef DEBTRACE
- htrc("File position is now %d\n", ftell(Stream));
-#endif
+ if (trace > 1)
+ htrc("File position is now %d\n", ftell(Stream));
//long tell = ftell(Stream); not used
@@ -266,9 +266,9 @@ int FIXFAM::ReadBuffer(PGLOBAL g)
sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
#endif
-#ifdef DEBTRACE
- htrc("%s\n", g->Message);
-#endif
+ if (trace)
+ htrc("%s\n", g->Message);
+
return RC_FX;
} // endelse
@@ -283,11 +283,9 @@ int FIXFAM::ReadBuffer(PGLOBAL g)
/***********************************************************************/
int FIXFAM::WriteBuffer(PGLOBAL g)
{
-#ifdef DEBTRACE
- fprintf(debug,
- "FIX WriteDB: Mode=%d buf=%p line=%p Nrec=%d Rbuf=%d CurNum=%d\n",
- Tdbp->GetMode(), To_Buf, Tdbp->GetLine(), Nrec, Rbuf, CurNum);
-#endif
+ if (trace > 1)
+ htrc("FIX WriteDB: Mode=%d buf=%p line=%p Nrec=%d Rbuf=%d CurNum=%d\n",
+ Tdbp->GetMode(), To_Buf, Tdbp->GetLine(), Nrec, Rbuf, CurNum);
if (Tdbp->GetMode() == MODE_INSERT) {
/*******************************************************************/
@@ -298,9 +296,8 @@ int FIXFAM::WriteBuffer(PGLOBAL g)
return RC_OK; // We write only full blocks
} // endif CurNum
-#ifdef DEBTRACE
- htrc(" First line is '%.*s'\n", Lrecl - 2, To_Buf);
-#endif
+ if (trace > 1)
+ htrc(" First line is '%.*s'\n", Lrecl - 2, To_Buf);
// Now start the writing process.
if (fwrite(To_Buf, Lrecl, Rbuf, Stream) != (size_t)Rbuf) {
@@ -313,9 +310,8 @@ int FIXFAM::WriteBuffer(PGLOBAL g)
CurNum = 0;
Tdbp->SetLine(To_Buf);
-#ifdef DEBTRACE
- htrc("write done\n");
-#endif
+ if (trace > 1)
+ htrc("write done\n");
} else { // Mode == MODE_UPDATE
// T_Stream is the temporary stream or the table file stream itself
@@ -353,20 +349,19 @@ int FIXFAM::DeleteRecords(PGLOBAL g, int irc)
/* file, and at the end erase all trailing records. */
/* This will be experimented. */
/*********************************************************************/
-#ifdef DEBTRACE
- fprintf(debug,
- "DOS DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
- irc, UseTemp, Fpos, Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("DOS DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
+ irc, UseTemp, Fpos, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the end-of-file position. */
/*******************************************************************/
Fpos = Tdbp->Cardinality(g);
-#ifdef DEBTRACE
- htrc("Fpos placed at file end=%d\n", Fpos);
-#endif
+
+ if (trace > 1)
+ htrc("Fpos placed at file end=%d\n", Fpos);
+
} else // Fpos is the deleted line position
Fpos = CurBlk * Nrec + CurNum;
@@ -414,9 +409,8 @@ int FIXFAM::DeleteRecords(PGLOBAL g, int irc)
OldBlk = -2; // To force fseek to be executed on next block
} // endif moved
-#ifdef DEBTRACE
- htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
} else {
/*******************************************************************/
@@ -464,9 +458,9 @@ int FIXFAM::DeleteRecords(PGLOBAL g, int irc)
close(h);
-#ifdef DEBTRACE
- htrc("done, h=%d irc=%d\n", h, irc);
-#endif
+ if (trace > 1)
+ htrc("done, h=%d irc=%d\n", h, irc);
+
} // endif UseTemp
} // endif irc
@@ -496,9 +490,8 @@ bool FIXFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
req = (size_t)min(n, Dbflen);
len = fread(DelBuf, Lrecl, req, Stream);
-#ifdef DEBTRACE
- htrc("after read req=%d len=%d\n", req, len);
-#endif
+ if (trace > 1)
+ htrc("after read req=%d len=%d\n", req, len);
if (len != req) {
sprintf(g->Message, MSG(DEL_READ_ERROR), (int) req, (int) len);
@@ -516,16 +509,14 @@ bool FIXFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
return true;
} // endif
-#ifdef DEBTRACE
- htrc("after write pos=%d\n", ftell(Stream));
-#endif
+ if (trace > 1)
+ htrc("after write pos=%d\n", ftell(Stream));
Tpos += (int)req;
Spos += (int)req;
-#ifdef DEBTRACE
- htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
*b = true;
} // endfor n
@@ -574,10 +565,10 @@ void FIXFAM::CloseTableFile(PGLOBAL g)
rc = PlugCloseFile(g, To_Fb);
fin:
-#ifdef DEBTRACE
- htrc("FIX CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
- To_File, mode, wrc, rc);
-#endif
+ if (trace)
+ htrc("FIX CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
+ To_File, mode, wrc, rc);
+
Stream = NULL; // So we can know whether table is open
} // end of CloseTableFile
@@ -641,9 +632,8 @@ int BGXFAM::BigRead(PGLOBAL g, HANDLE h, void *inbuf, int req)
DWORD nbr, drc, len = (DWORD)req;
bool brc = ReadFile(h, inbuf, len, &nbr, NULL);
-#ifdef DEBTRACE
- htrc("after read req=%d brc=%d nbr=%d\n", req, brc, nbr);
-#endif
+ if (trace > 1)
+ htrc("after read req=%d brc=%d nbr=%d\n", req, brc, nbr);
if (!brc) {
char buf[256]; // , *fn = (h == Hfile) ? To_File : "Tempfile";
@@ -653,9 +643,10 @@ int BGXFAM::BigRead(PGLOBAL g, HANDLE h, void *inbuf, int req)
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)buf, sizeof(buf), NULL);
sprintf(g->Message, MSG(READ_ERROR), To_File, buf);
-#ifdef DEBTRACE
- htrc("BIGREAD: %s\n", g->Message);
-#endif
+
+ if (trace > 1)
+ htrc("BIGREAD: %s\n", g->Message);
+
rc = -1;
} else
rc = (int)nbr;
@@ -680,9 +671,8 @@ bool BGXFAM::BigWrite(PGLOBAL g, HANDLE h, void *inbuf, int req)
DWORD nbw, drc, len = (DWORD)req;
bool brc = WriteFile(h, inbuf, len, &nbw, NULL);
-#ifdef DEBTRACE
- htrc("after write req=%d brc=%d nbw=%d\n", req, brc, nbw);
-#endif
+ if (trace > 1)
+ htrc("after write req=%d brc=%d nbw=%d\n", req, brc, nbw);
if (!brc || nbw != len) {
char buf[256], *fn = (h == Hfile) ? To_File : "Tempfile";
@@ -698,10 +688,10 @@ bool BGXFAM::BigWrite(PGLOBAL g, HANDLE h, void *inbuf, int req)
sprintf(g->Message, MSG(WRITE_STRERROR), fn, buf);
-#ifdef DEBTRACE
- htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
- nbw, len, drc, g->Message);
-#endif
+ if (trace > 1)
+ htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
+ nbw, len, drc, g->Message);
+
rc = true;
} // endif brc || nbw
#else // !WIN32
@@ -712,10 +702,11 @@ bool BGXFAM::BigWrite(PGLOBAL g, HANDLE h, void *inbuf, int req)
const char *fn = (h == Hfile) ? To_File : "Tempfile";
sprintf(g->Message, MSG(WRITE_STRERROR), fn, strerror(errno));
-#ifdef DEBTRACE
- htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
- nbw, len, errno, g->Message);
-#endif
+
+ if (trace > 1)
+ htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
+ nbw, len, errno, g->Message);
+
rc = true;
} // endif nbr
#endif // !WIN32
@@ -750,9 +741,8 @@ bool BGXFAM::OpenTableFile(PGLOBAL g)
PlugSetPath(filename, To_File, Tdbp->GetPath());
-#ifdef DEBTRACE
- htrc("OpenTableFile: filename=%s mode=%d\n", filename, mode);
-#endif
+ if (trace)
+ htrc("OpenTableFile: filename=%s mode=%d\n", filename, mode);
#if defined(WIN32)
DWORD rc, access, creation, share = 0;
@@ -811,11 +801,9 @@ bool BGXFAM::OpenTableFile(PGLOBAL g)
} else
rc = 0;
-#ifdef DEBTRACE
- fprintf(debug,
- " rc=%d access=%p share=%p creation=%d handle=%p fn=%s\n",
- rc, access, share, creation, Hfile, filename);
-#endif
+ if (trace > 1)
+ htrc(" rc=%d access=%p share=%p creation=%d handle=%p fn=%s\n",
+ rc, access, share, creation, Hfile, filename);
if (mode == MODE_INSERT)
/*******************************************************************/
@@ -866,10 +854,9 @@ bool BGXFAM::OpenTableFile(PGLOBAL g)
} else
rc = 0;
-#ifdef DEBTRACE
- htrc(" rc=%d oflag=%p tmode=%p handle=%p fn=%s\n",
- rc, oflag, tmode, Hfile, filename);
-#endif
+ if (trace > 1)
+ htrc(" rc=%d oflag=%p tmode=%p handle=%p fn=%s\n",
+ rc, oflag, tmode, Hfile, filename);
#endif // UNIX
@@ -951,14 +938,13 @@ int BGXFAM::Cardinality(PGLOBAL g)
if (Hfile == INVALID_HANDLE_VALUE) {
int h = open64(filename, O_RDONLY, 0);
-#ifdef DEBTRACE
- htrc(" h=%d\n", h);
-#endif
+ if (trace)
+ htrc(" h=%d\n", h);
if (h == INVALID_HANDLE_VALUE) {
-#ifdef DEBTRACE
- htrc(" errno=%d ENOENT=%d\n", errno, ENOENT);
-#endif
+ if (trace)
+ htrc(" errno=%d ENOENT=%d\n", errno, ENOENT);
+
if (errno != ENOENT) {
sprintf(g->Message, MSG(OPEN_ERROR_IS),
filename, strerror(errno));
@@ -1000,10 +986,9 @@ int BGXFAM::Cardinality(PGLOBAL g)
} else
card = (int)(fsize / (BIGINT)Lrecl); // Fixed length file
-#ifdef DEBTRACE
- htrc(" Computed max_K=%d fsize=%lf lrecl=%d\n",
- card, (double)fsize, Lrecl);
-#endif
+ if (trace)
+ htrc(" Computed max_K=%d fsize=%lf lrecl=%d\n",
+ card, (double)fsize, Lrecl);
// Set number of blocks for later use
Block = (card + Nrec - 1) / Nrec;
@@ -1101,9 +1086,8 @@ int BGXFAM::ReadBuffer(PGLOBAL g)
if (BigSeek(g, Hfile, (BIGINT)Fpos * (BIGINT)Lrecl))
return RC_FX;
-#ifdef DEBTRACE
- htrc("File position is now %d\n", Fpos);
-#endif
+ if (trace > 1)
+ htrc("File position is now %d\n", Fpos);
nbr = BigRead(g, Hfile, To_Buf, (Padded) ? Blksize : Lrecl * Nrec);
@@ -1126,11 +1110,9 @@ int BGXFAM::ReadBuffer(PGLOBAL g)
/***********************************************************************/
int BGXFAM::WriteBuffer(PGLOBAL g)
{
-#ifdef DEBTRACE
- fprintf(debug,
- "BIG WriteDB: Mode=%d buf=%p line=%p Nrec=%d Rbuf=%d CurNum=%d\n",
- Tdbp->GetMode(), To_Buf, Tdbp->GetLine(), Nrec, Rbuf, CurNum);
-#endif
+ if (trace > 1)
+ htrc("BIG WriteDB: Mode=%d buf=%p line=%p Nrec=%d Rbuf=%d CurNum=%d\n",
+ Tdbp->GetMode(), To_Buf, Tdbp->GetLine(), Nrec, Rbuf, CurNum);
if (Tdbp->GetMode() == MODE_INSERT) {
/*******************************************************************/
@@ -1141,9 +1123,8 @@ int BGXFAM::WriteBuffer(PGLOBAL g)
return RC_OK; // We write only full blocks
} // endif CurNum
-#ifdef DEBTRACE
- htrc(" First line is '%.*s'\n", Lrecl - 2, To_Buf);
-#endif
+ if (trace > 1)
+ htrc(" First line is '%.*s'\n", Lrecl - 2, To_Buf);
// Now start the writing process.
if (BigWrite(g, Hfile, To_Buf, Lrecl * Rbuf))
@@ -1153,9 +1134,8 @@ int BGXFAM::WriteBuffer(PGLOBAL g)
CurNum = 0;
Tdbp->SetLine(To_Buf);
-#ifdef DEBTRACE
- htrc("write done\n");
-#endif
+ if (trace > 1)
+ htrc("write done\n");
} else { // Mode == MODE_UPDATE
// Tfile is the temporary file or the table file handle itself
@@ -1190,20 +1170,19 @@ int BGXFAM::DeleteRecords(PGLOBAL g, int irc)
/* file, and at the end erase all trailing records. */
/* This will be experimented. */
/*********************************************************************/
-#ifdef DEBTRACE
- fprintf(debug,
- "BGX DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
- irc, UseTemp, Fpos, Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("BGX DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
+ irc, UseTemp, Fpos, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the end-of-file position. */
/*******************************************************************/
Fpos = Tdbp->Cardinality(g);
-#ifdef DEBTRACE
- htrc("Fpos placed at file end=%d\n", Fpos);
-#endif
+
+ if (trace > 1)
+ htrc("Fpos placed at file end=%d\n", Fpos);
+
} else // Fpos is the deleted line position
Fpos = CurBlk * Nrec + CurNum;
@@ -1239,9 +1218,9 @@ int BGXFAM::DeleteRecords(PGLOBAL g, int irc)
return RC_FX;
if (irc == RC_OK) {
-#ifdef DEBTRACE
- assert(Spos == Fpos);
-#endif
+ if (trace)
+ assert(Spos == Fpos);
+
Spos++; // New start position is on next line
if (moved) {
@@ -1251,9 +1230,8 @@ int BGXFAM::DeleteRecords(PGLOBAL g, int irc)
OldBlk = -2; // To force fseek to be executed on next block
} // endif moved
-#ifdef DEBTRACE
- htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
} else {
/*******************************************************************/
@@ -1385,9 +1363,8 @@ bool BGXFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
Tpos += (int)req;
Spos += (int)req;
-#ifdef DEBTRACE
- htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
-#endif
+ if (trace > 1)
+ htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
*b = true;
} // endfor n
@@ -1435,10 +1412,10 @@ void BGXFAM::CloseTableFile(PGLOBAL g)
rc = PlugCloseFile(g, To_Fb);
fin:
-#ifdef DEBTRACE
- htrc("BGX CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
- To_File, mode, wrc, rc);
-#endif
+ if (trace)
+ htrc("BGX CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
+ To_File, mode, wrc, rc);
+
Hfile = INVALID_HANDLE_VALUE; // So we can know whether table is open
} // end of CloseTableFile
diff --git a/storage/connect/filamtxt.cpp b/storage/connect/filamtxt.cpp
index 24459509e1f..56d25e973f8 100644
--- a/storage/connect/filamtxt.cpp
+++ b/storage/connect/filamtxt.cpp
@@ -1,1442 +1,1444 @@
-/*********** File AM Txt C++ Program Source Code File (.CPP) ***********/
-/* PROGRAM NAME: FILAMTXT */
-/* ------------- */
-/* Version 1.5 */
-/* */
-/* COPYRIGHT: */
-/* ---------- */
-/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are the Text file access method classes. */
-/* */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant sections of the System header files. */
-/***********************************************************************/
-#include "my_global.h"
-#if defined(WIN32)
-#include <io.h>
-#include <fcntl.h>
-#include <errno.h>
-#if defined(__BORLANDC__)
-#define __MFC_COMPAT__ // To define min/max as macro
-#endif // __BORLANDC__
-//#include <windows.h>
-#else // !WIN32
-#if defined(UNIX) || defined(UNIV_LINUX)
-#include <errno.h>
-#include <unistd.h>
-//#if !defined(sun) // Sun has the ftruncate fnc.
-//#define USETEMP // Force copy mode for DELETE
-//#endif // !sun
-#else // !UNIX
-#include <io.h>
-#endif // !UNIX
-#include <fcntl.h>
-#endif // !WIN32
-
-/***********************************************************************/
-/* Include application header files: */
-/* global.h is header containing all global declarations. */
-/* plgdbsem.h is header containing the DB application declarations. */
-/* filamtxt.h is header containing the file AM classes declarations. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "filamtxt.h"
-#include "tabdos.h"
-
-#if defined(UNIX) || defined(UNIV_LINUX)
-#include "osutil.h"
-#define _fileno fileno
-#define _O_RDONLY O_RDONLY
-#endif
-
-extern int num_read, num_there, num_eq[2]; // Statistics
-extern "C" int trace;
-
-/* --------------------------- Class TXTFAM -------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-TXTFAM::TXTFAM(PDOSDEF tdp)
- {
- Tdbp = NULL;
- To_Fb = NULL;
- To_File = tdp->Fn;
- Lrecl = tdp->Lrecl;
- Placed = false;
- IsRead = true;
- Blocked = false;
- To_Buf = NULL;
- DelBuf = NULL;
- BlkPos = NULL;
- BlkLen = 0;
- Buflen = 0;
- Dbflen = 0;
- Rows = 0;
- DelRows = 0;
- Headlen = 0;
- Block = 0;
- Last = 0;
- Nrec = 1;
- OldBlk = -1;
- CurBlk = -1;
- ReadBlks = 0;
- CurNum = 0;
- Rbuf = 0;
- Modif = 0;
- Blksize = 0;
- Padded = false;
- Eof = tdp->Eof;
- Ending = tdp->Ending;
- CrLf = (char*)(Ending == 2 ? "\r\n" : "\n");
- } // end of TXTFAM standard constructor
-
-TXTFAM::TXTFAM(PTXF txfp)
- {
- Tdbp = txfp->Tdbp;
- To_Fb = txfp->To_Fb;
- To_File = txfp->To_File;
- Lrecl = txfp->Lrecl;
- Placed = txfp->Placed;
- IsRead = txfp->IsRead;
- Blocked = txfp->Blocked;
- To_Buf = txfp->To_Buf;
- DelBuf = txfp->DelBuf;
- BlkPos = txfp->BlkPos;
- BlkLen = txfp->BlkLen;
- Buflen = txfp->Buflen;
- Dbflen = txfp->Dbflen;
- Rows = txfp->Rows;
- DelRows = txfp->DelRows;
- Headlen = txfp->Headlen;
- Block = txfp->Block;
- Last = txfp->Last;
- Nrec = txfp->Nrec;
- OldBlk = txfp->OldBlk;
- CurBlk = txfp->CurBlk;
- ReadBlks = txfp->ReadBlks;
- CurNum = txfp->CurNum;
- Rbuf = txfp->Rbuf;
- Modif = txfp->Modif;
- Blksize = txfp->Blksize;
- Padded = txfp->Padded;
- Eof = txfp->Eof;
- Ending = txfp->Ending;
- } // end of TXTFAM copy constructor
-
-/***********************************************************************/
-/* Reset: reset position values at the beginning of file. */
-/***********************************************************************/
-void TXTFAM::Reset(void)
- {
- Rows = 0;
- DelRows = 0;
- OldBlk = -1;
- CurBlk = -1;
- ReadBlks = 0;
- CurNum = 0;
- Rbuf = 0;
- Modif = 0;
- Placed = false;
- } // end of Reset
-
-/***********************************************************************/
-/* TXT GetFileLength: returns file size in number of bytes. */
-/***********************************************************************/
-int TXTFAM::GetFileLength(PGLOBAL g)
- {
- char filename[_MAX_PATH];
- int h;
- int len;
-
- PlugSetPath(filename, To_File, Tdbp->GetPath());
- h= global_open(g, MSGID_OPEN_MODE_STRERROR, filename, _O_RDONLY);
-
- if (trace)
- htrc("GetFileLength: fn=%s h=%d\n", filename, h);
-
- if (h == -1) {
- if (errno != ENOENT) {
- if (trace)
- htrc("%s\n", g->Message);
- len = -1;
- }
- else
- {
- len = 0; // File does not exist yet
- g->Message[0]= '\0';
- }
- } else {
- if ((len = _filelength(h)) < 0)
- sprintf(g->Message, MSG(FILELEN_ERROR), "_filelength", filename);
-
- if (Eof && len)
- len--; // Do not count the EOF character
-
- close(h);
- } // endif h
-
- return len;
- } // end of GetFileLength
-
-/***********************************************************************/
-/* Cardinality: returns table cardinality in number of rows. */
-/* This function can be called with a null argument to test the */
-/* availability of Cardinality implementation (1 yes, 0 no). */
-/* Note: This function is meant only for fixed length files but is */
-/* placed here to be available to FIXFAM and MPXFAM classes. */
-/***********************************************************************/
-int TXTFAM::Cardinality(PGLOBAL g)
- {
- if (g) {
- int card = -1;
- int len = GetFileLength(g);
-
- if (len >= 0) {
- if (Padded && Blksize) {
- if (!(len % Blksize))
- card = (len / Blksize) * Nrec;
- else
- sprintf(g->Message, MSG(NOT_FIXED_LEN), To_File, len, Lrecl);
-
- } else {
- if (!(len % Lrecl))
- card = len / (int)Lrecl; // Fixed length file
- else
- sprintf(g->Message, MSG(NOT_FIXED_LEN), To_File, len, Lrecl);
-
- } // endif Padded
-
- if (trace)
- htrc(" Computed max_K=%d Filen=%d lrecl=%d\n",
- card, len, Lrecl);
-
- } else
- card = 0;
-
- // Set number of blocks for later use
- Block = (card > 0) ? (card + Nrec - 1) / Nrec : 0;
- return card;
- } else
- return 1;
-
- } // end of Cardinality
-
-/***********************************************************************/
-/* Use BlockTest to reduce the table estimated size. */
-/* Note: This function is meant only for fixed length files but is */
-/* placed here to be available to FIXFAM and MPXFAM classes. */
-/***********************************************************************/
-int TXTFAM::MaxBlkSize(PGLOBAL g, int s)
- {
- int rc = RC_OK, savcur = CurBlk, blm1 = Block - 1;
- int size, last = s - blm1 * Nrec;
-
- // Roughly estimate the table size as the sum of blocks
- // that can contain good rows
- for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
- if ((rc = Tdbp->TestBlock(g)) == RC_OK)
- size += (CurBlk == blm1) ? last : Nrec;
- else if (rc == RC_EF)
- break;
-
- CurBlk = savcur;
- return size;
- } // end of MaxBlkSize
-
-/* --------------------------- Class DOSFAM -------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-DOSFAM::DOSFAM(PDOSDEF tdp) : TXTFAM(tdp)
- {
- To_Fbt = NULL;
- Stream = NULL;
- T_Stream = NULL;
- Fpos = Spos = Tpos = 0;
- UseTemp = false;
- Bin = false;
- } // end of DOSFAM standard constructor
-
-DOSFAM::DOSFAM(PDOSFAM tdfp) : TXTFAM(tdfp)
- {
- To_Fbt = tdfp->To_Fbt;
- Stream = tdfp->Stream;
- T_Stream = tdfp->T_Stream;
- Fpos = tdfp->Fpos;
- Spos = tdfp->Spos;
- Tpos = tdfp->Tpos;
- UseTemp = tdfp->UseTemp;
- Bin = tdfp->Bin;
- } // end of DOSFAM copy constructor
-
-/***********************************************************************/
-/* Reset: reset position values at the beginning of file. */
-/***********************************************************************/
-void DOSFAM::Reset(void)
- {
- TXTFAM::Reset();
- Bin = false;
- Fpos = Tpos = Spos = 0;
- } // end of Reset
-
-/***********************************************************************/
-/* DOS GetFileLength: returns file size in number of bytes. */
-/***********************************************************************/
-int DOSFAM::GetFileLength(PGLOBAL g)
- {
- int len;
-
- if (!Stream)
- len = TXTFAM::GetFileLength(g);
- else
- if ((len = _filelength(_fileno(Stream))) < 0)
- sprintf(g->Message, MSG(FILELEN_ERROR), "_filelength", To_File);
-
- if (trace)
- htrc("File length=%d\n", len);
-
- return len;
- } // end of GetFileLength
-
-/***********************************************************************/
-/* Cardinality: returns table cardinality in number of rows. */
-/* This function can be called with a null argument to test the */
-/* availability of Cardinality implementation (1 yes, 0 no). */
-/***********************************************************************/
-int DOSFAM::Cardinality(PGLOBAL g)
- {
- return (g) ? -1 : 0;
- } // end of Cardinality
-
-/***********************************************************************/
-/* Use BlockTest to reduce the table estimated size. */
-/* Note: This function is not really implemented yet. */
-/***********************************************************************/
-int DOSFAM::MaxBlkSize(PGLOBAL g, int s)
- {
- return s;
- } // end of MaxBlkSize
-
-/***********************************************************************/
-/* OpenTableFile: Open a DOS/UNIX table file using C standard I/Os. */
-/***********************************************************************/
-bool DOSFAM::OpenTableFile(PGLOBAL g)
- {
- char opmode[4], filename[_MAX_PATH];
-//int ftype = Tdbp->GetFtype();
- MODE mode = Tdbp->Mode;
- PDBUSER dbuserp = PlgGetUser(g);
-
- // This is required when using Unix files under Windows
- Bin = (Ending == 1);
-
- switch (mode) {
- case MODE_READ:
- strcpy(opmode, "r");
- break;
- case MODE_DELETE:
- if (!Tdbp->Next) {
- // Store the number of deleted lines
- DelRows = Cardinality(g);
-
- if (Blocked) {
- // Cardinality must return 0
- Block = 0;
- Last = Nrec;
- } // endif blocked
-
- // This will erase the entire file
- strcpy(opmode, "w");
- Tdbp->ResetSize();
- break;
- } // endif
-
- // Selective delete, pass thru
- Bin = true;
- case MODE_UPDATE:
- if ((UseTemp = Tdbp->IsUsingTemp(g))) {
- strcpy(opmode, "r");
- Bin = true;
- } else
- strcpy(opmode, "r+");
-
- break;
- case MODE_INSERT:
- strcpy(opmode, "a+");
- break;
- default:
- sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
- return true;
- } // endswitch Mode
-
- // For blocked I/O or for moving lines, open the table in binary
- strcat(opmode, (Blocked || Bin) ? "b" : "t");
-
- // Now open the file stream
- PlugSetPath(filename, To_File, Tdbp->GetPath());
-
- if (!(Stream = PlugOpenFile(g, filename, opmode))) {
- if (trace)
- htrc("%s\n", g->Message);
-
- return (mode == MODE_READ && errno == ENOENT)
- ? PushWarning(g, Tdbp) : true;
- } // endif Stream
-
- if (trace)
- htrc("File %s open Stream=%p mode=%s\n", filename, Stream, opmode);
-
- To_Fb = dbuserp->Openlist; // Keep track of File block
-
- /*********************************************************************/
- /* Allocate the line buffer. For mode Delete a bigger buffer has to */
- /* be allocated because is it also used to move lines into the file.*/
- /*********************************************************************/
- return AllocateBuffer(g);
- } // end of OpenTableFile
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete a bigger buffer has to */
-/* be allocated because is it also used to move lines into the file. */
-/***********************************************************************/
-bool DOSFAM::AllocateBuffer(PGLOBAL g)
- {
- MODE mode = Tdbp->Mode;
-
- // Lrecl does not include line ending
- Buflen = Lrecl + Ending + ((Bin) ? 1 : 0);
-
- if (trace)
- htrc("SubAllocating a buffer of %d bytes\n", Buflen);
-
- To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
-
- if (UseTemp || mode == MODE_DELETE) {
- // Have a big buffer to move lines
- Dbflen = Buflen * DOS_BUFF_LEN;
- DelBuf = PlugSubAlloc(g, NULL, Dbflen);
- } else if (mode == MODE_INSERT) {
- /*******************************************************************/
- /* Prepare the buffer so eventual gaps are filled with blanks. */
- /*******************************************************************/
- memset(To_Buf, ' ', Buflen);
- To_Buf[Buflen - 2] = '\n';
- To_Buf[Buflen - 1] = '\0';
- } // endif's mode
-
- return false;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* GetRowID: return the RowID of last read record. */
-/***********************************************************************/
-int DOSFAM::GetRowID(void)
- {
- return Rows;
- } // end of GetRowID
-
-/***********************************************************************/
-/* GetPos: return the position of last read record. */
-/***********************************************************************/
-int DOSFAM::GetPos(void)
- {
- return Fpos;
- } // end of GetPos
-
-/***********************************************************************/
-/* GetNextPos: return the position of next record. */
-/***********************************************************************/
-int DOSFAM::GetNextPos(void)
- {
- return ftell(Stream);
- } // end of GetNextPos
-
-/***********************************************************************/
-/* SetPos: Replace the table at the specified position. */
-/***********************************************************************/
-bool DOSFAM::SetPos(PGLOBAL g, int pos)
- {
- Fpos = pos;
-
- if (fseek(Stream, Fpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
- return true;
- } // endif
-
- Placed = true;
- return false;
- } // end of SetPos
-
-/***********************************************************************/
-/* Record file position in case of UPDATE or DELETE. */
-/***********************************************************************/
-bool DOSFAM::RecordPos(PGLOBAL g)
- {
- if ((Fpos = ftell(Stream)) < 0) {
- sprintf(g->Message, MSG(FTELL_ERROR), 0, strerror(errno));
- return true;
- } // endif Fpos
-
- return false;
- } // end of RecordPos
-
-/***********************************************************************/
-/* Skip one record in file. */
-/***********************************************************************/
-int DOSFAM::SkipRecord(PGLOBAL g, bool header)
- {
- PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
-
- // Skip this record
- if (!fgets(To_Buf, Buflen, Stream)) {
- if (feof(Stream))
- return RC_EF;
-
-#if defined(UNIX) || defined(UNIV_LINUX)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(0));
-#else
- sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
-#endif
- return RC_FX;
- } // endif fgets
-
- // Update progress information
- dup->ProgCur = GetPos();
-
- if (header) {
- // For Delete
- Fpos = ftell(Stream);
-
- if (!UseTemp)
- Tpos = Spos = Fpos; // No need to move header
-
- } // endif header
-
-#if defined(THREAD)
- return RC_NF; // To have progress info
-#else
- return RC_OK; // To loop locally
-#endif
- } // end of SkipRecord
-
-/***********************************************************************/
-/* ReadBuffer: Read one line for a text file. */
-/***********************************************************************/
-int DOSFAM::ReadBuffer(PGLOBAL g)
- {
- char *p;
- int rc;
-
- if (!Stream)
- return RC_EF;
-
- if (trace > 1)
- htrc("ReadBuffer: Tdbp=%p To_Line=%p Placed=%d\n",
- Tdbp, Tdbp->To_Line, Placed);
-
- if (!Placed) {
- /*******************************************************************/
- /* Record file position in case of UPDATE or DELETE. */
- /*******************************************************************/
- next:
- if (RecordPos(g))
- return RC_FX;
-
- CurBlk = (int)Rows++;
-
- if (trace > 1)
- htrc("ReadBuffer: CurBlk=%d\n", CurBlk);
-
- /*******************************************************************/
- /* Check whether optimization on ROWID */
- /* can be done, as well as for join as for local filtering. */
- /*******************************************************************/
- switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- // Skip this record
- if ((rc = SkipRecord(g, FALSE)) != RC_OK)
- return rc;
-
- goto next;
- } // endswitch rc
-
- } else
- Placed = false;
-
- if (trace > 1)
- htrc(" About to read: stream=%p To_Buf=%p Buflen=%d\n",
- Stream, To_Buf, Buflen);
-
- if (fgets(To_Buf, Buflen, Stream)) {
- p = To_Buf + strlen(To_Buf) - 1;
-
- if (trace > 1)
- htrc(" Read: To_Buf=%p p=%c\n", To_Buf, To_Buf, p);
-
-#if defined(UNIX)
- if (true) {
- // Data files can be imported from Windows (having CRLF)
-#else
- if (Bin) {
- // Data file is read in binary so CRLF remains
-#endif
- if (*p == '\n' || *p == '\r') {
- // is this enough for Unix ???
- *p = '\0'; // Eliminate ending CR or LF character
-
- if (p > To_Buf) {
- // is this enough for Unix ???
- p--;
-
- if (*p == '\n' || *p == '\r')
- *p = '\0'; // Eliminate ending CR or LF character
-
- } // endif To_Buf
-
- } // endif p
-
- } else if (*p == '\n')
- *p = '\0'; // Eliminate ending new-line character
-
- if (trace > 1)
- htrc(" To_Buf='%s'\n", To_Buf);
-
- strcpy(Tdbp->To_Line, To_Buf);
- num_read++;
- rc = RC_OK;
- } else if (feof(Stream)) {
- rc = RC_EF;
- } else {
-#if defined(UNIX)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(0));
-#else
- sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
-#endif
-
- if (trace)
- htrc("%s\n", g->Message);
-
- rc = RC_FX;
- } // endif's fgets
-
- if (trace > 1)
- htrc("ReadBuffer: rc=%d\n", rc);
-
- IsRead = true;
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* WriteBuffer: File write routine for DOS access method. */
-/***********************************************************************/
-int DOSFAM::WriteBuffer(PGLOBAL g)
- {
- char *crlf = "\n";
- int curpos = 0;
- bool moved = true;
-
- // T_Stream is the temporary stream or the table file stream itself
- if (!T_Stream)
- if (UseTemp && Tdbp->Mode == MODE_UPDATE) {
- if (OpenTempFile(g))
- return RC_FX;
-
- } else
- T_Stream = Stream;
-
- if (Tdbp->Mode == MODE_UPDATE) {
- /*******************************************************************/
- /* Here we simply rewrite a record on itself. There are two cases */
- /* were another method should be used, a/ when Update apply to */
- /* the whole file, b/ when updating the last field of a variable */
- /* length file. The method could be to rewrite a new file, then */
- /* to erase the old one and rename the new updated file. */
- /*******************************************************************/
- curpos = ftell(Stream);
-
- if (trace)
- htrc("Last : %d cur: %d\n", Fpos, curpos);
-
- if (UseTemp) {
- /*****************************************************************/
- /* We are using a temporary file. Before writing the updated */
- /* record, we must eventually copy all the intermediate records */
- /* that have not been updated. */
- /*****************************************************************/
- if (MoveIntermediateLines(g, &moved))
- return RC_FX;
-
- Spos = curpos; // New start position
- } else
- // Update is directly written back into the file,
- // with this (fast) method, record size cannot change.
- if (fseek(Stream, Fpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
- return RC_FX;
- } // endif
-
- } // endif mode
-
- /*********************************************************************/
- /* Prepare the write buffer. */
- /*********************************************************************/
-#if defined(WIN32)
- if (Bin)
- crlf = "\r\n";
-#endif // WIN32
- strcat(strcpy(To_Buf, Tdbp->To_Line), crlf);
-
- /*********************************************************************/
- /* Now start the writing process. */
- /*********************************************************************/
- if ((fputs(To_Buf, T_Stream)) == EOF) {
- sprintf(g->Message, MSG(FPUTS_ERROR), strerror(errno));
- return RC_FX;
- } // endif EOF
-
- if (Tdbp->Mode == MODE_UPDATE && moved)
- if (fseek(Stream, curpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
- return RC_FX;
- } // endif
-
- if (trace)
- htrc("write done\n");
-
- return RC_OK;
- } // end of WriteBuffer
-
-/***********************************************************************/
-/* Data Base delete line routine for DOS and BLK access methods. */
-/***********************************************************************/
-int DOSFAM::DeleteRecords(PGLOBAL g, int irc)
- {
- bool moved;
- int curpos = ftell(Stream);
-
- /*********************************************************************/
- /* There is an alternative here: */
- /* 1 - use a temporary file in which are copied all not deleted */
- /* lines, at the end the original file will be deleted and */
- /* the temporary file renamed to the original file name. */
- /* 2 - directly move the not deleted lines inside the original */
- /* file, and at the end erase all trailing records. */
- /* This will be experimented, but method 1 must be used for Unix as */
- /* the function needed to erase trailing records is not available. */
- /*********************************************************************/
- if (trace)
- htrc(
- "DOS DeleteDB: rc=%d UseTemp=%d curpos=%d Fpos=%d Tpos=%d Spos=%d\n",
- irc, UseTemp, curpos, Fpos, Tpos, Spos);
-
- if (irc != RC_OK) {
- /*******************************************************************/
- /* EOF: position Fpos at the end-of-file position. */
- /*******************************************************************/
- fseek(Stream, 0, SEEK_END);
- Fpos = ftell(Stream);
-
- if (trace)
- htrc("Fpos placed at file end=%d\n", Fpos);
-
- } // endif irc
-
- if (Tpos == Spos) {
- /*******************************************************************/
- /* First line to delete, Open temporary file. */
- /*******************************************************************/
- if (UseTemp) {
- if (OpenTempFile(g))
- return RC_FX;
-
- } else {
- /*****************************************************************/
- /* Move of eventual preceeding lines is not required here. */
- /* Set the target file as being the source file itself. */
- /* Set the future Tpos, and give Spos a value to block copying. */
- /*****************************************************************/
- T_Stream = Stream;
- Spos = Tpos = Fpos;
- } // endif UseTemp
-
- } // endif Tpos == Spos
-
- /*********************************************************************/
- /* Move any intermediate lines. */
- /*********************************************************************/
- if (MoveIntermediateLines(g, &moved))
- return RC_FX;
-
- if (irc == RC_OK) {
- /*******************************************************************/
- /* Reposition the file pointer and set Spos. */
- /*******************************************************************/
- if (!UseTemp || moved)
- if (fseek(Stream, curpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
- return RC_FX;
- } // endif
-
- Spos = GetNextPos(); // New start position
-
- if (trace)
- htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
-
- } else {
- /*******************************************************************/
- /* Last call after EOF has been reached. */
- /* The UseTemp case is treated in CloseTableFile. */
- /*******************************************************************/
- if (!UseTemp) {
- /*****************************************************************/
- /* Because the chsize functionality is only accessible with a */
- /* system call we must close the file and reopen it with the */
- /* open function (_fopen for MS ??) this is still to be checked */
- /* for compatibility with Text files and other OS's. */
- /*****************************************************************/
- char filename[_MAX_PATH];
- int h; // File handle, return code
-
- PlugSetPath(filename, To_File, Tdbp->GetPath());
- /*rc=*/ PlugCloseFile(g, To_Fb);
-
- if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0)
- return RC_FX;
-
- /*****************************************************************/
- /* Remove extra records. */
- /*****************************************************************/
-#if defined(UNIX)
- if (ftruncate(h, (off_t)Tpos)) {
- sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
- close(h);
- return RC_FX;
- } // endif
-#else
- if (chsize(h, Tpos)) {
- sprintf(g->Message, MSG(CHSIZE_ERROR), strerror(errno));
- close(h);
- return RC_FX;
- } // endif
-#endif
-
- close(h);
-
- if (trace)
- htrc("done, h=%d irc=%d\n", h, irc);
-
- } // endif !UseTemp
-
- } // endif irc
-
- return RC_OK; // All is correct
- } // end of DeleteRecords
-
-/***********************************************************************/
-/* Open a temporary file used while updating or deleting. */
-/***********************************************************************/
-bool DOSFAM::OpenTempFile(PGLOBAL g)
- {
- char tempname[_MAX_PATH];
- bool rc = false;
-
- /*********************************************************************/
- /* Open the temporary file, Spos is at the beginning of file. */
- /*********************************************************************/
- PlugSetPath(tempname, To_File, Tdbp->GetPath());
- strcat(PlugRemoveType(tempname, tempname), ".t");
-
- if (!(T_Stream = PlugOpenFile(g, tempname, "wb"))) {
- if (trace)
- htrc("%s\n", g->Message);
-
- rc = true;
- } else
- To_Fbt = PlgGetUser(g)->Openlist;
-
- return rc;
- } // end of OpenTempFile
-
-/***********************************************************************/
-/* Move intermediate deleted or updated lines. */
-/* This works only for file open in binary mode. */
-/***********************************************************************/
-bool DOSFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
- {
- int n;
- size_t req, len;
-
- for (*b = false, n = Fpos - Spos; n > 0; n -= req) {
- if (!UseTemp || !*b)
- if (fseek(Stream, Spos, SEEK_SET)) {
- sprintf(g->Message, MSG(READ_SEEK_ERROR), strerror(errno));
- return true;
- } // endif
-
- req = (size_t)min(n, Dbflen);
- len = fread(DelBuf, 1, req, Stream);
-
- if (trace)
- htrc("after read req=%d len=%d\n", req, len);
-
- if (len != req) {
- sprintf(g->Message, MSG(DEL_READ_ERROR), (int) req, (int) len);
- return true;
- } // endif len
-
- if (!UseTemp)
- if (fseek(T_Stream, Tpos, SEEK_SET)) {
- sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
- return true;
- } // endif
-
- if ((len = fwrite(DelBuf, 1, req, T_Stream)) != req) {
- sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
- return true;
- } // endif
-
- if (trace)
- htrc("after write pos=%d\n", ftell(Stream));
-
- Tpos += (int)req;
- Spos += (int)req;
-
- if (trace)
- htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
-
- *b = true;
- } // endfor n
-
- return false;
- } // end of MoveIntermediate Lines
-
-/***********************************************************************/
-/* Delete the old file and rename the new temp file. */
-/***********************************************************************/
-int DOSFAM::RenameTempFile(PGLOBAL g)
- {
- char *tempname, filetemp[_MAX_PATH], filename[_MAX_PATH];
- int rc;
-
- if (!To_Fbt)
- return RC_INFO; // Nothing to do ???
-
- // This loop is necessary because, in case of join,
- // To_File can have been open several times.
- for (PFBLOCK fb = PlgGetUser(g)->Openlist; fb; fb = fb->Next)
- rc = PlugCloseFile(g, fb);
-
- tempname = (char*)To_Fbt->Fname;
- PlugSetPath(filename, To_File, Tdbp->GetPath());
- strcat(PlugRemoveType(filetemp, filename), ".ttt");
- remove(filetemp); // May still be there from previous error
-
- if (rename(filename, filetemp)) { // Save file for security
- sprintf(g->Message, MSG(RENAME_ERROR),
- filename, filetemp, strerror(errno));
- rc = RC_FX;
- } else if (rename(tempname, filename)) {
- sprintf(g->Message, MSG(RENAME_ERROR),
- tempname, filename, strerror(errno));
- rc = rename(filetemp, filename); // Restore saved file
- rc = RC_FX;
- } else if (remove(filetemp)) {
- sprintf(g->Message, MSG(REMOVE_ERROR),
- filetemp, strerror(errno));
- rc = RC_INFO; // Acceptable
- } else
- rc = RC_OK;
-
- return rc;
- } // end of RenameTempFile
-
-/***********************************************************************/
-/* Table file close routine for DOS access method. */
-/***********************************************************************/
-void DOSFAM::CloseTableFile(PGLOBAL g)
- {
- int rc;
-
- if (UseTemp && T_Stream) {
- if (Tdbp->Mode == MODE_UPDATE) {
- // Copy eventually remaining lines
- bool b;
-
- fseek(Stream, 0, SEEK_END);
- Fpos = ftell(Stream);
- rc = MoveIntermediateLines(g, &b);
- } // endif Mode
-
- // Delete the old file and rename the new temp file.
- RenameTempFile(g); // Also close all files
- } else {
- rc = PlugCloseFile(g, To_Fb);
-
- if (trace)
- htrc("DOS Close: closing %s rc=%d\n", To_File, rc);
-
- } // endif UseTemp
-
- Stream = NULL; // So we can know whether table is open
- } // end of CloseTableFile
-
-/***********************************************************************/
-/* Rewind routine for DOS access method. */
-/***********************************************************************/
-void DOSFAM::Rewind(void)
- {
- rewind(Stream);
- Rows = 0;
- OldBlk = CurBlk = -1;
- } // end of Rewind
-
-/* --------------------------- Class BLKFAM -------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-BLKFAM::BLKFAM(PDOSDEF tdp) : DOSFAM(tdp)
- {
- Blocked = true;
- Block = tdp->GetBlock();
- Last = tdp->GetLast();
- Nrec = tdp->GetElemt();
- Closing = false;
- BlkPos = tdp->GetTo_Pos();
- CurLine = NULL;
- NxtLine = NULL;
- OutBuf = NULL;
- } // end of BLKFAM standard constructor
-
-BLKFAM::BLKFAM(PBLKFAM txfp) : DOSFAM(txfp)
- {
- Closing = txfp->Closing;
- CurLine = txfp->CurLine;
- NxtLine = txfp->NxtLine;
- OutBuf = txfp->OutBuf;
- } // end of BLKFAM copy constructor
-
-/***********************************************************************/
-/* Reset: reset position values at the beginning of file. */
-/***********************************************************************/
-void BLKFAM::Reset(void)
- {
- DOSFAM::Reset();
- Closing = false;
- } // end of Reset
-
-/***********************************************************************/
-/* Cardinality: returns table cardinality in number of rows. */
-/* This function can be called with a null argument to test the */
-/* availability of Cardinality implementation (1 yes, 0 no). */
-/***********************************************************************/
-int BLKFAM::Cardinality(PGLOBAL g)
- {
- // Should not be called in this version
- return (g) ? -1 : 0;
-//return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
- } // end of Cardinality
-
-/***********************************************************************/
-/* Use BlockTest to reduce the table estimated size. */
-/***********************************************************************/
-int BLKFAM::MaxBlkSize(PGLOBAL g, int s)
- {
- int rc = RC_OK, savcur = CurBlk;
- int size;
-
- // Roughly estimate the table size as the sum of blocks
- // that can contain good rows
- for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
- if ((rc = Tdbp->TestBlock(g)) == RC_OK)
- size += (CurBlk == Block - 1) ? Last : Nrec;
- else if (rc == RC_EF)
- break;
-
- CurBlk = savcur;
- return size;
- } // end of MaxBlkSize
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete or when a temp file is */
-/* used another big buffer has to be allocated because is it used */
-/* to move or update the lines into the (temp) file. */
-/***********************************************************************/
-bool BLKFAM::AllocateBuffer(PGLOBAL g)
- {
- int len;
- MODE mode = Tdbp->GetMode();
-
- // For variable length files, Lrecl does not include CRLF
- len = Lrecl + ((Tdbp->GetFtype()) ? 0 : Ending);
- Buflen = len * Nrec;
- CurLine = To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
-
- if (UseTemp || mode == MODE_DELETE) {
- if (mode == MODE_UPDATE)
- OutBuf = (char*)PlugSubAlloc(g, NULL, len + 1);
-
- Dbflen = Buflen;
- DelBuf = PlugSubAlloc(g, NULL, Dbflen);
- } else if (mode == MODE_INSERT)
- Rbuf = Nrec; // To be used by WriteDB
-
- return false;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* GetRowID: return the RowID of last read record. */
-/***********************************************************************/
-int BLKFAM::GetRowID(void)
- {
- return CurNum + Nrec * CurBlk + 1;
- } // end of GetRowID
-
-/***********************************************************************/
-/* GetPos: return the position of last read record. */
-/***********************************************************************/
-int BLKFAM::GetPos(void)
- {
- return (CurNum + Nrec * CurBlk); // Computed file index
- } // end of GetPos
-
-/***********************************************************************/
-/* GetNextPos: called by DeleteRecords. */
-/***********************************************************************/
-int BLKFAM::GetNextPos(void)
- {
- return Fpos + NxtLine - CurLine;
- } // end of GetNextPos
-
-/***********************************************************************/
-/* SetPos: Replace the table at the specified position. */
-/***********************************************************************/
-bool BLKFAM::SetPos(PGLOBAL g, int pos)
- {
- if (pos < 0) {
- strcpy(g->Message, MSG(INV_REC_POS));
- return true;
- } // endif recpos
-
- CurBlk = pos / Nrec;
- CurNum = pos % Nrec;
-#if defined(_DEBUG)
- num_eq[(CurBlk == OldBlk) ? 1 : 0]++;
-#endif
-
- // Indicate the table position was externally set
- Placed = true;
- return false;
- } // end of SetPos
-
-/***********************************************************************/
-/* Record file position in case of UPDATE or DELETE. */
-/* Not used yet for blocked tables. */
-/***********************************************************************/
-bool BLKFAM::RecordPos(PGLOBAL g)
- {
- Fpos = (CurNum + Nrec * CurBlk); // Computed file index
- return false;
- } // end of RecordPos
-
-/***********************************************************************/
-/* Skip one record in file. */
-/***********************************************************************/
-int BLKFAM::SkipRecord(PGLOBAL g, bool header)
- {
- if (header) {
- // For Delete
- Fpos = BlkPos[0]; // First block starts after the header
-
- if (!UseTemp)
- Tpos = Spos = Fpos; // No need to move header
-
- } // endif header
-
- OldBlk = -2; // To force fseek on first block
- return RC_OK;
- } // end of SkipRecord
-
-/***********************************************************************/
-/* ReadBuffer: Read one line for a text file. */
-/***********************************************************************/
-int BLKFAM::ReadBuffer(PGLOBAL g)
- {
- int i, n, rc = RC_OK;
-
- /*********************************************************************/
- /* Sequential reading when Placed is not true. */
- /*********************************************************************/
- if (Placed) {
- Placed = false;
- } else if (++CurNum < Rbuf) {
- CurLine = NxtLine;
-
- // Get the position of the next line in the buffer
- while (*NxtLine++ != '\n') ;
-
- // Set caller line buffer
- n = NxtLine - CurLine - Ending;
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
- goto fin;
- } else if (Rbuf < Nrec && CurBlk != -1) {
- return RC_EF;
- } else {
- /*******************************************************************/
- /* New block. */
- /*******************************************************************/
- CurNum = 0;
-
- next:
- if (++CurBlk >= Block)
- return RC_EF;
-
- /*******************************************************************/
- /* Before reading a new block, check whether block optimization */
- /* can be done, as well as for join as for local filtering. */
- /*******************************************************************/
- switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- goto next;
- } // endswitch rc
-
- } // endif's
-
- if (OldBlk == CurBlk)
- goto ok; // Block is already there
-
- // fseek is required only in non sequential reading
- if (CurBlk != OldBlk + 1)
- if (fseek(Stream, BlkPos[CurBlk], SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), BlkPos[CurBlk]);
- return RC_FX;
- } // endif fseek
-
- // Calculate the length of block to read
- BlkLen = BlkPos[CurBlk + 1] - BlkPos[CurBlk];
-
- if (trace)
- htrc("File position is now %d\n", ftell(Stream));
-
- // Read the entire next block
- n = fread(To_Buf, 1, (size_t)BlkLen, Stream);
-
- if (n == BlkLen) {
-// ReadBlks++;
- num_read++;
- Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
-
- ok:
- rc = RC_OK;
-
- // Get the position of the current line
- for (i = 0, CurLine = To_Buf; i < CurNum; i++)
- while (*CurLine++ != '\n') ; // What about Unix ???
-
- // Now get the position of the next line
- for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
-
- // Set caller line buffer
- n = NxtLine - CurLine - Ending;
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
- } else if (feof(Stream)) {
- rc = RC_EF;
- } else {
-#if defined(UNIX)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
-#else
- sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
-#endif
-
- if (trace)
- htrc("%s\n", g->Message);
-
- return RC_FX;
- } // endelse
-
- OldBlk = CurBlk; // Last block actually read
- IsRead = true; // Is read indeed
-
- fin:
- // Store the current record file position for Delete and Update
- Fpos = BlkPos[CurBlk] + CurLine - To_Buf;
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* WriteBuffer: File write routine for the blocked DOS access method. */
-/* Update is directly written back into the file, */
-/* with this (fast) method, record size cannot change. */
-/***********************************************************************/
-int BLKFAM::WriteBuffer(PGLOBAL g)
- {
- if (Tdbp->GetMode() == MODE_INSERT) {
- /*******************************************************************/
- /* In Insert mode, blocks are added sequentially to the file end. */
- /*******************************************************************/
- if (!Closing) { // Add line to the write buffer
- strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
-
- if (++CurNum != Rbuf) {
- CurLine += strlen(CurLine);
- return RC_OK; // We write only full blocks
- } // endif CurNum
-
- } // endif Closing
-
- // Now start the writing process.
- NxtLine = CurLine + strlen(CurLine);
- BlkLen = NxtLine - To_Buf;
-
- if (fwrite(To_Buf, 1, BlkLen, Stream) != (size_t)BlkLen) {
- sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
- Closing = true; // To tell CloseDB about a Write error
- return RC_FX;
- } // endif size
-
- CurBlk++;
- CurNum = 0;
- CurLine = To_Buf;
- } else {
- /*******************************************************************/
- /* Mode == MODE_UPDATE. */
- /*******************************************************************/
- char *crlf;
- size_t len;
- int curpos = ftell(Stream);
- bool moved = true;
-
- // T_Stream is the temporary stream or the table file stream itself
- if (!T_Stream)
- if (UseTemp /*&& Tdbp->GetMode() == MODE_UPDATE*/) {
- if (OpenTempFile(g))
- return RC_FX;
-
- } else
- T_Stream = Stream;
-
- if (UseTemp) {
- /*****************************************************************/
- /* We are using a temporary file. Before writing the updated */
- /* record, we must eventually copy all the intermediate records */
- /* that have not been updated. */
- /*****************************************************************/
- if (MoveIntermediateLines(g, &moved))
- return RC_FX;
-
- Spos = GetNextPos(); // New start position
-
- // Prepare the output buffer
-#if defined(WIN32)
- crlf = "\r\n";
-#else
- crlf = "\n";
-#endif // WIN32
- strcat(strcpy(OutBuf, Tdbp->GetLine()), crlf);
- len = strlen(OutBuf);
- } else {
- if (fseek(Stream, Fpos, SEEK_SET)) { // Fpos is last position
- sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
- return RC_FX;
- } // endif fseek
-
- // Replace the line inside read buffer (length has not changed)
- memcpy(CurLine, Tdbp->GetLine(), strlen(Tdbp->GetLine()));
- OutBuf = CurLine;
- len = (size_t)(NxtLine - CurLine);
- } // endif UseTemp
-
- if (fwrite(OutBuf, 1, len, T_Stream) != (size_t)len) {
- sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
- return RC_FX;
- } // endif fwrite
-
- if (moved)
- if (fseek(Stream, curpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
- return RC_FX;
- } // endif
-
- } // endif Mode
-
- return RC_OK;
- } // end of WriteBuffer
-
-/***********************************************************************/
-/* Table file close routine for DOS access method. */
-/***********************************************************************/
-void BLKFAM::CloseTableFile(PGLOBAL g)
- {
- int rc, wrc = RC_OK;
-
- if (UseTemp && T_Stream) {
- if (Tdbp->GetMode() == MODE_UPDATE) {
- // Copy eventually remaining lines
- bool b;
-
- fseek(Stream, 0, SEEK_END);
- Fpos = ftell(Stream);
- rc = MoveIntermediateLines(g, &b);
- } else
- rc = RC_OK;
-
- if (rc == RC_OK)
- // Delete the old file and rename the new temp file.
- rc = RenameTempFile(g); // Also close all files
- else
- rc = PlugCloseFile(g, To_Fb);
-
- } else {
- // Closing is True if last Write was in error
- if (Tdbp->GetMode() == MODE_INSERT && CurNum && !Closing) {
- // Some more inserted lines remain to be written
- Rbuf = CurNum--;
- Closing = true;
- wrc = WriteBuffer(g);
- } else if (Modif && !Closing) {
- // Last updated block remains to be written
- Closing = true;
- wrc = ReadBuffer(g);
- } // endif's
-
- rc = PlugCloseFile(g, To_Fb);
-
- if (trace)
- htrc("BLK CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
- To_File, Tdbp->GetMode(), wrc, rc);
-
- } // endif UseTemp
-
- Stream = NULL; // So we can know whether table is open
- } // end of CloseTableFile
-
-/***********************************************************************/
-/* Rewind routine for DOS access method. */
-/* Note: commenting out OldBlk = -1 has two advantages: */
-/* 1 - It forces fseek on first block, thus suppressing the need to */
-/* rewind the file, anyway unuseful when second pass if indexed. */
-/* 2 - It permit to avoid re-reading small tables having only 1 block.*/
-/***********************************************************************/
-void BLKFAM::Rewind(void)
- {
-//rewind(Stream); will be placed by fseek
- CurBlk = -1;
- CurNum = Rbuf;
-//OldBlk = -1; commented out in case we reuse last read block
-//Rbuf = 0; commented out in case we reuse last read block
- } // end of Rewind
-
+/*********** File AM Txt C++ Program Source Code File (.CPP) ***********/
+/* PROGRAM NAME: FILAMTXT */
+/* ------------- */
+/* Version 1.5 */
+/* */
+/* COPYRIGHT: */
+/* ---------- */
+/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are the Text file access method classes. */
+/* */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant sections of the System header files. */
+/***********************************************************************/
+#include "my_global.h"
+#if defined(WIN32)
+#include <io.h>
+#include <fcntl.h>
+#include <errno.h>
+#if defined(__BORLANDC__)
+#define __MFC_COMPAT__ // To define min/max as macro
+#endif // __BORLANDC__
+//#include <windows.h>
+#else // !WIN32
+#if defined(UNIX) || defined(UNIV_LINUX)
+#include <errno.h>
+#include <unistd.h>
+//#if !defined(sun) // Sun has the ftruncate fnc.
+//#define USETEMP // Force copy mode for DELETE
+//#endif // !sun
+#else // !UNIX
+#include <io.h>
+#endif // !UNIX
+#include <fcntl.h>
+#endif // !WIN32
+
+/***********************************************************************/
+/* Include application header files: */
+/* global.h is header containing all global declarations. */
+/* plgdbsem.h is header containing the DB application declarations. */
+/* filamtxt.h is header containing the file AM classes declarations. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "filamtxt.h"
+#include "tabdos.h"
+
+#if defined(UNIX) || defined(UNIV_LINUX)
+#include "osutil.h"
+#define _fileno fileno
+#define _O_RDONLY O_RDONLY
+#endif
+
+extern int num_read, num_there, num_eq[2]; // Statistics
+extern "C" int trace;
+
+/* --------------------------- Class TXTFAM -------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+TXTFAM::TXTFAM(PDOSDEF tdp)
+ {
+ Tdbp = NULL;
+ To_Fb = NULL;
+ To_File = tdp->Fn;
+ Lrecl = tdp->Lrecl;
+ Placed = false;
+ IsRead = true;
+ Blocked = false;
+ To_Buf = NULL;
+ DelBuf = NULL;
+ BlkPos = NULL;
+ BlkLen = 0;
+ Buflen = 0;
+ Dbflen = 0;
+ Rows = 0;
+ DelRows = 0;
+ Headlen = 0;
+ Block = 0;
+ Last = 0;
+ Nrec = 1;
+ OldBlk = -1;
+ CurBlk = -1;
+ ReadBlks = 0;
+ CurNum = 0;
+ Rbuf = 0;
+ Modif = 0;
+ Blksize = 0;
+ Padded = false;
+ Eof = tdp->Eof;
+ Ending = tdp->Ending;
+ CrLf = (char*)(Ending == 2 ? "\r\n" : "\n");
+ } // end of TXTFAM standard constructor
+
+TXTFAM::TXTFAM(PTXF txfp)
+ {
+ Tdbp = txfp->Tdbp;
+ To_Fb = txfp->To_Fb;
+ To_File = txfp->To_File;
+ Lrecl = txfp->Lrecl;
+ Placed = txfp->Placed;
+ IsRead = txfp->IsRead;
+ Blocked = txfp->Blocked;
+ To_Buf = txfp->To_Buf;
+ DelBuf = txfp->DelBuf;
+ BlkPos = txfp->BlkPos;
+ BlkLen = txfp->BlkLen;
+ Buflen = txfp->Buflen;
+ Dbflen = txfp->Dbflen;
+ Rows = txfp->Rows;
+ DelRows = txfp->DelRows;
+ Headlen = txfp->Headlen;
+ Block = txfp->Block;
+ Last = txfp->Last;
+ Nrec = txfp->Nrec;
+ OldBlk = txfp->OldBlk;
+ CurBlk = txfp->CurBlk;
+ ReadBlks = txfp->ReadBlks;
+ CurNum = txfp->CurNum;
+ Rbuf = txfp->Rbuf;
+ Modif = txfp->Modif;
+ Blksize = txfp->Blksize;
+ Padded = txfp->Padded;
+ Eof = txfp->Eof;
+ Ending = txfp->Ending;
+ } // end of TXTFAM copy constructor
+
+/***********************************************************************/
+/* Reset: reset position values at the beginning of file. */
+/***********************************************************************/
+void TXTFAM::Reset(void)
+ {
+ Rows = 0;
+ DelRows = 0;
+ OldBlk = -1;
+ CurBlk = -1;
+ ReadBlks = 0;
+ CurNum = 0;
+ Rbuf = 0;
+ Modif = 0;
+ Placed = false;
+ } // end of Reset
+
+/***********************************************************************/
+/* TXT GetFileLength: returns file size in number of bytes. */
+/***********************************************************************/
+int TXTFAM::GetFileLength(PGLOBAL g)
+ {
+ char filename[_MAX_PATH];
+ int h;
+ int len;
+
+ PlugSetPath(filename, To_File, Tdbp->GetPath());
+ h= global_open(g, MSGID_OPEN_MODE_STRERROR, filename, _O_RDONLY);
+
+ if (trace)
+ htrc("GetFileLength: fn=%s h=%d\n", filename, h);
+
+ if (h == -1) {
+ if (errno != ENOENT) {
+ if (trace)
+ htrc("%s\n", g->Message);
+ len = -1;
+ }
+ else
+ {
+ len = 0; // File does not exist yet
+ g->Message[0]= '\0';
+ }
+ } else {
+ if ((len = _filelength(h)) < 0)
+ sprintf(g->Message, MSG(FILELEN_ERROR), "_filelength", filename);
+
+ if (Eof && len)
+ len--; // Do not count the EOF character
+
+ close(h);
+ } // endif h
+
+ return len;
+ } // end of GetFileLength
+
+/***********************************************************************/
+/* Cardinality: returns table cardinality in number of rows. */
+/* This function can be called with a null argument to test the */
+/* availability of Cardinality implementation (1 yes, 0 no). */
+/* Note: This function is meant only for fixed length files but is */
+/* placed here to be available to FIXFAM and MPXFAM classes. */
+/***********************************************************************/
+int TXTFAM::Cardinality(PGLOBAL g)
+ {
+ if (g) {
+ int card = -1;
+ int len = GetFileLength(g);
+
+ if (len >= 0) {
+ if (Padded && Blksize) {
+ if (!(len % Blksize))
+ card = (len / Blksize) * Nrec;
+ else
+ sprintf(g->Message, MSG(NOT_FIXED_LEN), To_File, len, Lrecl);
+
+ } else {
+ if (!(len % Lrecl))
+ card = len / (int)Lrecl; // Fixed length file
+ else
+ sprintf(g->Message, MSG(NOT_FIXED_LEN), To_File, len, Lrecl);
+
+ } // endif Padded
+
+ if (trace)
+ htrc(" Computed max_K=%d Filen=%d lrecl=%d\n",
+ card, len, Lrecl);
+
+ } else
+ card = 0;
+
+ // Set number of blocks for later use
+ Block = (card > 0) ? (card + Nrec - 1) / Nrec : 0;
+ return card;
+ } else
+ return 1;
+
+ } // end of Cardinality
+
+/***********************************************************************/
+/* Use BlockTest to reduce the table estimated size. */
+/* Note: This function is meant only for fixed length files but is */
+/* placed here to be available to FIXFAM and MPXFAM classes. */
+/***********************************************************************/
+int TXTFAM::MaxBlkSize(PGLOBAL g, int s)
+ {
+ int rc = RC_OK, savcur = CurBlk, blm1 = Block - 1;
+ int size, last = s - blm1 * Nrec;
+
+ // Roughly estimate the table size as the sum of blocks
+ // that can contain good rows
+ for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
+ if ((rc = Tdbp->TestBlock(g)) == RC_OK)
+ size += (CurBlk == blm1) ? last : Nrec;
+ else if (rc == RC_EF)
+ break;
+
+ CurBlk = savcur;
+ return size;
+ } // end of MaxBlkSize
+
+/* --------------------------- Class DOSFAM -------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+DOSFAM::DOSFAM(PDOSDEF tdp) : TXTFAM(tdp)
+ {
+ To_Fbt = NULL;
+ Stream = NULL;
+ T_Stream = NULL;
+ Fpos = Spos = Tpos = 0;
+ UseTemp = false;
+ Bin = false;
+ } // end of DOSFAM standard constructor
+
+DOSFAM::DOSFAM(PDOSFAM tdfp) : TXTFAM(tdfp)
+ {
+ To_Fbt = tdfp->To_Fbt;
+ Stream = tdfp->Stream;
+ T_Stream = tdfp->T_Stream;
+ Fpos = tdfp->Fpos;
+ Spos = tdfp->Spos;
+ Tpos = tdfp->Tpos;
+ UseTemp = tdfp->UseTemp;
+ Bin = tdfp->Bin;
+ } // end of DOSFAM copy constructor
+
+/***********************************************************************/
+/* Reset: reset position values at the beginning of file. */
+/***********************************************************************/
+void DOSFAM::Reset(void)
+ {
+ TXTFAM::Reset();
+ Bin = false;
+ Fpos = Tpos = Spos = 0;
+ } // end of Reset
+
+/***********************************************************************/
+/* DOS GetFileLength: returns file size in number of bytes. */
+/***********************************************************************/
+int DOSFAM::GetFileLength(PGLOBAL g)
+ {
+ int len;
+
+ if (!Stream)
+ len = TXTFAM::GetFileLength(g);
+ else
+ if ((len = _filelength(_fileno(Stream))) < 0)
+ sprintf(g->Message, MSG(FILELEN_ERROR), "_filelength", To_File);
+
+ if (trace)
+ htrc("File length=%d\n", len);
+
+ return len;
+ } // end of GetFileLength
+
+/***********************************************************************/
+/* Cardinality: returns table cardinality in number of rows. */
+/* This function can be called with a null argument to test the */
+/* availability of Cardinality implementation (1 yes, 0 no). */
+/***********************************************************************/
+int DOSFAM::Cardinality(PGLOBAL g)
+ {
+ return (g) ? -1 : 0;
+ } // end of Cardinality
+
+/***********************************************************************/
+/* Use BlockTest to reduce the table estimated size. */
+/* Note: This function is not really implemented yet. */
+/***********************************************************************/
+int DOSFAM::MaxBlkSize(PGLOBAL g, int s)
+ {
+ return s;
+ } // end of MaxBlkSize
+
+/***********************************************************************/
+/* OpenTableFile: Open a DOS/UNIX table file using C standard I/Os. */
+/***********************************************************************/
+bool DOSFAM::OpenTableFile(PGLOBAL g)
+ {
+ char opmode[4], filename[_MAX_PATH];
+//int ftype = Tdbp->GetFtype();
+ MODE mode = Tdbp->Mode;
+ PDBUSER dbuserp = PlgGetUser(g);
+
+ // This is required when using Unix files under Windows
+ Bin = (Ending == 1);
+
+ switch (mode) {
+ case MODE_READ:
+ strcpy(opmode, "r");
+ break;
+ case MODE_DELETE:
+ if (!Tdbp->Next) {
+ // Store the number of deleted lines
+ DelRows = Cardinality(g);
+
+ if (Blocked) {
+ // Cardinality must return 0
+ Block = 0;
+ Last = Nrec;
+ } // endif blocked
+
+ // This will erase the entire file
+ strcpy(opmode, "w");
+ Tdbp->ResetSize();
+ break;
+ } // endif
+
+ // Selective delete, pass thru
+ Bin = true;
+ case MODE_UPDATE:
+ if ((UseTemp = Tdbp->IsUsingTemp(g))) {
+ strcpy(opmode, "r");
+ Bin = true;
+ } else
+ strcpy(opmode, "r+");
+
+ break;
+ case MODE_INSERT:
+ strcpy(opmode, "a+");
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
+ return true;
+ } // endswitch Mode
+
+ // For blocked I/O or for moving lines, open the table in binary
+ strcat(opmode, (Blocked || Bin) ? "b" : "t");
+
+ // Now open the file stream
+ PlugSetPath(filename, To_File, Tdbp->GetPath());
+
+ if (!(Stream = PlugOpenFile(g, filename, opmode))) {
+ if (trace)
+ htrc("%s\n", g->Message);
+
+ return (mode == MODE_READ && errno == ENOENT)
+ ? PushWarning(g, Tdbp) : true;
+ } // endif Stream
+
+ if (trace)
+ htrc("File %s open Stream=%p mode=%s\n", filename, Stream, opmode);
+
+ To_Fb = dbuserp->Openlist; // Keep track of File block
+
+ /*********************************************************************/
+ /* Allocate the line buffer. For mode Delete a bigger buffer has to */
+ /* be allocated because is it also used to move lines into the file.*/
+ /*********************************************************************/
+ return AllocateBuffer(g);
+ } // end of OpenTableFile
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete a bigger buffer has to */
+/* be allocated because is it also used to move lines into the file. */
+/***********************************************************************/
+bool DOSFAM::AllocateBuffer(PGLOBAL g)
+ {
+ MODE mode = Tdbp->Mode;
+
+ // Lrecl does not include line ending
+ Buflen = Lrecl + Ending + ((Bin) ? 1 : 0);
+
+ if (trace)
+ htrc("SubAllocating a buffer of %d bytes\n", Buflen);
+
+ To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
+
+ if (UseTemp || mode == MODE_DELETE) {
+ // Have a big buffer to move lines
+ Dbflen = Buflen * DOS_BUFF_LEN;
+ DelBuf = PlugSubAlloc(g, NULL, Dbflen);
+ } else if (mode == MODE_INSERT) {
+ /*******************************************************************/
+ /* Prepare the buffer so eventual gaps are filled with blanks. */
+ /*******************************************************************/
+ memset(To_Buf, ' ', Buflen);
+ To_Buf[Buflen - 2] = '\n';
+ To_Buf[Buflen - 1] = '\0';
+ } // endif's mode
+
+ return false;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* GetRowID: return the RowID of last read record. */
+/***********************************************************************/
+int DOSFAM::GetRowID(void)
+ {
+ return Rows;
+ } // end of GetRowID
+
+/***********************************************************************/
+/* GetPos: return the position of last read record. */
+/***********************************************************************/
+int DOSFAM::GetPos(void)
+ {
+ return Fpos;
+ } // end of GetPos
+
+/***********************************************************************/
+/* GetNextPos: return the position of next record. */
+/***********************************************************************/
+int DOSFAM::GetNextPos(void)
+ {
+ return ftell(Stream);
+ } // end of GetNextPos
+
+/***********************************************************************/
+/* SetPos: Replace the table at the specified position. */
+/***********************************************************************/
+bool DOSFAM::SetPos(PGLOBAL g, int pos)
+ {
+ Fpos = pos;
+
+ if (fseek(Stream, Fpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
+ return true;
+ } // endif
+
+ Placed = true;
+ return false;
+ } // end of SetPos
+
+/***********************************************************************/
+/* Record file position in case of UPDATE or DELETE. */
+/***********************************************************************/
+bool DOSFAM::RecordPos(PGLOBAL g)
+ {
+ if ((Fpos = ftell(Stream)) < 0) {
+ sprintf(g->Message, MSG(FTELL_ERROR), 0, strerror(errno));
+ return true;
+ } // endif Fpos
+
+ return false;
+ } // end of RecordPos
+
+/***********************************************************************/
+/* Skip one record in file. */
+/***********************************************************************/
+int DOSFAM::SkipRecord(PGLOBAL g, bool header)
+ {
+ PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
+
+ // Skip this record
+ if (!fgets(To_Buf, Buflen, Stream)) {
+ if (feof(Stream))
+ return RC_EF;
+
+#if defined(UNIX) || defined(UNIV_LINUX)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(0));
+#else
+ sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
+#endif
+ return RC_FX;
+ } // endif fgets
+
+ // Update progress information
+ dup->ProgCur = GetPos();
+
+ if (header) {
+ // For Delete
+ Fpos = ftell(Stream);
+
+ if (!UseTemp)
+ Tpos = Spos = Fpos; // No need to move header
+
+ } // endif header
+
+#if defined(THREAD)
+ return RC_NF; // To have progress info
+#else
+ return RC_OK; // To loop locally
+#endif
+ } // end of SkipRecord
+
+/***********************************************************************/
+/* ReadBuffer: Read one line for a text file. */
+/***********************************************************************/
+int DOSFAM::ReadBuffer(PGLOBAL g)
+ {
+ char *p;
+ int rc;
+
+ if (!Stream)
+ return RC_EF;
+
+ if (trace > 1)
+ htrc("ReadBuffer: Tdbp=%p To_Line=%p Placed=%d\n",
+ Tdbp, Tdbp->To_Line, Placed);
+
+ if (!Placed) {
+ /*******************************************************************/
+ /* Record file position in case of UPDATE or DELETE. */
+ /*******************************************************************/
+ next:
+ if (RecordPos(g))
+ return RC_FX;
+
+ CurBlk = (int)Rows++;
+
+ if (trace > 1)
+ htrc("ReadBuffer: CurBlk=%d\n", CurBlk);
+
+ /********************************************************************/
+ /* Check whether optimization on ROWID */
+ /* can be done, as well as for join as for local filtering. */
+ /*******************************************************************/
+ switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ // Skip this record
+ if ((rc = SkipRecord(g, FALSE)) != RC_OK)
+ return rc;
+
+ goto next;
+ } // endswitch rc
+
+ } else
+ Placed = false;
+
+ if (trace > 1)
+ htrc(" About to read: stream=%p To_Buf=%p Buflen=%d\n",
+ Stream, To_Buf, Buflen);
+
+ if (fgets(To_Buf, Buflen, Stream)) {
+ p = To_Buf + strlen(To_Buf) - 1;
+
+ if (trace > 1)
+ htrc(" Read: To_Buf=%p p=%c\n", To_Buf, To_Buf, p);
+
+#if defined(UNIX)
+ if (true) {
+ // Data files can be imported from Windows (having CRLF)
+#else
+ if (Bin) {
+ // Data file is read in binary so CRLF remains
+#endif
+ if (*p == '\n' || *p == '\r') {
+ // is this enough for Unix ???
+ *p = '\0'; // Eliminate ending CR or LF character
+
+ if (p > To_Buf) {
+ // is this enough for Unix ???
+ p--;
+
+ if (*p == '\n' || *p == '\r')
+ *p = '\0'; // Eliminate ending CR or LF character
+
+ } // endif To_Buf
+
+ } // endif p
+
+ } else if (*p == '\n')
+ *p = '\0'; // Eliminate ending new-line character
+
+ if (trace > 1)
+ htrc(" To_Buf='%s'\n", To_Buf);
+
+ strcpy(Tdbp->To_Line, To_Buf);
+ num_read++;
+ rc = RC_OK;
+ } else if (feof(Stream)) {
+ rc = RC_EF;
+ } else {
+#if defined(UNIX)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(0));
+#else
+ sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
+#endif
+
+ if (trace)
+ htrc("%s\n", g->Message);
+
+ rc = RC_FX;
+ } // endif's fgets
+
+ if (trace > 1)
+ htrc("ReadBuffer: rc=%d\n", rc);
+
+ IsRead = true;
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* WriteBuffer: File write routine for DOS access method. */
+/***********************************************************************/
+int DOSFAM::WriteBuffer(PGLOBAL g)
+ {
+ char *crlf = "\n";
+ int curpos = 0;
+ bool moved = true;
+
+ // T_Stream is the temporary stream or the table file stream itself
+ if (!T_Stream)
+ if (UseTemp && Tdbp->Mode == MODE_UPDATE) {
+ if (OpenTempFile(g))
+ return RC_FX;
+
+ } else
+ T_Stream = Stream;
+
+ if (Tdbp->Mode == MODE_UPDATE) {
+ /*******************************************************************/
+ /* Here we simply rewrite a record on itself. There are two cases */
+ /* were another method should be used, a/ when Update apply to */
+ /* the whole file, b/ when updating the last field of a variable */
+ /* length file. The method could be to rewrite a new file, then */
+ /* to erase the old one and rename the new updated file. */
+ /*******************************************************************/
+ curpos = ftell(Stream);
+
+ if (trace)
+ htrc("Last : %d cur: %d\n", Fpos, curpos);
+
+ if (UseTemp) {
+ /*****************************************************************/
+ /* We are using a temporary file. Before writing the updated */
+ /* record, we must eventually copy all the intermediate records */
+ /* that have not been updated. */
+ /*****************************************************************/
+ if (MoveIntermediateLines(g, &moved))
+ return RC_FX;
+
+ Spos = curpos; // New start position
+ } else
+ // Update is directly written back into the file,
+ // with this (fast) method, record size cannot change.
+ if (fseek(Stream, Fpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
+ return RC_FX;
+ } // endif
+
+ } // endif mode
+
+ /*********************************************************************/
+ /* Prepare the write buffer. */
+ /*********************************************************************/
+#if defined(WIN32)
+ if (Bin)
+ crlf = "\r\n";
+#endif // WIN32
+ strcat(strcpy(To_Buf, Tdbp->To_Line), crlf);
+
+ /*********************************************************************/
+ /* Now start the writing process. */
+ /*********************************************************************/
+ if ((fputs(To_Buf, T_Stream)) == EOF) {
+ sprintf(g->Message, MSG(FPUTS_ERROR), strerror(errno));
+ return RC_FX;
+ } // endif EOF
+
+ if (Tdbp->Mode == MODE_UPDATE && moved)
+ if (fseek(Stream, curpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
+ return RC_FX;
+ } // endif
+
+ if (trace)
+ htrc("write done\n");
+
+ return RC_OK;
+ } // end of WriteBuffer
+
+/***********************************************************************/
+/* Data Base delete line routine for DOS and BLK access methods. */
+/***********************************************************************/
+int DOSFAM::DeleteRecords(PGLOBAL g, int irc)
+ {
+ bool moved;
+ int curpos = ftell(Stream);
+
+ /*********************************************************************/
+ /* There is an alternative here: */
+ /* 1 - use a temporary file in which are copied all not deleted */
+ /* lines, at the end the original file will be deleted and */
+ /* the temporary file renamed to the original file name. */
+ /* 2 - directly move the not deleted lines inside the original */
+ /* file, and at the end erase all trailing records. */
+ /* This will be experimented, but method 1 must be used for Unix as */
+ /* the function needed to erase trailing records is not available. */
+ /*********************************************************************/
+ if (trace)
+ htrc(
+ "DOS DeleteDB: rc=%d UseTemp=%d curpos=%d Fpos=%d Tpos=%d Spos=%d\n",
+ irc, UseTemp, curpos, Fpos, Tpos, Spos);
+
+ if (irc != RC_OK) {
+ /*******************************************************************/
+ /* EOF: position Fpos at the end-of-file position. */
+ /*******************************************************************/
+ fseek(Stream, 0, SEEK_END);
+ Fpos = ftell(Stream);
+
+ if (trace)
+ htrc("Fpos placed at file end=%d\n", Fpos);
+
+ } // endif irc
+
+ if (Tpos == Spos) {
+ /*******************************************************************/
+ /* First line to delete, Open temporary file. */
+ /*******************************************************************/
+ if (UseTemp) {
+ if (OpenTempFile(g))
+ return RC_FX;
+
+ } else {
+ /*****************************************************************/
+ /* Move of eventual preceeding lines is not required here. */
+ /* Set the target file as being the source file itself. */
+ /* Set the future Tpos, and give Spos a value to block copying. */
+ /*****************************************************************/
+ T_Stream = Stream;
+ Spos = Tpos = Fpos;
+ } // endif UseTemp
+
+ } // endif Tpos == Spos
+
+ /*********************************************************************/
+ /* Move any intermediate lines. */
+ /*********************************************************************/
+ if (MoveIntermediateLines(g, &moved))
+ return RC_FX;
+
+ if (irc == RC_OK) {
+ /*******************************************************************/
+ /* Reposition the file pointer and set Spos. */
+ /*******************************************************************/
+ if (!UseTemp || moved)
+ if (fseek(Stream, curpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
+ return RC_FX;
+ } // endif
+
+ Spos = GetNextPos(); // New start position
+
+ if (trace)
+ htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
+
+ } else {
+ /*******************************************************************/
+ /* Last call after EOF has been reached. */
+ /* The UseTemp case is treated in CloseTableFile. */
+ /*******************************************************************/
+ if (!UseTemp) {
+ /*****************************************************************/
+ /* Because the chsize functionality is only accessible with a */
+ /* system call we must close the file and reopen it with the */
+ /* open function (_fopen for MS ??) this is still to be checked */
+ /* for compatibility with Text files and other OS's. */
+ /*****************************************************************/
+ char filename[_MAX_PATH];
+ int h; // File handle, return code
+
+ PlugSetPath(filename, To_File, Tdbp->GetPath());
+ /*rc=*/ PlugCloseFile(g, To_Fb);
+
+ if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0)
+ return RC_FX;
+
+ /*****************************************************************/
+ /* Remove extra records. */
+ /*****************************************************************/
+#if defined(UNIX)
+ if (ftruncate(h, (off_t)Tpos)) {
+ sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
+ close(h);
+ return RC_FX;
+ } // endif
+#else
+ if (chsize(h, Tpos)) {
+ sprintf(g->Message, MSG(CHSIZE_ERROR), strerror(errno));
+ close(h);
+ return RC_FX;
+ } // endif
+#endif
+
+ close(h);
+
+ if (trace)
+ htrc("done, h=%d irc=%d\n", h, irc);
+
+ } // endif !UseTemp
+
+ } // endif irc
+
+ return RC_OK; // All is correct
+ } // end of DeleteRecords
+
+/***********************************************************************/
+/* Open a temporary file used while updating or deleting. */
+/***********************************************************************/
+bool DOSFAM::OpenTempFile(PGLOBAL g)
+ {
+ char tempname[_MAX_PATH];
+ bool rc = false;
+
+ /*********************************************************************/
+ /* Open the temporary file, Spos is at the beginning of file. */
+ /*********************************************************************/
+ PlugSetPath(tempname, To_File, Tdbp->GetPath());
+ strcat(PlugRemoveType(tempname, tempname), ".t");
+
+ if (!(T_Stream = PlugOpenFile(g, tempname, "wb"))) {
+ if (trace)
+ htrc("%s\n", g->Message);
+
+ rc = true;
+ } else
+ To_Fbt = PlgGetUser(g)->Openlist;
+
+ return rc;
+ } // end of OpenTempFile
+
+/***********************************************************************/
+/* Move intermediate deleted or updated lines. */
+/* This works only for file open in binary mode. */
+/***********************************************************************/
+bool DOSFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
+ {
+ int n;
+ size_t req, len;
+
+ for (*b = false, n = Fpos - Spos; n > 0; n -= req) {
+ if (!UseTemp || !*b)
+ if (fseek(Stream, Spos, SEEK_SET)) {
+ sprintf(g->Message, MSG(READ_SEEK_ERROR), strerror(errno));
+ return true;
+ } // endif
+
+ req = (size_t)min(n, Dbflen);
+ len = fread(DelBuf, 1, req, Stream);
+
+ if (trace)
+ htrc("after read req=%d len=%d\n", req, len);
+
+ if (len != req) {
+ sprintf(g->Message, MSG(DEL_READ_ERROR), (int) req, (int) len);
+ return true;
+ } // endif len
+
+ if (!UseTemp)
+ if (fseek(T_Stream, Tpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
+ return true;
+ } // endif
+
+ if ((len = fwrite(DelBuf, 1, req, T_Stream)) != req) {
+ sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
+ return true;
+ } // endif
+
+ if (trace)
+ htrc("after write pos=%d\n", ftell(Stream));
+
+ Tpos += (int)req;
+ Spos += (int)req;
+
+ if (trace)
+ htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
+
+ *b = true;
+ } // endfor n
+
+ return false;
+ } // end of MoveIntermediate Lines
+
+/***********************************************************************/
+/* Delete the old file and rename the new temp file. */
+/***********************************************************************/
+int DOSFAM::RenameTempFile(PGLOBAL g)
+ {
+ char *tempname, filetemp[_MAX_PATH], filename[_MAX_PATH];
+ int rc;
+
+ if (!To_Fbt)
+ return RC_INFO; // Nothing to do ???
+
+ // This loop is necessary because, in case of join,
+ // To_File can have been open several times.
+ for (PFBLOCK fb = PlgGetUser(g)->Openlist; fb; fb = fb->Next)
+ rc = PlugCloseFile(g, fb);
+
+ tempname = (char*)To_Fbt->Fname;
+ PlugSetPath(filename, To_File, Tdbp->GetPath());
+ strcat(PlugRemoveType(filetemp, filename), ".ttt");
+ remove(filetemp); // May still be there from previous error
+
+ if (rename(filename, filetemp)) { // Save file for security
+ sprintf(g->Message, MSG(RENAME_ERROR),
+ filename, filetemp, strerror(errno));
+ rc = RC_FX;
+ } else if (rename(tempname, filename)) {
+ sprintf(g->Message, MSG(RENAME_ERROR),
+ tempname, filename, strerror(errno));
+ rc = rename(filetemp, filename); // Restore saved file
+ rc = RC_FX;
+ } else if (remove(filetemp)) {
+ sprintf(g->Message, MSG(REMOVE_ERROR),
+ filetemp, strerror(errno));
+ rc = RC_INFO; // Acceptable
+ } else
+ rc = RC_OK;
+
+ return rc;
+ } // end of RenameTempFile
+
+/***********************************************************************/
+/* Table file close routine for DOS access method. */
+/***********************************************************************/
+void DOSFAM::CloseTableFile(PGLOBAL g)
+ {
+ int rc;
+
+ if (UseTemp && T_Stream) {
+ if (Tdbp->Mode == MODE_UPDATE) {
+ // Copy eventually remaining lines
+ bool b;
+
+ fseek(Stream, 0, SEEK_END);
+ Fpos = ftell(Stream);
+ rc = MoveIntermediateLines(g, &b);
+ } // endif Mode
+
+ // Delete the old file and rename the new temp file.
+ RenameTempFile(g); // Also close all files
+ } else {
+ rc = PlugCloseFile(g, To_Fb);
+
+ if (trace)
+ htrc("DOS Close: closing %s rc=%d\n", To_File, rc);
+
+ } // endif UseTemp
+
+ Stream = NULL; // So we can know whether table is open
+ } // end of CloseTableFile
+
+/***********************************************************************/
+/* Rewind routine for DOS access method. */
+/***********************************************************************/
+void DOSFAM::Rewind(void)
+ {
+ if (Stream) // Can be NULL when making index on void table
+ rewind(Stream);
+
+ Rows = 0;
+ OldBlk = CurBlk = -1;
+ } // end of Rewind
+
+/* --------------------------- Class BLKFAM -------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+BLKFAM::BLKFAM(PDOSDEF tdp) : DOSFAM(tdp)
+ {
+ Blocked = true;
+ Block = tdp->GetBlock();
+ Last = tdp->GetLast();
+ Nrec = tdp->GetElemt();
+ Closing = false;
+ BlkPos = tdp->GetTo_Pos();
+ CurLine = NULL;
+ NxtLine = NULL;
+ OutBuf = NULL;
+ } // end of BLKFAM standard constructor
+
+BLKFAM::BLKFAM(PBLKFAM txfp) : DOSFAM(txfp)
+ {
+ Closing = txfp->Closing;
+ CurLine = txfp->CurLine;
+ NxtLine = txfp->NxtLine;
+ OutBuf = txfp->OutBuf;
+ } // end of BLKFAM copy constructor
+
+/***********************************************************************/
+/* Reset: reset position values at the beginning of file. */
+/***********************************************************************/
+void BLKFAM::Reset(void)
+ {
+ DOSFAM::Reset();
+ Closing = false;
+ } // end of Reset
+
+/***********************************************************************/
+/* Cardinality: returns table cardinality in number of rows. */
+/* This function can be called with a null argument to test the */
+/* availability of Cardinality implementation (1 yes, 0 no). */
+/***********************************************************************/
+int BLKFAM::Cardinality(PGLOBAL g)
+ {
+ // Should not be called in this version
+ return (g) ? -1 : 0;
+//return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
+ } // end of Cardinality
+
+/***********************************************************************/
+/* Use BlockTest to reduce the table estimated size. */
+/***********************************************************************/
+int BLKFAM::MaxBlkSize(PGLOBAL g, int s)
+ {
+ int rc = RC_OK, savcur = CurBlk;
+ int size;
+
+ // Roughly estimate the table size as the sum of blocks
+ // that can contain good rows
+ for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
+ if ((rc = Tdbp->TestBlock(g)) == RC_OK)
+ size += (CurBlk == Block - 1) ? Last : Nrec;
+ else if (rc == RC_EF)
+ break;
+
+ CurBlk = savcur;
+ return size;
+ } // end of MaxBlkSize
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete or when a temp file is */
+/* used another big buffer has to be allocated because is it used */
+/* to move or update the lines into the (temp) file. */
+/***********************************************************************/
+bool BLKFAM::AllocateBuffer(PGLOBAL g)
+ {
+ int len;
+ MODE mode = Tdbp->GetMode();
+
+ // For variable length files, Lrecl does not include CRLF
+ len = Lrecl + ((Tdbp->GetFtype()) ? 0 : Ending);
+ Buflen = len * Nrec;
+ CurLine = To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
+
+ if (UseTemp || mode == MODE_DELETE) {
+ if (mode == MODE_UPDATE)
+ OutBuf = (char*)PlugSubAlloc(g, NULL, len + 1);
+
+ Dbflen = Buflen;
+ DelBuf = PlugSubAlloc(g, NULL, Dbflen);
+ } else if (mode == MODE_INSERT)
+ Rbuf = Nrec; // To be used by WriteDB
+
+ return false;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* GetRowID: return the RowID of last read record. */
+/***********************************************************************/
+int BLKFAM::GetRowID(void)
+ {
+ return CurNum + Nrec * CurBlk + 1;
+ } // end of GetRowID
+
+/***********************************************************************/
+/* GetPos: return the position of last read record. */
+/***********************************************************************/
+int BLKFAM::GetPos(void)
+ {
+ return (CurNum + Nrec * CurBlk); // Computed file index
+ } // end of GetPos
+
+/***********************************************************************/
+/* GetNextPos: called by DeleteRecords. */
+/***********************************************************************/
+int BLKFAM::GetNextPos(void)
+ {
+ return Fpos + NxtLine - CurLine;
+ } // end of GetNextPos
+
+/***********************************************************************/
+/* SetPos: Replace the table at the specified position. */
+/***********************************************************************/
+bool BLKFAM::SetPos(PGLOBAL g, int pos)
+ {
+ if (pos < 0) {
+ strcpy(g->Message, MSG(INV_REC_POS));
+ return true;
+ } // endif recpos
+
+ CurBlk = pos / Nrec;
+ CurNum = pos % Nrec;
+#if defined(_DEBUG)
+ num_eq[(CurBlk == OldBlk) ? 1 : 0]++;
+#endif
+
+ // Indicate the table position was externally set
+ Placed = true;
+ return false;
+ } // end of SetPos
+
+/***********************************************************************/
+/* Record file position in case of UPDATE or DELETE. */
+/* Not used yet for blocked tables. */
+/***********************************************************************/
+bool BLKFAM::RecordPos(PGLOBAL g)
+ {
+ Fpos = (CurNum + Nrec * CurBlk); // Computed file index
+ return false;
+ } // end of RecordPos
+
+/***********************************************************************/
+/* Skip one record in file. */
+/***********************************************************************/
+int BLKFAM::SkipRecord(PGLOBAL g, bool header)
+ {
+ if (header) {
+ // For Delete
+ Fpos = BlkPos[0]; // First block starts after the header
+
+ if (!UseTemp)
+ Tpos = Spos = Fpos; // No need to move header
+
+ } // endif header
+
+ OldBlk = -2; // To force fseek on first block
+ return RC_OK;
+ } // end of SkipRecord
+
+/***********************************************************************/
+/* ReadBuffer: Read one line for a text file. */
+/***********************************************************************/
+int BLKFAM::ReadBuffer(PGLOBAL g)
+ {
+ int i, n, rc = RC_OK;
+
+ /*********************************************************************/
+ /* Sequential reading when Placed is not true. */
+ /*********************************************************************/
+ if (Placed) {
+ Placed = false;
+ } else if (++CurNum < Rbuf) {
+ CurLine = NxtLine;
+
+ // Get the position of the next line in the buffer
+ while (*NxtLine++ != '\n') ;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - Ending;
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+ goto fin;
+ } else if (Rbuf < Nrec && CurBlk != -1) {
+ return RC_EF;
+ } else {
+ /*******************************************************************/
+ /* New block. */
+ /*******************************************************************/
+ CurNum = 0;
+
+ next:
+ if (++CurBlk >= Block)
+ return RC_EF;
+
+ /*******************************************************************/
+ /* Before reading a new block, check whether block optimization */
+ /* can be done, as well as for join as for local filtering. */
+ /*******************************************************************/
+ switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ goto next;
+ } // endswitch rc
+
+ } // endif's
+
+ if (OldBlk == CurBlk)
+ goto ok; // Block is already there
+
+ // fseek is required only in non sequential reading
+ if (CurBlk != OldBlk + 1)
+ if (fseek(Stream, BlkPos[CurBlk], SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), BlkPos[CurBlk]);
+ return RC_FX;
+ } // endif fseek
+
+ // Calculate the length of block to read
+ BlkLen = BlkPos[CurBlk + 1] - BlkPos[CurBlk];
+
+ if (trace)
+ htrc("File position is now %d\n", ftell(Stream));
+
+ // Read the entire next block
+ n = fread(To_Buf, 1, (size_t)BlkLen, Stream);
+
+ if (n == BlkLen) {
+// ReadBlks++;
+ num_read++;
+ Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
+
+ ok:
+ rc = RC_OK;
+
+ // Get the position of the current line
+ for (i = 0, CurLine = To_Buf; i < CurNum; i++)
+ while (*CurLine++ != '\n') ; // What about Unix ???
+
+ // Now get the position of the next line
+ for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - Ending;
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+ } else if (feof(Stream)) {
+ rc = RC_EF;
+ } else {
+#if defined(UNIX)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
+#else
+ sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
+#endif
+
+ if (trace)
+ htrc("%s\n", g->Message);
+
+ return RC_FX;
+ } // endelse
+
+ OldBlk = CurBlk; // Last block actually read
+ IsRead = true; // Is read indeed
+
+ fin:
+ // Store the current record file position for Delete and Update
+ Fpos = BlkPos[CurBlk] + CurLine - To_Buf;
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* WriteBuffer: File write routine for the blocked DOS access method. */
+/* Update is directly written back into the file, */
+/* with this (fast) method, record size cannot change. */
+/***********************************************************************/
+int BLKFAM::WriteBuffer(PGLOBAL g)
+ {
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ /*******************************************************************/
+ /* In Insert mode, blocks are added sequentially to the file end. */
+ /*******************************************************************/
+ if (!Closing) { // Add line to the write buffer
+ strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
+
+ if (++CurNum != Rbuf) {
+ CurLine += strlen(CurLine);
+ return RC_OK; // We write only full blocks
+ } // endif CurNum
+
+ } // endif Closing
+
+ // Now start the writing process.
+ NxtLine = CurLine + strlen(CurLine);
+ BlkLen = NxtLine - To_Buf;
+
+ if (fwrite(To_Buf, 1, BlkLen, Stream) != (size_t)BlkLen) {
+ sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
+ Closing = true; // To tell CloseDB about a Write error
+ return RC_FX;
+ } // endif size
+
+ CurBlk++;
+ CurNum = 0;
+ CurLine = To_Buf;
+ } else {
+ /*******************************************************************/
+ /* Mode == MODE_UPDATE. */
+ /*******************************************************************/
+ char *crlf;
+ size_t len;
+ int curpos = ftell(Stream);
+ bool moved = true;
+
+ // T_Stream is the temporary stream or the table file stream itself
+ if (!T_Stream)
+ if (UseTemp /*&& Tdbp->GetMode() == MODE_UPDATE*/) {
+ if (OpenTempFile(g))
+ return RC_FX;
+
+ } else
+ T_Stream = Stream;
+
+ if (UseTemp) {
+ /*****************************************************************/
+ /* We are using a temporary file. Before writing the updated */
+ /* record, we must eventually copy all the intermediate records */
+ /* that have not been updated. */
+ /*****************************************************************/
+ if (MoveIntermediateLines(g, &moved))
+ return RC_FX;
+
+ Spos = GetNextPos(); // New start position
+
+ // Prepare the output buffer
+#if defined(WIN32)
+ crlf = "\r\n";
+#else
+ crlf = "\n";
+#endif // WIN32
+ strcat(strcpy(OutBuf, Tdbp->GetLine()), crlf);
+ len = strlen(OutBuf);
+ } else {
+ if (fseek(Stream, Fpos, SEEK_SET)) { // Fpos is last position
+ sprintf(g->Message, MSG(FSETPOS_ERROR), 0);
+ return RC_FX;
+ } // endif fseek
+
+ // Replace the line inside read buffer (length has not changed)
+ memcpy(CurLine, Tdbp->GetLine(), strlen(Tdbp->GetLine()));
+ OutBuf = CurLine;
+ len = (size_t)(NxtLine - CurLine);
+ } // endif UseTemp
+
+ if (fwrite(OutBuf, 1, len, T_Stream) != (size_t)len) {
+ sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
+ return RC_FX;
+ } // endif fwrite
+
+ if (moved)
+ if (fseek(Stream, curpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
+ return RC_FX;
+ } // endif
+
+ } // endif Mode
+
+ return RC_OK;
+ } // end of WriteBuffer
+
+/***********************************************************************/
+/* Table file close routine for DOS access method. */
+/***********************************************************************/
+void BLKFAM::CloseTableFile(PGLOBAL g)
+ {
+ int rc, wrc = RC_OK;
+
+ if (UseTemp && T_Stream) {
+ if (Tdbp->GetMode() == MODE_UPDATE) {
+ // Copy eventually remaining lines
+ bool b;
+
+ fseek(Stream, 0, SEEK_END);
+ Fpos = ftell(Stream);
+ rc = MoveIntermediateLines(g, &b);
+ } else
+ rc = RC_OK;
+
+ if (rc == RC_OK)
+ // Delete the old file and rename the new temp file.
+ rc = RenameTempFile(g); // Also close all files
+ else
+ rc = PlugCloseFile(g, To_Fb);
+
+ } else {
+ // Closing is True if last Write was in error
+ if (Tdbp->GetMode() == MODE_INSERT && CurNum && !Closing) {
+ // Some more inserted lines remain to be written
+ Rbuf = CurNum--;
+ Closing = true;
+ wrc = WriteBuffer(g);
+ } else if (Modif && !Closing) {
+ // Last updated block remains to be written
+ Closing = true;
+ wrc = ReadBuffer(g);
+ } // endif's
+
+ rc = PlugCloseFile(g, To_Fb);
+
+ if (trace)
+ htrc("BLK CloseTableFile: closing %s mode=%d wrc=%d rc=%d\n",
+ To_File, Tdbp->GetMode(), wrc, rc);
+
+ } // endif UseTemp
+
+ Stream = NULL; // So we can know whether table is open
+ } // end of CloseTableFile
+
+/***********************************************************************/
+/* Rewind routine for DOS access method. */
+/* Note: commenting out OldBlk = -1 has two advantages: */
+/* 1 - It forces fseek on first block, thus suppressing the need to */
+/* rewind the file, anyway unuseful when second pass if indexed. */
+/* 2 - It permit to avoid re-reading small tables having only 1 block.*/
+/***********************************************************************/
+void BLKFAM::Rewind(void)
+ {
+//rewind(Stream); will be placed by fseek
+ CurBlk = -1;
+ CurNum = Rbuf;
+//OldBlk = -1; commented out in case we reuse last read block
+//Rbuf = 0; commented out in case we reuse last read block
+ } // end of Rewind
+
diff --git a/storage/connect/filamzip.cpp b/storage/connect/filamzip.cpp
index 8ae7e5863af..1b687ab9ddc 100644
--- a/storage/connect/filamzip.cpp
+++ b/storage/connect/filamzip.cpp
@@ -1,1405 +1,1402 @@
-/*********** File AM Zip C++ Program Source Code File (.CPP) ***********/
-/* PROGRAM NAME: FILAMZIP */
-/* ------------- */
-/* Version 1.5 */
-/* */
-/* COPYRIGHT: */
-/* ---------- */
-/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are the ZLIB compressed files classes. */
-/* */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant MariaDB header file. */
-/***********************************************************************/
-#include "my_global.h"
-#if defined(WIN32)
-#include <io.h>
-#include <fcntl.h>
-#if defined(__BORLANDC__)
-#define __MFC_COMPAT__ // To define min/max as macro
-#endif
-//#include <windows.h>
-#else // !WIN32
-#if defined(UNIX)
-#include <errno.h>
-#else // !UNIX
-#include <io.h>
-#endif
-#include <fcntl.h>
-#endif // !WIN32
-
-/***********************************************************************/
-/* Include application header files: */
-/* global.h is header containing all global declarations. */
-/* plgdbsem.h is header containing the DB application declarations. */
-/* tabdos.h is header containing the TABDOS class declarations. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-//#include "catalog.h"
-//#include "reldef.h"
-//#include "xobject.h"
-//#include "kindex.h"
-#include "filamtxt.h"
-#include "tabdos.h"
-#if defined(UNIX)
-#include "osutil.h"
-#endif
-
-/***********************************************************************/
-/* This define prepares ZLIB function declarations. */
-/***********************************************************************/
-//#define ZLIB_DLL
-
-#include "filamzip.h"
-
-/***********************************************************************/
-/* DB static variables. */
-/***********************************************************************/
-extern int num_read, num_there, num_eq[]; // Statistics
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Implementation of the ZIPFAM class. */
-/***********************************************************************/
-ZIPFAM::ZIPFAM(PZIPFAM txfp) : TXTFAM(txfp)
- {
- Zfile = txfp->Zfile;
- Zpos = txfp->Zpos;
- } // end of ZIPFAM copy constructor
-
-/***********************************************************************/
-/* Zerror: Error function for gz calls. */
-/* gzerror returns the error message for the last error which occurred*/
-/* on the given compressed file. errnum is set to zlib error number. */
-/* If an error occurred in the file system and not in the compression */
-/* library, errnum is set to Z_ERRNO and the application may consult */
-/* errno to get the exact error code. */
-/***********************************************************************/
-int ZIPFAM::Zerror(PGLOBAL g)
- {
- int errnum;
-
- strcpy(g->Message, gzerror(Zfile, &errnum));
-
- if (errnum == Z_ERRNO)
-#if defined(WIN32)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(NULL));
-#else // !WIN32
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
-#endif // !WIN32
-
- return (errnum == Z_STREAM_END) ? RC_EF : RC_FX;
- } // end of Zerror
-
-/***********************************************************************/
-/* Reset: reset position values at the beginning of file. */
-/***********************************************************************/
-void ZIPFAM::Reset(void)
- {
- TXTFAM::Reset();
-//gzrewind(Zfile); // Useful ?????
- Zpos = 0;
- } // end of Reset
-
-/***********************************************************************/
-/* ZIP GetFileLength: returns an estimate of what would be the */
-/* uncompressed file size in number of bytes. */
-/***********************************************************************/
-int ZIPFAM::GetFileLength(PGLOBAL g)
- {
- int len = TXTFAM::GetFileLength(g);
-
- if (len > 0)
- // Estimate size reduction to a max of 6
- len *= 6;
-
- return len;
- } // end of GetFileLength
-
-/***********************************************************************/
-/* ZIP Access Method opening routine. */
-/***********************************************************************/
-bool ZIPFAM::OpenTableFile(PGLOBAL g)
- {
- char opmode[4], filename[_MAX_PATH];
- MODE mode = Tdbp->GetMode();
-
- switch (mode) {
- case MODE_READ:
- strcpy(opmode, "r");
- break;
- case MODE_UPDATE:
- /*****************************************************************/
- /* Updating ZIP files not implemented yet. */
- /*****************************************************************/
- strcpy(g->Message, MSG(UPD_ZIP_NOT_IMP));
- return true;
- case MODE_DELETE:
- if (!Tdbp->GetNext()) {
- // Store the number of deleted lines
- DelRows = Cardinality(g);
-
- // This will erase the entire file
- strcpy(opmode, "w");
-// Block = 0; // For ZBKFAM
-// Last = Nrec; // For ZBKFAM
- Tdbp->ResetSize();
- } else {
- sprintf(g->Message, MSG(NO_PART_DEL), "ZIP");
- return true;
- } // endif filter
-
- break;
- case MODE_INSERT:
- strcpy(opmode, "a+");
- break;
- default:
- sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
- return true;
- } // endswitch Mode
-
- /*********************************************************************/
- /* Open according to logical input/output mode required. */
- /* Use specific zlib functions. */
- /* Treat files as binary. */
- /*********************************************************************/
- strcat(opmode, "b");
- Zfile = gzopen(PlugSetPath(filename, To_File, Tdbp->GetPath()), opmode);
-
- if (Zfile == NULL) {
- sprintf(g->Message, MSG(GZOPEN_ERROR),
- opmode, (int)errno, filename);
- strcat(strcat(g->Message, ": "), strerror(errno));
- return (mode == MODE_READ && errno == ENOENT)
- ? PushWarning(g, Tdbp) : true;
- } // endif Zfile
-
- /*********************************************************************/
- /* Something to be done here. >>>>>>>> NOT DONE <<<<<<<< */
- /*********************************************************************/
-//To_Fb = dbuserp->Openlist; // Keep track of File block
-
- /*********************************************************************/
- /* Allocate the line buffer. */
- /*********************************************************************/
- return AllocateBuffer(g);
- } // end of OpenTableFile
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete a bigger buffer has to */
-/* be allocated because is it also used to move lines into the file. */
-/***********************************************************************/
-bool ZIPFAM::AllocateBuffer(PGLOBAL g)
- {
- MODE mode = Tdbp->GetMode();
-
- Buflen = Lrecl + 2; // Lrecl does not include CRLF
-//Buflen *= ((Mode == MODE_DELETE) ? DOS_BUFF_LEN : 1); NIY
-
-#ifdef DEBTRACE
- htrc("SubAllocating a buffer of %d bytes\n", Buflen);
-#endif
-
- To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
-
- if (mode == MODE_INSERT) {
- /*******************************************************************/
- /* For Insert buffer must be prepared. */
- /*******************************************************************/
- memset(To_Buf, ' ', Buflen);
- To_Buf[Buflen - 2] = '\n';
- To_Buf[Buflen - 1] = '\0';
- } // endif Insert
-
- return false;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* GetRowID: return the RowID of last read record. */
-/***********************************************************************/
-int ZIPFAM::GetRowID(void)
- {
- return Rows;
- } // end of GetRowID
-
-/***********************************************************************/
-/* GetPos: return the position of last read record. */
-/***********************************************************************/
-int ZIPFAM::GetPos(void)
- {
- return (int)Zpos;
- } // end of GetPos
-
-/***********************************************************************/
-/* GetNextPos: return the position of next record. */
-/***********************************************************************/
-int ZIPFAM::GetNextPos(void)
- {
- return gztell(Zfile);
- } // end of GetNextPos
-
-/***********************************************************************/
-/* SetPos: Replace the table at the specified position. */
-/***********************************************************************/
-bool ZIPFAM::SetPos(PGLOBAL g, int pos)
- {
- sprintf(g->Message, MSG(NO_SETPOS_YET), "ZIP");
- return true;
-#if 0
- Fpos = pos;
-
- if (fseek(Stream, Fpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
- return true;
- } // endif
-
- Placed = true;
- return false;
-#endif // 0
- } // end of SetPos
-
-/***********************************************************************/
-/* Record file position in case of UPDATE or DELETE. */
-/***********************************************************************/
-bool ZIPFAM::RecordPos(PGLOBAL g)
- {
- Zpos = gztell(Zfile);
- return false;
- } // end of RecordPos
-
-/***********************************************************************/
-/* Skip one record in file. */
-/***********************************************************************/
-int ZIPFAM::SkipRecord(PGLOBAL g, bool header)
- {
- // Skip this record
- if (gzeof(Zfile))
- return RC_EF;
- else if (gzgets(Zfile, To_Buf, Buflen) == Z_NULL)
- return Zerror(g);
-
- if (header)
- RecordPos(g);
-
- return RC_OK;
- } // end of SkipRecord
-
-/***********************************************************************/
-/* ReadBuffer: Read one line from a compressed text file. */
-/***********************************************************************/
-int ZIPFAM::ReadBuffer(PGLOBAL g)
- {
- char *p;
- int rc;
-
- if (!Zfile)
- return RC_EF;
-
- if (!Placed) {
- /*******************************************************************/
- /* Record file position in case of UPDATE or DELETE. */
- /*******************************************************************/
- next:
- if (RecordPos(g))
- return RC_FX;
-
- CurBlk = Rows++; // Update RowID
-
- /*******************************************************************/
- /* Check whether optimization on ROWID */
- /* can be done, as well as for join as for local filtering. */
- /*******************************************************************/
- switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- // Skip this record
- if ((rc = SkipRecord(g, FALSE)) != RC_OK)
- return rc;
-
- goto next;
- } // endswitch rc
-
- } else
- Placed = false;
-
- if (gzeof(Zfile)) {
- rc = RC_EF;
- } else if (gzgets(Zfile, To_Buf, Buflen) != Z_NULL) {
- p = To_Buf + strlen(To_Buf) - 1;
-
- if (*p == '\n')
- *p = '\0'; // Eliminate ending new-line character
-
- if (*(--p) == '\r')
- *p = '\0'; // Eliminate eventuel carriage return
-
- strcpy(Tdbp->GetLine(), To_Buf);
- IsRead = true;
- rc = RC_OK;
- num_read++;
- } else
- rc = Zerror(g);
-
-#ifdef DEBTRACE
- htrc(" Read: '%s' rc=%d\n", To_Buf, rc);
-#endif
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* WriteDB: Data Base write routine for ZDOS access method. */
-/* Update is not possible without using a temporary file (NIY). */
-/***********************************************************************/
-int ZIPFAM::WriteBuffer(PGLOBAL g)
- {
- /*********************************************************************/
- /* Prepare the write buffer. */
- /*********************************************************************/
- strcat(strcpy(To_Buf, Tdbp->GetLine()), CrLf);
-
- /*********************************************************************/
- /* Now start the writing process. */
- /*********************************************************************/
- if (gzputs(Zfile, To_Buf) < 0)
- return Zerror(g);
-
- return RC_OK;
- } // end of WriteBuffer
-
-/***********************************************************************/
-/* Data Base delete line routine for ZDOS access method. (NIY) */
-/***********************************************************************/
-int ZIPFAM::DeleteRecords(PGLOBAL g, int irc)
- {
- strcpy(g->Message, MSG(NO_ZIP_DELETE));
- return RC_FX;
- } // end of DeleteRecords
-
-/***********************************************************************/
-/* Data Base close routine for DOS access method. */
-/***********************************************************************/
-void ZIPFAM::CloseTableFile(PGLOBAL g)
- {
- int rc = gzclose(Zfile);
-
-#ifdef DEBTRACE
- htrc("ZIP CloseDB: closing %s rc=%d\n", To_File, rc);
-#endif
-
- Zfile = NULL; // So we can know whether table is open
-//To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
- } // end of CloseTableFile
-
-/***********************************************************************/
-/* Rewind routine for ZIP access method. */
-/***********************************************************************/
-void ZIPFAM::Rewind(void)
- {
- gzrewind(Zfile);
- } // end of Rewind
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-ZBKFAM::ZBKFAM(PDOSDEF tdp) : ZIPFAM(tdp)
- {
- Blocked = true;
- Block = tdp->GetBlock();
- Last = tdp->GetLast();
- Nrec = tdp->GetElemt();
- CurLine = NULL;
- NxtLine = NULL;
- Closing = false;
- BlkPos = tdp->GetTo_Pos();
- } // end of ZBKFAM standard constructor
-
-ZBKFAM::ZBKFAM(PZBKFAM txfp) : ZIPFAM(txfp)
- {
- CurLine = txfp->CurLine;
- NxtLine = txfp->NxtLine;
- Closing = txfp->Closing;
- } // end of ZBKFAM copy constructor
-
-/***********************************************************************/
-/* Use BlockTest to reduce the table estimated size. */
-/***********************************************************************/
-int ZBKFAM::MaxBlkSize(PGLOBAL g, int s)
- {
- int rc = RC_OK, savcur = CurBlk;
- int size;
-
- // Roughly estimate the table size as the sum of blocks
- // that can contain good rows
- for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
- if ((rc = Tdbp->TestBlock(g)) == RC_OK)
- size += (CurBlk == Block - 1) ? Last : Nrec;
- else if (rc == RC_EF)
- break;
-
- CurBlk = savcur;
- return size;
- } // end of MaxBlkSize
-
-/***********************************************************************/
-/* ZBK Cardinality: returns table cardinality in number of rows. */
-/* This function can be called with a null argument to test the */
-/* availability of Cardinality implementation (1 yes, 0 no). */
-/***********************************************************************/
-int ZBKFAM::Cardinality(PGLOBAL g)
- {
- // Should not be called in this version
- return (g) ? -1 : 0;
-//return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
- } // end of Cardinality
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete a bigger buffer has to */
-/* be allocated because is it also used to move lines into the file. */
-/***********************************************************************/
-bool ZBKFAM::AllocateBuffer(PGLOBAL g)
- {
- Buflen = Nrec * (Lrecl + 2);
- CurLine = To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
-
- if (Tdbp->GetMode() == MODE_INSERT) {
- // Set values so Block and Last can be recalculated
- if (Last == Nrec) {
- CurBlk = Block;
- Rbuf = Nrec; // To be used by WriteDB
- } else {
- // The last block must be completed
- CurBlk = Block - 1;
- Rbuf = Nrec - Last; // To be used by WriteDB
- } // endif Last
-
- } // endif Insert
-
- return false;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* GetRowID: return the RowID of last read record. */
-/***********************************************************************/
-int ZBKFAM::GetRowID(void)
- {
- return CurNum + Nrec * CurBlk + 1;
- } // end of GetRowID
-
-/***********************************************************************/
-/* GetPos: return the position of last read record. */
-/***********************************************************************/
-int ZBKFAM::GetPos(void)
- {
- return CurNum + Nrec * CurBlk; // Computed file index
- } // end of GetPos
-
-/***********************************************************************/
-/* Record file position in case of UPDATE or DELETE. */
-/* Not used yet for fixed tables. */
-/***********************************************************************/
-bool ZBKFAM::RecordPos(PGLOBAL g)
- {
-//strcpy(g->Message, "RecordPos not implemented for zip blocked tables");
-//return true;
- return RC_OK;
- } // end of RecordPos
-
-/***********************************************************************/
-/* Skip one record in file. */
-/***********************************************************************/
-int ZBKFAM::SkipRecord(PGLOBAL g, bool header)
- {
-//strcpy(g->Message, "SkipRecord not implemented for zip blocked tables");
-//return RC_FX;
- return RC_OK;
- } // end of SkipRecord
-
-/***********************************************************************/
-/* ReadBuffer: Read one line from a compressed text file. */
-/***********************************************************************/
-int ZBKFAM::ReadBuffer(PGLOBAL g)
- {
- int n, skip, rc = RC_OK;
-
- /*********************************************************************/
- /* Sequential reading when Placed is not true. */
- /*********************************************************************/
- if (++CurNum < Rbuf) {
- CurLine = NxtLine;
-
- // Get the position of the next line in the buffer
- while (*NxtLine++ != '\n') ;
-
- // Set caller line buffer
- n = NxtLine - CurLine - Ending;
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
- return RC_OK;
- } else if (Rbuf < Nrec && CurBlk != -1)
- return RC_EF;
-
- /*********************************************************************/
- /* New block. */
- /*********************************************************************/
- CurNum = 0;
- skip = 0;
-
- next:
- if (++CurBlk >= Block)
- return RC_EF;
-
- /*********************************************************************/
- /* Before using the new block, check whether block optimization */
- /* can be done, as well as for join as for local filtering. */
- /*********************************************************************/
- switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- skip++;
- goto next;
- } // endswitch rc
-
- if (skip)
- // Skip blocks rejected by block optimization
- for (int i = CurBlk - skip; i < CurBlk; i++) {
- BlkLen = BlkPos[i + 1] - BlkPos[i];
-
- if (gzseek(Zfile, (z_off_t)BlkLen, SEEK_CUR) < 0)
- return Zerror(g);
-
- } // endfor i
-
- BlkLen = BlkPos[CurBlk + 1] - BlkPos[CurBlk];
-
- if (!(n = gzread(Zfile, To_Buf, BlkLen))) {
- rc = RC_EF;
- } else if (n > 0) {
- // Get the position of the current line
- CurLine = To_Buf;
-
- // Now get the position of the next line
- for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
-
- // Set caller line buffer
- n = NxtLine - CurLine - Ending;
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
- Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
- IsRead = true;
- rc = RC_OK;
- num_read++;
- } else
- rc = Zerror(g);
-
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* WriteDB: Data Base write routine for ZDOS access method. */
-/* Update is not possible without using a temporary file (NIY). */
-/***********************************************************************/
-int ZBKFAM::WriteBuffer(PGLOBAL g)
- {
- /*********************************************************************/
- /* Prepare the write buffer. */
- /*********************************************************************/
- if (!Closing)
- strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
-
- /*********************************************************************/
- /* In Insert mode, blocs are added sequentialy to the file end. */
- /* Note: Update mode is not handled for zip files. */
- /*********************************************************************/
- if (++CurNum == Rbuf) {
- /*******************************************************************/
- /* New block, start the writing process. */
- /*******************************************************************/
- BlkLen = CurLine + strlen(CurLine) - To_Buf;
-
- if (gzwrite(Zfile, To_Buf, BlkLen) != BlkLen ||
- gzflush(Zfile, Z_FULL_FLUSH)) {
- Closing = true;
- return Zerror(g);
- } // endif gzwrite
-
- Rbuf = Nrec;
- CurBlk++;
- CurNum = 0;
- CurLine = To_Buf;
- } else
- CurLine += strlen(CurLine);
-
- return RC_OK;
- } // end of WriteBuffer
-
-/***********************************************************************/
-/* Data Base delete line routine for ZBK access method. */
-/* Implemented only for total deletion of the table, which is done */
-/* by opening the file in mode "wb". */
-/***********************************************************************/
-int ZBKFAM::DeleteRecords(PGLOBAL g, int irc)
- {
- if (irc == RC_EF) {
- LPCSTR name = Tdbp->GetName();
- PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
- PCATLG cat = PlgGetCatalog(g);
-
- defp->SetBlock(0);
- defp->SetLast(Nrec);
-
- if (!cat->SetIntCatInfo("Blocks", 0) ||
- !cat->SetIntCatInfo("Last", 0)) {
- sprintf(g->Message, MSG(UPDATE_ERROR), "Header");
- return RC_FX;
- } else
- return RC_OK;
-
- } else
- return irc;
-
- } // end of DeleteRecords
-
-/***********************************************************************/
-/* Data Base close routine for ZBK access method. */
-/***********************************************************************/
-void ZBKFAM::CloseTableFile(PGLOBAL g)
- {
- int rc = RC_OK;
-
- if (Tdbp->GetMode() == MODE_INSERT) {
- PCATLG cat = PlgGetCatalog(g);
- LPCSTR name = Tdbp->GetName();
- PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
-
- if (CurNum && !Closing) {
- // Some more inserted lines remain to be written
- Last = (Nrec - Rbuf) + CurNum;
- Block = CurBlk + 1;
- Rbuf = CurNum--;
- Closing = true;
- rc = WriteBuffer(g);
- } else if (Rbuf == Nrec) {
- Last = Nrec;
- Block = CurBlk;
- } // endif CurNum
-
- if (rc != RC_FX) {
- defp->SetBlock(Block);
- defp->SetLast(Last);
- cat->SetIntCatInfo("Blocks", Block);
- cat->SetIntCatInfo("Last", Last);
- } // endif
-
- gzclose(Zfile);
- } else if (Tdbp->GetMode() == MODE_DELETE) {
- rc = DeleteRecords(g, RC_EF);
- gzclose(Zfile);
- } else
- rc = gzclose(Zfile);
-
-#ifdef DEBTRACE
- htrc("ZIP CloseDB: closing %s rc=%d\n", To_File, rc);
-#endif
-
- Zfile = NULL; // So we can know whether table is open
-//To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
- } // end of CloseTableFile
-
-/***********************************************************************/
-/* Rewind routine for ZBK access method. */
-/***********************************************************************/
-void ZBKFAM::Rewind(void)
- {
- gzrewind(Zfile);
- CurBlk = -1;
- CurNum = Rbuf;
- } // end of Rewind
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-ZIXFAM::ZIXFAM(PDOSDEF tdp) : ZBKFAM(tdp)
- {
-//Block = tdp->GetBlock();
-//Last = tdp->GetLast();
- Nrec = (tdp->GetElemt()) ? tdp->GetElemt() : DOS_BUFF_LEN;
- Blksize = Nrec * Lrecl;
- } // end of ZIXFAM standard constructor
-
-/***********************************************************************/
-/* ZIX Cardinality: returns table cardinality in number of rows. */
-/* This function can be called with a null argument to test the */
-/* availability of Cardinality implementation (1 yes, 0 no). */
-/***********************************************************************/
-int ZIXFAM::Cardinality(PGLOBAL g)
- {
- if (Last)
- return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
- else // Last and Block not defined, cannot do it yet
- return 0;
-
- } // end of Cardinality
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete a bigger buffer has to */
-/* be allocated because is it also used to move lines into the file. */
-/***********************************************************************/
-bool ZIXFAM::AllocateBuffer(PGLOBAL g)
- {
- Buflen = Blksize;
- To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
-
- if (Tdbp->GetMode() == MODE_INSERT) {
- /*******************************************************************/
- /* For Insert the buffer must be prepared. */
- /*******************************************************************/
- memset(To_Buf, ' ', Buflen);
-
- if (Tdbp->GetFtype() < 2)
- // if not binary, the file is physically a text file
- for (int len = Lrecl; len <= Buflen; len += Lrecl) {
-#if defined(WIN32)
- To_Buf[len - 2] = '\r';
-#endif // WIN32
- To_Buf[len - 1] = '\n';
- } // endfor len
-
- // Set values so Block and Last can be recalculated
- if (Last == Nrec) {
- CurBlk = Block;
- Rbuf = Nrec; // To be used by WriteDB
- } else {
- // The last block must be completed
- CurBlk = Block - 1;
- Rbuf = Nrec - Last; // To be used by WriteDB
- } // endif Last
-
- } // endif Insert
-
- return false;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* ReadBuffer: Read one line from a compressed text file. */
-/***********************************************************************/
-int ZIXFAM::ReadBuffer(PGLOBAL g)
- {
- int n, rc = RC_OK;
-
- /*********************************************************************/
- /* Sequential reading when Placed is not true. */
- /*********************************************************************/
- if (++CurNum < Rbuf) {
- Tdbp->IncLine(Lrecl); // Used by DOSCOL functions
- return RC_OK;
- } else if (Rbuf < Nrec && CurBlk != -1)
- return RC_EF;
-
- /*********************************************************************/
- /* New block. */
- /*********************************************************************/
- CurNum = 0;
- Tdbp->SetLine(To_Buf);
-
- int skip = 0;
-
- next:
- if (++CurBlk >= Block)
- return RC_EF;
-
- /*********************************************************************/
- /* Before using the new block, check whether block optimization */
- /* can be done, as well as for join as for local filtering. */
- /*********************************************************************/
- switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- skip++;
- goto next;
- } // endswitch rc
-
- if (skip)
- // Skip blocks rejected by block optimization
- for (int i = 0; i < skip; i++) {
- if (gzseek(Zfile, (z_off_t)Buflen, SEEK_CUR) < 0)
- return Zerror(g);
-
- } // endfor i
-
- if (!(n = gzread(Zfile, To_Buf, Buflen))) {
- rc = RC_EF;
- } else if (n > 0) {
- Rbuf = n / Lrecl;
- IsRead = true;
- rc = RC_OK;
- num_read++;
- } else
- rc = Zerror(g);
-
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* WriteDB: Data Base write routine for ZDOS access method. */
-/* Update is not possible without using a temporary file (NIY). */
-/***********************************************************************/
-int ZIXFAM::WriteBuffer(PGLOBAL g)
- {
- /*********************************************************************/
- /* In Insert mode, blocs are added sequentialy to the file end. */
- /* Note: Update mode is not handled for zip files. */
- /*********************************************************************/
- if (++CurNum == Rbuf) {
- /*******************************************************************/
- /* New block, start the writing process. */
- /*******************************************************************/
- BlkLen = Rbuf * Lrecl;
-
- if (gzwrite(Zfile, To_Buf, BlkLen) != BlkLen ||
- gzflush(Zfile, Z_FULL_FLUSH)) {
- Closing = true;
- return Zerror(g);
- } // endif gzwrite
-
- Rbuf = Nrec;
- CurBlk++;
- CurNum = 0;
- Tdbp->SetLine(To_Buf);
- } else
- Tdbp->IncLine(Lrecl); // Used by FIXCOL functions
-
- return RC_OK;
- } // end of WriteBuffer
-
-/* --------------------------- Class ZLBFAM -------------------------- */
-
-/***********************************************************************/
-/* Constructors. */
-/***********************************************************************/
-ZLBFAM::ZLBFAM(PDOSDEF tdp) : BLKFAM(tdp)
- {
- Zstream = NULL;
- Zbuffer = NULL;
- Zlenp = NULL;
- Optimized = tdp->IsOptimized();
- } // end of ZLBFAM standard constructor
-
-ZLBFAM::ZLBFAM(PZLBFAM txfp) : BLKFAM(txfp)
- {
- Zstream = txfp->Zstream;
- Zbuffer = txfp->Zbuffer;
- Zlenp = txfp->Zlenp;
- Optimized = txfp->Optimized;
- } // end of ZLBFAM (dummy?) copy constructor
-
-/***********************************************************************/
-/* ZLB GetFileLength: returns an estimate of what would be the */
-/* uncompressed file size in number of bytes. */
-/***********************************************************************/
-int ZLBFAM::GetFileLength(PGLOBAL g)
- {
- int len = (Optimized) ? BlkPos[Block] : BLKFAM::GetFileLength(g);
-
- if (len > 0)
- // Estimate size reduction to a max of 5
- len *= 5;
-
- return len;
- } // end of GetFileLength
-
-/***********************************************************************/
-/* Allocate the line buffer. For mode Delete a bigger buffer has to */
-/* be allocated because is it also used to move lines into the file. */
-/***********************************************************************/
-bool ZLBFAM::AllocateBuffer(PGLOBAL g)
- {
- char *msg;
- int n, zrc;
-
-#if 0
- if (!Optimized && Tdbp->NeedIndexing(g)) {
- strcpy(g->Message, MSG(NOP_ZLIB_INDEX));
- return TRUE;
- } // endif indexing
-#endif // 0
-
-#if defined(NOLIB)
- if (!zlib && LoadZlib()) {
- sprintf(g->Message, MSG(DLL_LOAD_ERROR), GetLastError(), "zlib.dll");
- return TRUE;
- } // endif zlib
-#endif
-
- BLKFAM::AllocateBuffer(g);
-//Buflen = Nrec * (Lrecl + 2);
-//Rbuf = Nrec;
-
- // Allocate the compressed buffer
- n = Buflen + 16; // ?????????????????????????????????
- Zlenp = (int*)PlugSubAlloc(g, NULL, n);
- Zbuffer = (Byte*)(Zlenp + 1);
-
- // Allocate and initialize the Z stream
- Zstream = (z_streamp)PlugSubAlloc(g, NULL, sizeof(z_stream));
- Zstream->zalloc = (alloc_func)0;
- Zstream->zfree = (free_func)0;
- Zstream->opaque = (voidpf)0;
- Zstream->next_in = NULL;
- Zstream->avail_in = 0;
-
- if (Tdbp->GetMode() == MODE_READ) {
- msg = "inflateInit";
- zrc = inflateInit(Zstream);
- } else {
- msg = "deflateInit";
- zrc = deflateInit(Zstream, Z_DEFAULT_COMPRESSION);
- } // endif Mode
-
- if (zrc != Z_OK) {
- if (Zstream->msg)
- sprintf(g->Message, "%s error: %s", msg, Zstream->msg);
- else
- sprintf(g->Message, "%s error: %d", msg, zrc);
-
- return TRUE;
- } // endif zrc
-
- if (Tdbp->GetMode() == MODE_INSERT) {
- // Write the file header block
- if (Last == Nrec) {
- CurBlk = Block;
- CurNum = 0;
-
- if (!GetFileLength(g)) {
- // Write the zlib header as an extra block
- strcpy(To_Buf, "PlugDB");
- BlkLen = strlen("PlugDB") + 1;
-
- if (WriteCompressedBuffer(g))
- return TRUE;
-
- } // endif void file
-
- } else {
- // In mode insert, if Last != Nrec, last block must be updated
- CurBlk = Block - 1;
- CurNum = Last;
-
- strcpy(g->Message, MSG(NO_PAR_BLK_INS));
- return TRUE;
- } // endif Last
-
- } else { // MODE_READ
- // First thing to do is to read the header block
- void *rdbuf;
-
- if (Optimized) {
- BlkLen = BlkPos[0];
- rdbuf = Zlenp;
- } else {
- // Get the stored length from the file itself
- if (fread(Zlenp, sizeof(int), 1, Stream) != 1)
- return FALSE; // Empty file
-
- BlkLen = *Zlenp;
- rdbuf = Zbuffer;
- } // endif Optimized
-
- switch (ReadCompressedBuffer(g, rdbuf)) {
- case RC_EF:
- return FALSE;
- case RC_FX:
-#if defined(UNIX)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
-#else
- sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
-#endif
- case RC_NF:
- return TRUE;
- } // endswitch
-
- // Some old tables can have PlugDB in their header
- if (strcmp(To_Buf, "PlugDB")) {
- sprintf(g->Message, MSG(BAD_HEADER), Tdbp->GetFile(g));
- return TRUE;
- } // endif strcmp
-
- } // endif Mode
-
- return FALSE;
- } // end of AllocateBuffer
-
-/***********************************************************************/
-/* GetPos: return the position of last read record. */
-/***********************************************************************/
-int ZLBFAM::GetPos(void)
- {
- return (Optimized) ? (CurNum + Nrec * CurBlk) : Fpos;
- } // end of GetPos
-
-/***********************************************************************/
-/* GetNextPos: should not be called for this class. */
-/***********************************************************************/
-int ZLBFAM::GetNextPos(void)
- {
- if (Optimized) {
- assert(FALSE);
- return 0;
- } else
- return ftell(Stream);
-
- } // end of GetNextPos
-
-/***********************************************************************/
-/* ReadBuffer: Read one line for a text file. */
-/***********************************************************************/
-int ZLBFAM::ReadBuffer(PGLOBAL g)
- {
- int n;
- void *rdbuf;
-
- /*********************************************************************/
- /* Sequential reading when Placed is not true. */
- /*********************************************************************/
- if (Placed) {
- Placed = FALSE;
- } else if (++CurNum < Rbuf) {
- CurLine = NxtLine;
-
- // Get the position of the next line in the buffer
- if (Tdbp->GetFtype() == RECFM_VAR)
- while (*NxtLine++ != '\n') ;
- else
- NxtLine += Lrecl;
-
- // Set caller line buffer
- n = NxtLine - CurLine - ((Tdbp->GetFtype() == RECFM_BIN) ? 0 : Ending);
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
- return RC_OK;
- } else if (Rbuf < Nrec && CurBlk != -1) {
- CurNum--; // To have a correct Last value when optimizing
- return RC_EF;
- } else {
- /*******************************************************************/
- /* New block. */
- /*******************************************************************/
- CurNum = 0;
-
- next:
- if (++CurBlk >= Block)
- return RC_EF;
-
- /*******************************************************************/
- /* Before reading a new block, check whether block optimization */
- /* can be done, as well as for join as for local filtering. */
- /*******************************************************************/
- if (Optimized) switch (Tdbp->TestBlock(g)) {
- case RC_EF:
- return RC_EF;
- case RC_NF:
- goto next;
- } // endswitch rc
-
- } // endif's
-
- if (OldBlk == CurBlk)
- goto ok; // Block is already there
-
- if (Optimized) {
- // Store the position of next block
- Fpos = BlkPos[CurBlk];
-
- // fseek is required only in non sequential reading
- if (CurBlk != OldBlk + 1)
- if (fseek(Stream, Fpos, SEEK_SET)) {
- sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
- return RC_FX;
- } // endif fseek
-
- // Calculate the length of block to read
- BlkLen = BlkPos[CurBlk + 1] - Fpos;
- rdbuf = Zlenp;
- } else { // !Optimized
- if (CurBlk != OldBlk + 1) {
- strcpy(g->Message, MSG(INV_RAND_ACC));
- return RC_FX;
- } else
- Fpos = ftell(Stream); // Used when optimizing
-
- // Get the stored length from the file itself
- if (fread(Zlenp, sizeof(int), 1, Stream) != 1) {
- if (feof(Stream))
- return RC_EF;
-
- goto err;
- } // endif fread
-
- BlkLen = *Zlenp;
- rdbuf = Zbuffer;
- } // endif Optimized
-
- // Read the next block
- switch (ReadCompressedBuffer(g, rdbuf)) {
- case RC_FX: goto err;
- case RC_NF: return RC_FX;
- case RC_EF: return RC_EF;
- default: Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
- } // endswitch ReadCompressedBuffer
-
- ok:
- if (Tdbp->GetFtype() == RECFM_VAR) {
- int i;
-
- // Get the position of the current line
- for (i = 0, CurLine = To_Buf; i < CurNum; i++)
- while (*CurLine++ != '\n') ; // What about Unix ???
-
- // Now get the position of the next line
- for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
-
- // Set caller line buffer
- n = NxtLine - CurLine - Ending;
- } else {
- CurLine = To_Buf + CurNum * Lrecl;
- NxtLine = CurLine + Lrecl;
- n = Lrecl - ((Tdbp->GetFtype() == RECFM_BIN) ? 0 : Ending);
- } // endif Ftype
-
- memcpy(Tdbp->GetLine(), CurLine, n);
- Tdbp->GetLine()[n] = '\0';
-
- OldBlk = CurBlk; // Last block actually read
- IsRead = TRUE; // Is read indeed
- return RC_OK;
-
- err:
-#if defined(UNIX)
- sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
-#else
- sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
-#endif
- return RC_FX;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* Read and decompress a block from the stream. */
-/***********************************************************************/
-int ZLBFAM::ReadCompressedBuffer(PGLOBAL g, void *rdbuf)
- {
- if (fread(rdbuf, 1, (size_t)BlkLen, Stream) == (unsigned)BlkLen) {
- int zrc;
-
- num_read++;
-
- if (Optimized && BlkLen != signed(*Zlenp + sizeof(int))) {
- sprintf(g->Message, MSG(BAD_BLK_SIZE), CurBlk + 1);
- return RC_NF;
- } // endif BlkLen
-
- // HERE WE MUST INFLATE THE BLOCK
- Zstream->next_in = Zbuffer;
- Zstream->avail_in = (uInt)(*Zlenp);
- Zstream->next_out = (Byte*)To_Buf;
- Zstream->avail_out = Buflen;
- zrc = inflate(Zstream, Z_SYNC_FLUSH);
-
- if (zrc != Z_OK) {
- if (Zstream->msg)
- sprintf(g->Message, MSG(FUNC_ERR_S), "inflate", Zstream->msg);
- else
- sprintf(g->Message, MSG(FUNCTION_ERROR), "inflate", (int)zrc);
-
- return RC_NF;
- } // endif zrc
-
- } else if (feof(Stream)) {
- return RC_EF;
- } else
- return RC_FX;
-
- return RC_OK;
- } // end of ReadCompressedBuffer
-
-/***********************************************************************/
-/* WriteBuffer: File write routine for DOS access method. */
-/* Update is directly written back into the file, */
-/* with this (fast) method, record size cannot change. */
-/***********************************************************************/
-int ZLBFAM::WriteBuffer(PGLOBAL g)
- {
- assert (Tdbp->GetMode() == MODE_INSERT);
-
- /*********************************************************************/
- /* Prepare the write buffer. */
- /*********************************************************************/
- if (!Closing) {
- if (Tdbp->GetFtype() == RECFM_BIN)
- memcpy(CurLine, Tdbp->GetLine(), Lrecl);
- else
- strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
-
-#if defined(_DEBUG)
- if (Tdbp->GetFtype() == RECFM_FIX &&
- (signed)strlen(CurLine) != Lrecl + (signed)strlen(CrLf)) {
- strcpy(g->Message, MSG(BAD_LINE_LEN));
- Closing = TRUE;
- return RC_FX;
- } // endif Lrecl
-#endif // _DEBUG
- } // endif Closing
-
- /*********************************************************************/
- /* In Insert mode, blocs are added sequentialy to the file end. */
- /*********************************************************************/
- if (++CurNum != Rbuf) {
- if (Tdbp->GetFtype() == RECFM_VAR)
- CurLine += strlen(CurLine);
- else
- CurLine += Lrecl;
-
- return RC_OK; // We write only full blocks
- } // endif CurNum
-
- // HERE WE MUST DEFLATE THE BLOCK
- if (Tdbp->GetFtype() == RECFM_VAR)
- NxtLine = CurLine + strlen(CurLine);
- else
- NxtLine = CurLine + Lrecl;
-
- BlkLen = NxtLine - To_Buf;
-
- if (WriteCompressedBuffer(g)) {
- Closing = TRUE; // To tell CloseDB about a Write error
- return RC_FX;
- } // endif WriteCompressedBuffer
-
- CurBlk++;
- CurNum = 0;
- CurLine = To_Buf;
- return RC_OK;
- } // end of WriteBuffer
-
-/***********************************************************************/
-/* Compress the buffer and write the deflated output to stream. */
-/***********************************************************************/
-bool ZLBFAM::WriteCompressedBuffer(PGLOBAL g)
- {
- int zrc;
-
- Zstream->next_in = (Byte*)To_Buf;
- Zstream->avail_in = (uInt)BlkLen;
- Zstream->next_out = Zbuffer;
- Zstream->avail_out = Buflen + 16;
- Zstream->total_out = 0;
- zrc = deflate(Zstream, Z_FULL_FLUSH);
-
- if (zrc != Z_OK) {
- if (Zstream->msg)
- sprintf(g->Message, MSG(FUNC_ERR_S), "deflate", Zstream->msg);
- else
- sprintf(g->Message, MSG(FUNCTION_ERROR), "deflate", (int)zrc);
-
- return TRUE;
- } else
- *Zlenp = Zstream->total_out;
-
- // Now start the writing process.
- BlkLen = *Zlenp + sizeof(int);
-
- if (fwrite(Zlenp, 1, BlkLen, Stream) != (size_t)BlkLen) {
- sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
- return TRUE;
- } // endif size
-
- return FALSE;
- } // end of WriteCompressedBuffer
-
-/***********************************************************************/
-/* Table file close routine for DOS access method. */
-/***********************************************************************/
-void ZLBFAM::CloseTableFile(PGLOBAL g)
- {
- int rc = RC_OK;
-
- if (Tdbp->GetMode() == MODE_INSERT) {
- PCATLG cat = PlgGetCatalog(g);
- LPCSTR name = Tdbp->GetName();
- PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
-
- // Closing is True if last Write was in error
- if (CurNum && !Closing) {
- // Some more inserted lines remain to be written
- Last = (Nrec - Rbuf) + CurNum;
- Block = CurBlk + 1;
- Rbuf = CurNum--;
- Closing = TRUE;
- rc = WriteBuffer(g);
- } else if (Rbuf == Nrec) {
- Last = Nrec;
- Block = CurBlk;
- } // endif CurNum
-
- if (rc != RC_FX) {
- defp->SetBlock(Block);
- defp->SetLast(Last);
- cat->SetIntCatInfo("Blocks", Block);
- cat->SetIntCatInfo("Last", Last);
- } // endif
-
- fclose(Stream);
- } else
- rc = fclose(Stream);
-
-#ifdef DEBTRACE
- htrc("ZLB CloseTableFile: closing %s mode=%d rc=%d\n",
- To_File, Tdbp->GetMode(), rc);
-#endif
-
- Stream = NULL; // So we can know whether table is open
- To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
-
- if (Tdbp->GetMode() == MODE_READ)
- rc = inflateEnd(Zstream);
- else
- rc = deflateEnd(Zstream);
-
- } // end of CloseTableFile
-
-/***********************************************************************/
-/* Rewind routine for ZLIB access method. */
-/***********************************************************************/
-void ZLBFAM::Rewind(void)
- {
- // We must be positioned after the header block
- if (CurBlk >= 0) { // Nothing to do if no block read yet
- if (!Optimized) { // If optimized, fseek will be done in ReadBuffer
- rewind(Stream);
- fread(Zlenp, sizeof(int), 1, Stream);
- fseek(Stream, *Zlenp + sizeof(int), SEEK_SET);
- OldBlk = -1;
- } // endif Optimized
-
- CurBlk = -1;
- CurNum = Rbuf;
- } // endif CurBlk
-
-//OldBlk = -1;
-//Rbuf = 0; commented out in case we reuse last read block
- } // end of Rewind
-
-/* ------------------------ End of ZipFam ---------------------------- */
+/*********** File AM Zip C++ Program Source Code File (.CPP) ***********/
+/* PROGRAM NAME: FILAMZIP */
+/* ------------- */
+/* Version 1.5 */
+/* */
+/* COPYRIGHT: */
+/* ---------- */
+/* (C) Copyright to the author Olivier BERTRAND 2005-2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are the ZLIB compressed files classes. */
+/* */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant MariaDB header file. */
+/***********************************************************************/
+#include "my_global.h"
+#if defined(WIN32)
+#include <io.h>
+#include <fcntl.h>
+#if defined(__BORLANDC__)
+#define __MFC_COMPAT__ // To define min/max as macro
+#endif
+//#include <windows.h>
+#else // !WIN32
+#if defined(UNIX)
+#include <errno.h>
+#else // !UNIX
+#include <io.h>
+#endif
+#include <fcntl.h>
+#endif // !WIN32
+
+/***********************************************************************/
+/* Include application header files: */
+/* global.h is header containing all global declarations. */
+/* plgdbsem.h is header containing the DB application declarations. */
+/* tabdos.h is header containing the TABDOS class declarations. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+//#include "catalog.h"
+//#include "reldef.h"
+//#include "xobject.h"
+//#include "kindex.h"
+#include "filamtxt.h"
+#include "tabdos.h"
+#if defined(UNIX)
+#include "osutil.h"
+#endif
+
+/***********************************************************************/
+/* This define prepares ZLIB function declarations. */
+/***********************************************************************/
+//#define ZLIB_DLL
+
+#include "filamzip.h"
+
+/***********************************************************************/
+/* DB static variables. */
+/***********************************************************************/
+extern int num_read, num_there, num_eq[]; // Statistics
+extern "C" int trace;
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Implementation of the ZIPFAM class. */
+/***********************************************************************/
+ZIPFAM::ZIPFAM(PZIPFAM txfp) : TXTFAM(txfp)
+ {
+ Zfile = txfp->Zfile;
+ Zpos = txfp->Zpos;
+ } // end of ZIPFAM copy constructor
+
+/***********************************************************************/
+/* Zerror: Error function for gz calls. */
+/* gzerror returns the error message for the last error which occurred*/
+/* on the given compressed file. errnum is set to zlib error number. */
+/* If an error occurred in the file system and not in the compression */
+/* library, errnum is set to Z_ERRNO and the application may consult */
+/* errno to get the exact error code. */
+/***********************************************************************/
+int ZIPFAM::Zerror(PGLOBAL g)
+ {
+ int errnum;
+
+ strcpy(g->Message, gzerror(Zfile, &errnum));
+
+ if (errnum == Z_ERRNO)
+#if defined(WIN32)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(NULL));
+#else // !WIN32
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
+#endif // !WIN32
+
+ return (errnum == Z_STREAM_END) ? RC_EF : RC_FX;
+ } // end of Zerror
+
+/***********************************************************************/
+/* Reset: reset position values at the beginning of file. */
+/***********************************************************************/
+void ZIPFAM::Reset(void)
+ {
+ TXTFAM::Reset();
+//gzrewind(Zfile); // Useful ?????
+ Zpos = 0;
+ } // end of Reset
+
+/***********************************************************************/
+/* ZIP GetFileLength: returns an estimate of what would be the */
+/* uncompressed file size in number of bytes. */
+/***********************************************************************/
+int ZIPFAM::GetFileLength(PGLOBAL g)
+ {
+ int len = TXTFAM::GetFileLength(g);
+
+ if (len > 0)
+ // Estimate size reduction to a max of 6
+ len *= 6;
+
+ return len;
+ } // end of GetFileLength
+
+/***********************************************************************/
+/* ZIP Access Method opening routine. */
+/***********************************************************************/
+bool ZIPFAM::OpenTableFile(PGLOBAL g)
+ {
+ char opmode[4], filename[_MAX_PATH];
+ MODE mode = Tdbp->GetMode();
+
+ switch (mode) {
+ case MODE_READ:
+ strcpy(opmode, "r");
+ break;
+ case MODE_UPDATE:
+ /*****************************************************************/
+ /* Updating ZIP files not implemented yet. */
+ /*****************************************************************/
+ strcpy(g->Message, MSG(UPD_ZIP_NOT_IMP));
+ return true;
+ case MODE_DELETE:
+ if (!Tdbp->GetNext()) {
+ // Store the number of deleted lines
+ DelRows = Cardinality(g);
+
+ // This will erase the entire file
+ strcpy(opmode, "w");
+// Block = 0; // For ZBKFAM
+// Last = Nrec; // For ZBKFAM
+ Tdbp->ResetSize();
+ } else {
+ sprintf(g->Message, MSG(NO_PART_DEL), "ZIP");
+ return true;
+ } // endif filter
+
+ break;
+ case MODE_INSERT:
+ strcpy(opmode, "a+");
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
+ return true;
+ } // endswitch Mode
+
+ /*********************************************************************/
+ /* Open according to logical input/output mode required. */
+ /* Use specific zlib functions. */
+ /* Treat files as binary. */
+ /*********************************************************************/
+ strcat(opmode, "b");
+ Zfile = gzopen(PlugSetPath(filename, To_File, Tdbp->GetPath()), opmode);
+
+ if (Zfile == NULL) {
+ sprintf(g->Message, MSG(GZOPEN_ERROR),
+ opmode, (int)errno, filename);
+ strcat(strcat(g->Message, ": "), strerror(errno));
+ return (mode == MODE_READ && errno == ENOENT)
+ ? PushWarning(g, Tdbp) : true;
+ } // endif Zfile
+
+ /*********************************************************************/
+ /* Something to be done here. >>>>>>>> NOT DONE <<<<<<<< */
+ /*********************************************************************/
+//To_Fb = dbuserp->Openlist; // Keep track of File block
+
+ /*********************************************************************/
+ /* Allocate the line buffer. */
+ /*********************************************************************/
+ return AllocateBuffer(g);
+ } // end of OpenTableFile
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete a bigger buffer has to */
+/* be allocated because is it also used to move lines into the file. */
+/***********************************************************************/
+bool ZIPFAM::AllocateBuffer(PGLOBAL g)
+ {
+ MODE mode = Tdbp->GetMode();
+
+ Buflen = Lrecl + 2; // Lrecl does not include CRLF
+//Buflen *= ((Mode == MODE_DELETE) ? DOS_BUFF_LEN : 1); NIY
+
+ if (trace)
+ htrc("SubAllocating a buffer of %d bytes\n", Buflen);
+
+ To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
+
+ if (mode == MODE_INSERT) {
+ /*******************************************************************/
+ /* For Insert buffer must be prepared. */
+ /*******************************************************************/
+ memset(To_Buf, ' ', Buflen);
+ To_Buf[Buflen - 2] = '\n';
+ To_Buf[Buflen - 1] = '\0';
+ } // endif Insert
+
+ return false;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* GetRowID: return the RowID of last read record. */
+/***********************************************************************/
+int ZIPFAM::GetRowID(void)
+ {
+ return Rows;
+ } // end of GetRowID
+
+/***********************************************************************/
+/* GetPos: return the position of last read record. */
+/***********************************************************************/
+int ZIPFAM::GetPos(void)
+ {
+ return (int)Zpos;
+ } // end of GetPos
+
+/***********************************************************************/
+/* GetNextPos: return the position of next record. */
+/***********************************************************************/
+int ZIPFAM::GetNextPos(void)
+ {
+ return gztell(Zfile);
+ } // end of GetNextPos
+
+/***********************************************************************/
+/* SetPos: Replace the table at the specified position. */
+/***********************************************************************/
+bool ZIPFAM::SetPos(PGLOBAL g, int pos)
+ {
+ sprintf(g->Message, MSG(NO_SETPOS_YET), "ZIP");
+ return true;
+#if 0
+ Fpos = pos;
+
+ if (fseek(Stream, Fpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
+ return true;
+ } // endif
+
+ Placed = true;
+ return false;
+#endif // 0
+ } // end of SetPos
+
+/***********************************************************************/
+/* Record file position in case of UPDATE or DELETE. */
+/***********************************************************************/
+bool ZIPFAM::RecordPos(PGLOBAL g)
+ {
+ Zpos = gztell(Zfile);
+ return false;
+ } // end of RecordPos
+
+/***********************************************************************/
+/* Skip one record in file. */
+/***********************************************************************/
+int ZIPFAM::SkipRecord(PGLOBAL g, bool header)
+ {
+ // Skip this record
+ if (gzeof(Zfile))
+ return RC_EF;
+ else if (gzgets(Zfile, To_Buf, Buflen) == Z_NULL)
+ return Zerror(g);
+
+ if (header)
+ RecordPos(g);
+
+ return RC_OK;
+ } // end of SkipRecord
+
+/***********************************************************************/
+/* ReadBuffer: Read one line from a compressed text file. */
+/***********************************************************************/
+int ZIPFAM::ReadBuffer(PGLOBAL g)
+ {
+ char *p;
+ int rc;
+
+ if (!Zfile)
+ return RC_EF;
+
+ if (!Placed) {
+ /*******************************************************************/
+ /* Record file position in case of UPDATE or DELETE. */
+ /*******************************************************************/
+ next:
+ if (RecordPos(g))
+ return RC_FX;
+
+ CurBlk = Rows++; // Update RowID
+
+ /*******************************************************************/
+ /* Check whether optimization on ROWID */
+ /* can be done, as well as for join as for local filtering. */
+ /*******************************************************************/
+ switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ // Skip this record
+ if ((rc = SkipRecord(g, FALSE)) != RC_OK)
+ return rc;
+
+ goto next;
+ } // endswitch rc
+
+ } else
+ Placed = false;
+
+ if (gzeof(Zfile)) {
+ rc = RC_EF;
+ } else if (gzgets(Zfile, To_Buf, Buflen) != Z_NULL) {
+ p = To_Buf + strlen(To_Buf) - 1;
+
+ if (*p == '\n')
+ *p = '\0'; // Eliminate ending new-line character
+
+ if (*(--p) == '\r')
+ *p = '\0'; // Eliminate eventuel carriage return
+
+ strcpy(Tdbp->GetLine(), To_Buf);
+ IsRead = true;
+ rc = RC_OK;
+ num_read++;
+ } else
+ rc = Zerror(g);
+
+ if (trace > 1)
+ htrc(" Read: '%s' rc=%d\n", To_Buf, rc);
+
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* WriteDB: Data Base write routine for ZDOS access method. */
+/* Update is not possible without using a temporary file (NIY). */
+/***********************************************************************/
+int ZIPFAM::WriteBuffer(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Prepare the write buffer. */
+ /*********************************************************************/
+ strcat(strcpy(To_Buf, Tdbp->GetLine()), CrLf);
+
+ /*********************************************************************/
+ /* Now start the writing process. */
+ /*********************************************************************/
+ if (gzputs(Zfile, To_Buf) < 0)
+ return Zerror(g);
+
+ return RC_OK;
+ } // end of WriteBuffer
+
+/***********************************************************************/
+/* Data Base delete line routine for ZDOS access method. (NIY) */
+/***********************************************************************/
+int ZIPFAM::DeleteRecords(PGLOBAL g, int irc)
+ {
+ strcpy(g->Message, MSG(NO_ZIP_DELETE));
+ return RC_FX;
+ } // end of DeleteRecords
+
+/***********************************************************************/
+/* Data Base close routine for DOS access method. */
+/***********************************************************************/
+void ZIPFAM::CloseTableFile(PGLOBAL g)
+ {
+ int rc = gzclose(Zfile);
+
+ if (trace)
+ htrc("ZIP CloseDB: closing %s rc=%d\n", To_File, rc);
+
+ Zfile = NULL; // So we can know whether table is open
+//To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
+ } // end of CloseTableFile
+
+/***********************************************************************/
+/* Rewind routine for ZIP access method. */
+/***********************************************************************/
+void ZIPFAM::Rewind(void)
+ {
+ gzrewind(Zfile);
+ } // end of Rewind
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+ZBKFAM::ZBKFAM(PDOSDEF tdp) : ZIPFAM(tdp)
+ {
+ Blocked = true;
+ Block = tdp->GetBlock();
+ Last = tdp->GetLast();
+ Nrec = tdp->GetElemt();
+ CurLine = NULL;
+ NxtLine = NULL;
+ Closing = false;
+ BlkPos = tdp->GetTo_Pos();
+ } // end of ZBKFAM standard constructor
+
+ZBKFAM::ZBKFAM(PZBKFAM txfp) : ZIPFAM(txfp)
+ {
+ CurLine = txfp->CurLine;
+ NxtLine = txfp->NxtLine;
+ Closing = txfp->Closing;
+ } // end of ZBKFAM copy constructor
+
+/***********************************************************************/
+/* Use BlockTest to reduce the table estimated size. */
+/***********************************************************************/
+int ZBKFAM::MaxBlkSize(PGLOBAL g, int s)
+ {
+ int rc = RC_OK, savcur = CurBlk;
+ int size;
+
+ // Roughly estimate the table size as the sum of blocks
+ // that can contain good rows
+ for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
+ if ((rc = Tdbp->TestBlock(g)) == RC_OK)
+ size += (CurBlk == Block - 1) ? Last : Nrec;
+ else if (rc == RC_EF)
+ break;
+
+ CurBlk = savcur;
+ return size;
+ } // end of MaxBlkSize
+
+/***********************************************************************/
+/* ZBK Cardinality: returns table cardinality in number of rows. */
+/* This function can be called with a null argument to test the */
+/* availability of Cardinality implementation (1 yes, 0 no). */
+/***********************************************************************/
+int ZBKFAM::Cardinality(PGLOBAL g)
+ {
+ // Should not be called in this version
+ return (g) ? -1 : 0;
+//return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
+ } // end of Cardinality
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete a bigger buffer has to */
+/* be allocated because is it also used to move lines into the file. */
+/***********************************************************************/
+bool ZBKFAM::AllocateBuffer(PGLOBAL g)
+ {
+ Buflen = Nrec * (Lrecl + 2);
+ CurLine = To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
+
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ // Set values so Block and Last can be recalculated
+ if (Last == Nrec) {
+ CurBlk = Block;
+ Rbuf = Nrec; // To be used by WriteDB
+ } else {
+ // The last block must be completed
+ CurBlk = Block - 1;
+ Rbuf = Nrec - Last; // To be used by WriteDB
+ } // endif Last
+
+ } // endif Insert
+
+ return false;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* GetRowID: return the RowID of last read record. */
+/***********************************************************************/
+int ZBKFAM::GetRowID(void)
+ {
+ return CurNum + Nrec * CurBlk + 1;
+ } // end of GetRowID
+
+/***********************************************************************/
+/* GetPos: return the position of last read record. */
+/***********************************************************************/
+int ZBKFAM::GetPos(void)
+ {
+ return CurNum + Nrec * CurBlk; // Computed file index
+ } // end of GetPos
+
+/***********************************************************************/
+/* Record file position in case of UPDATE or DELETE. */
+/* Not used yet for fixed tables. */
+/***********************************************************************/
+bool ZBKFAM::RecordPos(PGLOBAL g)
+ {
+//strcpy(g->Message, "RecordPos not implemented for zip blocked tables");
+//return true;
+ return RC_OK;
+ } // end of RecordPos
+
+/***********************************************************************/
+/* Skip one record in file. */
+/***********************************************************************/
+int ZBKFAM::SkipRecord(PGLOBAL g, bool header)
+ {
+//strcpy(g->Message, "SkipRecord not implemented for zip blocked tables");
+//return RC_FX;
+ return RC_OK;
+ } // end of SkipRecord
+
+/***********************************************************************/
+/* ReadBuffer: Read one line from a compressed text file. */
+/***********************************************************************/
+int ZBKFAM::ReadBuffer(PGLOBAL g)
+ {
+ int n, skip, rc = RC_OK;
+
+ /*********************************************************************/
+ /* Sequential reading when Placed is not true. */
+ /*********************************************************************/
+ if (++CurNum < Rbuf) {
+ CurLine = NxtLine;
+
+ // Get the position of the next line in the buffer
+ while (*NxtLine++ != '\n') ;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - Ending;
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+ return RC_OK;
+ } else if (Rbuf < Nrec && CurBlk != -1)
+ return RC_EF;
+
+ /*********************************************************************/
+ /* New block. */
+ /*********************************************************************/
+ CurNum = 0;
+ skip = 0;
+
+ next:
+ if (++CurBlk >= Block)
+ return RC_EF;
+
+ /*********************************************************************/
+ /* Before using the new block, check whether block optimization */
+ /* can be done, as well as for join as for local filtering. */
+ /*********************************************************************/
+ switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ skip++;
+ goto next;
+ } // endswitch rc
+
+ if (skip)
+ // Skip blocks rejected by block optimization
+ for (int i = CurBlk - skip; i < CurBlk; i++) {
+ BlkLen = BlkPos[i + 1] - BlkPos[i];
+
+ if (gzseek(Zfile, (z_off_t)BlkLen, SEEK_CUR) < 0)
+ return Zerror(g);
+
+ } // endfor i
+
+ BlkLen = BlkPos[CurBlk + 1] - BlkPos[CurBlk];
+
+ if (!(n = gzread(Zfile, To_Buf, BlkLen))) {
+ rc = RC_EF;
+ } else if (n > 0) {
+ // Get the position of the current line
+ CurLine = To_Buf;
+
+ // Now get the position of the next line
+ for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - Ending;
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+ Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
+ IsRead = true;
+ rc = RC_OK;
+ num_read++;
+ } else
+ rc = Zerror(g);
+
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* WriteDB: Data Base write routine for ZDOS access method. */
+/* Update is not possible without using a temporary file (NIY). */
+/***********************************************************************/
+int ZBKFAM::WriteBuffer(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Prepare the write buffer. */
+ /*********************************************************************/
+ if (!Closing)
+ strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
+
+ /*********************************************************************/
+ /* In Insert mode, blocs are added sequentialy to the file end. */
+ /* Note: Update mode is not handled for zip files. */
+ /*********************************************************************/
+ if (++CurNum == Rbuf) {
+ /*******************************************************************/
+ /* New block, start the writing process. */
+ /*******************************************************************/
+ BlkLen = CurLine + strlen(CurLine) - To_Buf;
+
+ if (gzwrite(Zfile, To_Buf, BlkLen) != BlkLen ||
+ gzflush(Zfile, Z_FULL_FLUSH)) {
+ Closing = true;
+ return Zerror(g);
+ } // endif gzwrite
+
+ Rbuf = Nrec;
+ CurBlk++;
+ CurNum = 0;
+ CurLine = To_Buf;
+ } else
+ CurLine += strlen(CurLine);
+
+ return RC_OK;
+ } // end of WriteBuffer
+
+/***********************************************************************/
+/* Data Base delete line routine for ZBK access method. */
+/* Implemented only for total deletion of the table, which is done */
+/* by opening the file in mode "wb". */
+/***********************************************************************/
+int ZBKFAM::DeleteRecords(PGLOBAL g, int irc)
+ {
+ if (irc == RC_EF) {
+ LPCSTR name = Tdbp->GetName();
+ PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
+ PCATLG cat = PlgGetCatalog(g);
+
+ defp->SetBlock(0);
+ defp->SetLast(Nrec);
+
+ if (!cat->SetIntCatInfo("Blocks", 0) ||
+ !cat->SetIntCatInfo("Last", 0)) {
+ sprintf(g->Message, MSG(UPDATE_ERROR), "Header");
+ return RC_FX;
+ } else
+ return RC_OK;
+
+ } else
+ return irc;
+
+ } // end of DeleteRecords
+
+/***********************************************************************/
+/* Data Base close routine for ZBK access method. */
+/***********************************************************************/
+void ZBKFAM::CloseTableFile(PGLOBAL g)
+ {
+ int rc = RC_OK;
+
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ PCATLG cat = PlgGetCatalog(g);
+ LPCSTR name = Tdbp->GetName();
+ PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
+
+ if (CurNum && !Closing) {
+ // Some more inserted lines remain to be written
+ Last = (Nrec - Rbuf) + CurNum;
+ Block = CurBlk + 1;
+ Rbuf = CurNum--;
+ Closing = true;
+ rc = WriteBuffer(g);
+ } else if (Rbuf == Nrec) {
+ Last = Nrec;
+ Block = CurBlk;
+ } // endif CurNum
+
+ if (rc != RC_FX) {
+ defp->SetBlock(Block);
+ defp->SetLast(Last);
+ cat->SetIntCatInfo("Blocks", Block);
+ cat->SetIntCatInfo("Last", Last);
+ } // endif
+
+ gzclose(Zfile);
+ } else if (Tdbp->GetMode() == MODE_DELETE) {
+ rc = DeleteRecords(g, RC_EF);
+ gzclose(Zfile);
+ } else
+ rc = gzclose(Zfile);
+
+ if (trace)
+ htrc("ZIP CloseDB: closing %s rc=%d\n", To_File, rc);
+
+ Zfile = NULL; // So we can know whether table is open
+//To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
+ } // end of CloseTableFile
+
+/***********************************************************************/
+/* Rewind routine for ZBK access method. */
+/***********************************************************************/
+void ZBKFAM::Rewind(void)
+ {
+ gzrewind(Zfile);
+ CurBlk = -1;
+ CurNum = Rbuf;
+ } // end of Rewind
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+ZIXFAM::ZIXFAM(PDOSDEF tdp) : ZBKFAM(tdp)
+ {
+//Block = tdp->GetBlock();
+//Last = tdp->GetLast();
+ Nrec = (tdp->GetElemt()) ? tdp->GetElemt() : DOS_BUFF_LEN;
+ Blksize = Nrec * Lrecl;
+ } // end of ZIXFAM standard constructor
+
+/***********************************************************************/
+/* ZIX Cardinality: returns table cardinality in number of rows. */
+/* This function can be called with a null argument to test the */
+/* availability of Cardinality implementation (1 yes, 0 no). */
+/***********************************************************************/
+int ZIXFAM::Cardinality(PGLOBAL g)
+ {
+ if (Last)
+ return (g) ? (int)((Block - 1) * Nrec + Last) : 1;
+ else // Last and Block not defined, cannot do it yet
+ return 0;
+
+ } // end of Cardinality
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete a bigger buffer has to */
+/* be allocated because is it also used to move lines into the file. */
+/***********************************************************************/
+bool ZIXFAM::AllocateBuffer(PGLOBAL g)
+ {
+ Buflen = Blksize;
+ To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
+
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ /*******************************************************************/
+ /* For Insert the buffer must be prepared. */
+ /*******************************************************************/
+ memset(To_Buf, ' ', Buflen);
+
+ if (Tdbp->GetFtype() < 2)
+ // if not binary, the file is physically a text file
+ for (int len = Lrecl; len <= Buflen; len += Lrecl) {
+#if defined(WIN32)
+ To_Buf[len - 2] = '\r';
+#endif // WIN32
+ To_Buf[len - 1] = '\n';
+ } // endfor len
+
+ // Set values so Block and Last can be recalculated
+ if (Last == Nrec) {
+ CurBlk = Block;
+ Rbuf = Nrec; // To be used by WriteDB
+ } else {
+ // The last block must be completed
+ CurBlk = Block - 1;
+ Rbuf = Nrec - Last; // To be used by WriteDB
+ } // endif Last
+
+ } // endif Insert
+
+ return false;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* ReadBuffer: Read one line from a compressed text file. */
+/***********************************************************************/
+int ZIXFAM::ReadBuffer(PGLOBAL g)
+ {
+ int n, rc = RC_OK;
+
+ /*********************************************************************/
+ /* Sequential reading when Placed is not true. */
+ /*********************************************************************/
+ if (++CurNum < Rbuf) {
+ Tdbp->IncLine(Lrecl); // Used by DOSCOL functions
+ return RC_OK;
+ } else if (Rbuf < Nrec && CurBlk != -1)
+ return RC_EF;
+
+ /*********************************************************************/
+ /* New block. */
+ /*********************************************************************/
+ CurNum = 0;
+ Tdbp->SetLine(To_Buf);
+
+ int skip = 0;
+
+ next:
+ if (++CurBlk >= Block)
+ return RC_EF;
+
+ /*********************************************************************/
+ /* Before using the new block, check whether block optimization */
+ /* can be done, as well as for join as for local filtering. */
+ /*********************************************************************/
+ switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ skip++;
+ goto next;
+ } // endswitch rc
+
+ if (skip)
+ // Skip blocks rejected by block optimization
+ for (int i = 0; i < skip; i++) {
+ if (gzseek(Zfile, (z_off_t)Buflen, SEEK_CUR) < 0)
+ return Zerror(g);
+
+ } // endfor i
+
+ if (!(n = gzread(Zfile, To_Buf, Buflen))) {
+ rc = RC_EF;
+ } else if (n > 0) {
+ Rbuf = n / Lrecl;
+ IsRead = true;
+ rc = RC_OK;
+ num_read++;
+ } else
+ rc = Zerror(g);
+
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* WriteDB: Data Base write routine for ZDOS access method. */
+/* Update is not possible without using a temporary file (NIY). */
+/***********************************************************************/
+int ZIXFAM::WriteBuffer(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* In Insert mode, blocs are added sequentialy to the file end. */
+ /* Note: Update mode is not handled for zip files. */
+ /*********************************************************************/
+ if (++CurNum == Rbuf) {
+ /*******************************************************************/
+ /* New block, start the writing process. */
+ /*******************************************************************/
+ BlkLen = Rbuf * Lrecl;
+
+ if (gzwrite(Zfile, To_Buf, BlkLen) != BlkLen ||
+ gzflush(Zfile, Z_FULL_FLUSH)) {
+ Closing = true;
+ return Zerror(g);
+ } // endif gzwrite
+
+ Rbuf = Nrec;
+ CurBlk++;
+ CurNum = 0;
+ Tdbp->SetLine(To_Buf);
+ } else
+ Tdbp->IncLine(Lrecl); // Used by FIXCOL functions
+
+ return RC_OK;
+ } // end of WriteBuffer
+
+/* --------------------------- Class ZLBFAM -------------------------- */
+
+/***********************************************************************/
+/* Constructors. */
+/***********************************************************************/
+ZLBFAM::ZLBFAM(PDOSDEF tdp) : BLKFAM(tdp)
+ {
+ Zstream = NULL;
+ Zbuffer = NULL;
+ Zlenp = NULL;
+ Optimized = tdp->IsOptimized();
+ } // end of ZLBFAM standard constructor
+
+ZLBFAM::ZLBFAM(PZLBFAM txfp) : BLKFAM(txfp)
+ {
+ Zstream = txfp->Zstream;
+ Zbuffer = txfp->Zbuffer;
+ Zlenp = txfp->Zlenp;
+ Optimized = txfp->Optimized;
+ } // end of ZLBFAM (dummy?) copy constructor
+
+/***********************************************************************/
+/* ZLB GetFileLength: returns an estimate of what would be the */
+/* uncompressed file size in number of bytes. */
+/***********************************************************************/
+int ZLBFAM::GetFileLength(PGLOBAL g)
+ {
+ int len = (Optimized) ? BlkPos[Block] : BLKFAM::GetFileLength(g);
+
+ if (len > 0)
+ // Estimate size reduction to a max of 5
+ len *= 5;
+
+ return len;
+ } // end of GetFileLength
+
+/***********************************************************************/
+/* Allocate the line buffer. For mode Delete a bigger buffer has to */
+/* be allocated because is it also used to move lines into the file. */
+/***********************************************************************/
+bool ZLBFAM::AllocateBuffer(PGLOBAL g)
+ {
+ char *msg;
+ int n, zrc;
+
+#if 0
+ if (!Optimized && Tdbp->NeedIndexing(g)) {
+ strcpy(g->Message, MSG(NOP_ZLIB_INDEX));
+ return TRUE;
+ } // endif indexing
+#endif // 0
+
+#if defined(NOLIB)
+ if (!zlib && LoadZlib()) {
+ sprintf(g->Message, MSG(DLL_LOAD_ERROR), GetLastError(), "zlib.dll");
+ return TRUE;
+ } // endif zlib
+#endif
+
+ BLKFAM::AllocateBuffer(g);
+//Buflen = Nrec * (Lrecl + 2);
+//Rbuf = Nrec;
+
+ // Allocate the compressed buffer
+ n = Buflen + 16; // ?????????????????????????????????
+ Zlenp = (int*)PlugSubAlloc(g, NULL, n);
+ Zbuffer = (Byte*)(Zlenp + 1);
+
+ // Allocate and initialize the Z stream
+ Zstream = (z_streamp)PlugSubAlloc(g, NULL, sizeof(z_stream));
+ Zstream->zalloc = (alloc_func)0;
+ Zstream->zfree = (free_func)0;
+ Zstream->opaque = (voidpf)0;
+ Zstream->next_in = NULL;
+ Zstream->avail_in = 0;
+
+ if (Tdbp->GetMode() == MODE_READ) {
+ msg = "inflateInit";
+ zrc = inflateInit(Zstream);
+ } else {
+ msg = "deflateInit";
+ zrc = deflateInit(Zstream, Z_DEFAULT_COMPRESSION);
+ } // endif Mode
+
+ if (zrc != Z_OK) {
+ if (Zstream->msg)
+ sprintf(g->Message, "%s error: %s", msg, Zstream->msg);
+ else
+ sprintf(g->Message, "%s error: %d", msg, zrc);
+
+ return TRUE;
+ } // endif zrc
+
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ // Write the file header block
+ if (Last == Nrec) {
+ CurBlk = Block;
+ CurNum = 0;
+
+ if (!GetFileLength(g)) {
+ // Write the zlib header as an extra block
+ strcpy(To_Buf, "PlugDB");
+ BlkLen = strlen("PlugDB") + 1;
+
+ if (WriteCompressedBuffer(g))
+ return TRUE;
+
+ } // endif void file
+
+ } else {
+ // In mode insert, if Last != Nrec, last block must be updated
+ CurBlk = Block - 1;
+ CurNum = Last;
+
+ strcpy(g->Message, MSG(NO_PAR_BLK_INS));
+ return TRUE;
+ } // endif Last
+
+ } else { // MODE_READ
+ // First thing to do is to read the header block
+ void *rdbuf;
+
+ if (Optimized) {
+ BlkLen = BlkPos[0];
+ rdbuf = Zlenp;
+ } else {
+ // Get the stored length from the file itself
+ if (fread(Zlenp, sizeof(int), 1, Stream) != 1)
+ return FALSE; // Empty file
+
+ BlkLen = *Zlenp;
+ rdbuf = Zbuffer;
+ } // endif Optimized
+
+ switch (ReadCompressedBuffer(g, rdbuf)) {
+ case RC_EF:
+ return FALSE;
+ case RC_FX:
+#if defined(UNIX)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
+#else
+ sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
+#endif
+ case RC_NF:
+ return TRUE;
+ } // endswitch
+
+ // Some old tables can have PlugDB in their header
+ if (strcmp(To_Buf, "PlugDB")) {
+ sprintf(g->Message, MSG(BAD_HEADER), Tdbp->GetFile(g));
+ return TRUE;
+ } // endif strcmp
+
+ } // endif Mode
+
+ return FALSE;
+ } // end of AllocateBuffer
+
+/***********************************************************************/
+/* GetPos: return the position of last read record. */
+/***********************************************************************/
+int ZLBFAM::GetPos(void)
+ {
+ return (Optimized) ? (CurNum + Nrec * CurBlk) : Fpos;
+ } // end of GetPos
+
+/***********************************************************************/
+/* GetNextPos: should not be called for this class. */
+/***********************************************************************/
+int ZLBFAM::GetNextPos(void)
+ {
+ if (Optimized) {
+ assert(FALSE);
+ return 0;
+ } else
+ return ftell(Stream);
+
+ } // end of GetNextPos
+
+/***********************************************************************/
+/* ReadBuffer: Read one line for a text file. */
+/***********************************************************************/
+int ZLBFAM::ReadBuffer(PGLOBAL g)
+ {
+ int n;
+ void *rdbuf;
+
+ /*********************************************************************/
+ /* Sequential reading when Placed is not true. */
+ /*********************************************************************/
+ if (Placed) {
+ Placed = FALSE;
+ } else if (++CurNum < Rbuf) {
+ CurLine = NxtLine;
+
+ // Get the position of the next line in the buffer
+ if (Tdbp->GetFtype() == RECFM_VAR)
+ while (*NxtLine++ != '\n') ;
+ else
+ NxtLine += Lrecl;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - ((Tdbp->GetFtype() == RECFM_BIN) ? 0 : Ending);
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+ return RC_OK;
+ } else if (Rbuf < Nrec && CurBlk != -1) {
+ CurNum--; // To have a correct Last value when optimizing
+ return RC_EF;
+ } else {
+ /*******************************************************************/
+ /* New block. */
+ /*******************************************************************/
+ CurNum = 0;
+
+ next:
+ if (++CurBlk >= Block)
+ return RC_EF;
+
+ /*******************************************************************/
+ /* Before reading a new block, check whether block optimization */
+ /* can be done, as well as for join as for local filtering. */
+ /*******************************************************************/
+ if (Optimized) switch (Tdbp->TestBlock(g)) {
+ case RC_EF:
+ return RC_EF;
+ case RC_NF:
+ goto next;
+ } // endswitch rc
+
+ } // endif's
+
+ if (OldBlk == CurBlk)
+ goto ok; // Block is already there
+
+ if (Optimized) {
+ // Store the position of next block
+ Fpos = BlkPos[CurBlk];
+
+ // fseek is required only in non sequential reading
+ if (CurBlk != OldBlk + 1)
+ if (fseek(Stream, Fpos, SEEK_SET)) {
+ sprintf(g->Message, MSG(FSETPOS_ERROR), Fpos);
+ return RC_FX;
+ } // endif fseek
+
+ // Calculate the length of block to read
+ BlkLen = BlkPos[CurBlk + 1] - Fpos;
+ rdbuf = Zlenp;
+ } else { // !Optimized
+ if (CurBlk != OldBlk + 1) {
+ strcpy(g->Message, MSG(INV_RAND_ACC));
+ return RC_FX;
+ } else
+ Fpos = ftell(Stream); // Used when optimizing
+
+ // Get the stored length from the file itself
+ if (fread(Zlenp, sizeof(int), 1, Stream) != 1) {
+ if (feof(Stream))
+ return RC_EF;
+
+ goto err;
+ } // endif fread
+
+ BlkLen = *Zlenp;
+ rdbuf = Zbuffer;
+ } // endif Optimized
+
+ // Read the next block
+ switch (ReadCompressedBuffer(g, rdbuf)) {
+ case RC_FX: goto err;
+ case RC_NF: return RC_FX;
+ case RC_EF: return RC_EF;
+ default: Rbuf = (CurBlk == Block - 1) ? Last : Nrec;
+ } // endswitch ReadCompressedBuffer
+
+ ok:
+ if (Tdbp->GetFtype() == RECFM_VAR) {
+ int i;
+
+ // Get the position of the current line
+ for (i = 0, CurLine = To_Buf; i < CurNum; i++)
+ while (*CurLine++ != '\n') ; // What about Unix ???
+
+ // Now get the position of the next line
+ for (NxtLine = CurLine; *NxtLine++ != '\n';) ;
+
+ // Set caller line buffer
+ n = NxtLine - CurLine - Ending;
+ } else {
+ CurLine = To_Buf + CurNum * Lrecl;
+ NxtLine = CurLine + Lrecl;
+ n = Lrecl - ((Tdbp->GetFtype() == RECFM_BIN) ? 0 : Ending);
+ } // endif Ftype
+
+ memcpy(Tdbp->GetLine(), CurLine, n);
+ Tdbp->GetLine()[n] = '\0';
+
+ OldBlk = CurBlk; // Last block actually read
+ IsRead = TRUE; // Is read indeed
+ return RC_OK;
+
+ err:
+#if defined(UNIX)
+ sprintf(g->Message, MSG(READ_ERROR), To_File, strerror(errno));
+#else
+ sprintf(g->Message, MSG(READ_ERROR), To_File, _strerror(NULL));
+#endif
+ return RC_FX;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* Read and decompress a block from the stream. */
+/***********************************************************************/
+int ZLBFAM::ReadCompressedBuffer(PGLOBAL g, void *rdbuf)
+ {
+ if (fread(rdbuf, 1, (size_t)BlkLen, Stream) == (unsigned)BlkLen) {
+ int zrc;
+
+ num_read++;
+
+ if (Optimized && BlkLen != signed(*Zlenp + sizeof(int))) {
+ sprintf(g->Message, MSG(BAD_BLK_SIZE), CurBlk + 1);
+ return RC_NF;
+ } // endif BlkLen
+
+ // HERE WE MUST INFLATE THE BLOCK
+ Zstream->next_in = Zbuffer;
+ Zstream->avail_in = (uInt)(*Zlenp);
+ Zstream->next_out = (Byte*)To_Buf;
+ Zstream->avail_out = Buflen;
+ zrc = inflate(Zstream, Z_SYNC_FLUSH);
+
+ if (zrc != Z_OK) {
+ if (Zstream->msg)
+ sprintf(g->Message, MSG(FUNC_ERR_S), "inflate", Zstream->msg);
+ else
+ sprintf(g->Message, MSG(FUNCTION_ERROR), "inflate", (int)zrc);
+
+ return RC_NF;
+ } // endif zrc
+
+ } else if (feof(Stream)) {
+ return RC_EF;
+ } else
+ return RC_FX;
+
+ return RC_OK;
+ } // end of ReadCompressedBuffer
+
+/***********************************************************************/
+/* WriteBuffer: File write routine for DOS access method. */
+/* Update is directly written back into the file, */
+/* with this (fast) method, record size cannot change. */
+/***********************************************************************/
+int ZLBFAM::WriteBuffer(PGLOBAL g)
+ {
+ assert (Tdbp->GetMode() == MODE_INSERT);
+
+ /*********************************************************************/
+ /* Prepare the write buffer. */
+ /*********************************************************************/
+ if (!Closing) {
+ if (Tdbp->GetFtype() == RECFM_BIN)
+ memcpy(CurLine, Tdbp->GetLine(), Lrecl);
+ else
+ strcat(strcpy(CurLine, Tdbp->GetLine()), CrLf);
+
+#if defined(_DEBUG)
+ if (Tdbp->GetFtype() == RECFM_FIX &&
+ (signed)strlen(CurLine) != Lrecl + (signed)strlen(CrLf)) {
+ strcpy(g->Message, MSG(BAD_LINE_LEN));
+ Closing = TRUE;
+ return RC_FX;
+ } // endif Lrecl
+#endif // _DEBUG
+ } // endif Closing
+
+ /*********************************************************************/
+ /* In Insert mode, blocs are added sequentialy to the file end. */
+ /*********************************************************************/
+ if (++CurNum != Rbuf) {
+ if (Tdbp->GetFtype() == RECFM_VAR)
+ CurLine += strlen(CurLine);
+ else
+ CurLine += Lrecl;
+
+ return RC_OK; // We write only full blocks
+ } // endif CurNum
+
+ // HERE WE MUST DEFLATE THE BLOCK
+ if (Tdbp->GetFtype() == RECFM_VAR)
+ NxtLine = CurLine + strlen(CurLine);
+ else
+ NxtLine = CurLine + Lrecl;
+
+ BlkLen = NxtLine - To_Buf;
+
+ if (WriteCompressedBuffer(g)) {
+ Closing = TRUE; // To tell CloseDB about a Write error
+ return RC_FX;
+ } // endif WriteCompressedBuffer
+
+ CurBlk++;
+ CurNum = 0;
+ CurLine = To_Buf;
+ return RC_OK;
+ } // end of WriteBuffer
+
+/***********************************************************************/
+/* Compress the buffer and write the deflated output to stream. */
+/***********************************************************************/
+bool ZLBFAM::WriteCompressedBuffer(PGLOBAL g)
+ {
+ int zrc;
+
+ Zstream->next_in = (Byte*)To_Buf;
+ Zstream->avail_in = (uInt)BlkLen;
+ Zstream->next_out = Zbuffer;
+ Zstream->avail_out = Buflen + 16;
+ Zstream->total_out = 0;
+ zrc = deflate(Zstream, Z_FULL_FLUSH);
+
+ if (zrc != Z_OK) {
+ if (Zstream->msg)
+ sprintf(g->Message, MSG(FUNC_ERR_S), "deflate", Zstream->msg);
+ else
+ sprintf(g->Message, MSG(FUNCTION_ERROR), "deflate", (int)zrc);
+
+ return TRUE;
+ } else
+ *Zlenp = Zstream->total_out;
+
+ // Now start the writing process.
+ BlkLen = *Zlenp + sizeof(int);
+
+ if (fwrite(Zlenp, 1, BlkLen, Stream) != (size_t)BlkLen) {
+ sprintf(g->Message, MSG(FWRITE_ERROR), strerror(errno));
+ return TRUE;
+ } // endif size
+
+ return FALSE;
+ } // end of WriteCompressedBuffer
+
+/***********************************************************************/
+/* Table file close routine for DOS access method. */
+/***********************************************************************/
+void ZLBFAM::CloseTableFile(PGLOBAL g)
+ {
+ int rc = RC_OK;
+
+ if (Tdbp->GetMode() == MODE_INSERT) {
+ PCATLG cat = PlgGetCatalog(g);
+ LPCSTR name = Tdbp->GetName();
+ PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
+
+ // Closing is True if last Write was in error
+ if (CurNum && !Closing) {
+ // Some more inserted lines remain to be written
+ Last = (Nrec - Rbuf) + CurNum;
+ Block = CurBlk + 1;
+ Rbuf = CurNum--;
+ Closing = TRUE;
+ rc = WriteBuffer(g);
+ } else if (Rbuf == Nrec) {
+ Last = Nrec;
+ Block = CurBlk;
+ } // endif CurNum
+
+ if (rc != RC_FX) {
+ defp->SetBlock(Block);
+ defp->SetLast(Last);
+ cat->SetIntCatInfo("Blocks", Block);
+ cat->SetIntCatInfo("Last", Last);
+ } // endif
+
+ fclose(Stream);
+ } else
+ rc = fclose(Stream);
+
+ if (trace)
+ htrc("ZLB CloseTableFile: closing %s mode=%d rc=%d\n",
+ To_File, Tdbp->GetMode(), rc);
+
+ Stream = NULL; // So we can know whether table is open
+ To_Fb->Count = 0; // Avoid double closing by PlugCloseAll
+
+ if (Tdbp->GetMode() == MODE_READ)
+ rc = inflateEnd(Zstream);
+ else
+ rc = deflateEnd(Zstream);
+
+ } // end of CloseTableFile
+
+/***********************************************************************/
+/* Rewind routine for ZLIB access method. */
+/***********************************************************************/
+void ZLBFAM::Rewind(void)
+ {
+ // We must be positioned after the header block
+ if (CurBlk >= 0) { // Nothing to do if no block read yet
+ if (!Optimized) { // If optimized, fseek will be done in ReadBuffer
+ rewind(Stream);
+ fread(Zlenp, sizeof(int), 1, Stream);
+ fseek(Stream, *Zlenp + sizeof(int), SEEK_SET);
+ OldBlk = -1;
+ } // endif Optimized
+
+ CurBlk = -1;
+ CurNum = Rbuf;
+ } // endif CurBlk
+
+//OldBlk = -1;
+//Rbuf = 0; commented out in case we reuse last read block
+ } // end of Rewind
+
+/* ------------------------ End of ZipFam ---------------------------- */
diff --git a/storage/connect/ha_connect.cc b/storage/connect/ha_connect.cc
index c12cee58c40..98a0e21708b 100644
--- a/storage/connect/ha_connect.cc
+++ b/storage/connect/ha_connect.cc
@@ -1,5688 +1,5686 @@
-/* Copyright (C) Olivier Bertrand 2004 - 2014
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
-
-/**
- @file ha_connect.cc
-
- @brief
- The ha_connect engine is a stubbed storage engine that enables to create tables
- based on external data. Principally they are based on plain files of many
- different types, but also on collections of such files, collection of tables,
- ODBC tables retrieving data from other DBMS having an ODBC server, and even
- virtual tables.
-
- @details
- ha_connect will let you create/open/delete tables, the created table can be
- done specifying an already existing file, the drop table command will just
- suppress the table definition but not the eventual data file.
- Indexes are not supported for all table types but data can be inserted,
- updated or deleted.
-
- You can enable the CONNECT storage engine in your build by doing the
- following during your build process:<br> ./configure
- --with-connect-storage-engine
-
- You can install the CONNECT handler as all other storage handlers.
-
- Once this is done, MySQL will let you create tables with:<br>
- CREATE TABLE <table name> (...) ENGINE=CONNECT;
-
- The example storage engine does not use table locks. It
- implements an example "SHARE" that is inserted into a hash by table
- name. This is not used yet.
-
- Please read the object definition in ha_connect.h before reading the rest
- of this file.
-
- @note
- This MariaDB CONNECT handler is currently an adaptation of the XDB handler
- that was written for MySQL version 4.1.2-alpha. Its overall design should
- be enhanced in the future to meet MariaDB requirements.
-
- @note
- It was written also from the Brian's ha_example handler and contains parts
- of it that are there but not currently used, such as table variables.
-
- @note
- When you create an CONNECT table, the MySQL Server creates a table .frm
- (format) file in the database directory, using the table name as the file
- name as is customary with MySQL. No other files are created. To get an idea
- of what occurs, here is an example select that would do a scan of an entire
- table:
-
- @code
- ha-connect::open
- ha_connect::store_lock
- ha_connect::external_lock
- ha_connect::info
- ha_connect::rnd_init
- ha_connect::extra
- ENUM HA_EXTRA_CACHE Cache record in HA_rrnd()
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::rnd_next
- ha_connect::extra
- ENUM HA_EXTRA_NO_CACHE End caching of records (def)
- ha_connect::external_lock
- ha_connect::extra
- ENUM HA_EXTRA_RESET Reset database to after open
- @endcode
-
- Here you see that the connect storage engine has 9 rows called before
- rnd_next signals that it has reached the end of its data. Calls to
- ha_connect::extra() are hints as to what will be occuring to the request.
-
- Happy use!<br>
- -Olivier
-*/
-
-#ifdef USE_PRAGMA_IMPLEMENTATION
-#pragma implementation // gcc: Class implementation
-#endif
-
-#define MYSQL_SERVER 1
-#define DONT_DEFINE_VOID
-//#include "sql_partition.h"
-#include "sql_class.h"
-#include "create_options.h"
-#include "mysql_com.h"
-#include "field.h"
-#include "sql_parse.h"
-#include "sql_base.h"
-#include <sys/stat.h>
-#if defined(NEW_WAY)
-#include "sql_table.h"
-#endif // NEW_WAY
-#undef OFFSET
-
-#define NOPARSE
-#if defined(UNIX)
-#include "osutil.h"
-#endif // UNIX
-#include "global.h"
-#include "plgdbsem.h"
-#if defined(ODBC_SUPPORT)
-#include "odbccat.h"
-#endif // ODBC_SUPPORT
-#if defined(MYSQL_SUPPORT)
-#include "xtable.h"
-#include "tabmysql.h"
-#endif // MYSQL_SUPPORT
-#include "filamdbf.h"
-#include "tabxcl.h"
-#include "tabfmt.h"
-#include "reldef.h"
-#include "tabcol.h"
-#include "xindex.h"
-#if defined(WIN32)
-#include <io.h>
-#include "tabwmi.h"
-#endif // WIN32
-#include "connect.h"
-#include "user_connect.h"
-#include "ha_connect.h"
-#include "mycat.h"
-#include "myutil.h"
-#include "preparse.h"
-#include "inihandl.h"
-
-#define PLGXINI "plgcnx.ini" /* Configuration settings file */
-#define my_strupr(p) my_caseup_str(default_charset_info, (p));
-#define my_strlwr(p) my_casedn_str(default_charset_info, (p));
-#define my_stricmp(a,b) my_strcasecmp(default_charset_info, (a), (b))
-
-#ifdef LIBXML2_SUPPORT
-#include "libdoc.h"
-#endif // LIBXML2_SUPPORT
-
-#include "taboccur.h"
-#include "tabpivot.h"
-
-
-/***********************************************************************/
-/* DB static variables. */
-/***********************************************************************/
-extern "C" char plgxini[];
-extern "C" char plgini[];
-extern "C" char nmfile[];
-extern "C" char pdebug[];
-
-/***********************************************************************/
-/* Initialize the ha_connect static members. */
-/***********************************************************************/
-#define CONNECT_INI "connect.ini"
-extern "C" {
- char connectini[_MAX_PATH]= CONNECT_INI;
- char version[]= "Version 1.02.0001 February 03, 2014";
-
-#if defined(XMSG)
- char msglang[]; // Default message language
-#endif
- int trace= 0; // The general trace value
-} // extern "C"
-
-int xtrace= 0;
-ulong ha_connect::num= 0;
-//int DTVAL::Shift= 0;
-
-/***********************************************************************/
-/* Utility functions. */
-/***********************************************************************/
-PQRYRES OEMColumns(PGLOBAL g, PTOS topt, char *tab, char *db, bool info);
-
-static PCONNECT GetUser(THD *thd, PCONNECT xp);
-static PGLOBAL GetPlug(THD *thd, PCONNECT& lxp);
-
-static handler *connect_create_handler(handlerton *hton,
- TABLE_SHARE *table,
- MEM_ROOT *mem_root);
-
-static int connect_assisted_discovery(handlerton *hton, THD* thd,
- TABLE_SHARE *table_s,
- HA_CREATE_INFO *info);
-
-handlerton *connect_hton;
-
-/**
- CREATE TABLE option list (table options)
-
- These can be specified in the CREATE TABLE:
- CREATE TABLE ( ... ) {...here...}
-*/
-ha_create_table_option connect_table_option_list[]=
-{
- HA_TOPTION_STRING("TABLE_TYPE", type),
- HA_TOPTION_STRING("FILE_NAME", filename),
- HA_TOPTION_STRING("XFILE_NAME", optname),
-//HA_TOPTION_STRING("CONNECT_STRING", connect),
- HA_TOPTION_STRING("TABNAME", tabname),
- HA_TOPTION_STRING("TABLE_LIST", tablist),
- HA_TOPTION_STRING("DBNAME", dbname),
- HA_TOPTION_STRING("SEP_CHAR", separator),
- HA_TOPTION_STRING("QCHAR", qchar),
- HA_TOPTION_STRING("MODULE", module),
- HA_TOPTION_STRING("SUBTYPE", subtype),
- HA_TOPTION_STRING("CATFUNC", catfunc),
- HA_TOPTION_STRING("SRCDEF", srcdef),
- HA_TOPTION_STRING("COLIST", colist),
- HA_TOPTION_STRING("OPTION_LIST", oplist),
- HA_TOPTION_STRING("DATA_CHARSET", data_charset),
- HA_TOPTION_NUMBER("LRECL", lrecl, 0, 0, INT_MAX32, 1),
- HA_TOPTION_NUMBER("BLOCK_SIZE", elements, 0, 0, INT_MAX32, 1),
-//HA_TOPTION_NUMBER("ESTIMATE", estimate, 0, 0, INT_MAX32, 1),
- HA_TOPTION_NUMBER("MULTIPLE", multiple, 0, 0, 2, 1),
- HA_TOPTION_NUMBER("HEADER", header, 0, 0, 3, 1),
- HA_TOPTION_NUMBER("QUOTED", quoted, (ulonglong) -1, 0, 3, 1),
- HA_TOPTION_NUMBER("ENDING", ending, (ulonglong) -1, 0, INT_MAX32, 1),
- HA_TOPTION_NUMBER("COMPRESS", compressed, 0, 0, 2, 1),
-//HA_TOPTION_BOOL("COMPRESS", compressed, 0),
- HA_TOPTION_BOOL("MAPPED", mapped, 0),
- HA_TOPTION_BOOL("HUGE", huge, 0),
- HA_TOPTION_BOOL("SPLIT", split, 0),
- HA_TOPTION_BOOL("READONLY", readonly, 0),
- HA_TOPTION_BOOL("SEPINDEX", sepindex, 0),
- HA_TOPTION_END
-};
-
-
-/**
- CREATE TABLE option list (field options)
-
- These can be specified in the CREATE TABLE per field:
- CREATE TABLE ( field ... {...here...}, ... )
-*/
-ha_create_table_option connect_field_option_list[]=
-{
- HA_FOPTION_NUMBER("FLAG", offset, (ulonglong) -1, 0, INT_MAX32, 1),
- HA_FOPTION_NUMBER("MAX_DIST", freq, 0, 0, INT_MAX32, 1), // BLK_INDX
- HA_FOPTION_NUMBER("DISTRIB", opt, 0, 0, 2, 1), // used for BLK_INDX
- HA_FOPTION_NUMBER("FIELD_LENGTH", fldlen, 0, 0, INT_MAX32, 1),
- HA_FOPTION_STRING("DATE_FORMAT", dateformat),
- HA_FOPTION_STRING("FIELD_FORMAT", fieldformat),
- HA_FOPTION_STRING("SPECIAL", special),
- HA_FOPTION_END
-};
-
-/***********************************************************************/
-/* Push G->Message as a MySQL warning. */
-/***********************************************************************/
-bool PushWarning(PGLOBAL g, PTDBASE tdbp, int level)
- {
- PHC phc;
- THD *thd;
- MYCAT *cat= (MYCAT*)tdbp->GetDef()->GetCat();
- Sql_condition::enum_warning_level wlvl;
-
-
- if (!cat || !(phc= cat->GetHandler()) || !phc->GetTable() ||
- !(thd= (phc->GetTable())->in_use))
- return true;
-
-//push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- wlvl= (Sql_condition::enum_warning_level)level;
- push_warning(thd, wlvl, 0, g->Message);
- return false;
- } // end of PushWarning
-
-#ifdef HAVE_PSI_INTERFACE
-static PSI_mutex_key con_key_mutex_CONNECT_SHARE_mutex;
-
-static PSI_mutex_info all_connect_mutexes[]=
-{
- { &con_key_mutex_CONNECT_SHARE_mutex, "CONNECT_SHARE::mutex", 0}
-};
-
-static void init_connect_psi_keys()
-{
- const char* category= "connect";
- int count;
-
- if (PSI_server == NULL)
- return;
-
- count= array_elements(all_connect_mutexes);
- PSI_server->register_mutex(category, all_connect_mutexes, count);
-}
-#else
-static void init_connect_psi_keys() {}
-#endif
-
-
-DllExport LPCSTR PlugSetPath(LPSTR to, LPCSTR name, LPCSTR dir)
-{
- const char *res= PlugSetPath(to, mysql_data_home, name, dir);
- return res;
-}
-
-
-/**
- @brief
- If frm_error() is called then we will use this to determine
- the file extensions that exist for the storage engine. This is also
- used by the default rename_table and delete_table method in
- handler.cc.
-
- For engines that have two file name extentions (separate meta/index file
- and data file), the order of elements is relevant. First element of engine
- file name extentions array should be meta/index file extention. Second
- element - data file extention. This order is assumed by
- prepare_for_repair() when REPAIR TABLE ... USE_FRM is issued.
-
- @see
- rename_table method in handler.cc and
- delete_table method in handler.cc
-*/
-static const char *ha_connect_exts[]= {
- ".dos", ".fix", ".csv", ".bin", ".fmt", ".dbf", ".xml", ".ini", ".vec",
- ".dnx", ".fnx", ".bnx", ".vnx", ".dbx", ".dop", ".fop", ".bop", ".vop",
- NULL};
-
-/**
- @brief
- Plugin initialization
-*/
-static int connect_init_func(void *p)
-{
- DBUG_ENTER("connect_init_func");
- char dir[_MAX_PATH - sizeof(CONNECT_INI) - 1];
-
-#ifdef LIBXML2_SUPPORT
- XmlInitParserLib();
-#endif // LIBXML2_SUPPORT
-
- /* Build connect.ini file name */
- my_getwd(dir, sizeof(dir) - 1, MYF(0));
- snprintf(connectini, sizeof(connectini), "%s%s", dir, CONNECT_INI);
- sql_print_information("CONNECT: %s=%s", CONNECT_INI, connectini);
-
- if ((xtrace= GetPrivateProfileInt("CONNECT", "Trace", 0, connectini)))
- {
- sql_print_information("CONNECT: xtrace=%d", xtrace);
- sql_print_information("CONNECT: plgini=%s", plgini);
- sql_print_information("CONNECT: plgxini=%s", plgxini);
- sql_print_information("CONNECT: nmfile=%s", nmfile);
- sql_print_information("CONNECT: pdebug=%s", pdebug);
- sql_print_information("CONNECT: version=%s", version);
- trace= xtrace;
- } // endif xtrace
-
-#if !defined(WIN32)
- PROFILE_Close(connectini);
-#endif // !WIN32
-
- init_connect_psi_keys();
-
- connect_hton= (handlerton *)p;
- connect_hton->state= SHOW_OPTION_YES;
- connect_hton->create= connect_create_handler;
- connect_hton->flags= HTON_TEMPORARY_NOT_SUPPORTED | HTON_NO_PARTITION;
- connect_hton->table_options= connect_table_option_list;
- connect_hton->field_options= connect_field_option_list;
- connect_hton->tablefile_extensions= ha_connect_exts;
- connect_hton->discover_table_structure= connect_assisted_discovery;
-
- if (xtrace)
- sql_print_information("connect_init: hton=%p", p);
-
- DTVAL::SetTimeShift(); // Initialize time zone shift once for all
- DBUG_RETURN(0);
-}
-
-
-/**
- @brief
- Plugin clean up
-*/
-static int connect_done_func(void *p)
-{
- int error= 0;
- PCONNECT pc, pn;
- DBUG_ENTER("connect_done_func");
-
-#ifdef LIBXML2_SUPPORT
- XmlCleanupParserLib();
-#endif // LIBXML2_SUPPORT
-
-#if !defined(WIN32)
- PROFILE_End();
-#endif // !WIN32
-
- for (pc= user_connect::to_users; pc; pc= pn) {
- if (pc->g)
- PlugCleanup(pc->g, true);
-
- pn= pc->next;
- delete pc;
- } // endfor pc
-
- DBUG_RETURN(error);
-}
-
-
-/**
- @brief
- Example of simple lock controls. The "share" it creates is a
- structure we will pass to each example handler. Do you have to have
- one of these? Well, you have pieces that are used for locking, and
- they are needed to function.
-*/
-
-CONNECT_SHARE *ha_connect::get_share()
-{
- CONNECT_SHARE *tmp_share;
- lock_shared_ha_data();
- if (!(tmp_share= static_cast<CONNECT_SHARE*>(get_ha_share_ptr())))
- {
- tmp_share= new CONNECT_SHARE;
- if (!tmp_share)
- goto err;
- mysql_mutex_init(con_key_mutex_CONNECT_SHARE_mutex,
- &tmp_share->mutex, MY_MUTEX_INIT_FAST);
- set_ha_share_ptr(static_cast<Handler_share*>(tmp_share));
- }
-err:
- unlock_shared_ha_data();
- return tmp_share;
-}
-
-
-static handler* connect_create_handler(handlerton *hton,
- TABLE_SHARE *table,
- MEM_ROOT *mem_root)
-{
- handler *h= new (mem_root) ha_connect(hton, table);
-
- if (xtrace)
- htrc("New CONNECT %p, table: %s\n",
- h, table ? table->table_name.str : "<null>");
-
- return h;
-} // end of connect_create_handler
-
-/****************************************************************************/
-/* ha_connect constructor. */
-/****************************************************************************/
-ha_connect::ha_connect(handlerton *hton, TABLE_SHARE *table_arg)
- :handler(hton, table_arg)
-{
- hnum= ++num;
- xp= (table) ? GetUser(ha_thd(), NULL) : NULL;
- if (xp)
- xp->SetHandler(this);
- tdbp= NULL;
- sdvalin= NULL;
- sdvalout= NULL;
- xmod= MODE_ANY;
- istable= false;
-//*tname= '\0';
- bzero((char*) &xinfo, sizeof(XINFO));
- valid_info= false;
- valid_query_id= 0;
- creat_query_id= (table && table->in_use) ? table->in_use->query_id : 0;
- stop= false;
- alter= false;
- mrr= false;
- indexing= -1;
- locked= 0;
- data_file_name= NULL;
- index_file_name= NULL;
- enable_activate_all_index= 0;
- int_table_flags= (HA_NO_TRANSACTIONS | HA_NO_PREFIX_CHAR_KEYS);
- ref_length= sizeof(int);
- share= NULL;
- tshp= NULL;
-} // end of ha_connect constructor
-
-
-/****************************************************************************/
-/* ha_connect destructor. */
-/****************************************************************************/
-ha_connect::~ha_connect(void)
-{
- if (xtrace)
- htrc("Delete CONNECT %p, table: %s, xp=%p count=%d\n", this,
- table ? table->s->table_name.str : "<null>",
- xp, xp ? xp->count : 0);
-
- if (xp) {
- PCONNECT p;
-
- xp->count--;
-
- for (p= user_connect::to_users; p; p= p->next)
- if (p == xp)
- break;
-
- if (p && !p->count) {
- if (p->next)
- p->next->previous= p->previous;
-
- if (p->previous)
- p->previous->next= p->next;
- else
- user_connect::to_users= p->next;
-
- } // endif p
-
- if (!xp->count) {
- PlugCleanup(xp->g, true);
- delete xp;
- } // endif count
-
- } // endif xp
-
-} // end of ha_connect destructor
-
-
-/****************************************************************************/
-/* Get a pointer to the user of this handler. */
-/****************************************************************************/
-static PCONNECT GetUser(THD *thd, PCONNECT xp)
-{
- const char *dbn= NULL;
-
- if (!thd)
- return NULL;
-
- if (xp && thd == xp->thdp)
- return xp;
-
- for (xp= user_connect::to_users; xp; xp= xp->next)
- if (thd == xp->thdp)
- break;
-
- if (!xp) {
- xp= new user_connect(thd, dbn);
-
- if (xp->user_init()) {
- delete xp;
- xp= NULL;
- } // endif user_init
-
- } else
- xp->count++;
-
- return xp;
-} // end of GetUser
-
-
-/****************************************************************************/
-/* Get the global pointer of the user of this handler. */
-/****************************************************************************/
-static PGLOBAL GetPlug(THD *thd, PCONNECT& lxp)
-{
- lxp= GetUser(thd, lxp);
- return (lxp) ? lxp->g : NULL;
-} // end of GetPlug
-
-/****************************************************************************/
-/* Get the implied table type. */
-/****************************************************************************/
-TABTYPE ha_connect::GetRealType(PTOS pos)
-{
- TABTYPE type= GetTypeID(pos->type);
-
- if (type == TAB_UNDEF)
- type= pos->srcdef ? TAB_MYSQL : pos->tabname ? TAB_PRX : TAB_DOS;
-
- return type;
-} // end of GetRealType
-
-/** @brief
- This is a list of flags that indicate what functionality the storage
- engine implements. The current table flags are documented in handler.h
-*/
-ulonglong ha_connect::table_flags() const
-{
- ulonglong flags= HA_CAN_VIRTUAL_COLUMNS | HA_REC_NOT_IN_SEQ |
- HA_NO_AUTO_INCREMENT | HA_NO_PREFIX_CHAR_KEYS |
- HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
-// HA_NULL_IN_KEY | not implemented yet
-// HA_FAST_KEY_READ | causes error when sorting (???)
- HA_NO_TRANSACTIONS | HA_DUPLICATE_KEY_NOT_IN_ORDER |
- HA_NO_BLOBS | HA_MUST_USE_TABLE_CONDITION_PUSHDOWN;
- ha_connect *hp= (ha_connect*)this;
- PTOS pos= hp->GetTableOptionStruct(table);
-
- if (pos) {
- TABTYPE type= hp->GetRealType(pos);
-
- if (IsFileType(type))
- flags|= HA_FILE_BASED;
-
- if (IsExactType(type))
- flags|= (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT);
-
- // No data change on ALTER for outward tables
- if (!IsFileType(type) || hp->FileExists(pos->filename))
- flags|= HA_NO_COPY_ON_ALTER;
-
- } // endif pos
-
- return flags;
-} // end of table_flags
-
-/****************************************************************************/
-/* Return the value of an option specified in the option list. */
-/****************************************************************************/
-char *GetListOption(PGLOBAL g, const char *opname,
- const char *oplist, const char *def)
-{
- char key[16], val[256];
- char *pk, *pv, *pn;
- char *opval= (char*) def;
- int n;
-
- for (pk= (char*)oplist; pk; pk= ++pn) {
- pn= strchr(pk, ',');
- pv= strchr(pk, '=');
-
- if (pv && (!pn || pv < pn)) {
- n= pv - pk;
- memcpy(key, pk, n);
- key[n]= 0;
- pv++;
-
- if (pn) {
- n= pn - pv;
- memcpy(val, pv, n);
- val[n]= 0;
- } else
- strcpy(val, pv);
-
- } else {
- if (pn) {
- n= min(pn - pk, 15);
- memcpy(key, pk, n);
- key[n]= 0;
- } else
- strcpy(key, pk);
-
- val[0]= 0;
- } // endif pv
-
- if (!stricmp(opname, key)) {
- opval= (char*)PlugSubAlloc(g, NULL, strlen(val) + 1);
- strcpy(opval, val);
- break;
- } else if (!pn)
- break;
-
- } // endfor pk
-
- return opval;
-} // end of GetListOption
-
-/****************************************************************************/
-/* Return the table option structure. */
-/****************************************************************************/
-PTOS ha_connect::GetTableOptionStruct(TABLE *tab)
-{
- return (tshp) ? tshp->option_struct :
- (tab) ? tab->s->option_struct : NULL;
-} // end of GetTableOptionStruct
-
-/****************************************************************************/
-/* Return the value of a string option or NULL if not specified. */
-/****************************************************************************/
-char *ha_connect::GetStringOption(char *opname, char *sdef)
-{
- char *opval= NULL;
- PTOS options= GetTableOptionStruct(table);
-
- if (!options)
- ;
- else if (!stricmp(opname, "Type"))
- opval= (char*)options->type;
- else if (!stricmp(opname, "Filename"))
- opval= (char*)options->filename;
- else if (!stricmp(opname, "Optname"))
- opval= (char*)options->optname;
- else if (!stricmp(opname, "Tabname"))
- opval= (char*)options->tabname;
- else if (!stricmp(opname, "Tablist"))
- opval= (char*)options->tablist;
- else if (!stricmp(opname, "Database") ||
- !stricmp(opname, "DBname"))
- opval= (char*)options->dbname;
- else if (!stricmp(opname, "Separator"))
- opval= (char*)options->separator;
- else if (!stricmp(opname, "Connect"))
- opval= (tshp) ? tshp->connect_string.str : table->s->connect_string.str;
- else if (!stricmp(opname, "Qchar"))
- opval= (char*)options->qchar;
- else if (!stricmp(opname, "Module"))
- opval= (char*)options->module;
- else if (!stricmp(opname, "Subtype"))
- opval= (char*)options->subtype;
- else if (!stricmp(opname, "Catfunc"))
- opval= (char*)options->catfunc;
- else if (!stricmp(opname, "Srcdef"))
- opval= (char*)options->srcdef;
- else if (!stricmp(opname, "Colist"))
- opval= (char*)options->colist;
- else if (!stricmp(opname, "Data_charset"))
- opval= (char*)options->data_charset;
- else if (!stricmp(opname, "Query_String"))
- opval= thd_query_string(table->in_use)->str;
-
- if (!opval && options && options->oplist)
- opval= GetListOption(xp->g, opname, options->oplist);
-
- if (!opval) {
- if (sdef && !strcmp(sdef, "*")) {
- // Return the handler default value
- if (!stricmp(opname, "Dbname") || !stricmp(opname, "Database"))
- opval= (char*)GetDBName(NULL); // Current database
- else if (!stricmp(opname, "Type")) // Default type
- opval= (!options) ? NULL :
- (options->srcdef) ? (char*)"MYSQL" :
- (options->tabname) ? (char*)"PROXY" : (char*)"DOS";
- else if (!stricmp(opname, "User")) // Connected user
- opval= (char *) "root";
- else if (!stricmp(opname, "Host")) // Connected user host
- opval= (char *) "localhost";
- else
- opval= sdef; // Caller default
-
- } else
- opval= sdef; // Caller default
-
- } // endif !opval
-
- return opval;
-} // end of GetStringOption
-
-/****************************************************************************/
-/* Return the value of a Boolean option or bdef if not specified. */
-/****************************************************************************/
-bool ha_connect::GetBooleanOption(char *opname, bool bdef)
-{
- bool opval= bdef;
- char *pv;
- PTOS options= GetTableOptionStruct(table);
-
- if (!stricmp(opname, "View"))
- opval= (tshp) ? tshp->is_view : table->s->is_view;
- else if (!options)
- ;
- else if (!stricmp(opname, "Mapped"))
- opval= options->mapped;
- else if (!stricmp(opname, "Huge"))
- opval= options->huge;
-//else if (!stricmp(opname, "Compressed"))
-// opval= options->compressed;
- else if (!stricmp(opname, "Split"))
- opval= options->split;
- else if (!stricmp(opname, "Readonly"))
- opval= options->readonly;
- else if (!stricmp(opname, "SepIndex"))
- opval= options->sepindex;
- else if (options->oplist)
- if ((pv= GetListOption(xp->g, opname, options->oplist)))
- opval= (!*pv || *pv == 'y' || *pv == 'Y' || atoi(pv) != 0);
-
- return opval;
-} // end of GetBooleanOption
-
-/****************************************************************************/
-/* Set the value of the opname option (does not work for oplist options) */
-/* Currently used only to set the Sepindex value. */
-/****************************************************************************/
-bool ha_connect::SetBooleanOption(char *opname, bool b)
-{
- PTOS options= GetTableOptionStruct(table);
-
- if (!options)
- return true;
-
- if (!stricmp(opname, "SepIndex"))
- options->sepindex= b;
- else
- return true;
-
- return false;
-} // end of SetBooleanOption
-
-/****************************************************************************/
-/* Return the value of an integer option or NO_IVAL if not specified. */
-/****************************************************************************/
-int ha_connect::GetIntegerOption(char *opname)
-{
- ulonglong opval= NO_IVAL;
- char *pv;
- PTOS options= GetTableOptionStruct(table);
-
- if (!options)
- ;
- else if (!stricmp(opname, "Lrecl"))
- opval= options->lrecl;
- else if (!stricmp(opname, "Elements"))
- opval= options->elements;
- else if (!stricmp(opname, "Estimate"))
-// opval= options->estimate;
- opval= (int)table->s->max_rows;
- else if (!stricmp(opname, "Avglen"))
- opval= (int)table->s->avg_row_length;
- else if (!stricmp(opname, "Multiple"))
- opval= options->multiple;
- else if (!stricmp(opname, "Header"))
- opval= options->header;
- else if (!stricmp(opname, "Quoted"))
- opval= options->quoted;
- else if (!stricmp(opname, "Ending"))
- opval= options->ending;
- else if (!stricmp(opname, "Compressed"))
- opval= (options->compressed);
-
- if (opval == (ulonglong)NO_IVAL && options && options->oplist)
- if ((pv= GetListOption(xp->g, opname, options->oplist)))
- opval= CharToNumber(pv, strlen(pv), ULONGLONG_MAX, true);
-
- return (int)opval;
-} // end of GetIntegerOption
-
-/****************************************************************************/
-/* Set the value of the opname option (does not work for oplist options) */
-/* Currently used only to set the Lrecl value. */
-/****************************************************************************/
-bool ha_connect::SetIntegerOption(char *opname, int n)
-{
- PTOS options= GetTableOptionStruct(table);
-
- if (!options)
- return true;
-
- if (!stricmp(opname, "Lrecl"))
- options->lrecl= n;
- else if (!stricmp(opname, "Elements"))
- options->elements= n;
-//else if (!stricmp(opname, "Estimate"))
-// options->estimate= n;
- else if (!stricmp(opname, "Multiple"))
- options->multiple= n;
- else if (!stricmp(opname, "Header"))
- options->header= n;
- else if (!stricmp(opname, "Quoted"))
- options->quoted= n;
- else if (!stricmp(opname, "Ending"))
- options->ending= n;
- else if (!stricmp(opname, "Compressed"))
- options->compressed= n;
- else
- return true;
-//else if (options->oplist)
-// SetListOption(opname, options->oplist, n);
-
- return false;
-} // end of SetIntegerOption
-
-/****************************************************************************/
-/* Return a field option structure. */
-/****************************************************************************/
-PFOS ha_connect::GetFieldOptionStruct(Field *fdp)
-{
- return fdp->option_struct;
-} // end of GetFildOptionStruct
-
-/****************************************************************************/
-/* Returns the column description structure used to make the column. */
-/****************************************************************************/
-void *ha_connect::GetColumnOption(PGLOBAL g, void *field, PCOLINFO pcf)
-{
- const char *cp;
- ha_field_option_struct *fop;
- Field* fp;
- Field* *fldp;
-
- // Double test to be on the safe side
- if (!table)
- return NULL;
-
- // Find the column to describe
- if (field) {
- fldp= (Field**)field;
- fldp++;
- } else
- fldp= (tshp) ? tshp->field : table->field;
-
- if (!fldp || !(fp= *fldp))
- return NULL;
-
- // Get the CONNECT field options structure
- fop= GetFieldOptionStruct(fp);
- pcf->Flags= 0;
-
- // Now get column information
- pcf->Name= (char*)fp->field_name;
-
- if (fop && fop->special) {
- pcf->Fieldfmt= (char*)fop->special;
- pcf->Flags= U_SPECIAL;
- return fldp;
- } // endif special
-
- pcf->Scale= 0;
- pcf->Opt= (fop) ? (int)fop->opt : 0;
-
- if ((pcf->Length= fp->field_length) < 0)
- pcf->Length= 256; // BLOB?
-
- pcf->Precision= pcf->Length;
-
- if (fop) {
- pcf->Offset= (int)fop->offset;
- pcf->Freq= (int)fop->freq;
- pcf->Datefmt= (char*)fop->dateformat;
- pcf->Fieldfmt= (char*)fop->fieldformat;
- } else {
- pcf->Offset= -1;
- pcf->Freq= 0;
- pcf->Datefmt= NULL;
- pcf->Fieldfmt= NULL;
- } // endif fop
-
- switch (fp->type()) {
- case MYSQL_TYPE_BLOB:
- case MYSQL_TYPE_VARCHAR:
- case MYSQL_TYPE_VAR_STRING:
- pcf->Flags |= U_VAR;
- /* no break */
- default:
- pcf->Type= MYSQLtoPLG(fp->type());
- break;
- } // endswitch SQL type
-
- switch (pcf->Type) {
- case TYPE_STRING:
- // Do something for case
- cp= fp->charset()->name;
-
- // Find if collation name ends by _ci
- if (!strcmp(cp + strlen(cp) - 3, "_ci")) {
- pcf->Scale= 1; // Case insensitive
- pcf->Opt= 0; // Prevent index opt until it is safe
- } // endif ci
-
- break;
- case TYPE_DOUBLE:
- pcf->Scale= max(min(fp->decimals(), ((unsigned)pcf->Length - 2)), 0);
- break;
- case TYPE_DECIM:
- pcf->Precision= ((Field_new_decimal*)fp)->precision;
- pcf->Scale= fp->decimals();
- break;
- case TYPE_DATE:
- // Field_length is only used for DATE columns
- if (fop->fldlen)
- pcf->Length= (int)fop->fldlen;
- else {
- int len;
-
- if (pcf->Datefmt) {
- // Find the (max) length produced by the date format
- char buf[256];
- PGLOBAL g= GetPlug(table->in_use, xp);
- PDTP pdtp= MakeDateFormat(g, pcf->Datefmt, false, true, 0);
- struct tm datm;
- bzero(&datm, sizeof(datm));
- datm.tm_mday= 12;
- datm.tm_mon= 11;
- datm.tm_year= 112;
- len= strftime(buf, 256, pdtp->OutFmt, &datm);
- } else
- len= 0;
-
- // 11 is for signed numeric representation of the date
- pcf->Length= (len) ? len : 11;
- } // endelse
-
- break;
- default:
- break;
- } // endswitch type
-
- if (fp->flags & UNSIGNED_FLAG)
- pcf->Flags |= U_UNSIGNED;
-
- if (fp->flags & ZEROFILL_FLAG)
- pcf->Flags |= U_ZEROFILL;
-
- // This is used to skip null bit
- if (fp->real_maybe_null())
- pcf->Flags |= U_NULLS;
-
- // Mark virtual columns as such
- if (fp->vcol_info && !fp->stored_in_db)
- pcf->Flags |= U_VIRTUAL;
-
- pcf->Key= 0; // Not used when called from MySQL
-
- // Get the comment if any
- if (fp->comment.str && fp->comment.length) {
- pcf->Remark= (char*)PlugSubAlloc(g, NULL, fp->comment.length + 1);
- memcpy(pcf->Remark, fp->comment.str, fp->comment.length);
- pcf->Remark[fp->comment.length]= 0;
- } else
- pcf->Remark= NULL;
-
- return fldp;
-} // end of GetColumnOption
-
-/****************************************************************************/
-/* Returns the index description structure used to make the index. */
-/****************************************************************************/
-PIXDEF ha_connect::GetIndexInfo(TABLE_SHARE *s)
-{
- char *name, *pn;
- bool unique;
- PIXDEF xdp, pxd=NULL, toidx= NULL;
- PKPDEF kpp, pkp;
- KEY kp;
- PGLOBAL& g= xp->g;
-
- if (!s)
- s= table->s;
-
- for (int n= 0; (unsigned)n < s->keynames.count; n++) {
- if (xtrace)
- htrc("Getting created index %d info\n", n + 1);
-
- // Find the index to describe
- kp= s->key_info[n];
-
- // Now get index information
- pn= (char*)s->keynames.type_names[n];
- name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
- strcpy(name, pn); // This is probably unuseful
- unique= (kp.flags & 1) != 0;
- pkp= NULL;
-
- // Allocate the index description block
- xdp= new(g) INDEXDEF(name, unique, n);
-
- // Get the the key parts info
- for (int k= 0; (unsigned)k < kp.user_defined_key_parts; k++) {
- pn= (char*)kp.key_part[k].field->field_name;
- name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
- strcpy(name, pn); // This is probably unuseful
-
- // Allocate the key part description block
- kpp= new(g) KPARTDEF(name, k + 1);
- kpp->SetKlen(kp.key_part[k].length);
-
-#if 0 // NIY
- // Index on auto increment column can be an XXROW index
- if (kp.key_part[k].field->flags & AUTO_INCREMENT_FLAG &&
- kp.uder_defined_key_parts == 1) {
- char *type= GetStringOption("Type", "DOS");
- TABTYPE typ= GetTypeID(type);
-
- xdp->SetAuto(IsTypeFixed(typ));
- } // endif AUTO_INCREMENT
-#endif // 0
-
- if (pkp)
- pkp->SetNext(kpp);
- else
- xdp->SetToKeyParts(kpp);
-
- pkp= kpp;
- } // endfor k
-
- xdp->SetNParts(kp.user_defined_key_parts);
-
- if (pxd)
- pxd->SetNext(xdp);
- else
- toidx= xdp;
-
- pxd= xdp;
- } // endfor n
-
- return toidx;
-} // end of GetIndexInfo
-
-const char *ha_connect::GetDBName(const char* name)
-{
- return (name) ? name : table->s->db.str;
-} // end of GetDBName
-
-const char *ha_connect::GetTableName(void)
-{
- return (tshp) ? tshp->table_name.str : table->s->table_name.str;
-} // end of GetTableName
-
-#if 0
-/****************************************************************************/
-/* Returns the column real or special name length of a field. */
-/****************************************************************************/
-int ha_connect::GetColNameLen(Field *fp)
-{
- int n;
- PFOS fop= GetFieldOptionStruct(fp);
-
- // Now get the column name length
- if (fop && fop->special)
- n= strlen(fop->special) + 1;
- else
- n= strlen(fp->field_name);
-
- return n;
-} // end of GetColNameLen
-
-/****************************************************************************/
-/* Returns the column real or special name of a field. */
-/****************************************************************************/
-char *ha_connect::GetColName(Field *fp)
-{
- PFOS fop= GetFieldOptionStruct(fp);
-
- return (fop && fop->special) ? fop->special : (char*)fp->field_name;
-} // end of GetColName
-
-/****************************************************************************/
-/* Adds the column real or special name of a field to a string. */
-/****************************************************************************/
-void ha_connect::AddColName(char *cp, Field *fp)
-{
- PFOS fop= GetFieldOptionStruct(fp);
-
- // Now add the column name
- if (fop && fop->special)
- // The prefix * mark the column as "special"
- strcat(strcpy(cp, "*"), strupr(fop->special));
- else
- strcpy(cp, (char*)fp->field_name);
-
-} // end of AddColName
-#endif // 0
-
-/****************************************************************************/
-/* Get the table description block of a CONNECT table. */
-/****************************************************************************/
-PTDB ha_connect::GetTDB(PGLOBAL g)
-{
- const char *table_name;
- PTDB tp;
-
- // Double test to be on the safe side
- if (!g || !table)
- return NULL;
-
- table_name= GetTableName();
-
- if (!xp->CheckQuery(valid_query_id) && tdbp
- && !stricmp(tdbp->GetName(), table_name)
- && (tdbp->GetMode() == xmod
- || tdbp->GetAmType() == TYPE_AM_XML)) {
- tp= tdbp;
-// tp->SetMode(xmod);
- } else if ((tp= CntGetTDB(g, table_name, xmod, this))) {
- valid_query_id= xp->last_query_id;
- tp->SetMode(xmod);
- } else
- htrc("GetTDB: %s\n", g->Message);
-
- return tp;
-} // end of GetTDB
-
-/****************************************************************************/
-/* Open a CONNECT table, restricting column list if cols is true. */
-/****************************************************************************/
-int ha_connect::OpenTable(PGLOBAL g, bool del)
-{
- bool rc= false;
- char *c1= NULL, *c2=NULL;
-
- // Double test to be on the safe side
- if (!g || !table) {
- htrc("OpenTable logical error; g=%p table=%p\n", g, table);
- return HA_ERR_INITIALIZATION;
- } // endif g
-
- if (!(tdbp= GetTDB(g)))
- return RC_FX;
- else if (tdbp->IsReadOnly())
- switch (xmod) {
- case MODE_WRITE:
- case MODE_INSERT:
- case MODE_UPDATE:
- case MODE_DELETE:
- strcpy(g->Message, MSG(READ_ONLY));
- return HA_ERR_TABLE_READONLY;
- default:
- break;
- } // endswitch xmode
-
- if (xmod != MODE_INSERT || tdbp->GetAmType() == TYPE_AM_ODBC
- || tdbp->GetAmType() == TYPE_AM_MYSQL) {
- // Get the list of used fields (columns)
- char *p;
- unsigned int k1, k2, n1, n2;
- Field* *field;
- Field* fp;
- MY_BITMAP *map= (xmod == MODE_INSERT) ? table->write_set : table->read_set;
- MY_BITMAP *ump= (xmod == MODE_UPDATE) ? table->write_set : NULL;
-
- k1= k2= 0;
- n1= n2= 1; // 1 is space for final null character
-
- for (field= table->field; fp= *field; field++) {
- if (bitmap_is_set(map, fp->field_index)) {
- n1+= (strlen(fp->field_name) + 1);
- k1++;
- } // endif
-
- if (ump && bitmap_is_set(ump, fp->field_index)) {
- n2+= (strlen(fp->field_name) + 1);
- k2++;
- } // endif
-
- } // endfor field
-
- if (k1) {
- p= c1= (char*)PlugSubAlloc(g, NULL, n1);
-
- for (field= table->field; fp= *field; field++)
- if (bitmap_is_set(map, fp->field_index)) {
- strcpy(p, (char*)fp->field_name);
- p+= (strlen(p) + 1);
- } // endif used field
-
- *p= '\0'; // mark end of list
- } // endif k1
-
- if (k2) {
- p= c2= (char*)PlugSubAlloc(g, NULL, n2);
-
- for (field= table->field; fp= *field; field++)
- if (bitmap_is_set(ump, fp->field_index)) {
- strcpy(p, (char*)fp->field_name);
- p+= (strlen(p) + 1);
- } // endif used field
-
- *p= '\0'; // mark end of list
- } // endif k2
-
- } // endif xmod
-
- // Open the table
- if (!(rc= CntOpenTable(g, tdbp, xmod, c1, c2, del, this))) {
- istable= true;
-// strmake(tname, table_name, sizeof(tname)-1);
-
- // We may be in a create index query
- if (xmod == MODE_ANY && *tdbp->GetName() != '#') {
- // The current indexes
- PIXDEF oldpix= GetIndexInfo();
- } // endif xmod
-
- } else
- htrc("OpenTable: %s\n", g->Message);
-
- if (rc) {
- tdbp= NULL;
- valid_info= false;
- } // endif rc
-
- return (rc) ? HA_ERR_INITIALIZATION : 0;
-} // end of OpenTable
-
-
-/****************************************************************************/
-/* IsOpened: returns true if the table is already opened. */
-/****************************************************************************/
-bool ha_connect::IsOpened(void)
-{
- return (!xp->CheckQuery(valid_query_id) && tdbp
- && tdbp->GetUse() == USE_OPEN);
-} // end of IsOpened
-
-
-/****************************************************************************/
-/* Close a CONNECT table. */
-/****************************************************************************/
-int ha_connect::CloseTable(PGLOBAL g)
-{
- int rc= CntCloseTable(g, tdbp);
- tdbp= NULL;
- sdvalin=NULL;
- sdvalout=NULL;
- valid_info= false;
- indexing= -1;
- return rc;
-} // end of CloseTable
-
-
-/***********************************************************************/
-/* Make a pseudo record from current row values. Specific to MySQL. */
-/***********************************************************************/
-int ha_connect::MakeRecord(char *buf)
-{
- char *p, *fmt, val[32];
- int rc= 0;
- Field* *field;
- Field *fp;
- my_bitmap_map *org_bitmap;
- CHARSET_INFO *charset= tdbp->data_charset();
-//MY_BITMAP readmap;
- MY_BITMAP *map;
- PVAL value;
- PCOL colp= NULL;
- DBUG_ENTER("ha_connect::MakeRecord");
-
- if (xtrace > 1)
- htrc("Maps: read=%08X write=%08X vcol=%08X defr=%08X defw=%08X\n",
- *table->read_set->bitmap, *table->write_set->bitmap,
- *table->vcol_set->bitmap,
- *table->def_read_set.bitmap, *table->def_write_set.bitmap);
-
- // Avoid asserts in field::store() for columns that are not updated
- org_bitmap= dbug_tmp_use_all_columns(table, table->write_set);
-
- // This is for variable_length rows
- memset(buf, 0, table->s->null_bytes);
-
- // When sorting read_set selects all columns, so we use def_read_set
- map= (MY_BITMAP *)&table->def_read_set;
-
- // Make the pseudo record from field values
- for (field= table->field; *field && !rc; field++) {
- fp= *field;
-
- if (fp->vcol_info && !fp->stored_in_db)
- continue; // This is a virtual column
-
- if (bitmap_is_set(map, fp->field_index) || alter) {
- // This is a used field, fill the buffer with value
- for (colp= tdbp->GetColumns(); colp; colp= colp->GetNext())
- if ((!mrr || colp->GetKcol()) &&
- !stricmp(colp->GetName(), (char*)fp->field_name))
- break;
-
- if (!colp) {
- if (mrr)
- continue;
-
- htrc("Column %s not found\n", fp->field_name);
- dbug_tmp_restore_column_map(table->write_set, org_bitmap);
- DBUG_RETURN(HA_ERR_WRONG_IN_RECORD);
- } // endif colp
-
- value= colp->GetValue();
-
- // All this could be better optimized
- if (!value->IsNull()) {
- switch (value->GetType()) {
- case TYPE_DATE:
- if (!sdvalout)
- sdvalout= AllocateValue(xp->g, TYPE_STRING, 20);
-
- switch (fp->type()) {
- case MYSQL_TYPE_DATE:
- fmt= "%Y-%m-%d";
- break;
- case MYSQL_TYPE_TIME:
- fmt= "%H:%M:%S";
- break;
- case MYSQL_TYPE_YEAR:
- fmt= "%Y";
- break;
- default:
- fmt= "%Y-%m-%d %H:%M:%S";
- break;
- } // endswitch type
-
- // Get date in the format required by MySQL fields
- value->FormatValue(sdvalout, fmt);
- p= sdvalout->GetCharValue();
- break;
- case TYPE_DOUBLE:
- p= NULL;
- break;
- case TYPE_STRING:
- // Passthru
- default:
- p= value->GetCharString(val);
- break;
- } // endswitch Type
-
- if (p) {
- if (fp->store(p, strlen(p), charset, CHECK_FIELD_WARN)) {
- // Avoid "error" on null fields
- if (value->GetIntValue())
- rc= HA_ERR_WRONG_IN_RECORD;
-
- DBUG_PRINT("MakeRecord", ("%s", p));
- } // endif store
-
- } else
- if (fp->store(value->GetFloatValue())) {
-// rc= HA_ERR_WRONG_IN_RECORD; a Warning was ignored
- char buf[128];
- THD *thd= ha_thd();
-
- sprintf(buf, "Out of range value for column '%s' at row %ld",
- fp->field_name,
- thd->get_stmt_da()->current_row_for_warning());
-
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, buf);
- DBUG_PRINT("MakeRecord", ("%s", value->GetCharString(val)));
- } // endif store
-
- fp->set_notnull();
- } else
- fp->set_null();
-
- } // endif bitmap
-
- } // endfor field
-
- // This is copied from ha_tina and is necessary to avoid asserts
- dbug_tmp_restore_column_map(table->write_set, org_bitmap);
- DBUG_RETURN(rc);
-} // end of MakeRecord
-
-
-/***********************************************************************/
-/* Set row values from a MySQL pseudo record. Specific to MySQL. */
-/***********************************************************************/
-int ha_connect::ScanRecord(PGLOBAL g, uchar *buf)
-{
- char attr_buffer[1024];
- char data_buffer[1024];
- char *fmt;
- int rc= 0;
- PCOL colp;
- PVAL value;
- Field *fp;
- PTDBASE tp= (PTDBASE)tdbp;
- String attribute(attr_buffer, sizeof(attr_buffer),
- table->s->table_charset);
- my_bitmap_map *bmap= dbug_tmp_use_all_columns(table, table->read_set);
- const CHARSET_INFO *charset= tdbp->data_charset();
- String data_charset_value(data_buffer, sizeof(data_buffer), charset);
-
- // Scan the pseudo record for field values and set column values
- for (Field **field=table->field ; *field ; field++) {
- fp= *field;
-
- if ((fp->vcol_info && !fp->stored_in_db) ||
- fp->option_struct->special)
- continue; // Is a virtual column possible here ???
-
- if ((xmod == MODE_INSERT && tdbp->GetAmType() != TYPE_AM_MYSQL
- && tdbp->GetAmType() != TYPE_AM_ODBC) ||
- bitmap_is_set(table->write_set, fp->field_index)) {
- for (colp= tp->GetSetCols(); colp; colp= colp->GetNext())
- if (!stricmp(colp->GetName(), fp->field_name))
- break;
-
- if (!colp) {
- htrc("Column %s not found\n", fp->field_name);
- rc= HA_ERR_WRONG_IN_RECORD;
- goto err;
- } else
- value= colp->GetValue();
-
- // This is a used field, fill the value from the row buffer
- // All this could be better optimized
- if (fp->is_null()) {
- if (colp->IsNullable())
- value->SetNull(true);
-
- value->Reset();
- } else switch (value->GetType()) {
- case TYPE_DOUBLE:
- value->SetValue(fp->val_real());
- break;
- case TYPE_DATE:
- if (!sdvalin)
- sdvalin= (DTVAL*)AllocateValue(xp->g, TYPE_DATE, 19);
-
- // Get date in the format produced by MySQL fields
- switch (fp->type()) {
- case MYSQL_TYPE_DATE:
- fmt= "YYYY-MM-DD";
- break;
- case MYSQL_TYPE_TIME:
- fmt= "hh:mm:ss";
- break;
- case MYSQL_TYPE_YEAR:
- fmt= "YYYY";
- break;
- default:
- fmt= "YYYY-MM-DD hh:mm:ss";
- } // endswitch type
-
- ((DTVAL*)sdvalin)->SetFormat(g, fmt, strlen(fmt));
- fp->val_str(&attribute);
- sdvalin->SetValue_psz(attribute.c_ptr_safe());
- value->SetValue_pval(sdvalin);
- break;
- default:
- fp->val_str(&attribute);
-
- if (charset != &my_charset_bin) {
- // Convert from SQL field charset to DATA_CHARSET
- uint cnv_errors;
-
- data_charset_value.copy(attribute.ptr(), attribute.length(),
- attribute.charset(), charset, &cnv_errors);
- value->SetValue_psz(data_charset_value.c_ptr_safe());
- } else
- value->SetValue_psz(attribute.c_ptr_safe());
-
- break;
- } // endswitch Type
-
-#ifdef NEWCHANGE
- } else if (xmod == MODE_UPDATE) {
- PCOL cp;
-
- for (cp= tp->GetColumns(); cp; cp= cp->GetNext())
- if (!stricmp(colp->GetName(), cp->GetName()))
- break;
-
- if (!cp) {
- rc= HA_ERR_WRONG_IN_RECORD;
- goto err;
- } // endif cp
-
- value->SetValue_pval(cp->GetValue());
- } else // mode Insert
- value->Reset();
-#else
- } // endif bitmap_is_set
-#endif
-
- } // endfor field
-
- err:
- dbug_tmp_restore_column_map(table->read_set, bmap);
- return rc;
-} // end of ScanRecord
-
-
-/***********************************************************************/
-/* Check change in index column. Specific to MySQL. */
-/* Should be elaborated to check for real changes. */
-/***********************************************************************/
-int ha_connect::CheckRecord(PGLOBAL g, const uchar *oldbuf, uchar *newbuf)
-{
- return ScanRecord(g, newbuf);
-} // end of dummy CheckRecord
-
-
-/***********************************************************************/
-/* Return the string representing an operator. */
-/***********************************************************************/
-const char *ha_connect::GetValStr(OPVAL vop, bool neg)
-{
- const char *val;
-
- switch (vop) {
- case OP_EQ:
- val= " = ";
- break;
- case OP_NE:
- val= " <> ";
- break;
- case OP_GT:
- val= " > ";
- break;
- case OP_GE:
- val= " >= ";
- break;
- case OP_LT:
- val= " < ";
- break;
- case OP_LE:
- val= " <= ";
- break;
- case OP_IN:
- val= (neg) ? " NOT IN (" : " IN (";
- break;
- case OP_NULL:
- val= (neg) ? " IS NOT NULL" : " IS NULL";
- break;
- case OP_LIKE:
- val= " LIKE ";
- break;
- case OP_XX:
- val= (neg) ? " NOT BETWEEN " : " BETWEEN ";
- break;
- case OP_EXIST:
- val= (neg) ? " NOT EXISTS " : " EXISTS ";
- break;
- case OP_AND:
- val= " AND ";
- break;
- case OP_OR:
- val= " OR ";
- break;
- case OP_NOT:
- val= " NOT ";
- break;
- case OP_CNC:
- val= " || ";
- break;
- case OP_ADD:
- val= " + ";
- break;
- case OP_SUB:
- val= " - ";
- break;
- case OP_MULT:
- val= " * ";
- break;
- case OP_DIV:
- val= " / ";
- break;
- default:
- val= " ? ";
- break;
- } /* endswitch */
-
- return val;
-} // end of GetValStr
-
-
-/***********************************************************************/
-/* Check the WHERE condition and return a CONNECT filter. */
-/***********************************************************************/
-PFIL ha_connect::CondFilter(PGLOBAL g, Item *cond)
-{
- unsigned int i;
- bool ismul= false;
- OPVAL vop= OP_XX;
- PFIL filp= NULL;
-
- if (!cond)
- return NULL;
-
- if (xtrace)
- htrc("Cond type=%d\n", cond->type());
-
- if (cond->type() == COND::COND_ITEM) {
- PFIL fp;
- Item_cond *cond_item= (Item_cond *)cond;
-
- if (xtrace)
- htrc("Cond: Ftype=%d name=%s\n", cond_item->functype(),
- cond_item->func_name());
-
- switch (cond_item->functype()) {
- case Item_func::COND_AND_FUNC: vop= OP_AND; break;
- case Item_func::COND_OR_FUNC: vop= OP_OR; break;
- default: return NULL;
- } // endswitch functype
-
- List<Item>* arglist= cond_item->argument_list();
- List_iterator<Item> li(*arglist);
- Item *subitem;
-
- for (i= 0; i < arglist->elements; i++)
- if ((subitem= li++)) {
- if (!(fp= CondFilter(g, subitem))) {
- if (vop == OP_OR)
- return NULL;
- } else
- filp= (filp) ? MakeFilter(g, filp, vop, fp) : fp;
-
- } else
- return NULL;
-
- } else if (cond->type() == COND::FUNC_ITEM) {
- unsigned int i;
- bool iscol, neg= FALSE;
- PCOL colp[2]= {NULL,NULL};
- PPARM pfirst= NULL, pprec= NULL;
- POPER pop;
- Item_func *condf= (Item_func *)cond;
- Item* *args= condf->arguments();
-
- if (xtrace)
- htrc("Func type=%d argnum=%d\n", condf->functype(),
- condf->argument_count());
-
- switch (condf->functype()) {
- case Item_func::EQUAL_FUNC:
- case Item_func::EQ_FUNC: vop= OP_EQ; break;
- case Item_func::NE_FUNC: vop= OP_NE; break;
- case Item_func::LT_FUNC: vop= OP_LT; break;
- case Item_func::LE_FUNC: vop= OP_LE; break;
- case Item_func::GE_FUNC: vop= OP_GE; break;
- case Item_func::GT_FUNC: vop= OP_GT; break;
- case Item_func::IN_FUNC: vop= OP_IN;
- case Item_func::BETWEEN:
- ismul= true;
- neg= ((Item_func_opt_neg *)condf)->negated;
- break;
- default: return NULL;
- } // endswitch functype
-
- pop= (POPER)PlugSubAlloc(g, NULL, sizeof(OPER));
- pop->Name= NULL;
- pop->Val=vop;
- pop->Mod= 0;
-
- if (condf->argument_count() < 2)
- return NULL;
-
- for (i= 0; i < condf->argument_count(); i++) {
- if (xtrace)
- htrc("Argtype(%d)=%d\n", i, args[i]->type());
-
- if (i >= 2 && !ismul) {
- if (xtrace)
- htrc("Unexpected arg for vop=%d\n", vop);
-
- continue;
- } // endif i
-
- if ((iscol= args[i]->type() == COND::FIELD_ITEM)) {
- Item_field *pField= (Item_field *)args[i];
-
- // IN and BETWEEN clauses should be col VOP list
- if (i && ismul)
- return NULL;
-
- if (pField->field->table != table ||
- !(colp[i]= tdbp->ColDB(g, (PSZ)pField->field->field_name, 0)))
- return NULL; // Column does not belong to this table
-
- if (xtrace) {
- htrc("Field index=%d\n", pField->field->field_index);
- htrc("Field name=%s\n", pField->field->field_name);
- } // endif xtrace
-
- } else {
- char buff[256];
- String *res, tmp(buff, sizeof(buff), &my_charset_bin);
- Item_basic_constant *pval= (Item_basic_constant *)args[i];
- PPARM pp= (PPARM)PlugSubAlloc(g, NULL, sizeof(PARM));
-
- // IN and BETWEEN clauses should be col VOP list
- if (!i && (ismul))
- return NULL;
-
- if ((res= pval->val_str(&tmp)) == NULL)
- return NULL; // To be clarified
-
- switch (args[i]->real_type()) {
- case COND::STRING_ITEM:
- pp->Type= TYPE_STRING;
- pp->Value= PlugSubAlloc(g, NULL, res->length() + 1);
- strncpy((char*)pp->Value, res->ptr(), res->length() + 1);
- break;
- case COND::INT_ITEM:
- pp->Type= TYPE_INT;
- pp->Value= PlugSubAlloc(g, NULL, sizeof(int));
- *((int*)pp->Value)= (int)pval->val_int();
- break;
- case COND::DATE_ITEM:
- pp->Type= TYPE_DATE;
- pp->Value= PlugSubAlloc(g, NULL, sizeof(int));
- *((int*)pp->Value)= (int)pval->val_int_from_date();
- break;
- case COND::REAL_ITEM:
- pp->Type= TYPE_DOUBLE;
- pp->Value= PlugSubAlloc(g, NULL, sizeof(double));
- *((double*)pp->Value)= pval->val_real();
- break;
- case COND::DECIMAL_ITEM:
- pp->Type= TYPE_DOUBLE;
- pp->Value= PlugSubAlloc(g, NULL, sizeof(double));
- *((double*)pp->Value)= pval->val_real_from_decimal();
- break;
- case COND::CACHE_ITEM: // Possible ???
- case COND::NULL_ITEM: // TODO: handle this
- default:
- return NULL;
- } // endswitch type
-
- if (xtrace)
- htrc("Value=%.*s\n", res->length(), res->ptr());
-
- // Append the value to the argument list
- if (pprec)
- pprec->Next= pp;
- else
- pfirst= pp;
-
- pp->Domain= i;
- pp->Next= NULL;
- pprec= pp;
- } // endif type
-
- } // endfor i
-
- filp= MakeFilter(g, colp, pop, pfirst, neg);
- } else {
- if (xtrace)
- htrc("Unsupported condition\n");
-
- return NULL;
- } // endif's type
-
- return filp;
-} // end of CondFilter
-
-/***********************************************************************/
-/* Check the WHERE condition and return a MYSQL/ODBC/WQL filter. */
-/***********************************************************************/
-PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
-{
- char *body= filp->Body;
- unsigned int i;
- bool ismul= false, x= (tty == TYPE_AM_MYX || tty == TYPE_AM_XDBC);
- OPVAL vop= OP_XX;
-
- if (!cond)
- return NULL;
-
- if (xtrace)
- htrc("Cond type=%d\n", cond->type());
-
- if (cond->type() == COND::COND_ITEM) {
- char *p1, *p2;
- Item_cond *cond_item= (Item_cond *)cond;
-
- if (x)
- return NULL;
-
- if (xtrace)
- htrc("Cond: Ftype=%d name=%s\n", cond_item->functype(),
- cond_item->func_name());
-
- switch (cond_item->functype()) {
- case Item_func::COND_AND_FUNC: vop= OP_AND; break;
- case Item_func::COND_OR_FUNC: vop= OP_OR; break;
- default: return NULL;
- } // endswitch functype
-
- List<Item>* arglist= cond_item->argument_list();
- List_iterator<Item> li(*arglist);
- Item *subitem;
-
- p1= body + strlen(body);
- strcpy(p1, "(");
- p2= p1 + 1;
-
- for (i= 0; i < arglist->elements; i++)
- if ((subitem= li++)) {
- if (!CheckCond(g, filp, tty, subitem)) {
- if (vop == OP_OR)
- return NULL;
- else
- *p2= 0;
-
- } else {
- p1= p2 + strlen(p2);
- strcpy(p1, GetValStr(vop, FALSE));
- p2= p1 + strlen(p1);
- } // endif CheckCond
-
- } else
- return NULL;
-
- if (*p1 != '(')
- strcpy(p1, ")");
- else
- return NULL;
-
- } else if (cond->type() == COND::FUNC_ITEM) {
- unsigned int i;
-// int n;
- bool iscol, neg= FALSE;
- Item_func *condf= (Item_func *)cond;
- Item* *args= condf->arguments();
-
- if (xtrace)
- htrc("Func type=%d argnum=%d\n", condf->functype(),
- condf->argument_count());
-
-// neg= condf->
-
- switch (condf->functype()) {
- case Item_func::EQUAL_FUNC:
- case Item_func::EQ_FUNC: vop= OP_EQ; break;
- case Item_func::NE_FUNC: vop= OP_NE; break;
- case Item_func::LT_FUNC: vop= OP_LT; break;
- case Item_func::LE_FUNC: vop= OP_LE; break;
- case Item_func::GE_FUNC: vop= OP_GE; break;
- case Item_func::GT_FUNC: vop= OP_GT; break;
- case Item_func::IN_FUNC: vop= OP_IN;
- case Item_func::BETWEEN:
- ismul= true;
- neg= ((Item_func_opt_neg *)condf)->negated;
- break;
- default: return NULL;
- } // endswitch functype
-
- if (condf->argument_count() < 2)
- return NULL;
- else if (ismul && tty == TYPE_AM_WMI)
- return NULL; // Not supported by WQL
-
- if (x && (neg || !(vop == OP_EQ || vop == OP_IN)))
- return NULL;
-
- for (i= 0; i < condf->argument_count(); i++) {
- if (xtrace)
- htrc("Argtype(%d)=%d\n", i, args[i]->type());
-
- if (i >= 2 && !ismul) {
- if (xtrace)
- htrc("Unexpected arg for vop=%d\n", vop);
-
- continue;
- } // endif i
-
- if ((iscol= args[i]->type() == COND::FIELD_ITEM)) {
- const char *fnm;
- ha_field_option_struct *fop;
- Item_field *pField= (Item_field *)args[i];
-
- if (x && i)
- return NULL;
-
- if (pField->field->table != table)
- return NULL; // Field does not belong to this table
- else
- fop= GetFieldOptionStruct(pField->field);
-
- if (fop && fop->special) {
- if (tty == TYPE_AM_TBL && !stricmp(fop->special, "TABID"))
- fnm= "TABID";
- else if (tty == TYPE_AM_PLG)
- fnm= fop->special;
- else
- return NULL;
-
- } else if (tty == TYPE_AM_TBL)
- return NULL;
- else
- fnm= pField->field->field_name;
-
- if (xtrace) {
- htrc("Field index=%d\n", pField->field->field_index);
- htrc("Field name=%s\n", pField->field->field_name);
- } // endif xtrace
-
- // IN and BETWEEN clauses should be col VOP list
- if (i && ismul)
- return NULL;
-
- strcat(body, fnm);
- } else if (args[i]->type() == COND::FUNC_ITEM) {
- if (tty == TYPE_AM_MYSQL) {
- if (!CheckCond(g, filp, tty, args[i]))
- return NULL;
-
- } else
- return NULL;
-
- } else {
- char buff[256];
- String *res, tmp(buff, sizeof(buff), &my_charset_bin);
- Item_basic_constant *pval= (Item_basic_constant *)args[i];
-
- switch (args[i]->real_type()) {
- case COND::STRING_ITEM:
- case COND::INT_ITEM:
- case COND::REAL_ITEM:
- case COND::NULL_ITEM:
- case COND::DECIMAL_ITEM:
- case COND::DATE_ITEM:
- case COND::CACHE_ITEM:
- break;
- default:
- return NULL;
- } // endswitch type
-
- if ((res= pval->val_str(&tmp)) == NULL)
- return NULL; // To be clarified
-
- if (xtrace)
- htrc("Value=%.*s\n", res->length(), res->ptr());
-
- // IN and BETWEEN clauses should be col VOP list
- if (!i && (x || ismul))
- return NULL;
-
- if (!x) {
- // Append the value to the filter
- if (args[i]->field_type() == MYSQL_TYPE_VARCHAR)
- strcat(strcat(strcat(body, "'"), res->ptr()), "'");
- else
- strncat(body, res->ptr(), res->length());
-
- } else {
- if (args[i]->field_type() == MYSQL_TYPE_VARCHAR) {
- // Add the command to the list
- PCMD *ncp, cmdp= new(g) CMD(g, (char*)res->ptr());
-
- for (ncp= &filp->Cmds; *ncp; ncp= &(*ncp)->Next) ;
-
- *ncp= cmdp;
- } else
- return NULL;
-
- } // endif x
-
- } // endif
-
- if (!x) {
- if (!i)
- strcat(body, GetValStr(vop, neg));
- else if (vop == OP_XX && i == 1)
- strcat(body, " AND ");
- else if (vop == OP_IN)
- strcat(body, (i == condf->argument_count() - 1) ? ")" : ",");
-
- } // endif x
-
- } // endfor i
-
- if (x)
- filp->Op= vop;
-
- } else {
- if (xtrace)
- htrc("Unsupported condition\n");
-
- return NULL;
- } // endif's type
-
- return filp;
-} // end of CheckCond
-
-
- /**
- Push condition down to the table handler.
-
- @param cond Condition to be pushed. The condition tree must not be
- modified by the caller.
-
- @return
- The 'remainder' condition that caller must use to filter out records.
- NULL means the handler will not return rows that do not match the
- passed condition.
-
- @note
- CONNECT handles the filtering only for table types that construct
- an SQL or WQL query, but still leaves it to MySQL because only some
- parts of the filter may be relevant.
- The first suballocate finds the position where the string will be
- constructed in the sarea. The second one does make the suballocation
- with the proper length.
- */
-const COND *ha_connect::cond_push(const COND *cond)
-{
- DBUG_ENTER("ha_connect::cond_push");
-
- if (tdbp) {
- PGLOBAL& g= xp->g;
- AMT tty= tdbp->GetAmType();
- bool x= (tty == TYPE_AM_MYX || tty == TYPE_AM_XDBC);
- bool b= (tty == TYPE_AM_WMI || tty == TYPE_AM_ODBC ||
- tty == TYPE_AM_TBL || tty == TYPE_AM_MYSQL ||
- tty == TYPE_AM_PLG || x);
-
- if (b) {
- PCFIL filp= (PCFIL)PlugSubAlloc(g, NULL, sizeof(CONDFIL));
-
- filp->Body= (char*)PlugSubAlloc(g, NULL, (x) ? 128 : 0);
- *filp->Body= 0;
- filp->Op= OP_XX;
- filp->Cmds= NULL;
-
- if (CheckCond(g, filp, tty, (Item *)cond)) {
- if (xtrace)
- htrc("cond_push: %s\n", filp->Body);
-
- if (!x)
- PlugSubAlloc(g, NULL, strlen(filp->Body) + 1);
- else
- cond= NULL; // Does this work?
-
- tdbp->SetCondFil(filp);
- } else if (x && cond)
- tdbp->SetCondFil(filp); // Wrong filter
-
- } else
- tdbp->SetFilter(CondFilter(g, (Item *)cond));
-
- } // endif tdbp
-
- // Let MySQL do the filtering
- DBUG_RETURN(cond);
-} // end of cond_push
-
-/**
- Number of rows in table. It will only be called if
- (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
-*/
-ha_rows ha_connect::records()
-{
- if (!valid_info)
- info(HA_STATUS_VARIABLE);
-
- if (tdbp && tdbp->Cardinality(NULL))
- return stats.records;
- else
- return HA_POS_ERROR;
-
-} // end of records
-
-
-/**
- Return an error message specific to this handler.
-
- @param error error code previously returned by handler
- @param buf pointer to String where to add error message
-
- @return
- Returns true if this is a temporary error
-*/
-bool ha_connect::get_error_message(int error, String* buf)
-{
- DBUG_ENTER("ha_connect::get_error_message");
-
- if (xp && xp->g) {
- PGLOBAL g= xp->g;
- char *msg= (char*)PlugSubAlloc(g, NULL, strlen(g->Message) * 3);
- uint dummy_errors;
- uint32 len= copy_and_convert(msg, strlen(g->Message) * 3,
- system_charset_info,
- g->Message, strlen(g->Message),
- &my_charset_latin1,
- &dummy_errors);
- msg[len]= '\0';
- buf->copy(msg, (uint)strlen(msg), system_charset_info);
- } else
- buf->copy("Cannot retrieve msg", 19, system_charset_info);
-
- DBUG_RETURN(false);
-} // end of get_error_message
-
-
-/**
- @brief
- Used for opening tables. The name will be the name of the file.
-
- @details
- A table is opened when it needs to be opened; e.g. when a request comes in
- for a SELECT on the table (tables are not open and closed for each request,
- they are cached).
-
- Called from handler.cc by handler::ha_open(). The server opens all tables by
- calling ha_open() which then calls the handler specific open().
-
- @note
- For CONNECT no open can be done here because field information is not yet
- updated. >>>>> TO BE CHECKED <<<<<
- (Thread information could be get by using 'ha_thd')
-
- @see
- handler::ha_open() in handler.cc
-*/
-int ha_connect::open(const char *name, int mode, uint test_if_locked)
-{
- int rc= 0;
- DBUG_ENTER("ha_connect::open");
-
- if (xtrace)
- htrc("open: name=%s mode=%d test=%u\n", name, mode, test_if_locked);
-
- if (!(share= get_share()))
- DBUG_RETURN(1);
-
- thr_lock_data_init(&share->lock,&lock,NULL);
-
- // Try to get the user if possible
- xp= GetUser(ha_thd(), xp);
- PGLOBAL g= (xp) ? xp->g : NULL;
-
- // Try to set the database environment
- if (g) {
- rc= (CntCheckDB(g, this, name)) ? (-2) : 0;
-
- if (g->Mrr) {
- // This should only happen for the mrr secondary handler
- mrr= true;
- g->Mrr= false;
- } else
- mrr= false;
-
- } else
- rc= HA_ERR_INTERNAL_ERROR;
-
- DBUG_RETURN(rc);
-} // end of open
-
-/**
- @brief
- Make the indexes for this table
-*/
-int ha_connect::optimize(THD* thd, HA_CHECK_OPT* check_opt)
-{
- int rc= 0;
- PGLOBAL& g= xp->g;
- PDBUSER dup= PlgGetUser(g);
-
- // Ignore error on the opt file
- dup->Check &= ~CHK_OPT;
- tdbp= GetTDB(g);
- dup->Check |= CHK_OPT;
-
- if (tdbp) {
- bool b= ((PTDBASE)tdbp)->GetDef()->Indexable();
-
- if ((rc= ((PTDBASE)tdbp)->ResetTableOpt(g, true, b))) {
- if (rc == RC_INFO) {
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- rc= 0;
- } else
- rc= HA_ERR_INTERNAL_ERROR;
-
- } // endif rc
-
- } else
- rc= HA_ERR_INTERNAL_ERROR;
-
- return rc;
-} // end of optimize
-
-/**
- @brief
- Closes a table.
-
- @details
- Called from sql_base.cc, sql_select.cc, and table.cc. In sql_select.cc it is
- only used to close up temporary tables or during the process where a
- temporary table is converted over to being a myisam table.
-
- For sql_base.cc look at close_data_tables().
-
- @see
- sql_base.cc, sql_select.cc and table.cc
-*/
-int ha_connect::close(void)
-{
- int rc= 0;
- DBUG_ENTER("ha_connect::close");
-
- // If this is called by a later query, the table may have
- // been already closed and the tdbp is not valid anymore.
- if (tdbp && xp->last_query_id == valid_query_id)
- rc= CloseTable(xp->g);
-
- DBUG_RETURN(rc);
-} // end of close
-
-
-/**
- @brief
- write_row() inserts a row. No extra() hint is given currently if a bulk load
- is happening. buf() is a byte array of data. You can use the field
- information to extract the data from the native byte array type.
-
- @details
- Example of this would be:
- @code
- for (Field **field=table->field ; *field ; field++)
- {
- ...
- }
- @endcode
-
- See ha_tina.cc for an example of extracting all of the data as strings.
- ha_berekly.cc has an example of how to store it intact by "packing" it
- for ha_berkeley's own native storage type.
-
- See the note for update_row() on auto_increments and timestamps. This
- case also applies to write_row().
-
- Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
- sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
-
- @see
- item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
- sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc and sql_update.cc
-*/
-int ha_connect::write_row(uchar *buf)
-{
- int rc= 0;
- PGLOBAL& g= xp->g;
- DBUG_ENTER("ha_connect::write_row");
-
- // This is not tested yet
- if (xmod == MODE_ALTER)
- xmod= MODE_INSERT;
-
- // Open the table if it was not opened yet (locked)
- if (!IsOpened() || xmod != tdbp->GetMode()) {
- if (IsOpened())
- CloseTable(g);
-
- if ((rc= OpenTable(g)))
- DBUG_RETURN(rc);
-
- } // endif isopened
-
- if (tdbp->GetMode() == MODE_ANY)
- DBUG_RETURN(0);
-
-#if 0 // AUTO_INCREMENT NIY
- if (table->next_number_field && buf == table->record[0]) {
- int error;
-
- if ((error= update_auto_increment()))
- return error;
-
- } // endif nex_number_field
-#endif // 0
-
- // Set column values from the passed pseudo record
- if ((rc= ScanRecord(g, buf)))
- DBUG_RETURN(rc);
-
- // Return result code from write operation
- if (CntWriteRow(g, tdbp)) {
- DBUG_PRINT("write_row", ("%s", g->Message));
- htrc("write_row: %s\n", g->Message);
- rc= HA_ERR_INTERNAL_ERROR;
- } // endif RC
-
- DBUG_RETURN(rc);
-} // end of write_row
-
-
-/**
- @brief
- Yes, update_row() does what you expect, it updates a row. old_data will have
- the previous row record in it, while new_data will have the newest data in it.
- Keep in mind that the server can do updates based on ordering if an ORDER BY
- clause was used. Consecutive ordering is not guaranteed.
-
- @details
- Currently new_data will not have an updated auto_increament record, or
- and updated timestamp field. You can do these for example by doing:
- @code
- if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
- table->timestamp_field->set_time();
- if (table->next_number_field && record == table->record[0])
- update_auto_increment();
- @endcode
-
- Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
-
- @see
- sql_select.cc, sql_acl.cc, sql_update.cc and sql_insert.cc
-*/
-int ha_connect::update_row(const uchar *old_data, uchar *new_data)
-{
- int rc= 0;
- PGLOBAL& g= xp->g;
- DBUG_ENTER("ha_connect::update_row");
-
- if (xtrace > 1)
- htrc("update_row: old=%s new=%s\n", old_data, new_data);
-
- // Check values for possible change in indexed column
- if ((rc= CheckRecord(g, old_data, new_data)))
- return rc;
-
- if (CntUpdateRow(g, tdbp)) {
- DBUG_PRINT("update_row", ("%s", g->Message));
- htrc("update_row CONNECT: %s\n", g->Message);
- rc= HA_ERR_INTERNAL_ERROR;
- } // endif RC
-
- DBUG_RETURN(rc);
-} // end of update_row
-
-
-/**
- @brief
- This will delete a row. buf will contain a copy of the row to be deleted.
- The server will call this right after the current row has been called (from
- either a previous rnd_nexT() or index call).
-
- @details
- If you keep a pointer to the last row or can access a primary key it will
- make doing the deletion quite a bit easier. Keep in mind that the server does
- not guarantee consecutive deletions. ORDER BY clauses can be used.
-
- Called in sql_acl.cc and sql_udf.cc to manage internal table
- information. Called in sql_delete.cc, sql_insert.cc, and
- sql_select.cc. In sql_select it is used for removing duplicates
- while in insert it is used for REPLACE calls.
-
- @see
- sql_acl.cc, sql_udf.cc, sql_delete.cc, sql_insert.cc and sql_select.cc
-*/
-int ha_connect::delete_row(const uchar *buf)
-{
- int rc= 0;
- DBUG_ENTER("ha_connect::delete_row");
-
- if (CntDeleteRow(xp->g, tdbp, false)) {
- rc= HA_ERR_INTERNAL_ERROR;
- htrc("delete_row CONNECT: %s\n", xp->g->Message);
- } // endif DeleteRow
-
- DBUG_RETURN(rc);
-} // end of delete_row
-
-
-/****************************************************************************/
-/* We seem to come here at the begining of an index use. */
-/****************************************************************************/
-int ha_connect::index_init(uint idx, bool sorted)
-{
- int rc;
- PGLOBAL& g= xp->g;
- DBUG_ENTER("index_init");
-
- if (xtrace)
- htrc("index_init: this=%p idx=%u sorted=%d\n", this, idx, sorted);
-
- if ((rc= rnd_init(0)))
- return rc;
-
- if (locked == 2) {
- // Indexes are not updated in lock write mode
- active_index= MAX_KEY;
- indexing= 0;
- DBUG_RETURN(0);
- } // endif locked
-
- indexing= CntIndexInit(g, tdbp, (signed)idx);
-
- if (indexing <= 0) {
- DBUG_PRINT("index_init", ("%s", g->Message));
- htrc("index_init CONNECT: %s\n", g->Message);
- active_index= MAX_KEY;
- rc= HA_ERR_INTERNAL_ERROR;
- } else {
- if (((PTDBDOX)tdbp)->To_Kindex->GetNum_K()) {
- if (((PTDBASE)tdbp)->GetFtype() != RECFM_NAF)
- ((PTDBDOX)tdbp)->GetTxfp()->ResetBuffer(g);
-
- active_index= idx;
- } else // Void table
- indexing= 0;
-
- rc= 0;
- } // endif indexing
-
- if (xtrace)
- htrc("index_init: rc=%d indexing=%d active_index=%d\n",
- rc, indexing, active_index);
-
- DBUG_RETURN(rc);
-} // end of index_init
-
-/****************************************************************************/
-/* We seem to come here at the end of an index use. */
-/****************************************************************************/
-int ha_connect::index_end()
-{
- DBUG_ENTER("index_end");
- active_index= MAX_KEY;
- ds_mrr.dsmrr_close();
- DBUG_RETURN(rnd_end());
-} // end of index_end
-
-
-/****************************************************************************/
-/* This is internally called by all indexed reading functions. */
-/****************************************************************************/
-int ha_connect::ReadIndexed(uchar *buf, OPVAL op, const uchar *key, uint key_len)
-{
- int rc;
-
-//statistic_increment(ha_read_key_count, &LOCK_status);
-
- switch (CntIndexRead(xp->g, tdbp, op, key, (int)key_len, mrr)) {
- case RC_OK:
- xp->fnd++;
- rc= MakeRecord((char*)buf);
- break;
- case RC_EF: // End of file
- rc= HA_ERR_END_OF_FILE;
- break;
- case RC_NF: // Not found
- xp->nfd++;
- rc= (op == OP_SAME) ? HA_ERR_END_OF_FILE : HA_ERR_KEY_NOT_FOUND;
- break;
- default: // Read error
- DBUG_PRINT("ReadIndexed", ("%s", xp->g->Message));
- htrc("ReadIndexed: %s\n", xp->g->Message);
- rc= HA_ERR_INTERNAL_ERROR;
- break;
- } // endswitch RC
-
- if (xtrace > 1)
- htrc("ReadIndexed: op=%d rc=%d\n", op, rc);
-
- table->status= (rc == RC_OK) ? 0 : STATUS_NOT_FOUND;
- return rc;
-} // end of ReadIndexed
-
-
-#ifdef NOT_USED
-/**
- @brief
- Positions an index cursor to the index specified in the handle. Fetches the
- row if available. If the key value is null, begin at the first key of the
- index.
-*/
-int ha_connect::index_read_map(uchar *buf, const uchar *key,
- key_part_map keypart_map __attribute__((unused)),
- enum ha_rkey_function find_flag
- __attribute__((unused)))
-{
- DBUG_ENTER("ha_connect::index_read");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
-}
-#endif // NOT_USED
-
-
-/****************************************************************************/
-/* This is called by handler::index_read_map. */
-/****************************************************************************/
-int ha_connect::index_read(uchar * buf, const uchar * key, uint key_len,
- enum ha_rkey_function find_flag)
-{
- int rc;
- OPVAL op= OP_XX;
- DBUG_ENTER("ha_connect::index_read");
-
- switch(find_flag) {
- case HA_READ_KEY_EXACT: op= OP_EQ; break;
- case HA_READ_AFTER_KEY: op= OP_GT; break;
- case HA_READ_KEY_OR_NEXT: op= OP_GE; break;
- default: DBUG_RETURN(-1); break;
- } // endswitch find_flag
-
- if (xtrace > 1)
- htrc("%p index_read: op=%d\n", this, op);
-
- if (indexing > 0)
- rc= ReadIndexed(buf, op, key, key_len);
- else
- rc= HA_ERR_INTERNAL_ERROR;
-
- DBUG_RETURN(rc);
-} // end of index_read
-
-
-/**
- @brief
- Used to read forward through the index.
-*/
-int ha_connect::index_next(uchar *buf)
-{
- int rc;
- DBUG_ENTER("ha_connect::index_next");
- //statistic_increment(ha_read_next_count, &LOCK_status);
-
- if (indexing > 0)
- rc= ReadIndexed(buf, OP_NEXT);
- else if (!indexing)
- rc= rnd_next(buf);
- else
- rc= HA_ERR_INTERNAL_ERROR;
-
- DBUG_RETURN(rc);
-} // end of index_next
-
-
-#ifdef NOT_USED
-/**
- @brief
- Used to read backwards through the index.
-*/
-int ha_connect::index_prev(uchar *buf)
-{
- DBUG_ENTER("ha_connect::index_prev");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
-}
-#endif // NOT_USED
-
-
-/**
- @brief
- index_first() asks for the first key in the index.
-
- @details
- Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
-
- @see
- opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
-*/
-int ha_connect::index_first(uchar *buf)
-{
- int rc;
- DBUG_ENTER("ha_connect::index_first");
-
- if (indexing > 0)
- rc= ReadIndexed(buf, OP_FIRST);
- else if (indexing < 0)
- rc= HA_ERR_INTERNAL_ERROR;
- else if (CntRewindTable(xp->g, tdbp)) {
- table->status= STATUS_NOT_FOUND;
- rc= HA_ERR_INTERNAL_ERROR;
- } else
- rc= rnd_next(buf);
-
- DBUG_RETURN(rc);
-} // end of index_first
-
-
-#ifdef NOT_USED
-/**
- @brief
- index_last() asks for the last key in the index.
-
- @details
- Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
-
- @see
- opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
-*/
-int ha_connect::index_last(uchar *buf)
-{
- DBUG_ENTER("ha_connect::index_last");
- DBUG_RETURN(HA_ERR_WRONG_COMMAND);
-}
-#endif // NOT_USED
-
-
-/****************************************************************************/
-/* This is called to get more rows having the same index value. */
-/****************************************************************************/
-int ha_connect::index_next_same(uchar *buf, const uchar *key, uint keylen)
-{
- int rc;
- DBUG_ENTER("ha_connect::index_next_same");
-//statistic_increment(ha_read_next_count, &LOCK_status);
-
- if (!indexing)
- rc= rnd_next(buf);
- else if (indexing > 0)
- rc= ReadIndexed(buf, OP_SAME);
- else
- rc= HA_ERR_INTERNAL_ERROR;
-
- DBUG_RETURN(rc);
-} // end of index_next_same
-
-
-/**
- @brief
- rnd_init() is called when the system wants the storage engine to do a table
- scan. See the example in the introduction at the top of this file to see when
- rnd_init() is called.
-
- @details
- Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
- and sql_update.cc.
-
- @note
- We always call open and extern_lock/start_stmt before comming here.
-
- @see
- filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
-*/
-int ha_connect::rnd_init(bool scan)
-{
- int rc;
- PGLOBAL g= ((table && table->in_use) ? GetPlug(table->in_use, xp) :
- (xp) ? xp->g : NULL);
- DBUG_ENTER("ha_connect::rnd_init");
-
- // This is not tested yet
- if (xmod == MODE_ALTER) {
- xmod= MODE_READ;
- alter= 1;
- } // endif xmod
-
- if (xtrace)
- htrc("rnd_init: this=%p scan=%d xmod=%d alter=%d\n",
- this, scan, xmod, alter);
-
- if (!g || !table || xmod == MODE_INSERT)
- DBUG_RETURN(HA_ERR_INITIALIZATION);
-
- // Do not close the table if it was opened yet (locked?)
- if (IsOpened())
- DBUG_RETURN(0);
-// CloseTable(g); Was done before making things done twice
- else if (xp->CheckQuery(valid_query_id))
- tdbp= NULL; // Not valid anymore
-
- // When updating, to avoid skipped update, force the table
- // handler to retrieve write-only fields to be able to compare
- // records and detect data change.
- if (xmod == MODE_UPDATE)
- bitmap_union(table->read_set, table->write_set);
-
- if ((rc= OpenTable(g, xmod == MODE_DELETE)))
- DBUG_RETURN(rc);
-
- xp->nrd= xp->fnd= xp->nfd= 0;
- xp->tb1= my_interval_timer();
- DBUG_RETURN(0);
-} // end of rnd_init
-
-/**
- @brief
- Not described.
-
- @note
- The previous version said:
- Stop scanning of table. Note that this may be called several times during
- execution of a sub select.
- =====> This has been moved to external lock to avoid closing subselect tables.
-*/
-int ha_connect::rnd_end()
-{
- int rc= 0;
- DBUG_ENTER("ha_connect::rnd_end");
-
- // If this is called by a later query, the table may have
- // been already closed and the tdbp is not valid anymore.
-// if (tdbp && xp->last_query_id == valid_query_id)
-// rc= CloseTable(xp->g);
-
- ds_mrr.dsmrr_close();
- DBUG_RETURN(rc);
-} // end of rnd_end
-
-
-/**
- @brief
- This is called for each row of the table scan. When you run out of records
- you should return HA_ERR_END_OF_FILE. Fill buff up with the row information.
- The Field structure for the table is the key to getting data into buf
- in a manner that will allow the server to understand it.
-
- @details
- Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
- and sql_update.cc.
-
- @see
- filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
-*/
-int ha_connect::rnd_next(uchar *buf)
-{
- int rc;
- DBUG_ENTER("ha_connect::rnd_next");
-//statistic_increment(ha_read_rnd_next_count, &LOCK_status);
-
- if (tdbp->GetMode() == MODE_ANY) {
- // We will stop on next read
- if (!stop) {
- stop= true;
- DBUG_RETURN(RC_OK);
- } else
- DBUG_RETURN(HA_ERR_END_OF_FILE);
-
- } // endif Mode
-
- switch (CntReadNext(xp->g, tdbp)) {
- case RC_OK:
- rc= MakeRecord((char*)buf);
- break;
- case RC_EF: // End of file
- rc= HA_ERR_END_OF_FILE;
- break;
- case RC_NF: // Not found
- rc= HA_ERR_RECORD_DELETED;
- break;
- default: // Read error
- htrc("rnd_next CONNECT: %s\n", xp->g->Message);
- rc= (records()) ? HA_ERR_INTERNAL_ERROR : HA_ERR_END_OF_FILE;
- break;
- } // endswitch RC
-
- if (xtrace > 1 && (rc || !(xp->nrd++ % 16384))) {
- ulonglong tb2= my_interval_timer();
- double elapsed= (double) (tb2 - xp->tb1) / 1000000000ULL;
- DBUG_PRINT("rnd_next", ("rc=%d nrd=%u fnd=%u nfd=%u sec=%.3lf\n",
- rc, (uint)xp->nrd, (uint)xp->fnd,
- (uint)xp->nfd, elapsed));
- htrc("rnd_next: rc=%d nrd=%u fnd=%u nfd=%u sec=%.3lf\n",
- rc, (uint)xp->nrd, (uint)xp->fnd,
- (uint)xp->nfd, elapsed);
- xp->tb1= tb2;
- xp->fnd= xp->nfd= 0;
- } // endif nrd
-
- table->status= (!rc) ? 0 : STATUS_NOT_FOUND;
- DBUG_RETURN(rc);
-} // end of rnd_next
-
-
-/**
- @brief
- position() is called after each call to rnd_next() if the data needs
- to be ordered. You can do something like the following to store
- the position:
- @code
- my_store_ptr(ref, ref_length, current_position);
- @endcode
-
- @details
- The server uses ref to store data. ref_length in the above case is
- the size needed to store current_position. ref is just a byte array
- that the server will maintain. If you are using offsets to mark rows, then
- current_position should be the offset. If it is a primary key like in
- BDB, then it needs to be a primary key.
-
- Called from filesort.cc, sql_select.cc, sql_delete.cc, and sql_update.cc.
-
- @see
- filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc
-*/
-void ha_connect::position(const uchar *record)
-{
- DBUG_ENTER("ha_connect::position");
-//if (((PTDBASE)tdbp)->GetDef()->Indexable())
- my_store_ptr(ref, ref_length, (my_off_t)((PTDBASE)tdbp)->GetRecpos());
- DBUG_VOID_RETURN;
-} // end of position
-
-
-/**
- @brief
- This is like rnd_next, but you are given a position to use
- to determine the row. The position will be of the type that you stored in
- ref. You can use my_get_ptr(pos,ref_length) to retrieve whatever key
- or position you saved when position() was called.
-
- @details
- Called from filesort.cc, records.cc, sql_insert.cc, sql_select.cc, and sql_update.cc.
-
- @note
- Is this really useful? It was never called even when sorting.
-
- @see
- filesort.cc, records.cc, sql_insert.cc, sql_select.cc and sql_update.cc
-*/
-int ha_connect::rnd_pos(uchar *buf, uchar *pos)
-{
- int rc;
- PTDBASE tp= (PTDBASE)tdbp;
- DBUG_ENTER("ha_connect::rnd_pos");
-
- if (!tp->SetRecpos(xp->g, (int)my_get_ptr(pos, ref_length)))
- rc= rnd_next(buf);
- else
- rc= HA_ERR_KEY_NOT_FOUND;
-
- DBUG_RETURN(rc);
-} // end of rnd_pos
-
-
-/**
- @brief
- ::info() is used to return information to the optimizer. See my_base.h for
- the complete description.
-
- @details
- Currently this table handler doesn't implement most of the fields really needed.
- SHOW also makes use of this data.
-
- You will probably want to have the following in your code:
- @code
- if (records < 2)
- records= 2;
- @endcode
- The reason is that the server will optimize for cases of only a single
- record. If, in a table scan, you don't know the number of records, it
- will probably be better to set records to two so you can return as many
- records as you need. Along with records, a few more variables you may wish
- to set are:
- records
- deleted
- data_file_length
- index_file_length
- delete_length
- check_time
- Take a look at the public variables in handler.h for more information.
-
- Called in filesort.cc, ha_heap.cc, item_sum.cc, opt_sum.cc, sql_delete.cc,
- sql_delete.cc, sql_derived.cc, sql_select.cc, sql_select.cc, sql_select.cc,
- sql_select.cc, sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc,
- sql_table.cc, sql_union.cc, and sql_update.cc.
-
- @see
- filesort.cc, ha_heap.cc, item_sum.cc, opt_sum.cc, sql_delete.cc, sql_delete.cc,
- sql_derived.cc, sql_select.cc, sql_select.cc, sql_select.cc, sql_select.cc,
- sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_table.cc,
- sql_union.cc and sql_update.cc
-*/
-int ha_connect::info(uint flag)
-{
- bool pure= false;
- PGLOBAL g= GetPlug((table) ? table->in_use : NULL, xp);
-
- DBUG_ENTER("ha_connect::info");
-
- if (xtrace)
- htrc("%p In info: flag=%u valid_info=%d\n", this, flag, valid_info);
-
- if (!valid_info) {
- // tdbp must be available to get updated info
- if (xp->CheckQuery(valid_query_id) || !tdbp) {
- if (xmod == MODE_ANY || xmod == MODE_ALTER) {
- // Pure info, not a query
- pure= true;
- xp->CheckCleanup();
- } // endif xmod
-
- tdbp= GetTDB(g);
- } // endif tdbp
-
- valid_info= CntInfo(g, tdbp, &xinfo);
- } // endif valid_info
-
- if (flag & HA_STATUS_VARIABLE) {
- stats.records= xinfo.records;
- stats.deleted= 0;
- stats.data_file_length= xinfo.data_file_length;
- stats.index_file_length= 0;
- stats.delete_length= 0;
- stats.check_time= 0;
- stats.mean_rec_length= xinfo.mean_rec_length;
- } // endif HA_STATUS_VARIABLE
-
- if (flag & HA_STATUS_CONST) {
- // This is imported from the previous handler and must be reconsidered
- stats.max_data_file_length= 4294967295;
- stats.max_index_file_length= 4398046510080;
- stats.create_time= 0;
- data_file_name= xinfo.data_file_name;
- index_file_name= NULL;
-// sortkey= (uint) - 1; // Table is not sorted
- ref_length= sizeof(int); // Pointer size to row
- table->s->db_options_in_use= 03;
- stats.block_size= 1024;
- table->s->keys_in_use.set_prefix(table->s->keys);
- table->s->keys_for_keyread= table->s->keys_in_use;
-// table->s->keys_for_keyread.subtract(table->s->read_only_keys);
- table->s->db_record_offset= 0;
- } // endif HA_STATUS_CONST
-
- if (flag & HA_STATUS_ERRKEY) {
- errkey= 0;
- } // endif HA_STATUS_ERRKEY
-
- if (flag & HA_STATUS_TIME)
- stats.update_time= 0;
-
- if (flag & HA_STATUS_AUTO)
- stats.auto_increment_value= 1;
-
- if (tdbp && pure)
- CloseTable(g); // Not used anymore
-
- DBUG_RETURN(0);
-} // end of info
-
-
-/**
- @brief
- extra() is called whenever the server wishes to send a hint to
- the storage engine. The myisam engine implements the most hints.
- ha_innodb.cc has the most exhaustive list of these hints.
-
- @note
- This is not yet implemented for CONNECT.
-
- @see
- ha_innodb.cc
-*/
-int ha_connect::extra(enum ha_extra_function operation)
-{
- DBUG_ENTER("ha_connect::extra");
- DBUG_RETURN(0);
-} // end of extra
-
-
-/**
- @brief
- Used to delete all rows in a table, including cases of truncate and cases where
- the optimizer realizes that all rows will be removed as a result of an SQL statement.
-
- @details
- Called from item_sum.cc by Item_func_group_concat::clear(),
- Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
- Called from sql_delete.cc by mysql_delete().
- Called from sql_select.cc by JOIN::reinit().
- Called from sql_union.cc by st_select_lex_unit::exec().
-
- @see
- Item_func_group_concat::clear(), Item_sum_count_distinct::clear() and
- Item_func_group_concat::clear() in item_sum.cc;
- mysql_delete() in sql_delete.cc;
- JOIN::reinit() in sql_select.cc and
- st_select_lex_unit::exec() in sql_union.cc.
-*/
-int ha_connect::delete_all_rows()
-{
- int rc= 0;
- PGLOBAL g= xp->g;
- DBUG_ENTER("ha_connect::delete_all_rows");
-
- if (tdbp && tdbp->GetUse() == USE_OPEN &&
- tdbp->GetAmType() != TYPE_AM_XML &&
- ((PTDBASE)tdbp)->GetFtype() != RECFM_NAF)
- // Close and reopen the table so it will be deleted
- rc= CloseTable(g);
-
- if (!(rc= OpenTable(g))) {
- if (CntDeleteRow(g, tdbp, true)) {
- htrc("%s\n", g->Message);
- rc= HA_ERR_INTERNAL_ERROR;
- } // endif
-
- } // endif rc
-
- DBUG_RETURN(rc);
-} // end of delete_all_rows
-
-
-bool ha_connect::check_privileges(THD *thd, PTOS options, char *dbn)
-{
- const char *db= (dbn && *dbn) ? dbn : NULL;
- TABTYPE type=GetRealType(options);
-
- switch (type) {
- case TAB_UNDEF:
-// case TAB_CATLG:
- case TAB_PLG:
- case TAB_JCT:
- case TAB_DMY:
- case TAB_NIY:
- my_printf_error(ER_UNKNOWN_ERROR,
- "Unsupported table type %s", MYF(0), options->type);
- return true;
-
- case TAB_DOS:
- case TAB_FIX:
- case TAB_BIN:
- case TAB_CSV:
- case TAB_FMT:
- case TAB_DBF:
- case TAB_XML:
- case TAB_INI:
- case TAB_VEC:
- if (options->filename && *options->filename) {
- char *s, path[FN_REFLEN], dbpath[FN_REFLEN];
-#if defined(WIN32)
- s= "\\";
-#else // !WIN32
- s= "/";
-#endif // !WIN32
- strcpy(dbpath, mysql_real_data_home);
-
- if (db)
- strcat(strcat(dbpath, db), s);
-
- (void) fn_format(path, options->filename, dbpath, "",
- MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
-
- if (!is_secure_file_path(path)) {
- my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
- return true;
- } // endif path
-
- } else
- return false;
-
- /* Fall through to check FILE_ACL */
- case TAB_ODBC:
- case TAB_MYSQL:
- case TAB_DIR:
- case TAB_MAC:
- case TAB_WMI:
- case TAB_OEM:
- return check_access(thd, FILE_ACL, db, NULL, NULL, 0, 0);
-
- // This is temporary until a solution is found
- case TAB_TBL:
- case TAB_XCL:
- case TAB_PRX:
- case TAB_OCCUR:
- case TAB_PIVOT:
- return false;
- } // endswitch type
-
- my_printf_error(ER_UNKNOWN_ERROR, "check_privileges failed", MYF(0));
- return true;
-} // end of check_privileges
-
-// Check that two indexes are equivalent
-bool ha_connect::IsSameIndex(PIXDEF xp1, PIXDEF xp2)
-{
- bool b= true;
- PKPDEF kp1, kp2;
-
- if (stricmp(xp1->Name, xp2->Name))
- b= false;
- else if (xp1->Nparts != xp2->Nparts ||
- xp1->MaxSame != xp2->MaxSame ||
- xp1->Unique != xp2->Unique)
- b= false;
- else for (kp1= xp1->ToKeyParts, kp2= xp2->ToKeyParts;
- b && (kp1 || kp2);
- kp1= kp1->Next, kp2= kp2->Next)
- if (!kp1 || !kp2)
- b= false;
- else if (stricmp(kp1->Name, kp2->Name))
- b= false;
- else if (kp1->Klen != kp2->Klen)
- b= false;
-
- return b;
-} // end of IsSameIndex
-
-MODE ha_connect::CheckMode(PGLOBAL g, THD *thd,
- MODE newmode, bool *chk, bool *cras)
-{
- if (xtrace) {
- LEX_STRING *query_string= thd_query_string(thd);
- htrc("%p check_mode: cmdtype=%d\n", this, thd_sql_command(thd));
- htrc("Cmd=%.*s\n", (int) query_string->length, query_string->str);
- } // endif xtrace
-
- // Next code is temporarily replaced until sql_command is set
- stop= false;
-
- if (newmode == MODE_WRITE) {
- switch (thd_sql_command(thd)) {
- case SQLCOM_LOCK_TABLES:
- locked= 2;
- case SQLCOM_CREATE_TABLE:
- case SQLCOM_INSERT:
- case SQLCOM_LOAD:
- case SQLCOM_INSERT_SELECT:
- newmode= MODE_INSERT;
- break;
-// case SQLCOM_REPLACE:
-// case SQLCOM_REPLACE_SELECT:
-// newmode= MODE_UPDATE; // To be checked
-// break;
- case SQLCOM_DELETE:
- case SQLCOM_DELETE_MULTI:
- case SQLCOM_TRUNCATE:
- newmode= MODE_DELETE;
- break;
- case SQLCOM_UPDATE:
- case SQLCOM_UPDATE_MULTI:
- newmode= MODE_UPDATE;
- break;
- case SQLCOM_SELECT:
- case SQLCOM_OPTIMIZE:
- newmode= MODE_READ;
- break;
- case SQLCOM_DROP_TABLE:
- case SQLCOM_RENAME_TABLE:
- newmode= MODE_ANY;
- break;
- case SQLCOM_DROP_INDEX:
- case SQLCOM_CREATE_INDEX:
- newmode= MODE_ANY;
-// stop= true;
- break;
- case SQLCOM_CREATE_VIEW:
- case SQLCOM_DROP_VIEW:
- newmode= MODE_ANY;
- break;
- case SQLCOM_ALTER_TABLE:
- newmode= MODE_ALTER;
- break;
- default:
- htrc("Unsupported sql_command=%d", thd_sql_command(thd));
- strcpy(g->Message, "CONNECT Unsupported command");
- my_message(ER_NOT_ALLOWED_COMMAND, g->Message, MYF(0));
- newmode= MODE_ERROR;
- break;
- } // endswitch newmode
-
- } else if (newmode == MODE_READ) {
- switch (thd_sql_command(thd)) {
- case SQLCOM_CREATE_TABLE:
- *chk= true;
- *cras= true;
- case SQLCOM_INSERT:
- case SQLCOM_LOAD:
- case SQLCOM_INSERT_SELECT:
-// case SQLCOM_REPLACE:
-// case SQLCOM_REPLACE_SELECT:
- case SQLCOM_DELETE:
- case SQLCOM_DELETE_MULTI:
- case SQLCOM_TRUNCATE:
- case SQLCOM_UPDATE:
- case SQLCOM_UPDATE_MULTI:
- case SQLCOM_SELECT:
- case SQLCOM_OPTIMIZE:
- break;
- case SQLCOM_LOCK_TABLES:
- locked= 1;
- break;
- case SQLCOM_DROP_INDEX:
- case SQLCOM_CREATE_INDEX:
- *chk= true;
-// stop= true;
- case SQLCOM_DROP_TABLE:
- case SQLCOM_RENAME_TABLE:
- newmode= MODE_ANY;
- break;
- case SQLCOM_CREATE_VIEW:
- case SQLCOM_DROP_VIEW:
- newmode= MODE_ANY;
- break;
- case SQLCOM_ALTER_TABLE:
- *chk= true;
- newmode= MODE_ALTER;
- break;
- default:
- htrc("Unsupported sql_command=%d", thd_sql_command(thd));
- strcpy(g->Message, "CONNECT Unsupported command");
- my_message(ER_NOT_ALLOWED_COMMAND, g->Message, MYF(0));
- newmode= MODE_ERROR;
- break;
- } // endswitch newmode
-
- } // endif's newmode
-
- if (xtrace)
- htrc("New mode=%d\n", newmode);
-
- return newmode;
-} // end of check_mode
-
-int ha_connect::start_stmt(THD *thd, thr_lock_type lock_type)
-{
- int rc= 0;
- bool chk=false, cras= false;
- MODE newmode;
- PGLOBAL g= GetPlug(thd, xp);
- DBUG_ENTER("ha_connect::start_stmt");
-
- // Action will depend on lock_type
- switch (lock_type) {
- case TL_WRITE_ALLOW_WRITE:
- case TL_WRITE_CONCURRENT_INSERT:
- case TL_WRITE_DELAYED:
- case TL_WRITE_DEFAULT:
- case TL_WRITE_LOW_PRIORITY:
- case TL_WRITE:
- case TL_WRITE_ONLY:
- newmode= MODE_WRITE;
- break;
- case TL_READ:
- case TL_READ_WITH_SHARED_LOCKS:
- case TL_READ_HIGH_PRIORITY:
- case TL_READ_NO_INSERT:
- case TL_READ_DEFAULT:
- newmode= MODE_READ;
- break;
- case TL_UNLOCK:
- default:
- newmode= MODE_ANY;
- break;
- } // endswitch mode
-
- xmod= CheckMode(g, thd, newmode, &chk, &cras);
- DBUG_RETURN((xmod == MODE_ERROR) ? HA_ERR_INTERNAL_ERROR : 0);
-} // end of start_stmt
-
-/**
- @brief
- This create a lock on the table. If you are implementing a storage engine
- that can handle transacations look at ha_berkely.cc to see how you will
- want to go about doing this. Otherwise you should consider calling flock()
- here. Hint: Read the section "locking functions for mysql" in lock.cc to understand
- this.
-
- @details
- Called from lock.cc by lock_external() and unlock_external(). Also called
- from sql_table.cc by copy_data_between_tables().
-
- @note
- Following what we did in the MySQL XDB handler, we use this call to actually
- physically open the table. This could be reconsider when finalizing this handler
- design, which means we have a better understanding of what MariaDB does.
-
- @see
- lock.cc by lock_external() and unlock_external() in lock.cc;
- the section "locking functions for mysql" in lock.cc;
- copy_data_between_tables() in sql_table.cc.
-*/
-int ha_connect::external_lock(THD *thd, int lock_type)
-{
- int rc= 0;
- bool xcheck=false, cras= false;
- MODE newmode;
- PTOS options= GetTableOptionStruct(table);
- PGLOBAL g= GetPlug(thd, xp);
- DBUG_ENTER("ha_connect::external_lock");
-
- DBUG_ASSERT(thd == current_thd);
-
- if (xtrace)
- htrc("external_lock: this=%p thd=%p xp=%p g=%p lock_type=%d\n",
- this, thd, xp, g, lock_type);
-
- if (!g)
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
-
- // Action will depend on lock_type
- switch (lock_type) {
- case F_WRLCK:
- newmode= MODE_WRITE;
- break;
- case F_RDLCK:
- newmode= MODE_READ;
- break;
- case F_UNLCK:
- default:
- newmode= MODE_ANY;
- break;
- } // endswitch mode
-
- if (newmode == MODE_ANY) {
- int sqlcom= thd_sql_command(thd);
-
- // This is unlocking, do it by closing the table
- if (xp->CheckQueryID() && sqlcom != SQLCOM_UNLOCK_TABLES
- && sqlcom != SQLCOM_LOCK_TABLES)
- rc= 2; // Logical error ???
-// else if (g->Xchk && (sqlcom == SQLCOM_CREATE_INDEX ||
-// sqlcom == SQLCOM_DROP_INDEX)) {
- else if (g->Xchk) {
- if (!tdbp) {
- if (!(tdbp= GetTDB(g)))
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- else if (!((PTDBASE)tdbp)->GetDef()->Indexable()) {
- sprintf(g->Message, "external_lock: Table %s is not indexable", tdbp->GetName());
-// DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- DBUG_RETURN(0);
- } // endif Indexable
-
- bool oldsep= ((PCHK)g->Xchk)->oldsep;
- bool newsep= ((PCHK)g->Xchk)->newsep;
- PTDBDOS tdp= (PTDBDOS)tdbp;
-
- PDOSDEF ddp= (PDOSDEF)tdp->GetDef();
- PIXDEF xp, xp1, xp2, drp=NULL, adp= NULL;
- PIXDEF oldpix= ((PCHK)g->Xchk)->oldpix;
- PIXDEF newpix= ((PCHK)g->Xchk)->newpix;
- PIXDEF *xlst, *xprc;
-
- ddp->SetIndx(oldpix);
-
- if (oldsep != newsep) {
- // All indexes have to be remade
- ddp->DeleteIndexFile(g, NULL);
- oldpix= NULL;
- ddp->SetIndx(NULL);
- SetBooleanOption("Sepindex", newsep);
- } else if (newsep) {
- // Make the list of dropped indexes
- xlst= &drp; xprc= &oldpix;
-
- for (xp2= oldpix; xp2; xp2= xp) {
- for (xp1= newpix; xp1; xp1= xp1->Next)
- if (IsSameIndex(xp1, xp2))
- break; // Index not to drop
-
- xp= xp2->GetNext();
-
- if (!xp1) {
- *xlst= xp2;
- *xprc= xp;
- *(xlst= &xp2->Next)= NULL;
- } else
- xprc= &xp2->Next;
-
- } // endfor xp2
-
- if (drp) {
- // Here we erase the index files
- ddp->DeleteIndexFile(g, drp);
- } // endif xp1
-
- } else if (oldpix) {
- // TODO: optimize the case of just adding new indexes
- if (!newpix)
- ddp->DeleteIndexFile(g, NULL);
-
- oldpix= NULL; // To remake all indexes
- ddp->SetIndx(NULL);
- } // endif sepindex
-
- // Make the list of new created indexes
- xlst= &adp; xprc= &newpix;
-
- for (xp1= newpix; xp1; xp1= xp) {
- for (xp2= oldpix; xp2; xp2= xp2->Next)
- if (IsSameIndex(xp1, xp2))
- break; // Index already made
-
- xp= xp1->Next;
-
- if (!xp2) {
- *xlst= xp1;
- *xprc= xp;
- *(xlst= &xp1->Next)= NULL;
- } else
- xprc= &xp1->Next;
-
- } // endfor xp1
-
- if (adp)
- // Here we do make the new indexes
- if (tdp->MakeIndex(g, adp, true) == RC_FX) {
- // Make it a warning to avoid crash
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
- 0, g->Message);
- rc= 0;
- } // endif MakeIndex
-
- } // endif Tdbp
-
- } // endelse Xchk
-
- if (CloseTable(g)) {
- // This is an error while builing index
- // Make it a warning to avoid crash
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- rc= 0;
- } // endif Close
-
- locked= 0;
- DBUG_RETURN(rc);
- } // endif MODE_ANY
-
- DBUG_ASSERT(table && table->s);
-
- if (check_privileges(thd, options, table->s->db.str)) {
- strcpy(g->Message, "This operation requires the FILE privilege");
- htrc("%s\n", g->Message);
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif check_privileges
-
- // Table mode depends on the query type
- newmode= CheckMode(g, thd, newmode, &xcheck, &cras);
-
- if (newmode == MODE_ERROR)
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
-
- // If this is the start of a new query, cleanup the previous one
- if (xp->CheckCleanup()) {
- tdbp= NULL;
- valid_info= false;
- } // endif CheckCleanup
-
-#if 0
- if (xcheck) {
- // This must occur after CheckCleanup
- if (!g->Xchk) {
- g->Xchk= new(g) XCHK;
- ((PCHK)g->Xchk)->oldsep= GetBooleanOption("Sepindex", false);
- ((PCHK)g->Xchk)->oldpix= GetIndexInfo();
- } // endif Xchk
-
- } else
- g->Xchk= NULL;
-#endif // 0
-
- if (cras)
- g->Createas= 1; // To tell created table to ignore FLAG
-
- if (xtrace) {
-#if 0
- htrc("xcheck=%d cras=%d\n", xcheck, cras);
-
- if (xcheck)
- htrc("oldsep=%d oldpix=%p\n",
- ((PCHK)g->Xchk)->oldsep, ((PCHK)g->Xchk)->oldpix);
-#endif // 0
- htrc("Calling CntCheckDB db=%s cras=%d\n", GetDBName(NULL), cras);
- } // endif xtrace
-
- // Set or reset the good database environment
- if (CntCheckDB(g, this, GetDBName(NULL))) {
- htrc("%p external_lock: %s\n", this, g->Message);
- rc= HA_ERR_INTERNAL_ERROR;
- // This can NOT be called without open called first, but
- // the table can have been closed since then
- } else if (!tdbp || xp->CheckQuery(valid_query_id) || xmod != newmode) {
- if (tdbp) {
- // If this is called by a later query, the table may have
- // been already closed and the tdbp is not valid anymore.
- if (xp->last_query_id == valid_query_id)
- rc= CloseTable(g);
- else
- tdbp= NULL;
-
- } // endif tdbp
-
- xmod= newmode;
-
- // Delay open until used fields are known
- } // endif tdbp
-
- if (xtrace)
- htrc("external_lock: rc=%d\n", rc);
-
- DBUG_RETURN(rc);
-} // end of external_lock
-
-
-/**
- @brief
- The idea with handler::store_lock() is: The statement decides which locks
- should be needed for the table. For updates/deletes/inserts we get WRITE
- locks, for SELECT... we get read locks.
-
- @details
- Before adding the lock into the table lock handler (see thr_lock.c),
- mysqld calls store lock with the requested locks. Store lock can now
- modify a write lock to a read lock (or some other lock), ignore the
- lock (if we don't want to use MySQL table locks at all), or add locks
- for many tables (like we do when we are using a MERGE handler).
-
- Berkeley DB, for example, changes all WRITE locks to TL_WRITE_ALLOW_WRITE
- (which signals that we are doing WRITES, but are still allowing other
- readers and writers).
-
- When releasing locks, store_lock() is also called. In this case one
- usually doesn't have to do anything.
-
- In some exceptional cases MySQL may send a request for a TL_IGNORE;
- This means that we are requesting the same lock as last time and this
- should also be ignored. (This may happen when someone does a flush
- table when we have opened a part of the tables, in which case mysqld
- closes and reopens the tables and tries to get the same locks at last
- time). In the future we will probably try to remove this.
-
- Called from lock.cc by get_lock_data().
-
- @note
- In this method one should NEVER rely on table->in_use, it may, in fact,
- refer to a different thread! (this happens if get_lock_data() is called
- from mysql_lock_abort_for_thread() function)
-
- @see
- get_lock_data() in lock.cc
-*/
-THR_LOCK_DATA **ha_connect::store_lock(THD *thd,
- THR_LOCK_DATA **to,
- enum thr_lock_type lock_type)
-{
- if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
- lock.type=lock_type;
- *to++ = &lock;
- return to;
-}
-
-
-/**
- Searches for a pointer to the last occurrence of the
- character c in the string src.
- Returns true on failure, false on success.
-*/
-static bool
-strnrchr(LEX_CSTRING *ls, const char *src, size_t length, int c)
-{
- const char *srcend, *s;
- for (s= srcend= src + length; s > src; s--)
- {
- if (s[-1] == c)
- {
- ls->str= s;
- ls->length= srcend - s;
- return false;
- }
- }
- return true;
-}
-
-
-/**
- Split filename into database and table name.
-*/
-static bool
-filename_to_dbname_and_tablename(const char *filename,
- char *database, size_t database_size,
- char *table, size_t table_size)
-{
-#if defined(WIN32)
- char slash= '\\';
-#else // !WIN32
- char slash= '/';
-#endif // !WIN32
- LEX_CSTRING d, t;
- size_t length= strlen(filename);
-
- /* Find filename - the rightmost directory part */
- if (strnrchr(&t, filename, length, slash) || t.length + 1 > table_size)
- return true;
- memcpy(table, t.str, t.length);
- table[t.length]= '\0';
- if (!(length-= t.length))
- return true;
-
- length--; /* Skip slash */
-
- /* Find database name - the second rightmost directory part */
- if (strnrchr(&d, filename, length, slash) || d.length + 1 > database_size)
- return true;
- memcpy(database, d.str, d.length);
- database[d.length]= '\0';
- return false;
-} // end of filename_to_dbname_and_tablename
-
-/**
- @brief
- Used to delete or rename a table. By the time delete_table() has been
- called all opened references to this table will have been closed
- (and your globally shared references released) ===> too bad!!!
- The variable name will just be the name of the table.
- You will need to remove or rename any files you have created at
- this point.
-
- @details
- If you do not implement this, the default delete_table() is called from
- handler.cc and it will delete all files with the file extensions returned
- by bas_ext().
-
- Called from handler.cc by delete_table and ha_create_table(). Only used
- during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
- the storage engine.
-
- @see
- delete_table and ha_create_table() in handler.cc
-*/
-int ha_connect::delete_or_rename_table(const char *name, const char *to)
-{
- DBUG_ENTER("ha_connect::delete_or_rename_table");
- char db[128], tabname[128];
- int rc= 0;
- bool ok= false;
- THD *thd= current_thd;
- int sqlcom= thd_sql_command(thd);
-
- if (xtrace) {
- if (to)
- htrc("rename_table: this=%p thd=%p sqlcom=%d from=%s to=%s\n",
- this, thd, sqlcom, name, to);
- else
- htrc("delete_table: this=%p thd=%p sqlcom=%d name=%s\n",
- this, thd, sqlcom, name);
-
- } // endif xtrace
-
- if (to && (filename_to_dbname_and_tablename(to, db, sizeof(db),
- tabname, sizeof(tabname))
- || (*tabname == '#' && sqlcom == SQLCOM_CREATE_INDEX)))
- DBUG_RETURN(0);
-
- if (filename_to_dbname_and_tablename(name, db, sizeof(db),
- tabname, sizeof(tabname))
- || (*tabname == '#' && sqlcom == SQLCOM_CREATE_INDEX))
- DBUG_RETURN(0);
-
- // If a temporary file exists, all the tests below were passed
- // successfully when making it, so they are not needed anymore
- // in particular because they sometimes cause DBUG_ASSERT crash.
- if (*tabname != '#') {
- // We have to retrieve the information about this table options.
- ha_table_option_struct *pos;
- char key[MAX_DBKEY_LENGTH];
- uint key_length;
- TABLE_SHARE *share;
-
- key_length= tdc_create_key(key, db, tabname);
-
- // share contains the option struct that we need
- if (!(share= alloc_table_share(db, tabname, key, key_length)))
- DBUG_RETURN(rc);
-
-#if 0
- if (*tabname == '#') {
- // These are in ???? charset after renaming
- char *p= strchr(share->path.str, '@');
- strcpy(p, share->table_name.str);
- share->path.length= strlen(share->path.str);
- share->normalized_path.length= share->path.length;
- } // endif tabname
-#endif // 0
-
- // Get the share info from the .frm file
- if (!open_table_def(thd, share)) {
- // Now we can work
- if ((pos= share->option_struct)) {
- if (check_privileges(thd, pos, db))
- rc= HA_ERR_INTERNAL_ERROR; // ???
- else
- if (IsFileType(GetRealType(pos)) && !pos->filename)
- ok= true;
-
- } // endif pos
-
- } else // Avoid infamous DBUG_ASSERT
- thd->get_stmt_da()->reset_diagnostics_area();
-
- free_table_share(share);
- } else // Temporary file
- ok= true;
-
- if (ok) {
- // Let the base handler do the job
- if (to)
- rc= handler::rename_table(name, to);
- else if ((rc= handler::delete_table(name)) == ENOENT)
- rc= 0; // No files is not an error for CONNECT
-
- } // endif ok
-
- DBUG_RETURN(rc);
-} // end of delete_or_rename_table
-
-int ha_connect::delete_table(const char *name)
-{
- return delete_or_rename_table(name, NULL);
-} // end of delete_table
-
-int ha_connect::rename_table(const char *from, const char *to)
-{
- return delete_or_rename_table(from, to);
-} // end of rename_table
-
-/**
- @brief
- Given a starting key and an ending key, estimate the number of rows that
- will exist between the two keys.
-
- @details
- end_key may be empty, in which case determine if start_key matches any rows.
-
- Called from opt_range.cc by check_quick_keys().
-
- @see
- check_quick_keys() in opt_range.cc
-*/
-ha_rows ha_connect::records_in_range(uint inx, key_range *min_key,
- key_range *max_key)
-{
- ha_rows rows;
- DBUG_ENTER("ha_connect::records_in_range");
-
- if (indexing < 0 || inx != active_index)
- index_init(inx, false);
-
- if (xtrace)
- htrc("records_in_range: inx=%d indexing=%d\n", inx, indexing);
-
- if (indexing > 0) {
- int nval;
- uint len[2];
- const uchar *key[2];
- bool incl[2];
- key_part_map kmap[2];
-
- key[0]= (min_key) ? min_key->key : NULL;
- key[1]= (max_key) ? max_key->key : NULL;
- len[0]= (min_key) ? min_key->length : 0;
- len[1]= (max_key) ? max_key->length : 0;
- incl[0]= (min_key) ? (min_key->flag == HA_READ_KEY_EXACT) : false;
- incl[1]= (max_key) ? (max_key->flag == HA_READ_AFTER_KEY) : false;
- kmap[0]= (min_key) ? min_key->keypart_map : 0;
- kmap[1]= (max_key) ? max_key->keypart_map : 0;
-
- if ((nval= CntIndexRange(xp->g, tdbp, key, len, incl, kmap)) < 0)
- rows= HA_POS_ERROR;
- else
- rows= (ha_rows)nval;
-
- } else if (indexing < 0)
- rows= HA_POS_ERROR;
- else
- rows= 100000000; // Don't use missing index
-
- DBUG_RETURN(rows);
-} // end of records_in_range
-
-/**
- Convert an ISO-8859-1 column name to UTF-8
-*/
-static char *encode(PGLOBAL g, char *cnm)
- {
- char *buf= (char*)PlugSubAlloc(g, NULL, strlen(cnm) * 3);
- uint dummy_errors;
- uint32 len= copy_and_convert(buf, strlen(cnm) * 3,
- &my_charset_utf8_general_ci,
- cnm, strlen(cnm),
- &my_charset_latin1,
- &dummy_errors);
- buf[len]= '\0';
- return buf;
- } // end of Encode
-
-/**
- Store field definition for create.
-
- @return
- Return 0 if ok
-*/
-#if defined(NEW_WAY)
-static bool add_fields(PGLOBAL g,
- THD *thd,
- Alter_info *alter_info,
- char *name,
- int typ, int len, int dec,
- uint type_modifier,
- char *rem,
-// CHARSET_INFO *cs,
-// void *vcolinfo,
-// engine_option_value *create_options,
- int flg,
- bool dbf,
- char v)
-{
- register Create_field *new_field;
- char *length, *decimals= NULL;
- enum_field_types type;
-//Virtual_column_info *vcol_info= (Virtual_column_info *)vcolinfo;
- engine_option_value *crop;
- LEX_STRING *comment;
- LEX_STRING *field_name;
-
- DBUG_ENTER("ha_connect::add_fields");
-
- if (len) {
- if (!v && typ == TYPE_STRING && len > 255)
- v= 'V'; // Change CHAR to VARCHAR
-
- length= (char*)PlugSubAlloc(g, NULL, 8);
- sprintf(length, "%d", len);
-
- if (typ == TYPE_DOUBLE) {
- decimals= (char*)PlugSubAlloc(g, NULL, 8);
- sprintf(decimals, "%d", min(dec, (min(len, 31) - 1)));
- } // endif dec
-
- } else
- length= NULL;
-
- if (!rem)
- rem= "";
-
- type= PLGtoMYSQL(typ, dbf, v);
- comment= thd->make_lex_string(rem, strlen(rem));
- field_name= thd->make_lex_string(name, strlen(name));
-
- switch (v) {
- case 'Z': type_modifier|= ZEROFILL_FLAG;
- case 'U': type_modifier|= UNSIGNED_FLAG; break;
- } // endswitch v
-
- if (flg) {
- engine_option_value *start= NULL, *end= NULL;
- LEX_STRING *flag= thd->make_lex_string("flag", 4);
-
- crop= new(thd->mem_root) engine_option_value(*flag, (ulonglong)flg,
- &start, &end, thd->mem_root);
- } else
- crop= NULL;
-
- if (check_string_char_length(field_name, "", NAME_CHAR_LEN,
- system_charset_info, 1)) {
- my_error(ER_TOO_LONG_IDENT, MYF(0), field_name->str); /* purecov: inspected */
- DBUG_RETURN(1); /* purecov: inspected */
- } // endif field_name
-
- if (!(new_field= new Create_field()) ||
- new_field->init(thd, field_name->str, type, length, decimals,
- type_modifier, NULL, NULL, comment, NULL,
- NULL, NULL, 0, NULL, crop, true))
- DBUG_RETURN(1);
-
- alter_info->create_list.push_back(new_field);
- DBUG_RETURN(0);
-} // end of add_fields
-#else // !NEW_WAY
-static bool add_field(String *sql, const char *field_name, int typ,
- int len, int dec, uint tm, const char *rem,
- char *dft, char *xtra, int flag, bool dbf, char v)
-{
- char var = (len > 255) ? 'V' : v;
- bool error= false;
- const char *type= PLGtoMYSQLtype(typ, dbf, var);
-
- error|= sql->append('`');
- error|= sql->append(field_name);
- error|= sql->append("` ");
- error|= sql->append(type);
-
- if (len) {
- error|= sql->append('(');
- error|= sql->append_ulonglong(len);
-
- if (!strcmp(type, "DOUBLE")) {
- error|= sql->append(',');
- // dec must be < len and < 31
- error|= sql->append_ulonglong(min(dec, (min(len, 31) - 1)));
- } else if (dec > 0 && !strcmp(type, "DECIMAL")) {
- error|= sql->append(',');
- // dec must be < len
- error|= sql->append_ulonglong(min(dec, len - 1));
- } // endif dec
-
- error|= sql->append(')');
- } // endif len
-
- if (v == 'U')
- error|= sql->append(" UNSIGNED");
- else if (v == 'Z')
- error|= sql->append(" ZEROFILL");
-
- if (tm)
- error|= sql->append(STRING_WITH_LEN(" NOT NULL"), system_charset_info);
-
- if (dft && *dft) {
- error|= sql->append(" DEFAULT ");
-
- if (!IsTypeNum(typ)) {
- error|= sql->append("'");
- error|= sql->append_for_single_quote(dft, strlen(dft));
- error|= sql->append("'");
- } else
- error|= sql->append(dft);
-
- } // endif dft
-
- if (xtra && *xtra) {
- error|= sql->append(" ");
- error|= sql->append(xtra);
- } // endif rem
-
- if (rem && *rem) {
- error|= sql->append(" COMMENT '");
- error|= sql->append_for_single_quote(rem, strlen(rem));
- error|= sql->append("'");
- } // endif rem
-
- if (flag) {
- error|= sql->append(" FLAG=");
- error|= sql->append_ulonglong(flag);
- } // endif flag
-
- error|= sql->append(',');
- return error;
-} // end of add_field
-#endif // !NEW_WAY
-
-/**
- Initialise the table share with the new columns.
-
- @return
- Return 0 if ok
-*/
-#if defined(NEW_WAY)
-//static bool sql_unusable_for_discovery(THD *thd, const char *sql);
-
-static int init_table_share(THD *thd,
- TABLE_SHARE *table_s,
- HA_CREATE_INFO *create_info,
- Alter_info *alter_info)
-{
- KEY *not_used_1;
- uint not_used_2;
- int rc= 0;
- handler *file;
- LEX_CUSTRING frm= {0,0};
-
- DBUG_ENTER("init_table_share");
-
-#if 0
- ulonglong saved_mode= thd->variables.sql_mode;
- CHARSET_INFO *old_cs= thd->variables.character_set_client;
- Parser_state parser_state;
- char *sql_copy;
- LEX *old_lex;
- Query_arena *arena, backup;
- LEX tmp_lex;
-
- /*
- Ouch. Parser may *change* the string it's working on.
- Currently (2013-02-26) it is used to permanently disable
- conditional comments.
- Anyway, let's copy the caller's string...
- */
- if (!(sql_copy= thd->strmake(sql, sql_length)))
- DBUG_RETURN(HA_ERR_OUT_OF_MEM);
-
- if (parser_state.init(thd, sql_copy, sql_length))
- DBUG_RETURN(HA_ERR_OUT_OF_MEM);
-
- thd->variables.sql_mode= MODE_NO_ENGINE_SUBSTITUTION | MODE_NO_DIR_IN_CREATE;
- thd->variables.character_set_client= system_charset_info;
- old_lex= thd->lex;
- thd->lex= &tmp_lex;
-
- arena= thd->stmt_arena;
-
- if (arena->is_conventional())
- arena= 0;
- else
- thd->set_n_backup_active_arena(arena, &backup);
-
- lex_start(thd);
-
- if ((error= parse_sql(thd, & parser_state, NULL)))
- goto ret;
-
- if (table_s->sql_unusable_for_discovery(thd, NULL)) {
- my_error(ER_SQL_DISCOVER_ERROR, MYF(0), plugin_name(db_plugin)->str,
- db.str, table_name.str, sql_copy);
- goto ret;
- } // endif unusable
-
- thd->lex->create_info.db_type= plugin_data(db_plugin, handlerton *);
-
- if (tabledef_version.str)
- thd->lex->create_info.tabledef_version= tabledef_version;
-#endif // 0
-
- tmp_disable_binlog(thd);
-
- file= mysql_create_frm_image(thd, table_s->db.str, table_s->table_name.str,
- create_info, alter_info, C_ORDINARY_CREATE,
- &not_used_1, &not_used_2, &frm);
- if (file)
- delete file;
- else
- rc= OPEN_FRM_CORRUPTED;
-
- if (!rc && frm.str) {
- table_s->option_list= 0; // cleanup existing options ...
- table_s->option_struct= 0; // ... if it's an assisted discovery
- rc= table_s->init_from_binary_frm_image(thd, true, frm.str, frm.length);
- } // endif frm
-
-//ret:
- my_free(const_cast<uchar*>(frm.str));
- reenable_binlog(thd);
-#if 0
- lex_end(thd->lex);
- thd->lex= old_lex;
- if (arena)
- thd->restore_active_arena(arena, &backup);
- thd->variables.sql_mode= saved_mode;
- thd->variables.character_set_client= old_cs;
-#endif // 0
-
- if (thd->is_error() || rc) {
- thd->clear_error();
- my_error(ER_NO_SUCH_TABLE, MYF(0), table_s->db.str,
- table_s->table_name.str);
- DBUG_RETURN(HA_ERR_NOT_A_TABLE);
- } else
- DBUG_RETURN(0);
-
-} // end of init_table_share
-#else // !NEW_WAY
-static int init_table_share(THD* thd,
- TABLE_SHARE *table_s,
- HA_CREATE_INFO *create_info,
-// char *dsn,
- String *sql)
-{
- bool oom= false;
- PTOS topt= table_s->option_struct;
-
- sql->length(sql->length()-1); // remove the trailing comma
- sql->append(')');
-
- for (ha_create_table_option *opt= connect_table_option_list;
- opt->name; opt++) {
- ulonglong vull;
- const char *vstr;
-
- switch (opt->type) {
- case HA_OPTION_TYPE_ULL:
- vull= *(ulonglong*)(((char*)topt) + opt->offset);
-
- if (vull != opt->def_value) {
- oom|= sql->append(' ');
- oom|= sql->append(opt->name);
- oom|= sql->append('=');
- oom|= sql->append_ulonglong(vull);
- } // endif vull
-
- break;
- case HA_OPTION_TYPE_STRING:
- vstr= *(char**)(((char*)topt) + opt->offset);
-
- if (vstr) {
- oom|= sql->append(' ');
- oom|= sql->append(opt->name);
- oom|= sql->append("='");
- oom|= sql->append_for_single_quote(vstr, strlen(vstr));
- oom|= sql->append('\'');
- } // endif vstr
-
- break;
- case HA_OPTION_TYPE_BOOL:
- vull= *(bool*)(((char*)topt) + opt->offset);
-
- if (vull != opt->def_value) {
- oom|= sql->append(' ');
- oom|= sql->append(opt->name);
- oom|= sql->append('=');
- oom|= sql->append(vull ? "ON" : "OFF");
- } // endif vull
-
- break;
- default: // no enums here, good :)
- break;
- } // endswitch type
-
- if (oom)
- return HA_ERR_OUT_OF_MEM;
-
- } // endfor opt
-
- if (create_info->connect_string.length) {
-//if (dsn) {
- oom|= sql->append(' ');
- oom|= sql->append("CONNECTION='");
- oom|= sql->append_for_single_quote(create_info->connect_string.str,
- create_info->connect_string.length);
-// oom|= sql->append_for_single_quote(dsn, strlen(dsn));
- oom|= sql->append('\'');
-
- if (oom)
- return HA_ERR_OUT_OF_MEM;
-
- } // endif string
-
- if (create_info->default_table_charset) {
- oom|= sql->append(' ');
- oom|= sql->append("CHARSET=");
- oom|= sql->append(create_info->default_table_charset->csname);
-
- if (oom)
- return HA_ERR_OUT_OF_MEM;
-
- } // endif charset
-
- if (xtrace)
- htrc("s_init: %.*s\n", sql->length(), sql->ptr());
-
- return table_s->init_from_sql_statement_string(thd, true,
- sql->ptr(), sql->length());
-} // end of init_table_share
-#endif // !NEW_WAY
-
-// Add an option to the create_info option list
-static void add_option(THD* thd, HA_CREATE_INFO *create_info,
- const char *opname, const char *opval)
-{
-#if defined(NEW_WAY)
- LEX_STRING *opn= thd->make_lex_string(opname, strlen(opname));
- LEX_STRING *val= thd->make_lex_string(opval, strlen(opval));
- engine_option_value *pov, **start= &create_info->option_list, *end= NULL;
-
- for (pov= *start; pov; pov= pov->next)
- end= pov;
-
- pov= new(thd->mem_root) engine_option_value(*opn, *val, false, start, &end);
-#endif // NEW_WAY
-} // end of add_option
-
-// Used to check whether a MYSQL table is created on itself
-static bool CheckSelf(PGLOBAL g, TABLE_SHARE *s, const char *host,
- const char *db, char *tab, const char *src, int port)
-{
- if (src)
- return false;
- else if (host && stricmp(host, "localhost") && strcmp(host, "127.0.0.1"))
- return false;
- else if (db && stricmp(db, s->db.str))
- return false;
- else if (tab && stricmp(tab, s->table_name.str))
- return false;
- else if (port && port != (signed)GetDefaultPort())
- return false;
-
- strcpy(g->Message, "This MySQL table is defined on itself");
- return true;
-} // end of CheckSelf
-
-/**
- @brief
- connect_assisted_discovery() is called when creating a table with no columns.
-
- @details
- When assisted discovery is used the .frm file have not already been
- created. You can overwrite some definitions at this point but the
- main purpose of it is to define the columns for some table types.
-
- @note
- this function is no more called in case of CREATE .. SELECT
-*/
-static int connect_assisted_discovery(handlerton *hton, THD* thd,
- TABLE_SHARE *table_s,
- HA_CREATE_INFO *create_info)
-{
- char v, spc= ',', qch= 0;
- const char *fncn= "?";
- const char *user, *fn, *db, *host, *pwd, *sep, *tbl, *src;
- const char *col, *ocl, *rnk, *pic, *fcl;
- char *tab, *dsn, *shm;
-#if defined(WIN32)
- char *nsp= NULL, *cls= NULL;
-#endif // WIN32
- int port= 0, hdr= 0, mxr __attribute__((unused))= 0, mxe= 0, rc= 0;
- int cop __attribute__((unused)) = 0;
- uint tm, fnc= FNC_NO, supfnc= (FNC_NO | FNC_COL);
- bool bif, ok= false, dbf= false;
- TABTYPE ttp= TAB_UNDEF;
- PQRYRES qrp= NULL;
- PCOLRES crp;
- PCONNECT xp= NULL;
- PGLOBAL g= GetPlug(thd, xp);
- PTOS topt= table_s->option_struct;
-#if defined(NEW_WAY)
-//CHARSET_INFO *cs;
- Alter_info alter_info;
-#else // !NEW_WAY
- char buf[1024];
- String sql(buf, sizeof(buf), system_charset_info);
-
- sql.copy(STRING_WITH_LEN("CREATE TABLE whatever ("), system_charset_info);
-#endif // !NEW_WAY
-
- if (!g)
- return HA_ERR_INTERNAL_ERROR;
-
- user= host= pwd= tbl= src= col= ocl= pic= fcl= rnk= dsn= NULL;
-
- // Get the useful create options
- ttp= GetTypeID(topt->type);
- fn= topt->filename;
- tab= (char*)topt->tabname;
- src= topt->srcdef;
- db= topt->dbname;
- fncn= topt->catfunc;
- fnc= GetFuncID(fncn);
- sep= topt->separator;
- spc= (!sep || !strcmp(sep, "\\t")) ? '\t' : *sep;
- qch= topt->qchar ? *topt->qchar : (signed)topt->quoted >= 0 ? '"' : 0;
- hdr= (int)topt->header;
- tbl= topt->tablist;
- col= topt->colist;
-
- if (topt->oplist) {
- host= GetListOption(g, "host", topt->oplist, "localhost");
- user= GetListOption(g, "user", topt->oplist, "root");
- // Default value db can come from the DBNAME=xxx option.
- db= GetListOption(g, "database", topt->oplist, db);
- col= GetListOption(g, "colist", topt->oplist, col);
- ocl= GetListOption(g, "occurcol", topt->oplist, NULL);
- pic= GetListOption(g, "pivotcol", topt->oplist, NULL);
- fcl= GetListOption(g, "fnccol", topt->oplist, NULL);
- rnk= GetListOption(g, "rankcol", topt->oplist, NULL);
- pwd= GetListOption(g, "password", topt->oplist);
-#if defined(WIN32)
- nsp= GetListOption(g, "namespace", topt->oplist);
- cls= GetListOption(g, "class", topt->oplist);
-#endif // WIN32
- port= atoi(GetListOption(g, "port", topt->oplist, "0"));
- mxr= atoi(GetListOption(g,"maxres", topt->oplist, "0"));
- mxe= atoi(GetListOption(g,"maxerr", topt->oplist, "0"));
-#if defined(PROMPT_OK)
- cop= atoi(GetListOption(g, "checkdsn", topt->oplist, "0"));
-#endif // PROMPT_OK
- } else {
- host= "localhost";
- user= "root";
- } // endif option_list
-
- if (!(shm= (char*)db))
- db= table_s->db.str; // Default value
-
- // Check table type
- if (ttp == TAB_UNDEF) {
- topt->type= (src) ? "MYSQL" : (tab) ? "PROXY" : "DOS";
- ttp= GetTypeID(topt->type);
- sprintf(g->Message, "No table_type. Was set to %s", topt->type);
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- add_option(thd, create_info, "table_type", topt->type);
- } else if (ttp == TAB_NIY) {
- sprintf(g->Message, "Unsupported table type %s", topt->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } // endif ttp
-
- if (!tab) {
- if (ttp == TAB_TBL) {
- // Make tab the first table of the list
- char *p;
-
- if (!tbl) {
- strcpy(g->Message, "Missing table list");
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } // endif tbl
-
- tab= (char*)PlugSubAlloc(g, NULL, strlen(tbl) + 1);
- strcpy(tab, tbl);
-
- if ((p= strchr(tab, ',')))
- *p= 0;
-
- if ((p=strchr(tab, '.'))) {
- *p= 0;
- db= tab;
- tab= p + 1;
- } // endif p
-
- } else if (ttp != TAB_ODBC || !(fnc & (FNC_TABLE | FNC_COL)))
- tab= table_s->table_name.str; // Default value
-
-#if defined(NEW_WAY)
-// add_option(thd, create_info, "tabname", tab);
-#endif // NEW_WAY
- } // endif tab
-
- switch (ttp) {
-#if defined(ODBC_SUPPORT)
- case TAB_ODBC:
- dsn= create_info->connect_string.str;
-
- if (fnc & (FNC_DSN | FNC_DRIVER)) {
- ok= true;
-#if defined(PROMPT_OK)
- } else if (!stricmp(thd->main_security_ctx.host, "localhost")
- && cop == 1) {
- if ((dsn = ODBCCheckConnection(g, dsn, cop)) != NULL) {
- thd->make_lex_string(&create_info->connect_string, dsn, strlen(dsn));
- ok= true;
- } // endif dsn
-#endif // PROMPT_OK
-
- } else if (!dsn)
- sprintf(g->Message, "Missing %s connection string", topt->type);
- else
- ok= true;
-
- supfnc |= (FNC_TABLE | FNC_DSN | FNC_DRIVER);
- break;
-#endif // ODBC_SUPPORT
- case TAB_DBF:
- dbf= true;
- // Passthru
- case TAB_CSV:
- if (!fn && fnc != FNC_NO)
- sprintf(g->Message, "Missing %s file name", topt->type);
- else
- ok= true;
-
- break;
-#if defined(MYSQL_SUPPORT)
- case TAB_MYSQL:
- ok= true;
-
- if (create_info->connect_string.str) {
- int len= create_info->connect_string.length;
- PMYDEF mydef= new(g) MYSQLDEF();
- PDBUSER dup= PlgGetUser(g);
- PCATLG cat= (dup) ? dup->Catalog : NULL;
-
- dsn= (char*)PlugSubAlloc(g, NULL, len + 1);
- strncpy(dsn, create_info->connect_string.str, len);
- dsn[len]= 0;
- mydef->SetName(create_info->alias);
- mydef->SetCat(cat);
-
- if (!mydef->ParseURL(g, dsn, false)) {
- if (mydef->GetHostname())
- host= mydef->GetHostname();
-
- if (mydef->GetUsername())
- user= mydef->GetUsername();
-
- if (mydef->GetPassword())
- pwd= mydef->GetPassword();
-
- if (mydef->GetDatabase())
- db= mydef->GetDatabase();
-
- if (mydef->GetTabname())
- tab= mydef->GetTabname();
-
- if (mydef->GetPortnumber())
- port= mydef->GetPortnumber();
-
- } else
- ok= false;
-
- } else if (!user)
- user= "root";
-
- if (CheckSelf(g, table_s, host, db, tab, src, port))
- ok= false;
-
- break;
-#endif // MYSQL_SUPPORT
-#if defined(WIN32)
- case TAB_WMI:
- ok= true;
- break;
-#endif // WIN32
- case TAB_PIVOT:
- supfnc= FNC_NO;
- case TAB_PRX:
- case TAB_TBL:
- case TAB_XCL:
- case TAB_OCCUR:
- if (!src && !stricmp(tab, create_info->alias) &&
- (!db || !stricmp(db, table_s->db.str)))
- sprintf(g->Message, "A %s table cannot refer to itself", topt->type);
- else
- ok= true;
-
- break;
- case TAB_OEM:
- if (topt->module && topt->subtype)
- ok= true;
- else
- strcpy(g->Message, "Missing OEM module or subtype");
-
- break;
- default:
- sprintf(g->Message, "Cannot get column info for table type %s", topt->type);
- break;
- } // endif ttp
-
- // Check for supported catalog function
- if (ok && !(supfnc & fnc)) {
- sprintf(g->Message, "Unsupported catalog function %s for table type %s",
- fncn, topt->type);
- ok= false;
- } // endif supfnc
-
- if (src && fnc != FNC_NO) {
- strcpy(g->Message, "Cannot make catalog table from srcdef");
- ok= false;
- } // endif src
-
- if (ok) {
- char *cnm, *rem, *dft, *xtra;
- int i, len, prec, dec, typ, flg;
- PDBUSER dup= PlgGetUser(g);
- PCATLG cat= (dup) ? dup->Catalog : NULL;
-
- if (cat)
- cat->SetDataPath(g, table_s->db.str);
- else
- return HA_ERR_INTERNAL_ERROR; // Should never happen
-
- if (src && ttp != TAB_PIVOT && ttp != TAB_ODBC) {
- qrp= SrcColumns(g, host, db, user, pwd, src, port);
-
- if (qrp && ttp == TAB_OCCUR)
- if (OcrSrcCols(g, qrp, col, ocl, rnk)) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } // endif OcrSrcCols
-
- } else switch (ttp) {
- case TAB_DBF:
- qrp= DBFColumns(g, fn, fnc == FNC_COL);
- break;
-#if defined(ODBC_SUPPORT)
- case TAB_ODBC:
- switch (fnc) {
- case FNC_NO:
- case FNC_COL:
- if (src) {
- qrp= ODBCSrcCols(g, dsn, (char*)src);
- src= NULL; // for next tests
- } else
- qrp= ODBCColumns(g, dsn, shm, tab, NULL, mxr, fnc == FNC_COL);
-
- break;
- case FNC_TABLE:
- qrp= ODBCTables(g, dsn, shm, tab, mxr, true);
- break;
- case FNC_DSN:
- qrp= ODBCDataSources(g, mxr, true);
- break;
- case FNC_DRIVER:
- qrp= ODBCDrivers(g, mxr, true);
- break;
- default:
- sprintf(g->Message, "invalid catfunc %s", fncn);
- break;
- } // endswitch info
-
- break;
-#endif // ODBC_SUPPORT
-#if defined(MYSQL_SUPPORT)
- case TAB_MYSQL:
- qrp= MyColumns(g, host, db, user, pwd, tab,
- NULL, port, fnc == FNC_COL);
- break;
-#endif // MYSQL_SUPPORT
- case TAB_CSV:
- qrp= CSVColumns(g, fn, spc, qch, hdr, mxe, fnc == FNC_COL);
- break;
-#if defined(WIN32)
- case TAB_WMI:
- qrp= WMIColumns(g, nsp, cls, fnc == FNC_COL);
- break;
-#endif // WIN32
- case TAB_PRX:
- case TAB_TBL:
- case TAB_XCL:
- case TAB_OCCUR:
- bif= fnc == FNC_COL;
- qrp= TabColumns(g, thd, db, tab, bif);
-
- if (!qrp && bif && fnc != FNC_COL) // tab is a view
- qrp= MyColumns(g, host, db, user, pwd, tab, NULL, port, false);
-
- if (qrp && ttp == TAB_OCCUR && fnc != FNC_COL)
- if (OcrColumns(g, qrp, col, ocl, rnk)) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } // endif OcrColumns
-
- break;
- case TAB_PIVOT:
- qrp= PivotColumns(g, tab, src, pic, fcl, host, db, user, pwd, port);
- break;
- case TAB_OEM:
- qrp= OEMColumns(g, topt, tab, (char*)db, fnc == FNC_COL);
- break;
- default:
- strcpy(g->Message, "System error during assisted discovery");
- break;
- } // endswitch ttp
-
- if (!qrp) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } // endif qrp
-
- if (fnc != FNC_NO || src || ttp == TAB_PIVOT) {
- // Catalog like table
- for (crp= qrp->Colresp; !rc && crp; crp= crp->Next) {
- cnm= encode(g, crp->Name);
- typ= crp->Type;
- len= crp->Length;
- dec= crp->Prec;
- flg= crp->Flag;
-
- if (!len && typ == TYPE_STRING)
- len= 256; // STRBLK's have 0 length
-
-#if defined(NEW_WAY)
- // Now add the field
- rc= add_fields(g, thd, &alter_info, cnm, typ, len, dec,
- NOT_NULL_FLAG, "", flg, dbf, 0);
-#else // !NEW_WAY
- // Now add the field
- if (add_field(&sql, cnm, typ, len, dec, NOT_NULL_FLAG,
- NULL, NULL, NULL, flg, dbf, 0))
- rc= HA_ERR_OUT_OF_MEM;
-#endif // !NEW_WAY
- } // endfor crp
-
- } else // Not a catalog table
- for (i= 0; !rc && i < qrp->Nblin; i++) {
- typ= len= prec= dec= 0;
- tm= NOT_NULL_FLAG;
- cnm= (char*)"noname";
- dft= xtra= NULL;
-#if defined(NEW_WAY)
- rem= "";
-// cs= NULL;
-#else // !NEW_WAY
- rem= NULL;
-#endif // !NEW_WAY
-
- for (crp= qrp->Colresp; crp; crp= crp->Next)
- switch (crp->Fld) {
- case FLD_NAME:
- cnm= encode(g, crp->Kdata->GetCharValue(i));
- break;
- case FLD_TYPE:
- typ= crp->Kdata->GetIntValue(i);
- v = (crp->Nulls) ? crp->Nulls[i] : 0;
- break;
- case FLD_PREC:
- // PREC must be always before LENGTH
- len= prec= crp->Kdata->GetIntValue(i);
- break;
- case FLD_LENGTH:
- len= crp->Kdata->GetIntValue(i);
- break;
- case FLD_SCALE:
- dec= crp->Kdata->GetIntValue(i);
- break;
- case FLD_NULL:
- if (crp->Kdata->GetIntValue(i))
- tm= 0; // Nullable
-
- break;
- case FLD_REM:
- rem= crp->Kdata->GetCharValue(i);
- break;
-// case FLD_CHARSET:
- // No good because remote table is already translated
-// if (*(csn= crp->Kdata->GetCharValue(i)))
-// cs= get_charset_by_name(csn, 0);
-
-// break;
- case FLD_DEFAULT:
- dft= crp->Kdata->GetCharValue(i);
- break;
- case FLD_EXTRA:
- xtra= crp->Kdata->GetCharValue(i);
- break;
- default:
- break; // Ignore
- } // endswitch Fld
-
-#if defined(ODBC_SUPPORT)
- if (ttp == TAB_ODBC) {
- int plgtyp;
-
- // typ must be PLG type, not SQL type
- if (!(plgtyp= TranslateSQLType(typ, dec, prec, v))) {
- sprintf(g->Message, "Unsupported SQL type %d", typ);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
- } else
- typ= plgtyp;
-
- switch (typ) {
- case TYPE_DOUBLE:
- // Some data sources do not count dec in length (prec)
- prec += (dec + 2); // To be safe
- case TYPE_DECIM:
- break;
- default:
- dec= 0;
- } // endswitch typ
-
- } // endif ttp
-#endif // ODBC_SUPPORT
-
- // Make the arguments as required by add_fields
- if (typ == TYPE_DATE)
- prec= 0;
- else if (typ == TYPE_DOUBLE)
- prec= len;
-
- // Now add the field
-#if defined(NEW_WAY)
- rc= add_fields(g, thd, &alter_info, cnm, typ, prec, dec,
- tm, rem, 0, dbf, v);
-#else // !NEW_WAY
- if (add_field(&sql, cnm, typ, prec, dec, tm, rem, dft, xtra,
- 0, dbf, v))
- rc= HA_ERR_OUT_OF_MEM;
-#endif // !NEW_WAY
- } // endfor i
-
-#if defined(NEW_WAY)
- rc= init_table_share(thd, table_s, create_info, &alter_info);
-#else // !NEW_WAY
- if (!rc)
- rc= init_table_share(thd, table_s, create_info, &sql);
-// rc= init_table_share(thd, table_s, create_info, dsn, &sql);
-#endif // !NEW_WAY
-
- return rc;
- } // endif ok
-
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- return HA_ERR_INTERNAL_ERROR;
-} // end of connect_assisted_discovery
-
-/**
- Get the database name from a qualified table name.
-*/
-char *ha_connect::GetDBfromName(const char *name)
-{
- char *db, dbname[128], tbname[128];
-
- if (filename_to_dbname_and_tablename(name, dbname, sizeof(dbname),
- tbname, sizeof(tbname)))
- *dbname= 0;
-
- if (*dbname) {
- assert(xp && xp->g);
- db= (char*)PlugSubAlloc(xp->g, NULL, strlen(dbname + 1));
- strcpy(db, dbname);
- } else
- db= NULL;
-
- return db;
-} // end of GetDBfromName
-
-
-/**
- @brief
- create() is called to create a database. The variable name will have the name
- of the table.
-
- @details
- When create() is called you do not need to worry about
- opening the table. Also, the .frm file will have already been
- created so adjusting create_info is not necessary. You can overwrite
- the .frm file at this point if you wish to change the table
- definition, but there are no methods currently provided for doing
- so.
-
- Called from handle.cc by ha_create_table().
-
- @note
- Currently we do some checking on the create definitions and stop
- creating if an error is found. We wish we could change the table
- definition such as providing a default table type. However, as said
- above, there are no method to do so.
-
- @see
- ha_create_table() in handle.cc
-*/
-
-int ha_connect::create(const char *name, TABLE *table_arg,
- HA_CREATE_INFO *create_info)
-{
- int rc= RC_OK;
- bool dbf;
- Field* *field;
- Field *fp;
- TABTYPE type;
- TABLE *st= table; // Probably unuseful
- THD *thd= ha_thd();
- xp= GetUser(thd, xp);
- PGLOBAL g= xp->g;
-
- DBUG_ENTER("ha_connect::create");
- int sqlcom= thd_sql_command(table_arg->in_use);
- PTOS options= GetTableOptionStruct(table_arg);
-
- table= table_arg; // Used by called functions
-
- if (xtrace)
- htrc("create: this=%p thd=%p xp=%p g=%p sqlcom=%d name=%s\n",
- this, thd, xp, g, sqlcom, GetTableName());
-
- // CONNECT engine specific table options:
- DBUG_ASSERT(options);
- type= GetTypeID(options->type);
-
- // Check table type
- if (type == TAB_UNDEF) {
- options->type= (options->srcdef) ? "MYSQL" :
- (options->tabname) ? "PROXY" : "DOS";
- type= GetTypeID(options->type);
- sprintf(g->Message, "No table_type. Will be set to %s", options->type);
-
- if (sqlcom == SQLCOM_CREATE_TABLE)
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
-
- } else if (type == TAB_NIY) {
- sprintf(g->Message, "Unsupported table type %s", options->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif ttp
-
- if (check_privileges(thd, options, GetDBfromName(name)))
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
-
- if (options->data_charset) {
- const CHARSET_INFO *data_charset;
-
- if (!(data_charset= get_charset_by_csname(options->data_charset,
- MY_CS_PRIMARY, MYF(0)))) {
- my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), options->data_charset);
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif charset
-
- if (type == TAB_XML && data_charset != &my_charset_utf8_general_ci) {
- my_printf_error(ER_UNKNOWN_ERROR,
- "DATA_CHARSET='%s' is not supported for TABLE_TYPE=XML",
- MYF(0), options->data_charset);
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif utf8
-
- } // endif charset
-
- if (!g) {
- rc= HA_ERR_INTERNAL_ERROR;
- DBUG_RETURN(rc);
- } else
- dbf= (GetTypeID(options->type) == TAB_DBF && !options->catfunc);
-
- // Can be null in ALTER TABLE
- if (create_info->alias)
- // Check whether a table is defined on itself
- switch (type) {
- case TAB_PRX:
- case TAB_XCL:
- case TAB_PIVOT:
- case TAB_OCCUR:
- if (options->srcdef) {
- strcpy(g->Message, "Cannot check looping reference");
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- } else if (options->tabname) {
- if (!stricmp(options->tabname, create_info->alias) &&
- (!options->dbname || !stricmp(options->dbname, table_arg->s->db.str))) {
- sprintf(g->Message, "A %s table cannot refer to itself",
- options->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif tab
-
- } else {
- strcpy(g->Message, "Missing object table name or definition");
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif tabname
-
- case TAB_MYSQL:
- {const char *src= options->srcdef;
- char *host, *db, *tab= (char*)options->tabname;
- int port;
-
- host= GetListOption(g, "host", options->oplist, NULL);
- db= GetListOption(g, "database", options->oplist, NULL);
- port= atoi(GetListOption(g, "port", options->oplist, "0"));
-
- if (create_info->connect_string.str) {
- char *dsn;
- int len= create_info->connect_string.length;
- PMYDEF mydef= new(g) MYSQLDEF();
- PDBUSER dup= PlgGetUser(g);
- PCATLG cat= (dup) ? dup->Catalog : NULL;
-
- dsn= (char*)PlugSubAlloc(g, NULL, len + 1);
- strncpy(dsn, create_info->connect_string.str, len);
- dsn[len]= 0;
- mydef->SetName(create_info->alias);
- mydef->SetCat(cat);
-
- if (!mydef->ParseURL(g, dsn, false)) {
- if (mydef->GetHostname())
- host= mydef->GetHostname();
-
- if (mydef->GetDatabase())
- db= mydef->GetDatabase();
-
- if (mydef->GetTabname())
- tab= mydef->GetTabname();
-
- if (mydef->GetPortnumber())
- port= mydef->GetPortnumber();
-
- } else {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif ParseURL
-
- } // endif connect_string
-
- if (CheckSelf(g, table_arg->s, host, db, tab, src, port)) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif CheckSelf
-
- }break;
- default: /* do nothing */;
- break;
- } // endswitch ttp
-
- if (type == TAB_XML) {
- bool dom; // True: MS-DOM, False libxml2
- char *xsup= GetListOption(g, "Xmlsup", options->oplist, "*");
-
- // Note that if no support is specified, the default is MS-DOM
- // on Windows and libxml2 otherwise
- switch (*xsup) {
- case '*':
-#if defined(WIN32)
- dom= true;
-#else // !WIN32
- dom= false;
-#endif // !WIN32
- break;
- case 'M':
- case 'D':
- dom= true;
- break;
- default:
- dom= false;
- break;
- } // endswitch xsup
-
-#if !defined(DOMDOC_SUPPORT)
- if (dom) {
- strcpy(g->Message, "MS-DOM not supported by this version");
- xsup= NULL;
- } // endif DomDoc
-#endif // !DOMDOC_SUPPORT
-
-#if !defined(LIBXML2_SUPPORT)
- if (!dom) {
- strcpy(g->Message, "libxml2 not supported by this version");
- xsup= NULL;
- } // endif Libxml2
-#endif // !LIBXML2_SUPPORT
-
- if (!xsup) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- DBUG_RETURN(rc);
- } // endif xsup
-
- } // endif type
-
- // Check column types
- for (field= table_arg->field; *field; field++) {
- fp= *field;
-
- if (fp->vcol_info && !fp->stored_in_db)
- continue; // This is a virtual column
-
- if (fp->flags & AUTO_INCREMENT_FLAG) {
- strcpy(g->Message, "Auto_increment is not supported yet");
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- DBUG_RETURN(rc);
- } // endif flags
-
- if (fp->flags & (BLOB_FLAG | ENUM_FLAG | SET_FLAG)) {
- sprintf(g->Message, "Unsupported type for column %s",
- fp->field_name);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- DBUG_RETURN(rc);
- } // endif flags
-
- switch (fp->type()) {
- case MYSQL_TYPE_SHORT:
- case MYSQL_TYPE_LONG:
- case MYSQL_TYPE_FLOAT:
- case MYSQL_TYPE_DOUBLE:
- case MYSQL_TYPE_TIMESTAMP:
- case MYSQL_TYPE_DATE:
- case MYSQL_TYPE_TIME:
- case MYSQL_TYPE_DATETIME:
- case MYSQL_TYPE_YEAR:
- case MYSQL_TYPE_NEWDATE:
- case MYSQL_TYPE_VARCHAR:
- case MYSQL_TYPE_LONGLONG:
- case MYSQL_TYPE_TINY:
- break; // Ok
- case MYSQL_TYPE_VAR_STRING:
- case MYSQL_TYPE_STRING:
- case MYSQL_TYPE_DECIMAL:
- case MYSQL_TYPE_NEWDECIMAL:
- case MYSQL_TYPE_INT24:
- break; // To be checked
- case MYSQL_TYPE_BIT:
- case MYSQL_TYPE_NULL:
- case MYSQL_TYPE_ENUM:
- case MYSQL_TYPE_SET:
- case MYSQL_TYPE_TINY_BLOB:
- case MYSQL_TYPE_MEDIUM_BLOB:
- case MYSQL_TYPE_LONG_BLOB:
- case MYSQL_TYPE_BLOB:
- case MYSQL_TYPE_GEOMETRY:
- default:
-// fprintf(stderr, "Unsupported type column %s\n", fp->field_name);
- sprintf(g->Message, "Unsupported type for column %s",
- fp->field_name);
- rc= HA_ERR_INTERNAL_ERROR;
- my_printf_error(ER_UNKNOWN_ERROR,
- "Unsupported type for column '%s'",
- MYF(0), fp->field_name);
- DBUG_RETURN(rc);
- break;
- } // endswitch type
-
- if ((fp)->real_maybe_null() && !IsTypeNullable(type)) {
- my_printf_error(ER_UNKNOWN_ERROR,
- "Table type %s does not support nullable columns",
- MYF(0), options->type);
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
- } // endif !nullable
-
- if (dbf) {
- bool b= false;
-
- if ((b= strlen(fp->field_name) > 10))
- sprintf(g->Message, "DBF: Column name '%s' is too long (max=10)",
- fp->field_name);
- else if ((b= fp->field_length > 255))
- sprintf(g->Message, "DBF: Column length too big for '%s' (max=255)",
- fp->field_name);
-
- if (b) {
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- DBUG_RETURN(rc);
- } // endif b
-
- } // endif dbf
-
- } // endfor field
-
- if ((sqlcom == SQLCOM_CREATE_TABLE || *GetTableName() == '#')
- && IsFileType(type) && !options->filename) {
- // The file name is not specified, create a default file in
- // the database directory named table_name.table_type.
- // (temporarily not done for XML because a void file causes
- // the XML parsers to report an error on the first Insert)
- char buf[256], fn[_MAX_PATH], dbpath[128], lwt[12];
- int h;
-
- strcpy(buf, GetTableName());
-
- // Check for incompatible options
- if (options->sepindex) {
- my_message(ER_UNKNOWN_ERROR,
- "SEPINDEX is incompatible with unspecified file name",
- MYF(0));
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
- } else if (GetTypeID(options->type) == TAB_VEC)
- if (!table->s->max_rows || options->split) {
- my_printf_error(ER_UNKNOWN_ERROR,
- "%s tables whose file name is unspecified cannot be split",
- MYF(0), options->type);
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
- } else if (options->header == 2) {
- my_printf_error(ER_UNKNOWN_ERROR,
- "header=2 is not allowed for %s tables whose file name is unspecified",
- MYF(0), options->type);
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
- } // endif's
-
- // Fold type to lower case
- for (int i= 0; i < 12; i++)
- if (!options->type[i]) {
- lwt[i]= 0;
- break;
- } else
- lwt[i]= tolower(options->type[i]);
-
- strcat(strcat(buf, "."), lwt);
- sprintf(g->Message, "No file name. Table will use %s", buf);
-
- if (sqlcom == SQLCOM_CREATE_TABLE)
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
-
- strcat(strcat(strcpy(dbpath, "./"), table->s->db.str), "/");
- PlugSetPath(fn, buf, dbpath);
-
- if ((h= ::open(fn, O_CREAT | O_EXCL, 0666)) == -1) {
- if (errno == EEXIST)
- sprintf(g->Message, "Default file %s already exists", fn);
- else
- sprintf(g->Message, "Error %d creating file %s", errno, fn);
-
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
- } else
- ::close(h);
-
- if (type == TAB_FMT || options->readonly)
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
- "Congratulation, you just created a read-only void table!");
-
- } // endif
-
- if (xtrace)
- htrc("xchk=%p createas=%d\n", g->Xchk, g->Createas);
-
- // To check whether indices have to be made or remade
- if (!g->Xchk) {
- PIXDEF xdp;
-
- // We should be in CREATE TABLE or ALTER_TABLE
- if (sqlcom != SQLCOM_CREATE_TABLE && sqlcom != SQLCOM_ALTER_TABLE)
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
- "Wrong command in create, please contact CONNECT team");
-
- if (sqlcom == SQLCOM_ALTER_TABLE && g->Alchecked == 0 &&
- (!IsFileType(type) || FileExists(options->filename))) {
- // This is an ALTER to CONNECT from another engine.
- // It cannot be accepted because the table data would be lost
- // except when the target file does not exist.
- strcpy(g->Message, "Operation denied. Table data would be lost.");
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
- } // endif outward
-
- // Get the index definitions
- if (xdp= GetIndexInfo()) {
- if (IsTypeIndexable(type)) {
- PDBUSER dup= PlgGetUser(g);
- PCATLG cat= (dup) ? dup->Catalog : NULL;
-
- if (cat) {
- cat->SetDataPath(g, table_arg->s->db.str);
-
- if ((rc= optimize(table->in_use, NULL))) {
- htrc("Create rc=%d %s\n", rc, g->Message);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- } else
- CloseTable(g);
-
- } // endif cat
-
- } else {
- sprintf(g->Message, "Table type %s is not indexable", options->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_UNSUPPORTED;
- } // endif Indexable
-
- } // endif xdp
-
- } else {
- // This should not happen anymore with indexing new way
- my_message(ER_UNKNOWN_ERROR,
- "CONNECT index modification should be in-place", MYF(0));
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
-#if 0
- PIXDEF xdp= GetIndexInfo();
- PCHK xcp= (PCHK)g->Xchk;
-
- if (xdp) {
- if (!IsTypeIndexable(type)) {
- g->Xchk= NULL;
- sprintf(g->Message, "Table type %s is not indexable", options->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- rc= HA_ERR_INTERNAL_ERROR;
- } else {
- xcp->newpix= xdp;
- xcp->newsep= GetBooleanOption("Sepindex", false);
- } // endif Indexable
-
- } else if (!xcp->oldpix)
- g->Xchk= NULL;
-
- if (xtrace && g->Xchk)
- htrc("oldsep=%d newsep=%d oldpix=%p newpix=%p\n",
- xcp->oldsep, xcp->newsep, xcp->oldpix, xcp->newpix);
-
-// if (g->Xchk &&
-// (sqlcom != SQLCOM_CREATE_INDEX && sqlcom != SQLCOM_DROP_INDEX)) {
- if (g->Xchk) {
- PIXDEF xp1, xp2;
- bool b= false; // true if index changes
-
- if (xcp->oldsep == xcp->newsep) {
- for (xp1= xcp->newpix, xp2= xcp->oldpix;
- xp1 || xp2;
- xp1= xp1->Next, xp2= xp2->Next)
- if (!xp1 || !xp2 || !IsSameIndex(xp1, xp2)) {
- b= true;
- break;
- } // endif xp1
-
- } else
- b= true;
-
- if (!b)
- g->Xchk= NULL;
-
-#if 0
- if (b) {
- // CONNECT does not support indexing via ALTER TABLE
- my_message(ER_UNKNOWN_ERROR,
- "CONNECT does not support index modification via ALTER TABLE",
- MYF(0));
- DBUG_RETURN(HA_ERR_UNSUPPORTED);
- } // endif b
-#endif // 0
-
- } // endif Xchk
-
-#endif // 0
- } // endif Xchk
-
- table= st;
- DBUG_RETURN(rc);
-} // end of create
-
-/**
- Used to check whether a file based outward table can be populated by
- an ALTER TABLE command. The conditions are:
- - file does not exist or is void
- - user has file privilege
-*/
-bool ha_connect::FileExists(const char *fn)
-{
- if (!fn || !*fn)
- return false;
-
- if (table) {
- char *s, filename[_MAX_PATH], path[128];
- int n;
- struct stat info;
-
- if (check_access(ha_thd(), FILE_ACL, table->s->db.str,
- NULL, NULL, 0, 0))
- return true;
-
-#if defined(WIN32)
- s= "\\";
-#else // !WIN32
- s= "/";
-#endif // !WIN32
-
- strcat(strcat(strcat(strcpy(path, "."), s), table->s->db.str), s);
- PlugSetPath(filename, fn, path);
- n= stat(filename, &info);
-
- if (n < 0) {
- if (errno != ENOENT) {
- char buf[_MAX_PATH + 20];
-
- sprintf(buf, "Error %d for file %s", errno, filename);
- push_warning(table->in_use, Sql_condition::WARN_LEVEL_WARN, 0, buf);
- return true;
- } else
- return false;
-
- } else
- return (info.st_size) ? true : false;
-
- } // endif table
-
- return true;
-} // end of FileExists
-
-// Called by SameString and NoFieldOptionChange
-bool ha_connect::CheckString(const char *str1, const char *str2)
-{
- bool b1= (!str1 || !*str1), b2= (!str2 || !*str2);
-
- if (b1 && b2)
- return true;
- else if ((b1 && !b2) || (!b1 && b2) || stricmp(str1, str2))
- return false;
-
- return true;
-} // end of CheckString
-
-/**
- check whether a string option have changed
- */
-bool ha_connect::SameString(TABLE *tab, char *opn)
-{
- char *str1, *str2;
-
- tshp= tab->s; // The altered table
- str1= GetStringOption(opn);
- tshp= NULL;
- str2= GetStringOption(opn);
- return CheckString(str1, str2);
-} // end of SameString
-
-/**
- check whether a Boolean option have changed
- */
-bool ha_connect::SameBool(TABLE *tab, char *opn)
-{
- bool b1, b2;
-
- tshp= tab->s; // The altered table
- b1= GetBooleanOption(opn, false);
- tshp= NULL;
- b2= GetBooleanOption(opn, false);
- return (b1 == b2);
-} // end of SameBool
-
-/**
- check whether an integer option have changed
- */
-bool ha_connect::SameInt(TABLE *tab, char *opn)
-{
- int i1, i2;
-
- tshp= tab->s; // The altered table
- i1= GetIntegerOption(opn);
- tshp= NULL;
- i2= GetIntegerOption(opn);
-
- if (!stricmp(opn, "lrecl"))
- return (i1 == i2 || !i1 || !i2);
- else if (!stricmp(opn, "ending"))
- return (i1 == i2 || i1 <= 0 || i2 <= 0);
- else
- return (i1 == i2);
-
-} // end of SameInt
-
-/**
- check whether a field option have changed
- */
-bool ha_connect::NoFieldOptionChange(TABLE *tab)
-{
- bool rc= true;
- ha_field_option_struct *fop1, *fop2;
- Field* *fld1= table->s->field;
- Field* *fld2= tab->s->field;
-
- for (; rc && *fld1 && *fld2; fld1++, fld2++) {
- fop1= (*fld1)->option_struct;
- fop2= (*fld2)->option_struct;
-
- rc= (fop1->offset == fop2->offset &&
- fop1->fldlen == fop2->fldlen &&
- CheckString(fop1->dateformat, fop2->dateformat) &&
- CheckString(fop1->fieldformat, fop2->fieldformat) &&
- CheckString(fop1->special, fop2->special));
- } // endfor fld
-
- return rc;
-} // end of NoFieldOptionChange
-
- /**
- Check if a storage engine supports a particular alter table in-place
-
- @param altered_table TABLE object for new version of table.
- @param ha_alter_info Structure describing changes to be done
- by ALTER TABLE and holding data used
- during in-place alter.
-
- @retval HA_ALTER_ERROR Unexpected error.
- @retval HA_ALTER_INPLACE_NOT_SUPPORTED Not supported, must use copy.
- @retval HA_ALTER_INPLACE_EXCLUSIVE_LOCK Supported, but requires X lock.
- @retval HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE
- Supported, but requires SNW lock
- during main phase. Prepare phase
- requires X lock.
- @retval HA_ALTER_INPLACE_SHARED_LOCK Supported, but requires SNW lock.
- @retval HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE
- Supported, concurrent reads/writes
- allowed. However, prepare phase
- requires X lock.
- @retval HA_ALTER_INPLACE_NO_LOCK Supported, concurrent
- reads/writes allowed.
-
- @note The default implementation uses the old in-place ALTER API
- to determine if the storage engine supports in-place ALTER or not.
-
- @note Called without holding thr_lock.c lock.
- */
-enum_alter_inplace_result
-ha_connect::check_if_supported_inplace_alter(TABLE *altered_table,
- Alter_inplace_info *ha_alter_info)
-{
- DBUG_ENTER("check_if_supported_alter");
-
- bool idx= false, outward= false;
- THD *thd= ha_thd();
- int sqlcom= thd_sql_command(thd);
- TABTYPE newtyp, type= TAB_UNDEF;
- HA_CREATE_INFO *create_info= ha_alter_info->create_info;
-//PTOS pos= GetTableOptionStruct(table);
- PTOS newopt, oldopt;
- xp= GetUser(thd, xp);
- PGLOBAL g= xp->g;
-
- if (!g || !table) {
- my_message(ER_UNKNOWN_ERROR, "Cannot check ALTER operations", MYF(0));
- DBUG_RETURN(HA_ALTER_ERROR);
- } // endif Xchk
-
- newopt= altered_table->s->option_struct;
- oldopt= table->s->option_struct;
-
- // If this is the start of a new query, cleanup the previous one
- if (xp->CheckCleanup()) {
- tdbp= NULL;
- valid_info= false;
- } // endif CheckCleanup
-
- g->Alchecked= 1; // Tested in create
- g->Xchk= NULL;
- type= GetRealType(oldopt);
- newtyp= GetRealType(newopt);
-
- // No copy algorithm for outward tables
- outward= (!IsFileType(type) || (oldopt->filename && *oldopt->filename));
-
- // Index operations
- Alter_inplace_info::HA_ALTER_FLAGS index_operations=
- Alter_inplace_info::ADD_INDEX |
- Alter_inplace_info::DROP_INDEX |
- Alter_inplace_info::ADD_UNIQUE_INDEX |
- Alter_inplace_info::DROP_UNIQUE_INDEX |
- Alter_inplace_info::ADD_PK_INDEX |
- Alter_inplace_info::DROP_PK_INDEX;
-
- Alter_inplace_info::HA_ALTER_FLAGS inplace_offline_operations=
- Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH |
- Alter_inplace_info::ALTER_COLUMN_NAME |
- Alter_inplace_info::ALTER_COLUMN_DEFAULT |
- Alter_inplace_info::CHANGE_CREATE_OPTION |
- Alter_inplace_info::ALTER_RENAME | index_operations;
-
- if (ha_alter_info->handler_flags & index_operations ||
- !SameString(altered_table, "optname") ||
- !SameBool(altered_table, "sepindex")) {
- if (!IsTypeIndexable(type)) {
- sprintf(g->Message, "Table type %s is not indexable", oldopt->type);
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ALTER_ERROR);
- } // endif Indexable
-
- g->Xchk= new(g) XCHK;
- PCHK xcp= (PCHK)g->Xchk;
-
- xcp->oldpix= GetIndexInfo(table->s);
- xcp->newpix= GetIndexInfo(altered_table->s);
- xcp->oldsep= GetBooleanOption("sepindex", false);
- xcp->oldsep= xcp->SetName(g, GetStringOption("optname"));
- tshp= altered_table->s;
- xcp->newsep= GetBooleanOption("sepindex", false);
- xcp->newsep= xcp->SetName(g, GetStringOption("optname"));
- tshp= NULL;
-
- if (xtrace && g->Xchk)
- htrc(
- "oldsep=%d newsep=%d oldopn=%s newopn=%s oldpix=%p newpix=%p\n",
- xcp->oldsep, xcp->newsep,
- SVP(xcp->oldopn), SVP(xcp->newopn),
- xcp->oldpix, xcp->newpix);
-
- if (sqlcom == SQLCOM_ALTER_TABLE)
- idx= true;
- else
- DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
-
- } // endif index operation
-
- if (!SameString(altered_table, "filename")) {
- if (!outward) {
- // Conversion to outward table is only allowed for file based
- // tables whose file does not exist.
- tshp= altered_table->s;
- char *fn= GetStringOption("filename");
- tshp= NULL;
-
- if (FileExists(fn)) {
- strcpy(g->Message, "Operation denied. Table data would be lost.");
- my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
- DBUG_RETURN(HA_ALTER_ERROR);
- } else
- goto fin;
-
- } else
- goto fin;
-
- } // endif filename
-
- /* Is there at least one operation that requires copy algorithm? */
- if (ha_alter_info->handler_flags & ~inplace_offline_operations)
- goto fin;
-
- /*
- ALTER TABLE tbl_name CONVERT TO CHARACTER SET .. and
- ALTER TABLE table_name DEFAULT CHARSET = .. most likely
- change column charsets and so not supported in-place through
- old API.
-
- Changing of PACK_KEYS, MAX_ROWS and ROW_FORMAT options were
- not supported as in-place operations in old API either.
- */
- if (create_info->used_fields & (HA_CREATE_USED_CHARSET |
- HA_CREATE_USED_DEFAULT_CHARSET |
- HA_CREATE_USED_PACK_KEYS |
- HA_CREATE_USED_MAX_ROWS) ||
- (table->s->row_type != create_info->row_type))
- goto fin;
-
-#if 0
- uint table_changes= (ha_alter_info->handler_flags &
- Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH) ?
- IS_EQUAL_PACK_LENGTH : IS_EQUAL_YES;
-
- if (table->file->check_if_incompatible_data(create_info, table_changes)
- == COMPATIBLE_DATA_YES)
- DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
-#endif // 0
-
- // This was in check_if_incompatible_data
- if (NoFieldOptionChange(altered_table) &&
- type == newtyp &&
- SameInt(altered_table, "lrecl") &&
- SameInt(altered_table, "elements") &&
- SameInt(altered_table, "header") &&
- SameInt(altered_table, "quoted") &&
- SameInt(altered_table, "ending") &&
- SameInt(altered_table, "compressed"))
- DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
-
-fin:
- if (idx) {
- // Indexing is only supported inplace
- my_message(ER_ALTER_OPERATION_NOT_SUPPORTED,
- "Alter operations not supported together by CONNECT", MYF(0));
- DBUG_RETURN(HA_ALTER_ERROR);
- } else if (outward) {
- push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
- "This is an outward table, table data were not modified.");
- DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
- } else
- DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
-
-} // end of check_if_supported_inplace_alter
-
-
-/**
- check_if_incompatible_data() called if ALTER TABLE can't detect otherwise
- if new and old definition are compatible
-
- @details If there are no other explicit signs like changed number of
- fields this function will be called by compare_tables()
- (sql/sql_tables.cc) to decide should we rewrite whole table or only .frm
- file.
-
- @note: This function is no more called by check_if_supported_inplace_alter
-*/
-
-bool ha_connect::check_if_incompatible_data(HA_CREATE_INFO *info,
- uint table_changes)
-{
- DBUG_ENTER("ha_connect::check_if_incompatible_data");
- // TO DO: really implement and check it.
- push_warning(ha_thd(), Sql_condition::WARN_LEVEL_WARN, 0,
- "Unexpected call to check_if_incompatible_data.");
- DBUG_RETURN(COMPATIBLE_DATA_NO);
-} // end of check_if_incompatible_data
-
-/****************************************************************************
- * CONNECT MRR implementation: use DS-MRR
- This is just copied from myisam
- ***************************************************************************/
-
-int ha_connect::multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
- uint n_ranges, uint mode,
- HANDLER_BUFFER *buf)
-{
- return ds_mrr.dsmrr_init(this, seq, seq_init_param, n_ranges, mode, buf);
-} // end of multi_range_read_init
-
-int ha_connect::multi_range_read_next(range_id_t *range_info)
-{
- return ds_mrr.dsmrr_next(range_info);
-} // end of multi_range_read_next
-
-ha_rows ha_connect::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
- void *seq_init_param,
- uint n_ranges, uint *bufsz,
- uint *flags, Cost_estimate *cost)
-{
- /*
- This call is here because there is no location where this->table would
- already be known.
- TODO: consider moving it into some per-query initialization call.
- */
- ds_mrr.init(this, table);
-
- // MMR is implemented for "local" file based tables only
- if (!IsFileType(GetRealType(GetTableOptionStruct(table))))
- *flags|= HA_MRR_USE_DEFAULT_IMPL;
-
- ha_rows rows= ds_mrr.dsmrr_info_const(keyno, seq, seq_init_param, n_ranges,
- bufsz, flags, cost);
- xp->g->Mrr= !(*flags & HA_MRR_USE_DEFAULT_IMPL);
- return rows;
-} // end of multi_range_read_info_const
-
-ha_rows ha_connect::multi_range_read_info(uint keyno, uint n_ranges, uint keys,
- uint key_parts, uint *bufsz,
- uint *flags, Cost_estimate *cost)
-{
- ds_mrr.init(this, table);
-
- // MMR is implemented for "local" file based tables only
- if (!IsFileType(GetRealType(GetTableOptionStruct(table))))
- *flags|= HA_MRR_USE_DEFAULT_IMPL;
-
- ha_rows rows= ds_mrr.dsmrr_info(keyno, n_ranges, keys, key_parts, bufsz,
- flags, cost);
- xp->g->Mrr= !(*flags & HA_MRR_USE_DEFAULT_IMPL);
- return rows;
-} // end of multi_range_read_info
-
-
-int ha_connect::multi_range_read_explain_info(uint mrr_mode, char *str,
- size_t size)
-{
- return ds_mrr.dsmrr_explain_info(mrr_mode, str, size);
-} // end of multi_range_read_explain_info
-
-/* CONNECT MRR implementation ends */
-
-#if 0
-// Does this make sens for CONNECT?
-Item *ha_connect::idx_cond_push(uint keyno_arg, Item* idx_cond_arg)
-{
- pushed_idx_cond_keyno= keyno_arg;
- pushed_idx_cond= idx_cond_arg;
- in_range_check_pushed_down= TRUE;
- if (active_index == pushed_idx_cond_keyno)
- mi_set_index_cond_func(file, handler_index_cond_check, this);
- return NULL;
-}
-#endif // 0
-
-
-struct st_mysql_storage_engine connect_storage_engine=
-{ MYSQL_HANDLERTON_INTERFACE_VERSION };
-
-maria_declare_plugin(connect)
-{
- MYSQL_STORAGE_ENGINE_PLUGIN,
- &connect_storage_engine,
- "CONNECT",
- "Olivier Bertrand",
- "Management of External Data (SQL/MED), including many file formats",
- PLUGIN_LICENSE_GPL,
- connect_init_func, /* Plugin Init */
- connect_done_func, /* Plugin Deinit */
- 0x0103, /* version number (1.03) */
- NULL, /* status variables */
- NULL, /* system variables */
- "1.03", /* string version */
- MariaDB_PLUGIN_MATURITY_BETA /* maturity */
-}
-maria_declare_plugin_end;
+/* Copyright (C) Olivier Bertrand 2004 - 2014
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+/**
+ @file ha_connect.cc
+
+ @brief
+ The ha_connect engine is a stubbed storage engine that enables to create tables
+ based on external data. Principally they are based on plain files of many
+ different types, but also on collections of such files, collection of tables,
+ ODBC tables retrieving data from other DBMS having an ODBC server, and even
+ virtual tables.
+
+ @details
+ ha_connect will let you create/open/delete tables, the created table can be
+ done specifying an already existing file, the drop table command will just
+ suppress the table definition but not the eventual data file.
+ Indexes are not supported for all table types but data can be inserted,
+ updated or deleted.
+
+ You can enable the CONNECT storage engine in your build by doing the
+ following during your build process:<br> ./configure
+ --with-connect-storage-engine
+
+ You can install the CONNECT handler as all other storage handlers.
+
+ Once this is done, MySQL will let you create tables with:<br>
+ CREATE TABLE <table name> (...) ENGINE=CONNECT;
+
+ The example storage engine does not use table locks. It
+ implements an example "SHARE" that is inserted into a hash by table
+ name. This is not used yet.
+
+ Please read the object definition in ha_connect.h before reading the rest
+ of this file.
+
+ @note
+ This MariaDB CONNECT handler is currently an adaptation of the XDB handler
+ that was written for MySQL version 4.1.2-alpha. Its overall design should
+ be enhanced in the future to meet MariaDB requirements.
+
+ @note
+ It was written also from the Brian's ha_example handler and contains parts
+ of it that are there but not currently used, such as table variables.
+
+ @note
+ When you create an CONNECT table, the MySQL Server creates a table .frm
+ (format) file in the database directory, using the table name as the file
+ name as is customary with MySQL. No other files are created. To get an idea
+ of what occurs, here is an example select that would do a scan of an entire
+ table:
+
+ @code
+ ha-connect::open
+ ha_connect::store_lock
+ ha_connect::external_lock
+ ha_connect::info
+ ha_connect::rnd_init
+ ha_connect::extra
+ ENUM HA_EXTRA_CACHE Cache record in HA_rrnd()
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::rnd_next
+ ha_connect::extra
+ ENUM HA_EXTRA_NO_CACHE End caching of records (def)
+ ha_connect::external_lock
+ ha_connect::extra
+ ENUM HA_EXTRA_RESET Reset database to after open
+ @endcode
+
+ Here you see that the connect storage engine has 9 rows called before
+ rnd_next signals that it has reached the end of its data. Calls to
+ ha_connect::extra() are hints as to what will be occuring to the request.
+
+ Happy use!<br>
+ -Olivier
+*/
+
+#ifdef USE_PRAGMA_IMPLEMENTATION
+#pragma implementation // gcc: Class implementation
+#endif
+
+#define MYSQL_SERVER 1
+#define DONT_DEFINE_VOID
+//#include "sql_partition.h"
+#include "sql_class.h"
+#include "create_options.h"
+#include "mysql_com.h"
+#include "field.h"
+#include "sql_parse.h"
+#include "sql_base.h"
+#include <sys/stat.h>
+#if defined(NEW_WAY)
+#include "sql_table.h"
+#endif // NEW_WAY
+#undef OFFSET
+
+#define NOPARSE
+#if defined(UNIX)
+#include "osutil.h"
+#endif // UNIX
+#include "global.h"
+#include "plgdbsem.h"
+#if defined(ODBC_SUPPORT)
+#include "odbccat.h"
+#endif // ODBC_SUPPORT
+#if defined(MYSQL_SUPPORT)
+#include "xtable.h"
+#include "tabmysql.h"
+#endif // MYSQL_SUPPORT
+#include "filamdbf.h"
+#include "tabxcl.h"
+#include "tabfmt.h"
+#include "reldef.h"
+#include "tabcol.h"
+#include "xindex.h"
+#if defined(WIN32)
+#include <io.h>
+#include "tabwmi.h"
+#endif // WIN32
+#include "connect.h"
+#include "user_connect.h"
+#include "ha_connect.h"
+#include "mycat.h"
+#include "myutil.h"
+#include "preparse.h"
+#include "inihandl.h"
+#if defined(LIBXML2_SUPPORT)
+#include "libdoc.h"
+#endif // LIBXML2_SUPPORT
+#include "taboccur.h"
+#include "tabpivot.h"
+
+#define my_strupr(p) my_caseup_str(default_charset_info, (p));
+#define my_strlwr(p) my_casedn_str(default_charset_info, (p));
+#define my_stricmp(a,b) my_strcasecmp(default_charset_info, (a), (b))
+
+
+/***********************************************************************/
+/* Initialize the ha_connect static members. */
+/***********************************************************************/
+//efine CONNECT_INI "connect.ini"
+extern "C" {
+ char version[]= "Version 1.02.0001 February 03, 2014";
+
+#if defined(XMSG)
+ char msglang[]; // Default message language
+#endif
+ int trace= 0; // The general trace value
+} // extern "C"
+
+static int xtrace= 0;
+
+ulong ha_connect::num= 0;
+//int DTVAL::Shift= 0;
+
+/***********************************************************************/
+/* Utility functions. */
+/***********************************************************************/
+PQRYRES OEMColumns(PGLOBAL g, PTOS topt, char *tab, char *db, bool info);
+
+static PCONNECT GetUser(THD *thd, PCONNECT xp);
+static PGLOBAL GetPlug(THD *thd, PCONNECT& lxp);
+
+static handler *connect_create_handler(handlerton *hton,
+ TABLE_SHARE *table,
+ MEM_ROOT *mem_root);
+
+static int connect_assisted_discovery(handlerton *hton, THD* thd,
+ TABLE_SHARE *table_s,
+ HA_CREATE_INFO *info);
+
+static void update_connect_xtrace(MYSQL_THD thd,
+ struct st_mysql_sys_var *var,
+ void *var_ptr, const void *save)
+{
+ xtrace= *(int *)save;
+//xtrace= *(int *)var_ptr= *(int *)save;
+} // end of update_connect_xtrace
+
+handlerton *connect_hton;
+
+/**
+ CREATE TABLE option list (table options)
+
+ These can be specified in the CREATE TABLE:
+ CREATE TABLE ( ... ) {...here...}
+*/
+ha_create_table_option connect_table_option_list[]=
+{
+ HA_TOPTION_STRING("TABLE_TYPE", type),
+ HA_TOPTION_STRING("FILE_NAME", filename),
+ HA_TOPTION_STRING("XFILE_NAME", optname),
+//HA_TOPTION_STRING("CONNECT_STRING", connect),
+ HA_TOPTION_STRING("TABNAME", tabname),
+ HA_TOPTION_STRING("TABLE_LIST", tablist),
+ HA_TOPTION_STRING("DBNAME", dbname),
+ HA_TOPTION_STRING("SEP_CHAR", separator),
+ HA_TOPTION_STRING("QCHAR", qchar),
+ HA_TOPTION_STRING("MODULE", module),
+ HA_TOPTION_STRING("SUBTYPE", subtype),
+ HA_TOPTION_STRING("CATFUNC", catfunc),
+ HA_TOPTION_STRING("SRCDEF", srcdef),
+ HA_TOPTION_STRING("COLIST", colist),
+ HA_TOPTION_STRING("OPTION_LIST", oplist),
+ HA_TOPTION_STRING("DATA_CHARSET", data_charset),
+ HA_TOPTION_NUMBER("LRECL", lrecl, 0, 0, INT_MAX32, 1),
+ HA_TOPTION_NUMBER("BLOCK_SIZE", elements, 0, 0, INT_MAX32, 1),
+//HA_TOPTION_NUMBER("ESTIMATE", estimate, 0, 0, INT_MAX32, 1),
+ HA_TOPTION_NUMBER("MULTIPLE", multiple, 0, 0, 2, 1),
+ HA_TOPTION_NUMBER("HEADER", header, 0, 0, 3, 1),
+ HA_TOPTION_NUMBER("QUOTED", quoted, (ulonglong) -1, 0, 3, 1),
+ HA_TOPTION_NUMBER("ENDING", ending, (ulonglong) -1, 0, INT_MAX32, 1),
+ HA_TOPTION_NUMBER("COMPRESS", compressed, 0, 0, 2, 1),
+//HA_TOPTION_BOOL("COMPRESS", compressed, 0),
+ HA_TOPTION_BOOL("MAPPED", mapped, 0),
+ HA_TOPTION_BOOL("HUGE", huge, 0),
+ HA_TOPTION_BOOL("SPLIT", split, 0),
+ HA_TOPTION_BOOL("READONLY", readonly, 0),
+ HA_TOPTION_BOOL("SEPINDEX", sepindex, 0),
+ HA_TOPTION_END
+};
+
+
+/**
+ CREATE TABLE option list (field options)
+
+ These can be specified in the CREATE TABLE per field:
+ CREATE TABLE ( field ... {...here...}, ... )
+*/
+ha_create_table_option connect_field_option_list[]=
+{
+ HA_FOPTION_NUMBER("FLAG", offset, (ulonglong) -1, 0, INT_MAX32, 1),
+ HA_FOPTION_NUMBER("MAX_DIST", freq, 0, 0, INT_MAX32, 1), // BLK_INDX
+ HA_FOPTION_NUMBER("DISTRIB", opt, 0, 0, 2, 1), // used for BLK_INDX
+ HA_FOPTION_NUMBER("FIELD_LENGTH", fldlen, 0, 0, INT_MAX32, 1),
+ HA_FOPTION_STRING("DATE_FORMAT", dateformat),
+ HA_FOPTION_STRING("FIELD_FORMAT", fieldformat),
+ HA_FOPTION_STRING("SPECIAL", special),
+ HA_FOPTION_END
+};
+
+/***********************************************************************/
+/* Push G->Message as a MySQL warning. */
+/***********************************************************************/
+bool PushWarning(PGLOBAL g, PTDBASE tdbp, int level)
+ {
+ PHC phc;
+ THD *thd;
+ MYCAT *cat= (MYCAT*)tdbp->GetDef()->GetCat();
+ Sql_condition::enum_warning_level wlvl;
+
+
+ if (!cat || !(phc= cat->GetHandler()) || !phc->GetTable() ||
+ !(thd= (phc->GetTable())->in_use))
+ return true;
+
+//push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ wlvl= (Sql_condition::enum_warning_level)level;
+ push_warning(thd, wlvl, 0, g->Message);
+ return false;
+ } // end of PushWarning
+
+#ifdef HAVE_PSI_INTERFACE
+static PSI_mutex_key con_key_mutex_CONNECT_SHARE_mutex;
+
+static PSI_mutex_info all_connect_mutexes[]=
+{
+ { &con_key_mutex_CONNECT_SHARE_mutex, "CONNECT_SHARE::mutex", 0}
+};
+
+static void init_connect_psi_keys()
+{
+ const char* category= "connect";
+ int count;
+
+ if (PSI_server == NULL)
+ return;
+
+ count= array_elements(all_connect_mutexes);
+ PSI_server->register_mutex(category, all_connect_mutexes, count);
+}
+#else
+static void init_connect_psi_keys() {}
+#endif
+
+
+DllExport LPCSTR PlugSetPath(LPSTR to, LPCSTR name, LPCSTR dir)
+{
+ const char *res= PlugSetPath(to, mysql_data_home, name, dir);
+ return res;
+}
+
+
+/**
+ @brief
+ If frm_error() is called then we will use this to determine
+ the file extensions that exist for the storage engine. This is also
+ used by the default rename_table and delete_table method in
+ handler.cc.
+
+ For engines that have two file name extentions (separate meta/index file
+ and data file), the order of elements is relevant. First element of engine
+ file name extentions array should be meta/index file extention. Second
+ element - data file extention. This order is assumed by
+ prepare_for_repair() when REPAIR TABLE ... USE_FRM is issued.
+
+ @see
+ rename_table method in handler.cc and
+ delete_table method in handler.cc
+*/
+static const char *ha_connect_exts[]= {
+ ".dos", ".fix", ".csv", ".bin", ".fmt", ".dbf", ".xml", ".ini", ".vec",
+ ".dnx", ".fnx", ".bnx", ".vnx", ".dbx", ".dop", ".fop", ".bop", ".vop",
+ NULL};
+
+/**
+ @brief
+ Plugin initialization
+*/
+static int connect_init_func(void *p)
+{
+ DBUG_ENTER("connect_init_func");
+
+ sql_print_information("CONNECT: %s", version);
+
+ // xtrace is now a system variable
+ trace= xtrace;
+
+#ifdef LIBXML2_SUPPORT
+ XmlInitParserLib();
+#endif // LIBXML2_SUPPORT
+
+ init_connect_psi_keys();
+
+ connect_hton= (handlerton *)p;
+ connect_hton->state= SHOW_OPTION_YES;
+ connect_hton->create= connect_create_handler;
+ connect_hton->flags= HTON_TEMPORARY_NOT_SUPPORTED | HTON_NO_PARTITION;
+ connect_hton->table_options= connect_table_option_list;
+ connect_hton->field_options= connect_field_option_list;
+ connect_hton->tablefile_extensions= ha_connect_exts;
+ connect_hton->discover_table_structure= connect_assisted_discovery;
+
+ if (xtrace)
+ sql_print_information("connect_init: hton=%p", p);
+
+ DTVAL::SetTimeShift(); // Initialize time zone shift once for all
+ DBUG_RETURN(0);
+}
+
+
+/**
+ @brief
+ Plugin clean up
+*/
+static int connect_done_func(void *p)
+{
+ int error= 0;
+ PCONNECT pc, pn;
+ DBUG_ENTER("connect_done_func");
+
+#ifdef LIBXML2_SUPPORT
+ XmlCleanupParserLib();
+#endif // LIBXML2_SUPPORT
+
+#if !defined(WIN32)
+//PROFILE_End(); Causes signal 11
+#endif // !WIN32
+
+ for (pc= user_connect::to_users; pc; pc= pn) {
+ if (pc->g)
+ PlugCleanup(pc->g, true);
+
+ pn= pc->next;
+ delete pc;
+ } // endfor pc
+
+ DBUG_RETURN(error);
+}
+
+
+/**
+ @brief
+ Example of simple lock controls. The "share" it creates is a
+ structure we will pass to each example handler. Do you have to have
+ one of these? Well, you have pieces that are used for locking, and
+ they are needed to function.
+*/
+
+CONNECT_SHARE *ha_connect::get_share()
+{
+ CONNECT_SHARE *tmp_share;
+ lock_shared_ha_data();
+ if (!(tmp_share= static_cast<CONNECT_SHARE*>(get_ha_share_ptr())))
+ {
+ tmp_share= new CONNECT_SHARE;
+ if (!tmp_share)
+ goto err;
+ mysql_mutex_init(con_key_mutex_CONNECT_SHARE_mutex,
+ &tmp_share->mutex, MY_MUTEX_INIT_FAST);
+ set_ha_share_ptr(static_cast<Handler_share*>(tmp_share));
+ }
+err:
+ unlock_shared_ha_data();
+ return tmp_share;
+}
+
+
+static handler* connect_create_handler(handlerton *hton,
+ TABLE_SHARE *table,
+ MEM_ROOT *mem_root)
+{
+ handler *h= new (mem_root) ha_connect(hton, table);
+
+ if (xtrace)
+ htrc("New CONNECT %p, table: %s\n",
+ h, table ? table->table_name.str : "<null>");
+
+ return h;
+} // end of connect_create_handler
+
+/****************************************************************************/
+/* ha_connect constructor. */
+/****************************************************************************/
+ha_connect::ha_connect(handlerton *hton, TABLE_SHARE *table_arg)
+ :handler(hton, table_arg)
+{
+ hnum= ++num;
+ xp= (table) ? GetUser(ha_thd(), NULL) : NULL;
+ if (xp)
+ xp->SetHandler(this);
+ tdbp= NULL;
+ sdvalin= NULL;
+ sdvalout= NULL;
+ xmod= MODE_ANY;
+ istable= false;
+//*tname= '\0';
+ bzero((char*) &xinfo, sizeof(XINFO));
+ valid_info= false;
+ valid_query_id= 0;
+ creat_query_id= (table && table->in_use) ? table->in_use->query_id : 0;
+ stop= false;
+ alter= false;
+ mrr= false;
+ indexing= -1;
+ locked= 0;
+ data_file_name= NULL;
+ index_file_name= NULL;
+ enable_activate_all_index= 0;
+ int_table_flags= (HA_NO_TRANSACTIONS | HA_NO_PREFIX_CHAR_KEYS);
+ ref_length= sizeof(int);
+ share= NULL;
+ tshp= NULL;
+} // end of ha_connect constructor
+
+
+/****************************************************************************/
+/* ha_connect destructor. */
+/****************************************************************************/
+ha_connect::~ha_connect(void)
+{
+ if (xtrace)
+ htrc("Delete CONNECT %p, table: %s, xp=%p count=%d\n", this,
+ table ? table->s->table_name.str : "<null>",
+ xp, xp ? xp->count : 0);
+
+ if (xp) {
+ PCONNECT p;
+
+ xp->count--;
+
+ for (p= user_connect::to_users; p; p= p->next)
+ if (p == xp)
+ break;
+
+ if (p && !p->count) {
+ if (p->next)
+ p->next->previous= p->previous;
+
+ if (p->previous)
+ p->previous->next= p->next;
+ else
+ user_connect::to_users= p->next;
+
+ } // endif p
+
+ if (!xp->count) {
+ PlugCleanup(xp->g, true);
+ delete xp;
+ } // endif count
+
+ } // endif xp
+
+} // end of ha_connect destructor
+
+
+/****************************************************************************/
+/* Get a pointer to the user of this handler. */
+/****************************************************************************/
+static PCONNECT GetUser(THD *thd, PCONNECT xp)
+{
+ const char *dbn= NULL;
+
+ if (!thd)
+ return NULL;
+
+ if (xp && thd == xp->thdp)
+ return xp;
+
+ for (xp= user_connect::to_users; xp; xp= xp->next)
+ if (thd == xp->thdp)
+ break;
+
+ if (!xp) {
+ xp= new user_connect(thd, dbn);
+
+ if (xp->user_init()) {
+ delete xp;
+ xp= NULL;
+ } // endif user_init
+
+ } else
+ xp->count++;
+
+ return xp;
+} // end of GetUser
+
+
+/****************************************************************************/
+/* Get the global pointer of the user of this handler. */
+/****************************************************************************/
+static PGLOBAL GetPlug(THD *thd, PCONNECT& lxp)
+{
+ lxp= GetUser(thd, lxp);
+ return (lxp) ? lxp->g : NULL;
+} // end of GetPlug
+
+/****************************************************************************/
+/* Get the implied table type. */
+/****************************************************************************/
+TABTYPE ha_connect::GetRealType(PTOS pos)
+{
+ TABTYPE type= GetTypeID(pos->type);
+
+ if (type == TAB_UNDEF)
+ type= pos->srcdef ? TAB_MYSQL : pos->tabname ? TAB_PRX : TAB_DOS;
+
+ return type;
+} // end of GetRealType
+
+/** @brief
+ This is a list of flags that indicate what functionality the storage
+ engine implements. The current table flags are documented in handler.h
+*/
+ulonglong ha_connect::table_flags() const
+{
+ ulonglong flags= HA_CAN_VIRTUAL_COLUMNS | HA_REC_NOT_IN_SEQ |
+ HA_NO_AUTO_INCREMENT | HA_NO_PREFIX_CHAR_KEYS |
+ HA_BINLOG_ROW_CAPABLE | HA_BINLOG_STMT_CAPABLE |
+ HA_PARTIAL_COLUMN_READ |
+// HA_NULL_IN_KEY | not implemented yet
+// HA_FAST_KEY_READ | causes error when sorting (???)
+ HA_NO_TRANSACTIONS | HA_DUPLICATE_KEY_NOT_IN_ORDER |
+ HA_NO_BLOBS | HA_MUST_USE_TABLE_CONDITION_PUSHDOWN;
+ ha_connect *hp= (ha_connect*)this;
+ PTOS pos= hp->GetTableOptionStruct(table);
+
+ if (pos) {
+ TABTYPE type= hp->GetRealType(pos);
+
+ if (IsFileType(type))
+ flags|= HA_FILE_BASED;
+
+ if (IsExactType(type))
+ flags|= (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT);
+
+ // No data change on ALTER for outward tables
+ if (!IsFileType(type) || hp->FileExists(pos->filename))
+ flags|= HA_NO_COPY_ON_ALTER;
+
+ } // endif pos
+
+ return flags;
+} // end of table_flags
+
+/****************************************************************************/
+/* Return the value of an option specified in the option list. */
+/****************************************************************************/
+char *GetListOption(PGLOBAL g, const char *opname,
+ const char *oplist, const char *def)
+{
+ char key[16], val[256];
+ char *pk, *pv, *pn;
+ char *opval= (char*) def;
+ int n;
+
+ for (pk= (char*)oplist; pk; pk= ++pn) {
+ pn= strchr(pk, ',');
+ pv= strchr(pk, '=');
+
+ if (pv && (!pn || pv < pn)) {
+ n= pv - pk;
+ memcpy(key, pk, n);
+ key[n]= 0;
+ pv++;
+
+ if (pn) {
+ n= pn - pv;
+ memcpy(val, pv, n);
+ val[n]= 0;
+ } else
+ strcpy(val, pv);
+
+ } else {
+ if (pn) {
+ n= min(pn - pk, 15);
+ memcpy(key, pk, n);
+ key[n]= 0;
+ } else
+ strcpy(key, pk);
+
+ val[0]= 0;
+ } // endif pv
+
+ if (!stricmp(opname, key)) {
+ opval= (char*)PlugSubAlloc(g, NULL, strlen(val) + 1);
+ strcpy(opval, val);
+ break;
+ } else if (!pn)
+ break;
+
+ } // endfor pk
+
+ return opval;
+} // end of GetListOption
+
+/****************************************************************************/
+/* Return the table option structure. */
+/****************************************************************************/
+PTOS ha_connect::GetTableOptionStruct(TABLE *tab)
+{
+ return (tshp) ? tshp->option_struct :
+ (tab) ? tab->s->option_struct : NULL;
+} // end of GetTableOptionStruct
+
+/****************************************************************************/
+/* Return the value of a string option or NULL if not specified. */
+/****************************************************************************/
+char *ha_connect::GetStringOption(char *opname, char *sdef)
+{
+ char *opval= NULL;
+ PTOS options= GetTableOptionStruct(table);
+
+ if (!options)
+ ;
+ else if (!stricmp(opname, "Type"))
+ opval= (char*)options->type;
+ else if (!stricmp(opname, "Filename"))
+ opval= (char*)options->filename;
+ else if (!stricmp(opname, "Optname"))
+ opval= (char*)options->optname;
+ else if (!stricmp(opname, "Tabname"))
+ opval= (char*)options->tabname;
+ else if (!stricmp(opname, "Tablist"))
+ opval= (char*)options->tablist;
+ else if (!stricmp(opname, "Database") ||
+ !stricmp(opname, "DBname"))
+ opval= (char*)options->dbname;
+ else if (!stricmp(opname, "Separator"))
+ opval= (char*)options->separator;
+ else if (!stricmp(opname, "Connect"))
+ opval= (tshp) ? tshp->connect_string.str : table->s->connect_string.str;
+ else if (!stricmp(opname, "Qchar"))
+ opval= (char*)options->qchar;
+ else if (!stricmp(opname, "Module"))
+ opval= (char*)options->module;
+ else if (!stricmp(opname, "Subtype"))
+ opval= (char*)options->subtype;
+ else if (!stricmp(opname, "Catfunc"))
+ opval= (char*)options->catfunc;
+ else if (!stricmp(opname, "Srcdef"))
+ opval= (char*)options->srcdef;
+ else if (!stricmp(opname, "Colist"))
+ opval= (char*)options->colist;
+ else if (!stricmp(opname, "Data_charset"))
+ opval= (char*)options->data_charset;
+ else if (!stricmp(opname, "Query_String"))
+ opval= thd_query_string(table->in_use)->str;
+
+ if (!opval && options && options->oplist)
+ opval= GetListOption(xp->g, opname, options->oplist);
+
+ if (!opval) {
+ if (sdef && !strcmp(sdef, "*")) {
+ // Return the handler default value
+ if (!stricmp(opname, "Dbname") || !stricmp(opname, "Database"))
+ opval= (char*)GetDBName(NULL); // Current database
+ else if (!stricmp(opname, "Type")) // Default type
+ opval= (!options) ? NULL :
+ (options->srcdef) ? (char*)"MYSQL" :
+ (options->tabname) ? (char*)"PROXY" : (char*)"DOS";
+ else if (!stricmp(opname, "User")) // Connected user
+ opval= (char *) "root";
+ else if (!stricmp(opname, "Host")) // Connected user host
+ opval= (char *) "localhost";
+ else
+ opval= sdef; // Caller default
+
+ } else
+ opval= sdef; // Caller default
+
+ } // endif !opval
+
+ return opval;
+} // end of GetStringOption
+
+/****************************************************************************/
+/* Return the value of a Boolean option or bdef if not specified. */
+/****************************************************************************/
+bool ha_connect::GetBooleanOption(char *opname, bool bdef)
+{
+ bool opval= bdef;
+ char *pv;
+ PTOS options= GetTableOptionStruct(table);
+
+ if (!stricmp(opname, "View"))
+ opval= (tshp) ? tshp->is_view : table->s->is_view;
+ else if (!options)
+ ;
+ else if (!stricmp(opname, "Mapped"))
+ opval= options->mapped;
+ else if (!stricmp(opname, "Huge"))
+ opval= options->huge;
+//else if (!stricmp(opname, "Compressed"))
+// opval= options->compressed;
+ else if (!stricmp(opname, "Split"))
+ opval= options->split;
+ else if (!stricmp(opname, "Readonly"))
+ opval= options->readonly;
+ else if (!stricmp(opname, "SepIndex"))
+ opval= options->sepindex;
+ else if (options->oplist)
+ if ((pv= GetListOption(xp->g, opname, options->oplist)))
+ opval= (!*pv || *pv == 'y' || *pv == 'Y' || atoi(pv) != 0);
+
+ return opval;
+} // end of GetBooleanOption
+
+/****************************************************************************/
+/* Set the value of the opname option (does not work for oplist options) */
+/* Currently used only to set the Sepindex value. */
+/****************************************************************************/
+bool ha_connect::SetBooleanOption(char *opname, bool b)
+{
+ PTOS options= GetTableOptionStruct(table);
+
+ if (!options)
+ return true;
+
+ if (!stricmp(opname, "SepIndex"))
+ options->sepindex= b;
+ else
+ return true;
+
+ return false;
+} // end of SetBooleanOption
+
+/****************************************************************************/
+/* Return the value of an integer option or NO_IVAL if not specified. */
+/****************************************************************************/
+int ha_connect::GetIntegerOption(char *opname)
+{
+ ulonglong opval= NO_IVAL;
+ char *pv;
+ PTOS options= GetTableOptionStruct(table);
+
+ if (!options)
+ ;
+ else if (!stricmp(opname, "Lrecl"))
+ opval= options->lrecl;
+ else if (!stricmp(opname, "Elements"))
+ opval= options->elements;
+ else if (!stricmp(opname, "Estimate"))
+// opval= options->estimate;
+ opval= (int)table->s->max_rows;
+ else if (!stricmp(opname, "Avglen"))
+ opval= (int)table->s->avg_row_length;
+ else if (!stricmp(opname, "Multiple"))
+ opval= options->multiple;
+ else if (!stricmp(opname, "Header"))
+ opval= options->header;
+ else if (!stricmp(opname, "Quoted"))
+ opval= options->quoted;
+ else if (!stricmp(opname, "Ending"))
+ opval= options->ending;
+ else if (!stricmp(opname, "Compressed"))
+ opval= (options->compressed);
+
+ if (opval == (ulonglong)NO_IVAL && options && options->oplist)
+ if ((pv= GetListOption(xp->g, opname, options->oplist)))
+ opval= CharToNumber(pv, strlen(pv), ULONGLONG_MAX, true);
+
+ return (int)opval;
+} // end of GetIntegerOption
+
+/****************************************************************************/
+/* Set the value of the opname option (does not work for oplist options) */
+/* Currently used only to set the Lrecl value. */
+/****************************************************************************/
+bool ha_connect::SetIntegerOption(char *opname, int n)
+{
+ PTOS options= GetTableOptionStruct(table);
+
+ if (!options)
+ return true;
+
+ if (!stricmp(opname, "Lrecl"))
+ options->lrecl= n;
+ else if (!stricmp(opname, "Elements"))
+ options->elements= n;
+//else if (!stricmp(opname, "Estimate"))
+// options->estimate= n;
+ else if (!stricmp(opname, "Multiple"))
+ options->multiple= n;
+ else if (!stricmp(opname, "Header"))
+ options->header= n;
+ else if (!stricmp(opname, "Quoted"))
+ options->quoted= n;
+ else if (!stricmp(opname, "Ending"))
+ options->ending= n;
+ else if (!stricmp(opname, "Compressed"))
+ options->compressed= n;
+ else
+ return true;
+//else if (options->oplist)
+// SetListOption(opname, options->oplist, n);
+
+ return false;
+} // end of SetIntegerOption
+
+/****************************************************************************/
+/* Return a field option structure. */
+/****************************************************************************/
+PFOS ha_connect::GetFieldOptionStruct(Field *fdp)
+{
+ return fdp->option_struct;
+} // end of GetFildOptionStruct
+
+/****************************************************************************/
+/* Returns the column description structure used to make the column. */
+/****************************************************************************/
+void *ha_connect::GetColumnOption(PGLOBAL g, void *field, PCOLINFO pcf)
+{
+ const char *cp;
+ ha_field_option_struct *fop;
+ Field* fp;
+ Field* *fldp;
+
+ // Double test to be on the safe side
+ if (!table)
+ return NULL;
+
+ // Find the column to describe
+ if (field) {
+ fldp= (Field**)field;
+ fldp++;
+ } else
+ fldp= (tshp) ? tshp->field : table->field;
+
+ if (!fldp || !(fp= *fldp))
+ return NULL;
+
+ // Get the CONNECT field options structure
+ fop= GetFieldOptionStruct(fp);
+ pcf->Flags= 0;
+
+ // Now get column information
+ pcf->Name= (char*)fp->field_name;
+
+ if (fop && fop->special) {
+ pcf->Fieldfmt= (char*)fop->special;
+ pcf->Flags= U_SPECIAL;
+ return fldp;
+ } // endif special
+
+ pcf->Scale= 0;
+ pcf->Opt= (fop) ? (int)fop->opt : 0;
+
+ if ((pcf->Length= fp->field_length) < 0)
+ pcf->Length= 256; // BLOB?
+
+ pcf->Precision= pcf->Length;
+
+ if (fop) {
+ pcf->Offset= (int)fop->offset;
+ pcf->Freq= (int)fop->freq;
+ pcf->Datefmt= (char*)fop->dateformat;
+ pcf->Fieldfmt= (char*)fop->fieldformat;
+ } else {
+ pcf->Offset= -1;
+ pcf->Freq= 0;
+ pcf->Datefmt= NULL;
+ pcf->Fieldfmt= NULL;
+ } // endif fop
+
+ switch (fp->type()) {
+ case MYSQL_TYPE_BLOB:
+ case MYSQL_TYPE_VARCHAR:
+ case MYSQL_TYPE_VAR_STRING:
+ pcf->Flags |= U_VAR;
+ /* no break */
+ default:
+ pcf->Type= MYSQLtoPLG(fp->type());
+ break;
+ } // endswitch SQL type
+
+ switch (pcf->Type) {
+ case TYPE_STRING:
+ // Do something for case
+ cp= fp->charset()->name;
+
+ // Find if collation name ends by _ci
+ if (!strcmp(cp + strlen(cp) - 3, "_ci")) {
+ pcf->Scale= 1; // Case insensitive
+ pcf->Opt= 0; // Prevent index opt until it is safe
+ } // endif ci
+
+ break;
+ case TYPE_DOUBLE:
+ pcf->Scale= max(min(fp->decimals(), ((unsigned)pcf->Length - 2)), 0);
+ break;
+ case TYPE_DECIM:
+ pcf->Precision= ((Field_new_decimal*)fp)->precision;
+ pcf->Scale= fp->decimals();
+ break;
+ case TYPE_DATE:
+ // Field_length is only used for DATE columns
+ if (fop && fop->fldlen)
+ pcf->Length= (int)fop->fldlen;
+ else {
+ int len;
+
+ if (pcf->Datefmt) {
+ // Find the (max) length produced by the date format
+ char buf[256];
+ PGLOBAL g= GetPlug(table->in_use, xp);
+ PDTP pdtp= MakeDateFormat(g, pcf->Datefmt, false, true, 0);
+ struct tm datm;
+ bzero(&datm, sizeof(datm));
+ datm.tm_mday= 12;
+ datm.tm_mon= 11;
+ datm.tm_year= 112;
+ len= strftime(buf, 256, pdtp->OutFmt, &datm);
+ } else
+ len= 0;
+
+ // 11 is for signed numeric representation of the date
+ pcf->Length= (len) ? len : 11;
+ } // endelse
+
+ break;
+ default:
+ break;
+ } // endswitch type
+
+ if (fp->flags & UNSIGNED_FLAG)
+ pcf->Flags |= U_UNSIGNED;
+
+ if (fp->flags & ZEROFILL_FLAG)
+ pcf->Flags |= U_ZEROFILL;
+
+ // This is used to skip null bit
+ if (fp->real_maybe_null())
+ pcf->Flags |= U_NULLS;
+
+ // Mark virtual columns as such
+ if (fp->vcol_info && !fp->stored_in_db)
+ pcf->Flags |= U_VIRTUAL;
+
+ pcf->Key= 0; // Not used when called from MySQL
+
+ // Get the comment if any
+ if (fp->comment.str && fp->comment.length) {
+ pcf->Remark= (char*)PlugSubAlloc(g, NULL, fp->comment.length + 1);
+ memcpy(pcf->Remark, fp->comment.str, fp->comment.length);
+ pcf->Remark[fp->comment.length]= 0;
+ } else
+ pcf->Remark= NULL;
+
+ return fldp;
+} // end of GetColumnOption
+
+/****************************************************************************/
+/* Returns the index description structure used to make the index. */
+/****************************************************************************/
+PIXDEF ha_connect::GetIndexInfo(TABLE_SHARE *s)
+{
+ char *name, *pn;
+ bool unique;
+ PIXDEF xdp, pxd=NULL, toidx= NULL;
+ PKPDEF kpp, pkp;
+ KEY kp;
+ PGLOBAL& g= xp->g;
+
+ if (!s)
+ s= table->s;
+
+ for (int n= 0; (unsigned)n < s->keynames.count; n++) {
+ if (xtrace)
+ htrc("Getting created index %d info\n", n + 1);
+
+ // Find the index to describe
+ kp= s->key_info[n];
+
+ // Now get index information
+ pn= (char*)s->keynames.type_names[n];
+ name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
+ strcpy(name, pn); // This is probably unuseful
+ unique= (kp.flags & 1) != 0;
+ pkp= NULL;
+
+ // Allocate the index description block
+ xdp= new(g) INDEXDEF(name, unique, n);
+
+ // Get the the key parts info
+ for (int k= 0; (unsigned)k < kp.user_defined_key_parts; k++) {
+ pn= (char*)kp.key_part[k].field->field_name;
+ name= (char*)PlugSubAlloc(g, NULL, strlen(pn) + 1);
+ strcpy(name, pn); // This is probably unuseful
+
+ // Allocate the key part description block
+ kpp= new(g) KPARTDEF(name, k + 1);
+ kpp->SetKlen(kp.key_part[k].length);
+
+#if 0 // NIY
+ // Index on auto increment column can be an XXROW index
+ if (kp.key_part[k].field->flags & AUTO_INCREMENT_FLAG &&
+ kp.uder_defined_key_parts == 1) {
+ char *type= GetStringOption("Type", "DOS");
+ TABTYPE typ= GetTypeID(type);
+
+ xdp->SetAuto(IsTypeFixed(typ));
+ } // endif AUTO_INCREMENT
+#endif // 0
+
+ if (pkp)
+ pkp->SetNext(kpp);
+ else
+ xdp->SetToKeyParts(kpp);
+
+ pkp= kpp;
+ } // endfor k
+
+ xdp->SetNParts(kp.user_defined_key_parts);
+
+ if (pxd)
+ pxd->SetNext(xdp);
+ else
+ toidx= xdp;
+
+ pxd= xdp;
+ } // endfor n
+
+ return toidx;
+} // end of GetIndexInfo
+
+const char *ha_connect::GetDBName(const char* name)
+{
+ return (name) ? name : table->s->db.str;
+} // end of GetDBName
+
+const char *ha_connect::GetTableName(void)
+{
+ return (tshp) ? tshp->table_name.str : table->s->table_name.str;
+} // end of GetTableName
+
+#if 0
+/****************************************************************************/
+/* Returns the column real or special name length of a field. */
+/****************************************************************************/
+int ha_connect::GetColNameLen(Field *fp)
+{
+ int n;
+ PFOS fop= GetFieldOptionStruct(fp);
+
+ // Now get the column name length
+ if (fop && fop->special)
+ n= strlen(fop->special) + 1;
+ else
+ n= strlen(fp->field_name);
+
+ return n;
+} // end of GetColNameLen
+
+/****************************************************************************/
+/* Returns the column real or special name of a field. */
+/****************************************************************************/
+char *ha_connect::GetColName(Field *fp)
+{
+ PFOS fop= GetFieldOptionStruct(fp);
+
+ return (fop && fop->special) ? fop->special : (char*)fp->field_name;
+} // end of GetColName
+
+/****************************************************************************/
+/* Adds the column real or special name of a field to a string. */
+/****************************************************************************/
+void ha_connect::AddColName(char *cp, Field *fp)
+{
+ PFOS fop= GetFieldOptionStruct(fp);
+
+ // Now add the column name
+ if (fop && fop->special)
+ // The prefix * mark the column as "special"
+ strcat(strcpy(cp, "*"), strupr(fop->special));
+ else
+ strcpy(cp, (char*)fp->field_name);
+
+} // end of AddColName
+#endif // 0
+
+/****************************************************************************/
+/* Get the table description block of a CONNECT table. */
+/****************************************************************************/
+PTDB ha_connect::GetTDB(PGLOBAL g)
+{
+ const char *table_name;
+ PTDB tp;
+
+ // Double test to be on the safe side
+ if (!g || !table)
+ return NULL;
+
+ table_name= GetTableName();
+
+ if (!xp->CheckQuery(valid_query_id) && tdbp
+ && !stricmp(tdbp->GetName(), table_name)
+ && (tdbp->GetMode() == xmod
+ || tdbp->GetAmType() == TYPE_AM_XML)) {
+ tp= tdbp;
+// tp->SetMode(xmod);
+ } else if ((tp= CntGetTDB(g, table_name, xmod, this))) {
+ valid_query_id= xp->last_query_id;
+ tp->SetMode(xmod);
+ } else
+ htrc("GetTDB: %s\n", g->Message);
+
+ return tp;
+} // end of GetTDB
+
+/****************************************************************************/
+/* Open a CONNECT table, restricting column list if cols is true. */
+/****************************************************************************/
+int ha_connect::OpenTable(PGLOBAL g, bool del)
+{
+ bool rc= false;
+ char *c1= NULL, *c2=NULL;
+
+ // Double test to be on the safe side
+ if (!g || !table) {
+ htrc("OpenTable logical error; g=%p table=%p\n", g, table);
+ return HA_ERR_INITIALIZATION;
+ } // endif g
+
+ if (!(tdbp= GetTDB(g)))
+ return RC_FX;
+ else if (tdbp->IsReadOnly())
+ switch (xmod) {
+ case MODE_WRITE:
+ case MODE_INSERT:
+ case MODE_UPDATE:
+ case MODE_DELETE:
+ strcpy(g->Message, MSG(READ_ONLY));
+ return HA_ERR_TABLE_READONLY;
+ default:
+ break;
+ } // endswitch xmode
+
+ if (xmod != MODE_INSERT || tdbp->GetAmType() == TYPE_AM_ODBC
+ || tdbp->GetAmType() == TYPE_AM_MYSQL) {
+ // Get the list of used fields (columns)
+ char *p;
+ unsigned int k1, k2, n1, n2;
+ Field* *field;
+ Field* fp;
+ MY_BITMAP *map= (xmod == MODE_INSERT) ? table->write_set : table->read_set;
+ MY_BITMAP *ump= (xmod == MODE_UPDATE) ? table->write_set : NULL;
+
+ k1= k2= 0;
+ n1= n2= 1; // 1 is space for final null character
+
+ for (field= table->field; fp= *field; field++) {
+ if (bitmap_is_set(map, fp->field_index)) {
+ n1+= (strlen(fp->field_name) + 1);
+ k1++;
+ } // endif
+
+ if (ump && bitmap_is_set(ump, fp->field_index)) {
+ n2+= (strlen(fp->field_name) + 1);
+ k2++;
+ } // endif
+
+ } // endfor field
+
+ if (k1) {
+ p= c1= (char*)PlugSubAlloc(g, NULL, n1);
+
+ for (field= table->field; fp= *field; field++)
+ if (bitmap_is_set(map, fp->field_index)) {
+ strcpy(p, (char*)fp->field_name);
+ p+= (strlen(p) + 1);
+ } // endif used field
+
+ *p= '\0'; // mark end of list
+ } // endif k1
+
+ if (k2) {
+ p= c2= (char*)PlugSubAlloc(g, NULL, n2);
+
+ for (field= table->field; fp= *field; field++)
+ if (bitmap_is_set(ump, fp->field_index)) {
+ strcpy(p, (char*)fp->field_name);
+ p+= (strlen(p) + 1);
+ } // endif used field
+
+ *p= '\0'; // mark end of list
+ } // endif k2
+
+ } // endif xmod
+
+ // Open the table
+ if (!(rc= CntOpenTable(g, tdbp, xmod, c1, c2, del, this))) {
+ istable= true;
+// strmake(tname, table_name, sizeof(tname)-1);
+
+ // We may be in a create index query
+ if (xmod == MODE_ANY && *tdbp->GetName() != '#') {
+ // The current indexes
+ PIXDEF oldpix= GetIndexInfo();
+ } // endif xmod
+
+ } else
+ htrc("OpenTable: %s\n", g->Message);
+
+ if (rc) {
+ tdbp= NULL;
+ valid_info= false;
+ } // endif rc
+
+ return (rc) ? HA_ERR_INITIALIZATION : 0;
+} // end of OpenTable
+
+
+/****************************************************************************/
+/* IsOpened: returns true if the table is already opened. */
+/****************************************************************************/
+bool ha_connect::IsOpened(void)
+{
+ return (!xp->CheckQuery(valid_query_id) && tdbp
+ && tdbp->GetUse() == USE_OPEN);
+} // end of IsOpened
+
+
+/****************************************************************************/
+/* Close a CONNECT table. */
+/****************************************************************************/
+int ha_connect::CloseTable(PGLOBAL g)
+{
+ int rc= CntCloseTable(g, tdbp);
+ tdbp= NULL;
+ sdvalin=NULL;
+ sdvalout=NULL;
+ valid_info= false;
+ indexing= -1;
+ return rc;
+} // end of CloseTable
+
+
+/***********************************************************************/
+/* Make a pseudo record from current row values. Specific to MySQL. */
+/***********************************************************************/
+int ha_connect::MakeRecord(char *buf)
+{
+ char *p, *fmt, val[32];
+ int rc= 0;
+ Field* *field;
+ Field *fp;
+ my_bitmap_map *org_bitmap;
+ CHARSET_INFO *charset= tdbp->data_charset();
+//MY_BITMAP readmap;
+ MY_BITMAP *map;
+ PVAL value;
+ PCOL colp= NULL;
+ DBUG_ENTER("ha_connect::MakeRecord");
+
+ if (xtrace > 1)
+ htrc("Maps: read=%08X write=%08X vcol=%08X defr=%08X defw=%08X\n",
+ *table->read_set->bitmap, *table->write_set->bitmap,
+ *table->vcol_set->bitmap,
+ *table->def_read_set.bitmap, *table->def_write_set.bitmap);
+
+ // Avoid asserts in field::store() for columns that are not updated
+ org_bitmap= dbug_tmp_use_all_columns(table, table->write_set);
+
+ // This is for variable_length rows
+ memset(buf, 0, table->s->null_bytes);
+
+ // When sorting read_set selects all columns, so we use def_read_set
+ map= (MY_BITMAP *)&table->def_read_set;
+
+ // Make the pseudo record from field values
+ for (field= table->field; *field && !rc; field++) {
+ fp= *field;
+
+ if (fp->vcol_info && !fp->stored_in_db)
+ continue; // This is a virtual column
+
+ if (bitmap_is_set(map, fp->field_index) || alter) {
+ // This is a used field, fill the buffer with value
+ for (colp= tdbp->GetColumns(); colp; colp= colp->GetNext())
+ if ((!mrr || colp->GetKcol()) &&
+ !stricmp(colp->GetName(), (char*)fp->field_name))
+ break;
+
+ if (!colp) {
+ if (mrr)
+ continue;
+
+ htrc("Column %s not found\n", fp->field_name);
+ dbug_tmp_restore_column_map(table->write_set, org_bitmap);
+ DBUG_RETURN(HA_ERR_WRONG_IN_RECORD);
+ } // endif colp
+
+ value= colp->GetValue();
+
+ // All this could be better optimized
+ if (!value->IsNull()) {
+ switch (value->GetType()) {
+ case TYPE_DATE:
+ if (!sdvalout)
+ sdvalout= AllocateValue(xp->g, TYPE_STRING, 20);
+
+ switch (fp->type()) {
+ case MYSQL_TYPE_DATE:
+ fmt= "%Y-%m-%d";
+ break;
+ case MYSQL_TYPE_TIME:
+ fmt= "%H:%M:%S";
+ break;
+ case MYSQL_TYPE_YEAR:
+ fmt= "%Y";
+ break;
+ default:
+ fmt= "%Y-%m-%d %H:%M:%S";
+ break;
+ } // endswitch type
+
+ // Get date in the format required by MySQL fields
+ value->FormatValue(sdvalout, fmt);
+ p= sdvalout->GetCharValue();
+ break;
+ case TYPE_DOUBLE:
+ p= NULL;
+ break;
+ case TYPE_STRING:
+ // Passthru
+ default:
+ p= value->GetCharString(val);
+ break;
+ } // endswitch Type
+
+ if (p) {
+ if (fp->store(p, strlen(p), charset, CHECK_FIELD_WARN)) {
+ // Avoid "error" on null fields
+ if (value->GetIntValue())
+ rc= HA_ERR_WRONG_IN_RECORD;
+
+ DBUG_PRINT("MakeRecord", ("%s", p));
+ } // endif store
+
+ } else
+ if (fp->store(value->GetFloatValue())) {
+// rc= HA_ERR_WRONG_IN_RECORD; a Warning was ignored
+ char buf[128];
+ THD *thd= ha_thd();
+
+ sprintf(buf, "Out of range value for column '%s' at row %ld",
+ fp->field_name,
+ thd->get_stmt_da()->current_row_for_warning());
+
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, buf);
+ DBUG_PRINT("MakeRecord", ("%s", value->GetCharString(val)));
+ } // endif store
+
+ fp->set_notnull();
+ } else
+ fp->set_null();
+
+ } // endif bitmap
+
+ } // endfor field
+
+ // This is copied from ha_tina and is necessary to avoid asserts
+ dbug_tmp_restore_column_map(table->write_set, org_bitmap);
+ DBUG_RETURN(rc);
+} // end of MakeRecord
+
+
+/***********************************************************************/
+/* Set row values from a MySQL pseudo record. Specific to MySQL. */
+/***********************************************************************/
+int ha_connect::ScanRecord(PGLOBAL g, uchar *buf)
+{
+ char attr_buffer[1024];
+ char data_buffer[1024];
+ char *fmt;
+ int rc= 0;
+ PCOL colp;
+ PVAL value;
+ Field *fp;
+ PTDBASE tp= (PTDBASE)tdbp;
+ String attribute(attr_buffer, sizeof(attr_buffer),
+ table->s->table_charset);
+ my_bitmap_map *bmap= dbug_tmp_use_all_columns(table, table->read_set);
+ const CHARSET_INFO *charset= tdbp->data_charset();
+ String data_charset_value(data_buffer, sizeof(data_buffer), charset);
+
+ // Scan the pseudo record for field values and set column values
+ for (Field **field=table->field ; *field ; field++) {
+ fp= *field;
+
+ if ((fp->vcol_info && !fp->stored_in_db) ||
+ fp->option_struct->special)
+ continue; // Is a virtual column possible here ???
+
+ if ((xmod == MODE_INSERT && tdbp->GetAmType() != TYPE_AM_MYSQL
+ && tdbp->GetAmType() != TYPE_AM_ODBC) ||
+ bitmap_is_set(table->write_set, fp->field_index)) {
+ for (colp= tp->GetSetCols(); colp; colp= colp->GetNext())
+ if (!stricmp(colp->GetName(), fp->field_name))
+ break;
+
+ if (!colp) {
+ htrc("Column %s not found\n", fp->field_name);
+ rc= HA_ERR_WRONG_IN_RECORD;
+ goto err;
+ } else
+ value= colp->GetValue();
+
+ // This is a used field, fill the value from the row buffer
+ // All this could be better optimized
+ if (fp->is_null()) {
+ if (colp->IsNullable())
+ value->SetNull(true);
+
+ value->Reset();
+ } else switch (value->GetType()) {
+ case TYPE_DOUBLE:
+ value->SetValue(fp->val_real());
+ break;
+ case TYPE_DATE:
+ if (!sdvalin)
+ sdvalin= (DTVAL*)AllocateValue(xp->g, TYPE_DATE, 19);
+
+ // Get date in the format produced by MySQL fields
+ switch (fp->type()) {
+ case MYSQL_TYPE_DATE:
+ fmt= "YYYY-MM-DD";
+ break;
+ case MYSQL_TYPE_TIME:
+ fmt= "hh:mm:ss";
+ break;
+ case MYSQL_TYPE_YEAR:
+ fmt= "YYYY";
+ break;
+ default:
+ fmt= "YYYY-MM-DD hh:mm:ss";
+ } // endswitch type
+
+ ((DTVAL*)sdvalin)->SetFormat(g, fmt, strlen(fmt));
+ fp->val_str(&attribute);
+ sdvalin->SetValue_psz(attribute.c_ptr_safe());
+ value->SetValue_pval(sdvalin);
+ break;
+ default:
+ fp->val_str(&attribute);
+
+ if (charset != &my_charset_bin) {
+ // Convert from SQL field charset to DATA_CHARSET
+ uint cnv_errors;
+
+ data_charset_value.copy(attribute.ptr(), attribute.length(),
+ attribute.charset(), charset, &cnv_errors);
+ value->SetValue_psz(data_charset_value.c_ptr_safe());
+ } else
+ value->SetValue_psz(attribute.c_ptr_safe());
+
+ break;
+ } // endswitch Type
+
+#ifdef NEWCHANGE
+ } else if (xmod == MODE_UPDATE) {
+ PCOL cp;
+
+ for (cp= tp->GetColumns(); cp; cp= cp->GetNext())
+ if (!stricmp(colp->GetName(), cp->GetName()))
+ break;
+
+ if (!cp) {
+ rc= HA_ERR_WRONG_IN_RECORD;
+ goto err;
+ } // endif cp
+
+ value->SetValue_pval(cp->GetValue());
+ } else // mode Insert
+ value->Reset();
+#else
+ } // endif bitmap_is_set
+#endif
+
+ } // endfor field
+
+ err:
+ dbug_tmp_restore_column_map(table->read_set, bmap);
+ return rc;
+} // end of ScanRecord
+
+
+/***********************************************************************/
+/* Check change in index column. Specific to MySQL. */
+/* Should be elaborated to check for real changes. */
+/***********************************************************************/
+int ha_connect::CheckRecord(PGLOBAL g, const uchar *oldbuf, uchar *newbuf)
+{
+ return ScanRecord(g, newbuf);
+} // end of dummy CheckRecord
+
+
+/***********************************************************************/
+/* Return the string representing an operator. */
+/***********************************************************************/
+const char *ha_connect::GetValStr(OPVAL vop, bool neg)
+{
+ const char *val;
+
+ switch (vop) {
+ case OP_EQ:
+ val= " = ";
+ break;
+ case OP_NE:
+ val= " <> ";
+ break;
+ case OP_GT:
+ val= " > ";
+ break;
+ case OP_GE:
+ val= " >= ";
+ break;
+ case OP_LT:
+ val= " < ";
+ break;
+ case OP_LE:
+ val= " <= ";
+ break;
+ case OP_IN:
+ val= (neg) ? " NOT IN (" : " IN (";
+ break;
+ case OP_NULL:
+ val= (neg) ? " IS NOT NULL" : " IS NULL";
+ break;
+ case OP_LIKE:
+ val= " LIKE ";
+ break;
+ case OP_XX:
+ val= (neg) ? " NOT BETWEEN " : " BETWEEN ";
+ break;
+ case OP_EXIST:
+ val= (neg) ? " NOT EXISTS " : " EXISTS ";
+ break;
+ case OP_AND:
+ val= " AND ";
+ break;
+ case OP_OR:
+ val= " OR ";
+ break;
+ case OP_NOT:
+ val= " NOT ";
+ break;
+ case OP_CNC:
+ val= " || ";
+ break;
+ case OP_ADD:
+ val= " + ";
+ break;
+ case OP_SUB:
+ val= " - ";
+ break;
+ case OP_MULT:
+ val= " * ";
+ break;
+ case OP_DIV:
+ val= " / ";
+ break;
+ default:
+ val= " ? ";
+ break;
+ } /* endswitch */
+
+ return val;
+} // end of GetValStr
+
+
+/***********************************************************************/
+/* Check the WHERE condition and return a CONNECT filter. */
+/***********************************************************************/
+PFIL ha_connect::CondFilter(PGLOBAL g, Item *cond)
+{
+ unsigned int i;
+ bool ismul= false;
+ OPVAL vop= OP_XX;
+ PFIL filp= NULL;
+
+ if (!cond)
+ return NULL;
+
+ if (xtrace)
+ htrc("Cond type=%d\n", cond->type());
+
+ if (cond->type() == COND::COND_ITEM) {
+ PFIL fp;
+ Item_cond *cond_item= (Item_cond *)cond;
+
+ if (xtrace)
+ htrc("Cond: Ftype=%d name=%s\n", cond_item->functype(),
+ cond_item->func_name());
+
+ switch (cond_item->functype()) {
+ case Item_func::COND_AND_FUNC: vop= OP_AND; break;
+ case Item_func::COND_OR_FUNC: vop= OP_OR; break;
+ default: return NULL;
+ } // endswitch functype
+
+ List<Item>* arglist= cond_item->argument_list();
+ List_iterator<Item> li(*arglist);
+ Item *subitem;
+
+ for (i= 0; i < arglist->elements; i++)
+ if ((subitem= li++)) {
+ if (!(fp= CondFilter(g, subitem))) {
+ if (vop == OP_OR)
+ return NULL;
+ } else
+ filp= (filp) ? MakeFilter(g, filp, vop, fp) : fp;
+
+ } else
+ return NULL;
+
+ } else if (cond->type() == COND::FUNC_ITEM) {
+ unsigned int i;
+ bool iscol, neg= FALSE;
+ PCOL colp[2]= {NULL,NULL};
+ PPARM pfirst= NULL, pprec= NULL;
+ POPER pop;
+ Item_func *condf= (Item_func *)cond;
+ Item* *args= condf->arguments();
+
+ if (xtrace)
+ htrc("Func type=%d argnum=%d\n", condf->functype(),
+ condf->argument_count());
+
+ switch (condf->functype()) {
+ case Item_func::EQUAL_FUNC:
+ case Item_func::EQ_FUNC: vop= OP_EQ; break;
+ case Item_func::NE_FUNC: vop= OP_NE; break;
+ case Item_func::LT_FUNC: vop= OP_LT; break;
+ case Item_func::LE_FUNC: vop= OP_LE; break;
+ case Item_func::GE_FUNC: vop= OP_GE; break;
+ case Item_func::GT_FUNC: vop= OP_GT; break;
+ case Item_func::IN_FUNC: vop= OP_IN;
+ case Item_func::BETWEEN:
+ ismul= true;
+ neg= ((Item_func_opt_neg *)condf)->negated;
+ break;
+ default: return NULL;
+ } // endswitch functype
+
+ pop= (POPER)PlugSubAlloc(g, NULL, sizeof(OPER));
+ pop->Name= NULL;
+ pop->Val=vop;
+ pop->Mod= 0;
+
+ if (condf->argument_count() < 2)
+ return NULL;
+
+ for (i= 0; i < condf->argument_count(); i++) {
+ if (xtrace)
+ htrc("Argtype(%d)=%d\n", i, args[i]->type());
+
+ if (i >= 2 && !ismul) {
+ if (xtrace)
+ htrc("Unexpected arg for vop=%d\n", vop);
+
+ continue;
+ } // endif i
+
+ if ((iscol= args[i]->type() == COND::FIELD_ITEM)) {
+ Item_field *pField= (Item_field *)args[i];
+
+ // IN and BETWEEN clauses should be col VOP list
+ if (i && ismul)
+ return NULL;
+
+ if (pField->field->table != table ||
+ !(colp[i]= tdbp->ColDB(g, (PSZ)pField->field->field_name, 0)))
+ return NULL; // Column does not belong to this table
+
+ if (xtrace) {
+ htrc("Field index=%d\n", pField->field->field_index);
+ htrc("Field name=%s\n", pField->field->field_name);
+ } // endif xtrace
+
+ } else {
+ char buff[256];
+ String *res, tmp(buff, sizeof(buff), &my_charset_bin);
+ Item_basic_constant *pval= (Item_basic_constant *)args[i];
+ PPARM pp= (PPARM)PlugSubAlloc(g, NULL, sizeof(PARM));
+
+ // IN and BETWEEN clauses should be col VOP list
+ if (!i && (ismul))
+ return NULL;
+
+ if ((res= pval->val_str(&tmp)) == NULL)
+ return NULL; // To be clarified
+
+ switch (args[i]->real_type()) {
+ case COND::STRING_ITEM:
+ pp->Type= TYPE_STRING;
+ pp->Value= PlugSubAlloc(g, NULL, res->length() + 1);
+ strncpy((char*)pp->Value, res->ptr(), res->length() + 1);
+ break;
+ case COND::INT_ITEM:
+ pp->Type= TYPE_INT;
+ pp->Value= PlugSubAlloc(g, NULL, sizeof(int));
+ *((int*)pp->Value)= (int)pval->val_int();
+ break;
+ case COND::DATE_ITEM:
+ pp->Type= TYPE_DATE;
+ pp->Value= PlugSubAlloc(g, NULL, sizeof(int));
+ *((int*)pp->Value)= (int)pval->val_int_from_date();
+ break;
+ case COND::REAL_ITEM:
+ pp->Type= TYPE_DOUBLE;
+ pp->Value= PlugSubAlloc(g, NULL, sizeof(double));
+ *((double*)pp->Value)= pval->val_real();
+ break;
+ case COND::DECIMAL_ITEM:
+ pp->Type= TYPE_DOUBLE;
+ pp->Value= PlugSubAlloc(g, NULL, sizeof(double));
+ *((double*)pp->Value)= pval->val_real_from_decimal();
+ break;
+ case COND::CACHE_ITEM: // Possible ???
+ case COND::NULL_ITEM: // TODO: handle this
+ default:
+ return NULL;
+ } // endswitch type
+
+ if (xtrace)
+ htrc("Value=%.*s\n", res->length(), res->ptr());
+
+ // Append the value to the argument list
+ if (pprec)
+ pprec->Next= pp;
+ else
+ pfirst= pp;
+
+ pp->Domain= i;
+ pp->Next= NULL;
+ pprec= pp;
+ } // endif type
+
+ } // endfor i
+
+ filp= MakeFilter(g, colp, pop, pfirst, neg);
+ } else {
+ if (xtrace)
+ htrc("Unsupported condition\n");
+
+ return NULL;
+ } // endif's type
+
+ return filp;
+} // end of CondFilter
+
+/***********************************************************************/
+/* Check the WHERE condition and return a MYSQL/ODBC/WQL filter. */
+/***********************************************************************/
+PCFIL ha_connect::CheckCond(PGLOBAL g, PCFIL filp, AMT tty, Item *cond)
+{
+ char *body= filp->Body;
+ unsigned int i;
+ bool ismul= false, x= (tty == TYPE_AM_MYX || tty == TYPE_AM_XDBC);
+ OPVAL vop= OP_XX;
+
+ if (!cond)
+ return NULL;
+
+ if (xtrace)
+ htrc("Cond type=%d\n", cond->type());
+
+ if (cond->type() == COND::COND_ITEM) {
+ char *p1, *p2;
+ Item_cond *cond_item= (Item_cond *)cond;
+
+ if (x)
+ return NULL;
+
+ if (xtrace)
+ htrc("Cond: Ftype=%d name=%s\n", cond_item->functype(),
+ cond_item->func_name());
+
+ switch (cond_item->functype()) {
+ case Item_func::COND_AND_FUNC: vop= OP_AND; break;
+ case Item_func::COND_OR_FUNC: vop= OP_OR; break;
+ default: return NULL;
+ } // endswitch functype
+
+ List<Item>* arglist= cond_item->argument_list();
+ List_iterator<Item> li(*arglist);
+ Item *subitem;
+
+ p1= body + strlen(body);
+ strcpy(p1, "(");
+ p2= p1 + 1;
+
+ for (i= 0; i < arglist->elements; i++)
+ if ((subitem= li++)) {
+ if (!CheckCond(g, filp, tty, subitem)) {
+ if (vop == OP_OR)
+ return NULL;
+ else
+ *p2= 0;
+
+ } else {
+ p1= p2 + strlen(p2);
+ strcpy(p1, GetValStr(vop, FALSE));
+ p2= p1 + strlen(p1);
+ } // endif CheckCond
+
+ } else
+ return NULL;
+
+ if (*p1 != '(')
+ strcpy(p1, ")");
+ else
+ return NULL;
+
+ } else if (cond->type() == COND::FUNC_ITEM) {
+ unsigned int i;
+// int n;
+ bool iscol, neg= FALSE;
+ Item_func *condf= (Item_func *)cond;
+ Item* *args= condf->arguments();
+
+ if (xtrace)
+ htrc("Func type=%d argnum=%d\n", condf->functype(),
+ condf->argument_count());
+
+// neg= condf->
+
+ switch (condf->functype()) {
+ case Item_func::EQUAL_FUNC:
+ case Item_func::EQ_FUNC: vop= OP_EQ; break;
+ case Item_func::NE_FUNC: vop= OP_NE; break;
+ case Item_func::LT_FUNC: vop= OP_LT; break;
+ case Item_func::LE_FUNC: vop= OP_LE; break;
+ case Item_func::GE_FUNC: vop= OP_GE; break;
+ case Item_func::GT_FUNC: vop= OP_GT; break;
+ case Item_func::IN_FUNC: vop= OP_IN;
+ case Item_func::BETWEEN:
+ ismul= true;
+ neg= ((Item_func_opt_neg *)condf)->negated;
+ break;
+ default: return NULL;
+ } // endswitch functype
+
+ if (condf->argument_count() < 2)
+ return NULL;
+ else if (ismul && tty == TYPE_AM_WMI)
+ return NULL; // Not supported by WQL
+
+ if (x && (neg || !(vop == OP_EQ || vop == OP_IN)))
+ return NULL;
+
+ for (i= 0; i < condf->argument_count(); i++) {
+ if (xtrace)
+ htrc("Argtype(%d)=%d\n", i, args[i]->type());
+
+ if (i >= 2 && !ismul) {
+ if (xtrace)
+ htrc("Unexpected arg for vop=%d\n", vop);
+
+ continue;
+ } // endif i
+
+ if ((iscol= args[i]->type() == COND::FIELD_ITEM)) {
+ const char *fnm;
+ ha_field_option_struct *fop;
+ Item_field *pField= (Item_field *)args[i];
+
+ if (x && i)
+ return NULL;
+
+ if (pField->field->table != table)
+ return NULL; // Field does not belong to this table
+ else
+ fop= GetFieldOptionStruct(pField->field);
+
+ if (fop && fop->special) {
+ if (tty == TYPE_AM_TBL && !stricmp(fop->special, "TABID"))
+ fnm= "TABID";
+ else if (tty == TYPE_AM_PLG)
+ fnm= fop->special;
+ else
+ return NULL;
+
+ } else if (tty == TYPE_AM_TBL)
+ return NULL;
+ else
+ fnm= pField->field->field_name;
+
+ if (xtrace) {
+ htrc("Field index=%d\n", pField->field->field_index);
+ htrc("Field name=%s\n", pField->field->field_name);
+ } // endif xtrace
+
+ // IN and BETWEEN clauses should be col VOP list
+ if (i && ismul)
+ return NULL;
+
+ strcat(body, fnm);
+ } else if (args[i]->type() == COND::FUNC_ITEM) {
+ if (tty == TYPE_AM_MYSQL) {
+ if (!CheckCond(g, filp, tty, args[i]))
+ return NULL;
+
+ } else
+ return NULL;
+
+ } else {
+ char buff[256];
+ String *res, tmp(buff, sizeof(buff), &my_charset_bin);
+ Item_basic_constant *pval= (Item_basic_constant *)args[i];
+
+ switch (args[i]->real_type()) {
+ case COND::STRING_ITEM:
+ case COND::INT_ITEM:
+ case COND::REAL_ITEM:
+ case COND::NULL_ITEM:
+ case COND::DECIMAL_ITEM:
+ case COND::DATE_ITEM:
+ case COND::CACHE_ITEM:
+ break;
+ default:
+ return NULL;
+ } // endswitch type
+
+ if ((res= pval->val_str(&tmp)) == NULL)
+ return NULL; // To be clarified
+
+ if (xtrace)
+ htrc("Value=%.*s\n", res->length(), res->ptr());
+
+ // IN and BETWEEN clauses should be col VOP list
+ if (!i && (x || ismul))
+ return NULL;
+
+ if (!x) {
+ // Append the value to the filter
+ if (args[i]->field_type() == MYSQL_TYPE_VARCHAR)
+ strcat(strcat(strcat(body, "'"), res->ptr()), "'");
+ else
+ strncat(body, res->ptr(), res->length());
+
+ } else {
+ if (args[i]->field_type() == MYSQL_TYPE_VARCHAR) {
+ // Add the command to the list
+ PCMD *ncp, cmdp= new(g) CMD(g, (char*)res->ptr());
+
+ for (ncp= &filp->Cmds; *ncp; ncp= &(*ncp)->Next) ;
+
+ *ncp= cmdp;
+ } else
+ return NULL;
+
+ } // endif x
+
+ } // endif
+
+ if (!x) {
+ if (!i)
+ strcat(body, GetValStr(vop, neg));
+ else if (vop == OP_XX && i == 1)
+ strcat(body, " AND ");
+ else if (vop == OP_IN)
+ strcat(body, (i == condf->argument_count() - 1) ? ")" : ",");
+
+ } // endif x
+
+ } // endfor i
+
+ if (x)
+ filp->Op= vop;
+
+ } else {
+ if (xtrace)
+ htrc("Unsupported condition\n");
+
+ return NULL;
+ } // endif's type
+
+ return filp;
+} // end of CheckCond
+
+
+ /**
+ Push condition down to the table handler.
+
+ @param cond Condition to be pushed. The condition tree must not be
+ modified by the caller.
+
+ @return
+ The 'remainder' condition that caller must use to filter out records.
+ NULL means the handler will not return rows that do not match the
+ passed condition.
+
+ @note
+ CONNECT handles the filtering only for table types that construct
+ an SQL or WQL query, but still leaves it to MySQL because only some
+ parts of the filter may be relevant.
+ The first suballocate finds the position where the string will be
+ constructed in the sarea. The second one does make the suballocation
+ with the proper length.
+ */
+const COND *ha_connect::cond_push(const COND *cond)
+{
+ DBUG_ENTER("ha_connect::cond_push");
+
+ if (tdbp) {
+ PGLOBAL& g= xp->g;
+ AMT tty= tdbp->GetAmType();
+ bool x= (tty == TYPE_AM_MYX || tty == TYPE_AM_XDBC);
+ bool b= (tty == TYPE_AM_WMI || tty == TYPE_AM_ODBC ||
+ tty == TYPE_AM_TBL || tty == TYPE_AM_MYSQL ||
+ tty == TYPE_AM_PLG || x);
+
+ if (b) {
+ PCFIL filp= (PCFIL)PlugSubAlloc(g, NULL, sizeof(CONDFIL));
+
+ filp->Body= (char*)PlugSubAlloc(g, NULL, (x) ? 128 : 0);
+ *filp->Body= 0;
+ filp->Op= OP_XX;
+ filp->Cmds= NULL;
+
+ if (CheckCond(g, filp, tty, (Item *)cond)) {
+ if (xtrace)
+ htrc("cond_push: %s\n", filp->Body);
+
+ if (!x)
+ PlugSubAlloc(g, NULL, strlen(filp->Body) + 1);
+ else
+ cond= NULL; // Does this work?
+
+ tdbp->SetCondFil(filp);
+ } else if (x && cond)
+ tdbp->SetCondFil(filp); // Wrong filter
+
+ } else
+ tdbp->SetFilter(CondFilter(g, (Item *)cond));
+
+ } // endif tdbp
+
+ // Let MySQL do the filtering
+ DBUG_RETURN(cond);
+} // end of cond_push
+
+/**
+ Number of rows in table. It will only be called if
+ (table_flags() & (HA_HAS_RECORDS | HA_STATS_RECORDS_IS_EXACT)) != 0
+*/
+ha_rows ha_connect::records()
+{
+ if (!valid_info)
+ info(HA_STATUS_VARIABLE);
+
+ if (tdbp && tdbp->Cardinality(NULL))
+ return stats.records;
+ else
+ return HA_POS_ERROR;
+
+} // end of records
+
+
+/**
+ Return an error message specific to this handler.
+
+ @param error error code previously returned by handler
+ @param buf pointer to String where to add error message
+
+ @return
+ Returns true if this is a temporary error
+*/
+bool ha_connect::get_error_message(int error, String* buf)
+{
+ DBUG_ENTER("ha_connect::get_error_message");
+
+ if (xp && xp->g) {
+ PGLOBAL g= xp->g;
+ char *msg= (char*)PlugSubAlloc(g, NULL, strlen(g->Message) * 3);
+ uint dummy_errors;
+ uint32 len= copy_and_convert(msg, strlen(g->Message) * 3,
+ system_charset_info,
+ g->Message, strlen(g->Message),
+ &my_charset_latin1,
+ &dummy_errors);
+ msg[len]= '\0';
+ buf->copy(msg, (uint)strlen(msg), system_charset_info);
+ } else
+ buf->copy("Cannot retrieve msg", 19, system_charset_info);
+
+ DBUG_RETURN(false);
+} // end of get_error_message
+
+
+/**
+ @brief
+ Used for opening tables. The name will be the name of the file.
+
+ @details
+ A table is opened when it needs to be opened; e.g. when a request comes in
+ for a SELECT on the table (tables are not open and closed for each request,
+ they are cached).
+
+ Called from handler.cc by handler::ha_open(). The server opens all tables by
+ calling ha_open() which then calls the handler specific open().
+
+ @note
+ For CONNECT no open can be done here because field information is not yet
+ updated. >>>>> TO BE CHECKED <<<<<
+ (Thread information could be get by using 'ha_thd')
+
+ @see
+ handler::ha_open() in handler.cc
+*/
+int ha_connect::open(const char *name, int mode, uint test_if_locked)
+{
+ int rc= 0;
+ DBUG_ENTER("ha_connect::open");
+
+ if (xtrace)
+ htrc("open: name=%s mode=%d test=%u\n", name, mode, test_if_locked);
+
+ if (!(share= get_share()))
+ DBUG_RETURN(1);
+
+ thr_lock_data_init(&share->lock,&lock,NULL);
+
+ // Try to get the user if possible
+ xp= GetUser(ha_thd(), xp);
+ PGLOBAL g= (xp) ? xp->g : NULL;
+
+ // Try to set the database environment
+ if (g) {
+ rc= (CntCheckDB(g, this, name)) ? (-2) : 0;
+
+ if (g->Mrr) {
+ // This should only happen for the mrr secondary handler
+ mrr= true;
+ g->Mrr= false;
+ } else
+ mrr= false;
+
+ } else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ DBUG_RETURN(rc);
+} // end of open
+
+/**
+ @brief
+ Make the indexes for this table
+*/
+int ha_connect::optimize(THD* thd, HA_CHECK_OPT* check_opt)
+{
+ int rc= 0;
+ PGLOBAL& g= xp->g;
+ PDBUSER dup= PlgGetUser(g);
+
+ // Ignore error on the opt file
+ dup->Check &= ~CHK_OPT;
+ tdbp= GetTDB(g);
+ dup->Check |= CHK_OPT;
+
+ if (tdbp) {
+ bool b= ((PTDBASE)tdbp)->GetDef()->Indexable();
+
+ if ((rc= ((PTDBASE)tdbp)->ResetTableOpt(g, true, b))) {
+ if (rc == RC_INFO) {
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ rc= 0;
+ } else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ } // endif rc
+
+ } else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ return rc;
+} // end of optimize
+
+/**
+ @brief
+ Closes a table.
+
+ @details
+ Called from sql_base.cc, sql_select.cc, and table.cc. In sql_select.cc it is
+ only used to close up temporary tables or during the process where a
+ temporary table is converted over to being a myisam table.
+
+ For sql_base.cc look at close_data_tables().
+
+ @see
+ sql_base.cc, sql_select.cc and table.cc
+*/
+int ha_connect::close(void)
+{
+ int rc= 0;
+ DBUG_ENTER("ha_connect::close");
+
+ // If this is called by a later query, the table may have
+ // been already closed and the tdbp is not valid anymore.
+ if (tdbp && xp->last_query_id == valid_query_id)
+ rc= CloseTable(xp->g);
+
+ DBUG_RETURN(rc);
+} // end of close
+
+
+/**
+ @brief
+ write_row() inserts a row. No extra() hint is given currently if a bulk load
+ is happening. buf() is a byte array of data. You can use the field
+ information to extract the data from the native byte array type.
+
+ @details
+ Example of this would be:
+ @code
+ for (Field **field=table->field ; *field ; field++)
+ {
+ ...
+ }
+ @endcode
+
+ See ha_tina.cc for an example of extracting all of the data as strings.
+ ha_berekly.cc has an example of how to store it intact by "packing" it
+ for ha_berkeley's own native storage type.
+
+ See the note for update_row() on auto_increments and timestamps. This
+ case also applies to write_row().
+
+ Called from item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
+ sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc, and sql_update.cc.
+
+ @see
+ item_sum.cc, item_sum.cc, sql_acl.cc, sql_insert.cc,
+ sql_insert.cc, sql_select.cc, sql_table.cc, sql_udf.cc and sql_update.cc
+*/
+int ha_connect::write_row(uchar *buf)
+{
+ int rc= 0;
+ PGLOBAL& g= xp->g;
+ DBUG_ENTER("ha_connect::write_row");
+
+ // This is not tested yet
+ if (xmod == MODE_ALTER)
+ xmod= MODE_INSERT;
+
+ // Open the table if it was not opened yet (locked)
+ if (!IsOpened() || xmod != tdbp->GetMode()) {
+ if (IsOpened())
+ CloseTable(g);
+
+ if ((rc= OpenTable(g)))
+ DBUG_RETURN(rc);
+
+ } // endif isopened
+
+ if (tdbp->GetMode() == MODE_ANY)
+ DBUG_RETURN(0);
+
+#if 0 // AUTO_INCREMENT NIY
+ if (table->next_number_field && buf == table->record[0]) {
+ int error;
+
+ if ((error= update_auto_increment()))
+ return error;
+
+ } // endif nex_number_field
+#endif // 0
+
+ // Set column values from the passed pseudo record
+ if ((rc= ScanRecord(g, buf)))
+ DBUG_RETURN(rc);
+
+ // Return result code from write operation
+ if (CntWriteRow(g, tdbp)) {
+ DBUG_PRINT("write_row", ("%s", g->Message));
+ htrc("write_row: %s\n", g->Message);
+ rc= HA_ERR_INTERNAL_ERROR;
+ } // endif RC
+
+ DBUG_RETURN(rc);
+} // end of write_row
+
+
+/**
+ @brief
+ Yes, update_row() does what you expect, it updates a row. old_data will have
+ the previous row record in it, while new_data will have the newest data in it.
+ Keep in mind that the server can do updates based on ordering if an ORDER BY
+ clause was used. Consecutive ordering is not guaranteed.
+
+ @details
+ Currently new_data will not have an updated auto_increament record, or
+ and updated timestamp field. You can do these for example by doing:
+ @code
+ if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
+ table->timestamp_field->set_time();
+ if (table->next_number_field && record == table->record[0])
+ update_auto_increment();
+ @endcode
+
+ Called from sql_select.cc, sql_acl.cc, sql_update.cc, and sql_insert.cc.
+
+ @see
+ sql_select.cc, sql_acl.cc, sql_update.cc and sql_insert.cc
+*/
+int ha_connect::update_row(const uchar *old_data, uchar *new_data)
+{
+ int rc= 0;
+ PGLOBAL& g= xp->g;
+ DBUG_ENTER("ha_connect::update_row");
+
+ if (xtrace > 1)
+ htrc("update_row: old=%s new=%s\n", old_data, new_data);
+
+ // Check values for possible change in indexed column
+ if ((rc= CheckRecord(g, old_data, new_data)))
+ return rc;
+
+ if (CntUpdateRow(g, tdbp)) {
+ DBUG_PRINT("update_row", ("%s", g->Message));
+ htrc("update_row CONNECT: %s\n", g->Message);
+ rc= HA_ERR_INTERNAL_ERROR;
+ } // endif RC
+
+ DBUG_RETURN(rc);
+} // end of update_row
+
+
+/**
+ @brief
+ This will delete a row. buf will contain a copy of the row to be deleted.
+ The server will call this right after the current row has been called (from
+ either a previous rnd_nexT() or index call).
+
+ @details
+ If you keep a pointer to the last row or can access a primary key it will
+ make doing the deletion quite a bit easier. Keep in mind that the server does
+ not guarantee consecutive deletions. ORDER BY clauses can be used.
+
+ Called in sql_acl.cc and sql_udf.cc to manage internal table
+ information. Called in sql_delete.cc, sql_insert.cc, and
+ sql_select.cc. In sql_select it is used for removing duplicates
+ while in insert it is used for REPLACE calls.
+
+ @see
+ sql_acl.cc, sql_udf.cc, sql_delete.cc, sql_insert.cc and sql_select.cc
+*/
+int ha_connect::delete_row(const uchar *buf)
+{
+ int rc= 0;
+ DBUG_ENTER("ha_connect::delete_row");
+
+ if (CntDeleteRow(xp->g, tdbp, false)) {
+ rc= HA_ERR_INTERNAL_ERROR;
+ htrc("delete_row CONNECT: %s\n", xp->g->Message);
+ } // endif DeleteRow
+
+ DBUG_RETURN(rc);
+} // end of delete_row
+
+
+/****************************************************************************/
+/* We seem to come here at the begining of an index use. */
+/****************************************************************************/
+int ha_connect::index_init(uint idx, bool sorted)
+{
+ int rc;
+ PGLOBAL& g= xp->g;
+ DBUG_ENTER("index_init");
+
+ if (xtrace)
+ htrc("index_init: this=%p idx=%u sorted=%d\n", this, idx, sorted);
+
+ if ((rc= rnd_init(0)))
+ return rc;
+
+ if (locked == 2) {
+ // Indexes are not updated in lock write mode
+ active_index= MAX_KEY;
+ indexing= 0;
+ DBUG_RETURN(0);
+ } // endif locked
+
+ indexing= CntIndexInit(g, tdbp, (signed)idx);
+
+ if (indexing <= 0) {
+ DBUG_PRINT("index_init", ("%s", g->Message));
+ htrc("index_init CONNECT: %s\n", g->Message);
+ active_index= MAX_KEY;
+ rc= HA_ERR_INTERNAL_ERROR;
+ } else {
+ if (((PTDBDOX)tdbp)->To_Kindex->GetNum_K()) {
+ if (((PTDBASE)tdbp)->GetFtype() != RECFM_NAF)
+ ((PTDBDOX)tdbp)->GetTxfp()->ResetBuffer(g);
+
+ active_index= idx;
+ } else // Void table
+ indexing= 0;
+
+ rc= 0;
+ } // endif indexing
+
+ if (xtrace)
+ htrc("index_init: rc=%d indexing=%d active_index=%d\n",
+ rc, indexing, active_index);
+
+ DBUG_RETURN(rc);
+} // end of index_init
+
+/****************************************************************************/
+/* We seem to come here at the end of an index use. */
+/****************************************************************************/
+int ha_connect::index_end()
+{
+ DBUG_ENTER("index_end");
+ active_index= MAX_KEY;
+ ds_mrr.dsmrr_close();
+ DBUG_RETURN(rnd_end());
+} // end of index_end
+
+
+/****************************************************************************/
+/* This is internally called by all indexed reading functions. */
+/****************************************************************************/
+int ha_connect::ReadIndexed(uchar *buf, OPVAL op, const uchar *key, uint key_len)
+{
+ int rc;
+
+//statistic_increment(ha_read_key_count, &LOCK_status);
+
+ switch (CntIndexRead(xp->g, tdbp, op, key, (int)key_len, mrr)) {
+ case RC_OK:
+ xp->fnd++;
+ rc= MakeRecord((char*)buf);
+ break;
+ case RC_EF: // End of file
+ rc= HA_ERR_END_OF_FILE;
+ break;
+ case RC_NF: // Not found
+ xp->nfd++;
+ rc= (op == OP_SAME) ? HA_ERR_END_OF_FILE : HA_ERR_KEY_NOT_FOUND;
+ break;
+ default: // Read error
+ DBUG_PRINT("ReadIndexed", ("%s", xp->g->Message));
+ htrc("ReadIndexed: %s\n", xp->g->Message);
+ rc= HA_ERR_INTERNAL_ERROR;
+ break;
+ } // endswitch RC
+
+ if (xtrace > 1)
+ htrc("ReadIndexed: op=%d rc=%d\n", op, rc);
+
+ table->status= (rc == RC_OK) ? 0 : STATUS_NOT_FOUND;
+ return rc;
+} // end of ReadIndexed
+
+
+#ifdef NOT_USED
+/**
+ @brief
+ Positions an index cursor to the index specified in the handle. Fetches the
+ row if available. If the key value is null, begin at the first key of the
+ index.
+*/
+int ha_connect::index_read_map(uchar *buf, const uchar *key,
+ key_part_map keypart_map __attribute__((unused)),
+ enum ha_rkey_function find_flag
+ __attribute__((unused)))
+{
+ DBUG_ENTER("ha_connect::index_read");
+ DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+}
+#endif // NOT_USED
+
+
+/****************************************************************************/
+/* This is called by handler::index_read_map. */
+/****************************************************************************/
+int ha_connect::index_read(uchar * buf, const uchar * key, uint key_len,
+ enum ha_rkey_function find_flag)
+{
+ int rc;
+ OPVAL op= OP_XX;
+ DBUG_ENTER("ha_connect::index_read");
+
+ switch(find_flag) {
+ case HA_READ_KEY_EXACT: op= OP_EQ; break;
+ case HA_READ_AFTER_KEY: op= OP_GT; break;
+ case HA_READ_KEY_OR_NEXT: op= OP_GE; break;
+ default: DBUG_RETURN(-1); break;
+ } // endswitch find_flag
+
+ if (xtrace > 1)
+ htrc("%p index_read: op=%d\n", this, op);
+
+ if (indexing > 0)
+ rc= ReadIndexed(buf, op, key, key_len);
+ else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ DBUG_RETURN(rc);
+} // end of index_read
+
+
+/**
+ @brief
+ Used to read forward through the index.
+*/
+int ha_connect::index_next(uchar *buf)
+{
+ int rc;
+ DBUG_ENTER("ha_connect::index_next");
+ //statistic_increment(ha_read_next_count, &LOCK_status);
+
+ if (indexing > 0)
+ rc= ReadIndexed(buf, OP_NEXT);
+ else if (!indexing)
+ rc= rnd_next(buf);
+ else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ DBUG_RETURN(rc);
+} // end of index_next
+
+
+#ifdef NOT_USED
+/**
+ @brief
+ Used to read backwards through the index.
+*/
+int ha_connect::index_prev(uchar *buf)
+{
+ DBUG_ENTER("ha_connect::index_prev");
+ DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+}
+#endif // NOT_USED
+
+
+/**
+ @brief
+ index_first() asks for the first key in the index.
+
+ @details
+ Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
+
+ @see
+ opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
+*/
+int ha_connect::index_first(uchar *buf)
+{
+ int rc;
+ DBUG_ENTER("ha_connect::index_first");
+
+ if (indexing > 0)
+ rc= ReadIndexed(buf, OP_FIRST);
+ else if (indexing < 0)
+ rc= HA_ERR_INTERNAL_ERROR;
+ else if (CntRewindTable(xp->g, tdbp)) {
+ table->status= STATUS_NOT_FOUND;
+ rc= HA_ERR_INTERNAL_ERROR;
+ } else
+ rc= rnd_next(buf);
+
+ DBUG_RETURN(rc);
+} // end of index_first
+
+
+#ifdef NOT_USED
+/**
+ @brief
+ index_last() asks for the last key in the index.
+
+ @details
+ Called from opt_range.cc, opt_sum.cc, sql_handler.cc, and sql_select.cc.
+
+ @see
+ opt_range.cc, opt_sum.cc, sql_handler.cc and sql_select.cc
+*/
+int ha_connect::index_last(uchar *buf)
+{
+ DBUG_ENTER("ha_connect::index_last");
+ DBUG_RETURN(HA_ERR_WRONG_COMMAND);
+}
+#endif // NOT_USED
+
+
+/****************************************************************************/
+/* This is called to get more rows having the same index value. */
+/****************************************************************************/
+int ha_connect::index_next_same(uchar *buf, const uchar *key, uint keylen)
+{
+ int rc;
+ DBUG_ENTER("ha_connect::index_next_same");
+//statistic_increment(ha_read_next_count, &LOCK_status);
+
+ if (!indexing)
+ rc= rnd_next(buf);
+ else if (indexing > 0)
+ rc= ReadIndexed(buf, OP_SAME);
+ else
+ rc= HA_ERR_INTERNAL_ERROR;
+
+ DBUG_RETURN(rc);
+} // end of index_next_same
+
+
+/**
+ @brief
+ rnd_init() is called when the system wants the storage engine to do a table
+ scan. See the example in the introduction at the top of this file to see when
+ rnd_init() is called.
+
+ @details
+ Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
+ and sql_update.cc.
+
+ @note
+ We always call open and extern_lock/start_stmt before comming here.
+
+ @see
+ filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
+*/
+int ha_connect::rnd_init(bool scan)
+{
+ int rc;
+ PGLOBAL g= ((table && table->in_use) ? GetPlug(table->in_use, xp) :
+ (xp) ? xp->g : NULL);
+ DBUG_ENTER("ha_connect::rnd_init");
+
+ // This is not tested yet
+ if (xmod == MODE_ALTER) {
+ xmod= MODE_READ;
+ alter= 1;
+ } // endif xmod
+
+ if (xtrace)
+ htrc("rnd_init: this=%p scan=%d xmod=%d alter=%d\n",
+ this, scan, xmod, alter);
+
+ if (!g || !table || xmod == MODE_INSERT)
+ DBUG_RETURN(HA_ERR_INITIALIZATION);
+
+ // Do not close the table if it was opened yet (locked?)
+ if (IsOpened())
+ DBUG_RETURN(0);
+// CloseTable(g); Was done before making things done twice
+ else if (xp->CheckQuery(valid_query_id))
+ tdbp= NULL; // Not valid anymore
+
+ // When updating, to avoid skipped update, force the table
+ // handler to retrieve write-only fields to be able to compare
+ // records and detect data change.
+ if (xmod == MODE_UPDATE)
+ bitmap_union(table->read_set, table->write_set);
+
+ if ((rc= OpenTable(g, xmod == MODE_DELETE)))
+ DBUG_RETURN(rc);
+
+ xp->nrd= xp->fnd= xp->nfd= 0;
+ xp->tb1= my_interval_timer();
+ DBUG_RETURN(0);
+} // end of rnd_init
+
+/**
+ @brief
+ Not described.
+
+ @note
+ The previous version said:
+ Stop scanning of table. Note that this may be called several times during
+ execution of a sub select.
+ =====> This has been moved to external lock to avoid closing subselect tables.
+*/
+int ha_connect::rnd_end()
+{
+ int rc= 0;
+ DBUG_ENTER("ha_connect::rnd_end");
+
+ // If this is called by a later query, the table may have
+ // been already closed and the tdbp is not valid anymore.
+// if (tdbp && xp->last_query_id == valid_query_id)
+// rc= CloseTable(xp->g);
+
+ ds_mrr.dsmrr_close();
+ DBUG_RETURN(rc);
+} // end of rnd_end
+
+
+/**
+ @brief
+ This is called for each row of the table scan. When you run out of records
+ you should return HA_ERR_END_OF_FILE. Fill buff up with the row information.
+ The Field structure for the table is the key to getting data into buf
+ in a manner that will allow the server to understand it.
+
+ @details
+ Called from filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc,
+ and sql_update.cc.
+
+ @see
+ filesort.cc, records.cc, sql_handler.cc, sql_select.cc, sql_table.cc and sql_update.cc
+*/
+int ha_connect::rnd_next(uchar *buf)
+{
+ int rc;
+ DBUG_ENTER("ha_connect::rnd_next");
+//statistic_increment(ha_read_rnd_next_count, &LOCK_status);
+
+ if (tdbp->GetMode() == MODE_ANY) {
+ // We will stop on next read
+ if (!stop) {
+ stop= true;
+ DBUG_RETURN(RC_OK);
+ } else
+ DBUG_RETURN(HA_ERR_END_OF_FILE);
+
+ } // endif Mode
+
+ switch (CntReadNext(xp->g, tdbp)) {
+ case RC_OK:
+ rc= MakeRecord((char*)buf);
+ break;
+ case RC_EF: // End of file
+ rc= HA_ERR_END_OF_FILE;
+ break;
+ case RC_NF: // Not found
+ rc= HA_ERR_RECORD_DELETED;
+ break;
+ default: // Read error
+ htrc("rnd_next CONNECT: %s\n", xp->g->Message);
+ rc= (records()) ? HA_ERR_INTERNAL_ERROR : HA_ERR_END_OF_FILE;
+ break;
+ } // endswitch RC
+
+ if (xtrace > 1 && (rc || !(xp->nrd++ % 16384))) {
+ ulonglong tb2= my_interval_timer();
+ double elapsed= (double) (tb2 - xp->tb1) / 1000000000ULL;
+ DBUG_PRINT("rnd_next", ("rc=%d nrd=%u fnd=%u nfd=%u sec=%.3lf\n",
+ rc, (uint)xp->nrd, (uint)xp->fnd,
+ (uint)xp->nfd, elapsed));
+ htrc("rnd_next: rc=%d nrd=%u fnd=%u nfd=%u sec=%.3lf\n",
+ rc, (uint)xp->nrd, (uint)xp->fnd,
+ (uint)xp->nfd, elapsed);
+ xp->tb1= tb2;
+ xp->fnd= xp->nfd= 0;
+ } // endif nrd
+
+ table->status= (!rc) ? 0 : STATUS_NOT_FOUND;
+ DBUG_RETURN(rc);
+} // end of rnd_next
+
+
+/**
+ @brief
+ position() is called after each call to rnd_next() if the data needs
+ to be ordered. You can do something like the following to store
+ the position:
+ @code
+ my_store_ptr(ref, ref_length, current_position);
+ @endcode
+
+ @details
+ The server uses ref to store data. ref_length in the above case is
+ the size needed to store current_position. ref is just a byte array
+ that the server will maintain. If you are using offsets to mark rows, then
+ current_position should be the offset. If it is a primary key like in
+ BDB, then it needs to be a primary key.
+
+ Called from filesort.cc, sql_select.cc, sql_delete.cc, and sql_update.cc.
+
+ @see
+ filesort.cc, sql_select.cc, sql_delete.cc and sql_update.cc
+*/
+void ha_connect::position(const uchar *record)
+{
+ DBUG_ENTER("ha_connect::position");
+//if (((PTDBASE)tdbp)->GetDef()->Indexable())
+ my_store_ptr(ref, ref_length, (my_off_t)((PTDBASE)tdbp)->GetRecpos());
+ DBUG_VOID_RETURN;
+} // end of position
+
+
+/**
+ @brief
+ This is like rnd_next, but you are given a position to use
+ to determine the row. The position will be of the type that you stored in
+ ref. You can use my_get_ptr(pos,ref_length) to retrieve whatever key
+ or position you saved when position() was called.
+
+ @details
+ Called from filesort.cc, records.cc, sql_insert.cc, sql_select.cc, and sql_update.cc.
+
+ @note
+ Is this really useful? It was never called even when sorting.
+
+ @see
+ filesort.cc, records.cc, sql_insert.cc, sql_select.cc and sql_update.cc
+*/
+int ha_connect::rnd_pos(uchar *buf, uchar *pos)
+{
+ int rc;
+ PTDBASE tp= (PTDBASE)tdbp;
+ DBUG_ENTER("ha_connect::rnd_pos");
+
+ if (!tp->SetRecpos(xp->g, (int)my_get_ptr(pos, ref_length)))
+ rc= rnd_next(buf);
+ else
+ rc= HA_ERR_KEY_NOT_FOUND;
+
+ DBUG_RETURN(rc);
+} // end of rnd_pos
+
+
+/**
+ @brief
+ ::info() is used to return information to the optimizer. See my_base.h for
+ the complete description.
+
+ @details
+ Currently this table handler doesn't implement most of the fields really needed.
+ SHOW also makes use of this data.
+
+ You will probably want to have the following in your code:
+ @code
+ if (records < 2)
+ records= 2;
+ @endcode
+ The reason is that the server will optimize for cases of only a single
+ record. If, in a table scan, you don't know the number of records, it
+ will probably be better to set records to two so you can return as many
+ records as you need. Along with records, a few more variables you may wish
+ to set are:
+ records
+ deleted
+ data_file_length
+ index_file_length
+ delete_length
+ check_time
+ Take a look at the public variables in handler.h for more information.
+
+ Called in filesort.cc, ha_heap.cc, item_sum.cc, opt_sum.cc, sql_delete.cc,
+ sql_delete.cc, sql_derived.cc, sql_select.cc, sql_select.cc, sql_select.cc,
+ sql_select.cc, sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc,
+ sql_table.cc, sql_union.cc, and sql_update.cc.
+
+ @see
+ filesort.cc, ha_heap.cc, item_sum.cc, opt_sum.cc, sql_delete.cc, sql_delete.cc,
+ sql_derived.cc, sql_select.cc, sql_select.cc, sql_select.cc, sql_select.cc,
+ sql_select.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_show.cc, sql_table.cc,
+ sql_union.cc and sql_update.cc
+*/
+int ha_connect::info(uint flag)
+{
+ bool pure= false;
+ PGLOBAL g= GetPlug((table) ? table->in_use : NULL, xp);
+
+ DBUG_ENTER("ha_connect::info");
+
+ if (xtrace)
+ htrc("%p In info: flag=%u valid_info=%d\n", this, flag, valid_info);
+
+ if (!valid_info) {
+ // tdbp must be available to get updated info
+ if (xp->CheckQuery(valid_query_id) || !tdbp) {
+ if (xmod == MODE_ANY || xmod == MODE_ALTER) {
+ // Pure info, not a query
+ pure= true;
+ xp->CheckCleanup();
+ } // endif xmod
+
+ tdbp= GetTDB(g);
+ } // endif tdbp
+
+ valid_info= CntInfo(g, tdbp, &xinfo);
+ } // endif valid_info
+
+ if (flag & HA_STATUS_VARIABLE) {
+ stats.records= xinfo.records;
+ stats.deleted= 0;
+ stats.data_file_length= xinfo.data_file_length;
+ stats.index_file_length= 0;
+ stats.delete_length= 0;
+ stats.check_time= 0;
+ stats.mean_rec_length= xinfo.mean_rec_length;
+ } // endif HA_STATUS_VARIABLE
+
+ if (flag & HA_STATUS_CONST) {
+ // This is imported from the previous handler and must be reconsidered
+ stats.max_data_file_length= 4294967295;
+ stats.max_index_file_length= 4398046510080;
+ stats.create_time= 0;
+ data_file_name= xinfo.data_file_name;
+ index_file_name= NULL;
+// sortkey= (uint) - 1; // Table is not sorted
+ ref_length= sizeof(int); // Pointer size to row
+ table->s->db_options_in_use= 03;
+ stats.block_size= 1024;
+ table->s->keys_in_use.set_prefix(table->s->keys);
+ table->s->keys_for_keyread= table->s->keys_in_use;
+// table->s->keys_for_keyread.subtract(table->s->read_only_keys);
+ table->s->db_record_offset= 0;
+ } // endif HA_STATUS_CONST
+
+ if (flag & HA_STATUS_ERRKEY) {
+ errkey= 0;
+ } // endif HA_STATUS_ERRKEY
+
+ if (flag & HA_STATUS_TIME)
+ stats.update_time= 0;
+
+ if (flag & HA_STATUS_AUTO)
+ stats.auto_increment_value= 1;
+
+ if (tdbp && pure)
+ CloseTable(g); // Not used anymore
+
+ DBUG_RETURN(0);
+} // end of info
+
+
+/**
+ @brief
+ extra() is called whenever the server wishes to send a hint to
+ the storage engine. The myisam engine implements the most hints.
+ ha_innodb.cc has the most exhaustive list of these hints.
+
+ @note
+ This is not yet implemented for CONNECT.
+
+ @see
+ ha_innodb.cc
+*/
+int ha_connect::extra(enum ha_extra_function operation)
+{
+ DBUG_ENTER("ha_connect::extra");
+ DBUG_RETURN(0);
+} // end of extra
+
+
+/**
+ @brief
+ Used to delete all rows in a table, including cases of truncate and cases where
+ the optimizer realizes that all rows will be removed as a result of an SQL statement.
+
+ @details
+ Called from item_sum.cc by Item_func_group_concat::clear(),
+ Item_sum_count_distinct::clear(), and Item_func_group_concat::clear().
+ Called from sql_delete.cc by mysql_delete().
+ Called from sql_select.cc by JOIN::reinit().
+ Called from sql_union.cc by st_select_lex_unit::exec().
+
+ @see
+ Item_func_group_concat::clear(), Item_sum_count_distinct::clear() and
+ Item_func_group_concat::clear() in item_sum.cc;
+ mysql_delete() in sql_delete.cc;
+ JOIN::reinit() in sql_select.cc and
+ st_select_lex_unit::exec() in sql_union.cc.
+*/
+int ha_connect::delete_all_rows()
+{
+ int rc= 0;
+ PGLOBAL g= xp->g;
+ DBUG_ENTER("ha_connect::delete_all_rows");
+
+ if (tdbp && tdbp->GetUse() == USE_OPEN &&
+ tdbp->GetAmType() != TYPE_AM_XML &&
+ ((PTDBASE)tdbp)->GetFtype() != RECFM_NAF)
+ // Close and reopen the table so it will be deleted
+ rc= CloseTable(g);
+
+ if (!(rc= OpenTable(g))) {
+ if (CntDeleteRow(g, tdbp, true)) {
+ htrc("%s\n", g->Message);
+ rc= HA_ERR_INTERNAL_ERROR;
+ } // endif
+
+ } // endif rc
+
+ DBUG_RETURN(rc);
+} // end of delete_all_rows
+
+
+bool ha_connect::check_privileges(THD *thd, PTOS options, char *dbn)
+{
+ const char *db= (dbn && *dbn) ? dbn : NULL;
+ TABTYPE type=GetRealType(options);
+
+ switch (type) {
+ case TAB_UNDEF:
+// case TAB_CATLG:
+ case TAB_PLG:
+ case TAB_JCT:
+ case TAB_DMY:
+ case TAB_NIY:
+ my_printf_error(ER_UNKNOWN_ERROR,
+ "Unsupported table type %s", MYF(0), options->type);
+ return true;
+
+ case TAB_DOS:
+ case TAB_FIX:
+ case TAB_BIN:
+ case TAB_CSV:
+ case TAB_FMT:
+ case TAB_DBF:
+ case TAB_XML:
+ case TAB_INI:
+ case TAB_VEC:
+ if (options->filename && *options->filename) {
+ char *s, path[FN_REFLEN], dbpath[FN_REFLEN];
+#if defined(WIN32)
+ s= "\\";
+#else // !WIN32
+ s= "/";
+#endif // !WIN32
+ strcpy(dbpath, mysql_real_data_home);
+
+ if (db)
+ strcat(strcat(dbpath, db), s);
+
+ (void) fn_format(path, options->filename, dbpath, "",
+ MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
+
+ if (!is_secure_file_path(path)) {
+ my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--secure-file-priv");
+ return true;
+ } // endif path
+
+ } else
+ return false;
+
+ /* Fall through to check FILE_ACL */
+ case TAB_ODBC:
+ case TAB_MYSQL:
+ case TAB_DIR:
+ case TAB_MAC:
+ case TAB_WMI:
+ case TAB_OEM:
+ return check_access(thd, FILE_ACL, db, NULL, NULL, 0, 0);
+
+ // This is temporary until a solution is found
+ case TAB_TBL:
+ case TAB_XCL:
+ case TAB_PRX:
+ case TAB_OCCUR:
+ case TAB_PIVOT:
+ return false;
+ } // endswitch type
+
+ my_printf_error(ER_UNKNOWN_ERROR, "check_privileges failed", MYF(0));
+ return true;
+} // end of check_privileges
+
+// Check that two indexes are equivalent
+bool ha_connect::IsSameIndex(PIXDEF xp1, PIXDEF xp2)
+{
+ bool b= true;
+ PKPDEF kp1, kp2;
+
+ if (stricmp(xp1->Name, xp2->Name))
+ b= false;
+ else if (xp1->Nparts != xp2->Nparts ||
+ xp1->MaxSame != xp2->MaxSame ||
+ xp1->Unique != xp2->Unique)
+ b= false;
+ else for (kp1= xp1->ToKeyParts, kp2= xp2->ToKeyParts;
+ b && (kp1 || kp2);
+ kp1= kp1->Next, kp2= kp2->Next)
+ if (!kp1 || !kp2)
+ b= false;
+ else if (stricmp(kp1->Name, kp2->Name))
+ b= false;
+ else if (kp1->Klen != kp2->Klen)
+ b= false;
+
+ return b;
+} // end of IsSameIndex
+
+MODE ha_connect::CheckMode(PGLOBAL g, THD *thd,
+ MODE newmode, bool *chk, bool *cras)
+{
+ if (xtrace) {
+ LEX_STRING *query_string= thd_query_string(thd);
+ htrc("%p check_mode: cmdtype=%d\n", this, thd_sql_command(thd));
+ htrc("Cmd=%.*s\n", (int) query_string->length, query_string->str);
+ } // endif xtrace
+
+ // Next code is temporarily replaced until sql_command is set
+ stop= false;
+
+ if (newmode == MODE_WRITE) {
+ switch (thd_sql_command(thd)) {
+ case SQLCOM_LOCK_TABLES:
+ locked= 2;
+ case SQLCOM_CREATE_TABLE:
+ case SQLCOM_INSERT:
+ case SQLCOM_LOAD:
+ case SQLCOM_INSERT_SELECT:
+ newmode= MODE_INSERT;
+ break;
+// case SQLCOM_REPLACE:
+// case SQLCOM_REPLACE_SELECT:
+// newmode= MODE_UPDATE; // To be checked
+// break;
+ case SQLCOM_DELETE:
+ case SQLCOM_DELETE_MULTI:
+ case SQLCOM_TRUNCATE:
+ newmode= MODE_DELETE;
+ break;
+ case SQLCOM_UPDATE:
+ case SQLCOM_UPDATE_MULTI:
+ newmode= MODE_UPDATE;
+ break;
+ case SQLCOM_SELECT:
+ case SQLCOM_OPTIMIZE:
+ newmode= MODE_READ;
+ break;
+ case SQLCOM_DROP_TABLE:
+ case SQLCOM_RENAME_TABLE:
+ newmode= MODE_ANY;
+ break;
+ case SQLCOM_DROP_INDEX:
+ case SQLCOM_CREATE_INDEX:
+ newmode= MODE_ANY;
+// stop= true;
+ break;
+ case SQLCOM_CREATE_VIEW:
+ case SQLCOM_DROP_VIEW:
+ newmode= MODE_ANY;
+ break;
+ case SQLCOM_ALTER_TABLE:
+ newmode= MODE_ALTER;
+ break;
+ default:
+ htrc("Unsupported sql_command=%d", thd_sql_command(thd));
+ strcpy(g->Message, "CONNECT Unsupported command");
+ my_message(ER_NOT_ALLOWED_COMMAND, g->Message, MYF(0));
+ newmode= MODE_ERROR;
+ break;
+ } // endswitch newmode
+
+ } else if (newmode == MODE_READ) {
+ switch (thd_sql_command(thd)) {
+ case SQLCOM_CREATE_TABLE:
+ *chk= true;
+ *cras= true;
+ case SQLCOM_INSERT:
+ case SQLCOM_LOAD:
+ case SQLCOM_INSERT_SELECT:
+// case SQLCOM_REPLACE:
+// case SQLCOM_REPLACE_SELECT:
+ case SQLCOM_DELETE:
+ case SQLCOM_DELETE_MULTI:
+ case SQLCOM_TRUNCATE:
+ case SQLCOM_UPDATE:
+ case SQLCOM_UPDATE_MULTI:
+ case SQLCOM_SELECT:
+ case SQLCOM_OPTIMIZE:
+ break;
+ case SQLCOM_LOCK_TABLES:
+ locked= 1;
+ break;
+ case SQLCOM_DROP_INDEX:
+ case SQLCOM_CREATE_INDEX:
+ *chk= true;
+// stop= true;
+ case SQLCOM_DROP_TABLE:
+ case SQLCOM_RENAME_TABLE:
+ newmode= MODE_ANY;
+ break;
+ case SQLCOM_CREATE_VIEW:
+ case SQLCOM_DROP_VIEW:
+ newmode= MODE_ANY;
+ break;
+ case SQLCOM_ALTER_TABLE:
+ *chk= true;
+ newmode= MODE_ALTER;
+ break;
+ default:
+ htrc("Unsupported sql_command=%d", thd_sql_command(thd));
+ strcpy(g->Message, "CONNECT Unsupported command");
+ my_message(ER_NOT_ALLOWED_COMMAND, g->Message, MYF(0));
+ newmode= MODE_ERROR;
+ break;
+ } // endswitch newmode
+
+ } // endif's newmode
+
+ if (xtrace)
+ htrc("New mode=%d\n", newmode);
+
+ return newmode;
+} // end of check_mode
+
+int ha_connect::start_stmt(THD *thd, thr_lock_type lock_type)
+{
+ int rc= 0;
+ bool chk=false, cras= false;
+ MODE newmode;
+ PGLOBAL g= GetPlug(thd, xp);
+ DBUG_ENTER("ha_connect::start_stmt");
+
+ // Action will depend on lock_type
+ switch (lock_type) {
+ case TL_WRITE_ALLOW_WRITE:
+ case TL_WRITE_CONCURRENT_INSERT:
+ case TL_WRITE_DELAYED:
+ case TL_WRITE_DEFAULT:
+ case TL_WRITE_LOW_PRIORITY:
+ case TL_WRITE:
+ case TL_WRITE_ONLY:
+ newmode= MODE_WRITE;
+ break;
+ case TL_READ:
+ case TL_READ_WITH_SHARED_LOCKS:
+ case TL_READ_HIGH_PRIORITY:
+ case TL_READ_NO_INSERT:
+ case TL_READ_DEFAULT:
+ newmode= MODE_READ;
+ break;
+ case TL_UNLOCK:
+ default:
+ newmode= MODE_ANY;
+ break;
+ } // endswitch mode
+
+ xmod= CheckMode(g, thd, newmode, &chk, &cras);
+ DBUG_RETURN((xmod == MODE_ERROR) ? HA_ERR_INTERNAL_ERROR : 0);
+} // end of start_stmt
+
+/**
+ @brief
+ This create a lock on the table. If you are implementing a storage engine
+ that can handle transacations look at ha_berkely.cc to see how you will
+ want to go about doing this. Otherwise you should consider calling flock()
+ here. Hint: Read the section "locking functions for mysql" in lock.cc to understand
+ this.
+
+ @details
+ Called from lock.cc by lock_external() and unlock_external(). Also called
+ from sql_table.cc by copy_data_between_tables().
+
+ @note
+ Following what we did in the MySQL XDB handler, we use this call to actually
+ physically open the table. This could be reconsider when finalizing this handler
+ design, which means we have a better understanding of what MariaDB does.
+
+ @see
+ lock.cc by lock_external() and unlock_external() in lock.cc;
+ the section "locking functions for mysql" in lock.cc;
+ copy_data_between_tables() in sql_table.cc.
+*/
+int ha_connect::external_lock(THD *thd, int lock_type)
+{
+ int rc= 0;
+ bool xcheck=false, cras= false;
+ MODE newmode;
+ PTOS options= GetTableOptionStruct(table);
+ PGLOBAL g= GetPlug(thd, xp);
+ DBUG_ENTER("ha_connect::external_lock");
+
+ DBUG_ASSERT(thd == current_thd);
+
+ if (xtrace)
+ htrc("external_lock: this=%p thd=%p xp=%p g=%p lock_type=%d\n",
+ this, thd, xp, g, lock_type);
+
+ if (!g)
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+
+ // Action will depend on lock_type
+ switch (lock_type) {
+ case F_WRLCK:
+ newmode= MODE_WRITE;
+ break;
+ case F_RDLCK:
+ newmode= MODE_READ;
+ break;
+ case F_UNLCK:
+ default:
+ newmode= MODE_ANY;
+ break;
+ } // endswitch mode
+
+ if (newmode == MODE_ANY) {
+ int sqlcom= thd_sql_command(thd);
+
+ // This is unlocking, do it by closing the table
+ if (xp->CheckQueryID() && sqlcom != SQLCOM_UNLOCK_TABLES
+ && sqlcom != SQLCOM_LOCK_TABLES)
+ rc= 2; // Logical error ???
+// else if (g->Xchk && (sqlcom == SQLCOM_CREATE_INDEX ||
+// sqlcom == SQLCOM_DROP_INDEX)) {
+ else if (g->Xchk) {
+ if (!tdbp) {
+ if (!(tdbp= GetTDB(g)))
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ else if (!((PTDBASE)tdbp)->GetDef()->Indexable()) {
+ sprintf(g->Message, "external_lock: Table %s is not indexable", tdbp->GetName());
+// DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ DBUG_RETURN(0);
+ } // endif Indexable
+
+ bool oldsep= ((PCHK)g->Xchk)->oldsep;
+ bool newsep= ((PCHK)g->Xchk)->newsep;
+ PTDBDOS tdp= (PTDBDOS)tdbp;
+
+ PDOSDEF ddp= (PDOSDEF)tdp->GetDef();
+ PIXDEF xp, xp1, xp2, drp=NULL, adp= NULL;
+ PIXDEF oldpix= ((PCHK)g->Xchk)->oldpix;
+ PIXDEF newpix= ((PCHK)g->Xchk)->newpix;
+ PIXDEF *xlst, *xprc;
+
+ ddp->SetIndx(oldpix);
+
+ if (oldsep != newsep) {
+ // All indexes have to be remade
+ ddp->DeleteIndexFile(g, NULL);
+ oldpix= NULL;
+ ddp->SetIndx(NULL);
+ SetBooleanOption("Sepindex", newsep);
+ } else if (newsep) {
+ // Make the list of dropped indexes
+ xlst= &drp; xprc= &oldpix;
+
+ for (xp2= oldpix; xp2; xp2= xp) {
+ for (xp1= newpix; xp1; xp1= xp1->Next)
+ if (IsSameIndex(xp1, xp2))
+ break; // Index not to drop
+
+ xp= xp2->GetNext();
+
+ if (!xp1) {
+ *xlst= xp2;
+ *xprc= xp;
+ *(xlst= &xp2->Next)= NULL;
+ } else
+ xprc= &xp2->Next;
+
+ } // endfor xp2
+
+ if (drp) {
+ // Here we erase the index files
+ ddp->DeleteIndexFile(g, drp);
+ } // endif xp1
+
+ } else if (oldpix) {
+ // TODO: optimize the case of just adding new indexes
+ if (!newpix)
+ ddp->DeleteIndexFile(g, NULL);
+
+ oldpix= NULL; // To remake all indexes
+ ddp->SetIndx(NULL);
+ } // endif sepindex
+
+ // Make the list of new created indexes
+ xlst= &adp; xprc= &newpix;
+
+ for (xp1= newpix; xp1; xp1= xp) {
+ for (xp2= oldpix; xp2; xp2= xp2->Next)
+ if (IsSameIndex(xp1, xp2))
+ break; // Index already made
+
+ xp= xp1->Next;
+
+ if (!xp2) {
+ *xlst= xp1;
+ *xprc= xp;
+ *(xlst= &xp1->Next)= NULL;
+ } else
+ xprc= &xp1->Next;
+
+ } // endfor xp1
+
+ if (adp)
+ // Here we do make the new indexes
+ if (tdp->MakeIndex(g, adp, true) == RC_FX) {
+ // Make it a warning to avoid crash
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
+ 0, g->Message);
+ rc= 0;
+ } // endif MakeIndex
+
+ } // endif Tdbp
+
+ } // endelse Xchk
+
+ if (CloseTable(g)) {
+ // This is an error while builing index
+ // Make it a warning to avoid crash
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ rc= 0;
+ } // endif Close
+
+ locked= 0;
+ DBUG_RETURN(rc);
+ } // endif MODE_ANY
+
+ DBUG_ASSERT(table && table->s);
+
+ if (check_privileges(thd, options, table->s->db.str)) {
+ strcpy(g->Message, "This operation requires the FILE privilege");
+ htrc("%s\n", g->Message);
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif check_privileges
+
+ // Table mode depends on the query type
+ newmode= CheckMode(g, thd, newmode, &xcheck, &cras);
+
+ if (newmode == MODE_ERROR)
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+
+ // If this is the start of a new query, cleanup the previous one
+ if (xp->CheckCleanup()) {
+ tdbp= NULL;
+ valid_info= false;
+ } // endif CheckCleanup
+
+#if 0
+ if (xcheck) {
+ // This must occur after CheckCleanup
+ if (!g->Xchk) {
+ g->Xchk= new(g) XCHK;
+ ((PCHK)g->Xchk)->oldsep= GetBooleanOption("Sepindex", false);
+ ((PCHK)g->Xchk)->oldpix= GetIndexInfo();
+ } // endif Xchk
+
+ } else
+ g->Xchk= NULL;
+#endif // 0
+
+ if (cras)
+ g->Createas= 1; // To tell created table to ignore FLAG
+
+ if (xtrace) {
+#if 0
+ htrc("xcheck=%d cras=%d\n", xcheck, cras);
+
+ if (xcheck)
+ htrc("oldsep=%d oldpix=%p\n",
+ ((PCHK)g->Xchk)->oldsep, ((PCHK)g->Xchk)->oldpix);
+#endif // 0
+ htrc("Calling CntCheckDB db=%s cras=%d\n", GetDBName(NULL), cras);
+ } // endif xtrace
+
+ // Set or reset the good database environment
+ if (CntCheckDB(g, this, GetDBName(NULL))) {
+ htrc("%p external_lock: %s\n", this, g->Message);
+ rc= HA_ERR_INTERNAL_ERROR;
+ // This can NOT be called without open called first, but
+ // the table can have been closed since then
+ } else if (!tdbp || xp->CheckQuery(valid_query_id) || xmod != newmode) {
+ if (tdbp) {
+ // If this is called by a later query, the table may have
+ // been already closed and the tdbp is not valid anymore.
+ if (xp->last_query_id == valid_query_id)
+ rc= CloseTable(g);
+ else
+ tdbp= NULL;
+
+ } // endif tdbp
+
+ xmod= newmode;
+
+ // Delay open until used fields are known
+ } // endif tdbp
+
+ if (xtrace)
+ htrc("external_lock: rc=%d\n", rc);
+
+ DBUG_RETURN(rc);
+} // end of external_lock
+
+
+/**
+ @brief
+ The idea with handler::store_lock() is: The statement decides which locks
+ should be needed for the table. For updates/deletes/inserts we get WRITE
+ locks, for SELECT... we get read locks.
+
+ @details
+ Before adding the lock into the table lock handler (see thr_lock.c),
+ mysqld calls store lock with the requested locks. Store lock can now
+ modify a write lock to a read lock (or some other lock), ignore the
+ lock (if we don't want to use MySQL table locks at all), or add locks
+ for many tables (like we do when we are using a MERGE handler).
+
+ Berkeley DB, for example, changes all WRITE locks to TL_WRITE_ALLOW_WRITE
+ (which signals that we are doing WRITES, but are still allowing other
+ readers and writers).
+
+ When releasing locks, store_lock() is also called. In this case one
+ usually doesn't have to do anything.
+
+ In some exceptional cases MySQL may send a request for a TL_IGNORE;
+ This means that we are requesting the same lock as last time and this
+ should also be ignored. (This may happen when someone does a flush
+ table when we have opened a part of the tables, in which case mysqld
+ closes and reopens the tables and tries to get the same locks at last
+ time). In the future we will probably try to remove this.
+
+ Called from lock.cc by get_lock_data().
+
+ @note
+ In this method one should NEVER rely on table->in_use, it may, in fact,
+ refer to a different thread! (this happens if get_lock_data() is called
+ from mysql_lock_abort_for_thread() function)
+
+ @see
+ get_lock_data() in lock.cc
+*/
+THR_LOCK_DATA **ha_connect::store_lock(THD *thd,
+ THR_LOCK_DATA **to,
+ enum thr_lock_type lock_type)
+{
+ if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
+ lock.type=lock_type;
+ *to++ = &lock;
+ return to;
+}
+
+
+/**
+ Searches for a pointer to the last occurrence of the
+ character c in the string src.
+ Returns true on failure, false on success.
+*/
+static bool
+strnrchr(LEX_CSTRING *ls, const char *src, size_t length, int c)
+{
+ const char *srcend, *s;
+ for (s= srcend= src + length; s > src; s--)
+ {
+ if (s[-1] == c)
+ {
+ ls->str= s;
+ ls->length= srcend - s;
+ return false;
+ }
+ }
+ return true;
+}
+
+
+/**
+ Split filename into database and table name.
+*/
+static bool
+filename_to_dbname_and_tablename(const char *filename,
+ char *database, size_t database_size,
+ char *table, size_t table_size)
+{
+#if defined(WIN32)
+ char slash= '\\';
+#else // !WIN32
+ char slash= '/';
+#endif // !WIN32
+ LEX_CSTRING d, t;
+ size_t length= strlen(filename);
+
+ /* Find filename - the rightmost directory part */
+ if (strnrchr(&t, filename, length, slash) || t.length + 1 > table_size)
+ return true;
+ memcpy(table, t.str, t.length);
+ table[t.length]= '\0';
+ if (!(length-= t.length))
+ return true;
+
+ length--; /* Skip slash */
+
+ /* Find database name - the second rightmost directory part */
+ if (strnrchr(&d, filename, length, slash) || d.length + 1 > database_size)
+ return true;
+ memcpy(database, d.str, d.length);
+ database[d.length]= '\0';
+ return false;
+} // end of filename_to_dbname_and_tablename
+
+/**
+ @brief
+ Used to delete or rename a table. By the time delete_table() has been
+ called all opened references to this table will have been closed
+ (and your globally shared references released) ===> too bad!!!
+ The variable name will just be the name of the table.
+ You will need to remove or rename any files you have created at
+ this point.
+
+ @details
+ If you do not implement this, the default delete_table() is called from
+ handler.cc and it will delete all files with the file extensions returned
+ by bas_ext().
+
+ Called from handler.cc by delete_table and ha_create_table(). Only used
+ during create if the table_flag HA_DROP_BEFORE_CREATE was specified for
+ the storage engine.
+
+ @see
+ delete_table and ha_create_table() in handler.cc
+*/
+int ha_connect::delete_or_rename_table(const char *name, const char *to)
+{
+ DBUG_ENTER("ha_connect::delete_or_rename_table");
+ char db[128], tabname[128];
+ int rc= 0;
+ bool ok= false;
+ THD *thd= current_thd;
+ int sqlcom= thd_sql_command(thd);
+
+ if (xtrace) {
+ if (to)
+ htrc("rename_table: this=%p thd=%p sqlcom=%d from=%s to=%s\n",
+ this, thd, sqlcom, name, to);
+ else
+ htrc("delete_table: this=%p thd=%p sqlcom=%d name=%s\n",
+ this, thd, sqlcom, name);
+
+ } // endif xtrace
+
+ if (to && (filename_to_dbname_and_tablename(to, db, sizeof(db),
+ tabname, sizeof(tabname))
+ || (*tabname == '#' && sqlcom == SQLCOM_CREATE_INDEX)))
+ DBUG_RETURN(0);
+
+ if (filename_to_dbname_and_tablename(name, db, sizeof(db),
+ tabname, sizeof(tabname))
+ || (*tabname == '#' && sqlcom == SQLCOM_CREATE_INDEX))
+ DBUG_RETURN(0);
+
+ // If a temporary file exists, all the tests below were passed
+ // successfully when making it, so they are not needed anymore
+ // in particular because they sometimes cause DBUG_ASSERT crash.
+ if (*tabname != '#') {
+ // We have to retrieve the information about this table options.
+ ha_table_option_struct *pos;
+ char key[MAX_DBKEY_LENGTH];
+ uint key_length;
+ TABLE_SHARE *share;
+
+ key_length= tdc_create_key(key, db, tabname);
+
+ // share contains the option struct that we need
+ if (!(share= alloc_table_share(db, tabname, key, key_length)))
+ DBUG_RETURN(rc);
+
+#if 0
+ if (*tabname == '#') {
+ // These are in ???? charset after renaming
+ char *p= strchr(share->path.str, '@');
+ strcpy(p, share->table_name.str);
+ share->path.length= strlen(share->path.str);
+ share->normalized_path.length= share->path.length;
+ } // endif tabname
+#endif // 0
+
+ // Get the share info from the .frm file
+ if (!open_table_def(thd, share)) {
+ // Now we can work
+ if ((pos= share->option_struct)) {
+ if (check_privileges(thd, pos, db))
+ rc= HA_ERR_INTERNAL_ERROR; // ???
+ else
+ if (IsFileType(GetRealType(pos)) && !pos->filename)
+ ok= true;
+
+ } // endif pos
+
+ } else // Avoid infamous DBUG_ASSERT
+ thd->get_stmt_da()->reset_diagnostics_area();
+
+ free_table_share(share);
+ } else // Temporary file
+ ok= true;
+
+ if (ok) {
+ // Let the base handler do the job
+ if (to)
+ rc= handler::rename_table(name, to);
+ else if ((rc= handler::delete_table(name)) == ENOENT)
+ rc= 0; // No files is not an error for CONNECT
+
+ } // endif ok
+
+ DBUG_RETURN(rc);
+} // end of delete_or_rename_table
+
+int ha_connect::delete_table(const char *name)
+{
+ return delete_or_rename_table(name, NULL);
+} // end of delete_table
+
+int ha_connect::rename_table(const char *from, const char *to)
+{
+ return delete_or_rename_table(from, to);
+} // end of rename_table
+
+/**
+ @brief
+ Given a starting key and an ending key, estimate the number of rows that
+ will exist between the two keys.
+
+ @details
+ end_key may be empty, in which case determine if start_key matches any rows.
+
+ Called from opt_range.cc by check_quick_keys().
+
+ @see
+ check_quick_keys() in opt_range.cc
+*/
+ha_rows ha_connect::records_in_range(uint inx, key_range *min_key,
+ key_range *max_key)
+{
+ ha_rows rows;
+ DBUG_ENTER("ha_connect::records_in_range");
+
+ if (indexing < 0 || inx != active_index)
+ index_init(inx, false);
+
+ if (xtrace)
+ htrc("records_in_range: inx=%d indexing=%d\n", inx, indexing);
+
+ if (indexing > 0) {
+ int nval;
+ uint len[2];
+ const uchar *key[2];
+ bool incl[2];
+ key_part_map kmap[2];
+
+ key[0]= (min_key) ? min_key->key : NULL;
+ key[1]= (max_key) ? max_key->key : NULL;
+ len[0]= (min_key) ? min_key->length : 0;
+ len[1]= (max_key) ? max_key->length : 0;
+ incl[0]= (min_key) ? (min_key->flag == HA_READ_KEY_EXACT) : false;
+ incl[1]= (max_key) ? (max_key->flag == HA_READ_AFTER_KEY) : false;
+ kmap[0]= (min_key) ? min_key->keypart_map : 0;
+ kmap[1]= (max_key) ? max_key->keypart_map : 0;
+
+ if ((nval= CntIndexRange(xp->g, tdbp, key, len, incl, kmap)) < 0)
+ rows= HA_POS_ERROR;
+ else
+ rows= (ha_rows)nval;
+
+ } else if (indexing < 0)
+ rows= HA_POS_ERROR;
+ else
+ rows= 100000000; // Don't use missing index
+
+ DBUG_RETURN(rows);
+} // end of records_in_range
+
+/**
+ Convert an ISO-8859-1 column name to UTF-8
+*/
+static char *encode(PGLOBAL g, char *cnm)
+ {
+ char *buf= (char*)PlugSubAlloc(g, NULL, strlen(cnm) * 3);
+ uint dummy_errors;
+ uint32 len= copy_and_convert(buf, strlen(cnm) * 3,
+ &my_charset_utf8_general_ci,
+ cnm, strlen(cnm),
+ &my_charset_latin1,
+ &dummy_errors);
+ buf[len]= '\0';
+ return buf;
+ } // end of Encode
+
+/**
+ Store field definition for create.
+
+ @return
+ Return 0 if ok
+*/
+#if defined(NEW_WAY)
+static bool add_fields(PGLOBAL g,
+ THD *thd,
+ Alter_info *alter_info,
+ char *name,
+ int typ, int len, int dec,
+ uint type_modifier,
+ char *rem,
+// CHARSET_INFO *cs,
+// void *vcolinfo,
+// engine_option_value *create_options,
+ int flg,
+ bool dbf,
+ char v)
+{
+ register Create_field *new_field;
+ char *length, *decimals= NULL;
+ enum_field_types type;
+//Virtual_column_info *vcol_info= (Virtual_column_info *)vcolinfo;
+ engine_option_value *crop;
+ LEX_STRING *comment;
+ LEX_STRING *field_name;
+
+ DBUG_ENTER("ha_connect::add_fields");
+
+ if (len) {
+ if (!v && typ == TYPE_STRING && len > 255)
+ v= 'V'; // Change CHAR to VARCHAR
+
+ length= (char*)PlugSubAlloc(g, NULL, 8);
+ sprintf(length, "%d", len);
+
+ if (typ == TYPE_DOUBLE) {
+ decimals= (char*)PlugSubAlloc(g, NULL, 8);
+ sprintf(decimals, "%d", min(dec, (min(len, 31) - 1)));
+ } // endif dec
+
+ } else
+ length= NULL;
+
+ if (!rem)
+ rem= "";
+
+ type= PLGtoMYSQL(typ, dbf, v);
+ comment= thd->make_lex_string(rem, strlen(rem));
+ field_name= thd->make_lex_string(name, strlen(name));
+
+ switch (v) {
+ case 'Z': type_modifier|= ZEROFILL_FLAG;
+ case 'U': type_modifier|= UNSIGNED_FLAG; break;
+ } // endswitch v
+
+ if (flg) {
+ engine_option_value *start= NULL, *end= NULL;
+ LEX_STRING *flag= thd->make_lex_string("flag", 4);
+
+ crop= new(thd->mem_root) engine_option_value(*flag, (ulonglong)flg,
+ &start, &end, thd->mem_root);
+ } else
+ crop= NULL;
+
+ if (check_string_char_length(field_name, "", NAME_CHAR_LEN,
+ system_charset_info, 1)) {
+ my_error(ER_TOO_LONG_IDENT, MYF(0), field_name->str); /* purecov: inspected */
+ DBUG_RETURN(1); /* purecov: inspected */
+ } // endif field_name
+
+ if (!(new_field= new Create_field()) ||
+ new_field->init(thd, field_name->str, type, length, decimals,
+ type_modifier, NULL, NULL, comment, NULL,
+ NULL, NULL, 0, NULL, crop, true))
+ DBUG_RETURN(1);
+
+ alter_info->create_list.push_back(new_field);
+ DBUG_RETURN(0);
+} // end of add_fields
+#else // !NEW_WAY
+static bool add_field(String *sql, const char *field_name, int typ,
+ int len, int dec, uint tm, const char *rem,
+ char *dft, char *xtra, int flag, bool dbf, char v)
+{
+ char var = (len > 255) ? 'V' : v;
+ bool error= false;
+ const char *type= PLGtoMYSQLtype(typ, dbf, var);
+
+ error|= sql->append('`');
+ error|= sql->append(field_name);
+ error|= sql->append("` ");
+ error|= sql->append(type);
+
+ if (len && typ != TYPE_DATE) {
+ error|= sql->append('(');
+ error|= sql->append_ulonglong(len);
+
+ if (!strcmp(type, "DOUBLE")) {
+ error|= sql->append(',');
+ // dec must be < len and < 31
+ error|= sql->append_ulonglong(min(dec, (min(len, 31) - 1)));
+ } else if (dec > 0 && !strcmp(type, "DECIMAL")) {
+ error|= sql->append(',');
+ // dec must be < len
+ error|= sql->append_ulonglong(min(dec, len - 1));
+ } // endif dec
+
+ error|= sql->append(')');
+ } // endif len
+
+ if (v == 'U')
+ error|= sql->append(" UNSIGNED");
+ else if (v == 'Z')
+ error|= sql->append(" ZEROFILL");
+
+ if (tm)
+ error|= sql->append(STRING_WITH_LEN(" NOT NULL"), system_charset_info);
+
+ if (dft && *dft) {
+ error|= sql->append(" DEFAULT ");
+
+ if (!IsTypeNum(typ)) {
+ error|= sql->append("'");
+ error|= sql->append_for_single_quote(dft, strlen(dft));
+ error|= sql->append("'");
+ } else
+ error|= sql->append(dft);
+
+ } // endif dft
+
+ if (xtra && *xtra) {
+ error|= sql->append(" ");
+ error|= sql->append(xtra);
+ } // endif rem
+
+ if (rem && *rem) {
+ error|= sql->append(" COMMENT '");
+ error|= sql->append_for_single_quote(rem, strlen(rem));
+ error|= sql->append("'");
+ } // endif rem
+
+ if (flag) {
+ error|= sql->append(" FLAG=");
+ error|= sql->append_ulonglong(flag);
+ } // endif flag
+
+ error|= sql->append(',');
+ return error;
+} // end of add_field
+#endif // !NEW_WAY
+
+/**
+ Initialise the table share with the new columns.
+
+ @return
+ Return 0 if ok
+*/
+#if defined(NEW_WAY)
+//static bool sql_unusable_for_discovery(THD *thd, const char *sql);
+
+static int init_table_share(THD *thd,
+ TABLE_SHARE *table_s,
+ HA_CREATE_INFO *create_info,
+ Alter_info *alter_info)
+{
+ KEY *not_used_1;
+ uint not_used_2;
+ int rc= 0;
+ handler *file;
+ LEX_CUSTRING frm= {0,0};
+
+ DBUG_ENTER("init_table_share");
+
+#if 0
+ ulonglong saved_mode= thd->variables.sql_mode;
+ CHARSET_INFO *old_cs= thd->variables.character_set_client;
+ Parser_state parser_state;
+ char *sql_copy;
+ LEX *old_lex;
+ Query_arena *arena, backup;
+ LEX tmp_lex;
+
+ /*
+ Ouch. Parser may *change* the string it's working on.
+ Currently (2013-02-26) it is used to permanently disable
+ conditional comments.
+ Anyway, let's copy the caller's string...
+ */
+ if (!(sql_copy= thd->strmake(sql, sql_length)))
+ DBUG_RETURN(HA_ERR_OUT_OF_MEM);
+
+ if (parser_state.init(thd, sql_copy, sql_length))
+ DBUG_RETURN(HA_ERR_OUT_OF_MEM);
+
+ thd->variables.sql_mode= MODE_NO_ENGINE_SUBSTITUTION | MODE_NO_DIR_IN_CREATE;
+ thd->variables.character_set_client= system_charset_info;
+ old_lex= thd->lex;
+ thd->lex= &tmp_lex;
+
+ arena= thd->stmt_arena;
+
+ if (arena->is_conventional())
+ arena= 0;
+ else
+ thd->set_n_backup_active_arena(arena, &backup);
+
+ lex_start(thd);
+
+ if ((error= parse_sql(thd, & parser_state, NULL)))
+ goto ret;
+
+ if (table_s->sql_unusable_for_discovery(thd, NULL)) {
+ my_error(ER_SQL_DISCOVER_ERROR, MYF(0), plugin_name(db_plugin)->str,
+ db.str, table_name.str, sql_copy);
+ goto ret;
+ } // endif unusable
+
+ thd->lex->create_info.db_type= plugin_data(db_plugin, handlerton *);
+
+ if (tabledef_version.str)
+ thd->lex->create_info.tabledef_version= tabledef_version;
+#endif // 0
+
+ tmp_disable_binlog(thd);
+
+ file= mysql_create_frm_image(thd, table_s->db.str, table_s->table_name.str,
+ create_info, alter_info, C_ORDINARY_CREATE,
+ &not_used_1, &not_used_2, &frm);
+ if (file)
+ delete file;
+ else
+ rc= OPEN_FRM_CORRUPTED;
+
+ if (!rc && frm.str) {
+ table_s->option_list= 0; // cleanup existing options ...
+ table_s->option_struct= 0; // ... if it's an assisted discovery
+ rc= table_s->init_from_binary_frm_image(thd, true, frm.str, frm.length);
+ } // endif frm
+
+//ret:
+ my_free(const_cast<uchar*>(frm.str));
+ reenable_binlog(thd);
+#if 0
+ lex_end(thd->lex);
+ thd->lex= old_lex;
+ if (arena)
+ thd->restore_active_arena(arena, &backup);
+ thd->variables.sql_mode= saved_mode;
+ thd->variables.character_set_client= old_cs;
+#endif // 0
+
+ if (thd->is_error() || rc) {
+ thd->clear_error();
+ my_error(ER_NO_SUCH_TABLE, MYF(0), table_s->db.str,
+ table_s->table_name.str);
+ DBUG_RETURN(HA_ERR_NOT_A_TABLE);
+ } else
+ DBUG_RETURN(0);
+
+} // end of init_table_share
+#else // !NEW_WAY
+static int init_table_share(THD* thd,
+ TABLE_SHARE *table_s,
+ HA_CREATE_INFO *create_info,
+// char *dsn,
+ String *sql)
+{
+ bool oom= false;
+ PTOS topt= table_s->option_struct;
+
+ sql->length(sql->length()-1); // remove the trailing comma
+ sql->append(')');
+
+ for (ha_create_table_option *opt= connect_table_option_list;
+ opt->name; opt++) {
+ ulonglong vull;
+ const char *vstr;
+
+ switch (opt->type) {
+ case HA_OPTION_TYPE_ULL:
+ vull= *(ulonglong*)(((char*)topt) + opt->offset);
+
+ if (vull != opt->def_value) {
+ oom|= sql->append(' ');
+ oom|= sql->append(opt->name);
+ oom|= sql->append('=');
+ oom|= sql->append_ulonglong(vull);
+ } // endif vull
+
+ break;
+ case HA_OPTION_TYPE_STRING:
+ vstr= *(char**)(((char*)topt) + opt->offset);
+
+ if (vstr) {
+ oom|= sql->append(' ');
+ oom|= sql->append(opt->name);
+ oom|= sql->append("='");
+ oom|= sql->append_for_single_quote(vstr, strlen(vstr));
+ oom|= sql->append('\'');
+ } // endif vstr
+
+ break;
+ case HA_OPTION_TYPE_BOOL:
+ vull= *(bool*)(((char*)topt) + opt->offset);
+
+ if (vull != opt->def_value) {
+ oom|= sql->append(' ');
+ oom|= sql->append(opt->name);
+ oom|= sql->append('=');
+ oom|= sql->append(vull ? "ON" : "OFF");
+ } // endif vull
+
+ break;
+ default: // no enums here, good :)
+ break;
+ } // endswitch type
+
+ if (oom)
+ return HA_ERR_OUT_OF_MEM;
+
+ } // endfor opt
+
+ if (create_info->connect_string.length) {
+//if (dsn) {
+ oom|= sql->append(' ');
+ oom|= sql->append("CONNECTION='");
+ oom|= sql->append_for_single_quote(create_info->connect_string.str,
+ create_info->connect_string.length);
+// oom|= sql->append_for_single_quote(dsn, strlen(dsn));
+ oom|= sql->append('\'');
+
+ if (oom)
+ return HA_ERR_OUT_OF_MEM;
+
+ } // endif string
+
+ if (create_info->default_table_charset) {
+ oom|= sql->append(' ');
+ oom|= sql->append("CHARSET=");
+ oom|= sql->append(create_info->default_table_charset->csname);
+
+ if (oom)
+ return HA_ERR_OUT_OF_MEM;
+
+ } // endif charset
+
+ if (xtrace)
+ htrc("s_init: %.*s\n", sql->length(), sql->ptr());
+
+ return table_s->init_from_sql_statement_string(thd, true,
+ sql->ptr(), sql->length());
+} // end of init_table_share
+#endif // !NEW_WAY
+
+// Add an option to the create_info option list
+static void add_option(THD* thd, HA_CREATE_INFO *create_info,
+ const char *opname, const char *opval)
+{
+#if defined(NEW_WAY)
+ LEX_STRING *opn= thd->make_lex_string(opname, strlen(opname));
+ LEX_STRING *val= thd->make_lex_string(opval, strlen(opval));
+ engine_option_value *pov, **start= &create_info->option_list, *end= NULL;
+
+ for (pov= *start; pov; pov= pov->next)
+ end= pov;
+
+ pov= new(thd->mem_root) engine_option_value(*opn, *val, false, start, &end);
+#endif // NEW_WAY
+} // end of add_option
+
+// Used to check whether a MYSQL table is created on itself
+static bool CheckSelf(PGLOBAL g, TABLE_SHARE *s, const char *host,
+ const char *db, char *tab, const char *src, int port)
+{
+ if (src)
+ return false;
+ else if (host && stricmp(host, "localhost") && strcmp(host, "127.0.0.1"))
+ return false;
+ else if (db && stricmp(db, s->db.str))
+ return false;
+ else if (tab && stricmp(tab, s->table_name.str))
+ return false;
+ else if (port && port != (signed)GetDefaultPort())
+ return false;
+
+ strcpy(g->Message, "This MySQL table is defined on itself");
+ return true;
+} // end of CheckSelf
+
+/**
+ @brief
+ connect_assisted_discovery() is called when creating a table with no columns.
+
+ @details
+ When assisted discovery is used the .frm file have not already been
+ created. You can overwrite some definitions at this point but the
+ main purpose of it is to define the columns for some table types.
+
+ @note
+ this function is no more called in case of CREATE .. SELECT
+*/
+static int connect_assisted_discovery(handlerton *hton, THD* thd,
+ TABLE_SHARE *table_s,
+ HA_CREATE_INFO *create_info)
+{
+ char v, spc= ',', qch= 0;
+ const char *fncn= "?";
+ const char *user, *fn, *db, *host, *pwd, *sep, *tbl, *src;
+ const char *col, *ocl, *rnk, *pic, *fcl;
+ char *tab, *dsn, *shm;
+#if defined(WIN32)
+ char *nsp= NULL, *cls= NULL;
+#endif // WIN32
+ int port= 0, hdr= 0, mxr __attribute__((unused))= 0, mxe= 0, rc= 0;
+ int cop __attribute__((unused)) = 0;
+ uint tm, fnc= FNC_NO, supfnc= (FNC_NO | FNC_COL);
+ bool bif, ok= false, dbf= false;
+ TABTYPE ttp= TAB_UNDEF;
+ PQRYRES qrp= NULL;
+ PCOLRES crp;
+ PCONNECT xp= NULL;
+ PGLOBAL g= GetPlug(thd, xp);
+ PTOS topt= table_s->option_struct;
+#if defined(NEW_WAY)
+//CHARSET_INFO *cs;
+ Alter_info alter_info;
+#else // !NEW_WAY
+ char buf[1024];
+ String sql(buf, sizeof(buf), system_charset_info);
+
+ sql.copy(STRING_WITH_LEN("CREATE TABLE whatever ("), system_charset_info);
+#endif // !NEW_WAY
+
+ if (!g)
+ return HA_ERR_INTERNAL_ERROR;
+
+ user= host= pwd= tbl= src= col= ocl= pic= fcl= rnk= dsn= NULL;
+
+ // Get the useful create options
+ ttp= GetTypeID(topt->type);
+ fn= topt->filename;
+ tab= (char*)topt->tabname;
+ src= topt->srcdef;
+ db= topt->dbname;
+ fncn= topt->catfunc;
+ fnc= GetFuncID(fncn);
+ sep= topt->separator;
+ spc= (!sep || !strcmp(sep, "\\t")) ? '\t' : *sep;
+ qch= topt->qchar ? *topt->qchar : (signed)topt->quoted >= 0 ? '"' : 0;
+ hdr= (int)topt->header;
+ tbl= topt->tablist;
+ col= topt->colist;
+
+ if (topt->oplist) {
+ host= GetListOption(g, "host", topt->oplist, "localhost");
+ user= GetListOption(g, "user", topt->oplist, "root");
+ // Default value db can come from the DBNAME=xxx option.
+ db= GetListOption(g, "database", topt->oplist, db);
+ col= GetListOption(g, "colist", topt->oplist, col);
+ ocl= GetListOption(g, "occurcol", topt->oplist, NULL);
+ pic= GetListOption(g, "pivotcol", topt->oplist, NULL);
+ fcl= GetListOption(g, "fnccol", topt->oplist, NULL);
+ rnk= GetListOption(g, "rankcol", topt->oplist, NULL);
+ pwd= GetListOption(g, "password", topt->oplist);
+#if defined(WIN32)
+ nsp= GetListOption(g, "namespace", topt->oplist);
+ cls= GetListOption(g, "class", topt->oplist);
+#endif // WIN32
+ port= atoi(GetListOption(g, "port", topt->oplist, "0"));
+ mxr= atoi(GetListOption(g,"maxres", topt->oplist, "0"));
+ mxe= atoi(GetListOption(g,"maxerr", topt->oplist, "0"));
+#if defined(PROMPT_OK)
+ cop= atoi(GetListOption(g, "checkdsn", topt->oplist, "0"));
+#endif // PROMPT_OK
+ } else {
+ host= "localhost";
+ user= "root";
+ } // endif option_list
+
+ if (!(shm= (char*)db))
+ db= table_s->db.str; // Default value
+
+ // Check table type
+ if (ttp == TAB_UNDEF) {
+ topt->type= (src) ? "MYSQL" : (tab) ? "PROXY" : "DOS";
+ ttp= GetTypeID(topt->type);
+ sprintf(g->Message, "No table_type. Was set to %s", topt->type);
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ add_option(thd, create_info, "table_type", topt->type);
+ } else if (ttp == TAB_NIY) {
+ sprintf(g->Message, "Unsupported table type %s", topt->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } // endif ttp
+
+ if (!tab) {
+ if (ttp == TAB_TBL) {
+ // Make tab the first table of the list
+ char *p;
+
+ if (!tbl) {
+ strcpy(g->Message, "Missing table list");
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } // endif tbl
+
+ tab= (char*)PlugSubAlloc(g, NULL, strlen(tbl) + 1);
+ strcpy(tab, tbl);
+
+ if ((p= strchr(tab, ',')))
+ *p= 0;
+
+ if ((p=strchr(tab, '.'))) {
+ *p= 0;
+ db= tab;
+ tab= p + 1;
+ } // endif p
+
+ } else if (ttp != TAB_ODBC || !(fnc & (FNC_TABLE | FNC_COL)))
+ tab= table_s->table_name.str; // Default value
+
+#if defined(NEW_WAY)
+// add_option(thd, create_info, "tabname", tab);
+#endif // NEW_WAY
+ } // endif tab
+
+ switch (ttp) {
+#if defined(ODBC_SUPPORT)
+ case TAB_ODBC:
+ dsn= create_info->connect_string.str;
+
+ if (fnc & (FNC_DSN | FNC_DRIVER)) {
+ ok= true;
+#if defined(PROMPT_OK)
+ } else if (!stricmp(thd->main_security_ctx.host, "localhost")
+ && cop == 1) {
+ if ((dsn = ODBCCheckConnection(g, dsn, cop)) != NULL) {
+ thd->make_lex_string(&create_info->connect_string, dsn, strlen(dsn));
+ ok= true;
+ } // endif dsn
+#endif // PROMPT_OK
+
+ } else if (!dsn)
+ sprintf(g->Message, "Missing %s connection string", topt->type);
+ else
+ ok= true;
+
+ supfnc |= (FNC_TABLE | FNC_DSN | FNC_DRIVER);
+ break;
+#endif // ODBC_SUPPORT
+ case TAB_DBF:
+ dbf= true;
+ // Passthru
+ case TAB_CSV:
+ if (!fn && fnc != FNC_NO)
+ sprintf(g->Message, "Missing %s file name", topt->type);
+ else
+ ok= true;
+
+ break;
+#if defined(MYSQL_SUPPORT)
+ case TAB_MYSQL:
+ ok= true;
+
+ if (create_info->connect_string.str) {
+ int len= create_info->connect_string.length;
+ PMYDEF mydef= new(g) MYSQLDEF();
+ PDBUSER dup= PlgGetUser(g);
+ PCATLG cat= (dup) ? dup->Catalog : NULL;
+
+ dsn= (char*)PlugSubAlloc(g, NULL, len + 1);
+ strncpy(dsn, create_info->connect_string.str, len);
+ dsn[len]= 0;
+ mydef->SetName(create_info->alias);
+ mydef->SetCat(cat);
+
+ if (!mydef->ParseURL(g, dsn, false)) {
+ if (mydef->GetHostname())
+ host= mydef->GetHostname();
+
+ if (mydef->GetUsername())
+ user= mydef->GetUsername();
+
+ if (mydef->GetPassword())
+ pwd= mydef->GetPassword();
+
+ if (mydef->GetDatabase())
+ db= mydef->GetDatabase();
+
+ if (mydef->GetTabname())
+ tab= mydef->GetTabname();
+
+ if (mydef->GetPortnumber())
+ port= mydef->GetPortnumber();
+
+ } else
+ ok= false;
+
+ } else if (!user)
+ user= "root";
+
+ if (CheckSelf(g, table_s, host, db, tab, src, port))
+ ok= false;
+
+ break;
+#endif // MYSQL_SUPPORT
+#if defined(WIN32)
+ case TAB_WMI:
+ ok= true;
+ break;
+#endif // WIN32
+ case TAB_PIVOT:
+ supfnc= FNC_NO;
+ case TAB_PRX:
+ case TAB_TBL:
+ case TAB_XCL:
+ case TAB_OCCUR:
+ if (!src && !stricmp(tab, create_info->alias) &&
+ (!db || !stricmp(db, table_s->db.str)))
+ sprintf(g->Message, "A %s table cannot refer to itself", topt->type);
+ else
+ ok= true;
+
+ break;
+ case TAB_OEM:
+ if (topt->module && topt->subtype)
+ ok= true;
+ else
+ strcpy(g->Message, "Missing OEM module or subtype");
+
+ break;
+ default:
+ sprintf(g->Message, "Cannot get column info for table type %s", topt->type);
+ break;
+ } // endif ttp
+
+ // Check for supported catalog function
+ if (ok && !(supfnc & fnc)) {
+ sprintf(g->Message, "Unsupported catalog function %s for table type %s",
+ fncn, topt->type);
+ ok= false;
+ } // endif supfnc
+
+ if (src && fnc != FNC_NO) {
+ strcpy(g->Message, "Cannot make catalog table from srcdef");
+ ok= false;
+ } // endif src
+
+ if (ok) {
+ char *cnm, *rem, *dft, *xtra;
+ int i, len, prec, dec, typ, flg;
+ PDBUSER dup= PlgGetUser(g);
+ PCATLG cat= (dup) ? dup->Catalog : NULL;
+
+ if (cat)
+ cat->SetDataPath(g, table_s->db.str);
+ else
+ return HA_ERR_INTERNAL_ERROR; // Should never happen
+
+ if (src && ttp != TAB_PIVOT && ttp != TAB_ODBC) {
+ qrp= SrcColumns(g, host, db, user, pwd, src, port);
+
+ if (qrp && ttp == TAB_OCCUR)
+ if (OcrSrcCols(g, qrp, col, ocl, rnk)) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } // endif OcrSrcCols
+
+ } else switch (ttp) {
+ case TAB_DBF:
+ qrp= DBFColumns(g, fn, fnc == FNC_COL);
+ break;
+#if defined(ODBC_SUPPORT)
+ case TAB_ODBC:
+ switch (fnc) {
+ case FNC_NO:
+ case FNC_COL:
+ if (src) {
+ qrp= ODBCSrcCols(g, dsn, (char*)src);
+ src= NULL; // for next tests
+ } else
+ qrp= ODBCColumns(g, dsn, shm, tab, NULL, mxr, fnc == FNC_COL);
+
+ break;
+ case FNC_TABLE:
+ qrp= ODBCTables(g, dsn, shm, tab, mxr, true);
+ break;
+ case FNC_DSN:
+ qrp= ODBCDataSources(g, mxr, true);
+ break;
+ case FNC_DRIVER:
+ qrp= ODBCDrivers(g, mxr, true);
+ break;
+ default:
+ sprintf(g->Message, "invalid catfunc %s", fncn);
+ break;
+ } // endswitch info
+
+ break;
+#endif // ODBC_SUPPORT
+#if defined(MYSQL_SUPPORT)
+ case TAB_MYSQL:
+ qrp= MyColumns(g, host, db, user, pwd, tab,
+ NULL, port, fnc == FNC_COL);
+ break;
+#endif // MYSQL_SUPPORT
+ case TAB_CSV:
+ qrp= CSVColumns(g, fn, spc, qch, hdr, mxe, fnc == FNC_COL);
+ break;
+#if defined(WIN32)
+ case TAB_WMI:
+ qrp= WMIColumns(g, nsp, cls, fnc == FNC_COL);
+ break;
+#endif // WIN32
+ case TAB_PRX:
+ case TAB_TBL:
+ case TAB_XCL:
+ case TAB_OCCUR:
+ bif= fnc == FNC_COL;
+ qrp= TabColumns(g, thd, db, tab, bif);
+
+ if (!qrp && bif && fnc != FNC_COL) // tab is a view
+ qrp= MyColumns(g, host, db, user, pwd, tab, NULL, port, false);
+
+ if (qrp && ttp == TAB_OCCUR && fnc != FNC_COL)
+ if (OcrColumns(g, qrp, col, ocl, rnk)) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } // endif OcrColumns
+
+ break;
+ case TAB_PIVOT:
+ qrp= PivotColumns(g, tab, src, pic, fcl, host, db, user, pwd, port);
+ break;
+ case TAB_OEM:
+ qrp= OEMColumns(g, topt, tab, (char*)db, fnc == FNC_COL);
+ break;
+ default:
+ strcpy(g->Message, "System error during assisted discovery");
+ break;
+ } // endswitch ttp
+
+ if (!qrp) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } // endif qrp
+
+ if (fnc != FNC_NO || src || ttp == TAB_PIVOT) {
+ // Catalog like table
+ for (crp= qrp->Colresp; !rc && crp; crp= crp->Next) {
+ cnm= encode(g, crp->Name);
+ typ= crp->Type;
+ len= crp->Length;
+ dec= crp->Prec;
+ flg= crp->Flag;
+ v= crp->Var;
+
+ if (!len && typ == TYPE_STRING)
+ len= 256; // STRBLK's have 0 length
+
+#if defined(NEW_WAY)
+ // Now add the field
+ rc= add_fields(g, thd, &alter_info, cnm, typ, len, dec,
+ NOT_NULL_FLAG, "", flg, dbf, v);
+#else // !NEW_WAY
+ // Now add the field
+ if (add_field(&sql, cnm, typ, len, dec, NOT_NULL_FLAG,
+ NULL, NULL, NULL, flg, dbf, v))
+ rc= HA_ERR_OUT_OF_MEM;
+#endif // !NEW_WAY
+ } // endfor crp
+
+ } else // Not a catalog table
+ for (i= 0; !rc && i < qrp->Nblin; i++) {
+ typ= len= prec= dec= 0;
+ tm= NOT_NULL_FLAG;
+ cnm= (char*)"noname";
+ dft= xtra= NULL;
+#if defined(NEW_WAY)
+ rem= "";
+// cs= NULL;
+#else // !NEW_WAY
+ rem= NULL;
+#endif // !NEW_WAY
+
+ for (crp= qrp->Colresp; crp; crp= crp->Next)
+ switch (crp->Fld) {
+ case FLD_NAME:
+ cnm= encode(g, crp->Kdata->GetCharValue(i));
+ break;
+ case FLD_TYPE:
+ typ= crp->Kdata->GetIntValue(i);
+ v = (crp->Nulls) ? crp->Nulls[i] : 0;
+ break;
+ case FLD_PREC:
+ // PREC must be always before LENGTH
+ len= prec= crp->Kdata->GetIntValue(i);
+ break;
+ case FLD_LENGTH:
+ len= crp->Kdata->GetIntValue(i);
+ break;
+ case FLD_SCALE:
+ dec= crp->Kdata->GetIntValue(i);
+ break;
+ case FLD_NULL:
+ if (crp->Kdata->GetIntValue(i))
+ tm= 0; // Nullable
+
+ break;
+ case FLD_REM:
+ rem= crp->Kdata->GetCharValue(i);
+ break;
+// case FLD_CHARSET:
+ // No good because remote table is already translated
+// if (*(csn= crp->Kdata->GetCharValue(i)))
+// cs= get_charset_by_name(csn, 0);
+
+// break;
+ case FLD_DEFAULT:
+ dft= crp->Kdata->GetCharValue(i);
+ break;
+ case FLD_EXTRA:
+ xtra= crp->Kdata->GetCharValue(i);
+ break;
+ default:
+ break; // Ignore
+ } // endswitch Fld
+
+#if defined(ODBC_SUPPORT)
+ if (ttp == TAB_ODBC) {
+ int plgtyp;
+
+ // typ must be PLG type, not SQL type
+ if (!(plgtyp= TranslateSQLType(typ, dec, prec, v))) {
+ sprintf(g->Message, "Unsupported SQL type %d", typ);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+ } else
+ typ= plgtyp;
+
+ switch (typ) {
+ case TYPE_DOUBLE:
+ // Some data sources do not count dec in length (prec)
+ prec += (dec + 2); // To be safe
+ case TYPE_DECIM:
+ break;
+ default:
+ dec= 0;
+ } // endswitch typ
+
+ } // endif ttp
+#endif // ODBC_SUPPORT
+
+ // Make the arguments as required by add_fields
+ if (typ == TYPE_DATE)
+ prec= 0;
+ else if (typ == TYPE_DOUBLE)
+ prec= len;
+
+ // Now add the field
+#if defined(NEW_WAY)
+ rc= add_fields(g, thd, &alter_info, cnm, typ, prec, dec,
+ tm, rem, 0, dbf, v);
+#else // !NEW_WAY
+ if (add_field(&sql, cnm, typ, prec, dec, tm, rem, dft, xtra,
+ 0, dbf, v))
+ rc= HA_ERR_OUT_OF_MEM;
+#endif // !NEW_WAY
+ } // endfor i
+
+#if defined(NEW_WAY)
+ rc= init_table_share(thd, table_s, create_info, &alter_info);
+#else // !NEW_WAY
+ if (!rc)
+ rc= init_table_share(thd, table_s, create_info, &sql);
+// rc= init_table_share(thd, table_s, create_info, dsn, &sql);
+#endif // !NEW_WAY
+
+ return rc;
+ } // endif ok
+
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ return HA_ERR_INTERNAL_ERROR;
+} // end of connect_assisted_discovery
+
+/**
+ Get the database name from a qualified table name.
+*/
+char *ha_connect::GetDBfromName(const char *name)
+{
+ char *db, dbname[128], tbname[128];
+
+ if (filename_to_dbname_and_tablename(name, dbname, sizeof(dbname),
+ tbname, sizeof(tbname)))
+ *dbname= 0;
+
+ if (*dbname) {
+ assert(xp && xp->g);
+ db= (char*)PlugSubAlloc(xp->g, NULL, strlen(dbname + 1));
+ strcpy(db, dbname);
+ } else
+ db= NULL;
+
+ return db;
+} // end of GetDBfromName
+
+
+/**
+ @brief
+ create() is called to create a database. The variable name will have the name
+ of the table.
+
+ @details
+ When create() is called you do not need to worry about
+ opening the table. Also, the .frm file will have already been
+ created so adjusting create_info is not necessary. You can overwrite
+ the .frm file at this point if you wish to change the table
+ definition, but there are no methods currently provided for doing
+ so.
+
+ Called from handle.cc by ha_create_table().
+
+ @note
+ Currently we do some checking on the create definitions and stop
+ creating if an error is found. We wish we could change the table
+ definition such as providing a default table type. However, as said
+ above, there are no method to do so.
+
+ @see
+ ha_create_table() in handle.cc
+*/
+
+int ha_connect::create(const char *name, TABLE *table_arg,
+ HA_CREATE_INFO *create_info)
+{
+ int rc= RC_OK;
+ bool dbf;
+ Field* *field;
+ Field *fp;
+ TABTYPE type;
+ TABLE *st= table; // Probably unuseful
+ THD *thd= ha_thd();
+ xp= GetUser(thd, xp);
+ PGLOBAL g= xp->g;
+
+ DBUG_ENTER("ha_connect::create");
+ int sqlcom= thd_sql_command(table_arg->in_use);
+ PTOS options= GetTableOptionStruct(table_arg);
+
+ table= table_arg; // Used by called functions
+
+ if (xtrace)
+ htrc("create: this=%p thd=%p xp=%p g=%p sqlcom=%d name=%s\n",
+ this, thd, xp, g, sqlcom, GetTableName());
+
+ // CONNECT engine specific table options:
+ DBUG_ASSERT(options);
+ type= GetTypeID(options->type);
+
+ // Check table type
+ if (type == TAB_UNDEF) {
+ options->type= (options->srcdef) ? "MYSQL" :
+ (options->tabname) ? "PROXY" : "DOS";
+ type= GetTypeID(options->type);
+ sprintf(g->Message, "No table_type. Will be set to %s", options->type);
+
+ if (sqlcom == SQLCOM_CREATE_TABLE)
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+
+ } else if (type == TAB_NIY) {
+ sprintf(g->Message, "Unsupported table type %s", options->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif ttp
+
+ if (check_privileges(thd, options, GetDBfromName(name)))
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+
+ if (options->data_charset) {
+ const CHARSET_INFO *data_charset;
+
+ if (!(data_charset= get_charset_by_csname(options->data_charset,
+ MY_CS_PRIMARY, MYF(0)))) {
+ my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), options->data_charset);
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif charset
+
+ if (type == TAB_XML && data_charset != &my_charset_utf8_general_ci) {
+ my_printf_error(ER_UNKNOWN_ERROR,
+ "DATA_CHARSET='%s' is not supported for TABLE_TYPE=XML",
+ MYF(0), options->data_charset);
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif utf8
+
+ } // endif charset
+
+ if (!g) {
+ rc= HA_ERR_INTERNAL_ERROR;
+ DBUG_RETURN(rc);
+ } else
+ dbf= (GetTypeID(options->type) == TAB_DBF && !options->catfunc);
+
+ // Can be null in ALTER TABLE
+ if (create_info->alias)
+ // Check whether a table is defined on itself
+ switch (type) {
+ case TAB_PRX:
+ case TAB_XCL:
+ case TAB_PIVOT:
+ case TAB_OCCUR:
+ if (options->srcdef) {
+ strcpy(g->Message, "Cannot check looping reference");
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ } else if (options->tabname) {
+ if (!stricmp(options->tabname, create_info->alias) &&
+ (!options->dbname || !stricmp(options->dbname, table_arg->s->db.str))) {
+ sprintf(g->Message, "A %s table cannot refer to itself",
+ options->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif tab
+
+ } else {
+ strcpy(g->Message, "Missing object table name or definition");
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif tabname
+
+ case TAB_MYSQL:
+ {const char *src= options->srcdef;
+ char *host, *db, *tab= (char*)options->tabname;
+ int port;
+
+ host= GetListOption(g, "host", options->oplist, NULL);
+ db= GetListOption(g, "database", options->oplist, NULL);
+ port= atoi(GetListOption(g, "port", options->oplist, "0"));
+
+ if (create_info->connect_string.str) {
+ char *dsn;
+ int len= create_info->connect_string.length;
+ PMYDEF mydef= new(g) MYSQLDEF();
+ PDBUSER dup= PlgGetUser(g);
+ PCATLG cat= (dup) ? dup->Catalog : NULL;
+
+ dsn= (char*)PlugSubAlloc(g, NULL, len + 1);
+ strncpy(dsn, create_info->connect_string.str, len);
+ dsn[len]= 0;
+ mydef->SetName(create_info->alias);
+ mydef->SetCat(cat);
+
+ if (!mydef->ParseURL(g, dsn, false)) {
+ if (mydef->GetHostname())
+ host= mydef->GetHostname();
+
+ if (mydef->GetDatabase())
+ db= mydef->GetDatabase();
+
+ if (mydef->GetTabname())
+ tab= mydef->GetTabname();
+
+ if (mydef->GetPortnumber())
+ port= mydef->GetPortnumber();
+
+ } else {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif ParseURL
+
+ } // endif connect_string
+
+ if (CheckSelf(g, table_arg->s, host, db, tab, src, port)) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif CheckSelf
+
+ }break;
+ default: /* do nothing */;
+ break;
+ } // endswitch ttp
+
+ if (type == TAB_XML) {
+ bool dom; // True: MS-DOM, False libxml2
+ char *xsup= GetListOption(g, "Xmlsup", options->oplist, "*");
+
+ // Note that if no support is specified, the default is MS-DOM
+ // on Windows and libxml2 otherwise
+ switch (*xsup) {
+ case '*':
+#if defined(WIN32)
+ dom= true;
+#else // !WIN32
+ dom= false;
+#endif // !WIN32
+ break;
+ case 'M':
+ case 'D':
+ dom= true;
+ break;
+ default:
+ dom= false;
+ break;
+ } // endswitch xsup
+
+#if !defined(DOMDOC_SUPPORT)
+ if (dom) {
+ strcpy(g->Message, "MS-DOM not supported by this version");
+ xsup= NULL;
+ } // endif DomDoc
+#endif // !DOMDOC_SUPPORT
+
+#if !defined(LIBXML2_SUPPORT)
+ if (!dom) {
+ strcpy(g->Message, "libxml2 not supported by this version");
+ xsup= NULL;
+ } // endif Libxml2
+#endif // !LIBXML2_SUPPORT
+
+ if (!xsup) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ DBUG_RETURN(rc);
+ } // endif xsup
+
+ } // endif type
+
+ // Check column types
+ for (field= table_arg->field; *field; field++) {
+ fp= *field;
+
+ if (fp->vcol_info && !fp->stored_in_db)
+ continue; // This is a virtual column
+
+ if (fp->flags & AUTO_INCREMENT_FLAG) {
+ strcpy(g->Message, "Auto_increment is not supported yet");
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ DBUG_RETURN(rc);
+ } // endif flags
+
+ if (fp->flags & (BLOB_FLAG | ENUM_FLAG | SET_FLAG)) {
+ sprintf(g->Message, "Unsupported type for column %s",
+ fp->field_name);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ DBUG_RETURN(rc);
+ } // endif flags
+
+ switch (fp->type()) {
+ case MYSQL_TYPE_SHORT:
+ case MYSQL_TYPE_LONG:
+ case MYSQL_TYPE_FLOAT:
+ case MYSQL_TYPE_DOUBLE:
+ case MYSQL_TYPE_TIMESTAMP:
+ case MYSQL_TYPE_DATE:
+ case MYSQL_TYPE_TIME:
+ case MYSQL_TYPE_DATETIME:
+ case MYSQL_TYPE_YEAR:
+ case MYSQL_TYPE_NEWDATE:
+ case MYSQL_TYPE_LONGLONG:
+ case MYSQL_TYPE_TINY:
+ case MYSQL_TYPE_DECIMAL:
+ case MYSQL_TYPE_NEWDECIMAL:
+ case MYSQL_TYPE_INT24:
+ break; // Ok
+ case MYSQL_TYPE_VARCHAR:
+ case MYSQL_TYPE_VAR_STRING:
+ case MYSQL_TYPE_STRING:
+ if (!fp->field_length) {
+ sprintf(g->Message, "Unsupported 0 length for column %s",
+ fp->field_name);
+ rc= HA_ERR_INTERNAL_ERROR;
+ my_printf_error(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(rc);
+ } // endif fp
+
+ break; // To be checked
+ case MYSQL_TYPE_BIT:
+ case MYSQL_TYPE_NULL:
+ case MYSQL_TYPE_ENUM:
+ case MYSQL_TYPE_SET:
+ case MYSQL_TYPE_TINY_BLOB:
+ case MYSQL_TYPE_MEDIUM_BLOB:
+ case MYSQL_TYPE_LONG_BLOB:
+ case MYSQL_TYPE_BLOB:
+ case MYSQL_TYPE_GEOMETRY:
+ default:
+// fprintf(stderr, "Unsupported type column %s\n", fp->field_name);
+ sprintf(g->Message, "Unsupported type for column %s",
+ fp->field_name);
+ rc= HA_ERR_INTERNAL_ERROR;
+ my_printf_error(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(rc);
+ break;
+ } // endswitch type
+
+ if ((fp)->real_maybe_null() && !IsTypeNullable(type)) {
+ my_printf_error(ER_UNKNOWN_ERROR,
+ "Table type %s does not support nullable columns",
+ MYF(0), options->type);
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+ } // endif !nullable
+
+ if (dbf) {
+ bool b= false;
+
+ if ((b= strlen(fp->field_name) > 10))
+ sprintf(g->Message, "DBF: Column name '%s' is too long (max=10)",
+ fp->field_name);
+ else if ((b= fp->field_length > 255))
+ sprintf(g->Message, "DBF: Column length too big for '%s' (max=255)",
+ fp->field_name);
+
+ if (b) {
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ DBUG_RETURN(rc);
+ } // endif b
+
+ } // endif dbf
+
+ } // endfor field
+
+ if ((sqlcom == SQLCOM_CREATE_TABLE || *GetTableName() == '#')
+ && IsFileType(type) && !options->filename) {
+ // The file name is not specified, create a default file in
+ // the database directory named table_name.table_type.
+ // (temporarily not done for XML because a void file causes
+ // the XML parsers to report an error on the first Insert)
+ char buf[256], fn[_MAX_PATH], dbpath[128], lwt[12];
+ int h;
+
+ strcpy(buf, GetTableName());
+
+ // Check for incompatible options
+ if (options->sepindex) {
+ my_message(ER_UNKNOWN_ERROR,
+ "SEPINDEX is incompatible with unspecified file name",
+ MYF(0));
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+ } else if (GetTypeID(options->type) == TAB_VEC)
+ if (!table->s->max_rows || options->split) {
+ my_printf_error(ER_UNKNOWN_ERROR,
+ "%s tables whose file name is unspecified cannot be split",
+ MYF(0), options->type);
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+ } else if (options->header == 2) {
+ my_printf_error(ER_UNKNOWN_ERROR,
+ "header=2 is not allowed for %s tables whose file name is unspecified",
+ MYF(0), options->type);
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+ } // endif's
+
+ // Fold type to lower case
+ for (int i= 0; i < 12; i++)
+ if (!options->type[i]) {
+ lwt[i]= 0;
+ break;
+ } else
+ lwt[i]= tolower(options->type[i]);
+
+ strcat(strcat(buf, "."), lwt);
+ sprintf(g->Message, "No file name. Table will use %s", buf);
+
+ if (sqlcom == SQLCOM_CREATE_TABLE)
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+
+ strcat(strcat(strcpy(dbpath, "./"), table->s->db.str), "/");
+ PlugSetPath(fn, buf, dbpath);
+
+ if ((h= ::open(fn, O_CREAT | O_EXCL, 0666)) == -1) {
+ if (errno == EEXIST)
+ sprintf(g->Message, "Default file %s already exists", fn);
+ else
+ sprintf(g->Message, "Error %d creating file %s", errno, fn);
+
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0, g->Message);
+ } else
+ ::close(h);
+
+ if (type == TAB_FMT || options->readonly)
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
+ "Congratulation, you just created a read-only void table!");
+
+ } // endif
+
+ if (xtrace)
+ htrc("xchk=%p createas=%d\n", g->Xchk, g->Createas);
+
+ // To check whether indices have to be made or remade
+ if (!g->Xchk) {
+ PIXDEF xdp;
+
+ // We should be in CREATE TABLE or ALTER_TABLE
+ if (sqlcom != SQLCOM_CREATE_TABLE && sqlcom != SQLCOM_ALTER_TABLE)
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
+ "Wrong command in create, please contact CONNECT team");
+
+ if (sqlcom == SQLCOM_ALTER_TABLE && g->Alchecked == 0 &&
+ (!IsFileType(type) || FileExists(options->filename))) {
+ // This is an ALTER to CONNECT from another engine.
+ // It cannot be accepted because the table data would be lost
+ // except when the target file does not exist.
+ strcpy(g->Message, "Operation denied. Table data would be lost.");
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
+ } // endif outward
+
+ // Get the index definitions
+ if (xdp= GetIndexInfo()) {
+ if (IsTypeIndexable(type)) {
+ PDBUSER dup= PlgGetUser(g);
+ PCATLG cat= (dup) ? dup->Catalog : NULL;
+
+ if (cat) {
+ cat->SetDataPath(g, table_arg->s->db.str);
+
+ if ((rc= optimize(table->in_use, NULL))) {
+ htrc("Create rc=%d %s\n", rc, g->Message);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ } else
+ CloseTable(g);
+
+ } // endif cat
+
+ } else {
+ sprintf(g->Message, "Table type %s is not indexable", options->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_UNSUPPORTED;
+ } // endif Indexable
+
+ } // endif xdp
+
+ } else {
+ // This should not happen anymore with indexing new way
+ my_message(ER_UNKNOWN_ERROR,
+ "CONNECT index modification should be in-place", MYF(0));
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+#if 0
+ PIXDEF xdp= GetIndexInfo();
+ PCHK xcp= (PCHK)g->Xchk;
+
+ if (xdp) {
+ if (!IsTypeIndexable(type)) {
+ g->Xchk= NULL;
+ sprintf(g->Message, "Table type %s is not indexable", options->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ rc= HA_ERR_INTERNAL_ERROR;
+ } else {
+ xcp->newpix= xdp;
+ xcp->newsep= GetBooleanOption("Sepindex", false);
+ } // endif Indexable
+
+ } else if (!xcp->oldpix)
+ g->Xchk= NULL;
+
+ if (xtrace && g->Xchk)
+ htrc("oldsep=%d newsep=%d oldpix=%p newpix=%p\n",
+ xcp->oldsep, xcp->newsep, xcp->oldpix, xcp->newpix);
+
+// if (g->Xchk &&
+// (sqlcom != SQLCOM_CREATE_INDEX && sqlcom != SQLCOM_DROP_INDEX)) {
+ if (g->Xchk) {
+ PIXDEF xp1, xp2;
+ bool b= false; // true if index changes
+
+ if (xcp->oldsep == xcp->newsep) {
+ for (xp1= xcp->newpix, xp2= xcp->oldpix;
+ xp1 || xp2;
+ xp1= xp1->Next, xp2= xp2->Next)
+ if (!xp1 || !xp2 || !IsSameIndex(xp1, xp2)) {
+ b= true;
+ break;
+ } // endif xp1
+
+ } else
+ b= true;
+
+ if (!b)
+ g->Xchk= NULL;
+
+#if 0
+ if (b) {
+ // CONNECT does not support indexing via ALTER TABLE
+ my_message(ER_UNKNOWN_ERROR,
+ "CONNECT does not support index modification via ALTER TABLE",
+ MYF(0));
+ DBUG_RETURN(HA_ERR_UNSUPPORTED);
+ } // endif b
+#endif // 0
+
+ } // endif Xchk
+
+#endif // 0
+ } // endif Xchk
+
+ table= st;
+ DBUG_RETURN(rc);
+} // end of create
+
+/**
+ Used to check whether a file based outward table can be populated by
+ an ALTER TABLE command. The conditions are:
+ - file does not exist or is void
+ - user has file privilege
+*/
+bool ha_connect::FileExists(const char *fn)
+{
+ if (!fn || !*fn)
+ return false;
+
+ if (table) {
+ char *s, filename[_MAX_PATH], path[128];
+ int n;
+ struct stat info;
+
+ if (check_access(ha_thd(), FILE_ACL, table->s->db.str,
+ NULL, NULL, 0, 0))
+ return true;
+
+#if defined(WIN32)
+ s= "\\";
+#else // !WIN32
+ s= "/";
+#endif // !WIN32
+
+ strcat(strcat(strcat(strcpy(path, "."), s), table->s->db.str), s);
+ PlugSetPath(filename, fn, path);
+ n= stat(filename, &info);
+
+ if (n < 0) {
+ if (errno != ENOENT) {
+ char buf[_MAX_PATH + 20];
+
+ sprintf(buf, "Error %d for file %s", errno, filename);
+ push_warning(table->in_use, Sql_condition::WARN_LEVEL_WARN, 0, buf);
+ return true;
+ } else
+ return false;
+
+ } else
+ return (info.st_size) ? true : false;
+
+ } // endif table
+
+ return true;
+} // end of FileExists
+
+// Called by SameString and NoFieldOptionChange
+bool ha_connect::CheckString(const char *str1, const char *str2)
+{
+ bool b1= (!str1 || !*str1), b2= (!str2 || !*str2);
+
+ if (b1 && b2)
+ return true;
+ else if ((b1 && !b2) || (!b1 && b2) || stricmp(str1, str2))
+ return false;
+
+ return true;
+} // end of CheckString
+
+/**
+ check whether a string option have changed
+ */
+bool ha_connect::SameString(TABLE *tab, char *opn)
+{
+ char *str1, *str2;
+
+ tshp= tab->s; // The altered table
+ str1= GetStringOption(opn);
+ tshp= NULL;
+ str2= GetStringOption(opn);
+ return CheckString(str1, str2);
+} // end of SameString
+
+/**
+ check whether a Boolean option have changed
+ */
+bool ha_connect::SameBool(TABLE *tab, char *opn)
+{
+ bool b1, b2;
+
+ tshp= tab->s; // The altered table
+ b1= GetBooleanOption(opn, false);
+ tshp= NULL;
+ b2= GetBooleanOption(opn, false);
+ return (b1 == b2);
+} // end of SameBool
+
+/**
+ check whether an integer option have changed
+ */
+bool ha_connect::SameInt(TABLE *tab, char *opn)
+{
+ int i1, i2;
+
+ tshp= tab->s; // The altered table
+ i1= GetIntegerOption(opn);
+ tshp= NULL;
+ i2= GetIntegerOption(opn);
+
+ if (!stricmp(opn, "lrecl"))
+ return (i1 == i2 || !i1 || !i2);
+ else if (!stricmp(opn, "ending"))
+ return (i1 == i2 || i1 <= 0 || i2 <= 0);
+ else
+ return (i1 == i2);
+
+} // end of SameInt
+
+/**
+ check whether a field option have changed
+ */
+bool ha_connect::NoFieldOptionChange(TABLE *tab)
+{
+ bool rc= true;
+ ha_field_option_struct *fop1, *fop2;
+ Field* *fld1= table->s->field;
+ Field* *fld2= tab->s->field;
+
+ for (; rc && *fld1 && *fld2; fld1++, fld2++) {
+ fop1= (*fld1)->option_struct;
+ fop2= (*fld2)->option_struct;
+
+ rc= (fop1->offset == fop2->offset &&
+ fop1->fldlen == fop2->fldlen &&
+ CheckString(fop1->dateformat, fop2->dateformat) &&
+ CheckString(fop1->fieldformat, fop2->fieldformat) &&
+ CheckString(fop1->special, fop2->special));
+ } // endfor fld
+
+ return rc;
+} // end of NoFieldOptionChange
+
+ /**
+ Check if a storage engine supports a particular alter table in-place
+
+ @param altered_table TABLE object for new version of table.
+ @param ha_alter_info Structure describing changes to be done
+ by ALTER TABLE and holding data used
+ during in-place alter.
+
+ @retval HA_ALTER_ERROR Unexpected error.
+ @retval HA_ALTER_INPLACE_NOT_SUPPORTED Not supported, must use copy.
+ @retval HA_ALTER_INPLACE_EXCLUSIVE_LOCK Supported, but requires X lock.
+ @retval HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE
+ Supported, but requires SNW lock
+ during main phase. Prepare phase
+ requires X lock.
+ @retval HA_ALTER_INPLACE_SHARED_LOCK Supported, but requires SNW lock.
+ @retval HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE
+ Supported, concurrent reads/writes
+ allowed. However, prepare phase
+ requires X lock.
+ @retval HA_ALTER_INPLACE_NO_LOCK Supported, concurrent
+ reads/writes allowed.
+
+ @note The default implementation uses the old in-place ALTER API
+ to determine if the storage engine supports in-place ALTER or not.
+
+ @note Called without holding thr_lock.c lock.
+ */
+enum_alter_inplace_result
+ha_connect::check_if_supported_inplace_alter(TABLE *altered_table,
+ Alter_inplace_info *ha_alter_info)
+{
+ DBUG_ENTER("check_if_supported_alter");
+
+ bool idx= false, outward= false;
+ THD *thd= ha_thd();
+ int sqlcom= thd_sql_command(thd);
+ TABTYPE newtyp, type= TAB_UNDEF;
+ HA_CREATE_INFO *create_info= ha_alter_info->create_info;
+//PTOS pos= GetTableOptionStruct(table);
+ PTOS newopt, oldopt;
+ xp= GetUser(thd, xp);
+ PGLOBAL g= xp->g;
+
+ if (!g || !table) {
+ my_message(ER_UNKNOWN_ERROR, "Cannot check ALTER operations", MYF(0));
+ DBUG_RETURN(HA_ALTER_ERROR);
+ } // endif Xchk
+
+ newopt= altered_table->s->option_struct;
+ oldopt= table->s->option_struct;
+
+ // If this is the start of a new query, cleanup the previous one
+ if (xp->CheckCleanup()) {
+ tdbp= NULL;
+ valid_info= false;
+ } // endif CheckCleanup
+
+ g->Alchecked= 1; // Tested in create
+ g->Xchk= NULL;
+ type= GetRealType(oldopt);
+ newtyp= GetRealType(newopt);
+
+ // No copy algorithm for outward tables
+ outward= (!IsFileType(type) || (oldopt->filename && *oldopt->filename));
+
+ // Index operations
+ Alter_inplace_info::HA_ALTER_FLAGS index_operations=
+ Alter_inplace_info::ADD_INDEX |
+ Alter_inplace_info::DROP_INDEX |
+ Alter_inplace_info::ADD_UNIQUE_INDEX |
+ Alter_inplace_info::DROP_UNIQUE_INDEX |
+ Alter_inplace_info::ADD_PK_INDEX |
+ Alter_inplace_info::DROP_PK_INDEX;
+
+ Alter_inplace_info::HA_ALTER_FLAGS inplace_offline_operations=
+ Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH |
+ Alter_inplace_info::ALTER_COLUMN_NAME |
+ Alter_inplace_info::ALTER_COLUMN_DEFAULT |
+ Alter_inplace_info::CHANGE_CREATE_OPTION |
+ Alter_inplace_info::ALTER_RENAME | index_operations;
+
+ if (ha_alter_info->handler_flags & index_operations ||
+ !SameString(altered_table, "optname") ||
+ !SameBool(altered_table, "sepindex")) {
+ if (!IsTypeIndexable(type)) {
+ sprintf(g->Message, "Table type %s is not indexable", oldopt->type);
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ALTER_ERROR);
+ } // endif Indexable
+
+ g->Xchk= new(g) XCHK;
+ PCHK xcp= (PCHK)g->Xchk;
+
+ xcp->oldpix= GetIndexInfo(table->s);
+ xcp->newpix= GetIndexInfo(altered_table->s);
+ xcp->oldsep= GetBooleanOption("sepindex", false);
+ xcp->oldsep= xcp->SetName(g, GetStringOption("optname"));
+ tshp= altered_table->s;
+ xcp->newsep= GetBooleanOption("sepindex", false);
+ xcp->newsep= xcp->SetName(g, GetStringOption("optname"));
+ tshp= NULL;
+
+ if (xtrace && g->Xchk)
+ htrc(
+ "oldsep=%d newsep=%d oldopn=%s newopn=%s oldpix=%p newpix=%p\n",
+ xcp->oldsep, xcp->newsep,
+ SVP(xcp->oldopn), SVP(xcp->newopn),
+ xcp->oldpix, xcp->newpix);
+
+ if (sqlcom == SQLCOM_ALTER_TABLE)
+ idx= true;
+ else
+ DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
+
+ } // endif index operation
+
+ if (!SameString(altered_table, "filename")) {
+ if (!outward) {
+ // Conversion to outward table is only allowed for file based
+ // tables whose file does not exist.
+ tshp= altered_table->s;
+ char *fn= GetStringOption("filename");
+ tshp= NULL;
+
+ if (FileExists(fn)) {
+ strcpy(g->Message, "Operation denied. Table data would be lost.");
+ my_message(ER_UNKNOWN_ERROR, g->Message, MYF(0));
+ DBUG_RETURN(HA_ALTER_ERROR);
+ } else
+ goto fin;
+
+ } else
+ goto fin;
+
+ } // endif filename
+
+ /* Is there at least one operation that requires copy algorithm? */
+ if (ha_alter_info->handler_flags & ~inplace_offline_operations)
+ goto fin;
+
+ /*
+ ALTER TABLE tbl_name CONVERT TO CHARACTER SET .. and
+ ALTER TABLE table_name DEFAULT CHARSET = .. most likely
+ change column charsets and so not supported in-place through
+ old API.
+
+ Changing of PACK_KEYS, MAX_ROWS and ROW_FORMAT options were
+ not supported as in-place operations in old API either.
+ */
+ if (create_info->used_fields & (HA_CREATE_USED_CHARSET |
+ HA_CREATE_USED_DEFAULT_CHARSET |
+ HA_CREATE_USED_PACK_KEYS |
+ HA_CREATE_USED_MAX_ROWS) ||
+ (table->s->row_type != create_info->row_type))
+ goto fin;
+
+#if 0
+ uint table_changes= (ha_alter_info->handler_flags &
+ Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH) ?
+ IS_EQUAL_PACK_LENGTH : IS_EQUAL_YES;
+
+ if (table->file->check_if_incompatible_data(create_info, table_changes)
+ == COMPATIBLE_DATA_YES)
+ DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
+#endif // 0
+
+ // This was in check_if_incompatible_data
+ if (NoFieldOptionChange(altered_table) &&
+ type == newtyp &&
+ SameInt(altered_table, "lrecl") &&
+ SameInt(altered_table, "elements") &&
+ SameInt(altered_table, "header") &&
+ SameInt(altered_table, "quoted") &&
+ SameInt(altered_table, "ending") &&
+ SameInt(altered_table, "compressed"))
+ DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
+
+fin:
+ if (idx) {
+ // Indexing is only supported inplace
+ my_message(ER_ALTER_OPERATION_NOT_SUPPORTED,
+ "Alter operations not supported together by CONNECT", MYF(0));
+ DBUG_RETURN(HA_ALTER_ERROR);
+ } else if (outward) {
+ push_warning(thd, Sql_condition::WARN_LEVEL_WARN, 0,
+ "This is an outward table, table data were not modified.");
+ DBUG_RETURN(HA_ALTER_INPLACE_EXCLUSIVE_LOCK);
+ } else
+ DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
+
+} // end of check_if_supported_inplace_alter
+
+
+/**
+ check_if_incompatible_data() called if ALTER TABLE can't detect otherwise
+ if new and old definition are compatible
+
+ @details If there are no other explicit signs like changed number of
+ fields this function will be called by compare_tables()
+ (sql/sql_tables.cc) to decide should we rewrite whole table or only .frm
+ file.
+
+ @note: This function is no more called by check_if_supported_inplace_alter
+*/
+
+bool ha_connect::check_if_incompatible_data(HA_CREATE_INFO *info,
+ uint table_changes)
+{
+ DBUG_ENTER("ha_connect::check_if_incompatible_data");
+ // TO DO: really implement and check it.
+ push_warning(ha_thd(), Sql_condition::WARN_LEVEL_WARN, 0,
+ "Unexpected call to check_if_incompatible_data.");
+ DBUG_RETURN(COMPATIBLE_DATA_NO);
+} // end of check_if_incompatible_data
+
+/****************************************************************************
+ * CONNECT MRR implementation: use DS-MRR
+ This is just copied from myisam
+ ***************************************************************************/
+
+int ha_connect::multi_range_read_init(RANGE_SEQ_IF *seq, void *seq_init_param,
+ uint n_ranges, uint mode,
+ HANDLER_BUFFER *buf)
+{
+ return ds_mrr.dsmrr_init(this, seq, seq_init_param, n_ranges, mode, buf);
+} // end of multi_range_read_init
+
+int ha_connect::multi_range_read_next(range_id_t *range_info)
+{
+ return ds_mrr.dsmrr_next(range_info);
+} // end of multi_range_read_next
+
+ha_rows ha_connect::multi_range_read_info_const(uint keyno, RANGE_SEQ_IF *seq,
+ void *seq_init_param,
+ uint n_ranges, uint *bufsz,
+ uint *flags, Cost_estimate *cost)
+{
+ /*
+ This call is here because there is no location where this->table would
+ already be known.
+ TODO: consider moving it into some per-query initialization call.
+ */
+ ds_mrr.init(this, table);
+
+ // MMR is implemented for "local" file based tables only
+ if (!IsFileType(GetRealType(GetTableOptionStruct(table))))
+ *flags|= HA_MRR_USE_DEFAULT_IMPL;
+
+ ha_rows rows= ds_mrr.dsmrr_info_const(keyno, seq, seq_init_param, n_ranges,
+ bufsz, flags, cost);
+ xp->g->Mrr= !(*flags & HA_MRR_USE_DEFAULT_IMPL);
+ return rows;
+} // end of multi_range_read_info_const
+
+ha_rows ha_connect::multi_range_read_info(uint keyno, uint n_ranges, uint keys,
+ uint key_parts, uint *bufsz,
+ uint *flags, Cost_estimate *cost)
+{
+ ds_mrr.init(this, table);
+
+ // MMR is implemented for "local" file based tables only
+ if (!IsFileType(GetRealType(GetTableOptionStruct(table))))
+ *flags|= HA_MRR_USE_DEFAULT_IMPL;
+
+ ha_rows rows= ds_mrr.dsmrr_info(keyno, n_ranges, keys, key_parts, bufsz,
+ flags, cost);
+ xp->g->Mrr= !(*flags & HA_MRR_USE_DEFAULT_IMPL);
+ return rows;
+} // end of multi_range_read_info
+
+
+int ha_connect::multi_range_read_explain_info(uint mrr_mode, char *str,
+ size_t size)
+{
+ return ds_mrr.dsmrr_explain_info(mrr_mode, str, size);
+} // end of multi_range_read_explain_info
+
+/* CONNECT MRR implementation ends */
+
+#if 0
+// Does this make sens for CONNECT?
+Item *ha_connect::idx_cond_push(uint keyno_arg, Item* idx_cond_arg)
+{
+ pushed_idx_cond_keyno= keyno_arg;
+ pushed_idx_cond= idx_cond_arg;
+ in_range_check_pushed_down= TRUE;
+ if (active_index == pushed_idx_cond_keyno)
+ mi_set_index_cond_func(file, handler_index_cond_check, this);
+ return NULL;
+}
+#endif // 0
+
+
+struct st_mysql_storage_engine connect_storage_engine=
+{ MYSQL_HANDLERTON_INTERFACE_VERSION };
+
+static MYSQL_SYSVAR_INT(xtrace, xtrace,
+ PLUGIN_VAR_RQCMDARG, "Console trace value.",
+ NULL, update_connect_xtrace, 0, 0, INT_MAX, 1);
+
+static struct st_mysql_sys_var* connect_system_variables[]= {
+ MYSQL_SYSVAR(xtrace),
+ NULL
+};
+
+maria_declare_plugin(connect)
+{
+ MYSQL_STORAGE_ENGINE_PLUGIN,
+ &connect_storage_engine,
+ "CONNECT",
+ "Olivier Bertrand",
+ "Management of External Data (SQL/MED), including many file formats",
+ PLUGIN_LICENSE_GPL,
+ connect_init_func, /* Plugin Init */
+ connect_done_func, /* Plugin Deinit */
+ 0x0103, /* version number (1.03) */
+ NULL, /* status variables */
+ connect_system_variables, /* system variables */
+ "1.03", /* string version */
+ MariaDB_PLUGIN_MATURITY_BETA /* maturity */
+}
+maria_declare_plugin_end;
diff --git a/storage/connect/mycat.cc b/storage/connect/mycat.cc
index 15df8cf8ba2..909bccd8864 100644
--- a/storage/connect/mycat.cc
+++ b/storage/connect/mycat.cc
@@ -96,7 +96,7 @@
extern "C" HINSTANCE s_hModule; // Saved module handle
#endif // !WIN32
-extern int xtrace;
+extern "C" int trace;
PQRYRES OEMColumns(PGLOBAL g, PTOS topt, char *tab, char *db, bool info);
@@ -716,7 +716,7 @@ bool MYCAT::GetIndexInfo(PGLOBAL g, PTABDEF defp)
PRELDEF MYCAT::GetTableDesc(PGLOBAL g, LPCSTR name,
LPCSTR type, PRELDEF *prp)
{
- if (xtrace)
+ if (trace)
printf("GetTableDesc: name=%s am=%s\n", name, SVP(type));
// If not specified get the type of this table
@@ -735,7 +735,7 @@ PRELDEF MYCAT::MakeTableDesc(PGLOBAL g, LPCSTR name, LPCSTR am)
TABTYPE tc;
PRELDEF tdp= NULL;
- if (xtrace)
+ if (trace)
printf("MakeTableDesc: name=%s am=%s\n", name, SVP(am));
/*********************************************************************/
@@ -794,14 +794,14 @@ PTDB MYCAT::GetTable(PGLOBAL g, PTABLE tablep, MODE mode, LPCSTR type)
PTDB tdbp= NULL;
LPCSTR name= tablep->GetName();
- if (xtrace)
+ if (trace)
printf("GetTableDB: name=%s\n", name);
// Look for the description of the requested table
tdp= GetTableDesc(g, name, type);
if (tdp) {
- if (xtrace)
+ if (trace)
printf("tdb=%p type=%s\n", tdp, tdp->GetType());
if (tablep->GetQualifier())
@@ -811,7 +811,7 @@ PTDB MYCAT::GetTable(PGLOBAL g, PTABLE tablep, MODE mode, LPCSTR type)
} // endif tdp
if (tdbp) {
- if (xtrace)
+ if (trace)
printf("tdbp=%p name=%s amtype=%d\n", tdbp, tdbp->GetName(),
tdbp->GetAmType());
tablep->SetTo_Tdb(tdbp);
diff --git a/storage/connect/myconn.cpp b/storage/connect/myconn.cpp
index 1776a8f9168..13a6f996605 100644
--- a/storage/connect/myconn.cpp
+++ b/storage/connect/myconn.cpp
@@ -295,7 +295,7 @@ PQRYRES SrcColumns(PGLOBAL g, const char *host, const char *db,
// Send the source command to MySQL
if (myc.ExecSQL(g, query, &w) == RC_OK)
- qrp = myc.GetResult(g);
+ qrp = myc.GetResult(g, true);
myc.Close();
return qrp;
@@ -676,7 +676,7 @@ MYSQL_FIELD *MYSQLC::GetNextField(void)
/***********************************************************************/
PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
{
- char *fmt;
+ char *fmt, v;
int n;
bool uns;
PCOLRES *pcrp, crp;
@@ -705,7 +705,6 @@ PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
qrp->Nblin = 0;
qrp->Cursor = 0;
-
//for (fld = mysql_fetch_field(m_Res); fld;
// fld = mysql_fetch_field(m_Res)) {
for (fld = GetNextField(); fld; fld = GetNextField()) {
@@ -718,17 +717,19 @@ PQRYRES MYSQLC::GetResult(PGLOBAL g, bool pdb)
crp->Name = (char*)PlugSubAlloc(g, NULL, fld->name_length + 1);
strcpy(crp->Name, fld->name);
- if ((crp->Type = MYSQLtoPLG(fld->type)) == TYPE_ERROR) {
+ if ((crp->Type = MYSQLtoPLG(fld->type, &v)) == TYPE_ERROR) {
sprintf(g->Message, "Type %d not supported for column %s",
fld->type, crp->Name);
return NULL;
} else if (crp->Type == TYPE_DATE && !pdb)
// For direct MySQL connection, display the MySQL date string
crp->Type = TYPE_STRING;
+ else
+ crp->Var = v;
crp->Prec = (crp->Type == TYPE_DOUBLE || crp->Type == TYPE_DECIM)
? fld->decimals : 0;
- crp->Length = fld->max_length;
+ crp->Length = max(fld->length, fld->max_length);
crp->Clen = GetTypeSize(crp->Type, crp->Length);
uns = (fld->flags & (UNSIGNED_FLAG | ZEROFILL_FLAG)) ? true : false;
diff --git a/storage/connect/myutil.cpp b/storage/connect/myutil.cpp
index 1594afb1b3d..ea694389eaf 100644
--- a/storage/connect/myutil.cpp
+++ b/storage/connect/myutil.cpp
@@ -58,12 +58,8 @@ int MYSQLtoPLG(char *typname, char *var)
type = TYPE_ERROR;
if (var) {
- // This is to make the difference between CHAR and VARCHAR
- if (type == TYPE_STRING && stricmp(typname, "char"))
- *var = 'V';
-
- // This is to make the difference between temporal values
if (type == TYPE_DATE) {
+ // This is to make the difference between temporal values
if (!stricmp(typname, "date"))
*var = 'D';
else if (!stricmp(typname, "datetime"))
@@ -75,7 +71,11 @@ int MYSQLtoPLG(char *typname, char *var)
else if (!stricmp(typname, "year"))
*var = 'Y';
- } // endif type
+ } else if (type == TYPE_STRING && stricmp(typname, "char"))
+ // This is to make the difference between CHAR and VARCHAR
+ *var = 'V';
+ else
+ *var = 0;
} // endif var
@@ -200,7 +200,6 @@ int MYSQLtoPLG(int mytype, char *var)
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
- if (var) *var = 'V';
case MYSQL_TYPE_STRING:
type = TYPE_STRING;
break;
@@ -208,6 +207,25 @@ int MYSQLtoPLG(int mytype, char *var)
type = TYPE_ERROR;
} // endswitch mytype
+ if (var) switch (mytype) {
+ // This is to make the difference between CHAR and VARCHAR
+ case MYSQL_TYPE_VAR_STRING:
+#if !defined(ALPHA)
+ case MYSQL_TYPE_VARCHAR:
+#endif // !ALPHA)
+ case MYSQL_TYPE_BLOB:
+ case MYSQL_TYPE_TINY_BLOB:
+ case MYSQL_TYPE_MEDIUM_BLOB:
+ case MYSQL_TYPE_LONG_BLOB: *var = 'V'; break;
+ // This is to make the difference between temporal values
+ case MYSQL_TYPE_TIMESTAMP: *var = 'S'; break;
+ case MYSQL_TYPE_DATE: *var = 'D'; break;
+ case MYSQL_TYPE_DATETIME: *var = 'A'; break;
+ case MYSQL_TYPE_YEAR: *var = 'Y'; break;
+ case MYSQL_TYPE_TIME: *var = 'T'; break;
+ default: *var = 0;
+ } // endswitch mytype
+
return type;
} // end of MYSQLtoPLG
diff --git a/storage/connect/osutil.c b/storage/connect/osutil.c
index 5570e55f3b2..3c1ca0147c6 100644
--- a/storage/connect/osutil.c
+++ b/storage/connect/osutil.c
@@ -78,8 +78,8 @@ void _splitpath(LPCSTR name, LPSTR drive, LPSTR dir, LPSTR fn, LPSTR ft)
LPCSTR p2, p = name;
#ifdef DEBTRACE
- fprintf(debug,"SplitPath: name=%s [%s (%d)]\n",
- XSTR(name), XSTR(__FILE__), __LINE__);
+ htrc("SplitPath: name=%s [%s (%d)]\n",
+ XSTR(name), XSTR(__FILE__), __LINE__);
#endif
if (drive) *drive = '\0';
@@ -100,7 +100,7 @@ void _splitpath(LPCSTR name, LPSTR drive, LPSTR dir, LPSTR fn, LPSTR ft)
if (fn) strcpy(fn, p);
#ifdef DEBTRACE
- fprintf(debug,"SplitPath: name=%s drive=%s dir=%s filename=%s type=%s [%s(%d)]\n",
+ htrc("SplitPath: name=%s drive=%s dir=%s filename=%s type=%s [%s(%d)]\n",
XSTR(name), XSTR(drive), XSTR(dir), XSTR(fn), XSTR(ft), __FILE__, __LINE__);
#endif
} /* end of _splitpath */
diff --git a/storage/connect/plgdbsem.h b/storage/connect/plgdbsem.h
index e7c824aa164..0fe9e20e390 100644
--- a/storage/connect/plgdbsem.h
+++ b/storage/connect/plgdbsem.h
@@ -531,6 +531,7 @@ typedef struct _colres {
int Prec; /* Precision */
int Flag; /* Flag option value */
XFLD Fld; /* Type of field info */
+ char Var; /* Type added information */
} COLRES;
#if defined(WIN32) && !defined(NOEX)
diff --git a/storage/connect/plgdbutl.cpp b/storage/connect/plgdbutl.cpp
index 2987ef62e21..6b47aa7433e 100644
--- a/storage/connect/plgdbutl.cpp
+++ b/storage/connect/plgdbutl.cpp
@@ -80,9 +80,6 @@ extern pthread_mutex_t parmut;
#endif // !WIN32
#endif // THREAD
-#define PLGINI "plugdb.ini" /* Configuration settings file */
-#define PLGXINI "plgcnx.ini" /* Configuration settings file */
-
/***********************************************************************/
/* DB static variables. */
/***********************************************************************/
@@ -90,27 +87,13 @@ bool Initdone = false;
bool plugin = false; // True when called by the XDB plugin handler
extern "C" {
-extern char connectini[];
- char plgxini[_MAX_PATH] = PLGXINI;
- char plgini[_MAX_PATH] = PLGINI;
-#if defined(WIN32)
- char nmfile[_MAX_PATH] = ".\\Log\\plugdb.out";
- char pdebug[_MAX_PATH] = ".\\Log\\plgthread.out";
-
- HINSTANCE s_hModule; // Saved module handle
-#else // !WIN32
- char nmfile[_MAX_PATH] = "./Log/plugdb.out";
- char pdebug[_MAX_PATH] = "./Log/plgthread.out";
-#endif // !WIN32
-
#if defined(XMSG)
char msglang[16] = "ENGLISH"; // Default language
#endif
+extern int trace;
+extern char version[];
} // extern "C"
-extern "C" int trace;
-extern "C" char version[];
-
// The debug trace used by the main thread
FILE *pfile = NULL;
@@ -219,27 +202,6 @@ int global_open(GLOBAL *g, int msgid, const char *path, int flags, int mode)
return h;
}
-
-/**************************************************************************/
-/* Utility for external callers (such as XDB) */
-/**************************************************************************/
-DllExport char *GetIni(int n)
- {
- switch (n) {
- case 1: return plgxini; break;
- case 2: return nmfile; break;
- case 3: return pdebug; break;
- case 4: return version; break;
-#if defined(XMSG)
- case 5: return msglang; break;
-#endif // XMSG
- case 6: return connectini; break;
-// default: return plgini;
- } // endswitch GetIni
-
- return plgini;
- } // end of GetIni
-
DllExport void SetTrc(void)
{
// If tracing is on, debug must be initialized.
@@ -418,138 +380,9 @@ char *PlgGetDataPath(PGLOBAL g)
{
PCATLG cat = PlgGetCatalog(g, false);
-//if (!cat)
-// return GetIniString(g, NULL, "DataBase", "DataPath", "", plgini);
-
return (cat) ? cat->GetDataPath() : NULL;
} // end of PlgGetDataPath
-#if 0
-/***********************************************************************/
-/* PlgGetXdbPath: sets the fully qualified file name of a database */
-/* description file in lgn and the new datapath in dp. */
-/* New database description file is a Configuration Settings file */
-/* that will be used and updated in case of DB modifications such */
-/* as Insert into a VCT file. Look for it and use it if found. */
-/* By default the configuration file is DataPath\name.xdb but the */
-/* configuration file name may also be specified in Plugdb.ini. */
-/***********************************************************************/
-bool PlgSetXdbPath(PGLOBAL g, PSZ dbname, PSZ dbpath,
- char *lgn, int lgsize,
- char *path, int psize)
- {
- char *dp, datapath[_MAX_PATH], ft[_MAX_EXT] = ".xdb";
- int n;
-
- if (path) {
- dp = path;
- n = psize;
- } else {
- dp = datapath;
- n = sizeof(datapath);
- } // endif path
-
- GetPrivateProfileString("DataBase", "DataPath", "", dp, n, plgini);
-
- if (trace)
- htrc("PlgSetXdbPath: path=%s\n", dp);
-
- if (dbpath) {
- char fn[_MAX_FNAME];
-
- strcpy(lgn, dbpath);
- _splitpath(lgn, NULL, NULL, fn, NULL);
-
- if (!*fn) // Old style use command
- strcat(lgn, dbname);
-
- _splitpath(lgn, NULL, NULL, dbname, NULL); // Extract DB name
- } else if (strcspn(dbname, ":/\\.") < strlen(dbname)) {
- // dbname name contains the path name of the XDB file
- strcpy(lgn, dbname);
- _splitpath(lgn, NULL, NULL, dbname, NULL); // Extract DB name
- } else
- /*******************************************************************/
- /* New database description file is a Configuration Settings file */
- /* that will be used and updated in case of DB modifications such */
- /* as Insert into a VCT file. Look for it and use it if found. */
- /* By default the configuration file is DataPath\name.xdb but the */
- /* configuration file name may also be specified in Plugdb.ini. */
- /*******************************************************************/
- GetPrivateProfileString("DBnames", dbname, "", lgn, lgsize, plgini);
-
- if (*lgn) {
-#if !defined(UNIX)
- char drive[_MAX_DRIVE];
- char direc[_MAX_DIR];
-#endif
- char fname[_MAX_FNAME];
- char ftype[_MAX_EXT];
-
- _splitpath(lgn, NULL, NULL, fname, ftype);
-
- if (!*ftype)
- strcat(lgn, ft);
- else if (!stricmp(ftype, ".var")) {
- strcpy(g->Message, MSG(NO_MORE_VAR));
- return true;
- } // endif ftype
-
- // Given DB description path may be relative to data path
- PlugSetPath(lgn, lgn, dp);
-
- // New data path is the path of the configuration setting file
-#if !defined(UNIX)
- _splitpath(lgn, drive, direc, NULL, NULL);
- _makepath(dp, drive, direc, "", "");
-#else
-//#error This must be tested for trailing slash
- _splitpath(lgn, NULL, dp, NULL, NULL);
-#endif
- } else {
- // Try dbname[.ext] in the current directory
- strcpy(lgn, dbname);
-
- if (!strchr(dbname, '.'))
- strcat(lgn, ft);
-
- PlugSetPath(lgn, lgn, dp);
- } // endif lgn
-
- if (trace)
- htrc("PlgSetXdbPath: new DB description file=%s\n", lgn);
-
- return false;
- } // end of PlgSetXdbPath
-#endif // 0
-
-/***********************************************************************/
-/* Extract from a path name the required component. */
-/* This function assumes there is enough space in the buffer. */
-/***********************************************************************/
-#if 0
-char *ExtractFromPath(PGLOBAL g, char *pBuff, char *FileName, OPVAL op)
- {
- char *drive = NULL, *direc = NULL, *fname = NULL, *ftype = NULL;
-
- switch (op) { // Determine which part to extract
-#if !defined(UNIX)
- case OP_FDISK: drive = pBuff; break;
-#endif // !UNIX
- case OP_FPATH: direc = pBuff; break;
- case OP_FNAME: fname = pBuff; break;
- case OP_FTYPE: ftype = pBuff; break;
- default:
- sprintf(g->Message, MSG(INVALID_OPER), op, "ExtractFromPath");
- return NULL;
- } // endswitch op
-
- // Now do the extraction
- _splitpath(FileName, drive, direc, fname, ftype);
- return pBuff;
- } // end of PlgExtractFromPath
-#endif
-
/***********************************************************************/
/* Check the occurence and matching of a pattern against a string. */
/* Because this function is only used for catalog name checking, */
@@ -1459,19 +1292,17 @@ void *PlgDBSubAlloc(PGLOBAL g, void *memp, size_t size)
size = ((size + 7) / 8) * 8; /* Round up size to multiple of 8 */
pph = (PPOOLHEADER)memp;
-#if defined(DEBTRACE)
- htrc("PlgDBSubAlloc: memp=%p size=%d used=%d free=%d\n",
- memp, size, pph->To_Free, pph->FreeBlk);
-#endif
+ if (trace > 1)
+ htrc("PlgDBSubAlloc: memp=%p size=%d used=%d free=%d\n",
+ memp, size, pph->To_Free, pph->FreeBlk);
if ((uint)size > pph->FreeBlk) { /* Not enough memory left in pool */
sprintf(g->Message,
"Not enough memory in Work area for request of %d (used=%d free=%d)",
(int) size, pph->To_Free, pph->FreeBlk);
-#if defined(DEBTRACE)
- htrc("%s\n", g->Message);
-#endif
+ if (trace)
+ htrc("%s\n", g->Message);
return NULL;
} // endif size
@@ -1482,10 +1313,11 @@ void *PlgDBSubAlloc(PGLOBAL g, void *memp, size_t size)
memp = MakePtr(memp, pph->To_Free); // Points to suballocated block
pph->To_Free += size; // New offset of pool free block
pph->FreeBlk -= size; // New size of pool free block
-#if defined(DEBTRACE)
- htrc("Done memp=%p used=%d free=%d\n",
- memp, pph->To_Free, pph->FreeBlk);
-#endif
+
+ if (trace > 1)
+ htrc("Done memp=%p used=%d free=%d\n",
+ memp, pph->To_Free, pph->FreeBlk);
+
return (memp);
} // end of PlgDBSubAlloc
diff --git a/storage/connect/plugutil.c b/storage/connect/plugutil.c
index 6c2a1ed403f..627861c1c36 100644
--- a/storage/connect/plugutil.c
+++ b/storage/connect/plugutil.c
@@ -1,550 +1,547 @@
-/************** PlugUtil C Program Source Code File (.C) ***************/
-/* */
-/* PROGRAM NAME: PLUGUTIL */
-/* ------------- */
-/* Version 2.8 */
-/* */
-/* COPYRIGHT: */
-/* ---------- */
-/* (C) Copyright to the author Olivier BERTRAND 1993-2014 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are initialization and utility Plug routines. */
-/* */
-/* WHAT YOU NEED TO COMPILE THIS PROGRAM: */
-/* -------------------------------------- */
-/* */
-/* REQUIRED FILES: */
-/* --------------- */
-/* See Readme.C for a list and description of required SYSTEM files. */
-/* */
-/* PLUG.C - Source code */
-/* GLOBAL.H - Global declaration file */
-/* OPTION.H - Option declaration file */
-/* */
-/* REQUIRED LIBRARIES: */
-/* ------------------- */
-/* */
-/* OS2.LIB - OS2 libray */
-/* LLIBCE.LIB - Protect mode/standard combined large model C */
-/* library */
-/* */
-/* REQUIRED PROGRAMS: */
-/* ------------------ */
-/* */
-/* IBM C Compiler */
-/* IBM Linker */
-/* */
-/***********************************************************************/
-//efine DEBTRACE 3
-//efine DEBTRACE2
-
-/***********************************************************************/
-/* */
-/* Include relevant MariaDB header file. */
-/* */
-/***********************************************************************/
-#include "my_global.h"
-#if defined(WIN32)
-//#include <windows.h>
-#else
-#if defined(UNIX) || defined(UNIV_LINUX)
-#include <errno.h>
-#include <unistd.h>
-//#define __stdcall
-#else
-#include <dir.h>
-#endif
-#include <stdarg.h>
-#endif
-
-#if defined(WIN)
-#include <alloc.h>
-#endif
-#include <errno.h> /* definitions of ERANGE ENOMEM */
-#if !defined(UNIX) && !defined(UNIV_LINUX)
-#include <direct.h> /* Directory management library */
-#endif
-
-/***********************************************************************/
-/* */
-/* Include application header files */
-/* */
-/* global.h is header containing all global declarations. */
-/* */
-/***********************************************************************/
-#define STORAGE /* Initialize global variables */
-
-#include "osutil.h"
-#include "global.h"
-
-#if defined(WIN32)
-extern HINSTANCE s_hModule; /* Saved module handle */
-#endif // WIN32
-
-extern char plgini[];
-extern int trace;
-
-#if defined(XMSG)
-extern char msglang[];
-#endif // XMSG
-
-/***********************************************************************/
-/* Local Definitions and static variables */
-/***********************************************************************/
-typedef struct {
- ushort Segsize;
- ushort Size;
- } AREASIZE;
-
-ACTIVITY defActivity = { /* Describes activity and language */
- NULL, /* Points to user work area(s) */
- "Unknown"}; /* Application name */
-
-#if defined(XMSG) || defined(NEWMSG)
- static char stmsg[200];
-#endif // XMSG || NEWMSG
-
-#if defined(UNIX) || defined(UNIV_LINUX)
-#include "rcmsg.h"
-#endif // UNIX
-
-/**************************************************************************/
-/* Tracing output function. */
-/**************************************************************************/
-void htrc(char const *fmt, ...)
- {
- va_list ap;
- va_start (ap, fmt);
-
-//if (trace == 0 || (trace == 1 && !debug) || !fmt) {
-// printf("In %s wrong trace=%d debug=%p fmt=%p\n",
-// __FILE__, trace, debug, fmt);
-// trace = 0;
-// } // endif trace
-
-//if (trace == 1)
-// vfprintf(debug, fmt, ap);
-//else
- vfprintf(stderr, fmt, ap);
-
- va_end (ap);
- } // end of htrc
-
-/***********************************************************************/
-/* Plug initialization routine. */
-/* Language points on initial language name and eventual path. */
-/* Return value is the pointer to the Global structure. */
-/***********************************************************************/
-PGLOBAL PlugInit(LPCSTR Language, uint worksize)
- {
- PGLOBAL g;
-
- if (trace > 1)
- htrc("PlugInit: Language='%s'\n",
- ((!Language) ? "Null" : (char*)Language));
-
- if (!(g = malloc(sizeof(GLOBAL)))) {
- fprintf(stderr, MSG(GLOBAL_ERROR), (int)sizeof(GLOBAL));
- return NULL;
- } else {
- g->Sarea_Size = worksize;
- g->Trace = 0;
- g->Createas = 0;
- g->Alchecked = 0;
- g->Mrr = 0;
- g->Activityp = g->ActivityStart = NULL;
- g->Xchk = NULL;
- strcpy(g->Message, "");
-
- /*******************************************************************/
- /* Allocate the main work segment. */
- /*******************************************************************/
- if (!(g->Sarea = PlugAllocMem(g, worksize))) {
- char errmsg[256];
- sprintf(errmsg, MSG(WORK_AREA), g->Message);
- strcpy(g->Message, errmsg);
- } /* endif Sarea */
-
- } /* endif g */
-
- g->jump_level = -1; /* New setting to allow recursive call of Plug */
- return(g);
- } /* end of PlugInit */
-
-/***********************************************************************/
-/* PlugExit: Terminate Plug operations. */
-/***********************************************************************/
-int PlugExit(PGLOBAL g)
- {
- int rc = 0;
-
- if (!g)
- return rc;
-
- if (g->Sarea)
- free(g->Sarea);
-
- free(g);
- return rc;
- } /* end of PlugExit */
-
-/***********************************************************************/
-/* Remove the file type from a file name. */
-/* Note: this routine is not really implemented for Unix. */
-/***********************************************************************/
-LPSTR PlugRemoveType(LPSTR pBuff, LPCSTR FileName)
- {
-#if !defined(UNIX) && !defined(UNIV_LINUX)
- char drive[_MAX_DRIVE];
-#else
- char *drive = NULL;
-#endif
- char direc[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ftype[_MAX_EXT];
-
- _splitpath(FileName, drive, direc, fname, ftype);
-
- if (trace > 1) {
- htrc("after _splitpath: FileName=%s\n", FileName);
- htrc("drive=%s dir=%s fname=%s ext=%s\n",
- SVP(drive), direc, fname, ftype);
- } // endif trace
-
- _makepath(pBuff, drive, direc, fname, "");
-
- if (trace > 1)
- htrc("buff='%s'\n", pBuff);
-
- return pBuff;
- } // end of PlugRemoveType
-
-
-BOOL PlugIsAbsolutePath(LPCSTR path)
-{
-#if defined(WIN32)
- return ((path[0] >= 'a' && path[0] <= 'z') ||
- (path[0] >= 'A' && path[0] <= 'Z')) && path[1] == ':';
-#else
- return path[0] == '/';
-#endif
-}
-
-
-/***********************************************************************/
-/* Set the full path of a file relatively to a given path. */
-/* Note: this routine is not really implemented for Unix. */
-/***********************************************************************/
-LPCSTR PlugSetPath(LPSTR pBuff, LPCSTR prefix, LPCSTR FileName, LPCSTR defpath)
- {
- char newname[_MAX_PATH];
- char direc[_MAX_DIR], defdir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ftype[_MAX_EXT];
-#if !defined(UNIX) && !defined(UNIV_LINUX)
- char drive[_MAX_DRIVE], defdrv[_MAX_DRIVE];
-#else
- char *drive = NULL, *defdrv = NULL;
-#endif
-
- if (!strncmp(FileName, "//", 2) || !strncmp(FileName, "\\\\", 2)) {
- strcpy(pBuff, FileName); // Remote file
- return pBuff;
- } // endif
-
- if (PlugIsAbsolutePath(FileName))
- {
- strcpy(pBuff, FileName); // FileName includes absolute path
- return pBuff;
- } // endif
-
- if (strcmp(prefix, ".") && !PlugIsAbsolutePath(defpath))
- {
- char tmp[_MAX_PATH];
- int len= snprintf(tmp, sizeof(tmp) - 1, "%s%s%s",
- prefix, defpath, FileName);
- memcpy(pBuff, tmp, (size_t) len);
- pBuff[len]= '\0';
- return pBuff;
- }
-
- _splitpath(FileName, drive, direc, fname, ftype);
- _splitpath(defpath, defdrv, defdir, NULL, NULL);
-
- if (trace > 1) {
- htrc("after _splitpath: FileName=%s\n", FileName);
-#if defined(UNIX) || defined(UNIV_LINUX)
- htrc("dir=%s fname=%s ext=%s\n", direc, fname, ftype);
-#else
- htrc("drive=%s dir=%s fname=%s ext=%s\n", drive, direc, fname, ftype);
- htrc("defdrv=%s defdir=%s\n", defdrv, defdir);
-#endif
- } // endif trace
-
- if (drive && !*drive)
- strcpy(drive, defdrv);
-
- switch (*direc) {
- case '\0':
- strcpy(direc, defdir);
- break;
- case '\\':
- case '/':
- break;
- default:
- // This supposes that defdir ends with a SLASH
- strcpy(direc, strcat(defdir, direc));
- } // endswitch
-
- _makepath(newname, drive, direc, fname, ftype);
-
- if (trace > 1)
- htrc("newname='%s'\n", newname);
-
- if (_fullpath(pBuff, newname, _MAX_PATH)) {
- if (trace > 1)
- htrc("pbuff='%s'\n", pBuff);
-
- return pBuff;
- } else
- return FileName; // Error, return unchanged name
-
- } // end of PlugSetPath
-
-#if defined(XMSG)
-/***********************************************************************/
-/* PlugGetMessage: get a message from the message file. */
-/***********************************************************************/
-char *PlugReadMessage(PGLOBAL g, int mid, char *m)
- {
- char msgfile[_MAX_PATH], msgid[32], buff[256];
- char *msg;
- FILE *mfile = NULL;
-
- GetPrivateProfileString("Message", msglang, "Message\\english.msg",
- msgfile, _MAX_PATH, plgini);
-
- if (!(mfile = fopen(msgfile, "rt"))) {
- sprintf(stmsg, "Fail to open message file %s for %s", msgfile, msglang);
- goto err;
- } // endif mfile
-
- for (;;)
- if (!fgets(buff, 256, mfile)) {
- sprintf(stmsg, "Cannot get message %d %s", mid, SVP(m));
- goto fin;
- } else
- if (atoi(buff) == mid)
- break;
-
- if (sscanf(buff, " %*d %s \"%[^\"]", msgid, stmsg) < 2) {
- // Old message file
- if (!sscanf(buff, " %*d \"%[^\"]", stmsg)) {
- sprintf(stmsg, "Bad message file for %d %s", mid, SVP(m));
- goto fin;
- } else
- m = NULL;
-
- } // endif sscanf
-
- if (m && strcmp(m, msgid)) {
- // Message file is out of date
- strcpy(stmsg, m);
- goto fin;
- } // endif m
-
- fin:
- fclose(mfile);
-
- err:
- if (g) {
- // Called by STEP
- msg = (char *)PlugSubAlloc(g, NULL, strlen(stmsg) + 1);
- strcpy(msg, stmsg);
- } else // Called by MSG or PlgGetErrorMsg
- msg = stmsg;
-
- return msg;
- } // end of PlugReadMessage
-
-#elif defined(NEWMSG)
-/***********************************************************************/
-/* PlugGetMessage: get a message from the resource string table. */
-/***********************************************************************/
-char *PlugGetMessage(PGLOBAL g, int mid)
- {
- char *msg;
-
-#if !defined(UNIX) && !defined(UNIV_LINUX)
- int n = LoadString(s_hModule, (uint)mid, (LPTSTR)stmsg, 200);
-
- if (n == 0) {
- DWORD rc = GetLastError();
- msg = (char*)PlugSubAlloc(g, NULL, 512); // Extend buf allocation
- n = sprintf(msg, "Message %d, rc=%d: ", mid, rc);
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
- (LPTSTR)(msg + n), 512 - n, NULL);
- return msg;
- } // endif n
-
-#else // UNIX
- if (!GetRcString(mid, stmsg, 200))
- sprintf(stmsg, "Message %d not found", mid);
-#endif // UNIX
-
- if (g) {
- // Called by STEP
- msg = (char *)PlugSubAlloc(g, NULL, strlen(stmsg) + 1);
- strcpy(msg, stmsg);
- } else // Called by MSG or PlgGetErrorMsg
- msg = stmsg;
-
- return msg;
- } // end of PlugGetMessage
-#endif // NEWMSG
-
-#if defined(WIN32)
-/***********************************************************************/
-/* Return the line length of the console screen buffer. */
-/***********************************************************************/
-short GetLineLength(PGLOBAL g)
- {
- CONSOLE_SCREEN_BUFFER_INFO coninfo;
- HANDLE hcons = GetStdHandle(STD_OUTPUT_HANDLE);
- BOOL b = GetConsoleScreenBufferInfo(hcons, &coninfo);
-
- return (b) ? coninfo.dwSize.X : 0;
- } // end of GetLineLength
-#endif // WIN32
-
-/***********************************************************************/
-/* Program for memory allocation of work and language areas. */
-/***********************************************************************/
-void *PlugAllocMem(PGLOBAL g, uint size)
- {
- void *areap; /* Pointer to allocated area */
-
- /*********************************************************************/
- /* This is the allocation routine for the WIN32/UNIX/AIX version. */
- /*********************************************************************/
- if (!(areap = malloc(size)))
- sprintf(g->Message, MSG(MALLOC_ERROR), "malloc");
-
- if (trace > 1) {
- if (areap)
- htrc("Memory of %u allocated at %p\n", size, areap);
- else
- htrc("PlugAllocMem: %s\n", g->Message);
-
- } // endif trace
-
- return (areap);
- } /* end of PlugAllocMem */
-
-/***********************************************************************/
-/* Program for SubSet initialization of memory pools. */
-/* Here there should be some verification done such as validity of */
-/* the address and size not larger than memory size. */
-/***********************************************************************/
-BOOL PlugSubSet(PGLOBAL g __attribute__((unused)), void *memp, uint size)
- {
- PPOOLHEADER pph = memp;
-
- pph->To_Free = (OFFSET)sizeof(POOLHEADER);
- pph->FreeBlk = size - pph->To_Free;
-
- return FALSE;
- } /* end of PlugSubSet */
-
-/***********************************************************************/
-/* Program for sub-allocating one item in a storage area. */
-/* Note: SubAlloc routines of OS/2 are no more used to increase the */
-/* code portability and avoid problems when a grammar compiled under */
-/* one version of OS/2 is used under another version. */
-/* The simple way things are done here is also based on the fact */
-/* that no freeing of suballocated blocks is permitted in Plug. */
-/***********************************************************************/
-void *PlugSubAlloc(PGLOBAL g, void *memp, size_t size)
- {
- PPOOLHEADER pph; /* Points on area header. */
-
- if (!memp)
- /*******************************************************************/
- /* Allocation is to be done in the Sarea. */
- /*******************************************************************/
- memp = g->Sarea;
-
-//size = ((size + 3) / 4) * 4; /* Round up size to multiple of 4 */
- size = ((size + 7) / 8) * 8; /* Round up size to multiple of 8 */
- pph = (PPOOLHEADER)memp;
-
-#if defined(DEBUG2) || defined(DEBUG3)
- htrc("SubAlloc in %p size=%d used=%d free=%d\n",
- memp, size, pph->To_Free, pph->FreeBlk);
-#endif
-
- if ((uint)size > pph->FreeBlk) { /* Not enough memory left in pool */
- char *pname = "Work";
-
- sprintf(g->Message,
- "Not enough memory in %s area for request of %u (used=%d free=%d)",
- pname, (uint) size, pph->To_Free, pph->FreeBlk);
-
-#if defined(DEBUG2) || defined(DEBUG3)
- htrc("%s\n", g->Message);
-#endif
-
- longjmp(g->jumper[g->jump_level], 1);
- } /* endif size OS32 code */
-
- /*********************************************************************/
- /* Do the suballocation the simplest way. */
- /*********************************************************************/
- memp = MakePtr(memp, pph->To_Free); /* Points to suballocated block */
- pph->To_Free += size; /* New offset of pool free block */
- pph->FreeBlk -= size; /* New size of pool free block */
-#if defined(DEBUG2) || defined(DEBUG3)
- htrc("Done memp=%p used=%d free=%d\n",
- memp, pph->To_Free, pph->FreeBlk);
-#endif
- return (memp);
- } /* end of PlugSubAlloc */
-
-/***********************************************************************/
-/* This routine suballocate a copy of the passed string. */
-/***********************************************************************/
-char *PlugDup(PGLOBAL g, const char *str)
- {
- char *buf;
- size_t len;
-
- if (str && (len = strlen(str))) {
- buf = (char*)PlugSubAlloc(g, NULL, len + 1);
- strcpy(buf, str);
- } else
- buf = NULL;
-
- return(buf);
- } /* end of PlugDup */
-
-/***********************************************************************/
-/* This routine makes a pointer from an offset to a memory pointer. */
-/***********************************************************************/
-void *MakePtr(void *memp, OFFSET offset)
- {
- return ((offset == 0) ? NULL : &((char *)memp)[offset]);
- } /* end of MakePtr */
-
-/***********************************************************************/
-/* This routine makes an offset from a pointer new format. */
-/***********************************************************************/
-#if 0
-OFFSET MakeOff(void *memp, void *ptr)
- {
- return ((!ptr) ? 0 : (OFFSET)((char *)ptr - (char *)memp));
- } /* end of MakeOff */
-#endif
-/*--------------------- End of PLUGUTIL program -----------------------*/
+/************** PlugUtil C Program Source Code File (.C) ***************/
+/* */
+/* PROGRAM NAME: PLUGUTIL */
+/* ------------- */
+/* Version 2.8 */
+/* */
+/* COPYRIGHT: */
+/* ---------- */
+/* (C) Copyright to the author Olivier BERTRAND 1993-2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are initialization and utility Plug routines. */
+/* */
+/* WHAT YOU NEED TO COMPILE THIS PROGRAM: */
+/* -------------------------------------- */
+/* */
+/* REQUIRED FILES: */
+/* --------------- */
+/* See Readme.C for a list and description of required SYSTEM files. */
+/* */
+/* PLUG.C - Source code */
+/* GLOBAL.H - Global declaration file */
+/* OPTION.H - Option declaration file */
+/* */
+/* REQUIRED LIBRARIES: */
+/* ------------------- */
+/* */
+/* OS2.LIB - OS2 libray */
+/* LLIBCE.LIB - Protect mode/standard combined large model C */
+/* library */
+/* */
+/* REQUIRED PROGRAMS: */
+/* ------------------ */
+/* */
+/* IBM C Compiler */
+/* IBM Linker */
+/* */
+/***********************************************************************/
+
+/***********************************************************************/
+/* */
+/* Include relevant MariaDB header file. */
+/* */
+/***********************************************************************/
+#include "my_global.h"
+#if defined(WIN32)
+//#include <windows.h>
+#else
+#if defined(UNIX) || defined(UNIV_LINUX)
+#include <errno.h>
+#include <unistd.h>
+//#define __stdcall
+#else
+#include <dir.h>
+#endif
+#include <stdarg.h>
+#endif
+
+#if defined(WIN)
+#include <alloc.h>
+#endif
+#include <errno.h> /* definitions of ERANGE ENOMEM */
+#if !defined(UNIX) && !defined(UNIV_LINUX)
+#include <direct.h> /* Directory management library */
+#endif
+
+/***********************************************************************/
+/* */
+/* Include application header files */
+/* */
+/* global.h is header containing all global declarations. */
+/* */
+/***********************************************************************/
+#define STORAGE /* Initialize global variables */
+
+#include "osutil.h"
+#include "global.h"
+
+#if defined(WIN32)
+extern HINSTANCE s_hModule; /* Saved module handle */
+#endif // WIN32
+
+extern int trace;
+
+#if defined(XMSG)
+extern char msglang[];
+#endif // XMSG
+
+/***********************************************************************/
+/* Local Definitions and static variables */
+/***********************************************************************/
+typedef struct {
+ ushort Segsize;
+ ushort Size;
+ } AREASIZE;
+
+ACTIVITY defActivity = { /* Describes activity and language */
+ NULL, /* Points to user work area(s) */
+ "Unknown"}; /* Application name */
+
+#if defined(XMSG) || defined(NEWMSG)
+ static char stmsg[200];
+#endif // XMSG || NEWMSG
+
+#if defined(UNIX) || defined(UNIV_LINUX)
+#include "rcmsg.h"
+#endif // UNIX
+
+/**************************************************************************/
+/* Tracing output function. */
+/**************************************************************************/
+void htrc(char const *fmt, ...)
+ {
+ va_list ap;
+ va_start (ap, fmt);
+
+//if (trace == 0 || (trace == 1 && !debug) || !fmt) {
+// printf("In %s wrong trace=%d debug=%p fmt=%p\n",
+// __FILE__, trace, debug, fmt);
+// trace = 0;
+// } // endif trace
+
+//if (trace == 1)
+// vfprintf(debug, fmt, ap);
+//else
+ vfprintf(stderr, fmt, ap);
+
+ va_end (ap);
+ } // end of htrc
+
+/***********************************************************************/
+/* Plug initialization routine. */
+/* Language points on initial language name and eventual path. */
+/* Return value is the pointer to the Global structure. */
+/***********************************************************************/
+PGLOBAL PlugInit(LPCSTR Language, uint worksize)
+ {
+ PGLOBAL g;
+
+ if (trace > 1)
+ htrc("PlugInit: Language='%s'\n",
+ ((!Language) ? "Null" : (char*)Language));
+
+ if (!(g = malloc(sizeof(GLOBAL)))) {
+ fprintf(stderr, MSG(GLOBAL_ERROR), (int)sizeof(GLOBAL));
+ return NULL;
+ } else {
+ g->Sarea_Size = worksize;
+ g->Trace = 0;
+ g->Createas = 0;
+ g->Alchecked = 0;
+ g->Mrr = 0;
+ g->Activityp = g->ActivityStart = NULL;
+ g->Xchk = NULL;
+ strcpy(g->Message, "");
+
+ /*******************************************************************/
+ /* Allocate the main work segment. */
+ /*******************************************************************/
+ if (!(g->Sarea = PlugAllocMem(g, worksize))) {
+ char errmsg[256];
+ sprintf(errmsg, MSG(WORK_AREA), g->Message);
+ strcpy(g->Message, errmsg);
+ } /* endif Sarea */
+
+ } /* endif g */
+
+ g->jump_level = -1; /* New setting to allow recursive call of Plug */
+ return(g);
+ } /* end of PlugInit */
+
+/***********************************************************************/
+/* PlugExit: Terminate Plug operations. */
+/***********************************************************************/
+int PlugExit(PGLOBAL g)
+ {
+ int rc = 0;
+
+ if (!g)
+ return rc;
+
+ if (g->Sarea)
+ free(g->Sarea);
+
+ free(g);
+ return rc;
+ } /* end of PlugExit */
+
+/***********************************************************************/
+/* Remove the file type from a file name. */
+/* Note: this routine is not really implemented for Unix. */
+/***********************************************************************/
+LPSTR PlugRemoveType(LPSTR pBuff, LPCSTR FileName)
+ {
+#if !defined(UNIX) && !defined(UNIV_LINUX)
+ char drive[_MAX_DRIVE];
+#else
+ char *drive = NULL;
+#endif
+ char direc[_MAX_DIR];
+ char fname[_MAX_FNAME];
+ char ftype[_MAX_EXT];
+
+ _splitpath(FileName, drive, direc, fname, ftype);
+
+ if (trace > 1) {
+ htrc("after _splitpath: FileName=%s\n", FileName);
+ htrc("drive=%s dir=%s fname=%s ext=%s\n",
+ SVP(drive), direc, fname, ftype);
+ } // endif trace
+
+ _makepath(pBuff, drive, direc, fname, "");
+
+ if (trace > 1)
+ htrc("buff='%s'\n", pBuff);
+
+ return pBuff;
+ } // end of PlugRemoveType
+
+
+BOOL PlugIsAbsolutePath(LPCSTR path)
+{
+#if defined(WIN32)
+ return ((path[0] >= 'a' && path[0] <= 'z') ||
+ (path[0] >= 'A' && path[0] <= 'Z')) && path[1] == ':';
+#else
+ return path[0] == '/';
+#endif
+}
+
+
+/***********************************************************************/
+/* Set the full path of a file relatively to a given path. */
+/* Note: this routine is not really implemented for Unix. */
+/***********************************************************************/
+LPCSTR PlugSetPath(LPSTR pBuff, LPCSTR prefix, LPCSTR FileName, LPCSTR defpath)
+ {
+ char newname[_MAX_PATH];
+ char direc[_MAX_DIR], defdir[_MAX_DIR];
+ char fname[_MAX_FNAME];
+ char ftype[_MAX_EXT];
+#if !defined(UNIX) && !defined(UNIV_LINUX)
+ char drive[_MAX_DRIVE], defdrv[_MAX_DRIVE];
+#else
+ char *drive = NULL, *defdrv = NULL;
+#endif
+
+ if (!strncmp(FileName, "//", 2) || !strncmp(FileName, "\\\\", 2)) {
+ strcpy(pBuff, FileName); // Remote file
+ return pBuff;
+ } // endif
+
+ if (PlugIsAbsolutePath(FileName))
+ {
+ strcpy(pBuff, FileName); // FileName includes absolute path
+ return pBuff;
+ } // endif
+
+ if (strcmp(prefix, ".") && !PlugIsAbsolutePath(defpath))
+ {
+ char tmp[_MAX_PATH];
+ int len= snprintf(tmp, sizeof(tmp) - 1, "%s%s%s",
+ prefix, defpath, FileName);
+ memcpy(pBuff, tmp, (size_t) len);
+ pBuff[len]= '\0';
+ return pBuff;
+ }
+
+ _splitpath(FileName, drive, direc, fname, ftype);
+ _splitpath(defpath, defdrv, defdir, NULL, NULL);
+
+ if (trace > 1) {
+ htrc("after _splitpath: FileName=%s\n", FileName);
+#if defined(UNIX) || defined(UNIV_LINUX)
+ htrc("dir=%s fname=%s ext=%s\n", direc, fname, ftype);
+#else
+ htrc("drive=%s dir=%s fname=%s ext=%s\n", drive, direc, fname, ftype);
+ htrc("defdrv=%s defdir=%s\n", defdrv, defdir);
+#endif
+ } // endif trace
+
+ if (drive && !*drive)
+ strcpy(drive, defdrv);
+
+ switch (*direc) {
+ case '\0':
+ strcpy(direc, defdir);
+ break;
+ case '\\':
+ case '/':
+ break;
+ default:
+ // This supposes that defdir ends with a SLASH
+ strcpy(direc, strcat(defdir, direc));
+ } // endswitch
+
+ _makepath(newname, drive, direc, fname, ftype);
+
+ if (trace > 1)
+ htrc("newname='%s'\n", newname);
+
+ if (_fullpath(pBuff, newname, _MAX_PATH)) {
+ if (trace > 1)
+ htrc("pbuff='%s'\n", pBuff);
+
+ return pBuff;
+ } else
+ return FileName; // Error, return unchanged name
+
+ } // end of PlugSetPath
+
+#if defined(XMSG)
+/***********************************************************************/
+/* PlugGetMessage: get a message from the message file. */
+/***********************************************************************/
+char *PlugReadMessage(PGLOBAL g, int mid, char *m)
+ {
+ char msgfile[_MAX_PATH], msgid[32], buff[256];
+ char *msg;
+ FILE *mfile = NULL;
+
+ GetPrivateProfileString("Message", msglang, "Message\\english.msg",
+ msgfile, _MAX_PATH, plgini);
+
+ if (!(mfile = fopen(msgfile, "rt"))) {
+ sprintf(stmsg, "Fail to open message file %s for %s", msgfile, msglang);
+ goto err;
+ } // endif mfile
+
+ for (;;)
+ if (!fgets(buff, 256, mfile)) {
+ sprintf(stmsg, "Cannot get message %d %s", mid, SVP(m));
+ goto fin;
+ } else
+ if (atoi(buff) == mid)
+ break;
+
+ if (sscanf(buff, " %*d %s \"%[^\"]", msgid, stmsg) < 2) {
+ // Old message file
+ if (!sscanf(buff, " %*d \"%[^\"]", stmsg)) {
+ sprintf(stmsg, "Bad message file for %d %s", mid, SVP(m));
+ goto fin;
+ } else
+ m = NULL;
+
+ } // endif sscanf
+
+ if (m && strcmp(m, msgid)) {
+ // Message file is out of date
+ strcpy(stmsg, m);
+ goto fin;
+ } // endif m
+
+ fin:
+ fclose(mfile);
+
+ err:
+ if (g) {
+ // Called by STEP
+ msg = (char *)PlugSubAlloc(g, NULL, strlen(stmsg) + 1);
+ strcpy(msg, stmsg);
+ } else // Called by MSG or PlgGetErrorMsg
+ msg = stmsg;
+
+ return msg;
+ } // end of PlugReadMessage
+
+#elif defined(NEWMSG)
+/***********************************************************************/
+/* PlugGetMessage: get a message from the resource string table. */
+/***********************************************************************/
+char *PlugGetMessage(PGLOBAL g, int mid)
+ {
+ char *msg;
+
+#if !defined(UNIX) && !defined(UNIV_LINUX)
+ int n = LoadString(s_hModule, (uint)mid, (LPTSTR)stmsg, 200);
+
+ if (n == 0) {
+ DWORD rc = GetLastError();
+ msg = (char*)PlugSubAlloc(g, NULL, 512); // Extend buf allocation
+ n = sprintf(msg, "Message %d, rc=%d: ", mid, rc);
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
+ (LPTSTR)(msg + n), 512 - n, NULL);
+ return msg;
+ } // endif n
+
+#else // UNIX
+ if (!GetRcString(mid, stmsg, 200))
+ sprintf(stmsg, "Message %d not found", mid);
+#endif // UNIX
+
+ if (g) {
+ // Called by STEP
+ msg = (char *)PlugSubAlloc(g, NULL, strlen(stmsg) + 1);
+ strcpy(msg, stmsg);
+ } else // Called by MSG or PlgGetErrorMsg
+ msg = stmsg;
+
+ return msg;
+ } // end of PlugGetMessage
+#endif // NEWMSG
+
+#if defined(WIN32)
+/***********************************************************************/
+/* Return the line length of the console screen buffer. */
+/***********************************************************************/
+short GetLineLength(PGLOBAL g)
+ {
+ CONSOLE_SCREEN_BUFFER_INFO coninfo;
+ HANDLE hcons = GetStdHandle(STD_OUTPUT_HANDLE);
+ BOOL b = GetConsoleScreenBufferInfo(hcons, &coninfo);
+
+ return (b) ? coninfo.dwSize.X : 0;
+ } // end of GetLineLength
+#endif // WIN32
+
+/***********************************************************************/
+/* Program for memory allocation of work and language areas. */
+/***********************************************************************/
+void *PlugAllocMem(PGLOBAL g, uint size)
+ {
+ void *areap; /* Pointer to allocated area */
+
+ /*********************************************************************/
+ /* This is the allocation routine for the WIN32/UNIX/AIX version. */
+ /*********************************************************************/
+ if (!(areap = malloc(size)))
+ sprintf(g->Message, MSG(MALLOC_ERROR), "malloc");
+
+ if (trace > 1) {
+ if (areap)
+ htrc("Memory of %u allocated at %p\n", size, areap);
+ else
+ htrc("PlugAllocMem: %s\n", g->Message);
+
+ } // endif trace
+
+ return (areap);
+ } /* end of PlugAllocMem */
+
+/***********************************************************************/
+/* Program for SubSet initialization of memory pools. */
+/* Here there should be some verification done such as validity of */
+/* the address and size not larger than memory size. */
+/***********************************************************************/
+BOOL PlugSubSet(PGLOBAL g __attribute__((unused)), void *memp, uint size)
+ {
+ PPOOLHEADER pph = memp;
+
+ pph->To_Free = (OFFSET)sizeof(POOLHEADER);
+ pph->FreeBlk = size - pph->To_Free;
+
+ return FALSE;
+ } /* end of PlugSubSet */
+
+/***********************************************************************/
+/* Program for sub-allocating one item in a storage area. */
+/* Note: SubAlloc routines of OS/2 are no more used to increase the */
+/* code portability and avoid problems when a grammar compiled under */
+/* one version of OS/2 is used under another version. */
+/* The simple way things are done here is also based on the fact */
+/* that no freeing of suballocated blocks is permitted in Plug. */
+/***********************************************************************/
+void *PlugSubAlloc(PGLOBAL g, void *memp, size_t size)
+ {
+ PPOOLHEADER pph; /* Points on area header. */
+
+ if (!memp)
+ /*******************************************************************/
+ /* Allocation is to be done in the Sarea. */
+ /*******************************************************************/
+ memp = g->Sarea;
+
+//size = ((size + 3) / 4) * 4; /* Round up size to multiple of 4 */
+ size = ((size + 7) / 8) * 8; /* Round up size to multiple of 8 */
+ pph = (PPOOLHEADER)memp;
+
+#if defined(DEBUG2) || defined(DEBUG3)
+ htrc("SubAlloc in %p size=%d used=%d free=%d\n",
+ memp, size, pph->To_Free, pph->FreeBlk);
+#endif
+
+ if ((uint)size > pph->FreeBlk) { /* Not enough memory left in pool */
+ char *pname = "Work";
+
+ sprintf(g->Message,
+ "Not enough memory in %s area for request of %u (used=%d free=%d)",
+ pname, (uint) size, pph->To_Free, pph->FreeBlk);
+
+#if defined(DEBUG2) || defined(DEBUG3)
+ htrc("%s\n", g->Message);
+#endif
+
+ longjmp(g->jumper[g->jump_level], 1);
+ } /* endif size OS32 code */
+
+ /*********************************************************************/
+ /* Do the suballocation the simplest way. */
+ /*********************************************************************/
+ memp = MakePtr(memp, pph->To_Free); /* Points to suballocated block */
+ pph->To_Free += size; /* New offset of pool free block */
+ pph->FreeBlk -= size; /* New size of pool free block */
+#if defined(DEBUG2) || defined(DEBUG3)
+ htrc("Done memp=%p used=%d free=%d\n",
+ memp, pph->To_Free, pph->FreeBlk);
+#endif
+ return (memp);
+ } /* end of PlugSubAlloc */
+
+/***********************************************************************/
+/* This routine suballocate a copy of the passed string. */
+/***********************************************************************/
+char *PlugDup(PGLOBAL g, const char *str)
+ {
+ char *buf;
+ size_t len;
+
+ if (str && (len = strlen(str))) {
+ buf = (char*)PlugSubAlloc(g, NULL, len + 1);
+ strcpy(buf, str);
+ } else
+ buf = NULL;
+
+ return(buf);
+ } /* end of PlugDup */
+
+/***********************************************************************/
+/* This routine makes a pointer from an offset to a memory pointer. */
+/***********************************************************************/
+void *MakePtr(void *memp, OFFSET offset)
+ {
+ return ((offset == 0) ? NULL : &((char *)memp)[offset]);
+ } /* end of MakePtr */
+
+/***********************************************************************/
+/* This routine makes an offset from a pointer new format. */
+/***********************************************************************/
+#if 0
+OFFSET MakeOff(void *memp, void *ptr)
+ {
+ return ((!ptr) ? 0 : (OFFSET)((char *)ptr - (char *)memp));
+ } /* end of MakeOff */
+#endif
+/*--------------------- End of PLUGUTIL program -----------------------*/
diff --git a/storage/connect/reldef.cpp b/storage/connect/reldef.cpp
index 38a6fd2eb11..63cb2034e53 100644
--- a/storage/connect/reldef.cpp
+++ b/storage/connect/reldef.cpp
@@ -1,436 +1,432 @@
-/************* RelDef CPP Program Source Code File (.CPP) **************/
-/* PROGRAM NAME: REFDEF */
-/* ------------- */
-/* Version 1.4 */
-/* */
-/* COPYRIGHT: */
-/* ---------- */
-/* (C) Copyright to the author Olivier BERTRAND 2004-2014 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are the DB definition related routines. */
-/* */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant MariaDB header file. */
-/***********************************************************************/
-#include "my_global.h"
-#if defined(WIN32)
-#include <sqlext.h>
-#else
-#include <dlfcn.h> // dlopen(), dlclose(), dlsym() ...
-#include "osutil.h"
-//#include "sqlext.h"
-#endif
-
-/***********************************************************************/
-/* Include application header files */
-/* */
-/* global.h is header containing all global declarations. */
-/* plgdbsem.h is header containing DB application declarations. */
-/* catalog.h is header containing DB description declarations. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "mycat.h"
-#include "reldef.h"
-#include "colblk.h"
-#include "filamap.h"
-#include "filamfix.h"
-#include "filamvct.h"
-#if defined(ZIP_SUPPORT)
-#include "filamzip.h"
-#endif // ZIP_SUPPORT
-#include "tabdos.h"
-#include "valblk.h"
-#include "tabmul.h"
-
-/***********************************************************************/
-/* External static variables. */
-/***********************************************************************/
-//extern "C" char plgini[];
-
-/* --------------------------- Class RELDEF -------------------------- */
-
-/***********************************************************************/
-/* RELDEF Constructor. */
-/***********************************************************************/
-RELDEF::RELDEF(void)
- {
- Next = NULL;
- To_Cols = NULL;
- Name = NULL;
- Database = NULL;
- Cat = NULL;
- } // end of RELDEF constructor
-
-/* --------------------------- Class TABDEF -------------------------- */
-
-/***********************************************************************/
-/* TABDEF Constructor. */
-/***********************************************************************/
-TABDEF::TABDEF(void)
- {
- Schema = NULL;
- Desc = NULL;
- Catfunc = FNC_NO;
- Card = 0;
- Elemt = 0;
- Sort = 0;
- Multiple = 0;
- Degree = 0;
- Pseudo = 0;
- Read_Only = false;
- } // end of TABDEF constructor
-
-/***********************************************************************/
-/* Define: initialize the table definition block from XDB file. */
-/***********************************************************************/
-bool TABDEF::Define(PGLOBAL g, PCATLG cat, LPCSTR name, LPCSTR am)
- {
- int poff = 0;
-
- Name = (PSZ)PlugSubAlloc(g, NULL, strlen(name) + 1);
- strcpy(Name, name);
- Cat = cat;
- Catfunc = GetFuncID(Cat->GetStringCatInfo(g, "Catfunc", NULL));
- Elemt = cat->GetIntCatInfo("Elements", 0);
- Multiple = cat->GetIntCatInfo("Multiple", 0);
- Degree = cat->GetIntCatInfo("Degree", 0);
- Read_Only = cat->GetBoolCatInfo("ReadOnly", false);
- const char *data_charset_name= cat->GetStringCatInfo(g, "Data_charset", NULL);
- m_data_charset= data_charset_name ?
- get_charset_by_csname(data_charset_name, MY_CS_PRIMARY, 0):
- NULL;
-
- // Get The column definitions
- if ((poff = cat->GetColCatInfo(g, this)) < 0)
- return true;
-
- // Do the definition of AM specific fields
- return DefineAM(g, am, poff);
- } // end of Define
-
-/* --------------------------- Class OEMDEF -------------------------- */
-
-/***********************************************************************/
-/* GetXdef: get the external TABDEF from OEM module. */
-/***********************************************************************/
-PTABDEF OEMDEF::GetXdef(PGLOBAL g)
- {
- typedef PTABDEF (__stdcall *XGETDEF) (PGLOBAL, void *);
- char c, getname[40] = "Get";
- PTABDEF xdefp;
- XGETDEF getdef = NULL;
- PCATLG cat = Cat;
-
-#if defined(WIN32)
- // Is the DLL already loaded?
- if (!Hdll && !(Hdll = GetModuleHandle(Module)))
- // No, load the Dll implementing the function
- if (!(Hdll = LoadLibrary(Module))) {
- char buf[256];
- DWORD rc = GetLastError();
-
- sprintf(g->Message, MSG(DLL_LOAD_ERROR), rc, Module);
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
- (LPTSTR)buf, sizeof(buf), NULL);
- strcat(strcat(g->Message, ": "), buf);
- return NULL;
- } // endif hDll
-
- // The exported name is always in uppercase
- for (int i = 0; ; i++) {
- c = Subtype[i];
- getname[i + 3] = toupper(c);
- if (!c) break;
- } // endfor i
-
- // Get the function returning an instance of the external DEF class
- if (!(getdef = (XGETDEF)GetProcAddress((HINSTANCE)Hdll, getname))) {
- sprintf(g->Message, MSG(PROCADD_ERROR), GetLastError(), getname);
- FreeLibrary((HMODULE)Hdll);
- return NULL;
- } // endif getdef
-#else // !WIN32
- const char *error = NULL;
- // Is the library already loaded?
-// if (!Hdll && !(Hdll = ???))
- // Load the desired shared library
- if (!(Hdll = dlopen(Module, RTLD_LAZY))) {
- error = dlerror();
- sprintf(g->Message, MSG(SHARED_LIB_ERR), Module, SVP(error));
- return NULL;
- } // endif Hdll
-
- // The exported name is always in uppercase
- for (int i = 0; ; i++) {
- c = Subtype[i];
- getname[i + 3] = toupper(c);
- if (!c) break;
- } // endfor i
-
- // Get the function returning an instance of the external DEF class
- if (!(getdef = (XGETDEF)dlsym(Hdll, getname))) {
- error = dlerror();
- sprintf(g->Message, MSG(GET_FUNC_ERR), getname, SVP(error));
- dlclose(Hdll);
- return NULL;
- } // endif getdef
-#endif // !WIN32
-
- // Just in case the external Get function does not set error messages
- sprintf(g->Message, MSG(DEF_ALLOC_ERROR), Subtype);
-
- // Get the table definition block
- if (!(xdefp = getdef(g, NULL)))
- return NULL;
-
- // Have the external class do its complete definition
- if (!cat->Cbuf) {
- // Suballocate a temporary buffer for the entire column section
- cat->Cblen = cat->GetSizeCatInfo("Colsize", "8K");
- cat->Cbuf = (char*)PlugSubAlloc(g, NULL, cat->Cblen);
- } // endif Cbuf
-
- // Here "OEM" should be replace by a more useful value
- if (xdefp->Define(g, cat, Name, "OEM"))
- return NULL;
-
- // Ok, return external block
- return xdefp;
- } // end of GetXdef
-
-#if 0
-/***********************************************************************/
-/* DeleteTableFile: Delete an OEM table file if applicable. */
-/***********************************************************************/
-bool OEMDEF::DeleteTableFile(PGLOBAL g)
- {
- if (!Pxdef)
- Pxdef = GetXdef(g);
-
- return (Pxdef) ? Pxdef->DeleteTableFile(g) : true;
- } // end of DeleteTableFile
-#endif // 0
-
-/***********************************************************************/
-/* Define: initialize the table definition block from XDB file. */
-/***********************************************************************/
-bool OEMDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
- {
- Module = Cat->GetStringCatInfo(g, "Module", "");
- Subtype = Cat->GetStringCatInfo(g, "Subtype", Module);
-
- if (!*Module)
- Module = Subtype;
-
- Desc = (char*)PlugSubAlloc(g, NULL, strlen(Module)
- + strlen(Subtype) + 3);
- sprintf(Desc, "%s(%s)", Module, Subtype);
- return false;
- } // end of DefineAM
-
-/***********************************************************************/
-/* GetTable: makes a new Table Description Block. */
-/***********************************************************************/
-PTDB OEMDEF::GetTable(PGLOBAL g, MODE mode)
- {
- RECFM rfm;
- PTDBASE tdbp = NULL;
-
- // If define block not here yet, get it now
- if (!Pxdef && !(Pxdef = GetXdef(g)))
- return NULL; // Error
-
- /*********************************************************************/
- /* Allocate a TDB of the proper type. */
- /* Column blocks will be allocated only when needed. */
- /*********************************************************************/
- if (!(tdbp = (PTDBASE)Pxdef->GetTable(g, mode)))
- return NULL;
- else
- rfm = tdbp->GetFtype();
-
- if (rfm == RECFM_NAF)
- return tdbp;
- else if (rfm == RECFM_OEM) {
- if (Multiple)
- tdbp = new(g) TDBMUL(tdbp); // No block optimization yet
-
- return tdbp;
- } // endif OEM
-
- /*********************************************************************/
- /* The OEM table is based on a file type (currently DOS+ only) */
- /*********************************************************************/
- assert (rfm == RECFM_VAR || rfm == RECFM_FIX ||
- rfm == RECFM_BIN || rfm == RECFM_VCT);
-
- PTXF txfp = NULL;
- PDOSDEF defp = (PDOSDEF)Pxdef;
- bool map = defp->Mapped && mode != MODE_INSERT &&
- !(PlgGetUser(g)->UseTemp == TMP_FORCE &&
- (mode == MODE_UPDATE || mode == MODE_DELETE));
- int cmpr = defp->Compressed;
-
- /*********************************************************************/
- /* Allocate table and file processing class of the proper type. */
- /* Column blocks will be allocated only when needed. */
- /*********************************************************************/
- if (!((PTDBDOS)tdbp)->GetTxfp()) {
- if (cmpr) {
-#if defined(ZIP_SUPPORT)
- if (cmpr == 1)
- txfp = new(g) ZIPFAM(defp);
- else
- txfp = new(g) ZLBFAM(defp);
-#else // !ZIP_SUPPORT
- strcpy(g->Message, "Compress not supported");
- return NULL;
-#endif // !ZIP_SUPPORT
- } else if (rfm == RECFM_VAR) {
- if (map)
- txfp = new(g) MAPFAM(defp);
- else
- txfp = new(g) DOSFAM(defp);
-
- } else if (rfm == RECFM_FIX || rfm == RECFM_BIN) {
- if (map)
- txfp = new(g) MPXFAM(defp);
- else
- txfp = new(g) FIXFAM(defp);
-
- } else if (rfm == RECFM_VCT) {
- assert (Pxdef->GetDefType() == TYPE_AM_VCT);
-
- if (map)
- txfp = new(g) VCMFAM((PVCTDEF)defp);
- else
- txfp = new(g) VCTFAM((PVCTDEF)defp);
-
- } // endif's
-
- ((PTDBDOS)tdbp)->SetTxfp(txfp);
- } // endif Txfp
-
- if (Multiple)
- tdbp = new(g) TDBMUL(tdbp);
-
- return tdbp;
- } // end of GetTable
-
-/* --------------------------- Class COLCRT -------------------------- */
-
-/***********************************************************************/
-/* COLCRT Constructors. */
-/***********************************************************************/
-COLCRT::COLCRT(PSZ name)
- {
- Next = NULL;
- Name = name;
- Desc = NULL;
- Decode = NULL;
- Fmt = NULL;
- Offset = -1;
- Long = -1;
- Precision = -1;
- Freq = -1;
- Key = -1;
- Scale = -1;
- Opt = -1;
- DataType = '*';
- } // end of COLCRT constructor for table creation
-
-COLCRT::COLCRT(void)
- {
- Next = NULL;
- Name = NULL;
- Desc = NULL;
- Decode = NULL;
- Fmt = NULL;
- Offset = 0;
- Long = 0;
- Precision = 0;
- Freq = 0;
- Key = 0;
- Scale = 0;
- Opt = 0;
- DataType = '*';
- } // end of COLCRT constructor for table & view definition
-
-/* --------------------------- Class COLDEF -------------------------- */
-
-/***********************************************************************/
-/* COLDEF Constructor. */
-/***********************************************************************/
-COLDEF::COLDEF(void) : COLCRT()
- {
- To_Min = NULL;
- To_Max = NULL;
- To_Pos = NULL;
- Xdb2 = FALSE;
- To_Bmap = NULL;
- To_Dval = NULL;
- Ndv = 0;
- Nbm = 0;
- Buf_Type = TYPE_ERROR;
- Clen = 0;
- Poff = 0;
- memset(&F, 0, sizeof(FORMAT));
- Flags = 0;
- } // end of COLDEF constructor
-
-/***********************************************************************/
-/* Define: initialize a column definition from a COLINFO structure. */
-/***********************************************************************/
-int COLDEF::Define(PGLOBAL g, void *memp, PCOLINFO cfp, int poff)
- {
- Name = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Name) + 1);
- strcpy(Name, cfp->Name);
-
- if (!(cfp->Flags & U_SPECIAL)) {
- Poff = poff;
- Buf_Type = cfp->Type;
-
- if ((Clen = GetTypeSize(Buf_Type, cfp->Length)) <= 0) {
- sprintf(g->Message, MSG(BAD_COL_TYPE), GetTypeName(Buf_Type), Name);
- return -1;
- } // endswitch
-
- strcpy(F.Type, GetFormatType(Buf_Type));
- F.Length = cfp->Length;
- F.Prec = cfp->Scale;
- Offset = (cfp->Offset < 0) ? poff : cfp->Offset;
- Precision = cfp->Precision;
- Scale = cfp->Scale;
- Long = cfp->Length;
- Opt = cfp->Opt;
- Key = cfp->Key;
- Freq = cfp->Freq;
-
- if (cfp->Remark && *cfp->Remark) {
- Desc = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Remark) + 1);
- strcpy(Desc, cfp->Remark);
- } // endif Remark
-
- if (cfp->Datefmt) {
- Decode = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Datefmt) + 1);
- strcpy(Decode, cfp->Datefmt);
- } // endif Datefmt
-
- } // endif special
-
- if (cfp->Fieldfmt) {
- Fmt = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Fieldfmt) + 1);
- strcpy(Fmt, cfp->Fieldfmt);
- } // endif Fieldfmt
-
- Flags = cfp->Flags;
- return (Flags & (U_VIRTUAL|U_SPECIAL)) ? 0 : Long;
- } // end of Define
-
-/* ------------------------- End of RelDef --------------------------- */
+/************* RelDef CPP Program Source Code File (.CPP) **************/
+/* PROGRAM NAME: REFDEF */
+/* ------------- */
+/* Version 1.4 */
+/* */
+/* COPYRIGHT: */
+/* ---------- */
+/* (C) Copyright to the author Olivier BERTRAND 2004-2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are the DB definition related routines. */
+/* */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant MariaDB header file. */
+/***********************************************************************/
+#include "my_global.h"
+#if defined(WIN32)
+#include <sqlext.h>
+#else
+#include <dlfcn.h> // dlopen(), dlclose(), dlsym() ...
+#include "osutil.h"
+//#include "sqlext.h"
+#endif
+
+/***********************************************************************/
+/* Include application header files */
+/* */
+/* global.h is header containing all global declarations. */
+/* plgdbsem.h is header containing DB application declarations. */
+/* catalog.h is header containing DB description declarations. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "mycat.h"
+#include "reldef.h"
+#include "colblk.h"
+#include "filamap.h"
+#include "filamfix.h"
+#include "filamvct.h"
+#if defined(ZIP_SUPPORT)
+#include "filamzip.h"
+#endif // ZIP_SUPPORT
+#include "tabdos.h"
+#include "valblk.h"
+#include "tabmul.h"
+
+
+/* --------------------------- Class RELDEF -------------------------- */
+
+/***********************************************************************/
+/* RELDEF Constructor. */
+/***********************************************************************/
+RELDEF::RELDEF(void)
+ {
+ Next = NULL;
+ To_Cols = NULL;
+ Name = NULL;
+ Database = NULL;
+ Cat = NULL;
+ } // end of RELDEF constructor
+
+/* --------------------------- Class TABDEF -------------------------- */
+
+/***********************************************************************/
+/* TABDEF Constructor. */
+/***********************************************************************/
+TABDEF::TABDEF(void)
+ {
+ Schema = NULL;
+ Desc = NULL;
+ Catfunc = FNC_NO;
+ Card = 0;
+ Elemt = 0;
+ Sort = 0;
+ Multiple = 0;
+ Degree = 0;
+ Pseudo = 0;
+ Read_Only = false;
+ } // end of TABDEF constructor
+
+/***********************************************************************/
+/* Define: initialize the table definition block from XDB file. */
+/***********************************************************************/
+bool TABDEF::Define(PGLOBAL g, PCATLG cat, LPCSTR name, LPCSTR am)
+ {
+ int poff = 0;
+
+ Name = (PSZ)PlugSubAlloc(g, NULL, strlen(name) + 1);
+ strcpy(Name, name);
+ Cat = cat;
+ Catfunc = GetFuncID(Cat->GetStringCatInfo(g, "Catfunc", NULL));
+ Elemt = cat->GetIntCatInfo("Elements", 0);
+ Multiple = cat->GetIntCatInfo("Multiple", 0);
+ Degree = cat->GetIntCatInfo("Degree", 0);
+ Read_Only = cat->GetBoolCatInfo("ReadOnly", false);
+ const char *data_charset_name= cat->GetStringCatInfo(g, "Data_charset", NULL);
+ m_data_charset= data_charset_name ?
+ get_charset_by_csname(data_charset_name, MY_CS_PRIMARY, 0):
+ NULL;
+
+ // Get The column definitions
+ if ((poff = cat->GetColCatInfo(g, this)) < 0)
+ return true;
+
+ // Do the definition of AM specific fields
+ return DefineAM(g, am, poff);
+ } // end of Define
+
+/* --------------------------- Class OEMDEF -------------------------- */
+
+/***********************************************************************/
+/* GetXdef: get the external TABDEF from OEM module. */
+/***********************************************************************/
+PTABDEF OEMDEF::GetXdef(PGLOBAL g)
+ {
+ typedef PTABDEF (__stdcall *XGETDEF) (PGLOBAL, void *);
+ char c, getname[40] = "Get";
+ PTABDEF xdefp;
+ XGETDEF getdef = NULL;
+ PCATLG cat = Cat;
+
+#if defined(WIN32)
+ // Is the DLL already loaded?
+ if (!Hdll && !(Hdll = GetModuleHandle(Module)))
+ // No, load the Dll implementing the function
+ if (!(Hdll = LoadLibrary(Module))) {
+ char buf[256];
+ DWORD rc = GetLastError();
+
+ sprintf(g->Message, MSG(DLL_LOAD_ERROR), rc, Module);
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
+ (LPTSTR)buf, sizeof(buf), NULL);
+ strcat(strcat(g->Message, ": "), buf);
+ return NULL;
+ } // endif hDll
+
+ // The exported name is always in uppercase
+ for (int i = 0; ; i++) {
+ c = Subtype[i];
+ getname[i + 3] = toupper(c);
+ if (!c) break;
+ } // endfor i
+
+ // Get the function returning an instance of the external DEF class
+ if (!(getdef = (XGETDEF)GetProcAddress((HINSTANCE)Hdll, getname))) {
+ sprintf(g->Message, MSG(PROCADD_ERROR), GetLastError(), getname);
+ FreeLibrary((HMODULE)Hdll);
+ return NULL;
+ } // endif getdef
+#else // !WIN32
+ const char *error = NULL;
+ // Is the library already loaded?
+// if (!Hdll && !(Hdll = ???))
+ // Load the desired shared library
+ if (!(Hdll = dlopen(Module, RTLD_LAZY))) {
+ error = dlerror();
+ sprintf(g->Message, MSG(SHARED_LIB_ERR), Module, SVP(error));
+ return NULL;
+ } // endif Hdll
+
+ // The exported name is always in uppercase
+ for (int i = 0; ; i++) {
+ c = Subtype[i];
+ getname[i + 3] = toupper(c);
+ if (!c) break;
+ } // endfor i
+
+ // Get the function returning an instance of the external DEF class
+ if (!(getdef = (XGETDEF)dlsym(Hdll, getname))) {
+ error = dlerror();
+ sprintf(g->Message, MSG(GET_FUNC_ERR), getname, SVP(error));
+ dlclose(Hdll);
+ return NULL;
+ } // endif getdef
+#endif // !WIN32
+
+ // Just in case the external Get function does not set error messages
+ sprintf(g->Message, MSG(DEF_ALLOC_ERROR), Subtype);
+
+ // Get the table definition block
+ if (!(xdefp = getdef(g, NULL)))
+ return NULL;
+
+ // Have the external class do its complete definition
+ if (!cat->Cbuf) {
+ // Suballocate a temporary buffer for the entire column section
+ cat->Cblen = cat->GetSizeCatInfo("Colsize", "8K");
+ cat->Cbuf = (char*)PlugSubAlloc(g, NULL, cat->Cblen);
+ } // endif Cbuf
+
+ // Here "OEM" should be replace by a more useful value
+ if (xdefp->Define(g, cat, Name, "OEM"))
+ return NULL;
+
+ // Ok, return external block
+ return xdefp;
+ } // end of GetXdef
+
+#if 0
+/***********************************************************************/
+/* DeleteTableFile: Delete an OEM table file if applicable. */
+/***********************************************************************/
+bool OEMDEF::DeleteTableFile(PGLOBAL g)
+ {
+ if (!Pxdef)
+ Pxdef = GetXdef(g);
+
+ return (Pxdef) ? Pxdef->DeleteTableFile(g) : true;
+ } // end of DeleteTableFile
+#endif // 0
+
+/***********************************************************************/
+/* Define: initialize the table definition block from XDB file. */
+/***********************************************************************/
+bool OEMDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
+ {
+ Module = Cat->GetStringCatInfo(g, "Module", "");
+ Subtype = Cat->GetStringCatInfo(g, "Subtype", Module);
+
+ if (!*Module)
+ Module = Subtype;
+
+ Desc = (char*)PlugSubAlloc(g, NULL, strlen(Module)
+ + strlen(Subtype) + 3);
+ sprintf(Desc, "%s(%s)", Module, Subtype);
+ return false;
+ } // end of DefineAM
+
+/***********************************************************************/
+/* GetTable: makes a new Table Description Block. */
+/***********************************************************************/
+PTDB OEMDEF::GetTable(PGLOBAL g, MODE mode)
+ {
+ RECFM rfm;
+ PTDBASE tdbp = NULL;
+
+ // If define block not here yet, get it now
+ if (!Pxdef && !(Pxdef = GetXdef(g)))
+ return NULL; // Error
+
+ /*********************************************************************/
+ /* Allocate a TDB of the proper type. */
+ /* Column blocks will be allocated only when needed. */
+ /*********************************************************************/
+ if (!(tdbp = (PTDBASE)Pxdef->GetTable(g, mode)))
+ return NULL;
+ else
+ rfm = tdbp->GetFtype();
+
+ if (rfm == RECFM_NAF)
+ return tdbp;
+ else if (rfm == RECFM_OEM) {
+ if (Multiple)
+ tdbp = new(g) TDBMUL(tdbp); // No block optimization yet
+
+ return tdbp;
+ } // endif OEM
+
+ /*********************************************************************/
+ /* The OEM table is based on a file type (currently DOS+ only) */
+ /*********************************************************************/
+ assert (rfm == RECFM_VAR || rfm == RECFM_FIX ||
+ rfm == RECFM_BIN || rfm == RECFM_VCT);
+
+ PTXF txfp = NULL;
+ PDOSDEF defp = (PDOSDEF)Pxdef;
+ bool map = defp->Mapped && mode != MODE_INSERT &&
+ !(PlgGetUser(g)->UseTemp == TMP_FORCE &&
+ (mode == MODE_UPDATE || mode == MODE_DELETE));
+ int cmpr = defp->Compressed;
+
+ /*********************************************************************/
+ /* Allocate table and file processing class of the proper type. */
+ /* Column blocks will be allocated only when needed. */
+ /*********************************************************************/
+ if (!((PTDBDOS)tdbp)->GetTxfp()) {
+ if (cmpr) {
+#if defined(ZIP_SUPPORT)
+ if (cmpr == 1)
+ txfp = new(g) ZIPFAM(defp);
+ else
+ txfp = new(g) ZLBFAM(defp);
+#else // !ZIP_SUPPORT
+ strcpy(g->Message, "Compress not supported");
+ return NULL;
+#endif // !ZIP_SUPPORT
+ } else if (rfm == RECFM_VAR) {
+ if (map)
+ txfp = new(g) MAPFAM(defp);
+ else
+ txfp = new(g) DOSFAM(defp);
+
+ } else if (rfm == RECFM_FIX || rfm == RECFM_BIN) {
+ if (map)
+ txfp = new(g) MPXFAM(defp);
+ else
+ txfp = new(g) FIXFAM(defp);
+
+ } else if (rfm == RECFM_VCT) {
+ assert (Pxdef->GetDefType() == TYPE_AM_VCT);
+
+ if (map)
+ txfp = new(g) VCMFAM((PVCTDEF)defp);
+ else
+ txfp = new(g) VCTFAM((PVCTDEF)defp);
+
+ } // endif's
+
+ ((PTDBDOS)tdbp)->SetTxfp(txfp);
+ } // endif Txfp
+
+ if (Multiple)
+ tdbp = new(g) TDBMUL(tdbp);
+
+ return tdbp;
+ } // end of GetTable
+
+/* --------------------------- Class COLCRT -------------------------- */
+
+/***********************************************************************/
+/* COLCRT Constructors. */
+/***********************************************************************/
+COLCRT::COLCRT(PSZ name)
+ {
+ Next = NULL;
+ Name = name;
+ Desc = NULL;
+ Decode = NULL;
+ Fmt = NULL;
+ Offset = -1;
+ Long = -1;
+ Precision = -1;
+ Freq = -1;
+ Key = -1;
+ Scale = -1;
+ Opt = -1;
+ DataType = '*';
+ } // end of COLCRT constructor for table creation
+
+COLCRT::COLCRT(void)
+ {
+ Next = NULL;
+ Name = NULL;
+ Desc = NULL;
+ Decode = NULL;
+ Fmt = NULL;
+ Offset = 0;
+ Long = 0;
+ Precision = 0;
+ Freq = 0;
+ Key = 0;
+ Scale = 0;
+ Opt = 0;
+ DataType = '*';
+ } // end of COLCRT constructor for table & view definition
+
+/* --------------------------- Class COLDEF -------------------------- */
+
+/***********************************************************************/
+/* COLDEF Constructor. */
+/***********************************************************************/
+COLDEF::COLDEF(void) : COLCRT()
+ {
+ To_Min = NULL;
+ To_Max = NULL;
+ To_Pos = NULL;
+ Xdb2 = FALSE;
+ To_Bmap = NULL;
+ To_Dval = NULL;
+ Ndv = 0;
+ Nbm = 0;
+ Buf_Type = TYPE_ERROR;
+ Clen = 0;
+ Poff = 0;
+ memset(&F, 0, sizeof(FORMAT));
+ Flags = 0;
+ } // end of COLDEF constructor
+
+/***********************************************************************/
+/* Define: initialize a column definition from a COLINFO structure. */
+/***********************************************************************/
+int COLDEF::Define(PGLOBAL g, void *memp, PCOLINFO cfp, int poff)
+ {
+ Name = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Name) + 1);
+ strcpy(Name, cfp->Name);
+
+ if (!(cfp->Flags & U_SPECIAL)) {
+ Poff = poff;
+ Buf_Type = cfp->Type;
+
+ if ((Clen = GetTypeSize(Buf_Type, cfp->Length)) <= 0) {
+ sprintf(g->Message, MSG(BAD_COL_TYPE), GetTypeName(Buf_Type), Name);
+ return -1;
+ } // endswitch
+
+ strcpy(F.Type, GetFormatType(Buf_Type));
+ F.Length = cfp->Length;
+ F.Prec = cfp->Scale;
+ Offset = (cfp->Offset < 0) ? poff : cfp->Offset;
+ Precision = cfp->Precision;
+ Scale = cfp->Scale;
+ Long = cfp->Length;
+ Opt = cfp->Opt;
+ Key = cfp->Key;
+ Freq = cfp->Freq;
+
+ if (cfp->Remark && *cfp->Remark) {
+ Desc = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Remark) + 1);
+ strcpy(Desc, cfp->Remark);
+ } // endif Remark
+
+ if (cfp->Datefmt) {
+ Decode = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Datefmt) + 1);
+ strcpy(Decode, cfp->Datefmt);
+ } // endif Datefmt
+
+ } // endif special
+
+ if (cfp->Fieldfmt) {
+ Fmt = (PSZ)PlugSubAlloc(g, memp, strlen(cfp->Fieldfmt) + 1);
+ strcpy(Fmt, cfp->Fieldfmt);
+ } // endif Fieldfmt
+
+ Flags = cfp->Flags;
+ return (Flags & (U_VIRTUAL|U_SPECIAL)) ? 0 : Long;
+ } // end of Define
+
+/* ------------------------- End of RelDef --------------------------- */
diff --git a/storage/connect/tabcol.cpp b/storage/connect/tabcol.cpp
index efd863a88cf..96ec4f45861 100644
--- a/storage/connect/tabcol.cpp
+++ b/storage/connect/tabcol.cpp
@@ -22,6 +22,8 @@
#include "xtable.h"
#include "tabcol.h"
+extern "C" int trace;
+
/***********************************************************************/
/* XTAB public constructor. */
/***********************************************************************/
@@ -33,9 +35,9 @@ XTAB::XTAB(LPCSTR name, LPCSTR srcdef) : Name(name)
Schema = NULL;
Qualifier = NULL;
-#ifdef DEBTRACE
- htrc(" making new TABLE %s %s\n", Name, Srcdef);
-#endif
+ if (trace)
+ htrc("XTAB: making new TABLE %s %s\n", Name, Srcdef);
+
} // end of XTAB constructor
/***********************************************************************/
@@ -49,9 +51,9 @@ XTAB::XTAB(PTABLE tp) : Name(tp->Name)
Schema = tp->Schema;
Qualifier = tp->Qualifier;
-#ifdef DEBTRACE
- htrc(" making copy TABLE %s %s\n", Name, Srcdef);
-#endif
+ if (trace)
+ htrc(" making copy TABLE %s %s\n", Name, Srcdef);
+
} // end of XTAB constructor
/***********************************************************************/
@@ -61,9 +63,8 @@ PTABLE XTAB::Link(PTABLE tab2)
{
PTABLE tabp;
-#ifdef DEBTRACE
- htrc("Linking tables %s... to %s\n", Name, tab2->Name);
-#endif
+ if (trace)
+ htrc("Linking tables %s... to %s\n", Name, tab2->Name);
for (tabp = this; tabp->Next; tabp = tabp->Next) ;
@@ -118,9 +119,9 @@ COLUMN::COLUMN(LPCSTR name) : Name(name)
To_Col = NULL;
Qualifier = NULL;
-#ifdef DEBTRACE
- htrc(" making new COLUMN %s\n", Name);
-#endif
+ if (trace)
+ htrc(" making new COLUMN %s\n", Name);
+
} // end of COLUMN constructor
/***********************************************************************/
diff --git a/storage/connect/tabfmt.cpp b/storage/connect/tabfmt.cpp
index 2c62806ff52..0fbba03dde8 100644
--- a/storage/connect/tabfmt.cpp
+++ b/storage/connect/tabfmt.cpp
@@ -1,1429 +1,1429 @@
-/************* TabFmt C++ Program Source Code File (.CPP) **************/
-/* PROGRAM NAME: TABFMT */
-/* ------------- */
-/* Version 3.9 */
-/* */
-/* COPYRIGHT: */
-/* ---------- */
-/* (C) Copyright to the author Olivier BERTRAND 2001 - 2014 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* This program are the TABFMT classes DB execution routines. */
-/* The base class CSV is comma separated files. */
-/* FMT (Formatted) files are those having a complex internal record */
-/* format described in the Format keyword of their definition. */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant MariaDB header file. */
-/***********************************************************************/
-#include "my_global.h"
-
-#if defined(WIN32)
-#include <io.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <locale.h>
-#if defined(__BORLANDC__)
-#define __MFC_COMPAT__ // To define min/max as macro
-#endif
-//#include <windows.h>
-#include "osutil.h"
-#else
-#if defined(UNIX)
-#include <errno.h>
-#include <unistd.h>
-#include "osutil.h"
-#else
-#include <io.h>
-#endif
-#include <fcntl.h>
-#endif
-
-/***********************************************************************/
-/* Include application header files: */
-/* global.h is header containing all global declarations. */
-/* plgdbsem.h is header containing the DB application declarations. */
-/* tabdos.h is header containing the TABDOS class declarations. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "mycat.h"
-#include "filamap.h"
-#if defined(ZIP_SUPPORT)
-#include "filamzip.h"
-#endif // ZIP_SUPPORT
-#include "tabfmt.h"
-#include "tabmul.h"
-#define NO_FUNC
-#include "plgcnx.h" // For DB types
-#include "resource.h"
-
-/***********************************************************************/
-/* This should be an option. */
-/***********************************************************************/
-#define MAXCOL 200 /* Default max column nb in result */
-#define TYPE_UNKNOWN 10 /* Must be greater than other types */
-
-extern "C" int trace;
-
-/***********************************************************************/
-/* CSVColumns: constructs the result blocks containing the description */
-/* of all the columns of a CSV file that will be retrieved by #GetData.*/
-/* Note: the algorithm to set the type is based on the internal values */
-/* of types (TYPE_STRING < TYPE_DOUBLE < TYPE_INT) (1 < 2 < 7). */
-/* If these values are changed, this will have to be revisited. */
-/***********************************************************************/
-PQRYRES CSVColumns(PGLOBAL g, const char *fn, char sep, char q,
- int hdr, int mxr, bool info)
- {
- static int buftyp[] = {TYPE_STRING, TYPE_SHORT, TYPE_STRING,
- TYPE_INT, TYPE_INT, TYPE_SHORT};
- static XFLD fldtyp[] = {FLD_NAME, FLD_TYPE, FLD_TYPENAME,
- FLD_PREC, FLD_LENGTH, FLD_SCALE};
- static unsigned int length[] = {6, 6, 8, 10, 10, 6};
- char *p, *colname[MAXCOL], dechar, filename[_MAX_PATH], buf[4096];
- int i, imax, hmax, n, nerr, phase, blank, digit, dec, type;
- int ncol = sizeof(buftyp) / sizeof(int);
- int num_read = 0, num_max = 10000000; // Statistics
- int len[MAXCOL], typ[MAXCOL], prc[MAXCOL];
- FILE *infile;
- PQRYRES qrp;
- PCOLRES crp;
-
- if (info) {
- imax = hmax = 0;
- length[0] = 128;
- goto skipit;
- } // endif info
-
-// num_max = atoi(p+1); // Max num of record to test
-#if defined(WIN32)
- if (sep == ',' || strnicmp(setlocale(LC_NUMERIC, NULL), "French", 6))
- dechar = '.';
- else
- dechar = ',';
-#else // !WIN32
- dechar = '.';
-#endif // !WIN32
-
- if (trace)
- htrc("File %s sep=%c q=%c hdr=%d mxr=%d\n",
- SVP(fn), sep, q, hdr, mxr);
-
- if (!fn) {
- strcpy(g->Message, MSG(MISSING_FNAME));
- return NULL;
- } // endif fn
-
- imax = hmax = nerr = 0;
- mxr = max(0, mxr);
-
- for (i = 0; i < MAXCOL; i++) {
- colname[i] = NULL;
- len[i] = 0;
- typ[i] = TYPE_UNKNOWN;
- prc[i] = 0;
- } // endfor i
-
- /*********************************************************************/
- /* Open the input file. */
- /*********************************************************************/
- PlugSetPath(filename, fn, PlgGetDataPath(g));
-
- if (!(infile= global_fopen(g, MSGID_CANNOT_OPEN, filename, "r")))
- return NULL;
-
- if (hdr) {
- /*******************************************************************/
- /* Make the column names from the first line. */
- /*******************************************************************/
- phase = 0;
-
- if (fgets(buf, sizeof(buf), infile)) {
- n = strlen(buf) + 1;
- buf[n - 2] = '\0';
-#if defined(UNIX)
- // The file can be imported from Windows
- if (buf[n - 3] == '\r')
- buf[n - 3] = 0;
-#endif // UNIX
- p = (char*)PlugSubAlloc(g, NULL, n);
- memcpy(p, buf, n);
-
- //skip leading blanks
- for (; *p == ' '; p++) ;
-
- if (q && *p == q) {
- // Header is quoted
- p++;
- phase = 1;
- } // endif q
-
- colname[0] = p;
- } else {
- sprintf(g->Message, MSG(FILE_IS_EMPTY), fn);
- goto err;
- } // endif's
-
- for (i = 1; *p; p++)
- if (phase == 1 && *p == q) {
- *p = '\0';
- phase = 0;
- } else if (*p == sep && !phase) {
- *p = '\0';
-
- //skip leading blanks
- for (; *(p+1) == ' '; p++) ;
-
- if (q && *(p+1) == q) {
- // Header is quoted
- p++;
- phase = 1;
- } // endif q
-
- colname[i++] = p + 1;
- } // endif sep
-
- num_read++;
- imax = hmax = i;
-
- for (i = 0; i < hmax; i++)
- length[0] = max(length[0], strlen(colname[i]));
-
- } // endif hdr
-
- for (num_read++; num_read <= num_max; num_read++) {
- /*******************************************************************/
- /* Now start the reading process. Read one line. */
- /*******************************************************************/
- if (fgets(buf, sizeof(buf), infile)) {
- n = strlen(buf);
- buf[n - 1] = '\0';
-#if defined(UNIX)
- // The file can be imported from Windows
- if (buf[n - 2] == '\r')
- buf[n - 2] = 0;
-#endif // UNIX
- } else if (feof(infile)) {
- sprintf(g->Message, MSG(EOF_AFTER_LINE), num_read -1);
- break;
- } else {
- sprintf(g->Message, MSG(ERR_READING_REC), num_read, fn);
- goto err;
- } // endif's
-
- /*******************************************************************/
- /* Make the test for field lengths. */
- /*******************************************************************/
- i = n = phase = blank = digit = dec = 0;
-
- for (p = buf; *p; p++)
- if (*p == sep) {
- if (phase != 1) {
- if (i == MAXCOL - 1) {
- sprintf(g->Message, MSG(TOO_MANY_FIELDS), num_read, fn);
- goto err;
- } // endif i
-
- if (n) {
- len[i] = max(len[i], n);
- type = (digit || (dec && n == 1)) ? TYPE_STRING
- : (dec) ? TYPE_DOUBLE : TYPE_INT;
- typ[i] = min(type, typ[i]);
- prc[i] = max((typ[i] == TYPE_DOUBLE) ? (dec - 1) : 0, prc[i]);
- } // endif n
-
- i++;
- n = phase = blank = digit = dec = 0;
- } else // phase == 1
- n++;
-
- } else if (*p == ' ') {
- if (phase < 2)
- n++;
-
- if (blank)
- digit = 1;
-
- } else if (*p == q) {
- if (phase == 0) {
- if (blank)
- if (++nerr > mxr) {
- sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
- goto err;
- } else
- goto skip;
-
- n = 0;
- phase = digit = 1;
- } else if (phase == 1) {
- if (*(p+1) == q) {
- // This is currently not implemented for CSV tables
-// if (++nerr > mxr) {
-// sprintf(g->Message, MSG(QUOTE_IN_QUOTE), num_read);
-// goto err;
-// } else
-// goto skip;
-
- p++;
- n++;
- } else
- phase = 2;
-
- } else if (++nerr > mxr) { // phase == 2
- sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
- goto err;
- } else
- goto skip;
-
- } else {
- if (phase == 2)
- if (++nerr > mxr) {
- sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
- goto err;
- } else
- goto skip;
-
- // isdigit cannot be used here because of debug assert
- if (!strchr("0123456789", *p)) {
- if (!digit && *p == dechar)
- dec = 1; // Decimal point found
- else if (blank || !(*p == '-' || *p == '+'))
- digit = 1;
-
- } else if (dec)
- dec++; // More decimals
-
- n++;
- blank = 1;
- } // endif's *p
-
- if (phase == 1)
- if (++nerr > mxr) {
- sprintf(g->Message, MSG(UNBALANCE_QUOTE), num_read);
- goto err;
- } else
- goto skip;
-
- if (n) {
- len[i] = max(len[i], n);
- type = (digit || n == 0 || (dec && n == 1)) ? TYPE_STRING
- : (dec) ? TYPE_DOUBLE : TYPE_INT;
- typ[i] = min(type, typ[i]);
- prc[i] = max((typ[i] == TYPE_DOUBLE) ? (dec - 1) : 0, prc[i]);
- } // endif n
-
- imax = max(imax, i+1);
- skip: ; // Skip erroneous line
- } // endfor num_read
-
- if (trace) {
- htrc("imax=%d Lengths:", imax);
-
- for (i = 0; i < imax; i++)
- htrc(" %d", len[i]);
-
- htrc("\n");
- } // endif trace
-
- fclose(infile);
-
- skipit:
- if (trace)
- htrc("CSVColumns: imax=%d hmax=%d len=%d\n",
- imax, hmax, length[0]);
-
- /*********************************************************************/
- /* Allocate the structures used to refer to the result set. */
- /*********************************************************************/
- qrp = PlgAllocResult(g, ncol, imax, IDS_COLUMNS + 3,
- buftyp, fldtyp, length, false, false);
- if (info || !qrp)
- return qrp;
-
- qrp->Nblin = imax;
-
- /*********************************************************************/
- /* Now get the results into blocks. */
- /*********************************************************************/
- for (i = 0; i < imax; i++) {
- if (i >= hmax) {
- sprintf(buf, "COL%.3d", i+1);
- p = buf;
- } else
- p = colname[i];
-
- if (typ[i] == TYPE_UNKNOWN) // Void column
- typ[i] = TYPE_STRING;
-
- crp = qrp->Colresp; // Column Name
- crp->Kdata->SetValue(p, i);
- crp = crp->Next; // Data Type
- crp->Kdata->SetValue(typ[i], i);
- crp = crp->Next; // Type Name
- crp->Kdata->SetValue(GetTypeName(typ[i]), i);
- crp = crp->Next; // Precision
- crp->Kdata->SetValue(len[i], i);
- crp = crp->Next; // Length
- crp->Kdata->SetValue(len[i], i);
- crp = crp->Next; // Scale (precision)
- crp->Kdata->SetValue(prc[i], i);
- } // endfor i
-
- /*********************************************************************/
- /* Return the result pointer for use by GetData routines. */
- /*********************************************************************/
- return qrp;
-
- err:
- fclose(infile);
- return NULL;
- } // end of CSVCColumns
-
-/* --------------------------- Class CSVDEF -------------------------- */
-
-/***********************************************************************/
-/* CSVDEF constructor. */
-/***********************************************************************/
-CSVDEF::CSVDEF(void)
- {
- Fmtd = Accept = Header = false;
- Maxerr = 0;
- Quoted = -1;
- Sep = ',';
- Qot = '\0';
- } // end of CSVDEF constructor
-
-/***********************************************************************/
-/* DefineAM: define specific AM block values from XDB file. */
-/***********************************************************************/
-bool CSVDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
- {
- char buf[8];
-
- // Double check correctness of offset values
- if (Catfunc == FNC_NO)
- for (PCOLDEF cdp = To_Cols; cdp; cdp = cdp->GetNext())
- if (cdp->GetOffset() < 1) {
- strcpy(g->Message, MSG(BAD_OFFSET_VAL));
- return true;
- } // endif Offset
-
- // Call DOSDEF DefineAM with am=CSV so FMT is not confused with FIX
- if (DOSDEF::DefineAM(g, "CSV", poff))
- return true;
-
- Cat->GetCharCatInfo("Separator", ",", buf, sizeof(buf));
- Sep = (strlen(buf) == 2 && buf[0] == '\\' && buf[1] == 't') ? '\t' : *buf;
- Quoted = Cat->GetIntCatInfo("Quoted", -1);
- Cat->GetCharCatInfo("Qchar", "", buf, sizeof(buf));
- Qot = *buf;
-
- if (Qot && Quoted < 0)
- Quoted = 0;
- else if (!Qot && Quoted >= 0)
- Qot = '"';
-
- Fmtd = (!Sep || (am && (*am == 'F' || *am == 'f')));
- Header = (Cat->GetIntCatInfo("Header", 0) != 0);
- Maxerr = Cat->GetIntCatInfo("Maxerr", 0);
- Accept = (Cat->GetIntCatInfo("Accept", 0) != 0);
- return false;
- } // end of DefineAM
-
-/***********************************************************************/
-/* GetTable: makes a new Table Description Block. */
-/***********************************************************************/
-PTDB CSVDEF::GetTable(PGLOBAL g, MODE mode)
- {
- PTDBASE tdbp;
-
- if (Catfunc != FNC_COL) {
- USETEMP tmp = PlgGetUser(g)->UseTemp;
- bool map = Mapped && mode != MODE_INSERT &&
- !(tmp != TMP_NO && mode == MODE_UPDATE) &&
- !(tmp == TMP_FORCE &&
- (mode == MODE_UPDATE || mode == MODE_DELETE));
- PTXF txfp;
-
- /*******************************************************************/
- /* Allocate a file processing class of the proper type. */
- /*******************************************************************/
- if (map) {
- // Should be now compatible with UNIX
- txfp = new(g) MAPFAM(this);
- } else if (Compressed) {
-#if defined(ZIP_SUPPORT)
- if (Compressed == 1)
- txfp = new(g) ZIPFAM(this);
- else
- txfp = new(g) ZLBFAM(this);
-
-#else // !ZIP_SUPPORT
- strcpy(g->Message, "Compress not supported");
- return NULL;
-#endif // !ZIP_SUPPORT
- } else
- txfp = new(g) DOSFAM(this);
-
- /*******************************************************************/
- /* Allocate a TDB of the proper type. */
- /* Column blocks will be allocated only when needed. */
- /*******************************************************************/
- if (!Fmtd)
- tdbp = new(g) TDBCSV(this, txfp);
- else
- tdbp = new(g) TDBFMT(this, txfp);
-
- if (Multiple)
- tdbp = new(g) TDBMUL(tdbp);
-
- } else
- tdbp = new(g)TDBCCL(this);
-
- return tdbp;
- } // end of GetTable
-
-/* -------------------------- Class TDBCSV --------------------------- */
-
-/***********************************************************************/
-/* Implementation of the TDBCSV class. */
-/***********************************************************************/
-TDBCSV::TDBCSV(PCSVDEF tdp, PTXF txfp) : TDBDOS(tdp, txfp)
- {
-#if defined(_DEBUG)
- assert (tdp);
-#endif
- Field = NULL;
- Offset = NULL;
- Fldlen = NULL;
- Fields = 0;
- Nerr = 0;
- Quoted = tdp->Quoted;
- Maxerr = tdp->Maxerr;
- Accept = tdp->Accept;
- Header = tdp->Header;
- Sep = tdp->GetSep();
- Qot = tdp->GetQot();
- } // end of TDBCSV standard constructor
-
-TDBCSV::TDBCSV(PGLOBAL g, PTDBCSV tdbp) : TDBDOS(g, tdbp)
- {
- Fields = tdbp->Fields;
-
- if (Fields) {
- if (tdbp->Offset)
- Offset = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
-
- if (tdbp->Fldlen)
- Fldlen = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
-
- Field = (PSZ *)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
-
- for (int i = 0; i < Fields; i++) {
- if (Offset)
- Offset[i] = tdbp->Offset[i];
-
- if (Fldlen)
- Fldlen[i] = tdbp->Fldlen[i];
-
- if (Field) {
- assert (Fldlen);
- Field[i] = (PSZ)PlugSubAlloc(g, NULL, Fldlen[i] + 1);
- Field[i][Fldlen[i]] = '\0';
- } // endif Field
-
- } // endfor i
-
- } else {
- Field = NULL;
- Offset = NULL;
- Fldlen = NULL;
- } // endif Fields
-
- Nerr = tdbp->Nerr;
- Maxerr = tdbp->Maxerr;
- Quoted = tdbp->Quoted;
- Accept = tdbp->Accept;
- Header = tdbp->Header;
- Sep = tdbp->Sep;
- Qot = tdbp->Qot;
- } // end of TDBCSV copy constructor
-
-// Method
-PTDB TDBCSV::CopyOne(PTABS t)
- {
- PTDB tp;
- PCSVCOL cp1, cp2;
- PGLOBAL g = t->G; // Is this really useful ???
-
- tp = new(g) TDBCSV(g, this);
-
- for (cp1 = (PCSVCOL)Columns; cp1; cp1 = (PCSVCOL)cp1->GetNext()) {
- cp2 = new(g) CSVCOL(cp1, tp); // Make a copy
- NewPointer(t, cp1, cp2);
- } // endfor cp1
-
- return tp;
- } // end of CopyOne
-
-/***********************************************************************/
-/* Allocate CSV column description block. */
-/***********************************************************************/
-PCOL TDBCSV::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
- {
- return new(g) CSVCOL(g, cdp, this, cprec, n);
- } // end of MakeCol
-
-/***********************************************************************/
-/* Check whether the number of errors is greater than the maximum. */
-/***********************************************************************/
-bool TDBCSV::CheckErr(void)
- {
- return (++Nerr) > Maxerr;
- } // end of CheckErr
-
-/***********************************************************************/
-/* CSV EstimatedLength. Returns an estimated minimum line length. */
-/***********************************************************************/
-int TDBCSV::EstimatedLength(PGLOBAL g)
- {
- if (trace)
- htrc("EstimatedLength: Fields=%d Columns=%p\n", Fields, Columns);
-
- if (!Fields) {
- PCSVCOL colp;
-
- for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
- if (!colp->IsSpecial()) // Not a pseudo column
- Fields = max(Fields, (int)colp->Fldnum);
-
- if (Columns)
- Fields++; // Fldnum was 0 based
-
- } // endif Fields
-
- return (int)Fields; // Number of separators if all fields are null
- } // end of Estimated Length
-
-#if 0
-/***********************************************************************/
-/* CSV tables favor the use temporary files for Update. */
-/***********************************************************************/
-bool TDBCSV::IsUsingTemp(PGLOBAL g)
- {
- USETEMP usetemp = PlgGetUser(g)->UseTemp;
-
- return (usetemp == TMP_YES || usetemp == TMP_FORCE ||
- (usetemp == TMP_AUTO && Mode == MODE_UPDATE));
- } // end of IsUsingTemp
-#endif // 0 (Same as TDBDOS one)
-
-/***********************************************************************/
-/* CSV Access Method opening routine. */
-/* First allocate the Offset and Fldlen arrays according to the */
-/* greatest field used in that query. Then call the DOS opening fnc. */
-/***********************************************************************/
-bool TDBCSV::OpenDB(PGLOBAL g)
- {
- bool rc = false;
- PCOLDEF cdp;
- PDOSDEF tdp = (PDOSDEF)To_Def;
-
- if (Use != USE_OPEN && (Columns || Mode == MODE_UPDATE)) {
- // Allocate the storage used to read (or write) records
- int i, len;
- PCSVCOL colp;
-
- if (!Fields) // May have been set in TABFMT::OpenDB
- if (Mode != MODE_UPDATE && Mode != MODE_INSERT) {
- for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
- if (!colp->IsSpecial()) // Not a pseudo column
- Fields = max(Fields, (int)colp->Fldnum);
-
- if (Columns)
- Fields++; // Fldnum was 0 based
-
- } else
- for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
- Fields++;
-
- Offset = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
- Fldlen = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
-
- if (Mode == MODE_INSERT || Mode == MODE_UPDATE) {
- Field = (PSZ*)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
- Fldtyp = (bool*)PlugSubAlloc(g, NULL, sizeof(bool) * Fields);
- } // endif Mode
-
- for (i = 0; i < Fields; i++) {
- Offset[i] = 0;
- Fldlen[i] = 0;
-
- if (Field) {
- Field[i] = NULL;
- Fldtyp[i] = false;
- } // endif Field
-
- } // endfor i
-
- if (Field)
- // Prepare writing fields
- if (Mode != MODE_UPDATE)
- for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next) {
- i = colp->Fldnum;
- len = colp->GetLength();
- Field[i] = (PSZ)PlugSubAlloc(g, NULL, len + 1);
- Field[i][len] = '\0';
- Fldlen[i] = len;
- Fldtyp[i] = IsTypeNum(colp->GetResultType());
- } // endfor colp
-
- else // MODE_UPDATE
- for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext()) {
- i = cdp->GetOffset() - 1;
- len = cdp->GetLength();
- Field[i] = (PSZ)PlugSubAlloc(g, NULL, len + 1);
- Field[i][len] = '\0';
- Fldlen[i] = len;
- Fldtyp[i] = IsTypeNum(cdp->GetType());
- } // endfor colp
-
- } // endif Use
-
- if (Header) {
- // Check that the Lrecl is at least equal to the header line length
- int headlen = 0;
- PCOLDEF cdp;
- PDOSDEF tdp = (PDOSDEF)To_Def;
-
- for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
- headlen += strlen(cdp->GetName()) + 3; // 3 if names are quoted
-
- if (headlen > Lrecl) {
- Lrecl = headlen;
- Txfp->Lrecl = headlen;
- } // endif headlen
-
- } // endif Header
-
- Nerr = 0;
- rc = TDBDOS::OpenDB(g);
-
- if (!rc && Mode == MODE_UPDATE && To_Kindex)
- // Because KINDEX::Init is executed in mode READ, we must restore
- // the Fldlen array that was modified when reading the table file.
- for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
- Fldlen[cdp->GetOffset() - 1] = cdp->GetLength();
-
- return rc;
- } // end of OpenDB
-
-/***********************************************************************/
-/* SkipHeader: Physically skip first header line if applicable. */
-/* This is called from TDBDOS::OpenDB and must be executed before */
-/* Kindex construction if the file is accessed using an index. */
-/***********************************************************************/
-bool TDBCSV::SkipHeader(PGLOBAL g)
- {
- int len = GetFileLength(g);
- bool rc = false;
-
-#if defined(_DEBUG)
- if (len < 0)
- return true;
-#endif // _DEBUG
-
- if (Header) {
- if (Mode == MODE_INSERT) {
- if (!len) {
- // New file, the header line must be constructed and written
- int i, n = 0;
- int hlen = 0;
- bool q = Qot && Quoted > 0;
- PCOLDEF cdp;
-
- // Estimate the length of the header list
- for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext()) {
- hlen += (1 + strlen(cdp->GetName()));
- hlen += ((q) ? 2 : 0);
- n++; // Calculate the number of columns
- } // endfor cdp
-
- if (hlen > Lrecl) {
- sprintf(g->Message, MSG(LRECL_TOO_SMALL), hlen);
- return true;
- } // endif hlen
-
- // File is empty, write a header record
- memset(To_Line, 0, Lrecl);
-
- // The column order in the file is given by the offset value
- for (i = 1; i <= n; i++)
- for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext())
- if (cdp->GetOffset() == i) {
- if (q)
- To_Line[strlen(To_Line)] = Qot;
-
- strcat(To_Line, cdp->GetName());
-
- if (q)
- To_Line[strlen(To_Line)] = Qot;
-
- if (i < n)
- To_Line[strlen(To_Line)] = Sep;
-
- } // endif Offset
-
- rc = (Txfp->WriteBuffer(g) == RC_FX);
- } // endif !FileLength
-
- } else if (Mode == MODE_DELETE) {
- if (len)
- rc = (Txfp->SkipRecord(g, true) == RC_FX);
-
- } else if (len) // !Insert && !Delete
- rc = (Txfp->SkipRecord(g, false) == RC_FX || Txfp->RecordPos(g));
-
- } // endif Header
-
- return rc;
- } // end of SkipHeader
-
-/***********************************************************************/
-/* ReadBuffer: Physical read routine for the CSV access method. */
-/***********************************************************************/
-int TDBCSV::ReadBuffer(PGLOBAL g)
- {
- char *p1, *p2, *p = NULL;
- int i, n, len, rc = Txfp->ReadBuffer(g);
- bool bad = false;
-
- if (trace > 1)
- htrc("CSV: Row is '%s' rc=%d\n", To_Line, rc);
-
- if (rc != RC_OK || !Fields)
- return rc;
- else
- p2 = To_Line;
-
- // Find the offsets and lengths of the columns for this row
- for (i = 0; i < Fields; i++) {
- if (!bad) {
- if (Qot && *p2 == Qot) { // Quoted field
- for (n = 0, p1 = ++p2; (p = strchr(p1, Qot)); p1 = p + 2)
- if (*(p + 1) == Qot)
- n++; // Doubled internal quotes
- else
- break; // Final quote
-
- if (p) {
- len = p++ - p2;
-
-// if (Sep != ' ')
-// for (; *p == ' '; p++) ; // Skip blanks
-
- if (*p != Sep && i != Fields - 1) { // Should be the separator
- if (CheckErr()) {
- sprintf(g->Message, MSG(MISSING_FIELD),
- i+1, Name, RowNumber(g));
- return RC_FX;
- } else if (Accept)
- bad = true;
- else
- return RC_NF;
-
- } // endif p
-
- if (n) {
- int j, k;
-
- // Suppress the double of internal quotes
- for (j = k = 0; j < len; j++, k++) {
- if (p2[j] == Qot)
- j++; // skip first one
-
- p2[k] = p2[j];
- } // endfor i, j
-
- len -= n;
- } // endif n
-
- } else if (CheckErr()) {
- sprintf(g->Message, MSG(BAD_QUOTE_FIELD),
- Name, i+1, RowNumber(g));
- return RC_FX;
- } else if (Accept) {
- len = strlen(p2);
- bad = true;
- } else
- return RC_NF;
-
- } else if ((p = strchr(p2, Sep)))
- len = p - p2;
- else if (i == Fields - 1)
- len = strlen(p2);
- else if (Accept && Maxerr == 0) {
- len = strlen(p2);
- bad = true;
- } else if (CheckErr()) {
- sprintf(g->Message, MSG(MISSING_FIELD), i+1, Name, RowNumber(g));
- return RC_FX;
- } else if (Accept) {
- len = strlen(p2);
- bad = true;
- } else
- return RC_NF;
-
- } else
- len = 0;
-
- Offset[i] = p2 - To_Line;
-
- if (Mode != MODE_UPDATE)
- Fldlen[i] = len;
- else if (len > Fldlen[i]) {
- sprintf(g->Message, MSG(FIELD_TOO_LONG), i+1, RowNumber(g));
- return RC_FX;
- } else {
- strncpy(Field[i], p2, len);
- Field[i][len] = '\0';
- } // endif Mode
-
- if (p)
- p2 = p + 1;
-
- } // endfor i
-
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* Data Base write routine CSV file access method. */
-/***********************************************************************/
-int TDBCSV::WriteDB(PGLOBAL g)
- {
- char sep[2], qot[2];
- int i, nlen, oldlen = strlen(To_Line);
-
- if (trace > 1)
- htrc("CSV WriteDB: R%d Mode=%d key=%p link=%p\n",
- Tdb_No, Mode, To_Key_Col, To_Link);
-
- // Before writing the line we must check its length
- if ((nlen = CheckWrite(g)) < 0)
- return RC_FX;
-
- // Before writing the line we must make it
- sep[0] = Sep;
- sep[1] = '\0';
- qot[0] = Qot;
- qot[1] = '\0';
- *To_Line = '\0';
-
- for (i = 0; i < Fields; i++) {
- if (i)
- strcat(To_Line, sep);
-
- if (Field[i])
- if (!strlen(Field[i])) {
- // Generally null fields are not quoted
- if (Quoted > 2)
- // Except if explicitely required
- strcat(strcat(To_Line, qot), qot);
-
- } else if (Qot && (strchr(Field[i], Sep) || *Field[i] == Qot
- || Quoted > 1 || (Quoted == 1 && !Fldtyp[i])))
- if (strchr(Field[i], Qot)) {
- // Field contains quotes that must be doubled
- int j, k = strlen(To_Line), n = strlen(Field[i]);
-
- To_Line[k++] = Qot;
-
- for (j = 0; j < n; j++) {
- if (Field[i][j] == Qot)
- To_Line[k++] = Qot;
-
- To_Line[k++] = Field[i][j];
- } // endfor j
-
- To_Line[k++] = Qot;
- To_Line[k] = '\0';
- } else
- strcat(strcat(strcat(To_Line, qot), Field[i]), qot);
-
- else
- strcat(To_Line, Field[i]);
-
- } // endfor i
-
-#if defined(_DEBUG)
- assert ((unsigned)nlen == strlen(To_Line));
-#endif
-
- if (Mode == MODE_UPDATE && nlen < oldlen
- && !((PDOSFAM)Txfp)->GetUseTemp()) {
- // In Update mode with no temp file, line length must not change
- To_Line[nlen] = Sep;
-
- for (nlen++; nlen < oldlen; nlen++)
- To_Line[nlen] = ' ';
-
- To_Line[nlen] = '\0';
- } // endif
-
- if (trace > 1)
- htrc("Write: line is=%s", To_Line);
-
- /*********************************************************************/
- /* Now start the writing process. */
- /*********************************************************************/
- return Txfp->WriteBuffer(g);
- } // end of WriteDB
-
-/***********************************************************************/
-/* Check whether a new line fit in the file lrecl size. */
-/***********************************************************************/
-int TDBCSV::CheckWrite(PGLOBAL g)
- {
- int maxlen, n, nlen = (Fields - 1);
-
- if (trace > 1)
- htrc("CheckWrite: R%d Mode=%d\n", Tdb_No, Mode);
-
- // Before writing the line we must check its length
- maxlen = (Mode == MODE_UPDATE && !Txfp->GetUseTemp())
- ? strlen(To_Line) : Lrecl;
-
- // Check whether record is too int
- for (int i = 0; i < Fields; i++)
- if (Field[i]) {
- if (!(n = strlen(Field[i])))
- n += (Quoted > 2 ? 2 : 0);
- else if (strchr(Field[i], Sep) || (Qot && *Field[i] == Qot)
- || Quoted > 1 || (Quoted == 1 && !Fldtyp[i]))
- if (!Qot) {
- sprintf(g->Message, MSG(SEP_IN_FIELD), i + 1);
- return -1;
- } else {
- // Quotes inside a quoted field must be doubled
- char *p1, *p2;
-
- for (p1 = Field[i]; (p2 = strchr(p1, Qot)); p1 = p2 + 1)
- n++;
-
- n += 2; // Outside quotes
- } // endif
-
- if ((nlen += n) > maxlen) {
- strcpy(g->Message, MSG(LINE_TOO_LONG));
- return -1;
- } // endif nlen
-
- } // endif Field
-
- return nlen;
- } // end of CheckWrite
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Implementation of the TDBFMT class. */
-/***********************************************************************/
-TDBFMT::TDBFMT(PGLOBAL g, PTDBFMT tdbp) : TDBCSV(g, tdbp)
- {
- FldFormat = tdbp->FldFormat;
- To_Fld = tdbp->To_Fld;
- FmtTest = tdbp->FmtTest;
- Linenum = tdbp->Linenum;
- } // end of TDBFMT copy constructor
-
-// Method
-PTDB TDBFMT::CopyOne(PTABS t)
- {
- PTDB tp;
- PCSVCOL cp1, cp2;
-//PFMTCOL cp1, cp2;
- PGLOBAL g = t->G; // Is this really useful ???
-
- tp = new(g) TDBFMT(g, this);
-
- for (cp1 = (PCSVCOL)Columns; cp1; cp1 = (PCSVCOL)cp1->GetNext()) {
-//for (cp1 = (PFMTCOL)Columns; cp1; cp1 = (PFMTCOL)cp1->GetNext()) {
- cp2 = new(g) CSVCOL(cp1, tp); // Make a copy
-// cp2 = new(g) FMTCOL(cp1, tp); // Make a copy
- NewPointer(t, cp1, cp2);
- } // endfor cp1
-
- return tp;
- } // end of CopyOne
-
-/***********************************************************************/
-/* Allocate FMT column description block. */
-/***********************************************************************/
-PCOL TDBFMT::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
- {
- return new(g) CSVCOL(g, cdp, this, cprec, n);
-//return new(g) FMTCOL(cdp, this, cprec, n);
- } // end of MakeCol
-
-/***********************************************************************/
-/* FMT EstimatedLength. Returns an estimated minimum line length. */
-/* The big problem here is how can we astimated that minimum ? */
-/***********************************************************************/
-int TDBFMT::EstimatedLength(PGLOBAL g)
- {
- // This is rather stupid !!!
- return ((PDOSDEF)To_Def)->GetEnding() + (int)((Lrecl / 10) + 1);
- } // end of EstimatedLength
-
-/***********************************************************************/
-/* FMT Access Method opening routine. */
-/***********************************************************************/
-bool TDBFMT::OpenDB(PGLOBAL g)
- {
- Linenum = 0;
-
- if (Mode == MODE_INSERT || Mode == MODE_UPDATE) {
- sprintf(g->Message, MSG(FMT_WRITE_NIY), "FMT");
- return true; // NIY
- } // endif Mode
-
- if (Use != USE_OPEN && Columns) {
- // Make the formats used to read records
- PSZ pfm;
- int i, n;
- PCSVCOL colp;
- PCOLDEF cdp;
- PDOSDEF tdp = (PDOSDEF)To_Def;
-
- for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
- if (!colp->IsSpecial()) // Not a pseudo column
- Fields = max(Fields, (int)colp->Fldnum);
-
- if (Columns)
- Fields++; // Fldnum was 0 based
-
- To_Fld = PlugSubAlloc(g, NULL, Lrecl + 1);
- FldFormat = (PSZ*)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
- memset(FldFormat, 0, sizeof(PSZ) * Fields);
- FmtTest = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
- memset(FmtTest, 0, sizeof(int) * Fields);
-
- // Get the column formats
- for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
- if ((i = cdp->GetOffset() - 1) < Fields) {
- if (!(pfm = cdp->GetFmt())) {
- sprintf(g->Message, MSG(NO_FLD_FORMAT), i + 1, Name);
- return true;
- } // endif pfm
-
- // Roughly check the Fmt format
- if ((n = strlen(pfm) - 2) < 4) {
- sprintf(g->Message, MSG(BAD_FLD_FORMAT), i + 1, Name);
- return true;
- } // endif n
-
- FldFormat[i] = (PSZ)PlugSubAlloc(g, NULL, n + 5);
- strcpy(FldFormat[i], pfm);
-
- if (!strcmp(pfm + n, "%m")) {
- // This is a field that can be missing. Flag it so it can
- // be handled with special processing.
- FldFormat[i][n+1] = 'n'; // To have sscanf normal processing
- FmtTest[i] = 2;
- } else if (i+1 < Fields && strcmp(pfm + n, "%n")) {
- // There are trailing characters after the field contents
- // add a marker for the next field start position.
- strcat(FldFormat[i], "%n");
- FmtTest[i] = 1;
- } // endif's
-
- } // endif i
-
- } // endif Use
-
- return TDBCSV::OpenDB(g);
- } // end of OpenDB
-
-/***********************************************************************/
-/* ReadBuffer: Physical read routine for the FMT access method. */
-/***********************************************************************/
-int TDBFMT::ReadBuffer(PGLOBAL g)
- {
- int i, len, n, deb, fin, nwp, pos = 0, rc;
- bool bad = false;
-
- if ((rc = Txfp->ReadBuffer(g)) != RC_OK || !Fields)
- return rc;
- else
- ++Linenum;
-
- if (trace > 1)
- htrc("FMT: Row %d is '%s' rc=%d\n", Linenum, To_Line, rc);
-
- // Find the offsets and lengths of the columns for this row
- for (i = 0; i < Fields; i++) {
- if (!bad) {
- deb = fin = -1;
-
- if (!FldFormat[i]) {
- n = 0;
- } else if (FmtTest[i] == 1) {
- nwp = -1;
- n = sscanf(To_Line + pos, FldFormat[i], &deb, To_Fld, &fin, &nwp);
- } else {
- n = sscanf(To_Line + pos, FldFormat[i], &deb, To_Fld, &fin);
-
- if (n != 1 && (deb >= 0 || i == Fields - 1) && FmtTest[i] == 2) {
- // Missing optional field, not an error
- n = 1;
-
- if (i == Fields - 1)
- fin = deb = 0;
- else
- fin = deb;
-
- } // endif n
-
- nwp = fin;
- } // endif i
-
- if (n != 1 || deb < 0 || fin < 0 || nwp < 0) {
- // This is to avoid a very strange sscanf bug occuring
- // with fields that ends with a null character.
- // This bug causes subsequent sscanf to return in error,
- // so next lines are not parsed correctly.
- sscanf("a", "%*c"); // Seems to reset things Ok
-
- if (CheckErr()) {
- sprintf(g->Message, MSG(BAD_LINEFLD_FMT), Linenum, i + 1, Name);
- return RC_FX;
- } else if (Accept)
- bad = true;
- else
- return RC_NF;
-
- } // endif n...
-
- } // endif !bad
-
- if (!bad) {
- Offset[i] = pos + deb;
- len = fin - deb;
- } else {
- nwp = 0;
- Offset[i] = pos;
- len = 0;
- } // endif bad
-
-// if (Mode != MODE_UPDATE)
- Fldlen[i] = len;
-// else if (len > Fldlen[i]) {
-// sprintf(g->Message, MSG(FIELD_TOO_LONG), i+1, To_Tdb->RowNumber(g));
-// return RC_FX;
-// } else {
-// strncpy(Field[i], To_Line + pos, len);
-// Field[i][len] = '\0';
-// } // endif Mode
-
- pos += nwp;
- } // endfor i
-
- if (bad)
- Nerr++;
- else
- sscanf("a", "%*c"); // Seems to reset things Ok
-
- return rc;
- } // end of ReadBuffer
-
-/***********************************************************************/
-/* Data Base write routine FMT file access method. */
-/***********************************************************************/
-int TDBFMT::WriteDB(PGLOBAL g)
- {
- sprintf(g->Message, MSG(FMT_WRITE_NIY), "FMT");
- return RC_FX; // NIY
- } // end of WriteDB
-
-// ------------------------ CSVCOL functions ----------------------------
-
-/***********************************************************************/
-/* CSVCOL public constructor */
-/***********************************************************************/
-CSVCOL::CSVCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i)
- : DOSCOL(g, cdp, tdbp, cprec, i, "CSV")
- {
- Fldnum = Deplac - 1;
- Deplac = 0;
- } // end of CSVCOL constructor
-
-/***********************************************************************/
-/* CSVCOL constructor used for copying columns. */
-/* tdbp is the pointer to the new table descriptor. */
-/***********************************************************************/
-CSVCOL::CSVCOL(CSVCOL *col1, PTDB tdbp) : DOSCOL(col1, tdbp)
- {
- Fldnum = col1->Fldnum;
- } // end of CSVCOL copy constructor
-
-/***********************************************************************/
-/* VarSize: This function tells UpdateDB whether or not the block */
-/* optimization file must be redone if this column is updated, even */
-/* it is not sorted or clustered. This applies to a blocked table, */
-/* because if it is updated using a temporary file, the block size */
-/* may be modified. */
-/***********************************************************************/
-bool CSVCOL::VarSize(void)
- {
- PTXF txfp = ((PTDBCSV)To_Tdb)->Txfp;
-
- if (txfp->IsBlocked() && txfp->GetUseTemp())
- // Blocked table using a temporary file
- return true;
- else
- return false;
-
- } // end VarSize
-
-/***********************************************************************/
-/* ReadColumn: call DOSCOL::ReadColumn after having set the offet */
-/* and length of the field to read as calculated by TDBCSV::ReadDB. */
-/***********************************************************************/
-void CSVCOL::ReadColumn(PGLOBAL g)
- {
- int rc;
- PTDBCSV tdbp = (PTDBCSV)To_Tdb;
-
- /*********************************************************************/
- /* If physical reading of the line was deferred, do it now. */
- /*********************************************************************/
- if (!tdbp->IsRead())
- if ((rc = tdbp->ReadBuffer(g)) != RC_OK) {
- if (rc == RC_EF)
- sprintf(g->Message, MSG(INV_DEF_READ), rc);
-
- longjmp(g->jumper[g->jump_level], 34);
- } // endif
-
- if (tdbp->Mode != MODE_UPDATE) {
- int colen = Long; // Column length
-
- // Set the field offset and length for this row
- Deplac = tdbp->Offset[Fldnum]; // Field offset
- Long = tdbp->Fldlen[Fldnum]; // Field length
-
- if (trace > 1)
- htrc("CSV ReadColumn %s Fldnum=%d offset=%d fldlen=%d\n",
- Name, Fldnum, Deplac, Long);
-
- if (Long > colen && tdbp->CheckErr()) {
- Long = colen; // Restore column length
- sprintf(g->Message, MSG(FLD_TOO_LNG_FOR),
- Fldnum + 1, Name, To_Tdb->RowNumber(g), tdbp->GetFile(g));
- longjmp(g->jumper[g->jump_level], 34);
- } // endif Long
-
- // Now do the reading
- DOSCOL::ReadColumn(g);
-
- // Restore column length
- Long = colen;
- } else { // Mode Update
- // Field have been copied in TDB Field array
- PSZ fp = tdbp->Field[Fldnum];
-
- Value->SetValue_psz(fp);
-
- // Set null when applicable
- if (Nullable)
- Value->SetNull(Value->IsZero());
-
- } // endif Mode
-
- } // end of ReadColumn
-
-/***********************************************************************/
-/* WriteColumn: The column is written in TDBCSV matching Field. */
-/***********************************************************************/
-void CSVCOL::WriteColumn(PGLOBAL g)
- {
- char *p, buf[32];
- int flen;
- PTDBCSV tdbp = (PTDBCSV)To_Tdb;
-
- if (trace > 1)
- htrc("CSV WriteColumn: col %s R%d coluse=%.4X status=%.4X\n",
- Name, tdbp->GetTdb_No(), ColUse, Status);
-
- flen = GetLength();
-
- if (trace > 1)
- htrc("Lrecl=%d Long=%d field=%d coltype=%d colval=%p\n",
- tdbp->Lrecl, Long, flen, Buf_Type, Value);
-
- /*********************************************************************/
- /* Check whether the new value has to be converted to Buf_Type. */
- /*********************************************************************/
- if (Value != To_Val)
- Value->SetValue_pval(To_Val, false); // Convert the updated value
-
- /*********************************************************************/
- /* Get the string representation of the column value. */
- /*********************************************************************/
- p = Value->ShowValue(buf);
-
- if (trace > 1)
- htrc("new length(%p)=%d\n", p, strlen(p));
-
- if ((signed)strlen(p) > flen) {
- sprintf(g->Message, MSG(BAD_FLD_LENGTH), Name, p, flen,
- tdbp->RowNumber(g), tdbp->GetFile(g));
- longjmp(g->jumper[g->jump_level], 34);
- } // endif
-
- if (trace > 1)
- htrc("buffer=%s\n", p);
-
- /*********************************************************************/
- /* Updating must be done also during the first pass so writing the */
- /* updated record can be checked for acceptable record length. */
- /*********************************************************************/
- if (Fldnum < 0) {
- // This can happen for wrong offset value in XDB files
- sprintf(g->Message, MSG(BAD_FIELD_RANK), Fldnum + 1, Name);
- longjmp(g->jumper[g->jump_level], 34);
- } else
- strncpy(tdbp->Field[Fldnum], p, flen);
-
- if (trace > 1)
- htrc(" col written: '%s'\n", p);
-
- } // end of WriteColumn
-
-/* ---------------------------TDBCCL class --------------------------- */
-
-/***********************************************************************/
-/* TDBCCL class constructor. */
-/***********************************************************************/
-TDBCCL::TDBCCL(PCSVDEF tdp) : TDBCAT(tdp)
- {
- Fn = tdp->GetFn();
- Hdr = tdp->Header;
- Mxr = tdp->Maxerr;
- Qtd = tdp->Quoted;
- Sep = tdp->Sep;
- } // end of TDBCCL constructor
-
-/***********************************************************************/
-/* GetResult: Get the list the CSV file columns. */
-/***********************************************************************/
-PQRYRES TDBCCL::GetResult(PGLOBAL g)
- {
- return CSVColumns(g, Fn, Sep, Qtd, Hdr, Mxr, false);
- } // end of GetResult
-
-/* ------------------------ End of TabFmt ---------------------------- */
+/************* TabFmt C++ Program Source Code File (.CPP) **************/
+/* PROGRAM NAME: TABFMT */
+/* ------------- */
+/* Version 3.9 */
+/* */
+/* COPYRIGHT: */
+/* ---------- */
+/* (C) Copyright to the author Olivier BERTRAND 2001 - 2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* This program are the TABFMT classes DB execution routines. */
+/* The base class CSV is comma separated files. */
+/* FMT (Formatted) files are those having a complex internal record */
+/* format described in the Format keyword of their definition. */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant MariaDB header file. */
+/***********************************************************************/
+#include "my_global.h"
+
+#if defined(WIN32)
+#include <io.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <locale.h>
+#if defined(__BORLANDC__)
+#define __MFC_COMPAT__ // To define min/max as macro
+#endif
+//#include <windows.h>
+#include "osutil.h"
+#else
+#if defined(UNIX)
+#include <errno.h>
+#include <unistd.h>
+#include "osutil.h"
+#else
+#include <io.h>
+#endif
+#include <fcntl.h>
+#endif
+
+/***********************************************************************/
+/* Include application header files: */
+/* global.h is header containing all global declarations. */
+/* plgdbsem.h is header containing the DB application declarations. */
+/* tabdos.h is header containing the TABDOS class declarations. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "mycat.h"
+#include "filamap.h"
+#if defined(ZIP_SUPPORT)
+#include "filamzip.h"
+#endif // ZIP_SUPPORT
+#include "tabfmt.h"
+#include "tabmul.h"
+#define NO_FUNC
+#include "plgcnx.h" // For DB types
+#include "resource.h"
+
+/***********************************************************************/
+/* This should be an option. */
+/***********************************************************************/
+#define MAXCOL 200 /* Default max column nb in result */
+#define TYPE_UNKNOWN 10 /* Must be greater than other types */
+
+extern "C" int trace;
+
+/***********************************************************************/
+/* CSVColumns: constructs the result blocks containing the description */
+/* of all the columns of a CSV file that will be retrieved by #GetData.*/
+/* Note: the algorithm to set the type is based on the internal values */
+/* of types (TYPE_STRING < TYPE_DOUBLE < TYPE_INT) (1 < 2 < 7). */
+/* If these values are changed, this will have to be revisited. */
+/***********************************************************************/
+PQRYRES CSVColumns(PGLOBAL g, const char *fn, char sep, char q,
+ int hdr, int mxr, bool info)
+ {
+ static int buftyp[] = {TYPE_STRING, TYPE_SHORT, TYPE_STRING,
+ TYPE_INT, TYPE_INT, TYPE_SHORT};
+ static XFLD fldtyp[] = {FLD_NAME, FLD_TYPE, FLD_TYPENAME,
+ FLD_PREC, FLD_LENGTH, FLD_SCALE};
+ static unsigned int length[] = {6, 6, 8, 10, 10, 6};
+ char *p, *colname[MAXCOL], dechar, filename[_MAX_PATH], buf[4096];
+ int i, imax, hmax, n, nerr, phase, blank, digit, dec, type;
+ int ncol = sizeof(buftyp) / sizeof(int);
+ int num_read = 0, num_max = 10000000; // Statistics
+ int len[MAXCOL], typ[MAXCOL], prc[MAXCOL];
+ FILE *infile;
+ PQRYRES qrp;
+ PCOLRES crp;
+
+ if (info) {
+ imax = hmax = 0;
+ length[0] = 128;
+ goto skipit;
+ } // endif info
+
+// num_max = atoi(p+1); // Max num of record to test
+#if defined(WIN32)
+ if (sep == ',' || strnicmp(setlocale(LC_NUMERIC, NULL), "French", 6))
+ dechar = '.';
+ else
+ dechar = ',';
+#else // !WIN32
+ dechar = '.';
+#endif // !WIN32
+
+ if (trace)
+ htrc("File %s sep=%c q=%c hdr=%d mxr=%d\n",
+ SVP(fn), sep, q, hdr, mxr);
+
+ if (!fn) {
+ strcpy(g->Message, MSG(MISSING_FNAME));
+ return NULL;
+ } // endif fn
+
+ imax = hmax = nerr = 0;
+ mxr = max(0, mxr);
+
+ for (i = 0; i < MAXCOL; i++) {
+ colname[i] = NULL;
+ len[i] = 0;
+ typ[i] = TYPE_UNKNOWN;
+ prc[i] = 0;
+ } // endfor i
+
+ /*********************************************************************/
+ /* Open the input file. */
+ /*********************************************************************/
+ PlugSetPath(filename, fn, PlgGetDataPath(g));
+
+ if (!(infile= global_fopen(g, MSGID_CANNOT_OPEN, filename, "r")))
+ return NULL;
+
+ if (hdr) {
+ /*******************************************************************/
+ /* Make the column names from the first line. */
+ /*******************************************************************/
+ phase = 0;
+
+ if (fgets(buf, sizeof(buf), infile)) {
+ n = strlen(buf) + 1;
+ buf[n - 2] = '\0';
+#if defined(UNIX)
+ // The file can be imported from Windows
+ if (buf[n - 3] == '\r')
+ buf[n - 3] = 0;
+#endif // UNIX
+ p = (char*)PlugSubAlloc(g, NULL, n);
+ memcpy(p, buf, n);
+
+ //skip leading blanks
+ for (; *p == ' '; p++) ;
+
+ if (q && *p == q) {
+ // Header is quoted
+ p++;
+ phase = 1;
+ } // endif q
+
+ colname[0] = p;
+ } else {
+ sprintf(g->Message, MSG(FILE_IS_EMPTY), fn);
+ goto err;
+ } // endif's
+
+ for (i = 1; *p; p++)
+ if (phase == 1 && *p == q) {
+ *p = '\0';
+ phase = 0;
+ } else if (*p == sep && !phase) {
+ *p = '\0';
+
+ //skip leading blanks
+ for (; *(p+1) == ' '; p++) ;
+
+ if (q && *(p+1) == q) {
+ // Header is quoted
+ p++;
+ phase = 1;
+ } // endif q
+
+ colname[i++] = p + 1;
+ } // endif sep
+
+ num_read++;
+ imax = hmax = i;
+
+ for (i = 0; i < hmax; i++)
+ length[0] = max(length[0], strlen(colname[i]));
+
+ } // endif hdr
+
+ for (num_read++; num_read <= num_max; num_read++) {
+ /*******************************************************************/
+ /* Now start the reading process. Read one line. */
+ /*******************************************************************/
+ if (fgets(buf, sizeof(buf), infile)) {
+ n = strlen(buf);
+ buf[n - 1] = '\0';
+#if defined(UNIX)
+ // The file can be imported from Windows
+ if (buf[n - 2] == '\r')
+ buf[n - 2] = 0;
+#endif // UNIX
+ } else if (feof(infile)) {
+ sprintf(g->Message, MSG(EOF_AFTER_LINE), num_read -1);
+ break;
+ } else {
+ sprintf(g->Message, MSG(ERR_READING_REC), num_read, fn);
+ goto err;
+ } // endif's
+
+ /*******************************************************************/
+ /* Make the test for field lengths. */
+ /*******************************************************************/
+ i = n = phase = blank = digit = dec = 0;
+
+ for (p = buf; *p; p++)
+ if (*p == sep) {
+ if (phase != 1) {
+ if (i == MAXCOL - 1) {
+ sprintf(g->Message, MSG(TOO_MANY_FIELDS), num_read, fn);
+ goto err;
+ } // endif i
+
+ if (n) {
+ len[i] = max(len[i], n);
+ type = (digit || (dec && n == 1)) ? TYPE_STRING
+ : (dec) ? TYPE_DOUBLE : TYPE_INT;
+ typ[i] = min(type, typ[i]);
+ prc[i] = max((typ[i] == TYPE_DOUBLE) ? (dec - 1) : 0, prc[i]);
+ } // endif n
+
+ i++;
+ n = phase = blank = digit = dec = 0;
+ } else // phase == 1
+ n++;
+
+ } else if (*p == ' ') {
+ if (phase < 2)
+ n++;
+
+ if (blank)
+ digit = 1;
+
+ } else if (*p == q) {
+ if (phase == 0) {
+ if (blank)
+ if (++nerr > mxr) {
+ sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
+ goto err;
+ } else
+ goto skip;
+
+ n = 0;
+ phase = digit = 1;
+ } else if (phase == 1) {
+ if (*(p+1) == q) {
+ // This is currently not implemented for CSV tables
+// if (++nerr > mxr) {
+// sprintf(g->Message, MSG(QUOTE_IN_QUOTE), num_read);
+// goto err;
+// } else
+// goto skip;
+
+ p++;
+ n++;
+ } else
+ phase = 2;
+
+ } else if (++nerr > mxr) { // phase == 2
+ sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
+ goto err;
+ } else
+ goto skip;
+
+ } else {
+ if (phase == 2)
+ if (++nerr > mxr) {
+ sprintf(g->Message, MSG(MISPLACED_QUOTE), num_read);
+ goto err;
+ } else
+ goto skip;
+
+ // isdigit cannot be used here because of debug assert
+ if (!strchr("0123456789", *p)) {
+ if (!digit && *p == dechar)
+ dec = 1; // Decimal point found
+ else if (blank || !(*p == '-' || *p == '+'))
+ digit = 1;
+
+ } else if (dec)
+ dec++; // More decimals
+
+ n++;
+ blank = 1;
+ } // endif's *p
+
+ if (phase == 1)
+ if (++nerr > mxr) {
+ sprintf(g->Message, MSG(UNBALANCE_QUOTE), num_read);
+ goto err;
+ } else
+ goto skip;
+
+ if (n) {
+ len[i] = max(len[i], n);
+ type = (digit || n == 0 || (dec && n == 1)) ? TYPE_STRING
+ : (dec) ? TYPE_DOUBLE : TYPE_INT;
+ typ[i] = min(type, typ[i]);
+ prc[i] = max((typ[i] == TYPE_DOUBLE) ? (dec - 1) : 0, prc[i]);
+ } // endif n
+
+ imax = max(imax, i+1);
+ skip: ; // Skip erroneous line
+ } // endfor num_read
+
+ if (trace) {
+ htrc("imax=%d Lengths:", imax);
+
+ for (i = 0; i < imax; i++)
+ htrc(" %d", len[i]);
+
+ htrc("\n");
+ } // endif trace
+
+ fclose(infile);
+
+ skipit:
+ if (trace)
+ htrc("CSVColumns: imax=%d hmax=%d len=%d\n",
+ imax, hmax, length[0]);
+
+ /*********************************************************************/
+ /* Allocate the structures used to refer to the result set. */
+ /*********************************************************************/
+ qrp = PlgAllocResult(g, ncol, imax, IDS_COLUMNS + 3,
+ buftyp, fldtyp, length, false, false);
+ if (info || !qrp)
+ return qrp;
+
+ qrp->Nblin = imax;
+
+ /*********************************************************************/
+ /* Now get the results into blocks. */
+ /*********************************************************************/
+ for (i = 0; i < imax; i++) {
+ if (i >= hmax) {
+ sprintf(buf, "COL%.3d", i+1);
+ p = buf;
+ } else
+ p = colname[i];
+
+ if (typ[i] == TYPE_UNKNOWN) // Void column
+ typ[i] = TYPE_STRING;
+
+ crp = qrp->Colresp; // Column Name
+ crp->Kdata->SetValue(p, i);
+ crp = crp->Next; // Data Type
+ crp->Kdata->SetValue(typ[i], i);
+ crp = crp->Next; // Type Name
+ crp->Kdata->SetValue(GetTypeName(typ[i]), i);
+ crp = crp->Next; // Precision
+ crp->Kdata->SetValue(len[i], i);
+ crp = crp->Next; // Length
+ crp->Kdata->SetValue(len[i], i);
+ crp = crp->Next; // Scale (precision)
+ crp->Kdata->SetValue(prc[i], i);
+ } // endfor i
+
+ /*********************************************************************/
+ /* Return the result pointer for use by GetData routines. */
+ /*********************************************************************/
+ return qrp;
+
+ err:
+ fclose(infile);
+ return NULL;
+ } // end of CSVCColumns
+
+/* --------------------------- Class CSVDEF -------------------------- */
+
+/***********************************************************************/
+/* CSVDEF constructor. */
+/***********************************************************************/
+CSVDEF::CSVDEF(void)
+ {
+ Fmtd = Accept = Header = false;
+ Maxerr = 0;
+ Quoted = -1;
+ Sep = ',';
+ Qot = '\0';
+ } // end of CSVDEF constructor
+
+/***********************************************************************/
+/* DefineAM: define specific AM block values from XDB file. */
+/***********************************************************************/
+bool CSVDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
+ {
+ char buf[8];
+
+ // Double check correctness of offset values
+ if (Catfunc == FNC_NO)
+ for (PCOLDEF cdp = To_Cols; cdp; cdp = cdp->GetNext())
+ if (cdp->GetOffset() < 1) {
+ strcpy(g->Message, MSG(BAD_OFFSET_VAL));
+ return true;
+ } // endif Offset
+
+ // Call DOSDEF DefineAM with am=CSV so FMT is not confused with FIX
+ if (DOSDEF::DefineAM(g, "CSV", poff))
+ return true;
+
+ Cat->GetCharCatInfo("Separator", ",", buf, sizeof(buf));
+ Sep = (strlen(buf) == 2 && buf[0] == '\\' && buf[1] == 't') ? '\t' : *buf;
+ Quoted = Cat->GetIntCatInfo("Quoted", -1);
+ Cat->GetCharCatInfo("Qchar", "", buf, sizeof(buf));
+ Qot = *buf;
+
+ if (Qot && Quoted < 0)
+ Quoted = 0;
+ else if (!Qot && Quoted >= 0)
+ Qot = '"';
+
+ Fmtd = (!Sep || (am && (*am == 'F' || *am == 'f')));
+ Header = (Cat->GetIntCatInfo("Header", 0) != 0);
+ Maxerr = Cat->GetIntCatInfo("Maxerr", 0);
+ Accept = (Cat->GetIntCatInfo("Accept", 0) != 0);
+ return false;
+ } // end of DefineAM
+
+/***********************************************************************/
+/* GetTable: makes a new Table Description Block. */
+/***********************************************************************/
+PTDB CSVDEF::GetTable(PGLOBAL g, MODE mode)
+ {
+ PTDBASE tdbp;
+
+ if (Catfunc != FNC_COL) {
+ USETEMP tmp = PlgGetUser(g)->UseTemp;
+ bool map = Mapped && mode != MODE_INSERT &&
+ !(tmp != TMP_NO && mode == MODE_UPDATE) &&
+ !(tmp == TMP_FORCE &&
+ (mode == MODE_UPDATE || mode == MODE_DELETE));
+ PTXF txfp;
+
+ /*******************************************************************/
+ /* Allocate a file processing class of the proper type. */
+ /*******************************************************************/
+ if (map) {
+ // Should be now compatible with UNIX
+ txfp = new(g) MAPFAM(this);
+ } else if (Compressed) {
+#if defined(ZIP_SUPPORT)
+ if (Compressed == 1)
+ txfp = new(g) ZIPFAM(this);
+ else
+ txfp = new(g) ZLBFAM(this);
+
+#else // !ZIP_SUPPORT
+ strcpy(g->Message, "Compress not supported");
+ return NULL;
+#endif // !ZIP_SUPPORT
+ } else
+ txfp = new(g) DOSFAM(this);
+
+ /*******************************************************************/
+ /* Allocate a TDB of the proper type. */
+ /* Column blocks will be allocated only when needed. */
+ /*******************************************************************/
+ if (!Fmtd)
+ tdbp = new(g) TDBCSV(this, txfp);
+ else
+ tdbp = new(g) TDBFMT(this, txfp);
+
+ if (Multiple)
+ tdbp = new(g) TDBMUL(tdbp);
+
+ } else
+ tdbp = new(g)TDBCCL(this);
+
+ return tdbp;
+ } // end of GetTable
+
+/* -------------------------- Class TDBCSV --------------------------- */
+
+/***********************************************************************/
+/* Implementation of the TDBCSV class. */
+/***********************************************************************/
+TDBCSV::TDBCSV(PCSVDEF tdp, PTXF txfp) : TDBDOS(tdp, txfp)
+ {
+#if defined(_DEBUG)
+ assert (tdp);
+#endif
+ Field = NULL;
+ Offset = NULL;
+ Fldlen = NULL;
+ Fields = 0;
+ Nerr = 0;
+ Quoted = tdp->Quoted;
+ Maxerr = tdp->Maxerr;
+ Accept = tdp->Accept;
+ Header = tdp->Header;
+ Sep = tdp->GetSep();
+ Qot = tdp->GetQot();
+ } // end of TDBCSV standard constructor
+
+TDBCSV::TDBCSV(PGLOBAL g, PTDBCSV tdbp) : TDBDOS(g, tdbp)
+ {
+ Fields = tdbp->Fields;
+
+ if (Fields) {
+ if (tdbp->Offset)
+ Offset = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
+
+ if (tdbp->Fldlen)
+ Fldlen = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
+
+ Field = (PSZ *)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
+
+ for (int i = 0; i < Fields; i++) {
+ if (Offset)
+ Offset[i] = tdbp->Offset[i];
+
+ if (Fldlen)
+ Fldlen[i] = tdbp->Fldlen[i];
+
+ if (Field) {
+ assert (Fldlen);
+ Field[i] = (PSZ)PlugSubAlloc(g, NULL, Fldlen[i] + 1);
+ Field[i][Fldlen[i]] = '\0';
+ } // endif Field
+
+ } // endfor i
+
+ } else {
+ Field = NULL;
+ Offset = NULL;
+ Fldlen = NULL;
+ } // endif Fields
+
+ Nerr = tdbp->Nerr;
+ Maxerr = tdbp->Maxerr;
+ Quoted = tdbp->Quoted;
+ Accept = tdbp->Accept;
+ Header = tdbp->Header;
+ Sep = tdbp->Sep;
+ Qot = tdbp->Qot;
+ } // end of TDBCSV copy constructor
+
+// Method
+PTDB TDBCSV::CopyOne(PTABS t)
+ {
+ PTDB tp;
+ PCSVCOL cp1, cp2;
+ PGLOBAL g = t->G; // Is this really useful ???
+
+ tp = new(g) TDBCSV(g, this);
+
+ for (cp1 = (PCSVCOL)Columns; cp1; cp1 = (PCSVCOL)cp1->GetNext()) {
+ cp2 = new(g) CSVCOL(cp1, tp); // Make a copy
+ NewPointer(t, cp1, cp2);
+ } // endfor cp1
+
+ return tp;
+ } // end of CopyOne
+
+/***********************************************************************/
+/* Allocate CSV column description block. */
+/***********************************************************************/
+PCOL TDBCSV::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
+ {
+ return new(g) CSVCOL(g, cdp, this, cprec, n);
+ } // end of MakeCol
+
+/***********************************************************************/
+/* Check whether the number of errors is greater than the maximum. */
+/***********************************************************************/
+bool TDBCSV::CheckErr(void)
+ {
+ return (++Nerr) > Maxerr;
+ } // end of CheckErr
+
+/***********************************************************************/
+/* CSV EstimatedLength. Returns an estimated minimum line length. */
+/***********************************************************************/
+int TDBCSV::EstimatedLength(PGLOBAL g)
+ {
+ if (trace)
+ htrc("EstimatedLength: Fields=%d Columns=%p\n", Fields, Columns);
+
+ if (!Fields) {
+ PCSVCOL colp;
+
+ for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
+ if (!colp->IsSpecial()) // Not a pseudo column
+ Fields = max(Fields, (int)colp->Fldnum);
+
+ if (Columns)
+ Fields++; // Fldnum was 0 based
+
+ } // endif Fields
+
+ return (int)Fields; // Number of separators if all fields are null
+ } // end of Estimated Length
+
+#if 0
+/***********************************************************************/
+/* CSV tables favor the use temporary files for Update. */
+/***********************************************************************/
+bool TDBCSV::IsUsingTemp(PGLOBAL g)
+ {
+ USETEMP usetemp = PlgGetUser(g)->UseTemp;
+
+ return (usetemp == TMP_YES || usetemp == TMP_FORCE ||
+ (usetemp == TMP_AUTO && Mode == MODE_UPDATE));
+ } // end of IsUsingTemp
+#endif // 0 (Same as TDBDOS one)
+
+/***********************************************************************/
+/* CSV Access Method opening routine. */
+/* First allocate the Offset and Fldlen arrays according to the */
+/* greatest field used in that query. Then call the DOS opening fnc. */
+/***********************************************************************/
+bool TDBCSV::OpenDB(PGLOBAL g)
+ {
+ bool rc = false;
+ PCOLDEF cdp;
+ PDOSDEF tdp = (PDOSDEF)To_Def;
+
+ if (Use != USE_OPEN && (Columns || Mode == MODE_UPDATE)) {
+ // Allocate the storage used to read (or write) records
+ int i, len;
+ PCSVCOL colp;
+
+ if (!Fields) // May have been set in TABFMT::OpenDB
+ if (Mode != MODE_UPDATE && Mode != MODE_INSERT) {
+ for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
+ if (!colp->IsSpecial()) // Not a pseudo column
+ Fields = max(Fields, (int)colp->Fldnum);
+
+ if (Columns)
+ Fields++; // Fldnum was 0 based
+
+ } else
+ for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
+ Fields++;
+
+ Offset = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
+ Fldlen = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
+
+ if (Mode == MODE_INSERT || Mode == MODE_UPDATE) {
+ Field = (PSZ*)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
+ Fldtyp = (bool*)PlugSubAlloc(g, NULL, sizeof(bool) * Fields);
+ } // endif Mode
+
+ for (i = 0; i < Fields; i++) {
+ Offset[i] = 0;
+ Fldlen[i] = 0;
+
+ if (Field) {
+ Field[i] = NULL;
+ Fldtyp[i] = false;
+ } // endif Field
+
+ } // endfor i
+
+ if (Field)
+ // Prepare writing fields
+ if (Mode != MODE_UPDATE)
+ for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next) {
+ i = colp->Fldnum;
+ len = colp->GetLength();
+ Field[i] = (PSZ)PlugSubAlloc(g, NULL, len + 1);
+ Field[i][len] = '\0';
+ Fldlen[i] = len;
+ Fldtyp[i] = IsTypeNum(colp->GetResultType());
+ } // endfor colp
+
+ else // MODE_UPDATE
+ for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext()) {
+ i = cdp->GetOffset() - 1;
+ len = cdp->GetLength();
+ Field[i] = (PSZ)PlugSubAlloc(g, NULL, len + 1);
+ Field[i][len] = '\0';
+ Fldlen[i] = len;
+ Fldtyp[i] = IsTypeNum(cdp->GetType());
+ } // endfor colp
+
+ } // endif Use
+
+ if (Header) {
+ // Check that the Lrecl is at least equal to the header line length
+ int headlen = 0;
+ PCOLDEF cdp;
+ PDOSDEF tdp = (PDOSDEF)To_Def;
+
+ for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
+ headlen += strlen(cdp->GetName()) + 3; // 3 if names are quoted
+
+ if (headlen > Lrecl) {
+ Lrecl = headlen;
+ Txfp->Lrecl = headlen;
+ } // endif headlen
+
+ } // endif Header
+
+ Nerr = 0;
+ rc = TDBDOS::OpenDB(g);
+
+ if (!rc && Mode == MODE_UPDATE && To_Kindex)
+ // Because KINDEX::Init is executed in mode READ, we must restore
+ // the Fldlen array that was modified when reading the table file.
+ for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
+ Fldlen[cdp->GetOffset() - 1] = cdp->GetLength();
+
+ return rc;
+ } // end of OpenDB
+
+/***********************************************************************/
+/* SkipHeader: Physically skip first header line if applicable. */
+/* This is called from TDBDOS::OpenDB and must be executed before */
+/* Kindex construction if the file is accessed using an index. */
+/***********************************************************************/
+bool TDBCSV::SkipHeader(PGLOBAL g)
+ {
+ int len = GetFileLength(g);
+ bool rc = false;
+
+#if defined(_DEBUG)
+ if (len < 0)
+ return true;
+#endif // _DEBUG
+
+ if (Header) {
+ if (Mode == MODE_INSERT) {
+ if (!len) {
+ // New file, the header line must be constructed and written
+ int i, n = 0;
+ int hlen = 0;
+ bool q = Qot && Quoted > 0;
+ PCOLDEF cdp;
+
+ // Estimate the length of the header list
+ for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext()) {
+ hlen += (1 + strlen(cdp->GetName()));
+ hlen += ((q) ? 2 : 0);
+ n++; // Calculate the number of columns
+ } // endfor cdp
+
+ if (hlen > Lrecl) {
+ sprintf(g->Message, MSG(LRECL_TOO_SMALL), hlen);
+ return true;
+ } // endif hlen
+
+ // File is empty, write a header record
+ memset(To_Line, 0, Lrecl);
+
+ // The column order in the file is given by the offset value
+ for (i = 1; i <= n; i++)
+ for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext())
+ if (cdp->GetOffset() == i) {
+ if (q)
+ To_Line[strlen(To_Line)] = Qot;
+
+ strcat(To_Line, cdp->GetName());
+
+ if (q)
+ To_Line[strlen(To_Line)] = Qot;
+
+ if (i < n)
+ To_Line[strlen(To_Line)] = Sep;
+
+ } // endif Offset
+
+ rc = (Txfp->WriteBuffer(g) == RC_FX);
+ } // endif !FileLength
+
+ } else if (Mode == MODE_DELETE) {
+ if (len)
+ rc = (Txfp->SkipRecord(g, true) == RC_FX);
+
+ } else if (len) // !Insert && !Delete
+ rc = (Txfp->SkipRecord(g, false) == RC_FX || Txfp->RecordPos(g));
+
+ } // endif Header
+
+ return rc;
+ } // end of SkipHeader
+
+/***********************************************************************/
+/* ReadBuffer: Physical read routine for the CSV access method. */
+/***********************************************************************/
+int TDBCSV::ReadBuffer(PGLOBAL g)
+ {
+ char *p1, *p2, *p = NULL;
+ int i, n, len, rc = Txfp->ReadBuffer(g);
+ bool bad = false;
+
+ if (trace > 1)
+ htrc("CSV: Row is '%s' rc=%d\n", To_Line, rc);
+
+ if (rc != RC_OK || !Fields)
+ return rc;
+ else
+ p2 = To_Line;
+
+ // Find the offsets and lengths of the columns for this row
+ for (i = 0; i < Fields; i++) {
+ if (!bad) {
+ if (Qot && *p2 == Qot) { // Quoted field
+ for (n = 0, p1 = ++p2; (p = strchr(p1, Qot)); p1 = p + 2)
+ if (*(p + 1) == Qot)
+ n++; // Doubled internal quotes
+ else
+ break; // Final quote
+
+ if (p) {
+ len = p++ - p2;
+
+// if (Sep != ' ')
+// for (; *p == ' '; p++) ; // Skip blanks
+
+ if (*p != Sep && i != Fields - 1) { // Should be the separator
+ if (CheckErr()) {
+ sprintf(g->Message, MSG(MISSING_FIELD),
+ i+1, Name, RowNumber(g));
+ return RC_FX;
+ } else if (Accept)
+ bad = true;
+ else
+ return RC_NF;
+
+ } // endif p
+
+ if (n) {
+ int j, k;
+
+ // Suppress the double of internal quotes
+ for (j = k = 0; j < len; j++, k++) {
+ if (p2[j] == Qot)
+ j++; // skip first one
+
+ p2[k] = p2[j];
+ } // endfor i, j
+
+ len -= n;
+ } // endif n
+
+ } else if (CheckErr()) {
+ sprintf(g->Message, MSG(BAD_QUOTE_FIELD),
+ Name, i+1, RowNumber(g));
+ return RC_FX;
+ } else if (Accept) {
+ len = strlen(p2);
+ bad = true;
+ } else
+ return RC_NF;
+
+ } else if ((p = strchr(p2, Sep)))
+ len = p - p2;
+ else if (i == Fields - 1)
+ len = strlen(p2);
+ else if (Accept && Maxerr == 0) {
+ len = strlen(p2);
+ bad = true;
+ } else if (CheckErr()) {
+ sprintf(g->Message, MSG(MISSING_FIELD), i+1, Name, RowNumber(g));
+ return RC_FX;
+ } else if (Accept) {
+ len = strlen(p2);
+ bad = true;
+ } else
+ return RC_NF;
+
+ } else
+ len = 0;
+
+ Offset[i] = p2 - To_Line;
+
+ if (Mode != MODE_UPDATE)
+ Fldlen[i] = len;
+ else if (len > Fldlen[i]) {
+ sprintf(g->Message, MSG(FIELD_TOO_LONG), i+1, RowNumber(g));
+ return RC_FX;
+ } else {
+ strncpy(Field[i], p2, len);
+ Field[i][len] = '\0';
+ } // endif Mode
+
+ if (p)
+ p2 = p + 1;
+
+ } // endfor i
+
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* Data Base write routine CSV file access method. */
+/***********************************************************************/
+int TDBCSV::WriteDB(PGLOBAL g)
+ {
+ char sep[2], qot[2];
+ int i, nlen, oldlen = strlen(To_Line);
+
+ if (trace > 1)
+ htrc("CSV WriteDB: R%d Mode=%d key=%p link=%p\n",
+ Tdb_No, Mode, To_Key_Col, To_Link);
+
+ // Before writing the line we must check its length
+ if ((nlen = CheckWrite(g)) < 0)
+ return RC_FX;
+
+ // Before writing the line we must make it
+ sep[0] = Sep;
+ sep[1] = '\0';
+ qot[0] = Qot;
+ qot[1] = '\0';
+ *To_Line = '\0';
+
+ for (i = 0; i < Fields; i++) {
+ if (i)
+ strcat(To_Line, sep);
+
+ if (Field[i])
+ if (!strlen(Field[i])) {
+ // Generally null fields are not quoted
+ if (Quoted > 2)
+ // Except if explicitely required
+ strcat(strcat(To_Line, qot), qot);
+
+ } else if (Qot && (strchr(Field[i], Sep) || *Field[i] == Qot
+ || Quoted > 1 || (Quoted == 1 && !Fldtyp[i])))
+ if (strchr(Field[i], Qot)) {
+ // Field contains quotes that must be doubled
+ int j, k = strlen(To_Line), n = strlen(Field[i]);
+
+ To_Line[k++] = Qot;
+
+ for (j = 0; j < n; j++) {
+ if (Field[i][j] == Qot)
+ To_Line[k++] = Qot;
+
+ To_Line[k++] = Field[i][j];
+ } // endfor j
+
+ To_Line[k++] = Qot;
+ To_Line[k] = '\0';
+ } else
+ strcat(strcat(strcat(To_Line, qot), Field[i]), qot);
+
+ else
+ strcat(To_Line, Field[i]);
+
+ } // endfor i
+
+#if defined(_DEBUG)
+ assert ((unsigned)nlen == strlen(To_Line));
+#endif
+
+ if (Mode == MODE_UPDATE && nlen < oldlen
+ && !((PDOSFAM)Txfp)->GetUseTemp()) {
+ // In Update mode with no temp file, line length must not change
+ To_Line[nlen] = Sep;
+
+ for (nlen++; nlen < oldlen; nlen++)
+ To_Line[nlen] = ' ';
+
+ To_Line[nlen] = '\0';
+ } // endif
+
+ if (trace > 1)
+ htrc("Write: line is=%s", To_Line);
+
+ /*********************************************************************/
+ /* Now start the writing process. */
+ /*********************************************************************/
+ return Txfp->WriteBuffer(g);
+ } // end of WriteDB
+
+/***********************************************************************/
+/* Check whether a new line fit in the file lrecl size. */
+/***********************************************************************/
+int TDBCSV::CheckWrite(PGLOBAL g)
+ {
+ int maxlen, n, nlen = (Fields - 1);
+
+ if (trace > 1)
+ htrc("CheckWrite: R%d Mode=%d\n", Tdb_No, Mode);
+
+ // Before writing the line we must check its length
+ maxlen = (Mode == MODE_UPDATE && !Txfp->GetUseTemp())
+ ? strlen(To_Line) : Lrecl;
+
+ // Check whether record is too int
+ for (int i = 0; i < Fields; i++)
+ if (Field[i]) {
+ if (!(n = strlen(Field[i])))
+ n += (Quoted > 2 ? 2 : 0);
+ else if (strchr(Field[i], Sep) || (Qot && *Field[i] == Qot)
+ || Quoted > 1 || (Quoted == 1 && !Fldtyp[i]))
+ if (!Qot) {
+ sprintf(g->Message, MSG(SEP_IN_FIELD), i + 1);
+ return -1;
+ } else {
+ // Quotes inside a quoted field must be doubled
+ char *p1, *p2;
+
+ for (p1 = Field[i]; (p2 = strchr(p1, Qot)); p1 = p2 + 1)
+ n++;
+
+ n += 2; // Outside quotes
+ } // endif
+
+ if ((nlen += n) > maxlen) {
+ strcpy(g->Message, MSG(LINE_TOO_LONG));
+ return -1;
+ } // endif nlen
+
+ } // endif Field
+
+ return nlen;
+ } // end of CheckWrite
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Implementation of the TDBFMT class. */
+/***********************************************************************/
+TDBFMT::TDBFMT(PGLOBAL g, PTDBFMT tdbp) : TDBCSV(g, tdbp)
+ {
+ FldFormat = tdbp->FldFormat;
+ To_Fld = tdbp->To_Fld;
+ FmtTest = tdbp->FmtTest;
+ Linenum = tdbp->Linenum;
+ } // end of TDBFMT copy constructor
+
+// Method
+PTDB TDBFMT::CopyOne(PTABS t)
+ {
+ PTDB tp;
+ PCSVCOL cp1, cp2;
+//PFMTCOL cp1, cp2;
+ PGLOBAL g = t->G; // Is this really useful ???
+
+ tp = new(g) TDBFMT(g, this);
+
+ for (cp1 = (PCSVCOL)Columns; cp1; cp1 = (PCSVCOL)cp1->GetNext()) {
+//for (cp1 = (PFMTCOL)Columns; cp1; cp1 = (PFMTCOL)cp1->GetNext()) {
+ cp2 = new(g) CSVCOL(cp1, tp); // Make a copy
+// cp2 = new(g) FMTCOL(cp1, tp); // Make a copy
+ NewPointer(t, cp1, cp2);
+ } // endfor cp1
+
+ return tp;
+ } // end of CopyOne
+
+/***********************************************************************/
+/* Allocate FMT column description block. */
+/***********************************************************************/
+PCOL TDBFMT::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
+ {
+ return new(g) CSVCOL(g, cdp, this, cprec, n);
+//return new(g) FMTCOL(cdp, this, cprec, n);
+ } // end of MakeCol
+
+/***********************************************************************/
+/* FMT EstimatedLength. Returns an estimated minimum line length. */
+/* The big problem here is how can we astimated that minimum ? */
+/***********************************************************************/
+int TDBFMT::EstimatedLength(PGLOBAL g)
+ {
+ // This is rather stupid !!!
+ return ((PDOSDEF)To_Def)->GetEnding() + (int)((Lrecl / 10) + 1);
+ } // end of EstimatedLength
+
+/***********************************************************************/
+/* FMT Access Method opening routine. */
+/***********************************************************************/
+bool TDBFMT::OpenDB(PGLOBAL g)
+ {
+ Linenum = 0;
+
+ if (Mode == MODE_INSERT || Mode == MODE_UPDATE) {
+ sprintf(g->Message, MSG(FMT_WRITE_NIY), "FMT");
+ return true; // NIY
+ } // endif Mode
+
+ if (Use != USE_OPEN && Columns) {
+ // Make the formats used to read records
+ PSZ pfm;
+ int i, n;
+ PCSVCOL colp;
+ PCOLDEF cdp;
+ PDOSDEF tdp = (PDOSDEF)To_Def;
+
+ for (colp = (PCSVCOL)Columns; colp; colp = (PCSVCOL)colp->Next)
+ if (!colp->IsSpecial()) // Not a pseudo column
+ Fields = max(Fields, (int)colp->Fldnum);
+
+ if (Columns)
+ Fields++; // Fldnum was 0 based
+
+ To_Fld = PlugSubAlloc(g, NULL, Lrecl + 1);
+ FldFormat = (PSZ*)PlugSubAlloc(g, NULL, sizeof(PSZ) * Fields);
+ memset(FldFormat, 0, sizeof(PSZ) * Fields);
+ FmtTest = (int*)PlugSubAlloc(g, NULL, sizeof(int) * Fields);
+ memset(FmtTest, 0, sizeof(int) * Fields);
+
+ // Get the column formats
+ for (cdp = tdp->GetCols(); cdp; cdp = cdp->GetNext())
+ if ((i = cdp->GetOffset() - 1) < Fields) {
+ if (!(pfm = cdp->GetFmt())) {
+ sprintf(g->Message, MSG(NO_FLD_FORMAT), i + 1, Name);
+ return true;
+ } // endif pfm
+
+ // Roughly check the Fmt format
+ if ((n = strlen(pfm) - 2) < 4) {
+ sprintf(g->Message, MSG(BAD_FLD_FORMAT), i + 1, Name);
+ return true;
+ } // endif n
+
+ FldFormat[i] = (PSZ)PlugSubAlloc(g, NULL, n + 5);
+ strcpy(FldFormat[i], pfm);
+
+ if (!strcmp(pfm + n, "%m")) {
+ // This is a field that can be missing. Flag it so it can
+ // be handled with special processing.
+ FldFormat[i][n+1] = 'n'; // To have sscanf normal processing
+ FmtTest[i] = 2;
+ } else if (i+1 < Fields && strcmp(pfm + n, "%n")) {
+ // There are trailing characters after the field contents
+ // add a marker for the next field start position.
+ strcat(FldFormat[i], "%n");
+ FmtTest[i] = 1;
+ } // endif's
+
+ } // endif i
+
+ } // endif Use
+
+ return TDBCSV::OpenDB(g);
+ } // end of OpenDB
+
+/***********************************************************************/
+/* ReadBuffer: Physical read routine for the FMT access method. */
+/***********************************************************************/
+int TDBFMT::ReadBuffer(PGLOBAL g)
+ {
+ int i, len, n, deb, fin, nwp, pos = 0, rc;
+ bool bad = false;
+
+ if ((rc = Txfp->ReadBuffer(g)) != RC_OK || !Fields)
+ return rc;
+ else
+ ++Linenum;
+
+ if (trace > 1)
+ htrc("FMT: Row %d is '%s' rc=%d\n", Linenum, To_Line, rc);
+
+ // Find the offsets and lengths of the columns for this row
+ for (i = 0; i < Fields; i++) {
+ if (!bad) {
+ deb = fin = -1;
+
+ if (!FldFormat[i]) {
+ n = 0;
+ } else if (FmtTest[i] == 1) {
+ nwp = -1;
+ n = sscanf(To_Line + pos, FldFormat[i], &deb, To_Fld, &fin, &nwp);
+ } else {
+ n = sscanf(To_Line + pos, FldFormat[i], &deb, To_Fld, &fin);
+
+ if (n != 1 && (deb >= 0 || i == Fields - 1) && FmtTest[i] == 2) {
+ // Missing optional field, not an error
+ n = 1;
+
+ if (i == Fields - 1)
+ fin = deb = 0;
+ else
+ fin = deb;
+
+ } // endif n
+
+ nwp = fin;
+ } // endif i
+
+ if (n != 1 || deb < 0 || fin < 0 || nwp < 0) {
+ // This is to avoid a very strange sscanf bug occuring
+ // with fields that ends with a null character.
+ // This bug causes subsequent sscanf to return in error,
+ // so next lines are not parsed correctly.
+ sscanf("a", "%*c"); // Seems to reset things Ok
+
+ if (CheckErr()) {
+ sprintf(g->Message, MSG(BAD_LINEFLD_FMT), Linenum, i + 1, Name);
+ return RC_FX;
+ } else if (Accept)
+ bad = true;
+ else
+ return RC_NF;
+
+ } // endif n...
+
+ } // endif !bad
+
+ if (!bad) {
+ Offset[i] = pos + deb;
+ len = fin - deb;
+ } else {
+ nwp = 0;
+ Offset[i] = pos;
+ len = 0;
+ } // endif bad
+
+// if (Mode != MODE_UPDATE)
+ Fldlen[i] = len;
+// else if (len > Fldlen[i]) {
+// sprintf(g->Message, MSG(FIELD_TOO_LONG), i+1, To_Tdb->RowNumber(g));
+// return RC_FX;
+// } else {
+// strncpy(Field[i], To_Line + pos, len);
+// Field[i][len] = '\0';
+// } // endif Mode
+
+ pos += nwp;
+ } // endfor i
+
+ if (bad)
+ Nerr++;
+ else
+ sscanf("a", "%*c"); // Seems to reset things Ok
+
+ return rc;
+ } // end of ReadBuffer
+
+/***********************************************************************/
+/* Data Base write routine FMT file access method. */
+/***********************************************************************/
+int TDBFMT::WriteDB(PGLOBAL g)
+ {
+ sprintf(g->Message, MSG(FMT_WRITE_NIY), "FMT");
+ return RC_FX; // NIY
+ } // end of WriteDB
+
+// ------------------------ CSVCOL functions ----------------------------
+
+/***********************************************************************/
+/* CSVCOL public constructor */
+/***********************************************************************/
+CSVCOL::CSVCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i)
+ : DOSCOL(g, cdp, tdbp, cprec, i, "CSV")
+ {
+ Fldnum = Deplac - 1;
+ Deplac = 0;
+ } // end of CSVCOL constructor
+
+/***********************************************************************/
+/* CSVCOL constructor used for copying columns. */
+/* tdbp is the pointer to the new table descriptor. */
+/***********************************************************************/
+CSVCOL::CSVCOL(CSVCOL *col1, PTDB tdbp) : DOSCOL(col1, tdbp)
+ {
+ Fldnum = col1->Fldnum;
+ } // end of CSVCOL copy constructor
+
+/***********************************************************************/
+/* VarSize: This function tells UpdateDB whether or not the block */
+/* optimization file must be redone if this column is updated, even */
+/* it is not sorted or clustered. This applies to a blocked table, */
+/* because if it is updated using a temporary file, the block size */
+/* may be modified. */
+/***********************************************************************/
+bool CSVCOL::VarSize(void)
+ {
+ PTXF txfp = ((PTDBCSV)To_Tdb)->Txfp;
+
+ if (txfp->IsBlocked() && txfp->GetUseTemp())
+ // Blocked table using a temporary file
+ return true;
+ else
+ return false;
+
+ } // end VarSize
+
+/***********************************************************************/
+/* ReadColumn: call DOSCOL::ReadColumn after having set the offet */
+/* and length of the field to read as calculated by TDBCSV::ReadDB. */
+/***********************************************************************/
+void CSVCOL::ReadColumn(PGLOBAL g)
+ {
+ int rc;
+ PTDBCSV tdbp = (PTDBCSV)To_Tdb;
+
+ /*********************************************************************/
+ /* If physical reading of the line was deferred, do it now. */
+ /*********************************************************************/
+ if (!tdbp->IsRead())
+ if ((rc = tdbp->ReadBuffer(g)) != RC_OK) {
+ if (rc == RC_EF)
+ sprintf(g->Message, MSG(INV_DEF_READ), rc);
+
+ longjmp(g->jumper[g->jump_level], 34);
+ } // endif
+
+ if (tdbp->Mode != MODE_UPDATE) {
+ int colen = Long; // Column length
+
+ // Set the field offset and length for this row
+ Deplac = tdbp->Offset[Fldnum]; // Field offset
+ Long = tdbp->Fldlen[Fldnum]; // Field length
+
+ if (trace > 1)
+ htrc("CSV ReadColumn %s Fldnum=%d offset=%d fldlen=%d\n",
+ Name, Fldnum, Deplac, Long);
+
+ if (Long > colen && tdbp->CheckErr()) {
+ Long = colen; // Restore column length
+ sprintf(g->Message, MSG(FLD_TOO_LNG_FOR),
+ Fldnum + 1, Name, To_Tdb->RowNumber(g), tdbp->GetFile(g));
+ longjmp(g->jumper[g->jump_level], 34);
+ } // endif Long
+
+ // Now do the reading
+ DOSCOL::ReadColumn(g);
+
+ // Restore column length
+ Long = colen;
+ } else { // Mode Update
+ // Field have been copied in TDB Field array
+ PSZ fp = tdbp->Field[Fldnum];
+
+ Value->SetValue_psz(fp);
+
+ // Set null when applicable
+ if (Nullable)
+ Value->SetNull(Value->IsZero());
+
+ } // endif Mode
+
+ } // end of ReadColumn
+
+/***********************************************************************/
+/* WriteColumn: The column is written in TDBCSV matching Field. */
+/***********************************************************************/
+void CSVCOL::WriteColumn(PGLOBAL g)
+ {
+ char *p, buf[64];
+ int flen;
+ PTDBCSV tdbp = (PTDBCSV)To_Tdb;
+
+ if (trace > 1)
+ htrc("CSV WriteColumn: col %s R%d coluse=%.4X status=%.4X\n",
+ Name, tdbp->GetTdb_No(), ColUse, Status);
+
+ flen = GetLength();
+
+ if (trace > 1)
+ htrc("Lrecl=%d Long=%d field=%d coltype=%d colval=%p\n",
+ tdbp->Lrecl, Long, flen, Buf_Type, Value);
+
+ /*********************************************************************/
+ /* Check whether the new value has to be converted to Buf_Type. */
+ /*********************************************************************/
+ if (Value != To_Val)
+ Value->SetValue_pval(To_Val, false); // Convert the updated value
+
+ /*********************************************************************/
+ /* Get the string representation of the column value. */
+ /*********************************************************************/
+ p = Value->ShowValue(buf);
+
+ if (trace > 1)
+ htrc("new length(%p)=%d\n", p, strlen(p));
+
+ if ((signed)strlen(p) > flen) {
+ sprintf(g->Message, MSG(BAD_FLD_LENGTH), Name, p, flen,
+ tdbp->RowNumber(g), tdbp->GetFile(g));
+ longjmp(g->jumper[g->jump_level], 34);
+ } // endif
+
+ if (trace > 1)
+ htrc("buffer=%s\n", p);
+
+ /*********************************************************************/
+ /* Updating must be done also during the first pass so writing the */
+ /* updated record can be checked for acceptable record length. */
+ /*********************************************************************/
+ if (Fldnum < 0) {
+ // This can happen for wrong offset value in XDB files
+ sprintf(g->Message, MSG(BAD_FIELD_RANK), Fldnum + 1, Name);
+ longjmp(g->jumper[g->jump_level], 34);
+ } else
+ strncpy(tdbp->Field[Fldnum], p, flen);
+
+ if (trace > 1)
+ htrc(" col written: '%s'\n", p);
+
+ } // end of WriteColumn
+
+/* ---------------------------TDBCCL class --------------------------- */
+
+/***********************************************************************/
+/* TDBCCL class constructor. */
+/***********************************************************************/
+TDBCCL::TDBCCL(PCSVDEF tdp) : TDBCAT(tdp)
+ {
+ Fn = tdp->GetFn();
+ Hdr = tdp->Header;
+ Mxr = tdp->Maxerr;
+ Qtd = tdp->Quoted;
+ Sep = tdp->Sep;
+ } // end of TDBCCL constructor
+
+/***********************************************************************/
+/* GetResult: Get the list the CSV file columns. */
+/***********************************************************************/
+PQRYRES TDBCCL::GetResult(PGLOBAL g)
+ {
+ return CSVColumns(g, Fn, Sep, Qtd, Hdr, Mxr, false);
+ } // end of GetResult
+
+/* ------------------------ End of TabFmt ---------------------------- */
diff --git a/storage/connect/tabmysql.cpp b/storage/connect/tabmysql.cpp
index eb6efca9e00..2556051249c 100644
--- a/storage/connect/tabmysql.cpp
+++ b/storage/connect/tabmysql.cpp
@@ -1,1620 +1,1620 @@
-/************* TabMySQL C++ Program Source Code File (.CPP) *************/
-/* PROGRAM NAME: TABMYSQL */
-/* ------------- */
-/* Version 1.7 */
-/* */
-/* AUTHOR: */
-/* ------- */
-/* Olivier BERTRAND 2007-2013 */
-/* */
-/* WHAT THIS PROGRAM DOES: */
-/* ----------------------- */
-/* Implements a table type that are MySQL tables. */
-/* It can optionally use the embedded MySQL library. */
-/* */
-/* WHAT YOU NEED TO COMPILE THIS PROGRAM: */
-/* -------------------------------------- */
-/* */
-/* REQUIRED FILES: */
-/* --------------- */
-/* TABMYSQL.CPP - Source code */
-/* PLGDBSEM.H - DB application declaration file */
-/* TABMYSQL.H - TABODBC 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 */
-/* */
-/************************************************************************/
-#define MYSQL_SERVER 1
-#include "my_global.h"
-#include "sql_class.h"
-#include "sql_servers.h"
-#if defined(WIN32)
-//#include <windows.h>
-#else // !WIN32
-//#include <fnmatch.h>
-//#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "osutil.h"
-//#include <io.h>
-//#include <fcntl.h>
-#endif // !WIN32
-
-/***********************************************************************/
-/* Include application header files: */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "xtable.h"
-#include "tabcol.h"
-#include "colblk.h"
-#include "mycat.h"
-#include "reldef.h"
-#include "tabmysql.h"
-#include "valblk.h"
-#include "tabutil.h"
-
-#if defined(_CONSOLE)
-void PrintResult(PGLOBAL, PSEM, PQRYRES);
-#endif // _CONSOLE
-
-extern "C" int trace;
-
-/* -------------- Implementation of the MYSQLDEF class --------------- */
-
-/***********************************************************************/
-/* Constructor. */
-/***********************************************************************/
-MYSQLDEF::MYSQLDEF(void)
- {
- Pseudo = 2; // SERVID is Ok but not ROWID
- Hostname = NULL;
- Database = NULL;
- Tabname = NULL;
- Srcdef = NULL;
- Username = NULL;
- Password = NULL;
- Portnumber = 0;
- Isview = FALSE;
- Bind = FALSE;
- Delayed = FALSE;
- Xsrc = FALSE;
- } // end of MYSQLDEF constructor
-
-/***********************************************************************/
-/* Get connection info from the declared server. */
-/***********************************************************************/
-bool MYSQLDEF::GetServerInfo(PGLOBAL g, const char *server_name)
-{
- THD *thd= current_thd;
- MEM_ROOT *mem= thd->mem_root;
- FOREIGN_SERVER *server, server_buffer;
- DBUG_ENTER("GetServerInfo");
- DBUG_PRINT("info", ("server_name %s", server_name));
-
- if (!server_name || !strlen(server_name)) {
- DBUG_PRINT("info", ("server_name not defined!"));
- strcpy(g->Message, "server_name not defined!");
- DBUG_RETURN(true);
- } // endif server_name
-
- // get_server_by_name() clones the server if exists and allocates
- // copies of strings in the supplied mem_root
- if (!(server= get_server_by_name(mem, server_name, &server_buffer))) {
- DBUG_PRINT("info", ("get_server_by_name returned > 0 error condition!"));
- /* need to come up with error handling */
- strcpy(g->Message, "get_server_by_name returned > 0 error condition!");
- DBUG_RETURN(true);
- } // endif server
-
- DBUG_PRINT("info", ("get_server_by_name returned server at %lx",
- (long unsigned int) server));
-
- // TODO: We need to examine which of these can really be NULL
- Hostname = PlugDup(g, server->host);
- Database = PlugDup(g, server->db);
- Username = PlugDup(g, server->username);
- Password = PlugDup(g, server->password);
- Portnumber = (server->port) ? server->port : GetDefaultPort();
-
- DBUG_RETURN(false);
-} // end of GetServerInfo
-
-/***********************************************************************/
-/* Parse connection string */
-/* */
-/* SYNOPSIS */
-/* ParseURL() */
-/* url The connection string to parse */
-/* */
-/* DESCRIPTION */
-/* Populates the table with information about the connection */
-/* to the foreign database that will serve as the data source. */
-/* This string must be specified (currently) in the "CONNECTION" */
-/* field, listed in the CREATE TABLE statement. */
-/* */
-/* This string MUST be in the format of any of these: */
-/* */
-/* CONNECTION="scheme://user:pwd@host:port/database/table" */
-/* CONNECTION="scheme://user@host/database/table" */
-/* CONNECTION="scheme://user@host:port/database/table" */
-/* CONNECTION="scheme://user:pwd@host/database/table" */
-/* */
-/* _OR_ */
-/* */
-/* CONNECTION="connection name" (NIY) */
-/* */
-/* An Example: */
-/* */
-/* CREATE TABLE t1 (id int(32)) */
-/* ENGINE="CONNECT" TABLE_TYPE="MYSQL" */
-/* CONNECTION="mysql://joe:pwd@192.168.1.111:9308/dbname/tabname"; */
-/* */
-/* CREATE TABLE t2 ( */
-/* id int(4) NOT NULL auto_increment, */
-/* name varchar(32) NOT NULL, */
-/* PRIMARY KEY(id) */
-/* ) ENGINE="CONNECT" TABLE_TYPE="MYSQL" */
-/* CONNECTION="my_conn"; (NIY) */
-/* */
-/* 'password' and 'port' are both optional. */
-/* */
-/* RETURN VALUE */
-/* false success */
-/* true error */
-/* */
-/***********************************************************************/
-bool MYSQLDEF::ParseURL(PGLOBAL g, char *url, bool b)
- {
- if ((!strstr(url, "://") && (!strchr(url, '@')))) {
- // No :// or @ in connection string. Must be a straight
- // connection name of either "server" or "server/table"
- // ok, so we do a little parsing, but not completely!
- if ((Tabname= strchr(url, '/'))) {
- // If there is a single '/' in the connection string,
- // this means the user is specifying a table name
- *Tabname++= '\0';
-
- // there better not be any more '/'s !
- if (strchr(Tabname, '/'))
- return true;
-
- } else
- // Otherwise, straight server name,
- // use tablename of federatedx table as remote table name
- Tabname= Name;
-
- if (trace)
- htrc("server: %s Tabname: %s", url, Tabname);
-
- Server = url;
- return GetServerInfo(g, url);
- } else {
- // URL, parse it
- char *sport, *scheme = url;
-
- if (!(Username = strstr(url, "://"))) {
- strcpy(g->Message, "Connection is not an URL");
- return true;
- } // endif User
-
- scheme[Username - scheme] = 0;
-
- if (stricmp(scheme, "mysql")) {
- strcpy(g->Message, "scheme must be mysql");
- return true;
- } // endif scheme
-
- Username += 3;
-
- if (!(Hostname = strchr(Username, '@'))) {
- strcpy(g->Message, "No host specified in URL");
- return true;
- } else {
- *Hostname++ = 0; // End Username
- Server = Hostname;
- } // endif Hostname
-
- if ((Password = strchr(Username, ':'))) {
- *Password++ = 0; // End username
-
- // Make sure there isn't an extra / or @
- if ((strchr(Password, '/') || strchr(Hostname, '@'))) {
- strcpy(g->Message, "Syntax error in URL");
- return true;
- } // endif
-
- // Found that if the string is:
- // user:@hostname:port/db/table
- // Then password is a null string, so set to NULL
- if ((Password[0] == 0))
- Password = NULL;
-
- } // endif password
-
- // Make sure there isn't an extra / or @ */
- if ((strchr(Username, '/')) || (strchr(Hostname, '@'))) {
- strcpy(g->Message, "Syntax error in URL");
- return true;
- } // endif
-
- if ((Database = strchr(Hostname, '/'))) {
- *Database++ = 0;
-
- if ((Tabname = strchr(Database, '/'))) {
- *Tabname++ = 0;
-
- // Make sure there's not an extra /
- if ((strchr(Tabname, '/'))) {
- strcpy(g->Message, "Syntax error in URL");
- return true;
- } // endif /
-
- } // endif Tabname
-
- } // endif database
-
- if ((sport = strchr(Hostname, ':')))
- *sport++ = 0;
-
- // For unspecified values, get the values of old style options
- // but only if called from MYSQLDEF, else set them to NULL
- Portnumber = (sport && sport[0]) ? atoi(sport)
- : (b) ? Cat->GetIntCatInfo("Port", GetDefaultPort()) : 0;
-
- if (Username[0] == 0)
- Username = (b) ? Cat->GetStringCatInfo(g, "User", "*") : NULL;
-
- if (Hostname[0] == 0)
- Hostname = (b) ? Cat->GetStringCatInfo(g, "Host", "localhost") : NULL;
-
- if (!Database || !*Database)
- Database = (b) ? Cat->GetStringCatInfo(g, "Database", "*") : NULL;
-
- if (!Tabname || !*Tabname)
- Tabname = (b) ? Cat->GetStringCatInfo(g, "Tabname", Name) : NULL;
-
- if (!Password)
- Password = (b) ? Cat->GetStringCatInfo(g, "Password", NULL) : NULL;
- } // endif URL
-
-#if 0
- if (!share->port)
- if (!share->hostname || strcmp(share->hostname, my_localhost) == 0)
- share->socket= (char *) MYSQL_UNIX_ADDR;
- else
- share->port= MYSQL_PORT;
-#endif // 0
-
- return false;
- } // end of ParseURL
-
-/***********************************************************************/
-/* DefineAM: define specific AM block values from XCV file. */
-/***********************************************************************/
-bool MYSQLDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
- {
- char *url;
-
- Desc = "MySQL Table";
-
- if (stricmp(am, "MYPRX")) {
- // Normal case of specific MYSQL table
- url = Cat->GetStringCatInfo(g, "Connect", NULL);
-
- if (!url || !*url) {
- // Not using the connection URL
- Hostname = Cat->GetStringCatInfo(g, "Host", "localhost");
- Database = Cat->GetStringCatInfo(g, "Database", "*");
- Tabname = Cat->GetStringCatInfo(g, "Name", Name); // Deprecated
- Tabname = Cat->GetStringCatInfo(g, "Tabname", Tabname);
- Username = Cat->GetStringCatInfo(g, "User", "*");
- Password = Cat->GetStringCatInfo(g, "Password", NULL);
- Portnumber = Cat->GetIntCatInfo("Port", GetDefaultPort());
- Server = Hostname;
- } else if (ParseURL(g, url))
- return true;
-
- Bind = !!Cat->GetIntCatInfo("Bind", 0);
- Delayed = !!Cat->GetIntCatInfo("Delayed", 0);
- } else {
- // MYSQL access from a PROXY table
- Database = Cat->GetStringCatInfo(g, "Database", "*");
- Isview = Cat->GetBoolCatInfo("View", FALSE);
-
- // We must get other connection parms from the calling table
- Remove_tshp(Cat);
- url = Cat->GetStringCatInfo(g, "Connect", NULL);
-
- if (!url || !*url) {
- Hostname = Cat->GetStringCatInfo(g, "Host", "localhost");
- Username = Cat->GetStringCatInfo(g, "User", "*");
- Password = Cat->GetStringCatInfo(g, "Password", NULL);
- Portnumber = Cat->GetIntCatInfo("Port", GetDefaultPort());
- Server = Hostname;
- } else {
- char *locdb = Database;
-
- if (ParseURL(g, url))
- return true;
-
- Database = locdb;
- } // endif url
-
- Tabname = Name;
- } // endif am
-
- if ((Srcdef = Cat->GetStringCatInfo(g, "Srcdef", NULL)))
- Isview = true;
-
- // Used for Update and Delete
- Qrystr = Cat->GetStringCatInfo(g, "Query_String", "?");
- Quoted = Cat->GetIntCatInfo("Quoted", 0);
-
- // Specific for command executing tables
- Xsrc = Cat->GetBoolCatInfo("Execsrc", false);
- Mxr = Cat->GetIntCatInfo("Maxerr", 0);
- return FALSE;
- } // end of DefineAM
-
-/***********************************************************************/
-/* GetTable: makes a new TDB of the proper type. */
-/***********************************************************************/
-PTDB MYSQLDEF::GetTable(PGLOBAL g, MODE m)
- {
- if (Xsrc)
- return new(g) TDBMYEXC(this);
- else if (Catfunc == FNC_COL)
- return new(g) TDBMCL(this);
- else
- return new(g) TDBMYSQL(this);
-
- } // end of GetTable
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Implementation of the TDBMYSQL class. */
-/***********************************************************************/
-TDBMYSQL::TDBMYSQL(PMYDEF tdp) : TDBASE(tdp)
- {
- if (tdp) {
- Host = tdp->Hostname;
- Database = tdp->Database;
- Tabname = tdp->Tabname;
- Srcdef = tdp->Srcdef;
- User = tdp->Username;
- Pwd = tdp->Password;
- Server = tdp->Server;
- Qrystr = tdp->Qrystr;
- Quoted = max(0, tdp->Quoted);
- Port = tdp->Portnumber;
- Isview = tdp->Isview;
- Prep = tdp->Bind;
- Delayed = tdp->Delayed;
- } else {
- Host = NULL;
- Database = NULL;
- Tabname = NULL;
- Srcdef = NULL;
- User = NULL;
- Pwd = NULL;
- Server = NULL;
- Qrystr = NULL;
- Quoted = 0;
- Port = 0;
- Isview = FALSE;
- Prep = FALSE;
- Delayed = FALSE;
- } // endif tdp
-
- Bind = NULL;
- Query = NULL;
- Qbuf = NULL;
- Fetched = FALSE;
- m_Rc = RC_FX;
- AftRows = 0;
- N = -1;
- Nparm = 0;
- } // end of TDBMYSQL constructor
-
-TDBMYSQL::TDBMYSQL(PGLOBAL g, PTDBMY tdbp) : TDBASE(tdbp)
- {
- Host = tdbp->Host;
- Database = tdbp->Database;
- Tabname = tdbp->Tabname;
- Srcdef = tdbp->Srcdef;
- User = tdbp->User;
- Pwd = tdbp->Pwd;
- Qrystr = tdbp->Qrystr;
- Quoted = tdbp->Quoted;
- Port = tdbp->Port;
- Isview = tdbp->Isview;
- Prep = tdbp->Prep;
- Delayed = tdbp->Delayed;
- Bind = NULL;
- Query = tdbp->Query;
- Qbuf = NULL;
- Fetched = tdbp->Fetched;
- m_Rc = tdbp->m_Rc;
- AftRows = tdbp->AftRows;
- N = tdbp->N;
- Nparm = tdbp->Nparm;
- } // end of TDBMYSQL copy constructor
-
-// Is this really useful ???
-PTDB TDBMYSQL::CopyOne(PTABS t)
- {
- PTDB tp;
- PCOL cp1, cp2;
- PGLOBAL g = t->G;
-
- tp = new(g) TDBMYSQL(g, this);
-
- for (cp1 = Columns; cp1; cp1 = cp1->GetNext()) {
- cp2 = new(g) MYSQLCOL((PMYCOL)cp1, tp);
-
- NewPointer(t, cp1, cp2);
- } // endfor cp1
-
- return tp;
- } // end of CopyOne
-
-/***********************************************************************/
-/* Allocate MYSQL column description block. */
-/***********************************************************************/
-PCOL TDBMYSQL::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
- {
- return new(g) MYSQLCOL(cdp, this, cprec, n);
- } // end of MakeCol
-
-/***********************************************************************/
-/* MakeSelect: make the Select statement use with MySQL connection. */
-/* Note: when implementing EOM filtering, column only used in local */
-/* filter should be removed from column list. */
-/***********************************************************************/
-bool TDBMYSQL::MakeSelect(PGLOBAL g)
- {
- char *tk = "`";
- int rank = 0;
- bool b = FALSE;
- PCOL colp;
-//PDBUSER dup = PlgGetUser(g);
-
- if (Query)
- return FALSE; // already done
-
- if (Srcdef) {
- Query = Srcdef;
- return false;
- } // endif Srcdef
-
- //Find the address of the suballocated query
- Query = (char*)PlugSubAlloc(g, NULL, 0);
- strcpy(Query, "SELECT ");
-
- if (Columns) {
- for (colp = Columns; colp; colp = colp->GetNext())
- if (!colp->IsSpecial()) {
-// if (colp->IsSpecial()) {
-// strcpy(g->Message, MSG(NO_SPEC_COL));
-// return TRUE;
-// } else {
- if (b)
- strcat(Query, ", ");
- else
- b = TRUE;
-
- strcat(strcat(strcat(Query, tk), colp->GetName()), tk);
- ((PMYCOL)colp)->Rank = rank++;
- } // endif colp
-
- } else {
- // ncol == 0 can occur for views or queries such as
- // Query count(*) from... for which we will count the rows from
- // Query '*' from...
- // (the use of a char constant minimize the result storage)
- strcat(Query, (Isview) ? "*" : "'*'");
- } // endif ncol
-
- strcat(strcat(strcat(strcat(Query, " FROM "), tk), Tabname), tk);
-
- if (To_CondFil)
- strcat(strcat(Query, " WHERE "), To_CondFil->Body);
-
- if (trace)
- htrc("Query=%s\n", Query);
-
- // Now we know how much to suballocate
- PlugSubAlloc(g, NULL, strlen(Query) + 1);
- return FALSE;
- } // end of MakeSelect
-
-/***********************************************************************/
-/* MakeInsert: make the Insert statement used with MySQL connection. */
-/***********************************************************************/
-bool TDBMYSQL::MakeInsert(PGLOBAL g)
- {
- char *colist, *valist = NULL;
- char *tk = "`";
- int len = 0, qlen = 0;
- bool b = FALSE;
- PCOL colp;
-
- if (Query)
- return FALSE; // already done
-
- for (colp = Columns; colp; colp = colp->GetNext())
- if (!colp->IsSpecial()) {
-// if (colp->IsSpecial()) {
-// strcpy(g->Message, MSG(NO_SPEC_COL));
-// return TRUE;
-// } else {
- len += (strlen(colp->GetName()) + 4);
- ((PMYCOL)colp)->Rank = Nparm++;
- } // endif colp
-
- colist = (char*)PlugSubAlloc(g, NULL, len);
- *colist = '\0';
-
- if (Prep) {
-#if defined(MYSQL_PREPARED_STATEMENTS)
- valist = (char*)PlugSubAlloc(g, NULL, 2 * Nparm);
- *valist = '\0';
-#else // !MYSQL_PREPARED_STATEMENTS
- strcpy(g->Message, "Prepared statements not used (not supported)");
- PushWarning(g, this);
- Prep = FALSE;
-#endif // !MYSQL_PREPARED_STATEMENTS
- } // endif Prep
-
- for (colp = Columns; colp; colp = colp->GetNext()) {
- if (b) {
- strcat(colist, ", ");
- if (Prep) strcat(valist, ",");
- } else
- b = TRUE;
-
- strcat(strcat(strcat(colist, tk), colp->GetName()), tk);
-
- // Parameter marker
- if (!Prep) {
- if (colp->GetResultType() == TYPE_DATE)
- qlen += 20;
- else
- qlen += colp->GetLength();
-
- } // endif Prep
-
- if (Prep)
- strcat(valist, "?");
-
- } // endfor colp
-
- // Below 40 is enough to contain the fixed part of the query
- len = (strlen(Tabname) + strlen(colist)
- + ((Prep) ? strlen(valist) : 0) + 40);
- Query = (char*)PlugSubAlloc(g, NULL, len);
-
- if (Delayed)
- strcpy(Query, "INSERT DELAYED INTO ");
- else
- strcpy(Query, "INSERT INTO ");
-
- strcat(strcat(strcat(Query, tk), Tabname), tk);
- strcat(strcat(strcat(Query, " ("), colist), ") VALUES (");
-
- if (Prep)
- strcat(strcat(Query, valist), ")");
- else {
- qlen += (strlen(Query) + Nparm);
- Qbuf = (char *)PlugSubAlloc(g, NULL, qlen);
- } // endelse Prep
-
- return FALSE;
- } // end of MakeInsert
-
-/***********************************************************************/
-/* MakeCommand: make the Update or Delete statement to send to the */
-/* MySQL server. Limited to remote values and filtering. */
-/***********************************************************************/
-int TDBMYSQL::MakeCommand(PGLOBAL g)
- {
- Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
-
- if (Quoted > 0 || stricmp(Name, Tabname)) {
- char *p, *qrystr, name[68];
- bool qtd = Quoted > 0;
-
-
- // Make a lower case copy of the originale query
- qrystr = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 1);
- strlwr(strcpy(qrystr, Qrystr));
-
- // 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))
- strlwr(strcpy(name, Name)); // Not a keyword
-
- if ((p = strstr(qrystr, name))) {
- memcpy(Query, Qrystr, p - qrystr);
- Query[p - qrystr] = 0;
-
- if (qtd && *(p-1) == ' ')
- strcat(strcat(strcat(Query, "`"), Tabname), "`");
- else
- strcat(Query, Tabname);
-
- strcat(Query, Qrystr + (p - qrystr) + strlen(name));
- } else {
- sprintf(g->Message, "Cannot use this %s command",
- (Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
- return RC_FX;
- } // endif p
-
- } else
- strcpy(Query, Qrystr);
-
- return RC_OK;
- } // end of MakeCommand
-
-#if 0
-/***********************************************************************/
-/* MakeUpdate: make the Update statement use with MySQL connection. */
-/* Limited to remote values and filtering. */
-/***********************************************************************/
-int TDBMYSQL::MakeUpdate(PGLOBAL g)
- {
- char *qc, cmd[8], tab[96], end[1024];
-
- Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
- memset(end, 0, sizeof(end));
-
- if (sscanf(Qrystr, "%s `%[^`]`%1023c", cmd, tab, end) > 2 ||
- sscanf(Qrystr, "%s \"%[^\"]\"%1023c", cmd, tab, end) > 2)
- qc = "`";
- else if (sscanf(Qrystr, "%s %s%1023c", cmd, tab, end) > 2
- && !stricmp(tab, Name))
- qc = (Quoted) ? "`" : "";
- else {
- strcpy(g->Message, "Cannot use this UPDATE command");
- return RC_FX;
- } // endif sscanf
-
- assert(!stricmp(cmd, "update"));
- strcat(strcat(strcat(strcpy(Query, "UPDATE "), qc), Tabname), qc);
- strcat(Query, end);
- return RC_OK;
- } // end of MakeUpdate
-
-/***********************************************************************/
-/* MakeDelete: make the Delete statement used with MySQL connection. */
-/* Limited to remote filtering. */
-/***********************************************************************/
-int TDBMYSQL::MakeDelete(PGLOBAL g)
- {
- char *qc, cmd[8], from[8], tab[96], end[512];
-
- Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
- memset(end, 0, sizeof(end));
-
- if (sscanf(Qrystr, "%s %s `%[^`]`%511c", cmd, from, tab, end) > 2 ||
- sscanf(Qrystr, "%s %s \"%[^\"]\"%511c", cmd, from, tab, end) > 2)
- qc = "`";
- else if (sscanf(Qrystr, "%s %s %s%511c", cmd, from, tab, end) > 2)
- qc = (Quoted) ? "`" : "";
- else {
- strcpy(g->Message, "Cannot use this DELETE command");
- return RC_FX;
- } // endif sscanf
-
- assert(!stricmp(cmd, "delete") && !stricmp(from, "from"));
- strcat(strcat(strcat(strcpy(Query, "DELETE FROM "), qc), Tabname), qc);
-
- if (*end)
- strcat(Query, end);
-
- return RC_OK;
- } // end of MakeDelete
-#endif // 0
-
-/***********************************************************************/
-/* XCV GetMaxSize: returns the maximum number of rows in the table. */
-/***********************************************************************/
-int TDBMYSQL::GetMaxSize(PGLOBAL g)
- {
- if (MaxSize < 0) {
-#if 0
- if (MakeSelect(g))
- return -2;
-
- if (!Myc.Connected()) {
- if (Myc.Open(g, Host, Database, User, Pwd, Port))
- return -1;
-
- } // endif connected
-
- if ((MaxSize = Myc.GetResultSize(g, Query)) < 0) {
- Myc.Close();
- return -3;
- } // endif MaxSize
-
- // FIXME: Columns should be known when Info calls GetMaxSize
- if (!Columns)
- Query = NULL; // Must be remade when columns are known
-#endif // 0
-
- // Return 0 in mode DELETE in case of delete all.
- MaxSize = (Mode == MODE_DELETE) ? 0 : 10; // To make MySQL happy
- } // endif MaxSize
-
- return MaxSize;
- } // end of GetMaxSize
-
-/***********************************************************************/
-/* This a fake routine as ROWID does not exist in MySQL. */
-/***********************************************************************/
-int TDBMYSQL::RowNumber(PGLOBAL g, bool b)
- {
- return N;
- } // end of RowNumber
-
-/***********************************************************************/
-/* Return 0 in mode UPDATE to tell that the update is done. */
-/***********************************************************************/
-int TDBMYSQL::GetProgMax(PGLOBAL g)
- {
- return (Mode == MODE_UPDATE) ? 0 : GetMaxSize(g);
- } // end of GetProgMax
-
-/***********************************************************************/
-/* MySQL Bind Parameter function. */
-/***********************************************************************/
-int TDBMYSQL::BindColumns(PGLOBAL g)
- {
-#if defined(MYSQL_PREPARED_STATEMENTS)
- if (Prep) {
- Bind = (MYSQL_BIND*)PlugSubAlloc(g, NULL, Nparm * sizeof(MYSQL_BIND));
-
- for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next)
- colp->InitBind(g);
-
- return Myc.BindParams(g, Bind);
- } // endif prep
-#endif // MYSQL_PREPARED_STATEMENTS
-
- return RC_OK;
- } // end of BindColumns
-
-/***********************************************************************/
-/* MySQL Access Method opening routine. */
-/***********************************************************************/
-bool TDBMYSQL::OpenDB(PGLOBAL g)
- {
- if (Use == USE_OPEN) {
- /*******************************************************************/
- /* Table already open, just replace it at its beginning. */
- /*******************************************************************/
- Myc.Rewind();
- return false;
- } // endif use
-
- /*********************************************************************/
- /* Open a MySQL connection for this table. */
- /* Note: this may not be the proper way to do. Perhaps it is better */
- /* to test whether a connection is already open for this server */
- /* and if so to allocate just a new result set. But this only for */
- /* servers allowing concurency in getting results ??? */
- /*********************************************************************/
- if (!Myc.Connected()) {
- if (Myc.Open(g, Host, Database, User, Pwd, Port))
- return true;
-
- } // endif Connected
-
- /*********************************************************************/
- /* Take care of DATE columns. */
- /*********************************************************************/
- for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next)
- if (colp->Buf_Type == TYPE_DATE)
- // Format must match DATETIME MySQL type
- ((DTVAL*)colp->GetValue())->SetFormat(g, "YYYY-MM-DD hh:mm:ss", 19);
-
- /*********************************************************************/
- /* Allocate whatever is used for getting results. */
- /*********************************************************************/
- if (Mode == MODE_READ) {
- if (!MakeSelect(g))
- m_Rc = Myc.ExecSQL(g, Query);
-
-#if 0
- if (!Myc.m_Res || !Myc.m_Fields) {
- sprintf(g->Message, "%s result", (Myc.m_Res) ? "Void" : "No");
- Myc.Close();
- return true;
- } // endif m_Res
-#endif // 0
-
- if (!m_Rc && Srcdef)
- if (SetColumnRanks(g))
- return true;
-
- } else if (Mode == MODE_INSERT) {
- if (Srcdef) {
- strcpy(g->Message, "No insert into anonym views");
- return true;
- } // endif Srcdef
-
- if (!MakeInsert(g)) {
-#if defined(MYSQL_PREPARED_STATEMENTS)
- int n = (Prep) ? Myc.PrepareSQL(g, Query) : Nparm;
-
- if (Nparm != n) {
- if (n >= 0) // Other errors return negative values
- strcpy(g->Message, MSG(BAD_PARM_COUNT));
-
- } else
-#endif // MYSQL_PREPARED_STATEMENTS
- m_Rc = BindColumns(g);
-
- } // endif MakeInsert
-
- if (m_Rc != RC_FX) {
- char cmd[64];
- int w;
-
- sprintf(cmd, "ALTER TABLE `%s` DISABLE KEYS", Tabname);
- m_Rc = Myc.ExecSQL(g, cmd, &w);
- } // endif m_Rc
-
- } else
-// m_Rc = (Mode == MODE_DELETE) ? MakeDelete(g) : MakeUpdate(g);
- m_Rc = MakeCommand(g);
-
- if (m_Rc == RC_FX) {
- Myc.Close();
- return true;
- } // endif rc
-
- Use = USE_OPEN;
- return false;
- } // end of OpenDB
-
-/***********************************************************************/
-/* Set the rank of columns in the result set. */
-/***********************************************************************/
-bool TDBMYSQL::SetColumnRanks(PGLOBAL g)
- {
- for (PCOL colp = Columns; colp; colp = colp->GetNext())
- if (((PMYCOL)colp)->FindRank(g))
- return TRUE;
-
- return FALSE;
- } // end of SetColumnRanks
-
-/***********************************************************************/
-/* Called by Parent table to make the columns of a View. */
-/***********************************************************************/
-PCOL TDBMYSQL::MakeFieldColumn(PGLOBAL g, char *name)
- {
- int n;
- MYSQL_FIELD *fld;
- PCOL cp, colp = NULL;
-
- for (n = 0; n < Myc.m_Fields; n++) {
- fld = &Myc.m_Res->fields[n];
-
- if (!stricmp(name, fld->name)) {
- colp = new(g) MYSQLCOL(fld, this, n);
-
- if (colp->InitValue(g))
- return NULL;
-
- if (!Columns)
- Columns = colp;
- else for (cp = Columns; cp; cp = cp->GetNext())
- if (!cp->GetNext()) {
- cp->SetNext(colp);
- break;
- } // endif Next
-
- break;
- } // endif name
-
- } // endfor n
-
- if (!colp)
- sprintf(g->Message, "Column %s is not in view", name);
-
- return colp;
- } // end of MakeFieldColumn
-
-/***********************************************************************/
-/* Called by Pivot tables to find default column names in a View */
-/* as the name of last field not equal to the passed name. */
-/***********************************************************************/
-char *TDBMYSQL::FindFieldColumn(char *name)
- {
- int n;
- MYSQL_FIELD *fld;
- char *cp = NULL;
-
- for (n = Myc.m_Fields - 1; n >= 0; n--) {
- fld = &Myc.m_Res->fields[n];
-
- if (!name || stricmp(name, fld->name)) {
- cp = fld->name;
- break;
- } // endif name
-
- } // endfor n
-
- return cp;
- } // end of FindFieldColumn
-
-/***********************************************************************/
-/* Send an UPDATE or DELETE command to the remote server. */
-/***********************************************************************/
-int TDBMYSQL::SendCommand(PGLOBAL g)
- {
- int w;
-
- if (Myc.ExecSQLcmd(g, Query, &w) == RC_NF) {
- AftRows = Myc.m_Afrw;
- sprintf(g->Message, "%s: %d affected rows", Tabname, AftRows);
- PushWarning(g, this, 0); // 0 means a Note
-
- if (trace)
- htrc("%s\n", g->Message);
-
- if (w && Myc.ExecSQL(g, "SHOW WARNINGS") == RC_OK) {
- // We got warnings from the remote server
- while (Myc.Fetch(g, -1) == RC_OK) {
- sprintf(g->Message, "%s: (%s) %s", Tabname,
- Myc.GetCharField(1), Myc.GetCharField(2));
- PushWarning(g, this);
- } // endwhile Fetch
-
- Myc.FreeResult();
- } // endif w
-
- return RC_EF; // Nothing else to do
- } else
- return RC_FX; // Error
-
- } // end of SendCommand
-
-/***********************************************************************/
-/* Data Base read routine for MYSQL access method. */
-/***********************************************************************/
-int TDBMYSQL::ReadDB(PGLOBAL g)
- {
- int rc;
-
- if (trace > 1)
- htrc("MySQL ReadDB: R%d Mode=%d key=%p link=%p Kindex=%p\n",
- GetTdb_No(), Mode, To_Key_Col, To_Link, To_Kindex);
-
- if (Mode == MODE_UPDATE || Mode == MODE_DELETE)
- return SendCommand(g);
-
- /*********************************************************************/
- /* Now start the reading process. */
- /* Here is the place to fetch the line. */
- /*********************************************************************/
- N++;
- Fetched = ((rc = Myc.Fetch(g, -1)) == RC_OK);
-
- if (trace > 1)
- htrc(" Read: rc=%d\n", rc);
-
- return rc;
- } // end of ReadDB
-
-/***********************************************************************/
-/* WriteDB: Data Base write routine for MYSQL access methods. */
-/***********************************************************************/
-int TDBMYSQL::WriteDB(PGLOBAL g)
- {
-#if defined(MYSQL_PREPARED_STATEMENTS)
- if (Prep)
- return Myc.ExecStmt(g);
-#endif // MYSQL_PREPARED_STATEMENTS
-
- // Statement was not prepared, we must construct and execute
- // an insert query for each line to insert
- int rc;
- char buf[32];
-
- strcpy(Qbuf, Query);
-
- // Make the Insert command value list
- for (PCOL colp = Columns; colp; colp = colp->GetNext()) {
- if (!colp->GetValue()->IsNull()) {
- if (colp->GetResultType() == TYPE_STRING ||
- colp->GetResultType() == TYPE_DATE)
- strcat(Qbuf, "'");
-
- strcat(Qbuf, colp->GetValue()->GetCharString(buf));
-
- if (colp->GetResultType() == TYPE_STRING ||
- colp->GetResultType() == TYPE_DATE)
- strcat(Qbuf, "'");
-
- } else
- strcat(Qbuf, "NULL");
-
- strcat(Qbuf, (colp->GetNext()) ? "," : ")");
- } // endfor colp
-
- Myc.m_Rows = -1; // To execute the query
- rc = Myc.ExecSQL(g, Qbuf);
- return (rc == RC_NF) ? RC_OK : rc; // RC_NF is Ok
- } // end of WriteDB
-
-/***********************************************************************/
-/* Data Base delete all routine for MYSQL access methods. */
-/***********************************************************************/
-int TDBMYSQL::DeleteDB(PGLOBAL g, int irc)
- {
- if (irc == RC_FX)
- // Send the DELETE (all) command to the remote table
- return (SendCommand(g) == RC_FX) ? RC_FX : RC_OK;
- else
- return RC_OK; // Ignore
-
- } // end of DeleteDB
-
-/***********************************************************************/
-/* Data Base close routine for MySQL access method. */
-/***********************************************************************/
-void TDBMYSQL::CloseDB(PGLOBAL g)
- {
- if (Myc.Connected()) {
- if (Mode == MODE_INSERT) {
- char cmd[64];
- int w;
- PDBUSER dup = PlgGetUser(g);
-
- dup->Step = "Enabling indexes";
- sprintf(cmd, "ALTER TABLE `%s` ENABLE KEYS", Tabname);
- Myc.m_Rows = -1; // To execute the query
- m_Rc = Myc.ExecSQL(g, cmd, &w);
- } // endif m_Rc
-
- Myc.Close();
- } // endif Myc
-
- if (trace)
- htrc("MySQL CloseDB: closing %s rc=%d\n", Name, m_Rc);
-
- } // end of CloseDB
-
-// ------------------------ MYSQLCOL functions --------------------------
-
-/***********************************************************************/
-/* MYSQLCOL public constructor. */
-/***********************************************************************/
-MYSQLCOL::MYSQLCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PSZ am)
- : COLBLK(cdp, tdbp, i)
- {
- if (cprec) {
- Next = cprec->GetNext();
- cprec->SetNext(this);
- } else {
- Next = tdbp->GetColumns();
- tdbp->SetColumns(this);
- } // endif cprec
-
- // Set additional MySQL access method information for column.
- Precision = Long = cdp->GetLong();
- Bind = NULL;
- To_Val = NULL;
- Slen = 0;
- Rank = -1; // Not known yet
-
- if (trace)
- htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
-
- } // end of MYSQLCOL constructor
-
-/***********************************************************************/
-/* MYSQLCOL public constructor. */
-/***********************************************************************/
-MYSQLCOL::MYSQLCOL(MYSQL_FIELD *fld, PTDB tdbp, int i, PSZ am)
- : COLBLK(NULL, tdbp, i)
- {
- Name = fld->name;
- Opt = 0;
- Precision = Long = fld->length;
- Buf_Type = MYSQLtoPLG(fld->type);
- strcpy(Format.Type, GetFormatType(Buf_Type));
- Format.Length = Long;
- Format.Prec = fld->decimals;
- ColUse = U_P;
- Nullable = !IS_NOT_NULL(fld->flags);
-
- // Set additional MySQL access method information for column.
- Bind = NULL;
- To_Val = NULL;
- Slen = 0;
- Rank = i;
-
- if (trace)
- htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
-
- } // end of MYSQLCOL constructor
-
-/***********************************************************************/
-/* MYSQLCOL constructor used for copying columns. */
-/* tdbp is the pointer to the new table descriptor. */
-/***********************************************************************/
-MYSQLCOL::MYSQLCOL(MYSQLCOL *col1, PTDB tdbp) : COLBLK(col1, tdbp)
- {
- Long = col1->Long;
- Bind = NULL;
- To_Val = NULL;
- Slen = col1->Slen;
- Rank = col1->Rank;
- } // end of MYSQLCOL copy constructor
-
-/***********************************************************************/
-/* FindRank: Find the rank of this column in the result set. */
-/***********************************************************************/
-bool MYSQLCOL::FindRank(PGLOBAL g)
-{
- int n;
- MYSQLC myc = ((PTDBMY)To_Tdb)->Myc;
-
- for (n = 0; n < myc.m_Fields; n++)
- if (!stricmp(Name, myc.m_Res->fields[n].name)) {
- Rank = n;
- return false;
- } // endif Name
-
- sprintf(g->Message, "Column %s not in result set", Name);
- return true;
-} // end of FindRank
-
-/***********************************************************************/
-/* SetBuffer: prepare a column block for write operation. */
-/***********************************************************************/
-bool MYSQLCOL::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
-
-/***********************************************************************/
-/* InitBind: Initialize the bind structure according to type. */
-/***********************************************************************/
-void MYSQLCOL::InitBind(PGLOBAL g)
- {
- PTDBMY tdbp = (PTDBMY)To_Tdb;
-
- assert(tdbp->Bind && Rank < tdbp->Nparm);
-
- Bind = &tdbp->Bind[Rank];
- memset(Bind, 0, sizeof(MYSQL_BIND));
-
- if (Buf_Type == TYPE_DATE) {
- Bind->buffer_type = PLGtoMYSQL(TYPE_STRING, false);
- Bind->buffer = (char *)PlugSubAlloc(g,NULL, 20);
- Bind->buffer_length = 20;
- Bind->length = &Slen;
- } else {
- Bind->buffer_type = PLGtoMYSQL(Buf_Type, false);
- Bind->buffer = (char *)Value->GetTo_Val();
- Bind->buffer_length = Value->GetClen();
- Bind->length = (IsTypeChar(Buf_Type)) ? &Slen : NULL;
- } // endif Buf_Type
-
- } // end of InitBind
-
-/***********************************************************************/
-/* ReadColumn: */
-/***********************************************************************/
-void MYSQLCOL::ReadColumn(PGLOBAL g)
- {
- char *p, *buf, tim[20];
- int rc;
- PTDBMY tdbp = (PTDBMY)To_Tdb;
-
- /*********************************************************************/
- /* If physical fetching of the line was deferred, do it now. */
- /*********************************************************************/
- if (!tdbp->Fetched)
- if ((rc = tdbp->Myc.Fetch(g, tdbp->N)) != RC_OK) {
- if (rc == RC_EF)
- sprintf(g->Message, MSG(INV_DEF_READ), rc);
-
- longjmp(g->jumper[g->jump_level], 11);
- } else
- tdbp->Fetched = TRUE;
-
- if ((buf = ((PTDBMY)To_Tdb)->Myc.GetCharField(Rank))) {
- if (trace > 1)
- htrc("MySQL ReadColumn: name=%s buf=%s\n", Name, buf);
-
- // TODO: have a true way to differenciate temporal values
- if (Buf_Type == TYPE_DATE && strlen(buf) == 8)
- // This is a TIME value
- p = strcat(strcpy(tim, "1970-01-01 "), buf);
- else
- p = buf;
-
- if (Value->SetValue_char(p, strlen(p))) {
- sprintf(g->Message, "Out of range value for column %s at row %d",
- Name, tdbp->RowNumber(g));
- PushWarning(g, tdbp);
- } // endif SetValue_char
-
- } else {
- if (Nullable)
- Value->SetNull(true);
-
- Value->Reset(); // Null value
- } // endif buf
-
- } // end of ReadColumn
-
-/***********************************************************************/
-/* WriteColumn: make sure the bind buffer is updated. */
-/***********************************************************************/
-void MYSQLCOL::WriteColumn(PGLOBAL g)
- {
- /*********************************************************************/
- /* Do convert the column value if necessary. */
- /*********************************************************************/
- if (Value != To_Val)
- Value->SetValue_pval(To_Val, FALSE); // Convert the inserted value
-
-#if defined(MYSQL_PREPARED_STATEMENTS)
- if (((PTDBMY)To_Tdb)->Prep) {
- if (Buf_Type == TYPE_DATE) {
- Value->ShowValue((char *)Bind->buffer, (int)Bind->buffer_length);
- Slen = strlen((char *)Bind->buffer);
- } else if (IsTypeChar(Buf_Type))
- Slen = strlen(Value->GetCharValue());
-
- } // endif Prep
-#endif // MYSQL_PREPARED_STATEMENTS
-
- } // end of WriteColumn
-
-/* ------------------------------------------------------------------- */
-
-/***********************************************************************/
-/* Implementation of the TDBMYEXC class. */
-/***********************************************************************/
-TDBMYEXC::TDBMYEXC(PMYDEF tdp) : TDBMYSQL(tdp)
-{
- Cmdlist = NULL;
- Cmdcol = NULL;
- Shw = false;
- Havew = false;
- Isw = false;
- Warnings = 0;
- Mxr = tdp->Mxr;
- Nerr = 0;
-} // end of TDBMYEXC constructor
-
-TDBMYEXC::TDBMYEXC(PGLOBAL g, PTDBMYX tdbp) : TDBMYSQL(g, tdbp)
-{
- Cmdlist = tdbp->Cmdlist;
- Cmdcol = tdbp->Cmdcol;
- Shw = tdbp->Shw;
- Havew = tdbp->Havew;
- Isw = tdbp->Isw;
- Mxr = tdbp->Mxr;
- Nerr = tdbp->Nerr;
-} // end of TDBMYEXC copy constructor
-
-// Is this really useful ???
-PTDB TDBMYEXC::CopyOne(PTABS t)
- {
- PTDB tp;
- PCOL cp1, cp2;
- PGLOBAL g = t->G;
-
- tp = new(g) TDBMYEXC(g, this);
-
- for (cp1 = Columns; cp1; cp1 = cp1->GetNext()) {
- cp2 = new(g) MYXCOL((PMYXCOL)cp1, tp);
-
- NewPointer(t, cp1, cp2);
- } // endfor cp1
-
- return tp;
- } // end of CopyOne
-
-/***********************************************************************/
-/* Allocate MYSQL column description block. */
-/***********************************************************************/
-PCOL TDBMYEXC::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
- {
- PMYXCOL colp = new(g) MYXCOL(cdp, this, cprec, n);
-
- if (!colp->Flag)
- Cmdcol = colp->GetName();
-
- return colp;
- } // end of MakeCol
-
-/***********************************************************************/
-/* MakeCMD: make the SQL statement to send to MYSQL connection. */
-/***********************************************************************/
-PCMD TDBMYEXC::MakeCMD(PGLOBAL g)
- {
- PCMD xcmd = NULL;
-
- if (To_CondFil) {
- if (Cmdcol) {
- if (!stricmp(Cmdcol, To_CondFil->Body) &&
- (To_CondFil->Op == OP_EQ || To_CondFil->Op == OP_IN)) {
- xcmd = To_CondFil->Cmds;
- } else
- strcpy(g->Message, "Invalid command specification filter");
-
- } else
- strcpy(g->Message, "No command column in select list");
-
- } else if (!Srcdef)
- strcpy(g->Message, "No Srcdef default command");
- else
- xcmd = new(g) CMD(g, Srcdef);
-
- return xcmd;
- } // end of MakeCMD
-
-/***********************************************************************/
-/* EXC GetMaxSize: returns the maximum number of rows in the table. */
-/***********************************************************************/
-int TDBMYEXC::GetMaxSize(PGLOBAL g)
- {
- if (MaxSize < 0) {
- MaxSize = 10; // a guess
- } // endif MaxSize
-
- return MaxSize;
- } // end of GetMaxSize
-
-/***********************************************************************/
-/* MySQL Exec Access Method opening routine. */
-/***********************************************************************/
-bool TDBMYEXC::OpenDB(PGLOBAL g)
- {
- if (Use == USE_OPEN) {
- strcpy(g->Message, "Multiple execution is not allowed");
- return true;
- } // endif use
-
- /*********************************************************************/
- /* Open a MySQL connection for this table. */
- /* Note: this may not be the proper way to do. Perhaps it is better */
- /* to test whether a connection is already open for this server */
- /* and if so to allocate just a new result set. But this only for */
- /* servers allowing concurency in getting results ??? */
- /*********************************************************************/
- if (!Myc.Connected())
- if (Myc.Open(g, Host, Database, User, Pwd, Port))
- return true;
-
- Use = USE_OPEN; // Do it now in case we are recursively called
-
- if (Mode != MODE_READ) {
- strcpy(g->Message, "No INSERT/DELETE/UPDATE of MYSQL EXEC tables");
- return true;
- } // endif Mode
-
- /*********************************************************************/
- /* Get the command to execute. */
- /*********************************************************************/
- if (!(Cmdlist = MakeCMD(g))) {
- Myc.Close();
- return true;
- } // endif Query
-
- return false;
- } // end of OpenDB
-
-/***********************************************************************/
-/* Data Base read routine for MYSQL access method. */
-/***********************************************************************/
-int TDBMYEXC::ReadDB(PGLOBAL g)
- {
- if (Havew) {
- // Process result set from SHOW WARNINGS
- if (Myc.Fetch(g, -1) != RC_OK) {
- Myc.FreeResult();
- Havew = Isw = false;
- } else {
- N++;
- Isw = true;
- return RC_OK;
- } // endif Fetch
-
- } // endif m_Res
-
- if (Cmdlist) {
- // Process query to send
- int rc;
-
- do {
- Query = Cmdlist->Cmd;
-
- switch (rc = Myc.ExecSQLcmd(g, Query, &Warnings)) {
- case RC_NF:
- AftRows = Myc.m_Afrw;
- strcpy(g->Message, "Affected rows");
- break;
- case RC_OK:
- AftRows = Myc.m_Fields;
- strcpy(g->Message, "Result set columns");
- break;
- case RC_FX:
- AftRows = Myc.m_Afrw;
- Nerr++;
- break;
- case RC_INFO:
- Shw = true;
- } // endswitch rc
-
- Cmdlist = (Nerr > Mxr) ? NULL : Cmdlist->Next;
- } while (rc == RC_INFO);
-
- if (Shw && Warnings)
- Havew = (Myc.ExecSQL(g, "SHOW WARNINGS") == RC_OK);
-
- ++N;
- return RC_OK;
- } else
- return RC_EF;
-
- } // end of ReadDB
-
-/***********************************************************************/
-/* WriteDB: Data Base write routine for Exec MYSQL access methods. */
-/***********************************************************************/
-int TDBMYEXC::WriteDB(PGLOBAL g)
- {
- strcpy(g->Message, "EXEC MYSQL tables are read only");
- return RC_FX;
- } // end of WriteDB
-
-// ------------------------- MYXCOL functions ---------------------------
-
-/***********************************************************************/
-/* MYXCOL public constructor. */
-/***********************************************************************/
-MYXCOL::MYXCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PSZ am)
- : MYSQLCOL(cdp, tdbp, cprec, i, am)
- {
- // Set additional EXEC MYSQL access method information for column.
- Flag = cdp->GetOffset();
- } // end of MYSQLCOL constructor
-
-/***********************************************************************/
-/* MYSQLCOL public constructor. */
-/***********************************************************************/
-MYXCOL::MYXCOL(MYSQL_FIELD *fld, PTDB tdbp, int i, PSZ am)
- : MYSQLCOL(fld, tdbp, i, am)
- {
- if (trace)
- htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
-
- } // end of MYSQLCOL constructor
-
-/***********************************************************************/
-/* MYXCOL constructor used for copying columns. */
-/* tdbp is the pointer to the new table descriptor. */
-/***********************************************************************/
-MYXCOL::MYXCOL(MYXCOL *col1, PTDB tdbp) : MYSQLCOL(col1, tdbp)
- {
- Flag = col1->Flag;
- } // end of MYXCOL copy constructor
-
-/***********************************************************************/
-/* ReadColumn: */
-/***********************************************************************/
-void MYXCOL::ReadColumn(PGLOBAL g)
- {
- PTDBMYX tdbp = (PTDBMYX)To_Tdb;
-
- if (tdbp->Isw) {
- char *buf = NULL;
-
- if (Flag < 3) {
- buf = tdbp->Myc.GetCharField(Flag);
- Value->SetValue_psz(buf);
- } else
- Value->Reset();
-
- } else
- switch (Flag) {
- case 0: Value->SetValue_psz(tdbp->Query); break;
- case 1: Value->SetValue(tdbp->AftRows); break;
- case 2: Value->SetValue_psz(g->Message); break;
- case 3: Value->SetValue(tdbp->Warnings); break;
- default: Value->SetValue_psz("Invalid Flag"); break;
- } // endswitch Flag
-
- } // end of ReadColumn
-
-/***********************************************************************/
-/* WriteColumn: should never be called. */
-/***********************************************************************/
-void MYXCOL::WriteColumn(PGLOBAL g)
- {
- assert(false);
- } // end of WriteColumn
-
-/* ---------------------------TDBMCL class --------------------------- */
-
-/***********************************************************************/
-/* TDBMCL class constructor. */
-/***********************************************************************/
-TDBMCL::TDBMCL(PMYDEF tdp) : TDBCAT(tdp)
- {
- Host = tdp->Hostname;
- Db = tdp->Database;
- Tab = tdp->Tabname;
- User = tdp->Username;
- Pwd = tdp->Password;
- Port = tdp->Portnumber;
- } // end of TDBMCL constructor
-
-/***********************************************************************/
-/* GetResult: Get the list the MYSQL table columns. */
-/***********************************************************************/
-PQRYRES TDBMCL::GetResult(PGLOBAL g)
- {
- return MyColumns(g, Host, Db, User, Pwd, Tab, NULL, Port, false);
- } // end of GetResult
+/************* TabMySQL C++ Program Source Code File (.CPP) *************/
+/* PROGRAM NAME: TABMYSQL */
+/* ------------- */
+/* Version 1.8 */
+/* */
+/* AUTHOR: */
+/* ------- */
+/* Olivier BERTRAND 2007-2014 */
+/* */
+/* WHAT THIS PROGRAM DOES: */
+/* ----------------------- */
+/* Implements a table type that are MySQL tables. */
+/* It can optionally use the embedded MySQL library. */
+/* */
+/* WHAT YOU NEED TO COMPILE THIS PROGRAM: */
+/* -------------------------------------- */
+/* */
+/* REQUIRED FILES: */
+/* --------------- */
+/* TABMYSQL.CPP - Source code */
+/* PLGDBSEM.H - DB application declaration file */
+/* TABMYSQL.H - TABODBC 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 */
+/* */
+/************************************************************************/
+#define MYSQL_SERVER 1
+#include "my_global.h"
+#include "sql_class.h"
+#include "sql_servers.h"
+#if defined(WIN32)
+//#include <windows.h>
+#else // !WIN32
+//#include <fnmatch.h>
+//#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "osutil.h"
+//#include <io.h>
+//#include <fcntl.h>
+#endif // !WIN32
+
+/***********************************************************************/
+/* Include application header files: */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "xtable.h"
+#include "tabcol.h"
+#include "colblk.h"
+#include "mycat.h"
+#include "reldef.h"
+#include "tabmysql.h"
+#include "valblk.h"
+#include "tabutil.h"
+
+#if defined(_CONSOLE)
+void PrintResult(PGLOBAL, PSEM, PQRYRES);
+#endif // _CONSOLE
+
+extern "C" int trace;
+
+/* -------------- Implementation of the MYSQLDEF class --------------- */
+
+/***********************************************************************/
+/* Constructor. */
+/***********************************************************************/
+MYSQLDEF::MYSQLDEF(void)
+ {
+ Pseudo = 2; // SERVID is Ok but not ROWID
+ Hostname = NULL;
+ Database = NULL;
+ Tabname = NULL;
+ Srcdef = NULL;
+ Username = NULL;
+ Password = NULL;
+ Portnumber = 0;
+ Isview = FALSE;
+ Bind = FALSE;
+ Delayed = FALSE;
+ Xsrc = FALSE;
+ } // end of MYSQLDEF constructor
+
+/***********************************************************************/
+/* Get connection info from the declared server. */
+/***********************************************************************/
+bool MYSQLDEF::GetServerInfo(PGLOBAL g, const char *server_name)
+{
+ THD *thd= current_thd;
+ MEM_ROOT *mem= thd->mem_root;
+ FOREIGN_SERVER *server, server_buffer;
+ DBUG_ENTER("GetServerInfo");
+ DBUG_PRINT("info", ("server_name %s", server_name));
+
+ if (!server_name || !strlen(server_name)) {
+ DBUG_PRINT("info", ("server_name not defined!"));
+ strcpy(g->Message, "server_name not defined!");
+ DBUG_RETURN(true);
+ } // endif server_name
+
+ // get_server_by_name() clones the server if exists and allocates
+ // copies of strings in the supplied mem_root
+ if (!(server= get_server_by_name(mem, server_name, &server_buffer))) {
+ DBUG_PRINT("info", ("get_server_by_name returned > 0 error condition!"));
+ /* need to come up with error handling */
+ strcpy(g->Message, "get_server_by_name returned > 0 error condition!");
+ DBUG_RETURN(true);
+ } // endif server
+
+ DBUG_PRINT("info", ("get_server_by_name returned server at %lx",
+ (long unsigned int) server));
+
+ // TODO: We need to examine which of these can really be NULL
+ Hostname = PlugDup(g, server->host);
+ Database = PlugDup(g, server->db);
+ Username = PlugDup(g, server->username);
+ Password = PlugDup(g, server->password);
+ Portnumber = (server->port) ? server->port : GetDefaultPort();
+
+ DBUG_RETURN(false);
+} // end of GetServerInfo
+
+/***********************************************************************/
+/* Parse connection string */
+/* */
+/* SYNOPSIS */
+/* ParseURL() */
+/* url The connection string to parse */
+/* */
+/* DESCRIPTION */
+/* Populates the table with information about the connection */
+/* to the foreign database that will serve as the data source. */
+/* This string must be specified (currently) in the "CONNECTION" */
+/* field, listed in the CREATE TABLE statement. */
+/* */
+/* This string MUST be in the format of any of these: */
+/* */
+/* CONNECTION="scheme://user:pwd@host:port/database/table" */
+/* CONNECTION="scheme://user@host/database/table" */
+/* CONNECTION="scheme://user@host:port/database/table" */
+/* CONNECTION="scheme://user:pwd@host/database/table" */
+/* */
+/* _OR_ */
+/* */
+/* CONNECTION="connection name" (NIY) */
+/* */
+/* An Example: */
+/* */
+/* CREATE TABLE t1 (id int(32)) */
+/* ENGINE="CONNECT" TABLE_TYPE="MYSQL" */
+/* CONNECTION="mysql://joe:pwd@192.168.1.111:9308/dbname/tabname"; */
+/* */
+/* CREATE TABLE t2 ( */
+/* id int(4) NOT NULL auto_increment, */
+/* name varchar(32) NOT NULL, */
+/* PRIMARY KEY(id) */
+/* ) ENGINE="CONNECT" TABLE_TYPE="MYSQL" */
+/* CONNECTION="my_conn"; (NIY) */
+/* */
+/* 'password' and 'port' are both optional. */
+/* */
+/* RETURN VALUE */
+/* false success */
+/* true error */
+/* */
+/***********************************************************************/
+bool MYSQLDEF::ParseURL(PGLOBAL g, char *url, bool b)
+ {
+ if ((!strstr(url, "://") && (!strchr(url, '@')))) {
+ // No :// or @ in connection string. Must be a straight
+ // connection name of either "server" or "server/table"
+ // ok, so we do a little parsing, but not completely!
+ if ((Tabname= strchr(url, '/'))) {
+ // If there is a single '/' in the connection string,
+ // this means the user is specifying a table name
+ *Tabname++= '\0';
+
+ // there better not be any more '/'s !
+ if (strchr(Tabname, '/'))
+ return true;
+
+ } else
+ // Otherwise, straight server name,
+ // use tablename of federatedx table as remote table name
+ Tabname= Name;
+
+ if (trace)
+ htrc("server: %s Tabname: %s", url, Tabname);
+
+ Server = url;
+ return GetServerInfo(g, url);
+ } else {
+ // URL, parse it
+ char *sport, *scheme = url;
+
+ if (!(Username = strstr(url, "://"))) {
+ strcpy(g->Message, "Connection is not an URL");
+ return true;
+ } // endif User
+
+ scheme[Username - scheme] = 0;
+
+ if (stricmp(scheme, "mysql")) {
+ strcpy(g->Message, "scheme must be mysql");
+ return true;
+ } // endif scheme
+
+ Username += 3;
+
+ if (!(Hostname = strchr(Username, '@'))) {
+ strcpy(g->Message, "No host specified in URL");
+ return true;
+ } else {
+ *Hostname++ = 0; // End Username
+ Server = Hostname;
+ } // endif Hostname
+
+ if ((Password = strchr(Username, ':'))) {
+ *Password++ = 0; // End username
+
+ // Make sure there isn't an extra / or @
+ if ((strchr(Password, '/') || strchr(Hostname, '@'))) {
+ strcpy(g->Message, "Syntax error in URL");
+ return true;
+ } // endif
+
+ // Found that if the string is:
+ // user:@hostname:port/db/table
+ // Then password is a null string, so set to NULL
+ if ((Password[0] == 0))
+ Password = NULL;
+
+ } // endif password
+
+ // Make sure there isn't an extra / or @ */
+ if ((strchr(Username, '/')) || (strchr(Hostname, '@'))) {
+ strcpy(g->Message, "Syntax error in URL");
+ return true;
+ } // endif
+
+ if ((Database = strchr(Hostname, '/'))) {
+ *Database++ = 0;
+
+ if ((Tabname = strchr(Database, '/'))) {
+ *Tabname++ = 0;
+
+ // Make sure there's not an extra /
+ if ((strchr(Tabname, '/'))) {
+ strcpy(g->Message, "Syntax error in URL");
+ return true;
+ } // endif /
+
+ } // endif Tabname
+
+ } // endif database
+
+ if ((sport = strchr(Hostname, ':')))
+ *sport++ = 0;
+
+ // For unspecified values, get the values of old style options
+ // but only if called from MYSQLDEF, else set them to NULL
+ Portnumber = (sport && sport[0]) ? atoi(sport)
+ : (b) ? Cat->GetIntCatInfo("Port", GetDefaultPort()) : 0;
+
+ if (Username[0] == 0)
+ Username = (b) ? Cat->GetStringCatInfo(g, "User", "*") : NULL;
+
+ if (Hostname[0] == 0)
+ Hostname = (b) ? Cat->GetStringCatInfo(g, "Host", "localhost") : NULL;
+
+ if (!Database || !*Database)
+ Database = (b) ? Cat->GetStringCatInfo(g, "Database", "*") : NULL;
+
+ if (!Tabname || !*Tabname)
+ Tabname = (b) ? Cat->GetStringCatInfo(g, "Tabname", Name) : NULL;
+
+ if (!Password)
+ Password = (b) ? Cat->GetStringCatInfo(g, "Password", NULL) : NULL;
+ } // endif URL
+
+#if 0
+ if (!share->port)
+ if (!share->hostname || strcmp(share->hostname, my_localhost) == 0)
+ share->socket= (char *) MYSQL_UNIX_ADDR;
+ else
+ share->port= MYSQL_PORT;
+#endif // 0
+
+ return false;
+ } // end of ParseURL
+
+/***********************************************************************/
+/* DefineAM: define specific AM block values from XCV file. */
+/***********************************************************************/
+bool MYSQLDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
+ {
+ char *url;
+
+ Desc = "MySQL Table";
+
+ if (stricmp(am, "MYPRX")) {
+ // Normal case of specific MYSQL table
+ url = Cat->GetStringCatInfo(g, "Connect", NULL);
+
+ if (!url || !*url) {
+ // Not using the connection URL
+ Hostname = Cat->GetStringCatInfo(g, "Host", "localhost");
+ Database = Cat->GetStringCatInfo(g, "Database", "*");
+ Tabname = Cat->GetStringCatInfo(g, "Name", Name); // Deprecated
+ Tabname = Cat->GetStringCatInfo(g, "Tabname", Tabname);
+ Username = Cat->GetStringCatInfo(g, "User", "*");
+ Password = Cat->GetStringCatInfo(g, "Password", NULL);
+ Portnumber = Cat->GetIntCatInfo("Port", GetDefaultPort());
+ Server = Hostname;
+ } else if (ParseURL(g, url))
+ return true;
+
+ Bind = !!Cat->GetIntCatInfo("Bind", 0);
+ Delayed = !!Cat->GetIntCatInfo("Delayed", 0);
+ } else {
+ // MYSQL access from a PROXY table
+ Database = Cat->GetStringCatInfo(g, "Database", "*");
+ Isview = Cat->GetBoolCatInfo("View", FALSE);
+
+ // We must get other connection parms from the calling table
+ Remove_tshp(Cat);
+ url = Cat->GetStringCatInfo(g, "Connect", NULL);
+
+ if (!url || !*url) {
+ Hostname = Cat->GetStringCatInfo(g, "Host", "localhost");
+ Username = Cat->GetStringCatInfo(g, "User", "*");
+ Password = Cat->GetStringCatInfo(g, "Password", NULL);
+ Portnumber = Cat->GetIntCatInfo("Port", GetDefaultPort());
+ Server = Hostname;
+ } else {
+ char *locdb = Database;
+
+ if (ParseURL(g, url))
+ return true;
+
+ Database = locdb;
+ } // endif url
+
+ Tabname = Name;
+ } // endif am
+
+ if ((Srcdef = Cat->GetStringCatInfo(g, "Srcdef", NULL)))
+ Isview = true;
+
+ // Used for Update and Delete
+ Qrystr = Cat->GetStringCatInfo(g, "Query_String", "?");
+ Quoted = Cat->GetIntCatInfo("Quoted", 0);
+
+ // Specific for command executing tables
+ Xsrc = Cat->GetBoolCatInfo("Execsrc", false);
+ Mxr = Cat->GetIntCatInfo("Maxerr", 0);
+ return FALSE;
+ } // end of DefineAM
+
+/***********************************************************************/
+/* GetTable: makes a new TDB of the proper type. */
+/***********************************************************************/
+PTDB MYSQLDEF::GetTable(PGLOBAL g, MODE m)
+ {
+ if (Xsrc)
+ return new(g) TDBMYEXC(this);
+ else if (Catfunc == FNC_COL)
+ return new(g) TDBMCL(this);
+ else
+ return new(g) TDBMYSQL(this);
+
+ } // end of GetTable
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Implementation of the TDBMYSQL class. */
+/***********************************************************************/
+TDBMYSQL::TDBMYSQL(PMYDEF tdp) : TDBASE(tdp)
+ {
+ if (tdp) {
+ Host = tdp->Hostname;
+ Database = tdp->Database;
+ Tabname = tdp->Tabname;
+ Srcdef = tdp->Srcdef;
+ User = tdp->Username;
+ Pwd = tdp->Password;
+ Server = tdp->Server;
+ Qrystr = tdp->Qrystr;
+ Quoted = max(0, tdp->Quoted);
+ Port = tdp->Portnumber;
+ Isview = tdp->Isview;
+ Prep = tdp->Bind;
+ Delayed = tdp->Delayed;
+ } else {
+ Host = NULL;
+ Database = NULL;
+ Tabname = NULL;
+ Srcdef = NULL;
+ User = NULL;
+ Pwd = NULL;
+ Server = NULL;
+ Qrystr = NULL;
+ Quoted = 0;
+ Port = 0;
+ Isview = FALSE;
+ Prep = FALSE;
+ Delayed = FALSE;
+ } // endif tdp
+
+ Bind = NULL;
+ Query = NULL;
+ Qbuf = NULL;
+ Fetched = FALSE;
+ m_Rc = RC_FX;
+ AftRows = 0;
+ N = -1;
+ Nparm = 0;
+ } // end of TDBMYSQL constructor
+
+TDBMYSQL::TDBMYSQL(PGLOBAL g, PTDBMY tdbp) : TDBASE(tdbp)
+ {
+ Host = tdbp->Host;
+ Database = tdbp->Database;
+ Tabname = tdbp->Tabname;
+ Srcdef = tdbp->Srcdef;
+ User = tdbp->User;
+ Pwd = tdbp->Pwd;
+ Qrystr = tdbp->Qrystr;
+ Quoted = tdbp->Quoted;
+ Port = tdbp->Port;
+ Isview = tdbp->Isview;
+ Prep = tdbp->Prep;
+ Delayed = tdbp->Delayed;
+ Bind = NULL;
+ Query = tdbp->Query;
+ Qbuf = NULL;
+ Fetched = tdbp->Fetched;
+ m_Rc = tdbp->m_Rc;
+ AftRows = tdbp->AftRows;
+ N = tdbp->N;
+ Nparm = tdbp->Nparm;
+ } // end of TDBMYSQL copy constructor
+
+// Is this really useful ???
+PTDB TDBMYSQL::CopyOne(PTABS t)
+ {
+ PTDB tp;
+ PCOL cp1, cp2;
+ PGLOBAL g = t->G;
+
+ tp = new(g) TDBMYSQL(g, this);
+
+ for (cp1 = Columns; cp1; cp1 = cp1->GetNext()) {
+ cp2 = new(g) MYSQLCOL((PMYCOL)cp1, tp);
+
+ NewPointer(t, cp1, cp2);
+ } // endfor cp1
+
+ return tp;
+ } // end of CopyOne
+
+/***********************************************************************/
+/* Allocate MYSQL column description block. */
+/***********************************************************************/
+PCOL TDBMYSQL::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
+ {
+ return new(g) MYSQLCOL(cdp, this, cprec, n);
+ } // end of MakeCol
+
+/***********************************************************************/
+/* MakeSelect: make the Select statement use with MySQL connection. */
+/* Note: when implementing EOM filtering, column only used in local */
+/* filter should be removed from column list. */
+/***********************************************************************/
+bool TDBMYSQL::MakeSelect(PGLOBAL g)
+ {
+ char *tk = "`";
+ int rank = 0;
+ bool b = FALSE;
+ PCOL colp;
+//PDBUSER dup = PlgGetUser(g);
+
+ if (Query)
+ return FALSE; // already done
+
+ if (Srcdef) {
+ Query = Srcdef;
+ return false;
+ } // endif Srcdef
+
+ //Find the address of the suballocated query
+ Query = (char*)PlugSubAlloc(g, NULL, 0);
+ strcpy(Query, "SELECT ");
+
+ if (Columns) {
+ for (colp = Columns; colp; colp = colp->GetNext())
+ if (!colp->IsSpecial()) {
+// if (colp->IsSpecial()) {
+// strcpy(g->Message, MSG(NO_SPEC_COL));
+// return TRUE;
+// } else {
+ if (b)
+ strcat(Query, ", ");
+ else
+ b = TRUE;
+
+ strcat(strcat(strcat(Query, tk), colp->GetName()), tk);
+ ((PMYCOL)colp)->Rank = rank++;
+ } // endif colp
+
+ } else {
+ // ncol == 0 can occur for views or queries such as
+ // Query count(*) from... for which we will count the rows from
+ // Query '*' from...
+ // (the use of a char constant minimize the result storage)
+ strcat(Query, (Isview) ? "*" : "'*'");
+ } // endif ncol
+
+ strcat(strcat(strcat(strcat(Query, " FROM "), tk), Tabname), tk);
+
+ if (To_CondFil)
+ strcat(strcat(Query, " WHERE "), To_CondFil->Body);
+
+ if (trace)
+ htrc("Query=%s\n", Query);
+
+ // Now we know how much to suballocate
+ PlugSubAlloc(g, NULL, strlen(Query) + 1);
+ return FALSE;
+ } // end of MakeSelect
+
+/***********************************************************************/
+/* MakeInsert: make the Insert statement used with MySQL connection. */
+/***********************************************************************/
+bool TDBMYSQL::MakeInsert(PGLOBAL g)
+ {
+ char *colist, *valist = NULL;
+ char *tk = "`";
+ int len = 0, qlen = 0;
+ bool b = FALSE;
+ PCOL colp;
+
+ if (Query)
+ return FALSE; // already done
+
+ for (colp = Columns; colp; colp = colp->GetNext())
+ if (!colp->IsSpecial()) {
+// if (colp->IsSpecial()) {
+// strcpy(g->Message, MSG(NO_SPEC_COL));
+// return TRUE;
+// } else {
+ len += (strlen(colp->GetName()) + 4);
+ ((PMYCOL)colp)->Rank = Nparm++;
+ } // endif colp
+
+ colist = (char*)PlugSubAlloc(g, NULL, len);
+ *colist = '\0';
+
+ if (Prep) {
+#if defined(MYSQL_PREPARED_STATEMENTS)
+ valist = (char*)PlugSubAlloc(g, NULL, 2 * Nparm);
+ *valist = '\0';
+#else // !MYSQL_PREPARED_STATEMENTS
+ strcpy(g->Message, "Prepared statements not used (not supported)");
+ PushWarning(g, this);
+ Prep = FALSE;
+#endif // !MYSQL_PREPARED_STATEMENTS
+ } // endif Prep
+
+ for (colp = Columns; colp; colp = colp->GetNext()) {
+ if (b) {
+ strcat(colist, ", ");
+ if (Prep) strcat(valist, ",");
+ } else
+ b = TRUE;
+
+ strcat(strcat(strcat(colist, tk), colp->GetName()), tk);
+
+ // Parameter marker
+ if (!Prep) {
+ if (colp->GetResultType() == TYPE_DATE)
+ qlen += 20;
+ else
+ qlen += colp->GetLength();
+
+ } // endif Prep
+
+ if (Prep)
+ strcat(valist, "?");
+
+ } // endfor colp
+
+ // Below 40 is enough to contain the fixed part of the query
+ len = (strlen(Tabname) + strlen(colist)
+ + ((Prep) ? strlen(valist) : 0) + 40);
+ Query = (char*)PlugSubAlloc(g, NULL, len);
+
+ if (Delayed)
+ strcpy(Query, "INSERT DELAYED INTO ");
+ else
+ strcpy(Query, "INSERT INTO ");
+
+ strcat(strcat(strcat(Query, tk), Tabname), tk);
+ strcat(strcat(strcat(Query, " ("), colist), ") VALUES (");
+
+ if (Prep)
+ strcat(strcat(Query, valist), ")");
+ else {
+ qlen += (strlen(Query) + Nparm);
+ Qbuf = (char *)PlugSubAlloc(g, NULL, qlen);
+ } // endelse Prep
+
+ return FALSE;
+ } // end of MakeInsert
+
+/***********************************************************************/
+/* MakeCommand: make the Update or Delete statement to send to the */
+/* MySQL server. Limited to remote values and filtering. */
+/***********************************************************************/
+int TDBMYSQL::MakeCommand(PGLOBAL g)
+ {
+ Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
+
+ if (Quoted > 0 || stricmp(Name, Tabname)) {
+ char *p, *qrystr, name[68];
+ bool qtd = Quoted > 0;
+
+
+ // Make a lower case copy of the originale query
+ qrystr = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 1);
+ strlwr(strcpy(qrystr, Qrystr));
+
+ // 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))
+ strlwr(strcpy(name, Name)); // Not a keyword
+
+ if ((p = strstr(qrystr, name))) {
+ memcpy(Query, Qrystr, p - qrystr);
+ Query[p - qrystr] = 0;
+
+ if (qtd && *(p-1) == ' ')
+ strcat(strcat(strcat(Query, "`"), Tabname), "`");
+ else
+ strcat(Query, Tabname);
+
+ strcat(Query, Qrystr + (p - qrystr) + strlen(name));
+ } else {
+ sprintf(g->Message, "Cannot use this %s command",
+ (Mode == MODE_UPDATE) ? "UPDATE" : "DELETE");
+ return RC_FX;
+ } // endif p
+
+ } else
+ strcpy(Query, Qrystr);
+
+ return RC_OK;
+ } // end of MakeCommand
+
+#if 0
+/***********************************************************************/
+/* MakeUpdate: make the Update statement use with MySQL connection. */
+/* Limited to remote values and filtering. */
+/***********************************************************************/
+int TDBMYSQL::MakeUpdate(PGLOBAL g)
+ {
+ char *qc, cmd[8], tab[96], end[1024];
+
+ Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
+ memset(end, 0, sizeof(end));
+
+ if (sscanf(Qrystr, "%s `%[^`]`%1023c", cmd, tab, end) > 2 ||
+ sscanf(Qrystr, "%s \"%[^\"]\"%1023c", cmd, tab, end) > 2)
+ qc = "`";
+ else if (sscanf(Qrystr, "%s %s%1023c", cmd, tab, end) > 2
+ && !stricmp(tab, Name))
+ qc = (Quoted) ? "`" : "";
+ else {
+ strcpy(g->Message, "Cannot use this UPDATE command");
+ return RC_FX;
+ } // endif sscanf
+
+ assert(!stricmp(cmd, "update"));
+ strcat(strcat(strcat(strcpy(Query, "UPDATE "), qc), Tabname), qc);
+ strcat(Query, end);
+ return RC_OK;
+ } // end of MakeUpdate
+
+/***********************************************************************/
+/* MakeDelete: make the Delete statement used with MySQL connection. */
+/* Limited to remote filtering. */
+/***********************************************************************/
+int TDBMYSQL::MakeDelete(PGLOBAL g)
+ {
+ char *qc, cmd[8], from[8], tab[96], end[512];
+
+ Query = (char*)PlugSubAlloc(g, NULL, strlen(Qrystr) + 64);
+ memset(end, 0, sizeof(end));
+
+ if (sscanf(Qrystr, "%s %s `%[^`]`%511c", cmd, from, tab, end) > 2 ||
+ sscanf(Qrystr, "%s %s \"%[^\"]\"%511c", cmd, from, tab, end) > 2)
+ qc = "`";
+ else if (sscanf(Qrystr, "%s %s %s%511c", cmd, from, tab, end) > 2)
+ qc = (Quoted) ? "`" : "";
+ else {
+ strcpy(g->Message, "Cannot use this DELETE command");
+ return RC_FX;
+ } // endif sscanf
+
+ assert(!stricmp(cmd, "delete") && !stricmp(from, "from"));
+ strcat(strcat(strcat(strcpy(Query, "DELETE FROM "), qc), Tabname), qc);
+
+ if (*end)
+ strcat(Query, end);
+
+ return RC_OK;
+ } // end of MakeDelete
+#endif // 0
+
+/***********************************************************************/
+/* XCV GetMaxSize: returns the maximum number of rows in the table. */
+/***********************************************************************/
+int TDBMYSQL::GetMaxSize(PGLOBAL g)
+ {
+ if (MaxSize < 0) {
+#if 0
+ if (MakeSelect(g))
+ return -2;
+
+ if (!Myc.Connected()) {
+ if (Myc.Open(g, Host, Database, User, Pwd, Port))
+ return -1;
+
+ } // endif connected
+
+ if ((MaxSize = Myc.GetResultSize(g, Query)) < 0) {
+ Myc.Close();
+ return -3;
+ } // endif MaxSize
+
+ // FIXME: Columns should be known when Info calls GetMaxSize
+ if (!Columns)
+ Query = NULL; // Must be remade when columns are known
+#endif // 0
+
+ // Return 0 in mode DELETE in case of delete all.
+ MaxSize = (Mode == MODE_DELETE) ? 0 : 10; // To make MySQL happy
+ } // endif MaxSize
+
+ return MaxSize;
+ } // end of GetMaxSize
+
+/***********************************************************************/
+/* This a fake routine as ROWID does not exist in MySQL. */
+/***********************************************************************/
+int TDBMYSQL::RowNumber(PGLOBAL g, bool b)
+ {
+ return N;
+ } // end of RowNumber
+
+/***********************************************************************/
+/* Return 0 in mode UPDATE to tell that the update is done. */
+/***********************************************************************/
+int TDBMYSQL::GetProgMax(PGLOBAL g)
+ {
+ return (Mode == MODE_UPDATE) ? 0 : GetMaxSize(g);
+ } // end of GetProgMax
+
+/***********************************************************************/
+/* MySQL Bind Parameter function. */
+/***********************************************************************/
+int TDBMYSQL::BindColumns(PGLOBAL g)
+ {
+#if defined(MYSQL_PREPARED_STATEMENTS)
+ if (Prep) {
+ Bind = (MYSQL_BIND*)PlugSubAlloc(g, NULL, Nparm * sizeof(MYSQL_BIND));
+
+ for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next)
+ colp->InitBind(g);
+
+ return Myc.BindParams(g, Bind);
+ } // endif prep
+#endif // MYSQL_PREPARED_STATEMENTS
+
+ return RC_OK;
+ } // end of BindColumns
+
+/***********************************************************************/
+/* MySQL Access Method opening routine. */
+/***********************************************************************/
+bool TDBMYSQL::OpenDB(PGLOBAL g)
+ {
+ if (Use == USE_OPEN) {
+ /*******************************************************************/
+ /* Table already open, just replace it at its beginning. */
+ /*******************************************************************/
+ Myc.Rewind();
+ return false;
+ } // endif use
+
+ /*********************************************************************/
+ /* Open a MySQL connection for this table. */
+ /* Note: this may not be the proper way to do. Perhaps it is better */
+ /* to test whether a connection is already open for this server */
+ /* and if so to allocate just a new result set. But this only for */
+ /* servers allowing concurency in getting results ??? */
+ /*********************************************************************/
+ if (!Myc.Connected()) {
+ if (Myc.Open(g, Host, Database, User, Pwd, Port))
+ return true;
+
+ } // endif Connected
+
+ /*********************************************************************/
+ /* Take care of DATE columns. */
+ /*********************************************************************/
+ for (PMYCOL colp = (PMYCOL)Columns; colp; colp = (PMYCOL)colp->Next)
+ if (colp->Buf_Type == TYPE_DATE)
+ // Format must match DATETIME MySQL type
+ ((DTVAL*)colp->GetValue())->SetFormat(g, "YYYY-MM-DD hh:mm:ss", 19);
+
+ /*********************************************************************/
+ /* Allocate whatever is used for getting results. */
+ /*********************************************************************/
+ if (Mode == MODE_READ) {
+ if (!MakeSelect(g))
+ m_Rc = Myc.ExecSQL(g, Query);
+
+#if 0
+ if (!Myc.m_Res || !Myc.m_Fields) {
+ sprintf(g->Message, "%s result", (Myc.m_Res) ? "Void" : "No");
+ Myc.Close();
+ return true;
+ } // endif m_Res
+#endif // 0
+
+ if (!m_Rc && Srcdef)
+ if (SetColumnRanks(g))
+ return true;
+
+ } else if (Mode == MODE_INSERT) {
+ if (Srcdef) {
+ strcpy(g->Message, "No insert into anonym views");
+ return true;
+ } // endif Srcdef
+
+ if (!MakeInsert(g)) {
+#if defined(MYSQL_PREPARED_STATEMENTS)
+ int n = (Prep) ? Myc.PrepareSQL(g, Query) : Nparm;
+
+ if (Nparm != n) {
+ if (n >= 0) // Other errors return negative values
+ strcpy(g->Message, MSG(BAD_PARM_COUNT));
+
+ } else
+#endif // MYSQL_PREPARED_STATEMENTS
+ m_Rc = BindColumns(g);
+
+ } // endif MakeInsert
+
+ if (m_Rc != RC_FX) {
+ char cmd[64];
+ int w;
+
+ sprintf(cmd, "ALTER TABLE `%s` DISABLE KEYS", Tabname);
+ m_Rc = Myc.ExecSQL(g, cmd, &w);
+ } // endif m_Rc
+
+ } else
+// m_Rc = (Mode == MODE_DELETE) ? MakeDelete(g) : MakeUpdate(g);
+ m_Rc = MakeCommand(g);
+
+ if (m_Rc == RC_FX) {
+ Myc.Close();
+ return true;
+ } // endif rc
+
+ Use = USE_OPEN;
+ return false;
+ } // end of OpenDB
+
+/***********************************************************************/
+/* Set the rank of columns in the result set. */
+/***********************************************************************/
+bool TDBMYSQL::SetColumnRanks(PGLOBAL g)
+ {
+ for (PCOL colp = Columns; colp; colp = colp->GetNext())
+ if (((PMYCOL)colp)->FindRank(g))
+ return TRUE;
+
+ return FALSE;
+ } // end of SetColumnRanks
+
+/***********************************************************************/
+/* Called by Parent table to make the columns of a View. */
+/***********************************************************************/
+PCOL TDBMYSQL::MakeFieldColumn(PGLOBAL g, char *name)
+ {
+ int n;
+ MYSQL_FIELD *fld;
+ PCOL cp, colp = NULL;
+
+ for (n = 0; n < Myc.m_Fields; n++) {
+ fld = &Myc.m_Res->fields[n];
+
+ if (!stricmp(name, fld->name)) {
+ colp = new(g) MYSQLCOL(fld, this, n);
+
+ if (colp->InitValue(g))
+ return NULL;
+
+ if (!Columns)
+ Columns = colp;
+ else for (cp = Columns; cp; cp = cp->GetNext())
+ if (!cp->GetNext()) {
+ cp->SetNext(colp);
+ break;
+ } // endif Next
+
+ break;
+ } // endif name
+
+ } // endfor n
+
+ if (!colp)
+ sprintf(g->Message, "Column %s is not in view", name);
+
+ return colp;
+ } // end of MakeFieldColumn
+
+/***********************************************************************/
+/* Called by Pivot tables to find default column names in a View */
+/* as the name of last field not equal to the passed name. */
+/***********************************************************************/
+char *TDBMYSQL::FindFieldColumn(char *name)
+ {
+ int n;
+ MYSQL_FIELD *fld;
+ char *cp = NULL;
+
+ for (n = Myc.m_Fields - 1; n >= 0; n--) {
+ fld = &Myc.m_Res->fields[n];
+
+ if (!name || stricmp(name, fld->name)) {
+ cp = fld->name;
+ break;
+ } // endif name
+
+ } // endfor n
+
+ return cp;
+ } // end of FindFieldColumn
+
+/***********************************************************************/
+/* Send an UPDATE or DELETE command to the remote server. */
+/***********************************************************************/
+int TDBMYSQL::SendCommand(PGLOBAL g)
+ {
+ int w;
+
+ if (Myc.ExecSQLcmd(g, Query, &w) == RC_NF) {
+ AftRows = Myc.m_Afrw;
+ sprintf(g->Message, "%s: %d affected rows", Tabname, AftRows);
+ PushWarning(g, this, 0); // 0 means a Note
+
+ if (trace)
+ htrc("%s\n", g->Message);
+
+ if (w && Myc.ExecSQL(g, "SHOW WARNINGS") == RC_OK) {
+ // We got warnings from the remote server
+ while (Myc.Fetch(g, -1) == RC_OK) {
+ sprintf(g->Message, "%s: (%s) %s", Tabname,
+ Myc.GetCharField(1), Myc.GetCharField(2));
+ PushWarning(g, this);
+ } // endwhile Fetch
+
+ Myc.FreeResult();
+ } // endif w
+
+ return RC_EF; // Nothing else to do
+ } else
+ return RC_FX; // Error
+
+ } // end of SendCommand
+
+/***********************************************************************/
+/* Data Base read routine for MYSQL access method. */
+/***********************************************************************/
+int TDBMYSQL::ReadDB(PGLOBAL g)
+ {
+ int rc;
+
+ if (trace > 1)
+ htrc("MySQL ReadDB: R%d Mode=%d key=%p link=%p Kindex=%p\n",
+ GetTdb_No(), Mode, To_Key_Col, To_Link, To_Kindex);
+
+ if (Mode == MODE_UPDATE || Mode == MODE_DELETE)
+ return SendCommand(g);
+
+ /*********************************************************************/
+ /* Now start the reading process. */
+ /* Here is the place to fetch the line. */
+ /*********************************************************************/
+ N++;
+ Fetched = ((rc = Myc.Fetch(g, -1)) == RC_OK);
+
+ if (trace > 1)
+ htrc(" Read: rc=%d\n", rc);
+
+ return rc;
+ } // end of ReadDB
+
+/***********************************************************************/
+/* WriteDB: Data Base write routine for MYSQL access methods. */
+/***********************************************************************/
+int TDBMYSQL::WriteDB(PGLOBAL g)
+ {
+#if defined(MYSQL_PREPARED_STATEMENTS)
+ if (Prep)
+ return Myc.ExecStmt(g);
+#endif // MYSQL_PREPARED_STATEMENTS
+
+ // Statement was not prepared, we must construct and execute
+ // an insert query for each line to insert
+ int rc;
+ char buf[64];
+
+ strcpy(Qbuf, Query);
+
+ // Make the Insert command value list
+ for (PCOL colp = Columns; colp; colp = colp->GetNext()) {
+ if (!colp->GetValue()->IsNull()) {
+ if (colp->GetResultType() == TYPE_STRING ||
+ colp->GetResultType() == TYPE_DATE)
+ strcat(Qbuf, "'");
+
+ strcat(Qbuf, colp->GetValue()->GetCharString(buf));
+
+ if (colp->GetResultType() == TYPE_STRING ||
+ colp->GetResultType() == TYPE_DATE)
+ strcat(Qbuf, "'");
+
+ } else
+ strcat(Qbuf, "NULL");
+
+ strcat(Qbuf, (colp->GetNext()) ? "," : ")");
+ } // endfor colp
+
+ Myc.m_Rows = -1; // To execute the query
+ rc = Myc.ExecSQL(g, Qbuf);
+ return (rc == RC_NF) ? RC_OK : rc; // RC_NF is Ok
+ } // end of WriteDB
+
+/***********************************************************************/
+/* Data Base delete all routine for MYSQL access methods. */
+/***********************************************************************/
+int TDBMYSQL::DeleteDB(PGLOBAL g, int irc)
+ {
+ if (irc == RC_FX)
+ // Send the DELETE (all) command to the remote table
+ return (SendCommand(g) == RC_FX) ? RC_FX : RC_OK;
+ else
+ return RC_OK; // Ignore
+
+ } // end of DeleteDB
+
+/***********************************************************************/
+/* Data Base close routine for MySQL access method. */
+/***********************************************************************/
+void TDBMYSQL::CloseDB(PGLOBAL g)
+ {
+ if (Myc.Connected()) {
+ if (Mode == MODE_INSERT) {
+ char cmd[64];
+ int w;
+ PDBUSER dup = PlgGetUser(g);
+
+ dup->Step = "Enabling indexes";
+ sprintf(cmd, "ALTER TABLE `%s` ENABLE KEYS", Tabname);
+ Myc.m_Rows = -1; // To execute the query
+ m_Rc = Myc.ExecSQL(g, cmd, &w);
+ } // endif m_Rc
+
+ Myc.Close();
+ } // endif Myc
+
+ if (trace)
+ htrc("MySQL CloseDB: closing %s rc=%d\n", Name, m_Rc);
+
+ } // end of CloseDB
+
+// ------------------------ MYSQLCOL functions --------------------------
+
+/***********************************************************************/
+/* MYSQLCOL public constructor. */
+/***********************************************************************/
+MYSQLCOL::MYSQLCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PSZ am)
+ : COLBLK(cdp, tdbp, i)
+ {
+ if (cprec) {
+ Next = cprec->GetNext();
+ cprec->SetNext(this);
+ } else {
+ Next = tdbp->GetColumns();
+ tdbp->SetColumns(this);
+ } // endif cprec
+
+ // Set additional MySQL access method information for column.
+ Precision = Long = cdp->GetLong();
+ Bind = NULL;
+ To_Val = NULL;
+ Slen = 0;
+ Rank = -1; // Not known yet
+
+ if (trace)
+ htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
+
+ } // end of MYSQLCOL constructor
+
+/***********************************************************************/
+/* MYSQLCOL public constructor. */
+/***********************************************************************/
+MYSQLCOL::MYSQLCOL(MYSQL_FIELD *fld, PTDB tdbp, int i, PSZ am)
+ : COLBLK(NULL, tdbp, i)
+ {
+ Name = fld->name;
+ Opt = 0;
+ Precision = Long = fld->length;
+ Buf_Type = MYSQLtoPLG(fld->type);
+ strcpy(Format.Type, GetFormatType(Buf_Type));
+ Format.Length = Long;
+ Format.Prec = fld->decimals;
+ ColUse = U_P;
+ Nullable = !IS_NOT_NULL(fld->flags);
+
+ // Set additional MySQL access method information for column.
+ Bind = NULL;
+ To_Val = NULL;
+ Slen = 0;
+ Rank = i;
+
+ if (trace)
+ htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
+
+ } // end of MYSQLCOL constructor
+
+/***********************************************************************/
+/* MYSQLCOL constructor used for copying columns. */
+/* tdbp is the pointer to the new table descriptor. */
+/***********************************************************************/
+MYSQLCOL::MYSQLCOL(MYSQLCOL *col1, PTDB tdbp) : COLBLK(col1, tdbp)
+ {
+ Long = col1->Long;
+ Bind = NULL;
+ To_Val = NULL;
+ Slen = col1->Slen;
+ Rank = col1->Rank;
+ } // end of MYSQLCOL copy constructor
+
+/***********************************************************************/
+/* FindRank: Find the rank of this column in the result set. */
+/***********************************************************************/
+bool MYSQLCOL::FindRank(PGLOBAL g)
+{
+ int n;
+ MYSQLC myc = ((PTDBMY)To_Tdb)->Myc;
+
+ for (n = 0; n < myc.m_Fields; n++)
+ if (!stricmp(Name, myc.m_Res->fields[n].name)) {
+ Rank = n;
+ return false;
+ } // endif Name
+
+ sprintf(g->Message, "Column %s not in result set", Name);
+ return true;
+} // end of FindRank
+
+/***********************************************************************/
+/* SetBuffer: prepare a column block for write operation. */
+/***********************************************************************/
+bool MYSQLCOL::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
+
+/***********************************************************************/
+/* InitBind: Initialize the bind structure according to type. */
+/***********************************************************************/
+void MYSQLCOL::InitBind(PGLOBAL g)
+ {
+ PTDBMY tdbp = (PTDBMY)To_Tdb;
+
+ assert(tdbp->Bind && Rank < tdbp->Nparm);
+
+ Bind = &tdbp->Bind[Rank];
+ memset(Bind, 0, sizeof(MYSQL_BIND));
+
+ if (Buf_Type == TYPE_DATE) {
+ Bind->buffer_type = PLGtoMYSQL(TYPE_STRING, false);
+ Bind->buffer = (char *)PlugSubAlloc(g,NULL, 20);
+ Bind->buffer_length = 20;
+ Bind->length = &Slen;
+ } else {
+ Bind->buffer_type = PLGtoMYSQL(Buf_Type, false);
+ Bind->buffer = (char *)Value->GetTo_Val();
+ Bind->buffer_length = Value->GetClen();
+ Bind->length = (IsTypeChar(Buf_Type)) ? &Slen : NULL;
+ } // endif Buf_Type
+
+ } // end of InitBind
+
+/***********************************************************************/
+/* ReadColumn: */
+/***********************************************************************/
+void MYSQLCOL::ReadColumn(PGLOBAL g)
+ {
+ char *p, *buf, tim[20];
+ int rc;
+ PTDBMY tdbp = (PTDBMY)To_Tdb;
+
+ /*********************************************************************/
+ /* If physical fetching of the line was deferred, do it now. */
+ /*********************************************************************/
+ if (!tdbp->Fetched)
+ if ((rc = tdbp->Myc.Fetch(g, tdbp->N)) != RC_OK) {
+ if (rc == RC_EF)
+ sprintf(g->Message, MSG(INV_DEF_READ), rc);
+
+ longjmp(g->jumper[g->jump_level], 11);
+ } else
+ tdbp->Fetched = TRUE;
+
+ if ((buf = ((PTDBMY)To_Tdb)->Myc.GetCharField(Rank))) {
+ if (trace > 1)
+ htrc("MySQL ReadColumn: name=%s buf=%s\n", Name, buf);
+
+ // TODO: have a true way to differenciate temporal values
+ if (Buf_Type == TYPE_DATE && strlen(buf) == 8)
+ // This is a TIME value
+ p = strcat(strcpy(tim, "1970-01-01 "), buf);
+ else
+ p = buf;
+
+ if (Value->SetValue_char(p, strlen(p))) {
+ sprintf(g->Message, "Out of range value for column %s at row %d",
+ Name, tdbp->RowNumber(g));
+ PushWarning(g, tdbp);
+ } // endif SetValue_char
+
+ } else {
+ if (Nullable)
+ Value->SetNull(true);
+
+ Value->Reset(); // Null value
+ } // endif buf
+
+ } // end of ReadColumn
+
+/***********************************************************************/
+/* WriteColumn: make sure the bind buffer is updated. */
+/***********************************************************************/
+void MYSQLCOL::WriteColumn(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Do convert the column value if necessary. */
+ /*********************************************************************/
+ if (Value != To_Val)
+ Value->SetValue_pval(To_Val, FALSE); // Convert the inserted value
+
+#if defined(MYSQL_PREPARED_STATEMENTS)
+ if (((PTDBMY)To_Tdb)->Prep) {
+ if (Buf_Type == TYPE_DATE) {
+ Value->ShowValue((char *)Bind->buffer, (int)Bind->buffer_length);
+ Slen = strlen((char *)Bind->buffer);
+ } else if (IsTypeChar(Buf_Type))
+ Slen = strlen(Value->GetCharValue());
+
+ } // endif Prep
+#endif // MYSQL_PREPARED_STATEMENTS
+
+ } // end of WriteColumn
+
+/* ------------------------------------------------------------------- */
+
+/***********************************************************************/
+/* Implementation of the TDBMYEXC class. */
+/***********************************************************************/
+TDBMYEXC::TDBMYEXC(PMYDEF tdp) : TDBMYSQL(tdp)
+{
+ Cmdlist = NULL;
+ Cmdcol = NULL;
+ Shw = false;
+ Havew = false;
+ Isw = false;
+ Warnings = 0;
+ Mxr = tdp->Mxr;
+ Nerr = 0;
+} // end of TDBMYEXC constructor
+
+TDBMYEXC::TDBMYEXC(PGLOBAL g, PTDBMYX tdbp) : TDBMYSQL(g, tdbp)
+{
+ Cmdlist = tdbp->Cmdlist;
+ Cmdcol = tdbp->Cmdcol;
+ Shw = tdbp->Shw;
+ Havew = tdbp->Havew;
+ Isw = tdbp->Isw;
+ Mxr = tdbp->Mxr;
+ Nerr = tdbp->Nerr;
+} // end of TDBMYEXC copy constructor
+
+// Is this really useful ???
+PTDB TDBMYEXC::CopyOne(PTABS t)
+ {
+ PTDB tp;
+ PCOL cp1, cp2;
+ PGLOBAL g = t->G;
+
+ tp = new(g) TDBMYEXC(g, this);
+
+ for (cp1 = Columns; cp1; cp1 = cp1->GetNext()) {
+ cp2 = new(g) MYXCOL((PMYXCOL)cp1, tp);
+
+ NewPointer(t, cp1, cp2);
+ } // endfor cp1
+
+ return tp;
+ } // end of CopyOne
+
+/***********************************************************************/
+/* Allocate MYSQL column description block. */
+/***********************************************************************/
+PCOL TDBMYEXC::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
+ {
+ PMYXCOL colp = new(g) MYXCOL(cdp, this, cprec, n);
+
+ if (!colp->Flag)
+ Cmdcol = colp->GetName();
+
+ return colp;
+ } // end of MakeCol
+
+/***********************************************************************/
+/* MakeCMD: make the SQL statement to send to MYSQL connection. */
+/***********************************************************************/
+PCMD TDBMYEXC::MakeCMD(PGLOBAL g)
+ {
+ PCMD xcmd = NULL;
+
+ if (To_CondFil) {
+ if (Cmdcol) {
+ if (!stricmp(Cmdcol, To_CondFil->Body) &&
+ (To_CondFil->Op == OP_EQ || To_CondFil->Op == OP_IN)) {
+ xcmd = To_CondFil->Cmds;
+ } else
+ strcpy(g->Message, "Invalid command specification filter");
+
+ } else
+ strcpy(g->Message, "No command column in select list");
+
+ } else if (!Srcdef)
+ strcpy(g->Message, "No Srcdef default command");
+ else
+ xcmd = new(g) CMD(g, Srcdef);
+
+ return xcmd;
+ } // end of MakeCMD
+
+/***********************************************************************/
+/* EXC GetMaxSize: returns the maximum number of rows in the table. */
+/***********************************************************************/
+int TDBMYEXC::GetMaxSize(PGLOBAL g)
+ {
+ if (MaxSize < 0) {
+ MaxSize = 10; // a guess
+ } // endif MaxSize
+
+ return MaxSize;
+ } // end of GetMaxSize
+
+/***********************************************************************/
+/* MySQL Exec Access Method opening routine. */
+/***********************************************************************/
+bool TDBMYEXC::OpenDB(PGLOBAL g)
+ {
+ if (Use == USE_OPEN) {
+ strcpy(g->Message, "Multiple execution is not allowed");
+ return true;
+ } // endif use
+
+ /*********************************************************************/
+ /* Open a MySQL connection for this table. */
+ /* Note: this may not be the proper way to do. Perhaps it is better */
+ /* to test whether a connection is already open for this server */
+ /* and if so to allocate just a new result set. But this only for */
+ /* servers allowing concurency in getting results ??? */
+ /*********************************************************************/
+ if (!Myc.Connected())
+ if (Myc.Open(g, Host, Database, User, Pwd, Port))
+ return true;
+
+ Use = USE_OPEN; // Do it now in case we are recursively called
+
+ if (Mode != MODE_READ) {
+ strcpy(g->Message, "No INSERT/DELETE/UPDATE of MYSQL EXEC tables");
+ return true;
+ } // endif Mode
+
+ /*********************************************************************/
+ /* Get the command to execute. */
+ /*********************************************************************/
+ if (!(Cmdlist = MakeCMD(g))) {
+ Myc.Close();
+ return true;
+ } // endif Query
+
+ return false;
+ } // end of OpenDB
+
+/***********************************************************************/
+/* Data Base read routine for MYSQL access method. */
+/***********************************************************************/
+int TDBMYEXC::ReadDB(PGLOBAL g)
+ {
+ if (Havew) {
+ // Process result set from SHOW WARNINGS
+ if (Myc.Fetch(g, -1) != RC_OK) {
+ Myc.FreeResult();
+ Havew = Isw = false;
+ } else {
+ N++;
+ Isw = true;
+ return RC_OK;
+ } // endif Fetch
+
+ } // endif m_Res
+
+ if (Cmdlist) {
+ // Process query to send
+ int rc;
+
+ do {
+ Query = Cmdlist->Cmd;
+
+ switch (rc = Myc.ExecSQLcmd(g, Query, &Warnings)) {
+ case RC_NF:
+ AftRows = Myc.m_Afrw;
+ strcpy(g->Message, "Affected rows");
+ break;
+ case RC_OK:
+ AftRows = Myc.m_Fields;
+ strcpy(g->Message, "Result set columns");
+ break;
+ case RC_FX:
+ AftRows = Myc.m_Afrw;
+ Nerr++;
+ break;
+ case RC_INFO:
+ Shw = true;
+ } // endswitch rc
+
+ Cmdlist = (Nerr > Mxr) ? NULL : Cmdlist->Next;
+ } while (rc == RC_INFO);
+
+ if (Shw && Warnings)
+ Havew = (Myc.ExecSQL(g, "SHOW WARNINGS") == RC_OK);
+
+ ++N;
+ return RC_OK;
+ } else
+ return RC_EF;
+
+ } // end of ReadDB
+
+/***********************************************************************/
+/* WriteDB: Data Base write routine for Exec MYSQL access methods. */
+/***********************************************************************/
+int TDBMYEXC::WriteDB(PGLOBAL g)
+ {
+ strcpy(g->Message, "EXEC MYSQL tables are read only");
+ return RC_FX;
+ } // end of WriteDB
+
+// ------------------------- MYXCOL functions ---------------------------
+
+/***********************************************************************/
+/* MYXCOL public constructor. */
+/***********************************************************************/
+MYXCOL::MYXCOL(PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i, PSZ am)
+ : MYSQLCOL(cdp, tdbp, cprec, i, am)
+ {
+ // Set additional EXEC MYSQL access method information for column.
+ Flag = cdp->GetOffset();
+ } // end of MYSQLCOL constructor
+
+/***********************************************************************/
+/* MYSQLCOL public constructor. */
+/***********************************************************************/
+MYXCOL::MYXCOL(MYSQL_FIELD *fld, PTDB tdbp, int i, PSZ am)
+ : MYSQLCOL(fld, tdbp, i, am)
+ {
+ if (trace)
+ htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
+
+ } // end of MYSQLCOL constructor
+
+/***********************************************************************/
+/* MYXCOL constructor used for copying columns. */
+/* tdbp is the pointer to the new table descriptor. */
+/***********************************************************************/
+MYXCOL::MYXCOL(MYXCOL *col1, PTDB tdbp) : MYSQLCOL(col1, tdbp)
+ {
+ Flag = col1->Flag;
+ } // end of MYXCOL copy constructor
+
+/***********************************************************************/
+/* ReadColumn: */
+/***********************************************************************/
+void MYXCOL::ReadColumn(PGLOBAL g)
+ {
+ PTDBMYX tdbp = (PTDBMYX)To_Tdb;
+
+ if (tdbp->Isw) {
+ char *buf = NULL;
+
+ if (Flag < 3) {
+ buf = tdbp->Myc.GetCharField(Flag);
+ Value->SetValue_psz(buf);
+ } else
+ Value->Reset();
+
+ } else
+ switch (Flag) {
+ case 0: Value->SetValue_psz(tdbp->Query); break;
+ case 1: Value->SetValue(tdbp->AftRows); break;
+ case 2: Value->SetValue_psz(g->Message); break;
+ case 3: Value->SetValue(tdbp->Warnings); break;
+ default: Value->SetValue_psz("Invalid Flag"); break;
+ } // endswitch Flag
+
+ } // end of ReadColumn
+
+/***********************************************************************/
+/* WriteColumn: should never be called. */
+/***********************************************************************/
+void MYXCOL::WriteColumn(PGLOBAL g)
+ {
+ assert(false);
+ } // end of WriteColumn
+
+/* ---------------------------TDBMCL class --------------------------- */
+
+/***********************************************************************/
+/* TDBMCL class constructor. */
+/***********************************************************************/
+TDBMCL::TDBMCL(PMYDEF tdp) : TDBCAT(tdp)
+ {
+ Host = tdp->Hostname;
+ Db = tdp->Database;
+ Tab = tdp->Tabname;
+ User = tdp->Username;
+ Pwd = tdp->Password;
+ Port = tdp->Portnumber;
+ } // end of TDBMCL constructor
+
+/***********************************************************************/
+/* GetResult: Get the list the MYSQL table columns. */
+/***********************************************************************/
+PQRYRES TDBMCL::GetResult(PGLOBAL g)
+ {
+ return MyColumns(g, Host, Db, User, Pwd, Tab, NULL, Port, false);
+ } // end of GetResult
diff --git a/storage/connect/tabodbc.cpp b/storage/connect/tabodbc.cpp
index c7513601979..cbce9adc7ac 100644
--- a/storage/connect/tabodbc.cpp
+++ b/storage/connect/tabodbc.cpp
@@ -1066,7 +1066,7 @@ void ODBCCOL::ReadColumn(PGLOBAL g)
} // endif Buf_Type
if (g->Trace) {
- char buf[32];
+ char buf[64];
htrc("ODBC Column %s: rows=%d buf=%p type=%d value=%s\n",
Name, tdbp->Rows, Bufp, Buf_Type, Value->GetCharString(buf));
diff --git a/storage/connect/tabpivot.cpp b/storage/connect/tabpivot.cpp
index ec053357d19..7cde2ab4cbd 100644
--- a/storage/connect/tabpivot.cpp
+++ b/storage/connect/tabpivot.cpp
@@ -95,7 +95,7 @@ PIVAID::PIVAID(const char *tab, const char *src, const char *picol,
/***********************************************************************/
PQRYRES PIVAID::MakePivotColumns(PGLOBAL g)
{
- char *query, *colname, buf[32];
+ char *query, *colname, buf[64];
int ndif, nblin, w = 0;
PVAL valp;
PCOLRES *pcrp, crp, fncrp = NULL;
@@ -121,7 +121,7 @@ PQRYRES PIVAID::MakePivotColumns(PGLOBAL g)
} // endif Exec
// We must have a storage query to get pivot column values
- Qryp = Myc.GetResult(g);
+ Qryp = Myc.GetResult(g, true);
Myc.Close();
if (!Fncol) {
diff --git a/storage/connect/tabvct.cpp b/storage/connect/tabvct.cpp
index baa13fe36e7..251af4d02f8 100644
--- a/storage/connect/tabvct.cpp
+++ b/storage/connect/tabvct.cpp
@@ -76,6 +76,8 @@
char *strerror(int num);
#endif // UNIX
+extern "C" int trace;
+
/***********************************************************************/
/* Char VCT column blocks are right filled with blanks (blank = true) */
/* Conversion of block values allowed conditionally for insert only. */
@@ -287,10 +289,9 @@ PCOL TDBVCT::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
/***********************************************************************/
bool TDBVCT::OpenDB(PGLOBAL g)
{
-#ifdef DEBTRACE
- htrc("VCT OpenDB: tdbp=%p tdb=R%d use=%d key=%p mode=%d\n",
- this, Tdb_No, Use, To_Key_Col, Mode);
-#endif
+ if (trace)
+ htrc("VCT 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) {
/*******************************************************************/
@@ -344,12 +345,10 @@ bool TDBVCT::OpenDB(PGLOBAL g)
/***********************************************************************/
int TDBVCT::ReadDB(PGLOBAL g)
{
-#ifdef DEBTRACE
- fprintf(debug,
- "VCT ReadDB: R%d Mode=%d CurBlk=%d CurNum=%d key=%p link=%p Kindex=%p\n",
- GetTdb_No(), Mode, Txfp->CurBlk, Txfp->CurNum,
- To_Key_Col, To_Link, To_Kindex);
-#endif
+ if (trace)
+ htrc("VCT ReadDB: R%d Mode=%d CurBlk=%d CurNum=%d key=%p link=%p Kindex=%p\n",
+ GetTdb_No(), Mode, Txfp->CurBlk, Txfp->CurNum,
+ To_Key_Col, To_Link, To_Kindex);
if (To_Kindex) {
/*******************************************************************/
@@ -524,15 +523,13 @@ void VCTCOL::ReadColumn(PGLOBAL g)
{
PTXF txfp = ((PTDBVCT)To_Tdb)->Txfp;
-#if defined(_DEBUG) || defined(DEBTRACE)
+#if defined(_DEBUG)
assert (!To_Kcol);
#endif
-#ifdef DEBTRACE
- fprintf(debug,
- "VCT ReadColumn: col %s R%d coluse=%.4X status=%.4X buf_type=%d\n",
- Name, To_Tdb->GetTdb_No(), ColUse, Status, Buf_Type);
-#endif
+ if (trace > 1)
+ htrc("VCT ReadColumn: col %s R%d coluse=%.4X status=%.4X buf_type=%d\n",
+ Name, To_Tdb->GetTdb_No(), ColUse, Status, Buf_Type);
if (ColBlk != txfp->CurBlk)
ReadBlock(g);
@@ -558,11 +555,9 @@ void VCTCOL::WriteColumn(PGLOBAL g)
{
PTXF txfp = ((PTDBVCT)To_Tdb)->Txfp;;
-#ifdef DEBTRACE
- fprintf(debug,
- "VCT WriteColumn: col %s R%d coluse=%.4X status=%.4X buf_type=%d\n",
- Name, To_Tdb->GetTdb_No(), ColUse, Status, Buf_Type);
-#endif
+ if (trace > 1)
+ htrc("VCT WriteColumn: col %s R%d coluse=%.4X status=%.4X buf_type=%d\n",
+ Name, To_Tdb->GetTdb_No(), ColUse, Status, Buf_Type);
ColBlk = txfp->CurBlk;
ColPos = txfp->CurNum;
diff --git a/storage/connect/user_connect.cc b/storage/connect/user_connect.cc
index 165ef423e7c..e170352874f 100644
--- a/storage/connect/user_connect.cc
+++ b/storage/connect/user_connect.cc
@@ -1,161 +1,160 @@
-/* Copyright (C) Olivier Bertrand 2004 - 2012
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
-
-/**
- @file user_connect.cc
-
- @brief
- Implements the user_connect class.
-
- @details
- To support multi_threading, each query creates and use a PlugDB "user"
- that is a connection with its personnal memory allocation.
-
- @note
-
-*/
-
-/****************************************************************************/
-/* Author: Olivier Bertrand -- bertrandop@gmail.com -- 2004-2012 */
-/****************************************************************************/
-#ifdef USE_PRAGMA_IMPLEMENTATION
-#pragma implementation // gcc: Class implementation
-#endif
-
-#define DONT_DEFINE_VOID
-#define MYSQL_SERVER
-#include "sql_class.h"
-#undef OFFSET
-
-#define NOPARSE
-#include "osutil.h"
-#include "global.h"
-#include "plgdbsem.h"
-#include "connect.h"
-#include "user_connect.h"
-#include "mycat.h"
-
-extern "C" char plgxini[];
-extern int xtrace;
-
-/****************************************************************************/
-/* Initialize the user_connect static member. */
-/****************************************************************************/
-PCONNECT user_connect::to_users= NULL;
-
-/* -------------------------- class user_connect -------------------------- */
-
-/****************************************************************************/
-/* Constructor. */
-/****************************************************************************/
-user_connect::user_connect(THD *thd, const char *dbn)
-{
- thdp= thd;
- next= NULL;
- previous= NULL;
- g= NULL;
- last_query_id= 0;
- count= 0;
-
- // Statistics
- nrd= fnd= nfd= 0;
- tb1= 0;
-} // end of user_connect constructor
-
-
-/****************************************************************************/
-/* Destructor. */
-/****************************************************************************/
-user_connect::~user_connect()
-{
- // Terminate CONNECT and Plug-like environment, should return NULL
- g= CntExit(g);
-} // end of user_connect destructor
-
-
-/****************************************************************************/
-/* Initialization. */
-/****************************************************************************/
-bool user_connect::user_init()
-{
- // Initialize Plug-like environment
- PACTIVITY ap= NULL;
- PDBUSER dup= NULL;
-
- // Areasize= 64M because of VEC tables. Should be parameterisable
- g= PlugInit(NULL, 67108864);
-//g= PlugInit(NULL, 134217728); // 128M was because of old embedded tests
-
- // Check whether the initialization is complete
- if (!g || !g->Sarea || PlugSubSet(g, g->Sarea, g->Sarea_Size)
- || !(dup= PlgMakeUser(g))) {
- if (g)
- printf("%s\n", g->Message);
-
- int rc= PlugExit(g);
- g= NULL;
- free(dup);
- return true;
- } // endif g->
-
- dup->Catalog= new MYCAT(NULL);
-
- ap= new ACTIVITY;
- memset(ap, 0, sizeof(ACTIVITY));
- strcpy(ap->Ap_Name, "CONNECT");
- g->Activityp= ap;
- g->Activityp->Aptr= dup;
- next= to_users;
- to_users= this;
-
- if (next)
- next->previous= this;
-
- last_query_id= thdp->query_id;
- count= 1;
- return false;
-} // end of user_init
-
-
-void user_connect::SetHandler(ha_connect *hc)
-{
- PDBUSER dup= (PDBUSER)g->Activityp->Aptr;
- MYCAT *mc= (MYCAT*)dup->Catalog;
- mc->SetHandler(hc);
-}
-
-/****************************************************************************/
-/* Check whether we begin a new query and if so cleanup the previous one. */
-/****************************************************************************/
-bool user_connect::CheckCleanup(void)
-{
- if (thdp->query_id > last_query_id) {
- PlugCleanup(g, true);
- PlugSubSet(g, g->Sarea, g->Sarea_Size);
- g->Xchk = NULL;
- g->Createas = 0;
- g->Alchecked = 0;
- g->Mrr = 0;
- last_query_id= thdp->query_id;
-
- if (xtrace)
- printf("=====> Begin new query %llu\n", last_query_id);
-
- return true;
- } // endif query_id
-
- return false;
-} // end of CheckCleanup
-
+/* Copyright (C) Olivier Bertrand 2004 - 2014
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+/**
+ @file user_connect.cc
+
+ @brief
+ Implements the user_connect class.
+
+ @details
+ To support multi_threading, each query creates and use a PlugDB "user"
+ that is a connection with its personnal memory allocation.
+
+ @note
+
+*/
+
+/****************************************************************************/
+/* Author: Olivier Bertrand -- bertrandop@gmail.com -- 2004-2014 */
+/****************************************************************************/
+#ifdef USE_PRAGMA_IMPLEMENTATION
+#pragma implementation // gcc: Class implementation
+#endif
+
+#define DONT_DEFINE_VOID
+#define MYSQL_SERVER
+#include "sql_class.h"
+#undef OFFSET
+
+#define NOPARSE
+#include "osutil.h"
+#include "global.h"
+#include "plgdbsem.h"
+#include "connect.h"
+#include "user_connect.h"
+#include "mycat.h"
+
+extern "C" int trace;
+
+/****************************************************************************/
+/* Initialize the user_connect static member. */
+/****************************************************************************/
+PCONNECT user_connect::to_users= NULL;
+
+/* -------------------------- class user_connect -------------------------- */
+
+/****************************************************************************/
+/* Constructor. */
+/****************************************************************************/
+user_connect::user_connect(THD *thd, const char *dbn)
+{
+ thdp= thd;
+ next= NULL;
+ previous= NULL;
+ g= NULL;
+ last_query_id= 0;
+ count= 0;
+
+ // Statistics
+ nrd= fnd= nfd= 0;
+ tb1= 0;
+} // end of user_connect constructor
+
+
+/****************************************************************************/
+/* Destructor. */
+/****************************************************************************/
+user_connect::~user_connect()
+{
+ // Terminate CONNECT and Plug-like environment, should return NULL
+ g= CntExit(g);
+} // end of user_connect destructor
+
+
+/****************************************************************************/
+/* Initialization. */
+/****************************************************************************/
+bool user_connect::user_init()
+{
+ // Initialize Plug-like environment
+ PACTIVITY ap= NULL;
+ PDBUSER dup= NULL;
+
+ // Areasize= 64M because of VEC tables. Should be parameterisable
+ g= PlugInit(NULL, 67108864);
+//g= PlugInit(NULL, 134217728); // 128M was because of old embedded tests
+
+ // Check whether the initialization is complete
+ if (!g || !g->Sarea || PlugSubSet(g, g->Sarea, g->Sarea_Size)
+ || !(dup= PlgMakeUser(g))) {
+ if (g)
+ printf("%s\n", g->Message);
+
+ int rc= PlugExit(g);
+ g= NULL;
+ free(dup);
+ return true;
+ } // endif g->
+
+ dup->Catalog= new MYCAT(NULL);
+
+ ap= new ACTIVITY;
+ memset(ap, 0, sizeof(ACTIVITY));
+ strcpy(ap->Ap_Name, "CONNECT");
+ g->Activityp= ap;
+ g->Activityp->Aptr= dup;
+ next= to_users;
+ to_users= this;
+
+ if (next)
+ next->previous= this;
+
+ last_query_id= thdp->query_id;
+ count= 1;
+ return false;
+} // end of user_init
+
+
+void user_connect::SetHandler(ha_connect *hc)
+{
+ PDBUSER dup= (PDBUSER)g->Activityp->Aptr;
+ MYCAT *mc= (MYCAT*)dup->Catalog;
+ mc->SetHandler(hc);
+}
+
+/****************************************************************************/
+/* Check whether we begin a new query and if so cleanup the previous one. */
+/****************************************************************************/
+bool user_connect::CheckCleanup(void)
+{
+ if (thdp->query_id > last_query_id) {
+ PlugCleanup(g, true);
+ PlugSubSet(g, g->Sarea, g->Sarea_Size);
+ g->Xchk = NULL;
+ g->Createas = 0;
+ g->Alchecked = 0;
+ g->Mrr = 0;
+ last_query_id= thdp->query_id;
+
+ if (trace)
+ printf("=====> Begin new query %llu\n", last_query_id);
+
+ return true;
+ } // endif query_id
+
+ return false;
+} // end of CheckCleanup
+
diff --git a/storage/connect/valblk.cpp b/storage/connect/valblk.cpp
index 49766bbd447..893e8b1de81 100644
--- a/storage/connect/valblk.cpp
+++ b/storage/connect/valblk.cpp
@@ -43,6 +43,8 @@
#define CheckBlanks assert(!Blanks);
#define CheckParms(V, N) ChkIndx(N); ChkTyp(V);
+extern "C" int trace;
+
/***********************************************************************/
/* AllocValBlock: allocate a VALBLK according to type. */
/***********************************************************************/
@@ -51,10 +53,9 @@ PVBLK AllocValBlock(PGLOBAL g, void *mp, int type, int nval, int len,
{
PVBLK blkp;
-#ifdef DEBTRACE
- htrc("AVB: mp=%p type=%d nval=%d len=%d check=%u blank=%u\n",
- mp, type, nval, len, check, blank);
-#endif
+ if (trace)
+ htrc("AVB: mp=%p type=%d nval=%d len=%d check=%u blank=%u\n",
+ mp, type, nval, len, check, blank);
switch (type) {
case TYPE_STRING:
@@ -576,7 +577,7 @@ int TYPBLK<TYPE>::Find(PVAL vp)
template <class TYPE>
int TYPBLK<TYPE>::GetMaxLength(void)
{
- char buf[32];
+ char buf[64];
int i, n, m;
for (i = n = 0; i < Nval; i++) {
@@ -767,13 +768,13 @@ void CHRBLK::SetValue(char *sp, uint len, int n)
{
char *p = Chrp + n * Long;
-#if defined(_DEBUG) || defined(DEBTRACE)
+#if defined(_DEBUG)
if (Check && (signed)len > Long) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(SET_STR_TRUNC));
longjmp(g->jumper[g->jump_level], Type);
} // endif Check
-#endif
+#endif // _DEBUG
if (sp)
memcpy(p, sp, min((unsigned)Long, len));
@@ -846,13 +847,13 @@ void CHRBLK::SetMax(PVAL valp, int n)
/***********************************************************************/
void CHRBLK::SetValues(PVBLK pv, int k, int n)
{
-#if defined(_DEBUG) || defined(DEBTRACE)
+#if defined(_DEBUG)
if (Type != pv->GetType() || Long != ((CHRBLK*)pv)->Long) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BLKTYPLEN_MISM));
longjmp(g->jumper[g->jump_level], Type);
} // endif Type
-#endif
+#endif // _DEBUG
char *p = ((CHRBLK*)pv)->Chrp;
if (!k)
diff --git a/storage/connect/value.cpp b/storage/connect/value.cpp
index 5f89c1ac23c..654cf1d6907 100644
--- a/storage/connect/value.cpp
+++ b/storage/connect/value.cpp
@@ -1,2205 +1,2205 @@
-/************* Value C++ Functions Source Code File (.CPP) *************/
-/* Name: VALUE.CPP Version 2.4 */
-/* */
-/* (C) Copyright to the author Olivier BERTRAND 2001-2014 */
-/* */
-/* This file contains the VALUE and derived classes family functions. */
-/* These classes contain values of different types. They are used so */
-/* new object types can be defined and added to the processing simply */
-/* (hopefully) adding their specific functions in this file. */
-/* First family is VALUE that represent single typed objects. It is */
-/* used by columns (COLBLK), SELECT and FILTER (derived) objects. */
-/* Second family is VALBLK, representing simple suballocated arrays */
-/* of values treated sequentially by FIX, BIN and VCT tables and */
-/* columns, as well for min/max blocks as for VCT column blocks. */
-/* Q&A: why not using only one family ? Simple values are arrays that */
-/* have only one element and arrays could have functions for all kind */
-/* of processing. The answer is a-because historically it was simpler */
-/* to do that way, b-because of performance on single values, and c- */
-/* to avoid too complicated classes and unuseful duplication of many */
-/* functions used on one family only. The drawback is that for new */
-/* types of objects, we shall have more classes to update. */
-/* Currently the only implemented types are STRING, INT, SHORT, TINY, */
-/* DATE and LONGLONG. Recently we added some UNSIGNED types. */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant MariaDB header file. */
-/***********************************************************************/
-#include "my_global.h"
-#include "sql_class.h"
-#include "sql_time.h"
-
-#if defined(WIN32)
-//#include <windows.h>
-#else // !WIN32
-#include <string.h>
-#endif // !WIN32
-
-#include <math.h>
-
-#undef DOMAIN // Was defined in math.h
-
-/***********************************************************************/
-/* Include required application header files */
-/* global.h is header containing all global Plug declarations. */
-/* plgdbsem.h is header containing the DB applic. declarations. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "preparse.h" // For DATPAR
-//#include "value.h"
-#include "valblk.h"
-#define NO_FUNC // Already defined in ODBConn
-#include "plgcnx.h" // For DB types
-#include "osutil.h"
-
-/***********************************************************************/
-/* Check macro's. */
-/***********************************************************************/
-#if defined(_DEBUG)
-#define CheckType(V) if (Type != V->GetType()) { \
- PGLOBAL& g = Global; \
- strcpy(g->Message, MSG(VALTYPE_NOMATCH)); \
- longjmp(g->jumper[g->jump_level], Type); }
-#else
-#define CheckType(V)
-#endif
-
-#define FOURYEARS 126230400 // Four years in seconds (1 leap)
-
-/***********************************************************************/
-/* Static variables. */
-/***********************************************************************/
-
-extern "C" int trace;
-
-/***********************************************************************/
-/* Initialize the DTVAL static member. */
-/***********************************************************************/
-int DTVAL::Shift = 0;
-
-/***********************************************************************/
-/* Routines called externally. */
-/***********************************************************************/
-bool PlugEvalLike(PGLOBAL, LPCSTR, LPCSTR, bool);
-
-#if !defined(WIN32)
-extern "C" {
-PSZ strupr(PSZ s);
-PSZ strlwr(PSZ s);
-}
-#endif // !WIN32
-
-/***********************************************************************/
-/* Returns the bitmap representing the conditions that must not be */
-/* met when returning from TestValue for a given operator. */
-/* Bit one is EQ, bit 2 is LT, and bit 3 is GT. */
-/***********************************************************************/
-BYTE OpBmp(PGLOBAL g, OPVAL opc)
- {
- BYTE bt;
-
- switch (opc) {
- case OP_IN:
- case OP_EQ: bt = 0x06; break;
- case OP_NE: bt = 0x01; break;
- case OP_GT: bt = 0x03; break;
- case OP_GE: bt = 0x02; break;
- case OP_LT: bt = 0x05; break;
- case OP_LE: bt = 0x04; break;
- case OP_EXIST: bt = 0x00; break;
- default:
- sprintf(g->Message, MSG(BAD_FILTER_OP), opc);
- longjmp(g->jumper[g->jump_level], TYPE_ARRAY);
- } // endswitch opc
-
- return bt;
- } // end of OpBmp
-
-/***********************************************************************/
-/* Get a long long number from its character representation. */
-/* IN p: Pointer to the numeric string */
-/* IN n: The string length */
-/* IN maxval: The number max value */
-/* IN un: True if the number must be unsigned */
-/* OUT rc: Set to TRUE for out of range value */
-/* OUT minus: Set to true if the number is negative */
-/* Returned val: The resulting number */
-/***********************************************************************/
-ulonglong CharToNumber(char *p, int n, ulonglong maxval,
- bool un, bool *minus, bool *rc)
-{
- char *p2;
- uchar c;
- ulonglong val;
-
- if (minus) *minus = false;
- if (rc) *rc = false;
-
- // Eliminate leading blanks or 0
- for (p2 = p + n; p < p2 && (*p == ' ' || *p == '0'); p++) ;
-
- // Get an eventual sign character
- switch (*p) {
- case '-':
- if (un) {
- if (rc) *rc = true;
- return 0;
- } else {
- maxval++;
- if (minus) *minus = true;
- } // endif Unsigned
-
- case '+':
- p++;
- break;
- } // endswitch *p
-
- for (val = 0; p < p2 && (c = (uchar)(*p - '0')) < 10; p++)
- if (val > (maxval - c) / 10) {
- val = maxval;
- if (rc) *rc = true;
- break;
- } else
- val = val * 10 + c;
-
- return val;
-} // end of CharToNumber
-
-/***********************************************************************/
-/* GetTypeName: returns the PlugDB internal type name. */
-/***********************************************************************/
-PSZ GetTypeName(int type)
- {
- PSZ name;
-
- switch (type) {
- case TYPE_STRING: name = "CHAR"; break;
- case TYPE_SHORT: name = "SMALLINT"; break;
- case TYPE_INT: name = "INTEGER"; break;
- case TYPE_BIGINT: name = "BIGINT"; break;
- case TYPE_DATE: name = "DATE"; break;
- case TYPE_DOUBLE: name = "DOUBLE"; break;
- case TYPE_TINY: name = "TINY"; break;
- case TYPE_DECIM: name = "DECIMAL"; break;
- default: name = "UNKNOWN"; break;
- } // endswitch type
-
- return name;
- } // end of GetTypeName
-
-/***********************************************************************/
-/* GetTypeSize: returns the PlugDB internal type size. */
-/***********************************************************************/
-int GetTypeSize(int type, int len)
- {
- switch (type) {
- case TYPE_DECIM:
- case TYPE_STRING: len = len * sizeof(char); break;
- case TYPE_SHORT: len = sizeof(short); break;
- case TYPE_INT: len = sizeof(int); break;
- case TYPE_BIGINT: len = sizeof(longlong); break;
- case TYPE_DATE: len = sizeof(int); break;
- case TYPE_DOUBLE: len = sizeof(double); break;
- case TYPE_TINY: len = sizeof(char); break;
- default: len = 0;
- } // endswitch type
-
- return len;
- } // end of GetTypeSize
-
-/***********************************************************************/
-/* GetFormatType: returns the FORMAT character(s) according to type. */
-/***********************************************************************/
-char *GetFormatType(int type)
- {
- char *c = "X";
-
- switch (type) {
- case TYPE_STRING: c = "C"; break;
- case TYPE_SHORT: c = "S"; break;
- case TYPE_INT: c = "N"; break;
- case TYPE_BIGINT: c = "L"; break;
- case TYPE_DOUBLE: c = "F"; break;
- case TYPE_DATE: c = "D"; break;
- case TYPE_TINY: c = "T"; break;
- case TYPE_DECIM: c = "M"; break;
- } // endswitch type
-
- return c;
- } // end of GetFormatType
-
-/***********************************************************************/
-/* GetFormatType: returns the FORMAT type according to character. */
-/***********************************************************************/
-int GetFormatType(char c)
- {
- int type = TYPE_ERROR;
-
- switch (c) {
- case 'C': type = TYPE_STRING; break;
- case 'S': type = TYPE_SHORT; break;
- case 'N': type = TYPE_INT; break;
- case 'L': type = TYPE_BIGINT; break;
- case 'F': type = TYPE_DOUBLE; break;
- case 'D': type = TYPE_DATE; break;
- case 'T': type = TYPE_TINY; break;
- case 'M': type = TYPE_DECIM; break;
- } // endswitch type
-
- return type;
- } // end of GetFormatType
-
-/***********************************************************************/
-/* IsTypeChar: returns true for character type(s). */
-/***********************************************************************/
-bool IsTypeChar(int type)
- {
- switch (type) {
- case TYPE_STRING:
- case TYPE_DECIM:
- return true;
- } // endswitch type
-
- return false;
- } // end of IsTypeChar
-
-/***********************************************************************/
-/* IsTypeNum: returns true for numeric types. */
-/***********************************************************************/
-bool IsTypeNum(int type)
- {
- switch (type) {
- case TYPE_INT:
- case TYPE_BIGINT:
- case TYPE_DATE:
- case TYPE_DOUBLE:
- case TYPE_SHORT:
- case TYPE_NUM:
- case TYPE_TINY:
- case TYPE_DECIM:
- return true;
- } // endswitch type
-
- return false;
- } // end of IsTypeNum
-
-/***********************************************************************/
-/* GetFmt: returns the format to use with a typed value. */
-/***********************************************************************/
-const char *GetFmt(int type, bool un)
- {
- const char *fmt;
-
- switch (type) {
- case TYPE_DECIM:
- case TYPE_STRING: fmt = "%s"; break;
- case TYPE_SHORT: fmt = (un) ? "%hu" : "%hd"; break;
- case TYPE_BIGINT: fmt = (un) ? "%llu" : "%lld"; break;
- case TYPE_DOUBLE: fmt = "%.*lf"; break;
- default: fmt = (un) ? "%u" : "%d"; break;
- } // endswitch Type
-
- return fmt;
- } // end of GetFmt
-
-/***********************************************************************/
-/* ConvertType: what this function does is to determine the type to */
-/* which should be converted a value so no precision would be lost. */
-/* This can be a numeric type if num is true or non numeric if false. */
-/* Note: this is an ultra simplified version of this function that */
-/* should become more and more complex as new types are added. */
-/* Not evaluated types (TYPE_VOID or TYPE_UNDEF) return false from */
-/* IsType... functions so match does not prevent correct setting. */
-/***********************************************************************/
-int ConvertType(int target, int type, CONV kind, bool match)
- {
- switch (kind) {
- case CNV_CHAR:
- if (match && (!IsTypeChar(target) || !IsTypeChar(type)))
- return TYPE_ERROR;
-
- return TYPE_STRING;
- case CNV_NUM:
- if (match && (!IsTypeNum(target) || !IsTypeNum(type)))
- return TYPE_ERROR;
-
- return (target == TYPE_DOUBLE || type == TYPE_DOUBLE) ? TYPE_DOUBLE
- : (target == TYPE_DATE || type == TYPE_DATE) ? TYPE_DATE
- : (target == TYPE_BIGINT || type == TYPE_BIGINT) ? TYPE_BIGINT
- : (target == TYPE_INT || type == TYPE_INT) ? TYPE_INT
- : (target == TYPE_SHORT || type == TYPE_SHORT) ? TYPE_SHORT
- : TYPE_TINY;
- default:
- if (target == TYPE_ERROR || target == type)
- return type;
-
- if (match && ((IsTypeChar(target) && !IsTypeChar(type)) ||
- (IsTypeNum(target) && !IsTypeNum(type))))
- return TYPE_ERROR;
-
- return (target == TYPE_DOUBLE || type == TYPE_DOUBLE) ? TYPE_DOUBLE
- : (target == TYPE_DATE || type == TYPE_DATE) ? TYPE_DATE
- : (target == TYPE_BIGINT || type == TYPE_BIGINT) ? TYPE_BIGINT
- : (target == TYPE_INT || type == TYPE_INT) ? TYPE_INT
- : (target == TYPE_SHORT || type == TYPE_SHORT) ? TYPE_SHORT
- : (target == TYPE_STRING || type == TYPE_STRING) ? TYPE_STRING
- : (target == TYPE_TINY || type == TYPE_TINY) ? TYPE_TINY
- : TYPE_ERROR;
- } // endswitch kind
-
- } // end of ConvertType
-
-/***********************************************************************/
-/* AllocateConstant: allocates a constant Value. */
-/***********************************************************************/
-PVAL AllocateValue(PGLOBAL g, void *value, short type)
- {
- PVAL valp;
-
- if (trace)
- htrc("AllocateConstant: value=%p type=%hd\n", value, type);
-
- switch (type) {
- case TYPE_STRING:
- valp = new(g) TYPVAL<PSZ>((PSZ)value);
- break;
- case TYPE_SHORT:
- valp = new(g) TYPVAL<short>(*(short*)value, TYPE_SHORT);
- break;
- case TYPE_INT:
- valp = new(g) TYPVAL<int>(*(int*)value, TYPE_INT);
- break;
- case TYPE_BIGINT:
- valp = new(g) TYPVAL<longlong>(*(longlong*)value, TYPE_BIGINT);
- break;
- case TYPE_DOUBLE:
- valp = new(g) TYPVAL<double>(*(double *)value, TYPE_DOUBLE, 2);
- break;
- case TYPE_TINY:
- valp = new(g) TYPVAL<char>(*(char *)value, TYPE_TINY);
- break;
- default:
- sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
- return NULL;
- } // endswitch Type
-
- valp->SetGlobal(g);
- return valp;
- } // end of AllocateValue
-
-/***********************************************************************/
-/* Allocate a variable Value according to type, length and precision. */
-/***********************************************************************/
-PVAL AllocateValue(PGLOBAL g, int type, int len, int prec,
- bool uns, PSZ fmt)
- {
- PVAL valp;
-
- switch (type) {
- case TYPE_STRING:
- valp = new(g) TYPVAL<PSZ>(g, (PSZ)NULL, len, prec);
- break;
- case TYPE_DATE:
- valp = new(g) DTVAL(g, len, prec, fmt);
- break;
- case TYPE_INT:
- if (uns)
- valp = new(g) TYPVAL<uint>((uint)0, TYPE_INT, 0, true);
- else
- valp = new(g) TYPVAL<int>((int)0, TYPE_INT);
-
- break;
- case TYPE_BIGINT:
- if (uns)
- valp = new(g) TYPVAL<ulonglong>((ulonglong)0, TYPE_BIGINT, 0, true);
- else
- valp = new(g) TYPVAL<longlong>((longlong)0, TYPE_BIGINT);
-
- break;
- case TYPE_SHORT:
- if (uns)
- valp = new(g) TYPVAL<ushort>((ushort)0, TYPE_SHORT, 0, true);
- else
- valp = new(g) TYPVAL<short>((short)0, TYPE_SHORT);
-
- break;
- case TYPE_DOUBLE:
- valp = new(g) TYPVAL<double>(0.0, TYPE_DOUBLE, prec);
- break;
- case TYPE_TINY:
- if (uns)
- valp = new(g) TYPVAL<uchar>((uchar)0, TYPE_TINY, 0, true);
- else
- valp = new(g) TYPVAL<char>((char)0, TYPE_TINY);
-
- break;
- case TYPE_DECIM:
- valp = new(g) DECVAL(g, (PSZ)NULL, len, prec, uns);
- break;
- default:
- sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
- return NULL;
- } // endswitch type
-
- valp->SetGlobal(g);
- return valp;
- } // end of AllocateValue
-
-/***********************************************************************/
-/* Allocate a constant Value converted to newtype. */
-/* Can also be used to copy a Value eventually converted. */
-/***********************************************************************/
-PVAL AllocateValue(PGLOBAL g, PVAL valp, int newtype, int uns)
- {
- PSZ p, sp;
- bool un = (uns < 0) ? false : (uns > 0) ? true : valp->IsUnsigned();
-
- if (newtype == TYPE_VOID) // Means allocate a value of the same type
- newtype = valp->GetType();
-
- switch (newtype) {
- case TYPE_STRING:
- p = (PSZ)PlugSubAlloc(g, NULL, 1 + valp->GetValLen());
-
- if ((sp = valp->GetCharString(p)) != p)
- strcpy (p, sp);
-
- valp = new(g) TYPVAL<PSZ>(g, p, valp->GetValLen(), valp->GetValPrec());
- break;
- case TYPE_SHORT:
- if (un)
- valp = new(g) TYPVAL<ushort>(valp->GetUShortValue(),
- TYPE_SHORT, 0, true);
- else
- valp = new(g) TYPVAL<short>(valp->GetShortValue(), TYPE_SHORT);
-
- break;
- case TYPE_INT:
- if (un)
- valp = new(g) TYPVAL<uint>(valp->GetUIntValue(), TYPE_INT, 0, true);
- else
- valp = new(g) TYPVAL<int>(valp->GetIntValue(), TYPE_INT);
-
- break;
- case TYPE_BIGINT:
- if (un)
- valp = new(g) TYPVAL<ulonglong>(valp->GetUBigintValue(),
- TYPE_BIGINT, 0, true);
- else
- valp = new(g) TYPVAL<longlong>(valp->GetBigintValue(), TYPE_BIGINT);
-
- break;
- case TYPE_DATE:
- valp = new(g) DTVAL(g, valp->GetIntValue());
- break;
- case TYPE_DOUBLE:
- valp = new(g) TYPVAL<double>(valp->GetFloatValue(), TYPE_DOUBLE,
- valp->GetValPrec());
- break;
- case TYPE_TINY:
- if (un)
- valp = new(g) TYPVAL<uchar>(valp->GetUTinyValue(),
- TYPE_TINY, 0, true);
- else
- valp = new(g) TYPVAL<char>(valp->GetTinyValue(), TYPE_TINY);
-
- break;
- default:
- sprintf(g->Message, MSG(BAD_VALUE_TYPE), newtype);
- return NULL;
- } // endswitch type
-
- valp->SetGlobal(g);
- return valp;
- } // end of AllocateValue
-
-/* -------------------------- Class VALUE ---------------------------- */
-
-/***********************************************************************/
-/* Class VALUE protected constructor. */
-/***********************************************************************/
-VALUE::VALUE(int type, bool un) : Type(type)
- {
- Null = false;
- Nullable = false;
- Unsigned = un;
- Clen = 0;
- Prec = 0;
- Fmt = GetFmt(Type, Unsigned);
- Xfmt = GetXfmt();
- } // end of VALUE constructor
-
-/***********************************************************************/
-/* VALUE GetXfmt: returns the extended format to use with typed value. */
-/***********************************************************************/
-const char *VALUE::GetXfmt(void)
- {
- const char *fmt;
-
- switch (Type) {
- case TYPE_DECIM:
- case TYPE_STRING: fmt = "%*s"; break;
- case TYPE_SHORT: fmt = (Unsigned) ? "%*hu" : "%*hd"; break;
- case TYPE_BIGINT: fmt = (Unsigned) ? "%*llu" : "%*lld"; break;
- case TYPE_DOUBLE: fmt = "%*.*lf"; break;
- default: fmt = (Unsigned) ? "%*u" : "%*d"; break;
- } // endswitch Type
-
- return fmt;
- } // end of GetFmt
-
-/***********************************************************************/
-/* Returns a BYTE indicating the comparison between two values. */
-/* Bit 1 indicates equality, Bit 2 less than, and Bit3 greater than. */
-/* More than 1 bit can be set only in the case of TYPE_LIST. */
-/***********************************************************************/
-BYTE VALUE::TestValue(PVAL vp)
- {
- int n = CompareValue(vp);
-
- return (n > 0) ? 0x04 : (n < 0) ? 0x02 : 0x01;
- } // end of TestValue
-
-/* -------------------------- Class TYPVAL ---------------------------- */
-
-/***********************************************************************/
-/* TYPVAL public constructor from a constant typed value. */
-/***********************************************************************/
-template <class TYPE>
-TYPVAL<TYPE>::TYPVAL(TYPE n, int type, int prec, bool un)
- : VALUE(type, un)
- {
- Tval = n;
- Clen = sizeof(TYPE);
- Prec = prec;
- } // end of TYPVAL constructor
-
-/***********************************************************************/
-/* Return unsigned max value for the type. */
-/***********************************************************************/
-template <class TYPE>
-ulonglong TYPVAL<TYPE>::MaxVal(void) {DBUG_ASSERT(false); return 0;}
-
-template <>
-ulonglong TYPVAL<short>::MaxVal(void) {return INT_MAX16;}
-
-template <>
-ulonglong TYPVAL<ushort>::MaxVal(void) {return UINT_MAX16;}
-
-template <>
-ulonglong TYPVAL<int>::MaxVal(void) {return INT_MAX32;}
-
-template <>
-ulonglong TYPVAL<uint>::MaxVal(void) {return UINT_MAX32;}
-
-template <>
-ulonglong TYPVAL<char>::MaxVal(void) {return INT_MAX8;}
-
-template <>
-ulonglong TYPVAL<uchar>::MaxVal(void) {return UINT_MAX8;}
-
-template <>
-ulonglong TYPVAL<longlong>::MaxVal(void) {return INT_MAX64;}
-
-template <>
-ulonglong TYPVAL<ulonglong>::MaxVal(void) {return ULONGLONG_MAX;}
-
-/***********************************************************************/
-/* TYPVAL GetValLen: returns the print length of the typed object. */
-/***********************************************************************/
-template <class TYPE>
-int TYPVAL<TYPE>::GetValLen(void)
- {
- char c[32];
-
- return sprintf(c, Fmt, Tval);
- } // end of GetValLen
-
-template <>
-int TYPVAL<double>::GetValLen(void)
- {
- char c[32];
-
- return sprintf(c, Fmt, Prec, Tval);
- } // end of GetValLen
-
-/***********************************************************************/
-/* TYPVAL SetValue: copy the value of another Value object. */
-/* This function allows conversion if chktype is false. */
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::SetValue_pval(PVAL valp, bool chktype)
- {
- if (chktype && Type != valp->GetType())
- return true;
-
- if (!(Null = valp->IsNull() && Nullable))
- Tval = GetTypedValue(valp);
- else
- Reset();
-
- return false;
- } // end of SetValue
-
-template <>
-short TYPVAL<short>::GetTypedValue(PVAL valp)
- {return valp->GetShortValue();}
-
-template <>
-ushort TYPVAL<ushort>::GetTypedValue(PVAL valp)
- {return valp->GetUShortValue();}
-
-template <>
-int TYPVAL<int>::GetTypedValue(PVAL valp)
- {return valp->GetIntValue();}
-
-template <>
-uint TYPVAL<uint>::GetTypedValue(PVAL valp)
- {return valp->GetUIntValue();}
-
-template <>
-longlong TYPVAL<longlong>::GetTypedValue(PVAL valp)
- {return valp->GetBigintValue();}
-
-template <>
-ulonglong TYPVAL<ulonglong>::GetTypedValue(PVAL valp)
- {return valp->GetUBigintValue();}
-
-template <>
-double TYPVAL<double>::GetTypedValue(PVAL valp)
- {return valp->GetFloatValue();}
-
-template <>
-char TYPVAL<char>::GetTypedValue(PVAL valp)
- {return valp->GetTinyValue();}
-
-template <>
-uchar TYPVAL<uchar>::GetTypedValue(PVAL valp)
- {return valp->GetUTinyValue();}
-
-/***********************************************************************/
-/* TYPVAL SetValue: convert chars extracted from a line to TYPE value.*/
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::SetValue_char(char *p, int n)
- {
- bool rc, minus;
- ulonglong maxval = MaxVal();
- ulonglong val = CharToNumber(p, n, maxval, Unsigned, &minus, &rc);
-
- if (minus && val < maxval)
- Tval = (TYPE)(-(signed)val);
- else
- Tval = (TYPE)val;
-
- if (trace > 1) {
- char buf[64];
- htrc(strcat(strcat(strcpy(buf, " setting %s to: "), Fmt), "\n"),
- GetTypeName(Type), Tval);
- } // endif trace
-
- Null = false;
- return rc;
- } // end of SetValue
-
-template <>
-bool TYPVAL<double>::SetValue_char(char *p, int n)
- {
- if (p) {
- char buf[32];
-
- for (; n > 0 && *p == ' '; p++)
- n--;
-
- memcpy(buf, p, min(n, 31));
- buf[n] = '\0';
- Tval = atof(buf);
-
- if (trace > 1)
- htrc(" setting double: '%s' -> %lf\n", buf, Tval);
-
- Null = false;
- } else {
- Reset();
- Null = Nullable;
- } // endif p
-
- return false;
- } // end of SetValue
-
-/***********************************************************************/
-/* TYPVAL SetValue: fill a typed value from a string. */
-/***********************************************************************/
-template <class TYPE>
-void TYPVAL<TYPE>::SetValue_psz(PSZ s)
- {
- if (s) {
- SetValue_char(s, (int)strlen(s));
- Null = false;
- } else {
- Reset();
- Null = Nullable;
- } // endif p
-
- } // end of SetValue
-
-/***********************************************************************/
-/* TYPVAL SetValue: set value with a TYPE extracted from a block. */
-/***********************************************************************/
-template <class TYPE>
-void TYPVAL<TYPE>::SetValue_pvblk(PVBLK blk, int n)
- {
- Tval = GetTypedValue(blk, n);
- Null = false;
- } // end of SetValue
-
-template <>
-int TYPVAL<int>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetIntValue(n);}
-
-template <>
-uint TYPVAL<uint>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetUIntValue(n);}
-
-template <>
-short TYPVAL<short>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetShortValue(n);}
-
-template <>
-ushort TYPVAL<ushort>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetUShortValue(n);}
-
-template <>
-longlong TYPVAL<longlong>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetBigintValue(n);}
-
-template <>
-ulonglong TYPVAL<ulonglong>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetUBigintValue(n);}
-
-template <>
-double TYPVAL<double>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetFloatValue(n);}
-
-template <>
-char TYPVAL<char>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetTinyValue(n);}
-
-template <>
-uchar TYPVAL<uchar>::GetTypedValue(PVBLK blk, int n)
- {return blk->GetUTinyValue(n);}
-
-/***********************************************************************/
-/* TYPVAL SetBinValue: with bytes extracted from a line. */
-/***********************************************************************/
-template <class TYPE>
-void TYPVAL<TYPE>::SetBinValue(void *p)
- {
- Tval = *(TYPE *)p;
- Null = false;
- } // end of SetBinValue
-
-/***********************************************************************/
-/* GetBinValue: fill a buffer with the internal binary value. */
-/* This function checks whether the buffer length is enough and */
-/* returns true if not. Actual filling occurs only if go is true. */
-/* Currently used by WriteColumn of binary files. */
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::GetBinValue(void *buf, int buflen, bool go)
- {
- // Test on length was removed here until a variable in column give the
- // real field length. For BIN files the field length logically cannot
- // be different from the variable length because no conversion is done.
- // Therefore this test is useless anyway.
-//#if defined(_DEBUG)
-// if (sizeof(TYPE) > buflen)
-// return true;
-//#endif
-
- if (go)
- *(TYPE *)buf = Tval;
-
- Null = false;
- return false;
- } // end of GetBinValue
-
-/***********************************************************************/
-/* TYPVAL ShowValue: get string representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::ShowValue(char *buf, int len)
- {
- sprintf(buf, Xfmt, len, Tval);
- return buf;
- } // end of ShowValue
-
-template <>
-char *TYPVAL<double>::ShowValue(char *buf, int len)
- {
- // TODO: use snprintf to avoid possible overflow
- sprintf(buf, Xfmt, len, Prec, Tval);
- return buf;
- } // end of ShowValue
-
-/***********************************************************************/
-/* TYPVAL GetCharString: get string representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetCharString(char *p)
- {
- sprintf(p, Fmt, Tval);
- return p;
- } // end of GetCharString
-
-template <>
-char *TYPVAL<double>::GetCharString(char *p)
- {
- sprintf(p, Fmt, Prec, Tval);
- return p;
- } // end of GetCharString
-
-#if 0
-/***********************************************************************/
-/* TYPVAL GetShortString: get short representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetShortString(char *p, int n)
- {
- sprintf(p, "%*hd", n, (short)Tval);
- return p;
- } // end of GetShortString
-
-/***********************************************************************/
-/* TYPVAL GetIntString: get int representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetIntString(char *p, int n)
- {
- sprintf(p, "%*d", n, (int)Tval);
- return p;
- } // end of GetIntString
-
-/***********************************************************************/
-/* TYPVAL GetBigintString: get big int representation of a TYPE value.*/
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetBigintString(char *p, int n)
- {
- sprintf(p, "%*lld", n, (longlong)Tval);
- return p;
- } // end of GetBigintString
-
-/***********************************************************************/
-/* TYPVAL GetFloatString: get double representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetFloatString(char *p, int n, int prec)
- {
- sprintf(p, "%*.*lf", n, (prec < 0) ? 2 : prec, (double)Tval);
- return p;
- } // end of GetFloatString
-
-/***********************************************************************/
-/* TYPVAL GetTinyString: get char representation of a typed value. */
-/***********************************************************************/
-template <class TYPE>
-char *TYPVAL<TYPE>::GetTinyString(char *p, int n)
- {
- sprintf(p, "%*d", n, (int)(char)Tval);
- return p;
- } // end of GetIntString
-#endif // 0
-
-/***********************************************************************/
-/* TYPVAL compare value with another Value. */
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::IsEqual(PVAL vp, bool chktype)
- {
- if (this == vp)
- return true;
- else if (chktype && Type != vp->GetType())
- return false;
- else if (chktype && Unsigned != vp->IsUnsigned())
- return false;
- else if (Null || vp->IsNull())
- return false;
- else
- return (Tval == GetTypedValue(vp));
-
- } // end of IsEqual
-
-/***********************************************************************/
-/* Compare values and returns 1, 0 or -1 according to comparison. */
-/* This function is used for evaluation of numeric filters. */
-/***********************************************************************/
-template <class TYPE>
-int TYPVAL<TYPE>::CompareValue(PVAL vp)
- {
-//assert(vp->GetType() == Type);
-
- // Process filtering on numeric values.
- TYPE n = GetTypedValue(vp);
-
-//if (trace)
-// htrc(" Comparing: val=%d,%d\n", Tval, n);
-
- return (Tval > n) ? 1 : (Tval < n) ? (-1) : 0;
- } // end of CompareValue
-
-/***********************************************************************/
-/* FormatValue: This function set vp (a STRING value) to the string */
-/* constructed from its own value formated using the fmt format. */
-/* This function assumes that the format matches the value type. */
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::FormatValue(PVAL vp, char *fmt)
- {
- char *buf = (char*)vp->GetTo_Val(); // Should be big enough
- int n = sprintf(buf, fmt, Tval);
-
- return (n > vp->GetValLen());
- } // end of FormatValue
-
-/***********************************************************************/
-/* TYPVAL SetFormat function (used to set SELECT output format). */
-/***********************************************************************/
-template <class TYPE>
-bool TYPVAL<TYPE>::SetConstFormat(PGLOBAL g, FORMAT& fmt)
- {
- char c[32];
-
- fmt.Type[0] = *GetFormatType(Type);
- fmt.Length = sprintf(c, Fmt, Tval);
- fmt.Prec = Prec;
- return false;
- } // end of SetConstFormat
-
-/***********************************************************************/
-/* Make file output of a typed object. */
-/***********************************************************************/
-template <class TYPE>
-void TYPVAL<TYPE>::Print(PGLOBAL g, FILE *f, uint n)
- {
- char m[64], buf[12];
-
- memset(m, ' ', n); /* Make margin string */
- m[n] = '\0';
-
- if (Null)
- fprintf(f, "%s<null>\n", m);
- else
- fprintf(f, strcat(strcat(strcpy(buf, "%s"), Fmt), "\n"), m, Tval);
-
- } /* end of Print */
-
-/***********************************************************************/
-/* Make string output of a int object. */
-/***********************************************************************/
-template <class TYPE>
-void TYPVAL<TYPE>::Print(PGLOBAL g, char *ps, uint z)
- {
- if (Null)
- strcpy(ps, "<null>");
- else
- sprintf(ps, Fmt, Tval);
-
- } /* end of Print */
-
-/* -------------------------- Class STRING --------------------------- */
-
-/***********************************************************************/
-/* STRING public constructor from a constant string. */
-/***********************************************************************/
-TYPVAL<PSZ>::TYPVAL(PSZ s) : VALUE(TYPE_STRING)
- {
- Strp = s;
- Len = strlen(s);
- Clen = Len;
- Ci = false;
- } // end of STRING constructor
-
-/***********************************************************************/
-/* STRING public constructor from char. */
-/***********************************************************************/
-TYPVAL<PSZ>::TYPVAL(PGLOBAL g, PSZ s, int n, int c)
- : VALUE(TYPE_STRING)
- {
- Len = (g) ? n : strlen(s);
-
- if (!s) {
- if (g) {
- Strp = (char *)PlugSubAlloc(g, NULL, Len + 1);
- Strp[Len] = '\0';
- } else
- assert(false);
-
- } else
- Strp = s;
-
- Clen = Len;
- Ci = (c != 0);
- } // end of STRING constructor
-
-/***********************************************************************/
-/* Get the tiny value represented by the Strp string. */
-/***********************************************************************/
-char TYPVAL<PSZ>::GetTinyValue(void)
- {
- bool m;
- ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX8, false, &m);
-
- return (m && val < INT_MAX8) ? (char)(-(signed)val) : (char)val;
- } // end of GetTinyValue
-
-/***********************************************************************/
-/* Get the unsigned tiny value represented by the Strp string. */
-/***********************************************************************/
-uchar TYPVAL<PSZ>::GetUTinyValue(void)
- {
- return (uchar)CharToNumber(Strp, strlen(Strp), UINT_MAX8, true);
- } // end of GetUTinyValue
-
-/***********************************************************************/
-/* Get the short value represented by the Strp string. */
-/***********************************************************************/
-short TYPVAL<PSZ>::GetShortValue(void)
- {
- bool m;
- ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX16, false, &m);
-
- return (m && val < INT_MAX16) ? (short)(-(signed)val) : (short)val;
- } // end of GetShortValue
-
-/***********************************************************************/
-/* Get the unsigned short value represented by the Strp string. */
-/***********************************************************************/
-ushort TYPVAL<PSZ>::GetUShortValue(void)
- {
- return (ushort)CharToNumber(Strp, strlen(Strp), UINT_MAX16, true);
- } // end of GetUshortValue
-
-/***********************************************************************/
-/* Get the integer value represented by the Strp string. */
-/***********************************************************************/
-int TYPVAL<PSZ>::GetIntValue(void)
- {
- bool m;
- ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX32, false, &m);
-
- return (m && val < INT_MAX32) ? (int)(-(signed)val) : (int)val;
- } // end of GetIntValue
-
-/***********************************************************************/
-/* Get the unsigned integer value represented by the Strp string. */
-/***********************************************************************/
-uint TYPVAL<PSZ>::GetUIntValue(void)
- {
- return (uint)CharToNumber(Strp, strlen(Strp), UINT_MAX32, true);
- } // end of GetUintValue
-
-/***********************************************************************/
-/* Get the big integer value represented by the Strp string. */
-/***********************************************************************/
-longlong TYPVAL<PSZ>::GetBigintValue(void)
- {
- bool m;
- ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX64, false, &m);
-
- return (m && val < INT_MAX64) ? (-(signed)val) : (longlong)val;
- } // end of GetBigintValue
-
-/***********************************************************************/
-/* Get the unsigned big integer value represented by the Strp string. */
-/***********************************************************************/
-ulonglong TYPVAL<PSZ>::GetUBigintValue(void)
- {
- return CharToNumber(Strp, strlen(Strp), ULONGLONG_MAX, true);
- } // end of GetUBigintValue
-
-/***********************************************************************/
-/* STRING SetValue: copy the value of another Value object. */
-/***********************************************************************/
-bool TYPVAL<PSZ>::SetValue_pval(PVAL valp, bool chktype)
- {
- if (chktype && (valp->GetType() != Type || valp->GetSize() > Len))
- return true;
-
- char buf[32];
-
- if (!(Null = valp->IsNull() && Nullable))
- strncpy(Strp, valp->GetCharString(buf), Len);
- else
- Reset();
-
- return false;
- } // end of SetValue_pval
-
-/***********************************************************************/
-/* STRING SetValue: fill string with chars extracted from a line. */
-/***********************************************************************/
-bool TYPVAL<PSZ>::SetValue_char(char *p, int n)
- {
- bool rc;
-
- if (p) {
- rc = n > Len;
-
- if ((n = min(n, Len))) {
- strncpy(Strp, p, n);
-
-// for (p = Strp + n - 1; p >= Strp && (*p == ' ' || *p == '\0'); p--) ;
- for (p = Strp + n - 1; p >= Strp; p--)
- if (*p && *p != ' ')
- break;
-
- *(++p) = '\0';
-
- if (trace > 1)
- htrc(" Setting string to: '%s'\n", Strp);
-
- } else
- Reset();
-
- Null = false;
- } else {
- rc = false;
- Reset();
- Null = Nullable;
- } // endif p
-
- return rc;
- } // end of SetValue_char
-
-/***********************************************************************/
-/* STRING SetValue: fill string with another string. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue_psz(PSZ s)
- {
- if (s) {
- strncpy(Strp, s, Len);
- Null = false;
- } else {
- Reset();
- Null = Nullable;
- } // endif s
-
- } // end of SetValue_psz
-
-/***********************************************************************/
-/* STRING SetValue: fill string with a string extracted from a block. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue_pvblk(PVBLK blk, int n)
- {
- // STRBLK's can return a NULL pointer
- PSZ vp = blk->GetCharString(Strp, n);
-
- if (vp != Strp)
- SetValue_psz(vp);
-
- } // end of SetValue_pvblk
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of an integer. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(int n)
- {
- char buf[16];
- PGLOBAL& g = Global;
- int k = sprintf(buf, "%d", n);
-
- if (k > Len) {
- sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
- longjmp(g->jumper[g->jump_level], 138);
- } else
- SetValue_psz(buf);
-
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of an uint. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(uint n)
- {
- char buf[16];
- PGLOBAL& g = Global;
- int k = sprintf(buf, "%u", n);
-
- if (k > Len) {
- sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
- longjmp(g->jumper[g->jump_level], 138);
- } else
- SetValue_psz(buf);
-
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a short int. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(short i)
- {
- SetValue((int)i);
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a ushort int. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(ushort i)
- {
- SetValue((uint)i);
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a big integer.*/
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(longlong n)
- {
- char buf[24];
- PGLOBAL& g = Global;
- int k = sprintf(buf, "%lld", n);
-
- if (k > Len) {
- sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
- longjmp(g->jumper[g->jump_level], 138);
- } else
- SetValue_psz(buf);
-
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a big integer.*/
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(ulonglong n)
- {
- char buf[24];
- PGLOBAL& g = Global;
- int k = sprintf(buf, "%llu", n);
-
- if (k > Len) {
- sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
- longjmp(g->jumper[g->jump_level], 138);
- } else
- SetValue_psz(buf);
-
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a double. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(double f)
- {
- char *p, buf[32];
- PGLOBAL& g = Global;
- int k = sprintf(buf, "%lf", f);
-
- for (p = buf + k - 1; p >= buf; p--)
- if (*p == '0') {
- *p = 0;
- k--;
- } else
- break;
-
- if (k > Len) {
- sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
- longjmp(g->jumper[g->jump_level], 138);
- } else
- SetValue_psz(buf);
-
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a tiny int. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(char c)
- {
- SetValue((int)c);
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetValue: get the character representation of a tiny int. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetValue(uchar c)
- {
- SetValue((uint)c);
- Null = false;
- } // end of SetValue
-
-/***********************************************************************/
-/* STRING SetBinValue: fill string with chars extracted from a line. */
-/***********************************************************************/
-void TYPVAL<PSZ>::SetBinValue(void *p)
- {
- SetValue_char((char *)p, Len);
- } // end of SetBinValue
-
-/***********************************************************************/
-/* GetBinValue: fill a buffer with the internal binary value. */
-/* This function checks whether the buffer length is enough and */
-/* returns true if not. Actual filling occurs only if go is true. */
-/* Currently used by WriteColumn of binary files. */
-/***********************************************************************/
-bool TYPVAL<PSZ>::GetBinValue(void *buf, int buflen, bool go)
- {
- int len = (Null) ? 0 : strlen(Strp);
-
- if (len > buflen)
- return true;
- else if (go) {
- memset(buf, ' ', buflen);
- memcpy(buf, Strp, len);
- } // endif go
-
- return false;
- } // end of GetBinValue
-
-/***********************************************************************/
-/* STRING ShowValue: get string representation of a char value. */
-/***********************************************************************/
-char *TYPVAL<PSZ>::ShowValue(char *buf, int len)
- {
- return Strp;
- } // end of ShowValue
-
-/***********************************************************************/
-/* STRING GetCharString: get string representation of a char value. */
-/***********************************************************************/
-char *TYPVAL<PSZ>::GetCharString(char *p)
- {
- return Strp;
- } // end of GetCharString
-
-/***********************************************************************/
-/* STRING compare value with another Value. */
-/***********************************************************************/
-bool TYPVAL<PSZ>::IsEqual(PVAL vp, bool chktype)
- {
- if (this == vp)
- return true;
- else if (chktype && Type != vp->GetType())
- return false;
- else if (Null || vp->IsNull())
- return false;
-
- char buf[32];
-
- if (Ci || vp->IsCi())
- return !stricmp(Strp, vp->GetCharString(buf));
- else // (!Ci)
- return !strcmp(Strp, vp->GetCharString(buf));
-
- } // end of IsEqual
-
-/***********************************************************************/
-/* Compare values and returns 1, 0 or -1 according to comparison. */
-/* This function is used for evaluation of numeric filters. */
-/***********************************************************************/
-int TYPVAL<PSZ>::CompareValue(PVAL vp)
- {
- int n;
-//assert(vp->GetType() == Type);
-
- if (trace)
- htrc(" Comparing: val='%s','%s'\n", Strp, vp->GetCharValue());
-
- // Process filtering on character strings.
- if (Ci || vp->IsCi())
- n = stricmp(Strp, vp->GetCharValue());
- else
- n = strcmp(Strp, vp->GetCharValue());
-
-#if defined(WIN32)
- if (n == _NLSCMPERROR)
- return n; // Here we should raise an error
-#endif // WIN32
-
- return (n > 0) ? 1 : (n < 0) ? -1 : 0;
- } // end of CompareValue
-
-/***********************************************************************/
-/* FormatValue: This function set vp (a STRING value) to the string */
-/* constructed from its own value formated using the fmt format. */
-/* This function assumes that the format matches the value type. */
-/***********************************************************************/
-bool TYPVAL<PSZ>::FormatValue(PVAL vp, char *fmt)
- {
- char *buf = (char*)vp->GetTo_Val(); // Should be big enough
- int n = sprintf(buf, fmt, Strp);
-
- return (n > vp->GetValLen());
- } // end of FormatValue
-
-/***********************************************************************/
-/* STRING SetFormat function (used to set SELECT output format). */
-/***********************************************************************/
-bool TYPVAL<PSZ>::SetConstFormat(PGLOBAL g, FORMAT& fmt)
- {
- fmt.Type[0] = 'C';
- fmt.Length = Len;
- fmt.Prec = 0;
- return false;
- } // end of SetConstFormat
-
-/* -------------------------- Class DECIMAL -------------------------- */
-
-/***********************************************************************/
-/* DECIMAL public constructor from a constant string. */
-/***********************************************************************/
-DECVAL::DECVAL(PSZ s) : TYPVAL<PSZ>(s)
- {
- if (s) {
- char *p = strchr(Strp, '.');
-
- Prec = (p) ? Len - (p - Strp) : 0;
- } // endif s
-
- Type = TYPE_DECIM;
- } // end of DECVAL constructor
-
-/***********************************************************************/
-/* DECIMAL public constructor from char. */
-/***********************************************************************/
-DECVAL::DECVAL(PGLOBAL g, PSZ s, int n, int prec, bool uns)
- : TYPVAL<PSZ>(g, s, n + (prec ? 1 : 0) + (uns ? 0 : 1), 0)
- {
- Prec = prec;
- Unsigned = uns;
- Type = TYPE_DECIM;
- } // end of DECVAL constructor
-
-/***********************************************************************/
-/* DECIMAL: Check whether the numerica value is equal to 0. */
-/***********************************************************************/
-bool DECVAL::IsZero(void)
- {
- for (int i = 0; Strp[i]; i++)
- if (!strchr("0 +-.", Strp[i]))
- return false;
-
- return true;
- } // end of IsZero
-
-/***********************************************************************/
-/* DECIMAL: Reset value to zero. */
-/***********************************************************************/
-void DECVAL::Reset(void)
-{
- int i = 0;
-
- Strp[i++] = '0';
-
- if (Prec) {
- Strp[i++] = '.';
-
- do {
- Strp[i++] = '0';
- } while (i < Prec + 2);
-
- } // endif Prec
-
- Strp[i] = 0;
-} // end of Reset
-
-/***********************************************************************/
-/* DECIMAL ShowValue: get string representation right justified. */
-/***********************************************************************/
-char *DECVAL::ShowValue(char *buf, int len)
- {
- sprintf(buf, Xfmt, len, Strp);
- return buf;
- } // end of ShowValue
-
-/***********************************************************************/
-/* GetBinValue: fill a buffer with the internal binary value. */
-/* This function checks whether the buffer length is enough and */
-/* returns true if not. Actual filling occurs only if go is true. */
-/* Currently used by WriteColumn of binary files. */
-/***********************************************************************/
-bool DECVAL::GetBinValue(void *buf, int buflen, bool go)
- {
- int len = (Null) ? 0 : strlen(Strp);
-
- if (len > buflen)
- return true;
- else if (go) {
- memset(buf, ' ', buflen - len);
- memcpy((char*)buf + buflen - len, Strp, len);
- } // endif go
-
- return false;
- } // end of GetBinValue
-
-#if 0
-/***********************************************************************/
-/* DECIMAL SetValue: copy the value of another Value object. */
-/***********************************************************************/
-bool DECVAL::SetValue_pval(PVAL valp, bool chktype)
- {
- if (chktype && (valp->GetType() != Type || valp->GetSize() > Len))
- return true;
-
- char buf[32];
-
- if (!(Null = valp->IsNull() && Nullable))
- strncpy(Strp, valp->GetCharString(buf), Len);
- else
- Reset();
-
- return false;
- } // end of SetValue_pval
-
-/***********************************************************************/
-/* DECIMAL SetValue: fill string with chars extracted from a line. */
-/***********************************************************************/
-bool DECVAL::SetValue_char(char *p, int n)
- {
- bool rc;
-
- if (p) {
- rc = n > Len;
-
- if ((n = min(n, Len))) {
- strncpy(Strp, p, n);
-
-// for (p = Strp + n - 1; p >= Strp && (*p == ' ' || *p == '\0'); p--) ;
- for (p = Strp + n - 1; p >= Strp; p--)
- if (*p && *p != ' ')
- break;
-
- *(++p) = '\0';
-
- if (trace > 1)
- htrc(" Setting string to: '%s'\n", Strp);
-
- } else
- Reset();
-
- Null = false;
- } else {
- rc = false;
- Reset();
- Null = Nullable;
- } // endif p
-
- return rc;
- } // end of SetValue_char
-
-/***********************************************************************/
-/* DECIMAL SetValue: fill string with another string. */
-/***********************************************************************/
-void DECVAL::SetValue_psz(PSZ s)
- {
- if (s) {
- strncpy(Strp, s, Len);
- Null = false;
- } else {
- Reset();
- Null = Nullable;
- } // endif s
-
- } // end of SetValue_psz
-
-/***********************************************************************/
-/* DECIMAL SetValue: fill string with a string extracted from a block.*/
-/***********************************************************************/
-void DECVAL::SetValue_pvblk(PVBLK blk, int n)
- {
- // STRBLK's can return a NULL pointer
- SetValue_psz(blk->GetCharValue(n));
- } // end of SetValue_pvblk
-
-/***********************************************************************/
-/* DECIMAL SetBinValue: fill string with chars extracted from a line. */
-/***********************************************************************/
-void DECVAL::SetBinValue(void *p)
- {
- SetValue_char((char *)p, Len);
- } // end of SetBinValue
-
-/***********************************************************************/
-/* DECIMAL GetCharString: get string representation of a char value. */
-/***********************************************************************/
-char *DECVAL::GetCharString(char *p)
- {
- return Strp;
- } // end of GetCharString
-#endif // 0
-
-/***********************************************************************/
-/* DECIMAL compare value with another Value. */
-/***********************************************************************/
-bool DECVAL::IsEqual(PVAL vp, bool chktype)
- {
- if (this == vp)
- return true;
- else if (chktype && Type != vp->GetType())
- return false;
- else if (Null || vp->IsNull())
- return false;
-
- char buf[32];
-
- return !strcmp(Strp, vp->GetCharString(buf));
- } // end of IsEqual
-
-/***********************************************************************/
-/* Compare values and returns 1, 0 or -1 according to comparison. */
-/* This function is used for evaluation of numeric filters. */
-/***********************************************************************/
-int DECVAL::CompareValue(PVAL vp)
- {
-//assert(vp->GetType() == Type);
-
- // Process filtering on numeric values.
- double f = atof(Strp), n = vp->GetFloatValue();
-
-//if (trace)
-// htrc(" Comparing: val=%d,%d\n", f, n);
-
- return (f > n) ? 1 : (f < n) ? (-1) : 0;
- } // end of CompareValue
-
-#if 0
-/***********************************************************************/
-/* FormatValue: This function set vp (a STRING value) to the string */
-/* constructed from its own value formated using the fmt format. */
-/* This function assumes that the format matches the value type. */
-/***********************************************************************/
-bool DECVAL::FormatValue(PVAL vp, char *fmt)
- {
- char *buf = (char*)vp->GetTo_Val(); // Should be big enough
- int n = sprintf(buf, fmt, Strp);
-
- return (n > vp->GetValLen());
- } // end of FormatValue
-
-/***********************************************************************/
-/* DECIMAL SetFormat function (used to set SELECT output format). */
-/***********************************************************************/
-bool DECVAL::SetConstFormat(PGLOBAL g, FORMAT& fmt)
- {
- fmt.Type[0] = 'C';
- fmt.Length = Len;
- fmt.Prec = 0;
- return false;
- } // end of SetConstFormat
-#endif // 0
-
-/* -------------------------- Class DTVAL ---------------------------- */
-
-/***********************************************************************/
-/* DTVAL public constructor for new void values. */
-/***********************************************************************/
-DTVAL::DTVAL(PGLOBAL g, int n, int prec, PSZ fmt)
- : TYPVAL<int>((int)0, TYPE_DATE)
- {
- if (!fmt) {
- Pdtp = NULL;
- Sdate = NULL;
- DefYear = 0;
- Len = n;
- } else
- SetFormat(g, fmt, n, prec);
-
-//Type = TYPE_DATE;
- } // end of DTVAL constructor
-
-/***********************************************************************/
-/* DTVAL public constructor from int. */
-/***********************************************************************/
-DTVAL::DTVAL(PGLOBAL g, int n) : TYPVAL<int>(n, TYPE_DATE)
- {
- Pdtp = NULL;
- Len = 19;
-//Type = TYPE_DATE;
- Sdate = NULL;
- DefYear = 0;
- } // end of DTVAL constructor
-
-/***********************************************************************/
-/* Set format so formatted dates can be converted on input/output. */
-/***********************************************************************/
-bool DTVAL::SetFormat(PGLOBAL g, PSZ fmt, int len, int year)
- {
- Pdtp = MakeDateFormat(g, fmt, true, true, (year > 9999) ? 1 : 0);
- Sdate = (char*)PlugSubAlloc(g, NULL, len + 1);
- DefYear = (int)((year > 9999) ? (year - 10000) : year);
- Len = len;
- return false;
- } // end of SetFormat
-
-/***********************************************************************/
-/* Set format from the format of another date value. */
-/***********************************************************************/
-bool DTVAL::SetFormat(PGLOBAL g, PVAL valp)
- {
- DTVAL *vp;
-
- if (valp->GetType() != TYPE_DATE) {
- sprintf(g->Message, MSG(NO_FORMAT_TYPE), valp->GetType());
- return true;
- } else
- vp = (DTVAL*)valp;
-
- Len = vp->Len;
- Pdtp = vp->Pdtp;
- Sdate = (char*)PlugSubAlloc(g, NULL, Len + 1);
- DefYear = vp->DefYear;
- return false;
- } // end of SetFormat
-
-/***********************************************************************/
-/* We need TimeShift because the mktime C function does a correction */
-/* for local time zone that we want to override for DB operations. */
-/***********************************************************************/
-void DTVAL::SetTimeShift(void)
- {
- struct tm dtm;
- memset(&dtm, 0, sizeof(dtm));
- dtm.tm_mday=2;
- dtm.tm_mon=0;
- dtm.tm_year=70;
-
- Shift = (int)mktime(&dtm) - 86400;
-
- if (trace)
- htrc("DTVAL Shift=%d\n", Shift);
-
- } // end of SetTimeShift
-
-// Added by Alexander Barkov
-static void TIME_to_localtime(struct tm *tm, const MYSQL_TIME *ltime)
-{
- bzero(tm, sizeof(*tm));
- tm->tm_year= ltime->year - 1900;
- tm->tm_mon= ltime->month - 1;
- tm->tm_mday= ltime->day;
- tm->tm_hour= ltime->hour;
- tm->tm_min= ltime->minute;
- tm->tm_sec= ltime->second;
-}
-
-// Added by Alexander Barkov
-static struct tm *gmtime_mysql(const time_t *timep, struct tm *tm)
-{
- MYSQL_TIME ltime;
- thd_gmt_sec_to_TIME(current_thd, &ltime, (my_time_t) *timep);
- TIME_to_localtime(tm, &ltime);
- return tm;
-}
-
-/***********************************************************************/
-/* GetGmTime: returns a pointer to a static tm structure obtained */
-/* though the gmtime C function. The purpose of this function is to */
-/* extend the range of valid dates by accepting negative time values. */
-/***********************************************************************/
-struct tm *DTVAL::GetGmTime(struct tm *tm_buffer)
- {
- struct tm *datm;
- time_t t = (time_t)Tval;
-
- if (Tval < 0) {
- int n;
-
- for (n = 0; t < 0; n += 4)
- t += FOURYEARS;
-
- datm = gmtime_mysql(&t, tm_buffer);
-
- if (datm)
- datm->tm_year -= n;
-
- } else
- datm = gmtime_mysql(&t, tm_buffer);
-
- return datm;
- } // end of GetGmTime
-
-// Added by Alexander Barkov
-static time_t mktime_mysql(struct tm *ptm)
-{
- MYSQL_TIME ltime;
- localtime_to_TIME(&ltime, ptm);
- ltime.time_type= MYSQL_TIMESTAMP_DATETIME;
- uint error_code;
- time_t t= TIME_to_timestamp(current_thd, &ltime, &error_code);
- return error_code ? (time_t) -1 : t;
-}
-
-/***********************************************************************/
-/* MakeTime: calculates a date value from a tm structures using the */
-/* mktime C function. The purpose of this function is to extend the */
-/* range of valid dates by accepting to set negative time values. */
-/***********************************************************************/
-bool DTVAL::MakeTime(struct tm *ptm)
- {
- int n, y = ptm->tm_year;
- time_t t = mktime_mysql(ptm);
-
- if (trace > 1)
- htrc("MakeTime from (%d,%d,%d,%d,%d,%d)\n",
- ptm->tm_year, ptm->tm_mon, ptm->tm_mday,
- ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
-
- if (t == -1) {
- if (y < 1 || y > 71)
- return true;
-
- for (n = 0; t == -1 && n < 20; n++) {
- ptm->tm_year += 4;
- t = mktime_mysql(ptm);
- } // endfor t
-
- if (t == -1)
- return true;
-
- if ((t -= (n * FOURYEARS)) > 2000000000)
- return true;
-
- }
- Tval= (int) t;
-
- if (trace > 1)
- htrc("MakeTime Ival=%d\n", Tval);
-
- return false;
- } // end of MakeTime
-
-/***********************************************************************/
-/* Make a time_t datetime from its components (YY, MM, DD, hh, mm, ss) */
-/***********************************************************************/
-bool DTVAL::MakeDate(PGLOBAL g, int *val, int nval)
- {
- int i, m;
- int n;
- bool rc = false;
- struct tm datm;
- bzero(&datm, sizeof(datm));
- datm.tm_mday=1;
- datm.tm_mon=0;
- datm.tm_year=70;
-
- if (trace > 1)
- htrc("MakeDate from(%d,%d,%d,%d,%d,%d) nval=%d\n",
- val[0], val[1], val[2], val[3], val[4], val[5], nval);
-
- for (i = 0; i < nval; i++) {
- n = val[i];
-
-// if (trace > 1)
-// htrc("i=%d n=%d\n", i, n);
-
- switch (i) {
- case 0:
- if (n >= 1900)
- n -= 1900;
-
- datm.tm_year = n;
-
-// if (trace > 1)
-// htrc("n=%d tm_year=%d\n", n, datm.tm_year);
-
- break;
- case 1:
- // If mktime handles apparently correctly large or negative
- // day values, it is not the same for months. Therefore we
- // do the ajustment here, thus mktime has not to do it.
- if (n > 0) {
- m = (n - 1) % 12;
- n = (n - 1) / 12;
- } else {
- m = 11 + n % 12;
- n = n / 12 - 1;
- } // endfi n
-
- datm.tm_mon = m;
- datm.tm_year += n;
-
-// if (trace > 1)
-// htrc("n=%d m=%d tm_year=%d tm_mon=%d\n", n, m, datm.tm_year, datm.tm_mon);
-
- break;
- case 2:
- // For days, big or negative values may also cause problems
- m = n % 1461;
- n = 4 * (n / 1461);
-
- if (m < 0) {
- m += 1461;
- n -= 4;
- } // endif m
-
- datm.tm_mday = m;
- datm.tm_year += n;
-
-// if (trace > 1)
-// htrc("n=%d m=%d tm_year=%d tm_mon=%d\n", n, m, datm.tm_year, datm.tm_mon);
-
- break;
- case 3: datm.tm_hour = n; break;
- case 4: datm.tm_min = n; break;
- case 5: datm.tm_sec = n; break;
- } // endswitch i
-
- } // endfor i
-
- if (trace > 1)
- htrc("MakeDate datm=(%d,%d,%d,%d,%d,%d)\n",
- datm.tm_year, datm.tm_mon, datm.tm_mday,
- datm.tm_hour, datm.tm_min, datm.tm_sec);
-
- // Pass g to have an error return or NULL to set invalid dates to 0
- if (MakeTime(&datm))
- if (g) {
- strcpy(g->Message, MSG(BAD_DATETIME));
- rc = true;
- } else
- Tval = 0;
-
- return rc;
- } // end of MakeDate
-
-/***********************************************************************/
-/* DTVAL SetValue: copy the value of another Value object. */
-/* This function allows conversion if chktype is false. */
-/***********************************************************************/
-bool DTVAL::SetValue_pval(PVAL valp, bool chktype)
- {
- if (chktype && Type != valp->GetType())
- return true;
-
- if (!(Null = valp->IsNull() && Nullable)) {
- if (Pdtp && !valp->IsTypeNum()) {
- int ndv;
- int dval[6];
-
- ndv = ExtractDate(valp->GetCharValue(), Pdtp, DefYear, dval);
- MakeDate(NULL, dval, ndv);
- } else
- Tval = valp->GetIntValue();
-
- } else
- Reset();
-
- return false;
- } // end of SetValue
-
-/***********************************************************************/
-/* SetValue: convert chars extracted from a line to date value. */
-/***********************************************************************/
-bool DTVAL::SetValue_char(char *p, int n)
- {
- bool rc;
-
- if (Pdtp) {
- char *p2;
- int ndv;
- int dval[6];
-
- // Trim trailing blanks
- for (p2 = p + n -1; p < p2 && *p2 == ' '; p2--) ;
-
- if ((rc = (n = p2 - p + 1) > Len))
- n = Len;
-
- memcpy(Sdate, p, n);
- Sdate[n] = '\0';
-
- ndv = ExtractDate(Sdate, Pdtp, DefYear, dval);
- MakeDate(NULL, dval, ndv);
-
- if (trace > 1)
- htrc(" setting date: '%s' -> %d\n", Sdate, Tval);
-
- Null = false;
- } else
- rc = TYPVAL<int>::SetValue_char(p, n);
-
- return rc;
- } // end of SetValue
-
-/***********************************************************************/
-/* SetValue: convert a char string to date value. */
-/***********************************************************************/
-void DTVAL::SetValue_psz(PSZ p)
- {
- if (Pdtp) {
- int ndv;
- int dval[6];
-
- strncpy(Sdate, p, Len);
- Sdate[Len] = '\0';
-
- ndv = ExtractDate(Sdate, Pdtp, DefYear, dval);
- MakeDate(NULL, dval, ndv);
-
- if (trace > 1)
- htrc(" setting date: '%s' -> %d\n", Sdate, Tval);
-
- Null = false;
- } else
- TYPVAL<int>::SetValue_psz(p);
-
- } // end of SetValue
-
-/***********************************************************************/
-/* DTVAL SetValue: set value with a value extracted from a block. */
-/***********************************************************************/
-void DTVAL::SetValue_pvblk(PVBLK blk, int n)
- {
- if (Pdtp && !::IsTypeNum(blk->GetType())) {
- int ndv;
- int dval[6];
-
- ndv = ExtractDate(blk->GetCharValue(n), Pdtp, DefYear, dval);
- MakeDate(NULL, dval, ndv);
- } else
- Tval = blk->GetIntValue(n);
-
- } // end of SetValue
-
-/***********************************************************************/
-/* DTVAL GetCharString: get string representation of a date value. */
-/***********************************************************************/
-char *DTVAL::GetCharString(char *p)
- {
- if (Pdtp) {
- size_t n = 0;
- struct tm tm, *ptm= GetGmTime(&tm);
-
- if (ptm)
- n = strftime(Sdate, Len + 1, Pdtp->OutFmt, ptm);
-
- if (!n) {
- *Sdate = '\0';
- strncat(Sdate, "Error", Len + 1);
- } // endif n
-
- return Sdate;
- } else
- sprintf(p, "%d", Tval);
-
- Null = false;
- return p;
- } // end of GetCharString
-
-/***********************************************************************/
-/* DTVAL ShowValue: get string representation of a date value. */
-/***********************************************************************/
-char *DTVAL::ShowValue(char *buf, int len)
- {
- if (Pdtp) {
- char *p;
- size_t m, n = 0;
- struct tm tm, *ptm = GetGmTime(&tm);
-
- if (Len < len) {
- p = buf;
- m = len;
- } else {
- p = Sdate;
- m = Len + 1;
- } // endif Len
-
- if (ptm)
- n = strftime(p, m, Pdtp->OutFmt, ptm);
-
- if (!n) {
- *p = '\0';
- strncat(p, "Error", m);
- } // endif n
-
- return p;
- } else
- return TYPVAL<int>::ShowValue(buf, len);
-
- } // end of ShowValue
-
-#if 0 // Not used by CONNECT
-/***********************************************************************/
-/* Returns a member of the struct tm representation of the date. */
-/***********************************************************************/
-bool DTVAL::GetTmMember(OPVAL op, int& mval)
- {
- bool rc = false;
- struct tm tm, *ptm = GetGmTime(&tm);
-
- switch (op) {
- case OP_MDAY: mval = ptm->tm_mday; break;
- case OP_MONTH: mval = ptm->tm_mon + 1; break;
- case OP_YEAR: mval = ptm->tm_year + 1900; break;
- case OP_WDAY: mval = ptm->tm_wday + 1; break;
- case OP_YDAY: mval = ptm->tm_yday + 1; break;
- case OP_QUART: mval = ptm->tm_mon / 3 + 1; break;
- default:
- rc = true;
- } // endswitch op
-
- return rc;
- } // end of GetTmMember
-
-/***********************************************************************/
-/* Calculates the week number of the year for the internal date value.*/
-/* The International Standard ISO 8601 has decreed that Monday shall */
-/* be the first day of the week. A week that lies partly in one year */
-/* and partly in another is assigned a number in the year in which */
-/* most of its days lie. That means that week number 1 of any year is */
-/* the week that contains the January 4th. */
-/***********************************************************************/
-bool DTVAL::WeekNum(PGLOBAL g, int& nval)
- {
- // w is the start of the week SUN=0, MON=1, etc.
- int m, n, w = nval % 7;
- struct tm tm, *ptm = GetGmTime(&tm);
-
- // Which day is January 4th of this year?
- m = (367 + ptm->tm_wday - ptm->tm_yday) % 7;
-
- // When does the first week begins?
- n = 3 - (7 + m - w) % 7;
-
- // Now calculate the week number
- if (!(nval = (7 + ptm->tm_yday - n) / 7))
- nval = 52;
-
- // Everything should be Ok
- return false;
- } // end of WeekNum
-#endif // 0
-
-/***********************************************************************/
-/* FormatValue: This function set vp (a STRING value) to the string */
-/* constructed from its own value formated using the fmt format. */
-/* This function assumes that the format matches the value type. */
-/***********************************************************************/
-bool DTVAL::FormatValue(PVAL vp, char *fmt)
- {
- char *buf = (char*)vp->GetTo_Val(); // Should be big enough
- struct tm tm, *ptm = GetGmTime(&tm);
-
- if (trace > 1)
- htrc("FormatValue: ptm=%p len=%d\n", ptm, vp->GetValLen());
-
- if (ptm) {
- size_t n = strftime(buf, vp->GetValLen(), fmt, ptm);
-
- if (trace > 1)
- htrc("strftime: n=%d buf=%s\n", n, (n) ? buf : "???");
-
- return (n == 0);
- } else
- return true;
-
- } // end of FormatValue
-
-/* -------------------------- End of Value --------------------------- */
+/************* Value C++ Functions Source Code File (.CPP) *************/
+/* Name: VALUE.CPP Version 2.4 */
+/* */
+/* (C) Copyright to the author Olivier BERTRAND 2001-2014 */
+/* */
+/* This file contains the VALUE and derived classes family functions. */
+/* These classes contain values of different types. They are used so */
+/* new object types can be defined and added to the processing simply */
+/* (hopefully) adding their specific functions in this file. */
+/* First family is VALUE that represent single typed objects. It is */
+/* used by columns (COLBLK), SELECT and FILTER (derived) objects. */
+/* Second family is VALBLK, representing simple suballocated arrays */
+/* of values treated sequentially by FIX, BIN and VCT tables and */
+/* columns, as well for min/max blocks as for VCT column blocks. */
+/* Q&A: why not using only one family ? Simple values are arrays that */
+/* have only one element and arrays could have functions for all kind */
+/* of processing. The answer is a-because historically it was simpler */
+/* to do that way, b-because of performance on single values, and c- */
+/* to avoid too complicated classes and unuseful duplication of many */
+/* functions used on one family only. The drawback is that for new */
+/* types of objects, we shall have more classes to update. */
+/* Currently the only implemented types are STRING, INT, SHORT, TINY, */
+/* DATE and LONGLONG. Recently we added some UNSIGNED types. */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant MariaDB header file. */
+/***********************************************************************/
+#include "my_global.h"
+#include "sql_class.h"
+#include "sql_time.h"
+
+#if defined(WIN32)
+//#include <windows.h>
+#else // !WIN32
+#include <string.h>
+#endif // !WIN32
+
+#include <math.h>
+
+#undef DOMAIN // Was defined in math.h
+
+/***********************************************************************/
+/* Include required application header files */
+/* global.h is header containing all global Plug declarations. */
+/* plgdbsem.h is header containing the DB applic. declarations. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "preparse.h" // For DATPAR
+//#include "value.h"
+#include "valblk.h"
+#define NO_FUNC // Already defined in ODBConn
+#include "plgcnx.h" // For DB types
+#include "osutil.h"
+
+/***********************************************************************/
+/* Check macro's. */
+/***********************************************************************/
+#if defined(_DEBUG)
+#define CheckType(V) if (Type != V->GetType()) { \
+ PGLOBAL& g = Global; \
+ strcpy(g->Message, MSG(VALTYPE_NOMATCH)); \
+ longjmp(g->jumper[g->jump_level], Type); }
+#else
+#define CheckType(V)
+#endif
+
+#define FOURYEARS 126230400 // Four years in seconds (1 leap)
+
+/***********************************************************************/
+/* Static variables. */
+/***********************************************************************/
+
+extern "C" int trace;
+
+/***********************************************************************/
+/* Initialize the DTVAL static member. */
+/***********************************************************************/
+int DTVAL::Shift = 0;
+
+/***********************************************************************/
+/* Routines called externally. */
+/***********************************************************************/
+bool PlugEvalLike(PGLOBAL, LPCSTR, LPCSTR, bool);
+
+#if !defined(WIN32)
+extern "C" {
+PSZ strupr(PSZ s);
+PSZ strlwr(PSZ s);
+}
+#endif // !WIN32
+
+/***********************************************************************/
+/* Returns the bitmap representing the conditions that must not be */
+/* met when returning from TestValue for a given operator. */
+/* Bit one is EQ, bit 2 is LT, and bit 3 is GT. */
+/***********************************************************************/
+BYTE OpBmp(PGLOBAL g, OPVAL opc)
+ {
+ BYTE bt;
+
+ switch (opc) {
+ case OP_IN:
+ case OP_EQ: bt = 0x06; break;
+ case OP_NE: bt = 0x01; break;
+ case OP_GT: bt = 0x03; break;
+ case OP_GE: bt = 0x02; break;
+ case OP_LT: bt = 0x05; break;
+ case OP_LE: bt = 0x04; break;
+ case OP_EXIST: bt = 0x00; break;
+ default:
+ sprintf(g->Message, MSG(BAD_FILTER_OP), opc);
+ longjmp(g->jumper[g->jump_level], TYPE_ARRAY);
+ } // endswitch opc
+
+ return bt;
+ } // end of OpBmp
+
+/***********************************************************************/
+/* Get a long long number from its character representation. */
+/* IN p: Pointer to the numeric string */
+/* IN n: The string length */
+/* IN maxval: The number max value */
+/* IN un: True if the number must be unsigned */
+/* OUT rc: Set to TRUE for out of range value */
+/* OUT minus: Set to true if the number is negative */
+/* Returned val: The resulting number */
+/***********************************************************************/
+ulonglong CharToNumber(char *p, int n, ulonglong maxval,
+ bool un, bool *minus, bool *rc)
+{
+ char *p2;
+ uchar c;
+ ulonglong val;
+
+ if (minus) *minus = false;
+ if (rc) *rc = false;
+
+ // Eliminate leading blanks or 0
+ for (p2 = p + n; p < p2 && (*p == ' ' || *p == '0'); p++) ;
+
+ // Get an eventual sign character
+ switch (*p) {
+ case '-':
+ if (un) {
+ if (rc) *rc = true;
+ return 0;
+ } else {
+ maxval++;
+ if (minus) *minus = true;
+ } // endif Unsigned
+
+ case '+':
+ p++;
+ break;
+ } // endswitch *p
+
+ for (val = 0; p < p2 && (c = (uchar)(*p - '0')) < 10; p++)
+ if (val > (maxval - c) / 10) {
+ val = maxval;
+ if (rc) *rc = true;
+ break;
+ } else
+ val = val * 10 + c;
+
+ return val;
+} // end of CharToNumber
+
+/***********************************************************************/
+/* GetTypeName: returns the PlugDB internal type name. */
+/***********************************************************************/
+PSZ GetTypeName(int type)
+ {
+ PSZ name;
+
+ switch (type) {
+ case TYPE_STRING: name = "CHAR"; break;
+ case TYPE_SHORT: name = "SMALLINT"; break;
+ case TYPE_INT: name = "INTEGER"; break;
+ case TYPE_BIGINT: name = "BIGINT"; break;
+ case TYPE_DATE: name = "DATE"; break;
+ case TYPE_DOUBLE: name = "DOUBLE"; break;
+ case TYPE_TINY: name = "TINY"; break;
+ case TYPE_DECIM: name = "DECIMAL"; break;
+ default: name = "UNKNOWN"; break;
+ } // endswitch type
+
+ return name;
+ } // end of GetTypeName
+
+/***********************************************************************/
+/* GetTypeSize: returns the PlugDB internal type size. */
+/***********************************************************************/
+int GetTypeSize(int type, int len)
+ {
+ switch (type) {
+ case TYPE_DECIM:
+ case TYPE_STRING: len = len * sizeof(char); break;
+ case TYPE_SHORT: len = sizeof(short); break;
+ case TYPE_INT: len = sizeof(int); break;
+ case TYPE_BIGINT: len = sizeof(longlong); break;
+ case TYPE_DATE: len = sizeof(int); break;
+ case TYPE_DOUBLE: len = sizeof(double); break;
+ case TYPE_TINY: len = sizeof(char); break;
+ default: len = 0;
+ } // endswitch type
+
+ return len;
+ } // end of GetTypeSize
+
+/***********************************************************************/
+/* GetFormatType: returns the FORMAT character(s) according to type. */
+/***********************************************************************/
+char *GetFormatType(int type)
+ {
+ char *c = "X";
+
+ switch (type) {
+ case TYPE_STRING: c = "C"; break;
+ case TYPE_SHORT: c = "S"; break;
+ case TYPE_INT: c = "N"; break;
+ case TYPE_BIGINT: c = "L"; break;
+ case TYPE_DOUBLE: c = "F"; break;
+ case TYPE_DATE: c = "D"; break;
+ case TYPE_TINY: c = "T"; break;
+ case TYPE_DECIM: c = "M"; break;
+ } // endswitch type
+
+ return c;
+ } // end of GetFormatType
+
+/***********************************************************************/
+/* GetFormatType: returns the FORMAT type according to character. */
+/***********************************************************************/
+int GetFormatType(char c)
+ {
+ int type = TYPE_ERROR;
+
+ switch (c) {
+ case 'C': type = TYPE_STRING; break;
+ case 'S': type = TYPE_SHORT; break;
+ case 'N': type = TYPE_INT; break;
+ case 'L': type = TYPE_BIGINT; break;
+ case 'F': type = TYPE_DOUBLE; break;
+ case 'D': type = TYPE_DATE; break;
+ case 'T': type = TYPE_TINY; break;
+ case 'M': type = TYPE_DECIM; break;
+ } // endswitch type
+
+ return type;
+ } // end of GetFormatType
+
+/***********************************************************************/
+/* IsTypeChar: returns true for character type(s). */
+/***********************************************************************/
+bool IsTypeChar(int type)
+ {
+ switch (type) {
+ case TYPE_STRING:
+ case TYPE_DECIM:
+ return true;
+ } // endswitch type
+
+ return false;
+ } // end of IsTypeChar
+
+/***********************************************************************/
+/* IsTypeNum: returns true for numeric types. */
+/***********************************************************************/
+bool IsTypeNum(int type)
+ {
+ switch (type) {
+ case TYPE_INT:
+ case TYPE_BIGINT:
+ case TYPE_DATE:
+ case TYPE_DOUBLE:
+ case TYPE_SHORT:
+ case TYPE_NUM:
+ case TYPE_TINY:
+ case TYPE_DECIM:
+ return true;
+ } // endswitch type
+
+ return false;
+ } // end of IsTypeNum
+
+/***********************************************************************/
+/* GetFmt: returns the format to use with a typed value. */
+/***********************************************************************/
+const char *GetFmt(int type, bool un)
+ {
+ const char *fmt;
+
+ switch (type) {
+ case TYPE_DECIM:
+ case TYPE_STRING: fmt = "%s"; break;
+ case TYPE_SHORT: fmt = (un) ? "%hu" : "%hd"; break;
+ case TYPE_BIGINT: fmt = (un) ? "%llu" : "%lld"; break;
+ case TYPE_DOUBLE: fmt = "%.*lf"; break;
+ default: fmt = (un) ? "%u" : "%d"; break;
+ } // endswitch Type
+
+ return fmt;
+ } // end of GetFmt
+
+/***********************************************************************/
+/* ConvertType: what this function does is to determine the type to */
+/* which should be converted a value so no precision would be lost. */
+/* This can be a numeric type if num is true or non numeric if false. */
+/* Note: this is an ultra simplified version of this function that */
+/* should become more and more complex as new types are added. */
+/* Not evaluated types (TYPE_VOID or TYPE_UNDEF) return false from */
+/* IsType... functions so match does not prevent correct setting. */
+/***********************************************************************/
+int ConvertType(int target, int type, CONV kind, bool match)
+ {
+ switch (kind) {
+ case CNV_CHAR:
+ if (match && (!IsTypeChar(target) || !IsTypeChar(type)))
+ return TYPE_ERROR;
+
+ return TYPE_STRING;
+ case CNV_NUM:
+ if (match && (!IsTypeNum(target) || !IsTypeNum(type)))
+ return TYPE_ERROR;
+
+ return (target == TYPE_DOUBLE || type == TYPE_DOUBLE) ? TYPE_DOUBLE
+ : (target == TYPE_DATE || type == TYPE_DATE) ? TYPE_DATE
+ : (target == TYPE_BIGINT || type == TYPE_BIGINT) ? TYPE_BIGINT
+ : (target == TYPE_INT || type == TYPE_INT) ? TYPE_INT
+ : (target == TYPE_SHORT || type == TYPE_SHORT) ? TYPE_SHORT
+ : TYPE_TINY;
+ default:
+ if (target == TYPE_ERROR || target == type)
+ return type;
+
+ if (match && ((IsTypeChar(target) && !IsTypeChar(type)) ||
+ (IsTypeNum(target) && !IsTypeNum(type))))
+ return TYPE_ERROR;
+
+ return (target == TYPE_DOUBLE || type == TYPE_DOUBLE) ? TYPE_DOUBLE
+ : (target == TYPE_DATE || type == TYPE_DATE) ? TYPE_DATE
+ : (target == TYPE_BIGINT || type == TYPE_BIGINT) ? TYPE_BIGINT
+ : (target == TYPE_INT || type == TYPE_INT) ? TYPE_INT
+ : (target == TYPE_SHORT || type == TYPE_SHORT) ? TYPE_SHORT
+ : (target == TYPE_STRING || type == TYPE_STRING) ? TYPE_STRING
+ : (target == TYPE_TINY || type == TYPE_TINY) ? TYPE_TINY
+ : TYPE_ERROR;
+ } // endswitch kind
+
+ } // end of ConvertType
+
+/***********************************************************************/
+/* AllocateConstant: allocates a constant Value. */
+/***********************************************************************/
+PVAL AllocateValue(PGLOBAL g, void *value, short type)
+ {
+ PVAL valp;
+
+ if (trace)
+ htrc("AllocateConstant: value=%p type=%hd\n", value, type);
+
+ switch (type) {
+ case TYPE_STRING:
+ valp = new(g) TYPVAL<PSZ>((PSZ)value);
+ break;
+ case TYPE_SHORT:
+ valp = new(g) TYPVAL<short>(*(short*)value, TYPE_SHORT);
+ break;
+ case TYPE_INT:
+ valp = new(g) TYPVAL<int>(*(int*)value, TYPE_INT);
+ break;
+ case TYPE_BIGINT:
+ valp = new(g) TYPVAL<longlong>(*(longlong*)value, TYPE_BIGINT);
+ break;
+ case TYPE_DOUBLE:
+ valp = new(g) TYPVAL<double>(*(double *)value, TYPE_DOUBLE, 2);
+ break;
+ case TYPE_TINY:
+ valp = new(g) TYPVAL<char>(*(char *)value, TYPE_TINY);
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
+ return NULL;
+ } // endswitch Type
+
+ valp->SetGlobal(g);
+ return valp;
+ } // end of AllocateValue
+
+/***********************************************************************/
+/* Allocate a variable Value according to type, length and precision. */
+/***********************************************************************/
+PVAL AllocateValue(PGLOBAL g, int type, int len, int prec,
+ bool uns, PSZ fmt)
+ {
+ PVAL valp;
+
+ switch (type) {
+ case TYPE_STRING:
+ valp = new(g) TYPVAL<PSZ>(g, (PSZ)NULL, len, prec);
+ break;
+ case TYPE_DATE:
+ valp = new(g) DTVAL(g, len, prec, fmt);
+ break;
+ case TYPE_INT:
+ if (uns)
+ valp = new(g) TYPVAL<uint>((uint)0, TYPE_INT, 0, true);
+ else
+ valp = new(g) TYPVAL<int>((int)0, TYPE_INT);
+
+ break;
+ case TYPE_BIGINT:
+ if (uns)
+ valp = new(g) TYPVAL<ulonglong>((ulonglong)0, TYPE_BIGINT, 0, true);
+ else
+ valp = new(g) TYPVAL<longlong>((longlong)0, TYPE_BIGINT);
+
+ break;
+ case TYPE_SHORT:
+ if (uns)
+ valp = new(g) TYPVAL<ushort>((ushort)0, TYPE_SHORT, 0, true);
+ else
+ valp = new(g) TYPVAL<short>((short)0, TYPE_SHORT);
+
+ break;
+ case TYPE_DOUBLE:
+ valp = new(g) TYPVAL<double>(0.0, TYPE_DOUBLE, prec);
+ break;
+ case TYPE_TINY:
+ if (uns)
+ valp = new(g) TYPVAL<uchar>((uchar)0, TYPE_TINY, 0, true);
+ else
+ valp = new(g) TYPVAL<char>((char)0, TYPE_TINY);
+
+ break;
+ case TYPE_DECIM:
+ valp = new(g) DECVAL(g, (PSZ)NULL, len, prec, uns);
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
+ return NULL;
+ } // endswitch type
+
+ valp->SetGlobal(g);
+ return valp;
+ } // end of AllocateValue
+
+/***********************************************************************/
+/* Allocate a constant Value converted to newtype. */
+/* Can also be used to copy a Value eventually converted. */
+/***********************************************************************/
+PVAL AllocateValue(PGLOBAL g, PVAL valp, int newtype, int uns)
+ {
+ PSZ p, sp;
+ bool un = (uns < 0) ? false : (uns > 0) ? true : valp->IsUnsigned();
+
+ if (newtype == TYPE_VOID) // Means allocate a value of the same type
+ newtype = valp->GetType();
+
+ switch (newtype) {
+ case TYPE_STRING:
+ p = (PSZ)PlugSubAlloc(g, NULL, 1 + valp->GetValLen());
+
+ if ((sp = valp->GetCharString(p)) != p)
+ strcpy (p, sp);
+
+ valp = new(g) TYPVAL<PSZ>(g, p, valp->GetValLen(), valp->GetValPrec());
+ break;
+ case TYPE_SHORT:
+ if (un)
+ valp = new(g) TYPVAL<ushort>(valp->GetUShortValue(),
+ TYPE_SHORT, 0, true);
+ else
+ valp = new(g) TYPVAL<short>(valp->GetShortValue(), TYPE_SHORT);
+
+ break;
+ case TYPE_INT:
+ if (un)
+ valp = new(g) TYPVAL<uint>(valp->GetUIntValue(), TYPE_INT, 0, true);
+ else
+ valp = new(g) TYPVAL<int>(valp->GetIntValue(), TYPE_INT);
+
+ break;
+ case TYPE_BIGINT:
+ if (un)
+ valp = new(g) TYPVAL<ulonglong>(valp->GetUBigintValue(),
+ TYPE_BIGINT, 0, true);
+ else
+ valp = new(g) TYPVAL<longlong>(valp->GetBigintValue(), TYPE_BIGINT);
+
+ break;
+ case TYPE_DATE:
+ valp = new(g) DTVAL(g, valp->GetIntValue());
+ break;
+ case TYPE_DOUBLE:
+ valp = new(g) TYPVAL<double>(valp->GetFloatValue(), TYPE_DOUBLE,
+ valp->GetValPrec());
+ break;
+ case TYPE_TINY:
+ if (un)
+ valp = new(g) TYPVAL<uchar>(valp->GetUTinyValue(),
+ TYPE_TINY, 0, true);
+ else
+ valp = new(g) TYPVAL<char>(valp->GetTinyValue(), TYPE_TINY);
+
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_VALUE_TYPE), newtype);
+ return NULL;
+ } // endswitch type
+
+ valp->SetGlobal(g);
+ return valp;
+ } // end of AllocateValue
+
+/* -------------------------- Class VALUE ---------------------------- */
+
+/***********************************************************************/
+/* Class VALUE protected constructor. */
+/***********************************************************************/
+VALUE::VALUE(int type, bool un) : Type(type)
+ {
+ Null = false;
+ Nullable = false;
+ Unsigned = un;
+ Clen = 0;
+ Prec = 0;
+ Fmt = GetFmt(Type, Unsigned);
+ Xfmt = GetXfmt();
+ } // end of VALUE constructor
+
+/***********************************************************************/
+/* VALUE GetXfmt: returns the extended format to use with typed value. */
+/***********************************************************************/
+const char *VALUE::GetXfmt(void)
+ {
+ const char *fmt;
+
+ switch (Type) {
+ case TYPE_DECIM:
+ case TYPE_STRING: fmt = "%*s"; break;
+ case TYPE_SHORT: fmt = (Unsigned) ? "%*hu" : "%*hd"; break;
+ case TYPE_BIGINT: fmt = (Unsigned) ? "%*llu" : "%*lld"; break;
+ case TYPE_DOUBLE: fmt = "%*.*lf"; break;
+ default: fmt = (Unsigned) ? "%*u" : "%*d"; break;
+ } // endswitch Type
+
+ return fmt;
+ } // end of GetFmt
+
+/***********************************************************************/
+/* Returns a BYTE indicating the comparison between two values. */
+/* Bit 1 indicates equality, Bit 2 less than, and Bit3 greater than. */
+/* More than 1 bit can be set only in the case of TYPE_LIST. */
+/***********************************************************************/
+BYTE VALUE::TestValue(PVAL vp)
+ {
+ int n = CompareValue(vp);
+
+ return (n > 0) ? 0x04 : (n < 0) ? 0x02 : 0x01;
+ } // end of TestValue
+
+/* -------------------------- Class TYPVAL ---------------------------- */
+
+/***********************************************************************/
+/* TYPVAL public constructor from a constant typed value. */
+/***********************************************************************/
+template <class TYPE>
+TYPVAL<TYPE>::TYPVAL(TYPE n, int type, int prec, bool un)
+ : VALUE(type, un)
+ {
+ Tval = n;
+ Clen = sizeof(TYPE);
+ Prec = prec;
+ } // end of TYPVAL constructor
+
+/***********************************************************************/
+/* Return unsigned max value for the type. */
+/***********************************************************************/
+template <class TYPE>
+ulonglong TYPVAL<TYPE>::MaxVal(void) {DBUG_ASSERT(false); return 0;}
+
+template <>
+ulonglong TYPVAL<short>::MaxVal(void) {return INT_MAX16;}
+
+template <>
+ulonglong TYPVAL<ushort>::MaxVal(void) {return UINT_MAX16;}
+
+template <>
+ulonglong TYPVAL<int>::MaxVal(void) {return INT_MAX32;}
+
+template <>
+ulonglong TYPVAL<uint>::MaxVal(void) {return UINT_MAX32;}
+
+template <>
+ulonglong TYPVAL<char>::MaxVal(void) {return INT_MAX8;}
+
+template <>
+ulonglong TYPVAL<uchar>::MaxVal(void) {return UINT_MAX8;}
+
+template <>
+ulonglong TYPVAL<longlong>::MaxVal(void) {return INT_MAX64;}
+
+template <>
+ulonglong TYPVAL<ulonglong>::MaxVal(void) {return ULONGLONG_MAX;}
+
+/***********************************************************************/
+/* TYPVAL GetValLen: returns the print length of the typed object. */
+/***********************************************************************/
+template <class TYPE>
+int TYPVAL<TYPE>::GetValLen(void)
+ {
+ char c[32];
+
+ return sprintf(c, Fmt, Tval);
+ } // end of GetValLen
+
+template <>
+int TYPVAL<double>::GetValLen(void)
+ {
+ char c[32];
+
+ return sprintf(c, Fmt, Prec, Tval);
+ } // end of GetValLen
+
+/***********************************************************************/
+/* TYPVAL SetValue: copy the value of another Value object. */
+/* This function allows conversion if chktype is false. */
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::SetValue_pval(PVAL valp, bool chktype)
+ {
+ if (chktype && Type != valp->GetType())
+ return true;
+
+ if (!(Null = valp->IsNull() && Nullable))
+ Tval = GetTypedValue(valp);
+ else
+ Reset();
+
+ return false;
+ } // end of SetValue
+
+template <>
+short TYPVAL<short>::GetTypedValue(PVAL valp)
+ {return valp->GetShortValue();}
+
+template <>
+ushort TYPVAL<ushort>::GetTypedValue(PVAL valp)
+ {return valp->GetUShortValue();}
+
+template <>
+int TYPVAL<int>::GetTypedValue(PVAL valp)
+ {return valp->GetIntValue();}
+
+template <>
+uint TYPVAL<uint>::GetTypedValue(PVAL valp)
+ {return valp->GetUIntValue();}
+
+template <>
+longlong TYPVAL<longlong>::GetTypedValue(PVAL valp)
+ {return valp->GetBigintValue();}
+
+template <>
+ulonglong TYPVAL<ulonglong>::GetTypedValue(PVAL valp)
+ {return valp->GetUBigintValue();}
+
+template <>
+double TYPVAL<double>::GetTypedValue(PVAL valp)
+ {return valp->GetFloatValue();}
+
+template <>
+char TYPVAL<char>::GetTypedValue(PVAL valp)
+ {return valp->GetTinyValue();}
+
+template <>
+uchar TYPVAL<uchar>::GetTypedValue(PVAL valp)
+ {return valp->GetUTinyValue();}
+
+/***********************************************************************/
+/* TYPVAL SetValue: convert chars extracted from a line to TYPE value.*/
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::SetValue_char(char *p, int n)
+ {
+ bool rc, minus;
+ ulonglong maxval = MaxVal();
+ ulonglong val = CharToNumber(p, n, maxval, Unsigned, &minus, &rc);
+
+ if (minus && val < maxval)
+ Tval = (TYPE)(-(signed)val);
+ else
+ Tval = (TYPE)val;
+
+ if (trace > 1) {
+ char buf[64];
+ htrc(strcat(strcat(strcpy(buf, " setting %s to: "), Fmt), "\n"),
+ GetTypeName(Type), Tval);
+ } // endif trace
+
+ Null = false;
+ return rc;
+ } // end of SetValue
+
+template <>
+bool TYPVAL<double>::SetValue_char(char *p, int n)
+ {
+ if (p) {
+ char buf[64];
+
+ for (; n > 0 && *p == ' '; p++)
+ n--;
+
+ memcpy(buf, p, min(n, 31));
+ buf[n] = '\0';
+ Tval = atof(buf);
+
+ if (trace > 1)
+ htrc(" setting double: '%s' -> %lf\n", buf, Tval);
+
+ Null = false;
+ } else {
+ Reset();
+ Null = Nullable;
+ } // endif p
+
+ return false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* TYPVAL SetValue: fill a typed value from a string. */
+/***********************************************************************/
+template <class TYPE>
+void TYPVAL<TYPE>::SetValue_psz(PSZ s)
+ {
+ if (s) {
+ SetValue_char(s, (int)strlen(s));
+ Null = false;
+ } else {
+ Reset();
+ Null = Nullable;
+ } // endif p
+
+ } // end of SetValue
+
+/***********************************************************************/
+/* TYPVAL SetValue: set value with a TYPE extracted from a block. */
+/***********************************************************************/
+template <class TYPE>
+void TYPVAL<TYPE>::SetValue_pvblk(PVBLK blk, int n)
+ {
+ Tval = GetTypedValue(blk, n);
+ Null = false;
+ } // end of SetValue
+
+template <>
+int TYPVAL<int>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetIntValue(n);}
+
+template <>
+uint TYPVAL<uint>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetUIntValue(n);}
+
+template <>
+short TYPVAL<short>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetShortValue(n);}
+
+template <>
+ushort TYPVAL<ushort>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetUShortValue(n);}
+
+template <>
+longlong TYPVAL<longlong>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetBigintValue(n);}
+
+template <>
+ulonglong TYPVAL<ulonglong>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetUBigintValue(n);}
+
+template <>
+double TYPVAL<double>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetFloatValue(n);}
+
+template <>
+char TYPVAL<char>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetTinyValue(n);}
+
+template <>
+uchar TYPVAL<uchar>::GetTypedValue(PVBLK blk, int n)
+ {return blk->GetUTinyValue(n);}
+
+/***********************************************************************/
+/* TYPVAL SetBinValue: with bytes extracted from a line. */
+/***********************************************************************/
+template <class TYPE>
+void TYPVAL<TYPE>::SetBinValue(void *p)
+ {
+ Tval = *(TYPE *)p;
+ Null = false;
+ } // end of SetBinValue
+
+/***********************************************************************/
+/* GetBinValue: fill a buffer with the internal binary value. */
+/* This function checks whether the buffer length is enough and */
+/* returns true if not. Actual filling occurs only if go is true. */
+/* Currently used by WriteColumn of binary files. */
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::GetBinValue(void *buf, int buflen, bool go)
+ {
+ // Test on length was removed here until a variable in column give the
+ // real field length. For BIN files the field length logically cannot
+ // be different from the variable length because no conversion is done.
+ // Therefore this test is useless anyway.
+//#if defined(_DEBUG)
+// if (sizeof(TYPE) > buflen)
+// return true;
+//#endif
+
+ if (go)
+ *(TYPE *)buf = Tval;
+
+ Null = false;
+ return false;
+ } // end of GetBinValue
+
+/***********************************************************************/
+/* TYPVAL ShowValue: get string representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::ShowValue(char *buf, int len)
+ {
+ sprintf(buf, Xfmt, len, Tval);
+ return buf;
+ } // end of ShowValue
+
+template <>
+char *TYPVAL<double>::ShowValue(char *buf, int len)
+ {
+ // TODO: use snprintf to avoid possible overflow
+ sprintf(buf, Xfmt, len, Prec, Tval);
+ return buf;
+ } // end of ShowValue
+
+/***********************************************************************/
+/* TYPVAL GetCharString: get string representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetCharString(char *p)
+ {
+ sprintf(p, Fmt, Tval);
+ return p;
+ } // end of GetCharString
+
+template <>
+char *TYPVAL<double>::GetCharString(char *p)
+ {
+ sprintf(p, Fmt, Prec, Tval);
+ return p;
+ } // end of GetCharString
+
+#if 0
+/***********************************************************************/
+/* TYPVAL GetShortString: get short representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetShortString(char *p, int n)
+ {
+ sprintf(p, "%*hd", n, (short)Tval);
+ return p;
+ } // end of GetShortString
+
+/***********************************************************************/
+/* TYPVAL GetIntString: get int representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetIntString(char *p, int n)
+ {
+ sprintf(p, "%*d", n, (int)Tval);
+ return p;
+ } // end of GetIntString
+
+/***********************************************************************/
+/* TYPVAL GetBigintString: get big int representation of a TYPE value.*/
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetBigintString(char *p, int n)
+ {
+ sprintf(p, "%*lld", n, (longlong)Tval);
+ return p;
+ } // end of GetBigintString
+
+/***********************************************************************/
+/* TYPVAL GetFloatString: get double representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetFloatString(char *p, int n, int prec)
+ {
+ sprintf(p, "%*.*lf", n, (prec < 0) ? 2 : prec, (double)Tval);
+ return p;
+ } // end of GetFloatString
+
+/***********************************************************************/
+/* TYPVAL GetTinyString: get char representation of a typed value. */
+/***********************************************************************/
+template <class TYPE>
+char *TYPVAL<TYPE>::GetTinyString(char *p, int n)
+ {
+ sprintf(p, "%*d", n, (int)(char)Tval);
+ return p;
+ } // end of GetIntString
+#endif // 0
+
+/***********************************************************************/
+/* TYPVAL compare value with another Value. */
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::IsEqual(PVAL vp, bool chktype)
+ {
+ if (this == vp)
+ return true;
+ else if (chktype && Type != vp->GetType())
+ return false;
+ else if (chktype && Unsigned != vp->IsUnsigned())
+ return false;
+ else if (Null || vp->IsNull())
+ return false;
+ else
+ return (Tval == GetTypedValue(vp));
+
+ } // end of IsEqual
+
+/***********************************************************************/
+/* Compare values and returns 1, 0 or -1 according to comparison. */
+/* This function is used for evaluation of numeric filters. */
+/***********************************************************************/
+template <class TYPE>
+int TYPVAL<TYPE>::CompareValue(PVAL vp)
+ {
+//assert(vp->GetType() == Type);
+
+ // Process filtering on numeric values.
+ TYPE n = GetTypedValue(vp);
+
+//if (trace)
+// htrc(" Comparing: val=%d,%d\n", Tval, n);
+
+ return (Tval > n) ? 1 : (Tval < n) ? (-1) : 0;
+ } // end of CompareValue
+
+/***********************************************************************/
+/* FormatValue: This function set vp (a STRING value) to the string */
+/* constructed from its own value formated using the fmt format. */
+/* This function assumes that the format matches the value type. */
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::FormatValue(PVAL vp, char *fmt)
+ {
+ char *buf = (char*)vp->GetTo_Val(); // Should be big enough
+ int n = sprintf(buf, fmt, Tval);
+
+ return (n > vp->GetValLen());
+ } // end of FormatValue
+
+/***********************************************************************/
+/* TYPVAL SetFormat function (used to set SELECT output format). */
+/***********************************************************************/
+template <class TYPE>
+bool TYPVAL<TYPE>::SetConstFormat(PGLOBAL g, FORMAT& fmt)
+ {
+ char c[32];
+
+ fmt.Type[0] = *GetFormatType(Type);
+ fmt.Length = sprintf(c, Fmt, Tval);
+ fmt.Prec = Prec;
+ return false;
+ } // end of SetConstFormat
+
+/***********************************************************************/
+/* Make file output of a typed object. */
+/***********************************************************************/
+template <class TYPE>
+void TYPVAL<TYPE>::Print(PGLOBAL g, FILE *f, uint n)
+ {
+ char m[64], buf[12];
+
+ memset(m, ' ', n); /* Make margin string */
+ m[n] = '\0';
+
+ if (Null)
+ fprintf(f, "%s<null>\n", m);
+ else
+ fprintf(f, strcat(strcat(strcpy(buf, "%s"), Fmt), "\n"), m, Tval);
+
+ } /* end of Print */
+
+/***********************************************************************/
+/* Make string output of a int object. */
+/***********************************************************************/
+template <class TYPE>
+void TYPVAL<TYPE>::Print(PGLOBAL g, char *ps, uint z)
+ {
+ if (Null)
+ strcpy(ps, "<null>");
+ else
+ sprintf(ps, Fmt, Tval);
+
+ } /* end of Print */
+
+/* -------------------------- Class STRING --------------------------- */
+
+/***********************************************************************/
+/* STRING public constructor from a constant string. */
+/***********************************************************************/
+TYPVAL<PSZ>::TYPVAL(PSZ s) : VALUE(TYPE_STRING)
+ {
+ Strp = s;
+ Len = strlen(s);
+ Clen = Len;
+ Ci = false;
+ } // end of STRING constructor
+
+/***********************************************************************/
+/* STRING public constructor from char. */
+/***********************************************************************/
+TYPVAL<PSZ>::TYPVAL(PGLOBAL g, PSZ s, int n, int c)
+ : VALUE(TYPE_STRING)
+ {
+ Len = (g) ? n : strlen(s);
+
+ if (!s) {
+ if (g) {
+ Strp = (char *)PlugSubAlloc(g, NULL, Len + 1);
+ Strp[Len] = '\0';
+ } else
+ assert(false);
+
+ } else
+ Strp = s;
+
+ Clen = Len;
+ Ci = (c != 0);
+ } // end of STRING constructor
+
+/***********************************************************************/
+/* Get the tiny value represented by the Strp string. */
+/***********************************************************************/
+char TYPVAL<PSZ>::GetTinyValue(void)
+ {
+ bool m;
+ ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX8, false, &m);
+
+ return (m && val < INT_MAX8) ? (char)(-(signed)val) : (char)val;
+ } // end of GetTinyValue
+
+/***********************************************************************/
+/* Get the unsigned tiny value represented by the Strp string. */
+/***********************************************************************/
+uchar TYPVAL<PSZ>::GetUTinyValue(void)
+ {
+ return (uchar)CharToNumber(Strp, strlen(Strp), UINT_MAX8, true);
+ } // end of GetUTinyValue
+
+/***********************************************************************/
+/* Get the short value represented by the Strp string. */
+/***********************************************************************/
+short TYPVAL<PSZ>::GetShortValue(void)
+ {
+ bool m;
+ ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX16, false, &m);
+
+ return (m && val < INT_MAX16) ? (short)(-(signed)val) : (short)val;
+ } // end of GetShortValue
+
+/***********************************************************************/
+/* Get the unsigned short value represented by the Strp string. */
+/***********************************************************************/
+ushort TYPVAL<PSZ>::GetUShortValue(void)
+ {
+ return (ushort)CharToNumber(Strp, strlen(Strp), UINT_MAX16, true);
+ } // end of GetUshortValue
+
+/***********************************************************************/
+/* Get the integer value represented by the Strp string. */
+/***********************************************************************/
+int TYPVAL<PSZ>::GetIntValue(void)
+ {
+ bool m;
+ ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX32, false, &m);
+
+ return (m && val < INT_MAX32) ? (int)(-(signed)val) : (int)val;
+ } // end of GetIntValue
+
+/***********************************************************************/
+/* Get the unsigned integer value represented by the Strp string. */
+/***********************************************************************/
+uint TYPVAL<PSZ>::GetUIntValue(void)
+ {
+ return (uint)CharToNumber(Strp, strlen(Strp), UINT_MAX32, true);
+ } // end of GetUintValue
+
+/***********************************************************************/
+/* Get the big integer value represented by the Strp string. */
+/***********************************************************************/
+longlong TYPVAL<PSZ>::GetBigintValue(void)
+ {
+ bool m;
+ ulonglong val = CharToNumber(Strp, strlen(Strp), INT_MAX64, false, &m);
+
+ return (m && val < INT_MAX64) ? (-(signed)val) : (longlong)val;
+ } // end of GetBigintValue
+
+/***********************************************************************/
+/* Get the unsigned big integer value represented by the Strp string. */
+/***********************************************************************/
+ulonglong TYPVAL<PSZ>::GetUBigintValue(void)
+ {
+ return CharToNumber(Strp, strlen(Strp), ULONGLONG_MAX, true);
+ } // end of GetUBigintValue
+
+/***********************************************************************/
+/* STRING SetValue: copy the value of another Value object. */
+/***********************************************************************/
+bool TYPVAL<PSZ>::SetValue_pval(PVAL valp, bool chktype)
+ {
+ if (chktype && (valp->GetType() != Type || valp->GetSize() > Len))
+ return true;
+
+ char buf[64];
+
+ if (!(Null = valp->IsNull() && Nullable))
+ strncpy(Strp, valp->GetCharString(buf), Len);
+ else
+ Reset();
+
+ return false;
+ } // end of SetValue_pval
+
+/***********************************************************************/
+/* STRING SetValue: fill string with chars extracted from a line. */
+/***********************************************************************/
+bool TYPVAL<PSZ>::SetValue_char(char *p, int n)
+ {
+ bool rc;
+
+ if (p) {
+ rc = n > Len;
+
+ if ((n = min(n, Len))) {
+ strncpy(Strp, p, n);
+
+// for (p = Strp + n - 1; p >= Strp && (*p == ' ' || *p == '\0'); p--) ;
+ for (p = Strp + n - 1; p >= Strp; p--)
+ if (*p && *p != ' ')
+ break;
+
+ *(++p) = '\0';
+
+ if (trace > 1)
+ htrc(" Setting string to: '%s'\n", Strp);
+
+ } else
+ Reset();
+
+ Null = false;
+ } else {
+ rc = false;
+ Reset();
+ Null = Nullable;
+ } // endif p
+
+ return rc;
+ } // end of SetValue_char
+
+/***********************************************************************/
+/* STRING SetValue: fill string with another string. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue_psz(PSZ s)
+ {
+ if (s) {
+ strncpy(Strp, s, Len);
+ Null = false;
+ } else {
+ Reset();
+ Null = Nullable;
+ } // endif s
+
+ } // end of SetValue_psz
+
+/***********************************************************************/
+/* STRING SetValue: fill string with a string extracted from a block. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue_pvblk(PVBLK blk, int n)
+ {
+ // STRBLK's can return a NULL pointer
+ PSZ vp = blk->GetCharString(Strp, n);
+
+ if (vp != Strp)
+ SetValue_psz(vp);
+
+ } // end of SetValue_pvblk
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of an integer. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(int n)
+ {
+ char buf[16];
+ PGLOBAL& g = Global;
+ int k = sprintf(buf, "%d", n);
+
+ if (k > Len) {
+ sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
+ longjmp(g->jumper[g->jump_level], 138);
+ } else
+ SetValue_psz(buf);
+
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of an uint. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(uint n)
+ {
+ char buf[16];
+ PGLOBAL& g = Global;
+ int k = sprintf(buf, "%u", n);
+
+ if (k > Len) {
+ sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
+ longjmp(g->jumper[g->jump_level], 138);
+ } else
+ SetValue_psz(buf);
+
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a short int. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(short i)
+ {
+ SetValue((int)i);
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a ushort int. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(ushort i)
+ {
+ SetValue((uint)i);
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a big integer.*/
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(longlong n)
+ {
+ char buf[24];
+ PGLOBAL& g = Global;
+ int k = sprintf(buf, "%lld", n);
+
+ if (k > Len) {
+ sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
+ longjmp(g->jumper[g->jump_level], 138);
+ } else
+ SetValue_psz(buf);
+
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a big integer.*/
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(ulonglong n)
+ {
+ char buf[24];
+ PGLOBAL& g = Global;
+ int k = sprintf(buf, "%llu", n);
+
+ if (k > Len) {
+ sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
+ longjmp(g->jumper[g->jump_level], 138);
+ } else
+ SetValue_psz(buf);
+
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a double. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(double f)
+ {
+ char *p, buf[64];
+ PGLOBAL& g = Global;
+ int k = sprintf(buf, "%lf", f);
+
+ for (p = buf + k - 1; p >= buf; p--)
+ if (*p == '0') {
+ *p = 0;
+ k--;
+ } else
+ break;
+
+ if (k > Len) {
+ sprintf(g->Message, MSG(VALSTR_TOO_LONG), buf, Len);
+ longjmp(g->jumper[g->jump_level], 138);
+ } else
+ SetValue_psz(buf);
+
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a tiny int. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(char c)
+ {
+ SetValue((int)c);
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetValue: get the character representation of a tiny int. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetValue(uchar c)
+ {
+ SetValue((uint)c);
+ Null = false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* STRING SetBinValue: fill string with chars extracted from a line. */
+/***********************************************************************/
+void TYPVAL<PSZ>::SetBinValue(void *p)
+ {
+ SetValue_char((char *)p, Len);
+ } // end of SetBinValue
+
+/***********************************************************************/
+/* GetBinValue: fill a buffer with the internal binary value. */
+/* This function checks whether the buffer length is enough and */
+/* returns true if not. Actual filling occurs only if go is true. */
+/* Currently used by WriteColumn of binary files. */
+/***********************************************************************/
+bool TYPVAL<PSZ>::GetBinValue(void *buf, int buflen, bool go)
+ {
+ int len = (Null) ? 0 : strlen(Strp);
+
+ if (len > buflen)
+ return true;
+ else if (go) {
+ memset(buf, ' ', buflen);
+ memcpy(buf, Strp, len);
+ } // endif go
+
+ return false;
+ } // end of GetBinValue
+
+/***********************************************************************/
+/* STRING ShowValue: get string representation of a char value. */
+/***********************************************************************/
+char *TYPVAL<PSZ>::ShowValue(char *buf, int len)
+ {
+ return Strp;
+ } // end of ShowValue
+
+/***********************************************************************/
+/* STRING GetCharString: get string representation of a char value. */
+/***********************************************************************/
+char *TYPVAL<PSZ>::GetCharString(char *p)
+ {
+ return Strp;
+ } // end of GetCharString
+
+/***********************************************************************/
+/* STRING compare value with another Value. */
+/***********************************************************************/
+bool TYPVAL<PSZ>::IsEqual(PVAL vp, bool chktype)
+ {
+ if (this == vp)
+ return true;
+ else if (chktype && Type != vp->GetType())
+ return false;
+ else if (Null || vp->IsNull())
+ return false;
+
+ char buf[64];
+
+ if (Ci || vp->IsCi())
+ return !stricmp(Strp, vp->GetCharString(buf));
+ else // (!Ci)
+ return !strcmp(Strp, vp->GetCharString(buf));
+
+ } // end of IsEqual
+
+/***********************************************************************/
+/* Compare values and returns 1, 0 or -1 according to comparison. */
+/* This function is used for evaluation of numeric filters. */
+/***********************************************************************/
+int TYPVAL<PSZ>::CompareValue(PVAL vp)
+ {
+ int n;
+//assert(vp->GetType() == Type);
+
+ if (trace)
+ htrc(" Comparing: val='%s','%s'\n", Strp, vp->GetCharValue());
+
+ // Process filtering on character strings.
+ if (Ci || vp->IsCi())
+ n = stricmp(Strp, vp->GetCharValue());
+ else
+ n = strcmp(Strp, vp->GetCharValue());
+
+#if defined(WIN32)
+ if (n == _NLSCMPERROR)
+ return n; // Here we should raise an error
+#endif // WIN32
+
+ return (n > 0) ? 1 : (n < 0) ? -1 : 0;
+ } // end of CompareValue
+
+/***********************************************************************/
+/* FormatValue: This function set vp (a STRING value) to the string */
+/* constructed from its own value formated using the fmt format. */
+/* This function assumes that the format matches the value type. */
+/***********************************************************************/
+bool TYPVAL<PSZ>::FormatValue(PVAL vp, char *fmt)
+ {
+ char *buf = (char*)vp->GetTo_Val(); // Should be big enough
+ int n = sprintf(buf, fmt, Strp);
+
+ return (n > vp->GetValLen());
+ } // end of FormatValue
+
+/***********************************************************************/
+/* STRING SetFormat function (used to set SELECT output format). */
+/***********************************************************************/
+bool TYPVAL<PSZ>::SetConstFormat(PGLOBAL g, FORMAT& fmt)
+ {
+ fmt.Type[0] = 'C';
+ fmt.Length = Len;
+ fmt.Prec = 0;
+ return false;
+ } // end of SetConstFormat
+
+/* -------------------------- Class DECIMAL -------------------------- */
+
+/***********************************************************************/
+/* DECIMAL public constructor from a constant string. */
+/***********************************************************************/
+DECVAL::DECVAL(PSZ s) : TYPVAL<PSZ>(s)
+ {
+ if (s) {
+ char *p = strchr(Strp, '.');
+
+ Prec = (p) ? Len - (p - Strp) : 0;
+ } // endif s
+
+ Type = TYPE_DECIM;
+ } // end of DECVAL constructor
+
+/***********************************************************************/
+/* DECIMAL public constructor from char. */
+/***********************************************************************/
+DECVAL::DECVAL(PGLOBAL g, PSZ s, int n, int prec, bool uns)
+ : TYPVAL<PSZ>(g, s, n + (prec ? 1 : 0) + (uns ? 0 : 1), 0)
+ {
+ Prec = prec;
+ Unsigned = uns;
+ Type = TYPE_DECIM;
+ } // end of DECVAL constructor
+
+/***********************************************************************/
+/* DECIMAL: Check whether the numerica value is equal to 0. */
+/***********************************************************************/
+bool DECVAL::IsZero(void)
+ {
+ for (int i = 0; Strp[i]; i++)
+ if (!strchr("0 +-.", Strp[i]))
+ return false;
+
+ return true;
+ } // end of IsZero
+
+/***********************************************************************/
+/* DECIMAL: Reset value to zero. */
+/***********************************************************************/
+void DECVAL::Reset(void)
+{
+ int i = 0;
+
+ Strp[i++] = '0';
+
+ if (Prec) {
+ Strp[i++] = '.';
+
+ do {
+ Strp[i++] = '0';
+ } while (i < Prec + 2);
+
+ } // endif Prec
+
+ Strp[i] = 0;
+} // end of Reset
+
+/***********************************************************************/
+/* DECIMAL ShowValue: get string representation right justified. */
+/***********************************************************************/
+char *DECVAL::ShowValue(char *buf, int len)
+ {
+ sprintf(buf, Xfmt, len, Strp);
+ return buf;
+ } // end of ShowValue
+
+/***********************************************************************/
+/* GetBinValue: fill a buffer with the internal binary value. */
+/* This function checks whether the buffer length is enough and */
+/* returns true if not. Actual filling occurs only if go is true. */
+/* Currently used by WriteColumn of binary files. */
+/***********************************************************************/
+bool DECVAL::GetBinValue(void *buf, int buflen, bool go)
+ {
+ int len = (Null) ? 0 : strlen(Strp);
+
+ if (len > buflen)
+ return true;
+ else if (go) {
+ memset(buf, ' ', buflen - len);
+ memcpy((char*)buf + buflen - len, Strp, len);
+ } // endif go
+
+ return false;
+ } // end of GetBinValue
+
+#if 0
+/***********************************************************************/
+/* DECIMAL SetValue: copy the value of another Value object. */
+/***********************************************************************/
+bool DECVAL::SetValue_pval(PVAL valp, bool chktype)
+ {
+ if (chktype && (valp->GetType() != Type || valp->GetSize() > Len))
+ return true;
+
+ char buf[64];
+
+ if (!(Null = valp->IsNull() && Nullable))
+ strncpy(Strp, valp->GetCharString(buf), Len);
+ else
+ Reset();
+
+ return false;
+ } // end of SetValue_pval
+
+/***********************************************************************/
+/* DECIMAL SetValue: fill string with chars extracted from a line. */
+/***********************************************************************/
+bool DECVAL::SetValue_char(char *p, int n)
+ {
+ bool rc;
+
+ if (p) {
+ rc = n > Len;
+
+ if ((n = min(n, Len))) {
+ strncpy(Strp, p, n);
+
+// for (p = Strp + n - 1; p >= Strp && (*p == ' ' || *p == '\0'); p--) ;
+ for (p = Strp + n - 1; p >= Strp; p--)
+ if (*p && *p != ' ')
+ break;
+
+ *(++p) = '\0';
+
+ if (trace > 1)
+ htrc(" Setting string to: '%s'\n", Strp);
+
+ } else
+ Reset();
+
+ Null = false;
+ } else {
+ rc = false;
+ Reset();
+ Null = Nullable;
+ } // endif p
+
+ return rc;
+ } // end of SetValue_char
+
+/***********************************************************************/
+/* DECIMAL SetValue: fill string with another string. */
+/***********************************************************************/
+void DECVAL::SetValue_psz(PSZ s)
+ {
+ if (s) {
+ strncpy(Strp, s, Len);
+ Null = false;
+ } else {
+ Reset();
+ Null = Nullable;
+ } // endif s
+
+ } // end of SetValue_psz
+
+/***********************************************************************/
+/* DECIMAL SetValue: fill string with a string extracted from a block.*/
+/***********************************************************************/
+void DECVAL::SetValue_pvblk(PVBLK blk, int n)
+ {
+ // STRBLK's can return a NULL pointer
+ SetValue_psz(blk->GetCharValue(n));
+ } // end of SetValue_pvblk
+
+/***********************************************************************/
+/* DECIMAL SetBinValue: fill string with chars extracted from a line. */
+/***********************************************************************/
+void DECVAL::SetBinValue(void *p)
+ {
+ SetValue_char((char *)p, Len);
+ } // end of SetBinValue
+
+/***********************************************************************/
+/* DECIMAL GetCharString: get string representation of a char value. */
+/***********************************************************************/
+char *DECVAL::GetCharString(char *p)
+ {
+ return Strp;
+ } // end of GetCharString
+#endif // 0
+
+/***********************************************************************/
+/* DECIMAL compare value with another Value. */
+/***********************************************************************/
+bool DECVAL::IsEqual(PVAL vp, bool chktype)
+ {
+ if (this == vp)
+ return true;
+ else if (chktype && Type != vp->GetType())
+ return false;
+ else if (Null || vp->IsNull())
+ return false;
+
+ char buf[64];
+
+ return !strcmp(Strp, vp->GetCharString(buf));
+ } // end of IsEqual
+
+/***********************************************************************/
+/* Compare values and returns 1, 0 or -1 according to comparison. */
+/* This function is used for evaluation of numeric filters. */
+/***********************************************************************/
+int DECVAL::CompareValue(PVAL vp)
+ {
+//assert(vp->GetType() == Type);
+
+ // Process filtering on numeric values.
+ double f = atof(Strp), n = vp->GetFloatValue();
+
+//if (trace)
+// htrc(" Comparing: val=%d,%d\n", f, n);
+
+ return (f > n) ? 1 : (f < n) ? (-1) : 0;
+ } // end of CompareValue
+
+#if 0
+/***********************************************************************/
+/* FormatValue: This function set vp (a STRING value) to the string */
+/* constructed from its own value formated using the fmt format. */
+/* This function assumes that the format matches the value type. */
+/***********************************************************************/
+bool DECVAL::FormatValue(PVAL vp, char *fmt)
+ {
+ char *buf = (char*)vp->GetTo_Val(); // Should be big enough
+ int n = sprintf(buf, fmt, Strp);
+
+ return (n > vp->GetValLen());
+ } // end of FormatValue
+
+/***********************************************************************/
+/* DECIMAL SetFormat function (used to set SELECT output format). */
+/***********************************************************************/
+bool DECVAL::SetConstFormat(PGLOBAL g, FORMAT& fmt)
+ {
+ fmt.Type[0] = 'C';
+ fmt.Length = Len;
+ fmt.Prec = 0;
+ return false;
+ } // end of SetConstFormat
+#endif // 0
+
+/* -------------------------- Class DTVAL ---------------------------- */
+
+/***********************************************************************/
+/* DTVAL public constructor for new void values. */
+/***********************************************************************/
+DTVAL::DTVAL(PGLOBAL g, int n, int prec, PSZ fmt)
+ : TYPVAL<int>((int)0, TYPE_DATE)
+ {
+ if (!fmt) {
+ Pdtp = NULL;
+ Sdate = NULL;
+ DefYear = 0;
+ Len = n;
+ } else
+ SetFormat(g, fmt, n, prec);
+
+//Type = TYPE_DATE;
+ } // end of DTVAL constructor
+
+/***********************************************************************/
+/* DTVAL public constructor from int. */
+/***********************************************************************/
+DTVAL::DTVAL(PGLOBAL g, int n) : TYPVAL<int>(n, TYPE_DATE)
+ {
+ Pdtp = NULL;
+ Len = 19;
+//Type = TYPE_DATE;
+ Sdate = NULL;
+ DefYear = 0;
+ } // end of DTVAL constructor
+
+/***********************************************************************/
+/* Set format so formatted dates can be converted on input/output. */
+/***********************************************************************/
+bool DTVAL::SetFormat(PGLOBAL g, PSZ fmt, int len, int year)
+ {
+ Pdtp = MakeDateFormat(g, fmt, true, true, (year > 9999) ? 1 : 0);
+ Sdate = (char*)PlugSubAlloc(g, NULL, len + 1);
+ DefYear = (int)((year > 9999) ? (year - 10000) : year);
+ Len = len;
+ return false;
+ } // end of SetFormat
+
+/***********************************************************************/
+/* Set format from the format of another date value. */
+/***********************************************************************/
+bool DTVAL::SetFormat(PGLOBAL g, PVAL valp)
+ {
+ DTVAL *vp;
+
+ if (valp->GetType() != TYPE_DATE) {
+ sprintf(g->Message, MSG(NO_FORMAT_TYPE), valp->GetType());
+ return true;
+ } else
+ vp = (DTVAL*)valp;
+
+ Len = vp->Len;
+ Pdtp = vp->Pdtp;
+ Sdate = (char*)PlugSubAlloc(g, NULL, Len + 1);
+ DefYear = vp->DefYear;
+ return false;
+ } // end of SetFormat
+
+/***********************************************************************/
+/* We need TimeShift because the mktime C function does a correction */
+/* for local time zone that we want to override for DB operations. */
+/***********************************************************************/
+void DTVAL::SetTimeShift(void)
+ {
+ struct tm dtm;
+ memset(&dtm, 0, sizeof(dtm));
+ dtm.tm_mday=2;
+ dtm.tm_mon=0;
+ dtm.tm_year=70;
+
+ Shift = (int)mktime(&dtm) - 86400;
+
+ if (trace)
+ htrc("DTVAL Shift=%d\n", Shift);
+
+ } // end of SetTimeShift
+
+// Added by Alexander Barkov
+static void TIME_to_localtime(struct tm *tm, const MYSQL_TIME *ltime)
+{
+ bzero(tm, sizeof(*tm));
+ tm->tm_year= ltime->year - 1900;
+ tm->tm_mon= ltime->month - 1;
+ tm->tm_mday= ltime->day;
+ tm->tm_hour= ltime->hour;
+ tm->tm_min= ltime->minute;
+ tm->tm_sec= ltime->second;
+}
+
+// Added by Alexander Barkov
+static struct tm *gmtime_mysql(const time_t *timep, struct tm *tm)
+{
+ MYSQL_TIME ltime;
+ thd_gmt_sec_to_TIME(current_thd, &ltime, (my_time_t) *timep);
+ TIME_to_localtime(tm, &ltime);
+ return tm;
+}
+
+/***********************************************************************/
+/* GetGmTime: returns a pointer to a static tm structure obtained */
+/* though the gmtime C function. The purpose of this function is to */
+/* extend the range of valid dates by accepting negative time values. */
+/***********************************************************************/
+struct tm *DTVAL::GetGmTime(struct tm *tm_buffer)
+ {
+ struct tm *datm;
+ time_t t = (time_t)Tval;
+
+ if (Tval < 0) {
+ int n;
+
+ for (n = 0; t < 0; n += 4)
+ t += FOURYEARS;
+
+ datm = gmtime_mysql(&t, tm_buffer);
+
+ if (datm)
+ datm->tm_year -= n;
+
+ } else
+ datm = gmtime_mysql(&t, tm_buffer);
+
+ return datm;
+ } // end of GetGmTime
+
+// Added by Alexander Barkov
+static time_t mktime_mysql(struct tm *ptm)
+{
+ MYSQL_TIME ltime;
+ localtime_to_TIME(&ltime, ptm);
+ ltime.time_type= MYSQL_TIMESTAMP_DATETIME;
+ uint error_code;
+ time_t t= TIME_to_timestamp(current_thd, &ltime, &error_code);
+ return error_code ? (time_t) -1 : t;
+}
+
+/***********************************************************************/
+/* MakeTime: calculates a date value from a tm structures using the */
+/* mktime C function. The purpose of this function is to extend the */
+/* range of valid dates by accepting to set negative time values. */
+/***********************************************************************/
+bool DTVAL::MakeTime(struct tm *ptm)
+ {
+ int n, y = ptm->tm_year;
+ time_t t = mktime_mysql(ptm);
+
+ if (trace > 1)
+ htrc("MakeTime from (%d,%d,%d,%d,%d,%d)\n",
+ ptm->tm_year, ptm->tm_mon, ptm->tm_mday,
+ ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
+
+ if (t == -1) {
+ if (y < 1 || y > 71)
+ return true;
+
+ for (n = 0; t == -1 && n < 20; n++) {
+ ptm->tm_year += 4;
+ t = mktime_mysql(ptm);
+ } // endfor t
+
+ if (t == -1)
+ return true;
+
+ if ((t -= (n * FOURYEARS)) > 2000000000)
+ return true;
+
+ }
+ Tval= (int) t;
+
+ if (trace > 1)
+ htrc("MakeTime Ival=%d\n", Tval);
+
+ return false;
+ } // end of MakeTime
+
+/***********************************************************************/
+/* Make a time_t datetime from its components (YY, MM, DD, hh, mm, ss) */
+/***********************************************************************/
+bool DTVAL::MakeDate(PGLOBAL g, int *val, int nval)
+ {
+ int i, m;
+ int n;
+ bool rc = false;
+ struct tm datm;
+ bzero(&datm, sizeof(datm));
+ datm.tm_mday=1;
+ datm.tm_mon=0;
+ datm.tm_year=70;
+
+ if (trace > 1)
+ htrc("MakeDate from(%d,%d,%d,%d,%d,%d) nval=%d\n",
+ val[0], val[1], val[2], val[3], val[4], val[5], nval);
+
+ for (i = 0; i < nval; i++) {
+ n = val[i];
+
+// if (trace > 1)
+// htrc("i=%d n=%d\n", i, n);
+
+ switch (i) {
+ case 0:
+ if (n >= 1900)
+ n -= 1900;
+
+ datm.tm_year = n;
+
+// if (trace > 1)
+// htrc("n=%d tm_year=%d\n", n, datm.tm_year);
+
+ break;
+ case 1:
+ // If mktime handles apparently correctly large or negative
+ // day values, it is not the same for months. Therefore we
+ // do the ajustment here, thus mktime has not to do it.
+ if (n > 0) {
+ m = (n - 1) % 12;
+ n = (n - 1) / 12;
+ } else {
+ m = 11 + n % 12;
+ n = n / 12 - 1;
+ } // endfi n
+
+ datm.tm_mon = m;
+ datm.tm_year += n;
+
+// if (trace > 1)
+// htrc("n=%d m=%d tm_year=%d tm_mon=%d\n", n, m, datm.tm_year, datm.tm_mon);
+
+ break;
+ case 2:
+ // For days, big or negative values may also cause problems
+ m = n % 1461;
+ n = 4 * (n / 1461);
+
+ if (m < 0) {
+ m += 1461;
+ n -= 4;
+ } // endif m
+
+ datm.tm_mday = m;
+ datm.tm_year += n;
+
+// if (trace > 1)
+// htrc("n=%d m=%d tm_year=%d tm_mon=%d\n", n, m, datm.tm_year, datm.tm_mon);
+
+ break;
+ case 3: datm.tm_hour = n; break;
+ case 4: datm.tm_min = n; break;
+ case 5: datm.tm_sec = n; break;
+ } // endswitch i
+
+ } // endfor i
+
+ if (trace > 1)
+ htrc("MakeDate datm=(%d,%d,%d,%d,%d,%d)\n",
+ datm.tm_year, datm.tm_mon, datm.tm_mday,
+ datm.tm_hour, datm.tm_min, datm.tm_sec);
+
+ // Pass g to have an error return or NULL to set invalid dates to 0
+ if (MakeTime(&datm))
+ if (g) {
+ strcpy(g->Message, MSG(BAD_DATETIME));
+ rc = true;
+ } else
+ Tval = 0;
+
+ return rc;
+ } // end of MakeDate
+
+/***********************************************************************/
+/* DTVAL SetValue: copy the value of another Value object. */
+/* This function allows conversion if chktype is false. */
+/***********************************************************************/
+bool DTVAL::SetValue_pval(PVAL valp, bool chktype)
+ {
+ if (chktype && Type != valp->GetType())
+ return true;
+
+ if (!(Null = valp->IsNull() && Nullable)) {
+ if (Pdtp && !valp->IsTypeNum()) {
+ int ndv;
+ int dval[6];
+
+ ndv = ExtractDate(valp->GetCharValue(), Pdtp, DefYear, dval);
+ MakeDate(NULL, dval, ndv);
+ } else
+ Tval = valp->GetIntValue();
+
+ } else
+ Reset();
+
+ return false;
+ } // end of SetValue
+
+/***********************************************************************/
+/* SetValue: convert chars extracted from a line to date value. */
+/***********************************************************************/
+bool DTVAL::SetValue_char(char *p, int n)
+ {
+ bool rc;
+
+ if (Pdtp) {
+ char *p2;
+ int ndv;
+ int dval[6];
+
+ // Trim trailing blanks
+ for (p2 = p + n -1; p < p2 && *p2 == ' '; p2--) ;
+
+ if ((rc = (n = p2 - p + 1) > Len))
+ n = Len;
+
+ memcpy(Sdate, p, n);
+ Sdate[n] = '\0';
+
+ ndv = ExtractDate(Sdate, Pdtp, DefYear, dval);
+ MakeDate(NULL, dval, ndv);
+
+ if (trace > 1)
+ htrc(" setting date: '%s' -> %d\n", Sdate, Tval);
+
+ Null = false;
+ } else
+ rc = TYPVAL<int>::SetValue_char(p, n);
+
+ return rc;
+ } // end of SetValue
+
+/***********************************************************************/
+/* SetValue: convert a char string to date value. */
+/***********************************************************************/
+void DTVAL::SetValue_psz(PSZ p)
+ {
+ if (Pdtp) {
+ int ndv;
+ int dval[6];
+
+ strncpy(Sdate, p, Len);
+ Sdate[Len] = '\0';
+
+ ndv = ExtractDate(Sdate, Pdtp, DefYear, dval);
+ MakeDate(NULL, dval, ndv);
+
+ if (trace > 1)
+ htrc(" setting date: '%s' -> %d\n", Sdate, Tval);
+
+ Null = false;
+ } else
+ TYPVAL<int>::SetValue_psz(p);
+
+ } // end of SetValue
+
+/***********************************************************************/
+/* DTVAL SetValue: set value with a value extracted from a block. */
+/***********************************************************************/
+void DTVAL::SetValue_pvblk(PVBLK blk, int n)
+ {
+ if (Pdtp && !::IsTypeNum(blk->GetType())) {
+ int ndv;
+ int dval[6];
+
+ ndv = ExtractDate(blk->GetCharValue(n), Pdtp, DefYear, dval);
+ MakeDate(NULL, dval, ndv);
+ } else
+ Tval = blk->GetIntValue(n);
+
+ } // end of SetValue
+
+/***********************************************************************/
+/* DTVAL GetCharString: get string representation of a date value. */
+/***********************************************************************/
+char *DTVAL::GetCharString(char *p)
+ {
+ if (Pdtp) {
+ size_t n = 0;
+ struct tm tm, *ptm= GetGmTime(&tm);
+
+ if (ptm)
+ n = strftime(Sdate, Len + 1, Pdtp->OutFmt, ptm);
+
+ if (!n) {
+ *Sdate = '\0';
+ strncat(Sdate, "Error", Len + 1);
+ } // endif n
+
+ return Sdate;
+ } else
+ sprintf(p, "%d", Tval);
+
+ Null = false;
+ return p;
+ } // end of GetCharString
+
+/***********************************************************************/
+/* DTVAL ShowValue: get string representation of a date value. */
+/***********************************************************************/
+char *DTVAL::ShowValue(char *buf, int len)
+ {
+ if (Pdtp) {
+ char *p;
+ size_t m, n = 0;
+ struct tm tm, *ptm = GetGmTime(&tm);
+
+ if (Len < len) {
+ p = buf;
+ m = len;
+ } else {
+ p = Sdate;
+ m = Len + 1;
+ } // endif Len
+
+ if (ptm)
+ n = strftime(p, m, Pdtp->OutFmt, ptm);
+
+ if (!n) {
+ *p = '\0';
+ strncat(p, "Error", m);
+ } // endif n
+
+ return p;
+ } else
+ return TYPVAL<int>::ShowValue(buf, len);
+
+ } // end of ShowValue
+
+#if 0 // Not used by CONNECT
+/***********************************************************************/
+/* Returns a member of the struct tm representation of the date. */
+/***********************************************************************/
+bool DTVAL::GetTmMember(OPVAL op, int& mval)
+ {
+ bool rc = false;
+ struct tm tm, *ptm = GetGmTime(&tm);
+
+ switch (op) {
+ case OP_MDAY: mval = ptm->tm_mday; break;
+ case OP_MONTH: mval = ptm->tm_mon + 1; break;
+ case OP_YEAR: mval = ptm->tm_year + 1900; break;
+ case OP_WDAY: mval = ptm->tm_wday + 1; break;
+ case OP_YDAY: mval = ptm->tm_yday + 1; break;
+ case OP_QUART: mval = ptm->tm_mon / 3 + 1; break;
+ default:
+ rc = true;
+ } // endswitch op
+
+ return rc;
+ } // end of GetTmMember
+
+/***********************************************************************/
+/* Calculates the week number of the year for the internal date value.*/
+/* The International Standard ISO 8601 has decreed that Monday shall */
+/* be the first day of the week. A week that lies partly in one year */
+/* and partly in another is assigned a number in the year in which */
+/* most of its days lie. That means that week number 1 of any year is */
+/* the week that contains the January 4th. */
+/***********************************************************************/
+bool DTVAL::WeekNum(PGLOBAL g, int& nval)
+ {
+ // w is the start of the week SUN=0, MON=1, etc.
+ int m, n, w = nval % 7;
+ struct tm tm, *ptm = GetGmTime(&tm);
+
+ // Which day is January 4th of this year?
+ m = (367 + ptm->tm_wday - ptm->tm_yday) % 7;
+
+ // When does the first week begins?
+ n = 3 - (7 + m - w) % 7;
+
+ // Now calculate the week number
+ if (!(nval = (7 + ptm->tm_yday - n) / 7))
+ nval = 52;
+
+ // Everything should be Ok
+ return false;
+ } // end of WeekNum
+#endif // 0
+
+/***********************************************************************/
+/* FormatValue: This function set vp (a STRING value) to the string */
+/* constructed from its own value formated using the fmt format. */
+/* This function assumes that the format matches the value type. */
+/***********************************************************************/
+bool DTVAL::FormatValue(PVAL vp, char *fmt)
+ {
+ char *buf = (char*)vp->GetTo_Val(); // Should be big enough
+ struct tm tm, *ptm = GetGmTime(&tm);
+
+ if (trace > 1)
+ htrc("FormatValue: ptm=%p len=%d\n", ptm, vp->GetValLen());
+
+ if (ptm) {
+ size_t n = strftime(buf, vp->GetValLen(), fmt, ptm);
+
+ if (trace > 1)
+ htrc("strftime: n=%d buf=%s\n", n, (n) ? buf : "???");
+
+ return (n == 0);
+ } else
+ return true;
+
+ } // end of FormatValue
+
+/* -------------------------- End of Value --------------------------- */
diff --git a/storage/connect/xindex.cpp b/storage/connect/xindex.cpp
index 3037b0a829a..caa7c667419 100755
--- a/storage/connect/xindex.cpp
+++ b/storage/connect/xindex.cpp
@@ -1,3028 +1,3010 @@
-/***************** Xindex C++ Class Xindex Code (.CPP) *****************/
-/* Name: XINDEX.CPP Version 2.8 */
-/* */
-/* (C) Copyright to the author Olivier BERTRAND 2004-2013 */
-/* */
-/* This file contains the class XINDEX implementation code. */
-/***********************************************************************/
-
-/***********************************************************************/
-/* Include relevant sections of the System header files. */
-/***********************************************************************/
-#include "my_global.h"
-#if defined(WIN32)
-#include <io.h>
-#include <fcntl.h>
-#include <errno.h>
-//#include <windows.h>
-#else // !WIN32
-#if defined(UNIX)
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <unistd.h>
-#else // !UNIX
-#include <io.h>
-#endif // !UNIX
-#include <fcntl.h>
-#endif // !WIN32
-
-/***********************************************************************/
-/* Include required application header files */
-/* global.h is header containing all global Plug declarations. */
-/* plgdbsem.h is header containing the DB applic. declarations. */
-/* kindex.h is header containing the KINDEX class definition. */
-/***********************************************************************/
-#include "global.h"
-#include "plgdbsem.h"
-#include "osutil.h"
-#include "maputil.h"
-//nclude "filter.h"
-#include "tabcol.h"
-#include "xindex.h"
-#include "xobject.h"
-//nclude "scalfnc.h"
-//nclude "array.h"
-#include "filamtxt.h"
-#include "tabdos.h"
-
-/***********************************************************************/
-/* Macro or external routine definition */
-/***********************************************************************/
-#define NZ 7
-#define NW 5
-#define MAX_INDX 10
-#ifndef INVALID_SET_FILE_POINTER
-#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
-#endif
-
-/***********************************************************************/
-/* DB static external variables. */
-/***********************************************************************/
-extern MBLOCK Nmblk; /* Used to initialize MBLOCK's */
-
-/***********************************************************************/
-/* Last two parameters are true to enable type checking, and last one */
-/* to have rows filled by blanks to be compatible with QRY blocks. */
-/***********************************************************************/
-PVBLK AllocValBlock(PGLOBAL, void *, int, int, int, int,
- bool check = true, bool blank = true, bool un = false);
-
-/***********************************************************************/
-/* Check whether we have to create/update permanent indexes. */
-/***********************************************************************/
-int PlgMakeIndex(PGLOBAL g, PSZ name, PIXDEF pxdf, bool add)
- {
- int rc;
- PTABLE tablep;
- PTDBDOS tdbp;
- PCATLG cat = PlgGetCatalog(g, true);
-
- /*********************************************************************/
- /* Open a new table in mode read and with only the keys columns. */
- /*********************************************************************/
- tablep = new(g) XTAB(name);
-
- if (!(tdbp = (PTDBDOS)cat->GetTable(g, tablep)))
- rc = RC_NF;
- else if (!tdbp->GetDef()->Indexable()) {
- sprintf(g->Message, MSG(TABLE_NO_INDEX), name);
- rc = RC_NF;
- } else if ((rc = tdbp->MakeIndex(g, pxdf, add)) == RC_INFO)
- rc = RC_OK; // No index
-
- return rc;
- } // end of PlgMakeIndex
-
-/* -------------------------- Class INDEXDEF ------------------------- */
-
-/***********************************************************************/
-/* INDEXDEF Constructor. */
-/***********************************************************************/
-INDEXDEF::INDEXDEF(char *name, bool uniq, int n)
- {
-//To_Def = NULL;
- Next = NULL;
- ToKeyParts = NULL;
- Name = name;
- Unique = uniq;
- Invalid = false;
- AutoInc = false;
- Nparts = 0;
- ID = n;
-//Offset = 0;
-//Offhigh = 0;
-//Size = 0;
- MaxSame = 1;
- } // end of INDEXDEF constructor
-
-/***********************************************************************/
-/* Set the max same values for each colum after making the index. */
-/***********************************************************************/
-void INDEXDEF::SetMxsame(PXINDEX x)
- {
- PKPDEF kdp;
- PXCOL xcp;
-
- for (kdp = ToKeyParts, xcp = x->To_KeyCol;
- kdp && xcp; kdp = kdp->Next, xcp = xcp->Next)
- kdp->Mxsame = xcp->Mxs;
- } // end of SetMxsame
-
-/* -------------------------- Class KPARTDEF ------------------------- */
-
-/***********************************************************************/
-/* KPARTDEF Constructor. */
-/***********************************************************************/
-KPARTDEF::KPARTDEF(PSZ name, int n)
- {
- Next = NULL;
- Name = name;
- Mxsame = 0;
- Ncol = n;
- Klen = 0;
- } // end of KPARTDEF constructor
-
-/* -------------------------- XXBASE Class --------------------------- */
-
-/***********************************************************************/
-/* XXBASE public constructor. */
-/***********************************************************************/
-XXBASE::XXBASE(PTDBDOS tbxp, bool b) : CSORT(b),
- To_Rec((int*&)Record.Memp)
- {
- Tbxp = tbxp;
- Record = Nmblk;
- Cur_K = -1;
- Old_K = -1;
- Num_K = 0;
- Ndif = 0;
- Bot = Top = Inf = Sup = 0;
- Op = OP_EQ;
- To_KeyCol = NULL;
- Mul = false;
- Val_K = -1;
- Nblk = Sblk = 0;
- Thresh = 7;
- ID = -1;
- Nth = 0;
- } // end of XXBASE constructor
-
-/***********************************************************************/
-/* Make file output of XINDEX contents. */
-/***********************************************************************/
-void XXBASE::Print(PGLOBAL g, FILE *f, uint n)
- {
- char m[64];
-
- memset(m, ' ', n); // Make margin string
- m[n] = '\0';
- fprintf(f, "%sXINDEX: Tbxp=%p Num=%d\n", m, Tbxp, Num_K);
- } // end of Print
-
-/***********************************************************************/
-/* Make string output of XINDEX contents. */
-/***********************************************************************/
-void XXBASE::Print(PGLOBAL g, char *ps, uint z)
- {
- *ps = '\0';
- strncat(ps, "Xindex", z);
- } // end of Print
-
-/* -------------------------- XINDEX Class --------------------------- */
-
-/***********************************************************************/
-/* XINDEX public constructor. */
-/***********************************************************************/
-XINDEX::XINDEX(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp, int k)
- : XXBASE(tdbp, !xdp->IsUnique())
- {
- Xdp = xdp;
- ID = xdp->GetID();
- Tdbp = tdbp;
- X = pxp;
- To_LastCol = NULL;
- To_LastVal = NULL;
- To_Cols = cp;
- To_Vals = xp;
- Mul = !xdp->IsUnique();
- Srtd = false;
- Nk = xdp->GetNparts();
- Nval = (k) ? k : Nk;
- Incr = 0;
-//Defoff = xdp->GetOffset();
-//Defhigh = xdp->GetOffhigh();
-//Size = xdp->GetSize();
- MaxSame = xdp->GetMaxSame();
- } // end of XINDEX constructor
-
-/***********************************************************************/
-/* XINDEX Reset: re-initialize a Xindex block. */
-/***********************************************************************/
-void XINDEX::Reset(void)
- {
- for (PXCOL kp = To_KeyCol; kp; kp = kp->Next)
- kp->Val_K = kp->Ndf;
-
- Cur_K = Num_K;
- Old_K = -1; // Needed to avoid not setting CurBlk for Update
- Op = (Op == OP_FIRST || Op == OP_NEXT) ? OP_FIRST :
- (Op == OP_FSTDIF || Op == OP_NXTDIF) ? OP_FSTDIF : OP_EQ;
- Nth = 0;
- } // end of Reset
-
-/***********************************************************************/
-/* XINDEX Close: terminate index and free all allocated data. */
-/* Do not reset other values that are used at return to make. */
-/***********************************************************************/
-void XINDEX::Close(void)
- {
- // Close file or view of file
- X->Close();
-
- // De-allocate data
- PlgDBfree(Record);
- PlgDBfree(Index);
- PlgDBfree(Offset);
-
- // De-allocate Key data
- for (PXCOL kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->FreeData();
-
- // Column values cannot be retrieved from key anymore
- for (int k = 0; k < Nk; k++)
- To_Cols[k]->SetKcol(NULL);
-
- } // end of Close
-
-/***********************************************************************/
-/* XINDEX compare routine for C Quick/Insertion sort. */
-/***********************************************************************/
-int XINDEX::Qcompare(int *i1, int *i2)
- {
- register int k;
- register PXCOL kcp;
-
- for (kcp = To_KeyCol, k = 0; kcp; kcp = kcp->Next)
- if ((k = kcp->Compare(*i1, *i2)))
- break;
-
-#ifdef DEBTRACE
- num_comp++;
-#endif
-
- return k;
- } // end of Qcompare
-
-/***********************************************************************/
-/* Make: Make and index on key column(s). */
-/***********************************************************************/
-bool XINDEX::Make(PGLOBAL g, PIXDEF sxp)
- {
- /*********************************************************************/
- /* Table can be accessed through an index. */
- /*********************************************************************/
- int k, rc = RC_OK;
- int *bof, i, j, n, ndf, nkey;
- PKPDEF kdfp = Xdp->GetToKeyParts();
- bool brc = true;
- PCOL colp;
- PXCOL kp, prev = NULL, kcp = NULL;
- PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
-
- /*********************************************************************/
- /* Allocate the storage that will contain the keys and the file */
- /* positions corresponding to them. */
- /*********************************************************************/
- if ((n = Tdbp->GetMaxSize(g)) < 0)
- return true;
- else if (!n) {
- Num_K = Ndif = 0;
- MaxSame = 1;
-
- // The if condition was suppressed because this may be an existing
- // index that is now void because all table lines were deleted.
-// if (sxp)
- goto nox; // Truncate eventually existing index file
-// else
-// return false;
-
- } // endif n
-
- // File position must be stored
- Record.Size = n * sizeof(int);
-
- if (!PlgDBalloc(g, NULL, Record)) {
- sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", n);
- goto err; // Error
- } // endif
-
- /*********************************************************************/
- /* Allocate the KXYCOL blocks used to store column values. */
- /*********************************************************************/
- for (k = 0; k < Nk; k++) {
- colp = To_Cols[k];
-
- if (!kdfp) {
- sprintf(g->Message, MSG(INT_COL_ERROR),
- (colp) ? colp->GetName() : "???");
- goto err; // Error
- } // endif kdfp
-
- kcp = new(g) KXYCOL(this);
-
- if (kcp->Init(g, colp, n, true, kdfp->Klen))
- goto err; // Error
-
- if (prev) {
- kcp->Previous = prev;
- prev->Next = kcp;
- } else
- To_KeyCol = kcp;
-
- prev = kcp;
- kdfp = kdfp->Next;
- } // endfor k
-
- To_LastCol = prev;
-
- /*********************************************************************/
- /* Get the starting information for progress. */
- /*********************************************************************/
- dup->Step = (char*)PlugSubAlloc(g, NULL, 128);
- sprintf((char*)dup->Step, MSG(BUILD_INDEX), Xdp->GetName(), Tdbp->Name);
- dup->ProgMax = Tdbp->GetProgMax(g);
- dup->ProgCur = 0;
-
- /*********************************************************************/
- /* Standard init: read the file and construct the index table. */
- /* Note: reading will be sequential as To_Kindex is not set. */
- /*********************************************************************/
- for (i = nkey = 0; i < n && rc != RC_EF; i++) {
-#if defined(THREAD)
- if (!dup->Step) {
- strcpy(g->Message, MSG(QUERY_CANCELLED));
- longjmp(g->jumper[g->jump_level], 99);
- } // endif Step
-#endif // THREAD
-
- /*******************************************************************/
- /* Read a valid record from table file. */
- /*******************************************************************/
- rc = Tdbp->ReadDB(g);
-
- // Update progress information
- dup->ProgCur = Tdbp->GetProgCur();
-
- // Check return code and do whatever must be done according to it
- switch (rc) {
- case RC_OK:
- break;
- case RC_EF:
- goto end_of_file;
- case RC_NF:
- continue;
- default:
- sprintf(g->Message, MSG(RC_READING), rc, Tdbp->Name);
- goto err;
- } // endswitch rc
-
- /*******************************************************************/
- /* Get and Store the file position of the last read record for */
- /* future direct access. */
- /*******************************************************************/
- To_Rec[nkey] = Tdbp->GetRecpos();
-
- /*******************************************************************/
- /* Get the keys and place them in the key blocks. */
- /*******************************************************************/
- for (k = 0, kcp = To_KeyCol;
- k < Nk && kcp;
- k++, kcp = kcp->Next) {
- colp = To_Cols[k];
- colp->Reset();
-
- colp->ReadColumn(g);
-// if (colp->ReadColumn(g))
-// goto err;
-
- kcp->SetValue(colp, nkey);
- } // endfor k
-
- nkey++; // A new valid key was found
- } // endfor i
-
- end_of_file:
-
- // Update progress information
- dup->ProgCur = Tdbp->GetProgMax(g);
-
- /*********************************************************************/
- /* Record the Index size and eventually resize memory allocation. */
- /*********************************************************************/
- if ((Num_K = nkey) < n) {
- PlgDBrealloc(g, NULL, Record, Num_K * sizeof(int));
-
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->ReAlloc(g, Num_K);
-
- } // endif Num_K
-
- /*********************************************************************/
- /* Sort the index so we can use an optimized Find algorithm. */
- /* Note: for a unique index we use the non conservative sort */
- /* version because normally all index values are different. */
- /* This was set at CSORT class construction. */
- /* For all indexes, an offset array is made so we can check the */
- /* uniqueness of unique indexes. */
- /*********************************************************************/
- Index.Size = Num_K * sizeof(int);
-
- if (!PlgDBalloc(g, NULL, Index)) {
- sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
- goto err; // Error
- } // endif alloc
-
- Offset.Size = (Num_K + 1) * sizeof(int);
-
- if (!PlgDBalloc(g, NULL, Offset)) {
- sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Num_K + 1);
- goto err; // Error
- } // endif alloc
-
- // Call the sort program, it returns the number of distinct values
- if ((Ndif = Qsort(g, Num_K)) < 0)
- goto err; // Error during sort
-
- // Check whether the unique index is unique indeed
- if (!Mul)
- if (Ndif < Num_K) {
- strcpy(g->Message, MSG(INDEX_NOT_UNIQ));
- goto err;
- } else
- PlgDBfree(Offset); // Not used anymore
-
- // Use the index to physically reorder the xindex
- Srtd = Reorder(g);
-
- if (Ndif < Num_K) {
- // Resize the offset array
- PlgDBrealloc(g, NULL, Offset, (Ndif + 1) * sizeof(int));
-
- // Initial value of MaxSame
- MaxSame = Pof[1] - Pof[0];
-
- // Resize the Key array by only keeping the distinct values
- for (i = 1; i < Ndif; i++) {
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->Move(i, Pof[i]);
-
- MaxSame = max(MaxSame, Pof[i + 1] - Pof[i]);
- } // endfor i
-
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->ReAlloc(g, Ndif);
-
- } else {
- Mul = false; // Current index is unique
- PlgDBfree(Offset); // Not used anymore
- MaxSame = 1; // Reset it when remaking an index
- } // endif Ndif
-
- /*********************************************************************/
- /* Now do the reduction of the index. Indeed a multi-column index */
- /* can be used for only some of the first columns. For instance if */
- /* an index is defined for column A, B, C PlugDB can use it for */
- /* only the column A or the columns A, B. */
- /* What we do here is to reduce the data so column A will contain */
- /* only the sorted distinct values of A, B will contain data such */
- /* as only distinct values of A,B are stored etc. */
- /* This implies that for each column set an offset array is made */
- /* except if the subset originally contains unique values. */
- /*********************************************************************/
- // Update progress information
- dup->Step = STEP(REDUCE_INDEX);
-
- ndf = Ndif;
- To_LastCol->Mxs = MaxSame;
-
- for (kcp = To_LastCol->Previous; kcp; kcp = kcp->Previous) {
- if (!(bof = kcp->MakeOffset(g, ndf)))
- goto err;
- else
- *bof = 0;
-
- for (n = 0, i = j = 1; i < ndf; i++)
- for (kp = kcp; kp; kp = kp->Previous)
- if (kp->Compare(n, i)) {
- // Values are not equal to last ones
- bof[j++] = n = i;
- break;
- } // endif Compare
-
- if (j < ndf) {
- // Sub-index is multiple
- bof[j] = ndf;
- ndf = j; // New number of distinct values
-
- // Resize the Key array by only keeping the distinct values
- for (kp = kcp; kp; kp = kp->Previous) {
- for (i = 1; i < ndf; i++)
- kp->Move(i, bof[i]);
-
- kp->ReAlloc(g, ndf);
- } // endif kcp
-
- // Resize the offset array
- kcp->MakeOffset(g, ndf);
-
- // Calculate the max same value for this column
- kcp->Mxs = ColMaxSame(kcp);
- } else {
- // Current sub-index is unique
- kcp->MakeOffset(g, 0); // The offset is not used anymore
- kcp->Mxs = 1; // Unique
- } // endif j
-
- } // endfor kcp
-
- /*********************************************************************/
- /* For sorted columns and fixed record size, file position can be */
- /* calculated, so the Record array can be discarted. */
- /* Note: for Num_K = 1 any non null value is Ok. */
- /*********************************************************************/
- if (Srtd && Tdbp->Ftype != RECFM_VAR) {
- Incr = (Num_K > 1) ? To_Rec[1] : Num_K;
- PlgDBfree(Record);
- } // endif Srtd
-
- /*********************************************************************/
- /* Check whether a two-tier find algorithm can be implemented. */
- /* It is currently implemented only for single key indexes. */
- /*********************************************************************/
- if (Nk == 1 && ndf >= 65536) {
- // Implement a two-tier find algorithm
- for (Sblk = 256; (Sblk * Sblk * 4) < ndf; Sblk *= 2) ;
-
- Nblk = (ndf -1) / Sblk + 1;
-
- if (To_KeyCol->MakeBlockArray(g, Nblk, Sblk))
- goto err; // Error
-
- } // endif Num_K
-
- nox:
- /*********************************************************************/
- /* No valid record read yet for secondary file. */
- /*********************************************************************/
- Cur_K = Num_K;
-
- /*********************************************************************/
- /* Save the index so it has not to be recalculated. */
- /*********************************************************************/
- if (!SaveIndex(g, sxp))
- brc = false;
-
- err:
- // We don't need the index anymore
- Close();
-
- if (brc)
- printf("%s\n", g->Message);
-
- return brc;
- } // end of Make
-
-/***********************************************************************/
-/* Return the max size of the intermediate column. */
-/***********************************************************************/
-int XINDEX::ColMaxSame(PXCOL kp)
- {
- int *kof, i, ck1, ck2, ckn = 1;
- PXCOL kcp;
-
- // Calculate the max same value for this column
- for (i = 0; i < kp->Ndf; i++) {
- ck1 = i;
- ck2 = i + 1;
-
- for (kcp = kp; kcp; kcp = kcp->Next) {
- if (!(kof = (kcp->Next) ? kcp->Kof : Pof))
- break;
-
- ck1 = kof[ck1];
- ck2 = kof[ck2];
- } // endfor kcp
-
- ckn = max(ckn, ck2 - ck1);
- } // endfor i
-
- return ckn;
- } // end of ColMaxSame
-
-/***********************************************************************/
-/* Reorder: use the sort index to reorder the data in storage so */
-/* it will be physically sorted and sort index can be removed. */
-/***********************************************************************/
-bool XINDEX::Reorder(PGLOBAL g)
- {
- register int i, j, k, n;
- bool sorted = true;
- PXCOL kcp;
- PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
-
- if (Num_K > 500000) {
- // Update progress information
- dup->Step = STEP(REORDER_INDEX);
- dup->ProgMax = Num_K;
- dup->ProgCur = 0;
- } else
- dup = NULL;
-
- if (!Pex)
- return Srtd;
-
- for (i = 0; i < Num_K; i++) {
- if (Pex[i] == Num_K) { // Already moved
- continue;
- } else if (Pex[i] == i) { // Already placed
- if (dup)
- dup->ProgCur++;
-
- continue;
- } // endif's Pex
-
- sorted = false;
-
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->Save(i);
-
- n = To_Rec[i];
-
- for (j = i;; j = k) {
- k = Pex[j];
- Pex[j] = Num_K; // Mark position as set
-
- if (k == i) {
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->Restore(j);
-
- To_Rec[j] = n;
- break; // end of loop
- } else {
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->Move(j, k); // Move k to j
-
- To_Rec[j] = To_Rec[k];
- } // endif k
-
- if (dup)
- dup->ProgCur++;
-
- } // endfor j
-
- } // endfor i
-
- // The index is not used anymore
- PlgDBfree(Index);
- return sorted;
- } // end of Reorder
-
-/***********************************************************************/
-/* Save the index values for this table. */
-/* The problem here is to avoid name duplication, because more than */
-/* one data file can have the same name (but different types) and/or */
-/* the same data file can be used with different block sizes. This is */
-/* why we use Ofn that defaults to the file name but can be set to a */
-/* different name if necessary. */
-/***********************************************************************/
-bool XINDEX::SaveIndex(PGLOBAL g, PIXDEF sxp)
- {
- char *ftype;
- char fn[_MAX_PATH];
- int n[NZ], nof = (Mul) ? (Ndif + 1) : 0;
- int id = -1, size = 0;
- bool sep, rc = false;
- PXCOL kcp = To_KeyCol;
- PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
- PDBUSER dup = PlgGetUser(g);
-
- dup->Step = STEP(SAVING_INDEX);
- dup->ProgMax = 15 + 16 * Nk;
- dup->ProgCur = 0;
-
- switch (Tdbp->Ftype) {
- case RECFM_VAR: ftype = ".dnx"; break;
- case RECFM_FIX: ftype = ".fnx"; break;
- case RECFM_BIN: ftype = ".bnx"; break;
- case RECFM_VCT: ftype = ".vnx"; break;
- case RECFM_DBF: ftype = ".dbx"; break;
- default:
- sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
- return true;
- } // endswitch Ftype
-
- if ((sep = dup->Catalog->GetBoolCatInfo("SepIndex", false))) {
- // Index is saved in a separate file
-#if !defined(UNIX)
- char drive[_MAX_DRIVE];
-#else
- char *drive = NULL;
-#endif
- char direc[_MAX_DIR];
- char fname[_MAX_FNAME];
-
- _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
- strcat(strcat(fname, "_"), Xdp->GetName());
- _makepath(fn, drive, direc, fname, ftype);
- sxp = NULL;
- } else {
- id = ID;
- strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
- } // endif sep
-
- PlugSetPath(fn, fn, Tdbp->GetPath());
-
- if (X->Open(g, fn, id, (sxp) ? MODE_INSERT : MODE_WRITE)) {
- printf("%s\n", g->Message);
- return true;
- } // endif Open
-
- if (!Ndif)
- goto end; // Void index
-
- /*********************************************************************/
- /* Write the index values on the index file. */
- /*********************************************************************/
- n[0] = ID; // To check validity
- n[1] = Nk; // The number of indexed columns
- n[2] = nof; // The offset array size or 0
- n[3] = Num_K; // The index size
- n[4] = Incr; // Increment of record positions
- n[5] = Nblk; n[6] = Sblk;
-
-#if defined(TRACE)
- printf("Saving index %s\n", Xdp->GetName());
- printf("ID=%d Nk=%d nof=%d Num_K=%d Incr=%d Nblk=%d Sblk=%d\n",
- ID, Nk, nof, Num_K, Incr, Nblk, Sblk);
-#endif // TRACE
-
- size = X->Write(g, n, NZ, sizeof(int), rc);
- dup->ProgCur = 1;
-
- if (Mul) // Write the offset array
- size += X->Write(g, Pof, nof, sizeof(int), rc);
-
- dup->ProgCur = 5;
-
- if (!Incr) // Write the record position array(s)
- size += X->Write(g, To_Rec, Num_K, sizeof(int), rc);
-
- dup->ProgCur = 15;
-
- for (; kcp; kcp = kcp->Next) {
- n[0] = kcp->Ndf; // Number of distinct sub-values
- n[1] = (kcp->Kof) ? kcp->Ndf + 1 : 0; // 0 if unique
- n[2] = (kcp == To_KeyCol) ? Nblk : 0;
- n[3] = kcp->Klen; // To be checked later
- n[4] = kcp->Type; // To be checked later
-
- size += X->Write(g, n, NW, sizeof(int), rc);
- dup->ProgCur += 1;
-
- if (n[2])
- size += X->Write(g, kcp->To_Bkeys, Nblk, kcp->Klen, rc);
-
- dup->ProgCur += 5;
-
- size += X->Write(g, kcp->To_Keys, n[0], kcp->Klen, rc);
- dup->ProgCur += 5;
-
- if (n[1])
- size += X->Write(g, kcp->Kof, n[1], sizeof(int), rc);
-
- dup->ProgCur += 5;
- } // endfor kcp
-
-#if defined(TRACE)
- printf("Index %s saved, Size=%d\n", Xdp->GetName(), Size);
-#endif // TRACE
-
- end:
- X->Close(fn, id);
- return rc;
- } // end of SaveIndex
-
-#if !defined(XMAP)
-/***********************************************************************/
-/* Init: Open and Initialize a Key Index. */
-/***********************************************************************/
-bool XINDEX::Init(PGLOBAL g)
- {
- /*********************************************************************/
- /* Table will be accessed through an index table. */
- /* If sorting is required, this will be done later. */
- /*********************************************************************/
- char *ftype;
- char fn[_MAX_PATH];
- int k, n, nv[NZ], id = -1;
- bool estim = false;
- PCOL colp;
- PXCOL prev = NULL, kcp = NULL;
- PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
-
- /*********************************************************************/
- /* Get the estimated table size. */
- /* Note: for fixed tables we must use cardinality to avoid the call */
- /* to MaxBlkSize that could reduce the cardinality value. */
- /*********************************************************************/
- if (Tdbp->Cardinality(NULL)) {
- // For DBF tables, Cardinality includes bad or soft deleted lines
- // that are not included in the index, and can be larger then the
- // index size.
- estim = (Tdbp->Ftype == RECFM_DBF);
- n = Tdbp->Cardinality(g); // n is exact table size
- } else {
- // Variable table not optimized
- estim = true; // n is an estimate of the size
- n = Tdbp->GetMaxSize(g);
- } // endif Cardinality
-
- if (n <= 0)
- return !(n == 0); // n < 0 error, n = 0 void table
-
- /*********************************************************************/
- /* Get the first key column. */
- /*********************************************************************/
- if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
- strcpy(g->Message, MSG(NO_KEY_COL));
- return true; // Error
- } else
- colp = To_Cols[0];
-
- switch (Tdbp->Ftype) {
- case RECFM_VAR: ftype = ".dnx"; break;
- case RECFM_FIX: ftype = ".fnx"; break;
- case RECFM_BIN: ftype = ".bnx"; break;
- case RECFM_VCT: ftype = ".vnx"; break;
- case RECFM_DBF: ftype = ".dbx"; break;
- default:
- sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
- return true;
- } // endswitch Ftype
-
- if (defp->SepIndex()) {
- // Index was saved in a separate file
-#if !defined(UNIX)
- char drive[_MAX_DRIVE];
-#else
- char *drive = NULL;
-#endif
- char direc[_MAX_DIR];
- char fname[_MAX_FNAME];
-
- _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
- strcat(strcat(fname, "_"), Xdp->GetName());
- _makepath(fn, drive, direc, fname, ftype);
- } else {
- id = ID;
- strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
- } // endif sep
-
- PlugSetPath(fn, fn, Tdbp->GetPath());
-
-#if defined(TRACE)
- printf("Index %s file: %s\n", Xdp->GetName(), fn);
-#endif // TRACE
-
- /*********************************************************************/
- /* Open the index file and check its validity. */
- /*********************************************************************/
- if (X->Open(g, fn, id, MODE_READ))
- goto err; // No saved values
-
- // Now start the reading process.
- if (X->Read(g, nv, NZ, sizeof(int)))
- goto err;
-
-#if defined(TRACE)
- printf("nv=%d %d %d %d %d %d %d\n",
- nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6]);
-#endif // TRACE
-
- // The test on ID was suppressed because MariaDB can change an index ID
- // when other indexes are added or deleted
- if (/*nv[0] != ID ||*/ nv[1] != Nk) {
- sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
-#if defined(TRACE)
- printf("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
-#endif // TRACE
- goto err;
- } // endif
-
- if (nv[2]) {
- Mul = true;
- Ndif = nv[2];
-
- // Allocate the storage that will contain the offset array
- Offset.Size = Ndif * sizeof(int);
-
- if (!PlgDBalloc(g, NULL, Offset)) {
- sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Ndif);
- goto err;
- } // endif
-
- if (X->Read(g, Pof, Ndif, sizeof(int)))
- goto err;
-
- Ndif--; // nv[2] is offset size, equal to Ndif + 1
- } else {
- Mul = false;
- Ndif = nv[3];
- } // endif nv[2]
-
- if (nv[3] < n && estim)
- n = nv[3]; // n was just an evaluated max value
-
- if (nv[3] != n) {
- sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
- goto err;
- } // endif
-
- Num_K = nv[3];
- Incr = nv[4];
- Nblk = nv[5];
- Sblk = nv[6];
-
- if (!Incr) {
- /*******************************************************************/
- /* Allocate the storage that will contain the file positions. */
- /*******************************************************************/
- Record.Size = Num_K * sizeof(int);
-
- if (!PlgDBalloc(g, NULL, Record)) {
- sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
- goto err;
- } // endif
-
- if (X->Read(g, To_Rec, Num_K, sizeof(int)))
- goto err;
-
- } else
- Srtd = true; // Sorted positions can be calculated
-
- /*********************************************************************/
- /* Allocate the KXYCOL blocks used to store column values. */
- /*********************************************************************/
- for (k = 0; k < Nk; k++) {
- if (k == Nval)
- To_LastVal = prev;
-
- if (X->Read(g, nv, NW, sizeof(int)))
- goto err;
-
- colp = To_Cols[k];
-
- if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
- (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
- sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
- goto err; // Error
- } // endif GetKey
-
- kcp = new(g) KXYCOL(this);
-
- if (kcp->Init(g, colp, nv[0], true, (int)nv[3]))
- goto err; // Error
-
- /*******************************************************************/
- /* Read the index values from the index file. */
- /*******************************************************************/
- if (k == 0 && Nblk) {
- if (kcp->MakeBlockArray(g, Nblk, 0))
- goto err;
-
- // Read block values
- if (X->Read(g, kcp->To_Bkeys, Nblk, kcp->Klen))
- goto err;
-
- } // endif Nblk
-
- // Read the entire (small) index
- if (X->Read(g, kcp->To_Keys, nv[0], kcp->Klen))
- goto err;
-
- if (nv[1]) {
- if (!kcp->MakeOffset(g, nv[1] - 1))
- goto err;
-
- // Read the offset array
- if (X->Read(g, kcp->Kof, nv[1], sizeof(int)))
- goto err;
-
- } // endif n[1]
-
- if (!kcp->Prefix)
- // Indicate that the key column value can be found from KXYCOL
- colp->SetKcol(kcp);
-
- if (prev) {
- kcp->Previous = prev;
- prev->Next = kcp;
- } else
- To_KeyCol = kcp;
-
- prev = kcp;
- } // endfor k
-
- To_LastCol = prev;
-
- if (Mul && prev) {
- // Last key offset is the index offset
- kcp->Koff = Offset;
- kcp->Koff.Sub = true;
- } // endif Mul
-
- X->Close();
-
- /*********************************************************************/
- /* No valid record read yet for secondary file. */
- /*********************************************************************/
- Cur_K = Num_K;
- return false;
-
-err:
- Close();
- return true;
- } // end of Init
-
-#else // XMAP
-/***********************************************************************/
-/* Init: Open and Initialize a Key Index. */
-/***********************************************************************/
-bool XINDEX::Init(PGLOBAL g)
- {
- /*********************************************************************/
- /* Table will be accessed through an index table. */
- /* If sorting is required, this will be done later. */
- /*********************************************************************/
- const char *ftype;
- BYTE *mbase;
- char fn[_MAX_PATH];
- int *nv, k, n, id = -1;
- bool estim;
- PCOL colp;
- PXCOL prev = NULL, kcp = NULL;
- PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
- PDBUSER dup = PlgGetUser(g);
-
- /*********************************************************************/
- /* Get the estimated table size. */
- /* Note: for fixed tables we must use cardinality to avoid the call */
- /* to MaxBlkSize that could reduce the cardinality value. */
- /*********************************************************************/
- if (Tdbp->Cardinality(NULL)) {
- // For DBF tables, Cardinality includes bad or soft deleted lines
- // that are not included in the index, and can be larger then the
- // index size.
- estim = (Tdbp->Ftype == RECFM_DBF);
- n = Tdbp->Cardinality(g); // n is exact table size
- } else {
- // Variable table not optimized
- estim = true; // n is an estimate of the size
- n = Tdbp->GetMaxSize(g);
- } // endif Cardinality
-
- if (n <= 0)
- return !(n == 0); // n < 0 error, n = 0 void table
-
- /*********************************************************************/
- /* Get the first key column. */
- /*********************************************************************/
- if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
- strcpy(g->Message, MSG(NO_KEY_COL));
- return true; // Error
- } else
- colp = To_Cols[0];
-
- switch (Tdbp->Ftype) {
- case RECFM_VAR: ftype = ".dnx"; break;
- case RECFM_FIX: ftype = ".fnx"; break;
- case RECFM_BIN: ftype = ".bnx"; break;
- case RECFM_VCT: ftype = ".vnx"; break;
- case RECFM_DBF: ftype = ".dbx"; break;
- default:
- sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
- return true;
- } // endswitch Ftype
-
- if (defp->SepIndex()) {
- // Index was save in a separate file
-#if !defined(UNIX)
- char drive[_MAX_DRIVE];
-#else
- char *drive = NULL;
-#endif
- char direc[_MAX_DIR];
- char fname[_MAX_FNAME];
-
- _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
- strcat(strcat(fname, "_"), Xdp->GetName());
- _makepath(fn, drive, direc, fname, ftype);
- } else {
- id = ID;
- strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
- } // endif SepIndex
-
- PlugSetPath(fn, fn, Tdbp->GetPath());
-
-#if defined(TRACE)
- printf("Index %s file: %s\n", Xdp->GetName(), fn);
-#endif // TRACE
-
- /*********************************************************************/
- /* Get a view on the part of the index file containing this index. */
- /*********************************************************************/
- if (!(mbase = (BYTE*)X->FileView(g, fn)))
- goto err;
-
- if (id >= 0) {
- // Get offset from the header
- IOFF *noff = (IOFF*)mbase;
-
- // Position the memory base at the offset of this index
- mbase += noff[id].Low;
- } // endif id
-
- // Now start the mapping process.
- nv = (int*)mbase;
- mbase += NZ * sizeof(int);
-
-#if defined(TRACE)
- printf("nv=%d %d %d %d %d %d %d\n",
- nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6]);
-#endif // TRACE
-
- // The test on ID was suppressed because MariaDB can change an index ID
- // when other indexes are added or deleted
- if (/*nv[0] != ID ||*/ nv[1] != Nk) {
- // Not this index
- sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
-#if defined(TRACE)
- printf("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
-#endif // TRACE
- goto err;
- } // endif nv
-
- if (nv[2]) {
- // Set the offset array memory block
- Offset.Memp = mbase;
- Offset.Size = nv[2] * sizeof(int);
- Offset.Sub = true;
- Mul = true;
- Ndif = nv[2] - 1;
- mbase += Offset.Size;
- } else {
- Mul = false;
- Ndif = nv[3];
- } // endif nv[2]
-
- if (nv[3] < n && estim)
- n = nv[3]; // n was just an evaluated max value
-
- if (nv[3] != n) {
- sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
- goto err;
- } // endif
-
- Num_K = nv[3];
- Incr = nv[4];
- Nblk = nv[5];
- Sblk = nv[6];
-
- if (!Incr) {
- /*******************************************************************/
- /* Point to the storage that contains the file positions. */
- /*******************************************************************/
- Record.Size = Num_K * sizeof(int);
- Record.Memp = mbase;
- Record.Sub = true;
- mbase += Record.Size;
- } else
- Srtd = true; // Sorted positions can be calculated
-
- /*********************************************************************/
- /* Allocate the KXYCOL blocks used to store column values. */
- /*********************************************************************/
- for (k = 0; k < Nk; k++) {
- if (k == Nval)
- To_LastVal = prev;
-
- nv = (int*)mbase;
- mbase += (NW * sizeof(int));
-
- colp = To_Cols[k];
-
- if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
- (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
- sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
- goto err; // Error
- } // endif GetKey
-
- kcp = new(g) KXYCOL(this);
-
- if (!(mbase = kcp->MapInit(g, colp, nv, mbase)))
- goto err;
-
- if (!kcp->Prefix)
- // Indicate that the key column value can be found from KXYCOL
- colp->SetKcol(kcp);
-
- if (prev) {
- kcp->Previous = prev;
- prev->Next = kcp;
- } else
- To_KeyCol = kcp;
-
- prev = kcp;
- } // endfor k
-
- To_LastCol = prev;
-
- if (Mul && prev)
- // Last key offset is the index offset
- kcp->Koff = Offset;
-
- /*********************************************************************/
- /* No valid record read yet for secondary file. */
- /*********************************************************************/
- Cur_K = Num_K;
- return false;
-
-err:
- Close();
- return true;
- } // end of Init
-#endif // XMAP
-
-/***********************************************************************/
-/* Get Ndif and Num_K from the index file. */
-/***********************************************************************/
-bool XINDEX::GetAllSizes(PGLOBAL g, int &ndif, int &numk)
- {
- char *ftype;
- char fn[_MAX_PATH];
- int n, nv[NZ], id = -1;
- bool estim = false;
- PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
-
- ndif = numk = 0;
-
- /*********************************************************************/
- /* Get the estimated table size. */
- /* Note: for fixed tables we must use cardinality to avoid the call */
- /* to MaxBlkSize that could reduce the cardinality value. */
- /*********************************************************************/
- if (Tdbp->Cardinality(NULL)) {
- // For DBF tables, Cardinality includes bad or soft deleted lines
- // that are not included in the index, and can be larger then the
- // index size.
- estim = (Tdbp->Ftype == RECFM_DBF);
- n = Tdbp->Cardinality(g); // n is exact table size
- } else {
- // Variable table not optimized
- estim = true; // n is an estimate of the size
- n = Tdbp->GetMaxSize(g);
- } // endif Cardinality
-
- if (n <= 0)
- return !(n == 0); // n < 0 error, n = 0 void table
-
- /*********************************************************************/
- /* Check the key part number. */
- /*********************************************************************/
- if (!Nk) {
- strcpy(g->Message, MSG(NO_KEY_COL));
- return true; // Error
- } // endif Nk
-
- switch (Tdbp->Ftype) {
- case RECFM_VAR: ftype = ".dnx"; break;
- case RECFM_FIX: ftype = ".fnx"; break;
- case RECFM_BIN: ftype = ".bnx"; break;
- case RECFM_VCT: ftype = ".vnx"; break;
- case RECFM_DBF: ftype = ".dbx"; break;
- default:
- sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
- return true;
- } // endswitch Ftype
-
- if (defp->SepIndex()) {
- // Index was saved in a separate file
-#if !defined(UNIX)
- char drive[_MAX_DRIVE];
-#else
- char *drive = NULL;
-#endif
- char direc[_MAX_DIR];
- char fname[_MAX_FNAME];
-
- _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
- strcat(strcat(fname, "_"), Xdp->GetName());
- _makepath(fn, drive, direc, fname, ftype);
- } else {
- id = ID;
- strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
- } // endif sep
-
- PlugSetPath(fn, fn, Tdbp->GetPath());
-
-#if defined(TRACE)
- printf("Index %s file: %s\n", Xdp->GetName(), fn);
-#endif // TRACE
-
- /*********************************************************************/
- /* Open the index file and check its validity. */
- /*********************************************************************/
- if (X->Open(g, fn, id, MODE_READ))
- goto err; // No saved values
-
- // Get offset from XDB file
-//if (X->Seek(g, Defoff, Defhigh, SEEK_SET))
-// goto err;
-
- // Now start the reading process.
- if (X->Read(g, nv, NZ, sizeof(int)))
- goto err;
-
-#if defined(TRACE)
- printf("nv=%d %d %d %d\n", nv[0], nv[1], nv[2], nv[3]);
-#endif // TRACE
-
- // The test on ID was suppressed because MariaDB can change an index ID
- // when other indexes are added or deleted
- if (/*nv[0] != ID ||*/ nv[1] != Nk) {
- sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
-#if defined(TRACE)
- printf("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
-#endif // TRACE
- goto err;
- } // endif
-
- if (nv[2]) {
- Mul = true;
- Ndif = nv[2] - 1; // nv[2] is offset size, equal to Ndif + 1
- } else {
- Mul = false;
- Ndif = nv[3];
- } // endif nv[2]
-
- if (nv[3] < n && estim)
- n = nv[3]; // n was just an evaluated max value
-
- if (nv[3] != n) {
- sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
- goto err;
- } // endif
-
- Num_K = nv[3];
-
- if (Nk > 1) {
- if (nv[2] && X->Seek(g, nv[2] * sizeof(int), 0, SEEK_CUR))
- goto err;
-
- if (!nv[4] && X->Seek(g, Num_K * sizeof(int), 0, SEEK_CUR))
- goto err;
-
- if (X->Read(g, nv, NW, sizeof(int)))
- goto err;
-
- PCOL colp = *To_Cols;
-
- if (nv[4] != colp->GetResultType() ||
- (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
- sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
- goto err; // Error
- } // endif GetKey
-
- Ndif = nv[0];
- } // endif Nk
-
- /*********************************************************************/
- /* Set size values. */
- /*********************************************************************/
- ndif = Ndif;
- numk = Num_K;
- return false;
-
-err:
- X->Close();
- return true;
- } // end of GetAllSizes
-
-/***********************************************************************/
-/* RANGE: Tell how many records exist for a given value, for an array */
-/* of values, or in a given value range. */
-/***********************************************************************/
-int XINDEX::Range(PGLOBAL g, int limit, bool incl)
- {
- int i, k, n = 0;
- PXOB *xp = To_Vals;
- PXCOL kp = To_KeyCol;
- OPVAL op = Op;
-
- switch (limit) {
- case 1: Op = (incl) ? OP_GE : OP_GT; break;
- case 2: Op = (incl) ? OP_GT : OP_GE; break;
- default: return 0;
- } // endswitch limit
-
- /*********************************************************************/
- /* Currently only range of constant values with an EQ operator is */
- /* implemented. Find the number of rows for each given values. */
- /*********************************************************************/
- if (xp[0]->GetType() == TYPE_CONST) {
- for (i = 0; kp; kp = kp->Next) {
- kp->Valp->SetValue_pval(xp[i]->GetValue(), !kp->Prefix);
- if (++i == Nval) break;
- } // endfor kp
-
- if ((k = FastFind(Nval)) < Num_K)
- n = k;
-// if (limit)
-// n = (Mul) ? k : kp->Val_K;
-// else
-// n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
-
- } else {
- strcpy(g->Message, MSG(RANGE_NO_JOIN));
- n = -1; // Logical error
- } // endif'f Type
-
- Op = op;
- return n;
- } // end of Range
-
-/***********************************************************************/
-/* Return the size of the group (equal values) of the current value. */
-/***********************************************************************/
-int XINDEX::GroupSize(void)
- {
-#if defined(_DEBUG)
- assert(To_LastCol->Val_K >= 0 && To_LastCol->Val_K < Ndif);
-#endif // _DEBUG
-
- if (Nval == Nk)
- return (Pof) ? Pof[To_LastCol->Val_K + 1] - Pof[To_LastCol->Val_K]
- : 1;
-
-#if defined(_DEBUG)
- assert(To_LastVal);
-#endif // _DEBUG
-
- // Index whose only some columns are used
- int ck1, ck2;
-
- ck1 = To_LastVal->Val_K;
- ck2 = ck1 + 1;
-
-#if defined(_DEBUG)
- assert(ck1 >= 0 && ck1 < To_LastVal->Ndf);
-#endif // _DEBUG
-
- for (PXCOL kcp = To_LastVal; kcp; kcp = kcp->Next) {
- ck1 = (kcp->Kof) ? kcp->Kof[ck1] : ck1;
- ck2 = (kcp->Kof) ? kcp->Kof[ck2] : ck2;
- } // endfor kcp
-
- return ck2 - ck1;
- } // end of GroupSize
-
-/***********************************************************************/
-/* Find Cur_K and Val_K's of the next distinct value of the index. */
-/* Returns false if Ok, true if there are no more different values. */
-/***********************************************************************/
-bool XINDEX::NextValDif(void)
- {
- int curk;
- PXCOL kcp = (To_LastVal) ? To_LastVal : To_LastCol;
-
- if (++kcp->Val_K < kcp->Ndf) {
- Cur_K = curk = kcp->Val_K;
-
- // (Cur_K return is currently not used by SQLGBX)
- for (PXCOL kp = kcp; kp; kp = kp->Next)
- Cur_K = (kp->Kof) ? kp->Kof[Cur_K] : Cur_K;
-
- } else
- return true;
-
- for (kcp = kcp->Previous; kcp; kcp = kcp->Previous) {
- if (kcp->Kof && curk < kcp->Kof[kcp->Val_K + 1])
- break; // all previous columns have same value
-
- curk = ++kcp->Val_K; // This is a break, get new column value
- } // endfor kcp
-
- return false;
- } // end of NextValDif
-
-/***********************************************************************/
-/* XINDEX: Find Cur_K and Val_K's of next index entry. */
-/* If eq is true next values must be equal to last ones up to Nval. */
-/* Returns false if Ok, true if there are no more (equal) values. */
-/***********************************************************************/
-bool XINDEX::NextVal(bool eq)
- {
- int n, neq = Nk + 1, curk;
- PXCOL kcp;
-
- if (Cur_K == Num_K)
- return true;
- else
- curk = ++Cur_K;
-
- for (n = Nk, kcp = To_LastCol; kcp; n--, kcp = kcp->Previous) {
- if (kcp->Kof) {
- if (curk == kcp->Kof[kcp->Val_K + 1])
- neq = n;
-
- } else {
-#ifdef _DEBUG
- assert(curk == kcp->Val_K + 1);
-#endif // _DEBUG
- neq = n;
- } // endif Kof
-
-#ifdef _DEBUG
- assert(kcp->Val_K < kcp->Ndf);
-#endif // _DEBUG
-
- // If this is not a break...
- if (neq > n)
- break; // all previous columns have same value
-
- curk = ++kcp->Val_K; // This is a break, get new column value
- } // endfor kcp
-
- // Return true if no more values or, in case of "equal" values,
- // if the last used column value has changed
- return (Cur_K == Num_K || (eq && neq <= Nval));
- } // end of NextVal
-
-/***********************************************************************/
-/* XINDEX: Fetch a physical or logical record. */
-/***********************************************************************/
-int XINDEX::Fetch(PGLOBAL g)
- {
- int n;
- PXCOL kp;
-
- if (Num_K == 0)
- return -1; // means end of file
-
- /*********************************************************************/
- /* Table read through a sorted index. */
- /*********************************************************************/
- switch (Op) {
- case OP_NEXT: // Read next
- if (NextVal(false))
- return -1; // End of indexed file
-
- break;
- case OP_FIRST: // Read first
- for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
- kp->Val_K = 0;
-
- Op = OP_NEXT;
- break;
- case OP_SAME: // Read next same
- // Logically the key values should be the same as before
-#if defined(TRACE)
- printf("looking for next same value\n");
-#endif // TRACE
-
- if (NextVal(true)) {
- Op = OP_EQ;
- return -2; // no more equal values
- } // endif NextVal
-
- break;
- case OP_NXTDIF: // Read next dif
-// while (!NextVal(true)) ;
-
-// if (Cur_K >= Num_K)
-// return -1; // End of indexed file
- if (NextValDif())
- return -1; // End of indexed file
-
- break;
- case OP_FSTDIF: // Read first diff
- for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
- kp->Val_K = 0;
-
- Op = (Mul || Nval < Nk) ? OP_NXTDIF : OP_NEXT;
- break;
- default: // Should be OP_EQ
-// if (Tbxp->Key_Rank < 0) {
- /***************************************************************/
- /* Look for the first key equal to the link column values */
- /* and return its rank whithin the index table. */
- /***************************************************************/
- for (n = 0, kp = To_KeyCol; n < Nval && kp; n++, kp = kp->Next)
- if (kp->InitFind(g, To_Vals[n]))
- return -1; // No more constant values
-
- Nth++;
-
-#if defined(TRACE)
- printf("Fetch: Looking for new value\n");
-#endif // TRACE
- Cur_K = FastFind(Nval);
-
- if (Cur_K >= Num_K)
- /*************************************************************/
- /* Rank not whithin index table, signal record not found. */
- /*************************************************************/
- return -2;
-
- else if (Mul || Nval < Nk)
- Op = OP_SAME;
-
- } // endswitch Op
-
- /*********************************************************************/
- /* If rank is equal to stored rank, record is already there. */
- /*********************************************************************/
- if (Cur_K == Old_K)
- return -3; // Means record already there
- else
- Old_K = Cur_K; // Store rank of newly read record
-
- /*********************************************************************/
- /* Return the position of the required record. */
- /*********************************************************************/
- return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
- } // end of Fetch
-
-/***********************************************************************/
-/* FastFind: Returns the index of matching record in a join using an */
-/* optimized algorithm based on dichotomie and optimized comparing. */
-/***********************************************************************/
-int XINDEX::FastFind(int nv)
- {
- register int curk, sup, inf, i= 0, k, n = 2;
- register PXCOL kp, kcp;
-
- assert((int)nv == Nval);
-
- if (Nblk && Op == OP_EQ) {
- // Look in block values to find in which block to search
- sup = Nblk;
- inf = -1;
-
- while (n && sup - inf > 1) {
- i = (inf + sup) >> 1;
-
- n = To_KeyCol->CompBval(i);
-
- if (n < 0)
- sup = i;
- else
- inf = i;
-
- } // endwhile
-
- if (inf < 0)
- return Num_K;
-
-// i = inf;
- inf *= Sblk;
-
- if ((sup = inf + Sblk) > To_KeyCol->Ndf)
- sup = To_KeyCol->Ndf;
-
- inf--;
- } else {
- inf = -1;
- sup = To_KeyCol->Ndf;
- } // endif Nblk
-
- for (k = 0, kcp = To_KeyCol; kcp; kcp = kcp->Next) {
- while (sup - inf > 1) {
- i = (inf + sup) >> 1;
-
- n = kcp->CompVal(i);
-
- if (n < 0)
- sup = i;
- else if (n > 0)
- inf = i;
- else
- break;
-
- } // endwhile
-
- if (n) {
- if (Op != OP_EQ) {
- // Currently only OP_GT or OP_GE
- kcp->Val_K = curk = sup;
-
- // Check for value changes in previous key parts
- for (kp = kcp->Previous; kp; kp = kp->Previous)
- if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
- break;
- else
- curk = ++kp->Val_K;
-
- n = 0;
- } // endif Op
-
- break;
- } // endif n
-
- kcp->Val_K = i;
-
- if (++k == Nval) {
- if (Op == OP_GT) { // n is always 0
- curk = ++kcp->Val_K; // Increment value by 1
-
- // Check for value changes in previous key parts
- for (kp = kcp->Previous; kp; kp = kp->Previous)
- if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
- break; // Not changed
- else
- curk = ++kp->Val_K;
-
- } // endif Op
-
- break; // So kcp remains pointing the last tested block
- } // endif k
-
- if (kcp->Kof) {
- inf = kcp->Kof[i] - 1;
- sup = kcp->Kof[i + 1];
- } else {
- inf = i - 1;
- sup = i + 1;
- } // endif Kof
-
- } // endfor k, kcp
-
- if (n) {
- // Record not found
- for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
- kcp->Val_K = kcp->Ndf; // Not a valid value
-
- return Num_K;
- } // endif n
-
- for (curk = kcp->Val_K; kcp; kcp = kcp->Next) {
- kcp->Val_K = curk;
- curk = (kcp->Kof) ? kcp->Kof[kcp->Val_K] : kcp->Val_K;
- } // endfor kcp
-
- return curk;
- } // end of FastFind
-
-/* -------------------------- XINDXS Class --------------------------- */
-
-/***********************************************************************/
-/* XINDXS public constructor. */
-/***********************************************************************/
-XINDXS::XINDXS(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp)
- : XINDEX(tdbp, xdp, pxp, cp, xp)
- {
- Srtd = To_Cols[0]->GetOpt() == 2;
- } // end of XINDXS constructor
-
-/***********************************************************************/
-/* XINDXS compare routine for C Quick/Insertion sort. */
-/***********************************************************************/
-int XINDXS::Qcompare(int *i1, int *i2)
- {
-#ifdef DEBTRACE
- num_comp++;
-#endif
-
- return To_KeyCol->Compare(*i1, *i2);
- } // end of Qcompare
-
-/***********************************************************************/
-/* Range: Tell how many records exist for given value(s): */
-/* If limit=0 return range for these values. */
-/* If limit=1 return the start of range. */
-/* If limit=2 return the end of range. */
-/***********************************************************************/
-int XINDXS::Range(PGLOBAL g, int limit, bool incl)
- {
- int k, n = 0;
- PXOB xp = To_Vals[0];
- PXCOL kp = To_KeyCol;
- OPVAL op = Op;
-
- switch (limit) {
- case 1: Op = (incl) ? OP_GE : OP_GT; break;
- case 2: Op = (incl) ? OP_GT : OP_GE; break;
- default: Op = OP_EQ;
- } // endswitch limit
-
- /*********************************************************************/
- /* Currently only range of constant values with an EQ operator is */
- /* implemented. Find the number of rows for each given values. */
- /*********************************************************************/
- if (xp->GetType() == TYPE_CONST) {
- kp->Valp->SetValue_pval(xp->GetValue(), !kp->Prefix);
- k = FastFind(Nval);
-
- if (k < Num_K || Op != OP_EQ)
- if (limit)
- n = (Mul) ? k : kp->Val_K;
- else
- n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
-
- } else {
- strcpy(g->Message, MSG(RANGE_NO_JOIN));
- n = -1; // Logical error
- } // endif'f Type
-
- Op = op;
- return n;
- } // end of Range
-
-/***********************************************************************/
-/* Return the size of the group (equal values) of the current value. */
-/***********************************************************************/
-int XINDXS::GroupSize(void)
- {
-#if defined(_DEBUG)
- assert(To_KeyCol->Val_K >= 0 && To_KeyCol->Val_K < Ndif);
-#endif // _DEBUG
- return (Pof) ? Pof[To_KeyCol->Val_K + 1] - Pof[To_KeyCol->Val_K]
- : 1;
- } // end of GroupSize
-
-/***********************************************************************/
-/* XINDXS: Find Cur_K and Val_K of next index value. */
-/* If b is true next value must be equal to last one. */
-/* Returns false if Ok, true if there are no more (equal) values. */
-/***********************************************************************/
-bool XINDXS::NextVal(bool eq)
- {
- bool rc;
-
- if (To_KeyCol->Val_K == Ndif)
- return true;
-
- if (Mul) {
- int limit = Pof[To_KeyCol->Val_K + 1];
-
-#ifdef _DEBUG
- assert(Cur_K < limit);
- assert(To_KeyCol->Val_K < Ndif);
-#endif // _DEBUG
-
- if (++Cur_K == limit) {
- To_KeyCol->Val_K++;
- rc = (eq || limit == Num_K);
- } else
- rc = false;
-
- } else
- rc = (To_KeyCol->Val_K = ++Cur_K) == Num_K || eq;
-
- return rc;
- } // end of NextVal
-
-/***********************************************************************/
-/* XINDXS: Fetch a physical or logical record. */
-/***********************************************************************/
-int XINDXS::Fetch(PGLOBAL g)
- {
- if (Num_K == 0)
- return -1; // means end of file
-
- /*********************************************************************/
- /* Table read through a sorted index. */
- /*********************************************************************/
- switch (Op) {
- case OP_NEXT: // Read next
- if (NextVal(false))
- return -1; // End of indexed file
-
- break;
- case OP_FIRST: // Read first
- To_KeyCol->Val_K = Cur_K = 0;
- Op = OP_NEXT;
- break;
- case OP_SAME: // Read next same
-#if defined(TRACE)
-// printf("looking for next same value\n");
-#endif // TRACE
-
- if (!Mul || NextVal(true)) {
- Op = OP_EQ;
- return -2; // No more equal values
- } // endif Mul
-
- break;
- case OP_NXTDIF: // Read next dif
- if (++To_KeyCol->Val_K == Ndif)
- return -1; // End of indexed file
-
- Cur_K = Pof[To_KeyCol->Val_K];
- break;
- case OP_FSTDIF: // Read first diff
- To_KeyCol->Val_K = Cur_K = 0;
- Op = (Mul) ? OP_NXTDIF : OP_NEXT;
- break;
- default: // Should OP_EQ
- /*****************************************************************/
- /* Look for the first key equal to the link column values */
- /* and return its rank whithin the index table. */
- /*****************************************************************/
- if (To_KeyCol->InitFind(g, To_Vals[0]))
- return -1; // No more constant values
- else
- Nth++;
-
-#if defined(TRACE)
- printf("Fetch: Looking for new value\n");
-#endif // TRACE
-
- Cur_K = FastFind(1);
-
- if (Cur_K >= Num_K)
- // Rank not whithin index table, signal record not found
- return -2;
- else if (Mul)
- Op = OP_SAME;
-
- } // endswitch Op
-
- /*********************************************************************/
- /* If rank is equal to stored rank, record is already there. */
- /*********************************************************************/
- if (Cur_K == Old_K)
- return -3; // Means record already there
- else
- Old_K = Cur_K; // Store rank of newly read record
-
- /*********************************************************************/
- /* Return the position of the required record. */
- /*********************************************************************/
- return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
- } // end of Fetch
-
-/***********************************************************************/
-/* FastFind: Returns the index of matching indexed record using an */
-/* optimized algorithm based on dichotomie and optimized comparing. */
-/***********************************************************************/
-int XINDXS::FastFind(int nk)
- {
- register int sup, inf, i= 0, n = 2;
- register PXCOL kcp = To_KeyCol;
-
- if (Nblk && Op == OP_EQ) {
- // Look in block values to find in which block to search
- sup = Nblk;
- inf = -1;
-
- while (n && sup - inf > 1) {
- i = (inf + sup) >> 1;
-
- n = kcp->CompBval(i);
-
- if (n < 0)
- sup = i;
- else
- inf = i;
-
- } // endwhile
-
- if (inf < 0)
- return Num_K;
-
-// i = inf;
- inf *= Sblk;
-
- if ((sup = inf + Sblk) > Ndif)
- sup = Ndif;
-
- inf--;
- } else {
- inf = -1;
- sup = Ndif;
- } // endif Nblk
-
- while (sup - inf > 1) {
- i = (inf + sup) >> 1;
-
- n = kcp->CompVal(i);
-
- if (n < 0)
- sup = i;
- else if (n > 0)
- inf = i;
- else
- break;
-
- } // endwhile
-
- if (!n && Op == OP_GT) {
- ++i;
- } else if (n && Op != OP_EQ) {
- // Currently only OP_GT or OP_GE
- i = sup;
- n = 0;
- } // endif sup
-
- kcp->Val_K = i; // Used by FillValue
- return ((n) ? Num_K : (Mul) ? Pof[i] : i);
- } // end of FastFind
-
-/* -------------------------- XLOAD Class --------------------------- */
-
-/***********************************************************************/
-/* XLOAD constructor. */
-/***********************************************************************/
-XLOAD::XLOAD(void)
- {
- Hfile = INVALID_HANDLE_VALUE;
-#if defined(WIN32) && defined(XMAP)
- ViewBase = NULL;
-#endif // WIN32 && XMAP
- NewOff.Val = 0LL;
-} // end of XLOAD constructor
-
-/***********************************************************************/
-/* Close the index huge file. */
-/***********************************************************************/
-void XLOAD::Close(void)
- {
- if (Hfile != INVALID_HANDLE_VALUE) {
- CloseFileHandle(Hfile);
- Hfile = INVALID_HANDLE_VALUE;
- } // endif Hfile
-
-#if defined(WIN32) && defined(XMAP)
- if (ViewBase) {
- if (!UnmapViewOfFile(ViewBase))
- printf("Error %d closing Viewmap\n", GetLastError());
-
- ViewBase = NULL;
- } // endif ViewBase
-#endif // WIN32 && XMAP
-
- } // end of Close
-
-/* --------------------------- XFILE Class --------------------------- */
-
-/***********************************************************************/
-/* XFILE constructor. */
-/***********************************************************************/
-XFILE::XFILE(void) : XLOAD()
- {
- Xfile = NULL;
-#if defined(XMAP) && !defined(WIN32)
- Mmp = NULL;
-#endif // XMAP && !WIN32
- } // end of XFILE constructor
-
-/***********************************************************************/
-/* Xopen function: opens a file using native API's. */
-/***********************************************************************/
-bool XFILE::Open(PGLOBAL g, char *filename, int id, MODE mode)
- {
- char *pmod;
- bool rc;
- IOFF noff[MAX_INDX];
-
- /*********************************************************************/
- /* Open the index file according to mode. */
- /*********************************************************************/
- switch (mode) {
- case MODE_READ: pmod = "rb"; break;
- case MODE_WRITE: pmod = "wb"; break;
- case MODE_INSERT: pmod = "ab"; break;
- default:
- sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
- return true;
- } // endswitch mode
-
- if (!(Xfile= global_fopen(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, pmod))) {
-#if defined(TRACE)
- printf("Open: %s\n", g->Message);
-#endif // TRACE
- return true;
- } // endif Xfile
-
- if (mode == MODE_INSERT) {
- /*******************************************************************/
- /* Position the cursor at end of file so ftell returns file size. */
- /*******************************************************************/
- if (fseek(Xfile, 0, SEEK_END)) {
- sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
- return true;
- } // endif
-
- NewOff.Low = (int)ftell(Xfile);
- } else if (mode == MODE_WRITE) {
- if (id >= 0) {
- // New not sep index file. Write the header.
- memset(noff, 0, sizeof(noff));
- Write(g, noff, sizeof(IOFF), MAX_INDX, rc);
- fseek(Xfile, 0, SEEK_END);
- NewOff.Low = (int)ftell(Xfile);
- } // endif id
-
- } else if (mode == MODE_READ && id >= 0) {
- // Get offset from the header
- if (fread(noff, sizeof(IOFF), MAX_INDX, Xfile) != MAX_INDX) {
- sprintf(g->Message, MSG(XFILE_READERR), errno);
- return true;
- } // endif MAX_INDX
-
- // Position the cursor at the offset of this index
- if (fseek(Xfile, noff[id].Low, SEEK_SET)) {
- sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
- return true;
- } // endif
-
- } // endif mode
-
- return false;
- } // end of Open
-
-/***********************************************************************/
-/* Move into an index file. */
-/***********************************************************************/
-bool XFILE::Seek(PGLOBAL g, int low, int high, int origin)
- {
-#if defined(_DEBUG)
- assert(high == 0);
-#endif // !_DEBUG
-
- if (fseek(Xfile, low, origin)) {
- sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
- return true;
- } // endif
-
-//ftell(Xfile);
- return false;
- } // end of Seek
-
-/***********************************************************************/
-/* Read from the index file. */
-/***********************************************************************/
-bool XFILE::Read(PGLOBAL g, void *buf, int n, int size)
- {
- if (fread(buf, size, n, Xfile) != (size_t)n) {
- sprintf(g->Message, MSG(XFILE_READERR), errno);
- return true;
- } // endif size
-
- return false;
- } // end of Read
-
-/***********************************************************************/
-/* Write on index file, set rc and return the number of bytes written */
-/***********************************************************************/
-int XFILE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
- {
- int niw = (int)fwrite(buf, size, n, Xfile);
-
- if (niw != n) {
- sprintf(g->Message, MSG(XFILE_WRITERR), strerror(errno));
- rc = true;
- } // endif size
-
- return niw * size;
- } // end of Write
-
-/***********************************************************************/
-/* Update the file header and close the index file. */
-/***********************************************************************/
-void XFILE::Close(char *fn, int id)
- {
- if (id >= 0 && fn && Xfile) {
- fclose(Xfile);
-
- if ((Xfile = fopen(fn, "r+b")))
- if (!fseek(Xfile, id * sizeof(IOFF), SEEK_SET))
- fwrite(&NewOff, sizeof(int), 2, Xfile);
-
- } // endif id
-
- Close();
- } // end of Close
-
-/***********************************************************************/
-/* Close the index file. */
-/***********************************************************************/
-void XFILE::Close(void)
- {
- XLOAD::Close();
-
- if (Xfile) {
- fclose(Xfile);
- Xfile = NULL;
- } // endif Xfile
-
-#if defined(XMAP) && !defined(WIN32)
- if (Mmp) {
- CloseMemMap(Mmp->memory, Mmp->lenL);
- Mmp = NULL;
- } // endif Mmp
-#endif // XMAP
- } // end of Close
-
-#if defined(XMAP)
- /*********************************************************************/
- /* Map the entire index file. */
- /*********************************************************************/
-void *XFILE::FileView(PGLOBAL g, char *fn)
- {
- HANDLE h;
-
- Mmp = (MMP)PlugSubAlloc(g, NULL, sizeof(MEMMAP));
- h = CreateFileMap(g, fn, Mmp, MODE_READ, false);
-
- if (h == INVALID_HANDLE_VALUE || (!Mmp->lenH && !Mmp->lenL)) {
- if (!(*g->Message))
- strcpy(g->Message, MSG(FILE_MAP_ERR));
-
- CloseFileHandle(h); // Not used anymore
- return NULL; // No saved values
- } // endif h
-
- CloseFileHandle(h); // Not used anymore
- return Mmp->memory;
- } // end of FileView
-#endif // XMAP
-
-/* -------------------------- XHUGE Class --------------------------- */
-
-/***********************************************************************/
-/* Xopen function: opens a file using native API's. */
-/***********************************************************************/
-bool XHUGE::Open(PGLOBAL g, char *filename, int id, MODE mode)
- {
- IOFF noff[MAX_INDX];
-
- if (Hfile != INVALID_HANDLE_VALUE) {
- sprintf(g->Message, MSG(FILE_OPEN_YET), filename);
- return true;
- } // endif
-
-#if defined(TRACE)
- printf( "Xopen: filename=%s mode=%d\n", filename, mode);
-#endif // TRACE
-
-#if defined(WIN32)
- LONG high = 0;
- DWORD rc, drc, access, share, creation;
-
- /*********************************************************************/
- /* Create the file object according to access mode */
- /*********************************************************************/
- switch (mode) {
- case MODE_READ:
- access = GENERIC_READ;
- share = FILE_SHARE_READ;
- creation = OPEN_EXISTING;
- break;
- case MODE_WRITE:
- access = GENERIC_WRITE;
- share = 0;
- creation = CREATE_ALWAYS;
- break;
- case MODE_INSERT:
- access = GENERIC_WRITE;
- share = 0;
- creation = OPEN_EXISTING;
- break;
- default:
- sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
- return true;
- } // endswitch
-
- Hfile = CreateFile(filename, access, share, NULL, creation,
- FILE_ATTRIBUTE_NORMAL, NULL);
-
- if (Hfile == INVALID_HANDLE_VALUE) {
- rc = GetLastError();
- sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename);
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
- (LPTSTR)filename, sizeof(filename), NULL);
- strcat(g->Message, filename);
- return true;
- } // endif Hfile
-
-#ifdef DEBTRACE
- fprintf(debug,
- " access=%p share=%p creation=%d handle=%p fn=%s\n",
- access, share, creation, Hfile, filename);
-#endif
-
- if (mode == MODE_INSERT) {
- /*******************************************************************/
- /* In Insert mode we must position the cursor at end of file. */
- /*******************************************************************/
- rc = SetFilePointer(Hfile, 0, &high, FILE_END);
-
- if (rc == INVALID_SET_FILE_POINTER && (drc = GetLastError()) != NO_ERROR) {
- sprintf(g->Message, MSG(ERROR_IN_SFP), drc);
- CloseHandle(Hfile);
- Hfile = INVALID_HANDLE_VALUE;
- return true;
- } // endif
-
- NewOff.Low = (int)rc;
- NewOff.High = (int)high;
- } else if (mode == MODE_WRITE) {
- if (id >= 0) {
- // New not sep index file. Write the header.
- memset(noff, 0, sizeof(noff));
- rc = WriteFile(Hfile, noff, sizeof(noff), &drc, NULL);
- NewOff.Low = (int)drc;
- } // endif id
-
- } else if (mode == MODE_READ && id >= 0) {
- // Get offset from the header
- rc = ReadFile(Hfile, noff, sizeof(noff), &drc, NULL);
-
- if (!rc) {
- sprintf(g->Message, MSG(XFILE_READERR), GetLastError());
- return true;
- } // endif rc
-
- // Position the cursor at the offset of this index
- rc = SetFilePointer(Hfile, noff[id].Low,
- (PLONG)&noff[id].High, FILE_BEGIN);
-
- if (rc == INVALID_SET_FILE_POINTER) {
- sprintf(g->Message, MSG(FUNC_ERRNO), GetLastError(), "SetFilePointer");
- return true;
- } // endif
-
- } // endif Mode
-
-#else // UNIX
- int oflag = O_LARGEFILE; // Enable file size > 2G
- mode_t pmod = 0;
-
- /*********************************************************************/
- /* Create the file object according to access mode */
- /*********************************************************************/
- switch (mode) {
- case MODE_READ:
- oflag |= O_RDONLY;
- break;
- case MODE_WRITE:
- oflag |= O_WRONLY | O_CREAT | O_TRUNC;
- pmod = S_IREAD | S_IWRITE;
- break;
- case MODE_INSERT:
- oflag |= (O_WRONLY | O_APPEND);
- break;
- default:
- sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
- return true;
- } // endswitch
-
- Hfile= global_open(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, oflag, pmod);
-
- if (Hfile == INVALID_HANDLE_VALUE) {
- /*rc = errno;*/
-#if defined(TRACE)
- printf("Open: %s\n", g->Message);
-#endif // TRACE
- return true;
- } // endif Hfile
-
-#if defined(TRACE)
- printf(" rc=%d oflag=%p mode=%d handle=%d fn=%s\n",
- rc, oflag, mode, Hfile, filename);
-#endif // TRACE
-
- if (mode == MODE_INSERT) {
- /*******************************************************************/
- /* Position the cursor at end of file so ftell returns file size. */
- /*******************************************************************/
- if (!(NewOff.Val = (longlong)lseek64(Hfile, 0LL, SEEK_END))) {
- sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Seek");
- return true;
- } // endif
-
- } else if (mode == MODE_WRITE) {
- if (id >= 0) {
- // New not sep index file. Write the header.
- memset(noff, 0, sizeof(noff));
- NewOff.Low = write(Hfile, &noff, sizeof(noff));
- } // endif id
-
- } else if (mode == MODE_READ && id >= 0) {
- // Get offset from the header
- if (read(Hfile, noff, sizeof(noff)) != sizeof(noff)) {
- sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
- return true;
- } // endif MAX_INDX
-
- // Position the cursor at the offset of this index
- if (!lseek64(Hfile, noff[id].Val, SEEK_SET)) {
- sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Hseek");
- return true;
- } // endif
-
- } // endif mode
-#endif // UNIX
-
- return false;
- } // end of Open
-
-/***********************************************************************/
-/* Go to position in a huge file. */
-/***********************************************************************/
-bool XHUGE::Seek(PGLOBAL g, int low, int high, int origin)
- {
-#if defined(WIN32)
- LONG hi = high;
- DWORD rc = SetFilePointer(Hfile, low, &hi, origin);
-
- if (rc == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
- sprintf(g->Message, MSG(FUNC_ERROR), "Xseek");
- return true;
- } // endif
-
-#else // UNIX
- off64_t pos = (off64_t)low
- + (off64_t)high * ((off64_t)0x100 * (off64_t)0x1000000);
-
- if (lseek64(Hfile, pos, origin) < 0) {
- sprintf(g->Message, MSG(ERROR_IN_LSK), errno);
-#if defined(TRACE)
- printf("lseek64 error %d\n", errno);
-#endif // TRACE
- return true;
- } // endif lseek64
-
-#if defined(TRACE)
- printf("Seek: low=%d high=%d\n", low, high);
-#endif // TRACE
-#endif // UNIX
-
- return false;
- } // end of Seek
-
-/***********************************************************************/
-/* Read from a huge index file. */
-/***********************************************************************/
-bool XHUGE::Read(PGLOBAL g, void *buf, int n, int size)
- {
- bool rc = false;
-
-#if defined(WIN32)
- bool brc;
- DWORD nbr, count = (DWORD)(n * size);
-
- brc = ReadFile(Hfile, buf, count, &nbr, NULL);
-
- if (brc) {
- if (nbr != count) {
- strcpy(g->Message, MSG(EOF_INDEX_FILE));
- rc = true;
- } // endif nbr
-
- } else {
- char *buf[256];
- DWORD drc = GetLastError();
-
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
- (LPTSTR)buf, sizeof(buf), NULL);
- sprintf(g->Message, MSG(READ_ERROR), "index file", buf);
- rc = true;
- } // endif brc
-#else // UNIX
- ssize_t count = (ssize_t)(n * size);
-
-#if defined(TRACE)
- printf("Hfile=%d n=%d size=%d count=%d\n", Hfile, n, size, count);
-#endif // TRACE
-
- if (read(Hfile, buf, count) != count) {
- sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
-#if defined(TRACE)
- printf("read error %d\n", errno);
-#endif // TRACE
- rc = true;
- } // endif nbr
-#endif // UNIX
-
- return rc;
- } // end of Read
-
-/***********************************************************************/
-/* Write on a huge index file. */
-/***********************************************************************/
-int XHUGE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
- {
-#if defined(WIN32)
- bool brc;
- DWORD nbw, count = (DWORD)n * (DWORD) size;
-
- brc = WriteFile(Hfile, buf, count, &nbw, NULL);
-
- if (!brc) {
- char msg[256];
- DWORD drc = GetLastError();
-
- FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
- (LPTSTR)msg, sizeof(msg), NULL);
- sprintf(g->Message, MSG(WRITING_ERROR), "index file", msg);
- rc = true;
- } // endif size
-
- return (int)nbw;
-#else // UNIX
- ssize_t nbw;
- size_t count = (size_t)n * (size_t)size;
-
- nbw = write(Hfile, buf, count);
-
- if (nbw != (signed)count) {
- sprintf(g->Message, MSG(WRITING_ERROR),
- "index file", strerror(errno));
- rc = true;
- } // endif nbw
-
- return (int)nbw;
-#endif // UNIX
- } // end of Write
-
-/***********************************************************************/
-/* Update the file header and close the index file. */
-/***********************************************************************/
-void XHUGE::Close(char *fn, int id)
- {
-#if defined(WIN32)
- if (id >= 0 && fn) {
- CloseFileHandle(Hfile);
- Hfile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if (Hfile != INVALID_HANDLE_VALUE)
- if (SetFilePointer(Hfile, id * sizeof(IOFF), NULL, FILE_BEGIN)
- != INVALID_SET_FILE_POINTER) {
- DWORD nbw;
-
- WriteFile(Hfile, &NewOff, sizeof(IOFF), &nbw, NULL);
- } // endif SetFilePointer
-
- } // endif id
-#else // !WIN32
- if (id >= 0 && fn) {
- fcntl(Hfile, F_SETFD, O_WRONLY);
-
- if (lseek(Hfile, id * sizeof(IOFF), SEEK_SET))
- write(Hfile, &NewOff, sizeof(IOFF));
-
- } // endif id
-#endif // !WIN32
-
- XLOAD::Close();
- } // end of Close
-
-#if defined(XMAP)
-/***********************************************************************/
-/* Don't know whether this is possible for huge files. */
-/***********************************************************************/
-void *XHUGE::FileView(PGLOBAL g, char *fn)
- {
- strcpy(g->Message, MSG(NO_PART_MAP));
- return NULL;
- } // end of FileView
-#endif // XMAP
-
-/* -------------------------- XXROW Class --------------------------- */
-
-/***********************************************************************/
-/* XXROW Public Constructor. */
-/***********************************************************************/
-XXROW::XXROW(PTDBDOS tdbp) : XXBASE(tdbp, false)
- {
- Tdbp = tdbp;
- Valp = NULL;
- } // end of XXROW constructor
-
-/***********************************************************************/
-/* XXROW Reset: re-initialize a Kindex block. */
-/***********************************************************************/
-void XXROW::Reset(void)
- {
-#if defined(_DEBUG)
- assert(Tdbp->GetLink()); // This a join index
-#endif // _DEBUG
- } // end of Reset
-
-/***********************************************************************/
-/* Init: Open and Initialize a Key Index. */
-/***********************************************************************/
-bool XXROW::Init(PGLOBAL g)
- {
- /*********************************************************************/
- /* Table will be accessed through an index table. */
- /* To_Link should not be NULL. */
- /*********************************************************************/
- if (!Tdbp->GetLink() || Tbxp->GetKnum() != 1)
- return true;
-
- if ((*Tdbp->GetLink())->GetResultType() != TYPE_INT) {
- strcpy(g->Message, MSG(TYPE_MISMATCH));
- return true;
- } else
- Valp = (*Tdbp->GetLink())->GetValue();
-
- if ((Num_K = Tbxp->Cardinality(g)) < 0)
- return true; // Not a fixed file
-
- /*********************************************************************/
- /* The entire table is indexed, no need to construct the index. */
- /*********************************************************************/
- Cur_K = Num_K;
- return false;
- } // end of Init
-
-/***********************************************************************/
-/* RANGE: Tell how many record exist in a given value range. */
-/***********************************************************************/
-int XXROW::Range(PGLOBAL g, int limit, bool incl)
- {
- int n = Valp->GetIntValue();
-
- switch (limit) {
- case 1: n += ((incl) ? 0 : 1); break;
- case 2: n += ((incl) ? 1 : 0); break;
- default: n = 1;
- } // endswitch limit
-
- return n;
- } // end of Range
-
-/***********************************************************************/
-/* XXROW: Fetch a physical or logical record. */
-/***********************************************************************/
-int XXROW::Fetch(PGLOBAL g)
- {
- if (Num_K == 0)
- return -1; // means end of file
-
- /*********************************************************************/
- /* Look for a key equal to the link column of previous table, */
- /* and return its rank whithin the index table. */
- /*********************************************************************/
- Cur_K = FastFind(1);
-
- if (Cur_K >= Num_K)
- /*******************************************************************/
- /* Rank not whithin index table, signal record not found. */
- /*******************************************************************/
- return -2; // Means record not found
-
- /*********************************************************************/
- /* If rank is equal to stored rank, record is already there. */
- /*********************************************************************/
- if (Cur_K == Old_K)
- return -3; // Means record already there
- else
- Old_K = Cur_K; // Store rank of newly read record
-
- return Cur_K;
- } // end of Fetch
-
-/***********************************************************************/
-/* FastFind: Returns the index of matching record in a join. */
-/***********************************************************************/
-int XXROW::FastFind(int nk)
- {
- int n = Valp->GetIntValue();
-
- if (n < 0)
- return (Op == OP_EQ) ? (-1) : 0;
- else if (n > Num_K)
- return Num_K;
- else
- return (Op == OP_GT) ? n : (n - 1);
-
- } // end of FastFind
-
-/* ------------------------- KXYCOL Classes -------------------------- */
-
-/***********************************************************************/
-/* KXYCOL public constructor. */
-/***********************************************************************/
-KXYCOL::KXYCOL(PKXBASE kp) : To_Keys(Keys.Memp),
- To_Bkeys(Bkeys.Memp), Kof((CPINT&)Koff.Memp)
- {
- Next = NULL;
- Previous = NULL;
- Kxp = kp;
- Colp = NULL;
- IsSorted = false;
- Asc = true;
- Keys = Nmblk;
- Kblp = NULL;
- Bkeys = Nmblk;
- Blkp = NULL;
- Valp = NULL;
- Klen = 0;
- Kprec = 0;
- Type = TYPE_ERROR;
- Prefix = false;
- Koff = Nmblk;
- Val_K = 0;
- Ndf = 0;
- Mxs = 0;
- } // end of KXYCOL constructor
-
-/***********************************************************************/
-/* KXYCOL Init: initialize and allocate storage. */
-/* Key length kln can be smaller than column length for CHAR columns. */
-/***********************************************************************/
-bool KXYCOL::Init(PGLOBAL g, PCOL colp, int n, bool sm, int kln)
- {
- int len = colp->GetLength(), prec = colp->GetScale();
-
- // Currently no indexing on NULL columns
- if (colp->IsNullable()) {
- sprintf(g->Message, "Cannot index nullable column %s", colp->GetName());
- return true;
- } // endif nullable
-
- if (kln && len > kln && colp->GetResultType() == TYPE_STRING) {
- len = kln;
- Prefix = true;
- } // endif kln
-
-#ifdef DEBTRACE
- htrc("KCOL(%p) Init: col=%s n=%d type=%d sm=%d\n",
- this, colp->GetName(), n, colp->GetResultType(), sm);
-#endif
-
- // Allocate the Value object used when moving items
- Type = colp->GetResultType();
-
- if (!(Valp = AllocateValue(g, Type, len, colp->GetScale(),
- colp->IsUnsigned())))
- return true;
-
- Klen = Valp->GetClen();
- Keys.Size = n * Klen;
-
- if (!PlgDBalloc(g, NULL, Keys)) {
- sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, n);
- return true; // Error
- } // endif
-
- // Allocate the Valblock. The last parameter is to have rows filled
- // by blanks (if true) or keep the zero ending char (if false).
- // Currently we set it to true to be compatible with QRY blocks,
- // and the one before last is to enable length/type checking, set to
- // true if not a prefix key.
- Kblp = AllocValBlock(g, To_Keys, Type, n, len, prec, !Prefix, true);
- Asc = sm; // Sort mode: Asc=true Desc=false
- Ndf = n;
-
- // Store this information to avoid sorting when already done
- if (Asc)
- IsSorted = colp->GetOpt() == 2;
-
-//SetNulls(colp->IsNullable()); for when null columns will be indexable
- return false;
- } // end of Init
-
-#if defined(XMAP)
-/***********************************************************************/
-/* KXYCOL MapInit: initialize and address storage. */
-/* Key length kln can be smaller than column length for CHAR columns. */
-/***********************************************************************/
-BYTE* KXYCOL::MapInit(PGLOBAL g, PCOL colp, int *n, BYTE *m)
- {
- int len = colp->GetLength(), prec = colp->GetPrecision();
-
- if (n[3] && colp->GetLength() > n[3]
- && colp->GetResultType() == TYPE_STRING) {
- len = n[3];
- Prefix = true;
- } // endif kln
-
- Type = colp->GetResultType();
-
-#ifdef DEBTRACE
- htrc("MapInit(%p): colp=%p type=%d n=%d len=%d m=%p\n",
- this, colp, Type, n[0], len, m);
-#endif
-
- // Allocate the Value object used when moving items
- Valp = AllocateValue(g, Type, len, prec, false, NULL);
- Klen = Valp->GetClen();
-
- if (n[2]) {
- Bkeys.Size = n[2] * Klen;
- Bkeys.Memp = m;
- Bkeys.Sub = true;
-
- // Allocate the Valblk containing initial block key values
- Blkp = AllocValBlock(g, To_Bkeys, Type, n[2], len, prec, true, true);
- } // endif nb
-
- Keys.Size = n[0] * Klen;
- Keys.Memp = m + Bkeys.Size;
- Keys.Sub = true;
-
- // Allocate the Valblock. Last two parameters are to have rows filled
- // by blanks (if true) or keep the zero ending char (if false).
- // Currently we set it to true to be compatible with QRY blocks,
- // and last one to enable type checking (no conversion).
- Kblp = AllocValBlock(g, To_Keys, Type, n[0], len, prec, true, true);
-
- if (n[1]) {
- Koff.Size = n[1] * sizeof(int);
- Koff.Memp = m + Bkeys.Size + Keys.Size;
- Koff.Sub = true;
- } // endif n[1]
-
- Ndf = n[0];
- IsSorted = colp->GetOpt() < 0;
- return m + Bkeys.Size + Keys.Size + Koff.Size;
- } // end of MapInit
-#endif // XMAP
-
-/***********************************************************************/
-/* Allocate the offset block used by intermediate key columns. */
-/***********************************************************************/
-int *KXYCOL::MakeOffset(PGLOBAL g, int n)
- {
- if (!Kof) {
- // Calculate the initial size of the offset
- Koff.Size = (n + 1) * sizeof(int);
-
- // Allocate the required memory
- if (!PlgDBalloc(g, NULL, Koff)) {
- strcpy(g->Message, MSG(KEY_ALLOC_ERR));
- return NULL; // Error
- } // endif
-
- } else if (n) {
- // This is a reallocation call
- PlgDBrealloc(g, NULL, Koff, (n + 1) * sizeof(int));
- } else
- PlgDBfree(Koff);
-
- return (int*)Kof;
- } // end of MakeOffset
-
-/***********************************************************************/
-/* Make a front end array of key values that are the first value of */
-/* each blocks (of size n). This to reduce paging in FastFind. */
-/***********************************************************************/
-bool KXYCOL::MakeBlockArray(PGLOBAL g, int nb, int size)
- {
- int i, k;
-
- // Calculate the size of the block array in the index
- Bkeys.Size = nb * Klen;
-
- // Allocate the required memory
- if (!PlgDBalloc(g, NULL, Bkeys)) {
- sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, nb);
- return true; // Error
- } // endif
-
- // Allocate the Valblk used to contains initial block key values
- Blkp = AllocValBlock(g, To_Bkeys, Type, nb, Klen, Kprec);
-
- // Populate the array with values
- for (i = k = 0; i < nb; i++, k += size)
- Blkp->SetValue(Kblp, i, k);
-
- return false;
- } // end of MakeBlockArray
-
-/***********************************************************************/
-/* KXYCOL SetValue: read column value for nth array element. */
-/***********************************************************************/
-void KXYCOL::SetValue(PCOL colp, int i)
- {
-#if defined(_DEBUG)
- assert (Kblp != NULL);
-#endif
-
- Kblp->SetValue(colp->GetValue(), i);
- } // end of SetValue
-
-/***********************************************************************/
-/* InitFind: initialize finding the rank of column value in index. */
-/***********************************************************************/
-bool KXYCOL::InitFind(PGLOBAL g, PXOB xp)
- {
- if (xp->GetType() == TYPE_CONST) {
- if (Kxp->Nth)
- return true;
-
- Valp->SetValue_pval(xp->GetValue(), !Prefix);
- } else {
- xp->Reset();
- xp->Eval(g);
- Valp->SetValue_pval(xp->GetValue(), false);
-// Valp->SetValue_pval(xp->GetValue(), !Prefix);
- } // endif Type
-
- return false;
- } // end of InitFind
-
-/***********************************************************************/
-/* InitBinFind: initialize Value to the value pointed by vp. */
-/***********************************************************************/
-void KXYCOL::InitBinFind(void *vp)
- {
- Valp->SetBinValue(vp);
- } // end of InitBinFind
-
-/***********************************************************************/
-/* KXYCOL FillValue: called by COLBLK::Eval when a column value is */
-/* already in storage in the corresponding KXYCOL. */
-/***********************************************************************/
-void KXYCOL::FillValue(PVAL valp)
- {
- valp->SetValue_pvblk(Kblp, Val_K);
-
- // Set null when applicable (NIY)
-//if (valp->GetNullable())
-// valp->SetNull(valp->IsZero());
-
- } // end of FillValue
-
-/***********************************************************************/
-/* KXYCOL: Compare routine for one numeric value. */
-/***********************************************************************/
-int KXYCOL::Compare(int i1, int i2)
- {
- // Do the actual comparison between values.
- register int k = Kblp->CompVal(i1, i2);
-
-#ifdef DEBUG2
- htrc("Compare done result=%d\n", k);
-#endif
-
- return (Asc) ? k : -k;
- } // end of Compare
-
-/***********************************************************************/
-/* KXYCOL: Compare the ith key to the stored Value. */
-/***********************************************************************/
-int KXYCOL::CompVal(int i)
- {
- // Do the actual comparison between numerical values.
-#ifdef DEBUG2
- register int k = (int)Kblp->CompVal(Valp, (int)i);
-
- htrc("Compare done result=%d\n", k);
- return k;
-#endif
- return Kblp->CompVal(Valp, i);
- } // end of CompVal
-
-/***********************************************************************/
-/* KXYCOL: Compare the key to the stored block value. */
-/***********************************************************************/
-int KXYCOL::CompBval(int i)
- {
- // Do the actual comparison between key values.
- return Blkp->CompVal(Valp, i);
- } // end of CompBval
-
-/***********************************************************************/
-/* KXYCOL ReAlloc: ReAlloc To_Data if it is not suballocated. */
-/***********************************************************************/
-void KXYCOL::ReAlloc(PGLOBAL g, int n)
- {
- PlgDBrealloc(g, NULL, Keys, n * Klen);
- Kblp->ReAlloc(To_Keys, n);
- Ndf = n;
- } // end of ReAlloc
-
-/***********************************************************************/
-/* KXYCOL FreeData: Free To_Keys if it is not suballocated. */
-/***********************************************************************/
-void KXYCOL::FreeData(void)
- {
- PlgDBfree(Keys);
- Kblp = NULL;
- PlgDBfree(Bkeys);
- Blkp = NULL;
- PlgDBfree(Koff);
- Ndf = 0;
- } // end of FreeData
+/***************** Xindex C++ Class Xindex Code (.CPP) *****************/
+/* Name: XINDEX.CPP Version 2.9 */
+/* */
+/* (C) Copyright to the author Olivier BERTRAND 2004-2014 */
+/* */
+/* This file contains the class XINDEX implementation code. */
+/***********************************************************************/
+
+/***********************************************************************/
+/* Include relevant sections of the System header files. */
+/***********************************************************************/
+#include "my_global.h"
+#if defined(WIN32)
+#include <io.h>
+#include <fcntl.h>
+#include <errno.h>
+//#include <windows.h>
+#else // !WIN32
+#if defined(UNIX)
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#else // !UNIX
+#include <io.h>
+#endif // !UNIX
+#include <fcntl.h>
+#endif // !WIN32
+
+/***********************************************************************/
+/* Include required application header files */
+/* global.h is header containing all global Plug declarations. */
+/* plgdbsem.h is header containing the DB applic. declarations. */
+/* kindex.h is header containing the KINDEX class definition. */
+/***********************************************************************/
+#include "global.h"
+#include "plgdbsem.h"
+#include "osutil.h"
+#include "maputil.h"
+//nclude "filter.h"
+#include "tabcol.h"
+#include "xindex.h"
+#include "xobject.h"
+//nclude "scalfnc.h"
+//nclude "array.h"
+#include "filamtxt.h"
+#include "tabdos.h"
+
+/***********************************************************************/
+/* Macro or external routine definition */
+/***********************************************************************/
+#define NZ 7
+#define NW 5
+#define MAX_INDX 10
+#ifndef INVALID_SET_FILE_POINTER
+#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
+#endif
+
+/***********************************************************************/
+/* DB static external variables. */
+/***********************************************************************/
+extern MBLOCK Nmblk; /* Used to initialize MBLOCK's */
+extern "C" int trace;
+
+/***********************************************************************/
+/* Last two parameters are true to enable type checking, and last one */
+/* to have rows filled by blanks to be compatible with QRY blocks. */
+/***********************************************************************/
+PVBLK AllocValBlock(PGLOBAL, void *, int, int, int, int,
+ bool check = true, bool blank = true, bool un = false);
+
+/***********************************************************************/
+/* Check whether we have to create/update permanent indexes. */
+/***********************************************************************/
+int PlgMakeIndex(PGLOBAL g, PSZ name, PIXDEF pxdf, bool add)
+ {
+ int rc;
+ PTABLE tablep;
+ PTDBDOS tdbp;
+ PCATLG cat = PlgGetCatalog(g, true);
+
+ /*********************************************************************/
+ /* Open a new table in mode read and with only the keys columns. */
+ /*********************************************************************/
+ tablep = new(g) XTAB(name);
+
+ if (!(tdbp = (PTDBDOS)cat->GetTable(g, tablep)))
+ rc = RC_NF;
+ else if (!tdbp->GetDef()->Indexable()) {
+ sprintf(g->Message, MSG(TABLE_NO_INDEX), name);
+ rc = RC_NF;
+ } else if ((rc = tdbp->MakeIndex(g, pxdf, add)) == RC_INFO)
+ rc = RC_OK; // No index
+
+ return rc;
+ } // end of PlgMakeIndex
+
+/* -------------------------- Class INDEXDEF ------------------------- */
+
+/***********************************************************************/
+/* INDEXDEF Constructor. */
+/***********************************************************************/
+INDEXDEF::INDEXDEF(char *name, bool uniq, int n)
+ {
+//To_Def = NULL;
+ Next = NULL;
+ ToKeyParts = NULL;
+ Name = name;
+ Unique = uniq;
+ Invalid = false;
+ AutoInc = false;
+ Nparts = 0;
+ ID = n;
+//Offset = 0;
+//Offhigh = 0;
+//Size = 0;
+ MaxSame = 1;
+ } // end of INDEXDEF constructor
+
+/***********************************************************************/
+/* Set the max same values for each colum after making the index. */
+/***********************************************************************/
+void INDEXDEF::SetMxsame(PXINDEX x)
+ {
+ PKPDEF kdp;
+ PXCOL xcp;
+
+ for (kdp = ToKeyParts, xcp = x->To_KeyCol;
+ kdp && xcp; kdp = kdp->Next, xcp = xcp->Next)
+ kdp->Mxsame = xcp->Mxs;
+ } // end of SetMxsame
+
+/* -------------------------- Class KPARTDEF ------------------------- */
+
+/***********************************************************************/
+/* KPARTDEF Constructor. */
+/***********************************************************************/
+KPARTDEF::KPARTDEF(PSZ name, int n)
+ {
+ Next = NULL;
+ Name = name;
+ Mxsame = 0;
+ Ncol = n;
+ Klen = 0;
+ } // end of KPARTDEF constructor
+
+/* -------------------------- XXBASE Class --------------------------- */
+
+/***********************************************************************/
+/* XXBASE public constructor. */
+/***********************************************************************/
+XXBASE::XXBASE(PTDBDOS tbxp, bool b) : CSORT(b),
+ To_Rec((int*&)Record.Memp)
+ {
+ Tbxp = tbxp;
+ Record = Nmblk;
+ Cur_K = -1;
+ Old_K = -1;
+ Num_K = 0;
+ Ndif = 0;
+ Bot = Top = Inf = Sup = 0;
+ Op = OP_EQ;
+ To_KeyCol = NULL;
+ Mul = false;
+ Val_K = -1;
+ Nblk = Sblk = 0;
+ Thresh = 7;
+ ID = -1;
+ Nth = 0;
+ } // end of XXBASE constructor
+
+/***********************************************************************/
+/* Make file output of XINDEX contents. */
+/***********************************************************************/
+void XXBASE::Print(PGLOBAL g, FILE *f, uint n)
+ {
+ char m[64];
+
+ memset(m, ' ', n); // Make margin string
+ m[n] = '\0';
+ fprintf(f, "%sXINDEX: Tbxp=%p Num=%d\n", m, Tbxp, Num_K);
+ } // end of Print
+
+/***********************************************************************/
+/* Make string output of XINDEX contents. */
+/***********************************************************************/
+void XXBASE::Print(PGLOBAL g, char *ps, uint z)
+ {
+ *ps = '\0';
+ strncat(ps, "Xindex", z);
+ } // end of Print
+
+/* -------------------------- XINDEX Class --------------------------- */
+
+/***********************************************************************/
+/* XINDEX public constructor. */
+/***********************************************************************/
+XINDEX::XINDEX(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp, int k)
+ : XXBASE(tdbp, !xdp->IsUnique())
+ {
+ Xdp = xdp;
+ ID = xdp->GetID();
+ Tdbp = tdbp;
+ X = pxp;
+ To_LastCol = NULL;
+ To_LastVal = NULL;
+ To_Cols = cp;
+ To_Vals = xp;
+ Mul = !xdp->IsUnique();
+ Srtd = false;
+ Nk = xdp->GetNparts();
+ Nval = (k) ? k : Nk;
+ Incr = 0;
+//Defoff = xdp->GetOffset();
+//Defhigh = xdp->GetOffhigh();
+//Size = xdp->GetSize();
+ MaxSame = xdp->GetMaxSame();
+ } // end of XINDEX constructor
+
+/***********************************************************************/
+/* XINDEX Reset: re-initialize a Xindex block. */
+/***********************************************************************/
+void XINDEX::Reset(void)
+ {
+ for (PXCOL kp = To_KeyCol; kp; kp = kp->Next)
+ kp->Val_K = kp->Ndf;
+
+ Cur_K = Num_K;
+ Old_K = -1; // Needed to avoid not setting CurBlk for Update
+ Op = (Op == OP_FIRST || Op == OP_NEXT) ? OP_FIRST :
+ (Op == OP_FSTDIF || Op == OP_NXTDIF) ? OP_FSTDIF : OP_EQ;
+ Nth = 0;
+ } // end of Reset
+
+/***********************************************************************/
+/* XINDEX Close: terminate index and free all allocated data. */
+/* Do not reset other values that are used at return to make. */
+/***********************************************************************/
+void XINDEX::Close(void)
+ {
+ // Close file or view of file
+ X->Close();
+
+ // De-allocate data
+ PlgDBfree(Record);
+ PlgDBfree(Index);
+ PlgDBfree(Offset);
+
+ // De-allocate Key data
+ for (PXCOL kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->FreeData();
+
+ // Column values cannot be retrieved from key anymore
+ for (int k = 0; k < Nk; k++)
+ To_Cols[k]->SetKcol(NULL);
+
+ } // end of Close
+
+/***********************************************************************/
+/* XINDEX compare routine for C Quick/Insertion sort. */
+/***********************************************************************/
+int XINDEX::Qcompare(int *i1, int *i2)
+ {
+ register int k;
+ register PXCOL kcp;
+
+ for (kcp = To_KeyCol, k = 0; kcp; kcp = kcp->Next)
+ if ((k = kcp->Compare(*i1, *i2)))
+ break;
+
+//num_comp++;
+ return k;
+ } // end of Qcompare
+
+/***********************************************************************/
+/* Make: Make and index on key column(s). */
+/***********************************************************************/
+bool XINDEX::Make(PGLOBAL g, PIXDEF sxp)
+ {
+ /*********************************************************************/
+ /* Table can be accessed through an index. */
+ /*********************************************************************/
+ int k, rc = RC_OK;
+ int *bof, i, j, n, ndf, nkey;
+ PKPDEF kdfp = Xdp->GetToKeyParts();
+ bool brc = true;
+ PCOL colp;
+ PXCOL kp, prev = NULL, kcp = NULL;
+ PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
+
+ /*********************************************************************/
+ /* Allocate the storage that will contain the keys and the file */
+ /* positions corresponding to them. */
+ /*********************************************************************/
+ if ((n = Tdbp->GetMaxSize(g)) < 0)
+ return true;
+ else if (!n) {
+ Num_K = Ndif = 0;
+ MaxSame = 1;
+
+ // The if condition was suppressed because this may be an existing
+ // index that is now void because all table lines were deleted.
+// if (sxp)
+ goto nox; // Truncate eventually existing index file
+// else
+// return false;
+
+ } // endif n
+
+ // File position must be stored
+ Record.Size = n * sizeof(int);
+
+ if (!PlgDBalloc(g, NULL, Record)) {
+ sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", n);
+ goto err; // Error
+ } // endif
+
+ /*********************************************************************/
+ /* Allocate the KXYCOL blocks used to store column values. */
+ /*********************************************************************/
+ for (k = 0; k < Nk; k++) {
+ colp = To_Cols[k];
+
+ if (!kdfp) {
+ sprintf(g->Message, MSG(INT_COL_ERROR),
+ (colp) ? colp->GetName() : "???");
+ goto err; // Error
+ } // endif kdfp
+
+ kcp = new(g) KXYCOL(this);
+
+ if (kcp->Init(g, colp, n, true, kdfp->Klen))
+ goto err; // Error
+
+ if (prev) {
+ kcp->Previous = prev;
+ prev->Next = kcp;
+ } else
+ To_KeyCol = kcp;
+
+ prev = kcp;
+ kdfp = kdfp->Next;
+ } // endfor k
+
+ To_LastCol = prev;
+
+ /*********************************************************************/
+ /* Get the starting information for progress. */
+ /*********************************************************************/
+ dup->Step = (char*)PlugSubAlloc(g, NULL, 128);
+ sprintf((char*)dup->Step, MSG(BUILD_INDEX), Xdp->GetName(), Tdbp->Name);
+ dup->ProgMax = Tdbp->GetProgMax(g);
+ dup->ProgCur = 0;
+
+ /*********************************************************************/
+ /* Standard init: read the file and construct the index table. */
+ /* Note: reading will be sequential as To_Kindex is not set. */
+ /*********************************************************************/
+ for (i = nkey = 0; i < n && rc != RC_EF; i++) {
+#if defined(THREAD)
+ if (!dup->Step) {
+ strcpy(g->Message, MSG(QUERY_CANCELLED));
+ longjmp(g->jumper[g->jump_level], 99);
+ } // endif Step
+#endif // THREAD
+
+ /*******************************************************************/
+ /* Read a valid record from table file. */
+ /*******************************************************************/
+ rc = Tdbp->ReadDB(g);
+
+ // Update progress information
+ dup->ProgCur = Tdbp->GetProgCur();
+
+ // Check return code and do whatever must be done according to it
+ switch (rc) {
+ case RC_OK:
+ break;
+ case RC_EF:
+ goto end_of_file;
+ case RC_NF:
+ continue;
+ default:
+ sprintf(g->Message, MSG(RC_READING), rc, Tdbp->Name);
+ goto err;
+ } // endswitch rc
+
+ /*******************************************************************/
+ /* Get and Store the file position of the last read record for */
+ /* future direct access. */
+ /*******************************************************************/
+ To_Rec[nkey] = Tdbp->GetRecpos();
+
+ /*******************************************************************/
+ /* Get the keys and place them in the key blocks. */
+ /*******************************************************************/
+ for (k = 0, kcp = To_KeyCol;
+ k < Nk && kcp;
+ k++, kcp = kcp->Next) {
+ colp = To_Cols[k];
+ colp->Reset();
+
+ colp->ReadColumn(g);
+// if (colp->ReadColumn(g))
+// goto err;
+
+ kcp->SetValue(colp, nkey);
+ } // endfor k
+
+ nkey++; // A new valid key was found
+ } // endfor i
+
+ end_of_file:
+
+ // Update progress information
+ dup->ProgCur = Tdbp->GetProgMax(g);
+
+ /*********************************************************************/
+ /* Record the Index size and eventually resize memory allocation. */
+ /*********************************************************************/
+ if ((Num_K = nkey) < n) {
+ PlgDBrealloc(g, NULL, Record, Num_K * sizeof(int));
+
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->ReAlloc(g, Num_K);
+
+ } // endif Num_K
+
+ /*********************************************************************/
+ /* Sort the index so we can use an optimized Find algorithm. */
+ /* Note: for a unique index we use the non conservative sort */
+ /* version because normally all index values are different. */
+ /* This was set at CSORT class construction. */
+ /* For all indexes, an offset array is made so we can check the */
+ /* uniqueness of unique indexes. */
+ /*********************************************************************/
+ Index.Size = Num_K * sizeof(int);
+
+ if (!PlgDBalloc(g, NULL, Index)) {
+ sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
+ goto err; // Error
+ } // endif alloc
+
+ Offset.Size = (Num_K + 1) * sizeof(int);
+
+ if (!PlgDBalloc(g, NULL, Offset)) {
+ sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Num_K + 1);
+ goto err; // Error
+ } // endif alloc
+
+ // Call the sort program, it returns the number of distinct values
+ if ((Ndif = Qsort(g, Num_K)) < 0)
+ goto err; // Error during sort
+
+ // Check whether the unique index is unique indeed
+ if (!Mul)
+ if (Ndif < Num_K) {
+ strcpy(g->Message, MSG(INDEX_NOT_UNIQ));
+ goto err;
+ } else
+ PlgDBfree(Offset); // Not used anymore
+
+ // Use the index to physically reorder the xindex
+ Srtd = Reorder(g);
+
+ if (Ndif < Num_K) {
+ // Resize the offset array
+ PlgDBrealloc(g, NULL, Offset, (Ndif + 1) * sizeof(int));
+
+ // Initial value of MaxSame
+ MaxSame = Pof[1] - Pof[0];
+
+ // Resize the Key array by only keeping the distinct values
+ for (i = 1; i < Ndif; i++) {
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->Move(i, Pof[i]);
+
+ MaxSame = max(MaxSame, Pof[i + 1] - Pof[i]);
+ } // endfor i
+
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->ReAlloc(g, Ndif);
+
+ } else {
+ Mul = false; // Current index is unique
+ PlgDBfree(Offset); // Not used anymore
+ MaxSame = 1; // Reset it when remaking an index
+ } // endif Ndif
+
+ /*********************************************************************/
+ /* Now do the reduction of the index. Indeed a multi-column index */
+ /* can be used for only some of the first columns. For instance if */
+ /* an index is defined for column A, B, C PlugDB can use it for */
+ /* only the column A or the columns A, B. */
+ /* What we do here is to reduce the data so column A will contain */
+ /* only the sorted distinct values of A, B will contain data such */
+ /* as only distinct values of A,B are stored etc. */
+ /* This implies that for each column set an offset array is made */
+ /* except if the subset originally contains unique values. */
+ /*********************************************************************/
+ // Update progress information
+ dup->Step = STEP(REDUCE_INDEX);
+
+ ndf = Ndif;
+ To_LastCol->Mxs = MaxSame;
+
+ for (kcp = To_LastCol->Previous; kcp; kcp = kcp->Previous) {
+ if (!(bof = kcp->MakeOffset(g, ndf)))
+ goto err;
+ else
+ *bof = 0;
+
+ for (n = 0, i = j = 1; i < ndf; i++)
+ for (kp = kcp; kp; kp = kp->Previous)
+ if (kp->Compare(n, i)) {
+ // Values are not equal to last ones
+ bof[j++] = n = i;
+ break;
+ } // endif Compare
+
+ if (j < ndf) {
+ // Sub-index is multiple
+ bof[j] = ndf;
+ ndf = j; // New number of distinct values
+
+ // Resize the Key array by only keeping the distinct values
+ for (kp = kcp; kp; kp = kp->Previous) {
+ for (i = 1; i < ndf; i++)
+ kp->Move(i, bof[i]);
+
+ kp->ReAlloc(g, ndf);
+ } // endif kcp
+
+ // Resize the offset array
+ kcp->MakeOffset(g, ndf);
+
+ // Calculate the max same value for this column
+ kcp->Mxs = ColMaxSame(kcp);
+ } else {
+ // Current sub-index is unique
+ kcp->MakeOffset(g, 0); // The offset is not used anymore
+ kcp->Mxs = 1; // Unique
+ } // endif j
+
+ } // endfor kcp
+
+ /*********************************************************************/
+ /* For sorted columns and fixed record size, file position can be */
+ /* calculated, so the Record array can be discarted. */
+ /* Note: for Num_K = 1 any non null value is Ok. */
+ /*********************************************************************/
+ if (Srtd && Tdbp->Ftype != RECFM_VAR) {
+ Incr = (Num_K > 1) ? To_Rec[1] : Num_K;
+ PlgDBfree(Record);
+ } // endif Srtd
+
+ /*********************************************************************/
+ /* Check whether a two-tier find algorithm can be implemented. */
+ /* It is currently implemented only for single key indexes. */
+ /*********************************************************************/
+ if (Nk == 1 && ndf >= 65536) {
+ // Implement a two-tier find algorithm
+ for (Sblk = 256; (Sblk * Sblk * 4) < ndf; Sblk *= 2) ;
+
+ Nblk = (ndf -1) / Sblk + 1;
+
+ if (To_KeyCol->MakeBlockArray(g, Nblk, Sblk))
+ goto err; // Error
+
+ } // endif Num_K
+
+ nox:
+ /*********************************************************************/
+ /* No valid record read yet for secondary file. */
+ /*********************************************************************/
+ Cur_K = Num_K;
+
+ /*********************************************************************/
+ /* Save the index so it has not to be recalculated. */
+ /*********************************************************************/
+ if (!SaveIndex(g, sxp))
+ brc = false;
+
+ err:
+ // We don't need the index anymore
+ Close();
+
+ if (brc)
+ printf("%s\n", g->Message);
+
+ return brc;
+ } // end of Make
+
+/***********************************************************************/
+/* Return the max size of the intermediate column. */
+/***********************************************************************/
+int XINDEX::ColMaxSame(PXCOL kp)
+ {
+ int *kof, i, ck1, ck2, ckn = 1;
+ PXCOL kcp;
+
+ // Calculate the max same value for this column
+ for (i = 0; i < kp->Ndf; i++) {
+ ck1 = i;
+ ck2 = i + 1;
+
+ for (kcp = kp; kcp; kcp = kcp->Next) {
+ if (!(kof = (kcp->Next) ? kcp->Kof : Pof))
+ break;
+
+ ck1 = kof[ck1];
+ ck2 = kof[ck2];
+ } // endfor kcp
+
+ ckn = max(ckn, ck2 - ck1);
+ } // endfor i
+
+ return ckn;
+ } // end of ColMaxSame
+
+/***********************************************************************/
+/* Reorder: use the sort index to reorder the data in storage so */
+/* it will be physically sorted and sort index can be removed. */
+/***********************************************************************/
+bool XINDEX::Reorder(PGLOBAL g)
+ {
+ register int i, j, k, n;
+ bool sorted = true;
+ PXCOL kcp;
+ PDBUSER dup = (PDBUSER)g->Activityp->Aptr;
+
+ if (Num_K > 500000) {
+ // Update progress information
+ dup->Step = STEP(REORDER_INDEX);
+ dup->ProgMax = Num_K;
+ dup->ProgCur = 0;
+ } else
+ dup = NULL;
+
+ if (!Pex)
+ return Srtd;
+
+ for (i = 0; i < Num_K; i++) {
+ if (Pex[i] == Num_K) { // Already moved
+ continue;
+ } else if (Pex[i] == i) { // Already placed
+ if (dup)
+ dup->ProgCur++;
+
+ continue;
+ } // endif's Pex
+
+ sorted = false;
+
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->Save(i);
+
+ n = To_Rec[i];
+
+ for (j = i;; j = k) {
+ k = Pex[j];
+ Pex[j] = Num_K; // Mark position as set
+
+ if (k == i) {
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->Restore(j);
+
+ To_Rec[j] = n;
+ break; // end of loop
+ } else {
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->Move(j, k); // Move k to j
+
+ To_Rec[j] = To_Rec[k];
+ } // endif k
+
+ if (dup)
+ dup->ProgCur++;
+
+ } // endfor j
+
+ } // endfor i
+
+ // The index is not used anymore
+ PlgDBfree(Index);
+ return sorted;
+ } // end of Reorder
+
+/***********************************************************************/
+/* Save the index values for this table. */
+/* The problem here is to avoid name duplication, because more than */
+/* one data file can have the same name (but different types) and/or */
+/* the same data file can be used with different block sizes. This is */
+/* why we use Ofn that defaults to the file name but can be set to a */
+/* different name if necessary. */
+/***********************************************************************/
+bool XINDEX::SaveIndex(PGLOBAL g, PIXDEF sxp)
+ {
+ char *ftype;
+ char fn[_MAX_PATH];
+ int n[NZ], nof = (Mul) ? (Ndif + 1) : 0;
+ int id = -1, size = 0;
+ bool sep, rc = false;
+ PXCOL kcp = To_KeyCol;
+ PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
+ PDBUSER dup = PlgGetUser(g);
+
+ dup->Step = STEP(SAVING_INDEX);
+ dup->ProgMax = 15 + 16 * Nk;
+ dup->ProgCur = 0;
+
+ switch (Tdbp->Ftype) {
+ case RECFM_VAR: ftype = ".dnx"; break;
+ case RECFM_FIX: ftype = ".fnx"; break;
+ case RECFM_BIN: ftype = ".bnx"; break;
+ case RECFM_VCT: ftype = ".vnx"; break;
+ case RECFM_DBF: ftype = ".dbx"; break;
+ default:
+ sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
+ return true;
+ } // endswitch Ftype
+
+ if ((sep = dup->Catalog->GetBoolCatInfo("SepIndex", false))) {
+ // Index is saved in a separate file
+#if !defined(UNIX)
+ char drive[_MAX_DRIVE];
+#else
+ char *drive = NULL;
+#endif
+ char direc[_MAX_DIR];
+ char fname[_MAX_FNAME];
+
+ _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
+ strcat(strcat(fname, "_"), Xdp->GetName());
+ _makepath(fn, drive, direc, fname, ftype);
+ sxp = NULL;
+ } else {
+ id = ID;
+ strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
+ } // endif sep
+
+ PlugSetPath(fn, fn, Tdbp->GetPath());
+
+ if (X->Open(g, fn, id, (sxp) ? MODE_INSERT : MODE_WRITE)) {
+ printf("%s\n", g->Message);
+ return true;
+ } // endif Open
+
+ if (!Ndif)
+ goto end; // Void index
+
+ /*********************************************************************/
+ /* Write the index values on the index file. */
+ /*********************************************************************/
+ n[0] = ID; // To check validity
+ n[1] = Nk; // The number of indexed columns
+ n[2] = nof; // The offset array size or 0
+ n[3] = Num_K; // The index size
+ n[4] = Incr; // Increment of record positions
+ n[5] = Nblk; n[6] = Sblk;
+
+ if (trace) {
+ htrc("Saving index %s\n", Xdp->GetName());
+ htrc("ID=%d Nk=%d nof=%d Num_K=%d Incr=%d Nblk=%d Sblk=%d\n",
+ ID, Nk, nof, Num_K, Incr, Nblk, Sblk);
+ } // endif trace
+
+ size = X->Write(g, n, NZ, sizeof(int), rc);
+ dup->ProgCur = 1;
+
+ if (Mul) // Write the offset array
+ size += X->Write(g, Pof, nof, sizeof(int), rc);
+
+ dup->ProgCur = 5;
+
+ if (!Incr) // Write the record position array(s)
+ size += X->Write(g, To_Rec, Num_K, sizeof(int), rc);
+
+ dup->ProgCur = 15;
+
+ for (; kcp; kcp = kcp->Next) {
+ n[0] = kcp->Ndf; // Number of distinct sub-values
+ n[1] = (kcp->Kof) ? kcp->Ndf + 1 : 0; // 0 if unique
+ n[2] = (kcp == To_KeyCol) ? Nblk : 0;
+ n[3] = kcp->Klen; // To be checked later
+ n[4] = kcp->Type; // To be checked later
+
+ size += X->Write(g, n, NW, sizeof(int), rc);
+ dup->ProgCur += 1;
+
+ if (n[2])
+ size += X->Write(g, kcp->To_Bkeys, Nblk, kcp->Klen, rc);
+
+ dup->ProgCur += 5;
+
+ size += X->Write(g, kcp->To_Keys, n[0], kcp->Klen, rc);
+ dup->ProgCur += 5;
+
+ if (n[1])
+ size += X->Write(g, kcp->Kof, n[1], sizeof(int), rc);
+
+ dup->ProgCur += 5;
+ } // endfor kcp
+
+ if (trace)
+ htrc("Index %s saved, Size=%d\n", Xdp->GetName(), size);
+
+ end:
+ X->Close(fn, id);
+ return rc;
+ } // end of SaveIndex
+
+#if !defined(XMAP)
+/***********************************************************************/
+/* Init: Open and Initialize a Key Index. */
+/***********************************************************************/
+bool XINDEX::Init(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Table will be accessed through an index table. */
+ /* If sorting is required, this will be done later. */
+ /*********************************************************************/
+ char *ftype;
+ char fn[_MAX_PATH];
+ int k, n, nv[NZ], id = -1;
+ bool estim = false;
+ PCOL colp;
+ PXCOL prev = NULL, kcp = NULL;
+ PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
+
+ /*********************************************************************/
+ /* Get the estimated table size. */
+ /* Note: for fixed tables we must use cardinality to avoid the call */
+ /* to MaxBlkSize that could reduce the cardinality value. */
+ /*********************************************************************/
+ if (Tdbp->Cardinality(NULL)) {
+ // For DBF tables, Cardinality includes bad or soft deleted lines
+ // that are not included in the index, and can be larger then the
+ // index size.
+ estim = (Tdbp->Ftype == RECFM_DBF);
+ n = Tdbp->Cardinality(g); // n is exact table size
+ } else {
+ // Variable table not optimized
+ estim = true; // n is an estimate of the size
+ n = Tdbp->GetMaxSize(g);
+ } // endif Cardinality
+
+ if (n <= 0)
+ return !(n == 0); // n < 0 error, n = 0 void table
+
+ /*********************************************************************/
+ /* Get the first key column. */
+ /*********************************************************************/
+ if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
+ strcpy(g->Message, MSG(NO_KEY_COL));
+ return true; // Error
+ } else
+ colp = To_Cols[0];
+
+ switch (Tdbp->Ftype) {
+ case RECFM_VAR: ftype = ".dnx"; break;
+ case RECFM_FIX: ftype = ".fnx"; break;
+ case RECFM_BIN: ftype = ".bnx"; break;
+ case RECFM_VCT: ftype = ".vnx"; break;
+ case RECFM_DBF: ftype = ".dbx"; break;
+ default:
+ sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
+ return true;
+ } // endswitch Ftype
+
+ if (defp->SepIndex()) {
+ // Index was saved in a separate file
+#if !defined(UNIX)
+ char drive[_MAX_DRIVE];
+#else
+ char *drive = NULL;
+#endif
+ char direc[_MAX_DIR];
+ char fname[_MAX_FNAME];
+
+ _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
+ strcat(strcat(fname, "_"), Xdp->GetName());
+ _makepath(fn, drive, direc, fname, ftype);
+ } else {
+ id = ID;
+ strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
+ } // endif sep
+
+ PlugSetPath(fn, fn, Tdbp->GetPath());
+
+ if (trace)
+ htrc("Index %s file: %s\n", Xdp->GetName(), fn);
+
+ /*********************************************************************/
+ /* Open the index file and check its validity. */
+ /*********************************************************************/
+ if (X->Open(g, fn, id, MODE_READ))
+ goto err; // No saved values
+
+ // Now start the reading process.
+ if (X->Read(g, nv, NZ, sizeof(int)))
+ goto err;
+
+ if (trace)
+ htrc("nv=%d %d %d %d %d %d %d\n",
+ nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6]);
+
+ // The test on ID was suppressed because MariaDB can change an index ID
+ // when other indexes are added or deleted
+ if (/*nv[0] != ID ||*/ nv[1] != Nk) {
+ sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
+
+ if (trace)
+ htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
+
+ goto err;
+ } // endif
+
+ if (nv[2]) {
+ Mul = true;
+ Ndif = nv[2];
+
+ // Allocate the storage that will contain the offset array
+ Offset.Size = Ndif * sizeof(int);
+
+ if (!PlgDBalloc(g, NULL, Offset)) {
+ sprintf(g->Message, MSG(MEM_ALLOC_ERR), "offset", Ndif);
+ goto err;
+ } // endif
+
+ if (X->Read(g, Pof, Ndif, sizeof(int)))
+ goto err;
+
+ Ndif--; // nv[2] is offset size, equal to Ndif + 1
+ } else {
+ Mul = false;
+ Ndif = nv[3];
+ } // endif nv[2]
+
+ if (nv[3] < n && estim)
+ n = nv[3]; // n was just an evaluated max value
+
+ if (nv[3] != n) {
+ sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
+ goto err;
+ } // endif
+
+ Num_K = nv[3];
+ Incr = nv[4];
+ Nblk = nv[5];
+ Sblk = nv[6];
+
+ if (!Incr) {
+ /*******************************************************************/
+ /* Allocate the storage that will contain the file positions. */
+ /*******************************************************************/
+ Record.Size = Num_K * sizeof(int);
+
+ if (!PlgDBalloc(g, NULL, Record)) {
+ sprintf(g->Message, MSG(MEM_ALLOC_ERR), "index", Num_K);
+ goto err;
+ } // endif
+
+ if (X->Read(g, To_Rec, Num_K, sizeof(int)))
+ goto err;
+
+ } else
+ Srtd = true; // Sorted positions can be calculated
+
+ /*********************************************************************/
+ /* Allocate the KXYCOL blocks used to store column values. */
+ /*********************************************************************/
+ for (k = 0; k < Nk; k++) {
+ if (k == Nval)
+ To_LastVal = prev;
+
+ if (X->Read(g, nv, NW, sizeof(int)))
+ goto err;
+
+ colp = To_Cols[k];
+
+ if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
+ (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
+ sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
+ goto err; // Error
+ } // endif GetKey
+
+ kcp = new(g) KXYCOL(this);
+
+ if (kcp->Init(g, colp, nv[0], true, (int)nv[3]))
+ goto err; // Error
+
+ /*******************************************************************/
+ /* Read the index values from the index file. */
+ /*******************************************************************/
+ if (k == 0 && Nblk) {
+ if (kcp->MakeBlockArray(g, Nblk, 0))
+ goto err;
+
+ // Read block values
+ if (X->Read(g, kcp->To_Bkeys, Nblk, kcp->Klen))
+ goto err;
+
+ } // endif Nblk
+
+ // Read the entire (small) index
+ if (X->Read(g, kcp->To_Keys, nv[0], kcp->Klen))
+ goto err;
+
+ if (nv[1]) {
+ if (!kcp->MakeOffset(g, nv[1] - 1))
+ goto err;
+
+ // Read the offset array
+ if (X->Read(g, kcp->Kof, nv[1], sizeof(int)))
+ goto err;
+
+ } // endif n[1]
+
+ if (!kcp->Prefix)
+ // Indicate that the key column value can be found from KXYCOL
+ colp->SetKcol(kcp);
+
+ if (prev) {
+ kcp->Previous = prev;
+ prev->Next = kcp;
+ } else
+ To_KeyCol = kcp;
+
+ prev = kcp;
+ } // endfor k
+
+ To_LastCol = prev;
+
+ if (Mul && prev) {
+ // Last key offset is the index offset
+ kcp->Koff = Offset;
+ kcp->Koff.Sub = true;
+ } // endif Mul
+
+ X->Close();
+
+ /*********************************************************************/
+ /* No valid record read yet for secondary file. */
+ /*********************************************************************/
+ Cur_K = Num_K;
+ return false;
+
+err:
+ Close();
+ return true;
+ } // end of Init
+
+#else // XMAP
+/***********************************************************************/
+/* Init: Open and Initialize a Key Index. */
+/***********************************************************************/
+bool XINDEX::Init(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Table will be accessed through an index table. */
+ /* If sorting is required, this will be done later. */
+ /*********************************************************************/
+ const char *ftype;
+ BYTE *mbase;
+ char fn[_MAX_PATH];
+ int *nv, k, n, id = -1;
+ bool estim;
+ PCOL colp;
+ PXCOL prev = NULL, kcp = NULL;
+ PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
+ PDBUSER dup = PlgGetUser(g);
+
+ /*********************************************************************/
+ /* Get the estimated table size. */
+ /* Note: for fixed tables we must use cardinality to avoid the call */
+ /* to MaxBlkSize that could reduce the cardinality value. */
+ /*********************************************************************/
+ if (Tdbp->Cardinality(NULL)) {
+ // For DBF tables, Cardinality includes bad or soft deleted lines
+ // that are not included in the index, and can be larger then the
+ // index size.
+ estim = (Tdbp->Ftype == RECFM_DBF);
+ n = Tdbp->Cardinality(g); // n is exact table size
+ } else {
+ // Variable table not optimized
+ estim = true; // n is an estimate of the size
+ n = Tdbp->GetMaxSize(g);
+ } // endif Cardinality
+
+ if (n <= 0)
+ return !(n == 0); // n < 0 error, n = 0 void table
+
+ /*********************************************************************/
+ /* Get the first key column. */
+ /*********************************************************************/
+ if (!Nk || !To_Cols || (!To_Vals && Op != OP_FIRST && Op != OP_FSTDIF)) {
+ strcpy(g->Message, MSG(NO_KEY_COL));
+ return true; // Error
+ } else
+ colp = To_Cols[0];
+
+ switch (Tdbp->Ftype) {
+ case RECFM_VAR: ftype = ".dnx"; break;
+ case RECFM_FIX: ftype = ".fnx"; break;
+ case RECFM_BIN: ftype = ".bnx"; break;
+ case RECFM_VCT: ftype = ".vnx"; break;
+ case RECFM_DBF: ftype = ".dbx"; break;
+ default:
+ sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
+ return true;
+ } // endswitch Ftype
+
+ if (defp->SepIndex()) {
+ // Index was save in a separate file
+#if !defined(UNIX)
+ char drive[_MAX_DRIVE];
+#else
+ char *drive = NULL;
+#endif
+ char direc[_MAX_DIR];
+ char fname[_MAX_FNAME];
+
+ _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
+ strcat(strcat(fname, "_"), Xdp->GetName());
+ _makepath(fn, drive, direc, fname, ftype);
+ } else {
+ id = ID;
+ strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
+ } // endif SepIndex
+
+ PlugSetPath(fn, fn, Tdbp->GetPath());
+
+ if (trace)
+ htrc("Index %s file: %s\n", Xdp->GetName(), fn);
+
+ /*********************************************************************/
+ /* Get a view on the part of the index file containing this index. */
+ /*********************************************************************/
+ if (!(mbase = (BYTE*)X->FileView(g, fn)))
+ goto err;
+
+ if (id >= 0) {
+ // Get offset from the header
+ IOFF *noff = (IOFF*)mbase;
+
+ // Position the memory base at the offset of this index
+ mbase += noff[id].Low;
+ } // endif id
+
+ // Now start the mapping process.
+ nv = (int*)mbase;
+ mbase += NZ * sizeof(int);
+
+ if (trace)
+ htrc("nv=%d %d %d %d %d %d %d\n",
+ nv[0], nv[1], nv[2], nv[3], nv[4], nv[5], nv[6]);
+
+ // The test on ID was suppressed because MariaDB can change an index ID
+ // when other indexes are added or deleted
+ if (/*nv[0] != ID ||*/ nv[1] != Nk) {
+ // Not this index
+ sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
+
+ if (trace)
+ htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
+
+ goto err;
+ } // endif nv
+
+ if (nv[2]) {
+ // Set the offset array memory block
+ Offset.Memp = mbase;
+ Offset.Size = nv[2] * sizeof(int);
+ Offset.Sub = true;
+ Mul = true;
+ Ndif = nv[2] - 1;
+ mbase += Offset.Size;
+ } else {
+ Mul = false;
+ Ndif = nv[3];
+ } // endif nv[2]
+
+ if (nv[3] < n && estim)
+ n = nv[3]; // n was just an evaluated max value
+
+ if (nv[3] != n) {
+ sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
+ goto err;
+ } // endif
+
+ Num_K = nv[3];
+ Incr = nv[4];
+ Nblk = nv[5];
+ Sblk = nv[6];
+
+ if (!Incr) {
+ /*******************************************************************/
+ /* Point to the storage that contains the file positions. */
+ /*******************************************************************/
+ Record.Size = Num_K * sizeof(int);
+ Record.Memp = mbase;
+ Record.Sub = true;
+ mbase += Record.Size;
+ } else
+ Srtd = true; // Sorted positions can be calculated
+
+ /*********************************************************************/
+ /* Allocate the KXYCOL blocks used to store column values. */
+ /*********************************************************************/
+ for (k = 0; k < Nk; k++) {
+ if (k == Nval)
+ To_LastVal = prev;
+
+ nv = (int*)mbase;
+ mbase += (NW * sizeof(int));
+
+ colp = To_Cols[k];
+
+ if (nv[4] != colp->GetResultType() || !colp->GetValue() ||
+ (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
+ sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
+ goto err; // Error
+ } // endif GetKey
+
+ kcp = new(g) KXYCOL(this);
+
+ if (!(mbase = kcp->MapInit(g, colp, nv, mbase)))
+ goto err;
+
+ if (!kcp->Prefix)
+ // Indicate that the key column value can be found from KXYCOL
+ colp->SetKcol(kcp);
+
+ if (prev) {
+ kcp->Previous = prev;
+ prev->Next = kcp;
+ } else
+ To_KeyCol = kcp;
+
+ prev = kcp;
+ } // endfor k
+
+ To_LastCol = prev;
+
+ if (Mul && prev)
+ // Last key offset is the index offset
+ kcp->Koff = Offset;
+
+ /*********************************************************************/
+ /* No valid record read yet for secondary file. */
+ /*********************************************************************/
+ Cur_K = Num_K;
+ return false;
+
+err:
+ Close();
+ return true;
+ } // end of Init
+#endif // XMAP
+
+/***********************************************************************/
+/* Get Ndif and Num_K from the index file. */
+/***********************************************************************/
+bool XINDEX::GetAllSizes(PGLOBAL g, int &ndif, int &numk)
+ {
+ char *ftype;
+ char fn[_MAX_PATH];
+ int n, nv[NZ], id = -1;
+ bool estim = false;
+ PDOSDEF defp = (PDOSDEF)Tdbp->To_Def;
+
+ ndif = numk = 0;
+
+ /*********************************************************************/
+ /* Get the estimated table size. */
+ /* Note: for fixed tables we must use cardinality to avoid the call */
+ /* to MaxBlkSize that could reduce the cardinality value. */
+ /*********************************************************************/
+ if (Tdbp->Cardinality(NULL)) {
+ // For DBF tables, Cardinality includes bad or soft deleted lines
+ // that are not included in the index, and can be larger then the
+ // index size.
+ estim = (Tdbp->Ftype == RECFM_DBF);
+ n = Tdbp->Cardinality(g); // n is exact table size
+ } else {
+ // Variable table not optimized
+ estim = true; // n is an estimate of the size
+ n = Tdbp->GetMaxSize(g);
+ } // endif Cardinality
+
+ if (n <= 0)
+ return !(n == 0); // n < 0 error, n = 0 void table
+
+ /*********************************************************************/
+ /* Check the key part number. */
+ /*********************************************************************/
+ if (!Nk) {
+ strcpy(g->Message, MSG(NO_KEY_COL));
+ return true; // Error
+ } // endif Nk
+
+ switch (Tdbp->Ftype) {
+ case RECFM_VAR: ftype = ".dnx"; break;
+ case RECFM_FIX: ftype = ".fnx"; break;
+ case RECFM_BIN: ftype = ".bnx"; break;
+ case RECFM_VCT: ftype = ".vnx"; break;
+ case RECFM_DBF: ftype = ".dbx"; break;
+ default:
+ sprintf(g->Message, MSG(INVALID_FTYPE), Tdbp->Ftype);
+ return true;
+ } // endswitch Ftype
+
+ if (defp->SepIndex()) {
+ // Index was saved in a separate file
+#if !defined(UNIX)
+ char drive[_MAX_DRIVE];
+#else
+ char *drive = NULL;
+#endif
+ char direc[_MAX_DIR];
+ char fname[_MAX_FNAME];
+
+ _splitpath(defp->GetOfn(), drive, direc, fname, NULL);
+ strcat(strcat(fname, "_"), Xdp->GetName());
+ _makepath(fn, drive, direc, fname, ftype);
+ } else {
+ id = ID;
+ strcat(PlugRemoveType(fn, strcpy(fn, defp->GetOfn())), ftype);
+ } // endif sep
+
+ PlugSetPath(fn, fn, Tdbp->GetPath());
+
+ if (trace)
+ htrc("Index %s file: %s\n", Xdp->GetName(), fn);
+
+ /*********************************************************************/
+ /* Open the index file and check its validity. */
+ /*********************************************************************/
+ if (X->Open(g, fn, id, MODE_READ))
+ goto err; // No saved values
+
+ // Get offset from XDB file
+//if (X->Seek(g, Defoff, Defhigh, SEEK_SET))
+// goto err;
+
+ // Now start the reading process.
+ if (X->Read(g, nv, NZ, sizeof(int)))
+ goto err;
+
+ if (trace)
+ htrc("nv=%d %d %d %d\n", nv[0], nv[1], nv[2], nv[3]);
+
+ // The test on ID was suppressed because MariaDB can change an index ID
+ // when other indexes are added or deleted
+ if (/*nv[0] != ID ||*/ nv[1] != Nk) {
+ sprintf(g->Message, MSG(BAD_INDEX_FILE), fn);
+
+ if (trace)
+ htrc("nv[0]=%d ID=%d nv[1]=%d Nk=%d\n", nv[0], ID, nv[1], Nk);
+
+ goto err;
+ } // endif
+
+ if (nv[2]) {
+ Mul = true;
+ Ndif = nv[2] - 1; // nv[2] is offset size, equal to Ndif + 1
+ } else {
+ Mul = false;
+ Ndif = nv[3];
+ } // endif nv[2]
+
+ if (nv[3] < n && estim)
+ n = nv[3]; // n was just an evaluated max value
+
+ if (nv[3] != n) {
+ sprintf(g->Message, MSG(OPT_NOT_MATCH), fn);
+ goto err;
+ } // endif
+
+ Num_K = nv[3];
+
+ if (Nk > 1) {
+ if (nv[2] && X->Seek(g, nv[2] * sizeof(int), 0, SEEK_CUR))
+ goto err;
+
+ if (!nv[4] && X->Seek(g, Num_K * sizeof(int), 0, SEEK_CUR))
+ goto err;
+
+ if (X->Read(g, nv, NW, sizeof(int)))
+ goto err;
+
+ PCOL colp = *To_Cols;
+
+ if (nv[4] != colp->GetResultType() ||
+ (nv[3] != colp->GetValue()->GetClen() && nv[4] != TYPE_STRING)) {
+ sprintf(g->Message, MSG(XCOL_MISMATCH), colp->GetName());
+ goto err; // Error
+ } // endif GetKey
+
+ Ndif = nv[0];
+ } // endif Nk
+
+ /*********************************************************************/
+ /* Set size values. */
+ /*********************************************************************/
+ ndif = Ndif;
+ numk = Num_K;
+ return false;
+
+err:
+ X->Close();
+ return true;
+ } // end of GetAllSizes
+
+/***********************************************************************/
+/* RANGE: Tell how many records exist for a given value, for an array */
+/* of values, or in a given value range. */
+/***********************************************************************/
+int XINDEX::Range(PGLOBAL g, int limit, bool incl)
+ {
+ int i, k, n = 0;
+ PXOB *xp = To_Vals;
+ PXCOL kp = To_KeyCol;
+ OPVAL op = Op;
+
+ switch (limit) {
+ case 1: Op = (incl) ? OP_GE : OP_GT; break;
+ case 2: Op = (incl) ? OP_GT : OP_GE; break;
+ default: return 0;
+ } // endswitch limit
+
+ /*********************************************************************/
+ /* Currently only range of constant values with an EQ operator is */
+ /* implemented. Find the number of rows for each given values. */
+ /*********************************************************************/
+ if (xp[0]->GetType() == TYPE_CONST) {
+ for (i = 0; kp; kp = kp->Next) {
+ kp->Valp->SetValue_pval(xp[i]->GetValue(), !kp->Prefix);
+ if (++i == Nval) break;
+ } // endfor kp
+
+ if ((k = FastFind(Nval)) < Num_K)
+ n = k;
+// if (limit)
+// n = (Mul) ? k : kp->Val_K;
+// else
+// n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
+
+ } else {
+ strcpy(g->Message, MSG(RANGE_NO_JOIN));
+ n = -1; // Logical error
+ } // endif'f Type
+
+ Op = op;
+ return n;
+ } // end of Range
+
+/***********************************************************************/
+/* Return the size of the group (equal values) of the current value. */
+/***********************************************************************/
+int XINDEX::GroupSize(void)
+ {
+#if defined(_DEBUG)
+ assert(To_LastCol->Val_K >= 0 && To_LastCol->Val_K < Ndif);
+#endif // _DEBUG
+
+ if (Nval == Nk)
+ return (Pof) ? Pof[To_LastCol->Val_K + 1] - Pof[To_LastCol->Val_K]
+ : 1;
+
+#if defined(_DEBUG)
+ assert(To_LastVal);
+#endif // _DEBUG
+
+ // Index whose only some columns are used
+ int ck1, ck2;
+
+ ck1 = To_LastVal->Val_K;
+ ck2 = ck1 + 1;
+
+#if defined(_DEBUG)
+ assert(ck1 >= 0 && ck1 < To_LastVal->Ndf);
+#endif // _DEBUG
+
+ for (PXCOL kcp = To_LastVal; kcp; kcp = kcp->Next) {
+ ck1 = (kcp->Kof) ? kcp->Kof[ck1] : ck1;
+ ck2 = (kcp->Kof) ? kcp->Kof[ck2] : ck2;
+ } // endfor kcp
+
+ return ck2 - ck1;
+ } // end of GroupSize
+
+/***********************************************************************/
+/* Find Cur_K and Val_K's of the next distinct value of the index. */
+/* Returns false if Ok, true if there are no more different values. */
+/***********************************************************************/
+bool XINDEX::NextValDif(void)
+ {
+ int curk;
+ PXCOL kcp = (To_LastVal) ? To_LastVal : To_LastCol;
+
+ if (++kcp->Val_K < kcp->Ndf) {
+ Cur_K = curk = kcp->Val_K;
+
+ // (Cur_K return is currently not used by SQLGBX)
+ for (PXCOL kp = kcp; kp; kp = kp->Next)
+ Cur_K = (kp->Kof) ? kp->Kof[Cur_K] : Cur_K;
+
+ } else
+ return true;
+
+ for (kcp = kcp->Previous; kcp; kcp = kcp->Previous) {
+ if (kcp->Kof && curk < kcp->Kof[kcp->Val_K + 1])
+ break; // all previous columns have same value
+
+ curk = ++kcp->Val_K; // This is a break, get new column value
+ } // endfor kcp
+
+ return false;
+ } // end of NextValDif
+
+/***********************************************************************/
+/* XINDEX: Find Cur_K and Val_K's of next index entry. */
+/* If eq is true next values must be equal to last ones up to Nval. */
+/* Returns false if Ok, true if there are no more (equal) values. */
+/***********************************************************************/
+bool XINDEX::NextVal(bool eq)
+ {
+ int n, neq = Nk + 1, curk;
+ PXCOL kcp;
+
+ if (Cur_K == Num_K)
+ return true;
+ else
+ curk = ++Cur_K;
+
+ for (n = Nk, kcp = To_LastCol; kcp; n--, kcp = kcp->Previous) {
+ if (kcp->Kof) {
+ if (curk == kcp->Kof[kcp->Val_K + 1])
+ neq = n;
+
+ } else {
+#ifdef _DEBUG
+ assert(curk == kcp->Val_K + 1);
+#endif // _DEBUG
+ neq = n;
+ } // endif Kof
+
+#ifdef _DEBUG
+ assert(kcp->Val_K < kcp->Ndf);
+#endif // _DEBUG
+
+ // If this is not a break...
+ if (neq > n)
+ break; // all previous columns have same value
+
+ curk = ++kcp->Val_K; // This is a break, get new column value
+ } // endfor kcp
+
+ // Return true if no more values or, in case of "equal" values,
+ // if the last used column value has changed
+ return (Cur_K == Num_K || (eq && neq <= Nval));
+ } // end of NextVal
+
+/***********************************************************************/
+/* XINDEX: Fetch a physical or logical record. */
+/***********************************************************************/
+int XINDEX::Fetch(PGLOBAL g)
+ {
+ int n;
+ PXCOL kp;
+
+ if (Num_K == 0)
+ return -1; // means end of file
+
+ /*********************************************************************/
+ /* Table read through a sorted index. */
+ /*********************************************************************/
+ switch (Op) {
+ case OP_NEXT: // Read next
+ if (NextVal(false))
+ return -1; // End of indexed file
+
+ break;
+ case OP_FIRST: // Read first
+ for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
+ kp->Val_K = 0;
+
+ Op = OP_NEXT;
+ break;
+ case OP_SAME: // Read next same
+ // Logically the key values should be the same as before
+ if (trace > 1)
+ htrc("looking for next same value\n");
+
+ if (NextVal(true)) {
+ Op = OP_EQ;
+ return -2; // no more equal values
+ } // endif NextVal
+
+ break;
+ case OP_NXTDIF: // Read next dif
+// while (!NextVal(true)) ;
+
+// if (Cur_K >= Num_K)
+// return -1; // End of indexed file
+ if (NextValDif())
+ return -1; // End of indexed file
+
+ break;
+ case OP_FSTDIF: // Read first diff
+ for (Cur_K = 0, kp = To_KeyCol; kp; kp = kp->Next)
+ kp->Val_K = 0;
+
+ Op = (Mul || Nval < Nk) ? OP_NXTDIF : OP_NEXT;
+ break;
+ default: // Should be OP_EQ
+// if (Tbxp->Key_Rank < 0) {
+ /***************************************************************/
+ /* Look for the first key equal to the link column values */
+ /* and return its rank whithin the index table. */
+ /***************************************************************/
+ for (n = 0, kp = To_KeyCol; n < Nval && kp; n++, kp = kp->Next)
+ if (kp->InitFind(g, To_Vals[n]))
+ return -1; // No more constant values
+
+ Nth++;
+
+ if (trace > 1)
+ htrc("Fetch: Looking for new value\n");
+
+ Cur_K = FastFind(Nval);
+
+ if (Cur_K >= Num_K)
+ /*************************************************************/
+ /* Rank not whithin index table, signal record not found. */
+ /*************************************************************/
+ return -2;
+
+ else if (Mul || Nval < Nk)
+ Op = OP_SAME;
+
+ } // endswitch Op
+
+ /*********************************************************************/
+ /* If rank is equal to stored rank, record is already there. */
+ /*********************************************************************/
+ if (Cur_K == Old_K)
+ return -3; // Means record already there
+ else
+ Old_K = Cur_K; // Store rank of newly read record
+
+ /*********************************************************************/
+ /* Return the position of the required record. */
+ /*********************************************************************/
+ return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
+ } // end of Fetch
+
+/***********************************************************************/
+/* FastFind: Returns the index of matching record in a join using an */
+/* optimized algorithm based on dichotomie and optimized comparing. */
+/***********************************************************************/
+int XINDEX::FastFind(int nv)
+ {
+ register int curk, sup, inf, i= 0, k, n = 2;
+ register PXCOL kp, kcp;
+
+ assert((int)nv == Nval);
+
+ if (Nblk && Op == OP_EQ) {
+ // Look in block values to find in which block to search
+ sup = Nblk;
+ inf = -1;
+
+ while (n && sup - inf > 1) {
+ i = (inf + sup) >> 1;
+
+ n = To_KeyCol->CompBval(i);
+
+ if (n < 0)
+ sup = i;
+ else
+ inf = i;
+
+ } // endwhile
+
+ if (inf < 0)
+ return Num_K;
+
+// i = inf;
+ inf *= Sblk;
+
+ if ((sup = inf + Sblk) > To_KeyCol->Ndf)
+ sup = To_KeyCol->Ndf;
+
+ inf--;
+ } else {
+ inf = -1;
+ sup = To_KeyCol->Ndf;
+ } // endif Nblk
+
+ for (k = 0, kcp = To_KeyCol; kcp; kcp = kcp->Next) {
+ while (sup - inf > 1) {
+ i = (inf + sup) >> 1;
+
+ n = kcp->CompVal(i);
+
+ if (n < 0)
+ sup = i;
+ else if (n > 0)
+ inf = i;
+ else
+ break;
+
+ } // endwhile
+
+ if (n) {
+ if (Op != OP_EQ) {
+ // Currently only OP_GT or OP_GE
+ kcp->Val_K = curk = sup;
+
+ // Check for value changes in previous key parts
+ for (kp = kcp->Previous; kp; kp = kp->Previous)
+ if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
+ break;
+ else
+ curk = ++kp->Val_K;
+
+ n = 0;
+ } // endif Op
+
+ break;
+ } // endif n
+
+ kcp->Val_K = i;
+
+ if (++k == Nval) {
+ if (Op == OP_GT) { // n is always 0
+ curk = ++kcp->Val_K; // Increment value by 1
+
+ // Check for value changes in previous key parts
+ for (kp = kcp->Previous; kp; kp = kp->Previous)
+ if (kp->Kof && curk < kp->Kof[kp->Val_K + 1])
+ break; // Not changed
+ else
+ curk = ++kp->Val_K;
+
+ } // endif Op
+
+ break; // So kcp remains pointing the last tested block
+ } // endif k
+
+ if (kcp->Kof) {
+ inf = kcp->Kof[i] - 1;
+ sup = kcp->Kof[i + 1];
+ } else {
+ inf = i - 1;
+ sup = i + 1;
+ } // endif Kof
+
+ } // endfor k, kcp
+
+ if (n) {
+ // Record not found
+ for (kcp = To_KeyCol; kcp; kcp = kcp->Next)
+ kcp->Val_K = kcp->Ndf; // Not a valid value
+
+ return Num_K;
+ } // endif n
+
+ for (curk = kcp->Val_K; kcp; kcp = kcp->Next) {
+ kcp->Val_K = curk;
+ curk = (kcp->Kof) ? kcp->Kof[kcp->Val_K] : kcp->Val_K;
+ } // endfor kcp
+
+ return curk;
+ } // end of FastFind
+
+/* -------------------------- XINDXS Class --------------------------- */
+
+/***********************************************************************/
+/* XINDXS public constructor. */
+/***********************************************************************/
+XINDXS::XINDXS(PTDBDOS tdbp, PIXDEF xdp, PXLOAD pxp, PCOL *cp, PXOB *xp)
+ : XINDEX(tdbp, xdp, pxp, cp, xp)
+ {
+ Srtd = To_Cols[0]->GetOpt() == 2;
+ } // end of XINDXS constructor
+
+/***********************************************************************/
+/* XINDXS compare routine for C Quick/Insertion sort. */
+/***********************************************************************/
+int XINDXS::Qcompare(int *i1, int *i2)
+ {
+//num_comp++;
+ return To_KeyCol->Compare(*i1, *i2);
+ } // end of Qcompare
+
+/***********************************************************************/
+/* Range: Tell how many records exist for given value(s): */
+/* If limit=0 return range for these values. */
+/* If limit=1 return the start of range. */
+/* If limit=2 return the end of range. */
+/***********************************************************************/
+int XINDXS::Range(PGLOBAL g, int limit, bool incl)
+ {
+ int k, n = 0;
+ PXOB xp = To_Vals[0];
+ PXCOL kp = To_KeyCol;
+ OPVAL op = Op;
+
+ switch (limit) {
+ case 1: Op = (incl) ? OP_GE : OP_GT; break;
+ case 2: Op = (incl) ? OP_GT : OP_GE; break;
+ default: Op = OP_EQ;
+ } // endswitch limit
+
+ /*********************************************************************/
+ /* Currently only range of constant values with an EQ operator is */
+ /* implemented. Find the number of rows for each given values. */
+ /*********************************************************************/
+ if (xp->GetType() == TYPE_CONST) {
+ kp->Valp->SetValue_pval(xp->GetValue(), !kp->Prefix);
+ k = FastFind(Nval);
+
+ if (k < Num_K || Op != OP_EQ)
+ if (limit)
+ n = (Mul) ? k : kp->Val_K;
+ else
+ n = (Mul) ? Pof[kp->Val_K + 1] - k : 1;
+
+ } else {
+ strcpy(g->Message, MSG(RANGE_NO_JOIN));
+ n = -1; // Logical error
+ } // endif'f Type
+
+ Op = op;
+ return n;
+ } // end of Range
+
+/***********************************************************************/
+/* Return the size of the group (equal values) of the current value. */
+/***********************************************************************/
+int XINDXS::GroupSize(void)
+ {
+#if defined(_DEBUG)
+ assert(To_KeyCol->Val_K >= 0 && To_KeyCol->Val_K < Ndif);
+#endif // _DEBUG
+ return (Pof) ? Pof[To_KeyCol->Val_K + 1] - Pof[To_KeyCol->Val_K]
+ : 1;
+ } // end of GroupSize
+
+/***********************************************************************/
+/* XINDXS: Find Cur_K and Val_K of next index value. */
+/* If b is true next value must be equal to last one. */
+/* Returns false if Ok, true if there are no more (equal) values. */
+/***********************************************************************/
+bool XINDXS::NextVal(bool eq)
+ {
+ bool rc;
+
+ if (To_KeyCol->Val_K == Ndif)
+ return true;
+
+ if (Mul) {
+ int limit = Pof[To_KeyCol->Val_K + 1];
+
+#ifdef _DEBUG
+ assert(Cur_K < limit);
+ assert(To_KeyCol->Val_K < Ndif);
+#endif // _DEBUG
+
+ if (++Cur_K == limit) {
+ To_KeyCol->Val_K++;
+ rc = (eq || limit == Num_K);
+ } else
+ rc = false;
+
+ } else
+ rc = (To_KeyCol->Val_K = ++Cur_K) == Num_K || eq;
+
+ return rc;
+ } // end of NextVal
+
+/***********************************************************************/
+/* XINDXS: Fetch a physical or logical record. */
+/***********************************************************************/
+int XINDXS::Fetch(PGLOBAL g)
+ {
+ if (Num_K == 0)
+ return -1; // means end of file
+
+ /*********************************************************************/
+ /* Table read through a sorted index. */
+ /*********************************************************************/
+ switch (Op) {
+ case OP_NEXT: // Read next
+ if (NextVal(false))
+ return -1; // End of indexed file
+
+ break;
+ case OP_FIRST: // Read first
+ To_KeyCol->Val_K = Cur_K = 0;
+ Op = OP_NEXT;
+ break;
+ case OP_SAME: // Read next same
+ if (trace > 1)
+ htrc("looking for next same value\n");
+
+ if (!Mul || NextVal(true)) {
+ Op = OP_EQ;
+ return -2; // No more equal values
+ } // endif Mul
+
+ break;
+ case OP_NXTDIF: // Read next dif
+ if (++To_KeyCol->Val_K == Ndif)
+ return -1; // End of indexed file
+
+ Cur_K = Pof[To_KeyCol->Val_K];
+ break;
+ case OP_FSTDIF: // Read first diff
+ To_KeyCol->Val_K = Cur_K = 0;
+ Op = (Mul) ? OP_NXTDIF : OP_NEXT;
+ break;
+ default: // Should OP_EQ
+ /*****************************************************************/
+ /* Look for the first key equal to the link column values */
+ /* and return its rank whithin the index table. */
+ /*****************************************************************/
+ if (To_KeyCol->InitFind(g, To_Vals[0]))
+ return -1; // No more constant values
+ else
+ Nth++;
+
+ if (trace > 1)
+ htrc("Fetch: Looking for new value\n");
+
+ Cur_K = FastFind(1);
+
+ if (Cur_K >= Num_K)
+ // Rank not whithin index table, signal record not found
+ return -2;
+ else if (Mul)
+ Op = OP_SAME;
+
+ } // endswitch Op
+
+ /*********************************************************************/
+ /* If rank is equal to stored rank, record is already there. */
+ /*********************************************************************/
+ if (Cur_K == Old_K)
+ return -3; // Means record already there
+ else
+ Old_K = Cur_K; // Store rank of newly read record
+
+ /*********************************************************************/
+ /* Return the position of the required record. */
+ /*********************************************************************/
+ return (Incr) ? Cur_K * Incr : To_Rec[Cur_K];
+ } // end of Fetch
+
+/***********************************************************************/
+/* FastFind: Returns the index of matching indexed record using an */
+/* optimized algorithm based on dichotomie and optimized comparing. */
+/***********************************************************************/
+int XINDXS::FastFind(int nk)
+ {
+ register int sup, inf, i= 0, n = 2;
+ register PXCOL kcp = To_KeyCol;
+
+ if (Nblk && Op == OP_EQ) {
+ // Look in block values to find in which block to search
+ sup = Nblk;
+ inf = -1;
+
+ while (n && sup - inf > 1) {
+ i = (inf + sup) >> 1;
+
+ n = kcp->CompBval(i);
+
+ if (n < 0)
+ sup = i;
+ else
+ inf = i;
+
+ } // endwhile
+
+ if (inf < 0)
+ return Num_K;
+
+// i = inf;
+ inf *= Sblk;
+
+ if ((sup = inf + Sblk) > Ndif)
+ sup = Ndif;
+
+ inf--;
+ } else {
+ inf = -1;
+ sup = Ndif;
+ } // endif Nblk
+
+ while (sup - inf > 1) {
+ i = (inf + sup) >> 1;
+
+ n = kcp->CompVal(i);
+
+ if (n < 0)
+ sup = i;
+ else if (n > 0)
+ inf = i;
+ else
+ break;
+
+ } // endwhile
+
+ if (!n && Op == OP_GT) {
+ ++i;
+ } else if (n && Op != OP_EQ) {
+ // Currently only OP_GT or OP_GE
+ i = sup;
+ n = 0;
+ } // endif sup
+
+ kcp->Val_K = i; // Used by FillValue
+ return ((n) ? Num_K : (Mul) ? Pof[i] : i);
+ } // end of FastFind
+
+/* -------------------------- XLOAD Class --------------------------- */
+
+/***********************************************************************/
+/* XLOAD constructor. */
+/***********************************************************************/
+XLOAD::XLOAD(void)
+ {
+ Hfile = INVALID_HANDLE_VALUE;
+#if defined(WIN32) && defined(XMAP)
+ ViewBase = NULL;
+#endif // WIN32 && XMAP
+ NewOff.Val = 0LL;
+} // end of XLOAD constructor
+
+/***********************************************************************/
+/* Close the index huge file. */
+/***********************************************************************/
+void XLOAD::Close(void)
+ {
+ if (Hfile != INVALID_HANDLE_VALUE) {
+ CloseFileHandle(Hfile);
+ Hfile = INVALID_HANDLE_VALUE;
+ } // endif Hfile
+
+#if defined(WIN32) && defined(XMAP)
+ if (ViewBase) {
+ if (!UnmapViewOfFile(ViewBase))
+ printf("Error %d closing Viewmap\n", GetLastError());
+
+ ViewBase = NULL;
+ } // endif ViewBase
+#endif // WIN32 && XMAP
+
+ } // end of Close
+
+/* --------------------------- XFILE Class --------------------------- */
+
+/***********************************************************************/
+/* XFILE constructor. */
+/***********************************************************************/
+XFILE::XFILE(void) : XLOAD()
+ {
+ Xfile = NULL;
+#if defined(XMAP) && !defined(WIN32)
+ Mmp = NULL;
+#endif // XMAP && !WIN32
+ } // end of XFILE constructor
+
+/***********************************************************************/
+/* Xopen function: opens a file using native API's. */
+/***********************************************************************/
+bool XFILE::Open(PGLOBAL g, char *filename, int id, MODE mode)
+ {
+ char *pmod;
+ bool rc;
+ IOFF noff[MAX_INDX];
+
+ /*********************************************************************/
+ /* Open the index file according to mode. */
+ /*********************************************************************/
+ switch (mode) {
+ case MODE_READ: pmod = "rb"; break;
+ case MODE_WRITE: pmod = "wb"; break;
+ case MODE_INSERT: pmod = "ab"; break;
+ default:
+ sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
+ return true;
+ } // endswitch mode
+
+ if (!(Xfile= global_fopen(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, pmod))) {
+ if (trace)
+ htrc("Open: %s\n", g->Message);
+
+ return true;
+ } // endif Xfile
+
+ if (mode == MODE_INSERT) {
+ /*******************************************************************/
+ /* Position the cursor at end of file so ftell returns file size. */
+ /*******************************************************************/
+ if (fseek(Xfile, 0, SEEK_END)) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
+ return true;
+ } // endif
+
+ NewOff.Low = (int)ftell(Xfile);
+ } else if (mode == MODE_WRITE) {
+ if (id >= 0) {
+ // New not sep index file. Write the header.
+ memset(noff, 0, sizeof(noff));
+ Write(g, noff, sizeof(IOFF), MAX_INDX, rc);
+ fseek(Xfile, 0, SEEK_END);
+ NewOff.Low = (int)ftell(Xfile);
+ } // endif id
+
+ } else if (mode == MODE_READ && id >= 0) {
+ // Get offset from the header
+ if (fread(noff, sizeof(IOFF), MAX_INDX, Xfile) != MAX_INDX) {
+ sprintf(g->Message, MSG(XFILE_READERR), errno);
+ return true;
+ } // endif MAX_INDX
+
+ // Position the cursor at the offset of this index
+ if (fseek(Xfile, noff[id].Low, SEEK_SET)) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
+ return true;
+ } // endif
+
+ } // endif mode
+
+ return false;
+ } // end of Open
+
+/***********************************************************************/
+/* Move into an index file. */
+/***********************************************************************/
+bool XFILE::Seek(PGLOBAL g, int low, int high, int origin)
+ {
+#if defined(_DEBUG)
+ assert(high == 0);
+#endif // !_DEBUG
+
+ if (fseek(Xfile, low, origin)) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Xseek");
+ return true;
+ } // endif
+
+//ftell(Xfile);
+ return false;
+ } // end of Seek
+
+/***********************************************************************/
+/* Read from the index file. */
+/***********************************************************************/
+bool XFILE::Read(PGLOBAL g, void *buf, int n, int size)
+ {
+ if (fread(buf, size, n, Xfile) != (size_t)n) {
+ sprintf(g->Message, MSG(XFILE_READERR), errno);
+ return true;
+ } // endif size
+
+ return false;
+ } // end of Read
+
+/***********************************************************************/
+/* Write on index file, set rc and return the number of bytes written */
+/***********************************************************************/
+int XFILE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
+ {
+ int niw = (int)fwrite(buf, size, n, Xfile);
+
+ if (niw != n) {
+ sprintf(g->Message, MSG(XFILE_WRITERR), strerror(errno));
+ rc = true;
+ } // endif size
+
+ return niw * size;
+ } // end of Write
+
+/***********************************************************************/
+/* Update the file header and close the index file. */
+/***********************************************************************/
+void XFILE::Close(char *fn, int id)
+ {
+ if (id >= 0 && fn && Xfile) {
+ fclose(Xfile);
+
+ if ((Xfile = fopen(fn, "r+b")))
+ if (!fseek(Xfile, id * sizeof(IOFF), SEEK_SET))
+ fwrite(&NewOff, sizeof(int), 2, Xfile);
+
+ } // endif id
+
+ Close();
+ } // end of Close
+
+/***********************************************************************/
+/* Close the index file. */
+/***********************************************************************/
+void XFILE::Close(void)
+ {
+ XLOAD::Close();
+
+ if (Xfile) {
+ fclose(Xfile);
+ Xfile = NULL;
+ } // endif Xfile
+
+#if defined(XMAP) && !defined(WIN32)
+ if (Mmp) {
+ CloseMemMap(Mmp->memory, Mmp->lenL);
+ Mmp = NULL;
+ } // endif Mmp
+#endif // XMAP
+ } // end of Close
+
+#if defined(XMAP)
+ /*********************************************************************/
+ /* Map the entire index file. */
+ /*********************************************************************/
+void *XFILE::FileView(PGLOBAL g, char *fn)
+ {
+ HANDLE h;
+
+ Mmp = (MMP)PlugSubAlloc(g, NULL, sizeof(MEMMAP));
+ h = CreateFileMap(g, fn, Mmp, MODE_READ, false);
+
+ if (h == INVALID_HANDLE_VALUE || (!Mmp->lenH && !Mmp->lenL)) {
+ if (!(*g->Message))
+ strcpy(g->Message, MSG(FILE_MAP_ERR));
+
+ CloseFileHandle(h); // Not used anymore
+ return NULL; // No saved values
+ } // endif h
+
+ CloseFileHandle(h); // Not used anymore
+ return Mmp->memory;
+ } // end of FileView
+#endif // XMAP
+
+/* -------------------------- XHUGE Class --------------------------- */
+
+/***********************************************************************/
+/* Xopen function: opens a file using native API's. */
+/***********************************************************************/
+bool XHUGE::Open(PGLOBAL g, char *filename, int id, MODE mode)
+ {
+ IOFF noff[MAX_INDX];
+
+ if (Hfile != INVALID_HANDLE_VALUE) {
+ sprintf(g->Message, MSG(FILE_OPEN_YET), filename);
+ return true;
+ } // endif
+
+ if (trace)
+ htrc(" Xopen: filename=%s mode=%d\n", filename, mode);
+
+#if defined(WIN32)
+ LONG high = 0;
+ DWORD rc, drc, access, share, creation;
+
+ /*********************************************************************/
+ /* Create the file object according to access mode */
+ /*********************************************************************/
+ switch (mode) {
+ case MODE_READ:
+ access = GENERIC_READ;
+ share = FILE_SHARE_READ;
+ creation = OPEN_EXISTING;
+ break;
+ case MODE_WRITE:
+ access = GENERIC_WRITE;
+ share = 0;
+ creation = CREATE_ALWAYS;
+ break;
+ case MODE_INSERT:
+ access = GENERIC_WRITE;
+ share = 0;
+ creation = OPEN_EXISTING;
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
+ return true;
+ } // endswitch
+
+ Hfile = CreateFile(filename, access, share, NULL, creation,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+
+ if (Hfile == INVALID_HANDLE_VALUE) {
+ rc = GetLastError();
+ sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename);
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
+ (LPTSTR)filename, sizeof(filename), NULL);
+ strcat(g->Message, filename);
+ return true;
+ } // endif Hfile
+
+ if (trace)
+ htrc(" access=%p share=%p creation=%d handle=%p fn=%s\n",
+ access, share, creation, Hfile, filename);
+
+ if (mode == MODE_INSERT) {
+ /*******************************************************************/
+ /* In Insert mode we must position the cursor at end of file. */
+ /*******************************************************************/
+ rc = SetFilePointer(Hfile, 0, &high, FILE_END);
+
+ if (rc == INVALID_SET_FILE_POINTER && (drc = GetLastError()) != NO_ERROR) {
+ sprintf(g->Message, MSG(ERROR_IN_SFP), drc);
+ CloseHandle(Hfile);
+ Hfile = INVALID_HANDLE_VALUE;
+ return true;
+ } // endif
+
+ NewOff.Low = (int)rc;
+ NewOff.High = (int)high;
+ } else if (mode == MODE_WRITE) {
+ if (id >= 0) {
+ // New not sep index file. Write the header.
+ memset(noff, 0, sizeof(noff));
+ rc = WriteFile(Hfile, noff, sizeof(noff), &drc, NULL);
+ NewOff.Low = (int)drc;
+ } // endif id
+
+ } else if (mode == MODE_READ && id >= 0) {
+ // Get offset from the header
+ rc = ReadFile(Hfile, noff, sizeof(noff), &drc, NULL);
+
+ if (!rc) {
+ sprintf(g->Message, MSG(XFILE_READERR), GetLastError());
+ return true;
+ } // endif rc
+
+ // Position the cursor at the offset of this index
+ rc = SetFilePointer(Hfile, noff[id].Low,
+ (PLONG)&noff[id].High, FILE_BEGIN);
+
+ if (rc == INVALID_SET_FILE_POINTER) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), GetLastError(), "SetFilePointer");
+ return true;
+ } // endif
+
+ } // endif Mode
+
+#else // UNIX
+ int oflag = O_LARGEFILE; // Enable file size > 2G
+ mode_t pmod = 0;
+
+ /*********************************************************************/
+ /* Create the file object according to access mode */
+ /*********************************************************************/
+ switch (mode) {
+ case MODE_READ:
+ oflag |= O_RDONLY;
+ break;
+ case MODE_WRITE:
+ oflag |= O_WRONLY | O_CREAT | O_TRUNC;
+ pmod = S_IREAD | S_IWRITE;
+ break;
+ case MODE_INSERT:
+ oflag |= (O_WRONLY | O_APPEND);
+ break;
+ default:
+ sprintf(g->Message, MSG(BAD_FUNC_MODE), "Xopen", mode);
+ return true;
+ } // endswitch
+
+ Hfile= global_open(g, MSGID_OPEN_ERROR_AND_STRERROR, filename, oflag, pmod);
+
+ if (Hfile == INVALID_HANDLE_VALUE) {
+ /*rc = errno;*/
+ if (trace)
+ htrc("Open: %s\n", g->Message);
+
+ return true;
+ } // endif Hfile
+
+ if (trace)
+ htrc(" rc=%d oflag=%p mode=%d handle=%d fn=%s\n",
+ rc, oflag, mode, Hfile, filename);
+
+ if (mode == MODE_INSERT) {
+ /*******************************************************************/
+ /* Position the cursor at end of file so ftell returns file size. */
+ /*******************************************************************/
+ if (!(NewOff.Val = (longlong)lseek64(Hfile, 0LL, SEEK_END))) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Seek");
+ return true;
+ } // endif
+
+ } else if (mode == MODE_WRITE) {
+ if (id >= 0) {
+ // New not sep index file. Write the header.
+ memset(noff, 0, sizeof(noff));
+ NewOff.Low = write(Hfile, &noff, sizeof(noff));
+ } // endif id
+
+ } else if (mode == MODE_READ && id >= 0) {
+ // Get offset from the header
+ if (read(Hfile, noff, sizeof(noff)) != sizeof(noff)) {
+ sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
+ return true;
+ } // endif MAX_INDX
+
+ // Position the cursor at the offset of this index
+ if (!lseek64(Hfile, noff[id].Val, SEEK_SET)) {
+ sprintf(g->Message, MSG(FUNC_ERRNO), errno, "Hseek");
+ return true;
+ } // endif
+
+ } // endif mode
+#endif // UNIX
+
+ return false;
+ } // end of Open
+
+/***********************************************************************/
+/* Go to position in a huge file. */
+/***********************************************************************/
+bool XHUGE::Seek(PGLOBAL g, int low, int high, int origin)
+ {
+#if defined(WIN32)
+ LONG hi = high;
+ DWORD rc = SetFilePointer(Hfile, low, &hi, origin);
+
+ if (rc == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
+ sprintf(g->Message, MSG(FUNC_ERROR), "Xseek");
+ return true;
+ } // endif
+
+#else // UNIX
+ off64_t pos = (off64_t)low
+ + (off64_t)high * ((off64_t)0x100 * (off64_t)0x1000000);
+
+ if (lseek64(Hfile, pos, origin) < 0) {
+ sprintf(g->Message, MSG(ERROR_IN_LSK), errno);
+
+ if (trace)
+ htrc("lseek64 error %d\n", errno);
+
+ return true;
+ } // endif lseek64
+
+ if (trace)
+ htrc("Seek: low=%d high=%d\n", low, high);
+#endif // UNIX
+
+ return false;
+ } // end of Seek
+
+/***********************************************************************/
+/* Read from a huge index file. */
+/***********************************************************************/
+bool XHUGE::Read(PGLOBAL g, void *buf, int n, int size)
+ {
+ bool rc = false;
+
+#if defined(WIN32)
+ bool brc;
+ DWORD nbr, count = (DWORD)(n * size);
+
+ brc = ReadFile(Hfile, buf, count, &nbr, NULL);
+
+ if (brc) {
+ if (nbr != count) {
+ strcpy(g->Message, MSG(EOF_INDEX_FILE));
+ rc = true;
+ } // endif nbr
+
+ } else {
+ char *buf[256];
+ DWORD drc = GetLastError();
+
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
+ (LPTSTR)buf, sizeof(buf), NULL);
+ sprintf(g->Message, MSG(READ_ERROR), "index file", buf);
+ rc = true;
+ } // endif brc
+#else // UNIX
+ ssize_t count = (ssize_t)(n * size);
+
+ if (trace)
+ htrc("Hfile=%d n=%d size=%d count=%d\n", Hfile, n, size, count);
+
+ if (read(Hfile, buf, count) != count) {
+ sprintf(g->Message, MSG(READ_ERROR), "Index file", strerror(errno));
+
+ if (trace)
+ htrc("read error %d\n", errno);
+
+ rc = true;
+ } // endif nbr
+#endif // UNIX
+
+ return rc;
+ } // end of Read
+
+/***********************************************************************/
+/* Write on a huge index file. */
+/***********************************************************************/
+int XHUGE::Write(PGLOBAL g, void *buf, int n, int size, bool& rc)
+ {
+#if defined(WIN32)
+ bool brc;
+ DWORD nbw, count = (DWORD)n * (DWORD) size;
+
+ brc = WriteFile(Hfile, buf, count, &nbw, NULL);
+
+ if (!brc) {
+ char msg[256];
+ DWORD drc = GetLastError();
+
+ FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
+ (LPTSTR)msg, sizeof(msg), NULL);
+ sprintf(g->Message, MSG(WRITING_ERROR), "index file", msg);
+ rc = true;
+ } // endif size
+
+ return (int)nbw;
+#else // UNIX
+ ssize_t nbw;
+ size_t count = (size_t)n * (size_t)size;
+
+ nbw = write(Hfile, buf, count);
+
+ if (nbw != (signed)count) {
+ sprintf(g->Message, MSG(WRITING_ERROR),
+ "index file", strerror(errno));
+ rc = true;
+ } // endif nbw
+
+ return (int)nbw;
+#endif // UNIX
+ } // end of Write
+
+/***********************************************************************/
+/* Update the file header and close the index file. */
+/***********************************************************************/
+void XHUGE::Close(char *fn, int id)
+ {
+#if defined(WIN32)
+ if (id >= 0 && fn) {
+ CloseFileHandle(Hfile);
+ Hfile = CreateFile(fn, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+
+ if (Hfile != INVALID_HANDLE_VALUE)
+ if (SetFilePointer(Hfile, id * sizeof(IOFF), NULL, FILE_BEGIN)
+ != INVALID_SET_FILE_POINTER) {
+ DWORD nbw;
+
+ WriteFile(Hfile, &NewOff, sizeof(IOFF), &nbw, NULL);
+ } // endif SetFilePointer
+
+ } // endif id
+#else // !WIN32
+ if (id >= 0 && fn) {
+ fcntl(Hfile, F_SETFD, O_WRONLY);
+
+ if (lseek(Hfile, id * sizeof(IOFF), SEEK_SET))
+ write(Hfile, &NewOff, sizeof(IOFF));
+
+ } // endif id
+#endif // !WIN32
+
+ XLOAD::Close();
+ } // end of Close
+
+#if defined(XMAP)
+/***********************************************************************/
+/* Don't know whether this is possible for huge files. */
+/***********************************************************************/
+void *XHUGE::FileView(PGLOBAL g, char *fn)
+ {
+ strcpy(g->Message, MSG(NO_PART_MAP));
+ return NULL;
+ } // end of FileView
+#endif // XMAP
+
+/* -------------------------- XXROW Class --------------------------- */
+
+/***********************************************************************/
+/* XXROW Public Constructor. */
+/***********************************************************************/
+XXROW::XXROW(PTDBDOS tdbp) : XXBASE(tdbp, false)
+ {
+ Tdbp = tdbp;
+ Valp = NULL;
+ } // end of XXROW constructor
+
+/***********************************************************************/
+/* XXROW Reset: re-initialize a Kindex block. */
+/***********************************************************************/
+void XXROW::Reset(void)
+ {
+#if defined(_DEBUG)
+ assert(Tdbp->GetLink()); // This a join index
+#endif // _DEBUG
+ } // end of Reset
+
+/***********************************************************************/
+/* Init: Open and Initialize a Key Index. */
+/***********************************************************************/
+bool XXROW::Init(PGLOBAL g)
+ {
+ /*********************************************************************/
+ /* Table will be accessed through an index table. */
+ /* To_Link should not be NULL. */
+ /*********************************************************************/
+ if (!Tdbp->GetLink() || Tbxp->GetKnum() != 1)
+ return true;
+
+ if ((*Tdbp->GetLink())->GetResultType() != TYPE_INT) {
+ strcpy(g->Message, MSG(TYPE_MISMATCH));
+ return true;
+ } else
+ Valp = (*Tdbp->GetLink())->GetValue();
+
+ if ((Num_K = Tbxp->Cardinality(g)) < 0)
+ return true; // Not a fixed file
+
+ /*********************************************************************/
+ /* The entire table is indexed, no need to construct the index. */
+ /*********************************************************************/
+ Cur_K = Num_K;
+ return false;
+ } // end of Init
+
+/***********************************************************************/
+/* RANGE: Tell how many record exist in a given value range. */
+/***********************************************************************/
+int XXROW::Range(PGLOBAL g, int limit, bool incl)
+ {
+ int n = Valp->GetIntValue();
+
+ switch (limit) {
+ case 1: n += ((incl) ? 0 : 1); break;
+ case 2: n += ((incl) ? 1 : 0); break;
+ default: n = 1;
+ } // endswitch limit
+
+ return n;
+ } // end of Range
+
+/***********************************************************************/
+/* XXROW: Fetch a physical or logical record. */
+/***********************************************************************/
+int XXROW::Fetch(PGLOBAL g)
+ {
+ if (Num_K == 0)
+ return -1; // means end of file
+
+ /*********************************************************************/
+ /* Look for a key equal to the link column of previous table, */
+ /* and return its rank whithin the index table. */
+ /*********************************************************************/
+ Cur_K = FastFind(1);
+
+ if (Cur_K >= Num_K)
+ /*******************************************************************/
+ /* Rank not whithin index table, signal record not found. */
+ /*******************************************************************/
+ return -2; // Means record not found
+
+ /*********************************************************************/
+ /* If rank is equal to stored rank, record is already there. */
+ /*********************************************************************/
+ if (Cur_K == Old_K)
+ return -3; // Means record already there
+ else
+ Old_K = Cur_K; // Store rank of newly read record
+
+ return Cur_K;
+ } // end of Fetch
+
+/***********************************************************************/
+/* FastFind: Returns the index of matching record in a join. */
+/***********************************************************************/
+int XXROW::FastFind(int nk)
+ {
+ int n = Valp->GetIntValue();
+
+ if (n < 0)
+ return (Op == OP_EQ) ? (-1) : 0;
+ else if (n > Num_K)
+ return Num_K;
+ else
+ return (Op == OP_GT) ? n : (n - 1);
+
+ } // end of FastFind
+
+/* ------------------------- KXYCOL Classes -------------------------- */
+
+/***********************************************************************/
+/* KXYCOL public constructor. */
+/***********************************************************************/
+KXYCOL::KXYCOL(PKXBASE kp) : To_Keys(Keys.Memp),
+ To_Bkeys(Bkeys.Memp), Kof((CPINT&)Koff.Memp)
+ {
+ Next = NULL;
+ Previous = NULL;
+ Kxp = kp;
+ Colp = NULL;
+ IsSorted = false;
+ Asc = true;
+ Keys = Nmblk;
+ Kblp = NULL;
+ Bkeys = Nmblk;
+ Blkp = NULL;
+ Valp = NULL;
+ Klen = 0;
+ Kprec = 0;
+ Type = TYPE_ERROR;
+ Prefix = false;
+ Koff = Nmblk;
+ Val_K = 0;
+ Ndf = 0;
+ Mxs = 0;
+ } // end of KXYCOL constructor
+
+/***********************************************************************/
+/* KXYCOL Init: initialize and allocate storage. */
+/* Key length kln can be smaller than column length for CHAR columns. */
+/***********************************************************************/
+bool KXYCOL::Init(PGLOBAL g, PCOL colp, int n, bool sm, int kln)
+ {
+ int len = colp->GetLength(), prec = colp->GetScale();
+
+ // Currently no indexing on NULL columns
+ if (colp->IsNullable()) {
+ sprintf(g->Message, "Cannot index nullable column %s", colp->GetName());
+ return true;
+ } // endif nullable
+
+ if (kln && len > kln && colp->GetResultType() == TYPE_STRING) {
+ len = kln;
+ Prefix = true;
+ } // endif kln
+
+ if (trace)
+ htrc("KCOL(%p) Init: col=%s n=%d type=%d sm=%d\n",
+ this, colp->GetName(), n, colp->GetResultType(), sm);
+
+ // Allocate the Value object used when moving items
+ Type = colp->GetResultType();
+
+ if (!(Valp = AllocateValue(g, Type, len, colp->GetScale(),
+ colp->IsUnsigned())))
+ return true;
+
+ Klen = Valp->GetClen();
+ Keys.Size = n * Klen;
+
+ if (!PlgDBalloc(g, NULL, Keys)) {
+ sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, n);
+ return true; // Error
+ } // endif
+
+ // Allocate the Valblock. The last parameter is to have rows filled
+ // by blanks (if true) or keep the zero ending char (if false).
+ // Currently we set it to true to be compatible with QRY blocks,
+ // and the one before last is to enable length/type checking, set to
+ // true if not a prefix key.
+ Kblp = AllocValBlock(g, To_Keys, Type, n, len, prec, !Prefix, true);
+ Asc = sm; // Sort mode: Asc=true Desc=false
+ Ndf = n;
+
+ // Store this information to avoid sorting when already done
+ if (Asc)
+ IsSorted = colp->GetOpt() == 2;
+
+//SetNulls(colp->IsNullable()); for when null columns will be indexable
+ return false;
+ } // end of Init
+
+#if defined(XMAP)
+/***********************************************************************/
+/* KXYCOL MapInit: initialize and address storage. */
+/* Key length kln can be smaller than column length for CHAR columns. */
+/***********************************************************************/
+BYTE* KXYCOL::MapInit(PGLOBAL g, PCOL colp, int *n, BYTE *m)
+ {
+ int len = colp->GetLength(), prec = colp->GetPrecision();
+
+ if (n[3] && colp->GetLength() > n[3]
+ && colp->GetResultType() == TYPE_STRING) {
+ len = n[3];
+ Prefix = true;
+ } // endif kln
+
+ Type = colp->GetResultType();
+
+ if (trace)
+ htrc("MapInit(%p): colp=%p type=%d n=%d len=%d m=%p\n",
+ this, colp, Type, n[0], len, m);
+
+ // Allocate the Value object used when moving items
+ Valp = AllocateValue(g, Type, len, prec, false, NULL);
+ Klen = Valp->GetClen();
+
+ if (n[2]) {
+ Bkeys.Size = n[2] * Klen;
+ Bkeys.Memp = m;
+ Bkeys.Sub = true;
+
+ // Allocate the Valblk containing initial block key values
+ Blkp = AllocValBlock(g, To_Bkeys, Type, n[2], len, prec, true, true);
+ } // endif nb
+
+ Keys.Size = n[0] * Klen;
+ Keys.Memp = m + Bkeys.Size;
+ Keys.Sub = true;
+
+ // Allocate the Valblock. Last two parameters are to have rows filled
+ // by blanks (if true) or keep the zero ending char (if false).
+ // Currently we set it to true to be compatible with QRY blocks,
+ // and last one to enable type checking (no conversion).
+ Kblp = AllocValBlock(g, To_Keys, Type, n[0], len, prec, true, true);
+
+ if (n[1]) {
+ Koff.Size = n[1] * sizeof(int);
+ Koff.Memp = m + Bkeys.Size + Keys.Size;
+ Koff.Sub = true;
+ } // endif n[1]
+
+ Ndf = n[0];
+ IsSorted = colp->GetOpt() < 0;
+ return m + Bkeys.Size + Keys.Size + Koff.Size;
+ } // end of MapInit
+#endif // XMAP
+
+/***********************************************************************/
+/* Allocate the offset block used by intermediate key columns. */
+/***********************************************************************/
+int *KXYCOL::MakeOffset(PGLOBAL g, int n)
+ {
+ if (!Kof) {
+ // Calculate the initial size of the offset
+ Koff.Size = (n + 1) * sizeof(int);
+
+ // Allocate the required memory
+ if (!PlgDBalloc(g, NULL, Koff)) {
+ strcpy(g->Message, MSG(KEY_ALLOC_ERR));
+ return NULL; // Error
+ } // endif
+
+ } else if (n) {
+ // This is a reallocation call
+ PlgDBrealloc(g, NULL, Koff, (n + 1) * sizeof(int));
+ } else
+ PlgDBfree(Koff);
+
+ return (int*)Kof;
+ } // end of MakeOffset
+
+/***********************************************************************/
+/* Make a front end array of key values that are the first value of */
+/* each blocks (of size n). This to reduce paging in FastFind. */
+/***********************************************************************/
+bool KXYCOL::MakeBlockArray(PGLOBAL g, int nb, int size)
+ {
+ int i, k;
+
+ // Calculate the size of the block array in the index
+ Bkeys.Size = nb * Klen;
+
+ // Allocate the required memory
+ if (!PlgDBalloc(g, NULL, Bkeys)) {
+ sprintf(g->Message, MSG(KEY_ALLOC_ERROR), Klen, nb);
+ return true; // Error
+ } // endif
+
+ // Allocate the Valblk used to contains initial block key values
+ Blkp = AllocValBlock(g, To_Bkeys, Type, nb, Klen, Kprec);
+
+ // Populate the array with values
+ for (i = k = 0; i < nb; i++, k += size)
+ Blkp->SetValue(Kblp, i, k);
+
+ return false;
+ } // end of MakeBlockArray
+
+/***********************************************************************/
+/* KXYCOL SetValue: read column value for nth array element. */
+/***********************************************************************/
+void KXYCOL::SetValue(PCOL colp, int i)
+ {
+#if defined(_DEBUG)
+ assert (Kblp != NULL);
+#endif
+
+ Kblp->SetValue(colp->GetValue(), i);
+ } // end of SetValue
+
+/***********************************************************************/
+/* InitFind: initialize finding the rank of column value in index. */
+/***********************************************************************/
+bool KXYCOL::InitFind(PGLOBAL g, PXOB xp)
+ {
+ if (xp->GetType() == TYPE_CONST) {
+ if (Kxp->Nth)
+ return true;
+
+ Valp->SetValue_pval(xp->GetValue(), !Prefix);
+ } else {
+ xp->Reset();
+ xp->Eval(g);
+ Valp->SetValue_pval(xp->GetValue(), false);
+// Valp->SetValue_pval(xp->GetValue(), !Prefix);
+ } // endif Type
+
+ return false;
+ } // end of InitFind
+
+/***********************************************************************/
+/* InitBinFind: initialize Value to the value pointed by vp. */
+/***********************************************************************/
+void KXYCOL::InitBinFind(void *vp)
+ {
+ Valp->SetBinValue(vp);
+ } // end of InitBinFind
+
+/***********************************************************************/
+/* KXYCOL FillValue: called by COLBLK::Eval when a column value is */
+/* already in storage in the corresponding KXYCOL. */
+/***********************************************************************/
+void KXYCOL::FillValue(PVAL valp)
+ {
+ valp->SetValue_pvblk(Kblp, Val_K);
+
+ // Set null when applicable (NIY)
+//if (valp->GetNullable())
+// valp->SetNull(valp->IsZero());
+
+ } // end of FillValue
+
+/***********************************************************************/
+/* KXYCOL: Compare routine for one numeric value. */
+/***********************************************************************/
+int KXYCOL::Compare(int i1, int i2)
+ {
+ // Do the actual comparison between values.
+ register int k = Kblp->CompVal(i1, i2);
+
+ if (trace > 2)
+ htrc("Compare done result=%d\n", k);
+
+ return (Asc) ? k : -k;
+ } // end of Compare
+
+/***********************************************************************/
+/* KXYCOL: Compare the ith key to the stored Value. */
+/***********************************************************************/
+int KXYCOL::CompVal(int i)
+ {
+ // Do the actual comparison between numerical values.
+ if (trace > 2) {
+ register int k = (int)Kblp->CompVal(Valp, (int)i);
+
+ htrc("Compare done result=%d\n", k);
+ return k;
+ } else
+ return Kblp->CompVal(Valp, i);
+
+ } // end of CompVal
+
+/***********************************************************************/
+/* KXYCOL: Compare the key to the stored block value. */
+/***********************************************************************/
+int KXYCOL::CompBval(int i)
+ {
+ // Do the actual comparison between key values.
+ return Blkp->CompVal(Valp, i);
+ } // end of CompBval
+
+/***********************************************************************/
+/* KXYCOL ReAlloc: ReAlloc To_Data if it is not suballocated. */
+/***********************************************************************/
+void KXYCOL::ReAlloc(PGLOBAL g, int n)
+ {
+ PlgDBrealloc(g, NULL, Keys, n * Klen);
+ Kblp->ReAlloc(To_Keys, n);
+ Ndf = n;
+ } // end of ReAlloc
+
+/***********************************************************************/
+/* KXYCOL FreeData: Free To_Keys if it is not suballocated. */
+/***********************************************************************/
+void KXYCOL::FreeData(void)
+ {
+ PlgDBfree(Keys);
+ Kblp = NULL;
+ PlgDBfree(Bkeys);
+ Blkp = NULL;
+ PlgDBfree(Koff);
+ Ndf = 0;
+ } // end of FreeData