feat(core): split foundational capabilities into dedicated crates
This commit is contained in:
402 files changed
+4386
-3367
No files matched your search
@@ -0,0 +1,240 @@
|
||||
use crate::data_grid_sql::{DataGridCopyInsertMode, DataGridTableMeta};
|
||||
use crate::models::connection::DatabaseType;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::fmt;
|
||||
|
||||
pub const DATA_GRID_EXTRACTOR_CONTRACT_VERSION: u16 = 1;
|
||||
|
||||
const DEFAULT_DSV_COLUMN_SEPARATOR: &str = ",";
|
||||
const DEFAULT_DSV_ROW_SEPARATOR: &str = "\n";
|
||||
const DEFAULT_DSV_NULL_TEXT: &str = "NULL";
|
||||
const DEFAULT_DSV_QUOTE: char = '"';
|
||||
const DEFAULT_SQL_SKIP_COMPUTED_COLUMNS: bool = true;
|
||||
const DEFAULT_SQL_SKIP_GENERATED_COLUMNS: bool = true;
|
||||
const DEFAULT_JSON_PRETTY: bool = true;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridExtractorId {
|
||||
Raw,
|
||||
Tsv,
|
||||
TsvWithHeaders,
|
||||
Csv,
|
||||
CsvWithHeaders,
|
||||
PipeSeparated,
|
||||
Dsv,
|
||||
Json,
|
||||
JsonLines,
|
||||
OneRow,
|
||||
SqlInList,
|
||||
SqlInserts,
|
||||
SqlUpdates,
|
||||
SqlSelect,
|
||||
WhereClause,
|
||||
Markdown,
|
||||
Html,
|
||||
Xml,
|
||||
Pretty,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridSelectionKind {
|
||||
#[default]
|
||||
Cells,
|
||||
Rows,
|
||||
Columns,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridQuotePolicy {
|
||||
Always,
|
||||
#[default]
|
||||
Minimal,
|
||||
Never,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
|
||||
pub struct DataGridDsvOptions {
|
||||
pub column_separator: String,
|
||||
pub row_separator: String,
|
||||
pub null_text: String,
|
||||
pub quote: char,
|
||||
pub quote_policy: DataGridQuotePolicy,
|
||||
pub include_column_header: bool,
|
||||
pub include_row_header: bool,
|
||||
}
|
||||
|
||||
impl Default for DataGridDsvOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
column_separator: DEFAULT_DSV_COLUMN_SEPARATOR.to_owned(),
|
||||
row_separator: DEFAULT_DSV_ROW_SEPARATOR.to_owned(),
|
||||
null_text: DEFAULT_DSV_NULL_TEXT.to_owned(),
|
||||
quote: DEFAULT_DSV_QUOTE,
|
||||
quote_policy: DataGridQuotePolicy::Minimal,
|
||||
include_column_header: false,
|
||||
include_row_header: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
|
||||
pub struct DataGridSqlExtractorOptions {
|
||||
pub skip_computed_columns: bool,
|
||||
pub skip_generated_columns: bool,
|
||||
pub insert_mode: DataGridCopyInsertMode,
|
||||
pub exclude_primary_keys_from_insert: bool,
|
||||
}
|
||||
|
||||
impl Default for DataGridSqlExtractorOptions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
skip_computed_columns: DEFAULT_SQL_SKIP_COMPUTED_COLUMNS,
|
||||
skip_generated_columns: DEFAULT_SQL_SKIP_GENERATED_COLUMNS,
|
||||
insert_mode: DataGridCopyInsertMode::Merged,
|
||||
exclude_primary_keys_from_insert: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
|
||||
pub struct DataGridJsonExtractorOptions {
|
||||
pub pretty: bool,
|
||||
pub camel_case_field_names: bool,
|
||||
}
|
||||
|
||||
impl Default for DataGridJsonExtractorOptions {
|
||||
fn default() -> Self {
|
||||
Self { pretty: DEFAULT_JSON_PRETTY, camel_case_field_names: false }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
|
||||
pub struct DataGridExtractorOptions {
|
||||
pub dsv: DataGridDsvOptions,
|
||||
pub sql: DataGridSqlExtractorOptions,
|
||||
pub json: DataGridJsonExtractorOptions,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct DataGridExtractColumn {
|
||||
pub display_name: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub source_name: Option<String>,
|
||||
pub source_index: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct DataGridExtractRequest {
|
||||
pub version: u16,
|
||||
pub extractor: DataGridExtractorId,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub table_meta: Option<DataGridTableMeta>,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<DataGridExtractColumn>,
|
||||
#[serde(default)]
|
||||
pub selected_column_indexes: Vec<usize>,
|
||||
#[serde(default)]
|
||||
pub rows: Vec<Vec<Value>>,
|
||||
#[serde(default)]
|
||||
pub selection_kind: DataGridSelectionKind,
|
||||
#[serde(default)]
|
||||
pub options: DataGridExtractorOptions,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridExtractWarningCode {
|
||||
OmittedColumns,
|
||||
DuplicateJsonColumnNames,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DataGridExtractWarning {
|
||||
pub code: DataGridExtractWarningCode,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DataGridExtractResult {
|
||||
pub text: String,
|
||||
pub mime_type: String,
|
||||
pub file_extension: String,
|
||||
pub row_count: usize,
|
||||
pub column_count: usize,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub omitted_columns: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub warnings: Vec<DataGridExtractWarning>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridExtractErrorCode {
|
||||
UnsupportedVersion,
|
||||
EmptySelection,
|
||||
InvalidRawSelection,
|
||||
InvalidSelectSelection,
|
||||
InvalidColumnIndex,
|
||||
InvalidColumnMapping,
|
||||
MissingTableMetadata,
|
||||
MissingPrimaryKey,
|
||||
NullPrimaryKey,
|
||||
NoWritableColumns,
|
||||
UnsupportedDatabase,
|
||||
InvalidDsvConfiguration,
|
||||
InputTooLarge,
|
||||
OutputTooLarge,
|
||||
EncodingFailed,
|
||||
ExecutionFailed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DataGridExtractError {
|
||||
pub code: DataGridExtractErrorCode,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl DataGridExtractError {
|
||||
pub fn new(code: DataGridExtractErrorCode, message: impl Into<String>) -> Self {
|
||||
Self { code, message: message.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DataGridExtractError {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str(&self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for DataGridExtractError {}
|
||||
@@ -0,0 +1,232 @@
|
||||
use super::{
|
||||
value_text, write_bytes, DataGridDsvOptions, DataGridExtractError, DataGridExtractErrorCode, DataGridQuotePolicy,
|
||||
ExtractContext,
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use std::io::Write;
|
||||
|
||||
const MAX_SEPARATOR_CHARACTERS: usize = 8;
|
||||
const MAX_NULL_TEXT_CHARACTERS: usize = 64;
|
||||
|
||||
/// A single DSV data cell. NULL is carried as structured state (not pre-flattened
|
||||
/// into the `null_text` string) so the sentinel can be encoded distinctly from a
|
||||
/// string value that happens to equal it.
|
||||
enum DsvCell<'a> {
|
||||
Null,
|
||||
Text(Cow<'a, str>),
|
||||
}
|
||||
|
||||
pub(super) fn write_dsv(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
options: &DataGridDsvOptions,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
validate_dsv_options(options, true)?;
|
||||
let mut first_row = true;
|
||||
if options.include_column_header {
|
||||
write_dsv_row(
|
||||
output,
|
||||
context.selected_columns.iter().map(|column| Cow::Borrowed(column.display_name.as_str())),
|
||||
options,
|
||||
options.include_row_header.then(|| "#".to_string()),
|
||||
)?;
|
||||
first_row = false;
|
||||
}
|
||||
for (row_index, row) in context.request.rows.iter().enumerate() {
|
||||
if !first_row {
|
||||
write_bytes(output, options.row_separator.as_bytes())?;
|
||||
}
|
||||
write_dsv_data_row(
|
||||
output,
|
||||
context.selected_source_indexes.iter().map(|source_index| {
|
||||
let value = &row[*source_index];
|
||||
if value.is_null() {
|
||||
DsvCell::Null
|
||||
} else {
|
||||
DsvCell::Text(value_text(value))
|
||||
}
|
||||
}),
|
||||
options,
|
||||
options.include_row_header.then(|| (row_index + 1).to_string()),
|
||||
)?;
|
||||
first_row = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a row of header strings (column names / row-number header). Headers
|
||||
/// are never NULL, so they use the plain string field writer.
|
||||
fn write_dsv_row<'a>(
|
||||
output: &mut dyn Write,
|
||||
values: impl Iterator<Item = Cow<'a, str>>,
|
||||
options: &DataGridDsvOptions,
|
||||
row_header: Option<String>,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let mut first = true;
|
||||
if let Some(header) = row_header {
|
||||
write_dsv_field(output, &header, options)?;
|
||||
first = false;
|
||||
}
|
||||
for value in values {
|
||||
if !first {
|
||||
write_bytes(output, options.column_separator.as_bytes())?;
|
||||
}
|
||||
write_dsv_field(output, &value, options)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes a row of data cells, preserving NULL as a distinct sentinel.
|
||||
fn write_dsv_data_row<'a>(
|
||||
output: &mut dyn Write,
|
||||
cells: impl Iterator<Item = DsvCell<'a>>,
|
||||
options: &DataGridDsvOptions,
|
||||
row_header: Option<String>,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let mut first = true;
|
||||
if let Some(header) = row_header {
|
||||
write_dsv_field(output, &header, options)?;
|
||||
first = false;
|
||||
}
|
||||
for cell in cells {
|
||||
if !first {
|
||||
write_bytes(output, options.column_separator.as_bytes())?;
|
||||
}
|
||||
write_dsv_data_field(output, &cell, options)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn dsv_needs_quote(value: &str, options: &DataGridDsvOptions) -> bool {
|
||||
value.contains(&options.column_separator)
|
||||
|| value.contains(&options.row_separator)
|
||||
|| value.contains(options.quote)
|
||||
|| value.contains('\r')
|
||||
|| value.contains('\n')
|
||||
}
|
||||
|
||||
fn write_quoted_dsv_field(
|
||||
output: &mut dyn Write,
|
||||
value: &str,
|
||||
options: &DataGridDsvOptions,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let quote = options.quote;
|
||||
let mut quote_buffer = [0; 4];
|
||||
let quote_text = quote.encode_utf8(&mut quote_buffer);
|
||||
write_bytes(output, quote_text.as_bytes())?;
|
||||
let mut segments = value.split(quote);
|
||||
if let Some(first) = segments.next() {
|
||||
write_bytes(output, first.as_bytes())?;
|
||||
}
|
||||
for segment in segments {
|
||||
write_bytes(output, quote_text.as_bytes())?;
|
||||
write_bytes(output, quote_text.as_bytes())?;
|
||||
write_bytes(output, segment.as_bytes())?;
|
||||
}
|
||||
write_bytes(output, quote_text.as_bytes())
|
||||
}
|
||||
|
||||
/// Encodes a header (non-data) string field. Quotes per policy; a header equal
|
||||
/// to the NULL sentinel is NOT special-cased because headers are not data.
|
||||
fn write_dsv_field(
|
||||
output: &mut dyn Write,
|
||||
value: &str,
|
||||
options: &DataGridDsvOptions,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let should_quote = match options.quote_policy {
|
||||
DataGridQuotePolicy::Always => true,
|
||||
DataGridQuotePolicy::Never => false,
|
||||
DataGridQuotePolicy::Minimal => dsv_needs_quote(value, options),
|
||||
};
|
||||
if !should_quote {
|
||||
return write_bytes(output, value.as_bytes());
|
||||
}
|
||||
write_quoted_dsv_field(output, value, options)
|
||||
}
|
||||
|
||||
/// Encodes a data cell. NULL is always written as the bare sentinel (never
|
||||
/// quoted/escaped); a string equal to the sentinel is force-quoted in Minimal
|
||||
/// mode so it stays distinguishable from a real NULL. Under Always the string is
|
||||
/// quoted and the bare NULL still differs; under Never the two are inherently
|
||||
/// indistinguishable (documented limitation).
|
||||
fn write_dsv_data_field(
|
||||
output: &mut dyn Write,
|
||||
cell: &DsvCell<'_>,
|
||||
options: &DataGridDsvOptions,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
match cell {
|
||||
DsvCell::Null => write_bytes(output, options.null_text.as_bytes()),
|
||||
DsvCell::Text(value) => {
|
||||
let should_quote = match options.quote_policy {
|
||||
DataGridQuotePolicy::Always => true,
|
||||
DataGridQuotePolicy::Never => false,
|
||||
DataGridQuotePolicy::Minimal => {
|
||||
dsv_needs_quote(value, options) || value.as_ref() == options.null_text.as_str()
|
||||
}
|
||||
};
|
||||
if !should_quote {
|
||||
return write_bytes(output, value.as_bytes());
|
||||
}
|
||||
write_quoted_dsv_field(output, value, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn write_one_row(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
let options = DataGridDsvOptions { column_separator: ",".to_string(), ..context.request.options.dsv.clone() };
|
||||
validate_dsv_options(&options, false)?;
|
||||
let values = context
|
||||
.request
|
||||
.rows
|
||||
.iter()
|
||||
.flat_map(|row| context.selected_source_indexes.iter().map(move |source_index| &row[*source_index]));
|
||||
let mut first = true;
|
||||
for value in values {
|
||||
if !first {
|
||||
write_bytes(output, options.column_separator.as_bytes())?;
|
||||
}
|
||||
let cell = if value.is_null() { DsvCell::Null } else { DsvCell::Text(value_text(value)) };
|
||||
write_dsv_data_field(output, &cell, &options)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_dsv_options(options: &DataGridDsvOptions, uses_row_separator: bool) -> Result<(), DataGridExtractError> {
|
||||
if options.column_separator.is_empty() || (uses_row_separator && options.row_separator.is_empty()) {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidDsvConfiguration,
|
||||
"DSV column and row separators must not be empty.",
|
||||
));
|
||||
}
|
||||
if options.column_separator.chars().count() > MAX_SEPARATOR_CHARACTERS
|
||||
|| options.row_separator.chars().count() > MAX_SEPARATOR_CHARACTERS
|
||||
|| options.null_text.chars().count() > MAX_NULL_TEXT_CHARACTERS
|
||||
{
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidDsvConfiguration,
|
||||
"DSV separators may contain at most 8 characters and NULL text at most 64 characters.",
|
||||
));
|
||||
}
|
||||
if uses_row_separator
|
||||
&& (options.column_separator.contains(&options.row_separator)
|
||||
|| options.row_separator.contains(&options.column_separator))
|
||||
{
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidDsvConfiguration,
|
||||
"DSV column and row separators must not overlap.",
|
||||
));
|
||||
}
|
||||
if options.quote.is_control()
|
||||
|| options.column_separator.contains(options.quote)
|
||||
|| (uses_row_separator && options.row_separator.contains(options.quote))
|
||||
{
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidDsvConfiguration,
|
||||
"DSV quote must be a non-control character that is not part of a separator.",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
use super::{value_text, write_bytes, DataGridExtractError, ExtractContext};
|
||||
use std::io::Write;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
pub(super) fn write_markdown(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
let headers = context.selected_columns.iter().map(|column| markdown_text(&column.display_name));
|
||||
write_bytes(output, format!("| {} |\n", headers.collect::<Vec<_>>().join(" | ")).as_bytes())?;
|
||||
write_bytes(
|
||||
output,
|
||||
format!("| {} |", context.selected_columns.iter().map(|_| "---").collect::<Vec<_>>().join(" | ")).as_bytes(),
|
||||
)?;
|
||||
for row in &context.request.rows {
|
||||
write_bytes(output, b"\n")?;
|
||||
let values =
|
||||
context.selected_source_indexes.iter().map(|source_index| markdown_text(&value_text(&row[*source_index])));
|
||||
write_bytes(output, format!("| {} |", values.collect::<Vec<_>>().join(" | ")).as_bytes())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_html(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"<table>\n <thead><tr>")?;
|
||||
for column in &context.selected_columns {
|
||||
write_bytes(output, format!("<th>{}</th>", html_text(&column.display_name)).as_bytes())?;
|
||||
}
|
||||
write_bytes(output, b"</tr></thead>\n <tbody>")?;
|
||||
for row in &context.request.rows {
|
||||
write_bytes(output, b"\n <tr>")?;
|
||||
for source_index in &context.selected_source_indexes {
|
||||
let value = &row[*source_index];
|
||||
write_bytes(output, format!("<td>{}</td>", html_text(&value_text(value))).as_bytes())?;
|
||||
}
|
||||
write_bytes(output, b"</tr>")?;
|
||||
}
|
||||
write_bytes(output, b"\n </tbody>\n</table>")
|
||||
}
|
||||
|
||||
pub(super) fn write_xml(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rows>")?;
|
||||
for row in &context.request.rows {
|
||||
write_bytes(output, b"\n <row>")?;
|
||||
for (column, source_index) in context.selected_columns.iter().zip(&context.selected_source_indexes) {
|
||||
let value = &row[*source_index];
|
||||
let name = html_text(&column.display_name);
|
||||
if value.is_null() {
|
||||
write_bytes(output, format!("\n <column name=\"{name}\" null=\"true\" />").as_bytes())?;
|
||||
} else {
|
||||
write_bytes(
|
||||
output,
|
||||
format!("\n <column name=\"{name}\">{}</column>", html_text(&value_text(value))).as_bytes(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
write_bytes(output, b"\n </row>")?;
|
||||
}
|
||||
write_bytes(output, b"\n</rows>")
|
||||
}
|
||||
|
||||
pub(super) fn write_pretty(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
let widths = context
|
||||
.selected_columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, column)| {
|
||||
context
|
||||
.request
|
||||
.rows
|
||||
.iter()
|
||||
.map(|row| value_text(&row[context.selected_source_indexes[index]]).replace(['\r', '\n'], " ").width())
|
||||
.fold(column.display_name.width(), usize::max)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
write_pretty_border(output, &widths)?;
|
||||
write_pretty_row(output, context.selected_columns.iter().map(|column| column.display_name.as_str()), &widths)?;
|
||||
write_pretty_border(output, &widths)?;
|
||||
for row in &context.request.rows {
|
||||
let values = context
|
||||
.selected_source_indexes
|
||||
.iter()
|
||||
.map(|source_index| value_text(&row[*source_index]).replace(['\r', '\n'], " "));
|
||||
write_pretty_owned_row(output, values, &widths)?;
|
||||
}
|
||||
write_pretty_border(output, &widths)
|
||||
}
|
||||
|
||||
fn write_pretty_owned_row(
|
||||
output: &mut dyn Write,
|
||||
values: impl Iterator<Item = String>,
|
||||
widths: &[usize],
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"|")?;
|
||||
for (index, value) in values.enumerate() {
|
||||
let padding = widths.get(index).copied().unwrap_or_default().saturating_sub(value.width());
|
||||
write_bytes(output, b" ")?;
|
||||
write_bytes(output, value.as_bytes())?;
|
||||
write_repeated_byte(output, b' ', padding)?;
|
||||
write_bytes(output, b" |")?;
|
||||
}
|
||||
write_bytes(output, b"\n")
|
||||
}
|
||||
|
||||
fn write_pretty_border(output: &mut dyn Write, widths: &[usize]) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"+")?;
|
||||
for width in widths {
|
||||
write_repeated_byte(output, b'-', width.saturating_add(2))?;
|
||||
write_bytes(output, b"+")?;
|
||||
}
|
||||
write_bytes(output, b"\n")
|
||||
}
|
||||
|
||||
fn write_pretty_row<'a>(
|
||||
output: &mut dyn Write,
|
||||
values: impl Iterator<Item = &'a str>,
|
||||
widths: &[usize],
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"|")?;
|
||||
for (index, value) in values.enumerate() {
|
||||
let padding = widths.get(index).copied().unwrap_or_default().saturating_sub(value.width());
|
||||
write_bytes(output, b" ")?;
|
||||
write_bytes(output, value.as_bytes())?;
|
||||
write_repeated_byte(output, b' ', padding)?;
|
||||
write_bytes(output, b" |")?;
|
||||
}
|
||||
write_bytes(output, b"\n")
|
||||
}
|
||||
|
||||
fn write_repeated_byte(output: &mut dyn Write, byte: u8, mut count: usize) -> Result<(), DataGridExtractError> {
|
||||
let mut buffer = [0u8; 1024];
|
||||
buffer.fill(byte);
|
||||
while count > 0 {
|
||||
let chunk_size = count.min(buffer.len());
|
||||
write_bytes(output, &buffer[..chunk_size])?;
|
||||
count -= chunk_size;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn markdown_text(value: &str) -> String {
|
||||
value.replace('\\', "\\\\").replace('|', "\\|").replace(['\r', '\n'], "<br>")
|
||||
}
|
||||
|
||||
fn html_text(value: &str) -> String {
|
||||
value.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """).replace('\'', "'")
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
use super::{
|
||||
write_bytes, DataGridExtractColumn, DataGridExtractError, DataGridExtractErrorCode, DataGridExtractWarning,
|
||||
DataGridExtractWarningCode, ExtractContext,
|
||||
};
|
||||
use serde::ser::{SerializeMap, SerializeSeq};
|
||||
use serde::{Serialize, Serializer};
|
||||
use std::io::Write;
|
||||
|
||||
pub(super) fn write_json(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<Vec<DataGridExtractWarning>, DataGridExtractError> {
|
||||
let (names, warnings) =
|
||||
unique_json_names(&context.selected_columns, context.request.options.json.camel_case_field_names);
|
||||
let rows =
|
||||
JsonRows { names: &names, source_indexes: &context.selected_source_indexes, rows: &context.request.rows };
|
||||
let result = if context.request.options.json.pretty {
|
||||
serde_json::to_writer_pretty(output, &rows)
|
||||
} else {
|
||||
serde_json::to_writer(output, &rows)
|
||||
};
|
||||
result.map_err(|error| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::EncodingFailed,
|
||||
format!("Failed to encode JSON extractor output: {error}"),
|
||||
)
|
||||
})?;
|
||||
Ok(warnings)
|
||||
}
|
||||
|
||||
pub(super) fn write_json_lines(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<Vec<DataGridExtractWarning>, DataGridExtractError> {
|
||||
let (names, warnings) =
|
||||
unique_json_names(&context.selected_columns, context.request.options.json.camel_case_field_names);
|
||||
for (index, row) in context.request.rows.iter().enumerate() {
|
||||
if index > 0 {
|
||||
write_bytes(output, b"\n")?;
|
||||
}
|
||||
serde_json::to_writer(
|
||||
&mut *output,
|
||||
&JsonRow { names: &names, source_indexes: &context.selected_source_indexes, values: row },
|
||||
)
|
||||
.map_err(|error| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::EncodingFailed,
|
||||
format!("Failed to encode JSON Lines extractor output: {error}"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
Ok(warnings)
|
||||
}
|
||||
|
||||
struct JsonRows<'a, 'value> {
|
||||
names: &'a [String],
|
||||
source_indexes: &'a [usize],
|
||||
rows: &'value [Vec<serde_json::Value>],
|
||||
}
|
||||
|
||||
impl Serialize for JsonRows<'_, '_> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut sequence = serializer.serialize_seq(Some(self.rows.len()))?;
|
||||
for row in self.rows {
|
||||
sequence.serialize_element(&JsonRow {
|
||||
names: self.names,
|
||||
source_indexes: self.source_indexes,
|
||||
values: row,
|
||||
})?;
|
||||
}
|
||||
sequence.end()
|
||||
}
|
||||
}
|
||||
|
||||
struct JsonRow<'a, 'value> {
|
||||
names: &'a [String],
|
||||
source_indexes: &'a [usize],
|
||||
values: &'value [serde_json::Value],
|
||||
}
|
||||
|
||||
impl Serialize for JsonRow<'_, '_> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut map = serializer.serialize_map(Some(self.names.len()))?;
|
||||
for (name, source_index) in self.names.iter().zip(self.source_indexes) {
|
||||
map.serialize_entry(name, &self.values[*source_index])?;
|
||||
}
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
|
||||
fn unique_json_names(
|
||||
columns: &[&DataGridExtractColumn],
|
||||
camel_case_field_names: bool,
|
||||
) -> (Vec<String>, Vec<DataGridExtractWarning>) {
|
||||
let base_names = columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
if camel_case_field_names {
|
||||
lower_camel_json_name(&column.display_name)
|
||||
} else {
|
||||
column.display_name.clone()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let reserved = base_names.iter().map(String::as_str).collect::<std::collections::HashSet<_>>();
|
||||
let mut emitted = std::collections::HashSet::<String>::new();
|
||||
let mut suffixes = std::collections::HashMap::<String, usize>::new();
|
||||
let mut duplicate = false;
|
||||
let names = base_names
|
||||
.iter()
|
||||
.map(|base| {
|
||||
let base = base.clone();
|
||||
if emitted.insert(base.clone()) {
|
||||
return base;
|
||||
}
|
||||
duplicate = true;
|
||||
let suffix = suffixes.entry(base.clone()).or_insert(2);
|
||||
loop {
|
||||
let candidate = format!("{base}_{suffix}");
|
||||
*suffix += 1;
|
||||
if !reserved.contains(candidate.as_str()) && emitted.insert(candidate.clone()) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let warnings = if duplicate {
|
||||
vec![DataGridExtractWarning {
|
||||
code: DataGridExtractWarningCode::DuplicateJsonColumnNames,
|
||||
message: "Duplicate JSON column names were suffixed to prevent data loss.".to_string(),
|
||||
}]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
(names, warnings)
|
||||
}
|
||||
|
||||
fn lower_camel_json_name(name: &str) -> String {
|
||||
if !name.contains('_') {
|
||||
return if name.chars().any(char::is_lowercase) { name.to_owned() } else { name.to_lowercase() };
|
||||
}
|
||||
|
||||
let mut words = name.split('_').filter(|word| !word.is_empty());
|
||||
let Some(first) = words.next() else {
|
||||
return name.to_owned();
|
||||
};
|
||||
let mut converted = first.to_lowercase();
|
||||
for word in words {
|
||||
let mut characters = word.chars();
|
||||
if let Some(first_character) = characters.next() {
|
||||
converted.extend(first_character.to_uppercase());
|
||||
converted.extend(characters.flat_map(char::to_lowercase));
|
||||
}
|
||||
}
|
||||
converted
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
use serde_json::Value;
|
||||
use std::borrow::Cow;
|
||||
use std::io::{self, Write};
|
||||
|
||||
mod contract;
|
||||
mod delimited;
|
||||
mod document;
|
||||
mod json;
|
||||
mod raw;
|
||||
mod sql;
|
||||
|
||||
pub use contract::*;
|
||||
use delimited::{write_dsv, write_one_row};
|
||||
use document::{write_html, write_markdown, write_pretty, write_xml};
|
||||
use json::{write_json, write_json_lines};
|
||||
use raw::write_raw;
|
||||
use sql::{write_sql_in_list, write_sql_inserts, write_sql_select, write_sql_updates, write_where_clause};
|
||||
|
||||
const DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES: usize = 32 * 1024 * 1024;
|
||||
const DATA_GRID_EXTRACTOR_MAX_INPUT_BYTES: usize = 64 * 1024 * 1024;
|
||||
const DATA_GRID_EXTRACTOR_MAX_ROWS: usize = 100_000;
|
||||
const DATA_GRID_EXTRACTOR_MAX_COLUMNS: usize = 10_000;
|
||||
const DATA_GRID_EXTRACTOR_MAX_CELLS: usize = 2_000_000;
|
||||
|
||||
struct ExtractContext<'a> {
|
||||
request: &'a DataGridExtractRequest,
|
||||
selected_columns: Vec<&'a DataGridExtractColumn>,
|
||||
selected_source_indexes: Vec<usize>,
|
||||
selected_column_info: Vec<Option<&'a crate::data_grid_sql::DataGridColumnInfo>>,
|
||||
}
|
||||
|
||||
pub fn extract_data_grid_selection(
|
||||
request: DataGridExtractRequest,
|
||||
) -> Result<DataGridExtractResult, DataGridExtractError> {
|
||||
if request.version != DATA_GRID_EXTRACTOR_CONTRACT_VERSION {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::UnsupportedVersion,
|
||||
format!("Unsupported data grid extractor contract version: {}", request.version),
|
||||
));
|
||||
}
|
||||
let context = build_context(&request)?;
|
||||
let mut output = BoundedOutput::new(DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES);
|
||||
let metadata = write_extraction(&context, &mut output);
|
||||
if output.exceeded_limit() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::OutputTooLarge,
|
||||
"Extracted clipboard output exceeds 32 MiB; export the data to a file instead.",
|
||||
));
|
||||
}
|
||||
let metadata = metadata?;
|
||||
let text = String::from_utf8(output.into_bytes()).map_err(|error| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::EncodingFailed,
|
||||
format!("Extractor produced invalid UTF-8: {error}"),
|
||||
)
|
||||
})?;
|
||||
Ok(DataGridExtractResult {
|
||||
text,
|
||||
mime_type: metadata.mime_type.to_owned(),
|
||||
file_extension: metadata.file_extension.to_owned(),
|
||||
row_count: request.rows.len(),
|
||||
column_count: context.selected_columns.len(),
|
||||
omitted_columns: metadata.omitted_columns,
|
||||
warnings: metadata.warnings,
|
||||
})
|
||||
}
|
||||
|
||||
struct BoundedOutput {
|
||||
bytes: Vec<u8>,
|
||||
max_bytes: usize,
|
||||
exceeded_limit: bool,
|
||||
}
|
||||
|
||||
impl BoundedOutput {
|
||||
fn new(max_bytes: usize) -> Self {
|
||||
Self { bytes: Vec::new(), max_bytes, exceeded_limit: false }
|
||||
}
|
||||
|
||||
fn exceeded_limit(&self) -> bool {
|
||||
self.exceeded_limit
|
||||
}
|
||||
|
||||
fn into_bytes(self) -> Vec<u8> {
|
||||
self.bytes
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for BoundedOutput {
|
||||
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
|
||||
if buffer.len() > self.max_bytes.saturating_sub(self.bytes.len()) {
|
||||
self.exceeded_limit = true;
|
||||
return Err(io::Error::other("data grid extractor output limit exceeded"));
|
||||
}
|
||||
self.bytes.extend_from_slice(buffer);
|
||||
Ok(buffer.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn build_context(request: &DataGridExtractRequest) -> Result<ExtractContext<'_>, DataGridExtractError> {
|
||||
validate_request_budget(request)?;
|
||||
if request.rows.is_empty() || request.selected_column_indexes.is_empty() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::EmptySelection,
|
||||
"Select at least one data grid cell before extracting data.",
|
||||
));
|
||||
}
|
||||
let mut selected_columns = Vec::with_capacity(request.selected_column_indexes.len());
|
||||
let mut unique_selected_indexes = std::collections::HashSet::with_capacity(request.selected_column_indexes.len());
|
||||
for index in &request.selected_column_indexes {
|
||||
if !unique_selected_indexes.insert(*index) {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnIndex,
|
||||
format!("Selected column index {index} is duplicated."),
|
||||
));
|
||||
}
|
||||
let column = request.columns.get(*index).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnIndex,
|
||||
format!("Selected column index {index} is outside the extractor column list."),
|
||||
)
|
||||
})?;
|
||||
selected_columns.push(column);
|
||||
}
|
||||
let selected_source_indexes = selected_columns.iter().map(|column| column.source_index).collect::<Vec<_>>();
|
||||
for row in &request.rows {
|
||||
for column in &selected_columns {
|
||||
row.get(column.source_index).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnMapping,
|
||||
format!("Column '{}' maps to missing row index {}.", column.display_name, column.source_index),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
let column_info_by_name = request
|
||||
.table_meta
|
||||
.as_ref()
|
||||
.and_then(|table_meta| table_meta.columns.as_ref())
|
||||
.map(|columns| {
|
||||
columns
|
||||
.iter()
|
||||
.map(|info| (normalized_name(info.name.as_str()).to_ascii_uppercase(), info))
|
||||
.collect::<std::collections::HashMap<_, _>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let selected_column_info = selected_columns
|
||||
.iter()
|
||||
.map(|column| {
|
||||
let source_name = column.source_name.as_deref().unwrap_or(&column.display_name);
|
||||
column_info_by_name.get(&normalized_name(source_name).to_ascii_uppercase()).copied()
|
||||
})
|
||||
.collect();
|
||||
Ok(ExtractContext { request, selected_columns, selected_source_indexes, selected_column_info })
|
||||
}
|
||||
|
||||
fn validate_request_budget(request: &DataGridExtractRequest) -> Result<(), DataGridExtractError> {
|
||||
if request.rows.len() > DATA_GRID_EXTRACTOR_MAX_ROWS
|
||||
|| request.columns.len() > DATA_GRID_EXTRACTOR_MAX_COLUMNS
|
||||
|| request.selected_column_indexes.len() > DATA_GRID_EXTRACTOR_MAX_COLUMNS
|
||||
{
|
||||
return Err(input_too_large_error());
|
||||
}
|
||||
|
||||
let mut cell_count = 0usize;
|
||||
let mut estimated_bytes = request
|
||||
.columns
|
||||
.iter()
|
||||
.fold(0usize, |size, column| {
|
||||
size.saturating_add(column.display_name.len())
|
||||
.saturating_add(column.source_name.as_deref().map_or(0, str::len))
|
||||
})
|
||||
.saturating_add(request.options.dsv.column_separator.len())
|
||||
.saturating_add(request.options.dsv.row_separator.len())
|
||||
.saturating_add(request.options.dsv.null_text.len())
|
||||
.saturating_add(estimated_table_meta_bytes(request.table_meta.as_ref()));
|
||||
if estimated_bytes > DATA_GRID_EXTRACTOR_MAX_INPUT_BYTES {
|
||||
return Err(input_too_large_error());
|
||||
}
|
||||
for row in &request.rows {
|
||||
cell_count = cell_count.saturating_add(row.len());
|
||||
if cell_count > DATA_GRID_EXTRACTOR_MAX_CELLS {
|
||||
return Err(input_too_large_error());
|
||||
}
|
||||
for value in row {
|
||||
estimated_bytes = estimated_bytes.saturating_add(estimated_value_bytes(value));
|
||||
if estimated_bytes > DATA_GRID_EXTRACTOR_MAX_INPUT_BYTES {
|
||||
return Err(input_too_large_error());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn estimated_table_meta_bytes(table_meta: Option<&crate::data_grid_sql::DataGridTableMeta>) -> usize {
|
||||
let Some(table_meta) = table_meta else {
|
||||
return 0;
|
||||
};
|
||||
let mut size = table_meta
|
||||
.table_name
|
||||
.len()
|
||||
.saturating_add(table_meta.catalog.as_deref().map_or(0, str::len))
|
||||
.saturating_add(table_meta.database.as_deref().map_or(0, str::len))
|
||||
.saturating_add(table_meta.schema.as_deref().map_or(0, str::len));
|
||||
for primary_key in &table_meta.primary_keys {
|
||||
size = size.saturating_add(primary_key.len());
|
||||
}
|
||||
for column in table_meta.columns.as_deref().unwrap_or_default() {
|
||||
size = size
|
||||
.saturating_add(column.name.len())
|
||||
.saturating_add(column.data_type.len())
|
||||
.saturating_add(column.column_default.as_deref().map_or(0, str::len))
|
||||
.saturating_add(column.extra.as_deref().map_or(0, str::len));
|
||||
}
|
||||
size
|
||||
}
|
||||
|
||||
fn estimated_value_bytes(value: &Value) -> usize {
|
||||
match value {
|
||||
Value::Null => 4,
|
||||
Value::Bool(_) => 5,
|
||||
Value::Number(_) => 32,
|
||||
Value::String(value) => value.len(),
|
||||
Value::Array(values) => values
|
||||
.iter()
|
||||
.fold(2usize, |size, value| size.saturating_add(1).saturating_add(estimated_value_bytes(value))),
|
||||
Value::Object(values) => values.iter().fold(2usize, |size, (key, value)| {
|
||||
size.saturating_add(key.len()).saturating_add(2).saturating_add(estimated_value_bytes(value))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn input_too_large_error() -> DataGridExtractError {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InputTooLarge,
|
||||
"Selected data is too large for clipboard extraction; export the data to a file instead.",
|
||||
)
|
||||
}
|
||||
|
||||
struct WriteMetadata {
|
||||
mime_type: &'static str,
|
||||
file_extension: &'static str,
|
||||
omitted_columns: Vec<String>,
|
||||
warnings: Vec<DataGridExtractWarning>,
|
||||
}
|
||||
|
||||
fn write_extraction(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<WriteMetadata, DataGridExtractError> {
|
||||
let extractor = context.request.extractor;
|
||||
match extractor {
|
||||
DataGridExtractorId::Raw => {
|
||||
if context.request.rows.len() != 1 || context.selected_source_indexes.len() != 1 {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidRawSelection,
|
||||
"The raw extractor supports exactly one selected cell.",
|
||||
));
|
||||
}
|
||||
write_raw(context, output)?;
|
||||
Ok(text_metadata("text/plain", "txt"))
|
||||
}
|
||||
DataGridExtractorId::Tsv | DataGridExtractorId::TsvWithHeaders => {
|
||||
let options = DataGridDsvOptions {
|
||||
column_separator: "\t".to_string(),
|
||||
include_column_header: extractor == DataGridExtractorId::TsvWithHeaders,
|
||||
..context.request.options.dsv.clone()
|
||||
};
|
||||
write_dsv(context, output, &options)?;
|
||||
Ok(text_metadata("text/tab-separated-values", "tsv"))
|
||||
}
|
||||
DataGridExtractorId::Csv | DataGridExtractorId::CsvWithHeaders => {
|
||||
let options = DataGridDsvOptions {
|
||||
column_separator: ",".to_string(),
|
||||
include_column_header: extractor == DataGridExtractorId::CsvWithHeaders,
|
||||
..context.request.options.dsv.clone()
|
||||
};
|
||||
write_dsv(context, output, &options)?;
|
||||
Ok(text_metadata("text/csv", "csv"))
|
||||
}
|
||||
DataGridExtractorId::PipeSeparated => {
|
||||
let options =
|
||||
DataGridDsvOptions { column_separator: "|".to_string(), ..context.request.options.dsv.clone() };
|
||||
write_dsv(context, output, &options)?;
|
||||
Ok(text_metadata("text/plain", "txt"))
|
||||
}
|
||||
DataGridExtractorId::Dsv => {
|
||||
write_dsv(context, output, &context.request.options.dsv)?;
|
||||
Ok(text_metadata("text/plain", "txt"))
|
||||
}
|
||||
DataGridExtractorId::OneRow => {
|
||||
write_one_row(context, output)?;
|
||||
Ok(text_metadata("text/csv", "csv"))
|
||||
}
|
||||
DataGridExtractorId::Json => {
|
||||
let warnings = write_json(context, output)?;
|
||||
Ok(WriteMetadata {
|
||||
mime_type: "application/json",
|
||||
file_extension: "json",
|
||||
omitted_columns: Vec::new(),
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
DataGridExtractorId::JsonLines => {
|
||||
let warnings = write_json_lines(context, output)?;
|
||||
Ok(WriteMetadata {
|
||||
mime_type: "application/x-ndjson",
|
||||
file_extension: "jsonl",
|
||||
omitted_columns: Vec::new(),
|
||||
warnings,
|
||||
})
|
||||
}
|
||||
DataGridExtractorId::SqlInList => {
|
||||
write_sql_in_list(context, output)?;
|
||||
Ok(text_metadata("application/sql", "sql"))
|
||||
}
|
||||
DataGridExtractorId::SqlInserts => write_sql_inserts(context, output),
|
||||
DataGridExtractorId::SqlUpdates => write_sql_updates(context, output),
|
||||
DataGridExtractorId::SqlSelect => {
|
||||
write_sql_select(context, output)?;
|
||||
Ok(text_metadata("application/sql", "sql"))
|
||||
}
|
||||
DataGridExtractorId::WhereClause => {
|
||||
write_where_clause(context, output)?;
|
||||
Ok(text_metadata("application/sql", "sql"))
|
||||
}
|
||||
DataGridExtractorId::Markdown => {
|
||||
write_markdown(context, output)?;
|
||||
Ok(text_metadata("text/markdown", "md"))
|
||||
}
|
||||
DataGridExtractorId::Html => {
|
||||
write_html(context, output)?;
|
||||
Ok(text_metadata("text/html", "html"))
|
||||
}
|
||||
DataGridExtractorId::Xml => {
|
||||
write_xml(context, output)?;
|
||||
Ok(text_metadata("application/xml", "xml"))
|
||||
}
|
||||
DataGridExtractorId::Pretty => {
|
||||
write_pretty(context, output)?;
|
||||
Ok(text_metadata("text/plain", "txt"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn text_metadata(mime_type: &'static str, extension: &'static str) -> WriteMetadata {
|
||||
WriteMetadata { mime_type, file_extension: extension, omitted_columns: Vec::new(), warnings: Vec::new() }
|
||||
}
|
||||
|
||||
fn normalized_name_eq(left: &str, right: &str) -> bool {
|
||||
normalized_name(left).eq_ignore_ascii_case(normalized_name(right))
|
||||
}
|
||||
|
||||
fn normalized_name(name: &str) -> &str {
|
||||
name.trim_matches(|character| matches!(character, '`' | '"' | '[' | ']'))
|
||||
}
|
||||
|
||||
fn value_text(value: &Value) -> Cow<'_, str> {
|
||||
match value {
|
||||
Value::Null => Cow::Borrowed("NULL"),
|
||||
Value::String(value) => Cow::Borrowed(value),
|
||||
Value::Bool(value) => Cow::Owned(value.to_string()),
|
||||
Value::Number(value) => Cow::Owned(value.to_string()),
|
||||
Value::Array(_) | Value::Object(_) => Cow::Owned(value.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_bytes(output: &mut dyn Write, bytes: &[u8]) -> Result<(), DataGridExtractError> {
|
||||
output.write_all(bytes).map_err(|error| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::EncodingFailed,
|
||||
format!("Failed to write extractor output: {error}"),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,22 @@
|
||||
use super::{value_text, write_bytes, DataGridExtractError, ExtractContext};
|
||||
use std::io::Write;
|
||||
|
||||
/// Writes selected cell values without DSV escaping. Tabs and newlines retain
|
||||
/// the selection shape, while NULL cells remain empty for spreadsheet-friendly
|
||||
/// clipboard output.
|
||||
pub(super) fn write_raw(context: &ExtractContext<'_>, output: &mut dyn Write) -> Result<(), DataGridExtractError> {
|
||||
for (row_index, row) in context.request.rows.iter().enumerate() {
|
||||
if row_index > 0 {
|
||||
write_bytes(output, b"\n")?;
|
||||
}
|
||||
for (column_index, source_index) in context.selected_source_indexes.iter().enumerate() {
|
||||
if column_index > 0 {
|
||||
write_bytes(output, b"\t")?;
|
||||
}
|
||||
if !row[*source_index].is_null() {
|
||||
write_bytes(output, value_text(&row[*source_index]).as_bytes())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,466 @@
|
||||
use super::{
|
||||
estimated_value_bytes, normalized_name_eq, write_bytes, DataGridExtractError, DataGridExtractErrorCode,
|
||||
DataGridExtractWarning, DataGridExtractWarningCode, ExtractContext, WriteMetadata,
|
||||
DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES,
|
||||
};
|
||||
use crate::data_grid_sql::{
|
||||
build_column_predicate, build_data_grid_copy_insert_statement, build_data_grid_copy_update_statements,
|
||||
data_grid_qualified_table_name, format_grid_sql_literal_with_identifier_quote, is_auto_generated_column,
|
||||
is_grid_insert_omitted_column, is_non_identity_generated_column, supports_relational_copy_predicates,
|
||||
DataGridCopyInsertStatementOptions, DataGridCopyUpdateStatementOptions, DataGridTableMeta,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::collections::{hash_map::Entry, HashMap, HashSet};
|
||||
use std::io::Write;
|
||||
|
||||
pub(super) fn write_sql_in_list(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
write_bytes(output, b"(")?;
|
||||
let mut seen_rows = HashMap::<Vec<u8>, ()>::new();
|
||||
let mut has_output = false;
|
||||
for row in &context.request.rows {
|
||||
let mut tuple = Vec::new();
|
||||
if context.selected_columns.len() > 1 {
|
||||
tuple.push(b'(');
|
||||
}
|
||||
for (column_index, source_index) in context.selected_source_indexes.iter().enumerate() {
|
||||
if column_index > 0 {
|
||||
tuple.extend_from_slice(b", ");
|
||||
}
|
||||
let value = &row[*source_index];
|
||||
let info = context.selected_column_info[column_index];
|
||||
tuple.extend_from_slice(
|
||||
format_grid_sql_literal_with_identifier_quote(
|
||||
value,
|
||||
context.request.database_type,
|
||||
info,
|
||||
context.request.identifier_quote.as_deref(),
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
}
|
||||
if context.selected_columns.len() > 1 {
|
||||
tuple.push(b')');
|
||||
}
|
||||
if let Entry::Vacant(entry) = seen_rows.entry(tuple) {
|
||||
if has_output {
|
||||
write_bytes(output, b", ")?;
|
||||
}
|
||||
write_bytes(output, entry.key())?;
|
||||
entry.insert(());
|
||||
has_output = true;
|
||||
}
|
||||
}
|
||||
write_bytes(output, b")")
|
||||
}
|
||||
|
||||
pub(super) fn write_sql_inserts(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<WriteMetadata, DataGridExtractError> {
|
||||
ensure_sql_builder_budget(context)?;
|
||||
let data = sql_selected_data(context, false)?;
|
||||
let statement = build_data_grid_copy_insert_statement(DataGridCopyInsertStatementOptions {
|
||||
database_type: context.request.database_type,
|
||||
identifier_quote: context.request.identifier_quote.clone(),
|
||||
table_meta: context.request.table_meta.clone(),
|
||||
columns: data.columns,
|
||||
column_types: Some(data.column_types),
|
||||
source_columns: Some(data.source_columns),
|
||||
rows: data.rows,
|
||||
exclude_primary_keys: context.request.options.sql.exclude_primary_keys_from_insert,
|
||||
include_computed_columns: !context.request.options.sql.skip_computed_columns,
|
||||
insert_mode: context.request.options.sql.insert_mode,
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NoWritableColumns,
|
||||
"No insertable columns remain after applying extractor column rules.",
|
||||
)
|
||||
})?;
|
||||
write_bytes(output, statement.as_bytes())?;
|
||||
Ok(sql_metadata(data.omitted_columns))
|
||||
}
|
||||
|
||||
pub(super) fn write_sql_updates(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<WriteMetadata, DataGridExtractError> {
|
||||
ensure_sql_builder_budget(context)?;
|
||||
let table_meta = context.request.table_meta.as_ref().ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::MissingTableMetadata,
|
||||
"SQL Updates requires table metadata.",
|
||||
)
|
||||
})?;
|
||||
if table_meta.primary_keys.is_empty() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::MissingPrimaryKey,
|
||||
"SQL Updates requires at least one primary key column.",
|
||||
));
|
||||
}
|
||||
let mut data = sql_selected_data(context, true)?;
|
||||
append_missing_primary_keys(context, table_meta, &mut data.columns, &mut data.source_columns, &mut data.rows)?;
|
||||
|
||||
let primary_key_names = table_meta.primary_keys.iter().map(|name| normalize_name(name)).collect::<HashSet<_>>();
|
||||
let writable_count =
|
||||
data.source_columns.iter().flatten().filter(|name| !primary_key_names.contains(&normalize_name(name))).count();
|
||||
if writable_count == 0 {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NoWritableColumns,
|
||||
"Select at least one writable non-primary-key column for SQL Updates.",
|
||||
));
|
||||
}
|
||||
let statements = build_data_grid_copy_update_statements(DataGridCopyUpdateStatementOptions {
|
||||
database_type: context.request.database_type,
|
||||
identifier_quote: context.request.identifier_quote.clone(),
|
||||
table_meta: table_meta.clone(),
|
||||
columns: data.columns,
|
||||
source_columns: Some(data.source_columns),
|
||||
rows: data.rows,
|
||||
});
|
||||
if statements.len() != context.request.rows.len() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NullPrimaryKey,
|
||||
"One or more selected rows could not produce SQL Updates.",
|
||||
));
|
||||
}
|
||||
write_bytes(output, statements.join("\n").as_bytes())?;
|
||||
Ok(sql_metadata(data.omitted_columns))
|
||||
}
|
||||
|
||||
fn ensure_sql_builder_budget(context: &ExtractContext<'_>) -> Result<(), DataGridExtractError> {
|
||||
let value_bytes = context
|
||||
.request
|
||||
.rows
|
||||
.iter()
|
||||
.flatten()
|
||||
.fold(0usize, |size, value| size.saturating_add(estimated_value_bytes(value)));
|
||||
let identifier_bytes = context
|
||||
.request
|
||||
.table_meta
|
||||
.as_ref()
|
||||
.map_or(0, |table_meta| {
|
||||
table_meta
|
||||
.table_name
|
||||
.len()
|
||||
.saturating_add(table_meta.catalog.as_deref().map_or(0, str::len))
|
||||
.saturating_add(table_meta.database.as_deref().map_or(0, str::len))
|
||||
.saturating_add(table_meta.schema.as_deref().map_or(0, str::len))
|
||||
.saturating_add(table_meta.primary_keys.iter().fold(0usize, |size, key| size.saturating_add(key.len())))
|
||||
})
|
||||
.saturating_add(context.selected_columns.iter().fold(0usize, |size, column| {
|
||||
size.saturating_add(column.display_name.len())
|
||||
.saturating_add(column.source_name.as_deref().map_or(0, str::len))
|
||||
}));
|
||||
// WHERE-clause output (and SELECT built from a cell selection, which reuses
|
||||
// the same predicate builder) repeats every selected column's identifier
|
||||
// once per row via OR'd row groups, just like SqlUpdates/row-by-row insert.
|
||||
let repeats_identifiers_per_row = context.request.extractor == super::DataGridExtractorId::SqlUpdates
|
||||
|| context.request.options.sql.insert_mode == crate::data_grid_sql::DataGridCopyInsertMode::RowByRow
|
||||
|| context.request.extractor == super::DataGridExtractorId::WhereClause
|
||||
|| (context.request.extractor == super::DataGridExtractorId::SqlSelect
|
||||
&& context.request.selection_kind == super::DataGridSelectionKind::Cells);
|
||||
let statement_overhead = identifier_bytes
|
||||
.saturating_mul(4)
|
||||
.saturating_add(256)
|
||||
.saturating_mul(if repeats_identifiers_per_row { context.request.rows.len() } else { 1 });
|
||||
let estimated_bytes = value_bytes.saturating_mul(6).saturating_add(statement_overhead);
|
||||
if estimated_bytes > DATA_GRID_EXTRACTOR_MAX_OUTPUT_BYTES {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::OutputTooLarge,
|
||||
"Estimated SQL clipboard output exceeds 32 MiB; export the data to a file instead.",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn append_missing_primary_keys(
|
||||
context: &ExtractContext<'_>,
|
||||
table_meta: &DataGridTableMeta,
|
||||
columns: &mut Vec<String>,
|
||||
source_columns: &mut Vec<Option<String>>,
|
||||
rows: &mut [Vec<Value>],
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
for primary_key in &table_meta.primary_keys {
|
||||
if source_columns.iter().flatten().any(|column| normalized_name_eq(column, primary_key)) {
|
||||
continue;
|
||||
}
|
||||
let column = context
|
||||
.request
|
||||
.columns
|
||||
.iter()
|
||||
.find(|column| {
|
||||
normalized_name_eq(column.source_name.as_deref().unwrap_or(&column.display_name), primary_key)
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::MissingPrimaryKey,
|
||||
format!("Primary key column '{primary_key}' is not present in the result set."),
|
||||
)
|
||||
})?;
|
||||
if context.request.rows.iter().any(|row| match row.get(column.source_index) {
|
||||
Some(value) => value.is_null(),
|
||||
None => true,
|
||||
}) {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NullPrimaryKey,
|
||||
format!("Primary key column '{primary_key}' contains a NULL value."),
|
||||
));
|
||||
}
|
||||
columns.push(column.display_name.clone());
|
||||
source_columns.push(Some(primary_key.clone()));
|
||||
for (row_index, row) in context.request.rows.iter().enumerate() {
|
||||
let value = row.get(column.source_index).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnMapping,
|
||||
format!("Primary key column '{primary_key}' maps outside row {row_index}."),
|
||||
)
|
||||
})?;
|
||||
rows[row_index].push(value.clone());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct SqlSelectedData {
|
||||
columns: Vec<String>,
|
||||
source_columns: Vec<Option<String>>,
|
||||
column_types: Vec<Option<String>>,
|
||||
rows: Vec<Vec<Value>>,
|
||||
omitted_columns: Vec<String>,
|
||||
}
|
||||
|
||||
fn sql_selected_data(context: &ExtractContext<'_>, for_update: bool) -> Result<SqlSelectedData, DataGridExtractError> {
|
||||
let mut included = Vec::new();
|
||||
let mut omitted = Vec::new();
|
||||
let mut included_indexes = Vec::new();
|
||||
for (index, column) in context.selected_columns.iter().enumerate() {
|
||||
let source_name = column.source_name.as_deref().unwrap_or(&column.display_name);
|
||||
let info = context.selected_column_info[index];
|
||||
let is_primary_key = context.request.table_meta.as_ref().is_some_and(|meta| {
|
||||
meta.primary_keys.iter().any(|primary_key| normalized_name_eq(primary_key, source_name))
|
||||
});
|
||||
let omit = (!for_update
|
||||
&& is_grid_insert_omitted_column(
|
||||
context.request.database_type,
|
||||
info,
|
||||
Some(source_name),
|
||||
!context.request.options.sql.skip_computed_columns,
|
||||
))
|
||||
|| (for_update
|
||||
&& context.request.options.sql.skip_computed_columns
|
||||
&& info.is_some_and(|column| is_non_identity_generated_column(Some(column))))
|
||||
|| (context.request.options.sql.skip_generated_columns
|
||||
&& info.is_some_and(is_auto_generated_column)
|
||||
&& !is_primary_key)
|
||||
|| (for_update && is_primary_key)
|
||||
// Exclude only auto-generated primary keys; manually-assigned key
|
||||
// columns must survive "without primary keys" INSERT copies
|
||||
// (composite keys would otherwise lose NOT NULL data).
|
||||
|| (!for_update
|
||||
&& context.request.options.sql.exclude_primary_keys_from_insert
|
||||
&& is_primary_key
|
||||
&& info.is_some_and(is_auto_generated_column));
|
||||
if omit {
|
||||
omitted.push(source_name.to_string());
|
||||
} else {
|
||||
included.push(*column);
|
||||
included_indexes.push(index);
|
||||
}
|
||||
}
|
||||
if included.is_empty() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NoWritableColumns,
|
||||
"No writable columns remain after applying extractor column rules.",
|
||||
));
|
||||
}
|
||||
let columns = included.iter().map(|column| column.display_name.clone()).collect();
|
||||
let source_columns = included
|
||||
.iter()
|
||||
.map(|column| Some(column.source_name.clone().unwrap_or_else(|| column.display_name.clone())))
|
||||
.collect();
|
||||
let column_types = included_indexes
|
||||
.iter()
|
||||
.map(|index| context.selected_column_info[*index].map(|info| info.data_type.clone()))
|
||||
.collect();
|
||||
let rows = context
|
||||
.request
|
||||
.rows
|
||||
.iter()
|
||||
.map(|row| included.iter().map(|column| row[column.source_index].clone()).collect())
|
||||
.collect();
|
||||
Ok(SqlSelectedData { columns, source_columns, column_types, rows, omitted_columns: omitted })
|
||||
}
|
||||
|
||||
pub(super) fn write_where_clause(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
// WHERE-clause extraction emits relational SQL predicates and is not meaningful
|
||||
// for graph/document/time-series stores. Unlike `write_sql_updates`, this
|
||||
// intentionally does NOT auto-append primary keys: the predicate reflects the
|
||||
// exact cells the user selected.
|
||||
if !supports_relational_copy_predicates(context.request.database_type) {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::UnsupportedDatabase,
|
||||
"WHERE-clause extraction is not supported for this database type.",
|
||||
));
|
||||
}
|
||||
for (row_index, row) in context.request.rows.iter().enumerate() {
|
||||
if row_index > 0 {
|
||||
write_bytes(output, b" OR ")?;
|
||||
}
|
||||
if context.request.rows.len() > 1 {
|
||||
write_bytes(output, b"(")?;
|
||||
}
|
||||
for (column_index, source_index) in context.selected_source_indexes.iter().enumerate() {
|
||||
if column_index > 0 {
|
||||
write_bytes(output, b" AND ")?;
|
||||
}
|
||||
let column = context.selected_columns[column_index];
|
||||
let value = &row[*source_index];
|
||||
let source_name = column.source_name.as_deref().unwrap_or(&column.display_name);
|
||||
// Reuse the UPDATE predicate builder so NULL -> IS NULL, MySQL BINARY,
|
||||
// and MySQL JSON CAST(...) stay consistent with copy-as-UPDATE.
|
||||
let predicate = build_column_predicate(
|
||||
context.request.database_type,
|
||||
source_name,
|
||||
value,
|
||||
context.selected_column_info[column_index],
|
||||
true,
|
||||
context.request.identifier_quote.as_deref(),
|
||||
);
|
||||
write_bytes(output, predicate.as_bytes())?;
|
||||
}
|
||||
if context.request.rows.len() > 1 {
|
||||
write_bytes(output, b")")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn write_sql_select(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
ensure_sql_builder_budget(context)?;
|
||||
if !supports_relational_copy_predicates(context.request.database_type) {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::UnsupportedDatabase,
|
||||
"SELECT extraction is not supported for this database type.",
|
||||
));
|
||||
}
|
||||
let table_meta =
|
||||
context.request.table_meta.as_ref().filter(|meta| !meta.table_name.trim().is_empty()).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::MissingTableMetadata,
|
||||
"SELECT extraction requires one resolved table target.",
|
||||
)
|
||||
})?;
|
||||
if context.request.rows.is_empty()
|
||||
|| matches!(context.request.selection_kind, super::DataGridSelectionKind::Columns)
|
||||
|| (context.request.selection_kind == super::DataGridSelectionKind::Rows && context.request.rows.len() != 1)
|
||||
{
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidSelectSelection,
|
||||
"SELECT extraction supports cell selections or exactly one selected row.",
|
||||
));
|
||||
}
|
||||
|
||||
let table = data_grid_qualified_table_name(
|
||||
context.request.database_type,
|
||||
table_meta.catalog.as_deref(),
|
||||
table_meta.schema.as_deref(),
|
||||
table_meta.database.as_deref(),
|
||||
&table_meta.table_name,
|
||||
context.request.identifier_quote.as_deref(),
|
||||
);
|
||||
|
||||
if context.request.selection_kind == super::DataGridSelectionKind::Cells {
|
||||
// A cell-selection SELECT reuses the WHERE-clause predicate builder so a
|
||||
// multi-cell selection (same-row columns AND'd, multi-row selections
|
||||
// OR'd) produces the same predicate as "Copy as WHERE clause" for the
|
||||
// identical selection — including its fallback to display_name when a
|
||||
// column has no resolved source_name. build_context() already rejects
|
||||
// an empty column selection before this function runs.
|
||||
write_bytes(output, format!("SELECT * FROM {table} WHERE ").as_bytes())?;
|
||||
write_where_clause(context, output)?;
|
||||
return write_bytes(output, b";");
|
||||
}
|
||||
|
||||
// Only the single-row identity path (row-checkbox selection) reaches here;
|
||||
// cell selections returned above via the write_where_clause path.
|
||||
let row = &context.request.rows[0];
|
||||
let identity = table_meta
|
||||
.primary_keys
|
||||
.iter()
|
||||
.map(|primary_key| {
|
||||
context
|
||||
.request
|
||||
.columns
|
||||
.iter()
|
||||
.find(|column| {
|
||||
normalized_name_eq(column.source_name.as_deref().unwrap_or(&column.display_name), primary_key)
|
||||
})
|
||||
.filter(|column| row.get(column.source_index).is_some_and(|value| !value.is_null()))
|
||||
})
|
||||
.collect::<Option<Vec<_>>>();
|
||||
let predicate_columns =
|
||||
identity.filter(|columns| !columns.is_empty()).unwrap_or_else(|| context.request.columns.iter().collect());
|
||||
if predicate_columns.is_empty() {
|
||||
return Err(DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnMapping,
|
||||
"SELECT extraction has no source columns for its predicate.",
|
||||
));
|
||||
}
|
||||
|
||||
let mut predicates = Vec::with_capacity(predicate_columns.len());
|
||||
for column in predicate_columns {
|
||||
let value = row.get(column.source_index).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnMapping,
|
||||
format!("Column '{}' maps to missing row index {}.", column.display_name, column.source_index),
|
||||
)
|
||||
})?;
|
||||
let source_name = column.source_name.as_deref().filter(|name| !name.trim().is_empty()).ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::InvalidColumnMapping,
|
||||
format!("Column '{}' has no resolved source-column mapping.", column.display_name),
|
||||
)
|
||||
})?;
|
||||
let column_info = table_meta
|
||||
.columns
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.find(|info| normalized_name_eq(&info.name, source_name));
|
||||
predicates.push(build_column_predicate(
|
||||
context.request.database_type,
|
||||
source_name,
|
||||
value,
|
||||
column_info,
|
||||
true,
|
||||
context.request.identifier_quote.as_deref(),
|
||||
));
|
||||
}
|
||||
write_bytes(output, format!("SELECT * FROM {table} WHERE {};", predicates.join(" AND ")).as_bytes())
|
||||
}
|
||||
|
||||
fn sql_metadata(omitted_columns: Vec<String>) -> WriteMetadata {
|
||||
let warnings = if omitted_columns.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
vec![DataGridExtractWarning {
|
||||
code: DataGridExtractWarningCode::OmittedColumns,
|
||||
message: format!("Omitted columns: {}", omitted_columns.join(", ")),
|
||||
}]
|
||||
};
|
||||
WriteMetadata { mime_type: "application/sql", file_extension: "sql", omitted_columns, warnings }
|
||||
}
|
||||
|
||||
fn normalize_name(name: &str) -> String {
|
||||
name.trim_matches(|character| matches!(character, '`' | '"' | '[' | ']')).to_ascii_uppercase()
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user