clsqlの勉強 その2
clsqlでjoinをやってみよう、と思って、こんなコードを書いて、、、
def-view-classで複合主キーってどうやって指定するの?というところで詰まる。
(require :clsql) ;;1:1 (clsql:def-view-class test1 () ((test1-id :accessor test1-id :initarg :test1-id :type integer :db-kind :key :db-constraints (:not-null :unique)) (test1-string :accessor test1-string :initarg :test1-string :type (clsql:varchar 10)) (test1-test2-id :accessor test1-test2-id :initarg :test1-test2-id :type integer) (test1-test2 :accessor test1-test2 :db-kind :join :db-info (:join-class test2 :home-key test1-test2-id :foreign-key test2-id :set nil)))) (clsql:def-view-class test2 () ((test2-id :accessor test2-id :initarg :test2-id :type integer :db-kind :key :db-constraints (:not-null :unique)) (test2-string :accessor test2-string :initarg :test2-string :type (clsql:varchar 10)))) (clsql-sys:with-database (con '("localhost" "xxx" "xxx" nil) :if-exists :old :database-type :postgresql-socket) (ignore-errors (clsql:create-view-from-class 'test2 :database con))) (clsql-sys:with-database (con '("localhost" "xxx" "xxx" nil) :if-exists :old :database-type :postgresql-socket) (ignore-errors (clsql:create-view-from-class 'test1 :database con))) (defun test1-insert(id data test2id) (clsql:with-database (con '("localhost" "xxx" "xxx" nil) :if-exists :old :database-type :postgresql-socket) (let ((o (make-instance 'test1 :test1-id id :test1-string data :test1-test2-id test2id))) (clsql:update-records-from-instance o :database con)))) (defun test2-insert(id data) (clsql:with-database (con '("localhost" "xxx" "xxx" nil) :if-exists :old :database-type :postgresql-socket) (let ((o (make-instance 'test2 :test2-id id :test2-string data))) (clsql:update-records-from-instance o :database con)))) ;; (test1-insert 0 "あああ" 0) ;; (test1-insert 1 "いいい" 1) ;; (test1-insert 2 "いいい" 2) ;; (test2-insert 0 "2あああ") ;; (test2-insert 1 "2いいい") ;; (test2-insert 2 "2いいい") (clsql:file-enable-sql-reader-syntax) (clsql:with-database (con '("localhost" "xxx" "xxx" nil) :if-exists :old :database-type :postgresql-socket) (let ((result (clsql:select 'test1 :where [= [slot-value 'test1 'test1-id] 0] :flatp t :database con))) (mapc #'(lambda (o) (pprint (test1-id o)) (pprint (test1-string o)) (pprint (test1-test2-id o)) (pprint (test1-test2 o)) (pprint (test2-string (test1-test2 o)))) result))) ;;1:n (clsql:def-view-class header () ((header-id :accessor header-id :initarg :header-id :type integer :db-kind :key :db-constraints (:not-null :unique)) (header-data :accessor header-data :initarg :header-data :type (clsql:varchar 10)) (body :accessor body :db-kind :join :db-info (:join-class body :home-key header-id :foreign-key header-id :set t)))) (clsql:def-view-class body () ((header-id :accessor header-id :initarg :header-id :type integer :db-constraints (:primary-key :not-null)) (seq :accessor seq :initarg :seq :type integer :db-constraints (:primary-key :not-null)) (body-data :accessor body-data :initarg :body-data :type (clsql:varchar 10))))
ひょっとして対応してない?