Skip to content

Commit a954449

Browse files
ZacJWayman-sigma
authored andcommitted
Support procedure argmode (apache#1901)
1 parent c9b68de commit a954449

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

src/ast/ddl.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use sqlparser_derive::{Visit, VisitMut};
3030

3131
use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
33-
display_comma_separated, display_separated, CommentDef, CreateFunctionBody,
33+
display_comma_separated, display_separated, ArgMode, CommentDef, CreateFunctionBody,
3434
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
3535
FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexColumn, MySQLColumnPosition,
3636
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
@@ -1367,11 +1367,16 @@ impl fmt::Display for NullsDistinctOption {
13671367
pub struct ProcedureParam {
13681368
pub name: Ident,
13691369
pub data_type: DataType,
1370+
pub mode: Option<ArgMode>,
13701371
}
13711372

13721373
impl fmt::Display for ProcedureParam {
13731374
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1374-
write!(f, "{} {}", self.name, self.data_type)
1375+
if let Some(mode) = &self.mode {
1376+
write!(f, "{mode} {} {}", self.name, self.data_type)
1377+
} else {
1378+
write!(f, "{} {}", self.name, self.data_type)
1379+
}
13751380
}
13761381
}
13771382

src/parser/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7655,9 +7655,22 @@ impl<'a> Parser<'a> {
76557655
}
76567656

76577657
pub fn parse_procedure_param(&mut self) -> Result<ProcedureParam, ParserError> {
7658+
let mode = if self.parse_keyword(Keyword::IN) {
7659+
Some(ArgMode::In)
7660+
} else if self.parse_keyword(Keyword::OUT) {
7661+
Some(ArgMode::Out)
7662+
} else if self.parse_keyword(Keyword::INOUT) {
7663+
Some(ArgMode::InOut)
7664+
} else {
7665+
None
7666+
};
76587667
let name = self.parse_identifier()?;
76597668
let data_type = self.parse_data_type()?;
7660-
Ok(ProcedureParam { name, data_type })
7669+
Ok(ProcedureParam {
7670+
name,
7671+
data_type,
7672+
mode,
7673+
})
76617674
}
76627675

76637676
pub fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {

tests/sqlparser_common.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15382,3 +15382,65 @@ fn check_enforced() {
1538215382
"CREATE TABLE t (a INT, b INT, c INT, CHECK (a > 0) NOT ENFORCED, CHECK (b > 0) ENFORCED, CHECK (c > 0))",
1538315383
);
1538415384
}
15385+
15386+
#[test]
15387+
fn parse_create_procedure_with_parameter_modes() {
15388+
let sql = r#"CREATE PROCEDURE test_proc (IN a INTEGER, OUT b TEXT, INOUT c TIMESTAMP, d BOOL) AS BEGIN SELECT 1; END"#;
15389+
match verified_stmt(sql) {
15390+
Statement::CreateProcedure {
15391+
or_alter,
15392+
name,
15393+
params,
15394+
..
15395+
} => {
15396+
assert_eq!(or_alter, false);
15397+
assert_eq!(name.to_string(), "test_proc");
15398+
let fake_span = Span {
15399+
start: Location { line: 0, column: 0 },
15400+
end: Location { line: 0, column: 0 },
15401+
};
15402+
assert_eq!(
15403+
params,
15404+
Some(vec![
15405+
ProcedureParam {
15406+
name: Ident {
15407+
value: "a".into(),
15408+
quote_style: None,
15409+
span: fake_span,
15410+
},
15411+
data_type: DataType::Integer(None),
15412+
mode: Some(ArgMode::In)
15413+
},
15414+
ProcedureParam {
15415+
name: Ident {
15416+
value: "b".into(),
15417+
quote_style: None,
15418+
span: fake_span,
15419+
},
15420+
data_type: DataType::Text,
15421+
mode: Some(ArgMode::Out)
15422+
},
15423+
ProcedureParam {
15424+
name: Ident {
15425+
value: "c".into(),
15426+
quote_style: None,
15427+
span: fake_span,
15428+
},
15429+
data_type: DataType::Timestamp(None, TimezoneInfo::None),
15430+
mode: Some(ArgMode::InOut)
15431+
},
15432+
ProcedureParam {
15433+
name: Ident {
15434+
value: "d".into(),
15435+
quote_style: None,
15436+
span: fake_span,
15437+
},
15438+
data_type: DataType::Bool,
15439+
mode: None
15440+
},
15441+
])
15442+
);
15443+
}
15444+
_ => unreachable!(),
15445+
}
15446+
}

tests/sqlparser_mssql.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ fn parse_create_procedure() {
153153
quote_style: None,
154154
span: Span::empty(),
155155
},
156-
data_type: DataType::Int(None)
156+
data_type: DataType::Int(None),
157+
mode: None,
157158
},
158159
ProcedureParam {
159160
name: Ident {
@@ -164,7 +165,8 @@ fn parse_create_procedure() {
164165
data_type: DataType::Varchar(Some(CharacterLength::IntegerLength {
165166
length: 256,
166167
unit: None
167-
}))
168+
})),
169+
mode: None,
168170
}
169171
]),
170172
name: ObjectName::from(vec![Ident {

0 commit comments

Comments
 (0)