Crate sqlparse

Source
Expand description

SQL Parser and Formatter for Rust

Example code, see more on Github:

use sqlparse::{FormatOption, Formatter};

let sql = "SELECT a, b, 123, myfunc(b) \
           FROM table_1 \
           WHERE a > b AND b < 100 \
           ORDER BY a DESC";

let mut f = Formatter::default();
let mut formatter = FormatOption::default();
formatter.reindent = true;
formatter.reindent_aligned = true;
 
let formatted_sql = f.format(sql, &mut formatter);
println!("{}", formatted_sql);

Output:

SELECT a,
       b,
       123,
       myfunc(b)
  FROM table_1
 WHERE a > b
   AND b < 100
 ORDER BY a DESC

Structs§

FormatOption
sql format options
Formatter
format sql with multiple options
Parser
parse sql
Token
parsed sql token
TokenList
grouped tokens
Trie

Enums§

TokenType

Functions§

format
format sql to string, only for test
group_tokenlist
parse sql into grouped TokenList. only for test
parse
parse sql into tokens, only for test
parse_multi
parse multiple sqls into tokens, only for test
parse_no_grouping
parse sql into grouped tokens, only for test