NdbRecAttr.cpp 10.5 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
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15 16

   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 */


17
#include <ndb_global.h>
18 19
#include <NdbOut.hpp>
#include <NdbRecAttr.hpp>
unknown's avatar
unknown committed
20
#include <NdbBlob.hpp>
21
#include "NdbDictionaryImpl.hpp"
22
#include <NdbTCP.h>
23

unknown's avatar
ndb  
unknown committed
24
NdbRecAttr::NdbRecAttr(Ndb*)
25
{
26
  theStorageX = 0;
27
  init();
28 29 30 31 32 33 34
}

NdbRecAttr::~NdbRecAttr()
{
  release();
}

35 36 37 38 39
int
NdbRecAttr::setup(const class NdbDictionary::Column* col, char* aValue)
{
  return setup(&(col->m_impl), aValue);
}
unknown's avatar
unknown committed
40

41 42 43 44 45 46 47 48
int
NdbRecAttr::setup(const NdbColumnImpl* anAttrInfo, char* aValue)
{
  Uint32 tAttrSize = anAttrInfo->m_attrSize;
  Uint32 tArraySize = anAttrInfo->m_arraySize;
  Uint32 tAttrByteSize = tAttrSize * tArraySize;
  
  m_column = anAttrInfo;
49

50
  theAttrId = anAttrInfo->m_attrId;
unknown's avatar
unknown committed
51
  m_size_in_bytes = tAttrByteSize;
52
  theValue = aValue;
53 54 55

  if (theStorageX)
    delete[] theStorageX;
unknown's avatar
unknown committed
56
  
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  // check alignment to signal data
  // a future version could check alignment per data type as well
  
  if (aValue != NULL && (UintPtr(aValue)&3) == 0 && (tAttrByteSize&3) == 0) {
    theStorageX = NULL;
    theRef = aValue;
    return 0;
  }
  if (tAttrByteSize <= 32) {
    theStorageX = NULL;
    theStorage[0] = 0;
    theStorage[1] = 0;
    theStorage[2] = 0;
    theStorage[3] = 0;
    theRef = theStorage;
    return 0;
  }
  Uint32 tSize = (tAttrByteSize + 7) >> 3;
  Uint64* tRef = new Uint64[tSize];
  if (tRef != NULL) {
    for (Uint32 i = 0; i < tSize; i++) {
      tRef[i] = 0;
    }
    theStorageX = tRef;
    theRef = tRef;
    return 0;
  }
  return -1;
}

void
NdbRecAttr::copyout()
{
  char* tRef = (char*)theRef;
  char* tValue = theValue;
  if (tRef != tValue && tRef != NULL && tValue != NULL) {
unknown's avatar
unknown committed
93
    Uint32 n = m_size_in_bytes;
94 95 96 97 98 99 100 101
    while (n-- > 0) {
      *tValue++ = *tRef++;
    }
  }
}

NdbRecAttr *
NdbRecAttr::clone() const {
unknown's avatar
ndb  
unknown committed
102
  NdbRecAttr * ret = new NdbRecAttr(0);
103 104

  ret->theAttrId = theAttrId;
unknown's avatar
unknown committed
105
  ret->m_size_in_bytes = m_size_in_bytes;
106 107
  ret->m_column = m_column;
  
unknown's avatar
unknown committed
108
  Uint32 n = m_size_in_bytes;
109 110 111 112 113 114 115 116 117 118 119 120
  if(n <= 32){
    ret->theRef = (char*)&ret->theStorage[0];
    ret->theStorageX = 0;
    ret->theValue = 0;
  } else {
    ret->theStorageX = new Uint64[((n + 7) >> 3)];
    ret->theRef = (char*)ret->theStorageX;    
    ret->theValue = 0;
  }
  memcpy(ret->theRef, theRef, n);
  return ret;
}
unknown's avatar
unknown committed
121 122

bool
unknown's avatar
unknown committed
123 124
NdbRecAttr::receive_data(const Uint32 * data, Uint32 sz)
{
unknown's avatar
unknown committed
125 126
  if(sz)
  {
unknown's avatar
unknown committed
127
    if(!copyoutRequired())
unknown's avatar
unknown committed
128
      memcpy(theRef, data, sz);
unknown's avatar
unknown committed
129
    else
unknown's avatar
unknown committed
130 131
      memcpy(theValue, data, sz);
    m_size_in_bytes= sz;
unknown's avatar
unknown committed
132
    return true;
unknown's avatar
unknown committed
133 134 135 136
  } 
  else 
  {
    return setNULL();
unknown's avatar
unknown committed
137 138 139
  }
  return false;
}
unknown's avatar
merge  
unknown committed
140

