summaryrefslogtreecommitdiff
path: root/libpqpp/pq-bulkselectcommand.cpp
blob: 27674f5fabfdd4d72b7f5ac36ba3f4a697067af3 (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
#include "pq-bulkselectcommand.h"
#include "pq-connection.h"
#include "pq-column.h"
#include "pq-error.h"

PQ::BulkSelectCommand::BulkSelectCommand(Connection * conn, const std::string & sql, unsigned int no) :
	DB::Command(sql),
	DB::SelectCommand(sql),
	PQ::Command(conn, sql, no),
	executed(false)
{
	prepareSql(preparedSql, sql);
}

PQ::BulkSelectCommand::~BulkSelectCommand()
{
	if (execRes) {
		PQclear(execRes);
	}
}

void
PQ::BulkSelectCommand::execute()
{
	if (!executed) {
		execRes = c->checkResult(
				PQexecParams(c->conn, preparedSql.c_str(), values.size(), NULL, &values.front(), &lengths.front(), NULL, 0),
				PGRES_TUPLES_OK);
		nTuples = PQntuples(execRes);
		tuple = -1;
		unsigned int nFields = PQnfields(execRes);
		for (unsigned int f = 0; f < nFields; f += 1) {
			insertColumn(DB::ColumnPtr(new Column(this, f)));
		}
		executed = true;
	}
}

bool
PQ::BulkSelectCommand::fetch()
{
	execute();
	if (++tuple < nTuples) {
		return true;
	}
	else {
		PQclear(execRes);
		execRes = NULL;
		executed = false;
		return false;
	}
}