vibesql-server 0.1.3

Network server with PostgreSQL wire protocol for VibeSQL
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
//! Common test utilities for vibesql-server integration tests

use bytes::{BufMut, BytesMut};
use opentelemetry::global;
use std::net::SocketAddr;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{broadcast, oneshot};

use vibesql_server::auth::PasswordStore;
use vibesql_server::config::{
    AuthConfig, Config, HttpAuthConfig, HttpConfig, LoggingConfig, ServerConfig,
};
use vibesql_server::connection::{ConnectionHandler, TableMutationNotification};
use vibesql_server::http::create_http_router;
use vibesql_server::observability::{ObservabilityConfig, ObservabilityProvider, ServerMetrics};
use vibesql_server::registry::DatabaseRegistry;
use vibesql_server::subscription::SubscriptionConfig;
use vibesql_server::SubscriptionManager;
use vibesql_storage::Database;

/// Test server handle - holds the shutdown channel and address
pub struct TestServer {
    pub addr: SocketAddr,
    pub http_addr: Option<SocketAddr>,
    shutdown_tx: Option<oneshot::Sender<()>>,
}

impl TestServer {
    /// Get the server address (TCP wire protocol)
    pub fn addr(&self) -> SocketAddr {
        self.addr
    }

    /// Get the HTTP server address (if HTTP is enabled)
    pub fn http_addr(&self) -> Option<SocketAddr> {
        self.http_addr
    }

    /// Shutdown the server
    pub fn shutdown(mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(());
        }
    }
}

/// Start a test server with trust authentication on a random port
#[allow(dead_code)]
pub async fn start_test_server() -> TestServer {
    start_test_server_with_config(test_config()).await
}

/// Start a test server with the given configuration
pub async fn start_test_server_with_config(mut config: Config) -> TestServer {
    // Bind to a random available port for TCP wire protocol
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("Failed to bind");
    let addr = listener.local_addr().expect("Failed to get local address");

    // Update config with actual port
    config.server.port = addr.port();
    config.server.host = "127.0.0.1".to_string();

    // Bind HTTP server to a separate random port if enabled
    let http_addr = if config.http.enabled {
        let http_listener =
            TcpListener::bind("127.0.0.1:0").await.expect("Failed to bind HTTP server");
        let http_addr = http_listener.local_addr().expect("Failed to get HTTP local address");
        config.http.port = http_addr.port();
        config.http.host = "127.0.0.1".to_string();
        Some((http_addr, http_listener))
    } else {
        None
    };

    let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

    let config = Arc::new(config);

    // Initialize observability (disabled for tests)
    let observability = Arc::new(
        ObservabilityProvider::init(&config.observability).expect("Failed to init observability"),
    );

    // Load password store if configured
    let password_store = config.auth.password_file.as_ref().map(|password_file| {
        Arc::new(
            PasswordStore::load_from_file(password_file).expect("Failed to load password file"),
        )
    });

    // Track active connections
    let active_connections = Arc::new(AtomicUsize::new(0));

    // Create shared subscription manager for tests
    let subscription_manager = Arc::new(SubscriptionManager::new());

    // Create shared database registry for tests
    let database_registry = DatabaseRegistry::new();

    // Create broadcast channel for cross-connection subscription notifications
    let (mutation_broadcast_tx, _mutation_broadcast_rx) =
        broadcast::channel::<TableMutationNotification>(1024);

    // Start HTTP server if enabled
    let http_server_addr = if let Some((http_socket_addr, http_listener)) = http_addr {
        // Create a shared database for HTTP API (like main.rs does)
        let mut db = Database::new();
        let change_rx = db.enable_change_events(1024);
        let db = Arc::new(db);

        // Clone subscription manager for HTTP
        let subscription_manager_for_http = Arc::clone(&subscription_manager);
        let registry_for_http = database_registry.clone();

        // Spawn the subscription manager event loop
        let db_for_subscription_task = Arc::clone(&db);
        let subscription_manager_for_loop = Arc::clone(&subscription_manager);
        tokio::spawn(async move {
            subscription_manager_for_loop.run_event_loop(change_rx, db_for_subscription_task).await;
        });

        // Spawn HTTP server
        let db_for_http = Arc::clone(&db);
        let metrics_for_http = observability.metrics().cloned();
        tokio::spawn(async move {
            let app = create_http_router(db_for_http, registry_for_http, subscription_manager_for_http, metrics_for_http);
            axum::serve(http_listener, app).await.expect("HTTP server error");
        });

        Some(http_socket_addr)
    } else {
        None
    };

    // Spawn TCP wire protocol server task
    tokio::spawn(async move {
        loop {
            tokio::select! {
                result = listener.accept() => {
                    match result {
                        Ok((stream, peer_addr)) => {
                            let config = Arc::clone(&config);
                            let observability = Arc::clone(&observability);
                            let password_store = password_store.clone();
                            let active_connections = Arc::clone(&active_connections);
                            let database_registry = database_registry.clone();
                            let subscription_manager = Arc::clone(&subscription_manager);
                            let mutation_broadcast_tx = mutation_broadcast_tx.clone();

                            tokio::spawn(async move {
                                let mut handler = ConnectionHandler::new(
                                    stream,
                                    peer_addr,
                                    config,
                                    observability,
                                    password_store,
                                    active_connections,
                                    database_registry,
                                    subscription_manager,
                                    mutation_broadcast_tx,
                                );
                                let _ = handler.handle().await;
                            });
                        }
                        Err(_) => break,
                    }
                }
                _ = &mut shutdown_rx => {
                    break;
                }
            }
        }
    });

    TestServer { addr, http_addr: http_server_addr, shutdown_tx: Some(shutdown_tx) }
}

