summaryrefslogtreecommitdiff
path: root/libpqpp/command.cpp
blob: abd37e6eade60b104f9e86a5216646696ca6e5da (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 "command.h"
#include "connection.h"
#include <stdlib.h>
#include <string.h>
#include <boost/date_time/posix_time/posix_time.hpp>

static std::string addrStr(void * p, unsigned int no) {
	std::string r(50, ' ');
	r.resize(snprintf(const_cast<char *>(r.c_str()), r.length(), "pStatement_%u_%p", no, p));
	return r;
}

PQ::Command::Command(const Connection * conn, const std::string & sql, unsigned int no) :
	DB::Command(sql),
	stmntName(addrStr(this, no)),
	c(conn)
{
}

PQ::Command::~Command()
{
	for (std::vector<char *>::const_iterator i = values.begin(); i != values.end(); ++i) {
		free(*i);
	}
}

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

void
PQ::Command::bindParamI(unsigned int n, int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%d", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamI(unsigned int n, long int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%ld", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamI(unsigned int n, long long int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%lld", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamI(unsigned int n, unsigned int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%u", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamI(unsigned int n, long unsigned int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%lu", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamI(unsigned int n, long long unsigned int v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%llu", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamF(unsigned int n, double v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%g", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamF(unsigned int n, float v)
{
	paramsAtLeast(n);
	lengths[n] = asprintf(&values[n], "%g", v);
	formats[n] = 0;
}
void
PQ::Command::bindParamS(unsigned int n, const Glib::ustring & s)
{
	paramsAtLeast(n);
	values[n] = strndup(s.c_str(), s.bytes());
	formats[n] = 0;
	lengths[n] = s.bytes();
}
void
PQ::Command::bindParamT(unsigned int n, const boost::posix_time::time_duration & v)
{
	paramsAtLeast(n);
	auto buf = boost::posix_time::to_simple_string(v);
	values[n] = strdup(buf.c_str());
	formats[n] = 0;
	lengths[n] = buf.length();
}
void
PQ::Command::bindParamT(unsigned int n, const boost::posix_time::ptime & v)
{
	paramsAtLeast(n);
	auto buf = boost::posix_time::to_iso_extended_string(v);
	values[n] = strdup(buf.c_str());
	formats[n] = 0;
	lengths[n] = buf.length();
}
void
PQ::Command::bindNull(unsigned int n)
{
	paramsAtLeast(n);
}