NdbRecAttr.hpp 10.4 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16 17 18

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef NdbRecAttr_H
#define NdbRecAttr_H

unknown's avatar
ndb  
unknown committed
19 20
#include "NdbDictionary.hpp"
#include "Ndb.hpp"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

class NdbOperation;

/**
 * @class NdbRecAttr
 * @brief Contains value of an attribute.
 *
 * NdbRecAttr objects are used to store the attribute value 
 * after retrieving the value from the NDB Cluster using the method 
 * NdbOperation::getValue.  The objects are allocated by the NDB API.
 * An example application program follows:
 *
 * @code
 *   MyRecAttr = MyOperation->getValue("ATTR2", NULL);
 *   if (MyRecAttr == NULL) goto error;
 *
37
 *   if (MyTransaction->execute(Commit) == -1) goto error;
38 39 40 41
 *
 *   ndbout << MyRecAttr->u_32_value();
 * @endcode
 * For more examples, see 
unknown's avatar
unknown committed
42
 * @ref ndbapi_simple.cpp.
43 44
 *
 * @note The NdbRecAttr object is instantiated with its value when 
45
 *       NdbTransaction::execute is called.  Before this, the value is 
46 47 48
 *       undefined.  (NdbRecAttr::isNULL can be used to check 
 *       if the value is defined or not.)
 *       This means that an NdbRecAttr object only has valid information
49
 *       between the time of calling NdbTransaction::execute and
50 51
 *       the time of Ndb::closeTransaction.
 *       The value of the null indicator is -1 until the
52
 *       NdbTransaction::execute method have been called.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 *
 * For simple types, there are methods which directly getting the value
 * from the NdbRecAttr object.
 *
 * To get a reference to the value, there are two methods:
 * NdbRecAttr::aRef (memory is released by NDB API) and 
 * NdbRecAttr::getAttributeObject (memory must be released 
 * by application program).
 * The two methods may return different pointers.
 *
 * There are also methods to check attribute type, attribute size and
 * array size.  
 * The method NdbRecAttr::arraySize returns the number of elements in the
 * array (where each element is of size given by NdbRecAttr::attrSize). 
 * The NdbRecAttr::arraySize method is needed when reading variable-sized
 * attributes.
 *
 * @note Variable-sized attributes are not yet supported.
 */
class NdbRecAttr
{
74
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
75
  friend class NdbOperation;
unknown's avatar
unknown committed
76
  friend class NdbIndexScanOperation;
77
  friend class NdbEventOperationImpl;
unknown's avatar
unknown committed
78
  friend class NdbReceiver;
79
  friend class Ndb;
80
  friend class NdbOut& operator<<(class NdbOut&, const class AttributeS&);
81
#endif
82

83 84 85 86 87 88
public:
  /** 
   * @name Getting meta information
   * @{
   */
  const NdbDictionary::Column * getColumn() const;
89

90
  /**
91 92
   * Get type of column
   * @return Data type of the column
93 94 95 96 97 98 99
   */
  NdbDictionary::Column::Type getType() const;
  
  /**
   * Get attribute (element) size in bytes. 
   * 
   */
unknown's avatar
unknown committed
100
  Uint32 get_size_in_bytes() const { return m_size_in_bytes; }
101 102 103 104 105 106 107 108 109 110 111

  /** @} *********************************************************************/
  /** 
   * @name Getting stored value
   * @{
   */

