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

static std::string addrStr(void * p, unsigned int no) {
	std::string r;
	r.resize(50);
	r.resize(snprintf(const_cast<char *>(r.c_str()), 30, "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 tm * v)
{
	paramsAtLeast(n);
	values[n] = static_cast<char *>(malloc(20));
	formats[n] = 0;
	strftime(values[n], 20, "%F %T", v);
	lengths[n] = 19;
}
void
PQ::Command::bindParamT(unsigned int n, time_t v)
{
	struct tm t;
	gmtime_r(&v, &t);
	bindParamT(n, &t);
}
void
PQ::Command::bindNull(unsigned int n)
{
	paramsAtLeast(n);
}