Date: Friday, March 3, 2006 @ 13:39:34
Author: csaba
Path: /cvsroot/carob/libmysequoia
Modified: include/CarobMySQL.hpp (1.28 -> 1.29) src/CarobMySQL.cpp (1.58
-> 1.59) src/Converter.cpp (1.1 -> 1.2) src/MySQLAPI.cpp (1.49
-> 1.50)
mysql_real_escape_string() take in account the character set when escaping the
string.
------------------------+
include/CarobMySQL.hpp | 11 +++++++++++
src/CarobMySQL.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++
src/Converter.cpp | 6 ++----
src/MySQLAPI.cpp | 33 ++++++++++++++++++---------------
4 files changed, 73 insertions(+), 19 deletions(-)
Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.28
libmysequoia/include/CarobMySQL.hpp:1.29
--- libmysequoia/include/CarobMySQL.hpp:1.28 Fri Mar 3 10:57:48 2006
+++ libmysequoia/include/CarobMySQL.hpp Fri Mar 3 13:39:34 2006
@@ -233,6 +233,17 @@
*/
virtual std::wstring to_wstring(const std::string &s);
+ /**
+ * Escapes special characters in a string for use in an SQL statement,
+ * taking into account the current charset of the connection.
+ * @param to pointer to the converted string
+ * @param from pointer to the string to be converted
+ * @param length lenght of the string to convert
+ * @return length of the new string if success
+ * -1 if an error occured
+ */
+ unsigned long real_escape_string (char *to, const char *from, unsigned long
length);
+
private:
//pointer to a MYSQL structure which is connected to this class
MYSQL * mysqlPtr;
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.58
libmysequoia/src/CarobMySQL.cpp:1.59
--- libmysequoia/src/CarobMySQL.cpp:1.58 Fri Mar 3 10:57:48 2006
+++ libmysequoia/src/CarobMySQL.cpp Fri Mar 3 13:39:34 2006
@@ -1079,3 +1079,45 @@
{
return conv.to_wstring(s);
}
+
+unsigned long
+CarobMYSQL::real_escape_string (char *to, const char *from, unsigned long
length)
+{
+ wstring w = to_wstring(string(from, length));
+ LOG4CXX_DEBUG(logger, "alma0 " << from << ":" << length);
+ LOG4CXX_DEBUG(logger, "alma1 " << from_wstring(w));
+ wstring r;
+ std::wstring::const_iterator i = w.begin();
+ wchar_t esc;
+
+ for (; i != w.end(); i++)
+ {
+ esc = 0;
+ switch (*i)
+ {
+ case L'\0':
+ case L'\n':
+ case L'\r':
+ case L'\\':
+ case L'\'':
+ case L'"': esc = *i; break;
+ case L'\032': esc = L'Z'; break;
+ }
+ if (esc)
+ {
+ r.push_back(L'\\');
+ r.push_back(esc);
+ }
+ else
+ {
+ r.push_back(*i);
+ }
+ }
+ r.push_back(L'\0');
+
+ LOG4CXX_DEBUG(logger, "alma22 " << r.length());
+ string s = from_wstring(r);
+ LOG4CXX_DEBUG(logger, "alma2 " << s << ":" << s.length());
+ memcpy(to, s.data(), s.length() < 2*length ? s.length() : 2*length);
+ return (s.length() < 2*length) ? (unsigned long) (s.length()) : (unsigned
long) ~0;
+}
Index: libmysequoia/src/Converter.cpp
diff -u libmysequoia/src/Converter.cpp:1.1 libmysequoia/src/Converter.cpp:1.2
--- libmysequoia/src/Converter.cpp:1.1 Thu Mar 2 17:23:24 2006
+++ libmysequoia/src/Converter.cpp Fri Mar 3 13:39:34 2006
@@ -101,8 +101,7 @@
break;
}
}
- *((wchar_t *)outbuf) = L'\0';
- result += convertbuf;
+ result.append(convertbuf,(wchar_t *)outbuf - convertbuf);
}
return result;
@@ -144,8 +143,7 @@
break;
}
}
- *outbuf = '\0';
- result += convertbuf;
+ result.append(convertbuf,outbuf - convertbuf);
}
return result;
Index: libmysequoia/src/MySQLAPI.cpp
diff -u libmysequoia/src/MySQLAPI.cpp:1.49 libmysequoia/src/MySQLAPI.cpp:1.50
--- libmysequoia/src/MySQLAPI.cpp:1.49 Tue Feb 28 11:53:33 2006
+++ libmysequoia/src/MySQLAPI.cpp Fri Mar 3 13:39:34 2006
@@ -834,21 +834,6 @@
{
LOG4CXX_DEBUG(logger, "Entering mysql_escape_string: to=" << to << " from="
<< from << "length=" << length);
- unsigned long result = mysql_real_escape_string(0, to, from, length);
-
- LOG4CXX_DEBUG(logger, "Leaving mysql_escape_string: result=" << result);
- return result;
-}
-
-/* Escapes special characters in a string for use in an SQL statement,
- * taking into account the current charset of the connection. */
-unsigned long STDCALL
-mysql_real_escape_string (MYSQL * mysql, char *to, const char *from, unsigned
long length)
-{
- LOG4CXX_DEBUG(logger, "Entering mysql_real_escape_string: mysql=" << mysql
<< " to=" << to << " from=" << from << "length=" << length);
-
- // TODO use the encoding found in the mysql parameter
-
unsigned long result = 0;
char *start = to;
char *end = to + 2*length;
@@ -897,6 +882,24 @@
*to = 0;
result = (result == 0) ? (unsigned long) (to - start) : (unsigned long) ~0;
+
+ LOG4CXX_DEBUG(logger, "Leaving mysql_escape_string: result=" << result);
+ return result;
+}
+
+/* Escapes special characters in a string for use in an SQL statement,
+ * taking into account the current charset of the connection. */
+unsigned long STDCALL
+mysql_real_escape_string (MYSQL * mysql, char *to, const char *from, unsigned
long length)
+{
+ LOG4CXX_DEBUG(logger, "Entering mysql_real_escape_string: mysql=" << mysql
<< " to=" << to << " from=" << from << "length=" << length);
+
+ unsigned long result;
+
+ if (isCarobObject(mysql))
+ result = getCarob(mysql)->real_escape_string(to, from, length);
+ else
+ result = mysql_escape_string(to, from, length);
LOG4CXX_DEBUG(logger, "Leaving mysql_real_escape_string: result=" << result);
return result;
_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits