Implementation:2giosangmitom Sqmeow nvim export write
| Knowledge Sources | |
|---|---|
| Domains | Result_Export, Data_Processing |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
Concrete tool for encoding retained query results as CSV, JSON, or SQL text provided by the sqmeow-db crate.
Description
The export::write function encodes rows of a ResultSet in the specified format, respecting row and column scope.
Format encoders:
- CSV: Uses
polars_io::CsvWriterfor native columns; Object/binary values use the lossless compatibility formatter. Optional header row with original column names (duplicate names preserved). Single-field CSV with an empty name outputs a blank line. - JSON: Encodes each row as a JSON array of cell values (null, boolean, number, string). Binary and Object values use cell display text.
- SQL: Generates
INSERTstatements with dialect-specific quoting (Dialect::quote_ident). SupportsCREATE TABLEpreamble, multi-row batch inserts (up to 500 rows per INSERT), and a target table name override. Non-SQL dialects (Redis, MongoDB, SurrealDB) are rejected for SQL export.
The Format enum is parsed from a format name ("csv", "json", "sql"). The Rows struct specifies a row range (start, end), resolved through the active view when present.
Usage
Called by the export and export_preview RPC handlers when the user exports results to a file or clipboard.
Code Reference
Source Location
- Repository: sqmeow.nvim
- File: core/sqmeow-db/src/export.rs
- Lines: L12-28 (Format), L35-48 (Format::parse), L52-79 (Rows), L82-94 (write)
Signature
pub enum Format { Csv, Json, Sql(Sql) }
pub struct Sql {
pub dialect: Dialect,
pub table: Option<String>,
pub batch: bool,
pub create: bool,
}
pub struct Rows { pub start: usize, pub end: usize }
impl Format {
pub fn parse(name: &str) -> Option<Self>;
}
impl Rows {
pub fn all() -> Self;
pub fn resolve(self, result: &ResultSet, view: Option<&[usize]>) -> Vec<usize>;
}
pub fn write(
result: &ResultSet,
format: &Format,
rows: &[usize],
columns: Option<&[usize]>,
headers: bool,
) -> String;
Import
use sqmeow_db::export::{self, Format, Rows};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| result | &ResultSet | Yes /// Retained data | |
| format | &Format | Yes /// Export format (Csv, Json, Sql) | |
| rows | &[usize] | Yes /// Row indices to export | |
| columns | Option<&[usize]> | No /// Column selection (None = all) | |
| headers | bool | Yes /// (CSV) Include header row |
Outputs
| Name | Type | Description |
|---|---|---|
| write | String /// Encoded export text (CSV, JSON, or SQL INSERTs) |
Usage Examples
Exporting to CSV
let text = export::write(&result, &Format::Csv, &rows, None, true);
// → "id,name\n1,Alice\n2,Bob\n"
Exporting to SQL with CREATE TABLE
let format = Format::Sql(Sql {
dialect: Dialect::Postgres,
table: Some("users_backup".into()),
batch: true,
create: true,
});
let text = export::write(&result, &format, &rows, None, true);
// → "CREATE TABLE \"users_backup\" (\"id\" BIGINT, \"name\" TEXT);
// INSERT INTO \"users_backup\" (\"id\", \"name\") VALUES (1, 'Alice'), (2, 'Bob');"
Related Pages
Implements Principle
Requires Environment
- (None — string generation)