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

mysql_feature_manager.h
1 /* file: mysql_feature_manager.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Implementation of the MYSQL data source class.
45 //--
46 */
47 #ifndef __MYSQL_FEATURE_MANAGER_H__
48 #define __MYSQL_FEATURE_MANAGER_H__
49 
50 #include <sstream>
51 #include "services/daal_memory.h"
52 #include "data_management/data_source/data_source.h"
53 #include "data_management/data/data_dictionary.h"
54 #include "data_management/data/numeric_table.h"
55 #include "data_management/data/homogen_numeric_table.h"
56 
57 #ifdef _WIN32
58 #include <windows.h>
59 #endif
60 
61 #include <sql.h>
62 #include <sqltypes.h>
63 #include <sqlext.h>
64 
65 namespace daal
66 {
67 namespace data_management
68 {
69 
70 namespace interface1
71 {
80 class MySQLFeatureManager
81 {
82 public:
83  MySQLFeatureManager() : _errors(new services::ErrorCollection()) {}
84 
92  DataSourceIface::DataSourceStatus statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows);
93 
100  void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
101  {
102  SQLSMALLINT nFeatures = 0;
103  SQLRETURN ret = SQLNumResultCols(hdlStmt, &nFeatures);
104 
105  dict->setNumberOfFeatures( nFeatures );
106 
107  SQLLEN sqlType;
108  SQLLEN sqlIsUnsigned;
109  SQLLEN sqlTypeLength;
110  for (int i = 0 ; i < nFeatures; i++)
111  {
112  SQLSMALLINT bufferLenUsed;
113  int bufferSize = 64;
114  char label[64];
115  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_UNSIGNED, NULL, 0, NULL, &sqlIsUnsigned);
116  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
117 
118  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_TYPE, NULL, 0, NULL, &sqlType);
119  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
120 
121  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &sqlTypeLength);
122  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
123 
124  ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_NAME , (SQLPOINTER)label, (SQLSMALLINT)bufferSize, &bufferLenUsed, NULL);
125  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return; }
126 
127  sqlTypeLength *= 8;
128 
129  DataSourceFeature &feature = (*dict)[i];
130 
131  feature.setFeatureName(label);
132 
133  if (isToDouble(sqlType))
134  {
135  feature.setType<double>();
136  }
137  else if (isToFloat(sqlType))
138  {
139  feature.setType<float>();
140  }
141  else if (isToInt(sqlType))
142  {
143  if (sqlTypeLength <= 8)
144  {
145  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned char>() : feature.setType<char>();
146  }
147  else if (sqlTypeLength <= 16)
148  {
149  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned short>() : feature.setType<short>();
150  }
151  else if (sqlTypeLength <= 32)
152  {
153  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<unsigned int>() : feature.setType<int>();
154  }
155  else if (sqlTypeLength <= 64)
156  {
157  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
158  }
159  else if (sqlType == SQL_BIGINT)
160  {
161  (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
162  }
163  }
164  }
165  }
166 
176  std::string setLimitQuery(std::string &query, size_t idx_last_read, size_t maxRows)
177  {
178  std::stringstream ss;
179  ss << query << " LIMIT " << idx_last_read << ", " << maxRows << ";";
180  return ss.str();
181  }
182 
183  services::SharedPtr<services::ErrorCollection> getErrors()
184  {
185  return _errors;
186  }
187 
188 private:
189  services::SharedPtr<services::ErrorCollection> _errors;
190 
191  size_t getStrictureSize(NumericTableDictionary *dict);
192  size_t typeSize(data_feature_utils::IndexNumType indexNumType);
193  SQLSMALLINT getTargetType(data_feature_utils::IndexNumType indexNumType);
194 
195  bool isToDouble(int identifier)
196  {
197  const int arraySize = 4;
198  int SQLTypesToDouble[arraySize] = {SQL_NUMERIC, SQL_DECIMAL, SQL_DOUBLE, SQL_FLOAT};
199  return isContain(identifier, SQLTypesToDouble, arraySize);
200  }
201  bool isToFloat(int identifier)
202  {
203  const int arraySize = 1;
204  int SQLTypesToFloat[arraySize] = {SQL_REAL};
205  return isContain(identifier, SQLTypesToFloat, arraySize);
206  }
207  bool isToInt(int identifier)
208  {
209  const int arraySize = 6;
210  int SQLTypesToInt[arraySize] = {SQL_INTEGER, SQL_SMALLINT, SQL_TINYINT, SQL_BIGINT, SQL_BIT, SQL_BINARY};
211  return isContain(identifier, SQLTypesToInt, arraySize);
212  }
213  bool isContain(int identifier, int array[], int arraySize)
214  {
215  for (int i = 0; i < arraySize; i++)
216  {
217  if (array[i] == identifier)
218  {
219  return true;
220  }
221  }
222  return false;
223  }
224 };
225 
226 DataSourceIface::DataSourceStatus MySQLFeatureManager::statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows)
227 {
228  SQLRETURN ret;
229  size_t nFeatures = nt->getNumberOfColumns();
230  nt->resize(maxRows);
231  NumericTableDictionaryPtr dict = nt->getDictionarySharedPtr();
232  data_feature_utils::IndexNumType indexNumType = data_feature_utils::getIndexNumType<DAAL_DATA_TYPE>();
233 
234  SQLLEN *bindInd = (SQLLEN *)daal::services::daal_malloc(sizeof(SQLLEN) * nFeatures);
235  DAAL_DATA_TYPE *fetchBuffer = (DAAL_DATA_TYPE *)daal::services::daal_malloc(sizeof(DAAL_DATA_TYPE) * nFeatures);
236  for (int j = 0; j < nFeatures; j++)
237  {
238  if (indexNumType != data_feature_utils::DAAL_OTHER_T)
239  {
240  ret = SQLBindCol(hdlStmt, j + 1, getTargetType(indexNumType), (SQLPOINTER)&fetchBuffer[j], 0, &bindInd[j]);
241  if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC); return DataSource::notReady; }
242  }
243  else
244  {
245  bindInd[j] = 0;
246  }
247  }
248  size_t read = 0;
249 
250  BlockDescriptor<DAAL_DATA_TYPE> block;
251  nt->getBlockOfRows(0, maxRows, writeOnly, block);
252  DAAL_DATA_TYPE *ntBuffer = block.getBlockPtr();
253 
254  while (SQL_SUCCEEDED(ret = SQLFetchScroll(hdlStmt, SQL_FETCH_NEXT, 1)))
255  {
256  for (int j = 0; j < nFeatures; j++)
257  {
258  if (bindInd[j] == SQL_NULL_DATA)
259  {
260  ntBuffer[read * nFeatures + j] = 0.0;
261  continue;
262  }
263  indexNumType = (*dict)[j].indexType;
264  if (indexNumType != data_feature_utils::DAAL_OTHER_T)
265  {
266  ntBuffer[read * nFeatures + j] = *((DAAL_DATA_TYPE *) & (fetchBuffer[j]));
267  }
268  else
269  {
270  ntBuffer[read * nFeatures + j] = 0.0;
271  }
272  }
273  read++;
274  }
275  nt->resize(read);
276  nt->releaseBlockOfRows(block);
277 
278  DataSourceIface::DataSourceStatus status = DataSourceIface::readyForLoad;
279  if (ret != SQL_NO_DATA)
280  {
281  if (!SQL_SUCCEEDED(ret))
282  {
283  status = DataSourceIface::notReady;
284  _errors->add(services::ErrorODBC);
285  }
286  }
287  else
288  {
289  if (read < maxRows)
290  {
291  status = DataSourceIface::endOfData;
292  }
293  }
294  daal::services::daal_free(fetchBuffer);
295  daal::services::daal_free(bindInd);
296  return status;
297 }
298 
299 size_t MySQLFeatureManager::getStrictureSize(NumericTableDictionary *dict)
300 {
301  size_t structureSize = 0;
302  size_t nFeatures = dict->getNumberOfFeatures();
303  for (int i = 0; i < nFeatures; i++)
304  {
305  data_feature_utils::IndexNumType indexNumType = (*dict)[i].indexType;
306  structureSize += typeSize(indexNumType);
307  }
308  return structureSize;
309 }
310 
311 size_t MySQLFeatureManager::typeSize(data_feature_utils::IndexNumType indexNumType)
312 {
313  if (indexNumType == data_feature_utils::DAAL_FLOAT32) { return 4; }
314  else if (indexNumType == data_feature_utils::DAAL_FLOAT64) { return 8; }
315  else if (indexNumType == data_feature_utils::DAAL_INT32_S) { return 4; }
316  else if (indexNumType == data_feature_utils::DAAL_INT32_U) { return 4; }
317  else if (indexNumType == data_feature_utils::DAAL_INT64_S) { return 8; }
318  else if (indexNumType == data_feature_utils::DAAL_INT64_U) { return 8; }
319  else if (indexNumType == data_feature_utils::DAAL_INT8_S) { return 1; }
320  else if (indexNumType == data_feature_utils::DAAL_INT8_U) { return 1; }
321  else if (indexNumType == data_feature_utils::DAAL_INT16_S) { return 2; }
322  else if (indexNumType == data_feature_utils::DAAL_INT16_U) { return 2; }
323  else /*indexNumType == data_feature_utils::DAAL_OTHER_T)*/ { return 4; }
324 }
325 
326 SQLSMALLINT MySQLFeatureManager::getTargetType(data_feature_utils::IndexNumType indexNumType)
327 {
328  if (indexNumType == data_feature_utils::DAAL_FLOAT32) { return SQL_C_FLOAT; }
329  else if (indexNumType == data_feature_utils::DAAL_FLOAT64) { return SQL_C_DOUBLE; }
330  else if (indexNumType == data_feature_utils::DAAL_INT32_S) { return SQL_C_SLONG; }
331  else if (indexNumType == data_feature_utils::DAAL_INT32_U) { return SQL_C_ULONG; }
332  else if (indexNumType == data_feature_utils::DAAL_INT64_S) { return SQL_C_SBIGINT; }
333  else if (indexNumType == data_feature_utils::DAAL_INT64_U) { return SQL_C_UBIGINT; }
334  else if (indexNumType == data_feature_utils::DAAL_INT8_S) { return SQL_C_STINYINT; }
335  else if (indexNumType == data_feature_utils::DAAL_INT8_U) { return SQL_C_UTINYINT; }
336  else if (indexNumType == data_feature_utils::DAAL_INT16_S) { return SQL_C_SSHORT; }
337  else if (indexNumType == data_feature_utils::DAAL_INT16_U) { return SQL_C_USHORT; }
338  else /*indexNumType == data_feature_utils::DAAL_OTHER_T)*/ { return SQL_C_SLONG; }
339 }
341 } // namespace interface1
342 using interface1::MySQLFeatureManager;
343 
344 }
345 }
346 #endif
daal::data_management::interface1::Dictionary::getNumberOfFeatures
size_t getNumberOfFeatures() const
Definition: data_dictionary.h:308
daal::data_management::interface1::DataSourceIface::readyForLoad
Definition: data_source.h:85
daal::data_management::interface1::DataSourceIface::endOfData
Definition: data_source.h:87
daal
Definition: algorithm_base_common.h:57
daal::data_management::interface1::MySQLFeatureManager::statementResultsNumericTable
DataSourceIface::DataSourceStatus statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt, size_t maxRows)
Definition: mysql_feature_manager.h:226
daal::data_management::interface1::DataSourceFeature::setType
void setType()
Definition: data_source_dictionary.h:154
daal::data_management::interface1::DataSourceFeature
Data structure that describes the Data Source feature.
Definition: data_source_dictionary.h:75
daal::data_management::interface1::NumericTable::getDictionarySharedPtr
virtual NumericTableDictionaryPtr getDictionarySharedPtr() const DAAL_C11_OVERRIDE
Definition: numeric_table.h:658
daal::data_management::interface1::DataSourceIface::DataSourceStatus
DataSourceStatus
Specifies the status of the Data Source.
Definition: data_source.h:83
daal::data_management::interface1::BlockDescriptor::getBlockPtr
DataType * getBlockPtr() const
Definition: numeric_table.h:95
daal::data_management::interface1::MySQLFeatureManager::createDictionary
void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
Definition: mysql_feature_manager.h:100
daal::data_management::interface1::DataSourceFeature::setFeatureName
void setFeatureName(const std::string &featureName)
Definition: data_source_dictionary.h:138
daal::services::interface1::ErrorCollection
Class that represents an error collection.
Definition: error_handling.h:338
daal::data_management::interface1::MySQLFeatureManager
Contains MySQL-specific commands.
Definition: mysql_feature_manager.h:80
daal::services::interface1::SharedPtr
Shared pointer that retains shared ownership of an object through a pointer. Several SharedPtr object...
Definition: daal_shared_ptr.h:187
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:600
daal::data_management::interface1::NumericTable::resize
virtual services::Status resize(size_t nrows) DAAL_C11_OVERRIDE
Definition: numeric_table.h:662
daal::data_management::interface1::Dictionary::setNumberOfFeatures
virtual services::Status setNumberOfFeatures(size_t numberOfFeatures)
Definition: data_dictionary.h:289
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:176
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::data_management::interface1::BlockDescriptor< DAAL_DATA_TYPE >
daal::data_management::interface1::DenseNumericTableIface::releaseBlockOfRows
virtual services::Status releaseBlockOfRows(BlockDescriptor< double > &block)=0
daal::data_management::interface1::NumericTable::getNumberOfColumns
size_t getNumberOfColumns() const
Definition: numeric_table.h:677
daal::services::ErrorODBC
Definition: error_indexes.h:400
daal::data_management::interface1::DataSourceIface::notReady
Definition: data_source.h:88
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:184

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