sqlmodel-postgres 0.2.2

PostgreSQL driver for SQLModel Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! PostgreSQL connection implementation.
//!
//! This module implements the PostgreSQL wire protocol connection,
//! including connection establishment, authentication, and state management.
//!
//! # Console Integration
//!
//! When the `console` feature is enabled, the connection can report progress
//! during connection establishment. Use the `ConsoleAware` trait to attach
//! a console for rich output.
//!
//! ```rust,ignore
//! use sqlmodel_postgres::{PgConfig, PgConnection};
//! use sqlmodel_console::{SqlModelConsole, ConsoleAware};
//! use std::sync::Arc;
//!
//! let console = Arc::new(SqlModelConsole::new());
//! let mut conn = PgConnection::connect(config)?;
//! conn.set_console(Some(console));
//! ```

use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::TcpStream;
#[cfg(feature = "console")]
use std::sync::Arc;

use sqlmodel_core::Error;
use sqlmodel_core::error::{
    ConnectionError, ConnectionErrorKind, ProtocolError, QueryError, QueryErrorKind,
};

#[cfg(feature = "console")]
use sqlmodel_console::{ConsoleAware, SqlModelConsole};

use crate::auth::ScramClient;
use crate::config::PgConfig;
#[cfg(not(feature = "tls"))]
use crate::config::SslMode;
use crate::protocol::{
    BackendMessage, ErrorFields, FrontendMessage, MessageReader, MessageWriter, PROTOCOL_VERSION,
    TransactionStatus,
};

#[cfg(feature = "tls")]
use crate::tls;

enum PgStream {
    Plain(TcpStream),
    #[cfg(feature = "tls")]
    Tls(rustls::StreamOwned<rustls::ClientConnection, TcpStream>),
    #[cfg(feature = "tls")]
    Closed,
}

impl PgStream {
    #[cfg(feature = "tls")]
    fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> {
        match self {
            PgStream::Plain(s) => s.read_exact(buf),
            #[cfg(feature = "tls")]
            PgStream::Tls(s) => s.read_exact(buf),
            #[cfg(feature = "tls")]
            PgStream::Closed => Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "connection closed",
            )),
        }
    }

    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        match self {
            PgStream::Plain(s) => s.read(buf),
            #[cfg(feature = "tls")]
            PgStream::Tls(s) => s.read(buf),
            #[cfg(feature = "tls")]
            PgStream::Closed => Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "connection closed",
            )),
        }
    }

    fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
        match self {
            PgStream::Plain(s) => s.write_all(buf),
            #[cfg(feature = "tls")]
            PgStream::Tls(s) => s.write_all(buf),
            #[cfg(feature = "tls")]
            PgStream::Closed => Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "connection closed",
            )),
        }
    }

    fn flush(&mut self) -> std::io::Result<()> {
        match self {
            PgStream::Plain(s) => s.flush(),
            #[cfg(feature = "tls")]
            PgStream::Tls(s) => s.flush(),
            #[cfg(feature = "tls")]
            PgStream::Closed => Err(std::io::Error::new(
                std::io::ErrorKind::NotConnected,
                "connection closed",
            )),
        }
    }
}

/// Connection state in the PostgreSQL protocol state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    /// Not connected
    Disconnected,
    /// TCP connection established, sending startup
    Connecting,
    /// Performing authentication handshake
    Authenticating,
    /// Ready for queries
    Ready(TransactionStatusState),
    /// Currently executing a query
    InQuery,
    /// In a transaction block
    InTransaction(TransactionStatusState),
    /// Connection is in an error state
    Error,
    /// Connection has been closed
    Closed,
}

/// Transaction status from the server.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TransactionStatusState {
    /// Not in a transaction block ('I')
    #[default]
    Idle,
    /// In a transaction block ('T')
    InTransaction,
    /// In a failed transaction block ('E')
    InFailed,
}

impl From<TransactionStatus> for TransactionStatusState {
    fn from(status: TransactionStatus) -> Self {
        match status {
            TransactionStatus::Idle => TransactionStatusState::Idle,
            TransactionStatus::Transaction => TransactionStatusState::InTransaction,
            TransactionStatus::Error => TransactionStatusState::InFailed,
        }
    }
}

