carl douglas committed 8ea7017

Added mysql-null parameter and mysql-null? predicate for more flexible handling of NULL fields.

Comments (0)

Files changed (2)

 ;   user=root
 ;   password=secret
-; Note how MySQL (NULL) values are represented when
-; returned in an array of string pointers:
-; A (NULL) value is represented by a string containing 
-; a 0x04 0x00 char sequence.
-(module mysql-client (make-mysql-connection)
+(module mysql-client (make-mysql-connection mysql-null mysql-null?)
         (import scheme chicken foreign)
         (use irregex data-structures)
         (mysql-c-real-escape-string mysql-c 
           (alist-ref (irregex-match-substring r 0) stringified-keys string=?))))))
+(define mysql-null (make-parameter "(NULL)"))
+(define-external (mysql_null) c-string (mysql-null))
+(define (mysql-null? field)
+  (equal? (mysql-null) field))
 (foreign-declare "#include \"mysql.h\"")
 (define mysql-c-real-escape-string
 (define mysql-c-fetch-row
-  (foreign-lambda* c-string-list* ((c-pointer result))
+  (foreign-safe-lambda* c-string-list* ((c-pointer result))
   int num_fields = 0;
   int index = 0;
   for (;row && index--;) {
-    if (row[index] == NULL) 
-      fields[index] = strdup("\x04\x00");
+    if (row[index] == NULL)
+      fields[index] = strdup(mysql_null());
       fields[index] = strdup(row[index]);
           '(($name . "hell'o1")))
 (exec-sql "INSERT INTO scheme_test (created_at, name) VALUES (NOW(), '$name')" 
           '(($name . "hello%2")))
+(exec-sql "INSERT INTO scheme_test (created_at, name) VALUES (NOW(), NULL)") 
 (exec-sql "SELECT * FROM scheme_test")
+(assert (mysql-null? "(NULL)"))
+(mysql-null "(ANOTHER NULL)")
+(assert (mysql-null? "(ANOTHER NULL)"))
+(exec-sql "SELECT * FROM scheme_test")
 (exec-sql "DROP DATABASE chicken_scheme_mysql_client_test")
 (exec-sql "USE information_schema")