Skip to main content

qusql_parse/
kill.rs

1use crate::{
2    Span, Spanned,
3    expression::{Expression, PRIORITY_MAX, parse_expression_unreserved},
4    keywords::Keyword,
5    lexer::Token,
6    parser::{ParseError, Parser},
7};
8
9#[derive(Clone, Debug)]
10pub enum KillType {
11    Connection(Span),
12    Query(Span),
13}
14
15impl Spanned for KillType {
16    fn span(&self) -> Span {
17        match self {
18            KillType::Connection(s) => s.clone(),
19            KillType::Query(s) => s.clone(),
20        }
21    }
22}
23
24/// Represent a MySQL `KILL` statement
25///
26/// ```
27/// # use qusql_parse::{SQLDialect, SQLArguments, ParseOptions, parse_statements, Statement, Issues};
28/// # let options = ParseOptions::new().dialect(SQLDialect::MariaDB);
29/// let sql = "KILL CONNECTION 123;";
30/// let mut issues = Issues::new(sql);
31/// let mut stmts = parse_statements(sql, &mut issues, &options);
32/// # assert!(issues.is_ok(), "{}", issues);
33/// let kill_stmt = match stmts.pop() {
34///     Some(Statement::Kill(k)) => k,
35///     _ => panic!("We should get a kill statement"),
36/// };
37/// ```
38#[derive(Clone, Debug)]
39pub struct Kill<'a> {
40    pub kill_span: Span,
41    pub kill_type: Option<KillType>,
42    pub id: Expression<'a>,
43}
44
45impl<'a> Spanned for Kill<'a> {
46    fn span(&self) -> Span {
47        self.kill_span
48            .join_span(&self.kill_type)
49            .join_span(&self.id)
50    }
51}
52
53pub(crate) fn parse_kill<'a>(parser: &mut Parser<'a, '_>) -> Result<Kill<'a>, ParseError> {
54    let kill_span = parser.consume_keyword(Keyword::KILL)?;
55    let kill_type = match &parser.token {
56        Token::Ident(_, Keyword::CONNECTION) => Some(KillType::Connection(parser.consume())),
57        Token::Ident(_, Keyword::QUERY) => Some(KillType::Query(parser.consume())),
58        _ => None,
59    };
60    let id = parse_expression_unreserved(parser, PRIORITY_MAX)?;
61    Ok(Kill {
62        kill_span,
63        kill_type,
64        id,
65    })
66}