/// PostgreSQL connection.
///
/// Manages a TCP connection to a PostgreSQL server, handling the wire protocol,
/// authentication, and state tracking.
///
/// # Console Support
///
/// When the `console` feature is enabled, the connection can report progress
/// via an attached `SqlModelConsole`. This provides rich feedback during
/// connection establishment and query execution.
pub struct PgConnection {
    /// TCP stream to the server
    stream: PgStream,
    /// Current connection state
    state: ConnectionState,
    /// Backend process ID (for query cancellation)
    process_id: i32,
    /// Secret key (for query cancellation)
    secret_key: i32,
    /// Server parameters received during startup
    parameters: HashMap<String, String>,
    /// Connection configuration
    config: PgConfig,
    /// Message reader for parsing backend messages
    reader: MessageReader,
    /// Message writer for encoding frontend messages
    writer: MessageWriter,
    /// Read buffer
    read_buf: Vec<u8>,
    /// Optional console for rich output
    #[cfg(feature = "console")]
    console: Option<Arc<SqlModelConsole>>,
}

impl std::fmt::Debug for PgConnection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PgConnection")
            .field("state", &self.state)
            .field("process_id", &self.process_id)
            .field("host", &self.config.host)
            .field("port", &self.config.port)
            .field("database", &self.config.database)
            .finish_non_exhaustive()
    }
}

impl PgConnection {
    /// Establish a new connection to the PostgreSQL server.
    ///
    /// This performs the complete connection handshake:
    /// 1. TCP connection
    /// 2. SSL negotiation (if configured)
    /// 3. Startup message
    /// 4. Authentication
    /// 5. Receive server parameters and ReadyForQuery
    #[allow(clippy::result_large_err)]
    pub fn connect(config: PgConfig) -> Result<Self, Error> {
        // 1. TCP connection with timeout
        let stream = TcpStream::connect_timeout(
            &config.socket_addr().parse().map_err(|e| {
                Error::Connection(ConnectionError {
                    kind: ConnectionErrorKind::Connect,
                    message: format!("Invalid socket address: {}", e),
                    source: None,
                })
            })?,
            config.connect_timeout,
        )
        .map_err(|e| {
            let kind = if e.kind() == std::io::ErrorKind::ConnectionRefused {
                ConnectionErrorKind::Refused
            } else {
                ConnectionErrorKind::Connect
            };
            Error::Connection(ConnectionError {
                kind,
                message: format!("Failed to connect to {}: {}", config.socket_addr(), e),
                source: Some(Box::new(e)),
            })
        })?;

        // Set TCP options
        stream.set_nodelay(true).ok();
        stream.set_read_timeout(Some(config.connect_timeout)).ok();
        stream.set_write_timeout(Some(config.connect_timeout)).ok();

        let mut conn = Self {
            stream: PgStream::Plain(stream),
            state: ConnectionState::Connecting,
            process_id: 0,
            secret_key: 0,
            parameters: HashMap::new(),
            config,
            reader: MessageReader::new(),
            writer: MessageWriter::new(),
            read_buf: vec![0u8; 8192],
            #[cfg(feature = "console")]
            console: None,
        };

        // 2. SSL negotiation (if configured)
        if conn.config.ssl_mode.should_try_ssl() {
            #[cfg(feature = "tls")]
            conn.negotiate_ssl()?;

            #[cfg(not(feature = "tls"))]
            if conn.config.ssl_mode != SslMode::Prefer {
                return Err(Error::Connection(ConnectionError {
                    kind: ConnectionErrorKind::Ssl,
                    message:
                        "TLS requested but 'sqlmodel-postgres' was built without feature 'tls'"
                            .to_string(),
                    source: None,
                }));
            }
        }

        // 3. Send startup message
        conn.send_startup()?;
        conn.state = ConnectionState::Authenticating;

        // 4. Handle authentication
        conn.handle_auth()?;

        // 5. Read remaining startup messages until ReadyForQuery
        conn.read_startup_messages()?;

        Ok(conn)
    }

    /// Get the current connection state.
    pub fn state(&self) -> ConnectionState {
        self.state
    }

    /// Check if the connection is ready for queries.
    pub fn is_ready(&self) -> bool {
        matches!(self.state, ConnectionState::Ready(_))
    }

    /// Get the backend process ID (for query cancellation).
    pub fn process_id(&self) -> i32 {
        self.process_id
    }

    /// Get the secret key (for query cancellation).
    pub fn secret_key(&self) -> i32 {
        self.secret_key
    }

    /// Get a server parameter value.
    pub fn parameter(&self, name: &str) -> Option<&str> {
        self.parameters.get(name).map(|s| s.as_str())
    }

