Skip to content
Merged
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
13 changes: 1 addition & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "kite_sql"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
authors = ["Kould <[email protected]>", "Xwg <[email protected]>"]
description = "SQL as a Function for Rust"
Expand Down Expand Up @@ -48,7 +48,6 @@ itertools = { version = "0.12" }
ordered-float = { version = "4", features = ["serde"] }
paste = { version = "1" }
parking_lot = { version = "0.12", features = ["arc_lock"] }
petgraph = { version = "0.6" }
recursive = { version = "0.1" }
regex = { version = "1" }
rust_decimal = { version = "1" }
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test-wasm:

## Run the sqllogictest harness against the configured .slt suite.
test-slt:
$(CARGO) run -p sqllogictest-test -- --path $(SQLLOGIC_PATH)
$(CARGO) run -p sqllogictest-test -- --path "$(SQLLOGIC_PATH)"

## Convenience target to run every suite in sequence.
test-all: test test-wasm test-slt
134 changes: 69 additions & 65 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,85 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm32"))]
mod app {
use kite_sql::db::{DataBaseBuilder, ResultIter};
use kite_sql::errors::DatabaseError;
use kite_sql::implement_from_tuple;
use kite_sql::types::value::DataValue;

use kite_sql::db::{DataBaseBuilder, ResultIter};
use kite_sql::errors::DatabaseError;
use kite_sql::implement_from_tuple;
use kite_sql::types::value::DataValue;

#[derive(Default, Debug, PartialEq)]
struct MyStruct {
pub c1: i32,
pub c2: String,
}
#[derive(Default, Debug, PartialEq)]
pub struct MyStruct {
pub c1: i32,
pub c2: String,
}

implement_from_tuple!(
MyStruct, (
c1: i32 => |inner: &mut MyStruct, value| {
if let DataValue::Int32(val) = value {
inner.c1 = val;
implement_from_tuple!(
MyStruct, (
c1: i32 => |inner: &mut MyStruct, value| {
if let DataValue::Int32(val) = value {
inner.c1 = val;
}
},
c2: String => |inner: &mut MyStruct, value| {
if let DataValue::Utf8 { value, .. } = value {
inner.c2 = value;
}
}
},
c2: String => |inner: &mut MyStruct, value| {
if let DataValue::Utf8 { value, .. } = value {
inner.c2 = value;
}
}
)
);
)
);

#[cfg(feature = "macros")]
fn main() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./example_data/hello_world").build()?;
pub fn run() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./example_data/hello_world").build()?;

// 1) Create table and insert multiple rows with mixed types.
database
.run(
"create table if not exists my_struct (
c1 int primary key,
c2 varchar,
c3 int
)",
)?
.done()?;
database
.run(
r#"
insert into my_struct values
(0, 'zero', 0),
(1, 'one', 1),
(2, 'two', 2)
"#,
)?
.done()?;
database
.run(
"create table if not exists my_struct (
c1 int primary key,
c2 varchar,
c3 int
)",
)?
.done()?;
database
.run(
r#"
insert into my_struct values
(0, 'zero', 0),
(1, 'one', 1),
(2, 'two', 2)
"#,
)?
.done()?;

// 2) Update and delete demo.
database
.run("update my_struct set c3 = c3 + 10 where c1 = 1")?
.done()?;
database.run("delete from my_struct where c1 = 2")?.done()?;
database
.run("update my_struct set c3 = c3 + 10 where c1 = 1")?
.done()?;
database.run("delete from my_struct where c1 = 2")?.done()?;

// 3) Query and deserialize into Rust struct.
let iter = database.run("select * from my_struct")?;
let schema = iter.schema().clone();
let iter = database.run("select * from my_struct")?;
let schema = iter.schema().clone();

for tuple in iter {
println!("{:?}", MyStruct::from((&schema, tuple?)));
}
for tuple in iter {
println!("{:?}", MyStruct::from((&schema, tuple?)));
}

