Commit 6ec8f00a authored by Olivier Bertrand's avatar Olivier Bertrand

Added to CONNECT the missing type TYPE_BIGINT (longlong).

parent 5c8c4f4f
......@@ -76,6 +76,7 @@
#define TYPE_STRING 1
#define TYPE_FLOAT 2
#define TYPE_SHORT 3
#define TYPE_BIGINT 5
#define TYPE_LIST 6
#define TYPE_INT 7
......
......@@ -1075,6 +1075,9 @@ void *ha_connect::GetColumnOption(void *field, PCOLINFO pcf)
case MYSQL_TYPE_TIMESTAMP:
pcf->Type= TYPE_DATE;
break;
case MYSQL_TYPE_LONGLONG:
pcf->Type= TYPE_BIGINT;
break;
default:
pcf->Type=TYPE_ERROR;
} // endswitch type
......@@ -3509,6 +3512,7 @@ int ha_connect::create(const char *name, TABLE *table_arg,
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_NEWDATE:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_LONGLONG:
break; // Ok
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
......@@ -3518,7 +3522,6 @@ int ha_connect::create(const char *name, TABLE *table_arg,
break; // To be checked
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET:
......
......@@ -98,9 +98,12 @@ int MYSQLtoPLG(int mytype)
break;
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_ENUM: // ???
type = TYPE_INT;
break;
case MYSQL_TYPE_LONGLONG:
type = TYPE_BIGINT;
break;
case MYSQL_TYPE_DECIMAL:
#if !defined(ALPHA)
case MYSQL_TYPE_NEWDECIMAL:
......
......@@ -113,6 +113,9 @@ PVBLK AllocValBlock(PGLOBAL g, void *mp, int type, int nval, int len,
case TYPE_DATE: // ?????
blkp = new(g) DATBLK(mp, nval);
break;
case TYPE_BIGINT:
blkp = new(g) BIGBLK(mp, nval);
break;
case TYPE_FLOAT:
blkp = new(g) DBLBLK(mp, nval, prec);
break;
......@@ -233,6 +236,14 @@ int CHRBLK::GetIntValue(int n)
return atol((char *)GetValPtrEx(n));
} // end of GetIntValue
/***********************************************************************/
/* Return the value of the nth element converted to big int. */
/***********************************************************************/
longlong CHRBLK::GetBigintValue(int n)
{
return atoll((char *)GetValPtrEx(n));
} // end of GetIntValue
/***********************************************************************/
/* Return the value of the nth element converted to double. */
/***********************************************************************/
......@@ -1027,6 +1038,196 @@ void DATBLK::SetValue(PSZ p, int n)
} // end of SetValue
/* -------------------------- Class BIGBLK --------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
BIGBLK::BIGBLK(void *mp, int nval)
: VALBLK(mp, TYPE_BIGINT, nval), Lngp((longlong*&)Blkp)
{
} // end of BIGBLK constructor
/***********************************************************************/
/* Initialization routine. */
/***********************************************************************/
void BIGBLK::Init(PGLOBAL g, bool check)
{
if (!Blkp)
Blkp = PlugSubAlloc(g, NULL, Nval * sizeof(longlong));
Check = check;
Global = g;
} // end of Init
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void BIGBLK::SetValue(PVAL valp, int n)
{
CheckParms(valp, n)
Lngp[n] = valp->GetBigintValue();
} // end of SetValue
/***********************************************************************/
/* Set one value in a block. */
/***********************************************************************/
void BIGBLK::SetValue(PSZ p, int n)
{
#if defined(_DEBUG) || defined(DEBTRACE)
if (Check) {
PGLOBAL& g = Global;
strcpy(g->Message, MSG(BAD_SET_STRING));
longjmp(g->jumper[g->jump_level], Type);
} // endif Check
#endif
Lngp[n] = atoll(p);
} // end of SetValue
/***********************************************************************/
/* Set one value in a block if val is less than the current value. */
/***********************************************************************/
void BIGBLK::SetMin(PVAL valp, int n)
{
CheckParms(valp, n)
longlong lval = valp->GetIntValue();
longlong& lmin = Lngp[n];
if (lval < lmin)
lmin = lval;
} // end of SetMin
/***********************************************************************/
/* Set one value in a block if val is greater than the current value. */
/***********************************************************************/
void BIGBLK::SetMax(PVAL valp, int n)
{
CheckParms(valp, n)
longlong lval = valp->GetIntValue();
longlong& lmax = Lngp[n];
if (lval > lmax)
lmax = lval;
} // end of SetMax
/***********************************************************************/
/* Set one value in a block from a value in another block. */
/***********************************************************************/
void BIGBLK::SetValue(PVBLK pv, int n1, int n2)
{
CheckType(pv)
Lngp[n1] = ((BIGBLK*)pv)->Lngp[n2];
} // end of SetValue
/***********************************************************************/
/* Set many values in a block from values in another block. */
/***********************************************************************/
void BIGBLK::SetValues(PVBLK pv, int k, int n)
{
CheckType(pv)
longlong *lp = ((BIGBLK*)pv)->Lngp;
for (register int i = k; i < n; i++)
Lngp[i] = lp[i];
} // end of SetValues
/***********************************************************************/
/* This function is used by class RESCOL when calculating COUNT. */
/***********************************************************************/
void BIGBLK::AddMinus1(PVBLK pv, int n1, int n2)
{
assert(Type == pv->GetType());
Lngp[n1] += (((BIGBLK*)pv)->Lngp[n2] - 1);
} // end of AddMinus1
/***********************************************************************/
/* Move one value from i to j. */
/***********************************************************************/
void BIGBLK::Move(int i, int j)
{
Lngp[j] = Lngp[i];
} // end of Move
/***********************************************************************/
/* Compare a Value object with the nth value of the block. */
/***********************************************************************/
int BIGBLK::CompVal(PVAL vp, int n)
{
CheckParms(vp, n)
longlong mlv = Lngp[n];
longlong vlv = vp->GetBigintValue();
return (vlv > mlv) ? 1 : (vlv < mlv) ? (-1) : 0;
} // end of CompVal
/***********************************************************************/
/* Compare two values of the block. */
/***********************************************************************/
int BIGBLK::CompVal(int i1, int i2)
{
longlong lv1 = Lngp[i1];
longlong lv2 = Lngp[i2];
return (lv1 > lv2) ? 1 : (lv1 < lv2) ? (-1) : 0;
} // end of CompVal
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
void *BIGBLK::GetValPtr(int n)
{
CheckIndex(n)
return Lngp + n;
} // end of GetValPtr
/***********************************************************************/
/* Get a pointer on the nth value of the block. */
/***********************************************************************/
void *BIGBLK::GetValPtrEx(int n)
{
CheckIndex(n)
return Lngp + n;
} // end of GetValPtrEx
/***********************************************************************/
/* Returns index of matching value in block or -1. */
/***********************************************************************/
int BIGBLK::Find(PVAL vp)
{
CheckType(vp)
int i;
longlong n = vp->GetBigintValue();
for (i = 0; i < Nval; i++)
if (n == Lngp[i])
break;
return (i < Nval) ? i : (-1);
} // end of Find
/***********************************************************************/
/* Returns the length of the longest string in the block. */
/***********************************************************************/
int BIGBLK::GetMaxLength(void)
{
char buf[24];
int i, n;
for (i = n = 0; i < Nval; i++) {
sprintf(buf, "%lld", Lngp[i]);
n = max(n, (signed)strlen(buf));
} // endfor i
return n;
} // end of GetMaxLength
/* -------------------------- Class DBLBLK --------------------------- */
/***********************************************************************/
......
/*************** Valblk H Declares Source Code File (.H) ***************/
/* Name: VALBLK.H Version 1.6 */
/* Name: VALBLK.H Version 1.7 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2005-2012 */
/* (C) Copyright to the author Olivier BERTRAND 2005-2013 */
/* */
/* This file contains the VALBLK and derived classes declares. */
/***********************************************************************/
......@@ -42,6 +42,7 @@ class VALBLK : public BLOCK {
virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n) = 0;
virtual int GetIntValue(int n) = 0;
virtual longlong GetBigintValue(int n) = 0;
virtual double GetFloatValue(int n) = 0;
virtual void ReAlloc(void *mp, int n) {Blkp = mp; Nval = n;}
virtual void Reset(int n) = 0;
......@@ -52,6 +53,7 @@ class VALBLK : public BLOCK {
// Methods
virtual void SetValue(short sval, int n) {assert(false);}
virtual void SetValue(int lval, int n) {assert(false);}
virtual void SetValue(longlong lval, int n) {assert(false);}
virtual void SetValue(PSZ sp, int n) {assert(false);}
virtual void SetValue(PVAL valp, int n) = 0;
virtual void SetMin(PVAL valp, int n) = 0;
......@@ -98,6 +100,7 @@ class CHRBLK : public VALBLK {
virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n);
virtual int GetIntValue(int n);
virtual longlong GetBigintValue(int n);
virtual double GetFloatValue(int n);
virtual void Reset(int n);
virtual void SetPrec(int p) {Ci = (p != 0);}
......@@ -143,6 +146,7 @@ class STRBLK : public VALBLK {
virtual PSZ GetCharValue(int n) {return Strp[n];}
virtual short GetShortValue(int n) {return (short)atoi(Strp[n]);}
virtual int GetIntValue(int n) {return atol(Strp[n]);}
virtual longlong GetBigintValue(int n) {return atoll(Strp[n]);}
virtual double GetFloatValue(int n) {return atof(Strp[n]);}
virtual void Reset(int n) {Strp[n] = NULL;}
......@@ -180,6 +184,7 @@ class SHRBLK : public VALBLK {
//virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n) {return Shrp[n];}
virtual int GetIntValue(int n) {return (int)Shrp[n];}
virtual longlong GetBigintValue(int n) {return (longlong)Shrp[n];}
virtual double GetFloatValue(int n) {return (double)Shrp[n];}
virtual void Reset(int n) {Shrp[n] = 0;}
......@@ -187,6 +192,7 @@ class SHRBLK : public VALBLK {
virtual void SetValue(PSZ sp, int n);
virtual void SetValue(short sval, int n) {Shrp[n] = sval;}
virtual void SetValue(int lval, int n) {Shrp[n] = (short)lval;}
virtual void SetValue(longlong lval, int n) {Shrp[n] = (short)lval;}
virtual void SetValue(PVAL valp, int n);
virtual void SetMin(PVAL valp, int n);
virtual void SetMax(PVAL valp, int n);
......@@ -220,6 +226,7 @@ class LNGBLK : public VALBLK {
//virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n) {return (short)Lngp[n];}
virtual int GetIntValue(int n) {return Lngp[n];}
virtual longlong GetBigintValue(int n) {return (longlong)Lngp[n];}
virtual double GetFloatValue(int n) {return (double)Lngp[n];}
virtual void Reset(int n) {Lngp[n] = 0;}
......@@ -227,6 +234,7 @@ class LNGBLK : public VALBLK {
virtual void SetValue(PSZ sp, int n);
virtual void SetValue(short sval, int n) {Lngp[n] = (int)sval;}
virtual void SetValue(int lval, int n) {Lngp[n] = lval;}
virtual void SetValue(longlong lval, int n) {Lngp[n] = (int)lval;}
virtual void SetValue(PVAL valp, int n);
virtual void SetMin(PVAL valp, int n);
virtual void SetMax(PVAL valp, int n);
......@@ -265,6 +273,48 @@ class DATBLK : public LNGBLK {
PVAL Dvalp; // Date value used to convert string
}; // end of class DATBLK
/***********************************************************************/
/* Class LNGBLK: represents a block of int integer values. */
/***********************************************************************/
class BIGBLK : public VALBLK {
public:
// Constructors
BIGBLK(void *mp, int size);
// Implementation
virtual void Init(PGLOBAL g, bool check);
virtual int GetVlen(void) {return sizeof(longlong);}
//virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n) {return (short)Lngp[n];}
virtual int GetIntValue(int n) {return (int)Lngp[n];}
virtual longlong GetBigintValue(int n) {return Lngp[n];}
virtual double GetFloatValue(int n) {return (double)Lngp[n];}
virtual void Reset(int n) {Lngp[n] = 0LL;}
// Methods
virtual void SetValue(PSZ sp, int n);
virtual void SetValue(short sval, int n) {Lngp[n] = (longlong)sval;}
virtual void SetValue(int lval, int n) {Lngp[n] = (longlong)lval;}
virtual void SetValue(longlong lval, int n) {Lngp[n] = lval;}
virtual void SetValue(PVAL valp, int n);
virtual void SetMin(PVAL valp, int n);
virtual void SetMax(PVAL valp, int n);
virtual void SetValue(PVBLK pv, int n1, int n2);
virtual void SetValues(PVBLK pv, int k, int n);
virtual void AddMinus1(PVBLK pv, int n1, int n2);
virtual void Move(int i, int j);
virtual int CompVal(PVAL vp, int n);
virtual int CompVal(int i1, int i2);
virtual void *GetValPtr(int n);
virtual void *GetValPtrEx(int n);
virtual int Find(PVAL vp);
virtual int GetMaxLength(void);
protected:
// Members
longlong* const &Lngp;
}; // end of class BIGBLK
/***********************************************************************/
/* Class DBLBLK: represents a block of double float values. */
/***********************************************************************/
......@@ -279,6 +329,7 @@ class DBLBLK : public VALBLK {
//virtual PSZ GetCharValue(int n);
virtual short GetShortValue(int n) {return (short)Dblp[n];}
virtual int GetIntValue(int n) {return (int)Dblp[n];}
virtual longlong GetBigintValue(int n) {return (longlong)Dblp[n];}
virtual double GetFloatValue(int n) {return Dblp[n];}
virtual void Reset(int n) {Dblp[n] = 0.0;}
virtual void SetPrec(int p) {Prec = p;}
......
/************* Value C++ Functions Source Code File (.CPP) *************/
/* Name: VALUE.CPP Version 1.9 */
/* Name: VALUE.CPP Version 2.0 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2001-2012 */
/* (C) Copyright to the author Olivier BERTRAND 2001-2013 */
/* */
/* This file contains the VALUE and derived classes family functions. */
/* These classes contain values of different types. They are used so */
......@@ -131,6 +131,7 @@ PSZ GetTypeName(int 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_FLOAT: name = "FLOAT"; break;
} // endswitch type
......@@ -147,6 +148,7 @@ int GetTypeSize(int type, int len)
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_FLOAT: len = sizeof(double); break;
break;
......@@ -187,6 +189,7 @@ int GetDBType(int type)
case TYPE_STRING: tp = DB_CHAR; break;
case TYPE_SHORT: tp = DB_SHORT; break;
case TYPE_INT: tp = DB_INT; break;
case TYPE_BIGINT:
case TYPE_FLOAT: tp = DB_DOUBLE; break;
case TYPE_DATE: tp = DB_DATE; break;
default: tp = DB_ERROR;
......@@ -207,6 +210,7 @@ short GetSQLType(int type)
case TYPE_SHORT: tp = SQL_SMALLINT; break;
case TYPE_INT: tp = SQL_INTEGER; break;
case TYPE_DATE: tp = SQL_TIMESTAMP; break;
case TYPE_BIGINT:
case TYPE_FLOAT: tp = SQL_DOUBLE; break;
} // endswitch type
......@@ -225,6 +229,7 @@ int GetSQLCType(int type)
case TYPE_SHORT: tp = SQL_C_SHORT; break;
case TYPE_INT: tp = SQL_C_LONG; break;
case TYPE_DATE: tp = SQL_C_TIMESTAMP; break;
case TYPE_BIGINT:
case TYPE_FLOAT: tp = SQL_C_DOUBLE; break;
} // endswitch type
......@@ -242,6 +247,7 @@ char *GetFormatType(int 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_FLOAT: c = "F"; break;
case TYPE_DATE: c = "D"; break;
} // endswitch type
......@@ -260,6 +266,7 @@ int GetFormatType(char 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_FLOAT; break;
case 'D': type = TYPE_DATE; break;
} // endswitch type
......@@ -281,11 +288,12 @@ int TranslateSQLType(int stp, int prec, int& len)
break;
case SQL_LONGVARCHAR: // (-1)
type = TYPE_STRING;
len = min(abs(len), 128);
len = min(abs(len), 255);
break;
case SQL_NUMERIC: // 2
case SQL_DECIMAL: // 3
type = (prec) ? TYPE_FLOAT : TYPE_INT;
type = (prec) ? TYPE_FLOAT
: (len > 10) ? TYPE_BIGINT : TYPE_INT;
break;
case SQL_INTEGER: // 4
type = TYPE_INT;
......@@ -314,11 +322,13 @@ int TranslateSQLType(int stp, int prec, int& len)
type = TYPE_DATE;
len = 19 + ((prec) ? (prec+1) : 0);
break;
case SQL_BIGINT: // (-5)
type = TYPE_BIGINT;
break;
case SQL_UNKNOWN_TYPE: // 0
case SQL_BINARY: // (-2)
case SQL_VARBINARY: // (-3)
case SQL_LONGVARBINARY: // (-4)
case SQL_BIGINT: // (-5)
// case SQL_BIT: // (-7)
case SQL_GUID: // (-11)
default:
......@@ -349,6 +359,7 @@ bool IsTypeNum(int type)
{
switch (type) {
case TYPE_INT:
case TYPE_BIGINT:
case TYPE_DATE:
case TYPE_FLOAT:
case TYPE_SHORT:
......@@ -382,6 +393,7 @@ int ConvertType(int target, int type, CONV kind, bool match)
return (target == TYPE_FLOAT || type == TYPE_FLOAT) ? TYPE_FLOAT
: (target == TYPE_DATE || type == TYPE_DATE) ? TYPE_DATE
: (target == TYPE_BIGINT || type == TYPE_BIGINT) ? TYPE_BIGINT
: (target == TYPE_INT || type == TYPE_INT) ? TYPE_INT
: TYPE_SHORT;
default:
......@@ -394,6 +406,7 @@ int ConvertType(int target, int type, CONV kind, bool match)
return (target == TYPE_FLOAT || type == TYPE_FLOAT) ? TYPE_FLOAT
: (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
......@@ -416,6 +429,7 @@ PVAL AllocateValue(PGLOBAL g, void *value, short type)
case TYPE_STRING: valp = new(g) STRING((PSZ)value); break;
case TYPE_SHORT: valp = new(g) SHVAL(*(short*)value); break;
case TYPE_INT: valp = new(g) INTVAL(*(int*)value); break;
case TYPE_BIGINT: valp = new(g) BIGVAL(*(longlong*)value); break;
case TYPE_FLOAT: valp = new(g) DFVAL(*(double *)value); break;
default:
sprintf(g->Message, MSG(BAD_VALUE_TYPE), type);
......@@ -439,6 +453,7 @@ PVAL AllocateValue(PGLOBAL g, int type, int len, int prec,
break;
case TYPE_DATE: valp = new(g) DTVAL(g, len, prec, dom); break;
case TYPE_INT: valp = new(g) INTVAL((int)0); break;
case TYPE_BIGINT: valp = new(g) BIGVAL((longlong)0); break;
case TYPE_SHORT: valp = new(g) SHVAL((short)0); break;
case TYPE_FLOAT: valp = new(g) DFVAL(0.0, prec); break;
default:
......@@ -472,6 +487,7 @@ PVAL AllocateValue(PGLOBAL g, PVAL valp, int newtype)
break;
case TYPE_SHORT: valp = new(g) SHVAL(valp->GetShortValue()); break;
case TYPE_INT: valp = new(g) INTVAL(valp->GetIntValue()); break;
case TYPE_BIGINT: valp = new(g) BIGVAL(valp->GetBigintValue()); break;
case TYPE_DATE: valp = new(g) DTVAL(g, valp->GetIntValue()); break;
case TYPE_FLOAT: valp = new(g) DFVAL(valp->GetFloatValue()); break;
default:
......@@ -510,6 +526,9 @@ char *VALUE::ShowTypedValue(PGLOBAL g, char *buf, int typ, int n, int p)
case TYPE_SHORT:
buf = GetShortString(buf, n);
break;
case TYPE_BIGINT:
buf = GetBigintString(buf, n);
break;
default:
// More should be added for additional values.
if (trace)
......@@ -586,6 +605,17 @@ STRING::STRING(PGLOBAL g, int n) : VALUE(TYPE_STRING)
Ci = false;
} // end of STRING constructor
/***********************************************************************/
/* STRING public constructor from bigint. */
/***********************************************************************/
STRING::STRING(PGLOBAL g, longlong n) : VALUE(TYPE_STRING)
{
Strp = (char *)PlugSubAlloc(g, NULL, 12);
Len = sprintf(Strp, "%lld", n);
Clen = Len;
Ci = false;
} // end of STRING constructor
/***********************************************************************/
/* STRING public constructor from double. */
/***********************************************************************/
......@@ -645,7 +675,7 @@ void STRING::SetValue_pvblk(PVBLK blk, int n)
} // end of SetValue_pvblk
/***********************************************************************/
/* STRING SetValue: get the character representation of an integer. */
/* STRING SetValue: get the character representation of a short int. */
/***********************************************************************/
void STRING::SetValue(short n)
{
......@@ -669,6 +699,23 @@ void STRING::SetValue(int n)
} // end of SetValue
/***********************************************************************/
/* STRING SetValue: get the character representation of a big integer.*/
/***********************************************************************/
void STRING::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);
} // end of SetValue
/***********************************************************************/
/* STRING SetValue: get the character representation of a double. */
/***********************************************************************/
......@@ -763,10 +810,19 @@ char *STRING::GetShortString(char *p, int n)
/***********************************************************************/
char *STRING::GetIntString(char *p, int n)
{
sprintf(p, "%*ld", n, atol(Strp));
sprintf(p, "%*d", n, atol(Strp));
return p;
} // end of GetIntString
/***********************************************************************/
/* STRING GetIntString: get big int representation of a char value. */
/***********************************************************************/
char *STRING::GetBigintString(char *p, int n)
{
sprintf(p, "%*lld", n, atol(Strp));
return p;
} // end of GetBigintString
/***********************************************************************/
/* STRING GetFloatString: get double representation of a char value. */
/***********************************************************************/
......@@ -1391,6 +1447,15 @@ SHVAL::SHVAL(int n) : VALUE(TYPE_SHORT)
Clen = sizeof(short);
} // end of SHVAL constructor
/***********************************************************************/
/* SHVAL public constructor from big int. */
/***********************************************************************/
SHVAL::SHVAL(longlong n) : VALUE(TYPE_SHORT)
{
Sval = (short)n;
Clen = sizeof(short);
} // end of SHVAL constructor
/***********************************************************************/
/* SHVAL public constructor from double. */
/***********************************************************************/
......@@ -1554,10 +1619,19 @@ char *SHVAL::GetShortString(char *p, int n)
/***********************************************************************/
char *SHVAL::GetIntString(char *p, int n)
{
sprintf(p, "%*ld", n, (int)Sval);
sprintf(p, "%*d", n, (int)Sval);
return p;
} // end of GetIntString
/***********************************************************************/
/* SHVAL GetBigintString: get big int representation of a short value.*/
/***********************************************************************/
char *SHVAL::GetBigintString(char *p, int n)
{
sprintf(p, "%*lld", n, (longlong)Sval);
return p;
} // end of GetBigintString
/***********************************************************************/
/* SHVAL GetFloatString: get double representation of a short value. */
/***********************************************************************/
......@@ -1653,7 +1727,7 @@ bool SHVAL::Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op)
Sval = strlen(p);
if (trace)
htrc("Compute result=%d val=%s op=%d\n", Sval, p, op);
htrc("Compute result=%hd val=%s op=%d\n", Sval, p, op);
} else if (op == OP_INSTR || op == OP_LIKE || op == OP_CNTIN) {
char *p, *tp = g->Message;
......@@ -2039,7 +2113,7 @@ bool SHVAL::SetConstFormat(PGLOBAL g, FORMAT& fmt)
{
char c[16];
fmt.Type[0] = 'N';
fmt.Type[0] = 'S';
fmt.Length = sprintf(c, "%hd", Sval);
fmt.Prec = 0;
return false;
......@@ -2095,6 +2169,15 @@ INTVAL::INTVAL(int n) : VALUE(TYPE_INT)
Clen = sizeof(int);
} // end of INTVAL constructor
/***********************************************************************/
/* INTVAL public constructor from big int. */
/***********************************************************************/
INTVAL::INTVAL(longlong n) : VALUE(TYPE_INT)
{
Ival = (int)n;
Clen = sizeof(int);
} // end of INTVAL constructor
/***********************************************************************/
/* INTVAL public constructor from double. */
/***********************************************************************/
......@@ -2228,7 +2311,7 @@ void INTVAL::GetBinValue(void *buf, int buflen)
/***********************************************************************/
char *INTVAL::ShowValue(char *buf, int len)
{
sprintf(buf, "%*ld", len, Ival);
sprintf(buf, "%*d", len, Ival);
return buf;
} // end of ShowValue
......@@ -2255,10 +2338,19 @@ char *INTVAL::GetShortString(char *p, int n)
/***********************************************************************/
char *INTVAL::GetIntString(char *p, int n)
{
sprintf(p, "%*ld", n, Ival);
sprintf(p, "%*d", n, Ival);
return p;
} // end of GetIntString
/***********************************************************************/
/* INTVAL GetBigintString: get big int representation of a int value. */
/***********************************************************************/
char *INTVAL::GetBigintString(char *p, int n)
{
sprintf(p, "%*lld", n, (longlong)Ival);
return p;
} // end of GetBigintString
/***********************************************************************/
/* INTVAL GetFloatString: get double representation of a int value. */
/***********************************************************************/
......@@ -3443,113 +3535,136 @@ bool DTVAL::FormatValue(PVAL vp, char *fmt)
} // end of FormatValue
/* -------------------------- Class BIGVAL ---------------------------- */
/* -------------------------- Class DFVAL ---------------------------- */
/***********************************************************************/
/* BIGVAL public constructor from char. */
/***********************************************************************/
BIGVAL::BIGVAL(PSZ s) : VALUE(TYPE_BIGINT)
{
Lval = atoll(s);
Clen = sizeof(longlong);
} // end of BIGVAL constructor
/***********************************************************************/
/* DFVAL public constructor from char. */
/* BIGVAL public constructor from short. */
/***********************************************************************/
DFVAL::DFVAL(PSZ s, int prec) : VALUE(TYPE_FLOAT)
BIGVAL::BIGVAL(short n) : VALUE(TYPE_BIGINT)
{
Fval = atof(s);
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
Lval = (longlong)n;
Clen = sizeof(longlong);
} // end of BIGVAL constructor
/***********************************************************************/
/* DFVAL public constructor from short. */
/* BIGVAL public constructor from int. */
/***********************************************************************/
DFVAL::DFVAL(short n, int prec) : VALUE(TYPE_FLOAT)
BIGVAL::BIGVAL(int n) : VALUE(TYPE_BIGINT)
{
Fval = (double)n;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
Lval = (longlong)n;
Clen = sizeof(longlong);
} // end of BIGVAL constructor
/***********************************************************************/
/* DFVAL public constructor from int. */
/* BIGVAL public constructor from big int. */
/***********************************************************************/
DFVAL::DFVAL(int n, int prec) : VALUE(TYPE_FLOAT)
BIGVAL::BIGVAL(longlong n) : VALUE(TYPE_BIGINT)
{
Fval = (double)n;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
Lval = n;
Clen = sizeof(longlong);
} // end of BIGVAL constructor
/***********************************************************************/
/* DFVAL public constructor from double. */
/* BIGVAL public constructor from double. */
/***********************************************************************/
DFVAL::DFVAL(double f, int prec) : VALUE(TYPE_FLOAT)
BIGVAL::BIGVAL(double f) : VALUE(TYPE_BIGINT)
{
Fval = f;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
Lval = (longlong)f;
Clen = sizeof(longlong);
} // end of BIGVAL constructor
/***********************************************************************/
/* DFVAL GetValLen: returns the print length of the double object. */
/* BIGVAL GetValLen: returns the print length of the int object. */
/***********************************************************************/
int DFVAL::GetValLen(void)
int BIGVAL::GetValLen(void)
{
char c[32];
char c[24];
return sprintf(c, "%.*lf", Prec, Fval);
return sprintf(c, "%lld", Lval);
} // end of GetValLen
/***********************************************************************/
/* DFVAL SetValue: copy the value of another Value object. */
/* BIGVAL SetValue: copy the value of another Value object. */
/* This function allows conversion if chktype is false. */
/***********************************************************************/
bool DFVAL::SetValue_pval(PVAL valp, bool chktype)
bool BIGVAL::SetValue_pval(PVAL valp, bool chktype)
{
if (chktype && Type != valp->GetType())
return true;
Fval = valp->GetFloatValue();
Lval = valp->GetBigintValue();
return false;
} // end of SetValue
/***********************************************************************/
/* SetValue: convert chars extracted from a line to double value. */
/* BIGVAL SetValue: convert chars extracted from a line to a big int. */
/***********************************************************************/
void DFVAL::SetValue_char(char *p, int n)
void BIGVAL::SetValue_char(char *p, int n)
{
char *p2, buf[32];
char *p2;
bool minus;
for (p2 = p + n; p < p2 && *p == ' '; p++) ;
n = min(p2 - p, 31);
memcpy(buf, p, n);
buf[n] = '\0';
Fval = atof(buf);
for (Lval = 0LL, minus = false; p < p2; p++)
switch (*p) {
case '-':
minus = true;
case '+':
break;
case '0': Lval = Lval * 10LL; break;
case '1': Lval = Lval * 10LL + 1LL; break;
case '2': Lval = Lval * 10LL + 2LL; break;
case '3': Lval = Lval * 10LL + 3LL; break;
case '4': Lval = Lval * 10LL + 4LL; break;
case '5': Lval = Lval * 10LL + 5LL; break;
case '6': Lval = Lval * 10LL + 6LL; break;
case '7': Lval = Lval * 10LL + 7LL; break;
case '8': Lval = Lval * 10LL + 8LL; break;
case '9': Lval = Lval * 10LL + 9LL; break;
default:
p = p2;
} // endswitch *p
if (minus && Lval)
Lval = - Lval;
if (trace)
htrc(" setting double: '%s' -> %lf\n", buf, Fval);
htrc(" setting big int to: %lld\n", Lval);
} // end of SetValue
/***********************************************************************/
/* DFVAL SetValue: fill a double float value from a string. */
/* BIGVAL SetValue: fill a big int value from a string. */
/***********************************************************************/
void DFVAL::SetValue_psz(PSZ s)
void BIGVAL::SetValue_psz(PSZ s)
{
Fval = atof(s);
Lval = atoll(s);
} // end of SetValue
/***********************************************************************/
/* DFVAL SetValue: set value with a double extracted from a block. */
/* BIGVAL SetValue: set value with a int extracted from a block. */
/***********************************************************************/
void DFVAL::SetValue_pvblk(PVBLK blk, int n)
void BIGVAL::SetValue_pvblk(PVBLK blk, int n)
{
Fval = blk->GetFloatValue(n);
Lval = blk->GetBigintValue(n);
} // end of SetValue
/***********************************************************************/
/* SetBinValue: with bytes extracted from a line. */
/* BIGVAL SetBinValue: with bytes extracted from a line. */
/***********************************************************************/
void DFVAL::SetBinValue(void *p)
void BIGVAL::SetBinValue(void *p)
{
Fval = *(double *)p;
Lval = *(longlong *)p;
} // end of SetBinValue
/***********************************************************************/
......@@ -3558,19 +3673,19 @@ void DFVAL::SetBinValue(void *p)
/* returns true if not. Actual filling occurs only if go is true. */
/* Currently used by WriteColumn of binary files. */
/***********************************************************************/
bool DFVAL::GetBinValue(void *buf, int buflen, bool go)
bool BIGVAL::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(double) > buflen)
// if (sizeof(int) > buflen)
// return true;
//#endif
if (go)
*(double *)buf = Fval;
*(longlong *)buf = Lval;
return false;
} // end of GetBinValue
......@@ -3578,52 +3693,755 @@ bool DFVAL::GetBinValue(void *buf, int buflen, bool go)
/***********************************************************************/
/* GetBinValue: used by SELECT when called from QUERY and KINDEX. */
/* This is a fast implementation that does not do any checking. */
/* Note: type is not needed here and just kept for compatibility. */
/***********************************************************************/
void DFVAL::GetBinValue(void *buf, int buflen)
void BIGVAL::GetBinValue(void *buf, int buflen)
{
assert(buflen == sizeof(double));
assert(buflen == sizeof(longlong));
*(double *)buf = Fval;
*(longlong *)buf = Lval;
} // end of GetBinValue
/***********************************************************************/
/* DFVAL ShowValue: get string representation of a double value. */
/* BIGVAL ShowValue: get string representation of a big int value. */
/***********************************************************************/
char *DFVAL::ShowValue(char *buf, int len)
char *BIGVAL::ShowValue(char *buf, int len)
{
// TODO: use snprintf to avoid possible overflow
sprintf(buf, "%*.*lf", len, Prec, Fval);
sprintf(buf, "%*lld", len, Lval);
return buf;
} // end of ShowValue
/***********************************************************************/
/* DFVAL GetCharString: get string representation of a double value. */
/* BIGVAL GetCharString: get string representation of a big int value.*/
/***********************************************************************/
char *DFVAL::GetCharString(char *p)
char *BIGVAL::GetCharString(char *p)
{
sprintf(p, "%.*lf", Prec, Fval);
sprintf(p, "%lld", Lval);
return p;
} // end of GetCharString
/***********************************************************************/
/* DFVAL GetShortString: get short representation of a double value. */
/* BIGVAL GetShortString: get short representation of a int value. */
/***********************************************************************/
char *DFVAL::GetShortString(char *p, int n)
char *BIGVAL::GetShortString(char *p, int n)
{
sprintf(p, "%*hd", n, (short)Fval);
sprintf(p, "%*hd", n, (short)Lval);
return p;
} // end of GetShortString
/***********************************************************************/
/* DFVAL GetIntString: get int representation of a double value. */
/* BIGVAL GetIntString: get int representation of a int value. */
/***********************************************************************/
char *DFVAL::GetIntString(char *p, int n)
char *BIGVAL::GetIntString(char *p, int n)
{
sprintf(p, "%*ld", n, (int)Fval);
sprintf(p, "%*d", n, (int)Lval);
return p;
} // end of GetIntString
/***********************************************************************/
/* BIGVAL GetBigintString: get big int representation of a int value. */
/***********************************************************************/
char *BIGVAL::GetBigintString(char *p, int n)
{
sprintf(p, "%*lld", n, Lval);
return p;
} // end of GetBigintString
/***********************************************************************/
/* BIGVAL GetFloatString: get double representation of a int value. */
/***********************************************************************/
char *BIGVAL::GetFloatString(char *p, int n, int prec)
{
sprintf(p, "%*.*lf", n, (prec < 0) ? 2 : prec, (double)Lval);
return p;
} // end of GetFloatString
/***********************************************************************/
/* BIGVAL compare value with another Value. */
/***********************************************************************/
bool BIGVAL::IsEqual(PVAL vp, bool chktype)
{
if (this == vp)
return true;
else if (chktype && Type != vp->GetType())
return false;
else
return (Lval == vp->GetBigintValue());
} // end of IsEqual
/***********************************************************************/
/* Compare values and returns 1, 0 or -1 according to comparison. */
/* This function is used for evaluation of big int integer filters. */
/***********************************************************************/
int BIGVAL::CompareValue(PVAL vp)
{
//assert(vp->GetType() == Type);
// Process filtering on big int integers.
longlong n = vp->GetBigintValue();
if (trace > 1)
htrc(" Comparing: val=%lld,%lld\n", Lval, n);
return (Lval > n) ? 1 : (Lval < n) ? (-1) : 0;
} // end of CompareValue
/***********************************************************************/
/* SafeAdd: adds a value and test whether overflow/underflow occured. */
/***********************************************************************/
longlong BIGVAL::SafeAdd(longlong n1, longlong n2)
{
PGLOBAL& g = Global;
longlong n = n1 + n2;
if ((n2 > 0LL) && (n < n1)) {
// Overflow
strcpy(g->Message, MSG(FIX_OVFLW_ADD));
longjmp(g->jumper[g->jump_level], 138);
} else if ((n2 < 0LL) && (n > n1)) {
// Underflow
strcpy(g->Message, MSG(FIX_UNFLW_ADD));
longjmp(g->jumper[g->jump_level], 138);
} // endif's n2
return n;
} // end of SafeAdd
/***********************************************************************/
/* SafeMult: multiply values and test whether overflow occured. */
/***********************************************************************/
longlong BIGVAL::SafeMult(longlong n1, longlong n2)
{
PGLOBAL& g = Global;
double n = (double)n1 * (double)n2;
if (n > LLONG_MAX) {
// Overflow
strcpy(g->Message, MSG(FIX_OVFLW_TIMES));
longjmp(g->jumper[g->jump_level], 138);
} else if (n < LLONG_MIN) {
// Underflow
strcpy(g->Message, MSG(FIX_UNFLW_TIMES));
longjmp(g->jumper[g->jump_level], 138);
} // endif's n2
return n1 * n2;
} // end of SafeMult
/***********************************************************************/
/* Compute a function on a int integers. */
/***********************************************************************/
bool BIGVAL::Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op)
{
if (op == OP_LEN) {
assert(np == 1);
char buf[32];
char *p = vp[0]->GetCharString(buf);
Lval = strlen(p);
if (trace)
htrc("Compute result=%lld val=%s op=%d\n", Lval, p, op);
} else if (op == OP_INSTR || op == OP_LIKE || op == OP_CNTIN) {
char *p, *tp = g->Message;
char *p1, val1[32];
char *p2, val2[32];
bool b = (vp[0]->IsCi() || vp[1]->IsCi());
assert(np == 2);
p1 = vp[0]->GetCharString(val1);
p2 = vp[1]->GetCharString(val2);
if (op != OP_LIKE) {
if (!strcmp(p2, "\\t"))
p2 = "\t";
if (b) { // Case insensitive
if (strlen(p1) + strlen(p2) + 1 >= MAX_STR &&
!(tp = new char[strlen(p1) + strlen(p2) + 2])) {
strcpy(g->Message, MSG(NEW_RETURN_NULL));
return true;
} // endif p
// Make a lower case copy of p1 and p2
p1 = strlwr(strcpy(tp, p1));
p2 = strlwr(strcpy(tp + strlen(p1) + 1, p2));
} // endif b
if (op == OP_CNTIN) {
size_t t2 = strlen(p2);
for (Lval = 0LL; (p = strstr(p1, p2)); Lval++, p1 = p + t2) ;
} else // OP_INSTR
Lval = (p = strstr(p1, p2)) ? 1LL + (longlong)(p - p1) : 0LL;
if (tp != g->Message) // If working space was obtained
delete [] tp; // by the use of new, delete it.
} else // OP_LIKE
Lval = (PlugEvalLike(g, p1, p2, b)) ? 1LL : 0LL;
if (trace)
htrc("Compute result=%lld val=%s,%s op=%d\n", Lval, p1, p2, op);
} else {
longlong val[2];
assert(np <= 2);
for (int i = 0; i < np; i++)
val[i] = vp[i]->GetBigintValue();
switch (op) {
case OP_ABS:
assert(np == 1);
Lval = (*val >= 0LL) ? *val : -*val;
break;
case OP_SIGN:
assert(np == 1);
Lval = (*val < 0LL) ? (-1) : 1;
break;
case OP_CEIL:
case OP_FLOOR:
assert(np == 1);
Lval = *val;
break;
case OP_ADD:
assert(np == 2);
Lval = SafeAdd(val[0], val[1]);
break;
case OP_SUB:
assert(np == 2);
Lval = SafeAdd(val[0], -val[1]);
break;
case OP_MULT:
assert(np == 2);
Lval = SafeMult(val[0], val[1]);
break;
case OP_MIN:
assert(np == 2);
Lval = min(val[0], val[1]);
break;
case OP_MAX:
assert(np == 2);
Lval = max(val[0], val[1]);
break;
case OP_DIV:
assert(np == 2);
if (!val[1]) {
strcpy(g->Message, MSG(ZERO_DIVIDE));
return true;
} // endif
Lval = val[0] / val[1];
break;
case OP_MOD:
assert(np == 2);
if (!val[1]) {
strcpy(g->Message, MSG(ZERO_DIVIDE));
return true;
} // endif
Lval = val[0] % val[1];
break;
case OP_BITAND:
assert(np == 2);
Lval = val[0] & val[1];
break;
case OP_BITOR:
assert(np == 2);
Lval = val[0] | val[1];
break;
case OP_BITXOR:
assert(np == 2);
Lval = val[0] ^ val[1];
break;
case OP_BITNOT:
assert(np == 1);
Lval = ~val[0];
break;
case OP_DELTA:
// assert(np == 1);
Lval = val[0] - Lval;
break;
default:
sprintf(g->Message, MSG(BAD_EXP_OPER), op);
return true;
} // endswitch op
if (trace)
if (np = 1)
htrc(" result=%lld val=%lld op=%d\n", Lval, val[0], op);
else
htrc(" result=%lld val=%lld,%lld op=%d\n",
Lval, val[0], val[1], op);
} // endif op
return false;
} // end of Compute
/***********************************************************************/
/* Divide: used by aggregate functions when calculating average. */
/***********************************************************************/
void BIGVAL::Divide(int cnt)
{
Lval /= cnt;
} // end of Divide
/***********************************************************************/
/* StdVar: used by aggregate functions for Stddev and Variance. */
/***********************************************************************/
void BIGVAL::StdVar(PVAL vp, int cnt, bool b)
{
longlong lv2 = vp->GetBigintValue();
Lval = (cnt == 1) ? 0
: (SafeAdd(lv2, -(SafeMult(Lval, Lval) / cnt)) / (cnt - 1));
if (b) // Builtin == FNC_STDDEV
Lval = (longlong)sqrt((double)Lval);
} // end of StdVar
/***********************************************************************/
/* Times: used by aggregate functions for Stddev and Variance. */
/***********************************************************************/
void BIGVAL::Times(PVAL vp)
{
Lval = SafeMult(Lval, vp->GetBigintValue());
} // end of Times
/***********************************************************************/
/* Add: used by aggregate functions for Sum and other functions. */
/***********************************************************************/
void BIGVAL::Add(PVAL vp)
{
Lval = SafeAdd(Lval, vp->GetBigintValue());
} // end of Add
/***********************************************************************/
/* Add: used by QUERY for function Sum and other functions. */
/***********************************************************************/
void BIGVAL::Add(PVBLK vbp, int i)
{
Lval = SafeAdd(Lval, vbp->GetBigintValue(i));
} // end of Add
/***********************************************************************/
/* Add: used by QUERY for function Sum and other functions. */
/***********************************************************************/
void BIGVAL::Add(PVBLK vbp, int j, int k)
{
CheckType(vbp)
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++)
Lval = SafeAdd(Lval, lp[i]);
} // end of Add
/***********************************************************************/
/* Add: used by QUERY for function Sum and other functions. */
/***********************************************************************/
void BIGVAL::Add(PVBLK vbp, int *x, int j, int k)
{
CheckType(vbp)
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++)
Lval = SafeAdd(Lval, lp[x[i]]);
} // end of Add
/***********************************************************************/
/* AddSquare: used by aggregate functions for Stddev and Variance. */
/***********************************************************************/
void BIGVAL::AddSquare(PVAL vp)
{
longlong val = vp->GetBigintValue();
Lval = SafeAdd(Lval, SafeMult(val, val));
} // end of AddSquare
/***********************************************************************/
/* AddSquare: used by QUERY for functions Stddev and Variance. */
/***********************************************************************/
void BIGVAL::AddSquare(PVBLK vbp, int i)
{
longlong val = vbp->GetBigintValue(i);
Lval = SafeAdd(Lval, SafeMult(val, val));
} // end of AddSquare
/***********************************************************************/
/* AddSquare: used by QUERY for functions Stddev and Variance. */
/***********************************************************************/
void BIGVAL::AddSquare(PVBLK vbp, int j, int k)
{
CheckType(vbp)
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++)
Lval = SafeAdd(Lval, SafeMult(lp[i], lp[i]));
} // end of AddSquare
/***********************************************************************/
/* 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 BIGVAL::FormatValue(PVAL vp, char *fmt)
{
char *buf = (char*)vp->GetTo_Val(); // Should be big enough
int n = sprintf(buf, fmt, Lval);
return (n > vp->GetValLen());
} // end of FormatValue
/***********************************************************************/
/* SetMin: used by the aggregate function MIN. */
/***********************************************************************/
void BIGVAL::SetMin(PVAL vp)
{
longlong val = vp->GetBigintValue();
if (val < Lval)
Lval = val;
} // end of SetMin
/***********************************************************************/
/* SetMin: used by QUERY for the aggregate function MIN. */
/***********************************************************************/
void BIGVAL::SetMin(PVBLK vbp, int i)
{
longlong val = vbp->GetBigintValue(i);
if (val < Lval)
Lval = val;
} // end of SetMin
/***********************************************************************/
/* SetMin: used by QUERY for the aggregate function MIN. */
/***********************************************************************/
void BIGVAL::SetMin(PVBLK vbp, int j, int k)
{
CheckType(vbp)
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++)
if (lp[i] < Lval)
Lval = lp[i];
} // end of SetMin
/***********************************************************************/
/* SetMin: used by QUERY for the aggregate function MIN. */
/***********************************************************************/
void BIGVAL::SetMin(PVBLK vbp, int *x, int j, int k)
{
CheckType(vbp)
longlong val;
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++) {
val = lp[x[i]];
if (val < Lval)
Lval = val;
} // endfor i
} // end of SetMin
/***********************************************************************/
/* SetMax: used by the aggregate function MAX. */
/***********************************************************************/
void BIGVAL::SetMax(PVAL vp)
{
longlong val = vp->GetBigintValue();
if (val > Lval)
Lval = val;
} // end of SetMax
/***********************************************************************/
/* SetMax: used by QUERY for the aggregate function MAX. */
/***********************************************************************/
void BIGVAL::SetMax(PVBLK vbp, int i)
{
longlong val = vbp->GetBigintValue(i);
if (val > Lval)
Lval = val;
} // end of SetMax
/***********************************************************************/
/* SetMax: used by QUERY for the aggregate function MAX. */
/***********************************************************************/
void BIGVAL::SetMax(PVBLK vbp, int j, int k)
{
CheckType(vbp)
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++)
if (lp[i] > Lval)
Lval = lp[i];
} // end of SetMax
/***********************************************************************/
/* SetMax: used by QUERY for the aggregate function MIN. */
/***********************************************************************/
void BIGVAL::SetMax(PVBLK vbp, int *x, int j, int k)
{
CheckType(vbp)
longlong val;
longlong *lp = (longlong *)vbp->GetValPointer();
for (register int i = j; i < k; i++) {
val = lp[x[i]];
if (val > Lval)
Lval = val;
} // endfor i
} // end of SetMax
/***********************************************************************/
/* BIGVAL SetFormat function (used to set SELECT output format). */
/***********************************************************************/
bool BIGVAL::SetConstFormat(PGLOBAL g, FORMAT& fmt)
{
char c[16];
fmt.Type[0] = 'L';
fmt.Length = sprintf(c, "%lld", Lval);
fmt.Prec = 0;
return false;
} // end of SetConstFormat
/***********************************************************************/
/* Make file output of a big int object. */
/***********************************************************************/
void BIGVAL::Print(PGLOBAL g, FILE *f, uint n)
{
char m[64];
memset(m, ' ', n); /* Make margin string */
m[n] = '\0';
fprintf(f, "%s%lld\n", m, Lval);
} /* end of Print */
/***********************************************************************/
/* Make string output of a int object. */
/***********************************************************************/
void BIGVAL::Print(PGLOBAL g, char *ps, uint z)
{
sprintf(ps, "%lld", Lval);
} /* end of Print */
/* -------------------------- Class DFVAL ---------------------------- */
/***********************************************************************/
/* DFVAL public constructor from char. */
/***********************************************************************/
DFVAL::DFVAL(PSZ s, int prec) : VALUE(TYPE_FLOAT)
{
Fval = atof(s);
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
/***********************************************************************/
/* DFVAL public constructor from short. */
/***********************************************************************/
DFVAL::DFVAL(short n, int prec) : VALUE(TYPE_FLOAT)
{
Fval = (double)n;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
/***********************************************************************/
/* DFVAL public constructor from int. */
/***********************************************************************/
DFVAL::DFVAL(int n, int prec) : VALUE(TYPE_FLOAT)
{
Fval = (double)n;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
/***********************************************************************/
/* DFVAL public constructor from double. */
/***********************************************************************/
DFVAL::DFVAL(double f, int prec) : VALUE(TYPE_FLOAT)
{
Fval = f;
Prec = prec;
Clen = sizeof(double);
} // end of DFVAL constructor
/***********************************************************************/
/* DFVAL GetValLen: returns the print length of the double object. */
/***********************************************************************/
int DFVAL::GetValLen(void)
{
char c[32];
return sprintf(c, "%.*lf", Prec, Fval);
} // end of GetValLen
/***********************************************************************/
/* DFVAL SetValue: copy the value of another Value object. */
/* This function allows conversion if chktype is false. */
/***********************************************************************/
bool DFVAL::SetValue_pval(PVAL valp, bool chktype)
{
if (chktype && Type != valp->GetType())
return true;
Fval = valp->GetFloatValue();
return false;
} // end of SetValue
/***********************************************************************/
/* SetValue: convert chars extracted from a line to double value. */
/***********************************************************************/
void DFVAL::SetValue_char(char *p, int n)
{
char *p2, buf[32];
for (p2 = p + n; p < p2 && *p == ' '; p++) ;
n = min(p2 - p, 31);
memcpy(buf, p, n);
buf[n] = '\0';
Fval = atof(buf);
if (trace)
htrc(" setting double: '%s' -> %lf\n", buf, Fval);
} // end of SetValue
/***********************************************************************/
/* DFVAL SetValue: fill a double float value from a string. */
/***********************************************************************/
void DFVAL::SetValue_psz(PSZ s)
{
Fval = atof(s);
} // end of SetValue
/***********************************************************************/
/* DFVAL SetValue: set value with a double extracted from a block. */
/***********************************************************************/
void DFVAL::SetValue_pvblk(PVBLK blk, int n)
{
Fval = blk->GetFloatValue(n);
} // end of SetValue
/***********************************************************************/
/* SetBinValue: with bytes extracted from a line. */
/***********************************************************************/
void DFVAL::SetBinValue(void *p)
{
Fval = *(double *)p;
} // 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 DFVAL::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(double) > buflen)
// return true;
//#endif
if (go)
*(double *)buf = Fval;
return false;
} // end of GetBinValue
/***********************************************************************/
/* GetBinValue: used by SELECT when called from QUERY and KINDEX. */
/* This is a fast implementation that does not do any checking. */
/* Note: type is not needed here and just kept for compatibility. */
/***********************************************************************/
void DFVAL::GetBinValue(void *buf, int buflen)
{
assert(buflen == sizeof(double));
*(double *)buf = Fval;
} // end of GetBinValue
/***********************************************************************/
/* DFVAL ShowValue: get string representation of a double value. */
/***********************************************************************/
char *DFVAL::ShowValue(char *buf, int len)
{
// TODO: use snprintf to avoid possible overflow
sprintf(buf, "%*.*lf", len, Prec, Fval);
return buf;
} // end of ShowValue
/***********************************************************************/
/* DFVAL GetCharString: get string representation of a double value. */
/***********************************************************************/
char *DFVAL::GetCharString(char *p)
{
sprintf(p, "%.*lf", Prec, Fval);
return p;
} // end of GetCharString
/***********************************************************************/
/* DFVAL GetShortString: get short representation of a double value. */
/***********************************************************************/
char *DFVAL::GetShortString(char *p, int n)
{
sprintf(p, "%*hd", n, (short)Fval);
return p;
} // end of GetShortString
/***********************************************************************/
/* DFVAL GetIntString: get int representation of a double value. */
/***********************************************************************/
char *DFVAL::GetIntString(char *p, int n)
{
sprintf(p, "%*ld", n, (int)Fval);
return p;
} // end of GetIntString
/***********************************************************************/
/* DFVAL GetBigintString: get big int representation of a double val. */
/***********************************************************************/
char *DFVAL::GetBigintString(char *p, int n)
{
sprintf(p, "%*lld", n, (longlong)Fval);
return p;
} // end of GetBigintString
/***********************************************************************/
/* DFVAL GetFloatString: get double representation of a double value. */
/***********************************************************************/
......
/**************** Value H Declares Source Code File (.H) ***************/
/* Name: VALUE.H Version 1.6 */
/* Name: VALUE.H Version 1.7 */
/* */
/* (C) Copyright to the author Olivier BERTRAND 2001-2012 */
/* (C) Copyright to the author Olivier BERTRAND 2001-2013 */
/* */
/* This file contains the VALUE and derived classes declares. */
/***********************************************************************/
......@@ -16,6 +16,11 @@
#include "assert.h"
#include "block.h"
#if defined(WIN32)
#define strtoll _strtoi64
#define atoll(S) strtoll(S, NULL, 10)
#endif // WIN32
/***********************************************************************/
/* Types used in some class definitions. */
/***********************************************************************/
......@@ -72,6 +77,7 @@ class DllExport VALUE : public BLOCK {
virtual PSZ GetCharValue(void) {assert(false); return NULL;}
virtual short GetShortValue(void) {assert(false); return 0;}
virtual int GetIntValue(void) = 0;
virtual longlong GetBigintValue(void) = 0;
virtual double GetFloatValue(void) = 0;
virtual void *GetTo_Val(void) = 0;
int GetType(void) {return Type;}
......@@ -85,6 +91,7 @@ class DllExport VALUE : public BLOCK {
virtual void SetValue_bool(bool b) {assert(false);}
virtual void SetValue(short i) {assert(false);}
virtual void SetValue(int n) {assert(false);}
virtual void SetValue(longlong n) {assert(false);}
virtual void SetValue(double f) {assert(false);}
virtual void SetValue_pvblk(PVBLK blk, int n) = 0;
virtual void SetBinValue(void *p) = 0;
......@@ -116,6 +123,7 @@ class DllExport VALUE : public BLOCK {
virtual char *GetCharString(char *p) = 0;
virtual char *GetShortString(char *p, int n) {return "#####";}
virtual char *GetIntString(char *p, int n) = 0;
virtual char *GetBigintString(char *p, int n) = 0;
virtual char *GetFloatString(char *p, int n, int prec) = 0;
virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op) = 0;
virtual int GetTime(PGLOBAL g, PVAL *vp, int np) = 0;
......@@ -145,6 +153,7 @@ class STRING : public VALUE {
STRING(PGLOBAL g, PSZ s, int n, int c = 0);
STRING(PGLOBAL g, short i);
STRING(PGLOBAL g, int n);
STRING(PGLOBAL g, longlong n);
STRING(PGLOBAL g, double f);
// Implementation
......@@ -159,6 +168,7 @@ class STRING : public VALUE {
virtual PSZ GetCharValue(void) {return Strp;}
virtual short GetShortValue(void) {return (short)atoi(Strp);}
virtual int GetIntValue(void) {return atol(Strp);}
virtual longlong GetBigintValue(void) {return strtoll(Strp, NULL, 10);}
virtual double GetFloatValue(void) {return atof(Strp);}
virtual void *GetTo_Val(void) {return Strp;}
......@@ -169,6 +179,7 @@ class STRING : public VALUE {
virtual void SetValue_pvblk(PVBLK blk, int n);
virtual void SetValue(short i);
virtual void SetValue(int n);
virtual void SetValue(longlong n);
virtual void SetValue(double f);
virtual void SetBinValue(void *p);
virtual bool GetBinValue(void *buf, int buflen, bool go);
......@@ -177,6 +188,7 @@ class STRING : public VALUE {
virtual char *GetCharString(char *p);
virtual char *GetShortString(char *p, int n);
virtual char *GetIntString(char *p, int n);
virtual char *GetBigintString(char *p, int n);
virtual char *GetFloatString(char *p, int n, int prec = -1);
virtual bool IsEqual(PVAL vp, bool chktype);
virtual int CompareValue(PVAL vp);
......@@ -215,6 +227,7 @@ class SHVAL : public VALUE {
SHVAL(PSZ s);
SHVAL(short n);
SHVAL(int n);
SHVAL(longlong n);
SHVAL(double f);
// Implementation
......@@ -227,6 +240,7 @@ class SHVAL : public VALUE {
//virtual PSZ GetCharValue(void) {}
virtual short GetShortValue(void) {return Sval;}
virtual int GetIntValue(void) {return (int)Sval;}
virtual longlong GetBigintValue(void) {return (longlong)Sval;}
virtual double GetFloatValue(void) {return (double)Sval;}
virtual void *GetTo_Val(void) {return &Sval;}
......@@ -237,6 +251,7 @@ class SHVAL : public VALUE {
virtual void SetValue_bool(bool b) {Sval = (b) ? 1 : 0;}
virtual void SetValue(short i) {Sval = i;}
virtual void SetValue(int n) {Sval = (short)n;}
virtual void SetValue(longlong n) {Sval = (short)n;}
virtual void SetValue_pvblk(PVBLK blk, int n);
virtual void SetBinValue(void *p);
virtual bool GetBinValue(void *buf, int buflen, bool go);
......@@ -245,6 +260,7 @@ class SHVAL : public VALUE {
virtual char *GetCharString(char *p);
virtual char *GetShortString(char *p, int n);
virtual char *GetIntString(char *p, int n);
virtual char *GetBigintString(char *p, int n);
virtual char *GetFloatString(char *p, int n, int prec = -1);
virtual bool IsEqual(PVAL vp, bool chktype);
virtual int CompareValue(PVAL vp);
......@@ -293,6 +309,7 @@ class DllExport INTVAL : public VALUE {
INTVAL(PSZ s);
INTVAL(short i);
INTVAL(int n);
INTVAL(longlong n);
INTVAL(double f);
// Implementation
......@@ -305,6 +322,7 @@ class DllExport INTVAL : public VALUE {
//virtual PSZ GetCharValue(void) {}
virtual short GetShortValue(void) {return (short)Ival;}
virtual int GetIntValue(void) {return Ival;}
virtual longlong GetBigintValue(void) {return (longlong)Ival;}
virtual double GetFloatValue(void) {return (double)Ival;}
virtual void *GetTo_Val(void) {return &Ival;}
......@@ -315,6 +333,7 @@ class DllExport INTVAL : public VALUE {
virtual void SetValue_bool(bool b) {Ival = (b) ? 1 : 0;}
virtual void SetValue(short i) {Ival = (int)i;}
virtual void SetValue(int n) {Ival = n;}
virtual void SetValue(longlong n) {Ival = (int)n;}
virtual void SetValue(double f) {Ival = (int)f;}
virtual void SetValue_pvblk(PVBLK blk, int n);
virtual void SetBinValue(void *p);
......@@ -324,6 +343,7 @@ class DllExport INTVAL : public VALUE {
virtual char *GetCharString(char *p);
virtual char *GetShortString(char *p, int n);
virtual char *GetIntString(char *p, int n);
virtual char *GetBigintString(char *p, int n);
virtual char *GetFloatString(char *p, int n, int prec = -1);
virtual bool IsEqual(PVAL vp, bool chktype);
virtual int CompareValue(PVAL vp);
......@@ -373,6 +393,7 @@ class DllExport DTVAL : public INTVAL {
DTVAL(PGLOBAL g, PSZ s, int n);
DTVAL(PGLOBAL g, short i);
DTVAL(PGLOBAL g, int n);
DTVAL(PGLOBAL g, longlong n);
DTVAL(PGLOBAL g, double f);
// Implementation
......@@ -413,6 +434,89 @@ class DllExport DTVAL : public INTVAL {
int Len; // Used by CHAR scalar function
}; // end of class DTVAL
/***********************************************************************/
/* Class BIGVAL: represents bigint integer values. */
/***********************************************************************/
class DllExport BIGVAL : public VALUE {
public:
// Constructors
BIGVAL(PSZ s);
BIGVAL(short i);
BIGVAL(int n);
BIGVAL(longlong n);
BIGVAL(double f);
// Implementation
virtual bool IsTypeNum(void) {return true;}
virtual bool IsZero(void) {return Lval == 0;}
virtual void Reset(void) {Lval = 0;}
virtual int GetValLen(void);
virtual int GetValPrec() {return 0;}
virtual int GetSize(void) {return sizeof(longlong);}
//virtual PSZ GetCharValue(void) {}
virtual short GetShortValue(void) {return (short)Lval;}
virtual int GetIntValue(void) {return (int)Lval;}
virtual longlong GetBigintValue(void) {return Lval;}
virtual double GetFloatValue(void) {return (double)Lval;}
virtual void *GetTo_Val(void) {return &Lval;}
// Methods
virtual bool SetValue_pval(PVAL valp, bool chktype);
virtual void SetValue_char(char *p, int n);
virtual void SetValue_psz(PSZ s);
virtual void SetValue_bool(bool b) {Lval = (b) ? 1 : 0;}
virtual void SetValue(short i) {Lval = (longlong)i;}
virtual void SetValue(int n) {Lval = (longlong)n;}
virtual void SetValue(longlong n) {Lval = n;}
virtual void SetValue(double f) {Lval = (longlong)f;}
virtual void SetValue_pvblk(PVBLK blk, int n);
virtual void SetBinValue(void *p);
virtual bool GetBinValue(void *buf, int buflen, bool go);
virtual void GetBinValue(void *buf, int len);
virtual char *ShowValue(char *buf, int);
virtual char *GetCharString(char *p);
virtual char *GetShortString(char *p, int n);
virtual char *GetIntString(char *p, int n);
virtual char *GetBigintString(char *p, int n);
virtual char *GetFloatString(char *p, int n, int prec = -1);
virtual bool IsEqual(PVAL vp, bool chktype);
virtual int CompareValue(PVAL vp);
virtual void Divide(int cnt);
virtual void StdVar(PVAL vp, int cnt, bool b);
virtual void Add(int lv) {Lval += (longlong)lv;}
virtual void Add(PVAL vp);
virtual void Add(PVBLK vbp, int i);
virtual void Add(PVBLK vbp, int j, int k);
virtual void Add(PVBLK vbp, int *x, int j, int k);
virtual void AddSquare(PVAL vp);
virtual void AddSquare(PVBLK vbp, int i);
virtual void AddSquare(PVBLK vbp, int j, int k);
virtual void Times(PVAL vp);
virtual void SetMin(PVAL vp);
virtual void SetMin(PVBLK vbp, int i);
virtual void SetMin(PVBLK vbp, int j, int k);
virtual void SetMin(PVBLK vbp, int *x, int j, int k);
virtual void SetMax(PVAL vp);
virtual void SetMax(PVBLK vbp, int i);
virtual void SetMax(PVBLK vbp, int j, int k);
virtual void SetMax(PVBLK vbp, int *x, int j, int k);
virtual bool SetConstFormat(PGLOBAL, FORMAT&);
virtual bool Compute(PGLOBAL g, PVAL *vp, int np, OPVAL op);
virtual int GetTime(PGLOBAL g, PVAL *vp, int np) {return 0;}
virtual bool FormatValue(PVAL vp, char *fmt);
virtual void Print(PGLOBAL g, FILE *, uint);
virtual void Print(PGLOBAL g, char *, uint);
protected:
longlong SafeAdd(longlong n1, longlong n2);
longlong SafeMult(longlong n1, longlong n2);
// Default constructor not to be used
BIGVAL(void) : VALUE(TYPE_ERROR) {}
// Members
longlong Lval;
}; // end of class BIGVAL
/***********************************************************************/
/* Class DFVAL: represents double float values. */
/***********************************************************************/
......@@ -422,6 +526,7 @@ class DFVAL : public VALUE {
DFVAL(PSZ s, int prec = 2);
DFVAL(short i, int prec = 2);
DFVAL(int n, int prec = 2);
DFVAL(longlong n, int prec = 2);
DFVAL(double f, int prec = 2);
// Implementation
......@@ -434,6 +539,7 @@ class DFVAL : public VALUE {
//virtual PSZ GetCharValue(void) {}
virtual short GetShortValue(void) {return (short)Fval;}
virtual int GetIntValue(void) {return (int)Fval;}
virtual longlong GetBigintValue(void) {return (longlong)Fval;}
virtual double GetFloatValue(void) {return Fval;}
virtual void *GetTo_Val(void) {return &Fval;}
void SetPrec(int prec) {Prec = prec;}
......@@ -444,6 +550,7 @@ class DFVAL : public VALUE {
virtual void SetValue_psz(PSZ s);
virtual void SetValue(short i) {Fval = (double)i;}
virtual void SetValue(int n) {Fval = (double)n;}
virtual void SetValue(longlong n) {Fval = (double)n;}
virtual void SetValue(double f) {Fval = f;}
virtual void SetValue_pvblk(PVBLK blk, int n);
virtual void SetBinValue(void *p);
......@@ -453,6 +560,7 @@ class DFVAL : public VALUE {
virtual char *GetCharString(char *p);
virtual char *GetShortString(char *p, int n);
virtual char *GetIntString(char *p, int n);
virtual char *GetBigintString(char *p, int n);
virtual char *GetFloatString(char *p, int n, int prec = -1);
virtual bool IsEqual(PVAL vp, bool chktype);
virtual int CompareValue(PVAL vp);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment