47 #ifndef __MYSQL_FEATURE_MANAGER_H__
48 #define __MYSQL_FEATURE_MANAGER_H__
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"
69 namespace data_management
82 class MySQLFeatureManager
85 MySQLFeatureManager() : _errors(
new services::ErrorCollection()) {}
94 DataSourceIface::DataSourceStatus statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt,
size_t maxRows);
102 void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
104 SQLSMALLINT nFeatures = 0;
105 SQLRETURN ret = SQLNumResultCols(hdlStmt, &nFeatures);
107 dict->setNumberOfFeatures( nFeatures );
110 SQLLEN sqlIsUnsigned;
111 SQLLEN sqlTypeLength;
112 for (
int i = 0 ; i < nFeatures; i++)
114 SQLSMALLINT bufferLenUsed;
117 ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_UNSIGNED, NULL, 0, NULL, &sqlIsUnsigned);
118 if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC);
return; }
120 ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_TYPE, NULL, 0, NULL, &sqlType);
121 if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC);
return; }
123 ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &sqlTypeLength);
124 if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC);
return; }
126 ret = SQLColAttributes(hdlStmt, (SQLUSMALLINT)i + 1, SQL_DESC_NAME , (SQLPOINTER)label, (SQLSMALLINT)bufferSize, &bufferLenUsed, NULL);
127 if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC);
return; }
131 DataSourceFeature &feature = (*dict)[i];
133 feature.setFeatureName(label);
135 if (isToDouble(sqlType))
137 feature.setType<
double>();
139 else if (isToFloat(sqlType))
141 feature.setType<
float>();
143 else if (isToInt(sqlType))
145 if (sqlTypeLength <= 8)
147 (sqlIsUnsigned == SQL_TRUE) ? feature.setType<
unsigned char>() : feature.setType<
char>();
149 else if (sqlTypeLength <= 16)
151 (sqlIsUnsigned == SQL_TRUE) ? feature.setType<
unsigned short>() : feature.setType<
short>();
153 else if (sqlTypeLength <= 32)
155 (sqlIsUnsigned == SQL_TRUE) ? feature.setType<
unsigned int>() : feature.setType<
int>();
157 else if (sqlTypeLength <= 64)
159 (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
161 else if (sqlType == SQL_BIGINT)
163 (sqlIsUnsigned == SQL_TRUE) ? feature.setType<DAAL_UINT64>() : feature.setType<DAAL_INT64>();
178 std::string setLimitQuery(std::string &query,
size_t idx_last_read,
size_t maxRows)
180 std::stringstream ss;
181 ss << query <<
" LIMIT " << idx_last_read <<
", " << maxRows <<
";";
185 services::SharedPtr<services::ErrorCollection> getErrors()
191 services::SharedPtr<services::ErrorCollection> _errors;
193 size_t getStrictureSize(NumericTableDictionary *dict);
194 size_t typeSize(data_feature_utils::IndexNumType indexNumType);
195 SQLSMALLINT getTargetType(data_feature_utils::IndexNumType indexNumType);
197 bool isToDouble(
int identifier)
199 const int arraySize = 4;
200 int SQLTypesToDouble[arraySize] = {SQL_NUMERIC, SQL_DECIMAL, SQL_DOUBLE, SQL_FLOAT};
201 return isContain(identifier, SQLTypesToDouble, arraySize);
203 bool isToFloat(
int identifier)
205 const int arraySize = 1;
206 int SQLTypesToFloat[arraySize] = {SQL_REAL};
207 return isContain(identifier, SQLTypesToFloat, arraySize);
209 bool isToInt(
int identifier)
211 const int arraySize = 6;
212 int SQLTypesToInt[arraySize] = {SQL_INTEGER, SQL_SMALLINT, SQL_TINYINT, SQL_BIGINT, SQL_BIT, SQL_BINARY};
213 return isContain(identifier, SQLTypesToInt, arraySize);
215 bool isContain(
int identifier,
int array[],
int arraySize)
217 for (
int i = 0; i < arraySize; i++)
219 if (array[i] == identifier)
228 DataSourceIface::DataSourceStatus MySQLFeatureManager::statementResultsNumericTable(SQLHSTMT hdlStmt, NumericTable *nt,
size_t maxRows)
231 size_t nFeatures = nt->getNumberOfColumns();
233 NumericTableDictionaryPtr dict = nt->getDictionarySharedPtr();
234 data_feature_utils::IndexNumType indexNumType = data_feature_utils::getIndexNumType<DAAL_DATA_TYPE>();
236 SQLLEN *bindInd = (SQLLEN *)daal::services::daal_malloc(
sizeof(SQLLEN) * nFeatures);
237 DAAL_DATA_TYPE *fetchBuffer = (DAAL_DATA_TYPE *)daal::services::daal_malloc(
sizeof(DAAL_DATA_TYPE) * nFeatures);
238 for (
int j = 0; j < nFeatures; j++)
240 if (indexNumType != data_feature_utils::DAAL_OTHER_T)
242 ret = SQLBindCol(hdlStmt, j + 1, getTargetType(indexNumType), (SQLPOINTER)&fetchBuffer[j], 0, &bindInd[j]);
243 if (!SQL_SUCCEEDED(ret)) { _errors->add(services::ErrorODBC);
return DataSource::notReady; }
252 BlockDescriptor<DAAL_DATA_TYPE> block;
253 nt->getBlockOfRows(0, maxRows, writeOnly, block);
254 DAAL_DATA_TYPE *ntBuffer = block.getBlockPtr();
256 while (SQL_SUCCEEDED(ret = SQLFetchScroll(hdlStmt, SQL_FETCH_NEXT, 1)))
258 for (
int j = 0; j < nFeatures; j++)
260 if (bindInd[j] == SQL_NULL_DATA)
262 ntBuffer[read * nFeatures + j] = 0.0;
265 indexNumType = (*dict)[j].indexType;
266 if (indexNumType != data_feature_utils::DAAL_OTHER_T)
268 ntBuffer[read * nFeatures + j] = *((DAAL_DATA_TYPE *) & (fetchBuffer[j]));
272 ntBuffer[read * nFeatures + j] = 0.0;
278 nt->releaseBlockOfRows(block);
280 DataSourceIface::DataSourceStatus status = DataSourceIface::readyForLoad;
281 if (ret != SQL_NO_DATA)
283 if (!SQL_SUCCEEDED(ret))
285 status = DataSourceIface::notReady;
286 _errors->add(services::ErrorODBC);
293 status = DataSourceIface::endOfData;
296 daal::services::daal_free(fetchBuffer);
297 daal::services::daal_free(bindInd);
301 size_t MySQLFeatureManager::getStrictureSize(NumericTableDictionary *dict)
303 size_t structureSize = 0;
304 size_t nFeatures = dict->getNumberOfFeatures();
305 for (
int i = 0; i < nFeatures; i++)
307 data_feature_utils::IndexNumType indexNumType = (*dict)[i].indexType;
308 structureSize += typeSize(indexNumType);
310 return structureSize;
313 size_t MySQLFeatureManager::typeSize(data_feature_utils::IndexNumType indexNumType)
315 if (indexNumType == data_feature_utils::DAAL_FLOAT32) {
return 4; }
316 else if (indexNumType == data_feature_utils::DAAL_FLOAT64) {
return 8; }
317 else if (indexNumType == data_feature_utils::DAAL_INT32_S) {
return 4; }
318 else if (indexNumType == data_feature_utils::DAAL_INT32_U) {
return 4; }
319 else if (indexNumType == data_feature_utils::DAAL_INT64_S) {
return 8; }
320 else if (indexNumType == data_feature_utils::DAAL_INT64_U) {
return 8; }
321 else if (indexNumType == data_feature_utils::DAAL_INT8_S) {
return 1; }
322 else if (indexNumType == data_feature_utils::DAAL_INT8_U) {
return 1; }
323 else if (indexNumType == data_feature_utils::DAAL_INT16_S) {
return 2; }
324 else if (indexNumType == data_feature_utils::DAAL_INT16_U) {
return 2; }
328 SQLSMALLINT MySQLFeatureManager::getTargetType(data_feature_utils::IndexNumType indexNumType)
330 if (indexNumType == data_feature_utils::DAAL_FLOAT32) {
return SQL_C_FLOAT; }
331 else if (indexNumType == data_feature_utils::DAAL_FLOAT64) {
return SQL_C_DOUBLE; }
332 else if (indexNumType == data_feature_utils::DAAL_INT32_S) {
return SQL_C_SLONG; }
333 else if (indexNumType == data_feature_utils::DAAL_INT32_U) {
return SQL_C_ULONG; }
334 else if (indexNumType == data_feature_utils::DAAL_INT64_S) {
return SQL_C_SBIGINT; }
335 else if (indexNumType == data_feature_utils::DAAL_INT64_U) {
return SQL_C_UBIGINT; }
336 else if (indexNumType == data_feature_utils::DAAL_INT8_S) {
return SQL_C_STINYINT; }
337 else if (indexNumType == data_feature_utils::DAAL_INT8_U) {
return SQL_C_UTINYINT; }
338 else if (indexNumType == data_feature_utils::DAAL_INT16_S) {
return SQL_C_SSHORT; }
339 else if (indexNumType == data_feature_utils::DAAL_INT16_U) {
return SQL_C_USHORT; }
340 else {
return SQL_C_SLONG; }
344 using interface1::MySQLFeatureManager;
daal::data_management::interface1::BlockDescriptor::getBlockPtr
DataType * getBlockPtr() const
Definition: numeric_table.h:95
daal
Definition: algorithm_base_common.h:57
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::NumericTable::getNumberOfColumns
size_t getNumberOfColumns() const
Definition: numeric_table.h:677
daal::data_management::interface1::MySQLFeatureManager::createDictionary
void createDictionary(SQLHSTMT hdlStmt, DataSourceDictionary *dict)
Definition: mysql_feature_manager.h:102
daal::data_management::interface1::DataSourceFeature::setFeatureName
void setFeatureName(const std::string &featureName)
Definition: data_source_dictionary.h:138
daal::data_management::interface1::Dictionary::getNumberOfFeatures
size_t getNumberOfFeatures() const
Definition: data_dictionary.h:308
daal::data_management::interface1::MySQLFeatureManager
Contains MySQL-specific commands.
Definition: mysql_feature_manager.h:82
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:178
daal::data_management::interface1::DataSourceIface::DataSourceStatus
DataSourceStatus
Specifies the status of the Data Source.
Definition: data_source.h:83
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::services::ErrorODBC
Definition: error_indexes.h:393
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