Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ let kite_sql = DataBaseBuilder::path("./data")
- Pessimistic (Default)
- Optimistic
- Isolation level: repeatable read; every statement in a transaction reads the snapshot taken when it began
- SQL transaction control through `Database::session()`: `BEGIN`/`START TRANSACTION`, `COMMIT` and `ROLLBACK` as statements; nested `BEGIN` follows the dialect, and `SET autocommit` is accepted by a dialect that parses it (`SqlParser::with_set_autocommit()`)

### Checkpoint
KiteSQL exposes checkpoint as a storage capability rather than a full backup workflow. A checkpoint only creates a consistent local snapshot directory; compressing, uploading, retaining, and pruning backups should stay in application code.
Expand Down Expand Up @@ -436,6 +437,7 @@ The `SqlDialect` trait methods:
- `parse()` — Parses SQL text into KiteSQL statements; the dialect rejects syntax it does not support and may rewrite the statements
- `comparison_type()` — Optional hook to pick the type both operands of a comparison (`=`, `<>`, `<`, `<=`, `>`, `>=`, `IN`, `BETWEEN`) are cast to
- `boolean_text()` — Optional hook to choose the text of a boolean in `CAST(.. AS CHAR)` or `CAST(.. AS VARCHAR)` (default `'true'`/`'false'`)
- `begin_commits_active_transaction()` — Optional hook to make `BEGIN` in a `Session` commit the open transaction and start a new one (default: rejected)

`DataTypeMapper::map_data_type()` maps types inside expressions (`CAST`, typed literals); `map_column_type()` maps the types of column definitions and defaults to `map_data_type()`.

Expand Down
6 changes: 6 additions & 0 deletions src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub enum Statement {
},
ShowTables,
ShowViews,
/// `BEGIN` or `START TRANSACTION`; run through [`Session`](crate::db::Session).
StartTransaction,
Commit,
Rollback,
/// `SET autocommit = ..`
SetAutocommit(bool),
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
36 changes: 33 additions & 3 deletions src/binder/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,19 @@ pub fn command_type(stmt: &Statement) -> Result<CommandType, DatabaseError> {
| Statement::Insert(_) => CommandType::DML,
Statement::Copy(copy) if copy.to => CommandType::DQL,
Statement::Copy(_) => CommandType::DML,
Statement::StartTransaction
| Statement::Commit
| Statement::Rollback
| Statement::SetAutocommit(_) => return Err(transaction_control_unsupported()),
})
}

fn transaction_control_unsupported() -> DatabaseError {
DatabaseError::UnsupportedStmt(
"transaction control statements require `Database::session`".to_string(),
)
}

impl<S: Storage, D: SqlDialect> State<S, D> {
pub(crate) fn prepare_all<T: AsRef<str>>(
&self,
Expand Down Expand Up @@ -205,6 +215,14 @@ impl<S: Storage, D: SqlDialect> Database<S, D> {
.state
.prepare_all(sql)
.map_err(|err| err.with_sql_context(sql))?;
self.run_statements(sql, statements).await
}

pub(crate) async fn run_statements(
&self,
sql: &str,
statements: Vec<Statement>,
) -> Result<DatabaseIter<'_, S>, DatabaseError> {
let has_catalog_mutation = statements
.iter()
.try_fold(false, |has_mutation, stmt| {
Expand Down Expand Up @@ -273,10 +291,18 @@ impl<'txn, S: Storage, D: SqlDialect> Transaction<'txn, S, D> {
sql: T,
) -> Result<TransactionIter<'a, 'a, S::TransactionType<'txn>>, DatabaseError> {
let sql = sql.as_ref();
let mut statements = self
let statements = self
.state
.prepare_all(sql)
.map_err(|err| err.with_sql_context(sql))?;
self.run_statements(sql, statements).await
}

pub(crate) async fn run_statements<'a>(
&'a mut self,
sql: &str,
mut statements: Vec<Statement>,
) -> Result<TransactionIter<'a, 'a, S::TransactionType<'txn>>, DatabaseError> {
for statement in &statements {
if statement_mutates_catalog_or_statistics(statement)? {
return Err(DatabaseError::UnsupportedStmt(
Expand Down Expand Up @@ -385,6 +411,10 @@ where
}
Statement::CreateIndex(create) => self.create_index(create)?,
Statement::CreateView(create) => self.create_view(create)?,
Statement::StartTransaction
| Statement::Commit
| Statement::Rollback
| Statement::SetAutocommit(_) => return Err(transaction_control_unsupported()),
};

Ok(BindStatementComplete { plan })
Expand Down Expand Up @@ -2700,8 +2730,8 @@ mod tests {
}

assert_unsupported(
database.prepare_all("start transaction").unwrap_err(),
"START TRANSACTION",
command_type(&database.prepare_all("start transaction")?.pop().unwrap()).unwrap_err(),
"`Database::session`",
);

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

mod program;
mod session;
pub use crate::ast::Statement;
use crate::binder::{Binder, BinderContext};
use crate::dialect::{DynDialect, GenericSqlDialect, SqlDialect};
Expand Down Expand Up @@ -62,6 +63,7 @@ use crate::types::value::DataValue;
use crate::types::LogicalType;
use ordered_float::OrderedFloat;
pub use program::{Param, Program, ProgramIter};
pub use session::{Session, SessionIter};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;
Expand Down
Loading