Mercurial > code
comparison C++/DriverPostgres.cpp @ 190:d263f85f43a4
Update escape string for PostgreSQL strings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Nov 2013 10:25:58 +0100 |
parents | cc1e5fe1ee2c |
children | 6c49e5e3ecc8 |
comparison
equal
deleted
inserted
replaced
189:cc1e5fe1ee2c | 190:d263f85f43a4 |
---|---|
168 | 168 |
169 /* -------------------------------------------------------- | 169 /* -------------------------------------------------------- |
170 * Request PostgreSQL | 170 * Request PostgreSQL |
171 * -------------------------------------------------------- */ | 171 * -------------------------------------------------------- */ |
172 | 172 |
173 RequestPostgres::RequestPostgres(const std::string &command) | 173 RequestPostgres::RequestPostgres(std::shared_ptr<PGconn> conn, const std::string &command) |
174 : Request(command) | 174 : Request(command) |
175 { | 175 { |
176 m_connection = conn; | |
176 } | 177 } |
177 | 178 |
178 std::string RequestPostgres::bindBoolean(bool value) | 179 std::string RequestPostgres::bindBoolean(bool value) |
179 { | 180 { |
180 return (value) ? "'t'" : "'f'"; | 181 return (value) ? "'t'" : "'f'"; |
201 return std::to_string(value); | 202 return std::to_string(value); |
202 } | 203 } |
203 | 204 |
204 std::string RequestPostgres::bindString(std::string value) | 205 std::string RequestPostgres::bindString(std::string value) |
205 { | 206 { |
206 std::ostringstream oss; | 207 std::string result; |
207 | 208 char *tmp; |
208 oss << "'"; | 209 |
209 for (auto c : value) { | 210 tmp = PQescapeLiteral(m_connection.get(), value.c_str(), value.length()); |
210 if (c == '\'') | 211 if (tmp == nullptr) |
211 oss << "\\'"; | 212 return ""; |
212 else | 213 |
213 oss << c; | 214 result = std::string(tmp); |
214 } | 215 PQfreemem(tmp); |
215 | 216 |
216 oss << "'"; | 217 return result; |
217 | |
218 return oss.str(); | |
219 } | 218 } |
220 | 219 |
221 /* -------------------------------------------------------- | 220 /* -------------------------------------------------------- |
222 * Driver PostgreSQL | 221 * Driver PostgreSQL |
223 * -------------------------------------------------------- */ | 222 * -------------------------------------------------------- */ |
258 PQfinish(conn); | 257 PQfinish(conn); |
259 | 258 |
260 return false; | 259 return false; |
261 } | 260 } |
262 | 261 |
263 m_connection = PostgresConn(conn); | 262 m_connection = std::shared_ptr<PGconn>(conn, PGDeleter()); |
264 | 263 |
265 return true; | 264 return true; |
266 } | 265 } |
267 | 266 |
268 Request::Ptr DriverPostgres::prepare(const std::string &command) | 267 Request::Ptr DriverPostgres::prepare(const std::string &command) |
269 { | 268 { |
270 return std::make_shared<RequestPostgres>(command); | 269 return std::make_shared<RequestPostgres>(m_connection, command); |
271 } | 270 } |
272 | 271 |
273 Query::Ptr DriverPostgres::query(const std::string &cmd) | 272 Query::Ptr DriverPostgres::query(const std::string &cmd) |
274 { | 273 { |
275 PGresult *info; | 274 PGresult *info; |
306 | 305 |
307 std::string DriverPostgres::version() const | 306 std::string DriverPostgres::version() const |
308 { | 307 { |
309 std::ostringstream oss; | 308 std::ostringstream oss; |
310 | 309 |
311 // TODO: ADD VERSION | 310 oss << "PostgreSQL driver (version " << PQlibVersion() << ")"; |
312 | |
313 oss << "PostgreSQL driver"; | |
314 | 311 |
315 return oss.str(); | 312 return oss.str(); |
316 } | 313 } |