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