141 142
static void
ndbrecattr_print_string(NdbOut& out, const char *type,
143
			const char *aref, unsigned sz)
144
{
145
  const unsigned char* ref = (const unsigned char*)aref;
unknown's avatar
unknown committed
146
  int i, len, printable= 1;
147 148 149 150 151 152
  // trailing zeroes are not printed
  for (i=sz-1; i >= 0; i--)
    if (ref[i] == 0) sz--;
    else break;
  if (sz == 0) return; // empty

unknown's avatar
unknown committed
153 154
  for (len=0; len < (int)sz && ref[i] != 0; len++)
    if (printable && !isprint((int)ref[i]))
155
      printable= 0;
unknown's avatar
unknown committed
156

157
  if (printable)
unknown's avatar
unknown committed
158
    out.print("%.*s", len, ref);
159
  else
unknown's avatar
unknown committed
160 161
  {
    out.print("0x");
162
    for (i=0; i < len; i++)
unknown's avatar
unknown committed
163
      out.print("%02X", (int)ref[i]);
unknown's avatar
unknown committed
164
  }
165 166 167 168 169
  if (len != (int)sz)
  {
    out.print("[");
    for (i= len+1; ref[i] != 0; i++)
    out.print("%u]",len-i);
unknown's avatar
unknown committed
170
    assert((int)sz > i);
171
    ndbrecattr_print_string(out,type,aref+i,sz-i);
172 173 174
  }
}