    /// Get all server parameters.
    pub fn parameters(&self) -> &HashMap<String, String> {
        &self.parameters
    }

    /// Close the connection gracefully.
    #[allow(clippy::result_large_err)]
    pub fn close(&mut self) -> Result<(), Error> {
        if matches!(
            self.state,
            ConnectionState::Closed | ConnectionState::Disconnected
        ) {
            return Ok(());
        }

        // Send Terminate message
        self.send_message(&FrontendMessage::Terminate)?;
        self.state = ConnectionState::Closed;
        Ok(())
    }

    // ==================== SSL Negotiation ====================

    #[allow(clippy::result_large_err)]
    #[cfg(feature = "tls")]
    fn negotiate_ssl(&mut self) -> Result<(), Error> {
        // Send SSL request
        self.send_message(&FrontendMessage::SSLRequest)?;

        // Read single-byte response
        let mut buf = [0u8; 1];
        self.stream.read_exact(&mut buf).map_err(|e| {
            Error::Connection(ConnectionError {
                kind: ConnectionErrorKind::Ssl,
                message: format!("Failed to read SSL response: {}", e),
                source: Some(Box::new(e)),
            })
        })?;

        match buf[0] {
            b'S' => {
                // Server supports SSL; upgrade to TLS.
                #[cfg(feature = "tls")]
                {
                    let plain = match std::mem::replace(&mut self.stream, PgStream::Closed) {
                        PgStream::Plain(s) => s,
                        other => {
                            self.stream = other;
                            return Err(Error::Connection(ConnectionError {
                                kind: ConnectionErrorKind::Ssl,
                                message: "TLS upgrade requires a plain TCP stream".to_string(),
                                source: None,
                            }));
                        }
                    };

                    let config = tls::build_client_config(self.config.ssl_mode)?;
                    let server_name = tls::server_name(&self.config.host)?;
                    let conn =
                        rustls::ClientConnection::new(std::sync::Arc::new(config), server_name)
                            .map_err(|e| {
                                Error::Connection(ConnectionError {
                                    kind: ConnectionErrorKind::Ssl,
                                    message: format!("Failed to create TLS connection: {e}"),
                                    source: None,
                                })
                            })?;

                    let mut tls_stream = rustls::StreamOwned::new(conn, plain);
                    while tls_stream.conn.is_handshaking() {
                        tls_stream
                            .conn
                            .complete_io(&mut tls_stream.sock)
                            .map_err(|e| {
                                Error::Connection(ConnectionError {
                                    kind: ConnectionErrorKind::Ssl,
                                    message: format!("TLS handshake failed: {e}"),
                                    source: Some(Box::new(e)),
                                })
                            })?;
                    }

                    self.stream = PgStream::Tls(tls_stream);
                    Ok(())
                }

                #[cfg(not(feature = "tls"))]
                {
                    Err(Error::Connection(ConnectionError {
                        kind: ConnectionErrorKind::Ssl,
                        message:
                            "TLS requested but 'sqlmodel-postgres' was built without feature 'tls'"
                                .to_string(),
                        source: None,
                    }))
                }
            }
            b'N' => {
                // Server doesn't support SSL
                if self.config.ssl_mode.is_required() {
                    return Err(Error::Connection(ConnectionError {
                        kind: ConnectionErrorKind::Ssl,
                        message: "Server does not support SSL".to_string(),
                        source: None,
                    }));
                }
                // Continue without SSL (prefer mode)
                Ok(())
            }
            _ => Err(Error::Connection(ConnectionError {
                kind: ConnectionErrorKind::Ssl,
                message: format!("Unexpected SSL response: 0x{:02x}", buf[0]),
                source: None,
            })),
        }
    }

    // ==================== Startup ====================

    #[allow(clippy::result_large_err)]
    fn send_startup(&mut self) -> Result<(), Error> {
        let params = self.config.startup_params();
        let msg = FrontendMessage::Startup {
            version: PROTOCOL_VERSION,
            params,
        };
        self.send_message(&msg)
    }

    // ==================== Authentication ====================

    #[allow(clippy::result_large_err)]
    fn require_auth_value(&self, message: &'static str) -> Result<&str, Error> {
        // NOTE: Auth values are sourced from runtime config, not hardcoded.
        self.config
            .password
            .as_deref()
            .ok_or_else(|| auth_error(message))
    }

