diff 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
line wrap: on
line diff
--- a/C++/DriverPostgres.cpp	Tue Oct 29 21:33:00 2013 +0100
+++ b/C++/DriverPostgres.cpp	Mon Nov 11 18:05:47 2013 +0100
@@ -37,8 +37,7 @@
 	int idx = PQfnumber(m_result.get(), column.c_str());
 	long timestamp = static_cast<long>(time(0));
 
-	try
-	{
+	try {
 		std::ostringstream oss;
 		std::string value, req;
 
@@ -55,8 +54,7 @@
 
 		Query::Ptr result = m_driver->query(req);
 		timestamp = result->get<double>(0, "RESULT");
-	}
-	catch (...) { }
+	} catch (...) { }
 
 	return Date(static_cast<time_t>(timestamp));
 }
@@ -173,6 +171,58 @@
 }
 
 /* --------------------------------------------------------
+ * Request PostgreSQL
+ * -------------------------------------------------------- */
+
+RequestPostgres::RequestPostgres(const std::string &command)
+	: Request(command)
+{
+}
+
+std::string RequestPostgres::bindBoolean(bool value)
+{
+	return (value) ? "'t'" : "'f'";
+}
+
+std::string RequestPostgres::bindDate(Date value)
+{
+	std::ostringstream oss;
+
+	oss << "to_timestamp(";
+	oss << value.getTimestamp();
+	oss << ")";
+
+	return oss.str();
+}
+
+std::string RequestPostgres::bindDouble(double value)
+{
+	return std::to_string(value);
+}
+
+std::string RequestPostgres::bindInt(int value)
+{
+	return std::to_string(value);
+}
+
+std::string RequestPostgres::bindString(std::string value)
+{
+	std::ostringstream oss;
+
+	oss << "'";
+	for (auto c : value) {
+		if (c == '\'')
+			oss << "\\'";
+		else
+			oss << c;
+	}
+
+	oss << "'";
+
+	return oss.str();
+}
+
+/* --------------------------------------------------------
  * Driver PostgreSQL
  * -------------------------------------------------------- */
 
@@ -221,6 +271,11 @@
 	return true;
 }
 
+Request::Ptr DriverPostgres::prepare(const std::string &command)
+{
+	return std::make_shared<RequestPostgres>(command);
+}
+
 Query::Ptr DriverPostgres::query(const std::string &cmd)
 {
 	PGresult *info;