1
1
//// 🎂Cake 🐘PostgreSQL adapter which passes `PreparedStatement`s
2
- //// to the `gleam_pgo ` library for execution.
2
+ //// to the `pog ` library for execution.
3
3
////
4
4
5
5
import cake . {
@@ -13,11 +13,11 @@ import cake/param.{
13
13
import gleam/dynamic . { type DecodeError , type Dynamic }
14
14
import gleam/list
15
15
import gleam/option . { type Option }
16
- import gleam/pgo . { type Connection , type QueryError , type Returned , type Value }
16
+ import pog . { type Connection , type QueryError , type Returned , type Value }
17
17
18
18
/// Connection to a PostgreSQL database.
19
19
///
20
- /// This is a thin wrapper around the `gleam_pgo ` library's `Connection` type.
20
+ /// This is a thin wrapper around the `pog ` library's `Connection` type.
21
21
///
22
22
pub fn with_connection (
23
23
host host : String ,
@@ -28,18 +28,18 @@ pub fn with_connection(
28
28
callback callback : fn ( Connection ) -> a,
29
29
) -> a {
30
30
let connection =
31
- pgo . Config (
32
- .. pgo . default_config ( ) ,
31
+ pog . Config (
32
+ .. pog . default_config ( ) ,
33
33
host : host ,
34
34
port : port ,
35
35
user : username ,
36
36
password : password ,
37
37
database : database ,
38
38
)
39
- |> pgo . connect
39
+ |> pog . connect
40
40
41
41
let value = callback ( connection )
42
- pgo . disconnect ( connection )
42
+ pog . disconnect ( connection )
43
43
44
44
value
45
45
}
@@ -63,7 +63,7 @@ pub fn write_query_to_prepared_statement(
63
63
pub fn run_read_query (
64
64
query query : ReadQuery ,
65
65
decoder decoder : fn ( Dynamic ) -> Result ( a, List ( DecodeError ) ) ,
66
- db_connection db_connection : Connection ,
66
+ db_connection on : Connection ,
67
67
) {
68
68
let prepared_statement = query |> read_query_to_prepared_statement
69
69
let sql_string = prepared_statement |> cake . get_sql
@@ -74,10 +74,13 @@ pub fn run_read_query(
74
74
75
75
let result =
76
76
sql_string
77
- |> pgo . execute ( on : db_connection , with : db_params , expecting : decoder )
77
+ |> pog . query
78
+ |> pog_parameters ( db_params : )
79
+ |> pog . returning ( decoder )
80
+ |> pog . execute ( on : on )
78
81
79
82
case result {
80
- Ok ( pgo . Returned ( _result_count , v ) ) -> Ok ( v )
83
+ Ok ( pog . Returned ( _result_count , v ) ) -> Ok ( v )
81
84
Error ( e ) -> Error ( e )
82
85
}
83
86
}
@@ -87,7 +90,7 @@ pub fn run_read_query(
87
90
pub fn run_write_query (
88
91
query query : WriteQuery ( a) ,
89
92
decoder decoder : fn ( Dynamic ) -> Result ( a, List ( DecodeError ) ) ,
90
- db_connection db_connection : Connection ,
93
+ db_connection on : Connection ,
91
94
) -> Result ( List ( a) , QueryError ) {
92
95
let prepared_statement = query |> write_query_to_prepared_statement
93
96
let sql_string = prepared_statement |> cake . get_sql
@@ -98,10 +101,13 @@ pub fn run_write_query(
98
101
99
102
let result =
100
103
sql_string
101
- |> pgo . execute ( on : db_connection , with : db_params , expecting : decoder )
104
+ |> pog . query
105
+ |> pog_parameters ( db_params : )
106
+ |> pog . returning ( decoder )
107
+ |> pog . execute ( on : on )
102
108
103
109
case result {
104
- Ok ( pgo . Returned ( _result_count , v ) ) -> Ok ( v )
110
+ Ok ( pog . Returned ( _result_count , v ) ) -> Ok ( v )
105
111
Error ( e ) -> Error ( e )
106
112
}
107
113
}
@@ -125,18 +131,29 @@ pub fn run_query(
125
131
126
132
pub fn execute_raw_sql (
127
133
sql_string sql_string : String ,
128
- db_connection db_connection : Connection ,
129
- ) -> Result ( Returned ( Dynamic ) , QueryError ) {
134
+ db_connection on : Connection ,
135
+ ) -> Result ( Returned ( Nil ) , QueryError ) {
130
136
sql_string
131
- |> pgo . execute ( on : db_connection , with : [ ] , expecting : dynamic . dynamic )
137
+ |> pog . query
138
+ |> pog . execute ( on : )
132
139
}
133
140
134
141
fn cake_param_to_client_param ( param param : Param ) -> Value {
135
142
case param {
136
- BoolParam ( param ) -> pgo . bool ( param )
137
- FloatParam ( param ) -> pgo . float ( param )
138
- IntParam ( param ) -> pgo . int ( param )
139
- StringParam ( param ) -> pgo . text ( param )
140
- NullParam -> pgo . null ( )
143
+ BoolParam ( param ) -> pog . bool ( param )
144
+ FloatParam ( param ) -> pog . float ( param )
145
+ IntParam ( param ) -> pog . int ( param )
146
+ StringParam ( param ) -> pog . text ( param )
147
+ NullParam -> pog . null ( )
141
148
}
142
149
}
150
+
151
+ fn pog_parameters (
152
+ pog_query pg_qry : pog . Query ( a) ,
153
+ db_params db_params : List ( pog . Value ) ,
154
+ ) -> pog . Query ( a) {
155
+ db_params
156
+ |> list . fold ( pg_qry , fn ( pg_qry , db_param ) {
157
+ pg_qry |> pog . parameter ( db_param )
158
+ } )
159
+ }
0 commit comments