Haskellday rf

1,963 views
1,864 views

Published on

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,963
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Haskellday rf

  1. 1. Persistent の使い方 ∼ Yesod で使っている Persistent を単体で使う話 ∼ rf
  2. 2. 自己紹介rf @rf0444最近ホットな言語 Haskell今読んでいる本 「Learn You a Haskell for Great Good!」 (10章)
  3. 3. Persistent Yesod を入れると一緒に入ってくるやつ DB 部分をやってくれる 型安全
  4. 4. Persistent 使えるDB SQLite MongoDB PostgreSQL MySQL (experimental)
  5. 5. インストール共通 cabal install persistent-templateSQLite cabal install persistent-sqliteMongoDB cabal install persistent-mongoDB
  6. 6. インストール共通 cabal install persistent-templatePostgreSQL cabal install persistent-postgresqlMySQL cabal install persistent-mysql
  7. 7. つないでみる
  8. 8. つないでみるSQLite {-# LANGUAGE OverloadedStrings #-} import Database.Persist.Sqlite main = withSqliteConn path $ runSqlConn $ do return () where path = "hogesql.sqlite3"
  9. 9. つないでみるMongoDB{-# LANGUAGE OverloadedStrings #-}import Database.Persist.MongoDBmain = withMongoDBConn dbname hostname $ runMongoDBConn master $ do return ()where hostname = "localhost" dbname = "test"
  10. 10. つないでみるPostgreSQL{-# LANGUAGE OverloadedStrings #-}import Database.Persist.Postgresqlmain = withPostgresqlConn conf $ runSqlConn $ do return ()where conf = "host=localhost port=5432 user=hoge dbname=hoge password=hoge"
  11. 11. つないでみるMySQL import Database.Persist.MySQL main = withMySQLConn conf $ runSqlConn $ do return () where conf = defaultConnectInfo { connectHost = "localhost" -- default connectPort = 3306 -- default connectUser = "hoge" -- default : “root” connectPassword = "hoge" -- default : no password connectDatabase = "hoge" -- default : “test” }
  12. 12. Model の定義Yesod の Model を使いたい import 部分に Yesod に依存する部分があるので、 そこだけ切り離す 言語拡張をコード側へ
  13. 13. Model の定義Yesod の Model.hs (SQLite) model Model where import Prelude import Yesod import Data.Text (Text) import Database.Persist.Quasi share [mkPersist sqlSettings, mkMigrate "migrateAll"] $(persistFileWith lowerCaseSettings "config/models")
  14. 14. Model の定義Model.hs (SQLite){-# LANGUAGE TypeFamilies, TemplateHaskell, FlexibleContexts, GADTs #-}model Model whereimport Data.Text (Text)import Database.Persist.Quasiimport Database.Persist.Sqliteimport Database.Persist.THshare [mkPersist sqlSettings, mkMigrate "migrateAll"] $(persistFileWith lowerCaseSettings "config/models")
  15. 15. Model の定義 Yesod の Model.hs (MongoDB)model Model whereimport Preludeimport Yesodimport Data.Text (Text)import Database.Persist.Quasiimport Database.Persist.MongoDBimport Language.Haskell.TH.Syntaxshare [mkPersist MkPersistSettings { mpsBackend = ConT Action }, mkMigrate "migrateAll"] $(persistFileWith lowerCaseSettings "config/models")
  16. 16. Model の定義 Model.hs (MongoDB){-# LANGUAGE TypeFamilies, TemplateHaskell, FlexibleContexts, GADTs #-}model Model whereimport Data.Text (Text)import Database.Persist.Quasiimport Database.Persist.MongoDBimport Database.Persist.THimport Language.Haskell.TH.Syntaxshare [mkPersist MkPersistSettings { mpsBackend = ConT Action }, mkMigrate "migrateAll"] $(persistFileWith lowerCaseSettings "config/models")
  17. 17. Model の定義config/model User email Text password Text Maybe UniqueUser email
  18. 18. マイグレーション実行時に、テーブルがなかったら作ったりしてくれるやつRDB 系で使える MongoDB も unique index とかやってくれればいい のに・・・
  19. 19. マイグレーションSQLite {-# LANGUAGE OverloadedStrings #-} import Database.Persist.Sqlite import Model main = withSqliteConn path $ runSqlConn $ do runMigration migrateAll return () where path = "hogesql.sqlite3"
  20. 20. マイグレーションSQLite $ ls -F Model.hs config/ main.hs
  21. 21. マイグレーションSQLite $ ls -F Model.hs config/ main.hs $ runhaskell main.hs
  22. 22. マイグレーションSQLite $ ls -F Model.hs config/ main.hs $ runhaskell main.hs Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"email" VARCHAR NOT NULL,"password" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("email"))
  23. 23. マイグレーションSQLite $ ls -F Model.hs config/ main.hs $ runhaskell main.hs Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"email" VARCHAR NOT NULL,"password" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("email")) $ ls -F Model.hs config/ hogesql.sqlite3 main.hs
  24. 24. マイグレーションSQLite $ ls -F Model.hs config/ main.hs $ runhaskell main.hs Migrating: CREATE TABLE "user"("id" INTEGER PRIMARY KEY,"email" VARCHAR NOT NULL,"password" VARCHAR NULL,CONSTRAINT "unique_user" UNIQUE ("email")) $ ls -F Model.hs config/ hogesql.sqlite3 main.hs $ sqlite3 hogesql.sqlite3 .table user
  25. 25. あとは {-# LANGUAGE OverloadedStrings #-} import Database.Persist.Sqlite import Model main = withSqliteConn path $ runSqlConn $ do runMigration migrateAll insert $ User { userEmail = "hoge@hoge.jp", userPassword = Just "hoge" } return () where path = "hogesql.sqlite3"
  26. 26. あとは$ runhaskell main.hs$ sqlite3 hogesql.sqlite3 "select * from user"1|hoge@hoge.jp|hoge

×