PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/pq/preparedstatement.h

http://github.com/mozy/mordor
C Header | 175 lines | 157 code | 17 blank | 1 comment | 0 complexity | b3249ba42c89ce8910d10dd3058632cc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_PQ_PREPAREDSTATEMENT_H__
  2. #define __MORDOR_PQ_PREPAREDSTATEMENT_H__
  3. // Copyright (c) 2010 - Mozy, Inc.
  4. #include <vector>
  5. #include <boost/date_time/posix_time/posix_time_types.hpp>
  6. #include <boost/weak_ptr.hpp>
  7. #include "result.h"
  8. namespace Mordor {
  9. class IOManager;
  10. class Scheduler;
  11. namespace PQ {
  12. #ifdef WIN32
  13. typedef Scheduler SchedulerType;
  14. #else
  15. typedef IOManager SchedulerType;
  16. #endif
  17. struct Null {};
  18. struct Skip {};
  19. class PreparedStatement
  20. {
  21. friend class Connection;
  22. public:
  23. enum ResultFormat {
  24. TEXT = 0,
  25. BINARY = 1
  26. };
  27. private:
  28. PreparedStatement(boost::shared_ptr<PGconn> conn,
  29. const std::string &command, const std::string &name,
  30. SchedulerType *scheduler, ResultFormat format = BINARY)
  31. : m_conn(conn),
  32. m_command(command),
  33. m_name(name),
  34. m_scheduler(scheduler),
  35. m_resultFormat(format)
  36. {}
  37. public:
  38. PreparedStatement(): m_resultFormat(BINARY) {}
  39. void bind(size_t param, const Null &);
  40. void bind(size_t param, const char *value);
  41. void bind(size_t param, const std::string &value);
  42. void bind(size_t param, bool value);
  43. void bind(size_t param, char value);
  44. void bind(size_t param, short value);
  45. void bind(size_t param, int value);
  46. void bind(size_t param, long long value);
  47. void bind(size_t param, float value);
  48. void bind(size_t param, double value);
  49. void bind(size_t param, const boost::posix_time::ptime &value, bool timezone = false);
  50. void bindUntyped(size_t param, const std::string &value);
  51. Result execute();
  52. template <class T1>
  53. Result execute(const T1 &param1)
  54. {
  55. bind(1, param1);
  56. return execute();
  57. }
  58. template <class T1, class T2>
  59. Result execute(const T1 &param1, const T2 &param2)
  60. {
  61. bind(2, param2);
  62. bind(1, param1);
  63. return execute();
  64. }
  65. template <class T1, class T2, class T3>
  66. Result execute(const T1 &param1, const T2 &param2, const T3 &param3)
  67. {
  68. bind(3, param3);
  69. bind(2, param2);
  70. bind(1, param1);
  71. return execute();
  72. }
  73. template <class T1, class T2, class T3, class T4>
  74. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4)
  75. {
  76. bind(4, param4);
  77. bind(3, param3);
  78. bind(2, param2);
  79. bind(1, param1);
  80. return execute();
  81. }
  82. template <class T1, class T2, class T3, class T4, class T5>
  83. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5)
  84. {
  85. bind(5, param5);
  86. bind(4, param4);
  87. bind(3, param3);
  88. bind(2, param2);
  89. bind(1, param1);
  90. return execute();
  91. }
  92. template <class T1, class T2, class T3, class T4, class T5, class T6>
  93. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6)
  94. {
  95. bind(6, param6);
  96. bind(5, param5);
  97. bind(4, param4);
  98. bind(3, param3);
  99. bind(2, param2);
  100. bind(1, param1);
  101. return execute();
  102. }
  103. template <class T1, class T2, class T3, class T4, class T5, class T6, class T7>
  104. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7)
  105. {
  106. bind(7, param7);
  107. bind(6, param6);
  108. bind(5, param5);
  109. bind(4, param4);
  110. bind(3, param3);
  111. bind(2, param2);
  112. bind(1, param1);
  113. return execute();
  114. }
  115. template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8>
  116. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7, const T8 &param8)
  117. {
  118. bind(8, param8);
  119. bind(7, param7);
  120. bind(6, param6);
  121. bind(5, param5);
  122. bind(4, param4);
  123. bind(3, param3);
  124. bind(2, param2);
  125. bind(1, param1);
  126. return execute();
  127. }
  128. template <class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9>
  129. Result execute(const T1 &param1, const T2 &param2, const T3 &param3, const T4 &param4, const T5 &param5, const T6 &param6, const T7 &param7, const T8 &param8, const T9 &param9)
  130. {
  131. bind(9, param9);
  132. bind(8, param8);
  133. bind(7, param7);
  134. bind(6, param6);
  135. bind(5, param5);
  136. bind(4, param4);
  137. bind(3, param3);
  138. bind(2, param2);
  139. bind(1, param1);
  140. return execute();
  141. }
  142. private:
  143. void bind(size_t param, const Skip &) {}
  144. void setType(size_t param, Oid type);
  145. void ensure(size_t count);
  146. private:
  147. boost::weak_ptr<PGconn> m_conn;
  148. std::string m_command;
  149. std::string m_name;
  150. SchedulerType *m_scheduler;
  151. ResultFormat m_resultFormat;
  152. std::vector<Oid> m_paramTypes;
  153. std::vector<std::string> m_paramValues;
  154. std::vector<const char *> m_params;
  155. std::vector<int> m_paramLengths, m_paramFormats;
  156. };
  157. }}
  158. #endif