Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Commit

Permalink
Added support for parsing a primary key #2
Browse files Browse the repository at this point in the history
  • Loading branch information
chotchki committed Aug 28, 2021
1 parent 00d4860 commit 510aaba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/engine/objects/parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct RawColumn {
pub name: String,
pub sql_type: String,
pub null: bool,
pub primary_key: bool,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down
27 changes: 25 additions & 2 deletions src/engine/sql_parser/create/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,24 @@ fn match_columns<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
fn match_column_attribute<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, RawColumn, E> {
let (input, (_, name, _, sql_type, _, is_null, _)) = tuple((
let (input, (_, name, _, sql_type, _, is_null, _, is_primary_key, _)) = tuple((
maybe_take_whitespace,
parse_sql_identifier,
take_whitespace,
parse_sql_identifier,
maybe_take_whitespace,
is_null,
maybe_take_whitespace,
is_primary_key,
maybe_take_whitespace,
))(input)?;
Ok((
input,
RawColumn {
name: name.to_string(),
sql_type: sql_type.to_string(),
null: is_null,
primary_key: is_primary_key,
},
))
}
Expand All @@ -90,6 +93,16 @@ fn is_null<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
Ok((input, true))
}

fn is_primary_key<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, bool, E> {
let (input, primary_key) = opt(match_primary_key)(input)?;
match primary_key {
Some(()) => Ok((input, true)),
None => Ok((input, false)),
}
}

fn match_not_null<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, (), E> {
Expand All @@ -105,14 +118,22 @@ fn match_null<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
Ok((input, ()))
}

fn match_primary_key<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
input: &'a str,
) -> IResult<&'a str, (), E> {
let (input, (_, _, _)) =
tuple((tag_no_case("primary"), take_whitespace, tag_no_case("key")))(input)?;
Ok((input, ()))
}

#[cfg(test)]
mod tests {
use super::*;
use nom::error::{convert_error, VerboseError};

#[test]
fn test_simple_table() -> Result<(), Box<dyn std::error::Error>> {
let test_string = "create table foo (bar text, baz text not null)";
let test_string = "create table foo (bar text primary key, baz text not null)";

let (_, result) = parse_create_table::<VerboseError<&str>>(test_string)?;

Expand All @@ -128,11 +149,13 @@ mod tests {
name: "bar".to_string(),
sql_type: "text".to_string(),
null: true,
primary_key: true,
},
RawColumn {
name: "baz".to_string(),
sql_type: "text".to_string(),
null: false,
primary_key: false,
},
];
assert_eq!(columns, result.provided_columns);
Expand Down
6 changes: 4 additions & 2 deletions tests/sql_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ async fn create_table_with_nullable() -> Result<(), Box<dyn std::error::Error>>
client
.batch_execute(
"create table foo (
bar text,
bar text primary key,
baz text not null,
another text null
)",
)
.await?;

common::_request_shutdown(request_shutdown).await
common::_request_shutdown(request_shutdown).await?;

Ok(())
}

0 comments on commit 510aaba

Please sign in to comment.