From 4066e1ca6b68512726bf66a9a4526a222ce770fe Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 4 Feb 2008 13:34:01 -0600 Subject: [PATCH] start mysql --- extra/db/mysql/ffi/ffi.factor | 25 ++++++++++ extra/db/mysql/lib/lib.factor | 94 +++++++++++++++++++++++++++++++++++ extra/db/mysql/mysql.factor | 15 ++++++ 3 files changed, 134 insertions(+) create mode 100644 extra/db/mysql/ffi/ffi.factor create mode 100644 extra/db/mysql/lib/lib.factor create mode 100644 extra/db/mysql/mysql.factor diff --git a/extra/db/mysql/ffi/ffi.factor b/extra/db/mysql/ffi/ffi.factor new file mode 100644 index 0000000000..845381a23c --- /dev/null +++ b/extra/db/mysql/ffi/ffi.factor @@ -0,0 +1,25 @@ +! Copyright (C) 2007 Berlin Brown, 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +! Adapted from mysql.h and mysql.c +! Tested with MySQL version - 5.0.24a +USING: alien alien.syntax combinators kernel system ; +IN: db.mysql.ffi + +<< "mysql" { + { [ win32? ] [ "libmySQL.dll" "stdcall" ] } + { [ macosx? ] [ "libmysqlclient.14.dylib" "cdecl" ] } + { [ unix? ] [ "libmysqlclient.so.14" "cdecl" ] } +} cond add-library >> + +LIBRARY: mysql + +FUNCTION: void* mysql_init ( void* mysql ) ; +FUNCTION: char* mysql_error ( void* mysql ) ; +FUNCTION: void* mysql_real_connect ( void* mysql, char* host, char* user, char* passwd, char* db, int port, char* unixsocket, long clientflag ) ; +FUNCTION: void mysql_close ( void* sock ) ; +FUNCTION: int mysql_query ( void* mysql, char* q ) ; +FUNCTION: void* mysql_use_result ( void* mysql ) ; +FUNCTION: void mysql_free_result ( void* result ) ; +FUNCTION: char** mysql_fetch_row ( void* result ) ; +FUNCTION: int mysql_num_fields ( void* result ) ; +FUNCTION: ulong mysql_affected_rows ( void* mysql ) ; diff --git a/extra/db/mysql/lib/lib.factor b/extra/db/mysql/lib/lib.factor new file mode 100644 index 0000000000..7d5c2d55dc --- /dev/null +++ b/extra/db/mysql/lib/lib.factor @@ -0,0 +1,94 @@ +! Copyright (C) 2007 Berlin Brown, 2008 Doug Coleman. +! See http://factorcode.org/license.txt for license. +! Adapted from mysql.h and mysql.c +! Tested with MySQL version - 5.0.24a +USING: kernel alien io prettyprint sequences +namespaces arrays math db.mysql.ffi system ; +IN: db.mysql.lib + +SYMBOL: my-conn + +TUPLE: mysql-db handle host user password db port ; +TUPLE: mysql-statement ; +TUPLE: mysql-result-set ; + +: new-mysql ( -- conn ) + f mysql_init ; + +: mysql-error-string ( mysql-connection -- str ) + mysql-db-handle mysql_error ; + +: mysql-error ( mysql -- ) + mysql-error-string throw ; + +: mysql-connect ( mysql-connection -- ) + init-mysql swap + [ set-mysql-connection-mysqlconn ] 2keep + [ mysql-connection-host ] keep + [ mysql-connection-user ] keep + [ mysql-connection-password ] keep + [ mysql-connection-db ] keep + [ mysql-connection-port f 0 mysql_real_connect ] keep + [ set-mysql-connection-handle ] keep + dup mysql-connection-handle + [ connect-error-msg throw ] unless ; + +! ========================================================= +! Low level mysql utility definitions +! ========================================================= + +: (mysql-query) ( mysql-connection query -- ret ) + >r mysql-connection-mysqlconn r> mysql_query ; + +: (mysql-result) ( mysql-connection -- ret ) + [ mysql-connection-mysqlconn mysql_use_result ] keep + [ set-mysql-connection-resulthandle ] keep ; + +: (mysql-affected-rows) ( mysql-connection -- n ) + mysql-connection-mysqlconn mysql_affected_rows ; + +: (mysql-free-result) ( mysql-connection -- ) + mysql-connection-resulthandle drop ; + +: (mysql-row) ( mysql-connection -- row ) + mysql-connection-resulthandle mysql_fetch_row ; + +: (mysql-num-cols) ( mysql-connection -- n ) + mysql-connection-resulthandle mysql_num_fields ; + +: mysql-char*-nth ( index object -- str ) + #! Utility based on 'char*-nth' to perform an additional sanity check on the value + #! extracted from the array of strings. + void*-nth [ alien>char-string ] [ "" ] if* ; + +: mysql-row>seq ( object n -- seq ) + [ swap mysql-char*-nth ] map-with ; + +: (mysql-result>seq) ( seq -- seq ) + my-conn get (mysql-row) dup [ + my-conn get (mysql-num-cols) mysql-row>seq + over push + (mysql-result>seq) + ] [ drop ] if + ! Perform needed cleanup on fetched results + my-conn get (mysql-free-result) ; + +! ========================================================= +! Public Word Definitions +! ========================================================= + + +: mysql-query ( query -- ret ) + >r my-conn get r> (mysql-query) drop + my-conn get (mysql-result) ; + +: mysql-command ( query -- n ) + mysql-query drop + my-conn get (mysql-affected-rows) ; + +: with-mysql ( host user password db port quot -- ) + [ + >r my-conn set + my-conn get mysql-connect drop r> + [ my-conn get mysql-close ] cleanup + ] with-scope ; inline diff --git a/extra/db/mysql/mysql.factor b/extra/db/mysql/mysql.factor new file mode 100644 index 0000000000..8043bc2782 --- /dev/null +++ b/extra/db/mysql/mysql.factor @@ -0,0 +1,15 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for license. +USING: alien continuations io kernel prettyprint sequences +db ; +IN: db.mysql + +TUPLE: mysql-db handle host user password db port ; + +M: mysql-db db-open ( mysql-db -- ) + ; + +M: mysql-db dispose ( mysql-db -- ) + mysql-db-handle mysql_close ; + +