/// Create a test configuration with trust authentication
pub fn test_config() -> Config {
    Config {
        server: ServerConfig {
            host: "127.0.0.1".to_string(),
            port: 0, // Will be assigned
            max_connections: 10,
            ssl_enabled: false,
            ssl_cert: None,
            ssl_key: None,
        },
        auth: AuthConfig { method: "trust".to_string(), password_file: None },
        logging: LoggingConfig {
            level: "error".to_string(), // Quiet for tests
            file: None,
        },
        http: HttpConfig {
            enabled: false, // Disable HTTP for tests
            host: "127.0.0.1".to_string(),
            port: 0,
            auth: HttpAuthConfig::default(),
        },
        observability: ObservabilityConfig::default(),
        subscriptions: SubscriptionConfig::default(),
    }
}

/// Create a test configuration with password authentication
#[allow(dead_code)]
pub fn test_config_with_password(password_file: std::path::PathBuf) -> Config {
    let mut config = test_config();
    config.auth.method = "password".to_string();
    config.auth.password_file = Some(password_file);
    config
}

/// Test server with metrics handle - holds the test server and shared metrics for verification
#[allow(dead_code)]
pub struct TestServerWithMetrics {
    pub server: TestServer,
    pub metrics: ServerMetrics,
}

impl TestServerWithMetrics {
    /// Get the server address (TCP wire protocol)
    #[allow(dead_code)]
    pub fn addr(&self) -> SocketAddr {
        self.server.addr()
    }

    /// Get the HTTP server address (if HTTP is enabled)
    #[allow(dead_code)]
    pub fn http_addr(&self) -> Option<SocketAddr> {
        self.server.http_addr()
    }

    /// Shutdown the server
    #[allow(dead_code)]
    pub fn shutdown(self) {
        self.server.shutdown();
    }
}

/// Start a test server with metrics enabled for verification
///
/// This returns both the test server and a shared `ServerMetrics` instance
/// that can be used to verify metric values after running test scenarios.
#[allow(dead_code)]
pub async fn start_test_server_with_metrics(mut config: Config) -> TestServerWithMetrics {
    // Create metrics using the global meter
    let meter = global::meter("vibesql_test_metrics");
    let metrics = ServerMetrics::new(&meter);

    // Bind to a random available port for TCP wire protocol
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("Failed to bind");
    let addr = listener.local_addr().expect("Failed to get local address");

    // Update config with actual port
    config.server.port = addr.port();
    config.server.host = "127.0.0.1".to_string();

    // Bind HTTP server to a separate random port if enabled
    let http_addr = if config.http.enabled {
        let http_listener =
            TcpListener::bind("127.0.0.1:0").await.expect("Failed to bind HTTP server");
        let http_addr = http_listener.local_addr().expect("Failed to get HTTP local address");
        config.http.port = http_addr.port();
        config.http.host = "127.0.0.1".to_string();
        Some((http_addr, http_listener))
    } else {
        None
    };

    let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();

    let config = Arc::new(config);

    // Initialize observability (disabled for tests, we use custom metrics)
    let observability = Arc::new(
        ObservabilityProvider::init(&config.observability).expect("Failed to init observability"),
    );

    // Load password store if configured
    let password_store = config.auth.password_file.as_ref().map(|password_file| {
        Arc::new(
            PasswordStore::load_from_file(password_file).expect("Failed to load password file"),
        )
    });

    // Track active connections
    let active_connections = Arc::new(AtomicUsize::new(0));

    // Create shared subscription manager for tests
    let subscription_manager = Arc::new(SubscriptionManager::new());

    // Create shared database registry for tests
    let database_registry = DatabaseRegistry::new();

    // Create broadcast channel for cross-connection subscription notifications
    let (mutation_broadcast_tx, _mutation_broadcast_rx) =
        broadcast::channel::<TableMutationNotification>(1024);

    // Start HTTP server if enabled
    let http_server_addr = if let Some((http_socket_addr, http_listener)) = http_addr {
        // Create a shared database for HTTP API (like main.rs does)
        let mut db = Database::new();
        let change_rx = db.enable_change_events(1024);
        let db = Arc::new(db);

        // Clone subscription manager for HTTP
        let subscription_manager_for_http = Arc::clone(&subscription_manager);
        let registry_for_http = database_registry.clone();

        // Spawn the subscription manager event loop
        let db_for_subscription_task = Arc::clone(&db);
        let subscription_manager_for_loop = Arc::clone(&subscription_manager);
        tokio::spawn(async move {
            subscription_manager_for_loop.run_event_loop(change_rx, db_for_subscription_task).await;
        });

        // Spawn HTTP server with our test metrics
        let db_for_http = Arc::clone(&db);
        let metrics_for_http = Some(metrics.clone());
        tokio::spawn(async move {
            let app = create_http_router(db_for_http, registry_for_http, subscription_manager_for_http, metrics_for_http);
            axum::serve(http_listener, app).await.expect("HTTP server error");
        });

        Some(http_socket_addr)
    } else {
        None
    };

    // Spawn TCP wire protocol server task
    tokio::spawn(async move {
        loop {
            tokio::select! {
                result = listener.accept() => {
                    match result {
                        Ok((stream, peer_addr)) => {
                            let config = Arc::clone(&config);
                            let observability = Arc::clone(&observability);
                            let password_store = password_store.clone();
                            let active_connections = Arc::clone(&active_connections);
                            let database_registry = database_registry.clone();
                            let subscription_manager = Arc::clone(&subscription_manager);
                            let mutation_broadcast_tx = mutation_broadcast_tx.clone();

                            tokio::spawn(async move {
                                let mut handler = ConnectionHandler::new(
                                    stream,
                                    peer_addr,
                                    config,
                                    observability,
                                    password_store,
                                    active_connections,
                                    database_registry,
                                    subscription_manager,
                                    mutation_broadcast_tx,
                                );
                                let _ = handler.handle().await;
                            });
                        }
                        Err(_) => break,
                    }
                }
                _ = &mut shutdown_rx => {
                    break;
                }
            }
        }
    });

    let server = TestServer { addr, http_addr: http_server_addr, shutdown_tx: Some(shutdown_tx) };
    TestServerWithMetrics { server, metrics }
}

/// Test client for connecting to the server
pub struct TestClient {
    stream: TcpStream,
    read_buf: BytesMut,
    write_buf: BytesMut,
}

impl TestClient {
    /// Connect to a test server
    pub async fn connect(addr: SocketAddr) -> std::io::Result<Self> {
        let stream = TcpStream::connect(addr).await?;
        Ok(Self {
            stream,
            read_buf: BytesMut::with_capacity(8192),
            write_buf: BytesMut::with_capacity(8192),
        })
    }

    /// Send startup message
    pub async fn send_startup(&mut self, user: &str, database: &str) -> std::io::Result<()> {
        self.write_buf.clear();

        // Build startup message
        let mut params = BytesMut::new();
        put_cstring(&mut params, "user");
        put_cstring(&mut params, user);
        put_cstring(&mut params, "database");
        put_cstring(&mut params, database);
        params.put_u8(0); // End of parameters

        // Protocol version (3.0) = 196608
        let protocol_version: i32 = 196608;

        // Total length: 4 (length) + 4 (version) + params
        let len = 4 + 4 + params.len();

        self.write_buf.put_i32(len as i32);
        self.write_buf.put_i32(protocol_version);
        self.write_buf.extend_from_slice(&params);

        self.stream.write_all(&self.write_buf).await?;
        self.stream.flush().await?;
        Ok(())
    }

    /// Send SSL request
    #[allow(dead_code)]
    pub async fn send_ssl_request(&mut self) -> std::io::Result<()> {
        self.write_buf.clear();

        // SSL request message: length (8) + SSL magic number
        self.write_buf.put_i32(8);
        self.write_buf.put_i32(80877103); // SSL request code

        self.stream.write_all(&self.write_buf).await?;
        self.stream.flush().await?;
        Ok(())
    }

