MySQL and Unicode-enabled SBCL (patch)

Espen Wiborg <espenhw-Rm1MVgtoMKXfjOZ/[email protected]>
Newsgroups gmane.lisp.clsql.devel
Message-ID <[email protected]>
The attached patch fixes the issue where CLSQL sends the wrong length
of a query to MySQL on a Unicode-enabled SBCL if it contains non-ASCII
characters.

Given that UFFI treats all strings as UTF-8 coded if the feature
:sb-unicode is present, this should be quite safe.

Other backends may need similar patches; I haven't looked.


-- 
Espen Wiborg <espenhw-Rm1MVgtoMKXfjOZ/[email protected]>
I know you believe you understand what you think I said.  But I am not
sure you realize that what you heard is not what I meant.  -- Anon.

_______________________________________________
CLSQL-Devel mailing list
[email protected]
http://lists.b9.com/mailman/listinfo/clsql-devel
clsql.patch (text/x-patch, 2.1 KB)
--- /home/espenhw/clsql-3.1.15/db-mysql/mysql-sql.lisp	2005-04-06 17:58:10.000000000 +0200
+++ mysql-sql.lisp	2005-06-06 15:43:45.896009879 +0200
@@ -20,6 +20,12 @@
 
 (in-package #:clsql-mysql)
 
+;; if we have :sb-unicode, UFFI will treat :cstring as a UTF-8 string
+(defun expression-length (query-expression)
+  (length #+sb-unicode (sb-ext:string-to-octets query-expression
+						:external-format :utf8)
+	  #-sb-unicode query-expression))
+
 ;;; Field conversion functions
 
 (defun result-field-names (num-fields res-ptr)
@@ -169,7 +175,7 @@
   (let ((mysql-ptr (database-mysql-ptr database)))
     (uffi:with-cstring (query-native query-expression)
       (if (zerop (mysql-real-query mysql-ptr query-native 
-                                   (length query-expression)))
+                                   (expression-length query-expression)))
 	  (let ((res-ptr (mysql-use-result mysql-ptr)))
 	    (if res-ptr
 		(unwind-protect
@@ -215,7 +221,7 @@
     (let ((mysql-ptr (database-mysql-ptr database)))
       (declare (type mysql-mysql-ptr-def mysql-ptr))
       (if (zerop (mysql-real-query mysql-ptr sql-native 
-                                   (length sql-expression)))
+                                   (expression-length sql-expression)))
 	  t
 	(error 'sql-database-data-error
 	       :database database
@@ -238,7 +244,7 @@
     (let ((mysql-ptr (database-mysql-ptr database)))
      (declare (type mysql-mysql-ptr-def mysql-ptr))
       (if (zerop (mysql-real-query mysql-ptr query-native
-                                   (length query-expression)))
+                                   (expression-length query-expression)))
 	  (let ((res-ptr (if full-set
 			     (mysql-store-result mysql-ptr)
 			   (mysql-use-result mysql-ptr))))
@@ -507,7 +513,7 @@
 	     :message (mysql-error-string mysql-ptr)))
 
     (uffi:with-cstring (native-query sql-stmt)
-      (unless (zerop (mysql-stmt-prepare stmt native-query (length sql-stmt)))
+      (unless (zerop (mysql-stmt-prepare stmt native-query (expression-length sql-stmt)))
 	(mysql-stmt-close stmt)
 	(error 'sql-database-error
 	       :error-id (mysql-errno mysql-ptr)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.