2015-06-14 44 views
1

我編寫了一些代碼來管理postgresql數據庫,並且它正在控制檯上工作。現在我想通過hunchentoot把它放在我的內部網絡上。Hunchentoot處理程序更改另一個函數的定義

我用CLSQL並封裝數據庫代碼爲:

(defpackage :my-database 
    (:use :common-lisp) 
    (:documentation "several lines...") 
    (:export almost-all-functions...)) 

(in-package #:my-database) 

(defun new-patient (name gender height weight) 
    "adds new patient to the db. all parameters are of type string." 
    (let* ((height (parse-integer height)) 
     (weight (parse-integer weight)) 
     (corrected-weight (calculate-corrected-weight height weight gender))) 
    (clsql:insert-records :into 'patients 
          :attributes '(height weight corrected-weight name gender patient-at-ward) 
         :values (list height weight corrected-weight name gender t)))) 

我進口我的數據庫包,當我用這個處理程序:

(define-easy-handler (debug :uri "/debug") 
    (name gender height weight) 
    (let ((ad (substitute #\space #\+ (string-trim " " ad)))) 
    (progn (format nil "Name:~A, Gender:~A, Height:~A, Weight:~A" name gender height weight) 
      (redirect "/success")))) 

它顯示HTML就好了。但這種處理程序:

(define-easy-handler (new-patient :uri "/new-patient") 
    (name gender height weight) 
    (let ((name (substitute #\space #\+ (string-trim " " name)))) 
    (progn (my-database:new-patient name gender height weight) 
      (redirect "/success")))) 

拋出一個錯誤:

Incorrect keyword arguments in ("Frank Sinatra" "male" "150" "55") 

我注意到,當我在REPL鍵入(my-database:new-patient),emacs的命令迷你緩衝區顯示(my-database:new-patient &key name gender height weight)&key部分看起來對我很可疑。因此,我切換到我的數據庫文件,做了修正emacs命令minibuffer顯示的slime-eval-last-expression-in-repl。

但是這一次,我得到了以下錯誤:

Too few arguments in call to #<Compiled-function MY-DATABASE:NEW-PATIENT #x15870D76>: 
0 arguments provided, at least 4 required. 

,並重新評估hunchentoot處理程序造成的emacs命令minibuffer中再次顯示&關鍵。最後,我改變了處理程序代碼(my-database:new-patient :name name :gender gender :height height :weight weight),這引發此錯誤:

8 arguments were provided, but at most 4 are accepted by the current global 
definition of NEW-PATIENT 

可能是什麼原因?

+1

注你不需要'let'形式中的'progn'。 –

回答

2

原因是define-easy-handler語義:

The resulting handler will be a Lisp function with the name name and keyword parameters named by the var symbols.

(根據hunchentoot文檔:http://weitz.de/hunchentoot/),所以你應該爲處理程序使用不同的名稱,例如:

(define-easy-handler (do-new-patient :uri "/new-patient") 
    (name gender height weight) 
(let ((name (substitute #\space #\+ (string-trim " " name)))) 
    (progn (my-database:new-patient name gender height weight) 
     (redirect "/success")))) 
相關問題