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
|
#include "pq-command.h"
#include "pq-connection.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <compileTimeFormatter.h>
#include <cstdlib>
#include <cstring>
#include <factory.h>
NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory)
AdHocFormatter(PQCommondStatement, "pStatement_id%?");
PQ::Command::Command(Connection * conn, const std::string & sql, const DB::CommandOptionsCPtr & opts) :
DB::Command(sql), hash(opts && opts->hash ? *opts->hash : std::hash<std::string>()(sql)),
stmntName(PQCommondStatement::get(hash)), c(conn)
{
}
PQ::CommandOptions::CommandOptions(std::size_t hash, const DB::CommandOptionsMap & map) :
DB::CommandOptions(hash), fetchTuples(get(map, "page-size", 35U)), useCursor(!isSet(map, "no-cursor")),
fetchBinary(isSet(map, "fetch-binary"))
{
}
PQ::CommandOptions::CommandOptions(std::size_t hash, unsigned int ft, bool uc, bool fb) :
DB::CommandOptions(hash), fetchTuples(ft), useCursor(uc), fetchBinary(fb)
{
}
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, nullptr);
lengths.resize(n + 1, 0);
formats.resize(n + 1, 0);
bufs.resize(n + 1);
}
}
AdHocFormatter(PQCommandParamFmt, "%?");
template<typename... T>
void
PQ::Command::paramSet(unsigned int n, const T &... v)
{
paramsAtLeast(n);
bufs[n] = std::make_unique<std::string>(PQCommandParamFmt::get(v...));
lengths[n] = static_cast<int>(bufs[n]->length());
formats[n] = 0;
values[n] = bufs[n]->data();
}
void
PQ::Command::paramSet(unsigned int n, const std::string_view & b)
{
paramsAtLeast(n);
bufs[n] = std::make_unique<std::string>(b);
lengths[n] = static_cast<int>(b.length());
formats[n] = 0;
values[n] = bufs[n]->data();
}
void
PQ::Command::bindParamI(unsigned int n, int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamI(unsigned int n, long int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamI(unsigned int n, long long int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamI(unsigned int n, unsigned int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamI(unsigned int n, long unsigned int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamI(unsigned int n, long long unsigned int v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamB(unsigned int n, bool v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamF(unsigned int n, double v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamF(unsigned int n, float v)
{
paramSet(n, v);
}
void
PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s)
{
paramSet(n, std::string_view(s.data(), s.bytes()));
}
void
PQ::Command::bindParamS(unsigned int n, const std::string_view & 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::bindParamBLOB(unsigned int n, const DB::Blob & v)
{
paramsAtLeast(n);
lengths[n] = static_cast<int>(v.len);
formats[n] = 1;
values[n] = static_cast<const char *>(v.data);
bufs[n].reset();
}
void
PQ::Command::bindNull(unsigned int n)
{
paramsAtLeast(n);
lengths[n] = 0;
formats[n] = 0;
values[n] = nullptr;
bufs[n].reset();
}
|