1 ! See http://factorcode.org/license.txt for license.
2 ! Copyright (C) 2007 Berlin Brown
5 ! libs/mysql/mysql.factor
7 ! Adapted from mysql.h and mysql.c
8 ! Tested with MySQL version - 5.0.24a
11 USING: kernel alien errors io prettyprint
12 sequences namespaces arrays math tools generic ;
16 TUPLE: mysql-connection mysqlconn host user password db port handle resulthandle ;
18 : init-mysql ( -- conn )
21 C: mysql-connection ( host user password db port -- mysql-connection )
22 [ set-mysql-connection-port ] keep
23 [ set-mysql-connection-db ] keep
24 [ set-mysql-connection-password ] keep
25 [ set-mysql-connection-user ] keep
26 [ set-mysql-connection-host ] keep ;
28 : (mysql-error) ( mysql-connection -- str )
29 mysql-connection-mysqlconn mysql_error ;
31 : connect-error-msg ( mysql-connection -- s )
32 mysql-connection-mysqlconn mysql_error
34 "Couldn't connect to mysql database.\n" %
38 : mysql-connect ( mysql-connection -- )
40 [ set-mysql-connection-mysqlconn ] 2keep
41 [ mysql-connection-host ] keep
42 [ mysql-connection-user ] keep
43 [ mysql-connection-password ] keep
44 [ mysql-connection-db ] keep
45 [ mysql-connection-port f 0 mysql_real_connect ] keep
46 [ set-mysql-connection-handle ] keep
47 dup mysql-connection-handle
48 [ connect-error-msg throw ] unless ;
50 ! =========================================================
51 ! Low level mysql utility definitions
52 ! =========================================================
54 : (mysql-query) ( mysql-connection query -- ret )
55 >r mysql-connection-mysqlconn r> mysql_query ;
57 : (mysql-result) ( mysql-connection -- ret )
58 [ mysql-connection-mysqlconn mysql_use_result ] keep
59 [ set-mysql-connection-resulthandle ] keep ;
61 : (mysql-affected-rows) ( mysql-connection -- n )
62 mysql-connection-mysqlconn mysql_affected_rows ;
64 : (mysql-free-result) ( mysql-connection -- )
65 mysql-connection-resulthandle drop ;
67 : (mysql-row) ( mysql-connection -- row )
68 mysql-connection-resulthandle mysql_fetch_row ;
70 : (mysql-num-cols) ( mysql-connection -- n )
71 mysql-connection-resulthandle mysql_num_fields ;
73 : mysql-char*-nth ( index object -- str )
74 #! Utility based on 'char*-nth' to perform an additional sanity check on the value
75 #! extracted from the array of strings.
76 void*-nth [ alien>char-string ] [ "" ] if* ;
78 : mysql-row>seq ( object n -- seq )
79 [ swap mysql-char*-nth ] map-with ;
81 : (mysql-result>seq) ( seq -- seq )
82 my-conn get (mysql-row) dup [
83 my-conn get (mysql-num-cols) mysql-row>seq
87 ! Perform needed cleanup on fetched results
88 my-conn get (mysql-free-result) ;
90 ! =========================================================
91 ! Public Word Definitions
92 ! =========================================================
94 : mysql-close ( mysql-connection -- )
95 mysql-connection-mysqlconn mysql_close ;
97 : mysql-print-table ( seq -- )
98 [ [ write bl ] each "\n" write ] each ;
100 : mysql-query ( query -- ret )
101 >r my-conn get r> (mysql-query) drop
102 my-conn get (mysql-result) ;
104 : mysql-command ( query -- n )
106 my-conn get (mysql-affected-rows) ;
108 : mysql-error ( -- s )
109 #! Get the last mysql error
110 my-conn get (mysql-error) ;
112 : mysql-result>seq ( -- seq )
113 V{ } clone (mysql-result>seq) ;
115 : with-mysql ( host user password db port quot -- )
117 >r <mysql-connection> my-conn set
118 my-conn get mysql-connect drop r>
119 [ my-conn get mysql-close ] cleanup
120 ] with-scope ; inline
122 : with-mysql-catch ( host user password db port quot -- )
123 [ with-mysql ] catch [ "Caught: " write print ] when* ;