Skip to content

Commit 9cd2ec9

Browse files
committed
support for DEFAULT/DEFAULT(col) for INSERTs
1 parent 72f4824 commit 9cd2ec9

4 files changed

Lines changed: 207 additions & 53 deletions

File tree

src/common.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ use nom::branch::alt;
22
use nom::character::complete::{alphanumeric1, digit1, line_ending, multispace0, multispace1};
33
use nom::character::is_alphanumeric;
44
use nom::combinator::{map, not, peek};
5-
use nom::{IResult, InputLength, Parser, Err};
5+
use nom::{Err, IResult, InputLength, Parser};
66
use std::fmt::{self, Display};
77
use std::str;
88
use std::str::FromStr;
99

1010
use arithmetic::{arithmetic_expression, ArithmeticExpression};
1111
use case::case_when_column;
1212
use column::{Column, FunctionArgument, FunctionArguments, FunctionExpression};
13+
use insert::InsertDataValue;
1314
use keywords::{escape_if_keyword, sql_keyword};
1415
use nom::bytes::complete::{is_not, tag, tag_no_case, take, take_until, take_while1};
1516
use nom::combinator::opt;
@@ -812,7 +813,10 @@ pub fn sql_identifier(i: &[u8]) -> IResult<&[u8], &[u8]> {
812813
))(i)?;
813814

814815
if str::from_utf8(si).unwrap_or("0").parse::<usize>().is_ok() {
815-
return Err(Err::Error(Error { input: i, code: ErrorKind::IsA }));
816+
return Err(Err::Error(Error {
817+
input: i,
818+
code: ErrorKind::IsA,
819+
}));
816820
}
817821

818822
Ok((i, si))
@@ -879,7 +883,8 @@ fn field_value_expr(i: &[u8]) -> IResult<&[u8], FieldAssignmentValue> {
879883
FieldValueExpression::Literal(LiteralExpression {
880884
value: l.into(),
881885
alias: None,
882-
}).into()
886+
})
887+
.into()
883888
}),
884889
))(i)
885890
}
@@ -1066,6 +1071,28 @@ pub fn value_list(i: &[u8]) -> IResult<&[u8], Vec<Literal>> {
10661071
many0(delimited(multispace0, literal, opt(ws_sep_comma)))(i)
10671072
}
10681073

1074+
pub fn insert_data_value_list(i: &[u8]) -> IResult<&[u8], Vec<InsertDataValue>> {
1075+
many0(delimited(multispace0, insert_data_value, opt(ws_sep_comma)))(i)
1076+
}
1077+
1078+
pub fn insert_data_value(i: &[u8]) -> IResult<&[u8], InsertDataValue> {
1079+
alt((
1080+
map(
1081+
tuple((
1082+
tag_no_case("DEFAULT"),
1083+
tag("("),
1084+
multispace0,
1085+
column_identifier_no_alias,
1086+
multispace0,
1087+
tag(")"),
1088+
)),
1089+
|(_, _, _, c, _, _)| InsertDataValue::ColumnDefault(c),
1090+
),
1091+
map(tag_no_case("DEFAULT"), |_| InsertDataValue::Default),
1092+
map(literal, |l| InsertDataValue::Literal(l)),
1093+
))(i)
1094+
}
1095+
10691096
// Parse a reference to a named schema.table, with an optional alias
10701097
pub fn schema_table_reference(i: &[u8]) -> IResult<&[u8], Table> {
10711098
map(

0 commit comments

Comments
 (0)