    #[allow(clippy::result_large_err)]
    fn handle_auth(&mut self) -> Result<(), Error> {
        loop {
            let msg = self.receive_message()?;

            match msg {
                BackendMessage::AuthenticationOk => {
                    return Ok(());
                }
                BackendMessage::AuthenticationCleartextPassword => {
                    let auth_value =
                        self.require_auth_value("Authentication value required but not provided")?;
                    self.send_message(&FrontendMessage::PasswordMessage(auth_value.to_string()))?;
                }
                BackendMessage::AuthenticationMD5Password(salt) => {
                    let auth_value =
                        self.require_auth_value("Authentication value required but not provided")?;
                    let hash = md5_password(&self.config.user, auth_value, salt);
                    self.send_message(&FrontendMessage::PasswordMessage(hash))?;
                }
                BackendMessage::AuthenticationSASL(mechanisms) => {
                    if mechanisms.contains(&"SCRAM-SHA-256".to_string()) {
                        self.scram_auth()?;
                    } else {
                        return Err(auth_error(format!(
                            "Unsupported SASL mechanisms: {:?}",
                            mechanisms
                        )));
                    }
                }
                BackendMessage::ErrorResponse(e) => {
                    self.state = ConnectionState::Error;
                    return Err(error_from_fields(&e));
                }
                _ => {
                    return Err(Error::Protocol(ProtocolError {
                        message: format!("Unexpected message during auth: {:?}", msg),
                        raw_data: None,
                        source: None,
                    }));
                }
            }
        }
    }

    #[allow(clippy::result_large_err)]
    fn scram_auth(&mut self) -> Result<(), Error> {
        let auth_value =
            self.require_auth_value("Authentication value required for SCRAM-SHA-256")?;

        let mut client = ScramClient::new(&self.config.user, auth_value);

        // Send client-first message
        let client_first = client.client_first();
        self.send_message(&FrontendMessage::SASLInitialResponse {
            mechanism: "SCRAM-SHA-256".to_string(),
            data: client_first,
        })?;

        // Receive server-first
        let msg = self.receive_message()?;
        let server_first_data = match msg {
            BackendMessage::AuthenticationSASLContinue(data) => data,
            BackendMessage::ErrorResponse(e) => {
                self.state = ConnectionState::Error;
                return Err(error_from_fields(&e));
            }
            _ => {
                return Err(Error::Protocol(ProtocolError {
                    message: format!("Expected SASL continue, got: {:?}", msg),
                    raw_data: None,
                    source: None,
                }));
            }
        };

        // Generate and send client-final
        let client_final = client.process_server_first(&server_first_data)?;
        self.send_message(&FrontendMessage::SASLResponse(client_final))?;

        // Receive server-final
        let msg = self.receive_message()?;
        let server_final_data = match msg {
            BackendMessage::AuthenticationSASLFinal(data) => data,
            BackendMessage::ErrorResponse(e) => {
                self.state = ConnectionState::Error;
                return Err(error_from_fields(&e));
            }
            _ => {
                return Err(Error::Protocol(ProtocolError {
                    message: format!("Expected SASL final, got: {:?}", msg),
                    raw_data: None,
                    source: None,
                }));
            }
        };

        // Verify server signature
        client.verify_server_final(&server_final_data)?;

        // Wait for AuthenticationOk
        let msg = self.receive_message()?;
        match msg {
            BackendMessage::AuthenticationOk => Ok(()),
            BackendMessage::ErrorResponse(e) => {
                self.state = ConnectionState::Error;
                Err(error_from_fields(&e))
            }
            _ => Err(Error::Protocol(ProtocolError {
                message: format!("Expected AuthenticationOk, got: {:?}", msg),
                raw_data: None,
                source: None,
            })),
        }
    }

    // ==================== Startup Messages ====================

    #[allow(clippy::result_large_err)]
    fn read_startup_messages(&mut self) -> Result<(), Error> {
        loop {
            let msg = self.receive_message()?;

            match msg {
                BackendMessage::BackendKeyData {
                    process_id,
                    secret_key,
                } => {
                    self.process_id = process_id;
                    self.secret_key = secret_key;
                }
                BackendMessage::ParameterStatus { name, value } => {
                    self.parameters.insert(name, value);
                }
                BackendMessage::ReadyForQuery(status) => {
                    self.state = ConnectionState::Ready(status.into());
                    return Ok(());
                }
                BackendMessage::ErrorResponse(e) => {
                    self.state = ConnectionState::Error;
                    return Err(error_from_fields(&e));
                }
                BackendMessage::NoticeResponse(_notice) => {
                    // Log but continue - notices are informational
                }
                _ => {
                    return Err(Error::Protocol(ProtocolError {
                        message: format!("Unexpected startup message: {:?}", msg),
                        raw_data: None,
                        source: None,
                    }));
                }
            }
        }
    }

