netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! PostgreSQL protocol actions implementation

use crate::llm::actions::{
    protocol_trait::{ActionResult, Protocol, Server},
    ActionDefinition, Parameter,
};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::server::connection::ConnectionId;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::{Arc, LazyLock};
use tokio::sync::mpsc;
use tracing::debug;

/// PostgreSQL protocol action handler
pub struct PostgresqlProtocol {
    #[allow(dead_code)]
    connection_id: ConnectionId,
    #[allow(dead_code)]
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
}

impl PostgresqlProtocol {
    pub fn new(
        connection_id: ConnectionId,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Self {
        Self {
            connection_id,
            app_state,
            status_tx,
        }
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for PostgresqlProtocol {
    fn get_startup_parameters(&self) -> Vec<crate::llm::actions::ParameterDefinition> {
        vec![
                crate::llm::actions::ParameterDefinition {
                    name: "send_first".to_string(),
                    type_hint: "boolean".to_string(),
                    description: "Unsupported by this server and refused if set to true. PostgreSQL: the frontend sends the StartupMessage first, and a byte before it is a protocol violation the client cannot parse."
                        .to_string(),
                    required: false,
                    example: json!(false),
                    default: None,
                },
            ]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        // No user-triggered actions. (A `list_postgresql_connections` action used to be declared
        // here; its executor returned a hardcoded empty list, so it only ever misled the model.)
        vec![]
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            postgresql_query_response_action(),
            postgresql_error_response_action(),
            postgresql_ok_response_action(),
            close_this_connection_action(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "PostgreSQL"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        get_postgresql_event_types()
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>PostgreSQL"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["postgres", "psql"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            // Beta: exercised against two independent clients that share no code with each
            // other or with pgwire — tokio-postgres (Rust) and psql/libpq (C). Not Stable:
            // Stable additionally wants spec compliance and scripting support reviewed, which
            // has not been done here.
            .state(DevelopmentState::Beta)
            .well_known_port(5432)
            .implementation("pgwire v0.35 protocol library")
            .llm_control("Query responses (columns, rows, types)")
            .e2e_testing(
                "Two independent clients. tokio-postgres (tests/server/postgresql/test.rs, \
                 extended_query_test.rs, llm_failure_test.rs): simple query, and \
                 Parse/Bind/Describe/Execute with binary result columns decoded into real Rust \
                 types. psql 14 / libpq (real_client_test.rs, hard-fails when the binary is \
                 absent): the sslmode=prefer SSLRequest negotiation tokio-postgres+NoTls never \
                 sends, the startup handshake, a four-column three-row SELECT asserted on the \
                 exact text psql rendered (t/f booleans, a real NULL distinguished from an \
                 empty string), a CommandComplete tag, and an ErrorResponse whose SQLSTATE psql \
                 prints under VERBOSITY verbose. \
                 NOT proven: psql 14 sends every statement as a simple Query (no \\bind), so the \
                 extended protocol and binary formats rest on tokio-postgres alone; no client \
                 here exercises authentication or TLS (neither is implemented), bound parameter \
                 values, COPY, LISTEN/NOTIFY, cursors, or multi-statement simple queries.",
            )
            .notes(
                "No authentication, no TLS; simple and extended query protocols, text format only",
            )
            .build()
    }
    fn description(&self) -> &'static str {
        "PostgreSQL database server"
    }
    fn example_prompt(&self) -> &'static str {
        "Start a PostgreSQL server on port 5432"
    }
    fn group_name(&self) -> &'static str {
        "Database"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        use serde_json::json;

        // Deterministic answers for a version probe and generic SELECTs, with an
        // empty command-complete tag for writes. Real column/row shapes.
        let script = r#"import json, sys
data = json.load(sys.stdin)
event = data["event"]
if data["event_type_id"] == "postgresql_query":
    q = event.get("query", "").strip().rstrip(";").upper()
    if q.startswith("SELECT VERSION()"):
        actions = [{"type": "postgresql_query_response",
                    "columns": [{"name": "version", "type": "text"}],
                    "rows": [["PostgreSQL 16.2"]]}]
    elif q.startswith("SELECT"):
        actions = [{"type": "postgresql_query_response",
                    "columns": [{"name": "id", "type": "int4"},
                                {"name": "name", "type": "text"}],
                    "rows": [[1, "alice"], [2, "bob"]]}]
    else:
        actions = [{"type": "postgresql_ok_response", "tag": "OK"}]
else:
    actions = []
print(json.dumps({"actions": actions}))"#;

        StartupExamples::new(
            // LLM mode: reason about the SQL to synthesise coherent rows.
            json!({
                "type": "open_server",
                "port": 5432,
                "base_stack": "postgresql",
                "instruction": "Act as a PostgreSQL server for a 'users' database. Answer SELECT queries by generating plausible rows that satisfy the WHERE clause and requested columns; treat INSERT/UPDATE/DELETE as succeeding with a suitable command tag. Keep results consistent across queries in the same session."
            }),
            // Script mode: fixed answers for known queries, no LLM call.
            json!({
                "type": "open_server",
                "port": 5432,
                "base_stack": "postgresql",
                "event_handlers": [{
                    "event_pattern": "postgresql_query",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": script
                    }
                }]
            }),
            // Static mode: Fixed responses
            json!({
                "type": "open_server",
                "port": 5432,
                "base_stack": "postgresql",
                "event_handlers": [{
                    "event_pattern": "postgresql_query",
                    "handler": {
                        "type": "static",
                        "actions": [{
                            "type": "postgresql_query_response",
                            "columns": [{"name": "result", "type": "text"}],
                            "rows": [["OK"]]
                        }]
                    }
                }]
            }),
        )
    }
}

