feat(sqlserver): add sql server 2000 legacy support
This commit is contained in:
20 files changed
+682
-24
No files matched your search
@@ -235,6 +235,8 @@ pub struct DataGridColumnDistinctValuesSqlOptions {
|
||||
#[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 identifier_quote: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub catalog: Option<String>,
|
||||
@@ -892,6 +894,9 @@ pub fn build_data_grid_column_distinct_values_sql(options: DataGridColumnDistinc
|
||||
let from_clause = format!(" FROM {table}{where_clause}{group_by}{order_by}");
|
||||
|
||||
match table_pagination_strategy(options.database_type) {
|
||||
TablePaginationStrategy::SqlServerTop if is_sqlserver_legacy_profile(options.driver_profile.as_deref()) => {
|
||||
format!("SELECT {select_list}{from_clause}")
|
||||
}
|
||||
TablePaginationStrategy::SqlServerTop => format!("SELECT TOP ({limit}) {select_list}{from_clause}"),
|
||||
TablePaginationStrategy::IrisTop => format!("SELECT TOP {limit} {select_list}{from_clause}"),
|
||||
TablePaginationStrategy::InformixFirst => format!("SELECT FIRST {limit} {select_list}{from_clause}"),
|
||||
@@ -915,6 +920,10 @@ pub fn build_data_grid_column_distinct_values_sql(options: DataGridColumnDistinc
|
||||
}
|
||||
}
|
||||
|
||||
fn is_sqlserver_legacy_profile(driver_profile: Option<&str>) -> bool {
|
||||
driver_profile.is_some_and(|profile| profile.trim().eq_ignore_ascii_case("sqlserver-legacy"))
|
||||
}
|
||||
|
||||
pub fn build_data_grid_count_sql(options: DataGridCountSqlOptions) -> String {
|
||||
let table = if crate::sql_dialect::uses_connection_identifier_quote(
|
||||
options.database_type,
|
||||
@@ -4730,6 +4739,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
@@ -4747,6 +4757,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
@@ -4764,6 +4775,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
@@ -4778,9 +4790,28 @@ mod tests {
|
||||
}),
|
||||
"SELECT TOP (25) [id] AS dbx_value, COUNT(*) AS dbx_count FROM [users] WHERE [id] = 42 GROUP BY [id] ORDER BY dbx_count DESC, dbx_value"
|
||||
);
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
driver_profile: Some(" SQLSERVER-LEGACY ".to_string()),
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: None,
|
||||
table_name: "users".to_string(),
|
||||
column_name: "status".to_string(),
|
||||
column_info: Some(column("status", "nvarchar", true, None)),
|
||||
where_input: None,
|
||||
search_value: None,
|
||||
limit: Some(25),
|
||||
include_counts: true,
|
||||
}),
|
||||
"SELECT [status] AS dbx_value, COUNT(*) AS dbx_count FROM [users] GROUP BY [status] ORDER BY dbx_count DESC, dbx_value"
|
||||
);
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::Oracle),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
@@ -4798,6 +4829,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::Firebird),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
database: None,
|
||||
@@ -4816,6 +4848,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::Doris),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: Some("iceberg_catalog".to_string()),
|
||||
database: None,
|
||||
@@ -4833,6 +4866,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::StarRocks),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: Some("hive_catalog".to_string()),
|
||||
database: None,
|
||||
@@ -4851,6 +4885,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_column_distinct_values_sql(DataGridColumnDistinctValuesSqlOptions {
|
||||
database_type: Some(DatabaseType::Doris),
|
||||
driver_profile: None,
|
||||
identifier_quote: None,
|
||||
catalog: Some("internal".to_string()),
|
||||
database: None,
|
||||
|
||||
@@ -21,6 +21,8 @@ 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)]
|
||||
@@ -92,7 +94,7 @@ pub fn build_database_search_sql(options: DatabaseSearchSqlOptions) -> Option<Da
|
||||
let identifier = quote_table_identifier(options.database_type, &column.name);
|
||||
conditions.push(format!(
|
||||
"{} LIKE {} ESCAPE '~'",
|
||||
text_cast_expression(options.database_type, &identifier),
|
||||
text_cast_expression(options.database_type, options.driver_profile.as_deref(), &identifier),
|
||||
sql_string_literal(&like_pattern(&term))
|
||||
));
|
||||
}
|
||||
@@ -229,9 +231,14 @@ fn like_pattern(term: &str) -> String {
|
||||
pattern
|
||||
}
|
||||
|
||||
fn text_cast_expression(database_type: Option<DatabaseType>, identifier: &str) -> String {
|
||||
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)))")
|
||||
@@ -278,6 +285,7 @@ mod tests {
|
||||
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)],
|
||||
@@ -293,10 +301,30 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[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)],
|
||||
@@ -314,6 +342,7 @@ mod tests {
|
||||
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)],
|
||||
@@ -334,6 +363,7 @@ mod tests {
|
||||
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)],
|
||||
|
||||
@@ -131,6 +131,14 @@ pub fn completion_context_sql() -> &'static str {
|
||||
SQLSERVER_COMPLETION_CONTEXT_SQL
|
||||
}
|
||||
|
||||
pub fn completion_context_sql_for_profile(driver_profile: Option<&str>) -> &'static str {
|
||||
if driver_profile.is_some_and(|profile| profile.trim().eq_ignore_ascii_case(SQLSERVER_LEGACY_DRIVER_PROFILE)) {
|
||||
"SELECT TOP 1 u.name AS default_schema, 1 AS engine_edition FROM sysusers u WHERE u.name = USER_NAME()"
|
||||
} else {
|
||||
completion_context_sql()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn completion_context_from_query_result(result: QueryResult) -> Result<SqlServerCompletionContext, String> {
|
||||
let row = result.rows.first().ok_or_else(|| "SQL Server completion context query returned no rows".to_string())?;
|
||||
let default_schema = row.first().and_then(serde_json::Value::as_str);
|
||||
@@ -4163,6 +4171,17 @@ mod tests {
|
||||
assert!(SQLSERVER_COMPLETION_CONTEXT_SQL.contains("EngineEdition"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_legacy_completion_context_uses_sql_server_2000_catalogs() {
|
||||
let sql = super::completion_context_sql_for_profile(Some(" SQLSERVER-LEGACY "));
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 1 u.name AS default_schema, 1 AS engine_edition FROM sysusers u WHERE u.name = USER_NAME()"
|
||||
);
|
||||
assert!(!sql.contains("sys.schemas"));
|
||||
assert!(!sql.contains("SERVERPROPERTY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_completion_context_disables_use_for_azure_database_endpoints() {
|
||||
assert!(!sqlserver_supports_session_database_switch(5));
|
||||
|
||||
@@ -290,6 +290,9 @@ pub async fn get_sqlserver_completion_context_core(
|
||||
retry_metadata_connection(state, connection_id, Some(database), || async {
|
||||
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
|
||||
let db_config = connection_config(state, connection_id).await;
|
||||
let completion_context_sql = db::sqlserver::completion_context_sql_for_profile(
|
||||
db_config.as_ref().and_then(|config| config.driver_profile.as_deref()),
|
||||
);
|
||||
let connections = state.connections.read().await;
|
||||
if let Some(PoolKind::ExternalDriver { config, session, .. }) = connections.get(&pool_key) {
|
||||
let config = config.clone();
|
||||
@@ -301,7 +304,7 @@ pub async fn get_sqlserver_completion_context_core(
|
||||
serde_json::json!({
|
||||
"connection": config.as_ref(),
|
||||
"database": database,
|
||||
"sql": db::sqlserver::completion_context_sql(),
|
||||
"sql": completion_context_sql,
|
||||
"maxRows": 1
|
||||
}),
|
||||
agent_metadata_timeout(Some(config.as_ref())),
|
||||
@@ -316,7 +319,7 @@ pub async fn get_sqlserver_completion_context_core(
|
||||
let result = client
|
||||
.execute_query_with_timeout::<db::QueryResult>(
|
||||
agent_execute_query_params(
|
||||
db::sqlserver::completion_context_sql(),
|
||||
completion_context_sql,
|
||||
if database.is_empty() { None } else { Some(database) },
|
||||
None,
|
||||
QueryExecutionOptions { max_rows: Some(1), ..Default::default() },
|
||||
|
||||
@@ -359,6 +359,10 @@ pub fn build_table_data_select_sql_with_database(
|
||||
&options.columns,
|
||||
limit,
|
||||
options.offset.unwrap_or(0),
|
||||
options
|
||||
.driver_profile
|
||||
.as_deref()
|
||||
.is_some_and(|profile| profile.trim().eq_ignore_ascii_case("sqlserver-legacy")),
|
||||
),
|
||||
TablePaginationStrategy::QuestDbLimit => build_questdb_table_select_sql(
|
||||
&table_alias,
|
||||
@@ -641,6 +645,7 @@ pub(super) fn build_sqlserver_table_select_sql(
|
||||
columns: &[String],
|
||||
limit: usize,
|
||||
offset: usize,
|
||||
legacy_compatible: bool,
|
||||
) -> String {
|
||||
let columns_sql = if columns.is_empty() {
|
||||
"*".to_string()
|
||||
@@ -652,6 +657,9 @@ pub(super) fn build_sqlserver_table_select_sql(
|
||||
.join(", ")
|
||||
};
|
||||
let order = if order_by == "(SELECT NULL)" { String::new() } else { format!(" ORDER BY {order_by}") };
|
||||
if legacy_compatible {
|
||||
return format!("SELECT {columns_sql} FROM {table}{where_clause}{order}");
|
||||
}
|
||||
if offset == 0 {
|
||||
return format!("SELECT TOP ({limit}) {columns_sql} FROM {table}{where_clause}{order}");
|
||||
}
|
||||
@@ -893,4 +901,16 @@ mod tests {
|
||||
r#"count({__name__="rack\\\"temperature"})"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_legacy_table_preview_leaves_paging_to_the_agent_cursor() {
|
||||
let mut options = opts(DatabaseType::SqlServer, None, None, "users");
|
||||
options.driver_profile = Some(" SQLSERVER-LEGACY ".to_string());
|
||||
options.columns = vec!["id".to_string(), "name".to_string()];
|
||||
options.order_by = Some("[id] ASC".to_string());
|
||||
options.limit = Some(100);
|
||||
options.offset = Some(100);
|
||||
|
||||
assert_eq!(build_table_data_select_sql(options), "SELECT [id], [name] FROM [users] ORDER BY [id] ASC");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user