Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <exception>
- #include <string>
- #include <string.h>
- #include "DataAccess.h"
- DataAccess DataAccess::getDataAccess()
- {
- const char* conninfo;
- PGconn* conn;
- /* Connection string */
- conninfo = "host=127.0.0.1 port=5432 dbname=postgres user=postgres password=adv_admin";
- /* Make a connection to the database */
- conn = PQconnectdb(conninfo);
- /* Check to see that the backend connection was successfully made */
- if (PQstatus(conn) != CONNECTION_OK)
- {
- string msg{ PQerrorMessage(conn) };
- cout << msg << endl;
- PQfinish(conn);
- throw runtime_error(msg);
- }
- cout << "Successful connection." << endl;
- return DataAccess{ conn };
- }
- DataAccess::DataAccess(PGconn* conn) :_conn(conn)
- {
- }
- void DataAccess::insertOneRecord(tuple<string, int, int> args) //pasar una tupla
- {
- string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
- qry += "VALUES ($1, $2, $3) ";
- string stmt = "statement";
- _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- //Transformar los elementos de la tupla en c_str() y armar paramValues
- const char* paramValues[3];// = new char*[3][1];
- paramValues[0] = get<0>(args).c_str();
- printf("paramValues[0]: %s\n", paramValues[0]);
- //IMPORTANTE
- //Es imprescindible definir primero s1 y luego aplicar c_str().
- //Si se hace directamente to_string(get<1>(args)).c_str() la próxima vez que se ejecute
- //string::c_str() devolverá el mismo puntero y por lo tanto corromperá lo valores anteriores
- //de paramValues.
- //Más claramente, si hiciéramos:
- // paramValues[1] = to_string(get<1>(args)).c_str();
- // paramValues[2] = to_string(get<2>(args)).c_str();
- //Tanto paramValues[1] como paramValues[2] quedarían apuntando al mismo valor (que
- //es el que está en get<2>(args)). Esto porque si string::c_str() se llama sobre un valor
- //temporal, la próxima vez que se llame va a devolver el mismo puntero.
- string s1 = to_string(get<1>(args));
- paramValues[1] = s1.c_str();
- printf("paramValues[1]%: %s\n", paramValues[1]);
- string s2 = to_string(get<2>(args));
- paramValues[2] = s2.c_str();
- printf("paramValues[2]: %s\n", paramValues[2]);
- _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- _errorMessage = "DataAccess::easyInsert ejecutado Ok.";
- _res = PQexec(_conn, "DEALLOCATE statement");
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- return;
- }
- void DataAccess::insertOneRecord(tuple<string, int, double> args)
- {
- string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
- qry += "VALUES ($1, $2::int8, $3::float8)";
- string stmt = "statement";
- _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = "insertOneRecord PQprepare ";
- _errorMessage += PQerrorMessage(_conn);
- _res = PQexec(_conn, "DEALLOCATE statement");
- PQclear(_res);
- return;
- }
- PQclear(_res);
- //Transformar los elementos de la tupla en c_str() y armar paramValues
- const char* paramValues[3];
- paramValues[0] = get<0>(args).c_str();
- printf("paramValues[0]: %s\n", paramValues[0]);
- string s1 = to_string(get<1>(args));
- paramValues[1] = s1.c_str();
- printf("paramValues[1]%: %s\n", paramValues[1]);
- string s2 = to_string(get<2>(args));
- paramValues[2] = s2.c_str();
- printf("paramValues[2]: %s\n", paramValues[2]);
- printf("paramValues[1]%: %s\n", paramValues[1]);
- _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = "insertOneRecord PQexecPrepared ";
- _errorMessage += PQerrorMessage(_conn);
- _res = PQexec(_conn, "DEALLOCATE statement");
- PQclear(_res);
- return;
- }
- _errorMessage = "DataAccess::insertOneRecord ejecutado Ok.";
- _res = PQexec(_conn, "DEALLOCATE statement");
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = "insertOneRecord DEALLOCATE statement ";
- _errorMessage += PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- return;
- }
- void DataAccess::insertManyRecords(vector<tuple<string, int, double>> records, bool verbose)
- {
- string qry = "INSERT INTO test_libpq.string_integer_float (string, integer, float) ";
- qry += "VALUES ($1, $2, $3)";
- string stmt = "statement";
- _res = PQprepare(_conn, stmt.c_str(), qry.c_str(), 3, NULL);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- _errorMessage = "insertManyRecords PQPrepare" + _errorMessage;
- _res = PQexec(_conn, "DEALLOCATE statement");
- return;
- }
- //Se abre una transaccion
- _res = PQexec(_conn, "BEGIN");
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- //Loopeamos sobre la data
- for (const auto& record : records)
- {
- //Transformar los elementos de la tupla en c_str() y armar paramValues
- const char* paramValues[3];
- paramValues[0] = get<0>(record).c_str();
- if (verbose)
- printf("paramValues[0]: %s\n", paramValues[0]);
- string s1 = to_string(get<1>(record));
- paramValues[1] = s1.c_str();
- if (verbose)
- printf("paramValues[1]%: %s\n", paramValues[1]);
- string s2 = to_string(get<2>(record));
- paramValues[2] = s2.c_str();
- if (verbose)
- printf("paramValues[2]: %s\n", paramValues[2]);
- _res = PQexecPrepared(_conn, stmt.c_str(), 3, paramValues, NULL, NULL, 0);
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- }
- //Se cierra la transaccion
- _res = PQexec(_conn, "END");
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- _errorMessage = "DataAccess::insertManyRecords ejecutado Ok.";
- _res = PQexec(_conn, "DEALLOCATE statement");
- if (PQresultStatus(_res) != PGRES_COMMAND_OK)
- {
- _errorMessage = PQerrorMessage(_conn);
- PQclear(_res);
- return;
- }
- return;
- }
- string DataAccess::lastErrorMessage()
- {
- return _errorMessage;
- }
- DataAccess::~DataAccess()
- {
- PQclear(_res);
- PQfinish(_conn);
- cout << "Connection finished." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement