Mais conteúdo relacionado Semelhante a Integral - New O/R Mapper for Common Lisp (20) Integral - New O/R Mapper for Common Lisp1. New O/R Mapper
for Common Lisp
株式会社はてな
id:nitro_idiot
2014/01/23 Lisp Meet Up presented by Shibuya.lisp #13
7. 例: Postmodern
(defclass country ()!
((name :col-type string :initarg :name!
:reader country-name)!
(inhabitants :col-type integer :initarg :inhabitants!
:accessor country-inhabitants)!
(sovereign :col-type (or db-null string) :initarg :sovereign!
:accessor country-sovereign))!
(:metaclass dao-class)!
(:keys name))
8. 例: Postmodern
(defclass country ()!
((name :col-type string :initarg :name!
:reader country-name)!
(inhabitants :col-type integer :initarg :inhabitants!
:accessor country-inhabitants)!
(sovereign :col-type (or db-null string) :initarg :sovereign!
:accessor country-sovereign))!
(:metaclass dao-class)!
(:keys name))
CREATE TABLE country (!
name TEXT NOT NULL,!
inhabitants INTEGER NOT NULL,!
sovereign TEXT,!
PRIMARY KEY (name))
9. 例: Integral
(defclass country ()!
((name :col-type string :initarg :name!
:primary-key t!
:not-null t!
:reader country-name)!
(inhabitants :col-type integer :initarg :inhabitants!
:not-null t!
:accessor country-inhabitants)!
(sovereign :col-type string :initarg :sovereign!
:accessor country-sovereign))!
(:metaclass <dao-table-class>))
Integralでもほとんど同じ。
:metaclassを指定する
15. 既存のORMの問題
• こんなスキーマを開発初期から定義できる
わけねーだろーが!!
(defclass country ()!
((name :col-type string :initarg :name!
:reader country-name)!
(inhabitants :col-type integer :initarg :inhabitants!
:accessor country-inhabitants)!
(sovereign :col-type (or db-null string) :initarg :sovereign!
:accessor country-sovereign))!
(:metaclass dao-class)!
(:keys name))
21. マイグレーション
(defclass user ()!
((name :col-type text!
:initarg :name)!
(profile :col-type text!
:initarg :profile))!
(:metaclass <dao-table-class>))
(migrate-table 'user)!
;-> ALTER TABLE `user` ADD COLUMN `profile` TEXT AFTER `name`;!
;=> NIL
22. マイグレーション
(defclass user ()!
((id :col-type serial!
:primary-key t)!
(name :col-type (varchar 64)!
:initarg :name)!
(profile :col-type text!
:initarg :profile))!
(:metaclass <dao-table-class>))
(migrate-table 'user)!
;-> ALTER TABLE `user` DROP COLUMN `%oid`;!
;
ALTER TABLE `user` MODIFY COLUMN `name` VARCHAR(64);!
;
ALTER TABLE `user` ADD COLUMN `id` SERIAL NOT NULL PRIMARY
KEY FIRST;!
;=> NIL
29. :generate-slots
!
!
(defclass user () ()!
(:metaclass <dao-table-class>)!
(:generate-slots t))
DBからスキーマ定義を取ってきてスロット
をつっこむ
(make-instance 'user :name "Eitarow Fukamachi")!
;=> #<USER %oid: <unbound>>