summaryrefslogtreecommitdiff
path: root/libpqpp/selectcommand.cpp
blob: 359cdfd2eee82cf8396bc2eb7267d39fcb6738ef (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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "selectcommand.h"
#include "connection.h"
#include "column.h"
#include "error.h"

PQ::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql, unsigned int no) :
	DB::Command(sql),
	DB::SelectCommand(sql),
	PQ::Command(conn, sql, no),
	executed(false),
	txOpened(false),
	nTuples(0),
	tuple(0),
	execRes(NULL)
{
}

PQ::SelectCommand::~SelectCommand()
{
	if (txOpened) {
		c->commitTx();
	}
	if (executed) {
		PQclear(PQexec(c->conn, ("CLOSE " + stmntName).c_str()));
		if (execRes) {
			PQclear(execRes);
		}
	}
}

void
PQ::SelectCommand::execute()
{
	if (!executed) {
		std::string psql;
		psql.reserve(sql.length() + 40);
		char buf[4];
		int p = 1;
		bool inquote = false;
		psql += "DECLARE ";
		psql += stmntName;
		psql += " CURSOR FOR ";
		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->beginTx();
		txOpened = true;
		execRes = c->checkResult(
				PQexecParams(c->conn, psql.c_str(), values.size(), NULL, &values.front(), &lengths.front(), &formats.front(), 0),
				PGRES_COMMAND_OK);
		fetchTuples();
		unsigned int nFields = PQnfields(execRes);
		for (unsigned int f = 0; f < nFields; f += 1) {
			columns.insert(DB::ColumnPtr(new Column(this, f)));
		}
		executed = true;
	}
}

void
PQ::SelectCommand::fetchTuples()
{
	if (execRes) {
		PQclear(execRes);
	}
	execRes = NULL;
	execRes = c->checkResult(PQexec(c->conn, ("FETCH 35 IN " + stmntName).c_str()), PGRES_TUPLES_OK);
	nTuples = PQntuples(execRes);
	tuple = -1;
}

bool
PQ::SelectCommand::fetch()
{
	execute();
	if (tuple >= (nTuples - 1)) {
		fetchTuples();
	}
	if (tuple++ < (nTuples - 1)) {
		return true;
	}
	else {
		PQclear(PQexec(c->conn, ("CLOSE " + stmntName).c_str()));
		PQclear(execRes);
		executed = false;
		return false;
	}
}