File: manip.cpp

package info (click to toggle)
mysql%2B%2B 3.2.5-2.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 18,360 kB
  • sloc: cpp: 35,788; sh: 3,693; perl: 789; makefile: 730
file content (284 lines) | stat: -rw-r--r-- 7,196 bytes parent folder | download | duplicates (6)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/***********************************************************************
 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.txt 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"
#include "sqlstream.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 it's not a Query*, maybe it's a SQLStream*.
	SQLStream* psqls = pq ? 0 : dynamic_cast<SQLStream*>(o.ostr);

	// If it's a Query or a SQLStream, we'll be using unformatted output.
	if (pq || psqls) {
		if (in.quote_q()) o.ostr->put('\'');

		// Now, is escaping appropriate for source data type of 'in'?
		if (in.escape_q()) {
			string escaped;

			// If it's not a Query*, then it has to be a SQLStream.
			if (pq) {
				pq->escape_string(&escaped, in.data(), in.length());
			}
			else {
				psqls->escape_string(&escaped, in.data(), in.length());
			}

			o.ostr->write(escaped.data(), escaped.length());
		}
		else {
			o.ostr->write(in.data(), in.length());
		}

		if (in.quote_q()) o.ostr->put('\'');
	}
	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());
	}

	return *o.ostr;
}


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

	// If it's not a Query*, maybe it's a SQLStream*.
	SQLStream* psqls = pq ? 0 : dynamic_cast<SQLStream*>(o.ostr);

	// If it's a Query or SQLStream, use unformatted output
	if (pq || psqls) {
		if (in.quote_q()) o.ostr->put('\'');

		o.ostr->write(in.data(), in.length());

		if (in.quote_q()) o.ostr->put('\'');
	}
	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 << '\'';
	}

	return *o.ostr;
}


ostream&
operator <<(ostream& o, const SQLTypeAdapter& in)
{
	if (dynamic_cast<Query*>(&o) || dynamic_cast<SQLStream*>(&o)) {
		// It's a Query or a SQLStream, 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 it's not a Query*, maybe it's a SQLStream*.
	SQLStream* psqls = pq ? 0 : dynamic_cast<SQLStream*>(o.ostr);

	// If it's a Query or a SQLStream, use unformatted output
	if (pq || psqls) {
		if (in.quote_q()) o.ostr->put('"');

		o.ostr->write(in.data(), in.length());
	
		if (in.quote_q()) o.ostr->put('"');
	}
	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 << '"';
	}

	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 it's not a Query*, maybe it's a SQLStream*.
	SQLStream* psqls = pq ? 0 : dynamic_cast<SQLStream*>(o.ostr);

	if (pq || psqls) {
		// It's a Query or a SQLStream, so we'll be using unformatted output.
		// Now, is escaping appropriate for source data type of 'in'?
		if (in.escape_q()) {
			string escaped;

			// If it's not a Query*, then it has to be a SQLStream.
			if (pq) {
				pq->escape_string(&escaped, in.data(), in.length());
			}
			else {
				psqls->escape_string(&escaped, in.data(), in.length());
			}

			return o.ostr->write(escaped.data(), escaped.length());
		}
		else {
			// It's not escaped, so just write the 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 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) || dynamic_cast<SQLStream*>(o.ostr)) {
		// It's a Query or a SQLStream, 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