| @@ -28,7 +28,7 @@ import scram |
| 28 | 28 | import uuid |
| 29 | 29 | |
| 30 | 30 | /// What a call failed with. |
| 31 | | −@derive(Debug) |
| 31 | +@derive Debug |
| 32 | 32 | pub type Error |
| 33 | 33 | { /// The socket or the TLS layer. |
| 34 | 34 | Io(String) |
| @@ -43,7 +43,7 @@ pub type Error |
| 43 | 43 | /// A parameter's value; SQL `NULL` is `Null`. A value whose type a binary |
| 44 | 44 | /// codec exists for is declared in the `Parse` and binds in binary |
| 45 | 45 | /// (`Bytea` is the raw bytes, no `\x` form); the rest go in text. |
| 46 | | −@derive(Eq, Debug) |
| 46 | +@derive Eq Debug |
| 47 | 47 | pub type Value |
| 48 | 48 | { Null |
| 49 | 49 | , Integer(Int) |
| @@ -63,30 +63,30 @@ pub type QueryResult |
| 63 | 63 | pub type Row { Row(columns: List<protocol.Field>, values: List<Option<Bytes>>) } |
| 64 | 64 | |
| 65 | 65 | /// The SQLSTATE of a server error, if it has one. |
| 66 | | −pub fn sqlstate(e: Error) -> Option<String> { |
| 66 | +pub fn sqlstate :: e: Error -> Option<String> { |
| 67 | 67 | match e { |
| 68 | | − Server(fields) -> field(fields, "C") |
| 68 | + Server(fields) -> field fields "C" |
| 69 | 69 | , _ -> None |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /// The server's message for a server error, or the text otherwise. |
| 74 | | −pub fn message(e: Error) -> String { |
| 74 | +pub fn message :: e: Error -> String { |
| 75 | 75 | match e { |
| 76 | 76 | Io(m) -> m |
| 77 | | − , Server(fields) -> option.unwrap_or(field(fields, "M"), debug_fields(fields)) |
| 77 | + , Server(fields) -> option.unwrap_or (field fields "M") (debug_fields fields) |
| 78 | 78 | , Driver(m) -> m |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | | −fn field(fields: List<(String, String)>, name: String) -> Option<String> { |
| 82 | +fn field :: fields: List<(String, String)> name: String -> Option<String> { |
| 83 | 83 | match fields { |
| 84 | 84 | [] -> None |
| 85 | | − , [(n, v), ..rest] -> if n == name { Some(v) } else { field(rest, name) } |
| 85 | + , [(n, v), ..rest] -> if n == name { Some(v) } else { field rest name } |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | | −fn debug_fields(fields: List<(String, String)>) -> String { |
| 89 | +fn debug_fields :: fields: List<(String, String)> -> String { |
| 90 | 90 | "{fields:?}" |
| 91 | 91 | } |
| 92 | 92 | |
| @@ -112,54 +112,56 @@ pub type Socket |
| 112 | 112 | |
| 113 | 113 | /// Connect and authenticate, without the actor: the socket, for the |
| 114 | 114 | /// connection actor (`connect`) or a listener (`listen`). |
| 115 | | −pub fn connect_socket(cfg: Config) -> Result<Socket, Error> / <io, random> { |
| 116 | | − use tcp <- result.try(net.connect(cfg.host, cfg.port) |> result.map_error(Io)) |
| 117 | | − use conn <- result.try(maybe_tls(tcp, cfg)) |
| 118 | | − use (buffer, backend) <- result.try(handshake(conn, cfg)) |
| 115 | +pub fn connect_socket :: cfg: Config -> Result<Socket, Error> / <io, random> { |
| 116 | + use tcp <- result.try (net.connect cfg.host cfg.port |> result.map_error Io) |
| 117 | + use conn <- result.try (maybe_tls tcp cfg) |
| 118 | + use (buffer, backend) <- result.try (handshake conn cfg) |
| 119 | 119 | Ok(Socket(conn, buffer, cfg.host, cfg.port, backend)) |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /// Connect and authenticate. The connection is an actor; stop it with |
| 123 | 123 | /// `close`. |
| 124 | | −pub fn connect |
| 125 | | − (cfg: Config) |
| 124 | +pub fn connect :: |
| 125 | + cfg: Config |
| 126 | 126 | -> Result<Subject<Msg>, Error> / <io, proc, random> { |
| 127 | | − use socket <- result.try(connect_socket(cfg)) |
| 128 | | − Ok(connect_over(socket)) |
| 127 | + use socket <- result.try (connect_socket cfg) |
| 128 | + Ok(connect_over socket) |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /// The connection actor over an already-authenticated socket. |
| 132 | | −pub fn connect_over(socket: Socket) -> Subject<Msg> / <proc> { |
| 133 | | − actor.start(state_of(socket), fn(state, msg) { on_message(state, msg) }) |
| 132 | +pub fn connect_over :: socket: Socket -> Subject<Msg> / <proc> { |
| 133 | + actor.start (state_of socket) fn state msg { on_message state msg } |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | | −fn state_of(socket: Socket) -> State { |
| 136 | +fn state_of :: socket: Socket -> State { |
| 137 | 137 | let Socket(conn, buffer, host, port, backend) = socket |
| 138 | | − State(conn, buffer, 0, host, port, backend, dict.empty(), 1) |
| 138 | + State(conn, buffer, 0, host, port, backend, dict.empty (), 1) |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | /// TLS if the mode wants it: `SslRequest`, then the handshake if the |
| 142 | 142 | /// server agrees. `Require` refuses a plain answer. |
| 143 | | −fn maybe_tls |
| 144 | | − (tcp: Resource<TcpStream>, cfg: Config) |
| 143 | +fn maybe_tls :: |
| 144 | + tcp: Resource<TcpStream> |
| 145 | + cfg: Config |
| 145 | 146 | -> Result<Conn, Error> / <io> { |
| 146 | 147 | match cfg.tls { |
| 147 | 148 | Disable -> Ok(Tcp(tcp)) |
| 148 | 149 | , mode -> { |
| 149 | 150 | use _ <- |
| 150 | | − result.try( net.write(tcp, protocol.encode(protocol.SslRequest)) |
| 151 | | − |> result.map_error(Io) |
| 152 | | − ) |
| 153 | | − use answer <- result.try(net.read(tcp, 1) |> result.map_error(Io)) |
| 154 | | − match bytes.to_list(answer) { |
| 155 | | − [byte] -> match protocol.ssl_answer(byte) { |
| 151 | + result.try |
| 152 | + ( net.write tcp (protocol.encode protocol.SslRequest) |
| 153 | + |> result.map_error Io |
| 154 | + ) |
| 155 | + use answer <- result.try (net.read tcp 1 |> result.map_error Io) |
| 156 | + match bytes.to_list answer { |
| 157 | + [byte] -> match protocol.ssl_answer byte { |
| 156 | 158 | Some(protocol.Upgrade) -> { |
| 157 | 159 | use tls_config <- |
| 158 | | − result.try(tls.config(tls.defaults()) |> result.map_error(tls_io)) |
| 160 | + result.try |
| 161 | + (tls.config (tls.defaults ()) |> result.map_error tls_io) |
| 159 | 162 | use stream <- |
| 160 | | − result.try( tls.connect(tcp, cfg.host, tls_config) |
| 161 | | − |> result.map_error(tls_io) |
| 162 | | − ) |
| 163 | + result.try |
| 164 | + (tls.connect tcp cfg.host tls_config |> result.map_error tls_io) |
| 163 | 165 | Ok(Tls(stream)) |
| 164 | 166 | } |
| 165 | 167 | , Some(protocol.Plain) -> match mode { |
| @@ -175,45 +177,48 @@ fn maybe_tls |
| 175 | 177 | } |
| 176 | 178 | } |
| 177 | 179 | |
| 178 | | −fn tls_io(e: tls.TlsError) -> Error { |
| 180 | +fn tls_io :: e: tls.TlsError -> Error { |
| 179 | 181 | Io("TLS: {e}") |
| 180 | 182 | } |
| 181 | 183 | |
| 182 | 184 | /// The startup message, then authenticate until `ReadyForQuery`. The |
| 183 | 185 | /// buffer's leftover bytes come back (the server sent nothing extra in |
| 184 | 186 | /// practice), with the backend key data if it arrived. |
| 185 | | −fn handshake |
| 186 | | − (conn: Conn, cfg: Config) |
| 187 | +fn handshake :: |
| 188 | + conn: Conn |
| 189 | + cfg: Config |
| 187 | 190 | -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 188 | 191 | use _ <- |
| 189 | | − result.try( write_msg( conn |
| 190 | | − , protocol.Startup(cfg.user, cfg.database, cfg.application) |
| 191 | | − ) |
| 192 | | − ) |
| 192 | + result.try |
| 193 | + (write_msg conn protocol.Startup(cfg.user, cfg.database, cfg.application)) |
| 193 | 194 | use (buffer, at, backend) <- |
| 194 | | − result.try(auth_loop(conn, bytes.empty(), 0, cfg, None)) |
| 195 | | − Ok((bytes.slice(buffer, at, bytes.length(buffer)), backend)) |
| 195 | + result.try (auth_loop conn (bytes.empty ()) 0 cfg None) |
| 196 | + Ok((bytes.slice buffer at (bytes.length buffer), backend)) |
| 196 | 197 | } |
| 197 | 198 | |
| 198 | | −fn auth_loop |
| 199 | | − (conn: Conn, buffer: Bytes, at: Int, cfg: Config, backend: Option<(Int, Int)>) |
| 199 | +fn auth_loop :: |
| 200 | + conn: Conn |
| 201 | + buffer: Bytes |
| 202 | + at: Int |
| 203 | + cfg: Config |
| 204 | + backend: Option<(Int, Int)> |
| 200 | 205 | -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 201 | | − use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 206 | + use (msg, buffer, at) <- result.try (read_message conn buffer at) |
| 202 | 207 | match msg { |
| 203 | | − protocol.AuthenticationOk -> auth_loop(conn, buffer, at, cfg, backend) |
| 208 | + protocol.AuthenticationOk -> auth_loop conn buffer at cfg backend |
| 204 | 209 | , protocol.AuthenticationCleartext -> { |
| 205 | | − use _ <- result.try(cleartext_password(conn, cfg)) |
| 206 | | − auth_loop(conn, buffer, at, cfg, backend) |
| 210 | + use _ <- result.try (cleartext_password conn cfg) |
| 211 | + auth_loop conn buffer at cfg backend |
| 207 | 212 | } |
| 208 | 213 | , protocol.AuthenticationSasl(mechanisms) -> { |
| 209 | 214 | use (buffer, at) <- |
| 210 | | − result.try(scram_exchange(conn, buffer, at, cfg, mechanisms)) |
| 211 | | − auth_loop(conn, buffer, at, cfg, backend) |
| 215 | + result.try (scram_exchange conn buffer at cfg mechanisms) |
| 216 | + auth_loop conn buffer at cfg backend |
| 212 | 217 | } |
| 213 | | − , protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, cfg, backend) |
| 218 | + , protocol.ParameterStatus(..) -> auth_loop conn buffer at cfg backend |
| 214 | 219 | , protocol.BackendKeyData(pid, secret) -> |
| 215 | | − auth_loop(conn, buffer, at, cfg, Some((pid, secret))) |
| 216 | | − , protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, cfg, backend) |
| 220 | + auth_loop conn buffer at cfg Some((pid, secret)) |
| 221 | + , protocol.NoticeResponse(_) -> auth_loop conn buffer at cfg backend |
| 217 | 222 | , protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)) |
| 218 | 223 | , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 219 | 224 | , _ -> Error(Driver("an unexpected message during startup")) |
| @@ -221,7 +226,7 @@ fn auth_loop |
| 221 | 226 | } |
| 222 | 227 | |
| 223 | 228 | /// A cleartext password, over TLS or to localhost only. |
| 224 | | −fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 229 | +fn cleartext_password :: conn: Conn cfg: Config -> Result<(), Error> / <io> { |
| 225 | 230 | let safe = match conn { |
| 226 | 231 | Tls(_) -> True |
| 227 | 232 | , Tcp(_) -> |
| @@ -233,57 +238,60 @@ fn cleartext_password(conn: Conn, cfg: Config) -> Result<(), Error> / <io> { |
| 233 | 238 | ) |
| 234 | 239 | } else { |
| 235 | 240 | use password <- |
| 236 | | − result.try( option.to_result( cfg.password |
| 237 | | − , "the server wants a password; the URL has none" |
| 241 | + result.try |
| 242 | + ( option.to_result |
| 243 | + cfg.password |
| 244 | + "the server wants a password; the URL has none" |
| 245 | + |> result.map_error Driver |
| 238 | 246 | ) |
| 239 | | − |> result.map_error(Driver) |
| 240 | | − ) |
| 241 | | − write_msg(conn, protocol.Password(password)) |
| 247 | + write_msg conn protocol.Password(password) |
| 242 | 248 | } |
| 243 | 249 | } |
| 244 | 250 | |
| 245 | 251 | /// SCRAM-SHA-256, if the server offers it. |
| 246 | | −fn scram_exchange |
| 247 | | − (conn: Conn, buffer: Bytes, at: Int, cfg: Config, mechanisms: List<String>) |
| 252 | +fn scram_exchange :: |
| 253 | + conn: Conn |
| 254 | + buffer: Bytes |
| 255 | + at: Int |
| 256 | + cfg: Config |
| 257 | + mechanisms: List<String> |
| 248 | 258 | -> Result<(Bytes, Int), Error> / <io, random> { |
| 249 | | − if !list.any(mechanisms, fn(m) { m == "SCRAM-SHA-256" }) { |
| 259 | + if !list.any mechanisms fn m { m == "SCRAM-SHA-256" } { |
| 250 | 260 | Error(Driver("the server offers no SCRAM-SHA-256")) |
| 251 | 261 | } else { |
| 252 | 262 | use password <- |
| 253 | | − result.try( option.to_result( cfg.password |
| 254 | | − , "the server wants a password; the URL has none" |
| 263 | + result.try |
| 264 | + ( option.to_result |
| 265 | + cfg.password |
| 266 | + "the server wants a password; the URL has none" |
| 267 | + |> result.map_error Driver |
| 255 | 268 | ) |
| 256 | | − |> result.map_error(Driver) |
| 257 | | − ) |
| 258 | | − let nonce = base64.encode(random.bytes(18)) |
| 259 | | − let (state, first) = scram.client_first(nonce) |
| 269 | + let nonce = base64.encode (random.bytes 18) |
| 270 | + let (state, first) = scram.client_first nonce |
| 260 | 271 | use _ <- |
| 261 | | − result.try( write_msg( conn |
| 262 | | − , protocol.SaslInitialResponse("SCRAM-SHA-256", first) |
| 263 | | − ) |
| 264 | | − ) |
| 265 | | − use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 266 | | − use (state, reply) <- result.try(scram_continue(msg, state, password)) |
| 272 | + result.try |
| 273 | + (write_msg conn protocol.SaslInitialResponse("SCRAM-SHA-256", first)) |
| 274 | + use (msg, buffer, at) <- result.try (read_message conn buffer at) |
| 275 | + use (state, reply) <- result.try (scram_continue msg state password) |
| 267 | 276 | use _ <- |
| 268 | | − result.try( write_msg( conn |
| 269 | | − , protocol.SaslResponse(bytes.from_string(reply)) |
| 270 | | − ) |
| 271 | | − ) |
| 272 | | − use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 273 | | − use _ <- result.try(scram_final(msg, state)) |
| 277 | + result.try (write_msg conn protocol.SaslResponse(bytes.from_string reply)) |
| 278 | + use (msg, buffer, at) <- result.try (read_message conn buffer at) |
| 279 | + use _ <- result.try (scram_final msg state) |
| 274 | 280 | Ok((buffer, at)) |
| 275 | 281 | } |
| 276 | 282 | } |
| 277 | 283 | |
| 278 | 284 | /// The server-first step: the client-final message and the state the |
| 279 | 285 | /// final step verifies with. |
| 280 | | −fn scram_continue |
| 281 | | − (msg: protocol.In, state: scram.Scram, password: String) |
| 286 | +fn scram_continue :: |
| 287 | + msg: protocol.In |
| 288 | + state: scram.Scram |
| 289 | + password: String |
| 282 | 290 | -> Result<(scram.Scram, String), Error> { |
| 283 | 291 | match msg { |
| 284 | | − protocol.SaslContinue(data) -> match bytes.to_string(data) { |
| 292 | + protocol.SaslContinue(data) -> match bytes.to_string data { |
| 285 | 293 | Ok(text) -> |
| 286 | | − scram.client_final(state, password, text) |> result.map_error(Driver) |
| 294 | + scram.client_final state password text |> result.map_error Driver |
| 287 | 295 | , Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")) |
| 288 | 296 | } |
| 289 | 297 | , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| @@ -293,10 +301,10 @@ fn scram_continue |
| 293 | 301 | |
| 294 | 302 | /// The server-final step: verify the server's signature (it proves the |
| 295 | 303 | /// server knew the password too). |
| 296 | | −fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 304 | +fn scram_final :: msg: protocol.In state: scram.Scram -> Result<(), Error> { |
| 297 | 305 | match msg { |
| 298 | | − protocol.SaslFinal(data) -> match bytes.to_string(data) { |
| 299 | | − Ok(text) -> scram.verify_server(state, text) |> result.map_error(Driver) |
| 306 | + protocol.SaslFinal(data) -> match bytes.to_string data { |
| 307 | + Ok(text) -> scram.verify_server state text |> result.map_error Driver |
| 300 | 308 | , Error(e) -> Error(Driver("SASL data is not UTF-8: {e}")) |
| 301 | 309 | } |
| 302 | 310 | , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| @@ -344,27 +352,27 @@ pub type Msg |
| 344 | 352 | , Close(reply: Subject<()>) |
| 345 | 353 | } |
| 346 | 354 | |
| 347 | | −fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 355 | +fn on_message :: state: State msg: Msg -> actor.Next<State> / <io, proc> { |
| 348 | 356 | match msg { |
| 349 | 357 | DoQuery(sql, params, reply) -> { |
| 350 | | − let (res, state) = run_query(state, sql, params) |
| 351 | | − process.send(reply, res) |
| 358 | + let (res, state) = run_query state sql params |
| 359 | + process.send reply res |
| 352 | 360 | Continue(state) |
| 353 | 361 | } |
| 354 | 362 | , Ping(reply) -> { |
| 355 | | − let (res, buffer, at) = ping(state.conn, state.buffer, state.at) |
| 356 | | − process.send(reply, res) |
| 363 | + let (res, buffer, at) = ping state.conn state.buffer state.at |
| 364 | + process.send reply res |
| 357 | 365 | match res { |
| 358 | 366 | Ok(()) -> Continue(State(..state, buffer: buffer, at: at)) |
| 359 | 367 | , Error(_) -> { |
| 360 | | − close_conn(state.conn) |
| 368 | + close_conn state.conn |
| 361 | 369 | Stop |
| 362 | 370 | } |
| 363 | 371 | } |
| 364 | 372 | } |
| 365 | 373 | , Close(reply) -> { |
| 366 | | − close_conn(state.conn) |
| 367 | | − process.send(reply, ()) |
| 374 | + close_conn state.conn |
| 375 | + process.send reply () |
| 368 | 376 | Stop |
| 369 | 377 | } |
| 370 | 378 | } |
| @@ -372,20 +380,25 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 372 | 380 | |
| 373 | 381 | /// A bare `Sync`, then read to `ReadyForQuery`, which must report the |
| 374 | 382 | /// connection idle: a pooled connection has no transaction open. |
| 375 | | −fn ping |
| 376 | | − (conn: Conn, buffer: Bytes, at: Int) |
| 383 | +fn ping :: |
| 384 | + conn: Conn |
| 385 | + buffer: Bytes |
| 386 | + at: Int |
| 377 | 387 | -> (Result<(), Error>, Bytes, Int) / <io> { |
| 378 | | − match write_msg(conn, protocol.Sync) { |
| 379 | | − Error(e) -> (Error(e), bytes.empty(), 0) |
| 380 | | − , Ok(()) -> ping_read(conn, buffer, at, None) |
| 388 | + match write_msg conn protocol.Sync { |
| 389 | + Error(e) -> (Error(e), bytes.empty (), 0) |
| 390 | + , Ok(()) -> ping_read conn buffer at None |
| 381 | 391 | } |
| 382 | 392 | } |
| 383 | 393 | |
| 384 | | −fn ping_read |
| 385 | | − (conn: Conn, buffer: Bytes, at: Int, error: Option<Error>) |
| 394 | +fn ping_read :: |
| 395 | + conn: Conn |
| 396 | + buffer: Bytes |
| 397 | + at: Int |
| 398 | + error: Option<Error> |
| 386 | 399 | -> (Result<(), Error>, Bytes, Int) / <io> { |
| 387 | | − match read_message(conn, buffer, at) { |
| 388 | | − Error(e) -> (Error(e), bytes.empty(), 0) |
| 400 | + match read_message conn buffer at { |
| 401 | + Error(e) -> (Error(e), bytes.empty (), 0) |
| 389 | 402 | , Ok((msg, buffer, at)) -> match msg { |
| 390 | 403 | protocol.ReadyForQuery(status) -> match error { |
| 391 | 404 | Some(e) -> (Error(e), buffer, at) |
| @@ -398,8 +411,8 @@ fn ping_read |
| 398 | 411 | } |
| 399 | 412 | } |
| 400 | 413 | , protocol.ErrorResponse(fields) -> |
| 401 | | − ping_read(conn, buffer, at, Some(Server(fields))) |
| 402 | | − , _ -> ping_read(conn, buffer, at, error) |
| 414 | + ping_read conn buffer at Some(Server(fields)) |
| 415 | + , _ -> ping_read conn buffer at error |
| 403 | 416 | } |
| 404 | 417 | } |
| 405 | 418 | } |
| @@ -407,38 +420,41 @@ fn ping_read |
| 407 | 420 | /// A query: the statement is prepared once per connection (Parse + |
| 408 | 421 | /// Describe, one exchange), then Bind + Execute. Parameters travel as |
| 409 | 422 | /// `$1`, binary per type where a codec exists (binary.lse). |
| 410 | | −fn run_query |
| 411 | | − (state: State, sql: String, params: List<Value>) |
| 423 | +fn run_query :: |
| 424 | + state: State |
| 425 | + sql: String |
| 426 | + params: List<Value> |
| 412 | 427 | -> (Result<QueryResult, Error>, State) / <io> { |
| 413 | | − match prepare(state, sql, params) { |
| 428 | + match prepare state sql params { |
| 414 | 429 | (Error(e), state) -> (Error(e), state) |
| 415 | | − , (Ok(prep), state) -> if list.length(params) |
| 416 | | − != list.length(prep.param_oids) { |
| 417 | | − ( Error( Driver( "the statement takes {list.length( prep.param_oids |
| 418 | | − )} parameters, got {list.length(params)}" |
| 430 | + , ( Ok(prep) |
| 431 | + , state |
| 432 | + ) -> if list.length params != list.length prep.param_oids { |
| 433 | + ( Error( Driver( "the statement takes {list.length |
| 434 | + prep.param_oids} parameters, got {list.length params}" |
| 419 | 435 | ) |
| 420 | 436 | ) |
| 421 | 437 | , state |
| 422 | 438 | ) |
| 423 | 439 | } else { |
| 424 | | − let encoded = encode_params(params, prep.param_oids, []) |
| 440 | + let encoded = encode_params params prep.param_oids [] |
| 425 | 441 | let formats = |
| 426 | | − list.map( prep.fields |
| 427 | | − , fn(f) { if binary_known(f.type_oid) { 1 } else { 0 } } |
| 428 | | − ) |
| 442 | + list.map |
| 443 | + prep.fields |
| 444 | + fn f { if binary_known f.type_oid { 1 } else { 0 } } |
| 429 | 445 | let out = |
| 430 | 446 | [ protocol.Bind("", prep.name, encoded, formats) |
| 431 | 447 | , protocol.DescribePortal("") |
| 432 | 448 | , protocol.Execute("", 0) |
| 433 | 449 | , protocol.Sync |
| 434 | 450 | ] |
| 435 | | − match conn_write( state.conn |
| 436 | | − , bytes.concat_all(list.map(out, protocol.encode)) |
| 437 | | − ) { |
| 451 | + match conn_write |
| 452 | + state.conn |
| 453 | + (bytes.concat_all (list.map out protocol.encode)) { |
| 438 | 454 | Error(e) -> (Error(e), state) |
| 439 | 455 | , Ok(()) -> { |
| 440 | 456 | let (res, buffer, at) = |
| 441 | | − collect(state.conn, state.buffer, state.at, [], [], "", None) |
| 457 | + collect state.conn state.buffer state.at [] [] "" None |
| 442 | 458 | (res, State(..state, buffer: buffer, at: at)) |
| 443 | 459 | } |
| 444 | 460 | } |
| @@ -450,29 +466,27 @@ fn run_query |
| 450 | 466 | /// + Describe (one exchange). The parameters' types are declared where the |
| 451 | 467 | /// value names one unambiguously (`declared_oid`), so a `Bytea` binds in |
| 452 | 468 | /// binary whatever the SQL context infers. |
| 453 | | −fn prepare |
| 454 | | − (state: State, sql: String, params: List<Value>) |
| 469 | +fn prepare :: |
| 470 | + state: State |
| 471 | + sql: String |
| 472 | + params: List<Value> |
| 455 | 473 | -> (Result<Prepared, Error>, State) / <io> { |
| 456 | | − match dict.get(state.prepared, sql) { |
| 474 | + match dict.get state.prepared sql { |
| 457 | 475 | Some(prep) -> (Ok(prep), state) |
| 458 | 476 | , None -> { |
| 459 | 477 | let name = "q{state.next_statement}" |
| 460 | 478 | let out = |
| 461 | | − [ protocol.Parse(name, sql, list.map(params, declared_oid)) |
| 479 | + [ protocol.Parse(name, sql, list.map params declared_oid) |
| 462 | 480 | , protocol.DescribeStatement(name) |
| 463 | 481 | , protocol.Sync |
| 464 | 482 | ] |
| 465 | | − match conn_write( state.conn |
| 466 | | − , bytes.concat_all(list.map(out, protocol.encode)) |
| 467 | | − ) { |
| 483 | + match conn_write |
| 484 | + state.conn |
| 485 | + (bytes.concat_all (list.map out protocol.encode)) { |
| 468 | 486 | Error(e) -> (Error(e), state) |
| 469 | | − , Ok(()) -> match read_describe( state.conn |
| 470 | | − , state.buffer |
| 471 | | − , state.at |
| 472 | | − , [] |
| 473 | | − , [] |
| 474 | | − , None |
| 475 | | − ) { |
| 487 | + , Ok |
| 488 | + ( () |
| 489 | + ) -> match read_describe state.conn state.buffer state.at [] [] None { |
| 476 | 490 | (Error(e), buffer, at) -> ( Error(e) |
| 477 | 491 | , State(..state, buffer: buffer, at: at) |
| 478 | 492 | ) |
| @@ -482,7 +496,7 @@ fn prepare |
| 482 | 496 | , State( ..state |
| 483 | 497 | , buffer: buffer |
| 484 | 498 | , at: at |
| 485 | | − , prepared: dict.insert(state.prepared, sql, prep) |
| 499 | + , prepared: dict.insert state.prepared sql prep |
| 486 | 500 | , next_statement: state.next_statement + 1 |
| 487 | 501 | ) |
| 488 | 502 | ) |
| @@ -495,75 +509,65 @@ fn prepare |
| 495 | 509 | |
| 496 | 510 | /// Read the answers to Parse + Describe until `ReadyForQuery`: the |
| 497 | 511 | /// parameters' type OIDs and the result columns. |
| 498 | | −fn read_describe |
| 499 | | − ( conn: Conn |
| 500 | | − , buffer: Bytes |
| 501 | | − , at: Int |
| 502 | | − , param_oids: List<Int> |
| 503 | | − , fields: List<protocol.Field> |
| 504 | | − , error: Option<Error> |
| 505 | | − ) |
| 512 | +fn read_describe :: |
| 513 | + conn: Conn |
| 514 | + buffer: Bytes |
| 515 | + at: Int |
| 516 | + param_oids: List<Int> |
| 517 | + fields: List<protocol.Field> |
| 518 | + error: Option<Error> |
| 506 | 519 | -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes, Int) / <io> { |
| 507 | | − match read_message(conn, buffer, at) { |
| 508 | | − Error(e) -> (Error(e), bytes.empty(), 0) |
| 520 | + match read_message conn buffer at { |
| 521 | + Error(e) -> (Error(e), bytes.empty (), 0) |
| 509 | 522 | , Ok((msg, buffer, at)) -> match msg { |
| 510 | 523 | protocol.ParameterDescription(oids) -> |
| 511 | | − read_describe(conn, buffer, at, oids, fields, error) |
| 524 | + read_describe conn buffer at oids fields error |
| 512 | 525 | , protocol.RowDescription(f) -> |
| 513 | | − read_describe(conn, buffer, at, param_oids, f, error) |
| 514 | | − , protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error) |
| 526 | + read_describe conn buffer at param_oids f error |
| 527 | + , protocol.NoData -> read_describe conn buffer at param_oids [] error |
| 515 | 528 | , protocol.ErrorResponse(f) -> |
| 516 | | − read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))) |
| 529 | + read_describe conn buffer at param_oids fields Some(Server(f)) |
| 517 | 530 | , protocol.ReadyForQuery(_) -> match error { |
| 518 | 531 | Some(e) -> (Error(e), buffer, at) |
| 519 | 532 | , None -> (Ok((param_oids, fields)), buffer, at) |
| 520 | 533 | } |
| 521 | | − , _ -> read_describe(conn, buffer, at, param_oids, fields, error) |
| 534 | + , _ -> read_describe conn buffer at param_oids fields error |
| 522 | 535 | } |
| 523 | 536 | } |
| 524 | 537 | } |
| 525 | 538 | |
| 526 | | −fn collect |
| 527 | | − ( conn: Conn |
| 528 | | − , buffer: Bytes |
| 529 | | − , at: Int |
| 530 | | − , columns: List<protocol.Field> |
| 531 | | − , rows: List<Row> |
| 532 | | − , tag: String |
| 533 | | − , error: Option<Error> |
| 534 | | − ) |
| 539 | +fn collect :: |
| 540 | + conn: Conn |
| 541 | + buffer: Bytes |
| 542 | + at: Int |
| 543 | + columns: List<protocol.Field> |
| 544 | + rows: List<Row> |
| 545 | + tag: String |
| 546 | + error: Option<Error> |
| 535 | 547 | -> (Result<QueryResult, Error>, Bytes, Int) / <io> { |
| 536 | | − match read_message(conn, buffer, at) { |
| 537 | | − Error(e) -> (Error(e), bytes.empty(), 0) |
| 548 | + match read_message conn buffer at { |
| 549 | + Error(e) -> (Error(e), bytes.empty (), 0) |
| 538 | 550 | , Ok((msg, buffer, at)) -> match msg { |
| 539 | 551 | protocol.RowDescription(fields) -> |
| 540 | | − collect(conn, buffer, at, fields, rows, tag, error) |
| 552 | + collect conn buffer at fields rows tag error |
| 541 | 553 | , protocol.DataRow(values) -> |
| 542 | | − collect( conn |
| 543 | | − , buffer |
| 544 | | − , at |
| 545 | | − , columns |
| 546 | | − , [Row(columns, values), ..rows] |
| 547 | | − , tag |
| 548 | | − , error |
| 549 | | − ) |
| 550 | | − , protocol.CommandComplete(t) -> |
| 551 | | − collect(conn, buffer, at, columns, rows, t, error) |
| 554 | + collect conn buffer at columns [Row(columns, values), ..rows] tag error |
| 555 | + , protocol.CommandComplete(t) -> collect conn buffer at columns rows t error |
| 552 | 556 | , protocol.ErrorResponse(fields) -> |
| 553 | | − collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))) |
| 557 | + collect conn buffer at columns rows tag Some(Server(fields)) |
| 554 | 558 | , protocol.ReadyForQuery(_) -> match error { |
| 555 | 559 | Some(e) -> (Error(e), buffer, at) |
| 556 | | − , None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at) |
| 560 | + , None -> (Ok(QueryResult(columns, list.reverse rows, tag)), buffer, at) |
| 557 | 561 | } |
| 558 | 562 | , _ -> |
| 559 | | − collect( conn |
| 560 | | − , buffer |
| 561 | | − , at |
| 562 | | − , columns |
| 563 | | − , rows |
| 564 | | − , tag |
| 565 | | − , error |
| 566 | | − ) // ParseComplete, BindComplete, NoData, ... |
| 563 | + collect |
| 564 | + conn |
| 565 | + buffer |
| 566 | + at |
| 567 | + columns |
| 568 | + rows |
| 569 | + tag |
| 570 | + error // ParseComplete, BindComplete, NoData, ... |
| 567 | 571 | } |
| 568 | 572 | } |
| 569 | 573 | } |
| @@ -574,55 +578,61 @@ fn collect |
| 574 | 578 | /// the buffer and the offset parsing resumes at. A message already whole |
| 575 | 579 | /// in the buffer is parsed in place (nothing is copied); one that |
| 576 | 580 | /// straddles reads pays once for moving its tail into a builder. |
| 577 | | −fn read_message |
| 578 | | − (conn: Conn, buffer: Bytes, at: Int) |
| 581 | +fn read_message :: |
| 582 | + conn: Conn |
| 583 | + buffer: Bytes |
| 584 | + at: Int |
| 579 | 585 | -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 580 | | − match protocol.next(buffer, at) { |
| 581 | | − protocol.Message(msg, past) -> if past == bytes.length(buffer) { |
| 582 | | − Ok((msg, bytes.empty(), 0)) |
| 586 | + match protocol.next buffer at { |
| 587 | + protocol.Message(msg, past) -> if past == bytes.length buffer { |
| 588 | + Ok((msg, bytes.empty (), 0)) |
| 583 | 589 | } else { |
| 584 | 590 | Ok((msg, buffer, past)) |
| 585 | 591 | } |
| 586 | 592 | , protocol.Malformed(e) -> Error(Driver(e)) |
| 587 | 593 | , protocol.Incomplete -> |
| 588 | | − fill(conn, bytes.slice(buffer, at, bytes.length(buffer))) |
| 594 | + fill conn (bytes.slice buffer at (bytes.length buffer)) |
| 589 | 595 | } |
| 590 | 596 | } |
| 591 | 597 | |
| 592 | 598 | /// The rest of a message that straddles reads: chunks accumulate in a |
| 593 | 599 | /// builder (appending does not copy what was appended before) until the |
| 594 | 600 | /// frame is whole. |
| 595 | | −fn fill |
| 596 | | − (conn: Conn, tail: Bytes) |
| 601 | +fn fill :: |
| 602 | + conn: Conn |
| 603 | + tail: Bytes |
| 597 | 604 | -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 598 | | − use tail <- result.try(fill_header(conn, tail)) |
| 605 | + use tail <- result.try (fill_header conn tail) |
| 599 | 606 | use total <- |
| 600 | | − result.try(protocol.frame_total(tail, 0) |> result.map_error(Driver)) |
| 607 | + result.try (protocol.frame_total tail 0 |> result.map_error Driver) |
| 601 | 608 | let b = |
| 602 | | − bytes.append(bytes.builder_with_capacity(bytes.length(tail) + 65536), tail) |
| 603 | | − fill_loop(conn, b, bytes.length(tail), total) |
| 609 | + bytes.append (bytes.builder_with_capacity (bytes.length tail + 65536)) tail |
| 610 | + fill_loop conn b (bytes.length tail) total |
| 604 | 611 | } |
| 605 | 612 | |
| 606 | 613 | /// `tail` grown to a whole frame header (5 bytes). |
| 607 | | −fn fill_header(conn: Conn, tail: Bytes) -> Result<Bytes, Error> / <io> { |
| 608 | | − if bytes.length(tail) >= 5 { |
| 614 | +fn fill_header :: conn: Conn tail: Bytes -> Result<Bytes, Error> / <io> { |
| 615 | + if bytes.length tail >= 5 { |
| 609 | 616 | Ok(tail) |
| 610 | 617 | } else { |
| 611 | | − use chunk <- result.try(conn_read(conn, 65536)) |
| 612 | | − if bytes.length(chunk) == 0 { |
| 618 | + use chunk <- result.try (conn_read conn 65536) |
| 619 | + if bytes.length chunk == 0 { |
| 613 | 620 | Error(Io("the server closed the connection")) |
| 614 | 621 | } else { |
| 615 | | − fill_header(conn, bytes.concat(tail, chunk)) |
| 622 | + fill_header conn (bytes.concat tail chunk) |
| 616 | 623 | } |
| 617 | 624 | } |
| 618 | 625 | } |
| 619 | 626 | |
| 620 | | −fn fill_loop |
| 621 | | − (conn: Conn, b: bytes.Builder, have: Int, total: Int) |
| 627 | +fn fill_loop :: |
| 628 | + conn: Conn |
| 629 | + b: bytes.Builder |
| 630 | + have: Int |
| 631 | + total: Int |
| 622 | 632 | -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 623 | 633 | if have >= total { |
| 624 | | − let buffer = bytes.build(b) |
| 625 | | − match protocol.next(buffer, 0) { |
| 634 | + let buffer = bytes.build b |
| 635 | + match protocol.next buffer 0 { |
| 626 | 636 | protocol.Message(msg, past) -> Ok((msg, buffer, past)) |
| 627 | 637 | , protocol.Malformed(e) -> Error(Driver(e)) |
| 628 | 638 | , protocol.Incomplete -> |
| @@ -631,45 +641,45 @@ fn fill_loop |
| 631 | 641 | ) |
| 632 | 642 | } |
| 633 | 643 | } else { |
| 634 | | − use chunk <- result.try(conn_read(conn, 65536)) |
| 635 | | − if bytes.length(chunk) == 0 { |
| 644 | + use chunk <- result.try (conn_read conn 65536) |
| 645 | + if bytes.length chunk == 0 { |
| 636 | 646 | Error(Io("the server closed the connection")) |
| 637 | 647 | } else { |
| 638 | | − fill_loop(conn, bytes.append(b, chunk), have + bytes.length(chunk), total) |
| 648 | + fill_loop conn (bytes.append b chunk) (have + bytes.length chunk) total |
| 639 | 649 | } |
| 640 | 650 | } |
| 641 | 651 | } |
| 642 | 652 | |
| 643 | | −fn write_msg(conn: Conn, msg: protocol.Out) -> Result<(), Error> / <io> { |
| 644 | | − conn_write(conn, protocol.encode(msg)) |
| 653 | +fn write_msg :: conn: Conn msg: protocol.Out -> Result<(), Error> / <io> { |
| 654 | + conn_write conn (protocol.encode msg) |
| 645 | 655 | } |
| 646 | 656 | |
| 647 | | −fn conn_read(conn: Conn, max: Int) -> Result<Bytes, Error> / <io> { |
| 657 | +fn conn_read :: conn: Conn max: Int -> Result<Bytes, Error> / <io> { |
| 648 | 658 | match conn { |
| 649 | | − Tcp(c) -> net.read(c, max) |> result.map_error(Io) |
| 650 | | − , Tls(c) -> tls.read(c, max) |> result.map_error(tls_io) |
| 659 | + Tcp(c) -> net.read c max |> result.map_error Io |
| 660 | + , Tls(c) -> tls.read c max |> result.map_error tls_io |
| 651 | 661 | } |
| 652 | 662 | } |
| 653 | 663 | |
| 654 | | −fn conn_write(conn: Conn, data: Bytes) -> Result<(), Error> / <io> { |
| 664 | +fn conn_write :: conn: Conn data: Bytes -> Result<(), Error> / <io> { |
| 655 | 665 | match conn { |
| 656 | | − Tcp(c) -> net.write(c, data) |> result.map_error(Io) |
| 657 | | − , Tls(c) -> tls.write(c, data) |> result.map_error(tls_io) |
| 666 | + Tcp(c) -> net.write c data |> result.map_error Io |
| 667 | + , Tls(c) -> tls.write c data |> result.map_error tls_io |
| 658 | 668 | } |
| 659 | 669 | } |
| 660 | 670 | |
| 661 | | −fn close_conn(conn: Conn) -> () / <io> { |
| 662 | | − let _ = conn_write(conn, protocol.encode(protocol.Terminate)) |
| 671 | +fn close_conn :: conn: Conn -> () / <io> { |
| 672 | + let _ = conn_write conn (protocol.encode protocol.Terminate) |
| 663 | 673 | match conn { |
| 664 | | − Tcp(c) -> net.close(c) |
| 665 | | − , Tls(c) -> tls.close(c) |
| 674 | + Tcp(c) -> net.close c |
| 675 | + , Tls(c) -> tls.close c |
| 666 | 676 | } |
| 667 | 677 | } |
| 668 | 678 | |
| 669 | 679 | // ---- LISTEN/NOTIFY (a dedicated connection, outside any pool) ----------------- |
| 670 | 680 | |
| 671 | 681 | /// A notification from a `LISTEN`ed channel. |
| 672 | | −@derive(Debug) |
| 682 | +@derive Debug |
| 673 | 683 | pub type Notification |
| 674 | 684 | { Notification(channel: String, payload: String, process: Int) } |
| 675 | 685 | |
| @@ -678,72 +688,71 @@ pub type Notification |
| 678 | 688 | /// parked on the socket. Stop it with `process.kill` (a supervisor |
| 679 | 689 | /// restarts it: it re-`LISTEN`s). Quoting: the channel is an identifier, |
| 680 | 690 | /// sent as `LISTEN "channel"`. |
| 681 | | −pub fn listen |
| 682 | | − (cfg: Config, channel: String, subscriber: Subject<Notification>) |
| 691 | +pub fn listen :: |
| 692 | + cfg: Config |
| 693 | + channel: String |
| 694 | + subscriber: Subject<Notification> |
| 683 | 695 | -> Result<Pid, Error> / <io, proc, random> { |
| 684 | | − use socket <- result.try(connect_socket(cfg)) |
| 696 | + use socket <- result.try (connect_socket cfg) |
| 685 | 697 | let Socket(conn, buffer, ..) = socket |
| 686 | | − use (buffer, at) <- result.try(listen_command(conn, buffer, 0, channel)) |
| 687 | | − Ok( process.spawn( fn() { |
| 688 | | − listen_serve(conn, buffer, at, channel, subscriber) |
| 689 | | − } |
| 690 | | − ) |
| 691 | | − ) |
| 698 | + use (buffer, at) <- result.try (listen_command conn buffer 0 channel) |
| 699 | + Ok(process.spawn fn { listen_serve conn buffer at channel subscriber }) |
| 692 | 700 | } |
| 693 | 701 | |
| 694 | 702 | /// `LISTEN "channel"`, answered by `CommandComplete` and `ReadyForQuery`. |
| 695 | | −fn listen_command |
| 696 | | − (conn: Conn, buffer: Bytes, at: Int, channel: String) |
| 703 | +fn listen_command :: |
| 704 | + conn: Conn |
| 705 | + buffer: Bytes |
| 706 | + at: Int |
| 707 | + channel: String |
| 697 | 708 | -> Result<(Bytes, Int), Error> / <io> { |
| 698 | | − use _ <- |
| 699 | | − result.try(write_msg(conn, protocol.Query("LISTEN {quoted(channel)}"))) |
| 700 | | − drain_until_ready(conn, buffer, at) |
| 709 | + use _ <- result.try (write_msg conn protocol.Query("LISTEN {quoted channel}")) |
| 710 | + drain_until_ready conn buffer at |
| 701 | 711 | } |
| 702 | 712 | |
| 703 | 713 | /// An identifier in double quotes (inner quotes doubled). |
| 704 | | −fn quoted(name: String) -> String { |
| 714 | +fn quoted :: name: String -> String { |
| 705 | 715 | "\"" |
| 706 | | − <> string.concat( list.map( string.chars(name) |
| 707 | | − , fn(c) { if c == "\"" { "\"\"" } else { c } } |
| 708 | | − ) |
| 709 | | − ) |
| 716 | + <> string.concat |
| 717 | + (list.map (string.chars name) fn c { if c == "\"" { "\"\"" } else { c } }) |
| 710 | 718 | <> "\"" |
| 711 | 719 | } |
| 712 | 720 | |
| 713 | 721 | /// Read until `ReadyForQuery` (the simple query protocol's end). |
| 714 | | −fn drain_until_ready |
| 715 | | − (conn: Conn, buffer: Bytes, at: Int) |
| 722 | +fn drain_until_ready :: |
| 723 | + conn: Conn |
| 724 | + buffer: Bytes |
| 725 | + at: Int |
| 716 | 726 | -> Result<(Bytes, Int), Error> / <io> { |
| 717 | | − use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 727 | + use (msg, buffer, at) <- result.try (read_message conn buffer at) |
| 718 | 728 | match msg { |
| 719 | 729 | protocol.ReadyForQuery(_) -> Ok((buffer, at)) |
| 720 | 730 | , protocol.ErrorResponse(fields) -> Error(Server(fields)) |
| 721 | | − , _ -> drain_until_ready(conn, buffer, at) |
| 731 | + , _ -> drain_until_ready conn buffer at |
| 722 | 732 | } |
| 723 | 733 | } |
| 724 | 734 | |
| 725 | 735 | /// Read forever: notifications for our channel go to the subscriber. A |
| 726 | 736 | /// broken socket stops the listener. |
| 727 | | −fn listen_serve |
| 728 | | − ( conn: Conn |
| 729 | | − , buffer: Bytes |
| 730 | | − , at: Int |
| 731 | | − , channel: String |
| 732 | | − , subscriber: Subject<Notification> |
| 733 | | − ) |
| 737 | +fn listen_serve :: |
| 738 | + conn: Conn |
| 739 | + buffer: Bytes |
| 740 | + at: Int |
| 741 | + channel: String |
| 742 | + subscriber: Subject<Notification> |
| 734 | 743 | -> () / <io, proc> { |
| 735 | | − match read_message(conn, buffer, at) { |
| 744 | + match read_message conn buffer at { |
| 736 | 745 | Error(_) -> () |
| 737 | 746 | , Ok((msg, buffer, at)) -> { |
| 738 | 747 | match msg { |
| 739 | 748 | protocol.NotificationResponse(source, ch, payload) -> { |
| 740 | 749 | if ch == channel { |
| 741 | | − process.send(subscriber, Notification(ch, payload, source)) |
| 750 | + process.send subscriber Notification(ch, payload, source) |
| 742 | 751 | } |
| 743 | 752 | } |
| 744 | 753 | , _ -> () |
| 745 | 754 | } |
| 746 | | − listen_serve(conn, buffer, at, channel, subscriber) |
| 755 | + listen_serve conn buffer at channel subscriber |
| 747 | 756 | } |
| 748 | 757 | } |
| 749 | 758 | } |
| @@ -752,63 +761,65 @@ fn listen_serve |
| 752 | 761 | |
| 753 | 762 | /// Parameters encoded per their inferred types: binary where a codec |
| 754 | 763 | /// exists for the OID (binary.lse), text otherwise. `Null` has no format. |
| 755 | | −fn encode_params |
| 756 | | − (params: List<Value>, oids: List<Int>, acc: List<protocol.BindParam>) |
| 764 | +fn encode_params :: |
| 765 | + params: List<Value> |
| 766 | + oids: List<Int> |
| 767 | + acc: List<protocol.BindParam> |
| 757 | 768 | -> List<protocol.BindParam> { |
| 758 | 769 | match (params, oids) { |
| 759 | | − ([], _) -> list.reverse(acc) |
| 770 | + ([], _) -> list.reverse acc |
| 760 | 771 | , ([param, ..rest], [oid, ..rest_oids]) -> |
| 761 | | − encode_params(rest, rest_oids, [encode_one(param, oid), ..acc]) |
| 762 | | − , (_, []) -> list.reverse(acc) // the arity check in run_query came first |
| 772 | + encode_params rest rest_oids [encode_one param oid, ..acc] |
| 773 | + , (_, []) -> list.reverse acc // the arity check in run_query came first |
| 763 | 774 | } |
| 764 | 775 | } |
| 765 | 776 | |
| 766 | | −fn encode_one(value: Value, oid: Int) -> protocol.BindParam { |
| 777 | +fn encode_one :: value: Value oid: Int -> protocol.BindParam { |
| 767 | 778 | match value { |
| 768 | 779 | Null -> protocol.BindParam(None, 0) |
| 769 | 780 | , Integer(n) -> match oid { |
| 770 | | − 21 -> binary_param(binary.be16(n)) // int2 |
| 771 | | − , 23 -> binary_param(binary.be32(n)) // int4 |
| 772 | | − , 20 -> binary_param(binary.be64(n)) // int8 |
| 773 | | − , _ -> text_param("{n}") |
| 781 | + 21 -> binary_param (binary.be16 n) // int2 |
| 782 | + , 23 -> binary_param (binary.be32 n) // int4 |
| 783 | + , 20 -> binary_param (binary.be64 n) // int8 |
| 784 | + , _ -> text_param "{n}" |
| 774 | 785 | } |
| 775 | 786 | , Real(f) -> match oid { |
| 776 | | − 700 -> binary_param(binary.f32(f)) // float4 |
| 777 | | − , 701 -> binary_param(binary.f64(f)) // float8 |
| 778 | | − , _ -> text_param("{f}") |
| 787 | + 700 -> binary_param (binary.f32 f) // float4 |
| 788 | + , 701 -> binary_param (binary.f64 f) // float8 |
| 789 | + , _ -> text_param "{f}" |
| 779 | 790 | } |
| 780 | 791 | , Boolean |
| 781 | 792 | ( b |
| 782 | 793 | ) -> if oid == 16 { |
| 783 | | − binary_param(bytes.from_list([if b { 1 } else { 0 }])) |
| 794 | + binary_param (bytes.from_list [if b { 1 } else { 0 }]) |
| 784 | 795 | } else { |
| 785 | | − text_param(show(value)) |
| 796 | + text_param (show value) |
| 786 | 797 | } |
| 787 | | − , Text(s) -> text_param(s) |
| 798 | + , Text(s) -> text_param s |
| 788 | 799 | , Bytea(b) -> if oid == 17 { |
| 789 | | − binary_param(b) // raw, no `\x` form |
| 800 | + binary_param b // raw, no `\x` form |
| 790 | 801 | } else { |
| 791 | | − text_param(show(value)) |
| 802 | + text_param (show value) |
| 792 | 803 | } |
| 793 | 804 | , Uuid(id) -> if oid == 2950 { |
| 794 | | − binary_param(uuid.to_bytes(id)) |
| 805 | + binary_param (uuid.to_bytes id) |
| 795 | 806 | } else { |
| 796 | | − text_param(show(value)) |
| 807 | + text_param (show value) |
| 797 | 808 | } |
| 798 | 809 | , Timestamptz(t) -> if oid == 1184 { |
| 799 | | − binary_param(binary.be64(pg_micros(t))) |
| 810 | + binary_param (binary.be64 (pg_micros t)) |
| 800 | 811 | } else { |
| 801 | | − text_param(show(value)) |
| 812 | + text_param (show value) |
| 802 | 813 | } |
| 803 | 814 | } |
| 804 | 815 | } |
| 805 | 816 | |
| 806 | | −fn binary_param(data: Bytes) -> protocol.BindParam { |
| 817 | +fn binary_param :: data: Bytes -> protocol.BindParam { |
| 807 | 818 | protocol.BindParam(Some(data), 1) |
| 808 | 819 | } |
| 809 | 820 | |
| 810 | | −fn text_param(text: String) -> protocol.BindParam { |
| 811 | | − protocol.BindParam(Some(bytes.from_string(text)), 0) |
| 821 | +fn text_param :: text: String -> protocol.BindParam { |
| 822 | + protocol.BindParam(Some(bytes.from_string text), 0) |
| 812 | 823 | } |
| 813 | 824 | |
| 814 | 825 | /// The oid a `Parse` declares for a value whose type is unambiguous, so |
| @@ -816,7 +827,7 @@ fn text_param(text: String) -> protocol.BindParam { |
| 816 | 827 | /// `Integer` stays inferred: its width (int2/int4/int8) is the context's |
| 817 | 828 | /// call, and declaring int8 would break resolution of int4 functions |
| 818 | 829 | /// (`pg_terminate_backend($1)`). |
| 819 | | −fn declared_oid(value: Value) -> Int { |
| 830 | +fn declared_oid :: value: Value -> Int { |
| 820 | 831 | match value { |
| 821 | 832 | Boolean(_) -> 16 |
| 822 | 833 | , Bytea(_) -> 17 |
| @@ -828,29 +839,29 @@ fn declared_oid(value: Value) -> Int { |
| 828 | 839 | } |
| 829 | 840 | |
| 830 | 841 | /// A binary codec exists for the OID (results come back in it). |
| 831 | | −fn binary_known(oid: Int) -> Bool { |
| 832 | | − list.any([16, 17, 20, 21, 23, 700, 701, 1184, 2950], fn(o) { o == oid }) |
| 842 | +fn binary_known :: oid: Int -> Bool { |
| 843 | + list.any [16, 17, 20, 21, 23, 700, 701, 1184, 2950] fn o { o == oid } |
| 833 | 844 | } |
| 834 | 845 | |
| 835 | 846 | /// Microseconds since PostgreSQL's epoch (2000-01-01 00:00:00 UTC). |
| 836 | | −fn pg_micros(t: Timestamp) -> Int { |
| 837 | | − time.unix_ns(t) / 1000 - 946684800000000 |
| 847 | +fn pg_micros :: t: Timestamp -> Int { |
| 848 | + time.unix_ns t / 1000 - 946684800000000 |
| 838 | 849 | } |
| 839 | 850 | |
| 840 | 851 | /// The text format on the wire (`True`/`False` for booleans: PostgreSQL |
| 841 | 852 | /// reads them; `\x…` for bytea). In a `sql` template (lib.lse), write SQL |
| 842 | 853 | /// `null` literally — `Null` has no text. |
| 843 | 854 | impl Show<Value> { |
| 844 | | − fn show(value) { |
| 855 | + fn show :: value { |
| 845 | 856 | match value { |
| 846 | 857 | Null -> "" |
| 847 | 858 | , Integer(n) -> "{n}" |
| 848 | 859 | , Real(f) -> "{f}" |
| 849 | 860 | , Boolean(b) -> if b { "True" } else { "False" } |
| 850 | 861 | , Text(s) -> s |
| 851 | | − , Bytea(b) -> "\\x" <> hex(b) |
| 852 | | − , Uuid(id) -> uuid.to_rfc(id) |
| 853 | | − , Timestamptz(t) -> time.format(t) |
| 862 | + , Bytea(b) -> "\\x" <> hex b |
| 863 | + , Uuid(id) -> uuid.to_rfc id |
| 864 | + , Timestamptz(t) -> time.format t |
| 854 | 865 | } |
| 855 | 866 | } |
| 856 | 867 | } |
| @@ -858,26 +869,23 @@ impl Show<Value> { |
| 858 | 869 | /// Lowercase hex, built in one pass (a `bytea`'s text form): one buffer, |
| 859 | 870 | /// no per-byte strings — a list of them costs ~77 bytes and 0.55 µs per |
| 860 | 871 | /// byte (issue #311). |
| 861 | | −fn hex(b: Bytes) -> String { |
| 862 | | − bytes.build_string( hex_loop( b |
| 863 | | − , 0 |
| 864 | | − , bytes.builder_with_capacity(bytes.length(b) * 2) |
| 865 | | − ) |
| 866 | | − ) |
| 872 | +fn hex :: b: Bytes -> String { |
| 873 | + bytes.build_string |
| 874 | + (hex_loop b 0 (bytes.builder_with_capacity (bytes.length b * 2))) |
| 867 | 875 | } |
| 868 | 876 | |
| 869 | | −fn hex_loop(b: Bytes, at: Int, acc: bytes.Builder) -> bytes.Builder { |
| 870 | | − if at >= bytes.length(b) { |
| 877 | +fn hex_loop :: b: Bytes at: Int acc: bytes.Builder -> bytes.Builder { |
| 878 | + if at >= bytes.length b { |
| 871 | 879 | acc |
| 872 | 880 | } else { |
| 873 | | − let byte = bytes.read_u8(b, at) |
| 874 | | − let acc = bytes.append_byte(acc, hex_digit(byte / 16)) |
| 875 | | − hex_loop(b, at + 1, bytes.append_byte(acc, hex_digit(byte % 16))) |
| 881 | + let byte = bytes.read_u8 b at |
| 882 | + let acc = bytes.append_byte acc (hex_digit (byte / 16)) |
| 883 | + hex_loop b (at + 1) (bytes.append_byte acc (hex_digit (byte % 16))) |
| 876 | 884 | } |
| 877 | 885 | } |
| 878 | 886 | |
| 879 | 887 | /// The ASCII code of `d`'s hex digit (`0`–`9`, `a`–`f`). |
| 880 | | −fn hex_digit(d: Int) -> Int { |
| 888 | +fn hex_digit :: d: Int -> Int { |
| 881 | 889 | if d < 10 { 48 + d } else { 87 + d } |
| 882 | 890 | } |
| 883 | 891 | |
| @@ -886,44 +894,45 @@ fn hex_digit(d: Int) -> Int { |
| 886 | 894 | import std/test.{assert, assert_equal} |
| 887 | 895 | |
| 888 | 896 | @test |
| 889 | | −fn hex_is_lowercase_and_single_pass() { |
| 890 | | − assert_equal(hex(bytes.empty()), "") |
| 891 | | − assert_equal(hex(bytes.from_list([222, 173, 190, 239])), "deadbeef") |
| 892 | | − assert_equal(hex(bytes.from_list([0, 1, 15, 16, 255])), "00010f10ff") |
| 897 | +fn hex_is_lowercase_and_single_pass :: { |
| 898 | + assert_equal (hex (bytes.empty ())) "" |
| 899 | + assert_equal (hex (bytes.from_list [222, 173, 190, 239])) "deadbeef" |
| 900 | + assert_equal (hex (bytes.from_list [0, 1, 15, 16, 255])) "00010f10ff" |
| 893 | 901 | // A bytea's text form (the `sql` template's encoding of a hole). |
| 894 | | − assert_equal(show(Bytea(bytes.from_list([222, 173]))), "\\xdead") |
| 902 | + assert_equal (show Bytea(bytes.from_list [222, 173])) "\\xdead" |
| 895 | 903 | // Large enough that a per-byte-strings build would not fit the test |
| 896 | 904 | // budget: one buffer, one pass (issue #311). |
| 897 | 905 | let big = |
| 898 | | − bytes.build( list.fold( list.range(1, 200_000) |
| 899 | | − , bytes.builder_with_capacity(200_000) |
| 900 | | − , fn(b, i) { bytes.append_byte(b, i % 251) } |
| 906 | + bytes.build |
| 907 | + ( list.fold |
| 908 | + (list.range 1 200_000) |
| 909 | + (bytes.builder_with_capacity 200_000) |
| 910 | + fn b i { bytes.append_byte b (i % 251) } |
| 901 | 911 | ) |
| 902 | | − ) |
| 903 | | − let text = hex(big) |
| 904 | | − assert_equal(string.length(text), 400_000) |
| 905 | | − assert_equal(string.slice(text, 0, 8), "01020304") |
| 912 | + let text = hex big |
| 913 | + assert_equal (string.length text) 400_000 |
| 914 | + assert_equal (string.slice text 0 8) "01020304" |
| 906 | 915 | } |
| 907 | 916 | |
| 908 | 917 | @test |
| 909 | | −fn a_bytea_parameter_is_declared_and_bound_in_binary() { |
| 918 | +fn a_bytea_parameter_is_declared_and_bound_in_binary :: { |
| 910 | 919 | // The Parse declares the unambiguous types, so the binary codec is used |
| 911 | 920 | // whatever the SQL infers (issue #311); Integer/Text stay inferred. |
| 912 | | − assert_equal(declared_oid(Bytea(bytes.empty())), 17) |
| 913 | | − assert_equal(declared_oid(Boolean(True)), 16) |
| 914 | | − assert_equal(declared_oid(Real(1.5)), 701) |
| 915 | | − assert_equal(declared_oid(Integer(42)), 0) |
| 916 | | − assert_equal(declared_oid(Text("x")), 0) |
| 917 | | − assert_equal(declared_oid(Null), 0) |
| 918 | | − match uuid.from_rfc("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11") { |
| 919 | | − Some(id) -> assert_equal(declared_oid(Uuid(id)), 2950) |
| 920 | | − , None -> panic("a literal uuid") |
| 921 | | − } |
| 922 | | − assert_equal(declared_oid(Timestamptz(time.from_unix_ns(0))), 1184) |
| 921 | + assert_equal (declared_oid Bytea(bytes.empty ())) 17 |
| 922 | + assert_equal (declared_oid Boolean(True)) 16 |
| 923 | + assert_equal (declared_oid Real(1.5)) 701 |
| 924 | + assert_equal (declared_oid Integer(42)) 0 |
| 925 | + assert_equal (declared_oid Text("x")) 0 |
| 926 | + assert_equal (declared_oid Null) 0 |
| 927 | + match uuid.from_rfc "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" { |
| 928 | + Some(id) -> assert_equal (declared_oid Uuid(id)) 2950 |
| 929 | + , None -> panic "a literal uuid" |
| 930 | + } |
| 931 | + assert_equal (declared_oid Timestamptz(time.from_unix_ns 0)) 1184 |
| 923 | 932 | // And with the oid known, the parameter is the raw bytes, format 1. |
| 924 | | − let data = bytes.from_list([1, 2, 3]) |
| 925 | | − match encode_one(Bytea(data), 17) { |
| 926 | | − protocol.BindParam(Some(b), 1) -> assert(b == data, "raw bytes, binary") |
| 927 | | − , _ -> panic("a bytea binds in binary") |
| 933 | + let data = bytes.from_list [1, 2, 3] |
| 934 | + match encode_one Bytea(data) 17 { |
| 935 | + protocol.BindParam(Some(b), 1) -> assert (b == data) "raw bytes, binary" |
| 936 | + , _ -> panic "a bytea binds in binary" |
| 928 | 937 | } |
| 929 | 938 | } |