    // ==================== Low-Level I/O ====================

    #[allow(clippy::result_large_err)]
    fn send_message(&mut self, msg: &FrontendMessage) -> Result<(), Error> {
        let data = self.writer.write(msg);
        self.stream.write_all(data).map_err(|e| {
            self.state = ConnectionState::Error;
            Error::Io(e)
        })?;
        self.stream.flush().map_err(|e| {
            self.state = ConnectionState::Error;
            Error::Io(e)
        })?;
        Ok(())
    }

    #[allow(clippy::result_large_err)]
    fn receive_message(&mut self) -> Result<BackendMessage, Error> {
        // Try to parse any complete messages from buffer first
        loop {
            match self.reader.next_message() {
                Ok(Some(msg)) => return Ok(msg),
                Ok(None) => {
                    // Need more data
                    let n = self.stream.read(&mut self.read_buf).map_err(|e| {
                        if e.kind() == std::io::ErrorKind::TimedOut
                            || e.kind() == std::io::ErrorKind::WouldBlock
                        {
                            Error::Timeout
                        } else {
                            self.state = ConnectionState::Error;
                            Error::Connection(ConnectionError {
                                kind: ConnectionErrorKind::Disconnected,
                                message: format!("Failed to read from server: {}", e),
                                source: Some(Box::new(e)),
                            })
                        }
                    })?;

                    if n == 0 {
                        self.state = ConnectionState::Disconnected;
                        return Err(Error::Connection(ConnectionError {
                            kind: ConnectionErrorKind::Disconnected,
                            message: "Connection closed by server".to_string(),
                            source: None,
                        }));
                    }

                    // Feed data to reader
                    self.reader.feed(&self.read_buf[..n]).map_err(|e| {
                        Error::Protocol(ProtocolError {
                            message: format!("Protocol error: {}", e),
                            raw_data: None,
                            source: None,
                        })
                    })?;
                }
                Err(e) => {
                    self.state = ConnectionState::Error;
                    return Err(Error::Protocol(ProtocolError {
                        message: format!("Protocol error: {}", e),
                        raw_data: None,
                        source: None,
                    }));
                }
            }
        }
    }
}

impl Drop for PgConnection {
    fn drop(&mut self) {
        // Try to close gracefully, ignore errors
        let _ = self.close();
    }
}

// ==================== Console Support ====================

#[cfg(feature = "console")]
impl ConsoleAware for PgConnection {
    fn set_console(&mut self, console: Option<Arc<SqlModelConsole>>) {
        self.console = console;
    }

    fn console(&self) -> Option<&Arc<SqlModelConsole>> {
        self.console.as_ref()
    }

    fn has_console(&self) -> bool {
        self.console.is_some()
    }
}

/// Connection progress stage for console output.
#[cfg(feature = "console")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionStage {
    /// Resolving DNS
    DnsResolve,
    /// Establishing TCP connection
    TcpConnect,
    /// Negotiating SSL/TLS
    SslNegotiate,
    /// SSL/TLS established
    SslEstablished,
    /// Sending startup message
    Startup,
    /// Authenticating
    Authenticating,
    /// Authentication complete
    Authenticated,
    /// Ready for queries
    Ready,
}

#[cfg(feature = "console")]
impl ConnectionStage {
    /// Get a human-readable description of the stage.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::DnsResolve => "Resolving DNS",
            Self::TcpConnect => "Connecting (TCP)",
            Self::SslNegotiate => "Negotiating SSL",
            Self::SslEstablished => "SSL established",
            Self::Startup => "Sending startup",
            Self::Authenticating => "Authenticating",
            Self::Authenticated => "Authenticated",
            Self::Ready => "Ready",
        }
    }
}

#[cfg(feature = "console")]
impl PgConnection {
    /// Emit a connection progress message to the console.
    ///
    /// This is a no-op if no console is attached.
    pub fn emit_progress(&self, stage: ConnectionStage, success: bool) {
        if let Some(console) = &self.console {
            let status = if success { "[OK]" } else { "[..] " };
            let message = format!("{} {}", status, stage.description());
            console.info(&message);
        }
    }