    /// Send a password message
    #[allow(dead_code)]
    pub async fn send_password(&mut self, password: &str) -> std::io::Result<()> {
        self.write_buf.clear();

        let password_bytes = password.as_bytes();
        let len = 4 + password_bytes.len() + 1; // length + password + null

        self.write_buf.put_u8(b'p'); // Password message
        self.write_buf.put_i32(len as i32);
        self.write_buf.extend_from_slice(password_bytes);
        self.write_buf.put_u8(0); // Null terminator

        self.stream.write_all(&self.write_buf).await?;
        self.stream.flush().await?;
        Ok(())
    }

    /// Send a query message
    pub async fn send_query(&mut self, query: &str) -> std::io::Result<()> {
        self.write_buf.clear();

        let query_bytes = query.as_bytes();
        let len = 4 + query_bytes.len() + 1; // length + query + null

        self.write_buf.put_u8(b'Q'); // Query message
        self.write_buf.put_i32(len as i32);
        self.write_buf.extend_from_slice(query_bytes);
        self.write_buf.put_u8(0); // Null terminator

        self.stream.write_all(&self.write_buf).await?;
        self.stream.flush().await?;
        Ok(())
    }

    /// Send a terminate message
    #[allow(dead_code)]
    pub async fn send_terminate(&mut self) -> std::io::Result<()> {
        self.write_buf.clear();

        self.write_buf.put_u8(b'X'); // Terminate
        self.write_buf.put_i32(4); // Length

        self.stream.write_all(&self.write_buf).await?;
        self.stream.flush().await?;
        Ok(())
    }

    /// Read bytes from the server and return the data
    #[allow(dead_code)]
    pub async fn read_response(&mut self) -> std::io::Result<Vec<u8>> {
        self.read_buf.clear();
        let n = self.stream.read_buf(&mut self.read_buf).await?;
        if n == 0 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Connection closed",
            ));
        }
        Ok(self.read_buf.to_vec())
    }

    /// Read until we get a specific message type (with 5 second timeout)
    pub async fn read_until_message_type(&mut self, msg_type: u8) -> std::io::Result<Vec<u8>> {
        self.read_until_message_type_timeout(msg_type, std::time::Duration::from_secs(5)).await
    }

    /// Read until we get a specific message type with custom timeout
    pub async fn read_until_message_type_timeout(
        &mut self,
        msg_type: u8,
        timeout: std::time::Duration,
    ) -> std::io::Result<Vec<u8>> {
        let mut all_data = Vec::new();
        let deadline = tokio::time::Instant::now() + timeout;

        loop {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            if remaining.is_zero() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!("Timeout waiting for message type 0x{:02X}", msg_type),
                ));
            }

            match tokio::time::timeout(remaining, self.stream.read_buf(&mut self.read_buf)).await {
                Ok(Ok(0)) => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::UnexpectedEof,
                        "Connection closed",
                    ));
                }
                Ok(Ok(_n)) => {
                    all_data.extend_from_slice(&self.read_buf);
                    self.read_buf.clear();

                    // Check if we have the target message
                    if contains_message_type(&all_data, msg_type) {
                        return Ok(all_data);
                    }
                }
                Ok(Err(e)) => return Err(e),
                Err(_) => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::TimedOut,
                        format!("Timeout waiting for message type 0x{:02X}", msg_type),
                    ));
                }
            }
        }
    }

    /// Read a single byte (for SSL response)
    #[allow(dead_code)]
    pub async fn read_byte(&mut self) -> std::io::Result<u8> {
        let mut buf = [0u8; 1];
        self.stream.read_exact(&mut buf).await?;
        Ok(buf[0])
    }

    /// Get raw access to the read buffer
    #[allow(dead_code)]
    pub fn read_buffer(&self) -> &BytesMut {
        &self.read_buf
    }

    /// Check if connection is still open by attempting a zero-byte read
    #[allow(dead_code)]
    pub async fn is_connected(&mut self) -> bool {
        let mut buf = [0u8; 1];
        match self.stream.try_read(&mut buf) {
            Ok(0) => false,                                                   // Connection closed
            Ok(_) => true, // Data available (shouldn't happen in this context)
            Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => true, // Connection open
            Err(_) => false, // Other error, assume closed
        }
    }

    /// Write all bytes to the stream directly
    #[allow(dead_code)]
    pub async fn stream_write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
        self.stream.write_all(buf).await
    }

    /// Flush the stream
    #[allow(dead_code)]
    pub async fn stream_flush(&mut self) -> std::io::Result<()> {
        self.stream.flush().await
    }
}

