Skip to content

Commit

Permalink
Merge pull request #299 from roc-lang/sqlite
Browse files Browse the repository at this point in the history
Add Sqlite to basic-cli
  • Loading branch information
bhansconnect authored Dec 31, 2024
2 parents f9f25fd + 7e24848 commit 91ddbce
Show file tree
Hide file tree
Showing 16 changed files with 1,557 additions and 6 deletions.
56 changes: 50 additions & 6 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"crates/roc_io_error",
"crates/roc_stdio",
"crates/roc_env",
"crates/roc_sqlite",
]

[workspace.package]
Expand All @@ -36,6 +37,7 @@ roc_http = { path = "crates/roc_http" }
roc_io_error = { path = "crates/roc_io_error" }
roc_stdio = { path = "crates/roc_stdio" }
roc_env = { path = "crates/roc_env" }
roc_sqlite = { path = "crates/roc_sqlite" }
memchr = "=2.7.4"
hyper = { version = "=0.14.27", default-features = false, features = [
"http1",
Expand All @@ -53,3 +55,5 @@ crossterm = "=0.27.0"
memmap2 = "0.9.4"
libc = "=0.2.155"
backtrace = "=0.3.69"
libsqlite3-sys = { version = "0.30.1", features = ["bundled"] }
thread_local = "1.1.8"
2 changes: 2 additions & 0 deletions ci/all_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ for roc_file in $EXAMPLES_DIR*.roc; do
cd $EXAMPLES_DIR
$absolute_roc dev $base_file $ROC_BUILD_FLAGS
cd ..
elif [ "$base_file" == "sqlite.roc" ]; then
DB_PATH=${EXAMPLES_DIR}todos.db $ROC dev $roc_file $ROC_BUILD_FLAGS
elif [ "$base_file" == "temp-dir.roc" ]; then
$ROC dev $roc_file $ROC_BUILD_FLAGS --linker=legacy
else
Expand Down
28 changes: 28 additions & 0 deletions ci/expect_scripts/sqlite.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/expect

# uncomment line below for debugging
# exp_internal 1

set timeout 7

source ./ci/expect_scripts/shared-code.exp

set env(DB_PATH) $env(EXAMPLES_DIR)todos.db

spawn $env(EXAMPLES_DIR)sqlite


expect "Todo Tasks:" {
expect "\tid: 3, task: Share my ❤️ for Roc" {
expect "Completed Tasks:" {
expect "\tid: 1, task: Prepare for AoC" {
expect eof {
check_exit_and_segfault
}
}
}
}
}

puts stderr "\nError: output was different from expected value."
exit 1
1 change: 1 addition & 0 deletions crates/roc_host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ roc_io_error.workspace = true
roc_http.workspace = true
roc_stdio.workspace = true
roc_env.workspace = true
roc_sqlite.workspace = true
hyper.workspace = true
hyper-rustls.workspace = true
tokio.workspace = true
52 changes: 52 additions & 0 deletions crates/roc_host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub unsafe extern "C" fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) {
heap.dealloc(c_ptr);
return;
}
let heap = roc_sqlite::heap();
if heap.in_range(c_ptr) {
heap.dealloc(c_ptr);
return;
}
libc::free(c_ptr)
}

Expand Down Expand Up @@ -327,6 +332,12 @@ pub fn init() {
roc_fx_temp_dir as _,
roc_fx_get_locale as _,
roc_fx_get_locales as _,
roc_fx_sqlite_bind as _,
roc_fx_sqlite_column_value as _,
roc_fx_sqlite_columns as _,
roc_fx_sqlite_prepare as _,
roc_fx_sqlite_reset as _,
roc_fx_sqlite_step as _,
];
#[allow(forgetting_references)]
std::mem::forget(std::hint::black_box(funcs));
Expand Down Expand Up @@ -693,3 +704,44 @@ pub extern "C" fn roc_fx_get_locale() -> RocResult<RocStr, ()> {
pub extern "C" fn roc_fx_get_locales() -> RocList<RocStr> {
roc_env::get_locales()
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_bind(
stmt: RocBox<()>,
bindings: &RocList<roc_sqlite::SqliteBindings>,
) -> RocResult<(), roc_sqlite::SqliteError> {
roc_sqlite::bind(stmt, bindings)
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_prepare(
db_path: &roc_std::RocStr,
query: &roc_std::RocStr,
) -> roc_std::RocResult<RocBox<()>, roc_sqlite::SqliteError> {
roc_sqlite::prepare(db_path, query)
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_columns(stmt: RocBox<()>) -> RocList<RocStr> {
roc_sqlite::columns(stmt)
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_column_value(
stmt: RocBox<()>,
i: u64,
) -> RocResult<roc_sqlite::SqliteValue, roc_sqlite::SqliteError> {
roc_sqlite::column_value(stmt, i)
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_step(
stmt: RocBox<()>,
) -> RocResult<roc_sqlite::SqliteState, roc_sqlite::SqliteError> {
roc_sqlite::step(stmt)
}

#[no_mangle]
pub extern "C" fn roc_fx_sqlite_reset(stmt: RocBox<()>) -> RocResult<(), roc_sqlite::SqliteError> {
roc_sqlite::reset(stmt)
}
14 changes: 14 additions & 0 deletions crates/roc_sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "roc_sqlite"
description = "Common functionality for Roc to interface with sqlite"

authors.workspace = true
edition.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
roc_std.workspace = true
roc_std_heap.workspace = true
libsqlite3-sys.workspace = true
thread_local.workspace = true
Loading

0 comments on commit 91ddbce

Please sign in to comment.