使用HaskellDB来取消映射表记录的基本示例 [英] Basic example of using HaskellDB to unmap records of a table

查看:131
本文介绍了使用HaskellDB来取消映射表记录的基本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下(PostgreSQL)表定义:

pre $ code CREATE TABLE books
id serial NOT NULL,
标题字符变化NOT NULL,

PRIMARY KEY(id)
);

以下记录定义:

  data Book = 
{id :: Int
,title :: String
}

什么是查询数据库中所有图书的unmap函数的基本示例, allBooks :: Database - > IO [Book]

解决方案

事实证明,我正在以这种错误的方式前进。

Mats Rauhala 极其有用的博客标题为使用HaskellDB的例子,我能够编写一个测试项目来读取 books 表中的记录。



我首先需要定义布局 ,它使用haskelldb-th并不算太坏:

  { - #LANGUAGE TemplateHaskell# - } 

module Tables.Books(
books
,id
,title
,Books
)其中

导入Database.HaskellDB。 CodeGen
import Prelude hiding(id)

mkDBDirectTableBooks[
(id,[t | Int |])
,(title, [t | String |])
]

从那里开始, allBooks 函数是:

  allBooks db = query db $ do 
books < - table B.books
返还书

其中 B 是限定名称导入模块 Tables.Books allBooks 的类型为:

 
allBooks ::数据库
- > IO
[Record
(Database.HaskellDB.HDBRec.RecCons
Tables.Books.Id
Int
(Database.HaskellDB.HDBRec.RecCons
Tables .Books.Title
String
Database.HaskellDB.HDBRec.RecNil))]

打印每个标题,我用:

  main :: IO()
main = do
books< - postgresqlConnect [(host,localhost),(user,test),(password,********)] allBooks
mapM_ putStrLn(map(\ r)> r!B.title)books)
return()

编辑:我创建了一个包含此示例完整源代码的git存储库: dtrebbien / haskelldb-example

Suppose that I have the following (PostgreSQL) table definition:

CREATE TABLE books (
    id serial NOT NULL,
    title character varying NOT NULL,

    PRIMARY KEY (id)
);

And the following record definition:

data Book = 
  { id :: Int
  , title :: String
  }

What is a basic example of an "unmap" function to query all books in the database, allBooks :: Database -> IO [Book]?

解决方案

It turns out that I was going about this the wrong way.

After stumbling upon Mats Rauhala's exceedingly helpful blog post titled Example on using HaskellDB, I was able to write a test project to read the records of the books table.

I first needed to define the "layout", which, using haskelldb-th, is not too bad:

{-# LANGUAGE TemplateHaskell #-}

module Tables.Books (
    books
  , id
  , title
  , Books
  ) where

import Database.HaskellDB.CodeGen
import Prelude hiding (id)

mkDBDirectTable "Books" [
    ("id", [t|Int|])
  , ("title", [t|String|])
  ]

From there, the allBooks function is:

allBooks db = query db $ do
    books <- table B.books
    return books

where B is the qualified name of imported module Tables.Books. allBooks has the type:

allBooks :: Database
            -> IO
                 [Record
                    (Database.HaskellDB.HDBRec.RecCons
                       Tables.Books.Id
                       Int
                       (Database.HaskellDB.HDBRec.RecCons
                          Tables.Books.Title
                          String
                          Database.HaskellDB.HDBRec.RecNil))]

To print out each title, I used:

main :: IO ()
main = do
    books <- postgresqlConnect [("host", "localhost"), ("user", "test"), ("password", "********")] allBooks
    mapM_ putStrLn (map (\r -> r!B.title) books)
    return ()

EDIT: I created a git repository containing the complete sources of this example: dtrebbien/haskelldb-example

这篇关于使用HaskellDB来取消映射表记录的基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