Howdy,<br><br>With the recent chatter about MySQL on this list and some dabbling I've been doing with FFI (<a href="http://benjisimon.blogspot.com/2008/11/small-experiment-with-mzschemes-foriegn.html">http://benjisimon.blogspot.com/2008/11/small-experiment-with-mzschemes-foriegn.html</a>), I thought, what the heck, why not whip up a quick FFI interface to mysql.<br>
<br>I started in on it, and I've run into two major issues:<br><br>1) the MySQL C api (<a href="http://dev.mysql.com/doc/refman/5.0/en/c.html">http://dev.mysql.com/doc/refman/5.0/en/c.html</a>) is easy to use. I'm able to get access to the C function call <i>mysql_real_connect</i>. If I hand it invalid connection information, it gracefully fails like I want it to. It calls the C function <i>mysql_error()</i> which is supposed to return back char*. However, I get the following message from DrScheme:<br>
<br> bytes->string/utf8: string not well-formed UTF-8 encoding: #"0\344\22"<br><br>Any thoughts as to why <i>mysql_error</i> is returning back a string scheme is choking on?<br><br>2) DrScheme has been crashing nearly constantly since I started working on this. I'm running on Vista, and after having DrScheme open for a minute or two, I'll inevitably get a pop-up: <i>PLT Scheme GUI Application has stopped working</i>. It doesn't seem to correspond to any one statement I'm executing. For example, I kicked off DrScheme, ran the test code mentioned above, got the error, started writing this e-mail, and flipped back to find DrScheme had crashed.<br>
<br>I'm running 4.1.3.6-svn25dec2008.<br><br>Could this be a pre release issue?<br><br>Anyone have any ideas as to how I could get the specific cause of the crash?<br><br>This is probably just the result of some poorly written FFI code (which, I realize, is unsafe). Perhaps there's a glaring bug in the code below?<br>
<br>Here's the code I've worked up this far - For now, I'm testing it by running the (do-test) procedure.<br><br>Thanks,<br>Ben<br><br>------------------------------------------------------------------------------------------------------------------------------------------------<br>
<br>(require scheme/foreign)<br>(unsafe!)<br><br>(define current-mysql-handle (make-parameter #f))<br><br>(define libmysql (ffi-lib "libmysql"))<br><br>(define _handle (make-ctype _pointer #f <br> (lambda (handle) <br>
(when handle (register-finalizer handle c-mysql-close))<br> handle)))<br><br>(define _data (make-ctype _pointer #f<br> (lambda (data)<br>
(if data<br> data<br> (error 'mysql (c-mysql-error))))))<br><br>(define-fun-syntax _handle*<br> (syntax-id-rules ()<br> [_ (type: _handle expr: (current-mysql-handle))]))<br>
<br><br>(define c-mysql-init (get-ffi-obj "mysql_init" libmysql (_fun -> _handle)))<br>(define c-mysql-close (get-ffi-obj "mysql_close" libmysql (_fun _handle -> _void)))<br>(define c-mysql-error (get-ffi-obj "mysql_error" libmysql (_fun _handle* -> _string)))<br>
<br>(current-mysql-handle (c-mysql-init))<br><br><br>(define c-mysql-real-connect (get-ffi-obj "mysql_real_connect" <br> libmysql <br> (_fun _handle* _string _string _string _string _int<br>
_string _long -> _data)))<br><br>(define (do-test)<br> ; Intentionally fail to connect, if all goes well, raise a scheme error<br> (c-mysql-real-connect "localhos" "root" "invalid" "none" 0 "" 0))<br>
<br><br clear="all"><br>-- <br>Have an idea for software? I can make it happen - <a href="http://www.ideas2executables.com">http://www.ideas2executables.com</a><br>My Blog: <a href="http://benjisimon.blogspot.com">http://benjisimon.blogspot.com</a><br>