| @@ -9,6 +9,7 @@ |
| 9 | 9 | import std/actor.{Continue, Stop} |
| 10 | 10 | import std/base64 |
| 11 | 11 | import std/bytes |
| 12 | +import std/dict.{type Dict} |
| 12 | 13 | import std/ffi.{type Resource} |
| 13 | 14 | import std/int |
| 14 | 15 | import std/list |
| @@ -18,11 +19,14 @@ import std/process.{type Pid, type Subject, type proc} |
| 18 | 19 | import std/random.{type random} |
| 19 | 20 | import std/result |
| 20 | 21 | import std/string |
| 22 | +import std/time.{type Timestamp} |
| 21 | 23 | import std/tls.{type TlsStream} |
| 22 | 24 | |
| 25 | +import binary |
| 23 | 26 | import config.{type Config, Disable, Require} |
| 24 | 27 | import protocol |
| 25 | 28 | import scram |
| 29 | +import uuid |
| 26 | 30 | |
| 27 | 31 | /// What a call failed with. |
| 28 | 32 | @derive(Debug) |
| @@ -46,6 +50,8 @@ pub type Value { |
| 46 | 50 | Boolean(Bool) |
| 47 | 51 | Text(String) |
| 48 | 52 | Bytea(Bytes) |
| 53 | + Uuid(uuid.Uuid) |
| 54 | + Timestamptz(time.Timestamp) |
| 49 | 55 | } |
| 50 | 56 | |
| 51 | 57 | /// A row set's columns (the protocol's fields) and rows. |
| @@ -123,7 +129,7 @@ pub fn connect_over(socket: Socket) -> Subject<Msg> / <proc> { |
| 123 | 129 | |
| 124 | 130 | fn state_of(socket: Socket) -> State { |
| 125 | 131 | let Socket(conn, buffer, host, port, backend) = socket |
| 126 | | − State(conn, buffer, host, port, backend) |
| 132 | + State(conn, buffer, host, port, backend, dict.empty(), 1) |
| 127 | 133 | } |
| 128 | 134 | |
| 129 | 135 | /// TLS if the mode wants it: `SslRequest`, then the handshake if the |
| @@ -267,10 +273,25 @@ fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 267 | 273 | |
| 268 | 274 | // ---- the query exchange ------------------------------------------------------- |
| 269 | 275 | |
| 270 | | −/// The actor's state: the socket, the read buffer, the settings, and the |
| 271 | | −/// backend key data (for `CancelRequest`). |
| 276 | +/// The actor's state: the socket, the read buffer, the settings, the |
| 277 | +/// backend key data (for `CancelRequest`), and the statements prepared on |
| 278 | +/// this connection, by SQL text (issue #145 §1). |
| 272 | 279 | type State { |
| 273 | | − State(conn: Conn, buffer: Bytes, host: String, port: Int, backend: Option<(Int, Int)>) |
| 280 | + State( |
| 281 | + conn: Conn, |
| 282 | + buffer: Bytes, |
| 283 | + host: String, |
| 284 | + port: Int, |
| 285 | + backend: Option<(Int, Int)>, |
| 286 | + prepared: Dict<String, Prepared>, |
| 287 | + next_statement: Int, |
| 288 | + ) |
| 289 | +} |
| 290 | + |
| 291 | +/// A statement prepared on this connection: its name, its parameters' |
| 292 | +/// type OIDs, and its result columns. |
| 293 | +type Prepared { |
| 294 | + Prepared(name: String, param_oids: List<Int>, fields: List<protocol.Field>) |
| 274 | 295 | } |
| 275 | 296 | |
| 276 | 297 | /// The connection's messages. |
| @@ -282,9 +303,9 @@ pub type Msg { |
| 282 | 303 | fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 283 | 304 | match msg { |
| 284 | 305 | DoQuery(sql, params, reply) -> { |
| 285 | | − let (result, buffer) = run_query(state.conn, state.buffer, sql, params) |
| 306 | + let (result, state) = run_query(state, sql, params) |
| 286 | 307 | process.send(reply, result) |
| 287 | | − Continue(State(..state, buffer: buffer)) |
| 308 | + Continue(state) |
| 288 | 309 | }, |
| 289 | 310 | Close(reply) -> { |
| 290 | 311 | close_conn(state.conn) |
| @@ -294,25 +315,96 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 294 | 315 | } |
| 295 | 316 | } |
| 296 | 317 | |
| 297 | | −/// The exchange: Parse, Bind, Describe the portal, Execute, Sync — one |
| 298 | | −/// write — then collect until `ReadyForQuery`. |
| 299 | | −fn run_query( |
| 318 | +/// A query: the statement is prepared once per connection (Parse + |
| 319 | +/// Describe, one exchange), then Bind + Execute. Parameters travel as |
| 320 | +/// `$1`, binary per type where a codec exists (binary.lse). |
| 321 | +fn run_query(state: State, sql: String, params: List<Value>) -> (Result<QueryResult, Error>, State) / <io> { |
| 322 | + match prepare(state, sql) { |
| 323 | + (Error(e), state) -> (Error(e), state), |
| 324 | + (Ok(prep), state) -> |
| 325 | + if list.length(params) != list.length(prep.param_oids) { |
| 326 | + ( |
| 327 | + Error(Driver("the statement takes {list.length(prep.param_oids)} parameters, got {list.length(params)}")), |
| 328 | + state, |
| 329 | + ) |
| 330 | + } else { |
| 331 | + let encoded = encode_params(params, prep.param_oids, []) |
| 332 | + let formats = list.map(prep.fields, fn(f) { if binary_known(f.type_oid) { 1 } else { 0 } }) |
| 333 | + let out = [ |
| 334 | + protocol.Bind("", prep.name, encoded, formats), |
| 335 | + protocol.DescribePortal(""), |
| 336 | + protocol.Execute("", 0), |
| 337 | + protocol.Sync, |
| 338 | + ] |
| 339 | + match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 340 | + Error(e) -> (Error(e), state), |
| 341 | + Ok(()) -> { |
| 342 | + let (result, buffer) = collect(state.conn, state.buffer, [], [], "", None) |
| 343 | + (result, State(..state, buffer: buffer)) |
| 344 | + }, |
| 345 | + } |
| 346 | + }, |
| 347 | + } |
| 348 | +} |
| 349 | + |
| 350 | +/// The statement prepared on this connection: from the cache, or by Parse |
| 351 | +/// + Describe (one exchange). |
| 352 | +fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) / <io> { |
| 353 | + match dict.get(state.prepared, sql) { |
| 354 | + Some(prep) -> (Ok(prep), state), |
| 355 | + None -> { |
| 356 | + let name = "q{state.next_statement}" |
| 357 | + let out = [ |
| 358 | + protocol.Parse(name, sql), |
| 359 | + protocol.DescribeStatement(name), |
| 360 | + protocol.Sync, |
| 361 | + ] |
| 362 | + match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 363 | + Error(e) -> (Error(e), state), |
| 364 | + Ok(()) -> |
| 365 | + match read_describe(state.conn, state.buffer, [], [], None) { |
| 366 | + (Error(e), buffer) -> (Error(e), State(..state, buffer: buffer)), |
| 367 | + (Ok((oids, fields)), buffer) -> { |
| 368 | + let prep = Prepared(name, oids, fields) |
| 369 | + ( |
| 370 | + Ok(prep), |
| 371 | + State(..state, |
| 372 | + buffer: buffer, |
| 373 | + prepared: dict.insert(state.prepared, sql, prep), |
| 374 | + next_statement: state.next_statement + 1, |
| 375 | + ), |
| 376 | + ) |
| 377 | + }, |
| 378 | + }, |
| 379 | + } |
| 380 | + }, |
| 381 | + } |
| 382 | +} |
| 383 | + |
| 384 | +/// Read the answers to Parse + Describe until `ReadyForQuery`: the |
| 385 | +/// parameters' type OIDs and the result columns. |
| 386 | +fn read_describe( |
| 300 | 387 | conn: Conn, |
| 301 | 388 | buffer: Bytes, |
| 302 | | − sql: String, |
| 303 | | − params: List<Value>, |
| 304 | | −) -> (Result<QueryResult, Error>, Bytes) / <io> { |
| 305 | | − let encoded = list.map(params, encode_param) |
| 306 | | − let out = [ |
| 307 | | − protocol.Parse("", sql), |
| 308 | | − protocol.Bind("", "", encoded), |
| 309 | | − protocol.DescribePortal(""), |
| 310 | | − protocol.Execute("", 0), |
| 311 | | − protocol.Sync, |
| 312 | | − ] |
| 313 | | − match conn_write(conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 314 | | − Error(e) -> (Error(e), buffer), |
| 315 | | − Ok(()) -> collect(conn, buffer, [], [], "", None), |
| 389 | + param_oids: List<Int>, |
| 390 | + fields: List<protocol.Field>, |
| 391 | + error: Option<Error>, |
| 392 | +) -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes) / <io> { |
| 393 | + match read_message(conn, buffer) { |
| 394 | + Error(e) -> (Error(e), bytes.empty()), |
| 395 | + Ok((msg, buffer)) -> |
| 396 | + match msg { |
| 397 | + protocol.ParameterDescription(oids) -> read_describe(conn, buffer, oids, fields, error), |
| 398 | + protocol.RowDescription(f) -> read_describe(conn, buffer, param_oids, f, error), |
| 399 | + protocol.NoData -> read_describe(conn, buffer, param_oids, [], error), |
| 400 | + protocol.ErrorResponse(f) -> read_describe(conn, buffer, param_oids, fields, Some(Server(f))), |
| 401 | + protocol.ReadyForQuery(_) -> |
| 402 | + match error { |
| 403 | + Some(e) -> (Error(e), buffer), |
| 404 | + None -> (Ok((param_oids, fields)), buffer), |
| 405 | + }, |
| 406 | + _ -> read_describe(conn, buffer, param_oids, fields, error), |
| 407 | + } |
| 316 | 408 | } |
| 317 | 409 | } |
| 318 | 410 | |
| @@ -448,14 +540,83 @@ fn listen_serve(conn: Conn, buffer: Bytes, channel: String, subscriber: Subject< |
| 448 | 540 | |
| 449 | 541 | // ---- parameters --------------------------------------------------------------- |
| 450 | 542 | |
| 451 | | −/// A parameter in text format; `Null` is SQL `NULL`. |
| 452 | | −fn encode_param(param: Value) -> Option<Bytes> { |
| 453 | | − match param { |
| 454 | | − Null -> None, |
| 455 | | − value -> Some(bytes.from_string(show(value))), |
| 543 | +/// Parameters encoded per their inferred types: binary where a codec |
| 544 | +/// exists for the OID (binary.lse), text otherwise. `Null` has no format. |
| 545 | +fn encode_params( |
| 546 | + params: List<Value>, |
| 547 | + oids: List<Int>, |
| 548 | + acc: List<protocol.BindParam>, |
| 549 | +) -> List<protocol.BindParam> { |
| 550 | + match (params, oids) { |
| 551 | + ([], _) -> list.reverse(acc), |
| 552 | + ([param, ..rest], [oid, ..rest_oids]) -> |
| 553 | + encode_params(rest, rest_oids, [encode_one(param, oid), ..acc]), |
| 554 | + (_, []) -> list.reverse(acc), // the arity check in run_query came first |
| 555 | + } |
| 556 | +} |
| 557 | + |
| 558 | +fn encode_one(value: Value, oid: Int) -> protocol.BindParam { |
| 559 | + match value { |
| 560 | + Null -> protocol.BindParam(None, 0), |
| 561 | + Integer(n) -> |
| 562 | + match oid { |
| 563 | + 21 -> binary_param(binary.be16(n)), // int2 |
| 564 | + 23 -> binary_param(binary.be32(n)), // int4 |
| 565 | + 20 -> binary_param(binary.be64(n)), // int8 |
| 566 | + _ -> text_param("{n}"), |
| 567 | + }, |
| 568 | + Real(f) -> |
| 569 | + match oid { |
| 570 | + 700 -> binary_param(binary.f32(f)), // float4 |
| 571 | + 701 -> binary_param(binary.f64(f)), // float8 |
| 572 | + _ -> text_param("{f}"), |
| 573 | + }, |
| 574 | + Boolean(b) -> |
| 575 | + if oid == 16 { |
| 576 | + binary_param(bytes.from_list([if b { 1 } else { 0 }])) |
| 577 | + } else { |
| 578 | + text_param(show(value)) |
| 579 | + }, |
| 580 | + Text(s) -> text_param(s), |
| 581 | + Bytea(b) -> |
| 582 | + if oid == 17 { |
| 583 | + binary_param(b) // raw, no `\x` form |
| 584 | + } else { |
| 585 | + text_param(show(value)) |
| 586 | + }, |
| 587 | + Uuid(id) -> |
| 588 | + if oid == 2950 { |
| 589 | + binary_param(uuid.to_bytes(id)) |
| 590 | + } else { |
| 591 | + text_param(show(value)) |
| 592 | + }, |
| 593 | + Timestamptz(t) -> |
| 594 | + if oid == 1184 { |
| 595 | + binary_param(binary.be64(pg_micros(t))) |
| 596 | + } else { |
| 597 | + text_param(show(value)) |
| 598 | + }, |
| 456 | 599 | } |
| 457 | 600 | } |
| 458 | 601 | |
| 602 | +fn binary_param(data: Bytes) -> protocol.BindParam { |
| 603 | + protocol.BindParam(Some(data), 1) |
| 604 | +} |
| 605 | + |
| 606 | +fn text_param(text: String) -> protocol.BindParam { |
| 607 | + protocol.BindParam(Some(bytes.from_string(text)), 0) |
| 608 | +} |
| 609 | + |
| 610 | +/// A binary codec exists for the OID (results come back in it). |
| 611 | +fn binary_known(oid: Int) -> Bool { |
| 612 | + list.any([16, 17, 20, 21, 23, 700, 701, 1184, 2950], fn(o) { o == oid }) |
| 613 | +} |
| 614 | + |
| 615 | +/// Microseconds since PostgreSQL's epoch (2000-01-01 00:00:00 UTC). |
| 616 | +fn pg_micros(t: Timestamp) -> Int { |
| 617 | + time.unix_ns(t) / 1000 - 946684800000000 |
| 618 | +} |
| 619 | + |
| 459 | 620 | /// The text format on the wire (`True`/`False` for booleans: PostgreSQL |
| 460 | 621 | /// reads them; `\x…` for bytea). In a `sql` template (lib.lse), write SQL |
| 461 | 622 | /// `null` literally — `Null` has no text. |
| @@ -468,6 +629,8 @@ impl Show<Value> { |
| 468 | 629 | Boolean(b) -> if b { "True" } else { "False" }, |
| 469 | 630 | Text(s) -> s, |
| 470 | 631 | Bytea(b) -> "\\x" <> hex(b), |
| 632 | + Uuid(id) -> uuid.to_rfc(id), |
| 633 | + Timestamptz(t) -> time.format(t), |
| 471 | 634 | } |
| 472 | 635 | } |
| 473 | 636 | } |