Hatena::ブログ(Diary)

sirocco の書いてもすぐに忘れるメモ

2009-09-07

Gauche DBI(Postgresql) の NULL は "" を返す。

| 11:17 |

Gauche DBI(Postgresql) の NULL は "" を返す。

(use dbi)
(use gauche.collection)
 
(define (select-db dbname sql)
  (guard (e ((<dbi-error> e) (raise e)))
    (let* ((conn   (dbi-connect (string-append "dbi:pg:dbname=" dbname)))
           (result (dbi-execute (dbi-prepare conn sql)))
           (getter (relation-accessor result)))
       (let ((dat (map (lambda (row)
                             (map (lambda (x) (getter row x)) 
                                  (relation-column-names result)))
                       result)))
          (dbi-close conn)
          dat))))

(define dbname "hogedb") 
(define sql "SELECT hoge FROM kaiin where hoge is NULL LIMIT 10") 

(let ((n (select-db dbname sql)))
  (print n)                      ;;=> (() () () () () () () () () ())
  (print (car n))                ;;=> ()
  (print (length (car n)))       ;;=> 1
  (let ((hoge (car (car n))))
    (print (string? hoge))       ;;=> #t
    (print (string-length hoge)) ;;=> 0
    (print (string=? "" hoge)))) ;;=> #t
トラックバック - http://d.hatena.ne.jp/sirocco/20090907/1252289846