Mercurial > code
comparison C++/DriverPostgres.cpp @ 184:4c746050969a
Add support for request (PostgreSQL done)
Task: #199
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 11 Nov 2013 18:05:47 +0100 |
parents | f0cca031bcce |
children | cc1e5fe1ee2c |
comparison
equal
deleted
inserted
replaced
183:73146c7c763f | 184:4c746050969a |
---|---|
35 Date QueryPostgres::getDate(int row, const std::string &column) | 35 Date QueryPostgres::getDate(int row, const std::string &column) |
36 { | 36 { |
37 int idx = PQfnumber(m_result.get(), column.c_str()); | 37 int idx = PQfnumber(m_result.get(), column.c_str()); |
38 long timestamp = static_cast<long>(time(0)); | 38 long timestamp = static_cast<long>(time(0)); |
39 | 39 |
40 try | 40 try { |
41 { | |
42 std::ostringstream oss; | 41 std::ostringstream oss; |
43 std::string value, req; | 42 std::string value, req; |
44 | 43 |
45 value = PQgetvalue(m_result.get(), row, idx); | 44 value = PQgetvalue(m_result.get(), row, idx); |
46 | 45 |
53 oss << "') AS RESULT"; | 52 oss << "') AS RESULT"; |
54 req = oss.str(); | 53 req = oss.str(); |
55 | 54 |
56 Query::Ptr result = m_driver->query(req); | 55 Query::Ptr result = m_driver->query(req); |
57 timestamp = result->get<double>(0, "RESULT"); | 56 timestamp = result->get<double>(0, "RESULT"); |
58 } | 57 } catch (...) { } |
59 catch (...) { } | |
60 | 58 |
61 return Date(static_cast<time_t>(timestamp)); | 59 return Date(static_cast<time_t>(timestamp)); |
62 } | 60 } |
63 | 61 |
64 double QueryPostgres::getDouble(int row, const std::string &column) | 62 double QueryPostgres::getDouble(int row, const std::string &column) |
171 } | 169 } |
172 } | 170 } |
173 } | 171 } |
174 | 172 |
175 /* -------------------------------------------------------- | 173 /* -------------------------------------------------------- |
174 * Request PostgreSQL | |
175 * -------------------------------------------------------- */ | |
176 | |
177 RequestPostgres::RequestPostgres(const std::string &command) | |
178 : Request(command) | |
179 { | |
180 } | |
181 | |
182 std::string RequestPostgres::bindBoolean(bool value) | |
183 { | |
184 return (value) ? "'t'" : "'f'"; | |
185 } | |
186 | |
187 std::string RequestPostgres::bindDate(Date value) | |
188 { | |
189 std::ostringstream oss; | |
190 | |
191 oss << "to_timestamp("; | |
192 oss << value.getTimestamp(); | |
193 oss << ")"; | |
194 | |
195 return oss.str(); | |
196 } | |
197 | |
198 std::string RequestPostgres::bindDouble(double value) | |
199 { | |
200 return std::to_string(value); | |
201 } | |
202 | |
203 std::string RequestPostgres::bindInt(int value) | |
204 { | |
205 return std::to_string(value); | |
206 } | |
207 | |
208 std::string RequestPostgres::bindString(std::string value) | |
209 { | |
210 std::ostringstream oss; | |
211 | |
212 oss << "'"; | |
213 for (auto c : value) { | |
214 if (c == '\'') | |
215 oss << "\\'"; | |
216 else | |
217 oss << c; | |
218 } | |
219 | |
220 oss << "'"; | |
221 | |
222 return oss.str(); | |
223 } | |
224 | |
225 /* -------------------------------------------------------- | |
176 * Driver PostgreSQL | 226 * Driver PostgreSQL |
177 * -------------------------------------------------------- */ | 227 * -------------------------------------------------------- */ |
178 | 228 |
179 DriverPostgres::DriverPostgres() | 229 DriverPostgres::DriverPostgres() |
180 { | 230 { |
219 m_connection = PostgresConn(conn); | 269 m_connection = PostgresConn(conn); |
220 | 270 |
221 return true; | 271 return true; |
222 } | 272 } |
223 | 273 |
274 Request::Ptr DriverPostgres::prepare(const std::string &command) | |
275 { | |
276 return std::make_shared<RequestPostgres>(command); | |
277 } | |
278 | |
224 Query::Ptr DriverPostgres::query(const std::string &cmd) | 279 Query::Ptr DriverPostgres::query(const std::string &cmd) |
225 { | 280 { |
226 PGresult *info; | 281 PGresult *info; |
227 | 282 |
228 // If NULL, the libpq said no memory | 283 // If NULL, the libpq said no memory |