Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include = [
"src/**/*.rs",
"Cargo.toml",
]
edition = "2018"

[lib]
name = "sqlparser"
Expand All @@ -24,3 +25,4 @@ uuid = "0.7.1"

[dev-dependencies]
simple_logger = "1.0.1"
matches = "0.1"
12 changes: 7 additions & 5 deletions examples/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extern crate simple_logger;
extern crate sqlparser;
#![warn(clippy::all)]

use simple_logger;

///! A small command-line app to run the parser.
/// Run with `cargo run --example cli`
use std::fs;
Expand All @@ -14,8 +16,8 @@ fn main() {
.nth(1)
.expect("No arguments provided!\n\nUsage: cargo run --example cli FILENAME.sql");

let contents =
fs::read_to_string(&filename).expect(&format!("Unable to read the file {}", &filename));
let contents = fs::read_to_string(&filename)
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename));
let without_bom = if contents.chars().nth(0).unwrap() as u64 != 0xfeff {
contents.as_str()
} else {
Expand All @@ -31,7 +33,7 @@ fn main() {
"Round-trip:\n'{}'",
statements
.iter()
.map(|s| s.to_string())
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join("\n")
);
Expand Down
2 changes: 1 addition & 1 deletion examples/parse_select.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate sqlparser;
#![warn(clippy::all)]

use sqlparser::dialect::GenericSqlDialect;
use sqlparser::sqlparser::*;
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/ansi_sql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dialect::Dialect;
use crate::dialect::Dialect;

pub struct AnsiSqlDialect {}

Expand Down
2 changes: 1 addition & 1 deletion src/dialect/generic_sql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dialect::Dialect;
use crate::dialect::Dialect;
pub struct GenericSqlDialect {}

impl Dialect for GenericSqlDialect {
Expand Down
8 changes: 4 additions & 4 deletions src/dialect/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ keyword!(
);

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

pub const ALL_KEYWORDS: &'static [&'static str] = &[
pub const ALL_KEYWORDS: &[&str] = &[
ABS,
ADD,
ASC,
Expand Down Expand Up @@ -716,7 +716,7 @@ pub const ALL_KEYWORDS: &'static [&'static str] = &[

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

/// Can't be used as a column alias, so that `SELECT <expr> alias`
/// can be parsed unambiguously without looking ahead.
pub const RESERVED_FOR_COLUMN_ALIAS: &'static [&'static str] = &[
pub const RESERVED_FOR_COLUMN_ALIAS: &[&str] = &[
// Reserved as both a table and a column alias:
WITH, SELECT, WHERE, GROUP, ORDER, UNION, EXCEPT, INTERSECT,
// Reserved only as a column alias in the `SELECT` clause:
Expand Down
2 changes: 1 addition & 1 deletion src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dialect::Dialect;
use crate::dialect::Dialect;

pub struct PostgreSqlDialect {}

Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
//!
//! println!("AST: {:?}", ast);
//! ```

#[macro_use]
extern crate log;
extern crate chrono;
extern crate uuid;
#![warn(clippy::all)]

pub mod dialect;
pub mod sqlast;
Expand Down
24 changes: 9 additions & 15 deletions src/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl ToString for ASTNode {
#[derive(Debug, Clone, PartialEq)]
pub enum SQLStatement {
/// SELECT
SQLSelect(SQLQuery),
SQLQuery(Box<SQLQuery>),
/// INSERT
SQLInsert {
/// TABLE
Expand Down Expand Up @@ -240,7 +240,7 @@ pub enum SQLStatement {
SQLCreateView {
/// View name
name: SQLObjectName,
query: SQLQuery,
query: Box<SQLQuery>,
materialized: bool,
},
/// CREATE TABLE
Expand All @@ -264,17 +264,17 @@ pub enum SQLStatement {
impl ToString for SQLStatement {
fn to_string(&self) -> String {
match self {
SQLStatement::SQLSelect(s) => s.to_string(),
SQLStatement::SQLQuery(s) => s.to_string(),
SQLStatement::SQLInsert {
table_name,
columns,
values,
} => {
let mut s = format!("INSERT INTO {}", table_name.to_string());
if columns.len() > 0 {
if !columns.is_empty() {
s += &format!(" ({})", columns.join(", "));
}
if values.len() > 0 {
if !values.is_empty() {
s += &format!(
" VALUES({})",
values
Expand Down Expand Up @@ -307,12 +307,12 @@ impl ToString for SQLStatement {
);
}
s += " FROM stdin; ";
if values.len() > 0 {
if !values.is_empty() {
s += &format!(
"\n{}",
values
.iter()
.map(|v| v.clone().unwrap_or("\\N".to_string()))
.map(|v| v.clone().unwrap_or_else(|| "\\N".to_string()))
.collect::<Vec<String>>()
.join("\t")
);
Expand Down Expand Up @@ -381,13 +381,7 @@ impl ToString for SQLStatement {
file_format.as_ref().map(|f| f.to_string()).unwrap(),
location.as_ref().unwrap()
),
SQLStatement::SQLCreateTable {
name,
columns,
external: _,
file_format: _,
location: _,
} => format!(
SQLStatement::SQLCreateTable { name, columns, .. } => format!(
"CREATE TABLE {} ({})",
name.to_string(),
columns
Expand Down Expand Up @@ -483,7 +477,7 @@ impl ToString for FileFormat {
}
}

use sqlparser::ParserError;
use crate::sqlparser::ParserError;
use std::str::FromStr;
impl FromStr for FileFormat {
type Err = ParserError;
Expand Down
2 changes: 1 addition & 1 deletion src/sqlast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ToString for SQLQuery {
#[derive(Debug, Clone, PartialEq)]
pub enum SQLSetExpr {
/// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations)
Select(SQLSelect),
Select(Box<SQLSelect>),
/// Parenthesized SELECT subquery, which may include more set operations
/// in its body and an optional ORDER BY / LIMIT.
Query(Box<SQLQuery>),
Expand Down
40 changes: 14 additions & 26 deletions src/sqlast/sqltype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pub enum SQLType {
Blob(usize),
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
Decimal(Option<usize>, Option<usize>),
/// Floating point with optional precision e.g. FLOAT(8)
Float(Option<usize>),
/// Small integer
SmallInt,
/// Integer
Int,
/// Big integer
BigInt,
/// Floating point with optional precision e.g. FLOAT(8)
Float(Option<usize>),
/// Floating point e.g. REAL
Real,
/// Double e.g. DOUBLE PRECISION
Expand Down Expand Up @@ -54,20 +54,8 @@ pub enum SQLType {
impl ToString for SQLType {
fn to_string(&self) -> String {
match self {
SQLType::Char(size) => {
if let Some(size) = size {
format!("char({})", size)
} else {
"char".to_string()
}
}
SQLType::Varchar(size) => {
if let Some(size) = size {
format!("character varying({})", size)
} else {
"character varying".to_string()
}
}
SQLType::Char(size) => format_type_with_optional_length("char", size),
SQLType::Varchar(size) => format_type_with_optional_length("character varying", size),
SQLType::Uuid => "uuid".to_string(),
SQLType::Clob(size) => format!("clob({})", size),
SQLType::Binary(size) => format!("binary({})", size),
Expand All @@ -76,22 +64,14 @@ impl ToString for SQLType {
SQLType::Decimal(precision, scale) => {
if let Some(scale) = scale {
format!("numeric({},{})", precision.unwrap(), scale)
} else if let Some(precision) = precision {
format!("numeric({})", precision)
} else {
format!("numeric")
format_type_with_optional_length("numeric", precision)
}
}
SQLType::Float(size) => format_type_with_optional_length("float", size),
SQLType::SmallInt => "smallint".to_string(),
SQLType::Int => "int".to_string(),
SQLType::BigInt => "bigint".to_string(),
SQLType::Float(size) => {
if let Some(size) = size {
format!("float({})", size)
} else {
"float".to_string()
}
}
SQLType::Real => "real".to_string(),
SQLType::Double => "double".to_string(),
SQLType::Boolean => "boolean".to_string(),
Expand All @@ -106,3 +86,11 @@ impl ToString for SQLType {
}
}
}

fn format_type_with_optional_length(sql_type: &str, len: &Option<usize>) -> String {
let mut s = sql_type.to_string();
if let Some(len) = len {
s += &format!("({})", len);
}
s
}
Loading