[gambit-list] Oracle

Christian christian at pflanze.mine.nu
Wed Jan 25 14:14:23 EST 2006

At 16:47 Uhr +0000 25.01.2006, Joel Reymont wrote:
>Thank you Christian!

You're welcome!

Actually, since I will need mysql connectivity relatively soon, and 
to make sure I didn't promise too much, I've written a quick DBI 
interface--see below.

>  (use p-dbi)
>  (define db (dbi.connect "DBI:mysql:EiD" "eid" "the-passwd"))
>  (define sth (dbh.prepare db "select * from _Departementss"))
>  (sth.execute sth)
DBD::mysql::st execute failed: Table 'EiD._Departementss' doesn't exist.
>  (define sth (dbh.prepare db "select * from _Departement"))
>  (sth.execute sth)
>  (sth.fetchrow-array sth)
("1" "Elektrotechnik" "2")
>  (sth.fetchrow-array sth)
("2" "Werkstoffe" "2")
>  (sth.fetchrow-array sth)
("3" "Architektur" "1")
>  (sth.fetchrow-array sth)
("4" "Physik" "3")
>  (sth.fetchrow-array sth)
("5" "Umweltnaturwissenschaften" "4")
>  (dbi.errstr sth)
#f  ;; no error.

;; optional (gc will take care of it too):
>  (dbh.disconnect db)
DBI::db=HASH(0x84f37dc)->disconnect invalidates 1 active statement 
handle (either destroy statement handles or call finish on them 
before disconnecting).
;; the warning is because I didn't finish reading the result set.

If you want the functions to throw exceptions instead of returning 
false for errors, the natural way would be to give DBI a 
RaiseError=>1 attribute. But currently gperl is missing a way to 
generate perl hashes. If you need those, and don't see how to add the 
necessary perl XS functions to gperl, ask me to do it.


(requires gperl)
(namespace "") ;; trick to avoid having to define an exports list, and
                ;; work around old bug present in chjmodule (doesn't
                ;; reimport bindings after module reloads (*)), helpful
                ;; during developement of a module.
                ;; (*) of course I shall fix chjmodule some time soon..

;; cj Wed, 25 Jan 2006 19:08:57 +0100

(perl-use "DBI")

; (define-perl-method dbi.connect (string/sv string/sv string/sv)
;   sv)
;; todo: implement hash handling for:  #!optional attrs-hash
(define (dbi.connect data-source username auth #!optional attr)
   (if attr
       (error "attr argument not yet supported, requires perl hash 
handling functions in gperl")
       ((perl-method (string/sv string/sv string/sv string/sv)
        "DBI" data-source username auth)))

;; (implement perl-object type checking on scheme side ? (using perl 
isa operation) )

(define-perl-method dbh.do (sv string/sv)
   string ;;ok?

; (define-perl-method dbh.selectall_arrayref (sv)
;   ;;todo: implement array handling
;   ..)

; selectall_hashref

; selectcol_arrayref

(define-perl-method dbh.selectrow-array (sv string/sv)
   (#!rest string)) ;; return a list of strings.

; selectrow_arrayref

; selectrow_hashref

(define-perl-method dbh.prepare (sv string/sv)

; (define-perl-method sth.execute (sv #!rest string/sv)
;   ;; handle return value as false-or-number-of-results.
;   )
; (define (sth.execute sth #!rest bind-values)
;   (let ((res (apply
;	      (perl-method (sv #!rest string/sv)
;			   sv
;			   "execute")
;	      (cons sth bind-values))))
;     (if (sv-true? res)
;	(sv->number res)
;	#f)))
;ehr actually I did take care of this already in gperl..:
(define-perl-method sth.execute (sv #!rest string/sv)

(define-perl-method sth.fetchrow-array (sv)
   (#!rest string)) ;; return a list of strings

(define-perl-method dbh.begin-work (sv)
(define-perl-method dbh.commit (sv)
(define-perl-method dbh.rollback (sv)

(define-perl-method dbh.disconnect (sv)

(define-perl-method dbi.err (sv)

(define-perl-method dbi.errstr (sv)

(define-perl-method dbi.state (sv)

;; eof

More information about the Gambit-list mailing list