let mut agg = database.run("select count(*) from my_struct")?;
if let Some(count_row) = agg.next() {
println!("row count = {:?}", count_row?);
}
agg.done()?;

// 4) Aggregate example.
let mut agg = database.run("select count(*) from my_struct")?;
if let Some(count_row) = agg.next() {
println!("row count = {:?}", count_row?);
database.run("drop table my_struct")?.done()?;

Ok(())
}
agg.done()?;
}

database.run("drop table my_struct")?.done()?;
#[cfg(target_arch = "wasm32")]
fn main() {}

Ok(())
#[cfg(all(not(target_arch = "wasm32"), feature = "macros"))]
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
app::run()
}
91 changes: 49 additions & 42 deletions examples/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,62 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm32"))]
mod app {
use kite_sql::db::{DataBaseBuilder, ResultIter};
use kite_sql::errors::DatabaseError;
use kite_sql::types::tuple::Tuple;
use kite_sql::types::value::DataValue;

use kite_sql::db::{DataBaseBuilder, ResultIter};
use kite_sql::errors::DatabaseError;
use kite_sql::types::tuple::Tuple;
use kite_sql::types::value::DataValue;
pub fn run() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./example_data/transaction").build_optimistic()?;
database
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
.done()?;
let mut transaction = database.new_transaction()?;

fn main() -> Result<(), DatabaseError> {
let database = DataBaseBuilder::path("./example_data/transaction").build_optimistic()?;
database
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
.done()?;
let mut transaction = database.new_transaction()?;
transaction
.run("insert into t1 values(0, 0), (1, 1)")?
.done()?;

transaction
.run("insert into t1 values(0, 0), (1, 1)")?
.done()?;
assert!(database.run("select * from t1")?.next().is_none());

assert!(database.run("select * from t1")?.next().is_none());
transaction.commit()?;

transaction.commit()?;
let mut iter = database.run("select * from t1")?;
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
);
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
);
assert!(iter.next().is_none());

let mut iter = database.run("select * from t1")?;
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
);
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
);
assert!(iter.next().is_none());
let mut tx2 = database.new_transaction()?;
tx2.run("update t1 set c2 = 99 where c1 = 0")?.done()?;
assert_eq!(
database
.run("select c2 from t1 where c1 = 0")?
.next()
.unwrap()?
.values[0]
.i32(),
Some(0)
);
drop(tx2);

// Scenario: another transaction updates but does not commit; changes stay invisible.
let mut tx2 = database.new_transaction()?;
tx2.run("update t1 set c2 = 99 where c1 = 0")?.done()?;
assert_eq!(
database
.run("select c2 from t1 where c1 = 0")?
.next()
.unwrap()?
.values[0]
.i32(),
Some(0)
);
// rollback
drop(tx2);
database.run("drop table t1")?.done()?;

Ok(())
}
}

database.run("drop table t1")?.done()?;
#[cfg(target_arch = "wasm32")]
fn main() {}

Ok(())
#[cfg(not(target_arch = "wasm32"))]
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
app::run()
}
5 changes: 1 addition & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,8 @@ impl<S: Storage> State<S> {
/// Limit(1)
/// Project(a,b)
let source_plan = binder.bind(stmt)?;
// println!("source_plan plan: {:#?}", source_plan);

let best_plan = Self::default_optimizer(source_plan)
.find_best(Some(&transaction.meta_loader(meta_cache)))?;
// println!("best_plan plan: {:#?}", best_plan);

Ok(best_plan)
}
Expand Down Expand Up @@ -356,7 +353,7 @@ impl<S: Storage> Database<S> {
self.state.prepare(sql)
}

fn execute<A: AsRef<[(&'static str, DataValue)]>>(
pub fn execute<A: AsRef<[(&'static str, DataValue)]>>(
&self,
statement: &Statement,
params: A,
Expand Down
Loading
Loading