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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include "modifycommand.h"
#include "error.h"
#include <stdlib.h>
#include "connection.h"
PQ::ModifyCommand::ModifyCommand(const Connection * conn, const std::string & sql, unsigned int no) :
DB::Command(sql),
DB::ModifyCommand(sql),
PQ::Command(conn, sql, no),
prepared(false)
{
}
PQ::ModifyCommand::~ModifyCommand()
{
}
void
PQ::ModifyCommand::prepare() const
{
if (!prepared) {
std::string psql;
psql.reserve(sql.length() + 20);
char buf[4];
int p = 1;
bool inquote = false;
for(std::string::const_iterator i = sql.begin(); i != sql.end(); i++) {
if (*i == '?' && !inquote) {
snprintf(buf, 4, "$%d", p++);
psql += buf;
}
else if (*i == '\'') {
inquote = !inquote;
psql += *i;
}
else {
psql += *i;
}
}
c->checkResultFree(PQprepare(
c->conn, stmntName.c_str(), psql.c_str(), values.size(), NULL), PGRES_COMMAND_OK);
prepared = true;
}
}
unsigned int
PQ::ModifyCommand::execute(bool anc)
{
prepare();
PGresult * res = PQexecPrepared(c->conn, stmntName.c_str(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0);
c->checkResult(res, PGRES_COMMAND_OK, PGRES_TUPLES_OK);
unsigned int rows = atoi(PQcmdTuples(res));
PQclear(res);
if (rows == 0 && !anc) {
throw Error("No rows affected");
}
return rows;
}
|