-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathGeometry.hs
More file actions
71 lines (59 loc) · 2.05 KB
/
Copy pathGeometry.hs
File metadata and controls
71 lines (59 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{-|
Module : Database.PostgreSQL.Simple.Geometry
Description : Geometry types.
Copyright : (c) Alexander Vieth, 2015
Licence : BSD3
Maintainer : Leon P Smith <leon@melding-monads.com>
Stability : experimental
-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Database.PostgreSQL.Simple.Geometry (
Point(..)
, pointX
, pointY
) where
import Control.Applicative
import Data.Typeable
import Data.Attoparsec.ByteString.Char8 hiding (Result, char8)
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.Internal
import Database.PostgreSQL.Simple.Compat
import Database.PostgreSQL.Simple.Ok
import Database.PostgreSQL.Simple.Types
import Database.PostgreSQL.Simple.TypeInfo as TI
import qualified Database.PostgreSQL.Simple.TypeInfo.Static as TI
import Database.PostgreSQL.Simple.TypeInfo.Macro as TI
import Data.ByteString.Builder (byteString, char8)
data Point = Point {-# UNPACK #-} !Double {-# UNPACK #-} !Double
deriving (Eq, Ord, Typeable)
pointX :: Point -> Double
pointX (Point x _) = x
pointY :: Point -> Double
pointY (Point _ y) = y
instance FromField Point where
fromField f v =
if typeOid f /= $(inlineTypoid TI.point)
then returnError Incompatible f ""
else case v of
Nothing -> returnError UnexpectedNull f ""
Just bs ->
case parseOnly parser bs of
Left err -> returnError ConversionFailed f err
Right val -> pure val
where
parser = do
string "("
x <- double
string ","
y <- double
string ")"
return $ Point x y
instance ToField Point where
toField p = Many $
(Plain (byteString "point(")) :
(toField $ pointX p) :
(Plain (char8 ',')) :
(toField $ pointY p) :
[Plain (char8 ')')]