Guest User

Untitled

a guest
Mar 1st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2. {-# LANGUAGE ConstraintKinds #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4.  
  5. module Example.Example15 where
  6.  
  7. import Control.Exception.Safe (SomeException, catch)
  8.  
  9. -- my library
  10. import DataBase.MySQLX.NodeSession
  11. import DataBase.MySQLX.ResultSet
  12. import DataBase.MySQLX.Statement
  13.  
  14. import qualified Data.ByteString.Lazy as BL
  15. import qualified Data.Sequence as Seq
  16. import Data.Maybe
  17.  
  18. import Data.Geometry.Geos.Geometry
  19. import Data.Geometry.Geos.Serialize
  20. import Data.Geometry.Geos.Types
  21.  
  22. {-
  23. create table points_tbl (`id` int unsigned not null auto_increment, `location` Point not null, primary key(`id`));
  24.  
  25. insert into points_tbl (location) values (point(1,1));
  26.  
  27. mysql-sql> select st_astext(location) from points_tbl;
  28. +---------------------+
  29. | st_astext(location) |
  30. +---------------------+
  31. | POINT(1 1) |
  32. +---------------------+
  33. 1 row in set (0.01 sec)
  34.  
  35. -}
  36.  
  37. data Geo = Geo {
  38. p :: Geometry Point
  39. } deriving Show
  40.  
  41. geo:: RowFrom Geo
  42. geo= Geo <$> colVal
  43.  
  44. getColGeoPoint :: Row -> Int -> Geometry Point
  45. getColGeoPoint row idx = getColGeoPoint' $ Seq.index row idx
  46.  
  47. getColGeoPoint' :: BL.ByteString -> Geometry Point
  48. getColGeoPoint' x = ensurePoint $ fromJust $ readWkb (BL.toStrict $ BL.drop 4 $ getColByteString' x)
  49.  
  50. getColGeoPoint'' :: BL.ByteString -> Geometry Point
  51. getColGeoPoint'' x = ensurePoint $ fromJust $ readWkb (BL.toStrict $ getColByteString' x)
  52.  
  53. instance ColumnValuable (Geometry Point) where toColVal' = getColGeoPoint'
  54.  
  55. example15 :: IO ()
  56. example15 = do
  57. nodeSess <- openNodeSession $ defaultNodeSesssionInfo {database = "x_protocol_test", user = "root", password="root", port=8000}
  58. catch ( do
  59. result <- executeRawSql "select location from points_tbl" nodeSess
  60. print $ rowFrom geo $ head result
  61. ) (\(e::SomeException) -> print e)
  62. closeNodeSession nodeSess
  63.  
  64. {-
  65. *Example.Example15> example15
  66. Geo {p = PointGeometry (Point (Coordinate2 1.0 1.0)) Nothing}
  67. *Example.Example15>
  68. -}
Add Comment
Please, Sign In to add comment