summaryrefslogtreecommitdiff
path: root/libpqpp/pq-prepared.cpp
blob: 640b3db956fd19ea9ad6cbda16d0ee3714b6c651 (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
#include "pq-prepared.h"
#include "pq-connection.h"

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

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