// Implement Server trait (server-specific functionality)
impl Server for PostgresqlProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            use crate::server::postgresql::PostgresqlServer;
            // `send_first` stays declared so a caller that passes `false` still validates - an
            // undeclared key is refused outright - but `true` is refused rather than ignored.
            // It was read here, threaded through spawn, and dropped at the far end as
            // `_send_first`: a knob advertised to the model, plumbed through three hops, and
            // silently doing nothing. PostgreSQL: the frontend sends the StartupMessage first, and a byte before it is a protocol violation the client cannot parse.
            let send_first = ctx
                .startup_params
                .as_ref()
                .map(|p| p.get_optional_bool("send_first"))
                .transpose()?
                .flatten()
                .unwrap_or(false);
            if send_first {
                return Err(anyhow::anyhow!(
                    "send_first is not supported by the PostgreSQL server: the frontend sends the StartupMessage first, and a byte before it is a protocol violation the client cannot parse"
                ));
            }

            PostgresqlServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                send_first,
                ctx.server_id,
            )
            .await
        })
    }
    fn execute_action(&self, action: serde_json::Value) -> Result<ActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .context("Missing 'type' field in action")?;

        match action_type {
            "postgresql_query_response" => self.execute_postgresql_query_response(action),
            "postgresql_error_response" => self.execute_postgresql_error_response(action),
            "postgresql_ok_response" => self.execute_postgresql_ok_response(action),
            "close_this_connection" => Ok(ActionResult::CloseConnection),
            _ => Err(anyhow::anyhow!(
                "Unknown PostgreSQL action: {}",
                action_type
            )),
        }
    }
}

impl PostgresqlProtocol {
    fn execute_postgresql_query_response(&self, action: serde_json::Value) -> Result<ActionResult> {
        // Extract columns and rows from the action
        let columns = action
            .get("columns")
            .and_then(|v| v.as_array())
            .context("Missing 'columns' array")?;

        let rows = action
            .get("rows")
            .and_then(|v| v.as_array())
            .context("Missing 'rows' array")?;

        debug!(
            "PostgreSQL query response: {} columns, {} rows",
            columns.len(),
            rows.len()
        );

        let _ = self.status_tx.send(format!(
            "[DEBUG] PostgreSQL → Result set: {} columns, {} rows",
            columns.len(),
            rows.len()
        ));

        Ok(ActionResult::Custom {
            name: "postgresql_query_response".to_string(),
            data: json!({
                "columns": columns,
                "rows": rows
            }),
        })
    }

