fix(sqlserver): order top query pages by exposed column
Co-authored-by: zipg <4047349+zipg@users.noreply.github.com>
This commit is contained in:
1 parent
e67e7730d1
commit
24ac0f7413
1 file changed
+78
-7
@@ -441,10 +441,10 @@ fn add_sql_server_existing_top_pagination(statement: &str, limit: usize, offset:
|
||||
}
|
||||
|
||||
fn sql_server_default_pagination_order(statement: &str) -> String {
|
||||
first_simple_sqlserver_projection(statement).unwrap_or_else(|| "(SELECT NULL)".to_string())
|
||||
first_simple_sqlserver_projection_order_column(statement).unwrap_or_else(|| "(SELECT NULL)".to_string())
|
||||
}
|
||||
|
||||
fn first_simple_sqlserver_projection(statement: &str) -> Option<String> {
|
||||
fn first_simple_sqlserver_projection_order_column(statement: &str) -> Option<String> {
|
||||
let sql = statement.trim();
|
||||
let sql = &sql[skip_leading_sql_comments(sql, 0)..];
|
||||
if sql.len() < 6 || !sql[..6].eq_ignore_ascii_case("SELECT") {
|
||||
@@ -463,11 +463,7 @@ fn first_simple_sqlserver_projection(statement: &str) -> Option<String> {
|
||||
let projection_start = skip_sql_whitespace(sql, index);
|
||||
let projection_end = find_first_projection_end(sql, projection_start)?;
|
||||
let projection = sql[projection_start..projection_end].trim();
|
||||
if is_simple_sqlserver_order_projection(projection) {
|
||||
Some(projection.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
sql_server_derived_order_column_from_projection(projection)
|
||||
}
|
||||
|
||||
fn skip_leading_sql_comments(sql: &str, mut index: usize) -> usize {
|
||||
@@ -607,6 +603,37 @@ fn is_simple_sqlserver_order_projection(projection: &str) -> bool {
|
||||
saw_part && !expect_part
|
||||
}
|
||||
|
||||
fn sql_server_derived_order_column_from_projection(projection: &str) -> Option<String> {
|
||||
if !is_simple_sqlserver_order_projection(projection) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let last_part = last_sqlserver_identifier_part(projection);
|
||||
if last_part.starts_with('[') {
|
||||
return Some(last_part.to_string());
|
||||
}
|
||||
Some(quote_table_identifier(Some(DatabaseType::SqlServer), last_part))
|
||||
}
|
||||
|
||||
fn last_sqlserver_identifier_part(projection: &str) -> &str {
|
||||
let mut last_start = 0;
|
||||
let mut index = 0;
|
||||
while index < projection.len() {
|
||||
let ch = next_char(projection, index);
|
||||
if ch == '[' {
|
||||
index = skip_sql_bracket_identifier(projection, index);
|
||||
continue;
|
||||
}
|
||||
if ch == '.' {
|
||||
last_start = index + 1;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
index += ch.len_utf8();
|
||||
}
|
||||
projection[last_start..].trim()
|
||||
}
|
||||
|
||||
fn skip_sql_whitespace(sql: &str, mut index: usize) -> usize {
|
||||
while index < sql.len() && next_char(sql, index).is_whitespace() {
|
||||
index += next_char(sql, index).len_utf8();
|
||||
@@ -1430,6 +1457,50 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paginates_sqlserver_top_query_with_qualified_first_projection_by_exposed_column() {
|
||||
let sql = "select top 1 t1.FSUPPLIERID, kh.FNAME gysnm from GDWORKOUT t1 join SUPPLIER kh on t1.FSUPPLIERID=kh.FITEMID join GDWORKOUTS t2 on t1.fid=t2.fid";
|
||||
let first_page = build_paginated_query_sql(PaginatedQuerySqlOptions {
|
||||
original_sql: sql.to_string(),
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
});
|
||||
let second_page = build_paginated_query_sql(PaginatedQuerySqlOptions {
|
||||
original_sql: sql.to_string(),
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
limit: 100,
|
||||
offset: 100,
|
||||
});
|
||||
|
||||
assert!(first_page.ok);
|
||||
assert!(second_page.ok);
|
||||
assert_eq!(
|
||||
first_page.sql.unwrap(),
|
||||
"SELECT TOP (100) * FROM (select top 1 t1.FSUPPLIERID, kh.FNAME gysnm from GDWORKOUT t1 join SUPPLIER kh on t1.FSUPPLIERID=kh.FITEMID join GDWORKOUTS t2 on t1.fid=t2.fid) [dbx_page] ORDER BY [FSUPPLIERID];"
|
||||
);
|
||||
assert_eq!(
|
||||
second_page.sql.unwrap(),
|
||||
"SELECT * FROM (SELECT dbx_page_source.*, ROW_NUMBER() OVER (ORDER BY [FSUPPLIERID]) AS [__dbx_row_num] FROM (select top 1 t1.FSUPPLIERID, kh.FNAME gysnm from GDWORKOUT t1 join SUPPLIER kh on t1.FSUPPLIERID=kh.FITEMID join GDWORKOUTS t2 on t1.fid=t2.fid) dbx_page_source) dbx_page WHERE [__dbx_row_num] > 100 AND [__dbx_row_num] <= 200 ORDER BY [__dbx_row_num];"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paginates_sqlserver_top_query_with_dotted_bracket_identifier() {
|
||||
let result = build_paginated_query_sql(PaginatedQuerySqlOptions {
|
||||
original_sql: "SELECT TOP 1000 [order.id] FROM TicketInfo".to_string(),
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
});
|
||||
|
||||
assert!(result.ok);
|
||||
assert_eq!(
|
||||
result.sql.unwrap(),
|
||||
"SELECT TOP (100) * FROM (SELECT TOP 1000 [order.id] FROM TicketInfo) [dbx_page] ORDER BY [order.id];"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paginates_sqlserver_top_query_after_leading_comment_by_first_column() {
|
||||
let sql = "-- 测试\nSELECT TOP (500) [id], [order_no], [store_id], [product_id], [customer_name], [quantity], [amount], [order_status], [created_at] FROM [sales].[orders_10k]";
|
||||
|
||||
Reference in new issue
Block a user