sql-splitter 1.16.1

High-performance CLI tool for splitting large SQL dump files into individual table files
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
//! Streams a compiled generation run to dialect-correct SQL.
//!
//! [`SqlRenderer`] is a [`RowSink`]: [`crate::generate::GenerationEngine::run`]
//! drives it one table at a time, and it writes DDL (unless `data_only`) plus
//! batched `INSERT`/`COPY` statements (unless `schema_only`) straight to a
//! [`BufWriter`], reusing one row buffer per active table so a long
//! generation run never allocates a formatted string per cell or per row —
//! every value is written through a [`std::fmt::Display`] wrapper straight
//! into the destination buffer. See [`super::ddl`] for `CREATE TABLE`
//! rendering.

use std::fmt::{self, Write as _};
use std::io::{self, BufWriter, Write};

use crate::convert::{ConvertWarning, WarningCollector};
use crate::generate::{GenerateError, GeneratedRow, GeneratedValue, PlannedTable, RowSink};
use crate::parser::SqlDialect;
use crate::synthetic::OutputMode;
use crate::transform_common::quote_ident;

use super::ddl;
use super::row_batch::RowBatch;
use super::sql_string::SqlString;

/// Rendering knobs for [`SqlRenderer`], independent of the compiled
/// [`crate::generate::GenerationPlan`] so a caller (the CLI, or a test) can
/// render the same plan under several target dialects.
#[derive(Debug, Clone, Copy)]
pub struct RenderOptions {
    /// The dialect to render SQL for.
    pub dialect: SqlDialect,
    /// The dialect the source schema's types/DDL were captured in, if known.
    /// `None` when the model has no `source:` block (e.g. hand-authored).
    pub source_dialect: Option<SqlDialect>,
    /// Whether to render DDL, rows, or both.
    pub mode: OutputMode,
    /// Force multi-row `INSERT` for PostgreSQL instead of `COPY`.
    pub no_copy: bool,
    /// Rows per `INSERT`/`COPY` batch.
    pub batch_size: usize,
    /// Render MSSQL output in "production style": a `SET ANSI_NULLS ON;` +
    /// `SET QUOTED_IDENTIFIER ON;` session header, `[dbo].[table]`-qualified
    /// identifiers, a named `CONSTRAINT [PK_<table>] PRIMARY KEY CLUSTERED`
    /// instead of an inline `PRIMARY KEY`, and an `ON [PRIMARY]` filegroup
    /// clause on `CREATE TABLE`. No effect outside [`SqlDialect::Mssql`].
    pub mssql_production_style: bool,
    /// Emit a `GO` batch separator every N `INSERT` batches instead of after
    /// every batch (MSSQL only; `None` keeps today's per-batch `GO`). `CREATE
    /// TABLE` and `SET IDENTITY_INSERT` batches always get their own `GO`
    /// regardless of this setting — only `INSERT` batch cadence changes.
    pub mssql_go: Option<u64>,
}

impl Default for RenderOptions {
    fn default() -> Self {
        Self {
            dialect: SqlDialect::MySql,
            source_dialect: None,
            mode: OutputMode::SchemaAndData,
            no_copy: false,
            batch_size: 1000,
            mssql_production_style: false,
            mssql_go: None,
        }
    }
}

/// Per-table render state, live between `begin_table` and `end_table`.
struct TableState {
    quoted_table: String,
    /// Whether this run renders row data at all (`false` under `schema_only`).
    data_enabled: bool,
    /// Whether rows render as PostgreSQL `COPY` rather than `INSERT`.
    use_copy: bool,
    /// Rows per batch/statement.
    batch_size: usize,
    /// Column indices to render, in schema order, excluding every column
    /// that renders `DEFAULT` for every row. `None` until the first row
    /// (a `DEFAULT` column is a per-column, not per-row, decision - see
    /// `SqlRenderer::classify_columns` - but the renderer only learns it by
    /// inspecting the first generated row).
    insert_columns: Option<Vec<usize>>,
    /// Buffered `INSERT` row tuples, reused across batches.
    row_batch: RowBatch,
    /// Buffered `COPY` lines, reused across batches (`RowBatch` joins with
    /// `,\n`, which is INSERT-tuple shaped; `COPY` wants one bare line per
    /// row, so this is a plain reused buffer instead).
    copy_batch: String,
    /// Rows buffered in `copy_batch` since the last flush.
    copy_rows: usize,
    /// Whether the classified `insert_columns` carry an explicit value for an
    /// `IDENTITY`/`GENERATED ALWAYS AS IDENTITY` column — the normal case for a
    /// primary key referenced by generated foreign keys. Such a value is
    /// rejected by MSSQL/PostgreSQL unless the load is wrapped
    /// (`SET IDENTITY_INSERT ... ON`/`OVERRIDING SYSTEM VALUE`). `None` until the
    /// first row classifies the columns.
    identity_insert: Option<bool>,
    /// Whether a `SET IDENTITY_INSERT <t> ON` has been emitted for this table
    /// (MSSQL) and therefore an `OFF` must close it out in `end_table`.
    identity_on: bool,
    /// `INSERT` batches flushed for this table since the last `GO` (MSSQL,
    /// only meaningful when [`RenderOptions::mssql_go`] is `Some`).
    insert_batches_since_go: u64,
}

/// Streams generated rows to dialect-correct SQL as a [`RowSink`].
pub struct SqlRenderer<W: Write> {
    writer: BufWriter<W>,
    options: RenderOptions,
    warnings: WarningCollector,
    table: Option<TableState>,
    /// Whether the MSSQL production-style session header (`SET ANSI_NULLS
    /// ON;` / `SET QUOTED_IDENTIFIER ON;`) has already been written; it is
    /// emitted once, before the first table, not per table.
    mssql_header_written: bool,
}

impl<W: Write> SqlRenderer<W> {
    /// Wrap `writer` and render under `options`.
    pub fn new(writer: W, options: RenderOptions) -> Self {
        Self {
            writer: BufWriter::new(writer),
            options,
            warnings: WarningCollector::new(),
            table: None,
            mssql_header_written: false,
        }
    }

    /// Warnings collected while rendering DDL (e.g. a lossy cross-dialect
    /// type conversion).
    pub fn warnings(&self) -> &[ConvertWarning] {
        self.warnings.warnings()
    }

    /// Flush and unwrap the underlying writer.
    pub fn finish(mut self) -> Result<W, GenerateError> {
        self.writer.flush().map_err(io_err)?;
        self.writer
            .into_inner()
            .map_err(|err| io_err(err.into_error()))
    }

    /// Write the MSSQL production-style session header
    /// (`SET ANSI_NULLS ON;` / `SET QUOTED_IDENTIFIER ON;`, `GO`-terminated)
    /// once, before the first table. A no-op outside
    /// [`RenderOptions::mssql_production_style`] + [`SqlDialect::Mssql`], and
    /// idempotent (only ever writes once per renderer).
    fn maybe_write_mssql_header(&mut self) -> Result<(), GenerateError> {
        if self.mssql_header_written
            || !self.options.mssql_production_style
            || self.options.dialect != SqlDialect::Mssql
        {
            return Ok(());
        }
        writeln!(self.writer, "SET ANSI_NULLS ON;").map_err(io_err)?;
        writeln!(self.writer, "SET QUOTED_IDENTIFIER ON;").map_err(io_err)?;
        writeln!(self.writer, "GO").map_err(io_err)?;
        self.mssql_header_written = true;
        Ok(())
    }

    /// Render `table`'s DDL: the raw `create_statement` when the render
    /// target matches the source dialect (see [`ddl::should_preserve_raw_ddl`]),
    /// otherwise a normalized `CREATE TABLE` built from its [`PortableSchema`]
    /// via [`ddl::render_create_table`].
    ///
    /// [`PortableSchema`]: crate::synthetic::schema::PortableSchema
    fn render_ddl(&mut self, table: &PlannedTable) -> Result<(), GenerateError> {
        let dialect = self.options.dialect;
        if ddl::should_preserve_raw_ddl(&table.schema, self.options.source_dialect, dialect) {
            let statement = table.schema.create_statement.as_deref().unwrap_or_default();
            writeln!(self.writer, "{statement}").map_err(io_err)?;
        } else {
            let from = self.options.source_dialect.unwrap_or(dialect);
            let sql = ddl::render_create_table(
                &table.schema,
                from,
                dialect,
                &mut self.warnings,
                self.options.mssql_production_style,
            );
            write!(self.writer, "{sql}").map_err(io_err)?;
        }
        if dialect == SqlDialect::Mssql {
            writeln!(self.writer, "GO").map_err(io_err)?;
        }
        Ok(())
    }

    /// Flush the buffered `INSERT` batch (a no-op if empty).
    fn flush_insert(&mut self, table: &PlannedTable) -> Result<(), GenerateError> {
        let dialect = self.options.dialect;
        let mssql_go = self.options.mssql_go;
        let state = self
            .table
            .as_mut()
            .expect("flush_insert is only called while a table is active");
        if state.row_batch.is_empty() {
            return Ok(());
        }
        let indices = state
            .insert_columns
            .as_ref()
            .expect("insert_columns is set before the first row is buffered");
        let identity_insert = state.identity_insert.unwrap_or(false);
        let column_list = quoted_column_list(dialect, table, indices);
        // MSSQL rejects an explicit value for an IDENTITY column unless
        // IDENTITY_INSERT is toggled on for the table; open it once, before the
        // first batch, and close it in `end_table`.
        if identity_insert && dialect == SqlDialect::Mssql && !state.identity_on {
            writeln!(self.writer, "SET IDENTITY_INSERT {} ON", state.quoted_table)
                .map_err(io_err)?;
            writeln!(self.writer, "GO").map_err(io_err)?;
            state.identity_on = true;
        }
        // PostgreSQL rejects an explicit value for a `GENERATED ALWAYS AS
        // IDENTITY` column unless the statement declares `OVERRIDING SYSTEM
        // VALUE`.
        let overriding = if identity_insert && dialect == SqlDialect::Postgres {
            " OVERRIDING SYSTEM VALUE"
        } else {
            ""
        };
        if indices.is_empty() && dialect != SqlDialect::MySql {
            // Every column is a database default/identity: PostgreSQL, SQLite,
            // and SQL Server reject `() VALUES ()`, so emit one `DEFAULT VALUES`
            // row per buffered row (MySQL keeps the valid `() VALUES (),()` form).
            for _ in 0..state.row_batch.row_count() {
                writeln!(
                    self.writer,
                    "INSERT INTO {} DEFAULT VALUES;",
                    state.quoted_table
                )
                .map_err(io_err)?;
            }
        } else {
            writeln!(
                self.writer,
                "INSERT INTO {} ({column_list}){overriding} VALUES",
                state.quoted_table
            )
            .map_err(io_err)?;
            writeln!(self.writer, "{};", state.row_batch.as_str()).map_err(io_err)?;
        }
        if dialect == SqlDialect::Mssql {
            let should_go = match mssql_go {
                // Group `n` INSERT batches per GO instead of one-per-batch.
                Some(n) => {
                    state.insert_batches_since_go += 1;
                    if state.insert_batches_since_go >= n.max(1) {
                        state.insert_batches_since_go = 0;
                        true
                    } else {
                        false
                    }
                }
                // Default (and today's only) behavior: a GO after every batch.
                None => true,
            };
            if should_go {
                writeln!(self.writer, "GO").map_err(io_err)?;
            }
        }
        state.row_batch.clear();
        Ok(())
    }

    /// Flush the buffered `COPY` batch (a no-op if empty).
    fn flush_copy(&mut self, table: &PlannedTable) -> Result<(), GenerateError> {
        let state = self
            .table
            .as_mut()
            .expect("flush_copy is only called while a table is active");
        if state.copy_rows == 0 {
            return Ok(());
        }
        let indices = state
            .insert_columns
            .as_ref()
            .expect("insert_columns is set before the first row is buffered");
        let column_list = quoted_column_list(SqlDialect::Postgres, table, indices);
        writeln!(
            self.writer,
            "COPY {} ({column_list}) FROM stdin;",
            state.quoted_table
        )
        .map_err(io_err)?;
        write!(self.writer, "{}", state.copy_batch).map_err(io_err)?;
        writeln!(self.writer, "\\.").map_err(io_err)?;
        state.copy_batch.clear();
        state.copy_rows = 0;
        Ok(())
    }

    /// Determine, from the first generated row, which columns render
    /// `DEFAULT` for every row of this table (see [`TableState::insert_columns`]),
    /// and reject a `COPY` target for a `DEFAULT` column with no database
    /// default/identity to fall back on (`COPY`'s column list can omit a
    /// column, but every omitted column must have somewhere to get its value
    /// from).
    fn classify_columns(
        table: &PlannedTable,
        row: &GeneratedRow,
        use_copy: bool,
    ) -> Result<Vec<usize>, GenerateError> {
        let is_default_column: Vec<bool> = row
            .values
            .iter()
            .map(|value| matches!(value, GeneratedValue::Default))
            .collect();
        if use_copy {
            for (i, column) in table.columns.iter().enumerate() {
                if !is_default_column[i] {
                    continue;
                }
                let schema = &column.schema;
                if schema.default_sql.is_none() && !schema.identity && !schema.generated {
                    return Err(GenerateError::diagnostic(
                        &crate::diagnostic::codes::RENDER_COPY_DEFAULT,
                        format!("tables.{}.columns.{}", table.name, schema.name),
                        format!(
                            "table `{}` column `{}` renders as DEFAULT but has no database \
                             default/identity for PostgreSQL COPY to fall back on; render with \
                             `no_copy` (multi-row INSERT) instead",
                            table.name, schema.name
                        ),
                    ));
                }
            }
        }
        Ok((0..table.columns.len())
            .filter(|&i| !is_default_column[i])
            .collect())
    }
}

impl<W: Write> RowSink for SqlRenderer<W> {
    fn begin_table(&mut self, table: &PlannedTable) -> Result<(), GenerateError> {
        self.maybe_write_mssql_header()?;
        if self.options.mode != OutputMode::DataOnly {
            self.render_ddl(table)?;
        }
        let data_enabled = self.options.mode != OutputMode::SchemaOnly;
        // A table carrying an identity column that may receive explicit values
        // must render as multi-row `INSERT` (not `COPY`) on PostgreSQL, so the
        // load can add `OVERRIDING SYSTEM VALUE`; `COPY` has no such clause.
        let has_identity = table.columns.iter().any(|column| column.schema.identity);
        let use_copy = data_enabled
            && self.options.dialect == SqlDialect::Postgres
            && !self.options.no_copy
            && !has_identity;
        let batch_size = self.options.batch_size.max(1);
        self.table = Some(TableState {
            quoted_table: ddl::qualified_table(
                self.options.dialect,
                &table.name,
                self.options.mssql_production_style,
            ),
            data_enabled,
            use_copy,
            batch_size,
            insert_columns: None,
            row_batch: RowBatch::with_capacity(batch_size, batch_size.saturating_mul(64)),
            copy_batch: String::new(),
            copy_rows: 0,
            identity_insert: None,
            identity_on: false,
            insert_batches_since_go: 0,
        });
        Ok(())
    }

    fn write_row(&mut self, table: &PlannedTable, row: &GeneratedRow) -> Result<(), GenerateError> {
        let dialect = self.options.dialect;
        let (data_enabled, use_copy, needs_classification) = match &self.table {
            Some(state) => (
                state.data_enabled,
                state.use_copy,
                state.insert_columns.is_none(),
            ),
            None => return Ok(()),
        };
        if !data_enabled {
            return Ok(());
        }
        if needs_classification {
            let insert_columns = Self::classify_columns(table, row, use_copy)?;
            // An identity column that survives classification (i.e. carries an
            // explicit value rather than rendering `DEFAULT`) needs the
            // engine-specific identity-insert wrapper to load.
            let identity_insert = matches!(dialect, SqlDialect::Mssql | SqlDialect::Postgres)
                && insert_columns
                    .iter()
                    .any(|&i| table.columns[i].schema.identity);
            let state = self.table.as_mut().expect("checked above");
            state.insert_columns = Some(insert_columns);
            state.identity_insert = Some(identity_insert);
        }

        let state = self.table.as_mut().expect("checked above");
        let indices = state
            .insert_columns
            .as_ref()
            .expect("set just above on the first row");

        if state.use_copy {
            write!(
                state.copy_batch,
                "{}",
                CopyRow {
                    indices,
                    values: &row.values,
                }
            )
            .map_err(fmt_err)?;
            state.copy_batch.push('\n');
            state.copy_rows += 1;
            if state.copy_rows >= state.batch_size {
                self.flush_copy(table)?;
            }
        } else {
            state
                .row_batch
                .push_fmt(format_args!(
                    "{}",
                    RowTuple {
                        dialect,
                        indices,
                        values: &row.values,
                    }
                ))
                .map_err(fmt_err)?;
            if state.row_batch.row_count() >= state.batch_size {
                self.flush_insert(table)?;
            }
        }
        Ok(())
    }

    fn end_table(&mut self, table: &PlannedTable) -> Result<(), GenerateError> {
        let (data_enabled, use_copy) = match &self.table {
            Some(state) => (state.data_enabled, state.use_copy),
            None => (false, false),
        };
        if data_enabled {
            if use_copy {
                self.flush_copy(table)?;
            } else {
                self.flush_insert(table)?;
            }
        }
        // Under `--mssql-go N`, the last batch flushed above may not have
        // reached the Nth-batch boundary that triggers a `GO`; close this
        // table's batch out cleanly (rather than merging into whatever
        // follows — the next table's DDL, or the IDENTITY_INSERT OFF below)
        // by emitting the trailing `GO` here.
        if self.options.dialect == SqlDialect::Mssql && self.options.mssql_go.is_some() {
            if let Some(state) = &mut self.table {
                if state.insert_batches_since_go > 0 {
                    state.insert_batches_since_go = 0;
                    writeln!(self.writer, "GO").map_err(io_err)?;
                }
            }
        }
        // Close the MSSQL identity-insert wrapper opened in `flush_insert`.
        if let Some(state) = &self.table {
            if state.identity_on {
                writeln!(
                    self.writer,
                    "SET IDENTITY_INSERT {} OFF",
                    state.quoted_table
                )
                .map_err(io_err)?;
                writeln!(self.writer, "GO").map_err(io_err)?;
            }
        }
        self.table = None;
        Ok(())
    }
}

/// Map an [`io::Error`] into the closed [`GenerateError`] set: the
/// [`RowSink`] contract has no I/O variant, so a write
/// failure surfaces as a `GEN-RENDER-IO` diagnostic rather than silently
/// panicking or being swallowed.
fn io_err(err: io::Error) -> GenerateError {
    GenerateError::diagnostic(
        &crate::diagnostic::codes::RENDER_IO,
        "output",
        err.to_string(),
    )
}

/// Map a [`fmt::Error`] the same way as [`io_err`]; writing into an in-memory
/// buffer is infallible in practice; this only exists so `?` type-checks.
fn fmt_err(_: fmt::Error) -> GenerateError {
    GenerateError::diagnostic(
        &crate::diagnostic::codes::RENDER_IO,
        "output",
        "formatting error",
    )
}

/// Quote and comma-join the table's column names at `indices`, in order.
fn quoted_column_list(dialect: SqlDialect, table: &PlannedTable, indices: &[usize]) -> String {
    indices
        .iter()
        .map(|&i| quote_ident(dialect, &table.columns[i].schema.name))
        .collect::<Vec<_>>()
        .join(", ")
}

/// One `INSERT` row tuple: `(v1, v2, ...)`, written straight into the
/// destination [`RowBatch`] with no intermediate per-cell allocation.
struct RowTuple<'a> {
    dialect: SqlDialect,
    indices: &'a [usize],
    values: &'a [GeneratedValue],
}

impl fmt::Display for RowTuple<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("(")?;
        for (position, &i) in self.indices.iter().enumerate() {
            if position > 0 {
                f.write_str(", ")?;
            }
            write!(
                f,
                "{}",
                InsertValue {
                    dialect: self.dialect,
                    value: &self.values[i],
                }
            )?;
        }
        f.write_str(")")
    }
}

/// A single value rendered as an `INSERT`-literal, dialect-correct via
/// [`SqlString`] for text-like values.
struct InsertValue<'a> {
    dialect: SqlDialect,
    value: &'a GeneratedValue,
}

impl fmt::Display for InsertValue<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value {
            GeneratedValue::Null => f.write_str("NULL"),
            // Reachable only if a column classified as non-DEFAULT (from the
            // first row) ever renders `Default` on a later row; the engine's
            // per-table owner assignment makes that impossible today (see
            // `SqlRenderer::classify_columns`), but this still renders a
            // syntactically valid literal instead of corrupting the batch.
            GeneratedValue::Default => f.write_str("DEFAULT"),
            GeneratedValue::Boolean(value) => f.write_str(boolean_literal(self.dialect, *value)),
            GeneratedValue::Integer(value) => write!(f, "{value}"),
            GeneratedValue::Decimal { minor, scale } => write_decimal(f, *minor, *scale),
            GeneratedValue::Text(value) => write!(f, "{}", SqlString::new(self.dialect, value)),
            GeneratedValue::Bytes(value) => write_bytes_literal(f, self.dialect, value),
            GeneratedValue::DateTime(value) => write!(f, "{}", SqlString::new(self.dialect, value)),
            GeneratedValue::Json(value) => write!(f, "{}", SqlString::new(self.dialect, value)),
        }
    }
}

/// The dialect-correct boolean literal. MySQL/SQLite have no boolean type and
/// MSSQL's `BIT` only accepts `0`/`1`; PostgreSQL's `boolean` accepts (and
/// pretty-prints as) `TRUE`/`FALSE`.
fn boolean_literal(dialect: SqlDialect, value: bool) -> &'static str {
    match (dialect, value) {
        (SqlDialect::Postgres, true) => "TRUE",
        (SqlDialect::Postgres, false) => "FALSE",
        (_, true) => "1",
        (_, false) => "0",
    }
}

/// Render a fixed-point `{ minor, scale }` value as `123.45` (or a bare
/// integer when `scale` is `0`), without going through floating point.
fn write_decimal(f: &mut fmt::Formatter<'_>, minor: i128, scale: u32) -> fmt::Result {
    if scale == 0 {
        return write!(f, "{minor}");
    }
    let divisor = 10i128.pow(scale);
    if minor < 0 {
        f.write_str("-")?;
    }
    let magnitude = minor.unsigned_abs();
    let whole = magnitude / divisor as u128;
    let fraction = magnitude % divisor as u128;
    write!(f, "{whole}.{fraction:0width$}", width = scale as usize)
}

/// Render a bytes value as the dialect's binary-string-literal syntax.
fn write_bytes_literal(
    f: &mut fmt::Formatter<'_>,
    dialect: SqlDialect,
    bytes: &[u8],
) -> fmt::Result {
    match dialect {
        SqlDialect::MySql | SqlDialect::Sqlite => {
            f.write_str("X'")?;
            write_hex(f, bytes)?;
            f.write_str("'")
        }
        SqlDialect::Postgres => {
            f.write_str("'\\x")?;
            write_hex(f, bytes)?;
            f.write_str("'")
        }
        SqlDialect::Mssql => {
            f.write_str("0x")?;
            write_hex(f, bytes)
        }
    }
}

/// Write `bytes` as lowercase hex, two characters per byte, straight into
/// the formatter (no intermediate hex-encoded `String`).
fn write_hex(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
    for byte in bytes {
        write!(f, "{byte:02x}")?;
    }
    Ok(())
}

/// One `COPY` line: dialect-fixed to PostgreSQL's tab-delimited text format,
/// which differs from `INSERT`-literal escaping (see [`CopyValue`]).
struct CopyRow<'a> {
    indices: &'a [usize],
    values: &'a [GeneratedValue],
}

impl fmt::Display for CopyRow<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (position, &i) in self.indices.iter().enumerate() {
            if position > 0 {
                f.write_str("\t")?;
            }
            write!(f, "{}", CopyValue(&self.values[i]))?;
        }
        Ok(())
    }
}

/// A single value in PostgreSQL `COPY` text format: unquoted, tab/newline
/// delimited, `NULL` spelled `\N`, and backslash-escaped rather than
/// SQL-string-escaped (see the module docs for why this cannot reuse
/// [`SqlString`]).
struct CopyValue<'a>(&'a GeneratedValue);

impl fmt::Display for CopyValue<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.0 {
            GeneratedValue::Null => f.write_str("\\N"),
            // `Default` cannot reach a COPY stream: `classify_columns` excludes
            // every DEFAULT column from `indices` before any `CopyValue` is
            // constructed. Fail loudly if that invariant is ever broken rather
            // than silently corrupting a default into a NULL.
            GeneratedValue::Default => Err(fmt::Error),
            GeneratedValue::Boolean(value) => f.write_str(if *value { "t" } else { "f" }),
            GeneratedValue::Integer(value) => write!(f, "{value}"),
            GeneratedValue::Decimal { minor, scale } => write_decimal(f, *minor, *scale),
            GeneratedValue::Text(value) => write_copy_escaped(f, value),
            GeneratedValue::Bytes(value) => {
                f.write_str("\\\\x")?;
                write_hex(f, value)
            }
            GeneratedValue::DateTime(value) => write_copy_escaped(f, value),
            GeneratedValue::Json(value) => write_copy_escaped(f, value),
        }
    }
}

/// Escape `value` for PostgreSQL `COPY` text format: backslash, tab,
/// newline, and carriage return each get a backslash-letter escape; every
/// other character (including `'`) is copied through unquoted.
fn write_copy_escaped(f: &mut fmt::Formatter<'_>, value: &str) -> fmt::Result {
    for ch in value.chars() {
        match ch {
            '\\' => f.write_str("\\\\")?,
            '\t' => f.write_str("\\t")?,
            '\n' => f.write_str("\\n")?,
            '\r' => f.write_str("\\r")?,
            other => f.write_char(other)?,
        }
    }
    Ok(())
}

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

    #[test]
    fn copy_value_default_is_a_hard_error_not_silent_null() {
        // A `Default` reaching a COPY stream is an invariant violation and must
        // fail, not silently corrupt into a NULL (`\N`). A real NULL still emits
        // `\N`.
        let mut buf = String::new();
        assert!(
            write!(&mut buf, "{}", CopyValue(&GeneratedValue::Default)).is_err(),
            "Default in COPY must error, rendered `{buf}`"
        );

        let mut null = String::new();
        write!(&mut null, "{}", CopyValue(&GeneratedValue::Null)).unwrap();
        assert_eq!(null, "\\N");
    }
}