start mysql

db4
Doug Coleman 2008-02-04 13:34:01 -06:00
parent c9a7f35e9c
commit 4066e1ca6b
3 changed files with 134 additions and 0 deletions

View File

@ -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 ) ;

View File

@ -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 <mysql-connection> my-conn set
my-conn get mysql-connect drop r>
[ my-conn get mysql-close ] cleanup
] with-scope ; inline

View File

@ -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 ;