Modeler Extensions Framework
DataHelper.h
Go to the documentation of this file.
1 //============================================================================
2 // IBM Confidential
3 //
4 // OCO Source Materials
5 //
6 // IBM SPSS Products: Modeler Common
7 //
8 // (C) Copyright IBM Corp. 1994, 2011
9 //
10 // The source code for this program is not published or otherwise divested of its trade secrets,
11 // irrespective of what has been deposited with the U.S. Copyright Office.
12 //============================================================================
13 /*
14  Copyright (c) Integral Solutions Ltd 2006-2008
15  Clementine Extensions Framework C++ Helpers (DataHelper.h)
16  Version 12.0.1
17 */
18 
19 #ifndef DATA_HELPER_H
20 #define DATA_HELPER_H
21 
22 #include "clemext.h"
23 
24 #include "BufferHelper.h"
25 
26 #include <string.h>
27 #include <string>
28 #include <vector>
29 
30 namespace ClemextCppHelper {
31 
34 class DataRecord {
35 
36 public:
39  struct DataValue {
40 
41  enum {
46  };
47  unsigned typeCode_;
48  union {
52  } val_;
53  bool isNull_;
54 
58  typeCode_ = CLEMEXT_TYPE_UNKNOWN;
59  isNull_ = true;
60  }
61 
65  void clearValue() {
66  if (typeCode_ == CLEMEXT_TYPE_STRING)
67  delete val_.stringValue_;
68  typeCode_ = CLEMEXT_TYPE_UNKNOWN;
69  isNull_ = true;
70  }
71 
77  void setValue(const CLEMEXTInteger &v, bool isNull) {
78  if (typeCode_ == CLEMEXT_TYPE_STRING)
79  delete val_.stringValue_;
80  typeCode_ = CLEMEXT_TYPE_INTEGER;
81  isNull_ = isNull;
82  val_.integerValue_ = v;
83  }
84 
90  void setValue(const CLEMEXTReal &v, bool isNull) {
91  if (typeCode_ == CLEMEXT_TYPE_STRING)
92  delete val_.stringValue_;
93  typeCode_ = CLEMEXT_TYPE_REAL;
94  isNull_ = isNull;
95  val_.realValue_ = v;
96  }
97 
103  void setValue(const char *v, bool isNull) {
104  isNull_ = isNull;
105  if (typeCode_ != CLEMEXT_TYPE_STRING) {
106  val_.stringValue_ = new BufferHelper;
107  }
108 
109  if (!isNull) {
110  size_t reqd_sz = strlen(v)+1;
111  if (val_.stringValue_->operator size_t() < reqd_sz) {
112  val_.stringValue_->resize(reqd_sz);
113  }
114  strcpy(val_.stringValue_->operator char *(),v);
115  }
116  typeCode_ = CLEMEXT_TYPE_STRING;
117  }
118  };
119 
122  std::vector<DataValue> values_;
123 
128  void clear() {
129  std::vector<DataValue>::iterator it = values_.begin();
130  while(it != values_.end()) {
131  (*it).clearValue();
132  it++;
133  }
134  }
135 
140  size_t size() {
141  return values_.size();
142  }
143 
149  clear();
150  }
151 };
152 
153 
158 class DataHelper {
159 
161  CLEMEXTIterator *iter_;
162 
164  DataRecord record_;
165 
167  bool passThrough_;
168 
170  int retrieved_;
171 
173  void *value_;
174  size_t value_length_;
175 
176 public:
177 
180 
181  CLEMEXTStatus status_;
182  CLEMEXTErrorCode errorCode_;
183  std::string apiFnName_;
184 
185  public:
186  DataHelperException(CLEMEXTStatus status, CLEMEXTErrorCode errorCode, const char *apiFnName) {
187  status_ = status;
188  errorCode_ = errorCode;
189  apiFnName_ = apiFnName_;
190  }
191 
193  status_ = 0;
194  errorCode_ = 0;
195  apiFnName_ = "";
196  }
197 
199  return status_;
200  }
201 
203  return errorCode_;
204  }
205 
206  std::string getApiFnName() {
207  return apiFnName_;
208  }
209 
210  };
211 
216  DataHelper(CLEMEXTIterator *iter, bool passThrough=true);
217 
219  ~DataHelper();
220 
222  void clear();
223 
227  bool nextRecord();
228 
235  bool rewind();
236 
243  template<class T> void getInputValue(size_t index, T &val, bool &isNull) {
244  CLEMEXTErrorCode errorCode;
245 
246  CLEMEXTStatus status = iter_->getRecordValue(iter_->target,index,&value_,&value_length_,&errorCode);
247  if (status != CLEMEXT_OK) {
248  throw DataHelperException(status,errorCode,"getRecordValue");
249  }
250  if (value_ != NULL) {
251  if (value_length_ != sizeof(T)) {
252  throw DataHelperException();
253  }
254  val = *(T *)value_;
255  isNull = false;
256  } else {
257  isNull = true;
258  }
259  }
260 
267  void getInputStringValue(size_t index, const char *&val, bool &isNull);
268 
275  template<class T> void setOutputValue(size_t index, const T &val, bool isNull) {
276  if (record_.values_.size() <= index) {
277  record_.values_.resize(index+1,DataRecord::DataValue());
278  }
279 
280  record_.values_[index].setValue(val,isNull);
281  }
282 
289  void setOutputStringValue(size_t index, const char *val, bool isNull);
290 
304  CLEMEXTStatus getRecordValue(size_t index, void **value, size_t *value_len, CLEMEXTErrorCode *errorCode);
305 
306 
312  int getNumberRecordsRetrieved() { return retrieved_; };
313 };
314 
315 } // namespace CLEMEXTCppHelper
316 
317 #endif
union ClemextCppHelper::DataRecord::DataValue::@1 val_
void setValue(const char *v, bool isNull)
Definition: DataHelper.h:103
double CLEMEXTReal
Definition: clemext.h:113
#define CLEMEXT_OK
Definition: clemext.h:46
long long CLEMEXTInteger
Definition: clemext.h:118
std::vector< DataValue > values_
Definition: DataHelper.h:122
void getInputStringValue(size_t index, const char *&val, bool &isNull)
void setOutputStringValue(size_t index, const char *val, bool isNull)
CLEMEXTStatus getRecordValue(size_t index, void **value, size_t *value_len, CLEMEXTErrorCode *errorCode)
int CLEMEXTErrorCode
Definition: clemext.h:77
void setValue(const CLEMEXTInteger &v, bool isNull)
Definition: DataHelper.h:77
void setOutputValue(size_t index, const T &val, bool isNull)
Definition: DataHelper.h:275
void getInputValue(size_t index, T &val, bool &isNull)
Definition: DataHelper.h:243
void setValue(const CLEMEXTReal &v, bool isNull)
Definition: DataHelper.h:90
DataHelper(CLEMEXTIterator *iter, bool passThrough=true)
DataHelperException(CLEMEXTStatus status, CLEMEXTErrorCode errorCode, const char *apiFnName)
Definition: DataHelper.h:186
unsigned CLEMEXTStatus
Definition: clemext.h:44