1use crate::sql::parser::ast::*;
8use std::fmt::Write;
9
10pub struct FormatConfig {
12 pub indent: String,
14 pub items_per_line: usize,
16 pub uppercase_keywords: bool,
18 pub compact: bool,
20}
21
22impl Default for FormatConfig {
23 fn default() -> Self {
24 Self {
25 indent: " ".to_string(),
26 items_per_line: 5,
27 uppercase_keywords: true,
28 compact: false,
29 }
30 }
31}
32
33pub fn format_select_statement(stmt: &SelectStatement) -> String {
35 format_select_with_config(stmt, &FormatConfig::default())
36}
37
38pub fn format_select_with_config(stmt: &SelectStatement, config: &FormatConfig) -> String {
40 let formatter = AstFormatter::new(config);
41 formatter.format_select(stmt, 0)
42}
43
44pub fn format_expression(expr: &SqlExpression) -> String {
47 let config = FormatConfig::default();
48 let formatter = AstFormatter::new(&config);
49 formatter.format_expression(expr)
50}
51
52struct AstFormatter<'a> {
53 config: &'a FormatConfig,
54}
55
56impl<'a> AstFormatter<'a> {
57 fn new(config: &'a FormatConfig) -> Self {
58 Self { config }
59 }
60
61 fn keyword(&self, word: &str) -> String {
62 if self.config.uppercase_keywords {
63 word.to_uppercase()
64 } else {
65 word.to_lowercase()
66 }
67 }
68
69 fn indent(&self, level: usize) -> String {
70 self.config.indent.repeat(level)
71 }
72
73 fn format_select(&self, stmt: &SelectStatement, indent_level: usize) -> String {
74 let mut result = String::new();
75 let indent = self.indent(indent_level);
76
77 for comment in &stmt.leading_comments {
79 self.format_comment(&mut result, comment, &indent);
80 }
81
82 if !stmt.ctes.is_empty() {
84 writeln!(&mut result, "{}{}", indent, self.keyword("WITH")).unwrap();
85 for (i, cte) in stmt.ctes.iter().enumerate() {
86 let is_last = i == stmt.ctes.len() - 1;
87 self.format_cte(&mut result, cte, indent_level + 1, is_last);
88 }
89 }
90
91 if !stmt.leading_comments.is_empty() || !stmt.ctes.is_empty() {
93 writeln!(&mut result, "{}{}", indent, self.keyword("SELECT")).unwrap();
94 } else {
95 write!(&mut result, "{}{}", indent, self.keyword("SELECT")).unwrap();
96 }
97 if stmt.distinct {
98 write!(&mut result, " {}", self.keyword("DISTINCT")).unwrap();
99 }
100
101 if stmt.select_items.is_empty() && !stmt.columns.is_empty() {
103 self.format_column_list(&mut result, &stmt.columns, indent_level);
105 } else {
106 self.format_select_items(&mut result, &stmt.select_items, indent_level);
107 }
108
109 if let Some(ref into_table) = stmt.into_table {
111 writeln!(&mut result).unwrap();
112 write!(
113 &mut result,
114 "{}{} {}",
115 indent,
116 self.keyword("INTO"),
117 into_table.name
118 )
119 .unwrap();
120 }
121
122 if let Some(ref table) = stmt.from_table {
124 writeln!(&mut result).unwrap();
125 write!(&mut result, "{}{} {}", indent, self.keyword("FROM"), table).unwrap();
126 } else if let Some(ref subquery) = stmt.from_subquery {
127 writeln!(&mut result).unwrap();
128 write!(&mut result, "{}{} (", indent, self.keyword("FROM")).unwrap();
129 writeln!(&mut result).unwrap();
130 let subquery_sql = self.format_select(subquery, indent_level + 1);
131 write!(&mut result, "{}", subquery_sql).unwrap();
132 write!(&mut result, "\n{}", indent).unwrap();
133 write!(&mut result, ")").unwrap();
134 if let Some(ref alias) = stmt.from_alias {
135 write!(&mut result, " {} {}", self.keyword("AS"), alias).unwrap();
136 }
137 } else if let Some(ref func) = stmt.from_function {
138 writeln!(&mut result).unwrap();
139 write!(&mut result, "{}{} ", indent, self.keyword("FROM")).unwrap();
140 self.format_table_function(&mut result, func);
141 if let Some(ref alias) = stmt.from_alias {
142 write!(&mut result, " {} {}", self.keyword("AS"), alias).unwrap();
143 }
144 }
145
146 for join in &stmt.joins {
148 writeln!(&mut result).unwrap();
149 self.format_join(&mut result, join, indent_level);
150 }
151
152 if let Some(ref where_clause) = stmt.where_clause {
154 writeln!(&mut result).unwrap();
155 write!(&mut result, "{}{}", indent, self.keyword("WHERE")).unwrap();
156 self.format_where_clause(&mut result, where_clause, indent_level);
157 }
158
159 if let Some(ref group_by) = stmt.group_by {
161 writeln!(&mut result).unwrap();
162 write!(&mut result, "{}{} ", indent, self.keyword("GROUP BY")).unwrap();
163 for (i, expr) in group_by.iter().enumerate() {
164 if i > 0 {
165 write!(&mut result, ", ").unwrap();
166 }
167 write!(&mut result, "{}", self.format_expression(expr)).unwrap();
168 }
169 }
170
171 if let Some(ref having) = stmt.having {
173 writeln!(&mut result).unwrap();
174 write!(
175 &mut result,
176 "{}{} {}",
177 indent,
178 self.keyword("HAVING"),
179 self.format_expression(having)
180 )
181 .unwrap();
182 }
183
184 if let Some(ref order_by) = stmt.order_by {
186 writeln!(&mut result).unwrap();
187 write!(&mut result, "{}{} ", indent, self.keyword("ORDER BY")).unwrap();
188 for (i, col) in order_by.iter().enumerate() {
189 if i > 0 {
190 write!(&mut result, ", ").unwrap();
191 }
192 write!(&mut result, "{}", self.format_expression(&col.expr)).unwrap();
193 match col.direction {
194 SortDirection::Asc => write!(&mut result, " {}", self.keyword("ASC")).unwrap(),
195 SortDirection::Desc => {
196 write!(&mut result, " {}", self.keyword("DESC")).unwrap()
197 }
198 }
199 }
200 }
201
202 if let Some(limit) = stmt.limit {
204 writeln!(&mut result).unwrap();
205 write!(&mut result, "{}{} {}", indent, self.keyword("LIMIT"), limit).unwrap();
206 }
207
208 if let Some(offset) = stmt.offset {
210 writeln!(&mut result).unwrap();
211 write!(
212 &mut result,
213 "{}{} {}",
214 indent,
215 self.keyword("OFFSET"),
216 offset
217 )
218 .unwrap();
219 }
220
221 if let Some(ref comment) = stmt.trailing_comment {
223 write!(&mut result, " ").unwrap();
224 self.format_inline_comment(&mut result, comment);
225 }
226
227 result
228 }
229
230 fn format_comment(&self, result: &mut String, comment: &Comment, indent: &str) {
232 if comment.is_line_comment {
233 writeln!(result, "{}-- {}", indent, comment.text.trim()).unwrap();
234 } else {
235 writeln!(result, "{}/* {} */", indent, comment.text.trim()).unwrap();
237 }
238 }
239
240 fn format_inline_comment(&self, result: &mut String, comment: &Comment) {
242 if comment.is_line_comment {
243 write!(result, "-- {}", comment.text.trim()).unwrap();
244 } else {
245 write!(result, "/* {} */", comment.text.trim()).unwrap();
246 }
247 }
248
249 fn format_cte(&self, result: &mut String, cte: &CTE, indent_level: usize, is_last: bool) {
250 let indent = self.indent(indent_level);
251
252 let is_web = matches!(&cte.cte_type, crate::sql::parser::ast::CTEType::Web(_));
254 if is_web {
255 write!(result, "{}{} {}", indent, self.keyword("WEB"), cte.name).unwrap();
256 } else {
257 write!(result, "{}{}", indent, cte.name).unwrap();
258 }
259
260 if let Some(ref columns) = cte.column_list {
261 write!(result, "(").unwrap();
262 for (i, col) in columns.iter().enumerate() {
263 if i > 0 {
264 write!(result, ", ").unwrap();
265 }
266 write!(result, "{}", col).unwrap();
267 }
268 write!(result, ")").unwrap();
269 }
270
271 writeln!(result, " {} (", self.keyword("AS")).unwrap();
272 let cte_sql = match &cte.cte_type {
273 crate::sql::parser::ast::CTEType::Standard(query) => {
274 self.format_select(query, indent_level + 1)
275 }
276 crate::sql::parser::ast::CTEType::Web(web_spec) => {
277 let mut web_str = format!(
279 "{}{} '{}'",
280 " ".repeat(indent_level + 1),
281 self.keyword("URL"),
282 web_spec.url
283 );
284
285 if let Some(method) = &web_spec.method {
287 web_str.push_str(&format!(
288 " {} {}",
289 self.keyword("METHOD"),
290 match method {
291 crate::sql::parser::ast::HttpMethod::GET => "GET",
292 crate::sql::parser::ast::HttpMethod::POST => "POST",
293 crate::sql::parser::ast::HttpMethod::PUT => "PUT",
294 crate::sql::parser::ast::HttpMethod::DELETE => "DELETE",
295 crate::sql::parser::ast::HttpMethod::PATCH => "PATCH",
296 }
297 ));
298 }
299
300 if let Some(body) = &web_spec.body {
302 let trimmed_body = body.trim();
304 if (trimmed_body.starts_with('{') && trimmed_body.ends_with('}'))
305 || (trimmed_body.starts_with('[') && trimmed_body.ends_with(']'))
306 {
307 match serde_json::from_str::<serde_json::Value>(trimmed_body) {
309 Ok(json_val) => {
310 match serde_json::to_string_pretty(&json_val) {
312 Ok(pretty_json) => {
313 let is_complex = pretty_json.lines().count() > 1
315 || pretty_json.contains('"')
316 || pretty_json.contains('\\');
317
318 if is_complex {
319 let base_indent = " ".repeat(indent_level + 1);
321 let json_lines: Vec<String> = pretty_json
322 .lines()
323 .enumerate()
324 .map(|(i, line)| {
325 if i == 0 {
326 line.to_string()
327 } else {
328 format!("{}{}", base_indent, line)
329 }
330 })
331 .collect();
332 let formatted_json = json_lines.join("\n");
333
334 web_str.push_str(&format!(
335 " {} $JSON${}\n{}$JSON$\n{}",
336 self.keyword("BODY"),
337 formatted_json,
338 base_indent,
339 base_indent
340 ));
341 } else {
342 web_str.push_str(&format!(
344 " {} '{}'",
345 self.keyword("BODY"),
346 pretty_json
347 ));
348 }
349 }
350 Err(_) => {
351 web_str.push_str(&format!(
353 " {} '{}'",
354 self.keyword("BODY"),
355 body
356 ));
357 }
358 }
359 }
360 Err(_) => {
361 web_str.push_str(&format!(" {} '{}'", self.keyword("BODY"), body));
363 }
364 }
365 } else {
366 web_str.push_str(&format!(" {} '{}'", self.keyword("BODY"), body));
368 }
369 }
370
371 if let Some(format) = &web_spec.format {
373 web_str.push_str(&format!(
374 " {} {}",
375 self.keyword("FORMAT"),
376 match format {
377 crate::sql::parser::ast::DataFormat::CSV => "CSV",
378 crate::sql::parser::ast::DataFormat::JSON => "JSON",
379 crate::sql::parser::ast::DataFormat::Auto => "AUTO",
380 }
381 ));
382 }
383
384 if let Some(json_path) = &web_spec.json_path {
386 web_str.push_str(&format!(" {} '{}'", self.keyword("JSON_PATH"), json_path));
387 }
388
389 if let Some(cache) = web_spec.cache_seconds {
391 web_str.push_str(&format!(" {} {}", self.keyword("CACHE"), cache));
392 }
393
394 for (field_name, file_path) in &web_spec.form_files {
396 web_str.push_str(&format!(
397 "\n{}{} '{}' '{}'",
398 " ".repeat(indent_level + 1),
399 self.keyword("FORM_FILE"),
400 field_name,
401 file_path
402 ));
403 }
404
405 for (field_name, value) in &web_spec.form_fields {
407 let trimmed_value = value.trim();
409 if (trimmed_value.starts_with('{') && trimmed_value.ends_with('}'))
410 || (trimmed_value.starts_with('[') && trimmed_value.ends_with(']'))
411 {
412 match serde_json::from_str::<serde_json::Value>(trimmed_value) {
414 Ok(json_val) => {
415 match serde_json::to_string_pretty(&json_val) {
417 Ok(pretty_json) => {
418 let is_complex = pretty_json.lines().count() > 1
420 || pretty_json.contains('"')
421 || pretty_json.contains('\\');
422
423 if is_complex {
424 let base_indent = " ".repeat(indent_level + 1);
426 let json_lines: Vec<String> = pretty_json
427 .lines()
428 .enumerate()
429 .map(|(i, line)| {
430 if i == 0 {
431 line.to_string()
432 } else {
433 format!("{}{}", base_indent, line)
434 }
435 })
436 .collect();
437 let formatted_json = json_lines.join("\n");
438
439 web_str.push_str(&format!(
440 "\n{}{} '{}' $JSON${}\n{}$JSON$",
441 base_indent,
442 self.keyword("FORM_FIELD"),
443 field_name,
444 formatted_json,
445 base_indent
446 ));
447 } else {
448 web_str.push_str(&format!(
450 "\n{}{} '{}' '{}'",
451 " ".repeat(indent_level + 1),
452 self.keyword("FORM_FIELD"),
453 field_name,
454 pretty_json
455 ));
456 }
457 }
458 Err(_) => {
459 web_str.push_str(&format!(
461 "\n{}{} '{}' '{}'",
462 " ".repeat(indent_level + 1),
463 self.keyword("FORM_FIELD"),
464 field_name,
465 value
466 ));
467 }
468 }
469 }
470 Err(_) => {
471 web_str.push_str(&format!(
473 "\n{}{} '{}' '{}'",
474 " ".repeat(indent_level + 1),
475 self.keyword("FORM_FIELD"),
476 field_name,
477 value
478 ));
479 }
480 }
481 } else {
482 web_str.push_str(&format!(
484 "\n{}{} '{}' '{}'",
485 " ".repeat(indent_level + 1),
486 self.keyword("FORM_FIELD"),
487 field_name,
488 value
489 ));
490 }
491 }
492
493 if !web_spec.headers.is_empty() {
495 web_str.push_str(&format!(" {} (", self.keyword("HEADERS")));
496 for (i, (key, value)) in web_spec.headers.iter().enumerate() {
497 if i > 0 {
498 web_str.push_str(", ");
499 }
500 web_str.push_str(&format!("'{}': '{}'", key, value));
501 }
502 web_str.push(')');
503 }
504
505 web_str
506 }
507 };
508 write!(result, "{}", cte_sql).unwrap();
509 writeln!(result).unwrap();
510 write!(result, "{}", indent).unwrap();
511 if is_last {
512 writeln!(result, ")").unwrap();
513 } else {
514 writeln!(result, "),").unwrap();
515 }
516 }
517
518 fn format_column_list(&self, result: &mut String, columns: &[String], indent_level: usize) {
519 if columns.len() <= self.config.items_per_line {
520 write!(result, " ").unwrap();
522 for (i, col) in columns.iter().enumerate() {
523 if i > 0 {
524 write!(result, ", ").unwrap();
525 }
526 write!(result, "{}", col).unwrap();
527 }
528 } else {
529 writeln!(result).unwrap();
531 let indent = self.indent(indent_level + 1);
532 for (i, col) in columns.iter().enumerate() {
533 write!(result, "{}{}", indent, col).unwrap();
534 if i < columns.len() - 1 {
535 writeln!(result, ",").unwrap();
536 }
537 }
538 }
539 }
540
541 fn format_select_items(&self, result: &mut String, items: &[SelectItem], indent_level: usize) {
542 if items.is_empty() {
543 write!(result, " *").unwrap();
544 return;
545 }
546
547 let _non_star_count = items
549 .iter()
550 .filter(|i| !matches!(i, SelectItem::Star { .. }))
551 .count();
552
553 let has_complex_items = items.iter().any(|item| match item {
555 SelectItem::Expression { expr, .. } => self.is_complex_expression(expr),
556 _ => false,
557 });
558
559 let single_line_length: usize = items
561 .iter()
562 .map(|item| {
563 match item {
564 SelectItem::Star { .. } => 1,
565 SelectItem::StarExclude {
566 excluded_columns, ..
567 } => {
568 11 + excluded_columns.iter().map(|c| c.len()).sum::<usize>()
570 + (excluded_columns.len().saturating_sub(1) * 2) }
572 SelectItem::Column { column: col, .. } => col.name.len(),
573 SelectItem::Expression { expr, alias, .. } => {
574 self.format_expression(expr).len() + 4 + alias.len() }
576 }
577 })
578 .sum::<usize>()
579 + (items.len() - 1) * 2; let use_single_line = match items.len() {
585 1 => !has_complex_items, 2..=3 => !has_complex_items && single_line_length < 40, _ => false, };
589
590 if !use_single_line {
591 writeln!(result).unwrap();
593 let indent = self.indent(indent_level + 1);
594 for (i, item) in items.iter().enumerate() {
595 write!(result, "{}", indent).unwrap();
596 self.format_select_item(result, item);
597 if i < items.len() - 1 {
598 writeln!(result, ",").unwrap();
599 }
600 }
601 } else {
602 write!(result, " ").unwrap();
604 for (i, item) in items.iter().enumerate() {
605 if i > 0 {
606 write!(result, ", ").unwrap();
607 }
608 self.format_select_item(result, item);
609 }
610 }
611 }
612
613 fn is_complex_expression(&self, expr: &SqlExpression) -> bool {
614 match expr {
615 SqlExpression::CaseExpression { .. } => true,
616 SqlExpression::FunctionCall { .. } => true,
617 SqlExpression::WindowFunction { .. } => true,
618 SqlExpression::ScalarSubquery { .. } => true,
619 SqlExpression::InSubquery { .. } => true,
620 SqlExpression::NotInSubquery { .. } => true,
621 SqlExpression::BinaryOp { left, right, .. } => {
622 self.is_complex_expression(left) || self.is_complex_expression(right)
623 }
624 _ => false,
625 }
626 }
627
628 fn format_select_item(&self, result: &mut String, item: &SelectItem) {
629 match item {
630 SelectItem::Star { .. } => write!(result, "*").unwrap(),
631 SelectItem::StarExclude {
632 excluded_columns, ..
633 } => {
634 write!(
635 result,
636 "* {} ({})",
637 self.keyword("EXCLUDE"),
638 excluded_columns.join(", ")
639 )
640 .unwrap();
641 }
642 SelectItem::Column { column: col, .. } => write!(result, "{}", col.to_sql()).unwrap(),
643 SelectItem::Expression { expr, alias, .. } => {
644 write!(
645 result,
646 "{} {} {}",
647 self.format_expression(expr),
648 self.keyword("AS"),
649 alias
650 )
651 .unwrap();
652 }
653 }
654 }
655
656 fn format_expression(&self, expr: &SqlExpression) -> String {
657 match expr {
658 SqlExpression::Column(column_ref) => column_ref.to_sql(),
659 SqlExpression::StringLiteral(s) => format!("'{}'", s),
660 SqlExpression::NumberLiteral(n) => n.clone(),
661 SqlExpression::BooleanLiteral(b) => b.to_string().to_uppercase(),
662 SqlExpression::Null => self.keyword("NULL"),
663 SqlExpression::BinaryOp { left, op, right } => {
664 if op == "IS NULL" || op == "IS NOT NULL" {
666 format!("{} {}", self.format_expression(left), op)
667 } else {
668 format!(
669 "{} {} {}",
670 self.format_expression(left),
671 op,
672 self.format_expression(right)
673 )
674 }
675 }
676 SqlExpression::FunctionCall {
677 name,
678 args,
679 distinct,
680 } => {
681 let mut result = name.clone();
682 result.push('(');
683 if *distinct {
684 result.push_str(&self.keyword("DISTINCT"));
685 result.push(' ');
686 }
687 for (i, arg) in args.iter().enumerate() {
688 if i > 0 {
689 result.push_str(", ");
690 }
691 result.push_str(&self.format_expression(arg));
692 }
693 result.push(')');
694 result
695 }
696 SqlExpression::CaseExpression {
697 when_branches,
698 else_branch,
699 } => {
700 let mut result = String::new();
702 result.push_str(&self.keyword("CASE"));
703 result.push('\n');
704
705 for branch in when_branches {
707 result.push_str(" "); result.push_str(&format!(
709 "{} {} {} {}",
710 self.keyword("WHEN"),
711 self.format_expression(&branch.condition),
712 self.keyword("THEN"),
713 self.format_expression(&branch.result)
714 ));
715 result.push('\n');
716 }
717
718 if let Some(else_expr) = else_branch {
720 result.push_str(" "); result.push_str(&format!(
722 "{} {}",
723 self.keyword("ELSE"),
724 self.format_expression(else_expr)
725 ));
726 result.push('\n');
727 }
728
729 result.push_str(" "); result.push_str(&self.keyword("END"));
731 result
732 }
733 SqlExpression::SimpleCaseExpression {
734 expr,
735 when_branches,
736 else_branch,
737 } => {
738 let mut result = String::new();
740 result.push_str(&format!(
741 "{} {}",
742 self.keyword("CASE"),
743 self.format_expression(expr)
744 ));
745 result.push('\n');
746
747 for branch in when_branches {
749 result.push_str(" "); result.push_str(&format!(
751 "{} {} {} {}",
752 self.keyword("WHEN"),
753 self.format_expression(&branch.value),
754 self.keyword("THEN"),
755 self.format_expression(&branch.result)
756 ));
757 result.push('\n');
758 }
759
760 if let Some(else_expr) = else_branch {
762 result.push_str(" "); result.push_str(&format!(
764 "{} {}",
765 self.keyword("ELSE"),
766 self.format_expression(else_expr)
767 ));
768 result.push('\n');
769 }
770
771 result.push_str(" "); result.push_str(&self.keyword("END"));
773 result
774 }
775 SqlExpression::Between { expr, lower, upper } => {
776 format!(
777 "{} {} {} {} {}",
778 self.format_expression(expr),
779 self.keyword("BETWEEN"),
780 self.format_expression(lower),
781 self.keyword("AND"),
782 self.format_expression(upper)
783 )
784 }
785 SqlExpression::InList { expr, values } => {
786 let mut result =
787 format!("{} {} (", self.format_expression(expr), self.keyword("IN"));
788 for (i, val) in values.iter().enumerate() {
789 if i > 0 {
790 result.push_str(", ");
791 }
792 result.push_str(&self.format_expression(val));
793 }
794 result.push(')');
795 result
796 }
797 SqlExpression::NotInList { expr, values } => {
798 let mut result = format!(
799 "{} {} {} (",
800 self.format_expression(expr),
801 self.keyword("NOT"),
802 self.keyword("IN")
803 );
804 for (i, val) in values.iter().enumerate() {
805 if i > 0 {
806 result.push_str(", ");
807 }
808 result.push_str(&self.format_expression(val));
809 }
810 result.push(')');
811 result
812 }
813 SqlExpression::Not { expr } => {
814 format!("{} {}", self.keyword("NOT"), self.format_expression(expr))
815 }
816 SqlExpression::ScalarSubquery { query } => {
817 let subquery_str = self.format_select(query, 0);
819 if subquery_str.contains('\n') || subquery_str.len() > 60 {
820 format!("(\n{}\n)", self.format_select(query, 1))
822 } else {
823 format!("({})", subquery_str)
825 }
826 }
827 SqlExpression::InSubquery { expr, subquery } => {
828 let subquery_str = self.format_select(subquery, 0);
829 if subquery_str.contains('\n') || subquery_str.len() > 60 {
830 format!(
832 "{} {} (\n{}\n)",
833 self.format_expression(expr),
834 self.keyword("IN"),
835 self.format_select(subquery, 1)
836 )
837 } else {
838 format!(
840 "{} {} ({})",
841 self.format_expression(expr),
842 self.keyword("IN"),
843 subquery_str
844 )
845 }
846 }
847 SqlExpression::NotInSubquery { expr, subquery } => {
848 let subquery_str = self.format_select(subquery, 0);
849 if subquery_str.contains('\n') || subquery_str.len() > 60 {
850 format!(
852 "{} {} {} (\n{}\n)",
853 self.format_expression(expr),
854 self.keyword("NOT"),
855 self.keyword("IN"),
856 self.format_select(subquery, 1)
857 )
858 } else {
859 format!(
861 "{} {} {} ({})",
862 self.format_expression(expr),
863 self.keyword("NOT"),
864 self.keyword("IN"),
865 subquery_str
866 )
867 }
868 }
869 SqlExpression::MethodCall {
870 object,
871 method,
872 args,
873 } => {
874 let mut result = format!("{}.{}", object, method);
875 result.push('(');
876 for (i, arg) in args.iter().enumerate() {
877 if i > 0 {
878 result.push_str(", ");
879 }
880 result.push_str(&self.format_expression(arg));
881 }
882 result.push(')');
883 result
884 }
885 SqlExpression::ChainedMethodCall { base, method, args } => {
886 let mut result = format!("{}.{}", self.format_expression(base), method);
887 result.push('(');
888 for (i, arg) in args.iter().enumerate() {
889 if i > 0 {
890 result.push_str(", ");
891 }
892 result.push_str(&self.format_expression(arg));
893 }
894 result.push(')');
895 result
896 }
897 SqlExpression::WindowFunction {
898 name,
899 args,
900 window_spec,
901 } => {
902 let mut result = format!("{}(", name);
903
904 for (i, arg) in args.iter().enumerate() {
906 if i > 0 {
907 result.push_str(", ");
908 }
909 result.push_str(&self.format_expression(arg));
910 }
911 result.push_str(") ");
912 result.push_str(&self.keyword("OVER"));
913 result.push_str(" (");
914
915 if !window_spec.partition_by.is_empty() {
917 result.push_str(&self.keyword("PARTITION BY"));
918 result.push(' ');
919 for (i, col) in window_spec.partition_by.iter().enumerate() {
920 if i > 0 {
921 result.push_str(", ");
922 }
923 result.push_str(col);
924 }
925 }
926
927 if !window_spec.order_by.is_empty() {
929 if !window_spec.partition_by.is_empty() {
930 result.push(' ');
931 }
932 result.push_str(&self.keyword("ORDER BY"));
933 result.push(' ');
934 for (i, col) in window_spec.order_by.iter().enumerate() {
935 if i > 0 {
936 result.push_str(", ");
937 }
938 result.push_str(&self.format_expression(&col.expr));
939 match col.direction {
940 SortDirection::Asc => {
941 result.push(' ');
942 result.push_str(&self.keyword("ASC"));
943 }
944 SortDirection::Desc => {
945 result.push(' ');
946 result.push_str(&self.keyword("DESC"));
947 }
948 }
949 }
950 }
951
952 if let Some(frame) = &window_spec.frame {
954 if !window_spec.partition_by.is_empty() || !window_spec.order_by.is_empty() {
956 result.push(' ');
957 }
958
959 match frame.unit {
961 FrameUnit::Rows => result.push_str(&self.keyword("ROWS")),
962 FrameUnit::Range => result.push_str(&self.keyword("RANGE")),
963 }
964
965 result.push(' ');
966
967 if let Some(end) = &frame.end {
969 result.push_str(&self.keyword("BETWEEN"));
971 result.push(' ');
972 result.push_str(&self.format_frame_bound(&frame.start));
973 result.push(' ');
974 result.push_str(&self.keyword("AND"));
975 result.push(' ');
976 result.push_str(&self.format_frame_bound(end));
977 } else {
978 result.push_str(&self.format_frame_bound(&frame.start));
980 }
981 }
982
983 result.push(')');
984 result
985 }
986 SqlExpression::DateTimeConstructor {
987 year,
988 month,
989 day,
990 hour,
991 minute,
992 second,
993 } => {
994 if let (Some(h), Some(m), Some(s)) = (hour, minute, second) {
995 format!(
996 "DateTime({}, {}, {}, {}, {}, {})",
997 year, month, day, h, m, s
998 )
999 } else {
1000 format!("DateTime({}, {}, {})", year, month, day)
1001 }
1002 }
1003 SqlExpression::DateTimeToday {
1004 hour,
1005 minute,
1006 second,
1007 } => {
1008 if let (Some(h), Some(m), Some(s)) = (hour, minute, second) {
1009 format!("Today({}, {}, {})", h, m, s)
1010 } else {
1011 "Today()".to_string()
1012 }
1013 }
1014 _ => format!("{:?}", expr), }
1016 }
1017
1018 fn format_where_clause(
1019 &self,
1020 result: &mut String,
1021 where_clause: &WhereClause,
1022 indent_level: usize,
1023 ) {
1024 let needs_multiline = where_clause.conditions.len() > 1;
1025
1026 if needs_multiline {
1027 writeln!(result).unwrap();
1028 let indent = self.indent(indent_level + 1);
1029 for (i, condition) in where_clause.conditions.iter().enumerate() {
1030 if i > 0 {
1031 if let Some(ref connector) = where_clause.conditions[i - 1].connector {
1032 let connector_str = match connector {
1033 LogicalOp::And => self.keyword("AND"),
1034 LogicalOp::Or => self.keyword("OR"),
1035 };
1036 writeln!(result).unwrap();
1037 write!(result, "{}{} ", indent, connector_str).unwrap();
1038 }
1039 } else {
1040 write!(result, "{}", indent).unwrap();
1041 }
1042 write!(result, "{}", self.format_expression(&condition.expr)).unwrap();
1043 }
1044 } else if let Some(condition) = where_clause.conditions.first() {
1045 write!(result, " {}", self.format_expression(&condition.expr)).unwrap();
1046 }
1047 }
1048
1049 fn format_frame_bound(&self, bound: &FrameBound) -> String {
1050 match bound {
1051 FrameBound::UnboundedPreceding => self.keyword("UNBOUNDED PRECEDING"),
1052 FrameBound::CurrentRow => self.keyword("CURRENT ROW"),
1053 FrameBound::UnboundedFollowing => self.keyword("UNBOUNDED FOLLOWING"),
1054 FrameBound::Preceding(n) => format!("{} {}", n, self.keyword("PRECEDING")),
1055 FrameBound::Following(n) => format!("{} {}", n, self.keyword("FOLLOWING")),
1056 }
1057 }
1058
1059 fn format_join(&self, result: &mut String, join: &JoinClause, indent_level: usize) {
1060 let indent = self.indent(indent_level);
1061 let join_type = match join.join_type {
1062 JoinType::Inner => self.keyword("INNER JOIN"),
1063 JoinType::Left => self.keyword("LEFT JOIN"),
1064 JoinType::Right => self.keyword("RIGHT JOIN"),
1065 JoinType::Full => self.keyword("FULL JOIN"),
1066 JoinType::Cross => self.keyword("CROSS JOIN"),
1067 };
1068
1069 write!(result, "{}{} ", indent, join_type).unwrap();
1070
1071 match &join.table {
1072 TableSource::Table(name) => write!(result, "{}", name).unwrap(),
1073 TableSource::DerivedTable { query, alias } => {
1074 writeln!(result, "(").unwrap();
1075 let subquery_sql = self.format_select(query, indent_level + 1);
1076 write!(result, "{}", subquery_sql).unwrap();
1077 writeln!(result).unwrap();
1078 write!(result, "{}) {} {}", indent, self.keyword("AS"), alias).unwrap();
1079 }
1080 }
1081
1082 if let Some(ref alias) = join.alias {
1083 write!(result, " {} {}", self.keyword("AS"), alias).unwrap();
1084 }
1085
1086 if !join.condition.conditions.is_empty() {
1087 write!(result, " {}", self.keyword("ON")).unwrap();
1088 for (i, condition) in join.condition.conditions.iter().enumerate() {
1089 if i > 0 {
1090 write!(result, " {}", self.keyword("AND")).unwrap();
1091 }
1092 write!(
1093 result,
1094 " {} {} {}",
1095 self.format_expression(&condition.left_expr),
1096 self.format_join_operator(&condition.operator),
1097 self.format_expression(&condition.right_expr)
1098 )
1099 .unwrap();
1100 }
1101 }
1102 }
1103
1104 fn format_join_operator(&self, op: &JoinOperator) -> String {
1105 match op {
1106 JoinOperator::Equal => "=",
1107 JoinOperator::NotEqual => "!=",
1108 JoinOperator::LessThan => "<",
1109 JoinOperator::GreaterThan => ">",
1110 JoinOperator::LessThanOrEqual => "<=",
1111 JoinOperator::GreaterThanOrEqual => ">=",
1112 }
1113 .to_string()
1114 }
1115
1116 fn format_table_function(&self, result: &mut String, func: &TableFunction) {
1117 match func {
1118 TableFunction::Generator { name, args } => {
1119 write!(result, "{}(", self.keyword(&name.to_uppercase())).unwrap();
1120 for (i, arg) in args.iter().enumerate() {
1121 if i > 0 {
1122 write!(result, ", ").unwrap();
1123 }
1124 write!(result, "{}", self.format_expression(arg)).unwrap();
1125 }
1126 write!(result, ")").unwrap();
1127 }
1128 }
1129 }
1130}
1131
1132pub fn format_sql_ast(query: &str) -> Result<String, String> {
1134 use crate::sql::recursive_parser::Parser;
1135
1136 let mut parser = Parser::new(query);
1137 match parser.parse() {
1138 Ok(stmt) => Ok(format_select_statement(&stmt)),
1139 Err(e) => Err(format!("Parse error: {}", e)),
1140 }
1141}
1142
1143pub fn format_sql_ast_with_config(query: &str, config: &FormatConfig) -> Result<String, String> {
1145 use crate::sql::recursive_parser::Parser;
1146
1147 let mut parser = Parser::new(query);
1148 match parser.parse() {
1149 Ok(stmt) => Ok(format_select_with_config(&stmt, &config)),
1150 Err(e) => Err(format!("Parse error: {}", e)),
1151 }
1152}