    fn execute_postgresql_error_response(&self, action: serde_json::Value) -> Result<ActionResult> {
        let severity = action
            .get("severity")
            .and_then(|v| v.as_str())
            .unwrap_or("ERROR");

        let code = action
            .get("code")
            .and_then(|v| v.as_str())
            .unwrap_or("XX000");

        let message = action
            .get("message")
            .and_then(|v| v.as_str())
            .unwrap_or("Unknown error");

        debug!(
            "PostgreSQL error response: {} {} - {}",
            severity, code, message
        );

        let _ = self.status_tx.send(format!(
            "[DEBUG] PostgreSQL ✗ {} {}: {}",
            severity, code, message
        ));

        Ok(ActionResult::Custom {
            name: "postgresql_error".to_string(),
            data: json!({
                "severity": severity,
                "code": code,
                "message": message
            }),
        })
    }

    fn execute_postgresql_ok_response(&self, action: serde_json::Value) -> Result<ActionResult> {
        let tag = action.get("tag").and_then(|v| v.as_str()).unwrap_or("OK");

        debug!("PostgreSQL OK response: {}", tag);

        let _ = self
            .status_tx
            .send(format!("[DEBUG] PostgreSQL → OK: {}", tag));

        Ok(ActionResult::Custom {
            name: "postgresql_ok".to_string(),
            data: json!({
                "tag": tag
            }),
        })
    }
}

/// Action definition: Send PostgreSQL query response
pub fn postgresql_query_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "postgresql_query_response".to_string(),
        description: "The answer to any statement that returns rows - SELECT (including \
                      SELECT 1, SELECT current_user, SELECT version()), SHOW, VALUES, WITH. \
                      A single value is one column and one row. Never answer a SELECT with \
                      postgresql_ok_response: a command tag carries no rows, so the client \
                      prints the tag and never sees the value. One response per query."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "columns".to_string(),
                type_hint: "array".to_string(),
                description: "Array of column definitions. Each column needs 'name' and 'type'. \
                              Recognised types: int2/smallint, int4/int/integer, int8/bigint, \
                              float4/real, float8/double, bool/boolean, date, time, timestamp, \
                              text, varchar (anything else is sent as varchar). Rows shorter than \
                              the column list are padded with NULLs; extra values are dropped"
                    .to_string(),
                required: true,
            },
            Parameter {
                name: "rows".to_string(),
                type_hint: "array".to_string(),
                description:
                    "Array of rows. Each row is an array of values matching the column order"
                        .to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "postgresql_query_response",
            "columns": [{"name": "id", "type": "int4"}, {"name": "name", "type": "text"}],
            "rows": [[1, "Alice"], [2, "Bob"]]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL {columns_len} cols, {rows_len} rows")
                .with_debug("PostgreSQL query_response: {columns_len} columns, {rows_len} rows"),
        ),
    }
}

/// Action definition: Send PostgreSQL error response
pub fn postgresql_error_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "postgresql_error_response".to_string(),
        description: "Send an error response to the client".to_string(),
        parameters: vec![
            Parameter {
                name: "severity".to_string(),
                type_hint: "string".to_string(),
                description: "Error severity (ERROR, FATAL, PANIC, WARNING, NOTICE, DEBUG, INFO, LOG)".to_string(),
                required: false,
            },
            Parameter {
                name: "code".to_string(),
                type_hint: "string".to_string(),
                description: "PostgreSQL error code (e.g. '42P01' for undefined_table, '42601' for syntax_error)".to_string(),
                required: false,
            },
            Parameter {
                name: "message".to_string(),
                type_hint: "string".to_string(),
                description: "Error message to display to the client".to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "postgresql_error_response",
            "severity": "ERROR",
            "code": "42P01",
            "message": "relation \"table_name\" does not exist"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL {severity} {code}: {message}")
                .with_debug("PostgreSQL error_response: {severity} {code}"),
        ),
    }
}

/// Action definition: Send PostgreSQL command complete response
pub fn postgresql_ok_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "postgresql_ok_response".to_string(),
        description: "Command-complete tag for a statement that returns no rows: INSERT, \
                      UPDATE, DELETE, CREATE, SET, BEGIN. Never for SELECT or SHOW - those \
                      are answered with postgresql_query_response. The tag names the statement \
                      that ran (a SELECT answered here would print 'INSERT 0 1')."
            .to_string(),
        parameters: vec![Parameter {
            name: "tag".to_string(),
            type_hint: "string".to_string(),
            description: "Command tag (e.g. 'INSERT 0 1', 'UPDATE 3', 'DELETE 2', 'CREATE TABLE')"
                .to_string(),
            required: true,
        }],
        example: json!({
            "type": "postgresql_ok_response",
            "tag": "INSERT 0 1"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL OK: {tag}")
                .with_debug("PostgreSQL ok_response: {tag}"),
        ),
    }
}

/// Action definition: Close current PostgreSQL connection
pub fn close_this_connection_action() -> ActionDefinition {
    ActionDefinition {
        name: "close_this_connection".to_string(),
        description: "Close the current PostgreSQL connection".to_string(),
        parameters: vec![],
        example: json!({"type": "close_this_connection"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("PostgreSQL connection closed")
                .with_debug("PostgreSQL close_this_connection"),
        ),
    }
}

// ============================================================================
// PostgreSQL Action Constants
// ============================================================================

/// PostgreSQL query response action constant
pub static POSTGRESQL_QUERY_RESPONSE_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| ActionDefinition {
        name: "postgresql_query_response".to_string(),
        description: "The answer to any statement that returns rows - SELECT (including \
                      SELECT 1, SELECT current_user, SELECT version()), SHOW, VALUES, WITH. \
                      A single value is one column and one row. Never answer a SELECT with \
                      postgresql_ok_response: a command tag carries no rows, so the client \
                      prints the tag and never sees the value. One response per query."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "columns".to_string(),
                type_hint: "array".to_string(),
                description: "Array of column definitions. Each column needs 'name' and 'type'. \
                              Recognised types: int2/smallint, int4/int/integer, int8/bigint, \
                              float4/real, float8/double, bool/boolean, date, time, timestamp, \
                              text, varchar (anything else is sent as varchar). Rows shorter than \
                              the column list are padded with NULLs; extra values are dropped"
                    .to_string(),
                required: true,
            },
            Parameter {
                name: "rows".to_string(),
                type_hint: "array".to_string(),
                description:
                    "Array of rows. Each row is an array of values matching the column order"
                        .to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "postgresql_query_response",
            "columns": [{"name": "id", "type": "int4"}, {"name": "name", "type": "text"}],
            "rows": [[1, "Alice"], [2, "Bob"]]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL {columns_len} cols, {rows_len} rows")
                .with_debug("PostgreSQL query_response: {columns_len} columns, {rows_len} rows"),
        ),
    });

