|
| 1 | +[](https://tldrlegal.com/license/bsd-3-clause-license-(revised)) |
| 2 | +[](https://hackage.haskell.org/package/postgresql-simple-bind) |
| 3 | +[](https://travis-ci.org/zohl/postgresql-simple-bind) |
| 4 | + |
1 | 5 | # postgresql-simple-bind |
2 | 6 |
|
3 | 7 | ## Description |
|
9 | 13 | communicates with a database via API hiding the internal structure |
10 | 14 | of the latter. |
11 | 15 |
|
| 16 | +## Status |
| 17 | +The library is quite stable. |
| 18 | +There are no plans to introduce breaking changes into the current API. |
| 19 | + |
12 | 20 | ## Example |
13 | 21 | Suppose we have the following functions in our database: |
14 | 22 |
|
15 | | - ``` |
| 23 | + ```sql |
16 | 24 | function add_num(p_x bigint) returns void |
17 | 25 | function get_all_nums() returns setof bigint |
18 | 26 | ``` |
19 | 27 |
|
20 | 28 | In order to use them in a haskell application we write the following code: |
21 | 29 |
|
22 | | - ``` |
23 | | - import Database.PostgreSQL.Simple.Bind (bindFunction, defaultOptions) |
| 30 | + ```haskell |
| 31 | + import Data.Default |
| 32 | + import Database.PostgreSQL.Simple.Bind (bindFunction, PostgresBindOptions) |
24 | 33 | import Database.PostgreSQL.Simple.Bind.Types() |
25 | 34 |
|
26 | | - bindFunction defaultOptions "function add_num(p_x bigint) returns void" |
27 | | - bindFunction defaultOptions "function get_all_nums() returns setof bigint" |
| 35 | + bindFunction (def::PostgresBindOptions) "function add_num(p_x bigint) returns void" |
| 36 | + bindFunction (def::PostgresBindOptions) "function get_all_nums() returns setof bigint" |
28 | 37 | ``` |
29 | 38 |
|
30 | 39 | That's it. Now we can stick them wherever we want to: |
31 | | - ``` |
| 40 | + ```haskell |
32 | 41 | add_many_nums :: Connection -> [Int] -> IO () |
33 | 42 | add_many_nums conn xs = sequence_ $ map (add_num conn) xs |
34 | 43 |
|
|
42 | 51 | then this type family provides us the final type. |
43 | 52 | For example `add_num` is translated the following way: |
44 | 53 |
|
45 | | - ``` |
| 54 | + ```haskell |
46 | 55 | -- original PostgreSQL declaration |
47 | | - function add_num(p_x bigint) returns void |
| 56 | + "function add_num(p_x bigint) returns void" |
48 | 57 |
|
49 | 58 | -- first step |
50 | 59 | add_num :: ( PostgresType "bigint" ~ a, ToField a |
|
55 | 64 | ``` |
56 | 65 |
|
57 | 66 | where |
58 | | - ``` |
| 67 | + ```haskell |
59 | 68 | type instance PostgresType "bigint" = Int |
60 | 69 | type instance PostgresType "void" = () |
61 | 70 | ``` |
|
91 | 100 | ## Customization |
92 | 101 | There are not so many ways to change behaviour of `bindFunction` (yet). |
93 | 102 | In the most cases the only required tweak is renaming stored functions. |
94 | | - This can be done by specifying `nameModifier` and `schemaModifier` options. |
| 103 | + This can be done by specifying `pboFunctionName` option. |
95 | 104 | For example if database and application code adhere snake case and camel case |
96 | 105 | naming conventions respectively, conversion can be made like this: |
97 | 106 |
|
98 | | - ``` |
| 107 | + ```haskell |
99 | 108 | import Text.CaseConversion |
100 | | - import Database.PostgreSQL.Simple.Bind (Options(..), defaultOptions) |
| 109 | + import Database.PostgreSQL.Simple.Bind (PostgresBindOptions(..)) |
101 | 110 |
|
102 | | - bindOptions :: Options |
103 | | - bindOptions = defaultOptions { |
104 | | - nameModifier = convertCase Snake Camel |
| 111 | + bindOptions :: PostgresBindOptions |
| 112 | + bindOptions = PostgresBindOptions { |
| 113 | + pboFunctionName = (\(PGFunction _schema name _args _result) -> convertCase Snake Camel name) |
105 | 114 | } |
106 | 115 | ``` |
107 | 116 |
|
|
112 | 121 | preBuild hook: set `build-type` to `Custom` in .cabal-file and define |
113 | 122 | `main` in Setup.hs like this |
114 | 123 |
|
115 | | - ``` |
116 | | - import Database.PostgreSQL.Simple.Bind.Util (generateBindingsModule) |
| 124 | + ```haskell |
| 125 | + import Database.PostgreSQL.Simple.Bind.Utils (generateBindingsModule) |
117 | 126 |
|
118 | 127 | main :: IO () |
119 | 128 | main = defaultMainWithHooks $ simpleUserHooks { preBuild = mkBindings } |
|
122 | 131 | mkBindings args buildFlags = do |
123 | 132 | conn <- connect connectInfo |
124 | 133 | (generateBindingsModule conn |
125 | | - "Database.PostgreSQL.Simple.Bind.defaultOptions" "Bindings" [ |
| 134 | + "Your.Module.customBindOptions" "Bindings" [ |
126 | 135 | "stored_function_1" |
127 | 136 | , "stored_function_2" |
128 | 137 | -- ... |
|
0 commit comments