1 | /* |
---|
2 | Copyright (C) 2012 |
---|
3 | Alejandro Mujica (amujica@cenditel.gob.ve) |
---|
4 | Erwin Paredes (eparedes@cenditel.gob.ve) |
---|
5 | José Ruiz (jruiz@cenditel.gob.ve) |
---|
6 | Rodolfo Rangel (rrangel@cenditel.gob.ve) |
---|
7 | Julie Vera (jvera@cenditel.gob.ve) |
---|
8 | |
---|
9 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
10 | Tecnologías Libres |
---|
11 | |
---|
12 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
13 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
14 | |
---|
15 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
16 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
17 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
18 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
19 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
20 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | Autor: Alejandro J. Mujica |
---|
25 | Fecha de creación: 26/07/2013 |
---|
26 | Este archivo contiene la implementación de la clase DBQuery. |
---|
27 | */ |
---|
28 | |
---|
29 | # include <stdexcept> |
---|
30 | # include <dbQuery.H> |
---|
31 | |
---|
32 | DBQuery::DBQuery(DBConnection & connection) |
---|
33 | : connection(connection), pgresult(NULL), numCols(0), numRows(0), |
---|
34 | currRow(0) { |
---|
35 | |
---|
36 | // Empty |
---|
37 | } |
---|
38 | |
---|
39 | DBQuery::~DBQuery() { |
---|
40 | clear(); |
---|
41 | } |
---|
42 | |
---|
43 | bool DBQuery::exec(const std::string & strQuery) { |
---|
44 | |
---|
45 | pgresult = PQexec(connection.pgconn, strQuery.c_str()); |
---|
46 | |
---|
47 | ExecStatusType est = PQresultStatus(pgresult); |
---|
48 | |
---|
49 | if (est == PGRES_TUPLES_OK) { |
---|
50 | numCols = PQnfields(pgresult); |
---|
51 | numRows = PQntuples(pgresult); |
---|
52 | return true; |
---|
53 | } |
---|
54 | |
---|
55 | clear(); |
---|
56 | |
---|
57 | return false; |
---|
58 | } |
---|
59 | |
---|
60 | void DBQuery::clear() { |
---|
61 | |
---|
62 | if (pgresult == NULL) |
---|
63 | return; |
---|
64 | |
---|
65 | PQclear(pgresult); |
---|
66 | pgresult = NULL; |
---|
67 | numCols = 0; |
---|
68 | numRows = 0; |
---|
69 | currRow = 0; |
---|
70 | } |
---|
71 | |
---|
72 | void DBQuery::reset() { |
---|
73 | currRow = 0; |
---|
74 | } |
---|
75 | |
---|
76 | bool DBQuery::next() { |
---|
77 | return currRow++ < numRows; |
---|
78 | } |
---|
79 | |
---|
80 | bool DBQuery::hasResult() const { |
---|
81 | return numRows > 0; |
---|
82 | } |
---|
83 | |
---|
84 | char * DBQuery::getValue(const size_t & colNumber) { |
---|
85 | |
---|
86 | if (not hasResult()) |
---|
87 | throw std::domain_error("there is not result"); |
---|
88 | |
---|
89 | if (currRow > numRows) |
---|
90 | throw std::overflow_error("there is not more tuples"); |
---|
91 | |
---|
92 | if (colNumber >= numCols) |
---|
93 | throw std::overflow_error("column number is out of range"); |
---|
94 | |
---|
95 | return PQgetvalue(pgresult, currRow - 1, colNumber); |
---|
96 | } |
---|
97 | |
---|
98 | char * DBQuery::getValue(const std::string & colName) { |
---|
99 | return getValue(PQfnumber(pgresult, colName.c_str())); |
---|
100 | } |
---|
101 | |
---|