|
| 1 | +# Description |
| 2 | + A FFI-like bindings for PostgreSQL strored functions. |
| 3 | + |
| 4 | + `postgresql-simple-bind` is an extension for `postgresql-simple` |
| 5 | + library that faciliates and automates bindings creation. This is |
| 6 | + especially useful in a design pattern where an application |
| 7 | + communicates with a database via API hiding the internal structure |
| 8 | + of the latter. |
| 9 | + |
| 10 | +# Example |
| 11 | + Suppose we have the following functions in our database: |
| 12 | + |
| 13 | + ``` |
| 14 | + function add_num(p_x bigint) returns void |
| 15 | + function get_all_nums() returns setof bigint |
| 16 | + ``` |
| 17 | + |
| 18 | + In order to use them in a haskell application we write the following code: |
| 19 | + |
| 20 | + ``` |
| 21 | + import Database.PostgreSQL.Simple.Bind (bindFunction, defaultOptions) |
| 22 | + import Database.PostgreSQL.Simple.Bind.Types() |
| 23 | + |
| 24 | + bindFunction defaultOptions "function add_num(p_x bigint) returns void" |
| 25 | + bindFunction defaultOptions "function get_all_nums() returns setof bigint" |
| 26 | + ``` |
| 27 | + |
| 28 | + That's it. Now we can stick them wherever we want to: |
| 29 | + ``` |
| 30 | + add_many_nums :: Connection -> [Int] -> IO () |
| 31 | + add_many_nums conn xs = sequence_ $ map (add_num conn) xs |
| 32 | + |
| 33 | + get_sum :: Connection -> IO Int |
| 34 | + get_sum conn = sum <$> (get_all_nums conn) |
| 35 | + ``` |
| 36 | + |
| 37 | +# Behind the scenes |
| 38 | + It worth to mention that type translation from PostrgeSQL language to haskell |
| 39 | + is two-step. Firstly, a PostgreSQL type mapped to `PostgresType` instance and |
| 40 | + then this type family provides us the final type. |
| 41 | + For example `add_num` is translated the following way: |
| 42 | + |
| 43 | + ``` |
| 44 | + -- original PostgreSQL declaration |
| 45 | + function add_num(p_x bigint) returns void |
| 46 | + |
| 47 | + -- first step |
| 48 | + add_num :: ( PostgresType "bigint" ~ a, ToField a |
| 49 | + , PostgresType "void" ~ b, FromField b) => Connection -> a -> IO b |
| 50 | + |
| 51 | + -- second step |
| 52 | + add_num :: Connection -> Int -> IO () |
| 53 | + ``` |
| 54 | + |
| 55 | + where |
| 56 | + ``` |
| 57 | + type instance PostgresType "bigint" = Int |
| 58 | + type instance PostgresType "void" = () |
| 59 | + ``` |
| 60 | + as they are defined in `Database.PostgreSQL.Simple.Bind.Types`. |
| 61 | + |
| 62 | + What if the provided instances give us unwanted types (e.g. `varchar` is |
| 63 | + mapped to `Text` while we want `String`)? This is why all the instances are |
| 64 | + defined into a separated module. We just do not import the module and define |
| 65 | + our own instances. |
| 66 | + |
| 67 | + |
| 68 | +# On types |
| 69 | + As we mentioned in the previous section there are certain restrictions on the |
| 70 | + types that can be used in `PostgresType` instances. |
| 71 | + |
| 72 | + One of them comes naturally: all argument and result types must be instances of |
| 73 | + `ToField` and `FromField` respectively. |
| 74 | + |
| 75 | + In case there is an argument with a default value, it's corresponding type |
| 76 | + will be wrapped into `Maybe` constructor. |
| 77 | + |
| 78 | + Complex types cannot be specified unless there are corresponding `FromRow` |
| 79 | + and/or `ToRow` instances. This means there is no support for `record` return |
| 80 | + type as it doesn't disclose any information on it's structure. |
| 81 | + |
| 82 | + Another caveat is about functions returning tables (or sets of composite |
| 83 | + types). There is no way to put `not null` constraint on the resulting columns, |
| 84 | + so such function can return result with `null` in any column. At the current |
| 85 | + moment this behaviour is not supported, so each function returning table is |
| 86 | + supposed to return non-`null`-values. |
| 87 | + |
| 88 | + |
| 89 | +# Customization |
| 90 | + There are not so much ways to change behaviour of `bindFunction` (yet). |
| 91 | + In the most cases the only required tweak is renaming stored functions. |
| 92 | + This can be done by specifying `nameModifier` and `schemaModifier` options. |
| 93 | + For example if database and application code adhere snake case and camel case |
| 94 | + naming conventions respectively, conversion can be made like this: |
| 95 | + |
| 96 | + ``` |
| 97 | + import Text.CaseConversion |
| 98 | + import Database.PostgreSQL.Simple.Bind (Options(..), defaultOptions) |
| 99 | + |
| 100 | + bindOptions :: Options |
| 101 | + bindOptions = defaultOptions { |
| 102 | + nameModifier = convertCase Snake Camel |
| 103 | + } |
| 104 | + ``` |
| 105 | + |
| 106 | +# Automated generation |
| 107 | + It can be tedious to manually maintain consistent function declarations |
| 108 | + across the codebase. More convenient way is to automatically generate module |
| 109 | + during the compilation time. In case of cabal it can be done by using |
| 110 | + preBuild hook: set `build-type` to `Custom` in .cabal-file and define |
| 111 | + `main` in Setup.hs like this |
| 112 | + |
| 113 | + ``` |
| 114 | + import Database.PostgreSQL.Simple.Bind.Util (generateBindingsModule) |
| 115 | + |
| 116 | + main :: IO () |
| 117 | + main = defaultMainWithHooks $ simpleUserHooks { preBuild = mkBindings } |
| 118 | + |
| 119 | + mkBindings :: Args -> BuildFlags -> IO HookedBuildInfo |
| 120 | + mkBindings args buildFlags = do |
| 121 | + conn <- connect connectInfo |
| 122 | + (generateBindingsModule conn |
| 123 | + "Database.PostgreSQL.Simple.Bind.defaultOptions" "Bindings" [ |
| 124 | + "stored_function_1" |
| 125 | + , "stored_function_2" |
| 126 | + -- ... |
| 127 | + ]) >>= (writeFile "./src/Bindings.hs") |
| 128 | + close conn |
| 129 | + return emptyHookedBuildInfo |
| 130 | + ``` |
| 131 | + |
| 132 | + Every time the build procedure is executed, there will be database |
| 133 | + lookup for function signatures. |
| 134 | + |
| 135 | + |
| 136 | + |
0 commit comments