CDBResultSet.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "CDBResultSet.h"
  2. #include <string.h>
  3. namespace YADB
  4. {
  5. CDBResultSet::CDBResultSet()
  6. {
  7. __pQueryRes = 0;
  8. __CurRow = 0;
  9. __FieldCount = 0;
  10. }
  11. CDBResultSet::~CDBResultSet()
  12. {
  13. Close();
  14. }
  15. void CDBResultSet::__BuildFNameIdxLst()
  16. {
  17. if (0 == __pQueryRes)
  18. {
  19. return;
  20. }
  21. for ( int i = 0; i < __FieldCount; i++ )
  22. {
  23. MYSQL_FIELD *pField = mysql_fetch_field_direct( __pQueryRes, i );
  24. if ( pField == NULL )
  25. {
  26. continue;
  27. }
  28. __FNameIndexList.insert( std::make_pair( pField->name, i ) );
  29. }
  30. }
  31. bool CDBResultSet::Bind( MYSQL_RES *pQueryRes, std::string& Error )
  32. {
  33. Close();
  34. __pQueryRes = pQueryRes;
  35. if ( __pQueryRes != 0 )
  36. {
  37. __FieldCount = mysql_num_fields( pQueryRes );
  38. }
  39. else
  40. {
  41. Error = "Error,pQueryRes is null!";
  42. __FieldCount = 0;
  43. return false;
  44. }
  45. __BuildFNameIdxLst();
  46. return true;
  47. }
  48. void CDBResultSet::Close()
  49. {
  50. if ( 0 == __pQueryRes )
  51. {
  52. return;
  53. }
  54. mysql_free_result( __pQueryRes );
  55. __pQueryRes = 0;
  56. __CurRow = 0;
  57. __FieldCount = 0;
  58. __FNameIndexList.clear();
  59. }
  60. bool CDBResultSet::SeekData( my_ulonglong Offset, std::string& Error )
  61. {
  62. if ( 0 == __pQueryRes )
  63. {
  64. Error = "Error,__pQueryRes is not initialized!";
  65. return false;
  66. }
  67. mysql_data_seek( __pQueryRes, Offset );
  68. return true;
  69. }
  70. bool CDBResultSet::GetNextRecod( std::string & Error )
  71. {
  72. if ( 0 == __pQueryRes )
  73. {
  74. Error = "Error,__pQueryRes is not initialized!";
  75. return false;
  76. }
  77. if ( ( __CurRow = mysql_fetch_row( __pQueryRes ) ) != NULL )
  78. {
  79. return true;
  80. }
  81. else
  82. {
  83. Error = "Error, failed to mysql_fetch_row!";
  84. return false;
  85. }
  86. return true;
  87. }
  88. my_ulonglong CDBResultSet::GetRecordCount( std::string & Error )
  89. {
  90. if ( 0 == __pQueryRes )
  91. {
  92. Error = "Error,__pQueryRes is not initialized!";
  93. return DB_ERR_QUERY_RES_NOT_INITED;
  94. }
  95. return mysql_num_rows( __pQueryRes );
  96. }
  97. bool CDBResultSet::__GetField( int fdIndex, char*& szValue, bool& IsNull, std::string& Error )
  98. {
  99. IsNull = false;
  100. if ( 0 == __CurRow )
  101. {
  102. Error = "Error,__CurRow is not initialized!";
  103. return false;
  104. }
  105. if ( fdIndex < 0 || fdIndex >= __FieldCount )
  106. {
  107. Error = "fdIndex is invalid!";
  108. return false;
  109. }
  110. szValue = __CurRow[fdIndex];
  111. if ( szValue == NULL || szValue[0] == 0)
  112. {
  113. IsNull = true;
  114. }
  115. return true;
  116. }
  117. bool CDBResultSet::GetField( int fdIndex, int & value, std::string & Error )
  118. {
  119. value = 0;
  120. char* pField = 0;
  121. bool IsNull = false;
  122. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  123. {
  124. return false;
  125. }
  126. if ( !IsNull )
  127. {
  128. value = atoi( pField );
  129. }
  130. return true;
  131. }
  132. bool CDBResultSet::GetField( int fdIndex, unsigned int & value, std::string & Error )
  133. {
  134. value = 0;
  135. char* pField = 0;
  136. bool IsNull = false;
  137. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  138. {
  139. return false;
  140. }
  141. if ( !IsNull )
  142. {
  143. value = atoi( pField );
  144. }
  145. return true;
  146. }
  147. bool CDBResultSet::GetField( int fdIndex, long long & value, std::string & Error )
  148. {
  149. value = 0;
  150. char* pField = 0;
  151. bool IsNull = false;
  152. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  153. {
  154. return false;
  155. }
  156. if ( !IsNull )
  157. {
  158. value = atoll(pField);
  159. }
  160. return true;
  161. }
  162. bool CDBResultSet::GetField( int fdIndex, std::string & value, std::string & Error )
  163. {
  164. value = "";
  165. char* pField = 0;
  166. bool IsNull = false;
  167. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  168. {
  169. return false;
  170. }
  171. if ( !IsNull )
  172. {
  173. value = pField;
  174. }
  175. return true;
  176. }
  177. bool CDBResultSet::GetField( int fdIndex, char * value, int Length, std::string & Error )
  178. {
  179. memset( value, 0, Length );
  180. char* pField = 0;
  181. bool IsNull = false;
  182. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  183. {
  184. return false;
  185. }
  186. if ( !IsNull )
  187. {
  188. strncpy( value, pField, Length );
  189. }
  190. return true;
  191. }
  192. bool CDBResultSet::GetField( int fdIndex, bool & value, std::string & Error )
  193. {
  194. value = false;
  195. char* pField = 0;
  196. bool IsNull = false;
  197. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  198. {
  199. return false;
  200. }
  201. if ( !IsNull )
  202. {
  203. value = atoi( pField );
  204. }
  205. return true;
  206. }
  207. bool CDBResultSet::GetField( int fdIndex, float & value, std::string & Error )
  208. {
  209. value = 0.0;
  210. char* pField = 0;
  211. bool IsNull = false;
  212. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  213. {
  214. return false;
  215. }
  216. if ( !IsNull )
  217. {
  218. value = atof( pField );
  219. }
  220. return true;
  221. }
  222. bool CDBResultSet::GetField( int fdIndex, double & value, std::string & Error )
  223. {
  224. value = 0.0;
  225. char* pField = 0;
  226. bool IsNull = false;
  227. if (!__GetField( fdIndex, pField, IsNull, Error ) )
  228. {
  229. return false;
  230. }
  231. if ( !IsNull )
  232. {
  233. value = std::stod( pField );
  234. }
  235. return true;
  236. }
  237. bool CDBResultSet::GetField( int fdIndex, MYSQL_TIME & value, std::string & Error )
  238. {
  239. __DBHelper.InitMySQLTime( value );
  240. char* pField = 0;
  241. bool IsNull = false;
  242. if ( !__GetField( fdIndex, pField, IsNull, Error ) )
  243. {
  244. return false;
  245. }
  246. if ( !IsNull )
  247. {
  248. __DBHelper.String2DateTime( pField, value );
  249. }
  250. return true;
  251. }
  252. int CDBResultSet::GetFdIndexByName( const char * fdName, std::string & Error )
  253. {
  254. std::map<std::string,int>::iterator mit_fi;
  255. mit_fi = __FNameIndexList.find( fdName );
  256. if ( mit_fi != __FNameIndexList.end() )
  257. {
  258. return mit_fi->second;
  259. }
  260. Error = "Failed to find field: ";
  261. Error += fdName;
  262. return -1;
  263. }
  264. bool CDBResultSet::GetField( const char * fdName, int & value, std::string & Error )
  265. {
  266. int fdIndex = GetFdIndexByName( fdName, Error );
  267. if ( fdIndex >= 0 )
  268. {
  269. return GetField( fdIndex, value, Error );
  270. }
  271. return false;
  272. }
  273. bool CDBResultSet::GetField( const char * fdName, long long & value, std::string & Error )
  274. {
  275. int fdIndex = GetFdIndexByName( fdName, Error );
  276. if ( fdIndex >= 0 )
  277. {
  278. return GetField( fdIndex, value, Error );
  279. }
  280. return false;
  281. }
  282. bool CDBResultSet::GetField( const char * fdName, unsigned int & value, std::string & Error )
  283. {
  284. int fdIndex = GetFdIndexByName( fdName, Error );
  285. if ( fdIndex >= 0 )
  286. {
  287. return GetField( fdIndex, value, Error );
  288. }
  289. return false;
  290. }
  291. bool CDBResultSet::GetField( const char * fdName, std::string & value, std::string & Error )
  292. {
  293. int fdIndex = GetFdIndexByName( fdName, Error );
  294. if ( fdIndex >= 0 )
  295. {
  296. return GetField( fdIndex, value, Error );
  297. }
  298. return false;
  299. }
  300. bool CDBResultSet::GetField( const char * fdName, char * value, int Length, std::string & Error )
  301. {
  302. int fdIndex = GetFdIndexByName( fdName, Error );
  303. if ( fdIndex >= 0 )
  304. {
  305. return GetField( fdIndex, value, Length, Error );
  306. }
  307. return false;
  308. }
  309. bool CDBResultSet::GetField( const char * fdName, bool & value, std::string & Error )
  310. {
  311. int fdIndex = GetFdIndexByName( fdName, Error );
  312. if ( fdIndex >= 0 )
  313. {
  314. return GetField( fdIndex, value, Error );
  315. }
  316. return false;
  317. }
  318. bool CDBResultSet::GetField( const char * fdName, float & value, std::string & Error )
  319. {
  320. int fdIndex = GetFdIndexByName( fdName, Error );
  321. if ( fdIndex >= 0 )
  322. {
  323. return GetField( fdIndex, value, Error );
  324. }
  325. return false;
  326. }
  327. bool CDBResultSet::GetField( const char * fdName, double & value, std::string & Error )
  328. {
  329. int fdIndex = GetFdIndexByName( fdName, Error );
  330. if ( fdIndex >= 0 )
  331. {
  332. return GetField( fdIndex, value, Error );
  333. }
  334. return false;
  335. }
  336. bool CDBResultSet::GetField( const char * fdName, MYSQL_TIME & value, std::string & Error )
  337. {
  338. int fdIndex = GetFdIndexByName( fdName, Error );
  339. if ( fdIndex >= 0 )
  340. {
  341. return GetField( fdIndex, value, Error );
  342. }
  343. return false;
  344. }
  345. int CDBResultSet::IsNull( int fdIndex, std::string& Error )
  346. {
  347. if (0 == __CurRow)
  348. {
  349. Error = "Error,__CurRow is not initialized!";
  350. return -1;
  351. }
  352. if ( fdIndex < 0 || fdIndex >= __FieldCount )
  353. {
  354. Error = "fdIndex is invalid!";
  355. return -1;
  356. }
  357. char* pField = __CurRow[fdIndex];
  358. if ( pField == NULL || pField[0] == 0 )
  359. {
  360. return 1;
  361. }
  362. return 0;
  363. }
  364. }