/// Helper to put a null-terminated C string
fn put_cstring(buf: &mut BytesMut, s: &str) {
    buf.extend_from_slice(s.as_bytes());
    buf.put_u8(0);
}

/// Check if data contains a message of a specific type
fn contains_message_type(data: &[u8], msg_type: u8) -> bool {
    let mut pos = 0;
    while pos < data.len() {
        if data[pos] == msg_type {
            return true;
        }
        // Skip to next message
        if pos + 5 <= data.len() {
            let len =
                i32::from_be_bytes([data[pos + 1], data[pos + 2], data[pos + 3], data[pos + 4]])
                    as usize;
            pos += 1 + len;
        } else {
            break;
        }
    }
    false
}

/// Parse backend messages from raw bytes
#[allow(dead_code)]
pub fn parse_backend_messages(data: &[u8]) -> Vec<ParsedMessage> {
    let mut messages = Vec::new();
    let mut pos = 0;

    while pos < data.len() {
        if pos + 5 > data.len() {
            break;
        }

        let msg_type = data[pos];
        let len = i32::from_be_bytes([data[pos + 1], data[pos + 2], data[pos + 3], data[pos + 4]])
            as usize;

        if pos + 1 + len > data.len() {
            break;
        }

        let payload = &data[pos + 5..pos + 1 + len];
        messages.push(ParsedMessage { msg_type, payload: payload.to_vec() });

        pos += 1 + len;
    }

    messages
}

/// A parsed backend message
#[derive(Debug, Clone)]
pub struct ParsedMessage {
    pub msg_type: u8,
    pub payload: Vec<u8>,
}

#[allow(dead_code)]
impl ParsedMessage {
    /// Check if this is an AuthenticationOk message
    pub fn is_auth_ok(&self) -> bool {
        self.msg_type == b'R'
            && self.payload.len() >= 4
            && i32::from_be_bytes([
                self.payload[0],
                self.payload[1],
                self.payload[2],
                self.payload[3],
            ]) == 0
    }

    /// Check if this is a ReadyForQuery message
    pub fn is_ready_for_query(&self) -> bool {
        self.msg_type == b'Z'
    }

    /// Check if this is an ErrorResponse message
    pub fn is_error(&self) -> bool {
        self.msg_type == b'E'
    }

    /// Check if this is a RowDescription message
    pub fn is_row_description(&self) -> bool {
        self.msg_type == b'T'
    }

    /// Check if this is a DataRow message
    pub fn is_data_row(&self) -> bool {
        self.msg_type == b'D'
    }

    /// Check if this is a CommandComplete message
    pub fn is_command_complete(&self) -> bool {
        self.msg_type == b'C'
    }

    /// Check if this is a ParameterStatus message
    pub fn is_parameter_status(&self) -> bool {
        self.msg_type == b'S'
    }

    /// Check if this is a BackendKeyData message
    pub fn is_backend_key_data(&self) -> bool {
        self.msg_type == b'K'
    }

    /// Check if this is an EmptyQueryResponse message
    pub fn is_empty_query_response(&self) -> bool {
        self.msg_type == b'I'
    }

    /// Check if this is an AuthenticationCleartextPassword request
    pub fn is_cleartext_password_request(&self) -> bool {
        self.msg_type == b'R'
            && self.payload.len() >= 4
            && i32::from_be_bytes([
                self.payload[0],
                self.payload[1],
                self.payload[2],
                self.payload[3],
            ]) == 3
    }

    /// Check if this is an AuthenticationMD5Password request
    pub fn is_md5_password_request(&self) -> bool {
        self.msg_type == b'R'
            && self.payload.len() >= 4
            && i32::from_be_bytes([
                self.payload[0],
                self.payload[1],
                self.payload[2],
                self.payload[3],
            ]) == 5
    }

    /// Get MD5 salt if this is an MD5 password request
    pub fn get_md5_salt(&self) -> Option<[u8; 4]> {
        if self.is_md5_password_request() && self.payload.len() >= 8 {
            let mut salt = [0u8; 4];
            salt.copy_from_slice(&self.payload[4..8]);
            Some(salt)
        } else {
            None
        }
    }

    /// Get command tag if this is a CommandComplete message
    pub fn get_command_tag(&self) -> Option<String> {
        if self.is_command_complete() {
            // Find null terminator
            let null_pos = self.payload.iter().position(|&b| b == 0)?;
            String::from_utf8(self.payload[..null_pos].to_vec()).ok()
        } else {
            None
        }
    }
}