Permalink: 2008-05-28 10:16:00+09:00 by tu1978 in tags: CLSQL

CLSQLを使って、Webアプリを作るという計画。hunchentoot, common-coldというライブラリを使ってみる。

何となくできそうだが、うまく動かない。コードは次のような感じ。

-----top.lisp-----
(require :asdf)
(push #P"/home/aaaa/bin/" asdf:*central-registry*)
(asdf:oos 'asdf:load-op 'common-cold)
(asdf:operate 'asdf:load-op 'clsql)
(asdf:operate 'asdf:load-op 'clsql-mysql)
(asdf:operate 'asdf:load-op 'clsql-sqlite3)
(load "kaimonoweb.lisp")
(hunchentoot:start-server :port 9877)

-----------kaimonoweb.lisp----------
(cl:in-package "COMMON-COLD")

(clsql:connect '("memory.db") :database-type :sqlite3)
(setf clsql:*default-caching* nil)
(clsql:enable-sql-reader-syntax)

(defun gregorian-to-date (lst) ;; '(5 1 2008)をclsql:dateに変換
(clsql:make-date :year (caddr lst) :month (car lst) :day (cadr lst)))
(defun date-to-mjd (date)
(clsql:gregorian-to-mjd `((month ,date) (day ,date) (year ,date))))
(defun mjd-to-date (mjd)
(gregorian-to-date (clsql:mjd-to-gregorian mjd)))

(clsql:def-view-class Kaimono ()
((id :accessor id-of :db-kind :key :db-constraints :not-null :type integer :initarg :id)
(item :accessor item-of :type (string 64) :initarg :item)
(cost :accessor cost-of :type integer :initarg :cost)
;; (date :accessor date-of :type clsql:date :initarg :date) ;; SQLite3 はDATE型がない!!
(date :accessor date-of :type clsql:date :db-type "integer" :initarg :date ;; INTEGERで保存して、DATE(MJD)に変換する
:db-writer clsql:date-mjd ;; #'は付けてはいけない!!
:db-reader mjd-to-date) ;; これは自前で定義
(kamoku-id :type integer :initarg :kamoku-id)
(shop-id :type integer :initarg :shop-id)
(shop :accessor shop-of :db-kind :join
:db-info (:join-class Shop :home-key shop-id :foreign-key id :set nil))
(info :accessor info-of :type (string 64)))
(:base-table Kaimonos))

(defun test (&optional (val 0))
(send/suspend (k)
(cl-who:with-html-output-to-string (*standard-output*)
(:html (:head (:title "kaimonos"))
(:body
(:a :href k "hoge")
(:table
(clsql:with-transaction ()
(let ((kaimonos (clsql:select 'Kaimono :where [< [slot-value 'Kaimono 'id] 100] :flatp t))) (loop for row in kaimonos do (format *standard-output* "~a~a~a~a~a~a" (id-of row) (item-of row) (cost-of row) (date-of row) (info-of row) (shop-of row)))))) )))) (test)) (ensure-all-builders) ; compile all the program fragments (push (hunchentoot:create-prefix-dispatcher "/kaimonoweb/" (make-continuation-handler 'test ; begin with test if URL "/kaimonoweb/" ; == this )) hunchentoot:*dispatch-table*)

SQLリーダシンタックスがうまく動作しない??

二つのファイルをフォームごとに上からREPLに渡していけばselect文が動作するが、上のように、一番目から二番目をloadすると、select文がリードマクロで展開されていないような気がする...。loadしてから、(cl:in-package "COMMON-COLD")してselectを実行すると、"[<"などがシンボルとして認識されてしまう。loadすると、トップレベルフォームがread, evalされるはずだから、clsql:enable-sql-reader-syntaxがevalされて、以後のselectがリードマクロで展開されると思っていたのだけれど。

しかも、一回loadしてしまうと、手でREPLに渡していってもselectが動作しなくなってしまうようだ....

それに加えて、cl-whoの使い方もまだ間違っているようだ...

Comments