File: manip.cpp

package info (click to toggle)
mysql%2B%2B 3.0.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 10,328 kB
  • ctags: 9,487
  • sloc: cpp: 33,486; sh: 3,091; perl: 809; makefile: 683
file content (253 lines) | stat: -rw-r--r-- 6,219 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***********************************************************************
 manip.cpp - Implements MySQL++'s various quoting/escaping stream
	manipulators.

 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
 (c) 2004-2007 by Educational Technology Resources, Inc.  Others may
 also hold copyrights on code in this file.  See the CREDITS file in
 the top directory of the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#include "manip.h"

#include "query.h"

using namespace std;

namespace mysqlpp {

SQLQueryParms&
operator <<(quote_type2 p, SQLTypeAdapter& in)
{
	if (in.quote_q()) {
		string temp("'", 1), escaped;
		p.qparms->escape_string(&escaped, in.data(), in.length());
		temp.append(escaped);
		temp.append("'", 1);
		*p.qparms << SQLTypeAdapter(temp, true);
		return *p.qparms;
	}
	else {
		in.set_processed();
		return *p.qparms << in;
	}
}


ostream&
operator <<(quote_type1 o, const SQLTypeAdapter& in)
{
	Query* pq = dynamic_cast<Query*>(o.ostr);

	if (pq && in.quote_q()) o.ostr->put('\'');

	if (pq) {
		// It's a Query stream, so we'll be using unformatted output.
		// Now, is escaping appropriate for source data type of 'in'?
		if (in.escape_q()) {
			string escaped;
			pq->escape_string(&escaped, in.data(), in.length());
			o.ostr->write(escaped.data(), escaped.length());
		}
		else {
			o.ostr->write(in.data(), in.length());
		}
	}
	else {
		// Some other stream type, so use formatted output.  User
		// shouldn't be trying to use the quote manipulator, but
		// that's no reason to break their formatting.
		*o.ostr << string(in.data(), in.length());
	}

	if (pq && in.quote_q()) o.ostr->put('\'');

	return *o.ostr;
}


ostream&
operator <<(quote_only_type1 o, const SQLTypeAdapter& in)
{
	Query* pq = dynamic_cast<Query*>(o.ostr);

	if (pq && in.quote_q()) o.ostr->put('\'');

	if (pq) {
		// It's a Query stream, so use unformatted output
		o.ostr->write(in.data(), in.length());
	}
	else {
		// Some other stream type, so use formatted output.  User
		// shouldn't be trying to use this manipulator on a non-Query
		// stream, but that's no reason to break their formatting.
		*o.ostr << '\'' << in << '\'';
	}

	if (pq && in.quote_q()) o.ostr->put('\'');

	return *o.ostr;
}


ostream&
operator <<(ostream& o, const SQLTypeAdapter& in)
{
	if (dynamic_cast<Query*>(&o)) {
		// It's a Query stream, so use unformatted output.
		return o.write(in.data(), in.length());
	}
	else {
		// Some other stream type, so use formatted output.  We do this
		// through the temporary so we remain null-friendly.
		return o << string(in.data(), in.length());
	}
}


SQLQueryParms&
operator <<(quote_only_type2 p, SQLTypeAdapter& in)
{
	if (in.quote_q()) {
		string temp("'", 1);
		temp.append(in.data(), in.length());
		temp.append("'", 1);
		return *p.qparms << SQLTypeAdapter(temp, true);
	}
	else {
		in.set_processed();
		return *p.qparms << in;
	}
}


SQLQueryParms&
operator <<(quote_double_only_type2 p, SQLTypeAdapter& in)
{
	if (in.quote_q()) {
		string temp("\"", 1);
		temp.append(in.data(), in.length());
		temp.append("\"", 1);
		return *p.qparms << SQLTypeAdapter(temp, true);
	}
	else {
		in.set_processed();
		return *p.qparms << in;
	}
}


ostream&
operator <<(quote_double_only_type1 o, const SQLTypeAdapter& in)
{
	Query* pq = dynamic_cast<Query*>(o.ostr);

	if (pq && in.quote_q()) o.ostr->put('"');

	if (pq) {
		// It's a Query stream, so use unformatted output
		o.ostr->write(in.data(), in.length());
	}
	else {
		// Some other stream type, so use formatted output.  User
		// shouldn't be trying to use this manipulator on a non-Query
		// stream, but that's no reason to break their formatting.
		*o.ostr << '"' << in << '"';
	}

	if (pq && in.quote_q()) o.ostr->put('"');

	return *o.ostr;
}


SQLQueryParms&
operator <<(escape_type2 p, SQLTypeAdapter& in)
{
	if (in.escape_q()) {
		string escaped;
		p.qparms->escape_string(&escaped, in.data(), in.length());
		*p.qparms << SQLTypeAdapter(escaped, true);
		return *p.qparms;
	}
	else {
		in.set_processed();
		return *p.qparms << in;
	}
}


ostream&
operator <<(escape_type1 o, const SQLTypeAdapter& in)
{
	Query* pq = dynamic_cast<Query*>(o.ostr);
	if (pq) {
		// It's a Query stream, so we'll be using unformatted output.
		// Now, is escaping appropriate for source data type of 'in'?
		if (in.escape_q()) {
			string escaped;
			pq->escape_string(&escaped, in.data(), in.length());
			return o.ostr->write(escaped.data(), escaped.length());
		}
		else {
			return o.ostr->write(in.data(), in.length());
		}
	}
	else {
		// Some other stream type, so use formatted output.  User
		// shouldn't be trying to use the escape manipulator, but
		// that's no reason to break their formatting.
		return *o.ostr << string(in.data(), in.length());
	}
}


SQLQueryParms&
operator <<(do_nothing_type2 p, SQLTypeAdapter& in)
{
	in.set_processed();
	return *p.qparms << in;
}


ostream&
operator <<(do_nothing_type1 o, const SQLTypeAdapter& in)
{
	if (dynamic_cast<Query*>(o.ostr)) {
		// It's a Query stream, so use unformatted output
		return o.ostr->write(in.data(), in.length());
	}
	else {
		// Some other stream type, so use formatted output.  User
		// shouldn't be trying to use this manipulator on a non-Query
		// stream, but that's no reason to break their formatting.
		return *o.ostr << in;
	}
}


SQLQueryParms&
operator <<(ignore_type2 p, SQLTypeAdapter& in)
{
	return *p.qparms << in;
}

} // end namespace mysqlpp