(module sqld-oracle-internal mzscheme
(require (lib "time.ss" "srfi" "19"))
(require "c-sqld-oracle.scm")
(define-syntax integer->string
(syntax-rules ()
((_ n) (number->string n))))
(define-syntax string->integer
(syntax-rules ()
((_ s) (string->number s))))
(define (ierr . args)
(define (f args)
(if (null? args)
(display "")
(begin
(display (car args))
(f (cdr args)))))
(display "ERROR (sqld-oracle): ")
(f args)
(newline)
#f)
(define (string2db s)
(c-oracle-escape s))
(define (int2db n)
(integer->string n))
(define (number2db n)
(number->string n))
(define (date2db dt)
(string-append "timestamp'"
(date->string dt "~Y-~m-~d ~H:~M:~S")
"'"))
(define (bool2db b)
(if (eq? b #f)
"0"
"1"))
(define (db2bool b)
(if (= (string->integer b) 0)
#f
#t))
(define (db2date dt)
(string->date dt "~Y-~m-~d ~H:~M:~S")
)
(define (sqld-oracle-connect connection-string)
(let* ((dbh (c-oracle-open connection-string))
(valid-handle (string=? (c-oracle-lasterr dbh) ""))
(commit #t)
(sth 'nil))
(define (query q)
(set! sth (c-oracle-query dbh q)))
(define (fetch)
(if (c-oracle-eoq sth)
#f
(let ((row (c-oracle-fields sth)))
(c-oracle-fetch sth)
row)
))
(define (begin-transaction)
(c-oracle-autocommit-off dbh))
(define (commit)
(c-oracle-query dbh "COMMIT")
(c-oracle-autocommit-on dbh))
(define (rollback)
(c-oracle-query dbh "ROLLBACK")
(c-oracle-autocommit-on dbh))
(define (disconnect)
(c-oracle-close dbh)
(set! valid-handle #f))
(begin
(if (eq? valid-handle #t)
(begin
(c-oracle-autocommit-on dbh)
(query "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'")
(query "ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'")))
(lambda (cmd . args)
(if (eq? valid-handle #f)
(ierr "ERROR: disconnected handle")
(cond
((eq? cmd 'string2db) (string2db (car args)))
((eq? cmd 'int2db) (int2db (car args)))
((eq? cmd 'number2db) (number2db (car args)))
((eq? cmd 'date2db) (date2db (car args)))
((eq? cmd 'bool2db) (bool2db (car args)))
((eq? cmd 'db2bool) (db2bool (car args)))
((eq? cmd 'db2date) (db2date (car args)))
((eq? cmd 'fetchrow) (fetch))
((eq? cmd 'lasterr) (c-oracle-lasterr dbh))
((eq? cmd 'begin) (begin-transaction))
((eq? cmd 'commit) (commit))
((eq? cmd 'rollback) (rollback))
((eq? cmd 'query) (query (car args)))
((eq? cmd 'disconnect) (disconnect))
(else (ierr "Unknown command " cmd))))))))
(define (sqld-oracle-new _connection-info)
(let ((connection-info _connection-info))
(lambda (cmd . args)
(cond
((eq? cmd 'connect) (sqld-oracle-connect connection-info))
((eq? cmd 'clean) #t)
((eq? cmd 'name) "oracle")
((eq? cmd 'version) (c-oracle-version))
(else (ierr "ERROR: Connect to the datebase first"))))))
(provide sqld-oracle-new)
)