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
// 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,
//! like `BEGIN` and `COMMIT`.

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

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

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

pub fn describe_set_transaction(
    _: &StatementContext,
    _: SetTransactionStatement,
) -> Result<StatementDesc, anyhow::Error> {
    bail_unsupported!("SET TRANSACTION")
}

pub fn plan_set_transaction(
    _: &StatementContext,
    _: SetTransactionStatement,
) -> Result<Plan, anyhow::Error> {
    bail_unsupported!("SET TRANSACTION")
}

fn verify_transaction_modes(
    modes: Vec<TransactionMode>,
) -> Result<Option<TransactionAccessMode>, anyhow::Error> {
    let mut access = None;
    for mode in modes {
        match mode {
            // Although we are only serializable, it's not wrong to accept lower isolation
            // levels because we still meet the required guarantees for those.
            TransactionMode::IsolationLevel(_) => {}
            TransactionMode::AccessMode(mode) => {
                access = Some(mode);
            }
        }
    }
    Ok(access)
}

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

pub fn plan_rollback(
    _: &StatementContext,
    RollbackStatement { chain }: RollbackStatement,
) -> Result<Plan, anyhow::Error> {
    verify_chain(chain)?;
    Ok(Plan::AbortTransaction)
}

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

pub fn plan_commit(
    _: &StatementContext,
    CommitStatement { chain }: CommitStatement,
) -> Result<Plan, anyhow::Error> {
    verify_chain(chain)?;
    Ok(Plan::CommitTransaction)
}

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