C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

mysql_feature_manager.h
1 /* file: mysql_feature_manager.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 /*
17 //++
18 // Implementation of the MYSQL data source class.
19 //--
20 */
21 #ifndef __MYSQL_FEATURE_MANAGER_H__
22 #define __MYSQL_FEATURE_MANAGER_H__
23 
24 #include <sstream>
25 #include "services/daal_memory.h"
26 #include "data_management/data_source/data_source.h"
27 #include "data_management/data/data_dictionary.h"
28 #include "data_management/data/numeric_table.h"
29 #include "data_management/data/homogen_numeric_table.h"
30 
31 #ifdef _WIN32
32 #include <windows.h>
33 #endif
34 
35 #include <sql.h>
36 #include <sqltypes.h>
37 #include <sqlext.h>
38 
39 namespace daal
40 {
41 namespace data_management
42 {
43 
44 namespace interface1
45 {
54 class MySQLFeatureManager
55 {
56 public:
57  MySQLFeatureManager() : _errors(new services::ErrorCollection()) {}
58 
66  DataSourceIface::DataSourceStatus statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows);
67 
74  void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
75  {
76  SQLSMALLINT nFeatures = 0;
77  SQLRETURN ret = SQLNumResultCols(hdlStmt, &nFeatures);
78 
79  dict->setNumberOfFeatures( nFeatures );
80 
81  SQLLEN sqlType;
82  SQLLEN sqlIsUnsigned;
83  SQLLEN sqlTypeLength;
84  for (int i = 0 ; i < nFeatures; i++)
85  {
86  SQLSMALLINT bufferLenUsed;
87  int bufferSize = 64;
88  char label[64];
89  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_UNSIGNED, NULL, 0, NULL, &sqlIsUnsigned);
90  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
91 
92  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_TYPE, NULL, 0, NULL, &sqlType);
93  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
94 
95  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &sqlTypeLength);
96  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
97 
98  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_NAME , (SQLPOINTER)label, (SQLSMALLINT)bufferSize, &bufferLenUsed, NULL);
99  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
100 
101  sqlTypeLength *= 8;
102 
103  DataSourceFeature &feature = (*dict)[i];
104 
105  feature.setFeatureName(label);
106 
107  if (isToDouble(sqlType))
108  {
109  feature.setType<double>();
110  }
111  else if (isToFloat(sqlType))
112  {
113  feature.setType<float>();
114  }
115  else if (isToInt(sqlType))
116  {
117  if (sqlTypeLength <= 8)
118  {
119  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned char>() : feature.setType<char>();
120  }
121  else if (sqlTypeLength <= 16)
122  {
123  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned short>() : feature.setType<short>();
124  }
125  else if (sqlTypeLength <= 32)
126  {
127  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned int>() : feature.setType<int>();
128  }
129  else if (sqlTypeLength <= 64)
130  {
131  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
132  }
133  else if (sqlType == SQL_BIGINT)
134  {
135  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
136  }
137  }
138  }
139  }
140 
150  std::string setLimitQuery(std::string &query, size_t idx_last_read, size_t maxRows)
151  {
152  std::stringstream ss;
153  ss << query << " LIMIT " << idx_last_read << ", " << maxRows << ";";
154  return ss.str();
155  }
156 
157  services::SharedPtr<services::ErrorCollection> getErrors()
158  {
159  return _errors;
160  }
161 
162 private:
163  services::SharedPtr<services::ErrorCollection> _errors;
164 
165  size_t getStrictureSize(NumericTableDictionary *dict);
166  size_t typeSize(data_feature_utils::IndexNumType indexNumType);
167  SQLSMALLINT getTargetType(data_feature_utils::IndexNumType indexNumType);
168 
169  bool isToDouble(int identifier)
170  {
171  const int arraySize = 4;
172  int SQLTypesToDouble[arraySize] = {SQL_NUMERIC, SQL_DECIMAL, SQL_DOUBLE, SQL_FLOAT};
173  return isContain(identifier, SQLTypesToDouble, arraySize);
174  }
175  bool isToFloat(int identifier)
176  {
177  const int arraySize = 1;
178  int SQLTypesToFloat[arraySize] = {SQL_REAL};
179  return isContain(identifier, SQLTypesToFloat, arraySize);
180  }
181  bool isToInt(int identifier)
182  {
183  const int arraySize = 6;
184  int SQLTypesToInt[arraySize] = {SQL_INTEGER, SQL_SMALLINT, SQL_TINYINT, SQL_BIGINT, SQL_BIT, SQL_BINARY};
185  return isContain(identifier, SQLTypesToInt, arraySize);
186  }
187  bool isContain(int identifier, int array[], int arraySize)
188  {
189  for (int i = 0; i < arraySize; i++)
190  {
191  if (array[i] == identifier)
192  {
193  return true;
194  }
195  }
196  return false;
197  }
198 };
199 
200 DataSourceIface::DataSourceStatus MySQLFeatureManager::statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows)
201 {
202  SQLRETURN ret;
203  size_t nFeatures = nt->getNumberOfColumns();
204  nt->resize(maxRows);
205  NumericTableDictionaryPtr dict = nt->getDictionarySharedPtr();
206  data_feature_utils::IndexNumType indexNumType = data_feature_utils::getIndexNumType<DAAL_DATA_TYPE>();
207 
208  SQLLEN *bindInd = (SQLLEN *)daal::services::daal_malloc(sizeof(SQLLEN) * nFeatures);
209  DAAL_DATA_TYPE *fetchBuffer = (DAAL_DATA_TYPE *)daal::services::daal_malloc(sizeof(DAAL_DATA_TYPE) * nFeatures);
210  for (int j = 0; j < nFeatures; j++)
211  {
212  if (indexNumType != data_feature_utils::DAAL_OTHER_T)
213  {
214  ret = SQLBindCol(hdlStmt, j + 1, getTargetType(indexNumType), (SQLPOINTER)&fetchBuffer[j], 0, &bindInd[j]);
215  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return DataSource::notReady; }
216  }
217  else
218  {
219  bindInd[j] = 0;
220  }
221  }
222  size_t read = 0;
223 
224  BlockDescriptor<DAAL_DATA_TYPE> block;
225  nt->getBlockOfRows(0, maxRows, writeOnly, block);
226  DAAL_DATA_TYPE *ntBuffer = block.getBlockPtr();
227 
228  while (SQL_SUCCEEDED(ret = SQLFetchScroll(hdlStmt, SQL_FETCH_NEXT, 1)))
229  {
230  for (int j = 0; j < nFeatures; j++)
231  {
232  if (bindInd[j] == SQL_NULL_DATA)
233  {
234  ntBuffer[read * nFeatures + j] = 0.0;
235  continue;
236  }
237  indexNumType = (*dict)[j].indexType;
238  if (indexNumType != data_feature_utils::DAAL_OTHER_T)
239  {
240  ntBuffer[read * nFeatures + j] = *((DAAL_DATA_TYPE *) & (fetchBuffer[j]));
241  }
242  else
243  {
244  ntBuffer[read * nFeatures + j] = 0.0;
245  }
246  }
247  read++;
248  }
249  nt->resize(read);
250  nt->releaseBlockOfRows(block);
251 
252  DataSourceIface::DataSourceStatus status = DataSourceIface::readyForLoad;
253  if (ret != SQL_NO_DATA)
254  {
255  if (!SQL_SUCCEEDED(ret))
256  {
257  status = DataSourceIface::notReady;
258  _errors->add(services::ErrorODBC);
259  }
260  }
261  else
262  {
263  if (read < maxRows)
264  {
265  status = DataSourceIface::endOfData;
266  }
267  }
268  daal::services::daal_free(fetchBuffer);
269  daal::services::daal_free(bindInd);
270  return status;
271 }
272 
273 size_t MySQLFeatureManager::getStrictureSize(NumericTableDictionary *dict)
274 {
275  size_t structureSize = 0;
276  size_t nFeatures = dict->getNumberOfFeatures();
277  for (int i = 0; i < nFeatures; i++)
278  {
279  data_feature_utils::IndexNumType indexNumType = (*dict)[i].indexType;
280  structureSize += typeSize(indexNumType);
281  }
282  return structureSize;
283 }
284 
285 size_t MySQLFeatureManager::typeSize(data_feature_utils::IndexNumType indexNumType)
286 {
287  if (indexNumType == data_feature_utils::DAAL_FLOAT32) { return 4; }
288  else if (indexNumType == data_feature_utils::DAAL_FLOAT64) { return 8; }
289  else if (indexNumType == data_feature_utils::DAAL_INT32_S) { return 4; }
290  else if (indexNumType == data_feature_utils::DAAL_INT32_U) { return 4; }
291  else if (indexNumType == data_feature_utils::DAAL_INT64_S) { return 8; }
292  else if (indexNumType == data_feature_utils::DAAL_INT64_U) { return 8; }
293  else if (indexNumType == data_feature_utils::DAAL_INT8_S) { return 1; }
294  else if (indexNumType == data_feature_utils::DAAL_INT8_U) { return 1; }
295  else if (indexNumType == data_feature_utils::DAAL_INT16_S) { return 2; }
296  else if (indexNumType == data_feature_utils::DAAL_INT16_U) { return 2; }
297  else /*indexNumType == data_feature_utils::DAAL_OTHER_T)*/ { return 4; }
298 }
299 
300 SQLSMALLINT MySQLFeatureManager::getTargetType(data_feature_utils::IndexNumType indexNumType)
301 {
302  if (indexNumType == data_feature_utils::DAAL_FLOAT32) { return SQL_C_FLOAT; }
303  else if (indexNumType == data_feature_utils::DAAL_FLOAT64) { return SQL_C_DOUBLE; }
304  else if (indexNumType == data_feature_utils::DAAL_INT32_S) { return SQL_C_SLONG; }
305  else if (indexNumType == data_feature_utils::DAAL_INT32_U) { return SQL_C_ULONG; }
306  else if (indexNumType == data_feature_utils::DAAL_INT64_S) { return SQL_C_SBIGINT; }
307  else if (indexNumType == data_feature_utils::DAAL_INT64_U) { return SQL_C_UBIGINT; }
308  else if (indexNumType == data_feature_utils::DAAL_INT8_S) { return SQL_C_STINYINT; }
309  else if (indexNumType == data_feature_utils::DAAL_INT8_U) { return SQL_C_UTINYINT; }
310  else if (indexNumType == data_feature_utils::DAAL_INT16_S) { return SQL_C_SSHORT; }
311  else if (indexNumType == data_feature_utils::DAAL_INT16_U) { return SQL_C_USHORT; }
312  else /*indexNumType == data_feature_utils::DAAL_OTHER_T)*/ { return SQL_C_SLONG; }
313 }
315 } // namespace interface1
316 using interface1::MySQLFeatureManager;
317 
318 }
319 }
320 #endif
daal::data_management::interface1::BlockDescriptor::getBlockPtr
DataType * getBlockPtr() const
Definition: numeric_table.h:69
daal
Definition: algorithm_base_common.h:31
daal::data_management::interface1::MySQLFeatureManager::statementResultsNumericTable
DataSourceIface::DataSourceStatus statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows)
Definition: mysql_feature_manager.h:200
daal::data_management::interface1::DataSourceFeature::setType
void setType()
Definition: data_source_dictionary.h:128
daal::data_management::interface1::DataSourceFeature
Data structure that describes the Data Source feature.
Definition: data_source_dictionary.h:49
daal::data_management::interface1::NumericTable::getDictionarySharedPtr
virtual NumericTableDictionaryPtr getDictionarySharedPtr() const DAAL_C11_OVERRIDE
Definition: numeric_table.h:632
daal::data_management::interface1::NumericTable::getNumberOfColumns
size_t getNumberOfColumns() const
Definition: numeric_table.h:651
daal::data_management::interface1::MySQLFeatureManager::createDictionary
void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
Definition: mysql_feature_manager.h:74
daal::data_management::interface1::DataSourceFeature::setFeatureName
void setFeatureName(const std::string &featureName)
Definition: data_source_dictionary.h:112
daal::data_management::interface1::Dictionary::getNumberOfFeatures
size_t getNumberOfFeatures() const
Definition: data_dictionary.h:282
daal::data_management::interface1::MySQLFeatureManager
Contains MySQL-specific commands.
Definition: mysql_feature_manager.h:54
daal::services::daal_malloc
DAAL_EXPORT void * daal_malloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::data_management::interface1::NumericTable
Class for a data management component responsible for representation of data in the numeric format...
Definition: numeric_table.h:574
daal::data_management::interface1::NumericTable::resize
virtual services::Status resize(size_t nrows) DAAL_C11_OVERRIDE
Definition: numeric_table.h:636
daal::data_management::interface1::Dictionary::setNumberOfFeatures
virtual services::Status setNumberOfFeatures(size_t numberOfFeatures)
Definition: data_dictionary.h:263
daal::data_management::interface1::MySQLFeatureManager::setLimitQuery
std::string setLimitQuery(std::string &query, size_t idx_last_read, size_t maxRows)
Definition: mysql_feature_manager.h:150
daal::data_management::interface1::DataSourceIface::DataSourceStatus
DataSourceStatus
Specifies the status of the Data Source.
Definition: data_source.h:57
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::data_management::interface1::DataSourceIface::readyForLoad
Definition: data_source.h:59
daal::data_management::interface1::BlockDescriptor< DAAL_DATA_TYPE >
daal::data_management::interface1::DataSourceIface::endOfData
Definition: data_source.h:61
daal::data_management::interface1::DenseNumericTableIface::releaseBlockOfRows
virtual services::Status releaseBlockOfRows(BlockDescriptor< double > &block)=0
daal::services::ErrorODBC
Definition: error_indexes.h:376
daal::data_management::interface1::DataSourceIface::notReady
Definition: data_source.h:62
daal::data_management::interface1::DenseNumericTableIface::getBlockOfRows
virtual services::Status getBlockOfRows(size_t vector_idx, size_t vector_num, ReadWriteMode rwflag, BlockDescriptor< double > &block)=0
daal::data_management::interface1::Dictionary
Class that represents a dictionary of a data set and provides methods to work with the data dictionar...
Definition: data_dictionary.h:158

For more complete information about compiler optimizations, see our Optimization Notice.