Skip to content

Commit

Permalink
replication: support SAVEPOINT
Browse files Browse the repository at this point in the history
Now that we support savepoints in the server, let's also allow them
in the client library.

Refs tursodatabase/libsql-experimental-python#6
  • Loading branch information
psarna committed Oct 28, 2023
1 parent e39fe86 commit 5c1b525
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 9 additions & 0 deletions libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ impl State {

(State::TxnReadOnly, StmtKind::TxnEnd) | (State::Txn, StmtKind::TxnEnd) => State::Init,

// Savepoint only makes sense within a transaction and doesn't change the transaction kind
(State::TxnReadOnly, StmtKind::Savepoint) => State::TxnReadOnly,
(State::Txn, StmtKind::Savepoint) => State::Txn,
(_, StmtKind::Savepoint) => State::Invalid,
// Releasing a savepoint only makes sense inside a transaction and it doesn't change its state
(State::TxnReadOnly, StmtKind::Release) => State::TxnReadOnly,
(State::Txn, StmtKind::Release) => State::Txn,
(_, StmtKind::Release) => State::Invalid,

(state, StmtKind::Other | StmtKind::Write | StmtKind::Read) => state,
(State::Invalid, _) => State::Invalid,

Expand Down
32 changes: 31 additions & 1 deletion libsql/src/replication/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum StmtKind {
TxnEnd,
Read,
Write,
Savepoint,
Release,
Other,
}

Expand All @@ -52,7 +54,13 @@ impl StmtKind {
Some(Self::TxnBeginReadOnly)
}
Cmd::Stmt(Stmt::Begin { .. }) => Some(Self::TxnBegin),
Cmd::Stmt(Stmt::Commit { .. } | Stmt::Rollback { .. }) => Some(Self::TxnEnd),
Cmd::Stmt(
Stmt::Commit { .. }
| Stmt::Rollback {
savepoint_name: None,
..
},
) => Some(Self::TxnEnd),
Cmd::Stmt(
Stmt::CreateVirtualTable { tbl_name, .. }
| Stmt::CreateTable {
Expand Down Expand Up @@ -100,6 +108,12 @@ impl StmtKind {
temporary: false, ..
}) => Some(Self::Write),
Cmd::Stmt(Stmt::DropView { .. }) => Some(Self::Write),
Cmd::Stmt(Stmt::Savepoint(_)) => Some(Self::Savepoint),
Cmd::Stmt(Stmt::Release(_))
| Cmd::Stmt(Stmt::Rollback {
savepoint_name: Some(_),
..
}) => Some(Self::Release),
_ => None,
}
}
Expand Down Expand Up @@ -168,6 +182,22 @@ impl StmtKind {
},
}
}

/// Returns `true` if the stmt kind is [`Savepoint`].
///
/// [`Savepoint`]: StmtKind::Savepoint
#[must_use]
pub fn is_savepoint(&self) -> bool {
matches!(self, Self::Savepoint)
}

/// Returns `true` if the stmt kind is [`Release`].
///
/// [`Release`]: StmtKind::Release
#[must_use]
pub fn is_release(&self) -> bool {
matches!(self, Self::Release)
}
}

impl Statement {
Expand Down

0 comments on commit 5c1b525

Please sign in to comment.