refactor(workspace): split SQL and database driver crates
This commit is contained in:
180 files changed
+1422
-538
No files matched your search
@@ -0,0 +1,242 @@
|
||||
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,
|
||||
pub include_database_name: 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,
|
||||
include_database_name: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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,250 @@
|
||||
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,
|
||||
quote_char_triggers_quoting: bool,
|
||||
) -> 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()),
|
||||
quote_char_triggers_quoting,
|
||||
)?;
|
||||
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()),
|
||||
quote_char_triggers_quoting,
|
||||
)?;
|
||||
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>,
|
||||
quote_char_triggers_quoting: bool,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let mut first = true;
|
||||
if let Some(header) = row_header {
|
||||
write_dsv_field(output, &header, options, quote_char_triggers_quoting)?;
|
||||
first = false;
|
||||
}
|
||||
for value in values {
|
||||
if !first {
|
||||
write_bytes(output, options.column_separator.as_bytes())?;
|
||||
}
|
||||
write_dsv_field(output, &value, options, quote_char_triggers_quoting)?;
|
||||
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>,
|
||||
quote_char_triggers_quoting: bool,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let mut first = true;
|
||||
if let Some(header) = row_header {
|
||||
write_dsv_field(output, &header, options, quote_char_triggers_quoting)?;
|
||||
first = false;
|
||||
}
|
||||
for cell in cells {
|
||||
if !first {
|
||||
write_bytes(output, options.column_separator.as_bytes())?;
|
||||
}
|
||||
write_dsv_data_field(output, &cell, options, quote_char_triggers_quoting)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Whether a value must be wrapped in quotes. A value is always quoted when it
|
||||
/// contains a separator or a line break, because those would otherwise corrupt
|
||||
/// the record shape. Whether a bare quote character forces quoting is
|
||||
/// format-dependent: RFC4180 formats (CSV/DSV/one-row) must escape an embedded
|
||||
/// quote, but plain delimited formats such as TSV are parsed by splitting on the
|
||||
/// separator alone (there is no quote state machine on paste-back), so a value
|
||||
/// that merely contains `"` must be emitted verbatim rather than wrapped and
|
||||
/// doubled.
|
||||
fn dsv_needs_quote(value: &str, options: &DataGridDsvOptions, quote_char_triggers_quoting: bool) -> bool {
|
||||
value.contains(&options.column_separator)
|
||||
|| value.contains(&options.row_separator)
|
||||
|| (quote_char_triggers_quoting && 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,
|
||||
quote_char_triggers_quoting: bool,
|
||||
) -> Result<(), DataGridExtractError> {
|
||||
let should_quote = match options.quote_policy {
|
||||
DataGridQuotePolicy::Always => true,
|
||||
DataGridQuotePolicy::Never => false,
|
||||
DataGridQuotePolicy::Minimal => dsv_needs_quote(value, options, quote_char_triggers_quoting),
|
||||
};
|
||||
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,
|
||||
quote_char_triggers_quoting: bool,
|
||||
) -> 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, quote_char_triggers_quoting)
|
||||
|| 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)) };
|
||||
// A one-row extraction is an RFC4180 CSV record, so embedded quotes must
|
||||
// be escaped.
|
||||
write_dsv_data_field(output, &cell, &options, true)?;
|
||||
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,383 @@
|
||||
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()
|
||||
};
|
||||
// TSV is split on the tab separator with no quote state machine, so a
|
||||
// value that merely contains a double quote must not be escaped.
|
||||
write_dsv(context, output, &options, false)?;
|
||||
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, true)?;
|
||||
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, false)?;
|
||||
Ok(text_metadata("text/plain", "txt"))
|
||||
}
|
||||
DataGridExtractorId::Dsv => {
|
||||
write_dsv(context, output, &context.request.options.dsv, true)?;
|
||||
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,469 @@
|
||||
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_generated_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,
|
||||
include_database_name: context.request.options.sql.include_database_name,
|
||||
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,
|
||||
include_database_name: context.request.options.sql.include_database_name,
|
||||
});
|
||||
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_generated_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(),
|
||||
context.request.options.sql.include_database_name,
|
||||
);
|
||||
|
||||
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
@@ -0,0 +1,425 @@
|
||||
use serde_json::Value;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::*;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn uses_iotdb_table_model_save(options: &DataGridSaveStatementOptions) -> bool {
|
||||
options.database_type == Some(DatabaseType::Iotdb) && !options.table_meta.primary_keys.is_empty()
|
||||
}
|
||||
|
||||
pub(super) fn build_iotdb_data_grid_save_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let save_columns = effective_columns(options);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_iotdb_changed_row_insert(options, &save_columns, row, changes, true) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_iotdb_delete_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for row in &options.new_rows {
|
||||
if let Some(statement) = build_iotdb_full_row_insert(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn build_iotdb_data_grid_rollback_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let save_columns = effective_columns(options);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for row in &options.new_rows {
|
||||
if let Some(statement) = build_iotdb_delete_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_iotdb_full_row_insert(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
let requires_full_restore = changes.iter().any(|(column_index, _)| {
|
||||
let value = row.get(*column_index).unwrap_or(&Value::Null);
|
||||
let column = save_columns.get(*column_index).and_then(Option::as_deref);
|
||||
value.is_null()
|
||||
|| column.is_some_and(|column| {
|
||||
empty_string_saves_as_null(value, column_info_for(iotdb_column_info(options), column))
|
||||
})
|
||||
});
|
||||
if requires_full_restore {
|
||||
if let Some(statement) = build_iotdb_delete_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
if let Some(statement) = build_iotdb_full_row_insert(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
} else if let Some(statement) = build_iotdb_changed_row_insert(options, &save_columns, row, changes, false) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn validate_iotdb_existing_rows(options: &DataGridSaveStatementOptions) -> Option<String> {
|
||||
if !uses_iotdb_table_model_save(options) || options.dirty_rows.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let save_columns = effective_columns(options);
|
||||
let primary_keys =
|
||||
options.table_meta.primary_keys.iter().map(|column| normalize_column_name(column)).collect::<HashSet<_>>();
|
||||
for (_, changes) in &options.dirty_rows {
|
||||
for (column_index, value) in changes {
|
||||
let Some(column) = save_columns.get(*column_index).and_then(Option::as_deref) else {
|
||||
continue;
|
||||
};
|
||||
if primary_keys.contains(&normalize_column_name(column)) {
|
||||
return Some("IoTDB row identifier columns (time and TAG) cannot be edited.".to_string());
|
||||
}
|
||||
if value.is_null() || empty_string_saves_as_null(value, column_info_for(iotdb_column_info(options), column))
|
||||
{
|
||||
return Some(
|
||||
"IoTDB Table model cannot clear a FIELD or ATTRIBUTE value without deleting the row. Enter a non-NULL value or delete the row instead."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn build_iotdb_changed_row_insert(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
save_columns: &[Option<String>],
|
||||
row: &[Value],
|
||||
changes: &[(usize, Value)],
|
||||
use_changed_values: bool,
|
||||
) -> Option<String> {
|
||||
let changed_indexes = changes.iter().map(|(index, _)| *index).collect::<HashSet<_>>();
|
||||
let primary_keys =
|
||||
options.table_meta.primary_keys.iter().map(|column| normalize_column_name(column)).collect::<HashSet<_>>();
|
||||
let insert_pairs = save_columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, column)| {
|
||||
let column = column.as_deref()?;
|
||||
let is_primary_key = primary_keys.contains(&normalize_column_name(column));
|
||||
if !is_primary_key && !changed_indexes.contains(&index) {
|
||||
return None;
|
||||
}
|
||||
let value = if use_changed_values && !is_primary_key {
|
||||
changes.iter().find(|(changed_index, _)| *changed_index == index).map(|(_, value)| value)?
|
||||
} else {
|
||||
row.get(index).unwrap_or(&Value::Null)
|
||||
};
|
||||
Some((column, value))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
build_iotdb_insert_statement(options, insert_pairs)
|
||||
}
|
||||
|
||||
fn build_iotdb_full_row_insert(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
save_columns: &[Option<String>],
|
||||
row: &[Value],
|
||||
) -> Option<String> {
|
||||
let insert_pairs = save_columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, column)| Some((column.as_deref()?, row.get(index).unwrap_or(&Value::Null))))
|
||||
.filter(|(_, value)| !value.is_null())
|
||||
.collect::<Vec<_>>();
|
||||
build_iotdb_insert_statement(options, insert_pairs)
|
||||
}
|
||||
|
||||
fn build_iotdb_insert_statement(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
insert_pairs: Vec<(&str, &Value)>,
|
||||
) -> Option<String> {
|
||||
if insert_pairs.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let columns = insert_pairs
|
||||
.iter()
|
||||
.map(|(column, _)| data_grid_identifier(Some(DatabaseType::Iotdb), column, options.identifier_quote.as_deref()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let values = insert_pairs
|
||||
.iter()
|
||||
.map(|(column, value)| {
|
||||
format_grid_save_sql_literal(
|
||||
value,
|
||||
Some(DatabaseType::Iotdb),
|
||||
column_info_for(iotdb_column_info(options), column),
|
||||
options.identifier_quote.as_deref(),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
Some(data_grid_statement(
|
||||
Some(DatabaseType::Iotdb),
|
||||
format!("INSERT INTO {} ({columns}) VALUES ({values})", iotdb_table(options)),
|
||||
))
|
||||
}
|
||||
|
||||
fn build_iotdb_delete_statement(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
save_columns: &[Option<String>],
|
||||
row: &[Value],
|
||||
) -> Option<String> {
|
||||
let where_clause = build_primary_key_where(
|
||||
Some(DatabaseType::Iotdb),
|
||||
&options.table_meta.primary_keys,
|
||||
save_columns,
|
||||
row,
|
||||
iotdb_column_info(options),
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
if where_clause.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(data_grid_statement(
|
||||
Some(DatabaseType::Iotdb),
|
||||
data_grid_delete_sql(Some(DatabaseType::Iotdb), &iotdb_table(options), &where_clause),
|
||||
))
|
||||
}
|
||||
|
||||
fn iotdb_table(options: &DataGridSaveStatementOptions) -> String {
|
||||
data_grid_qualified_table_name(
|
||||
Some(DatabaseType::Iotdb),
|
||||
options.table_meta.catalog.as_deref(),
|
||||
options.table_meta.schema.as_deref(),
|
||||
options.table_meta.database.as_deref(),
|
||||
&options.table_meta.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
fn iotdb_column_info(options: &DataGridSaveStatementOptions) -> &[DataGridColumnInfo] {
|
||||
options.table_meta.columns.as_deref().unwrap_or(&[])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn column(name: &str, data_type: &str, nullable: bool, category: &str) -> DataGridColumnInfo {
|
||||
DataGridColumnInfo {
|
||||
name: name.to_string(),
|
||||
data_type: data_type.to_string(),
|
||||
is_nullable: nullable,
|
||||
is_primary_key: matches!(category, "TIME" | "TAG"),
|
||||
column_default: None,
|
||||
extra: Some(category.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn table_options() -> DataGridSaveStatementOptions {
|
||||
DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Iotdb),
|
||||
identifier_quote: None,
|
||||
table_meta: DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("dbx_edit_preview".to_string()),
|
||||
table_name: "events".to_string(),
|
||||
primary_keys: vec!["time".to_string(), "device".to_string()],
|
||||
columns: Some(vec![
|
||||
column("time", "TIMESTAMP", false, "TIME"),
|
||||
column("device", "STRING", false, "TAG"),
|
||||
column("event_time", "TIMESTAMP", true, "FIELD"),
|
||||
column("temperature", "DOUBLE", true, "FIELD"),
|
||||
]),
|
||||
},
|
||||
columns: vec![
|
||||
"time".to_string(),
|
||||
"device".to_string(),
|
||||
"event_time".to_string(),
|
||||
"temperature".to_string(),
|
||||
],
|
||||
source_columns: None,
|
||||
rows: vec![vec![json!("1786954706123"), json!("sensor-a"), json!("1786954706123"), json!(23.5)]],
|
||||
dirty_rows: Vec::new(),
|
||||
deleted_rows: Vec::new(),
|
||||
new_rows: Vec::new(),
|
||||
include_database_name: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepares_iotdb_timestamp_field_overwrite_and_rollback() {
|
||||
let mut options = table_options();
|
||||
options.dirty_rows = vec![(0, vec![(2, json!("2026-08-17 17:18:27.123"))])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(result.validation_error, None);
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec!["INSERT INTO dbx_edit_preview.events (time, device, event_time) VALUES (1786954706123, 'sensor-a', '2026-08-17 17:18:27.123');"]
|
||||
);
|
||||
assert_eq!(
|
||||
result.rollback_statements,
|
||||
vec!["INSERT INTO dbx_edit_preview.events (time, device, event_time) VALUES (1786954706123, 'sensor-a', 1786954706123);"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepares_iotdb_only_with_changed_fields() {
|
||||
let mut options = table_options();
|
||||
options.dirty_rows = vec![(0, vec![(2, json!("1786958307123")), (3, json!(24.75))])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec!["INSERT INTO dbx_edit_preview.events (time, device, event_time, temperature) VALUES (1786954706123, 'sensor-a', 1786958307123, 24.75);"]
|
||||
);
|
||||
assert_eq!(
|
||||
result.rollback_statements,
|
||||
vec!["INSERT INTO dbx_edit_preview.events (time, device, event_time, temperature) VALUES (1786954706123, 'sensor-a', 1786954706123, 23.5);"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepares_iotdb_insert_delete_and_rollback() {
|
||||
let mut options = table_options();
|
||||
options.deleted_rows = vec![0];
|
||||
options.new_rows = vec![vec![json!("1786961906789"), json!("sensor-b"), json!("1786961906789"), json!(22.75)]];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec![
|
||||
"DELETE FROM dbx_edit_preview.events WHERE time = 1786954706123 AND device = 'sensor-a';",
|
||||
"INSERT INTO dbx_edit_preview.events (time, device, event_time, temperature) VALUES (1786961906789, 'sensor-b', 1786961906789, 22.75);",
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
result.rollback_statements,
|
||||
vec![
|
||||
"DELETE FROM dbx_edit_preview.events WHERE time = 1786961906789 AND device = 'sensor-b';",
|
||||
"INSERT INTO dbx_edit_preview.events (time, device, event_time, temperature) VALUES (1786954706123, 'sensor-a', 1786954706123, 23.5);",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_iotdb_row_identifier_edits() {
|
||||
let mut options = table_options();
|
||||
options.dirty_rows = vec![(0, vec![(1, json!("sensor-b"))])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.validation_error,
|
||||
Some("IoTDB row identifier columns (time and TAG) cannot be edited.".to_string())
|
||||
);
|
||||
assert!(result.statements.is_empty());
|
||||
assert!(result.rollback_statements.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_iotdb_null_field_edits_that_insert_cannot_clear() {
|
||||
let mut options = table_options();
|
||||
options.dirty_rows = vec![(0, vec![(2, Value::Null)])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.validation_error,
|
||||
Some(
|
||||
"IoTDB Table model cannot clear a FIELD or ATTRIBUTE value without deleting the row. Enter a non-NULL value or delete the row instead."
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
assert!(result.statements.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restores_iotdb_original_null_with_delete_and_full_row_insert() {
|
||||
let mut options = table_options();
|
||||
options.rows[0][2] = Value::Null;
|
||||
options.dirty_rows = vec![(0, vec![(2, json!("1786958307123"))])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec!["INSERT INTO dbx_edit_preview.events (time, device, event_time) VALUES (1786954706123, 'sensor-a', 1786958307123);"]
|
||||
);
|
||||
assert_eq!(
|
||||
result.rollback_statements,
|
||||
vec![
|
||||
"DELETE FROM dbx_edit_preview.events WHERE time = 1786954706123 AND device = 'sensor-a';",
|
||||
"INSERT INTO dbx_edit_preview.events (time, device, temperature) VALUES (1786954706123, 'sensor-a', 23.5);",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_timestamp_precision_strings_as_unquoted_integer_literals() {
|
||||
for (original, changed) in [
|
||||
("1786954706123", "1786958307123"),
|
||||
("1786954706123456", "1786958307123456"),
|
||||
("1786954706123456789", "1786958307123456789"),
|
||||
] {
|
||||
let mut options = table_options();
|
||||
options.rows[0][0] = json!(original);
|
||||
options.rows[0][2] = json!(original);
|
||||
options.dirty_rows = vec![(0, vec![(2, json!(changed))])];
|
||||
|
||||
let result = prepare_data_grid_save(options);
|
||||
|
||||
assert_eq!(
|
||||
result.statements,
|
||||
vec![format!(
|
||||
"INSERT INTO dbx_edit_preview.events (time, device, event_time) VALUES ({original}, 'sensor-a', {changed});"
|
||||
)]
|
||||
);
|
||||
assert_eq!(
|
||||
result.rollback_statements,
|
||||
vec![format!(
|
||||
"INSERT INTO dbx_edit_preview.events (time, device, event_time) VALUES ({original}, 'sensor-a', {original});"
|
||||
)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_iotdb_tree_model_on_the_existing_sql_path() {
|
||||
let mut options = table_options();
|
||||
options.table_meta.primary_keys.clear();
|
||||
options.table_meta.schema = Some("root.dbx".to_string());
|
||||
options.table_meta.table_name = "d1".to_string();
|
||||
|
||||
assert!(!uses_iotdb_table_model_save(&options));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use super::*;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn build_neo4j_data_grid_save_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let label = quote_ident(Some(DatabaseType::Neo4j), &options.table_meta.table_name);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
let sets = changes
|
||||
.iter()
|
||||
.filter_map(|(column_index, value)| {
|
||||
let column = options.columns.get(*column_index)?;
|
||||
if is_neo4j_element_id(Some(DatabaseType::Neo4j), Some(column)) {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"n.{} = {}",
|
||||
quote_ident(Some(DatabaseType::Neo4j), column),
|
||||
format_grid_sql_literal(value, Some(DatabaseType::Neo4j), None)
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
if sets.is_empty() {
|
||||
continue;
|
||||
}
|
||||
statements
|
||||
.push(format!("MATCH (n:{label}) WHERE {} SET {sets};", neo4j_element_id_predicate(&options.columns, row)));
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
statements.push(format!(
|
||||
"MATCH (n:{label}) WHERE {} DETACH DELETE n;",
|
||||
neo4j_element_id_predicate(&options.columns, row)
|
||||
));
|
||||
}
|
||||
|
||||
for row in &options.new_rows {
|
||||
let props = options
|
||||
.columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, column)| !is_neo4j_element_id(Some(DatabaseType::Neo4j), Some(column)))
|
||||
.filter_map(|(index, column)| {
|
||||
let value = row.get(index).unwrap_or(&Value::Null);
|
||||
if value.is_null() {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"{}: {}",
|
||||
quote_ident(Some(DatabaseType::Neo4j), column),
|
||||
format_grid_sql_literal(value, Some(DatabaseType::Neo4j), None)
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
statements.push(if props.is_empty() {
|
||||
format!("CREATE (n:{label});")
|
||||
} else {
|
||||
format!("CREATE (n:{label} {{{props}}});")
|
||||
});
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn build_neo4j_data_grid_rollback_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let label = quote_ident(Some(DatabaseType::Neo4j), &options.table_meta.table_name);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for row in &options.new_rows {
|
||||
let where_clause = neo4j_row_property_where(&options.columns, row);
|
||||
statements.push(if where_clause.is_empty() {
|
||||
format!("MATCH (n:{label}) DETACH DELETE n;")
|
||||
} else {
|
||||
format!("MATCH (n:{label}) WHERE {where_clause} DETACH DELETE n;")
|
||||
});
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
let props = options
|
||||
.columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, column)| !is_neo4j_element_id(Some(DatabaseType::Neo4j), Some(column)))
|
||||
.filter_map(|(index, column)| {
|
||||
let value = row.get(index).unwrap_or(&Value::Null);
|
||||
if value.is_null() {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"{}: {}",
|
||||
quote_ident(Some(DatabaseType::Neo4j), column),
|
||||
format_grid_sql_literal(value, Some(DatabaseType::Neo4j), None)
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
statements.push(if props.is_empty() {
|
||||
format!("CREATE (n:{label});")
|
||||
} else {
|
||||
format!("CREATE (n:{label} {{{props}}});")
|
||||
});
|
||||
}
|
||||
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
let sets = changes
|
||||
.iter()
|
||||
.filter_map(|(column_index, _)| {
|
||||
let column = options.columns.get(*column_index)?;
|
||||
if is_neo4j_element_id(Some(DatabaseType::Neo4j), Some(column)) {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"n.{} = {}",
|
||||
quote_ident(Some(DatabaseType::Neo4j), column),
|
||||
format_grid_sql_literal(
|
||||
row.get(*column_index).unwrap_or(&Value::Null),
|
||||
Some(DatabaseType::Neo4j),
|
||||
None
|
||||
)
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
if sets.is_empty() {
|
||||
continue;
|
||||
}
|
||||
statements
|
||||
.push(format!("MATCH (n:{label}) WHERE {} SET {sets};", neo4j_element_id_predicate(&options.columns, row)));
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
fn neo4j_element_id_predicate(columns: &[String], row: &[Value]) -> String {
|
||||
let index = columns.iter().position(|column| column == DBX_NEO4J_ELEMENT_ID_COLUMN).unwrap_or(usize::MAX);
|
||||
format!(
|
||||
"elementId(n) = {}",
|
||||
format_grid_sql_literal(row.get(index).unwrap_or(&Value::Null), Some(DatabaseType::Neo4j), None)
|
||||
)
|
||||
}
|
||||
|
||||
fn neo4j_row_property_where(columns: &[String], row: &[Value]) -> String {
|
||||
columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, column)| {
|
||||
if is_neo4j_element_id(Some(DatabaseType::Neo4j), Some(column)) {
|
||||
return None;
|
||||
}
|
||||
let value = row.get(index).unwrap_or(&Value::Null);
|
||||
let ident = format!("n.{}", quote_ident(Some(DatabaseType::Neo4j), column));
|
||||
if value.is_null() {
|
||||
Some(format!("{ident} IS NULL"))
|
||||
} else {
|
||||
Some(format!("{ident} = {}", format_grid_sql_literal(value, Some(DatabaseType::Neo4j), None)))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" AND ")
|
||||
}
|
||||
@@ -0,0 +1,459 @@
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use super::*;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
/// Header line emitted before every JSON body. The driver's
|
||||
/// `parse_salesforce_statement` matches this case-insensitively.
|
||||
const SALESFORCE_DML_HEADER: &str = "DBX SALESFORCE DML";
|
||||
|
||||
/// Build the save statements for a Salesforce grid save. Order matches the
|
||||
/// generic SQL builder: updates, deletes, inserts (see `build_data_grid_save_statements`).
|
||||
///
|
||||
/// Each statement is a `DBX SALESFORCE DML` pseudo-command carrying a JSON
|
||||
/// body. The driver routes them to the appropriate REST endpoint.
|
||||
pub(super) fn build_salesforce_data_grid_save_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let object = &options.table_meta.table_name;
|
||||
let columns = resolve_salesforce_columns(options);
|
||||
let column_info = options.table_meta.columns.as_deref().unwrap_or(&[]);
|
||||
let id_column_index = find_salesforce_id_column_index(&columns, &options.table_meta.primary_keys);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
// 1. Updates (dirty_rows): one statement per dirty row, fields = only the changed columns.
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else { continue };
|
||||
let Some(id_index) = id_column_index else { continue };
|
||||
let Some(id_value) = row.get(id_index).and_then(Value::as_str).filter(|s| !s.is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let mut fields = Map::new();
|
||||
for (column_index, value) in changes {
|
||||
let Some(field_name) = columns.get(*column_index).and_then(|c| c.as_deref()) else { continue };
|
||||
// Id is the identity, never a write target.
|
||||
if field_name.eq_ignore_ascii_case("Id") {
|
||||
continue;
|
||||
}
|
||||
// Skip columns marked non-updateable in the describe metadata.
|
||||
if !salesforce_column_is_updateable(column_info_for(column_info, field_name)) {
|
||||
continue;
|
||||
}
|
||||
fields.insert(field_name.to_string(), value.clone());
|
||||
}
|
||||
if fields.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let body = serde_json::json!({
|
||||
"op": "update",
|
||||
"object": object,
|
||||
"id": id_value,
|
||||
"fields": fields
|
||||
});
|
||||
statements.push(format!("{SALESFORCE_DML_HEADER}\n{body}"));
|
||||
}
|
||||
|
||||
// 2. Deletes (deleted_rows): one statement per deleted row index.
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else { continue };
|
||||
let Some(id_index) = id_column_index else { continue };
|
||||
let Some(id_value) = row.get(id_index).and_then(Value::as_str).filter(|s| !s.is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let body = serde_json::json!({
|
||||
"op": "delete",
|
||||
"object": object,
|
||||
"id": id_value
|
||||
});
|
||||
statements.push(format!("{SALESFORCE_DML_HEADER}\n{body}"));
|
||||
}
|
||||
|
||||
// 3. Inserts (new_rows): one statement per new row, skipping null and empty-string cells.
|
||||
for row in &options.new_rows {
|
||||
let mut fields = Map::new();
|
||||
for (index, field_name) in columns.iter().enumerate() {
|
||||
let Some(field_name) = field_name.as_deref() else { continue };
|
||||
// Id is server-generated for inserts; never send it.
|
||||
if field_name.eq_ignore_ascii_case("Id") {
|
||||
continue;
|
||||
}
|
||||
// Skip columns marked non-createable in the describe metadata.
|
||||
if !salesforce_column_is_createable(column_info_for(column_info, field_name)) {
|
||||
continue;
|
||||
}
|
||||
let value = row.get(index).unwrap_or(&Value::Null);
|
||||
// Untouched cells must not be sent.
|
||||
if value.is_null() {
|
||||
continue;
|
||||
}
|
||||
if value.is_string() && value.as_str().unwrap_or("").is_empty() {
|
||||
continue;
|
||||
}
|
||||
fields.insert(field_name.to_string(), value.clone());
|
||||
}
|
||||
if fields.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let body = serde_json::json!({
|
||||
"op": "insert",
|
||||
"object": object,
|
||||
"fields": fields
|
||||
});
|
||||
statements.push(format!("{SALESFORCE_DML_HEADER}\n{body}"));
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
/// Salesforce REST has no transactions — rollback statements are always empty.
|
||||
pub(super) fn build_salesforce_data_grid_rollback_statements(_options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Validate that the Salesforce sObject has a resolvable Id column. Called
|
||||
/// from `validate_data_grid_save` before the builder runs.
|
||||
pub(super) fn validate_salesforce_id_column(options: &DataGridSaveStatementOptions) -> Option<String> {
|
||||
if options.database_type != Some(DatabaseType::Salesforce) {
|
||||
return None;
|
||||
}
|
||||
// Only validate when there are updates, deletes, or inserts that would
|
||||
// need an Id (updates/deletes need it from existing rows; inserts don't
|
||||
// strictly need it, but the grid always has an Id column from the query).
|
||||
if options.dirty_rows.is_empty() && options.deleted_rows.is_empty() && options.new_rows.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let columns = resolve_salesforce_columns(options);
|
||||
if find_salesforce_id_column_index(&columns, &options.table_meta.primary_keys).is_none() {
|
||||
return Some(format!(
|
||||
"Cannot save Salesforce {}: no Id column found. The grid must include the record Id to enable editing.",
|
||||
options.table_meta.table_name
|
||||
));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Resolve the field name for each display column: `source_columns[i]` when
|
||||
/// present, else `columns[i]`.
|
||||
fn resolve_salesforce_columns(options: &DataGridSaveStatementOptions) -> Vec<Option<String>> {
|
||||
match &options.source_columns {
|
||||
Some(source_columns) if source_columns.len() == options.columns.len() => source_columns.clone(),
|
||||
_ => options.columns.iter().map(|column| Some(column.clone())).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Locate the Id column by matching `table_meta.primary_keys` (case-insensitive)
|
||||
/// against the resolved field names.
|
||||
fn find_salesforce_id_column_index(columns: &[Option<String>], primary_keys: &[String]) -> Option<usize> {
|
||||
// Try primary keys first (case-insensitive).
|
||||
for pk in primary_keys {
|
||||
let pk_upper = pk.to_ascii_uppercase();
|
||||
if let Some(index) =
|
||||
columns.iter().position(|col| col.as_deref().is_some_and(|name| name.to_ascii_uppercase() == pk_upper))
|
||||
{
|
||||
return Some(index);
|
||||
}
|
||||
}
|
||||
// Fallback: look for a column literally named "Id" (case-insensitive).
|
||||
columns.iter().position(|col| col.as_deref().is_some_and(|name| name.eq_ignore_ascii_case("Id")))
|
||||
}
|
||||
|
||||
/// Whether a column should be included in update payloads. Returns `true`
|
||||
/// unless the column's `extra` metadata explicitly marks `updateable: false`.
|
||||
/// Missing or unparseable `extra` → treat as writable (fail-open; Salesforce
|
||||
/// enforces the real rules server-side).
|
||||
fn salesforce_column_is_updateable(info: Option<&DataGridColumnInfo>) -> bool {
|
||||
salesforce_extra_flag(info, "updateable").unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Whether a column should be included in insert payloads. Returns `true`
|
||||
/// unless the column's `extra` metadata explicitly marks `createable: false`.
|
||||
fn salesforce_column_is_createable(info: Option<&DataGridColumnInfo>) -> bool {
|
||||
salesforce_extra_flag(info, "createable").unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Read a boolean flag from the `extra` JSON string on a `DataGridColumnInfo`.
|
||||
/// Returns `None` when `extra` is absent, unparseable, or lacks the key.
|
||||
fn salesforce_extra_flag(info: Option<&DataGridColumnInfo>, key: &str) -> Option<bool> {
|
||||
let extra_str = info?.extra.as_deref()?;
|
||||
let extra: Value = serde_json::from_str(extra_str).ok()?;
|
||||
extra.get(key).and_then(Value::as_bool)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn make_options(
|
||||
columns: Vec<&str>,
|
||||
primary_keys: Vec<&str>,
|
||||
dirty_rows: Vec<(usize, Vec<(usize, Value)>)>,
|
||||
deleted_rows: Vec<usize>,
|
||||
new_rows: Vec<Vec<Value>>,
|
||||
rows: Vec<Vec<Value>>,
|
||||
) -> DataGridSaveStatementOptions {
|
||||
DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Salesforce),
|
||||
identifier_quote: None,
|
||||
table_meta: DataGridTableMeta {
|
||||
database: None,
|
||||
catalog: None,
|
||||
schema: None,
|
||||
table_name: "Account".to_string(),
|
||||
primary_keys: primary_keys.into_iter().map(str::to_string).collect(),
|
||||
columns: None,
|
||||
},
|
||||
columns: columns.into_iter().map(str::to_string).collect(),
|
||||
source_columns: None,
|
||||
rows,
|
||||
dirty_rows,
|
||||
deleted_rows,
|
||||
new_rows,
|
||||
include_database_name: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn make_options_with_extra(
|
||||
columns: Vec<&str>,
|
||||
primary_keys: Vec<&str>,
|
||||
extras: Vec<Option<&str>>,
|
||||
) -> DataGridSaveStatementOptions {
|
||||
let cols: Vec<DataGridColumnInfo> = columns
|
||||
.iter()
|
||||
.zip(extras.iter())
|
||||
.map(|(name, extra)| DataGridColumnInfo {
|
||||
name: name.to_string(),
|
||||
data_type: String::new(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: extra.map(|s| s.to_string()),
|
||||
})
|
||||
.collect();
|
||||
DataGridSaveStatementOptions {
|
||||
database_type: Some(DatabaseType::Salesforce),
|
||||
identifier_quote: None,
|
||||
table_meta: DataGridTableMeta {
|
||||
database: None,
|
||||
catalog: None,
|
||||
schema: None,
|
||||
table_name: "Account".to_string(),
|
||||
primary_keys: primary_keys.into_iter().map(str::to_string).collect(),
|
||||
columns: Some(cols),
|
||||
},
|
||||
columns: columns.into_iter().map(str::to_string).collect(),
|
||||
source_columns: None,
|
||||
rows: Vec::new(),
|
||||
dirty_rows: Vec::new(),
|
||||
deleted_rows: Vec::new(),
|
||||
new_rows: Vec::new(),
|
||||
include_database_name: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_generates_dml_statement() {
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name", "Industry"],
|
||||
vec!["Id"],
|
||||
vec![(0, vec![(1, Value::String("Acme".to_string()))])],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![vec![
|
||||
Value::String("001xx000003DGbY".to_string()),
|
||||
Value::String("Old Name".to_string()),
|
||||
Value::String("Tech".to_string()),
|
||||
]],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
assert!(stmts[0].starts_with("DBX SALESFORCE DML\n"));
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert_eq!(body["op"], "update");
|
||||
assert_eq!(body["object"], "Account");
|
||||
assert_eq!(body["id"], "001xx000003DGbY");
|
||||
assert_eq!(body["fields"]["Name"], "Acme");
|
||||
// Industry was not changed → not in fields.
|
||||
assert!(body["fields"].get("Industry").is_none());
|
||||
// Id is never in fields.
|
||||
assert!(body["fields"].get("Id").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_generates_dml_statement() {
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name"],
|
||||
vec!["Id"],
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![],
|
||||
vec![vec![Value::String("001xx000003DGbY".to_string()), Value::String("Acme".to_string())]],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert_eq!(body["op"], "delete");
|
||||
assert_eq!(body["id"], "001xx000003DGbY");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_generates_dml_statement_skipping_nulls_and_empties() {
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name", "Phone", "Website"],
|
||||
vec!["Id"],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![vec![
|
||||
Value::Null,
|
||||
Value::String("Acme".to_string()),
|
||||
Value::String("".to_string()), // empty → skip
|
||||
Value::Null, // null → skip
|
||||
]],
|
||||
vec![],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert_eq!(body["op"], "insert");
|
||||
assert_eq!(body["object"], "Account");
|
||||
assert_eq!(body["fields"]["Name"], "Acme");
|
||||
// Id, Phone, Website excluded.
|
||||
assert!(body["fields"].get("Id").is_none());
|
||||
assert!(body["fields"].get("Phone").is_none());
|
||||
assert!(body["fields"].get("Website").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_excluded_from_update_fields() {
|
||||
// Even if Id appears in the changes, it must not be in the fields payload.
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name"],
|
||||
vec!["Id"],
|
||||
vec![(0, vec![(0, Value::String("new-id".to_string())), (1, Value::String("Acme".to_string()))])],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![vec![Value::String("001xx000003DGbY".to_string()), Value::String("Old".to_string())]],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert!(body["fields"].get("Id").is_none());
|
||||
assert_eq!(body["fields"]["Name"], "Acme");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updateable_false_columns_skipped_on_update() {
|
||||
let opts = make_options_with_extra(
|
||||
vec!["Id", "Name", "SystemField"],
|
||||
vec!["Id"],
|
||||
vec![
|
||||
None,
|
||||
Some(r#"{"updateable":true,"createable":true}"#),
|
||||
Some(r#"{"updateable":false,"createable":true}"#),
|
||||
],
|
||||
);
|
||||
let mut opts = opts;
|
||||
opts.rows = vec![vec![
|
||||
Value::String("001xx000003DGbY".to_string()),
|
||||
Value::String("Old".to_string()),
|
||||
Value::String("system".to_string()),
|
||||
]];
|
||||
opts.dirty_rows =
|
||||
vec![(0, vec![(1, Value::String("New".to_string())), (2, Value::String("changed".to_string()))])];
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert_eq!(body["fields"]["Name"], "New");
|
||||
assert!(body["fields"].get("SystemField").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn createable_false_columns_skipped_on_insert() {
|
||||
let opts = make_options_with_extra(
|
||||
vec!["Id", "Name", "AutoField"],
|
||||
vec!["Id"],
|
||||
vec![
|
||||
None,
|
||||
Some(r#"{"updateable":true,"createable":true}"#),
|
||||
Some(r#"{"updateable":true,"createable":false}"#),
|
||||
],
|
||||
);
|
||||
let mut opts = opts;
|
||||
opts.new_rows = vec![vec![Value::Null, Value::String("Acme".to_string()), Value::String("auto".to_string())]];
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 1);
|
||||
let body: Value = serde_json::from_str(stmts[0].split_once('\n').unwrap().1).unwrap();
|
||||
assert_eq!(body["fields"]["Name"], "Acme");
|
||||
assert!(body["fields"].get("AutoField").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_id_column_produces_validation_error() {
|
||||
let opts = make_options(
|
||||
vec!["Name", "Industry"],
|
||||
vec![], // no primary keys
|
||||
vec![(0, vec![(0, Value::String("Acme".to_string()))])],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![vec![Value::String("Old".to_string()), Value::String("Tech".to_string())]],
|
||||
);
|
||||
let error = validate_salesforce_id_column(&opts);
|
||||
assert!(error.is_some());
|
||||
assert!(error.unwrap().contains("no Id column found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rollback_statements_are_empty() {
|
||||
let opts = make_options(vec!["Id", "Name"], vec!["Id"], vec![], vec![], vec![], vec![]);
|
||||
assert!(build_salesforce_data_grid_rollback_statements(&opts).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn statement_order_is_updates_deletes_inserts() {
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name"],
|
||||
vec!["Id"],
|
||||
vec![(0, vec![(1, Value::String("Updated".to_string()))])],
|
||||
vec![1],
|
||||
vec![vec![Value::Null, Value::String("New".to_string())]],
|
||||
vec![
|
||||
vec![Value::String("id1".to_string()), Value::String("Row1".to_string())],
|
||||
vec![Value::String("id2".to_string()), Value::String("Row2".to_string())],
|
||||
],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert_eq!(stmts.len(), 3);
|
||||
let ops: Vec<&str> = stmts
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let body: Value = serde_json::from_str(s.split_once('\n').unwrap().1).unwrap();
|
||||
// Leak to get a &'static str — fine in tests.
|
||||
Box::leak(body["op"].as_str().unwrap().to_string().into_boxed_str()) as &str
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(ops, vec!["update", "delete", "insert"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_change_set_after_filtering_skips_row() {
|
||||
let opts = make_options(
|
||||
vec!["Id", "Name"],
|
||||
vec!["Id"],
|
||||
// Only change is to Id itself → filtered out → empty fields → skip.
|
||||
vec![(0, vec![(0, Value::String("new-id".to_string()))])],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![vec![Value::String("001xx000003DGbY".to_string()), Value::String("Old".to_string())]],
|
||||
);
|
||||
let stmts = build_salesforce_data_grid_save_statements(&opts);
|
||||
assert!(stmts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_salesforce_database_type_returns_no_validation_error() {
|
||||
let mut opts = make_options(vec!["Id"], vec!["Id"], vec![], vec![], vec![], vec![]);
|
||||
opts.database_type = Some(DatabaseType::Postgres);
|
||||
assert!(validate_salesforce_id_column(&opts).is_none());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,303 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use super::*;
|
||||
use crate::models::connection::DatabaseType;
|
||||
|
||||
pub(super) fn build_tdengine_data_grid_save_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
let save_columns = effective_columns(options);
|
||||
let mut statements = Vec::new();
|
||||
let has_tbname = save_columns
|
||||
.iter()
|
||||
.any(|column| column.as_deref().is_some_and(|column| column.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN)));
|
||||
let requires_tbname = options
|
||||
.table_meta
|
||||
.primary_keys
|
||||
.iter()
|
||||
.any(|primary_key| primary_key.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN));
|
||||
|
||||
if has_tbname || !requires_tbname {
|
||||
for (row_index, changes) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
let mut after_row = row.clone();
|
||||
for (column_index, value) in changes {
|
||||
if *column_index < after_row.len() {
|
||||
after_row[*column_index] = value.clone();
|
||||
}
|
||||
}
|
||||
if let Some(statement) = build_tdengine_insert_statement(options, &save_columns, &after_row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_tdengine_delete_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for row in &options.new_rows {
|
||||
if let Some(statement) = build_tdengine_insert_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn build_tdengine_data_grid_rollback_statements(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
if has_tdengine_composite_primary_key(options) && !options.new_rows.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let save_columns = effective_columns(options);
|
||||
let mut statements = Vec::new();
|
||||
|
||||
for row in &options.new_rows {
|
||||
if let Some(statement) = build_tdengine_delete_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for row_index in &options.deleted_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_tdengine_insert_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
for (row_index, _) in &options.dirty_rows {
|
||||
let Some(row) = options.rows.get(*row_index) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(statement) = build_tdengine_insert_statement(options, &save_columns, row) {
|
||||
statements.push(statement);
|
||||
}
|
||||
}
|
||||
|
||||
statements
|
||||
}
|
||||
|
||||
pub(super) fn validate_tdengine_inserted_rows(options: &DataGridSaveStatementOptions) -> Option<String> {
|
||||
if options.database_type != Some(DatabaseType::Tdengine) || options.new_rows.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let requires_tbname = options
|
||||
.table_meta
|
||||
.primary_keys
|
||||
.iter()
|
||||
.any(|primary_key| primary_key.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN));
|
||||
if !requires_tbname {
|
||||
return None;
|
||||
}
|
||||
|
||||
let save_columns = effective_columns(options);
|
||||
if options
|
||||
.new_rows
|
||||
.iter()
|
||||
.any(|row| tdengine_tbname_value(&save_columns, row).is_none_or(|tbname| tbname.trim().is_empty()))
|
||||
{
|
||||
return Some(tdengine_insert_identity_error());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub(super) fn validate_tdengine_existing_rows(options: &DataGridSaveStatementOptions) -> Option<String> {
|
||||
if options.database_type != Some(DatabaseType::Tdengine)
|
||||
|| (options.deleted_rows.is_empty() && options.dirty_rows.is_empty())
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
if has_tdengine_composite_primary_key(options) && !options.deleted_rows.is_empty() {
|
||||
return Some(tdengine_composite_key_delete_error());
|
||||
}
|
||||
|
||||
let save_columns = effective_columns(options);
|
||||
let primary_keys = tdengine_row_primary_keys(options);
|
||||
let requires_tbname = options
|
||||
.table_meta
|
||||
.primary_keys
|
||||
.iter()
|
||||
.any(|primary_key| primary_key.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN));
|
||||
if (requires_tbname
|
||||
&& !save_columns.iter().any(|column| {
|
||||
column.as_deref().is_some_and(|column| column.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN))
|
||||
}))
|
||||
|| primary_keys.is_empty()
|
||||
|| primary_keys
|
||||
.iter()
|
||||
.any(|primary_key| find_column_index(options.database_type, &save_columns, primary_key).is_none())
|
||||
{
|
||||
return Some(tdengine_row_identity_error());
|
||||
}
|
||||
|
||||
if options.dirty_rows.iter().any(|(_, changes)| {
|
||||
changes.iter().any(|(column_index, _)| {
|
||||
save_columns.get(*column_index).and_then(Option::as_deref).is_some_and(|column| {
|
||||
options.table_meta.primary_keys.iter().any(|primary_key| primary_key.eq_ignore_ascii_case(column))
|
||||
})
|
||||
})
|
||||
}) {
|
||||
return Some(tdengine_row_identity_readonly_error());
|
||||
}
|
||||
|
||||
for row_index in
|
||||
options.deleted_rows.iter().copied().chain(options.dirty_rows.iter().map(|(row_index, _)| *row_index))
|
||||
{
|
||||
let Some(row) = options.rows.get(row_index) else {
|
||||
return Some(tdengine_row_identity_error());
|
||||
};
|
||||
if (requires_tbname && tdengine_tbname_value(&save_columns, row).is_none_or(|tbname| tbname.trim().is_empty()))
|
||||
|| primary_keys.iter().any(|primary_key| {
|
||||
find_column_index(options.database_type, &save_columns, primary_key)
|
||||
.and_then(|index| row.get(index))
|
||||
.is_none_or(Value::is_null)
|
||||
})
|
||||
{
|
||||
return Some(tdengine_row_identity_error());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn build_tdengine_delete_statement(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
save_columns: &[Option<String>],
|
||||
row: &[Value],
|
||||
) -> Option<String> {
|
||||
if has_tdengine_composite_primary_key(options) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let table_name = tdengine_tbname_value(save_columns, row).unwrap_or_else(|| options.table_meta.table_name.clone());
|
||||
let primary_keys = tdengine_row_primary_keys(options);
|
||||
let where_clause = build_primary_key_where(
|
||||
Some(DatabaseType::Tdengine),
|
||||
&primary_keys,
|
||||
save_columns,
|
||||
row,
|
||||
options.table_meta.columns.as_deref().unwrap_or(&[]),
|
||||
None,
|
||||
);
|
||||
if where_clause.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let table = qualified_table_name(Some(DatabaseType::Tdengine), options.table_meta.schema.as_deref(), &table_name);
|
||||
Some(data_grid_statement(
|
||||
Some(DatabaseType::Tdengine),
|
||||
data_grid_delete_sql(Some(DatabaseType::Tdengine), &table, &where_clause),
|
||||
))
|
||||
}
|
||||
|
||||
fn tdengine_row_primary_keys(options: &DataGridSaveStatementOptions) -> Vec<String> {
|
||||
options
|
||||
.table_meta
|
||||
.primary_keys
|
||||
.iter()
|
||||
.filter(|primary_key| !primary_key.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn has_tdengine_composite_primary_key(options: &DataGridSaveStatementOptions) -> bool {
|
||||
tdengine_row_primary_keys(options).len() > 1
|
||||
}
|
||||
|
||||
fn tdengine_row_identity_error() -> String {
|
||||
"TDengine row editing requires all row identifier columns in the result.".to_string()
|
||||
}
|
||||
|
||||
fn tdengine_row_identity_readonly_error() -> String {
|
||||
"TDengine row identifier columns cannot be edited.".to_string()
|
||||
}
|
||||
|
||||
fn tdengine_composite_key_delete_error() -> String {
|
||||
"TDengine tables with composite keys do not support row deletion.".to_string()
|
||||
}
|
||||
|
||||
fn tdengine_insert_identity_error() -> String {
|
||||
"TDengine STABLE inserts require a child table name (tbname).".to_string()
|
||||
}
|
||||
|
||||
fn build_tdengine_insert_statement(
|
||||
options: &DataGridSaveStatementOptions,
|
||||
save_columns: &[Option<String>],
|
||||
row: &[Value],
|
||||
) -> Option<String> {
|
||||
let tbname = tdengine_tbname_value(save_columns, row);
|
||||
let table = qualified_table_name(
|
||||
Some(DatabaseType::Tdengine),
|
||||
options.table_meta.schema.as_deref(),
|
||||
&options.table_meta.table_name,
|
||||
);
|
||||
let tag_columns = tdengine_tag_columns(options.table_meta.columns.as_deref());
|
||||
let insert_pairs: Vec<(&str, &Value)> = save_columns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, column)| Some((column.as_deref()?, row.get(index).unwrap_or(&Value::Null))))
|
||||
.filter(|(_, value)| !value.is_null())
|
||||
.filter(|(column, _)| {
|
||||
tdengine_can_insert_column(column, &options.table_meta.table_name, tbname.as_deref(), &tag_columns)
|
||||
})
|
||||
.collect();
|
||||
if insert_pairs.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let columns = insert_pairs
|
||||
.iter()
|
||||
.map(|(column, _)| quote_ident(Some(DatabaseType::Tdengine), column))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let values = insert_pairs
|
||||
.iter()
|
||||
.map(|(_, value)| format_grid_sql_literal(value, Some(DatabaseType::Tdengine), None))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
Some(format!("INSERT INTO {table} ({columns}) VALUES ({values});"))
|
||||
}
|
||||
|
||||
fn tdengine_tbname_value(save_columns: &[Option<String>], row: &[Value]) -> Option<String> {
|
||||
let index = save_columns.iter().position(|column| {
|
||||
column.as_deref().is_some_and(|column| column.eq_ignore_ascii_case(DBX_TDENGINE_TBNAME_COLUMN))
|
||||
})?;
|
||||
let value = row.get(index)?;
|
||||
if value.is_null() {
|
||||
None
|
||||
} else if let Some(value) = value.as_str() {
|
||||
Some(value.to_string())
|
||||
} else {
|
||||
Some(value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn tdengine_can_insert_column(column: &str, table_name: &str, tbname: Option<&str>, tag_columns: &[String]) -> bool {
|
||||
let normalized = column.to_ascii_lowercase();
|
||||
let target_is_child_table = tbname.is_none_or(|tbname| tbname == table_name);
|
||||
if !target_is_child_table {
|
||||
return true;
|
||||
}
|
||||
if normalized == DBX_TDENGINE_TBNAME_COLUMN {
|
||||
return false;
|
||||
}
|
||||
!tag_columns.iter().any(|tag| tag == &normalized)
|
||||
}
|
||||
|
||||
fn tdengine_tag_columns(columns: Option<&[DataGridColumnInfo]>) -> Vec<String> {
|
||||
columns
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.filter(|column| column.extra.as_deref().unwrap_or("").to_ascii_lowercase().contains("tag"))
|
||||
.map(|column| column.name.to_ascii_lowercase())
|
||||
.collect()
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::sql_dialect::{
|
||||
firebird_rows_clause, pagination_strategy, qualified_table_name, quote_table_identifier, PaginationContext,
|
||||
TablePaginationStrategy,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct DatabaseSearchColumn {
|
||||
pub name: String,
|
||||
pub data_type: String,
|
||||
#[serde(default)]
|
||||
pub is_primary_key: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DatabaseSearchSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub driver_profile: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub table_name: String,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<DatabaseSearchColumn>,
|
||||
pub term: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub limit: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DatabaseSearchSql {
|
||||
pub sql: String,
|
||||
pub searchable_columns: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SearchResultWhereOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default)]
|
||||
pub columns: Vec<DatabaseSearchColumn>,
|
||||
#[serde(default)]
|
||||
pub result_columns: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub row: Vec<Value>,
|
||||
#[serde(default)]
|
||||
pub matched_columns: Vec<String>,
|
||||
}
|
||||
|
||||
const TEXT_TYPES: &[&str] =
|
||||
&["char", "text", "clob", "varchar", "nvarchar", "nchar", "uuid", "uniqueidentifier", "enum"];
|
||||
const NUMBER_TYPES: &[&str] = &["int", "serial", "number", "numeric", "decimal", "float", "double", "real", "money"];
|
||||
const SKIPPED_TYPES: &[&str] = &["blob", "binary", "bytea", "image", "geometry", "geography"];
|
||||
|
||||
pub fn is_text_search_column(column: &DatabaseSearchColumn) -> bool {
|
||||
let data_type = column.data_type.to_ascii_lowercase();
|
||||
if SKIPPED_TYPES.iter().any(|skipped| data_type.contains(skipped)) {
|
||||
return false;
|
||||
}
|
||||
TEXT_TYPES.iter().any(|text_type| data_type.contains(text_type))
|
||||
}
|
||||
|
||||
pub fn is_numeric_search_column(column: &DatabaseSearchColumn) -> bool {
|
||||
let data_type = column.data_type.to_ascii_lowercase();
|
||||
if SKIPPED_TYPES.iter().any(|skipped| data_type.contains(skipped)) {
|
||||
return false;
|
||||
}
|
||||
NUMBER_TYPES.iter().any(|number_type| data_type.contains(number_type))
|
||||
}
|
||||
|
||||
pub fn build_database_search_sql(options: DatabaseSearchSqlOptions) -> Option<DatabaseSearchSql> {
|
||||
let term = clean_term(&options.term);
|
||||
if term.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let text_columns: Vec<_> = options.columns.iter().filter(|column| is_text_search_column(column)).collect();
|
||||
let numeric_term = parse_numeric_term(&term);
|
||||
let numeric_columns: Vec<_> = if numeric_term.is_some() {
|
||||
options.columns.iter().filter(|column| is_numeric_search_column(column)).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
let mut conditions = Vec::new();
|
||||
for column in &text_columns {
|
||||
let identifier = quote_table_identifier(options.database_type, &column.name);
|
||||
conditions.push(format!(
|
||||
"{} LIKE {} ESCAPE '~'",
|
||||
text_cast_expression(options.database_type, options.driver_profile.as_deref(), &identifier),
|
||||
sql_string_literal(&like_pattern(&term))
|
||||
));
|
||||
}
|
||||
for column in &numeric_columns {
|
||||
let identifier = quote_table_identifier(options.database_type, &column.name);
|
||||
conditions.push(format!("{identifier} = {}", numeric_term.as_deref().unwrap_or_default()));
|
||||
}
|
||||
if conditions.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let table = qualified_table_name(options.database_type, options.schema.as_deref(), &options.table_name);
|
||||
let where_clause = conditions.join(" OR ");
|
||||
let limit = clamp_limit(options.limit);
|
||||
let searchable_columns =
|
||||
text_columns.iter().chain(numeric_columns.iter()).map(|column| column.name.clone()).collect();
|
||||
|
||||
let sql = match pagination_strategy(options.database_type, PaginationContext::BoundedRead) {
|
||||
TablePaginationStrategy::SqlServerTop => format!("SELECT TOP {limit} * FROM {table} WHERE ({where_clause})"),
|
||||
TablePaginationStrategy::IrisTop => format!("SELECT TOP {limit} * FROM {table} WHERE ({where_clause})"),
|
||||
TablePaginationStrategy::InformixFirst => format!("SELECT FIRST {limit} * FROM {table} WHERE ({where_clause})"),
|
||||
TablePaginationStrategy::FirebirdRows => {
|
||||
let rows = firebird_rows_clause(limit, 0);
|
||||
format!("SELECT * FROM {table} WHERE ({where_clause}) {rows}")
|
||||
}
|
||||
TablePaginationStrategy::Db2FetchFirst | TablePaginationStrategy::FetchFirst => {
|
||||
format!("SELECT * FROM {table} WHERE ({where_clause}) FETCH FIRST {limit} ROWS ONLY")
|
||||
}
|
||||
TablePaginationStrategy::Rownum => {
|
||||
format!("SELECT * FROM (SELECT * FROM {table} WHERE ({where_clause})) WHERE ROWNUM <= {limit}")
|
||||
}
|
||||
TablePaginationStrategy::AgentMaxRows => format!("SELECT * FROM {table} WHERE ({where_clause});"),
|
||||
TablePaginationStrategy::QuestDbLimit | TablePaginationStrategy::LimitOffset => {
|
||||
format!("SELECT * FROM {table} WHERE ({where_clause}) LIMIT {limit};")
|
||||
}
|
||||
TablePaginationStrategy::Unbounded => format!("SELECT * FROM {table} WHERE ({where_clause})"),
|
||||
};
|
||||
|
||||
Some(DatabaseSearchSql { sql, searchable_columns })
|
||||
}
|
||||
|
||||
pub fn build_search_result_where(options: SearchResultWhereOptions) -> String {
|
||||
let mut value_by_column = std::collections::HashMap::new();
|
||||
for (index, column) in options.result_columns.iter().enumerate() {
|
||||
if let Some(value) = options.row.get(index) {
|
||||
value_by_column.insert(column.to_ascii_lowercase(), value);
|
||||
}
|
||||
}
|
||||
|
||||
let primary_columns: Vec<_> = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|column| column.is_primary_key && value_by_column.contains_key(&column.name.to_ascii_lowercase()))
|
||||
.collect();
|
||||
let fallback_columns: Vec<_> = options
|
||||
.columns
|
||||
.iter()
|
||||
.filter(|column| {
|
||||
options.matched_columns.iter().any(|name| name.eq_ignore_ascii_case(&column.name))
|
||||
&& value_by_column.contains_key(&column.name.to_ascii_lowercase())
|
||||
})
|
||||
.collect();
|
||||
let selected_columns = if primary_columns.is_empty() { fallback_columns } else { primary_columns };
|
||||
|
||||
selected_columns
|
||||
.iter()
|
||||
.take(3)
|
||||
.map(|column| {
|
||||
let identifier = quote_table_identifier(options.database_type, &column.name);
|
||||
let value = value_by_column.get(&column.name.to_ascii_lowercase()).copied().unwrap_or(&Value::Null);
|
||||
if value.is_null() {
|
||||
format!("{identifier} IS NULL")
|
||||
} else {
|
||||
format!("{identifier} = {}", sql_value_literal(options.database_type, column, value))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" AND ")
|
||||
}
|
||||
|
||||
fn clean_term(term: &str) -> String {
|
||||
term.trim().to_string()
|
||||
}
|
||||
|
||||
fn clamp_limit(limit: Option<usize>) -> usize {
|
||||
limit.unwrap_or(20).clamp(1, 100)
|
||||
}
|
||||
|
||||
fn parse_numeric_term(term: &str) -> Option<String> {
|
||||
let trimmed = clean_term(term);
|
||||
if trimmed.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let bytes = trimmed.as_bytes();
|
||||
let start = usize::from(matches!(bytes.first(), Some(b'+') | Some(b'-')));
|
||||
let number = &trimmed[start..];
|
||||
if number.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut dot_count = 0;
|
||||
let mut digit_count = 0;
|
||||
for ch in number.chars() {
|
||||
if ch == '.' {
|
||||
dot_count += 1;
|
||||
if dot_count > 1 {
|
||||
return None;
|
||||
}
|
||||
} else if ch.is_ascii_digit() {
|
||||
digit_count += 1;
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if digit_count == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
fn sql_string_literal(value: &str) -> String {
|
||||
format!("'{}'", value.replace('\'', "''"))
|
||||
}
|
||||
|
||||
fn like_pattern(term: &str) -> String {
|
||||
let mut pattern = String::from("%");
|
||||
for ch in clean_term(term).to_ascii_lowercase().chars() {
|
||||
if matches!(ch, '~' | '%' | '_') {
|
||||
pattern.push('~');
|
||||
}
|
||||
pattern.push(ch);
|
||||
}
|
||||
pattern.push('%');
|
||||
pattern
|
||||
}
|
||||
|
||||
fn text_cast_expression(database_type: Option<DatabaseType>, driver_profile: Option<&str>, identifier: &str) -> String {
|
||||
match database_type {
|
||||
Some(DatabaseType::Mysql) => format!("LOWER(CAST({identifier} AS CHAR))"),
|
||||
Some(DatabaseType::SqlServer)
|
||||
if driver_profile.is_some_and(|profile| profile.trim().eq_ignore_ascii_case("sqlserver-legacy")) =>
|
||||
{
|
||||
format!("LOWER(CAST({identifier} AS NVARCHAR(4000)))")
|
||||
}
|
||||
Some(DatabaseType::SqlServer) => format!("LOWER(CAST({identifier} AS NVARCHAR(MAX)))"),
|
||||
Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle) => {
|
||||
format!("LOWER(CAST({identifier} AS VARCHAR2(4000)))")
|
||||
}
|
||||
Some(DatabaseType::ClickHouse) => format!("lower(toString({identifier}))"),
|
||||
_ => format!("LOWER(CAST({identifier} AS TEXT))"),
|
||||
}
|
||||
}
|
||||
|
||||
fn sql_value_literal(database_type: Option<DatabaseType>, column: &DatabaseSearchColumn, value: &Value) -> String {
|
||||
match value {
|
||||
Value::Null => "NULL".to_string(),
|
||||
Value::Number(number) => number.to_string(),
|
||||
Value::Bool(value) => {
|
||||
if *value {
|
||||
"TRUE".to_string()
|
||||
} else {
|
||||
"FALSE".to_string()
|
||||
}
|
||||
}
|
||||
Value::String(value) => {
|
||||
if is_numeric_search_column(column) && parse_numeric_term(value).is_some() {
|
||||
return value.trim().to_string();
|
||||
}
|
||||
if database_type == Some(DatabaseType::SqlServer) {
|
||||
format!("N{}", sql_string_literal(value))
|
||||
} else {
|
||||
sql_string_literal(value)
|
||||
}
|
||||
}
|
||||
other => sql_string_literal(&other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn col(name: &str, data_type: &str, is_primary_key: bool) -> DatabaseSearchColumn {
|
||||
DatabaseSearchColumn { name: name.to_string(), data_type: data_type.to_string(), is_primary_key }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_table_search_query_over_text_columns() {
|
||||
let query = build_database_search_sql(DatabaseSearchSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
driver_profile: None,
|
||||
schema: None,
|
||||
table_name: "users".to_string(),
|
||||
columns: vec![col("id", "bigint", true), col("email", "varchar", false), col("avatar", "blob", false)],
|
||||
term: "alice@example.com".to_string(),
|
||||
limit: Some(20),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(query.searchable_columns, vec!["email"]);
|
||||
assert_eq!(
|
||||
query.sql,
|
||||
"SELECT * FROM `users` WHERE (LOWER(CAST(`email` AS CHAR)) LIKE '%alice@example.com%' ESCAPE '~') LIMIT 20;"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_legacy_sqlserver_cast_without_max_type() {
|
||||
let query = build_database_search_sql(DatabaseSearchSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
driver_profile: Some(" SQLSERVER-LEGACY ".to_string()),
|
||||
schema: Some("dbo".to_string()),
|
||||
table_name: "users".to_string(),
|
||||
columns: vec![col("name", "nvarchar", false)],
|
||||
term: "alice".to_string(),
|
||||
limit: Some(20),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
query.sql,
|
||||
"SELECT TOP 20 * FROM [dbo].[users] WHERE (LOWER(CAST([name] AS NVARCHAR(4000))) LIKE '%alice%' ESCAPE '~')"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adds_numeric_predicates_only_for_numeric_terms() {
|
||||
let query = build_database_search_sql(DatabaseSearchSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
driver_profile: None,
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "orders".to_string(),
|
||||
columns: vec![col("id", "integer", true), col("note", "text", false)],
|
||||
term: "42".to_string(),
|
||||
limit: Some(10),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(query.searchable_columns, vec!["note", "id"]);
|
||||
assert!(query.sql.contains("\"id\" = 42"));
|
||||
assert!(query.sql.contains("FROM \"public\".\"orders\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_oceanbase_oracle_search_query_with_rownum_limit() {
|
||||
let query = build_database_search_sql(DatabaseSearchSqlOptions {
|
||||
database_type: Some(DatabaseType::OceanbaseOracle),
|
||||
driver_profile: None,
|
||||
schema: Some("APP".to_string()),
|
||||
table_name: "USERS".to_string(),
|
||||
columns: vec![col("NAME", "varchar2", false)],
|
||||
term: "alice".to_string(),
|
||||
limit: Some(20),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(query.searchable_columns, vec!["NAME"]);
|
||||
assert_eq!(
|
||||
query.sql,
|
||||
"SELECT * FROM (SELECT * FROM \"APP\".\"USERS\" WHERE (LOWER(CAST(\"NAME\" AS VARCHAR2(4000))) LIKE '%alice%' ESCAPE '~')) WHERE ROWNUM <= 20"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn returns_none_for_tables_without_searchable_columns() {
|
||||
assert_eq!(
|
||||
build_database_search_sql(DatabaseSearchSqlOptions {
|
||||
database_type: Some(DatabaseType::Sqlite),
|
||||
driver_profile: None,
|
||||
schema: None,
|
||||
table_name: "files".to_string(),
|
||||
columns: vec![col("payload", "blob", false)],
|
||||
term: "needle".to_string(),
|
||||
limit: None,
|
||||
}),
|
||||
None
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_stable_where_predicate_for_opening_result_rows() {
|
||||
let where_clause = build_search_result_where(SearchResultWhereOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
columns: vec![col("id", "integer", true), col("email", "varchar", false)],
|
||||
result_columns: vec!["id".to_string(), "email".to_string()],
|
||||
row: vec![Value::from(42), Value::from("alice@example.com")],
|
||||
matched_columns: Vec::new(),
|
||||
});
|
||||
|
||||
assert_eq!(where_clause, "[id] = 42");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
pub use dbx_sql_core::{sql, sqlserver_temporal, tdsql_mysql, value_literals};
|
||||
pub use dbx_sql_dialect::sql_dialect;
|
||||
pub use dbx_types::{database_manifest, models, types};
|
||||
|
||||
pub mod data_grid_extractors;
|
||||
pub mod data_grid_sql;
|
||||
pub mod database_search_sql;
|
||||
pub mod query_result_sql;
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user