S3PTableInfoCatch.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:2k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // S3PTableInfoCatch.cpp: implementation of the S3PTableInfoCatch class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "S3PTableInfoCatch.h"
  5. #include "S3PDBConnection.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. S3PTableInfoCatch * S3PTableInfoCatch::m_pInstance = NULL;
  10. S3PTableInfoCatch::S3PTableInfoCatch()
  11. {
  12. }
  13. S3PTableInfoCatch::~S3PTableInfoCatch()
  14. {
  15. std::map<std::string,std::map<std::string, std::string> *>::iterator i;
  16. for(i=m_initedMap.begin(); i!=m_initedMap.end(); i++)
  17. {
  18. delete i->second;
  19. }
  20. m_initedMap.clear();
  21. }
  22. S3PTableInfoCatch * S3PTableInfoCatch::Instance()
  23. {
  24. if (m_pInstance==NULL)
  25. {
  26. m_pInstance = new S3PTableInfoCatch;
  27. }
  28. return m_pInstance;
  29. }
  30. void S3PTableInfoCatch::Release()
  31. {
  32. if (m_pInstance != NULL)
  33. {
  34. delete m_pInstance;
  35. m_pInstance = NULL;
  36. }
  37. }
  38. std::map<std::string, std::string> * 
  39. S3PTableInfoCatch::GetColumnInfo(std::string tableName, S3PDBConnection * pConn)
  40. {
  41. std::map<std::string,std::map<std::string, std::string> *>::iterator i;
  42. i = m_initedMap.find(tableName);
  43.     if(i != m_initedMap.end() )
  44. {
  45. return i->second;
  46. }
  47. else
  48. {
  49. std::string strQuery;
  50. strQuery = "select * from " + tableName + " where 1<>1";
  51. if ( pConn->QueryBySql(strQuery.c_str()) )
  52. {
  53. Result * pResult = (Result*)(pConn->GetRes());
  54. if (pResult)
  55. {
  56. int col_count = pResult->num_fields();
  57. if (col_count>0)
  58. {
  59. std::map<std::string, std::string> * columnInfoMap = 
  60. new std::map<std::string, std::string>;
  61. //Question no delete up! must delete in somewhere ,romandou
  62. for (unsigned int i = 0; i < col_count; i++)
  63. {
  64. std::string column = pResult->names(i);
  65. std::string type = pResult->types(i).sql_name();
  66. columnInfoMap->insert(std::map<std::string, std::string>::value_type(column,type));
  67. }
  68. m_initedMap[tableName] = columnInfoMap;
  69. return columnInfoMap;
  70. }
  71. else
  72. {
  73. return NULL;
  74. }
  75. }
  76. else
  77. {
  78. // Error
  79. return NULL;
  80. }
  81. }
  82. else
  83. {
  84. //失败
  85. return NULL;
  86. }
  87. }
  88. return NULL;
  89. }