1; mysql.ss - MySQL library
2;
3;   Copyright (c) 2009  Higepon(Taro Minowa)  <higepon@users.sourceforge.jp>
4;
5;   Redistribution and use in source and binary forms, with or without
6;   modification, are permitted provided that the following conditions
7;   are met:
8;
9;   1. Redistributions of source code must retain the above copyright
10;      notice, this list of conditions and the following disclaimer.
11;
12;   2. Redistributions in binary form must reproduce the above copyright
13;      notice, this list of conditions and the following disclaimer in the
14;      documentation and/or other materials provided with the distribution.
15;
16;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17;   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18;   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19;   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20;   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21;   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22;   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23;   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24;   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25;   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26;   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27;
28;  $Id: mysql.ss 621 2008-11-09 06:22:47Z higepon $
29
30 (library (mosh mysql)
31  (export mysql-init mysql-real-connect NULL mysql-query mysql-store-result
32          mysql-num-rows mysql-fetch-row mysql-row-ref mysql-close mysql-free-result mysql-affected-rows
33          mysql-get-client-info mysql-autocommit mysql-change-user mysql-character-set-name
34          mysql-commit mysql-data-seek mysql-dump-debug-info mysql-errno mysql-error mysql-fetch-field
35          mysql-field-name mysql-fetch-field-direct mysql-fetch-fields mysql-fetch-lengths
36          mysql-field-count mysql-field-seek mysql-field-tell mysql-get-client-version mysql-get-host-info
37          mysql-get-proto-info mysql-get-server-info mysql-get-server-version mysql-get-ssl-cipher
38          mysql-hex-string mysql-info mysql-insert-id mysql-library-end mysql-library-init
39          mysql-list-dbs mysql-list-processes mysql-list-tables mysql-more-results mysql-next-result
40          mysql-num-fields mysql-options mysql-ping mysql-real-escape-string mysql-real-query
41          mysql-refresh mysql-reload mysql-rollback mysql-row-seek mysql-row-tell
42          mysql-select-db mysql-set-character-set mysql-set-server-option mysql-shutdwon
43          mysql-sqlstate mysql-ssl-set mysql-stat mysql-thread-id mysql-use-result mysql-warning-count)
44  (import (only (rnrs) define guard apply define-syntax syntax-case ... cond lambda syntax else set!)
45          (mosh ffi))
46
47(define NULL pointer-null)
48
49(define-syntax c-function-wrap
50  (lambda (x)
51    (syntax-case x ()
52      [(_ lib more ...)
53       #'(cond
54          [lib
55           (c-function lib more ...)]
56          [else
57           "libmysqlclient not found"])])))
58
59(define libmysqlclient (guard [c (#t #f)] (open-shared-library "@PATH_TO_MYSQLCLIENT@")))
60
61
62(define %mysql-init        (c-function-wrap libmysqlclient void* mysql_init void*))
63
64;; attempts to establish a connection to a MySQL database engine running on host.
65;; .form (real-connect obj host user password db port sock client-flag)
66;; .returns obj
67(define mysql-real-connect (c-function-wrap libmysqlclient void* mysql_real_connect void* char* char* char* char* int char* int))
68
69;; Executes the SQL statement
70;; .form (query obj query)
71;; .returns Zero if the statement was successful. Non-zero if an error occurred.
72(define mysql-query        (c-function-wrap libmysqlclient int mysql_query        void* char*))
73
74;; After invoking query, you must call store-result() for every statement that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK TABLE, and so forth).
75;; .form (store-result obj)
76;; .returns A MYSQL_RES result structure with the results. NULL (0) if an error occurred.
77(define mysql-store-result (c-function-wrap libmysqlclient void* mysql_store_result void*))
78
79;; Returns the number of rows in the result set.
80;; .form (num-rows result)
81;; .returns the number of rows
82(define mysql-num-rows     (c-function-wrap libmysqlclient int mysql_num_rows void*))
83
84;; Retrieves the next row of a result set.
85;; .form (fetch-row result)
86;; .returns A MYSQL_ROW structure for the next row. NULL if there are no more rows to retrieve or if an error occurred.
87(define mysql-fetch-row    (c-function-wrap libmysqlclient void* mysql_fetch_row void*))
88
89;; Retrieves the index field
90;; .form (row-ref row index)
91;; .returns Field as string
92(define (mysql-row-ref result row index)
93  (pointer->string (pointer-ref-c-pointer row index) (pointer-ref-c-unsigned-long (mysql-fetch-lengths result) index)))
94
95;; Closes a previously opened connection
96;; .form (close obj)
97;; .returns none
98(define mysql-close        (c-function-wrap libmysqlclient void mysql_close void*))
99
100;; Frees the memory allocated for a result set
101;; .form (free-result result)
102;; .returns none
103(define mysql-free-result  (c-function-wrap libmysqlclient void mysql_free_result  void*))
104
105;; .form (get-client-info)
106;; .returns Returns a string that represents the client library version.
107(define mysql-get-client-info (c-function-wrap libmysqlclient char* mysql_get_client_info))
108
109;; After executing a statement with query returns the number of rows changed (for UPDATE), deleted (for DELETE), or inserted (for INSERT).
110;; .form (affected-rows (obj))
111;; .returns An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error or that, for a SELECT query.
112(define mysql-affected-rows (c-function-wrap libmysqlclient int mysql_affected_rows void*))
113
114;; Sets autocommit mode on if mode is 1, off if mode is 0.
115;; .form (autocommit mysql-obj 0 or 1)
116;; .returns Zero if successful. Non-zero if an error occurred.
117(define mysql-autocommit (c-function-wrap libmysqlclient int mysql_autocommit void* int))
118
119;; Changes the user and causes the database specified by db to become the default (current) database on the connection specified by mysql. In subsequent queries, this database is the default for table references that do not include an explicit database specifier.
120;; .form (change-user mysql-obj user password db)
121;; .returns Zero for success. Non-zero if an error occurred.
122(define mysql-change-user (c-function-wrap libmysqlclient int mysql_change_user void* char* char* char*))
123
124;; Returns the default character set name for the current connection.
125;; .form (character-set-name mysql-obj)
126;; .returns The default character set name
127(define mysql-character-set-name (c-function-wrap libmysqlclient char* mysql_character_set_name void*))
128
129;; Commits the current transaction.
130;; .form (commit mysql-obj)
131;; .returns Zero if successful. Non-zero if an error occurred.
132(define mysql-commit (c-function-wrap libmysqlclient int mysql_commit void*))
133
134;; Seeks to an arbitrary row in a query result set. The offset value is a row number and should be in the range from 0 to (mysql-num-rows result) -1.
135;; .form (data-seek mysql-obj offset)
136(define mysql-data-seek (c-function-wrap libmysqlclient void mysql_data_seek void* int))
137
138;; Instructs the server to write some debug information to the log. For this to work, the connected user must have the SUPER privileg
139;; .form (dump-debug-info mysql-obj)
140;; Zero if the command was successful. Non-zero if an error occurred.
141(define mysql-dump-debug-info (c-function-wrap libmysqlclient int mysql_dump_debug_info void*))
142
143;; returns the error code for the most recently invoked API function that can succeed or fail.
144;; .form (errno mysql-obj)
145(define mysql-errno (c-function-wrap libmysqlclient int mysql_errno void*))
146
147;; Null-terminated string containing the error message for the most recently invoked API function that failed.
148;; .form (error mysql-obj)
149(define mysql-error (c-function-wrap libmysqlclient char* mysql_error void*))
150
151;; Returns the definition of one column of a result set as a MYSQL_FIELD structure. Call this function repeatedly to retrieve information about all columns in the result set. mysql_fetch_field()  returns NULL when no more fields
152;; .form (fetch-field result)
153(define mysql-fetch-field (c-function-wrap libmysqlclient void* mysql_fetch_field void*))
154
155;; Returns the field name of field. TODO: this depends on MYSQL_FIELD structure.
156;; .form (field-name result)
157(define (mysql-field-name field) (pointer->string (pointer-ref-c-pointer field 0)))
158
159;; Given a field number fieldnr for a column within a result set, returns that column's field definition as a MYSQL_FIELD structure.
160;; .form (mysql-fetch-field-direct result fieldnr)
161;; .returns The MYSQL_FIELD structure for the specified column.
162(define mysql-fetch-field-direct (c-function-wrap libmysqlclient void* mysql_fetch_field_direct void* int))
163
164;; Returns an array of all MYSQL_FIELD  structures for a result set. Each structure provides the field definition for one column of the result set.
165;; .form (mysql-fetch-fields result)
166;; .returns An array of MYSQL_FIELD structures for all columns of a result set.
167(define mysql-fetch-fields (c-function-wrap libmysqlclient void* mysql_fetch_fields void*))
168
169;; Returns the lengths of the columns of the current row within a result set. If you plan to copy field values, this length information is also useful for optimization, because you can avoid calling strlen().
170;; .form (mysql-fetch-fields result)
171;; .returns An array of unsigned long integers representing the size of each column (not including any terminating null characters). NULL if an error occurred.
172(define mysql-fetch-lengths (c-function-wrap libmysqlclient void* mysql_fetch_lengths void*))
173
174;; Returns the number of columns for the most recent query on the connection.
175;; .form (mysql-field-count mysql-obj)
176;; .returns An unsigned integer representing the number of columns in a result set.
177(define mysql-field-count (c-function-wrap libmysqlclient int mysql_field_count void*))
178
179;;  Sets the field cursor to the given offset. The next call to mysql-fetch-field retrieves the field definition of the column associated with that offset.
180;;  To seek to the beginning of a row, pass an offset value of zero.
181;; .form (mysql-field-seek result offset)
182;; .returns The previous value of the field cursor.
183(define mysql-field-seek (c-function-wrap libmysqlclient int mysql_field_seek void* int))
184
185;; Returns the position of the field cursor used for the last
186;; .form (mysql-field-tell result)
187;; .returns The current offset of the field cursor.
188(define mysql-field-tell (c-function-wrap libmysqlclient int mysql_field_tell void*))
189
190;; not supported
191;;(define mysql-get-character-set-info )
192
193
194;; Returns an integer that represents the client library version.
195;; .form ((mysql-get-client-info)
196;; .returns Returns an integer that represents the client library version.
197(define mysql-get-client-version (c-function-wrap libmysqlclient int mysql_get_client_version))
198
199;; Returns a string describing the type of connection in use, including the server host name.
200;; .form (mysql-get-host-info mysql-obj)
201(define mysql-get-host-info (c-function-wrap libmysqlclient char* mysql_get_host_info void*))
202
203;; Returns the protocol version used by current connection.
204;; .form (mysql-get-proto-info mysql-obj)
205;; .returns An unsigned integer representing the protocol version used by the current connection.
206(define mysql-get-proto-info (c-function-wrap libmysqlclient int mysql_get_proto_info void*))
207
208;; Returns a string that represents the server version number.
209;; .form (mysql-get-server-info mysql-obj)
210;; .returns A character string that represents the server version number.
211(define mysql-get-server-info (c-function-wrap libmysqlclient char* mysql_get_server_info void*))
212
213;; Returns the version number of the server as an integer.
214;; .form (mysql-get-srever-version mysql-obj)
215;; .returns A number that represents the MySQL server.
216(define mysql-get-server-version (c-function-wrap libmysqlclient int mysql_get_server_version void*))
217
218;; mysql-get-ssl-cipher returns the SSL cipher used for the given connection to the server. mysql is the connection handler returned from mysql-init.
219;; .form (mysql-get-ssl-cipher mysql-obj)
220;; .returns A string naming the SSL cipher used for the connection, or NULL if no cipher is being used.
221(define mysql-get-ssl-cipher (c-function-wrap libmysqlclient char* mysql_get_ssl_cipher void*))
222
223;; This function is used to create a legal SQL string that you can use in an SQL statement. See Section 8.1.1, "Strings" on MySQL Manual.
224;; .form (mysql-hex-string bv-to str-from len)
225;; .returns The length of the value placed into to, not including the terminating null character.
226(define mysql-hex-string (c-function-wrap libmysqlclient int mysql_hex_string char* char* int))
227
228;; Retrieves a string providing information about the most recently executed statement, but only for the statements listed here.
229;; .form (mysql-info mysql-obj)
230;; .returns A character string representing additional information about the most recently executed statement. NULL if no information is available for the statement.
231(define mysql-info (c-function-wrap libmysqlclient char* mysql_info void*))
232
233;; Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement. TODO: May overflow?
234;; .form (mysql-insert-id mysql-obj)
235;; .returns Described in the preceding discussion.
236(define mysql-insert-id (c-function-wrap libmysqlclient void* mysql_insert_id void*)) ;; use void* as return value
237
238;; Initialize MySQL client
239;; .form (init)
240;; .returns obj
241(define (mysql-init) (%mysql-init NULL))
242
243;; This function finalizes the MySQL library. You should call it when you are done using the library (for example, after disconnecting from the server).This function was added in MySQL 5.0.3.
244;; .form (mysql-library-end)
245;; .returns #f if client doen't supported this function
246(define mysql-library-end (guard (c [#t (lambda x #f)])
247                               (c-function-wrap libmysqlclient void mysql_library_end)))
248
249;; This function should be called to initialize the MySQL library before you call any other MySQL function, whether your application is a regular client program or uses the embedded server. In a non-multi-threaded environment, the call to mysql_library_init() may be omitted, because mysql_init()  will invoke it automatically as necessary.
250;; .form (mysql-library-init argc argv groups)
251;; .returns Zero if successful. Non-zero if an error occurred.
252(define mysql-library-init (guard (c [#t (lambda x #f)])
253                               (c-function-wrap libmysqlclient int mysql_library_init int void* void*)))
254
255
256;; Returns a result set consisting of database names on the server that match the simple regular expression specified by the wild parameter. wild may contain the wildcard characters “%” or “_”, or may be a NULL pointer to match all databases. Calling mysql-list-dbs is similar to executing the query SHOW DATABASES [LIKE wild].
257;; .form (mysql-list-dbs mysql-obj wild)
258;; .returns A MYSQL_RES result set for success. NULL if an error occurred.
259(define mysql-list-dbs (c-function-wrap libmysqlclient void* mysql_list_dbs void* char*))
260
261;; Returns a result set describing the current server threads.
262;; .form (mysql-list-processes mysql-obj)
263;; .returns A MYSQL_RES result set for success. NULL if an error occurred.
264(define mysql-list-processes (c-function-wrap libmysqlclient void* mysql_list_processes void*))
265
266;; Returns a result set consisting of table names in the current database that match the simple regular expression specified by the wild parameter. wild  may contain the wildcard characters “%” or “_”, or may be a NULL pointer to match all tables.
267;; .form (mysql-list-tables mysql-obj wild)
268;; .returns A MYSQL_RES result set for success. NULL if an error occurred.
269(define mysql-list-tables (c-function-wrap libmysqlclient void* mysql_list_tables void* char*))
270
271;; This function is used when you execute multiple statements specified as a single statement string, or when you execute CALL statements, which can return multiple result sets.
272;; .form (mysql-more-results mysql-obj)
273;; .returns TRUE (1) if more results exist. FALSE (0) if no more results exist.
274(define mysql-more-results (c-function-wrap libmysqlclient int mysql_more_results void*))
275
276;; If more statement results exist, mysql_next_result() reads the next statement result and returns the status back to the application. I
277;; .form (mysql-next-result mysql-obj)
278;; .returns 0:Successful and there are more results, -1:Successful and there are no more results, >0:An error occurred
279(define mysql-next-result (c-function-wrap libmysqlclient int mysql_next_result void*))
280
281;; Returns the number of columns in a result set.
282;; .form (mysql-num-fields result)
283;; .returns An unsigned integer representing the number of columns in a result set.
284(define mysql-num-fields (c-function-wrap libmysqlclient int mysql_num_fields void*))
285
286;; Can be used to set extra connect options and affect behavior for a connection. This function may be called multiple times to set several options.
287;; .form (mysql-options mysql-obj option arg)
288;; .returns Zero for success. Non-zero if you specify an unknown option.
289(define mysql-options (c-function-wrap libmysqlclient int mysql_options void* int char*))
290
291;; Checks whether the connection to the server is working. If the connection has gone down and auto-reconnect is enabled an attempt to reconnect is made.
292;; .form (mysql-ping mysql-obj)
293;; .returns Zero if the connection to the server is alive. Non-zero if an error occurred.
294(define mysql-ping (c-function-wrap libmysqlclient int mysql_ping void*))
295
296;; This function is used to create a legal SQL string that you can use in an SQL statement. The string in from is encoded to an escaped SQL string, taking into account the current character set of the connection. The result is placed in to and a terminating null byte is appended. Characters encoded are NUL (ASCII 0), “\n”, “\r”, “\”, “'”, “"”, and Control-Z (see Section 8.1, “Literal Values”). (Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in log files.)
297;; .form (mysql-real-escape-string mysql-obj to-bv from len)
298;; .returns The length of the value placed into to, not including the terminating null character.
299(define mysql-real-escape-string (c-function-wrap libmysqlclient int mysql_real_escape_string void* char* char* int))
300
301;; Executes the SQL statement pointed to by stmt_str, which should be a string length bytes long.
302;; .form (mysql-real-query mysql-obj stmt int)
303;; .returns Zero if the statement was successful. Non-zero if an error occurred.
304(define mysql-real-query (c-function-wrap libmysqlclient int mysql_real_query void* char* int))
305
306;; This function flushes tables or caches, or resets replication server information. The connected user must have the RELOAD privilege.
307;; .form (mysql-refresh mysql-obj options)
308;; .returns
309(define mysql-refresh (c-function-wrap libmysqlclient int mysql_refresh void* int))
310
311;;  Asks the MySQL server to reload the grant tables. The connected user must have the RELOAD  privilege. This function is deprecated. It is preferable to use mysql_query() to issue an SQL FLUSH PRIVILEGES statement instead.
312;; .form (mysql-reload mysql-obj)
313;; .returns Zero for success. Non-zero if an error occurred.
314(define mysql-reload (guard (c [#t (lambda x #f)])
315                               (c-function-wrap libmysqlclient int mysql_reload void*)))
316
317;;Rolls back the current transaction.
318;; .form (mysql-rollback mysql-obj)
319;; .returns Zero if successful. Non-zero if an error occurred.
320(define mysql-rollback (c-function-wrap libmysqlclient int mysql_rollback void*))
321
322;; Sets the row cursor to an arbitrary row in a query result set. The offset value is a row offset that should be a value returned from mysql-row-tell or from mysql-row-seek.
323;; .form (mysql-row-seek result offset)
324;; .returns The previous value of the row cursor. This value may be passed to a subsequent call to mysql-row-seek.
325(define mysql-row-seek (c-function-wrap libmysqlclient int mysql_row_seek void* int))
326
327;; Returns the current position of the row cursor for the last mysql-fetch-row. This value can be used as an argument to mysql-row-seek.
328;; .form (mysql-row-tell result)
329;; .returns The current offset of the row cursor.
330(define mysql-row-tell (c-function-wrap libmysqlclient int mysql_row_tell void*))
331
332;; Causes the database specified by db to become the default (current) database on the connection specified by mysql. In subsequent queries, this database is the default for table references that do not include an explicit database specifier.
333;; .form (mysql-select-db db)
334;; .returns Zero for success. Non-zero if an error occurred.
335(define mysql-select-db (c-function-wrap libmysqlclient int mysql_select_db void* char*))
336
337;; This function is used to set the default character set for the current connection. The string csname  specifies a valid character set name. The connection collation becomes the default collation of the character set.
338;; .form (mysql-set-character-set mysql-obj csname)
339;; .returns Zero for success. Non-zero if an error occurred.
340(define mysql-set-character-set (guard (c [#t (lambda x #f)])
341                               (c-function-wrap libmysqlclient int mysql_set_character_set void* char*)))
342
343;; Not supporeted
344;; .form ()
345;; .returns
346(define mysql-set-local-infile-default #f)
347
348;; Not supporeted
349;; .form ()
350;; .returns
351(define mysql-set-local-infile-handler #f)
352
353;; Enables or disables an option for the connection.
354;; .form (mysql-set-server-option mysql-obj option)
355;; .returns Zero for success. Non-zero if an error occurred.
356(define mysql-set-server-option (c-function-wrap libmysqlclient int mysql_set_server_option void* int))
357
358;; Asks the database server to shut down. The connected user must have the SHUTDOWN privilege. The shutdown_level argument was added in MySQL 5.0.1. MySQL 5.0 servers support only one type of shutdown; shutdown_level must be equal to SHUTDOWN_DEFAULT.
359;; .form (mysql-shutdwon mysql-obj shutdown_level)
360;; .returns Zero for success. Non-zero if an error occurred.
361(define mysql-shutdwon (c-function-wrap libmysqlclient int mysql_shutdown void* int))
362
363;; Returns a null-terminated string containing the SQLSTATE error code for the most recently executed SQL statement.
364;; .form (mysql-sqlstate mysql-obj)
365;; .returns A null-terminated character string containing the SQLSTATE error code.
366(define mysql-sqlstate (c-function-wrap libmysqlclient char* mysql_sqlstate void*))
367
368;;  mysql-ssl-set() is used for establishing secure connections using SSL. It must be called before mysql-real-connect.
369;; .form (mysql-ssl-set mysql-obj key cert ca capath cipher)
370;; .returns This function always returns 0. If SSL setup is incorrect, mysql-real-connect returns an error when you attempt to connect.
371(define mysql-ssl-set  (c-function-wrap libmysqlclient int mysql_ssl_set void* char* char* char* char*))
372
373;; Returns a character string containing information similar to that provided by the mysqladmin status  command. This includes uptime in seconds and the number of running threads, questions, reloads, and open tables.
374;; .form (mysql-stat mysql-obj)
375;; .returns A character string describing the server status. NULL if an error occurred.
376(define mysql-stat (c-function-wrap libmysqlclient char* mysql_stat void*))
377
378;; Returns the thread ID of the current connection. This value can be used as an argument to mysql_kill() to kill the thread.
379;; .form (mysql-thread-id mysql-obj)
380;; .returns The thread ID of the current connection.
381(define mysql-thread-id (c-function-wrap libmysqlclient int mysql_thread_id void*))
382
383;; mysql-use-result initiates a result set retrieval but does not actually read the result set into the client like mysql-store-result does. Instead, each row must be retrieved individually by making calls to mysql-fetch-row. This reads the result of a query directly from the server without storing it in a temporary table or local buffer, which is somewhat faster and uses much less memory than mysql-store-result.
384;; .form (mysql-use-result mysql)
385;; .returns A MYSQL_RES result structure. NULL if an error occurred.
386(define mysql-use-result (c-function-wrap libmysqlclient void* mysql_use_result void*))
387
388;; Returns the number of warnings generated during execution of the previous SQL statement.
389;; .form (mysql-warning-count mysql-obj)
390;; .returns The warning count.
391(define mysql-warning-count (c-function-wrap libmysqlclient int mysql_warning_count void*))
392
393)
394