Commits

carl douglas committed d4de756

Use irregex instead of regex.

  • Participants
  • Parent commits 303f0ee

Comments (0)

Files changed (3)

File mysql-client.meta

  (category db)
  (author "A. Carl Douglas")
  (synopsis "A MySQL client library.")
- (depends regex)
  (files "mysql-client.scm" "mysql-client.setup"))

File mysql-client.scm

 
 (module mysql-client (make-mysql-connection)
         (import scheme chicken foreign)
-        (use srfi-1 regex)
+        (use irregex data-structures)
 
 (define (make-mysql-connection host user pass database)
   (define mysql-c (make-mysql-c-connection host user pass database))
   (proc conn parameters))
 
 (define (escape-placeholder-params conn query parameters)
-  (let ((escaped-parameters (map (lambda(x)
-                                   (cons (string-append "\\" (symbol->string (car x))) (mysql-c-real-escape-string conn (cdr x))))
-                                 parameters)))
-       (string-substitute* query escaped-parameters)))
+  (let ((escaped-parameters 
+          (map (lambda(x)
+                 (cons (symbol->string (car x)) (mysql-c-real-escape-string conn (cdr x))))
+               parameters)))
+       (irregex-replace/all 
+         (flatten (list 'or (map (lambda(x) (car x)) escaped-parameters)))
+         query
+         (lambda (r) 
+           (alist-ref (irregex-match-substring r 0) escaped-parameters string=?)))))
 
 (foreign-declare "#include \"mysql.h\"")
 

File tests/run.scm

 (if (not (pointer? (mysql (lambda(c . a) c))))
   (error "closure did not dispatch connection object"))
 
-(define fetch2 (mysql "select * from tables where table_name = '$1' or table_name = '$2'" 
-                      '(($1 . "reads")($2 . "'unknown'"))))
+(define fetch2 
+  (mysql "select * from tables where table_name = '$1' or table_name = '$foo'"
+ '(($1 . "reads")($foo . "'unknown'"))))
 (printf "~A~%" (fetch2))