1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/************* TabXcl CPP Declares Source Code File (.CPP) *************/
/* Name: TABXCL.CPP Version 1.0 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2013 */
/* */
/* XCOL: Table having one column containing several values */
/* comma separated. When creating the table, the name of the X */
/* column is given by the Name option. */
/* This first version has one limitation: */
/* - The X column has the same length than in the physical file. */
/* This tables produces as many rows for a physical row than the */
/* number of items in the X column (eventually 0). */
/***********************************************************************/
/***********************************************************************/
/* Include relevant section of system dependant header files. */
/***********************************************************************/
#include "my_global.h"
#include "table.h" // MySQL table definitions
#if defined(WIN32)
#include <stdlib.h>
#include <stdio.h>
#if defined(__BORLANDC__)
#define __MFC_COMPAT__ // To define min/max as macro
#endif
//#include <windows.h>
#else
#if defined(UNIX)
#include <fnmatch.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "osutil.h"
#else
//#include <io.h>
#endif
//#include <fcntl.h>
#endif
/***********************************************************************/
/* Include application header files: */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"
#include "plgcnx.h" // For DB types
#include "resource.h"
#include "reldef.h"
#include "filamtxt.h"
#include "tabdos.h"
#include "tabcol.h"
#include "tabxcl.h"
#include "xtable.h"
#if defined(MYSQL_SUPPORT)
#include "tabmysql.h"
#endif // MYSQL_SUPPORT
#include "ha_connect.h"
#include "mycat.h"
/* -------------- Implementation of the XCOL classes ---------------- */
/***********************************************************************/
/* XCLDEF constructor. */
/***********************************************************************/
XCLDEF::XCLDEF(void)
{
Xcol = NULL;
Sep = ',';
Mult = 10;
} // end of XCLDEF constructor
/***********************************************************************/
/* DefineAM: define specific AM block values from XCOL table. */
/***********************************************************************/
bool XCLDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
{
char buf[8];
Xcol = GetStringCatInfo(g, "Colname", "");
GetCharCatInfo("Separator", ",", buf, sizeof(buf));
Sep = (strlen(buf) == 2 && buf[0] == '\\' && buf[1] == 't') ? '\t' : *buf;
Mult = GetIntCatInfo("Mult", 10);
return PRXDEF::DefineAM(g, am, poff);
} // end of DefineAM
/***********************************************************************/
/* GetTable: makes a new TDB of the proper type. */
/***********************************************************************/
PTDB XCLDEF::GetTable(PGLOBAL g, MODE mode)
{
if (Catfunc == FNC_COL)
return new(g) TDBTBC(this);
else
return new(g) TDBXCL(this);
} // end of GetTable
/* ------------------------------------------------------------------- */
/***********************************************************************/
/* Implementation of the TDBXCL class. */
/***********************************************************************/
TDBXCL::TDBXCL(PXCLDEF tdp) : TDBPRX(tdp)
{
Xcolumn = tdp->Xcol; // CSV column name
Xcolp = NULL; // To the XCLCOL column
Mult = tdp->Mult; // Multiplication factor
N = 0; // The current table index
M = 0; // The occurence rank
RowFlag = 0; // 0: Ok, 1: Same, 2: Skip
New = TRUE; // TRUE for new line
Sep = tdp->Sep; // The Xcol separator
} // end of TDBXCL constructor
/***********************************************************************/
/* Allocate XCL column description block. */
/***********************************************************************/
PCOL TDBXCL::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
{
PCOL colp;
if (!stricmp(cdp->GetName(), Xcolumn)) {
Xcolp = new(g) XCLCOL(g, cdp, this, cprec, n);
colp = Xcolp;
} else
colp = new(g) PRXCOL(cdp, this, cprec, n);
return colp;
} // end of MakeCol
/***********************************************************************/
/* XCL GetMaxSize: returns the maximum number of rows in the table. */
/***********************************************************************/
int TDBXCL::GetMaxSize(PGLOBAL g)
{
if (MaxSize < 0) {
if (InitTable(g))
return 0;
MaxSize = Mult * Tdbp->GetMaxSize(g);
} // endif MaxSize
return MaxSize;
} // end of GetMaxSize
/***********************************************************************/
/* For this table type, ROWID is the (virtual) row number, */
/* while ROWNUM is be the occurence rank in the multiple column. */
/***********************************************************************/
int TDBXCL::RowNumber(PGLOBAL g, bool b)
{
return (b) ? M : N;
} // end of RowNumber
/***********************************************************************/
/* XCL Access Method opening routine. */
/***********************************************************************/
bool TDBXCL::OpenDB(PGLOBAL g)
{
if (Use == USE_OPEN) {
/*******************************************************************/
/* Table already open, just replace it at its beginning. */
/*******************************************************************/
M = N = 0;
RowFlag = 0;
New = TRUE;
return Tdbp->OpenDB(g);
} // endif use
if (Mode != MODE_READ) {
/*******************************************************************/
/* Currently XCOL tables cannot be modified. */
/*******************************************************************/
strcpy(g->Message, "XCOL tables are read only");
return TRUE;
} // endif Mode
if (InitTable(g))
return TRUE;
/*********************************************************************/
/* Check and initialize the subtable columns. */
/*********************************************************************/
for (PCOL cp = Columns; cp; cp = cp->GetNext())
if (!cp->IsSpecial())
if (((PPRXCOL)cp)->Init(g))
return TRUE;
/*********************************************************************/
/* Physically open the object table. */
/*********************************************************************/
if (Tdbp->OpenDB(g))
return TRUE;
Use = USE_OPEN;
return FALSE;
} // end of OpenDB
/***********************************************************************/
/* Data Base read routine for XCL access method. */
/***********************************************************************/
int TDBXCL::ReadDB(PGLOBAL g)
{
int rc = RC_OK;
/*********************************************************************/
/* Now start the multi reading process. */
/*********************************************************************/
do {
if (RowFlag != 1) {
if ((rc = Tdbp->ReadDB(g)) != RC_OK)
break;
New = TRUE;
M = 1;
} else {
New = FALSE;
M++;
} // endif RowFlag
if (Xcolp) {
RowFlag = 0;
Xcolp->ReadColumn(g);
} // endif Xcolp
N++;
} while (RowFlag == 2);
return rc;
} // end of ReadDB
// ------------------------ XCLCOL functions ----------------------------
/***********************************************************************/
/* XCLCOL public constructor. */
/***********************************************************************/
XCLCOL::XCLCOL(PGLOBAL g, PCOLDEF cdp, PTDB tdbp, PCOL cprec, int i)
: PRXCOL(cdp, tdbp, cprec, i, "XCL")
{
// Set additional XXL access method information for column.
Cbuf = NULL; // Will be allocated later
Cp = NULL; // Pointer to current position in Cbuf
Sep = ((PTDBXCL)tdbp)->Sep;
AddStatus(BUF_READ); // Only evaluated from TDBXCL::ReadDB
} // end of XCLCOL constructor
/***********************************************************************/
/* XCLCOL initialization routine. */
/* Allocate Cbuf that will contain the Colp value. */
/***********************************************************************/
bool XCLCOL::Init(PGLOBAL g, PTDBASE tp)
{
if (PRXCOL::Init(g, tp))
return true;
Cbuf = (char*)PlugSubAlloc(g, NULL, Colp->GetLength() + 1);
return false;
} // end of Init
/***********************************************************************/
/* What this routine does is to get the comma-separated string */
/* from the source table column, extract the single values and */
/* set the flag for the table ReadDB function. */
/***********************************************************************/
void XCLCOL::ReadColumn(PGLOBAL g)
{
if (((PTDBXCL)To_Tdb)->New) {
Colp->Reset(); // Moved here in case of failed filtering
Colp->Eval(g);
strncpy(Cbuf, To_Val->GetCharValue(), Colp->GetLength());
Cbuf[Colp->GetLength()] = 0;
Cp = Cbuf;
} // endif New
if (*Cp) {
PSZ p;
// Trim left
for (p = Cp; *p == ' '; p++) ;
if ((Cp = strchr(Cp, Sep)))
// Separator is found
*Cp++ = '\0';
Value->SetValue_psz(p);
} else if (Nullable) {
Value->Reset();
Value->SetNull(true);
} else {
// Skip that row
((PTDBXCL)To_Tdb)->RowFlag = 2;
Colp->Reset();
} // endif Cp
if (Cp && *Cp)
// More to come from the same row
((PTDBXCL)To_Tdb)->RowFlag = 1;
} // end of ReadColumn
|