/// PostgreSQL error response action constant
pub static POSTGRESQL_ERROR_RESPONSE_ACTION: LazyLock<ActionDefinition> = LazyLock::new(|| {
    ActionDefinition {
        name: "postgresql_error_response".to_string(),
        description: "Send an error response to the client".to_string(),
        parameters: vec![
            Parameter {
                name: "severity".to_string(),
                type_hint: "string".to_string(),
                description: "Error severity (ERROR, FATAL, PANIC, WARNING, NOTICE, DEBUG, INFO, LOG)".to_string(),
                required: false,
            },
            Parameter {
                name: "code".to_string(),
                type_hint: "string".to_string(),
                description: "PostgreSQL error code (e.g. '42P01' for undefined_table, '42601' for syntax_error)".to_string(),
                required: false,
            },
            Parameter {
                name: "message".to_string(),
                type_hint: "string".to_string(),
                description: "Error message to display to the client".to_string(),
                required: true,
            },
        ],
        example: json!({
            "type": "postgresql_error_response",
            "severity": "ERROR",
            "code": "42P01",
            "message": "relation \"table_name\" does not exist"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL {severity} {code}: {message}")
                .with_debug("PostgreSQL error_response: {severity} {code}"),
        ),
    }
});

/// PostgreSQL OK response action constant
pub static POSTGRESQL_OK_RESPONSE_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| ActionDefinition {
        name: "postgresql_ok_response".to_string(),
        description: "Command-complete tag for a statement that returns no rows: INSERT, \
                      UPDATE, DELETE, CREATE, SET, BEGIN. Never for SELECT or SHOW - those \
                      are answered with postgresql_query_response. The tag names the statement \
                      that ran (a SELECT answered here would print 'INSERT 0 1')."
            .to_string(),
        parameters: vec![Parameter {
            name: "tag".to_string(),
            type_hint: "string".to_string(),
            description: "Command tag (e.g. 'INSERT 0 1', 'UPDATE 3', 'DELETE 2', 'CREATE TABLE')"
                .to_string(),
            required: true,
        }],
        example: json!({
            "type": "postgresql_ok_response",
            "tag": "INSERT 0 1"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> PostgreSQL OK: {tag}")
                .with_debug("PostgreSQL ok_response: {tag}"),
        ),
    });

/// PostgreSQL close connection action constant
pub static POSTGRESQL_CLOSE_CONNECTION_ACTION: LazyLock<ActionDefinition> =
    LazyLock::new(|| ActionDefinition {
        name: "close_this_connection".to_string(),
        description: "Close the current PostgreSQL connection".to_string(),
        parameters: vec![],
        example: json!({"type": "close_this_connection"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("PostgreSQL connection closed")
                .with_debug("PostgreSQL close_this_connection"),
        ),
    });

// ============================================================================
// PostgreSQL Event Type Constants
// ============================================================================

/// PostgreSQL query event - triggered when client sends a query
pub static POSTGRESQL_QUERY_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "postgresql_query",
        "PostgreSQL query received from client",
        json!({"type": "placeholder", "event_id": "postgresql_query"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "query".to_string(),
            type_hint: "string".to_string(),
            description: "The SQL query string sent by the client".to_string(),
            required: true,
        },
        Parameter {
            name: "answer_with".to_string(),
            type_hint: "string".to_string(),
            description: "Which response shape the statement needs, from its first keyword. \
                          Absent when the server cannot tell."
                .to_string(),
            required: false,
        },
    ])
    .with_actions(vec![
        POSTGRESQL_QUERY_RESPONSE_ACTION.clone(),
        POSTGRESQL_ERROR_RESPONSE_ACTION.clone(),
        POSTGRESQL_OK_RESPONSE_ACTION.clone(),
        POSTGRESQL_CLOSE_CONNECTION_ACTION.clone(),
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("PostgreSQL {client_ip}: {preview(query,80)}")
            .with_debug("PostgreSQL query from {client_ip}:{client_port}")
            .with_trace("PostgreSQL: {json_pretty(.)}"),
    )
});

/// The `answer_with` event field for `sql`, or `None` when its first keyword does not say.
///
/// See `crate::utils::sql`: the real-model eval answered `SELECT current_user` with the command
/// tag `INSERT 0 1`, copied from the ok action's example, so the client printed a tag instead of
/// the value it asked for.
pub fn answer_with_for_query(sql: &str) -> Option<&'static str> {
    use crate::utils::sql::{statement_shape, StatementShape};
    match statement_shape(sql) {
        StatementShape::Rows => Some(
            "postgresql_query_response: this statement returns rows - columns and rows, one \
             column and one row for a single value - never a command tag",
        ),
        StatementShape::NoRows => Some(
            "postgresql_ok_response: this statement returns no rows - the command tag of what \
             ran, e.g. 'INSERT 0 1', 'UPDATE 3', 'SET'",
        ),
        StatementShape::Unknown => None,
    }
}

/// Get PostgreSQL event types
pub fn get_postgresql_event_types() -> Vec<EventType> {
    vec![POSTGRESQL_QUERY_EVENT.clone()]
}