(module mysql-test mzscheme
(require (lib "time.ss" "srfi" "19"))
(require (planet "sqld-mysql-internal.scm" ("oesterholt" "sqlid.plt" 1 2)))
(require (planet "sqli.scm" ("oesterholt" "sqlid.plt" 1 2)))
(define (displayp . args)
(define (p args)
(if (null? args)
(newline)
(begin
(display (car args))
(p (cdr args)))))
(p args))
(define (main . argv)
(let* ((sqld (sqld-mysql-new "user=test passwd=test host=localhost db=test"))
(sqli (sqli-connect sqld))
(results (sqli-query sqli "CREATE TABLE test (name varchar(1000),age numeric(4),nice boolean, dt datetime)"))
(i 0))
(displayp results)
(displayp (sqli-error? sqli) " - " (sqli-error-message sqli))
(displayp (sqli-version) " - " (sqli-driver-name sqld) " - " (sqli-driver-version sqld))
(displayp "select * from test - test")
(do
((a (sqli-fetchrow sqli) (sqli-fetchrow sqli)))
((eq? a #f) #t)
(display a))
(displayp)
(displayp "Transaction of insert statements")
(sqli-begin sqli)
(do
((i 1 (+ i 1)))
((> i 10) #t)
(sqli-query sqli "INSERT INTO test VALUES ($1, $2, $3, $4)" (string-append "row'" (number->string i)) i #t (current-date)))
(sqli-commit sqli)
(set! results (sqli-query sqli "SELECT * FROM test"))
(displayp (sqli-error? sqli) " - " (sqli-error-message sqli))
(displayp (sqli-fetchall sqli))
(displayp)
(sqli-begin sqli)
(sqli-query sqli "INSERT INTO test VALUES ('joost',42);SELEKT * FROM test")
(displayp (sqli-error? sqli) " - " (sqli-error-message sqli))
(if (sqli-error? sqli)
(sqli-rollback sqli)
(sqli-commit sqli))
(sqli-register sqli 'select
"SELECT $1 FROM test WHERE age $2 $3"
(let ((c 0))
(lambda (a)
(begin
(set! c (+ c 1))
(list c (car a))))))
(displayp sqli)
(displayp (sqli-exec sqli 'select 'name '> 2))
(displayp "FETCH 10:" (sqli-fetch sqli 10))
(sqli-register sqli 'select-convert
"SELECT * FROM test WHERE age=10"
'string 'integer 'boolean 'date)
(displayp sqli)
(displayp (sqli-exec sqli 'select-convert))
(displayp "FETCHING ALL:" (sqli-fetchall sqli))
(displayp "CLOSURE TEST")
(let ((clos (sqli-closure sqli)))
(displayp (clos 'exec 'select 'age '<= 2))
(displayp (clos 'fetchall))
(clos 'register 'count "SELECT COUNT(name) FROM test"
(lambda (row) (car row)))
(clos 'exec 'count) (displayp (clos 'fetchrow))
(clos 'register 'and "SELECT nice FROM test"
(lambda (row)
(let ((b (clos 'convert (car row) 'boolean)))
(list b))))
(displayp (clos 'exec 'and))
(displayp (clos 'fetchall))
(clos 'query "INSERT INTO test VALUES ('false',0,$1,$2)" #f (current-date))
(displayp (clos 'exec 'and))
(displayp (clos 'fetchall))
(clos 'hithere "errors"))
(displayp sqli)
(displayp (sqli-exec sqli 'and))
(displayp (sqli-fetchall sqli))
(sqli-disconnect sqli)
0))
(provide main))