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