summaryrefslogtreecommitdiff
path: root/libpqpp/pq-command.cpp
blob: 1db81a69974006d3ad42dfebd0b08acf4588bfa1 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "pq-command.h"
#include "pq-connection.h"
#include <stdlib.h>
#include <string.h>
#include <compileTimeFormatter.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <factory.h>

NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory);

AdHocFormatter(PQCommondStatement, "pStatement_id%?");
PQ::Command::Command(Connection * conn, const std::string & sql, const DB::CommandOptions * opts) :
	DB::Command(sql),
	hash(opts && opts->hash ? *opts->hash : std::hash<std::string>()(sql)),
	stmntName(PQCommondStatement::get(hash)),
	c(conn)
{
}

PQ::Command::~Command()
{
	for (auto i = values.size(); i-- > 0;) {
		if (bufs[i]) {
			delete bufs[i];
		}
		else {
			free(values[i]);
		}
	}
}

PQ::CommandOptions::CommandOptions(std::size_t hash, const DB::CommandOptionsMap &) :
	DB::CommandOptions(hash)
{

}

PQ::CommandOptions::CommandOptions(std::size_t hash,
		unsigned int ft,
		bool uc) :
	DB::CommandOptions(hash),
	fetchTuples(ft),
	useCursor(uc)
{
}

AdHocFormatter(PQCommandParamName, "$%?");
void
PQ::Command::prepareSql(std::stringstream & psql, const std::string & sql) const
{
	if (values.empty()) {
		psql << sql;
		return;
	}
	int p = 1;
	bool inquote = false;
	for (const auto & i : sql) {
		if (i == '?' && !inquote) {
			PQCommandParamName::write(psql, p++);
		}
		else if (i == '\'') {
			inquote = !inquote;
			psql << i;
		}
		else {
			psql << i;
		}
	}
}

void
PQ::Command::paramsAtLeast(unsigned int n)
{
	if (values.size() <= n) {
		values.resize(n + 1, NULL);
		lengths.resize(n + 1, 0);
		bufs.resize(n + 1, NULL);
	}
	else {
		if (bufs[n]) {
			delete bufs[n];
			bufs[n] = nullptr;
		}
		else {
			free(values[n]);
		}
		values[n] = NULL;
	}
}

template<typename ... T>
void
PQ::Command::paramSet(unsigned int n, const char * fmt, const T & ... v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], fmt, v...);
	delete bufs[n];
	bufs[n] = nullptr;
}

void
PQ::Command::paramSet(unsigned int n, const std::string & b)
{
	paramsAtLeast(n);
	delete bufs[n];
	bufs[n] = new std::string(b);
	lengths[n] = b.length();
	values[n] = const_cast<char *>(bufs[n]->data());
}

void
PQ::Command::bindParamI(unsigned int n, int v)
{
	paramSet(n, "%d", v);
}
void
PQ::Command::bindParamI(unsigned int n, long int v)
{
	paramSet(n, "%ld", v);
}
void
PQ::Command::bindParamI(unsigned int n, long long int v)
{
	paramSet(n, "%lld", v);
}
void
PQ::Command::bindParamI(unsigned int n, unsigned int v)
{
	paramSet(n, "%u", v);
}
void
PQ::Command::bindParamI(unsigned int n, long unsigned int v)
{
	paramSet(n, "%lu", v);
}
void
PQ::Command::bindParamI(unsigned int n, long long unsigned int v)
{
	paramSet(n, "%llu", v);
}
void
PQ::Command::bindParamB(unsigned int n, bool v)
{
	paramSet(n, "%s", v ? "true" : "false");
}
void
PQ::Command::bindParamF(unsigned int n, double v)
{
	paramSet(n, "%g", v);
}
void
PQ::Command::bindParamF(unsigned int n, float v)
{
	paramSet(n, "%g", v);
}
void
PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s)
{
	paramSet(n, s);
}
void
PQ::Command::bindParamT(unsigned int n, const boost::posix_time::time_duration & v)
{
	paramSet(n, boost::posix_time::to_simple_string(v));
}
void
PQ::Command::bindParamT(unsigned int n, const boost::posix_time::ptime & v)
{
	paramSet(n, boost::posix_time::to_iso_extended_string(v));
}
void
PQ::Command::bindNull(unsigned int n)
{
	paramsAtLeast(n);
}