ODBC BIGINT hacking

Nathan Bird <[email protected]> Fri, 15 Jun 2007 18:38:30 -0400
Newsgroups gmane.lisp.clsql.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------000102070503040208060104
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

BIGINTs on ODBC didn't work for me. I hacked for a while and now it
does, but you probably won't like it. With this it actually reads
bigints as 8 byte signed integers instead of trying to read them in as a
string (which left me with the first half of the number).  I think this
has something to do with the way freetds or sqlserver structures that
column. It looks like bignum on odbc->postgres did work, it still works
in this patch.

The downside: I didn't figure out how to make it work with uffi. This
uses :long-long out of the cffi-uffi-compat library.  If someone can
figure out a comparable item in uffi, or can teach uffi then this might
be more useful, as is I'm posting it for reference as much as anything.

I also included a patch for switch from uffi to the cffi-uffi-compat
library if anyone with this problem (I have a feeling there aren't many
of us:-) is interested in trying that.

Nathan Bird

PS: and with that, all the local patches i have are submitted! Cheers,
and thanks for the great library.

--------------000102070503040208060104
Content-Type: text/plain;
 name="uffi-cffi-swap.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="uffi-cffi-swap.patch"

Thu Jun 14 16:28:20 EDT 2007  Nathan Bird <[email protected]>
  * switiching to use cffi-uffi-compat instead of uffi
diff -rN -u old-clsql/clsql-mysql.asd new-clsql/clsql-mysql.asd
--- old-clsql/clsql-mysql.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql-mysql.asd	2007-06-15 18:35:51.000000000 -0400
@@ -80,7 +80,7 @@
   :description "Common Lisp SQL MySQL Driver"
   :long-description "cl-sql-mysql package provides a database driver to the MySQL database system."
 
