Ok, this is what I came up for a mysql factor binding, as requested; 3
files in post.  Note, download below for cleaner view.

http://www.newspiritcompany.com/factor/mysql_feb12.zip

--

! License: See http://factor.sf.net/license.txt for BSD license.
! Copyright (C) 2007 Berlin Brown
! Date: 1/17/2007
!
! libs/mysql/libmysql.factor
!
! Adapted from mysql.h and mysql.c
! Tested with MySQL version - 5.0.24a

IN: mysql
USING: alien ;

! ##to include## "mysql" "libmySQL.dll" "stdcall" add-library

LIBRARY: mysql

! ===============================================
! typedefs
! ===============================================

TYPEDEF: void* MYSQL

! ===============================================
! mysql.c
! ===============================================

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


! License: See http://factor.sf.net/license.txt for BSD license.
! Copyright (C) 2007 Berlin Brown
! Date: 1/17/2007
!
! libs/mysql/mysql.factor
!
! Adapted from mysql.h and mysql.c
! Tested with MySQL version - 5.0.24a

IN: mysql
USING: kernel alien errors io prettyprint sequences namespaces arrays
math tools ;

SYMBOL: my-conn

TUPLE: mysql-connection mysqlconn host user password db port handle
resulthandle ;

: init-mysql ( -- conn )
        f mysql_init ;
        
C: mysql-connection ( host user password db port -- mysql-connection )
        [ set-mysql-connection-port ] keep
        [ set-mysql-connection-db ] keep
        [ set-mysql-connection-password ] keep
        [ set-mysql-connection-user ] keep
        [ set-mysql-connection-host ] keep ;

: (mysql-error) ( mysql-connection -- str )
        mysql-connection-mysqlconn mysql_error ;

: connect-error-msg ( mysql-connection -- s )
        mysql-connection-mysqlconn mysql_error
        [
                "Couldn't connect to mysql database.\n" %
                "Message: " % %
        ] "" make ;

: 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-row>seq) ( object n -- seq )
        #! Convert the input fetched row into a seq of
        #! of string values.
        V{ } clone -rot
        [               
                swap char*-nth
                over push
        ] each-with  ;
        
: (mysql-result>seq) ( seq -- seq )
        my-conn get (mysql-row) dup f = [
                drop
        ] [             
                my-conn get (mysql-num-cols) (mysql-row>seq)
                over push
                (mysql-result>seq)
        ] if ;
                
: (mysql-close) ( mysql-connection -- )
        mysql-connection-mysqlconn mysql_close ;

! =========================================================
!  Public Word Definitions
! =========================================================

: mysql-print-table ( seq -- )
    [ [ write bl ] each "\n" write ] each ;

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

: mysql-error ( -- s )
        #! Get the last mysql error
        my-conn get (mysql-error) ;     

: mysql-result>seq ( -- seq )
        V{ } clone (mysql-result>seq) ;
                
: 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
        
: with-mysql-catch ( host user password db port quot -- )
    [ with-mysql ] catch [ "Caught: " write print ] when* ;



! Simple test for mysql library
! libs/mysql/test/mysql-example.factor

IN: mysql-example
REQUIRES: libs/mysql ;
USING: sequences mysql modules prettyprint kernel io math tools namespaces ;

"Testing..." print terpri

: get-drop-table ( -- s )
       "DROP TABLE if exists DISCUSSION_FORUM" ;

: get-insert-table ( -- s )
        {
                "INSERT INTO DISCUSSION_FORUM(category, full_name, email, title,
main_url, keywords, message) "
                "VALUES('none', 'John Doe', '[EMAIL PROTECTED]', 'The Message',
'http://johndoe.com', 'none', 'Testing')"
        } "" join ;

: get-update-table ( -- s )
        "UPDATE DISCUSSION_FORUM set category = 'my-new-category'" ;
        
: get-delete-table ( -- s )
        "DELETE FROM DISCUSSION_FORUM where id = 2" ;

: get-create-table ( -- s )
        {
                "create table DISCUSSION_FORUM("
        "id                     int(11) NOT NULL auto_increment,"
        "category               varchar(128) NOT NULL,"
        "full_name              varchar(128) NOT NULL,"
        "email                  varchar(128) NOT NULL,"
        "title                  varchar(255) NOT NULL,"
        "main_url               varchar(255),"
                "keywords               varchar(255),"
        "message                text NOT NULL,"
        "created_on             DATETIME NOT NULL DEFAULT '0000-00-0000:00:00',"
        "PRIMARY KEY (id));"
        } "" join ;

"localhost" "factoruser" "mysqlfactor" "factordb_development" 0 [
        get-drop-table mysql-command drop
        get-create-table mysql-command drop
        get-update-table mysql-command drop
        get-delete-table mysql-command drop
        
        ! Insert multiple records
        10 [
                get-insert-table mysql-command 2drop
        ] each
                
        "select * from discussion_forum order by created_on" mysql-query drop
        mysql-result>seq mysql-print-table

] with-mysql-catch

"Done" print


-- 
Berlin Brown

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to