  /** 
   * Check if attribute value is NULL.
   *
   * @return -1 = Not defined (Failure or 
112
   *              NdbTransaction::execute not yet called).<br>
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
   *          0 = Attribute value is defined, but not equal to NULL.<br>
   *          1 = Attribute value is defined and equal to NULL.
   */
  int isNULL() const; 

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  64 bit long value.
   */
  Int64 int64_value() const;  

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  32 bit int value.
   */   
  Int32 int32_value() const;  

unknown's avatar
unknown committed
132 133 134 135 136 137 138
  /**
   * Get value stored in NdbRecAttr object.
   * 
   * @return  Medium value.
   */
  Int32 medium_value() const;

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
  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  Short value.
   */
  short short_value() const;

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  Char value.
   */           
  char  char_value() const;           

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  64 bit unsigned value.
   */
  Uint64 u_64_value() const;          

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  32 bit unsigned value.
   */
  Uint32 u_32_value() const;          

unknown's avatar
unknown committed
167 168 169 170 171 172 173
  /**
   * Get value stored in NdbRecAttr object.
   * 
   * @return  Unsigned medium value.
   */
  Uint32 u_medium_value() const;

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
  /**
   * Get value stored in NdbRecAttr object.
   * 
   * @return  Unsigned short value.
   */
  Uint16 u_short_value() const;

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  Unsigned char value.
   */   
  Uint8 u_char_value() const;        

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  Float value.
   */
  float float_value() const;         

  /**
   * Get value stored in NdbRecAttr object.
   *
   * @return  Double value.
   */
  double double_value() const;        
  
  /** @} *********************************************************************/
  /** 
   * @name Getting reference to stored value
   * @{
   */

  /**
   * Get reference to attribute value. 
   * 
   * Returns a char*-pointer to the value.
   * The pointer is aligned appropriately for the data type.  
   * The memory is released when Ndb::closeTransaction is executed 
   * for the transaction which read the value.
   *
   * @note The memory is released by NDB API.
   * 
   * @note The pointer to the attribute value stored in an NdbRecAttr
   *       object (i.e. the pointer returned by aRef) is constant.  
   *       This means that this method can be called anytime after 
   *       NdbOperation::getValue has been called.
   * 
   * @return Pointer to attribute value.         
   */
  char* aRef() const;                 
                                
  /** @} *********************************************************************/
                             
  /**
   * Make a copy of RecAttr object including all data.
   *
   * @note  Copy needs to be deleted by application program.
   */
  NdbRecAttr * clone() const;
  
  /**
   * Destructor
   *
   * @note  You should only delete RecAttr-copies, 
   *        i.e. objects that has been cloned.
   */
  ~NdbRecAttr();    
243 244

public:
245
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
246
  const NdbRecAttr* next() const;
247
#endif
248 249 250
private:

  Uint32 attrId() const;        /* Get attribute id                     */
unknown's avatar
unknown committed
251
  bool setNULL();               /* Set NULL indicator                   */
unknown's avatar
unknown committed
252 253
  void setUNDEFINED();          //

unknown's avatar
unknown committed
254
  bool receive_data(const Uint32*, Uint32);
255 256 257 258

  void release();               /* Release memory if allocated          */
  void init();                  /* Initialise object when allocated     */

unknown's avatar
ndb  
unknown committed
259
  NdbRecAttr(Ndb*);
260
  void next(NdbRecAttr* aRecAttr);
261
  NdbRecAttr* next();
262

263
  int setup(const class NdbDictionary::Column* col, char* aValue);
264 265 266 267 268 269 270 271 272 273 274 275
  int setup(const class NdbColumnImpl* anAttrInfo, char* aValue);
                                /* Set up attributes and buffers        */
  bool copyoutRequired() const; /* Need to copy data to application     */
  void copyout();               /* Copy from storage to application     */

  Uint64        theStorage[4];  /* The data storage here if <= 32 bytes */
  Uint64*       theStorageX;    /* The data storage here if >  32 bytes */
  char*         theValue;       /* The data storage in the application  */
  void*         theRef;         /* Pointer to one of above              */

  NdbRecAttr*   theNext;        /* Next pointer                         */
  Uint32        theAttrId;      /* The attribute id                     */
unknown's avatar
unknown committed
276 277
  
  Int32 m_size_in_bytes;
278
  const NdbDictionary::Column* m_column;
unknown's avatar
ndb  
unknown committed
279 280

  friend struct Ndb_free_list_t<NdbRecAttr>;
281 282
};

unknown's avatar
unknown committed
283 284
#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
inline
NdbDictionary::Column::Type
NdbRecAttr::getType() const {
  return m_column->getType();
}

