comparison C++/DriverPostgres.cpp @ 172:a61cddaf7547

Rename Driver postgres
author David Demelier <markand@malikania.fr>
date Wed, 11 Sep 2013 15:27:00 +0200
parents C++/DriverPG.cpp@e47c4e9e3f9d
children f0cca031bcce
comparison
equal deleted inserted replaced
171:e47c4e9e3f9d 172:a61cddaf7547
1 /*
2 * DriverPostgres.cpp -- PostgreSQL driver
3 *
4 * Copyright (c) 2013, David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sstream>
20
21 #include "DriverPostgres.h"
22
23 /* --------------------------------------------------------
24 * Query PostgreSQL (protected methods)
25 * -------------------------------------------------------- */
26
27 QueryCheck QueryPostgres::checkRequest(int row, const std::string &column, ColumnType type)
28 {
29 QueryCheck ret = QueryCheck::InvalidType;
30 Oid pqType;
31 int colIndex;
32 bool success = false;
33
34 // Invalid row
35 if (row >= PQntuples(m_result.get()))
36 return QueryCheck::InvalidRow;
37 if ((colIndex = PQfnumber(m_result.get(), column.c_str())) == -1)
38 return QueryCheck::InvalidColumn;
39
40 pqType = PQftype(m_result.get(), colIndex);
41 switch (type)
42 {
43 case ColumnType::Boolean:
44 success = (pqType == 16);
45 break;
46 case ColumnType::Date:
47 success = (pqType == 1082) || (pqType == 1083) || (pqType == 1114);
48 break;
49 case ColumnType::Double:
50 success = (pqType = 1700) || (pqType == 700) || (pqType == 701);
51 break;
52 case ColumnType::Integer:
53 success = (pqType == 20) || (pqType == 21) || (pqType == 23) || (pqType == 1700);
54 break;
55 case ColumnType::String:
56 success = (pqType == 25) || (pqType == 1042) || (pqType == 1043);
57 break;
58 default:
59 ret = QueryCheck::InvalidType;
60 }
61
62 // valid type requested?
63 if (success)
64 ret = QueryCheck::NoError;
65
66 return ret;
67 }
68
69 /* --------------------------------------------------------
70 * Query PostgreSQL (public methods)
71 * -------------------------------------------------------- */
72
73 QueryPostgres::QueryPostgres(PostgresResult result)
74 {
75 m_result = std::move(result);
76 }
77
78 QueryPostgres::~QueryPostgres()
79 {
80 }
81
82 int QueryPostgres::countRows()
83 {
84 return PQntuples(m_result.get());
85 }
86
87 int QueryPostgres::countColumns()
88 {
89 return PQnfields(m_result.get());
90 }
91
92 bool QueryPostgres::checkBool(int row, const std::string &column)
93 {
94 int idx = PQfnumber(m_result.get(), column.c_str());
95 std::string code = PQgetvalue(m_result.get(), row, idx);
96
97 return code[0] == 't';
98 }
99
100 Date QueryPostgres::checkDate(int row, const std::string &column)
101 {
102 int idx = PQfnumber(m_result.get(), column.c_str());
103 long timestamp = static_cast<long>(time(0));
104
105 try
106 {
107 timestamp = std::stol(PQgetvalue(m_result.get(), row, idx));
108 }
109 catch (std::invalid_argument)
110 {
111 }
112
113 return Date(static_cast<time_t>(timestamp));
114 }
115
116 double QueryPostgres::checkDouble(int row, const std::string &column)
117 {
118 int idx = PQfnumber(m_result.get(), column.c_str());
119 double d = 0;
120
121 try
122 {
123 d = std::stod(PQgetvalue(m_result.get(), row, idx));
124 }
125 catch (std::invalid_argument)
126 {
127 }
128
129 return d;
130 }
131
132 int QueryPostgres::checkInt(int row, const std::string &column)
133 {
134 int idx = PQfnumber(m_result.get(), column.c_str());
135 int i = 0;
136
137 try
138 {
139 i = std::stoi(PQgetvalue(m_result.get(), row, idx));
140 }
141 catch (std::invalid_argument)
142 {
143 }
144
145 return i;
146 }
147
148 std::string QueryPostgres::checkString(int row, const std::string &column)
149 {
150 int idx = PQfnumber(m_result.get(), column.c_str());
151
152 return std::string(PQgetvalue(m_result.get(), row, idx));
153 }
154
155 bool QueryPostgres::isNull(int row, const std::string &column)
156 {
157 int idx = PQfnumber(m_result.get(), column.c_str());
158
159 return PQgetisnull(m_result.get(), row, idx) == 1;
160 }
161
162 void QueryPostgres::dump(void)
163 {
164 std::cout << "Dumping PostgreSQL result, ";
165 std::cout << countRows() << " rows, ";
166 std::cout << countColumns() << " columns" << std::endl;
167
168 for (int r = 0; r < countRows(); ++r)
169 {
170 std::cout << "Dumping row " << r << std::endl;
171 std::cout << "==============================" << std::endl;
172
173 for (int c = 0; c < countColumns(); ++c)
174 {
175 std::cout << "\t" << PQfname(m_result.get(), c);
176 std::cout << " = " << PQgetvalue(m_result.get(), r, c) << std::endl;
177 }
178 }
179 }
180
181 /* --------------------------------------------------------
182 * Driver PostgreSQL
183 * -------------------------------------------------------- */
184
185 DriverPostgres::DriverPostgres()
186 {
187 }
188
189 DriverPostgres::~DriverPostgres()
190 {
191 }
192
193 std::string DriverPostgres::convert(Params &params)
194 {
195 std::ostringstream oss;
196
197 oss << "host = " << params["host"] << " ";
198 oss << "port = " << params["port"] << " ";
199 oss << "user = " << params["user"] << " ";
200 oss << "dbname = " << params["database"] << " ";
201 oss << "password = " << params["password"];
202
203 return oss.str();
204 }
205
206 bool DriverPostgres::connect(const Params &params)
207 {
208 Params copy = params;
209 PGconn *conn = PQconnectdb(convert(copy).c_str());
210
211 if (conn == nullptr)
212 {
213 m_error = strerror(ENOMEM);
214 return false;
215 }
216
217 if (PQstatus(conn) == CONNECTION_BAD)
218 {
219 m_error = PQerrorMessage(conn);
220 PQfinish(conn);
221
222 return false;
223 }
224
225 m_connection = PostgresConn(conn);
226
227 return true;
228 }
229
230 std::unique_ptr<Query> DriverPostgres::query(const std::string &cmd)
231 {
232 PGresult *info;
233
234 // If NULL, the libpq said no memory
235 info = PQexec(m_connection.get(), cmd.c_str());
236 if (info == nullptr)
237 throw QueryError(strerror(ENOMEM));
238
239 // If an error occured
240 int errorCode = PQresultStatus(info);
241 if (errorCode != PGRES_COMMAND_OK && errorCode != PGRES_TUPLES_OK)
242 {
243 std::string error = PQresultErrorMessage(info);
244 PQclear(info);
245 throw QueryError(error);
246 }
247
248 return std::unique_ptr<Query>(new QueryPostgres(QueryPostgres::PostgresResult(info)));
249 }
250
251 std::string DriverPostgres::description() const
252 {
253 std::ostringstream oss;
254
255 oss << "Connected on PostgreSQL database: " << std::endl;
256 oss << " host: " << PQhost(m_connection.get()) << std::endl;
257 oss << " port: " << PQport(m_connection.get()) << std::endl;
258 oss << " user: " << PQuser(m_connection.get()) << std::endl;
259 oss << " database: " << PQdb(m_connection.get()) << std::endl;
260
261 return oss.str();
262 }