summaryrefslogtreecommitdiff
path: root/libpqpp/pq-prepared.cpp
blob: 4cd4540659568b35105c728694cff597311acf45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "pq-prepared.h"
#include "pq-connection.h"

PQ::PreparedStatement::PreparedStatement(Connection * c, const std::string & sql, unsigned int no) :
	DB::Command(sql),
	Command(c, sql, no),
	pstmt(nullptr)
{
}

const char *
PQ::PreparedStatement::prepare() const
{
	if (pstmt) {
		return pstmt;
	}
	auto hash(std::hash<std::string>()(sql));
	auto i = c->preparedStatements.find(hash);
	if (i != c->preparedStatements.end()) {
		return (pstmt = i->second.c_str());
	}
	std::string psql;
	psql.reserve(sql.length() + 20);
	prepareSql(psql, sql);
	c->checkResultFree(PQprepare(
				c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK);
	return (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str());
}