summaryrefslogtreecommitdiff
path: root/libpqpp/selectcommand.cpp
blob: 9d3f3562ac945f3136939c41bc077040411e0c92 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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),
	nTuples(0),
	tuple(0),
	execRes(NULL)
{
	c->beginTx();
}

PQ::SelectCommand::~SelectCommand()
{
	c->commitTx();
	if (executed) {
		PQclear(PQexec(c->conn, ("CLOSE " + stmntName).c_str()));
		PQclear(execRes);
	}
	for (unsigned int f = 0; f < fields.size(); f += 1) {
		delete fields[f];
	}
}

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->checkResultFree(
				PQexecParams(c->conn, psql.c_str(), values.size(), NULL, &values.front(), &lengths.front(), &formats.front(), 0),
				PGRES_COMMAND_OK);
		executed = true;
	}
}

bool
PQ::SelectCommand::fetch()
{
	execute();
	if (tuple >= (nTuples - 1)) {
		if (execRes) {
			PQclear(execRes);
		}
		execRes = c->checkResult(PQexec(c->conn, ("FETCH 35 IN " + stmntName).c_str()), PGRES_TUPLES_OK);
        nTuples = PQntuples(execRes);
		tuple = -1;
	}
	if (fields.empty()) {
		unsigned int nFields = PQnfields(execRes);
		fields.resize(nFields);
		for (unsigned int f = 0; f < nFields; f += 1) {
			Column * c = new Column(this, f);
			fields[f] = c;
			fieldsName[c->name] = c;
		}
	}
	if (tuple++ < (nTuples - 1)) {
		return true;
	}
	else {
		PQclear(PQexec(c->conn, ("CLOSE " + stmntName).c_str()));
		PQclear(execRes);
		executed = false;
		return false;
	}
}

const DB::Column&
PQ::SelectCommand::operator[](unsigned int n) const
{
	if (n < fields.size()) {
		return *fields[n];
	}
	throw Error();
}

const DB::Column&
PQ::SelectCommand::operator[](const Glib::ustring & n) const
{
	std::map<Glib::ustring, Column *>::const_iterator i = fieldsName.find(n);
	if (i != fieldsName.end()) {
		return *i->second;
	}
	throw Error();
}

unsigned int
PQ::SelectCommand::getOrdinal(const Glib::ustring & n) const
{
	std::map<Glib::ustring, Column *>::const_iterator i = fieldsName.find(n);
	if (i != fieldsName.end()) {
		return i->second->colNo;
	}
	throw Error();
}

unsigned int
PQ::SelectCommand::columnCount() const
{
	return fields.size();
}