-  :depends-on (uffi clsql clsql-uffi)
+  :depends-on (cffi-uffi-compat clsql clsql-uffi)
   :components
   ((:module :db-mysql
 	    :components
diff -rN -u old-clsql/clsql-odbc.asd new-clsql/clsql-odbc.asd
--- old-clsql/clsql-odbc.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql-odbc.asd	2007-06-15 18:35:51.000000000 -0400
@@ -27,7 +27,7 @@
   :description "Common Lisp SQL ODBC Driver"
   :long-description "cl-sql-odbc package provides a database driver to the ODBC database system."
 
-  :depends-on (uffi clsql clsql-uffi)
+  :depends-on (cffi-uffi-compat clsql clsql-uffi)
   :components
   ((:module :db-odbc
 	    :components
diff -rN -u old-clsql/clsql-postgresql-socket.asd new-clsql/clsql-postgresql-socket.asd
--- old-clsql/clsql-postgresql-socket.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql-postgresql-socket.asd	2007-06-15 18:35:51.000000000 -0400
@@ -29,7 +29,7 @@
   :description "Common Lisp SQL PostgreSQL Socket Driver"
   :long-description "cl-sql-postgresql-socket package provides a database driver to the PostgreSQL database via a socket interface."
 
-  :depends-on (clsql uffi md5 #+sbcl sb-bsd-sockets)
+  :depends-on (clsql cffi-uffi-compat md5 #+sbcl sb-bsd-sockets)
   :components
   ((:module :db-postgresql-socket
 	    :components
diff -rN -u old-clsql/clsql-postgresql.asd new-clsql/clsql-postgresql.asd
--- old-clsql/clsql-postgresql.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql-postgresql.asd	2007-06-15 18:35:51.000000000 -0400
@@ -29,7 +29,7 @@
   :description "Common Lisp PostgreSQL API Driver"
   :long-description "cl-sql-postgresql package provides a the database driver for the PostgreSQL API."
 
-  :depends-on (uffi clsql clsql-uffi)
+  :depends-on (cffi-uffi-compat clsql clsql-uffi)
   :components
   ((:module :db-postgresql
 	    :components
diff -rN -u old-clsql/clsql-uffi.asd new-clsql/clsql-uffi.asd
--- old-clsql/clsql-uffi.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql-uffi.asd	2007-06-15 18:35:51.000000000 -0400
@@ -79,7 +79,7 @@
   :description "Common UFFI Helper functions for Common Lisp SQL Interface Library"
   :long-description "cl-sql-uffi package provides common helper functions using the UFFI for the CLSQL package."
 
-  :depends-on (uffi clsql)
+  :depends-on (cffi-uffi-compat clsql)
 
   :components
   ((:module :uffi
diff -rN -u old-clsql/clsql.asd new-clsql/clsql.asd
--- old-clsql/clsql.asd	2007-06-15 18:35:51.000000000 -0400
+++ new-clsql/clsql.asd	2007-06-15 18:35:51.000000000 -0400
@@ -27,7 +27,7 @@
 ;; need to load uffi for below perform :after method
 (eval-when (:compile-toplevel :load-toplevel :execute)
   (unless (find-package 'uffi)
-    (asdf:operate 'asdf:load-op 'uffi)))
+    (asdf:operate 'asdf:load-op 'cffi-uffi-compat)))
 
 (defsystem clsql
     :name "CLSQL"


--------------000102070503040208060104
Content-Type: text/plain;
 name="odbc-bigint.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="odbc-bigint.patch"

Thu Jun 14 15:25:43 EDT 2007  Nathan Bird <[email protected]>
  * Implementing bigint support for odbc + mssql
diff -rN -u old-clsql/db-odbc/odbc-api.lisp new-clsql/db-odbc/odbc-api.lisp
--- old-clsql/db-odbc/odbc-api.lisp	2007-06-15 17:57:28.000000000 -0400
+++ new-clsql/db-odbc/odbc-api.lisp	2007-06-15 17:57:28.000000000 -0400
@@ -240,7 +240,7 @@
     (SQLTransact
      henv hdbc $SQL_ROLLBACK)))
 
-; col-nr is zero-based in Lisp
+; col-nr is zero-based in Lisp but 1 based in sql
 ; col-nr = :bookmark retrieves a bookmark.
 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
   (with-error-handling
@@ -494,6 +494,7 @@
 	   (deref-pointer column-nullable-p-ptr :short)))))))
 
 ;; parameter counting is 1-based
+;; this function isn't used, which is good because FreeTDS dosn't support it.
 (defun %describe-parameter (hstmt parameter-nr)
   (with-foreign-objects ((column-sql-type-ptr :short)
 			 (column-precision-ptr #.$ODBC-ULONG-TYPE)
@@ -584,9 +585,10 @@
 (defun sql-to-c-type (sql-type)
   (ecase sql-type
     ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
-      #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9 -10) $SQL_C_CHAR) ;; Added -10 for MSSQL ntext type
+      #.$SQL_NUMERIC #.$SQL_DECIMAL -8 -9 -10) $SQL_C_CHAR) ;; Added -10 for MSSQL ntext type
     (#.$SQL_INTEGER $SQL_C_SLONG)
     (#.$SQL_SMALLINT $SQL_C_SSHORT)
+    (#.$SQL_BIGINT $SQL_C_SBIGINT)
     (#.$SQL_DOUBLE $SQL_C_DOUBLE)
     (#.$SQL_FLOAT $SQL_C_DOUBLE)
     (#.$SQL_REAL $SQL_C_FLOAT)
@@ -604,6 +606,7 @@
 (def-type short-pointer-type (* :short))
 (def-type int-pointer-type (* :int))
 (def-type long-pointer-type (* #.$ODBC-LONG-TYPE))
+(def-type big-pointer-type (* #.$ODBC-BIG-TYPE))
 (def-type float-pointer-type (* :float))
 (def-type double-pointer-type (* :double))
 (def-type string-pointer-type (* :unsigned-char))
@@ -624,6 +627,10 @@
   (locally (declare (type long-pointer-type ptr))
     (deref-pointer ptr #.$ODBC-LONG-TYPE)))
 
+(defun get-cast-big (ptr)
+  (locally (declare (type big-pointer-type ptr))
+    (deref-pointer ptr #.$ODBC-BIG-TYPE)))
+
 (defun get-cast-single-float (ptr)
   (locally (declare (type float-pointer-type ptr))
     (deref-pointer ptr :float)))
@@ -670,8 +677,7 @@
 		   (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
 		   (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
 		   (#.$SQL_INTEGER (get-cast-int data-ptr))
-		   (#.$SQL_BIGINT (read-from-string
-				   (get-cast-foreign-string data-ptr)))
+		   (#.$SQL_BIGINT (get-cast-big data-ptr))
 		   (#.$SQL_DECIMAL
 		    (let ((*read-base* 10))
 		      (read-from-string (get-cast-foreign-string data-ptr))))
@@ -741,6 +747,7 @@
             (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
             (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
             (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
+	    (#.$SQL_C_SBIGINT (uffi:allocate-foreign-object #.$ODBC-BIG-TYPE))
             (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
             (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
             (t
diff -rN -u old-clsql/db-odbc/odbc-constants.lisp new-clsql/db-odbc/odbc-constants.lisp
--- old-clsql/db-odbc/odbc-constants.lisp	2007-06-15 17:57:28.000000000 -0400
+++ new-clsql/db-odbc/odbc-constants.lisp	2007-06-15 17:57:28.000000000 -0400
@@ -21,6 +21,7 @@
 ;; on SuSE AMD64 9.0, unixODBC is compiled with with SQLLEN being 4 bytes long
 (defconstant $ODBC-LONG-TYPE :int)
 (defconstant $ODBC-ULONG-TYPE :unsigned-int)
+(defconstant $ODBC-BIG-TYPE :long-long)
 
 ;; (defconstant $ODBCVER	#x0210)
 
@@ -934,6 +935,7 @@
 (defconstant $SQL_C_BINARY $SQL_BINARY)
 (defconstant $SQL_C_BIT $SQL_BIT)
 (defconstant $SQL_C_TINYINT $SQL_TINYINT)
+(defconstant $SQL_C_SBIGINT (+ $SQL_BIGINT $SQL_SIGNED_OFFSET))
 (defconstant $SQL_C_SLONG (+ $SQL_C_LONG $SQL_SIGNED_OFFSET)) ;; SIGNED INTEGER
 (defconstant $SQL_C_SSHORT (+ $SQL_C_SHORT $SQL_SIGNED_OFFSET)) ;; SIGNED SMALLINT
 (defconstant $SQL_C_STINYINT (+ $SQL_TINYINT $SQL_SIGNED_OFFSET)) ;; SIGNED TINYINT
diff -rN -u old-clsql/db-odbc/odbc-dbi.lisp new-clsql/db-odbc/odbc-dbi.lisp
--- old-clsql/db-odbc/odbc-dbi.lisp	2007-06-15 17:57:28.000000000 -0400
+++ new-clsql/db-odbc/odbc-dbi.lisp	2007-06-15 17:57:28.000000000 -0400
@@ -454,22 +454,19 @@
     (setf computed-result-types (make-array column-count))
     (dotimes (i column-count)
       (setf (aref computed-result-types i) 
-	(cond
-	 ((consp result-types)
-	  (nth i result-types))
-	 ((eq result-types :auto)
-	  (if (eq (aref column-sql-types i) odbc::$SQL_BIGINT)
-	      :number
-	    (case (aref column-c-types i)
-	      (#.odbc::$SQL_C_SLONG :int)
-	      (#.odbc::$SQL_C_DOUBLE :double)
-	      (#.odbc::$SQL_C_FLOAT :float)
-	      (#.odbc::$SQL_C_SSHORT :short)
-	      (#.odbc::$SQL_C_STINYINT :short)
-	      (#.odbc::$SQL_BIGINT :short)
-	      (t t))))
-	  (t
-	   t)))))
+	    (cond
+	      ((consp result-types)
+	       (nth i result-types))
+	      ((eq result-types :auto)
+	       (case (aref column-c-types i)
+		 (#.odbc::$SQL_C_SLONG :int)
+		 (#.odbc::$SQL_C_DOUBLE :double)
+		 (#.odbc::$SQL_C_FLOAT :float)
+		 (#.odbc::$SQL_C_SSHORT :short)
+		 (#.odbc::$SQL_C_STINYINT :short)
+		 (#.odbc::$SQL_C_SBIGINT #.odbc::$ODBC-BIG-TYPE)
+		 (t t)))
+	      (t t)))))
   query)
 
 (defun db-close-query (query &key drop-p)
@@ -564,7 +561,8 @@
 (defun sql-to-lisp-type (sql-type)
   (ecase sql-type
     ((#.odbc::$SQL_CHAR #.odbc::$SQL_VARCHAR #.odbc::$SQL_LONGVARCHAR) :string)
-    ((#.odbc::$SQL_NUMERIC #.odbc::$SQL_DECIMAL #.odbc::$SQL_BIGINT) :string) ; ??
+    ((#.odbc::$SQL_NUMERIC #.odbc::$SQL_DECIMAL ) :string) ; ??
+    (#.odbc::$SQL_BIGINT #.odbc::$ODBC-BIG-TYPE)
     (#.odbc::$SQL_INTEGER #.odbc::$ODBC-LONG-TYPE)
     (#.odbc::$SQL_SMALLINT :short)
     ((#.odbc::$SQL_FLOAT #.odbc::$SQL_DOUBLE) #.odbc::$ODBC-LONG-TYPE)


--------------000102070503040208060104
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
CLSQL-Devel mailing list
[email protected]
http://lists.b9.com/mailman/listinfo/clsql-devel

--------------000102070503040208060104--