Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. #include <exception>
  2. #include <string>
  3. #include <string.h>
  4.  
  5. #include "DataAccess.h"
  6.  
  7. DataAccess DataAccess::getDataAccess()
  8. {
  9. const char* conninfo;
  10. PGconn* conn;
  11.  
  12. /* Connection string */
  13. conninfo = "host=127.0.0.1 port=5432 dbname=postgres user=postgres password=adv_admin";
  14.  
  15. /* Make a connection to the database */
  16. conn = PQconnectdb(conninfo);
  17.  
  18. /* Check to see that the backend connection was successfully made */
  19. if (PQstatus(conn) != CONNECTION_OK)
  20. {
  21. string msg{ PQerrorMessage(conn) };
  22. cout << msg << endl;
  23. PQfinish(conn);
  24. throw runtime_error(msg);
  25. }
  26. cout << "Successful connection." << endl;
  27. return DataAccess{ conn };
  28. }
  29.  
  30. DataAccess::DataAccess(PGconn* conn) :_conn(conn)
  31. {
  32. }
  33.  
  34. void DataAccess::insertOneRecord(tuple<string, int, int> args) //pasar una tupla
  35. {
  36. string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
  37. qry += "VALUES ($1, $2, $3) ";
  38.  
  39. string stmt = "statement";
  40.  
  41. _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
  42.  
  43. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  44. {
  45. _errorMessage = PQerrorMessage(_conn);
  46. PQclear(_res);
  47. return;
  48. }
  49.  
  50. //Transformar los elementos de la tupla en c_str() y armar paramValues
  51. const char* paramValues[3];// = new char*[3][1];
  52.  
  53. paramValues[0] = get<0>(args).c_str();
  54. printf("paramValues[0]: %s\n", paramValues[0]);
  55.  
  56. //IMPORTANTE
  57. //Es imprescindible definir primero s1 y luego aplicar c_str().
  58. //Si se hace directamente to_string(get<1>(args)).c_str() la próxima vez que se ejecute
  59. //string::c_str() devolverá el mismo puntero y por lo tanto corromperá lo valores anteriores
  60. //de paramValues.
  61. //Más claramente, si hiciéramos:
  62. // paramValues[1] = to_string(get<1>(args)).c_str();
  63. // paramValues[2] = to_string(get<2>(args)).c_str();
  64. //Tanto paramValues[1] como paramValues[2] quedarían apuntando al mismo valor (que
  65. //es el que está en get<2>(args)). Esto porque si string::c_str() se llama sobre un valor
  66. //temporal, la próxima vez que se llame va a devolver el mismo puntero.
  67.  
  68. string s1 = to_string(get<1>(args));
  69. paramValues[1] = s1.c_str();
  70. printf("paramValues[1]%: %s\n", paramValues[1]);
  71.  
  72. string s2 = to_string(get<2>(args));
  73. paramValues[2] = s2.c_str();
  74. printf("paramValues[2]: %s\n", paramValues[2]);
  75.  
  76. _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
  77.  
  78. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  79. {
  80. _errorMessage = PQerrorMessage(_conn);
  81. PQclear(_res);
  82. return;
  83. }
  84. _errorMessage = "DataAccess::easyInsert ejecutado Ok.";
  85.  
  86. _res = PQexec(_conn, "DEALLOCATE statement");
  87. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  88. {
  89. _errorMessage = PQerrorMessage(_conn);
  90. PQclear(_res);
  91. return;
  92. }
  93.  
  94. return;
  95. }
  96.  
  97. void DataAccess::insertOneRecord(tuple<string, int, double> args)
  98. {
  99. string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
  100. qry += "VALUES ($1, $2::int8, $3::float8)";
  101.  
  102. string stmt = "statement";
  103.  
  104. _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
  105.  
  106. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  107. {
  108. _errorMessage = "insertOneRecord PQprepare ";
  109. _errorMessage += PQerrorMessage(_conn);
  110. _res = PQexec(_conn, "DEALLOCATE statement");
  111. PQclear(_res);
  112. return;
  113. }
  114. PQclear(_res);
  115.  
  116. //Transformar los elementos de la tupla en c_str() y armar paramValues
  117. const char* paramValues[3];
  118.  
  119. paramValues[0] = get<0>(args).c_str();
  120. printf("paramValues[0]: %s\n", paramValues[0]);
  121.  
  122. string s1 = to_string(get<1>(args));
  123. paramValues[1] = s1.c_str();
  124. printf("paramValues[1]%: %s\n", paramValues[1]);
  125.  
  126. string s2 = to_string(get<2>(args));
  127. paramValues[2] = s2.c_str();
  128. printf("paramValues[2]: %s\n", paramValues[2]);
  129. printf("paramValues[1]%: %s\n", paramValues[1]);
  130.  
  131. _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
  132.  
  133. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  134. {
  135. _errorMessage = "insertOneRecord PQexecPrepared ";
  136. _errorMessage += PQerrorMessage(_conn);
  137. _res = PQexec(_conn, "DEALLOCATE statement");
  138. PQclear(_res);
  139. return;
  140. }
  141.  
  142. _errorMessage = "DataAccess::insertOneRecord ejecutado Ok.";
  143.  
  144. _res = PQexec(_conn, "DEALLOCATE statement");
  145. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  146. {
  147. _errorMessage = "insertOneRecord DEALLOCATE statement ";
  148. _errorMessage += PQerrorMessage(_conn);
  149. PQclear(_res);
  150. return;
  151. }
  152.  
  153. return;
  154. }
  155.  
  156. void DataAccess::insertManyRecords(vector<tuple<string, int, double>> records, bool verbose)
  157. {
  158. string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
  159. qry += "VALUES ($1, $2, $3)";
  160.  
  161. string stmt = "statement";
  162.  
  163. _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
  164.  
  165. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  166. {
  167. _errorMessage = PQerrorMessage(_conn);
  168. _errorMessage = "insertManyRecords PQPrepare" + _errorMessage;
  169. _res = PQexec(_conn, "DEALLOCATE statement");
  170. return;
  171. }
  172.  
  173. //Se abre una transaccion
  174. _res = PQexec(_conn, "BEGIN");
  175. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  176. {
  177. _errorMessage = PQerrorMessage(_conn);
  178. PQclear(_res);
  179. return;
  180. }
  181.  
  182. //Loopeamos sobre la data
  183.  
  184. for (const auto& record : records)
  185. {
  186. //Transformar los elementos de la tupla en c_str() y armar paramValues
  187. const char* paramValues[3];
  188.  
  189. paramValues[0] = get<0>(record).c_str();
  190. if (verbose)
  191. printf("paramValues[0]: %s\n", paramValues[0]);
  192.  
  193. string s1 = to_string(get<1>(record));
  194. paramValues[1] = s1.c_str();
  195. if (verbose)
  196. printf("paramValues[1]%: %s\n", paramValues[1]);
  197.  
  198. string s2 = to_string(get<2>(record));
  199. paramValues[2] = s2.c_str();
  200. if (verbose)
  201. printf("paramValues[2]: %s\n", paramValues[2]);
  202.  
  203. _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
  204.  
  205. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  206. {
  207. _errorMessage = PQerrorMessage(_conn);
  208. PQclear(_res);
  209. return;
  210. }
  211. }
  212. //Se cierra la transaccion
  213. _res = PQexec(_conn, "END");
  214. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  215. {
  216. _errorMessage = PQerrorMessage(_conn);
  217. PQclear(_res);
  218. return;
  219. }
  220. _errorMessage = "DataAccess::insertManyRecords ejecutado Ok.";
  221.  
  222. _res = PQexec(_conn, "DEALLOCATE statement");
  223. if (PQresultStatus(_res) != PGRES_COMMAND_OK)
  224. {
  225. _errorMessage = PQerrorMessage(_conn);
  226. PQclear(_res);
  227. return;
  228. }
  229.  
  230. return;
  231. }
  232.  
  233. string DataAccess::lastErrorMessage()
  234. {
  235. return _errorMessage;
  236. }
  237.  
  238. DataAccess::~DataAccess()
  239. {
  240. PQclear(_res);
  241. PQfinish(_conn);
  242. cout << "Connection finished." << endl;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement