Principle:2giosangmitom Sqmeow nvim Result Export
| Knowledge Sources | |
|---|---|
| Domains | Result_Export, Data_Processing |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
A result export principle that encodes retained query results as CSV, JSON, or SQL text, respecting active view scope, column selection, and dialect-specific quoting.
Description
Result Export is the principle of encoding rows from a retained ResultSet into a portable text format (CSV, JSON, or SQL INSERT statements) for saving to a file or copying to the clipboard. The export respects the active view (only exported rows in the view are included, unless all is set), column selection, and row range (offset/limit).
This principle addresses the challenge of exporting query results in multiple formats while preserving exact values and handling dialect-specific syntax. CSV uses polars_io::CsvWriter for native columns with Object fallback formatting. JSON encodes each row as an array of values. SQL generates INSERT statements with dialect-specific identifier quoting, optional CREATE TABLE preamble, and multi-row batch inserts (up to 500 rows per INSERT). Non-SQL dialects (Redis, MongoDB, SurrealDB) are rejected for SQL export.
The export dialect for SQL format is resolved from the result's connection (if open), or the dialect argument, defaulting to PostgreSQL.
Usage
Use this principle when a database tool exports query results: support multiple formats, respect active view and column scope, handle dialect-specific quoting, and offer both file and clipboard destinations.
Theoretical Basis
The principle encodes retained rows through format-specific encoders:
- Row resolution:
Rows::resolvemaps a row range through the active view (or all rows ifallor no view). - Column selection:
chosenfilters column indices to those that exist. - CSV: Build a Polars frame with text/numeric projections; write with
CsvWriter. Optional header row with original names. - JSON: Encode each row as a JSON array of cell values (null, bool, number, string).
- SQL: For each row, generate
INSERT INTO "table" ("col1", "col2") VALUES (val1, val2)with dialect quoting. OptionalCREATE TABLEand batch mode (multi-row INSERT).
# Abstract pattern (NOT real implementation):
def write(result, format, rows, columns, headers) -> str:
cols = columns or range(len(result.columns))
match format:
case CSV: return csv_encode(result, rows, cols, headers)
case JSON: return json_encode(result, rows, cols)
case SQL: return sql_insert_encode(result, rows, cols, format.dialect)