Skip to content

Commit

Permalink
libsql,namespaces: add client-side ATTACH support
Browse files Browse the repository at this point in the history
  • Loading branch information
psarna committed Dec 14, 2023
1 parent bc47b06 commit 04e85b0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 1 addition & 2 deletions libsql-server/tests/namespaces/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ fn meta_store() {
)?;
let foo_conn = foo.connect()?;

foo_conn.execute("attach foo as foo", ()).await.unwrap();
foo_conn
.execute("select * from foo.sqlite_master", ())
.execute_batch("attach foo as foo; select * from foo.sqlite_master")
.await
.unwrap();
}
Expand Down
10 changes: 9 additions & 1 deletion libsql/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{Error, Result};
use fallible_iterator::FallibleIterator;
use sqlite3_parser::ast::{Cmd, PragmaBody, QualifiedName, Stmt, TransactionType};
use sqlite3_parser::ast::{Cmd, PragmaBody, QualifiedName, Stmt, TransactionType, Expr, Id};
use sqlite3_parser::lexer::sql::{Parser, ParserError};

/// A group of statements to be executed together.
Expand Down Expand Up @@ -30,6 +30,8 @@ pub enum StmtKind {
Write,
Savepoint,
Release,
Attach,
Detach,
Other,
}

Expand Down Expand Up @@ -116,6 +118,12 @@ impl StmtKind {
savepoint_name: Some(_),
..
}) => Some(Self::Release),
Cmd::Stmt(Stmt::Attach {
expr: Expr::Id(Id(expr)),
db_name: Expr::Id(Id(name)),
..
}) if expr == name => Some(Self::Attach),
Cmd::Stmt(Stmt::Detach(_)) => Some(Self::Detach),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion libsql/src/replication/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl State {
(State::Txn, StmtKind::Release) => State::Txn,
(_, StmtKind::Release) => State::Invalid,

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

(State::Init, StmtKind::TxnBegin) => State::Txn,
Expand Down

0 comments on commit 04e85b0

Please sign in to comment.