SQLite3を使ったCLSQLの実験。買い物を整理するデータベースのつもり。クラスを定義する



(clsql:def-view-class Kaimono ()
((id :accessor id-of :db-kind :key :db-constraints :not-null :type integer :initarg :id)
(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) ;; これは自前で定義
(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)))
(:base-table Kaimonos))
(clsql:def-view-class Shop ()
((id :accessor id-of :db-kind :key :db-constraints :not-null :type integer :initarg :id)
(kaimonos :db-kind :join
:db-info (:join-class Kaimono :home-key id :foreign-key shop-id :set nil))
(name :accessor name-of :type (string 64) :initarg :name))
(:base-table Shops))

(defun mjd-to-date (mjd)
(gregorian-to-date (clsql:mjd-to-gregorian mjd)))

クラスKaimono, Shopを定義し、Kaimonos, Shopsテーブルにマップする。苦戦したのが、Kaimonoのdateスロット。SQLite3は日付型が無いので、とりあえず数値として日付を保存することにした。で、dateスロット自体は、CLSQL:DATE型とする。DBに書き込む/から読み出すときに、数値<->オブジェクトの変換を行う。そのために、db-writer, db-readerを使う。コメントにも書いたが、使用する関数には#'を付けてはいけないらしい(CL初心者...)。


なんとかオブジェクトで出し入れができるようになってきた。

Comments