unknown's avatar
unknown committed
175
NdbOut& operator<<(NdbOut& out, const NdbRecAttr &r)
176 177 178
{
  if (r.isNULL())
  {
unknown's avatar
unknown committed
179 180
    out << "[NULL]";
    return out;
181
  }
unknown's avatar
unknown committed
182
  
183 184
  const NdbDictionary::Column* c = r.getColumn();
  uint length = c->getLength();
185
  if (length > 1)
unknown's avatar
unknown committed
186
    out << "[";
187

188
  for (Uint32 j = 0; j < length; j++) 
189 190
  {
    if (j > 0)
unknown's avatar
unknown committed
191
      out << " ";
192

unknown's avatar
unknown committed
193 194 195 196 197 198 199 200
    switch(r.getType()){
    case NdbDictionary::Column::Bigunsigned:
      out << r.u_64_value();
      break;
    case NdbDictionary::Column::Bit:
      out << hex << "H'" << r.u_32_value() << dec;
      break;
    case NdbDictionary::Column::Unsigned:
201
      out << *((Uint32*)r.aRef() + j);
unknown's avatar
unknown committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
      break;
    case NdbDictionary::Column::Smallunsigned:
      out << r.u_short_value();
      break;
    case NdbDictionary::Column::Tinyunsigned:
      out << (unsigned) r.u_char_value();
      break;
    case NdbDictionary::Column::Bigint:
      out << r.int64_value();
      break;
    case NdbDictionary::Column::Int:
      out << r.int32_value();
      break;
    case NdbDictionary::Column::Smallint:
      out << r.short_value();
      break;
    case NdbDictionary::Column::Tinyint:
      out << (int) r.char_value();
      break;
    case NdbDictionary::Column::Binary:
      j = r.get_size_in_bytes();
      ndbrecattr_print_string(out,"Binary", r.aRef(), j);
      break;
    case NdbDictionary::Column::Char:
      j = r.get_size_in_bytes();
      ndbrecattr_print_string(out,"Char", r.aRef(), j);
      break;
    case NdbDictionary::Column::Varchar:
    {
      unsigned len = *(const unsigned char*)r.aRef();
      ndbrecattr_print_string(out,"Varchar", r.aRef()+1,len);
      j = length;
    }
    break;
    case NdbDictionary::Column::Varbinary:
    {
      unsigned len = *(const unsigned char*)r.aRef();
      ndbrecattr_print_string(out,"Varbinary", r.aRef()+1,len);
      j = length;
    }
    break;
    case NdbDictionary::Column::Float:
      out << r.float_value();
      break;
    case NdbDictionary::Column::Double:
      out << r.double_value();
      break;
    case NdbDictionary::Column::Olddecimal:
    {
      short len = 1 + c->getPrecision() + (c->getScale() > 0);
      out.print("%.*s", len, r.aRef());
    }
    break;
    case NdbDictionary::Column::Olddecimalunsigned:
    {
      short len = 0 + c->getPrecision() + (c->getScale() > 0);
      out.print("%.*s", len, r.aRef());
    }
    break;
    case NdbDictionary::Column::Decimal:
    case NdbDictionary::Column::Decimalunsigned:
      goto unknown;   // TODO
      break;
265
      // for dates cut-and-paste from field.cc
unknown's avatar
unknown committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    case NdbDictionary::Column::Datetime:
    {
      ulonglong tmp=r.u_64_value();
      long part1,part2,part3;
      part1=(long) (tmp/LL(1000000));
      part2=(long) (tmp - (ulonglong) part1*LL(1000000));
      char buf[40];
      char* pos=(char*) buf+19;
      *pos--=0;
      *pos--= (char) ('0'+(char) (part2%10)); part2/=10; 
      *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
      *pos--= ':';
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos--= ':';
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos--= (char) ('0'+(char) part3);
      *pos--= '/';
      *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
      *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
      *pos--= '-';
      *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
      *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
      *pos--= '-';
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
      *pos=(char) ('0'+(char) part3);
      out << buf;
    }
    break;
    case NdbDictionary::Column::Date:
    {
      uint32 tmp=(uint32) uint3korr(r.aRef());
      int part;
      char buf[40];
      char *pos=(char*) buf+10;
      *pos--=0;
      part=(int) (tmp & 31);
      *pos--= (char) ('0'+part%10);
      *pos--= (char) ('0'+part/10);
      *pos--= '-';
      part=(int) (tmp >> 5 & 15);
      *pos--= (char) ('0'+part%10);
      *pos--= (char) ('0'+part/10);
      *pos--= '-';
      part=(int) (tmp >> 9);
      *pos--= (char) ('0'+part%10); part/=10;
      *pos--= (char) ('0'+part%10); part/=10;
      *pos--= (char) ('0'+part%10); part/=10;
      *pos=   (char) ('0'+part);
      out << buf;
    }
    break;
    case NdbDictionary::Column::Time:
    {
      long tmp=(long) sint3korr(r.aRef());
      int hour=(uint) (tmp/10000);
      int minute=(uint) (tmp/100 % 100);
      int second=(uint) (tmp % 100);
      char buf[40];
      sprintf(buf, "%02d:%02d:%02d", hour, minute, second);
      out << buf;
    }
    break;
    case NdbDictionary::Column::Year:
    {
      uint year = 1900 + r.u_char_value();
      char buf[40];
      sprintf(buf, "%04d", year);
      out << buf;
    }
    break;
    case NdbDictionary::Column::Timestamp:
    {
      time_t time = r.u_32_value();
      out << (uint)time;
    }
    break;
    case NdbDictionary::Column::Blob:
    case NdbDictionary::Column::Text:
    {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
      // user defined aRef() may not be aligned to Uint64
      NdbBlob::Head head;
      memcpy(&head, r.aRef(), sizeof(head));
      out << head.length << ":";
      const unsigned char* p = (const unsigned char*)r.aRef() + sizeof(head);
      if (r.get_size_in_bytes() < sizeof(head))
        out << "***error***"; // really cannot happen
      else {
        unsigned n = r.get_size_in_bytes() - sizeof(head);
        for (unsigned k = 0; k < n && k < head.length; k++) {
          if (r.getType() == NdbDictionary::Column::Blob)
            out.print("%02X", (int)p[k]);
          else
            out.print("%c", (int)p[k]);
        }
      }
unknown's avatar
unknown committed
364 365 366 367 368 369 370 371 372 373
      j = length;
    }
    break;
    case NdbDictionary::Column::Longvarchar:
    {
      unsigned len = uint2korr(r.aRef());
      ndbrecattr_print_string(out,"Longvarchar", r.aRef()+2,len);
      j = length;
    }
    break;
unknown's avatar
unknown committed
374 375 376 377 378 379

    case NdbDictionary::Column::Undefined:
    case NdbDictionary::Column::Mediumint:
    case NdbDictionary::Column::Mediumunsigned:
    case NdbDictionary::Column::Longvarbinary:
    unknown:
unknown's avatar
unknown committed
380 381 382 383 384 385 386
    //default: /* no print functions for the rest, just print type */
    out << (int) r.getType();
    j = length;
    if (j > 1)
      out << " " << j << " times";
    break;
    }
387 388
  }

389
  if (length > 1)
390
  {
unknown's avatar
unknown committed
391
    out << "]";
392 393
  }

unknown's avatar
unknown committed
394
  return out;
395
}
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

Int64
NdbRecAttr::int64_value() const 
{
  Int64 val;
  memcpy(&val,theRef,8);
  return val;
}

Uint64
NdbRecAttr::u_64_value() const
{
  Uint64 val;
  memcpy(&val,theRef,8);
  return val;
}

float
NdbRecAttr::float_value() const
{
  float val;
  memcpy(&val,theRef,sizeof(val));
  return val;
}

double
NdbRecAttr::double_value() const
{
  double val;
  memcpy(&val,theRef,sizeof(val));
  return val;
}