Skip to content

Commit 8a9a27c

Browse files
committed
refactor key_words to common_parser
1 parent 8197583 commit 8a9a27c

14 files changed

+423
-245
lines changed

README.md

Lines changed: 185 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,190 @@
22

33
> A SQL parser for MySQL with nom. Written in Rust.
44
5-
## Data Definition Statements
5+
## Quick Start
6+
7+
### Example parsing SQL
8+
```rust
9+
use sqlparser_mysql::parser::Parser;
10+
use sqlparser_mysql::parser::ParseConfig;
11+
12+
let config = ParseConfig::default();
13+
let sql = "SELECT a, b, 123, myfunc(b) \
14+
FROM table_1 \
15+
WHERE a > b AND b < 100 \
16+
ORDER BY a DESC, b";
17+
// parse to a Statement
18+
let ast = Parser::parse(&config, sql).unwrap();
19+
20+
println!("AST: {:#?}", ast);
21+
```
22+
The output should be:
23+
```rust
24+
AST: Select(
25+
SelectStatement {
26+
tables: [
27+
Table {
28+
name: "table_1",
29+
alias: None,
30+
schema: None,
31+
},
32+
],
33+
distinct: false,
34+
fields: [
35+
Col(
36+
Column {
37+
name: "a",
38+
alias: None,
39+
table: None,
40+
function: None,
41+
},
42+
),
43+
Col(
44+
Column {
45+
name: "b",
46+
alias: None,
47+
table: None,
48+
function: None,
49+
},
50+
),
51+
Value(
52+
Literal(
53+
LiteralExpression {
54+
value: Integer(
55+
123,
56+
),
57+
alias: None,
58+
},
59+
),
60+
),
61+
Col(
62+
Column {
63+
name: "myfunc(b)",
64+
alias: None,
65+
table: None,
66+
function: Some(
67+
Generic(
68+
"myfunc",
69+
FunctionArguments {
70+
arguments: [
71+
Column(
72+
Column {
73+
name: "b",
74+
alias: None,
75+
table: None,
76+
function: None,
77+
},
78+
),
79+
],
80+
},
81+
),
82+
),
83+
},
84+
),
85+
],
86+
join: [],
87+
where_clause: Some(
88+
LogicalOp(
89+
ConditionTree {
90+
operator: And,
91+
left: ComparisonOp(
92+
ConditionTree {
93+
operator: Greater,
94+
left: Base(
95+
Field(
96+
Column {
97+
name: "a",
98+
alias: None,
99+
table: None,
100+
function: None,
101+
},
102+
),
103+
),
104+
right: Base(
105+
Field(
106+
Column {
107+
name: "b",
108+
alias: None,
109+
table: None,
110+
function: None,
111+
},
112+
),
113+
),
114+
},
115+
),
116+
right: ComparisonOp(
117+
ConditionTree {
118+
operator: Less,
119+
left: Base(
120+
Field(
121+
Column {
122+
name: "b",
123+
alias: None,
124+
table: None,
125+
function: None,
126+
},
127+
),
128+
),
129+
right: Base(
130+
Literal(
131+
Integer(
132+
100,
133+
),
134+
),
135+
),
136+
},
137+
),
138+
},
139+
),
140+
),
141+
group_by: None,
142+
order: Some(
143+
OrderClause {
144+
columns: [
145+
(
146+
Column {
147+
name: "a",
148+
alias: None,
149+
table: None,
150+
function: None,
151+
},
152+
Desc,
153+
),
154+
(
155+
Column {
156+
name: "b",
157+
alias: None,
158+
table: None,
159+
function: None,
160+
},
161+
Asc,
162+
),
163+
],
164+
},
165+
),
166+
limit: None,
167+
},
168+
)
169+
```
170+
171+
### Creating SQL text from AST
172+
```rust
173+
use sqlparser_mysql::parser::Parser;
174+
use sqlparser_mysql::parser::ParseConfig;
175+
176+
let sql = "SELECT a FROM table_1";
177+
let config = ParseConfig::default();
178+
179+
// parse to a Statement
180+
let ast = Parser::parse(&config, sql).unwrap();
181+
182+
// The original SQL text can be generated from the AST
183+
assert_eq!(ast.to_string(), sql);
184+
```
185+
186+
## Supported Statements
187+
188+
### Data Definition Statements
6189

7190
[MySQL Doc](https://dev.mysql.com/doc/refman/8.0/en/sql-data-definition-statements.html)
8191

@@ -44,5 +227,5 @@
44227
- [x] RENAME TABLE Statement
45228
- [x] TRUNCATE TABLE Statement
46229

47-
## Database Administration Statements
230+
### Database Administration Statements
48231
- [x] SET Statements

src/base/column.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use nom::sequence::{delimited, pair, preceded, terminated, tuple};
1212
use nom::IResult;
1313

1414
use base::error::ParseSQLErrorKind;
15-
use base::keywords::escape_if_keyword;
16-
use base::{CaseWhenExpression, CommonParser, DataType, Literal, ParseSQLError, Real};
15+
use base::{CaseWhenExpression, CommonParser, DataType, DisplayUtil, Literal, ParseSQLError, Real};
1716

1817
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
1918
pub enum FunctionExpression {
@@ -278,16 +277,16 @@ impl fmt::Display for Column {
278277
write!(
279278
f,
280279
"{}.{}",
281-
escape_if_keyword(table),
282-
escape_if_keyword(&self.name)
280+
DisplayUtil::escape_if_keyword(table),
281+
DisplayUtil::escape_if_keyword(&self.name)
283282
)?;
284283
} else if let Some(ref function) = self.function {
285284
write!(f, "{}", *function)?;
286285
} else {
287-
write!(f, "{}", escape_if_keyword(&self.name))?;
286+
write!(f, "{}", DisplayUtil::escape_if_keyword(&self.name))?;
288287
}
289288
if let Some(ref alias) = self.alias {
290-
write!(f, " AS {}", escape_if_keyword(alias))?;
289+
write!(f, " AS {}", DisplayUtil::escape_if_keyword(alias))?;
291290
}
292291
Ok(())
293292
}
@@ -609,7 +608,7 @@ impl fmt::Display for ColumnSpecification {
609608
write!(
610609
f,
611610
"{} {}",
612-
escape_if_keyword(&self.column.name),
611+
DisplayUtil::escape_if_keyword(&self.column.name),
613612
self.sql_type
614613
)?;
615614
for constraint in self.constraints.iter() {

0 commit comments

Comments
 (0)