 
 C: <field> field
 
-: odbc-get-field ( statement column -- field )
-  dup column? [ dupd odbc-describe-column ] unless dup >r number>>
-  SQL-C-DEFAULT
-  8192 CHAR: space <string> ascii string>alien dup >r
-  8192
-  f SQLGetData succeeded? [
-    r> r> [ dereference-type-pointer ] keep <field>
-  ] [
-    r> drop r> [
+:: odbc-get-field ( statement column! -- field )
+  column column? [ statement column odbc-describe-column column! ] unless
+  8192 :> bufferLen
+  bufferLen CHAR: space <string> ascii string>alien :> targetValuePtr
+
+  statement column number>> SQL-C-DEFAULT
+  targetValuePtr bufferLen f SQLGetData succeeded? [
+      targetValuePtr column [ dereference-type-pointer ] keep <field>
+  ] [
+    column [
       "SQLGetData Failed for Column: " %
       dup name>> %
       " of type: " % dup type>> name>> %

! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel alien alien.strings alien.syntax
combinators alien.c-types strings sequences namespaces make
words math threads io.encodings.ascii ;
IN: odbc

<< "odbc" "odbc32.dll" stdcall add-library >>

  SQLDisconnect succeeded? [ "odbc-disconnect failed" throw ] unless ;

: odbc-prepare ( dbc string -- statement )
  [ alloc-stmt-handle dup ] dip dup length SQLPrepare succeeded? [ "odbc-prepare failed" throw ] unless ;

: odbc-free-statement ( statement -- )
  SQL-HANDLE-STMT swap SQLFreeHandle succeeded? [ "odbc-free-statement failed" throw ] unless ;
