Skip to content

Commit

Permalink
refactor key_words to common_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
xring committed Apr 6, 2024
1 parent 8197583 commit 8a9a27c
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 245 deletions.
187 changes: 185 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,190 @@

> A SQL parser for MySQL with nom. Written in Rust.
## Data Definition Statements
## Quick Start

### Example parsing SQL
```rust
use sqlparser_mysql::parser::Parser;
use sqlparser_mysql::parser::ParseConfig;

let config = ParseConfig::default();
let sql = "SELECT a, b, 123, myfunc(b) \
FROM table_1 \
WHERE a > b AND b < 100 \
ORDER BY a DESC, b";
// parse to a Statement
let ast = Parser::parse(&config, sql).unwrap();

println!("AST: {:#?}", ast);
```
The output should be:
```rust
AST: Select(
SelectStatement {
tables: [
Table {
name: "table_1",
alias: None,
schema: None,
},
],
distinct: false,
fields: [
Col(
Column {
name: "a",
alias: None,
table: None,
function: None,
},
),
Col(
Column {
name: "b",
alias: None,
table: None,
function: None,
},
),
Value(
Literal(
LiteralExpression {
value: Integer(
123,
),
alias: None,
},
),
),
Col(
Column {
name: "myfunc(b)",
alias: None,
table: None,
function: Some(
Generic(
"myfunc",
FunctionArguments {
arguments: [
Column(
Column {
name: "b",
alias: None,
table: None,
function: None,
},
),
],
},
),
),
},
),
],
join: [],
where_clause: Some(
LogicalOp(
ConditionTree {
operator: And,
left: ComparisonOp(
ConditionTree {
operator: Greater,
left: Base(
Field(
Column {
name: "a",
alias: None,
table: None,
function: None,
},
),
),
right: Base(
Field(
Column {
name: "b",
alias: None,
table: None,
function: None,
},
),
),
},
),
right: ComparisonOp(
ConditionTree {
operator: Less,
left: Base(
Field(
Column {
name: "b",
alias: None,
table: None,
function: None,
},
),
),
right: Base(
Literal(
Integer(
100,
),
),
),
},
),
},
),
),
group_by: None,
order: Some(
OrderClause {
columns: [
(
Column {
name: "a",
alias: None,
table: None,
function: None,
},
Desc,
),
(
Column {
name: "b",
alias: None,
table: None,
function: None,
},
Asc,
),
],
},
),
limit: None,
},
)
```

### Creating SQL text from AST
```rust
use sqlparser_mysql::parser::Parser;
use sqlparser_mysql::parser::ParseConfig;

let sql = "SELECT a FROM table_1";
let config = ParseConfig::default();

// parse to a Statement
let ast = Parser::parse(&config, sql).unwrap();

// The original SQL text can be generated from the AST
assert_eq!(ast.to_string(), sql);
```

## Supported Statements

### Data Definition Statements

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

Expand Down Expand Up @@ -44,5 +227,5 @@
- [x] RENAME TABLE Statement
- [x] TRUNCATE TABLE Statement

## Database Administration Statements
### Database Administration Statements
- [x] SET Statements
13 changes: 6 additions & 7 deletions src/base/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::IResult;

use base::error::ParseSQLErrorKind;
use base::keywords::escape_if_keyword;
use base::{CaseWhenExpression, CommonParser, DataType, Literal, ParseSQLError, Real};
use base::{CaseWhenExpression, CommonParser, DataType, DisplayUtil, Literal, ParseSQLError, Real};

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum FunctionExpression {
Expand Down Expand Up @@ -278,16 +277,16 @@ impl fmt::Display for Column {
write!(
f,
"{}.{}",
escape_if_keyword(table),
escape_if_keyword(&self.name)
DisplayUtil::escape_if_keyword(table),
DisplayUtil::escape_if_keyword(&self.name)
)?;
} else if let Some(ref function) = self.function {
write!(f, "{}", *function)?;
} else {
write!(f, "{}", escape_if_keyword(&self.name))?;
write!(f, "{}", DisplayUtil::escape_if_keyword(&self.name))?;
}
if let Some(ref alias) = self.alias {
write!(f, " AS {}", escape_if_keyword(alias))?;
write!(f, " AS {}", DisplayUtil::escape_if_keyword(alias))?;
}
Ok(())
}
Expand Down Expand Up @@ -609,7 +608,7 @@ impl fmt::Display for ColumnSpecification {
write!(
f,
"{} {}",
escape_if_keyword(&self.column.name),
DisplayUtil::escape_if_keyword(&self.column.name),
self.sql_type
)?;
for constraint in self.constraints.iter() {
Expand Down
Loading

0 comments on commit 8a9a27c

Please sign in to comment.