Skip to content

Commit 4cdd003

Browse files
authored
Merge pull request #51 from nickolay/rust-2018
Rust 2018 and clippy fixes
2 parents ef76baa + 098d1c4 commit 4cdd003

16 files changed

+180
-247
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include = [
1212
"src/**/*.rs",
1313
"Cargo.toml",
1414
]
15+
edition = "2018"
1516

1617
[lib]
1718
name = "sqlparser"
@@ -24,3 +25,4 @@ uuid = "0.7.1"
2425

2526
[dev-dependencies]
2627
simple_logger = "1.0.1"
28+
matches = "0.1"

examples/cli.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
extern crate simple_logger;
2-
extern crate sqlparser;
1+
#![warn(clippy::all)]
2+
3+
use simple_logger;
4+
35
///! A small command-line app to run the parser.
46
/// Run with `cargo run --example cli`
57
use std::fs;
@@ -14,8 +16,8 @@ fn main() {
1416
.nth(1)
1517
.expect("No arguments provided!\n\nUsage: cargo run --example cli FILENAME.sql");
1618

17-
let contents =
18-
fs::read_to_string(&filename).expect(&format!("Unable to read the file {}", &filename));
19+
let contents = fs::read_to_string(&filename)
20+
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename));
1921
let without_bom = if contents.chars().nth(0).unwrap() as u64 != 0xfeff {
2022
contents.as_str()
2123
} else {
@@ -31,7 +33,7 @@ fn main() {
3133
"Round-trip:\n'{}'",
3234
statements
3335
.iter()
34-
.map(|s| s.to_string())
36+
.map(std::string::ToString::to_string)
3537
.collect::<Vec<_>>()
3638
.join("\n")
3739
);

examples/parse_select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extern crate sqlparser;
1+
#![warn(clippy::all)]
22

33
use sqlparser::dialect::GenericSqlDialect;
44
use sqlparser::sqlparser::*;

src/dialect/ansi_sql.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dialect::Dialect;
1+
use crate::dialect::Dialect;
22

33
pub struct AnsiSqlDialect {}
44

src/dialect/generic_sql.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dialect::Dialect;
1+
use crate::dialect::Dialect;
22
pub struct GenericSqlDialect {}
33

44
impl Dialect for GenericSqlDialect {

src/dialect/keywords.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ keyword!(
365365
);
366366

367367
/// special case of keyword where the it is an invalid identifier
368-
pub const END_EXEC: &'static str = "END-EXEC";
368+
pub const END_EXEC: &str = "END-EXEC";
369369

370-
pub const ALL_KEYWORDS: &'static [&'static str] = &[
370+
pub const ALL_KEYWORDS: &[&str] = &[
371371
ABS,
372372
ADD,
373373
ASC,
@@ -716,7 +716,7 @@ pub const ALL_KEYWORDS: &'static [&'static str] = &[
716716

717717
/// These keywords can't be used as a table alias, so that `FROM table_name alias`
718718
/// can be parsed unambiguously without looking ahead.
719-
pub const RESERVED_FOR_TABLE_ALIAS: &'static [&'static str] = &[
719+
pub const RESERVED_FOR_TABLE_ALIAS: &[&str] = &[
720720
// Reserved as both a table and a column alias:
721721
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
722722
// Reserved only as a table alias in the `FROM`/`JOIN` clauses:
@@ -725,7 +725,7 @@ pub const RESERVED_FOR_TABLE_ALIAS: &'static [&'static str] = &[
725725

726726
/// Can't be used as a column alias, so that `SELECT <expr> alias`
727727
/// can be parsed unambiguously without looking ahead.
728-
pub const RESERVED_FOR_COLUMN_ALIAS: &'static [&'static str] = &[
728+
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
729729
// Reserved as both a table and a column alias:
730730
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
731731
// Reserved only as a column alias in the `SELECT` clause:

src/dialect/postgresql.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dialect::Dialect;
1+
use crate::dialect::Dialect;
22

33
pub struct PostgreSqlDialect {}
44

src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@
3434
//!
3535
//! println!("AST: {:?}", ast);
3636
//! ```
37-
38-
#[macro_use]
39-
extern crate log;
40-
extern crate chrono;
41-
extern crate uuid;
37+
#![warn(clippy::all)]
4238

4339
pub mod dialect;
4440
pub mod sqlast;

src/sqlast/mod.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl ToString for ASTNode {
202202
#[derive(Debug, Clone, PartialEq)]
203203
pub enum SQLStatement {
204204
/// SELECT
205-
SQLSelect(SQLQuery),
205+
SQLQuery(Box<SQLQuery>),
206206
/// INSERT
207207
SQLInsert {
208208
/// TABLE
@@ -240,7 +240,7 @@ pub enum SQLStatement {
240240
SQLCreateView {
241241
/// View name
242242
name: SQLObjectName,
243-
query: SQLQuery,
243+
query: Box<SQLQuery>,
244244
materialized: bool,
245245
},
246246
/// CREATE TABLE
@@ -264,17 +264,17 @@ pub enum SQLStatement {
264264
impl ToString for SQLStatement {
265265
fn to_string(&self) -> String {
266266
match self {
267-
SQLStatement::SQLSelect(s) => s.to_string(),
267+
SQLStatement::SQLQuery(s) => s.to_string(),
268268
SQLStatement::SQLInsert {
269269
table_name,
270270
columns,
271271
values,
272272
} => {
273273
let mut s = format!("INSERT INTO {}", table_name.to_string());
274-
if columns.len() > 0 {
274+
if !columns.is_empty() {
275275
s += &format!(" ({})", columns.join(", "));
276276
}
277-
if values.len() > 0 {
277+
if !values.is_empty() {
278278
s += &format!(
279279
" VALUES({})",
280280
values
@@ -307,12 +307,12 @@ impl ToString for SQLStatement {
307307
);
308308
}
309309
s += " FROM stdin; ";
310-
if values.len() > 0 {
310+
if !values.is_empty() {
311311
s += &format!(
312312
"\n{}",
313313
values
314314
.iter()
315-
.map(|v| v.clone().unwrap_or("\\N".to_string()))
315+
.map(|v| v.clone().unwrap_or_else(|| "\\N".to_string()))
316316
.collect::<Vec<String>>()
317317
.join("\t")
318318
);
@@ -381,13 +381,7 @@ impl ToString for SQLStatement {
381381
file_format.as_ref().map(|f| f.to_string()).unwrap(),
382382
location.as_ref().unwrap()
383383
),
384-
SQLStatement::SQLCreateTable {
385-
name,
386-
columns,
387-
external: _,
388-
file_format: _,
389-
location: _,
390-
} => format!(
384+
SQLStatement::SQLCreateTable { name, columns, .. } => format!(
391385
"CREATE TABLE {} ({})",
392386
name.to_string(),
393387
columns
@@ -483,7 +477,7 @@ impl ToString for FileFormat {
483477
}
484478
}
485479

486-
use sqlparser::ParserError;
480+
use crate::sqlparser::ParserError;
487481
use std::str::FromStr;
488482
impl FromStr for FileFormat {
489483
type Err = ParserError;

src/sqlast/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl ToString for SQLQuery {
5050
#[derive(Debug, Clone, PartialEq)]
5151
pub enum SQLSetExpr {
5252
/// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations)
53-
Select(SQLSelect),
53+
Select(Box<SQLSelect>),
5454
/// Parenthesized SELECT subquery, which may include more set operations
5555
/// in its body and an optional ORDER BY / LIMIT.
5656
Query(Box<SQLQuery>),

src/sqlast/sqltype.rs

+14-26
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ pub enum SQLType {
1919
Blob(usize),
2020
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
2121
Decimal(Option<usize>, Option<usize>),
22+
/// Floating point with optional precision e.g. FLOAT(8)
23+
Float(Option<usize>),
2224
/// Small integer
2325
SmallInt,
2426
/// Integer
2527
Int,
2628
/// Big integer
2729
BigInt,
28-
/// Floating point with optional precision e.g. FLOAT(8)
29-
Float(Option<usize>),
3030
/// Floating point e.g. REAL
3131
Real,
3232
/// Double e.g. DOUBLE PRECISION
@@ -54,20 +54,8 @@ pub enum SQLType {
5454
impl ToString for SQLType {
5555
fn to_string(&self) -> String {
5656
match self {
57-
SQLType::Char(size) => {
58-
if let Some(size) = size {
59-
format!("char({})", size)
60-
} else {
61-
"char".to_string()
62-
}
63-
}
64-
SQLType::Varchar(size) => {
65-
if let Some(size) = size {
66-
format!("character varying({})", size)
67-
} else {
68-
"character varying".to_string()
69-
}
70-
}
57+
SQLType::Char(size) => format_type_with_optional_length("char", size),
58+
SQLType::Varchar(size) => format_type_with_optional_length("character varying", size),
7159
SQLType::Uuid => "uuid".to_string(),
7260
SQLType::Clob(size) => format!("clob({})", size),
7361
SQLType::Binary(size) => format!("binary({})", size),
@@ -76,22 +64,14 @@ impl ToString for SQLType {
7664
SQLType::Decimal(precision, scale) => {
7765
if let Some(scale) = scale {
7866
format!("numeric({},{})", precision.unwrap(), scale)
79-
} else if let Some(precision) = precision {
80-
format!("numeric({})", precision)
8167
} else {
82-
format!("numeric")
68+
format_type_with_optional_length("numeric", precision)
8369
}
8470
}
71+
SQLType::Float(size) => format_type_with_optional_length("float", size),
8572
SQLType::SmallInt => "smallint".to_string(),
8673
SQLType::Int => "int".to_string(),
8774
SQLType::BigInt => "bigint".to_string(),
88-
SQLType::Float(size) => {
89-
if let Some(size) = size {
90-
format!("float({})", size)
91-
} else {
92-
"float".to_string()
93-
}
94-
}
9575
SQLType::Real => "real".to_string(),
9676
SQLType::Double => "double".to_string(),
9777
SQLType::Boolean => "boolean".to_string(),
@@ -106,3 +86,11 @@ impl ToString for SQLType {
10686
}
10787
}
10888
}
89+
90+
fn format_type_with_optional_length(sql_type: &str, len: &Option<usize>) -> String {
91+
let mut s = sql_type.to_string();
92+
if let Some(len) = len {
93+
s += &format!("({})", len);
94+
}
95+
s
96+
}

0 commit comments

Comments
 (0)