MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NDBT_ResultRow.cpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 #include "NDBT_ResultRow.hpp"
21 #include <NdbOut.hpp>
22 #include <NdbSchemaCon.hpp>
23 
24 NDBT_ResultRow::NDBT_ResultRow(const NdbDictionary::Table& tab,
25  char attrib_delimiter)
26  : m_table(tab)
27 {
29 
30  cols = tab.getNoOfColumns();
31  names = new char * [cols];
32  data = new NdbRecAttr * [cols];
33 
34  for(int i = 0; i<cols; i++){
35  names[i] = new char[255];
36  strcpy(names[i], tab.getColumn(i)->getName());
37  }
38 
39  ad[0] = attrib_delimiter;
40  ad[1] = 0;
41  m_ownData = false;
42 }
43 
44 NDBT_ResultRow::~NDBT_ResultRow(){
45  for(int i = 0; i<cols; i++){
46  delete [] names[i];
47  }
48  delete [] names;
49 
50  if(m_ownData){
51  for(int i = 0; i<cols; i++)
52  delete data[i];
53  }
54  delete [] data;
55 }
56 
57 NdbRecAttr* &
58 NDBT_ResultRow::attributeStore(int i){
59 
60  return data[i];
61 }
62 
63 const NdbRecAttr*
64 NDBT_ResultRow::attributeStore(int i) const {
65  return data[i];
66 }
67 
68 const
69 NdbRecAttr *
70 NDBT_ResultRow::attributeStore(const char* name) const {
71  for(int i = 0; i<cols; i++){
72  if (strcmp(names[i], name) == 0)
73  return data[i];
74  }
75  assert(false);
76  return 0;
77 }
78 
79 NdbOut &
80 NDBT_ResultRow::header (NdbOut & out) const {
81  for(int i = 0; i<cols; i++){
82  out << names[i];
83  if (i < cols-1)
84  out << ad;
85  }
86  return out;
87 }
88 
89 BaseString NDBT_ResultRow::c_str() const {
90 
91  BaseString str;
92 
93  char buf[10];
94  for(int i = 0; i<cols; i++){
95  if(data[i]->isNULL()){
96  sprintf(buf, "NULL");
97  str.append(buf);
98  }else{
99  Uint32* p = (Uint32*)data[i]->aRef();
100  Uint32 sizeInBytes = data[i]->get_size_in_bytes();
101  for (Uint32 j = 0; j < sizeInBytes; j+=(sizeof(Uint32))){
102  str.append("H'");
103  if (j + 4 < sizeInBytes)
104  {
105  sprintf(buf, "%.8x", *p);
106  }
107  else
108  {
109  Uint32 tmp = 0;
110  memcpy(&tmp, p, sizeInBytes - j);
111  sprintf(buf, "%.8x", tmp);
112  }
113  p++;
114  str.append(buf);
115  if ((j + sizeof(Uint32)) < sizeInBytes)
116  str.append(", ");
117  }
118  }
119  str.append("\n");
120  }
121  str.append("*");
122 
123  //ndbout << "NDBT_ResultRow::c_str() = " << str.c_str() << endl;
124 
125  return str;
126 }
127 
128 NdbOut &
129 operator << (NdbOut& ndbout, const NDBT_ResultRow & res)
130 {
131  if (res.cols != 0)
132  {
133  ndbout << *(res.data[0]);
134  for(int i = 1; i<res.cols; i++)
135  ndbout << res.ad << *(res.data[i]);
136  }
137  return ndbout;
138 }
139 
142 
143  NDBT_ResultRow * row = new NDBT_ResultRow(m_table, ad[0]);
144  row->m_ownData = true;
145  Uint32 noOfColumns = m_table.getNoOfColumns();
146  for(Uint32 i = 0; i < noOfColumns; i++){
147  row->data[i] = data[i]->clone();
148  }
149 
150  return row;
151 }
152 
153 bool
154 NDBT_ResultRow::operator==(const NDBT_ResultRow& other) const
155 {
156  // quick and dirty
157  return c_str() == other.c_str();
158 }