inline
const NdbDictionary::Column *
NdbRecAttr::getColumn() const {
  return m_column;
}

inline
Int32
NdbRecAttr::int32_value() const 
{
  return *(Int32*)theRef;
}

unknown's avatar
unknown committed
304 305 306 307
inline
Int32
NdbRecAttr::medium_value() const
{
unknown's avatar
unknown committed
308
  return sint3korr((unsigned char *)theRef);
unknown's avatar
unknown committed
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
inline
short
NdbRecAttr::short_value() const
{
  return *(short*)theRef;
}

inline
char
NdbRecAttr::char_value() const
{
  return *(char*)theRef;
}

inline
Uint32
NdbRecAttr::u_32_value() const
{
  return *(Uint32*)theRef;
}

unknown's avatar
unknown committed
332 333 334 335
inline
Uint32
NdbRecAttr::u_medium_value() const
{
unknown's avatar
unknown committed
336
  return uint3korr((unsigned char*)theRef);
unknown's avatar
unknown committed
337 338
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
inline
Uint16
NdbRecAttr::u_short_value() const
{
  return *(Uint16*)theRef;
}

inline
Uint8
NdbRecAttr::u_char_value() const
{
  return *(Uint8*)theRef;
}

inline
void
NdbRecAttr::release()
{
357
  if (theStorageX != 0) {
358
    delete [] theStorageX;
359
    theStorageX = 0;
360 361 362 363 364 365 366
  }
}

inline
void
NdbRecAttr::init()
{
367 368 369 370
  theStorageX = 0;
  theValue = 0;
  theRef = 0;
  theNext = 0;
371 372 373 374 375 376 377 378 379 380 381 382
  theAttrId = 0xFFFF;
}

inline
void
NdbRecAttr::next(NdbRecAttr* aRecAttr)
{
  theNext = aRecAttr;
}

inline
NdbRecAttr*
383 384 385 386 387 388 389
NdbRecAttr::next()
{
  return theNext;
}

inline
const NdbRecAttr*
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
NdbRecAttr::next() const
{
  return theNext;
}

inline
char*
NdbRecAttr::aRef() const
{
  return (char*)theRef; 
}

inline
bool
NdbRecAttr::copyoutRequired() const
{
406
  return theRef != theValue && theValue != 0;
407 408 409 410 411 412 413 414 415 416
}

inline
Uint32
NdbRecAttr::attrId() const
{
  return theAttrId;
}

inline
unknown's avatar
unknown committed
417
bool
418 419
NdbRecAttr::setNULL()
{
unknown's avatar
unknown committed
420 421
  m_size_in_bytes= 0;
  return true;
422 423
}

424
inline
unknown's avatar
unknown committed
425 426
int
NdbRecAttr::isNULL() const
427
{
unknown's avatar
unknown committed
428
  return m_size_in_bytes == 0 ? 1 : (m_size_in_bytes > 0 ? 0 : -1);
429 430
}

431
inline
unknown's avatar
unknown committed
432 433
void
NdbRecAttr::setUNDEFINED()
434
{
unknown's avatar
unknown committed
435
  m_size_in_bytes= -1;
436 437
}

438 439
class NdbOut& operator <<(class NdbOut&, const NdbRecAttr &);

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
class NdbRecordPrintFormat
{
public:
  NdbRecordPrintFormat();
  virtual ~NdbRecordPrintFormat();
  const char *lines_terminated_by;
  const char *fields_terminated_by;
  const char *start_array_enclosure;
  const char *end_array_enclosure;
  const char *fields_enclosed_by;
  const char *fields_optionally_enclosed_by;
  const char *hex_prefix;
  const char *null_string;
  int hex_format;
};
NdbOut&
ndbrecattr_print_formatted(NdbOut& out, const NdbRecAttr &r,
                           const NdbRecordPrintFormat &f);

unknown's avatar
unknown committed
459 460
#endif // ifndef DOXYGEN_SHOULD_SKIP_INTERNAL

461 462
#endif