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, "{}", col.column).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::Column { column: col, .. } => col.name.len(),
566 SelectItem::Expression { expr, alias, .. } => {
567 self.format_expression(expr).len() + 4 + alias.len() }
569 }
570 })
571 .sum::<usize>()
572 + (items.len() - 1) * 2; let use_single_line = match items.len() {
578 1 => !has_complex_items, 2..=3 => !has_complex_items && single_line_length < 40, _ => false, };
582
583 if !use_single_line {
584 writeln!(result).unwrap();
586 let indent = self.indent(indent_level + 1);
587 for (i, item) in items.iter().enumerate() {
588 write!(result, "{}", indent).unwrap();
589 self.format_select_item(result, item);
590 if i < items.len() - 1 {
591 writeln!(result, ",").unwrap();
592 }
593 }
594 } else {
595 write!(result, " ").unwrap();
597 for (i, item) in items.iter().enumerate() {
598 if i > 0 {
599 write!(result, ", ").unwrap();
600 }
601 self.format_select_item(result, item);
602 }
603 }
604 }
605
606 fn is_complex_expression(&self, expr: &SqlExpression) -> bool {
607 match expr {
608 SqlExpression::CaseExpression { .. } => true,
609 SqlExpression::FunctionCall { .. } => true,
610 SqlExpression::WindowFunction { .. } => true,
611 SqlExpression::ScalarSubquery { .. } => true,
612 SqlExpression::InSubquery { .. } => true,
613 SqlExpression::NotInSubquery { .. } => true,
614 SqlExpression::BinaryOp { left, right, .. } => {
615 self.is_complex_expression(left) || self.is_complex_expression(right)
616 }
617 _ => false,
618 }
619 }
620
621 fn format_select_item(&self, result: &mut String, item: &SelectItem) {
622 match item {
623 SelectItem::Star { .. } => write!(result, "*").unwrap(),
624 SelectItem::Column { column: col, .. } => write!(result, "{}", col.to_sql()).unwrap(),
625 SelectItem::Expression { expr, alias, .. } => {
626 write!(
627 result,
628 "{} {} {}",
629 self.format_expression(expr),
630 self.keyword("AS"),
631 alias
632 )
633 .unwrap();
634 }
635 }
636 }
637
638 fn format_expression(&self, expr: &SqlExpression) -> String {
639 match expr {
640 SqlExpression::Column(column_ref) => column_ref.to_sql(),
641 SqlExpression::StringLiteral(s) => format!("'{}'", s),
642 SqlExpression::NumberLiteral(n) => n.clone(),
643 SqlExpression::BooleanLiteral(b) => b.to_string().to_uppercase(),
644 SqlExpression::Null => self.keyword("NULL"),
645 SqlExpression::BinaryOp { left, op, right } => {
646 if op == "IS NULL" || op == "IS NOT NULL" {
648 format!("{} {}", self.format_expression(left), op)
649 } else {
650 format!(
651 "{} {} {}",
652 self.format_expression(left),
653 op,
654 self.format_expression(right)
655 )
656 }
657 }
658 SqlExpression::FunctionCall {
659 name,
660 args,
661 distinct,
662 } => {
663 let mut result = name.clone();
664 result.push('(');
665 if *distinct {
666 result.push_str(&self.keyword("DISTINCT"));
667 result.push(' ');
668 }
669 for (i, arg) in args.iter().enumerate() {
670 if i > 0 {
671 result.push_str(", ");
672 }
673 result.push_str(&self.format_expression(arg));
674 }
675 result.push(')');
676 result
677 }
678 SqlExpression::CaseExpression {
679 when_branches,
680 else_branch,
681 } => {
682 let mut result = String::new();
684 result.push_str(&self.keyword("CASE"));
685 result.push('\n');
686
687 for branch in when_branches {
689 result.push_str(" "); result.push_str(&format!(
691 "{} {} {} {}",
692 self.keyword("WHEN"),
693 self.format_expression(&branch.condition),
694 self.keyword("THEN"),
695 self.format_expression(&branch.result)
696 ));
697 result.push('\n');
698 }
699
700 if let Some(else_expr) = else_branch {
702 result.push_str(" "); result.push_str(&format!(
704 "{} {}",
705 self.keyword("ELSE"),
706 self.format_expression(else_expr)
707 ));
708 result.push('\n');
709 }
710
711 result.push_str(" "); result.push_str(&self.keyword("END"));
713 result
714 }
715 SqlExpression::SimpleCaseExpression {
716 expr,
717 when_branches,
718 else_branch,
719 } => {
720 let mut result = String::new();
722 result.push_str(&format!(
723 "{} {}",
724 self.keyword("CASE"),
725 self.format_expression(expr)
726 ));
727 result.push('\n');
728
729 for branch in when_branches {
731 result.push_str(" "); result.push_str(&format!(
733 "{} {} {} {}",
734 self.keyword("WHEN"),
735 self.format_expression(&branch.value),
736 self.keyword("THEN"),
737 self.format_expression(&branch.result)
738 ));
739 result.push('\n');
740 }
741
742 if let Some(else_expr) = else_branch {
744 result.push_str(" "); result.push_str(&format!(
746 "{} {}",
747 self.keyword("ELSE"),
748 self.format_expression(else_expr)
749 ));
750 result.push('\n');
751 }
752
753 result.push_str(" "); result.push_str(&self.keyword("END"));
755 result
756 }
757 SqlExpression::Between { expr, lower, upper } => {
758 format!(
759 "{} {} {} {} {}",
760 self.format_expression(expr),
761 self.keyword("BETWEEN"),
762 self.format_expression(lower),
763 self.keyword("AND"),
764 self.format_expression(upper)
765 )
766 }
767 SqlExpression::InList { expr, values } => {
768 let mut result =
769 format!("{} {} (", self.format_expression(expr), self.keyword("IN"));
770 for (i, val) in values.iter().enumerate() {
771 if i > 0 {
772 result.push_str(", ");
773 }
774 result.push_str(&self.format_expression(val));
775 }
776 result.push(')');
777 result
778 }
779 SqlExpression::NotInList { expr, values } => {
780 let mut result = format!(
781 "{} {} {} (",
782 self.format_expression(expr),
783 self.keyword("NOT"),
784 self.keyword("IN")
785 );
786 for (i, val) in values.iter().enumerate() {
787 if i > 0 {
788 result.push_str(", ");
789 }
790 result.push_str(&self.format_expression(val));
791 }
792 result.push(')');
793 result
794 }
795 SqlExpression::Not { expr } => {
796 format!("{} {}", self.keyword("NOT"), self.format_expression(expr))
797 }
798 SqlExpression::ScalarSubquery { query } => {
799 let subquery_str = self.format_select(query, 0);
801 if subquery_str.contains('\n') || subquery_str.len() > 60 {
802 format!("(\n{}\n)", self.format_select(query, 1))
804 } else {
805 format!("({})", subquery_str)
807 }
808 }
809 SqlExpression::InSubquery { expr, subquery } => {
810 let subquery_str = self.format_select(subquery, 0);
811 if subquery_str.contains('\n') || subquery_str.len() > 60 {
812 format!(
814 "{} {} (\n{}\n)",
815 self.format_expression(expr),
816 self.keyword("IN"),
817 self.format_select(subquery, 1)
818 )
819 } else {
820 format!(
822 "{} {} ({})",
823 self.format_expression(expr),
824 self.keyword("IN"),
825 subquery_str
826 )
827 }
828 }
829 SqlExpression::NotInSubquery { expr, subquery } => {
830 let subquery_str = self.format_select(subquery, 0);
831 if subquery_str.contains('\n') || subquery_str.len() > 60 {
832 format!(
834 "{} {} {} (\n{}\n)",
835 self.format_expression(expr),
836 self.keyword("NOT"),
837 self.keyword("IN"),
838 self.format_select(subquery, 1)
839 )
840 } else {
841 format!(
843 "{} {} {} ({})",
844 self.format_expression(expr),
845 self.keyword("NOT"),
846 self.keyword("IN"),
847 subquery_str
848 )
849 }
850 }
851 SqlExpression::MethodCall {
852 object,
853 method,
854 args,
855 } => {
856 let mut result = format!("{}.{}", object, method);
857 result.push('(');
858 for (i, arg) in args.iter().enumerate() {
859 if i > 0 {
860 result.push_str(", ");
861 }
862 result.push_str(&self.format_expression(arg));
863 }
864 result.push(')');
865 result
866 }
867 SqlExpression::ChainedMethodCall { base, method, args } => {
868 let mut result = format!("{}.{}", self.format_expression(base), method);
869 result.push('(');
870 for (i, arg) in args.iter().enumerate() {
871 if i > 0 {
872 result.push_str(", ");
873 }
874 result.push_str(&self.format_expression(arg));
875 }
876 result.push(')');
877 result
878 }
879 SqlExpression::WindowFunction {
880 name,
881 args,
882 window_spec,
883 } => {
884 let mut result = format!("{}(", name);
885
886 for (i, arg) in args.iter().enumerate() {
888 if i > 0 {
889 result.push_str(", ");
890 }
891 result.push_str(&self.format_expression(arg));
892 }
893 result.push_str(") ");
894 result.push_str(&self.keyword("OVER"));
895 result.push_str(" (");
896
897 if !window_spec.partition_by.is_empty() {
899 result.push_str(&self.keyword("PARTITION BY"));
900 result.push(' ');
901 for (i, col) in window_spec.partition_by.iter().enumerate() {
902 if i > 0 {
903 result.push_str(", ");
904 }
905 result.push_str(col);
906 }
907 }
908
909 if !window_spec.order_by.is_empty() {
911 if !window_spec.partition_by.is_empty() {
912 result.push(' ');
913 }
914 result.push_str(&self.keyword("ORDER BY"));
915 result.push(' ');
916 for (i, col) in window_spec.order_by.iter().enumerate() {
917 if i > 0 {
918 result.push_str(", ");
919 }
920 result.push_str(&col.column);
921 match col.direction {
922 SortDirection::Asc => {
923 result.push(' ');
924 result.push_str(&self.keyword("ASC"));
925 }
926 SortDirection::Desc => {
927 result.push(' ');
928 result.push_str(&self.keyword("DESC"));
929 }
930 }
931 }
932 }
933
934 if let Some(frame) = &window_spec.frame {
936 if !window_spec.partition_by.is_empty() || !window_spec.order_by.is_empty() {
938 result.push(' ');
939 }
940
941 match frame.unit {
943 FrameUnit::Rows => result.push_str(&self.keyword("ROWS")),
944 FrameUnit::Range => result.push_str(&self.keyword("RANGE")),
945 }
946
947 result.push(' ');
948
949 if let Some(end) = &frame.end {
951 result.push_str(&self.keyword("BETWEEN"));
953 result.push(' ');
954 result.push_str(&self.format_frame_bound(&frame.start));
955 result.push(' ');
956 result.push_str(&self.keyword("AND"));
957 result.push(' ');
958 result.push_str(&self.format_frame_bound(end));
959 } else {
960 result.push_str(&self.format_frame_bound(&frame.start));
962 }
963 }
964
965 result.push(')');
966 result
967 }
968 SqlExpression::DateTimeConstructor {
969 year,
970 month,
971 day,
972 hour,
973 minute,
974 second,
975 } => {
976 if let (Some(h), Some(m), Some(s)) = (hour, minute, second) {
977 format!(
978 "DateTime({}, {}, {}, {}, {}, {})",
979 year, month, day, h, m, s
980 )
981 } else {
982 format!("DateTime({}, {}, {})", year, month, day)
983 }
984 }
985 SqlExpression::DateTimeToday {
986 hour,
987 minute,
988 second,
989 } => {
990 if let (Some(h), Some(m), Some(s)) = (hour, minute, second) {
991 format!("Today({}, {}, {})", h, m, s)
992 } else {
993 "Today()".to_string()
994 }
995 }
996 _ => format!("{:?}", expr), }
998 }
999
1000 fn format_where_clause(
1001 &self,
1002 result: &mut String,
1003 where_clause: &WhereClause,
1004 indent_level: usize,
1005 ) {
1006 let needs_multiline = where_clause.conditions.len() > 1;
1007
1008 if needs_multiline {
1009 writeln!(result).unwrap();
1010 let indent = self.indent(indent_level + 1);
1011 for (i, condition) in where_clause.conditions.iter().enumerate() {
1012 if i > 0 {
1013 if let Some(ref connector) = where_clause.conditions[i - 1].connector {
1014 let connector_str = match connector {
1015 LogicalOp::And => self.keyword("AND"),
1016 LogicalOp::Or => self.keyword("OR"),
1017 };
1018 writeln!(result).unwrap();
1019 write!(result, "{}{} ", indent, connector_str).unwrap();
1020 }
1021 } else {
1022 write!(result, "{}", indent).unwrap();
1023 }
1024 write!(result, "{}", self.format_expression(&condition.expr)).unwrap();
1025 }
1026 } else if let Some(condition) = where_clause.conditions.first() {
1027 write!(result, " {}", self.format_expression(&condition.expr)).unwrap();
1028 }
1029 }
1030
1031 fn format_frame_bound(&self, bound: &FrameBound) -> String {
1032 match bound {
1033 FrameBound::UnboundedPreceding => self.keyword("UNBOUNDED PRECEDING"),
1034 FrameBound::CurrentRow => self.keyword("CURRENT ROW"),
1035 FrameBound::UnboundedFollowing => self.keyword("UNBOUNDED FOLLOWING"),
1036 FrameBound::Preceding(n) => format!("{} {}", n, self.keyword("PRECEDING")),
1037 FrameBound::Following(n) => format!("{} {}", n, self.keyword("FOLLOWING")),
1038 }
1039 }
1040
1041 fn format_join(&self, result: &mut String, join: &JoinClause, indent_level: usize) {
1042 let indent = self.indent(indent_level);
1043 let join_type = match join.join_type {
1044 JoinType::Inner => self.keyword("INNER JOIN"),
1045 JoinType::Left => self.keyword("LEFT JOIN"),
1046 JoinType::Right => self.keyword("RIGHT JOIN"),
1047 JoinType::Full => self.keyword("FULL JOIN"),
1048 JoinType::Cross => self.keyword("CROSS JOIN"),
1049 };
1050
1051 write!(result, "{}{} ", indent, join_type).unwrap();
1052
1053 match &join.table {
1054 TableSource::Table(name) => write!(result, "{}", name).unwrap(),
1055 TableSource::DerivedTable { query, alias } => {
1056 writeln!(result, "(").unwrap();
1057 let subquery_sql = self.format_select(query, indent_level + 1);
1058 write!(result, "{}", subquery_sql).unwrap();
1059 writeln!(result).unwrap();
1060 write!(result, "{}) {} {}", indent, self.keyword("AS"), alias).unwrap();
1061 }
1062 }
1063
1064 if let Some(ref alias) = join.alias {
1065 write!(result, " {} {}", self.keyword("AS"), alias).unwrap();
1066 }
1067
1068 if !join.condition.conditions.is_empty() {
1069 write!(result, " {}", self.keyword("ON")).unwrap();
1070 for (i, condition) in join.condition.conditions.iter().enumerate() {
1071 if i > 0 {
1072 write!(result, " {}", self.keyword("AND")).unwrap();
1073 }
1074 write!(
1075 result,
1076 " {} {} {}",
1077 condition.left_column,
1078 self.format_join_operator(&condition.operator),
1079 self.format_expression(&condition.right_expr)
1080 )
1081 .unwrap();
1082 }
1083 }
1084 }
1085
1086 fn format_join_operator(&self, op: &JoinOperator) -> String {
1087 match op {
1088 JoinOperator::Equal => "=",
1089 JoinOperator::NotEqual => "!=",
1090 JoinOperator::LessThan => "<",
1091 JoinOperator::GreaterThan => ">",
1092 JoinOperator::LessThanOrEqual => "<=",
1093 JoinOperator::GreaterThanOrEqual => ">=",
1094 }
1095 .to_string()
1096 }
1097
1098 fn format_table_function(&self, result: &mut String, func: &TableFunction) {
1099 match func {
1100 TableFunction::Generator { name, args } => {
1101 write!(result, "{}(", self.keyword(&name.to_uppercase())).unwrap();
1102 for (i, arg) in args.iter().enumerate() {
1103 if i > 0 {
1104 write!(result, ", ").unwrap();
1105 }
1106 write!(result, "{}", self.format_expression(arg)).unwrap();
1107 }
1108 write!(result, ")").unwrap();
1109 }
1110 }
1111 }
1112}
1113
1114pub fn format_sql_ast(query: &str) -> Result<String, String> {
1116 use crate::sql::recursive_parser::Parser;
1117
1118 let mut parser = Parser::new(query);
1119 match parser.parse() {
1120 Ok(stmt) => Ok(format_select_statement(&stmt)),
1121 Err(e) => Err(format!("Parse error: {}", e)),
1122 }
1123}
1124
1125pub fn format_sql_ast_with_config(query: &str, config: &FormatConfig) -> Result<String, String> {
1127 use crate::sql::recursive_parser::Parser;
1128
1129 let mut parser = Parser::new(query);
1130 match parser.parse() {
1131 Ok(stmt) => Ok(format_select_with_config(&stmt, &config)),
1132 Err(e) => Err(format!("Parse error: {}", e)),
1133 }
1134}