ResultMetadata.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // MySQLException.cpp
  3. //
  4. // $Id: //poco/1.4/Data/MySQL/src/ResultMetadata.cpp#1 $
  5. //
  6. // Library: Data
  7. // Package: MySQL
  8. // Module: ResultMetadata
  9. //
  10. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/Data/MySQL/ResultMetadata.h"
  36. #include "Poco/Data/MySQL/MySQLException.h"
  37. #include <cstring>
  38. namespace
  39. {
  40. class ResultMetadataHandle
  41. /// Simple exception-safe wrapper
  42. {
  43. public:
  44. explicit ResultMetadataHandle(MYSQL_STMT* stmt)
  45. {
  46. h = mysql_stmt_result_metadata(stmt);
  47. }
  48. ~ResultMetadataHandle()
  49. {
  50. if (h)
  51. {
  52. mysql_free_result(h);
  53. }
  54. }
  55. operator MYSQL_RES* ()
  56. {
  57. return h;
  58. }
  59. private:
  60. MYSQL_RES* h;
  61. };
  62. std::size_t fieldSize(const MYSQL_FIELD& field)
  63. /// Convert field MySQL-type and field MySQL-length to actual field length
  64. {
  65. switch (field.type)
  66. {
  67. case MYSQL_TYPE_TINY: return sizeof(char);
  68. case MYSQL_TYPE_SHORT: return sizeof(short);
  69. case MYSQL_TYPE_INT24:
  70. case MYSQL_TYPE_LONG: return sizeof(Poco::Int32);
  71. case MYSQL_TYPE_FLOAT: return sizeof(float);
  72. case MYSQL_TYPE_DOUBLE: return sizeof(double);
  73. case MYSQL_TYPE_LONGLONG: return sizeof(Poco::Int64);
  74. case MYSQL_TYPE_DATE:
  75. case MYSQL_TYPE_TIME:
  76. case MYSQL_TYPE_DATETIME:
  77. return sizeof(MYSQL_TIME);
  78. case MYSQL_TYPE_DECIMAL:
  79. case MYSQL_TYPE_NEWDECIMAL:
  80. case MYSQL_TYPE_STRING:
  81. case MYSQL_TYPE_VAR_STRING:
  82. case MYSQL_TYPE_TINY_BLOB:
  83. case MYSQL_TYPE_MEDIUM_BLOB:
  84. case MYSQL_TYPE_LONG_BLOB:
  85. case MYSQL_TYPE_BLOB:
  86. return field.length;
  87. default:
  88. throw Poco::Data::MySQL::StatementException("unknown field type");
  89. }
  90. }
  91. Poco::Data::MetaColumn::ColumnDataType fieldType(const MYSQL_FIELD& field)
  92. /// Convert field MySQL-type to Poco-type
  93. {
  94. bool unsig = ((field.flags & UNSIGNED_FLAG) == UNSIGNED_FLAG);
  95. switch (field.type)
  96. {
  97. case MYSQL_TYPE_TINY:
  98. if (unsig) return Poco::Data::MetaColumn::FDT_UINT8;
  99. return Poco::Data::MetaColumn::FDT_INT8;
  100. case MYSQL_TYPE_SHORT:
  101. if (unsig) return Poco::Data::MetaColumn::FDT_UINT16;
  102. return Poco::Data::MetaColumn::FDT_INT16;
  103. case MYSQL_TYPE_INT24:
  104. case MYSQL_TYPE_LONG:
  105. if (unsig) return Poco::Data::MetaColumn::FDT_UINT32;
  106. return Poco::Data::MetaColumn::FDT_INT32;
  107. case MYSQL_TYPE_FLOAT:
  108. return Poco::Data::MetaColumn::FDT_FLOAT;
  109. case MYSQL_TYPE_DOUBLE:
  110. return Poco::Data::MetaColumn::FDT_DOUBLE;
  111. case MYSQL_TYPE_LONGLONG:
  112. if (unsig) return Poco::Data::MetaColumn::FDT_UINT64;
  113. return Poco::Data::MetaColumn::FDT_INT64;
  114. case MYSQL_TYPE_STRING:
  115. case MYSQL_TYPE_VAR_STRING:
  116. return Poco::Data::MetaColumn::FDT_STRING;
  117. case MYSQL_TYPE_TINY_BLOB:
  118. case MYSQL_TYPE_MEDIUM_BLOB:
  119. case MYSQL_TYPE_LONG_BLOB:
  120. case MYSQL_TYPE_BLOB:
  121. return Poco::Data::MetaColumn::FDT_BLOB;
  122. default:
  123. return Poco::Data::MetaColumn::FDT_UNKNOWN;
  124. }
  125. }
  126. } // namespace
  127. namespace Poco {
  128. namespace Data {
  129. namespace MySQL {
  130. void ResultMetadata::reset()
  131. {
  132. _columns.resize(0);
  133. _row.resize(0);
  134. _buffer.resize(0);
  135. _lengths.resize(0);
  136. _isNull.resize(0);
  137. }
  138. void ResultMetadata::init(MYSQL_STMT* stmt)
  139. {
  140. ResultMetadataHandle h(stmt);
  141. if (!h)
  142. {
  143. // all right, it is normal
  144. // querys such an "INSERT INTO" just does not have result at all
  145. reset();
  146. return;
  147. }
  148. std::size_t count = mysql_num_fields(h);
  149. MYSQL_FIELD* fields = mysql_fetch_fields(h);
  150. std::size_t commonSize = 0;
  151. _columns.reserve(count);
  152. {for (std::size_t i = 0; i < count; i++)
  153. {
  154. _columns.push_back(MetaColumn(
  155. i, // position
  156. fields[i].name, // name
  157. fieldType(fields[i]), // type
  158. fieldSize(fields[i]), // length
  159. 0, // TODO: precision
  160. !IS_NOT_NULL(fields[i].flags) // nullable
  161. ));
  162. commonSize += _columns[i].length();
  163. }}
  164. _buffer.resize(commonSize);
  165. _row.resize(count);
  166. _lengths.resize(count);
  167. _isNull.resize(count);
  168. std::size_t offset = 0;
  169. {for (std::size_t i = 0; i < count; i++)
  170. {
  171. std::memset(&_row[i], 0, sizeof(MYSQL_BIND));
  172. _row[i].buffer_type = fields[i].type;
  173. _row[i].buffer_length = static_cast<unsigned int>(_columns[i].length());
  174. _row[i].buffer = &_buffer[0] + offset;
  175. _row[i].length = &_lengths[i];
  176. _row[i].is_null = &_isNull[i];
  177. offset += _row[i].buffer_length;
  178. }}
  179. }
  180. std::size_t ResultMetadata::columnsReturned() const
  181. {
  182. return static_cast<std::size_t>(_columns.size());
  183. }
  184. const MetaColumn& ResultMetadata::metaColumn(std::size_t pos) const
  185. {
  186. return _columns[pos];
  187. }
  188. MYSQL_BIND* ResultMetadata::row()
  189. {
  190. return &_row[0];
  191. }
  192. std::size_t ResultMetadata::length(std::size_t pos) const
  193. {
  194. return _lengths[pos];
  195. }
  196. const unsigned char* ResultMetadata::rawData(std::size_t pos) const
  197. {
  198. return reinterpret_cast<const unsigned char*>(_row[pos].buffer);
  199. }
  200. bool ResultMetadata::isNull(std::size_t pos) const
  201. {
  202. return (_isNull[pos] != 0);
  203. }
  204. }}} // namespace Poco::Data::MySQL