1 ! Copyright (C) 2007 Berlin Brown, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for license.
3 ! Adapted from mysql.h and mysql.c
4 ! Tested with MySQL version - 5.0.24a
5 USING: kernel alien io prettyprint sequences
6 namespaces arrays math db.mysql.ffi system accessors ;
11 TUPLE: mysql-db handle host user password db port ;
12 TUPLE: mysql-statement ;
13 TUPLE: mysql-result-set ;
15 : new-mysql ( -- conn )
18 : mysql-error ( mysql -- )
19 [ mysql_error throw ] when* ;
21 ! : mysql-connect ( mysql-connection -- )
22 ! new-mysql over set-mysql-db-handle
30 ! } get-slots f 0 mysql_real_connect mysql-error ;
32 ! =========================================================
33 ! Low level mysql utility definitions
34 ! =========================================================
36 : (mysql-query) ( mysql-connection query -- ret )
37 >r db-handle>> r> mysql_query ;
39 ! : (mysql-result) ( mysql-connection -- ret )
40 ! [ mysql-db-handle mysql_use_result ] keep
41 ! [ set-mysql-connection-resulthandle ] keep ;
43 ! : (mysql-affected-rows) ( mysql-connection -- n )
44 ! mysql-connection-mysqlconn mysql_affected_rows ;
46 ! : (mysql-free-result) ( mysql-connection -- )
47 ! mysql-connection-resulthandle drop ;
49 ! : (mysql-row) ( mysql-connection -- row )
50 ! mysql-connection-resulthandle mysql_fetch_row ;
52 ! : (mysql-num-cols) ( mysql-connection -- n )
53 ! mysql-connection-resulthandle mysql_num_fields ;
55 ! : mysql-char*-nth ( index object -- str )
56 ! #! Utility based on 'char*-nth' to perform an additional sanity check on the value
57 ! #! extracted from the array of strings.
58 ! void*-nth [ alien>char-string ] [ "" ] if* ;
60 ! : mysql-row>seq ( object n -- seq )
61 ! [ swap mysql-char*-nth ] map-with ;
63 ! : (mysql-result>seq) ( seq -- seq )
64 ! my-conn get (mysql-row) dup [
65 ! my-conn get (mysql-num-cols) mysql-row>seq
69 ! ! Perform needed cleanup on fetched results
70 ! my-conn get (mysql-free-result) ;
72 ! : mysql-query ( query -- ret )
73 ! >r my-conn get r> (mysql-query) drop
74 ! my-conn get (mysql-result) ;
76 ! : mysql-command ( query -- n )
78 ! my-conn get (mysql-affected-rows) ;