Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
TOC »
MySQL client
Description
A small MySQL client library for chicken-scheme. This egg makes it possible to execute an SQL query on a MySQL database.
Authors
Requirements
Requires MySQL database system development components, libmysqlclient and mysql_config.
The mysql_config program needs to be in the $PATH so the linker can find the native MySQL libraries.
On a Linux machine, consider trying:
apt-get install libmysqlclient-dev
Installation
chicken-install -s mysql-client
With MySQL 5.5.24, the compiled mysql-client library does not have an absolute path to the libmysqlclient dependency, this can be corrected with the following command
sudo install_name_tool -change libmysqlclient.18.dylib $(mysql_config --variable=pkglibdir)/libmysqlclient.18.dylib /usr/local/lib/chicken/6/mysql-client.so
API
- make-mysql-connection hostname username password schemaprocedure
This procedure will return a procedure that contains a closed off MySQL connection and can execute a query when called:
- unnamed-mysql-connection-closure sql-statement #!rest placeholder-values-alistprocedure
This will return a row fetcher closure, either provide a procedure to apply to all rows or call with no parameters to return the next row:
- unnamed-mysql-row-fetcher-closure #!rest thunkprocedure
Alternatively to apply a procedure directly to the MySQL connection pointer, pass a procedure:
- mysql-null valueparameter
MySQL null field parameter. Called with one argument, will set the value representing a MySQL null.
- mysql-null? valueprocedure
Returns true if value is the MySQL null representative.
Examples
Here is a trivial example:
(use mysql-client) (define mysql (make-mysql-connection "localhost" "root" #f "information_schema")) (define fetch (mysql "SELECT * FROM schemata")) (display (fetch)) (newline)
Here is an example that will use the mysql_real_escape_string MySQL function to safely escape parameters in the query string using a placeholder syntax. In this case, the first argument is a string containing an SQL query, and the second argument will be an association list where the key represents the placeholder symbol in the query.
(use mysql-client) (define mysql (make-mysql-connection "localhost" "username" "secret" "database")) (define fetch (mysql "SELECT * FROM t1 WHERE c1 = '$1' OR c2 = '$2'" '(($1 . "value 1';' ;")($2 . "value 2'; drop database; ")))) (display (fetch)) (newline)
Other placeholder conventions are possible as well, as the keys from the association list are matched in the query.
(define fetch (mysql "SELECT * FROM t WHERE created_on < '?to' AND created_on > '?from' LIMIT 5" '((?from . "2012-04-01 00:00")(?to . "2012-04-01 24:00"))))
To apply a procedure to every row, pass a procedure to the fetcher closure.
(fetch (lambda(r) (printf "~S~%" r)))
For power users, to access directly the MySQL connection object pointer, pass a procedure to the MySQL client closure.
(use mysql-client) (define mysql (make-mysql-connection "localhost" "username" "secret" "database")) (define fetch (mysql (lambda(c)(printf "~S ~%" c)))) (display (fetch)) (newline)
License
Copyright (C) 2012, A. Carl Douglas All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Repository
https://bitbucket.org/carldouglas/mysql-client.egg
Version History
- 0.8 Added mysql-null? predicate and mysql-null parameter to specify how null fields are represented.
- 0.7 Use myql_config --cflags instead of --include when compiling, detect MySQL 5.5.24 and attempt to apply install_name_tool work around.
- 0.6 Fixed setup so it passes all options returned from mysql_config through to the compiler.
- 0.5 Return fetch procedure that returns false when there are no rows. Fetch procedure can apply a procedure to all rows.
- 0.4 Initial publication.