1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Transaction control language (TCL).
//!
//! This module houses the handlers for statements that manipulate the session's transaction,
//! like `BEGIN` and `COMMIT`.

use mz_sql_parser::ast::TransactionIsolationLevel;

use crate::ast::{
    CommitStatement, RollbackStatement, SetTransactionStatement, StartTransactionStatement,
    TransactionAccessMode, TransactionMode,
};
use crate::plan::statement::{StatementContext, StatementDesc};
use crate::plan::{
    AbortTransactionPlan, CommitTransactionPlan, Plan, PlanError, SetTransactionPlan,
    StartTransactionPlan, TransactionType,
};

pub fn describe_start_transaction(
    _: &StatementContext,
    _: StartTransactionStatement,
) -> Result<StatementDesc, PlanError> {
    Ok(StatementDesc::new(None))
}

pub fn plan_start_transaction(
    _: &StatementContext,
    StartTransactionStatement { modes }: StartTransactionStatement,
) -> Result<Plan, PlanError> {
    let (access, isolation_level) = verify_transaction_modes(modes)?;
    Ok(Plan::StartTransaction(StartTransactionPlan {
        access,
        isolation_level,
    }))
}

pub fn describe_set_transaction(
    _: &StatementContext,
    _: SetTransactionStatement,
) -> Result<StatementDesc, PlanError> {
    Ok(StatementDesc::new(None))
}

pub fn plan_set_transaction(
    _: &StatementContext,
    SetTransactionStatement { local, modes }: SetTransactionStatement,
) -> Result<Plan, PlanError> {
    Ok(Plan::SetTransaction(SetTransactionPlan { local, modes }))
}

fn verify_transaction_modes(
    modes: Vec<TransactionMode>,
) -> Result<
    (
        Option<TransactionAccessMode>,
        Option<TransactionIsolationLevel>,
    ),
    PlanError,
> {
    let mut access = None;
    let mut isolation = None;
    for mode in modes {
        match mode {
            TransactionMode::IsolationLevel(level) => isolation = Some(level),
            TransactionMode::AccessMode(mode) => {
                access = Some(mode);
            }
        }
    }
    Ok((access, isolation))
}

pub fn describe_rollback(
    _: &StatementContext,
    _: RollbackStatement,
) -> Result<StatementDesc, PlanError> {
    Ok(StatementDesc::new(None))
}

pub fn plan_rollback(
    _: &StatementContext,
    RollbackStatement { chain }: RollbackStatement,
) -> Result<Plan, PlanError> {
    verify_chain(chain)?;
    Ok(Plan::AbortTransaction(AbortTransactionPlan {
        transaction_type: TransactionType::Explicit,
    }))
}

pub fn describe_commit(
    _: &StatementContext,
    _: CommitStatement,
) -> Result<StatementDesc, PlanError> {
    Ok(StatementDesc::new(None))
}

pub fn plan_commit(
    _: &StatementContext,
    CommitStatement { chain }: CommitStatement,
) -> Result<Plan, PlanError> {
    verify_chain(chain)?;
    Ok(Plan::CommitTransaction(CommitTransactionPlan {
        transaction_type: TransactionType::Explicit,
    }))
}

fn verify_chain(chain: bool) -> Result<(), PlanError> {
    if chain {
        bail_unsupported!("CHAIN");
    }
    Ok(())
}