| @@ -59,9 +59,7 @@ pub type QueryResult { |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | /// A row: its columns and its values in text format (`None` is `NULL`). |
| 62 | | −pub type Row { |
| 63 | | − Row(columns: List<protocol.Field>, values: List<Option<Bytes>>) |
| 64 | | −} |
| 62 | +pub type Row { Row(columns: List<protocol.Field>, values: List<Option<Bytes>>) } |
| 65 | 63 | |
| 66 | 64 | /// The SQLSTATE of a server error, if it has one. |
| 67 | 65 | pub fn sqlstate(e: Error) -> Option<String> { |
| @@ -75,7 +73,8 @@ pub fn sqlstate(e: Error) -> Option<String> { |
| 75 | 73 | pub fn message(e: Error) -> String { |
| 76 | 74 | match e { |
| 77 | 75 | Io(m) -> m, |
| 78 | | − Server(fields) -> option.unwrap_or(field(fields, "M"), debug_fields(fields)), |
| 76 | + Server(fields) -> |
| 77 | + option.unwrap_or(field(fields, "M"), debug_fields(fields)), |
| 79 | 78 | Driver(m) -> m, |
| 80 | 79 | } |
| 81 | 80 | } |
| @@ -102,7 +101,13 @@ type Conn { |
| 102 | 101 | /// A connection's socket after the handshake: the connection, the read |
| 103 | 102 | /// buffer, the settings, and the backend key data (for `CancelRequest`). |
| 104 | 103 | pub type Socket { |
| 105 | | − Socket(conn: Conn, buffer: Bytes, host: String, port: Int, backend: Option<(Int, Int)>) |
| 104 | + Socket( |
| 105 | + conn: Conn |
| 106 | + , buffer: Bytes |
| 107 | + , host: String |
| 108 | + , port: Int |
| 109 | + , backend: Option<(Int, Int)> |
| 110 | + ) |
| 106 | 111 | } |
| 107 | 112 | |
| 108 | 113 | /// Connect and authenticate, without the actor: the socket, for the |
| @@ -116,7 +121,8 @@ pub fn connect_socket(cfg: Config) -> Result<Socket, Error> / <io, random> { |
| 116 | 121 | |
| 117 | 122 | /// Connect and authenticate. The connection is an actor; stop it with |
| 118 | 123 | /// `close`. |
| 119 | | −pub fn connect(cfg: Config) -> Result<Subject<Msg>, Error> / <io, proc, random> { |
| 124 | +pub fn connect(cfg: Config) -> Result<Subject<Msg>, Error> |
| 125 | + / <io, proc, random> { |
| 120 | 126 | use socket <- result.try(connect_socket(cfg)) |
| 121 | 127 | Ok(connect_over(socket)) |
| 122 | 128 | } |
| @@ -133,27 +139,34 @@ fn state_of(socket: Socket) -> State { |
| 133 | 139 | |
| 134 | 140 | /// TLS if the mode wants it: `SslRequest`, then the handshake if the |
| 135 | 141 | /// server agrees. `Require` refuses a plain answer. |
| 136 | | −fn maybe_tls(tcp: Resource<TcpStream>, cfg: Config) -> Result<Conn, Error> / <io> { |
| 142 | +fn maybe_tls(tcp: Resource<TcpStream>, cfg: Config) -> Result<Conn, Error> |
| 143 | + / <io> { |
| 137 | 144 | match cfg.tls { |
| 138 | 145 | Disable -> Ok(Tcp(tcp)), |
| 139 | 146 | mode -> { |
| 140 | | − use _ <- result.try(net.write(tcp, protocol.encode(protocol.SslRequest)) |> result.map_error(Io)) |
| 147 | + use _ <- result.try( |
| 148 | + net.write(tcp, protocol.encode(protocol.SslRequest)) |
| 149 | + |> result.map_error(Io) |
| 150 | + ) |
| 141 | 151 | use answer <- result.try(net.read(tcp, 1) |> result.map_error(Io)) |
| 142 | 152 | match bytes.to_list(answer) { |
| 143 | | − [byte] -> |
| 144 | | − match protocol.ssl_answer(byte) { |
| 145 | | − Some(protocol.Upgrade) -> { |
| 146 | | − use tls_config <- result.try(tls.config(tls.defaults()) |> result.map_error(tls_io)) |
| 147 | | − use stream <- result.try(tls.connect(tcp, cfg.host, tls_config) |> result.map_error(tls_io)) |
| 148 | | − Ok(Tls(stream)) |
| 149 | | − }, |
| 150 | | − Some(protocol.Plain) -> |
| 151 | | − match mode { |
| 152 | | − Require -> Error(Driver("the server does not do TLS (sslmode=require)")), |
| 153 | | − _ -> Ok(Tcp(tcp)), |
| 154 | | − }, |
| 155 | | − None -> Error(Driver("a byte that is not an SSL answer")), |
| 153 | + [byte] -> match protocol.ssl_answer(byte) { |
| 154 | + Some(protocol.Upgrade) -> { |
| 155 | + use tls_config <- result.try( |
| 156 | + tls.config(tls.defaults()) |> result.map_error(tls_io) |
| 157 | + ) |
| 158 | + use stream <- result.try( |
| 159 | + tls.connect(tcp, cfg.host, tls_config) |> result.map_error(tls_io) |
| 160 | + ) |
| 161 | + Ok(Tls(stream)) |
| 156 | 162 | }, |
| 163 | + Some(protocol.Plain) -> match mode { |
| 164 | + Require -> |
| 165 | + Error(Driver("the server does not do TLS (sslmode=require)")), |
| 166 | + _ -> Ok(Tcp(tcp)), |
| 167 | + }, |
| 168 | + None -> Error(Driver("a byte that is not an SSL answer")), |
| 169 | + }, |
| 157 | 170 | _ -> Error(Io("the server closed the connection")), |
| 158 | 171 | } |
| 159 | 172 | } |
| @@ -167,13 +180,27 @@ fn tls_io(e: tls.TlsError) -> Error { |
| 167 | 180 | /// The startup message, then authenticate until `ReadyForQuery`. The |
| 168 | 181 | /// buffer's leftover bytes come back (the server sent nothing extra in |
| 169 | 182 | /// practice), with the backend key data if it arrived. |
| 170 | | −fn handshake(conn: Conn, cfg: Config) -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 171 | | − use _ <- result.try(write_msg(conn, protocol.Startup(cfg.user, cfg.database, cfg.application))) |
| 172 | | − use (buffer, at, backend) <- result.try(auth_loop(conn, bytes.empty(), 0, cfg, None)) |
| 183 | +fn handshake(conn: Conn, cfg: Config) -> Result< |
| 184 | + (Bytes, Option<(Int, Int)>) |
| 185 | +, Error |
| 186 | +> |
| 187 | + / <io, random> { |
| 188 | + use _ <- result.try( |
| 189 | + write_msg(conn, protocol.Startup(cfg.user, cfg.database, cfg.application)) |
| 190 | + ) |
| 191 | + use (buffer, at, backend) <- result.try( |
| 192 | + auth_loop(conn, bytes.empty(), 0, cfg, None) |
| 193 | + ) |
| 173 | 194 | Ok((bytes.slice(buffer, at, bytes.length(buffer)), backend)) |
| 174 | 195 | } |
| 175 | 196 | |
| 176 | | −fn auth_loop(conn: Conn, buffer: Bytes, at: Int, cfg: Config, backend: Option<(Int, Int)>) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 197 | +fn auth_loop( |
| 198 | + conn: Conn |
| 199 | +, buffer: Bytes |
| 200 | +, at: Int |
| 201 | +, cfg: Config |
| 202 | +, backend: Option<(Int, Int)> |
| 203 | +) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 177 | 204 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 178 | 205 | match msg { |
| 179 | 206 | protocol.AuthenticationOk -> auth_loop(conn, buffer, at, cfg, backend), |
| @@ -182,11 +209,14 @@ fn auth_loop(conn: Conn, buffer: Bytes, at: Int, cfg: Config, backend: Option<(I |
| 182 | 209 | auth_loop(conn, buffer, at, cfg, backend) |
| 183 | 210 | }, |
| 184 | 211 | protocol.AuthenticationSasl(mechanisms) -> { |
| 185 | | − use (buffer, at) <- result.try(scram_exchange(conn, buffer, at, cfg, mechanisms)) |
| 212 | + use (buffer, at) <- result.try( |
| 213 | + scram_exchange(conn, buffer, at, cfg, mechanisms) |
| 214 | + ) |
| 186 | 215 | auth_loop(conn, buffer, at, cfg, backend) |
| 187 | 216 | }, |
| 188 | 217 | protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, cfg, backend), |
| 189 | | − protocol.BackendKeyData(pid, secret) -> auth_loop(conn, buffer, at, cfg, Some((pid, secret))), |
| 218 | + protocol.BackendKeyData(pid, secret) -> |
| 219 | + auth_loop(conn, buffer, at, cfg, Some((pid, secret))), |
| 190 | 220 | protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, cfg, backend), |
| 191 | 221 | protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)), |
| 192 | 222 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| @@ -196,18 +226,26 @@ fn auth_loop(conn: Conn, buffer: Bytes, at: Int, cfg: Config, backend: Option<(I |
| 196 | 226 | |
| 197 | 227 | /// A cleartext password, over TLS or to localhost only. |
| 198 | 228 | fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 199 | | − let safe = |
| 200 | | − match conn { |
| 201 | | − Tls(_) -> True, |
| 202 | | − Tcp(_) -> |
| 203 | | − cfg.host == "localhost" || cfg.host == "127.0.0.1" || cfg.host == "::1", |
| 204 | | − } |
| 229 | + let safe = match conn { |
| 230 | + Tls(_) -> True, |
| 231 | + Tcp( |
| 232 | + _ |
| 233 | + ) -> |
| 234 | + cfg.host == "localhost" || cfg.host == "127.0.0.1" || cfg.host == "::1", |
| 235 | + } |
| 205 | 236 | if !safe { |
| 206 | | − Error(Driver("the server wants a cleartext password, which needs TLS or localhost")) |
| 237 | + Error( |
| 238 | + Driver( |
| 239 | + "the server wants a cleartext password, which needs TLS or localhost" |
| 240 | + ) |
| 241 | + ) |
| 207 | 242 | } else { |
| 208 | 243 | use password <- result.try( |
| 209 | | − option.to_result(cfg.password, "the server wants a password; the URL has none") |
| 210 | | − |> result.map_error(Driver), |
| 244 | + option.to_result( |
| 245 | + cfg.password |
| 246 | + , "the server wants a password; the URL has none" |
| 247 | + ) |
| 248 | + |> result.map_error(Driver) |
| 211 | 249 | ) |
| 212 | 250 | write_msg(conn, protocol.Password(password)) |
| 213 | 251 | } |
| @@ -215,25 +253,32 @@ fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 215 | 253 | |
| 216 | 254 | /// SCRAM-SHA-256, if the server offers it. |
| 217 | 255 | fn scram_exchange( |
| 218 | | − conn: Conn, |
| 219 | | − buffer: Bytes, |
| 220 | | − at: Int, |
| 221 | | − cfg: Config, |
| 222 | | − mechanisms: List<String>, |
| 256 | + conn: Conn |
| 257 | +, buffer: Bytes |
| 258 | +, at: Int |
| 259 | +, cfg: Config |
| 260 | +, mechanisms: List<String> |
| 223 | 261 | ) -> Result<(Bytes, Int), Error> / <io, random> { |
| 224 | 262 | if !list.any(mechanisms, fn(m) { m == "SCRAM-SHA-256" }) { |
| 225 | 263 | Error(Driver("the server offers no SCRAM-SHA-256")) |
| 226 | 264 | } else { |
| 227 | 265 | use password <- result.try( |
| 228 | | − option.to_result(cfg.password, "the server wants a password; the URL has none") |
| 229 | | − |> result.map_error(Driver), |
| 266 | + option.to_result( |
| 267 | + cfg.password |
| 268 | + , "the server wants a password; the URL has none" |
| 269 | + ) |
| 270 | + |> result.map_error(Driver) |
| 230 | 271 | ) |
| 231 | 272 | let nonce = base64.encode(random.bytes(18)) |
| 232 | 273 | let (state, first) = scram.client_first(nonce) |
| 233 | | − use _ <- result.try(write_msg(conn, protocol.SaslInitialResponse("SCRAM-SHA-256", first))) |
| 274 | + use _ <- result.try( |
| 275 | + write_msg(conn, protocol.SaslInitialResponse("SCRAM-SHA-256", first)) |
| 276 | + ) |
| 234 | 277 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 235 | 278 | use (state, reply) <- result.try(scram_continue(msg, state, password)) |
| 236 | | − use _ <- result.try(write_msg(conn, protocol.SaslResponse(bytes.from_string(reply)))) |
| 279 | + use _ <- result.try( |
| 280 | + write_msg(conn, protocol.SaslResponse(bytes.from_string(reply))) |
| 281 | + ) |
| 237 | 282 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 238 | 283 | use _ <- result.try(scram_final(msg, state)) |
| 239 | 284 | Ok((buffer, at)) |
| @@ -243,16 +288,16 @@ fn scram_exchange( |
| 243 | 288 | /// The server-first step: the client-final message and the state the |
| 244 | 289 | /// final step verifies with. |
| 245 | 290 | fn scram_continue( |
| 246 | | − msg: protocol.In, |
| 247 | | − state: scram.Scram, |
| 248 | | − password: String, |
| 291 | + msg: protocol.In |
| 292 | +, state: scram.Scram |
| 293 | +, password: String |
| 249 | 294 | ) -> Result<(scram.Scram, String), Error> { |
| 250 | 295 | match msg { |
| 251 | | − protocol.SaslContinue(data) -> |
| 252 | | − match bytes.to_string(data) { |
| 253 | | − Ok(text) -> scram.client_final(state, password, text) |> result.map_error(Driver), |
| 254 | | − Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 255 | | − }, |
| 296 | + protocol.SaslContinue(data) -> match bytes.to_string(data) { |
| 297 | + Ok(text) -> |
| 298 | + scram.client_final(state, password, text) |> result.map_error(Driver), |
| 299 | + Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 300 | + }, |
| 256 | 301 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 257 | 302 | _ -> Error(Driver("expected a SASL continuation")), |
| 258 | 303 | } |
| @@ -262,11 +307,10 @@ fn scram_continue( |
| 262 | 307 | /// server knew the password too). |
| 263 | 308 | fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 264 | 309 | match msg { |
| 265 | | − protocol.SaslFinal(data) -> |
| 266 | | − match bytes.to_string(data) { |
| 267 | | − Ok(text) -> scram.verify_server(state, text) |> result.map_error(Driver), |
| 268 | | − Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 269 | | − }, |
| 310 | + protocol.SaslFinal(data) -> match bytes.to_string(data) { |
| 311 | + Ok(text) -> scram.verify_server(state, text) |> result.map_error(Driver), |
| 312 | + Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")), |
| 313 | + }, |
| 270 | 314 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 271 | 315 | _ -> Error(Driver("expected a SASL final")), |
| 272 | 316 | } |
| @@ -280,14 +324,14 @@ fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 280 | 324 | /// #145 §1). |
| 281 | 325 | type State { |
| 282 | 326 | State( |
| 283 | | − conn: Conn, |
| 284 | | − buffer: Bytes, |
| 285 | | − at: Int, |
| 286 | | − host: String, |
| 287 | | − port: Int, |
| 288 | | − backend: Option<(Int, Int)>, |
| 289 | | − prepared: Dict<String, Prepared>, |
| 290 | | − next_statement: Int, |
| 327 | + conn: Conn |
| 328 | + , buffer: Bytes |
| 329 | + , at: Int |
| 330 | + , host: String |
| 331 | + , port: Int |
| 332 | + , backend: Option<(Int, Int)> |
| 333 | + , prepared: Dict<String, Prepared> |
| 334 | + , next_statement: Int |
| 291 | 335 | ) |
| 292 | 336 | } |
| 293 | 337 | |
| @@ -299,7 +343,11 @@ type Prepared { |
| 299 | 343 | |
| 300 | 344 | /// The connection's messages. |
| 301 | 345 | pub type Msg { |
| 302 | | − DoQuery(sql: String, params: List<Value>, reply: Subject<Result<QueryResult, Error>>) |
| 346 | + DoQuery( |
| 347 | + sql: String |
| 348 | + , params: List<Value> |
| 349 | + , reply: Subject<Result<QueryResult, Error>> |
| 350 | + ) |
| 303 | 351 | /// A liveness check: a bare `Sync`, answered by `ReadyForQuery` — the |
| 304 | 352 | /// cheapest round trip the protocol has. It fails when the connection |
| 305 | 353 | /// is not idle (a pooled connection has no transaction open). The actor |
| @@ -336,95 +384,141 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 336 | 384 | |
| 337 | 385 | /// A bare `Sync`, then read to `ReadyForQuery`, which must report the |
| 338 | 386 | /// connection idle: a pooled connection has no transaction open. |
| 339 | | −fn ping(conn: Conn, buffer: Bytes, at: Int) -> (Result<(), Error>, Bytes, Int) / <io> { |
| 387 | +fn ping(conn: Conn, buffer: Bytes, at: Int) -> (Result<(), Error>, Bytes, Int) |
| 388 | + / <io> { |
| 340 | 389 | match write_msg(conn, protocol.Sync) { |
| 341 | 390 | Error(e) -> (Error(e), bytes.empty(), 0), |
| 342 | 391 | Ok(()) -> ping_read(conn, buffer, at, None), |
| 343 | 392 | } |
| 344 | 393 | } |
| 345 | 394 | |
| 346 | | −fn ping_read(conn: Conn, buffer: Bytes, at: Int, error: Option<Error>) -> (Result<(), Error>, Bytes, Int) / <io> { |
| 395 | +fn ping_read(conn: Conn, buffer: Bytes, at: Int, error: Option<Error>) -> ( |
| 396 | + Result<(), Error> |
| 397 | +, Bytes |
| 398 | +, Int |
| 399 | +) |
| 400 | + / <io> { |
| 347 | 401 | match read_message(conn, buffer, at) { |
| 348 | 402 | Error(e) -> (Error(e), bytes.empty(), 0), |
| 349 | | − Ok((msg, buffer, at)) -> |
| 350 | | − match msg { |
| 351 | | − protocol.ReadyForQuery(status) -> |
| 352 | | − match error { |
| 353 | | − Some(e) -> (Error(e), buffer, at), |
| 354 | | − None -> |
| 355 | | − match status { |
| 356 | | − protocol.Idle -> (Ok(()), buffer, at), |
| 357 | | − _ -> (Error(Driver("the connection has a transaction open")), buffer, at), |
| 358 | | − }, |
| 359 | | − }, |
| 360 | | − protocol.ErrorResponse(fields) -> ping_read(conn, buffer, at, Some(Server(fields))), |
| 361 | | − _ -> ping_read(conn, buffer, at, error), |
| 403 | + Ok((msg, buffer, at)) -> match msg { |
| 404 | + protocol.ReadyForQuery(status) -> match error { |
| 405 | + Some(e) -> (Error(e), buffer, at), |
| 406 | + None -> match status { |
| 407 | + protocol.Idle -> (Ok(()), buffer, at), |
| 408 | + _ -> ( |
| 409 | + Error(Driver("the connection has a transaction open")) |
| 410 | + , buffer |
| 411 | + , at |
| 412 | + ), |
| 413 | + }, |
| 362 | 414 | }, |
| 415 | + protocol.ErrorResponse(fields) -> |
| 416 | + ping_read(conn, buffer, at, Some(Server(fields))), |
| 417 | + _ -> ping_read(conn, buffer, at, error), |
| 418 | + }, |
| 363 | 419 | } |
| 364 | 420 | } |
| 365 | 421 | |
| 366 | 422 | /// A query: the statement is prepared once per connection (Parse + |
| 367 | 423 | /// Describe, one exchange), then Bind + Execute. Parameters travel as |
| 368 | 424 | /// `$1`, binary per type where a codec exists (binary.lse). |
| 369 | | −fn run_query(state: State, sql: String, params: List<Value>) -> (Result<QueryResult, Error>, State) / <io> { |
| 425 | +fn run_query(state: State, sql: String, params: List<Value>) -> ( |
| 426 | + Result<QueryResult, Error> |
| 427 | +, State |
| 428 | +) |
| 429 | + / <io> { |
| 370 | 430 | match prepare(state, sql) { |
| 371 | 431 | (Error(e), state) -> (Error(e), state), |
| 372 | | − (Ok(prep), state) -> |
| 373 | | − if list.length(params) != list.length(prep.param_oids) { |
| 374 | | − ( |
| 375 | | − Error(Driver("the statement takes {list.length(prep.param_oids)} parameters, got {list.length(params)}")), |
| 376 | | − state, |
| 432 | + (Ok(prep), state) -> if list.length(params) |
| 433 | + != list.length(prep.param_oids) { |
| 434 | + ( |
| 435 | + Error( |
| 436 | + Driver( |
| 437 | + "the statement takes {list.length( |
| 438 | + prep.param_oids |
| 439 | + )} parameters, got {list.length(params)}" |
| 440 | + ) |
| 377 | 441 | ) |
| 378 | | − } else { |
| 379 | | − let encoded = encode_params(params, prep.param_oids, []) |
| 380 | | − let formats = list.map(prep.fields, fn(f) { if binary_known(f.type_oid) { 1 } else { 0 } }) |
| 381 | | − let out = [ |
| 382 | | − protocol.Bind("", prep.name, encoded, formats), |
| 383 | | − protocol.DescribePortal(""), |
| 384 | | − protocol.Execute("", 0), |
| 385 | | − protocol.Sync, |
| 386 | | − ] |
| 387 | | − match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 388 | | − Error(e) -> (Error(e), state), |
| 389 | | − Ok(()) -> { |
| 390 | | − let (res, buffer, at) = collect(state.conn, state.buffer, state.at, [], [], "", None) |
| 391 | | − (res, State(..state, buffer: buffer, at: at)) |
| 392 | | − }, |
| 393 | | − } |
| 394 | | − }, |
| 442 | + , state |
| 443 | + ) |
| 444 | + } else { |
| 445 | + let encoded = encode_params(params, prep.param_oids, []) |
| 446 | + let formats = list.map( |
| 447 | + prep.fields |
| 448 | + , fn(f) { if binary_known(f.type_oid) { 1 } else { 0 } } |
| 449 | + ) |
| 450 | + let out = [ |
| 451 | + protocol.Bind("", prep.name, encoded, formats) |
| 452 | + , protocol.DescribePortal("") |
| 453 | + , protocol.Execute("", 0) |
| 454 | + , protocol.Sync |
| 455 | + ] |
| 456 | + match conn_write( |
| 457 | + state.conn |
| 458 | + , bytes.concat_all(list.map(out, protocol.encode)) |
| 459 | + ) { |
| 460 | + Error(e) -> (Error(e), state), |
| 461 | + Ok(()) -> { |
| 462 | + let (res, buffer, at) = collect( |
| 463 | + state.conn |
| 464 | + , state.buffer |
| 465 | + , state.at |
| 466 | + , [] |
| 467 | + , [] |
| 468 | + , "" |
| 469 | + , None |
| 470 | + ) |
| 471 | + (res, State(..state, buffer: buffer, at: at)) |
| 472 | + }, |
| 473 | + } |
| 474 | + }, |
| 395 | 475 | } |
| 396 | 476 | } |
| 397 | 477 | |
| 398 | 478 | /// The statement prepared on this connection: from the cache, or by Parse |
| 399 | 479 | /// + Describe (one exchange). |
| 400 | | −fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) / <io> { |
| 480 | +fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) |
| 481 | + / <io> { |
| 401 | 482 | match dict.get(state.prepared, sql) { |
| 402 | 483 | Some(prep) -> (Ok(prep), state), |
| 403 | 484 | None -> { |
| 404 | 485 | let name = "q{state.next_statement}" |
| 405 | 486 | let out = [ |
| 406 | | − protocol.Parse(name, sql), |
| 407 | | − protocol.DescribeStatement(name), |
| 408 | | − protocol.Sync, |
| 487 | + protocol.Parse(name, sql) |
| 488 | + , protocol.DescribeStatement(name) |
| 489 | + , protocol.Sync |
| 409 | 490 | ] |
| 410 | | − match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 491 | + match conn_write( |
| 492 | + state.conn |
| 493 | + , bytes.concat_all(list.map(out, protocol.encode)) |
| 494 | + ) { |
| 411 | 495 | Error(e) -> (Error(e), state), |
| 412 | | − Ok(()) -> |
| 413 | | − match read_describe(state.conn, state.buffer, state.at, [], [], None) { |
| 414 | | − (Error(e), buffer, at) -> (Error(e), State(..state, buffer: buffer, at: at)), |
| 415 | | − (Ok((oids, fields)), buffer, at) -> { |
| 416 | | − let prep = Prepared(name, oids, fields) |
| 417 | | − ( |
| 418 | | − Ok(prep), |
| 419 | | − State(..state, |
| 420 | | − buffer: buffer, |
| 421 | | − at: at, |
| 422 | | − prepared: dict.insert(state.prepared, sql, prep), |
| 423 | | − next_statement: state.next_statement + 1, |
| 424 | | − ), |
| 496 | + Ok(()) -> match read_describe( |
| 497 | + state.conn |
| 498 | + , state.buffer |
| 499 | + , state.at |
| 500 | + , [] |
| 501 | + , [] |
| 502 | + , None |
| 503 | + ) { |
| 504 | + (Error(e), buffer, at) -> ( |
| 505 | + Error(e) |
| 506 | + , State(..state, buffer: buffer, at: at) |
| 507 | + ), |
| 508 | + (Ok((oids, fields)), buffer, at) -> { |
| 509 | + let prep = Prepared(name, oids, fields) |
| 510 | + ( |
| 511 | + Ok(prep) |
| 512 | + , State( |
| 513 | + ..state |
| 514 | + , buffer: buffer |
| 515 | + , at: at |
| 516 | + , prepared: dict.insert(state.prepared, sql, prep) |
| 517 | + , next_statement: state.next_statement + 1 |
| 425 | 518 | ) |
| 426 | | − }, |
| 519 | + ) |
| 427 | 520 | }, |
| 521 | + }, |
| 428 | 522 | } |
| 429 | 523 | }, |
| 430 | 524 | } |
| @@ -433,55 +527,75 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) / <io> |
| 433 | 527 | /// Read the answers to Parse + Describe until `ReadyForQuery`: the |
| 434 | 528 | /// parameters' type OIDs and the result columns. |
| 435 | 529 | fn read_describe( |
| 436 | | − conn: Conn, |
| 437 | | − buffer: Bytes, |
| 438 | | − at: Int, |
| 439 | | − param_oids: List<Int>, |
| 440 | | − fields: List<protocol.Field>, |
| 441 | | − error: Option<Error>, |
| 530 | + conn: Conn |
| 531 | +, buffer: Bytes |
| 532 | +, at: Int |
| 533 | +, param_oids: List<Int> |
| 534 | +, fields: List<protocol.Field> |
| 535 | +, error: Option<Error> |
| 442 | 536 | ) -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes, Int) / <io> { |
| 443 | 537 | match read_message(conn, buffer, at) { |
| 444 | 538 | Error(e) -> (Error(e), bytes.empty(), 0), |
| 445 | | − Ok((msg, buffer, at)) -> |
| 446 | | − match msg { |
| 447 | | − protocol.ParameterDescription(oids) -> read_describe(conn, buffer, at, oids, fields, error), |
| 448 | | − protocol.RowDescription(f) -> read_describe(conn, buffer, at, param_oids, f, error), |
| 449 | | − protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error), |
| 450 | | − protocol.ErrorResponse(f) -> read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))), |
| 451 | | − protocol.ReadyForQuery(_) -> |
| 452 | | − match error { |
| 453 | | − Some(e) -> (Error(e), buffer, at), |
| 454 | | − None -> (Ok((param_oids, fields)), buffer, at), |
| 455 | | − }, |
| 456 | | − _ -> read_describe(conn, buffer, at, param_oids, fields, error), |
| 457 | | − } |
| 539 | + Ok((msg, buffer, at)) -> match msg { |
| 540 | + protocol.ParameterDescription(oids) -> |
| 541 | + read_describe(conn, buffer, at, oids, fields, error), |
| 542 | + protocol.RowDescription(f) -> |
| 543 | + read_describe(conn, buffer, at, param_oids, f, error), |
| 544 | + protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error), |
| 545 | + protocol.ErrorResponse(f) -> |
| 546 | + read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))), |
| 547 | + protocol.ReadyForQuery(_) -> match error { |
| 548 | + Some(e) -> (Error(e), buffer, at), |
| 549 | + None -> (Ok((param_oids, fields)), buffer, at), |
| 550 | + }, |
| 551 | + _ -> read_describe(conn, buffer, at, param_oids, fields, error), |
| 552 | + } |
| 458 | 553 | } |
| 459 | 554 | } |
| 460 | 555 | |
| 461 | 556 | fn collect( |
| 462 | | − conn: Conn, |
| 463 | | − buffer: Bytes, |
| 464 | | − at: Int, |
| 465 | | − columns: List<protocol.Field>, |
| 466 | | − rows: List<Row>, |
| 467 | | − tag: String, |
| 468 | | − error: Option<Error>, |
| 557 | + conn: Conn |
| 558 | +, buffer: Bytes |
| 559 | +, at: Int |
| 560 | +, columns: List<protocol.Field> |
| 561 | +, rows: List<Row> |
| 562 | +, tag: String |
| 563 | +, error: Option<Error> |
| 469 | 564 | ) -> (Result<QueryResult, Error>, Bytes, Int) / <io> { |
| 470 | 565 | match read_message(conn, buffer, at) { |
| 471 | 566 | Error(e) -> (Error(e), bytes.empty(), 0), |
| 472 | | − Ok((msg, buffer, at)) -> |
| 473 | | − match msg { |
| 474 | | − protocol.RowDescription(fields) -> collect(conn, buffer, at, fields, rows, tag, error), |
| 475 | | − protocol.DataRow(values) -> collect(conn, buffer, at, columns, [Row(columns, values), ..rows], tag, error), |
| 476 | | − protocol.CommandComplete(t) -> collect(conn, buffer, at, columns, rows, t, error), |
| 477 | | − protocol.ErrorResponse(fields) -> collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))), |
| 478 | | − protocol.ReadyForQuery(_) -> |
| 479 | | − match error { |
| 480 | | − Some(e) -> (Error(e), buffer, at), |
| 481 | | − None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at), |
| 482 | | − }, |
| 483 | | − _ -> collect(conn, buffer, at, columns, rows, tag, error), // ParseComplete, BindComplete, NoData, ... |
| 484 | | − } |
| 567 | + Ok((msg, buffer, at)) -> match msg { |
| 568 | + protocol.RowDescription(fields) -> |
| 569 | + collect(conn, buffer, at, fields, rows, tag, error), |
| 570 | + protocol.DataRow(values) -> |
| 571 | + collect( |
| 572 | + conn |
| 573 | + , buffer |
| 574 | + , at |
| 575 | + , columns |
| 576 | + , [Row(columns, values), ..rows] |
| 577 | + , tag |
| 578 | + , error |
| 579 | + ), |
| 580 | + protocol.CommandComplete(t) -> |
| 581 | + collect(conn, buffer, at, columns, rows, t, error), |
| 582 | + protocol.ErrorResponse(fields) -> |
| 583 | + collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))), |
| 584 | + protocol.ReadyForQuery(_) -> match error { |
| 585 | + Some(e) -> (Error(e), buffer, at), |
| 586 | + None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at), |
| 587 | + }, |
| 588 | + _ -> |
| 589 | + collect( |
| 590 | + conn |
| 591 | + , buffer |
| 592 | + , at |
| 593 | + , columns |
| 594 | + , rows |
| 595 | + , tag |
| 596 | + , error |
| 597 | + ), // ParseComplete, BindComplete, NoData, ... |
| 598 | + } |
| 485 | 599 | } |
| 486 | 600 | } |
| 487 | 601 | |
| @@ -491,26 +605,36 @@ fn collect( |
| 491 | 605 | /// the buffer and the offset parsing resumes at. A message already whole |
| 492 | 606 | /// in the buffer is parsed in place (nothing is copied); one that |
| 493 | 607 | /// straddles reads pays once for moving its tail into a builder. |
| 494 | | −fn read_message(conn: Conn, buffer: Bytes, at: Int) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 608 | +fn read_message(conn: Conn, buffer: Bytes, at: Int) -> Result< |
| 609 | + (protocol.In, Bytes, Int) |
| 610 | +, Error |
| 611 | +> |
| 612 | + / <io> { |
| 495 | 613 | match protocol.next(buffer, at) { |
| 496 | | − protocol.Message(msg, past) -> |
| 497 | | − if past == bytes.length(buffer) { |
| 498 | | − Ok((msg, bytes.empty(), 0)) |
| 499 | | − } else { |
| 500 | | − Ok((msg, buffer, past)) |
| 501 | | − }, |
| 614 | + protocol.Message(msg, past) -> if past == bytes.length(buffer) { |
| 615 | + Ok((msg, bytes.empty(), 0)) |
| 616 | + } else { |
| 617 | + Ok((msg, buffer, past)) |
| 618 | + }, |
| 502 | 619 | protocol.Malformed(e) -> Error(Driver(e)), |
| 503 | | − protocol.Incomplete -> fill(conn, bytes.slice(buffer, at, bytes.length(buffer))), |
| 620 | + protocol.Incomplete -> |
| 621 | + fill(conn, bytes.slice(buffer, at, bytes.length(buffer))), |
| 504 | 622 | } |
| 505 | 623 | } |
| 506 | 624 | |
| 507 | 625 | /// The rest of a message that straddles reads: chunks accumulate in a |
| 508 | 626 | /// builder (appending does not copy what was appended before) until the |
| 509 | 627 | /// frame is whole. |
| 510 | | −fn fill(conn: Conn, tail: Bytes) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 628 | +fn fill(conn: Conn, tail: Bytes) -> Result<(protocol.In, Bytes, Int), Error> |
| 629 | + / <io> { |
| 511 | 630 | use tail <- result.try(fill_header(conn, tail)) |
| 512 | | − use total <- result.try(protocol.frame_total(tail, 0) |> result.map_error(Driver)) |
| 513 | | − let b = bytes.append(bytes.builder_with_capacity(bytes.length(tail) + 65536), tail) |
| 631 | + use total <- result.try( |
| 632 | + protocol.frame_total(tail, 0) |> result.map_error(Driver) |
| 633 | + ) |
| 634 | + let b = bytes.append( |
| 635 | + bytes.builder_with_capacity(bytes.length(tail) + 65536) |
| 636 | + , tail |
| 637 | + ) |
| 514 | 638 | fill_loop(conn, b, bytes.length(tail), total) |
| 515 | 639 | } |
| 516 | 640 | |
| @@ -528,13 +652,20 @@ fn fill_header(conn: Conn, tail: Bytes) -> Result<Bytes, Error> / <io> { |
| 528 | 652 | } |
| 529 | 653 | } |
| 530 | 654 | |
| 531 | | −fn fill_loop(conn: Conn, b: bytes.Builder, have: Int, total: Int) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 655 | +fn fill_loop(conn: Conn, b: bytes.Builder, have: Int, total: Int) -> Result< |
| 656 | + (protocol.In, Bytes, Int) |
| 657 | +, Error |
| 658 | +> |
| 659 | + / <io> { |
| 532 | 660 | if have >= total { |
| 533 | 661 | let buffer = bytes.build(b) |
| 534 | 662 | match protocol.next(buffer, 0) { |
| 535 | 663 | protocol.Message(msg, past) -> Ok((msg, buffer, past)), |
| 536 | 664 | protocol.Malformed(e) -> Error(Driver(e)), |
| 537 | | − protocol.Incomplete -> Error(Driver("protocol: a whole frame did not parse (a bug in the codec)")), |
| 665 | + protocol.Incomplete -> |
| 666 | + Error( |
| 667 | + Driver("protocol: a whole frame did not parse (a bug in the codec)") |
| 668 | + ), |
| 538 | 669 | } |
| 539 | 670 | } else { |
| 540 | 671 | use chunk <- result.try(conn_read(conn, 65536)) |
| @@ -585,26 +716,47 @@ pub type Notification { |
| 585 | 716 | /// parked on the socket. Stop it with `process.kill` (a supervisor |
| 586 | 717 | /// restarts it: it re-`LISTEN`s). Quoting: the channel is an identifier, |
| 587 | 718 | /// sent as `LISTEN "channel"`. |
| 588 | | −pub fn listen(cfg: Config, channel: String, subscriber: Subject<Notification>) -> Result<Pid, Error> / <io, proc, random> { |
| 719 | +pub fn listen( |
| 720 | + cfg: Config |
| 721 | +, channel: String |
| 722 | +, subscriber: Subject<Notification> |
| 723 | +) -> Result<Pid, Error> / <io, proc, random> { |
| 589 | 724 | use socket <- result.try(connect_socket(cfg)) |
| 590 | 725 | let Socket(conn, buffer, ..) = socket |
| 591 | 726 | use (buffer, at) <- result.try(listen_command(conn, buffer, 0, channel)) |
| 592 | | − Ok(process.spawn(fn() { listen_serve(conn, buffer, at, channel, subscriber) })) |
| 727 | + Ok( |
| 728 | + process.spawn(fn() { listen_serve(conn, buffer, at, channel, subscriber) }) |
| 729 | + ) |
| 593 | 730 | } |
| 594 | 731 | |
| 595 | 732 | /// `LISTEN "channel"`, answered by `CommandComplete` and `ReadyForQuery`. |
| 596 | | −fn listen_command(conn: Conn, buffer: Bytes, at: Int, channel: String) -> Result<(Bytes, Int), Error> / <io> { |
| 597 | | − use _ <- result.try(write_msg(conn, protocol.Query("LISTEN {quoted(channel)}"))) |
| 733 | +fn listen_command( |
| 734 | + conn: Conn |
| 735 | +, buffer: Bytes |
| 736 | +, at: Int |
| 737 | +, channel: String |
| 738 | +) -> Result<(Bytes, Int), Error> / <io> { |
| 739 | + use _ <- result.try( |
| 740 | + write_msg(conn, protocol.Query("LISTEN {quoted(channel)}")) |
| 741 | + ) |
| 598 | 742 | drain_until_ready(conn, buffer, at) |
| 599 | 743 | } |
| 600 | 744 | |
| 601 | 745 | /// An identifier in double quotes (inner quotes doubled). |
| 602 | 746 | fn quoted(name: String) -> String { |
| 603 | | − "\"" <> string.concat(list.map(string.chars(name), fn(c) { if c == "\"" { "\"\"" } else { c} })) <> "\"" |
| 747 | + "\"" |
| 748 | + <> string.concat( |
| 749 | + list.map(string.chars(name), fn(c) { if c == "\"" { "\"\"" } else { c } }) |
| 750 | + ) |
| 751 | + <> "\"" |
| 604 | 752 | } |
| 605 | 753 | |
| 606 | 754 | /// Read until `ReadyForQuery` (the simple query protocol's end). |
| 607 | | −fn drain_until_ready(conn: Conn, buffer: Bytes, at: Int) -> Result<(Bytes, Int), Error> / <io> { |
| 755 | +fn drain_until_ready(conn: Conn, buffer: Bytes, at: Int) -> Result< |
| 756 | + (Bytes, Int) |
| 757 | +, Error |
| 758 | +> |
| 759 | + / <io> { |
| 608 | 760 | use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 609 | 761 | match msg { |
| 610 | 762 | protocol.ReadyForQuery(_) -> Ok((buffer, at)), |
| @@ -615,12 +767,22 @@ fn drain_until_ready(conn: Conn, buffer: Bytes, at: Int) -> Result<(Bytes, Int), |
| 615 | 767 | |
| 616 | 768 | /// Read forever: notifications for our channel go to the subscriber. A |
| 617 | 769 | /// broken socket stops the listener. |
| 618 | | −fn listen_serve(conn: Conn, buffer: Bytes, at: Int, channel: String, subscriber: Subject<Notification>) -> () / <io, proc> { |
| 770 | +fn listen_serve( |
| 771 | + conn: Conn |
| 772 | +, buffer: Bytes |
| 773 | +, at: Int |
| 774 | +, channel: String |
| 775 | +, subscriber: Subject<Notification> |
| 776 | +) -> () / <io, proc> { |
| 619 | 777 | match read_message(conn, buffer, at) { |
| 620 | 778 | Error(_) -> (), |
| 621 | 779 | Ok((msg, buffer, at)) -> { |
| 622 | 780 | match msg { |
| 623 | | − protocol.NotificationResponse(source, ch, payload) -> { |
| 781 | + protocol.NotificationResponse( |
| 782 | + source |
| 783 | + , ch |
| 784 | + , payload |
| 785 | + ) -> { |
| 624 | 786 | if ch == channel { |
| 625 | 787 | process.send(subscriber, Notification(ch, payload, source)) |
| 626 | 788 | } |
| @@ -637,9 +799,9 @@ fn listen_serve(conn: Conn, buffer: Bytes, at: Int, channel: String, subscriber: |
| 637 | 799 | /// Parameters encoded per their inferred types: binary where a codec |
| 638 | 800 | /// exists for the OID (binary.lse), text otherwise. `Null` has no format. |
| 639 | 801 | fn encode_params( |
| 640 | | − params: List<Value>, |
| 641 | | − oids: List<Int>, |
| 642 | | − acc: List<protocol.BindParam>, |
| 802 | + params: List<Value> |
| 803 | +, oids: List<Int> |
| 804 | +, acc: List<protocol.BindParam> |
| 643 | 805 | ) -> List<protocol.BindParam> { |
| 644 | 806 | match (params, oids) { |
| 645 | 807 | ([], _) -> list.reverse(acc), |
| @@ -652,44 +814,38 @@ fn encode_params( |
| 652 | 814 | fn encode_one(value: Value, oid: Int) -> protocol.BindParam { |
| 653 | 815 | match value { |
| 654 | 816 | Null -> protocol.BindParam(None, 0), |
| 655 | | − Integer(n) -> |
| 656 | | − match oid { |
| 657 | | − 21 -> binary_param(binary.be16(n)), // int2 |
| 658 | | − 23 -> binary_param(binary.be32(n)), // int4 |
| 659 | | − 20 -> binary_param(binary.be64(n)), // int8 |
| 660 | | − _ -> text_param("{n}"), |
| 661 | | − }, |
| 662 | | − Real(f) -> |
| 663 | | − match oid { |
| 664 | | − 700 -> binary_param(binary.f32(f)), // float4 |
| 665 | | − 701 -> binary_param(binary.f64(f)), // float8 |
| 666 | | − _ -> text_param("{f}"), |
| 667 | | − }, |
| 668 | | − Boolean(b) -> |
| 669 | | − if oid == 16 { |
| 670 | | − binary_param(bytes.from_list([if b { 1 } else { 0 }])) |
| 671 | | − } else { |
| 672 | | − text_param(show(value)) |
| 673 | | − }, |
| 817 | + Integer(n) -> match oid { |
| 818 | + 21 -> binary_param(binary.be16(n)), // int2 |
| 819 | + 23 -> binary_param(binary.be32(n)), // int4 |
| 820 | + 20 -> binary_param(binary.be64(n)), // int8 |
| 821 | + _ -> text_param("{n}"), |
| 822 | + }, |
| 823 | + Real(f) -> match oid { |
| 824 | + 700 -> binary_param(binary.f32(f)), // float4 |
| 825 | + 701 -> binary_param(binary.f64(f)), // float8 |
| 826 | + _ -> text_param("{f}"), |
| 827 | + }, |
| 828 | + Boolean(b) -> if oid == 16 { |
| 829 | + binary_param(bytes.from_list([if b { 1 } else { 0 }])) |
| 830 | + } else { |
| 831 | + text_param(show(value)) |
| 832 | + }, |
| 674 | 833 | Text(s) -> text_param(s), |
| 675 | | − Bytea(b) -> |
| 676 | | − if oid == 17 { |
| 677 | | − binary_param(b) // raw, no `\x` form |
| 678 | | − } else { |
| 679 | | − text_param(show(value)) |
| 680 | | − }, |
| 681 | | − Uuid(id) -> |
| 682 | | − if oid == 2950 { |
| 683 | | − binary_param(uuid.to_bytes(id)) |
| 684 | | − } else { |
| 685 | | − text_param(show(value)) |
| 686 | | − }, |
| 687 | | − Timestamptz(t) -> |
| 688 | | − if oid == 1184 { |
| 689 | | − binary_param(binary.be64(pg_micros(t))) |
| 690 | | − } else { |
| 691 | | − text_param(show(value)) |
| 692 | | − }, |
| 834 | + Bytea(b) -> if oid == 17 { |
| 835 | + binary_param(b) // raw, no `\x` form |
| 836 | + } else { |
| 837 | + text_param(show(value)) |
| 838 | + }, |
| 839 | + Uuid(id) -> if oid == 2950 { |
| 840 | + binary_param(uuid.to_bytes(id)) |
| 841 | + } else { |
| 842 | + text_param(show(value)) |
| 843 | + }, |
| 844 | + Timestamptz(t) -> if oid == 1184 { |
| 845 | + binary_param(binary.be64(pg_micros(t))) |
| 846 | + } else { |
| 847 | + text_param(show(value)) |
| 848 | + }, |
| 693 | 849 | } |
| 694 | 850 | } |
| 695 | 851 | |