cargo format
This commit is contained in:
parent
4d45da0cd1
commit
845db102c2
33 changed files with 885 additions and 530 deletions
|
|
@ -1,13 +1,14 @@
|
|||
use nom::{
|
||||
bytes::complete::tag,
|
||||
character::complete::{char, multispace0, multispace1},
|
||||
combinator::opt,
|
||||
multi::separated_list0,
|
||||
sequence::terminated,
|
||||
IResult, combinator::opt,
|
||||
IResult,
|
||||
};
|
||||
|
||||
use super::common::{parse_table_name, parse_identifier, parse_db_type};
|
||||
use crate::syntax::{RawTableSchema, ColumnSchema, RawQuerySyntax};
|
||||
use super::common::{parse_db_type, parse_identifier, parse_table_name};
|
||||
use crate::syntax::{ColumnSchema, RawQuerySyntax, RawTableSchema};
|
||||
|
||||
pub fn parse_create(input: &str) -> IResult<&str, RawQuerySyntax> {
|
||||
let (input, _) = tag("CREATE")(input)?;
|
||||
|
|
@ -27,10 +28,7 @@ pub fn parse_create(input: &str) -> IResult<&str, RawQuerySyntax> {
|
|||
table_name: table_name.to_string(),
|
||||
columns: column_definitions,
|
||||
};
|
||||
Ok((
|
||||
input,
|
||||
RawQuerySyntax::CreateTable(schema),
|
||||
))
|
||||
Ok((input, RawQuerySyntax::CreateTable(schema)))
|
||||
}
|
||||
|
||||
fn parse_column_definitions(input: &str) -> IResult<&str, Vec<ColumnSchema>> {
|
||||
|
|
@ -51,7 +49,14 @@ fn parse_column_definition(input: &str) -> IResult<&str, ColumnSchema> {
|
|||
let (input, db_type) = parse_db_type(input)?;
|
||||
let (input, pk) = opt(parse_primary_key)(input).map(|(input, pk)| (input, pk.is_some()))?;
|
||||
let (input, _) = multispace0(input)?;
|
||||
Ok((input, ColumnSchema { column_name: identifier.to_string(), type_: db_type, is_primary: pk }))
|
||||
Ok((
|
||||
input,
|
||||
ColumnSchema {
|
||||
column_name: identifier.to_string(),
|
||||
type_: db_type,
|
||||
is_primary: pk,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -66,22 +71,28 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_parse_create_primary_key() {
|
||||
parse_create("CREATE TABLE \"Table1\"(id UUID PRIMARY KEY,column1 INT);").expect("should parse");
|
||||
parse_create("CREATE TABLE \"Table1\"(id UUID PRIMARY KEY,column1 INT);")
|
||||
.expect("should parse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_create_no_quotes_table_name() {
|
||||
parse_create("CREATE TABLE Table1(id UUID PRIMARY KEY,column1 INT);").expect("should parse");
|
||||
parse_create("CREATE TABLE Table1(id UUID PRIMARY KEY,column1 INT);")
|
||||
.expect("should parse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_create_primary_key_with_spaces() {
|
||||
parse_create("CREATE TABLE \"Table1\" ( id UUID PRIMARY KEY , column1 INT ) ;").expect("should parse");
|
||||
parse_create(
|
||||
"CREATE TABLE \"Table1\" ( id UUID PRIMARY KEY , column1 INT ) ;",
|
||||
)
|
||||
.expect("should parse");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_create() {
|
||||
let (_, create) = parse_create("CREATE TABLE \"Table1\"( id UUID , column1 INT );").expect("should parse");
|
||||
let (_, create) = parse_create("CREATE TABLE \"Table1\"( id UUID , column1 INT );")
|
||||
.expect("should parse");
|
||||
assert!(matches!(create, RawQuerySyntax::CreateTable(_)));
|
||||
match create {
|
||||
RawQuerySyntax::CreateTable(schema) => {
|
||||
|
|
@ -95,7 +106,9 @@ mod tests {
|
|||
|
||||
let result_column1 = schema.get_column(&"column1".to_string());
|
||||
assert!(matches!(result_column1, Some(_)));
|
||||
let Some(column1_column) = result_column1 else { panic!() };
|
||||
let Some(column1_column) = result_column1 else {
|
||||
panic!()
|
||||
};
|
||||
assert_eq!(column1_column.column_name, "column1".to_string());
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue