rawSql
爲您提供了原始動力!有了這個,你可以從字面上做任何事情。在你的情況,這應該工作:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
age Int
deriving Show
|]
getUser :: MonadIO m => ReaderT SqlBackend m [Entity User]
getUser = rawSql "select ?? from user where age * ? > ?" [PersistInt64 2, PersistInt64 40]
main :: IO()
main = runSqlite "/home/sibi/test.db" $ do
runMigration migrateAll
johnId <- insert $ User 40
janeId <- insert $ User 41
users <- getUser
liftIO $ print (users :: [Entity User])
爲了更多地瞭解特殊的佔位符?
和??
,閱讀documentation here。我在那裏增加了一個很大的例子來說明問題。讓我知道你是否仍然有麻煩。
上面的代碼將產生一個這樣的輸出:
$ ./sqlitePersist
[Entity {entityKey = UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 1}}, entityVal = User {userAge = 40}},Entity {entityKey = UserKey {unUserKey = SqlBackendKey {unSqlBackendKey = 2}}, entityVal = User {userAge = 41}}]