    /// Emit a connection success message with server info.
    pub fn emit_connected(&self) {
        if let Some(console) = &self.console {
            let server_version = self
                .parameters
                .get("server_version")
                .map_or("unknown", |s| s.as_str());
            let message = format!(
                "Connected to PostgreSQL {} at {}:{}",
                server_version, self.config.host, self.config.port
            );
            console.success(&message);
        }
    }

    /// Emit a plain-text connection summary (for agent mode).
    pub fn emit_connected_plain(&self) -> String {
        let server_version = self
            .parameters
            .get("server_version")
            .map_or("unknown", |s| s.as_str());
        format!(
            "Connected to PostgreSQL {} at {}:{}",
            server_version, self.config.host, self.config.port
        )
    }
}

// ==================== Helper Functions ====================

/// Compute MD5 password hash as per PostgreSQL protocol.
fn md5_password(user: &str, password: &str, salt: [u8; 4]) -> String {
    use std::fmt::Write;

    // md5(md5(password + user) + salt)
    let inner = format!("{}{}", password, user);
    let inner_hash = md5::compute(inner.as_bytes());

    let mut outer_input = format!("{:x}", inner_hash).into_bytes();
    outer_input.extend_from_slice(&salt);
    let outer_hash = md5::compute(&outer_input);

    let mut result = String::with_capacity(35);
    result.push_str("md5");
    write!(&mut result, "{:x}", outer_hash).unwrap();
    result
}

fn auth_error(msg: impl Into<String>) -> Error {
    Error::Connection(ConnectionError {
        kind: ConnectionErrorKind::Authentication,
        message: msg.into(),
        source: None,
    })
}

fn error_from_fields(fields: &ErrorFields) -> Error {
    // Determine error kind from SQLSTATE
    let kind = match fields.code.get(..2) {
        Some("08") => {
            // Connection exception
            return Error::Connection(ConnectionError {
                kind: ConnectionErrorKind::Connect,
                message: fields.message.clone(),
                source: None,
            });
        }
        Some("28") => {
            // Invalid authorization specification
            return Error::Connection(ConnectionError {
                kind: ConnectionErrorKind::Authentication,
                message: fields.message.clone(),
                source: None,
            });
        }
        Some("42") => QueryErrorKind::Syntax, // Syntax error or access rule violation
        Some("23") => QueryErrorKind::Constraint, // Integrity constraint violation
        Some("40") => {
            if fields.code == "40001" {
                QueryErrorKind::Serialization
            } else {
                QueryErrorKind::Deadlock
            }
        }
        Some("57") => {
            if fields.code == "57014" {
                QueryErrorKind::Cancelled
            } else {
                QueryErrorKind::Timeout
            }
        }
        _ => QueryErrorKind::Database,
    };

    Error::Query(QueryError {
        kind,
        sql: None,
        sqlstate: Some(fields.code.clone()),
        message: fields.message.clone(),
        detail: fields.detail.clone(),
        hint: fields.hint.clone(),
        position: fields.position.map(|p| p as usize),
        source: None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_md5_password() {
        // Example from PostgreSQL documentation
        let hash = md5_password("postgres", "mysecretpassword", *b"abcd");
        assert!(hash.starts_with("md5"));
        assert_eq!(hash.len(), 35); // "md5" + 32 hex chars
    }

    #[test]
    fn test_transaction_status_conversion() {
        assert_eq!(
            TransactionStatusState::from(TransactionStatus::Idle),
            TransactionStatusState::Idle
        );
        assert_eq!(
            TransactionStatusState::from(TransactionStatus::Transaction),
            TransactionStatusState::InTransaction
        );
        assert_eq!(
            TransactionStatusState::from(TransactionStatus::Error),
            TransactionStatusState::InFailed
        );
    }

    #[test]
    fn test_error_classification() {
        let fields = ErrorFields {
            severity: "ERROR".to_string(),
            code: "23505".to_string(),
            message: "unique violation".to_string(),
            ..Default::default()
        };
        let err = error_from_fields(&fields);
        assert!(matches!(err, Error::Query(q) if q.kind == QueryErrorKind::Constraint));

        let fields = ErrorFields {
            severity: "FATAL".to_string(),
            code: "28P01".to_string(),
            message: "password authentication failed".to_string(),
            ..Default::default()
        };
        let err = error_from_fields(&fields);
        assert!(matches!(
            err,
            Error::Connection(c) if c.kind == ConnectionErrorKind::Authentication
        ));
    }
}