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. #f
(define sth (dbh.prepare db "select * from _Departement")) (sth.execute sth)
26
(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). #t ;; 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.
Cheers Christian.
;;p-dbi-module.scm------------------------------ (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..
;;p-dbi.scm------------------------------------- ;; cj Wed, 25 Jan 2006 19:08:57 +0100
(gperl-activate) (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) sv "connect") "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) 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) number/false)
(define-perl-method sth.fetchrow-array (sv) (#!rest string)) ;; return a list of strings
(define-perl-method dbh.begin-work (sv) boolean;;ok? ;string/false ) (define-perl-method dbh.commit (sv) boolean;;ok? ;string/false ) (define-perl-method dbh.rollback (sv) boolean;;ok? ;string/false )
(define-perl-method dbh.disconnect (sv) boolean)
(define-perl-method dbi.err (sv) number/false)
(define-perl-method dbi.errstr (sv) string/false)
(define-perl-method dbi.state (sv) string)
;; eof