1 /*************** Valblk H Declares Source Code File (.H) ***************/
2 /*  Name: VALBLK.H    Version 2.1                                      */
3 /*                                                                     */
4 /*  (C) Copyright to the author Olivier BERTRAND          2005-2014    */
5 /*                                                                     */
6 /*  This file contains the VALBLK and derived classes declares.        */
7 /***********************************************************************/
8 
9 /***********************************************************************/
10 /*  Include required application header files                          */
11 /*  assert.h     is header required when using the assert function.    */
12 /*  block.h      is header containing Block    global declarations.    */
13 /***********************************************************************/
14 #ifndef __VALBLK__H__
15 #define __VALBLK__H__
16 #include "value.h"
17 
18 /***********************************************************************/
19 /*  Utility used to allocate value blocks.                             */
20 /***********************************************************************/
21 DllExport PVBLK AllocValBlock(PGLOBAL, void*, int, int, int, int,
22                                               bool, bool, bool);
23 const char *GetFmt(int type, bool un = false);
24 
25 /***********************************************************************/
26 /*  DB static external variables.                                      */
27 /***********************************************************************/
28 extern MBLOCK Nmblk;                /* Used to initialize MBLOCK's     */
29 
30 /***********************************************************************/
31 /*  Class MBVALS is a utility class for (re)allocating VALBLK's.       */
32 /***********************************************************************/
33 class MBVALS : public BLOCK {
34 //friend class LSTBLK;
35   friend class ARRAY;
36  public:
37   // Constructors
MBVALS(void)38   MBVALS(void) {Vblk = NULL; Mblk = Nmblk;}
39 
40   // Methods
GetMemp(void)41   void  *GetMemp(void) {return Mblk.Memp;}
42   PVBLK  Allocate(PGLOBAL g, int type, int len, int prec,
43                              int n, bool sub = false);
44   bool   ReAllocate(PGLOBAL g, int n);
45   void   Free(void);
46 
47  protected:
48   // Members
49   PVBLK  Vblk;                    // Pointer to VALBLK
50   MBLOCK Mblk;                    // The memory block
51   }; // end of class MBVALS
52 
53 typedef class MBVALS *PMBV;
54 
55 /***********************************************************************/
56 /*  Class VALBLK represent a base class for variable blocks.           */
57 /***********************************************************************/
58 class VALBLK : public BLOCK {
59  public:
60   // Constructors
61   VALBLK(void *mp, int type, int nval, bool un = false);
62 
63   // Implementation
GetNval(void)64           int    GetNval(void) {return Nval;}
SetNval(int n)65           void   SetNval(int n) {Nval = n;}
GetValPointer(void)66           void  *GetValPointer(void) {return Blkp;}
SetValPointer(void * mp)67           void   SetValPointer(void *mp) {Blkp = mp;}
GetType(void)68           int    GetType(void) {return Type;}
GetPrec(void)69           int    GetPrec(void) {return Prec;}
SetCheck(bool b)70           void   SetCheck(bool b) {Check = b;}
MoveNull(int i,int j)71           void   MoveNull(int i, int j)
72                   {if (To_Nulls) To_Nulls[j] = To_Nulls[i];}
SetNull(int n,bool b)73   virtual void   SetNull(int n, bool b)
74                   {if (To_Nulls) {To_Nulls[n] = (b) ? '*' : 0;}}
IsNull(int n)75   virtual bool   IsNull(int n) {return To_Nulls && To_Nulls[n];}
IsNullable(void)76 	virtual bool   IsNullable(void) {return Nullable;}
77 	virtual void   SetNullable(bool b);
IsUnsigned(void)78   virtual bool   IsUnsigned(void) {return Unsigned;}
79   virtual bool   Init(PGLOBAL g, bool check) = 0;
80   virtual int    GetVlen(void) = 0;
81   virtual PSZ    GetCharValue(int n);
82   virtual char   GetTinyValue(int n) = 0;
83   virtual uchar  GetUTinyValue(int n) = 0;
84   virtual short  GetShortValue(int n) = 0;
85   virtual ushort GetUShortValue(int n) = 0;
86   virtual int    GetIntValue(int n) = 0;
87   virtual uint   GetUIntValue(int n) = 0;
88   virtual longlong GetBigintValue(int n) = 0;
89   virtual ulonglong GetUBigintValue(int n) = 0;
90   virtual double GetFloatValue(int n) = 0;
91   virtual char  *GetCharString(char *p, int n) = 0;
ReAlloc(void * mp,int n)92   virtual void   ReAlloc(void *mp, int n) {Blkp = mp; Nval = n;}
93   virtual void   Reset(int n) = 0;
94   virtual bool   SetFormat(PGLOBAL g, PCSZ fmt, int len, int year = 0);
SetPrec(int p)95   virtual void   SetPrec(int p) {}
IsCi(void)96   virtual bool   IsCi(void) {return false;}
97 
98   // Methods
SetValue(short,int)99   virtual void   SetValue(short, int) {assert(false);}
SetValue(ushort,int)100   virtual void   SetValue(ushort, int) {assert(false);}
SetValue(int,int)101   virtual void   SetValue(int, int) {assert(false);}
SetValue(uint,int)102   virtual void   SetValue(uint, int) {assert(false);}
SetValue(longlong,int)103   virtual void   SetValue(longlong, int) {assert(false);}
SetValue(ulonglong,int)104   virtual void   SetValue(ulonglong, int) {assert(false);}
SetValue(double,int)105   virtual void   SetValue(double, int) {assert(false);}
SetValue(char,int)106   virtual void   SetValue(char, int) {assert(false);}
SetValue(uchar,int)107   virtual void   SetValue(uchar, int) {assert(false);}
SetValue(PCSZ,int)108   virtual void   SetValue(PCSZ, int) {assert(false);}
SetValue(const char *,uint,int)109   virtual void   SetValue(const char *, uint, int) {assert(false);}
110   virtual void   SetValue(PVAL valp, int n) = 0;
111   virtual void   SetValue(PVBLK pv, int n1, int n2) = 0;
112   virtual void   SetMin(PVAL valp, int n) = 0;
113   virtual void   SetMax(PVAL valp, int n) = 0;
114   virtual void   Move(int i, int j) = 0;
115   virtual int    CompVal(PVAL vp, int n) = 0;
116   virtual int    CompVal(int i1, int i2) = 0;
117   virtual void  *GetValPtr(int n) = 0;
118   virtual void  *GetValPtrEx(int n) = 0;
119   virtual int    Find(PVAL vp) = 0;
120   virtual int    GetMaxLength(void) = 0;
121           bool   Locate(PVAL vp, int& i);
122 
123  protected:
124   bool AllocBuff(PGLOBAL g, size_t size);
125   void ChkIndx(int n);
126   void ChkTyp(PVAL v);
127   void ChkTyp(PVBLK vb);
128 
129   // Members
130   PGLOBAL Global;           // Used for messages and allocation
131   MBLOCK  Mblk;             // Used to allocate buffer
132   char   *To_Nulls;         // Null values array
133   void   *Blkp;             // To value block
134   bool    Check;            // If true SetValue types must match
135   bool    Nullable;         // True if values can be null
136   bool    Unsigned;         // True if values are unsigned
137   int     Type;             // Type of individual values
138   int     Nval;             // Max number of values in block
139   int     Prec;             // Precision of float values
140   }; // end of class VALBLK
141 
142 /***********************************************************************/
143 /*  Class TYPBLK: represents a block of typed values.                  */
144 /***********************************************************************/
145 template <class TYPE>
146 class TYPBLK : public VALBLK {
147  public:
148   // Constructors
149   TYPBLK(void *mp, int size, int type, int prec = 0, bool un = false);
150 
151   // Implementation
152   virtual bool   Init(PGLOBAL g, bool check);
GetVlen(void)153   virtual int    GetVlen(void) {return sizeof(TYPE);}
154 
GetTinyValue(int n)155   virtual char   GetTinyValue(int n) {return (char)UnalignedRead(n);}
GetUTinyValue(int n)156   virtual uchar  GetUTinyValue(int n) {return (uchar)UnalignedRead(n);}
GetShortValue(int n)157   virtual short  GetShortValue(int n) {return (short)UnalignedRead(n);}
GetUShortValue(int n)158   virtual ushort GetUShortValue(int n) {return (ushort)UnalignedRead(n);}
GetIntValue(int n)159   virtual int    GetIntValue(int n) {return (int)UnalignedRead(n);}
GetUIntValue(int n)160   virtual uint   GetUIntValue(int n) {return (uint)UnalignedRead(n);}
GetBigintValue(int n)161   virtual longlong GetBigintValue(int n) {return (longlong)UnalignedRead(n);}
GetUBigintValue(int n)162   virtual ulonglong GetUBigintValue(int n) {return (ulonglong)UnalignedRead(n);}
GetFloatValue(int n)163   virtual double GetFloatValue(int n) {return (double)UnalignedRead(n);}
164   virtual char  *GetCharString(char *p, int n);
Reset(int n)165   virtual void   Reset(int n) {UnalignedWrite(n, 0);}
166 
167   // Methods
168   using VALBLK::SetValue;
169   virtual void   SetValue(PCSZ sp, int n);
170   virtual void   SetValue(const char *sp, uint len, int n);
SetValue(short sval,int n)171   virtual void   SetValue(short sval, int n)
172                   {UnalignedWrite(n, (TYPE)sval); SetNull(n, false);}
SetValue(ushort sval,int n)173   virtual void   SetValue(ushort sval, int n)
174                   {UnalignedWrite(n, (TYPE)sval); SetNull(n, false);}
SetValue(int lval,int n)175   virtual void   SetValue(int lval, int n)
176                   {UnalignedWrite(n, (TYPE)lval); SetNull(n, false);}
SetValue(uint lval,int n)177   virtual void   SetValue(uint lval, int n)
178                   {UnalignedWrite(n, (TYPE)lval); SetNull(n, false);}
SetValue(longlong lval,int n)179   virtual void   SetValue(longlong lval, int n)
180                   {UnalignedWrite(n, (TYPE)lval); SetNull(n, false);}
SetValue(ulonglong lval,int n)181   virtual void   SetValue(ulonglong lval, int n)
182                   {UnalignedWrite(n, (TYPE)lval); SetNull(n, false);}
SetValue(double fval,int n)183   virtual void   SetValue(double fval, int n)
184                   {UnalignedWrite(n, (TYPE)fval); SetNull(n, false);}
SetValue(char cval,int n)185   virtual void   SetValue(char cval, int n)
186                   {UnalignedWrite(n, (TYPE)cval); SetNull(n, false);}
SetValue(uchar cval,int n)187   virtual void   SetValue(uchar cval, int n)
188                   {UnalignedWrite(n, (TYPE)cval); SetNull(n, false);}
189   virtual void   SetValue(PVAL valp, int n);
190   virtual void   SetValue(PVBLK pv, int n1, int n2);
191   virtual void   SetMin(PVAL valp, int n);
192   virtual void   SetMax(PVAL valp, int n);
193   virtual void   Move(int i, int j);
194   virtual int    CompVal(PVAL vp, int n);
195   virtual int    CompVal(int i1, int i2);
196   virtual void  *GetValPtr(int n);
197   virtual void  *GetValPtrEx(int n);
198   virtual int    Find(PVAL vp);
199   virtual int    GetMaxLength(void);
200 
201  protected:
202   // Specialized functions
203   static ulonglong MaxVal(void);
204   TYPE GetTypedValue(PVAL vp);
205   TYPE GetTypedValue(PVBLK blk, int n);
206 
207   // Members
208   TYPE* const &Typp;
209   const char  *Fmt;
210 
211   // Unaligned access
UnalignedRead(int n)212   TYPE UnalignedRead(int n) const {
213     TYPE result;
214     memcpy(&result, Typp + n, sizeof(TYPE));
215     return result;
216   }
217 
UnalignedWrite(int n,TYPE value)218   void UnalignedWrite(int n, TYPE value) {
219     memcpy(Typp + n, &value, sizeof(TYPE));
220   }
221   }; // end of class TYPBLK
222 
223 /***********************************************************************/
224 /*  Class CHRBLK: represent a block of fixed length strings.           */
225 /***********************************************************************/
226 class CHRBLK : public VALBLK {
227  public:
228   // Constructors
229   CHRBLK(void *mp, int size, int type, int len, int prec, bool b);
230 
231   // Implementation
232   virtual bool   Init(PGLOBAL g, bool check);
GetVlen(void)233   virtual int    GetVlen(void) {return Long;}
234   virtual PSZ    GetCharValue(int n);
235   virtual char   GetTinyValue(int n);
236   virtual uchar  GetUTinyValue(int n);
237   virtual short  GetShortValue(int n);
238   virtual ushort GetUShortValue(int n);
239   virtual int    GetIntValue(int n);
240   virtual uint   GetUIntValue(int n);
241   virtual longlong GetBigintValue(int n);
242   virtual ulonglong GetUBigintValue(int n);
243   virtual double GetFloatValue(int n);
244   virtual char  *GetCharString(char *p, int n);
245   virtual void   Reset(int n);
SetPrec(int p)246   virtual void   SetPrec(int p) {Ci = (p != 0);}
IsCi(void)247   virtual bool   IsCi(void) {return Ci;}
248 
249   // Methods
250   using VALBLK::SetValue;
251   virtual void   SetValue(PCSZ sp, int n);
252   virtual void   SetValue(const char *sp, uint len, int n);
253   virtual void   SetValue(PVAL valp, int n);
254   virtual void   SetValue(PVBLK pv, int n1, int n2);
255   virtual void   SetMin(PVAL valp, int n);
256   virtual void   SetMax(PVAL valp, int n);
257   virtual void   Move(int i, int j);
258   virtual int    CompVal(PVAL vp, int n);
259   virtual int    CompVal(int i1, int i2);
260   virtual void  *GetValPtr(int n);
261   virtual void  *GetValPtrEx(int n);
262   virtual int    Find(PVAL vp);
263   virtual int    GetMaxLength(void);
264 
265  protected:
266   // Members
267   char* const &Chrp;             // Pointer to char buffer
268   PSZ   Valp;                    // Used to make a zero ended value
269   bool  Blanks;                  // True for right filling with blanks
270   bool  Ci;                      // True if case insensitive
271   int   Long;                    // Length of each string
272   }; // end of class CHRBLK
273 
274 /***********************************************************************/
275 /*  Class STRBLK: represent a block of string pointers.                */
276 /*  Currently this class is used only by the DECODE scalar function    */
277 /*  and by the MyColumn function to store date formats.                */
278 /***********************************************************************/
279 class STRBLK : public VALBLK {
280  public:
281   // Constructors
282   STRBLK(PGLOBAL g, void *mp, int size, int type);
283 
284   // Implementation
SetNull(int n,bool b)285   virtual void   SetNull(int n, bool b) {if (b) {Strp[n] = NULL;}}
IsNull(int n)286   virtual bool   IsNull(int n) {return Strp[n] == NULL;}
SetNullable(bool)287   virtual void   SetNullable(bool) {}      // Always nullable
288   virtual bool   Init(PGLOBAL g, bool check);
GetVlen(void)289   virtual int    GetVlen(void) {return sizeof(PSZ);}
GetCharValue(int n)290   virtual PSZ    GetCharValue(int n) {return Strp[n];}
291   virtual char   GetTinyValue(int n);
292   virtual uchar  GetUTinyValue(int n);
293   virtual short  GetShortValue(int n);
294   virtual ushort GetUShortValue(int n);
295   virtual int    GetIntValue(int n);
296   virtual uint   GetUIntValue(int n);
297   virtual longlong GetBigintValue(int n);
298   virtual ulonglong GetUBigintValue(int n);
GetFloatValue(int n)299   virtual double GetFloatValue(int n) {return atof(Strp[n]);}
GetCharString(char *,int n)300   virtual char  *GetCharString(char *, int n) {return Strp[n];}
Reset(int n)301   virtual void   Reset(int n) {Strp[n] = NULL;}
302 
303   // Methods
304   using VALBLK::SetValue;
305   virtual void   SetValue(PCSZ sp, int n);
306   virtual void   SetValue(const char *sp, uint len, int n);
307   virtual void   SetValue(PVAL valp, int n);
308   virtual void   SetValue(PVBLK pv, int n1, int n2);
309   virtual void   SetMin(PVAL valp, int n);
310   virtual void   SetMax(PVAL valp, int n);
311   virtual void   Move(int i, int j);
312   virtual int    CompVal(PVAL vp, int n);
313   virtual int    CompVal(int i1, int i2);
314   virtual void  *GetValPtr(int n);
315   virtual void  *GetValPtrEx(int n);
316   virtual int    Find(PVAL vp);
317   virtual int    GetMaxLength(void);
318 
319   // Specific
SetSorted(bool b)320           void   SetSorted(bool b) {Sorted = b;}
321 
322  protected:
323   // Members
324   PSZ* const &Strp;              // Pointer to PSZ buffer
325   bool        Sorted;            // Values are (semi?) sorted
326   }; // end of class STRBLK
327 
328 /***********************************************************************/
329 /*  Class DATBLK: represents a block of time stamp values.             */
330 /***********************************************************************/
331 class DATBLK : public TYPBLK<int> {
332  public:
333   // Constructor
334   DATBLK(void *mp, int size);
335 
336   // Implementation
337   virtual bool  SetFormat(PGLOBAL g, PCSZ fmt, int len, int year = 0);
338   virtual char *GetCharString(char *p, int n);
339 
340   // Methods
341   using TYPBLK<int>::SetValue;
342   virtual void  SetValue(PCSZ sp, int n);
343 
344  protected:
345   // Members
346   PVAL Dvalp;                    // Date value used to convert string
347   }; // end of class DATBLK
348 
349 /***********************************************************************/
350 /*  Class PTRBLK: represent a block of char pointers.                  */
351 /*  Currently this class is used only by the ARRAY class to make and   */
352 /*  sort a list of char pointers.                                      */
353 /***********************************************************************/
354 class PTRBLK : public STRBLK {
355   friend class ARRAY;
356   friend PVBLK AllocValBlock(PGLOBAL, void *, int, int, int, int,
357                                               bool, bool, bool);
358  protected:
359   // Constructors
PTRBLK(PGLOBAL g,void * mp,int size)360   PTRBLK(PGLOBAL g, void *mp, int size) : STRBLK(g, mp, size, TYPE_PCHAR) {}
361 
362   // Implementation
363 
364   // Methods
365   using STRBLK::SetValue;
366   using STRBLK::CompVal;
SetValue(PCSZ p,int n)367   virtual void   SetValue(PCSZ p, int n) {Strp[n] = (char*)p;}
368   virtual int    CompVal(int i1, int i2);
369 
370  protected:
371   // Members
372   }; // end of class PTRBLK
373 
374 #endif // __VALBLK__H__
375 
376