feat(extractor): configure INSERT quoting and portable temporal output
Closes #10559
This commit is contained in:
1 parent
a95762a536
commit
d4b4cf8279
13 files changed
+725
-20
No files matched your search
@@ -95,6 +95,8 @@ pub struct DataGridSqlExtractorOptions {
|
||||
pub insert_mode: DataGridCopyInsertMode,
|
||||
pub exclude_primary_keys_from_insert: bool,
|
||||
pub include_database_name: bool,
|
||||
pub quote_identifiers: bool,
|
||||
pub temporal_format: DataGridTemporalFormat,
|
||||
}
|
||||
|
||||
impl Default for DataGridSqlExtractorOptions {
|
||||
@@ -105,10 +107,21 @@ impl Default for DataGridSqlExtractorOptions {
|
||||
insert_mode: DataGridCopyInsertMode::Merged,
|
||||
exclude_primary_keys_from_insert: false,
|
||||
include_database_name: true,
|
||||
quote_identifiers: true,
|
||||
temporal_format: DataGridTemporalFormat::Native,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum DataGridTemporalFormat {
|
||||
#[default]
|
||||
Native,
|
||||
String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
|
||||
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
use super::{
|
||||
estimated_value_bytes, normalized_name_eq, write_bytes, DataGridExtractError, DataGridExtractErrorCode,
|
||||
DataGridExtractWarning, DataGridExtractWarningCode, ExtractContext, WriteMetadata,
|
||||
DataGridExtractWarning, DataGridExtractWarningCode, DataGridTemporalFormat, 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,
|
||||
build_column_predicate, build_data_grid_copy_insert_statement_with_formatters,
|
||||
build_data_grid_copy_update_statements, data_grid_generated_table_name, format_grid_copy_insert_sql_literal,
|
||||
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, DataGridColumnInfo,
|
||||
DataGridCopyInsertStatementOptions, DataGridCopyUpdateStatementOptions, DataGridTableMeta,
|
||||
};
|
||||
use crate::models::connection::DatabaseType;
|
||||
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
|
||||
use dbx_types::types::is_opaque_aggregate_state_type;
|
||||
use serde_json::Value;
|
||||
use std::collections::{hash_map::Entry, HashMap, HashSet};
|
||||
use std::io::Write;
|
||||
|
||||
#[path = "sql_identifiers.rs"]
|
||||
mod identifiers;
|
||||
|
||||
pub(super) fn write_sql_in_list(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
@@ -64,19 +70,40 @@ pub(super) fn write_sql_inserts(
|
||||
ensure_sql_builder_budget(context)?;
|
||||
reject_selected_opaque_aggregate_states(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,
|
||||
})
|
||||
let statement = build_data_grid_copy_insert_statement_with_formatters(
|
||||
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,
|
||||
},
|
||||
|reference| {
|
||||
if context.request.options.sql.quote_identifiers {
|
||||
reference
|
||||
} else {
|
||||
identifiers::unquote_optional_identifiers(
|
||||
reference,
|
||||
context.request.database_type,
|
||||
context.request.identifier_quote.as_deref(),
|
||||
)
|
||||
}
|
||||
},
|
||||
|value, database_type, info, identifier_quote| {
|
||||
if context.request.options.sql.temporal_format == DataGridTemporalFormat::String {
|
||||
if let Some(literal) = portable_temporal_literal(value, database_type, info) {
|
||||
return literal;
|
||||
}
|
||||
}
|
||||
format_grid_copy_insert_sql_literal(value, database_type, info, identifier_quote)
|
||||
},
|
||||
)
|
||||
.ok_or_else(|| {
|
||||
DataGridExtractError::new(
|
||||
DataGridExtractErrorCode::NoWritableColumns,
|
||||
@@ -87,6 +114,50 @@ pub(super) fn write_sql_inserts(
|
||||
Ok(sql_metadata(data.omitted_columns))
|
||||
}
|
||||
|
||||
fn portable_temporal_literal(
|
||||
value: &Value,
|
||||
database_type: Option<DatabaseType>,
|
||||
info: Option<&DataGridColumnInfo>,
|
||||
) -> Option<String> {
|
||||
let data_type = info?.data_type.trim().to_ascii_lowercase();
|
||||
let kind = data_type.split(['(', ':', ' ']).next()?;
|
||||
if database_type == Some(DatabaseType::SqlServer) && kind == "timestamp" {
|
||||
return None;
|
||||
}
|
||||
if !matches!(
|
||||
kind,
|
||||
"date"
|
||||
| "time"
|
||||
| "timetz"
|
||||
| "timestamp"
|
||||
| "timestamptz"
|
||||
| "datetime"
|
||||
| "datetime2"
|
||||
| "datetimeoffset"
|
||||
| "smalldatetime"
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
let text = value.as_str()?;
|
||||
if database_type == Some(DatabaseType::Iotdb) && kind == "timestamp" && text.parse::<i64>().is_ok() {
|
||||
return None;
|
||||
}
|
||||
let normalized = DateTime::parse_from_rfc3339(text)
|
||||
.map(|datetime| datetime.naive_local())
|
||||
.ok()
|
||||
.or_else(|| NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M:%S%.f").ok())
|
||||
.or_else(|| NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f").ok())
|
||||
.or_else(|| NaiveDate::parse_from_str(text, "%Y-%m-%d").ok().and_then(|date| date.and_hms_opt(0, 0, 0)))
|
||||
.map(|datetime| {
|
||||
datetime
|
||||
.format(if matches!(kind, "time" | "timetz") { "%H:%M:%S" } else { "%Y-%m-%d %H:%M:%S" })
|
||||
.to_string()
|
||||
})
|
||||
.or_else(|| NaiveTime::parse_from_str(text, "%H:%M:%S%.f").ok().map(|time| time.format("%H:%M:%S").to_string()))
|
||||
.unwrap_or_else(|| text.to_string());
|
||||
Some(format!("'{}'", normalized.replace('\'', "''")))
|
||||
}
|
||||
|
||||
pub(super) fn write_sql_updates(
|
||||
context: &ExtractContext<'_>,
|
||||
output: &mut dyn Write,
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::sql_dialect::quote_table_data_identifier;
|
||||
use sqlparser::dialect::{Dialect, GenericDialect, MsSqlDialect};
|
||||
use sqlparser::tokenizer::{Token, Tokenizer};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub(super) fn unquote_optional_identifiers(
|
||||
reference: String,
|
||||
database_type: Option<DatabaseType>,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
let dialect: &dyn Dialect =
|
||||
if database_type == Some(DatabaseType::SqlServer) { &MsSqlDialect {} } else { &GenericDialect {} };
|
||||
let Ok(tokens) = Tokenizer::new(dialect, &reference).with_unescape(false).tokenize() else {
|
||||
return reference;
|
||||
};
|
||||
if tokens.iter().any(|token| !matches!(token, Token::Word(_) | Token::Period)) {
|
||||
return reference;
|
||||
}
|
||||
tokens
|
||||
.into_iter()
|
||||
.map(|token| match token {
|
||||
Token::Word(mut word) => {
|
||||
if word.quote_style.is_some() && can_unquote(&word.value, database_type, identifier_quote) {
|
||||
word.quote_style = None;
|
||||
}
|
||||
word.to_string()
|
||||
}
|
||||
_ => token.to_string(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn can_unquote(name: &str, database_type: Option<DatabaseType>, identifier_quote: Option<&str>) -> bool {
|
||||
let mut chars = name.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
match database_type {
|
||||
Some(DatabaseType::Oracle | DatabaseType::OceanbaseOracle) => {
|
||||
first.is_ascii_uppercase()
|
||||
&& chars.all(|ch| ch.is_ascii_uppercase() || ch.is_ascii_digit() || matches!(ch, '_' | '$' | '#'))
|
||||
&& !oracle_reserved(name)
|
||||
}
|
||||
Some(DatabaseType::Dameng) => {
|
||||
(first.is_ascii_uppercase() || first == '_')
|
||||
&& chars.all(|ch| ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_')
|
||||
&& !dameng_reserved(name)
|
||||
}
|
||||
Some(
|
||||
DatabaseType::Postgres
|
||||
| DatabaseType::Gaussdb
|
||||
| DatabaseType::OpenGauss
|
||||
| DatabaseType::Kingbase
|
||||
| DatabaseType::Jdbc,
|
||||
) if identifier_quote != Some("`") => {
|
||||
quote_table_data_identifier(Some(DatabaseType::Postgres), name, Some("\"")) == name
|
||||
}
|
||||
_ => {
|
||||
let simple =
|
||||
|ch: char| ch.is_ascii_alphabetic() || matches!(ch, '_' | '$') || ('\u{80}'..='\u{ffff}').contains(&ch);
|
||||
let lower = name.to_ascii_lowercase();
|
||||
simple(first) && chars.all(|ch| simple(ch) || ch.is_ascii_digit()) && !mysql_reserved(&lower)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn oracle_reserved(name: &str) -> bool {
|
||||
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
|
||||
KEYWORDS
|
||||
.get_or_init(|| {
|
||||
concat!(
|
||||
"ACCESS ADD ALL ALTER AND ANY AS ASC AUDIT BETWEEN BY CHAR CHECK CLUSTER COLUMN COMMENT COMPRESS ",
|
||||
"CONNECT CREATE CURRENT DATE DECIMAL DEFAULT DELETE DESC DISTINCT DROP ELSE EXCLUSIVE EXISTS FILE ",
|
||||
"FLOAT FOR FROM GRANT GROUP HAVING IDENTIFIED IMMEDIATE IN INCREMENT INDEX INITIAL INSERT INTEGER ",
|
||||
"INTERSECT INTO IS LEVEL LIKE LOCK LONG MAXEXTENTS MINUS MLSLABEL MODE MODIFY NOAUDIT NOCOMPRESS NOT ",
|
||||
"NOWAIT NULL NUMBER OF OFFLINE ON ONLINE OPTION OR ORDER PCTFREE PRIOR PRIVILEGES PUBLIC RAW RENAME ",
|
||||
"RESOURCE REVOKE ROW ROWID ROWNUM ROWS SELECT SESSION SET SHARE SIZE SMALLINT START SUCCESSFUL ",
|
||||
"SYNONYM SYSDATE TABLE THEN TO TRIGGER UID UNION UNIQUE UPDATE USER VALIDATE VALUES VARCHAR VARCHAR2 ",
|
||||
"VIEW WHENEVER WHERE WITH",
|
||||
)
|
||||
.split_ascii_whitespace()
|
||||
.collect()
|
||||
})
|
||||
.contains(name)
|
||||
}
|
||||
|
||||
fn dameng_reserved(name: &str) -> bool {
|
||||
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
|
||||
KEYWORDS
|
||||
.get_or_init(|| {
|
||||
concat!(
|
||||
"ABSOLUTE ABSTRACT ADD ADMIN ALL ALTER AND ANY ARRAY ARRAYLEN AS ASC ASSIGN AUDIT AUTHORIZATION ",
|
||||
"AUTO_INCREMENT BEGIN BETWEEN BIGDATEDIFF BINARY BOOL BOTH BREAK BSTRING BY BYTE CALL CASE CAST CATCH ",
|
||||
"CHAR CHECK CLASS CLUSTER CLUSTERBTR COLLATION COLLECT COLUMN COMMENT COMMIT COMMITWORK CONNECT ",
|
||||
"CONNECT_BY_ROOT CONST CONSTRAINT CONTAINS CONTEXT CONTINUE CONVERT CORRESPONDING CREATE CROSS CRYPTO ",
|
||||
"CUBE CURRENT CURSOR DATEADD DATEDIFF DATEPART DECIMAL DECLARE DECODE DEFAULT DELETE DESC DISABLE ",
|
||||
"DISKSPACE DISTINCT DISTRIBUTED DO DOMAIN DOUBLE DROP ELSE ELSEIF ELSIF ENABLE END EQU EXCHANGE EXEC ",
|
||||
"EXECUTE EXISTS EXIT EXPLAIN EXTERN EXTRACT FETCH FINAL FINALLY FIRST FLASHBACK FLOAT FOR FOREIGN ",
|
||||
"FROM FULL FULLY FUNCTION GET GOTO GRANT GROUP GROUPING HAVING IDENTITY IF IFNULL IMMEDIATE IN INDEX ",
|
||||
"INLINE INNER INSERT INT INTERSECT INTERVAL INTO IS JOIN KEEP LEADING LEFT LEXER LIKE LIST LNNVL ",
|
||||
"LOGIN LOOP MEMBER MINUS MULTISET NATURAL NEW NEXT NOCOPY NOCYCLE NOT NULL OBJECT OF ON OR ORDER OUT ",
|
||||
"OVER OVERLAY OVERRIDE PARTITION PENDANT PERCENT PRIMARY PRINT PRIOR PRIVATE PRIVILEGES PROCEDURE ",
|
||||
"PROTECTED PUBLIC RAISE RECORD REF REFERENCE REFERENCES REFERENCING RELATIVE REPEAT REPLICATE RETURN ",
|
||||
"RETURNING REVERSE REVOKE RIGHT ROLLBACK ROLLUP ROW ROWNUM ROWS SAVEPOINT SBYTE SCHEMA SEALED SECTION ",
|
||||
"SELECT SET SETS SHORT SIZEOF SOME STATIC STRUCT SUBPARTITION SWITCH SYNONYM TABLE THROW TIMESTAMPADD ",
|
||||
"TIMESTAMPDIFF TO TOP TRAILING TRIGGER TRIM TRUNCATE TRY TYPEDEF TYPEOF UINT ULONG UNION UNIQUE UNTIL ",
|
||||
"UPDATE USER USHORT USING VALUES VARRAY VERIFY VIEW VIRTUAL VOID WHEN WHENEVER WHERE WHILE WITH ",
|
||||
"WITHIN XMLAGG XMLPARSE XMLTABLE",
|
||||
)
|
||||
.split_ascii_whitespace()
|
||||
.collect()
|
||||
})
|
||||
.contains(name)
|
||||
}
|
||||
|
||||
fn mysql_reserved(name: &str) -> bool {
|
||||
static KEYWORDS: OnceLock<HashSet<&'static str>> = OnceLock::new();
|
||||
KEYWORDS
|
||||
.get_or_init(|| {
|
||||
concat!(
|
||||
"all analyse analyze and any array as asc asymmetric authorization binary both case cast check ",
|
||||
"collate collation column concurrently constraint create cross current_catalog current_date ",
|
||||
"current_role current_schema current_time current_timestamp current_user default deferrable desc ",
|
||||
"distinct do else end except false fetch for foreign freeze from full grant group having ilike in ",
|
||||
"initially inner intersect into is isnull join lateral leading left like limit localtime ",
|
||||
"localtimestamp natural not notnull null offset on only or order outer overlaps placing primary ",
|
||||
"references returning right select session_user similar some symmetric system_user table tablesample ",
|
||||
"then to trailing true union unique user using variadic verbose when where window with accessible ",
|
||||
"auto_increment change database databases delayed describe div dual enclosed escaped explain force ",
|
||||
"fulltext high_priority ignore index infile key keys kill linear lines load lock low_priority ",
|
||||
"master_ssl_verify_server_cert maxvalue mediumint mod no_write_to_binlog optimize optionally outfile ",
|
||||
"partition purge range read_write regexp release rename replace require rlike schema schemas ",
|
||||
"separator show spatial sql_big_result sql_calc_found_rows sql_small_result ssl starting ",
|
||||
"straight_join terminated tinyint unlock unsigned use utc_date utc_time utc_timestamp values ",
|
||||
"varbinary varchar write xor zerofill",
|
||||
)
|
||||
.split_ascii_whitespace()
|
||||
.collect()
|
||||
})
|
||||
.contains(name)
|
||||
}
|
||||
@@ -22,6 +22,231 @@ fn request(extractor: DataGridExtractorId) -> DataGridExtractRequest {
|
||||
}
|
||||
}
|
||||
|
||||
fn temporal_insert_request() -> DataGridExtractRequest {
|
||||
let mut native = request(DataGridExtractorId::SqlInserts);
|
||||
native.database_type = Some(DatabaseType::Oracle);
|
||||
native.columns = vec![column("CREATED_AT", 0), column("NOTE", 1), column("AMOUNT", 2)];
|
||||
native.selected_column_indexes = vec![0, 1, 2];
|
||||
native.rows = vec![vec![json!("2022-08-25T09:58:43Z"), json!("it's text"), json!(42)]];
|
||||
native.table_meta = Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("APP".into()),
|
||||
table_name: "EVENTS".into(),
|
||||
primary_keys: vec![],
|
||||
columns: Some(
|
||||
[("CREATED_AT", "DATE"), ("NOTE", "VARCHAR2"), ("AMOUNT", "NUMBER")]
|
||||
.into_iter()
|
||||
.map(|(name, data_type)| DataGridColumnInfo {
|
||||
name: name.into(),
|
||||
data_type: data_type.into(),
|
||||
is_nullable: true,
|
||||
is_primary_key: false,
|
||||
column_default: None,
|
||||
extra: None,
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
});
|
||||
native
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_temporal_format_contract_and_output() {
|
||||
let native = temporal_insert_request();
|
||||
assert_eq!(extract_data_grid_selection(native.clone()).unwrap().text,
|
||||
"INSERT INTO \"APP\".\"EVENTS\" (\"CREATED_AT\", \"NOTE\", \"AMOUNT\") VALUES (TO_DATE('2022-08-25 09:58:43', 'YYYY-MM-DD HH24:MI:SS'), 'it''s text', 42);");
|
||||
let mut payload = serde_json::to_value(&native).unwrap();
|
||||
payload["options"]["sql"]["quoteIdentifiers"] = json!(false);
|
||||
payload["options"]["sql"]["temporalFormat"] = json!("string");
|
||||
let portable: DataGridExtractRequest = serde_json::from_value(payload).unwrap();
|
||||
assert_eq!(
|
||||
extract_data_grid_selection(portable).unwrap().text,
|
||||
"INSERT INTO APP.EVENTS (CREATED_AT, NOTE, AMOUNT) VALUES ('2022-08-25 09:58:43', 'it''s text', 42);"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_portable_temporals_preserve_wall_time_and_escape_fallback_text() {
|
||||
for (data_type, value, native_literal, portable_literal) in [
|
||||
("DATE", "2022-08-25", "DATE '2022-08-25'", "'2022-08-25 00:00:00'"),
|
||||
(
|
||||
"TIMESTAMP(6)",
|
||||
"2022-08-25 09:58:43.123456",
|
||||
"TO_TIMESTAMP('2022-08-25 09:58:43.123456', 'YYYY-MM-DD HH24:MI:SS.FF')",
|
||||
"'2022-08-25 09:58:43'",
|
||||
),
|
||||
(
|
||||
"TIMESTAMP WITH TIME ZONE",
|
||||
"2022-08-25T09:58:43.123456+08:00",
|
||||
"TO_TIMESTAMP_TZ('2022-08-25 09:58:43.123456 +08:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM')",
|
||||
"'2022-08-25 09:58:43'",
|
||||
),
|
||||
(
|
||||
"TIMESTAMP WITH LOCAL TIME ZONE",
|
||||
"2022-08-25T09:58:43Z",
|
||||
"TO_TIMESTAMP_TZ('2022-08-25 09:58:43 +00:00', 'YYYY-MM-DD HH24:MI:SS TZH:TZM')",
|
||||
"'2022-08-25 09:58:43'",
|
||||
),
|
||||
("DATE", "not a date's value", "'not a date''s value'", "'not a date''s value'"),
|
||||
("VARCHAR2", "2022-08-25T09:58:43Z", "'2022-08-25T09:58:43Z'", "'2022-08-25T09:58:43Z'"),
|
||||
] {
|
||||
for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
|
||||
let mut request = temporal_insert_request();
|
||||
request.database_type = Some(database_type);
|
||||
request.rows[0][0] = json!(value);
|
||||
request.table_meta.as_mut().unwrap().columns.as_mut().unwrap()[0].data_type = data_type.into();
|
||||
let native = extract_data_grid_selection(request.clone()).unwrap().text;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
let portable = extract_data_grid_selection(request).unwrap().text;
|
||||
assert!(native.contains(&format!("VALUES ({native_literal}, 'it''s text', 42)")), "{native}");
|
||||
assert!(portable.contains(&format!("VALUES ({portable_literal}, 'it''s text', 42)")), "{portable}");
|
||||
}
|
||||
}
|
||||
for value in [Value::Null, json!(42), json!(true), json!("")] {
|
||||
let mut request = temporal_insert_request();
|
||||
request.rows[0][0] = value;
|
||||
let native = extract_data_grid_selection(request.clone()).unwrap().text;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
assert_eq!(extract_data_grid_selection(request).unwrap().text, native);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_quote_opt_out_keeps_required_delimiters_and_namespaces() {
|
||||
for (database_type, quote, name, expected) in [
|
||||
(DatabaseType::Oracle, None, "EVENTS", "EVENTS"),
|
||||
(DatabaseType::Oracle, None, "Events", "\"Events\""),
|
||||
(DatabaseType::Oracle, None, "ORDER", "\"ORDER\""),
|
||||
(DatabaseType::Oracle, None, "A\"B", "\"A\"\"B\""),
|
||||
(DatabaseType::Oracle, None, "EVENT LOG", "\"EVENT LOG\""),
|
||||
(DatabaseType::Mysql, None, "events", "events"),
|
||||
(DatabaseType::Mysql, None, "order", "`order`"),
|
||||
(DatabaseType::Mysql, None, "a`b", "`a``b`"),
|
||||
(DatabaseType::Postgres, None, "events", "events"),
|
||||
(DatabaseType::Postgres, None, "Events", "\"Events\""),
|
||||
(DatabaseType::SqlServer, None, "events", "events"),
|
||||
(DatabaseType::SqlServer, None, "a]b", "[a]]b]"),
|
||||
(DatabaseType::Dameng, None, "EVENTS", "EVENTS"),
|
||||
(DatabaseType::Dameng, None, "ABSOLUTE", "\"ABSOLUTE\""),
|
||||
(DatabaseType::Kingbase, Some("`"), "events", "events"),
|
||||
(DatabaseType::Kingbase, Some("`"), "order", "`order`"),
|
||||
(DatabaseType::Jdbc, Some("\""), "events", "events"),
|
||||
(DatabaseType::Jdbc, Some("\""), "Events", "\"Events\""),
|
||||
(DatabaseType::Spanner, Some("\""), "order", "\"order\""),
|
||||
] {
|
||||
for include_database_name in [true, false] {
|
||||
let mut request = request(DataGridExtractorId::SqlInserts);
|
||||
request.database_type = Some(database_type);
|
||||
request.identifier_quote = quote.map(str::to_string);
|
||||
request.columns = vec![column(name, 0)];
|
||||
request.selected_column_indexes = vec![0];
|
||||
request.rows = vec![vec![json!(1)]];
|
||||
request.table_meta = Some(DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("APP".into()),
|
||||
table_name: name.into(),
|
||||
primary_keys: vec![],
|
||||
columns: None,
|
||||
});
|
||||
request.options.sql.quote_identifiers = false;
|
||||
request.options.sql.include_database_name = include_database_name;
|
||||
let text = extract_data_grid_selection(request).unwrap().text;
|
||||
assert!(text.contains(&format!("{expected} ({expected}) VALUES (1);")), "{database_type:?}: {text}");
|
||||
if !include_database_name {
|
||||
assert_eq!(text, format!("INSERT INTO {expected} ({expected}) VALUES (1);"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_portable_options_keep_insert_modes_and_type_fallbacks() {
|
||||
for insert_mode in
|
||||
[crate::data_grid_sql::DataGridCopyInsertMode::Merged, crate::data_grid_sql::DataGridCopyInsertMode::RowByRow]
|
||||
{
|
||||
let mut request = temporal_insert_request();
|
||||
request.database_type = Some(DatabaseType::Mysql);
|
||||
request.rows.push(vec![Value::Null, json!("O'Reilly"), json!(0)]);
|
||||
request.options.sql.quote_identifiers = false;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
request.options.sql.insert_mode = insert_mode;
|
||||
request.options.sql.include_database_name = false;
|
||||
let result = extract_data_grid_selection(request).unwrap();
|
||||
assert!(result.text.contains("('2022-08-25 09:58:43', 'it''s text', 42)"));
|
||||
assert!(result.text.contains("(NULL, 'O''Reilly', 0)"));
|
||||
assert_eq!(
|
||||
result.text.matches("INSERT INTO EVENTS").count(),
|
||||
if insert_mode == crate::data_grid_sql::DataGridCopyInsertMode::Merged { 1 } else { 2 }
|
||||
);
|
||||
}
|
||||
let mut request = temporal_insert_request();
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
request.table_meta = None;
|
||||
let text = extract_data_grid_selection(request).unwrap().text;
|
||||
assert!(text.contains("'2022-08-25T09:58:43Z'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_string_format_preserves_binary_timestamps_and_numeric_epochs() {
|
||||
for (database_type, data_type, value) in [
|
||||
(DatabaseType::SqlServer, "timestamp", "0x00000000000007D3"),
|
||||
(DatabaseType::Iotdb, "TIMESTAMP", "1747308643123456789"),
|
||||
] {
|
||||
let mut request = temporal_insert_request();
|
||||
request.database_type = Some(database_type);
|
||||
request.table_meta.as_mut().unwrap().columns.as_mut().unwrap()[0].data_type = data_type.into();
|
||||
request.rows[0][0] = json!(value);
|
||||
let native = extract_data_grid_selection(request.clone()).unwrap().text;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
assert_eq!(extract_data_grid_selection(request).unwrap().text, native);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_insert_policy_contract_rejects_unknown_modes_and_invalid_boolean_types() {
|
||||
for invalid in [json!({"sql": {"temporalFormat": "custom"}}), json!({"sql": {"quoteIdentifiers": "false"}})] {
|
||||
assert!(serde_json::from_value::<DataGridExtractorOptions>(invalid).is_err());
|
||||
}
|
||||
let options: DataGridExtractorOptions =
|
||||
serde_json::from_value(json!({"sql": {"temporalFormat": "string", "quoteIdentifiers": false}})).unwrap();
|
||||
let roundtrip: DataGridExtractorOptions = serde_json::from_value(serde_json::to_value(options).unwrap()).unwrap();
|
||||
assert_eq!(roundtrip.sql.temporal_format, DataGridTemporalFormat::String);
|
||||
assert!(!roundtrip.sql.quote_identifiers);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_policies_do_not_change_other_extractors_or_column_errors() {
|
||||
for extractor in [
|
||||
DataGridExtractorId::Csv,
|
||||
DataGridExtractorId::Tsv,
|
||||
DataGridExtractorId::Json,
|
||||
DataGridExtractorId::JsonLines,
|
||||
DataGridExtractorId::Markdown,
|
||||
DataGridExtractorId::Html,
|
||||
DataGridExtractorId::Xml,
|
||||
DataGridExtractorId::SqlUpdates,
|
||||
DataGridExtractorId::SqlInList,
|
||||
DataGridExtractorId::WhereClause,
|
||||
] {
|
||||
let mut request = temporal_insert_request();
|
||||
request.extractor = extractor;
|
||||
request.table_meta.as_mut().unwrap().primary_keys = vec!["AMOUNT".into()];
|
||||
let native = extract_data_grid_selection(request.clone()).unwrap().text;
|
||||
request.options.sql.quote_identifiers = false;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
assert_eq!(extract_data_grid_selection(request).unwrap().text, native, "{extractor:?}");
|
||||
}
|
||||
let mut request = temporal_insert_request();
|
||||
request.options.sql.quote_identifiers = false;
|
||||
request.options.sql.temporal_format = DataGridTemporalFormat::String;
|
||||
for info in request.table_meta.as_mut().unwrap().columns.as_mut().unwrap() {
|
||||
info.extra = Some("VIRTUAL GENERATED".into());
|
||||
}
|
||||
assert_eq!(extract_data_grid_selection(request).unwrap_err().code, DataGridExtractErrorCode::NoWritableColumns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partially_deserialized_options_use_the_canonical_defaults() {
|
||||
let options = serde_json::from_value::<DataGridExtractorOptions>(json!({
|
||||
@@ -43,6 +268,8 @@ fn partially_deserialized_options_use_the_canonical_defaults() {
|
||||
assert_eq!(options.sql.insert_mode, crate::data_grid_sql::DataGridCopyInsertMode::Merged);
|
||||
assert!(!options.sql.exclude_primary_keys_from_insert);
|
||||
assert!(options.sql.include_database_name);
|
||||
assert!(options.sql.quote_identifiers);
|
||||
assert_eq!(options.sql.temporal_format, DataGridTemporalFormat::Native);
|
||||
assert!(options.json.pretty);
|
||||
assert!(!options.json.camel_case_field_names);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user