Skip to content

Instantly share code, notes, and snippets.

@rnewman
Created January 23, 2018 19:05
Show Gist options
  • Save rnewman/6235ef7da38431eb2c6d8ad87ddf123f to your computer and use it in GitHub Desktop.
Save rnewman/6235ef7da38431eb2c6d8ad87ddf123f to your computer and use it in GitHub Desktop.
Example test for prepared query
#[test]
fn test_simple_prepared_query() {
let mut c = new_connection("").expect("Couldn't open conn.");
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
conn.transact(&mut c, r#"[
[:db/add "s" :db/ident :foo/boolean]
[:db/add "s" :db/valueType :db.type/boolean]
[:db/add "s" :db/cardinality :db.cardinality/one]
]"#).expect("successful transaction");
conn.transact(&mut c, r#"[
[:db/add "u" :foo/boolean true]
[:db/add "p" :foo/boolean false]
]"#).expect("successful transaction");
let vv = Variable::from_valid_name("?v");
// TODO: write `with_type_sequence`.
let types = QueryInputs::with_type_sequence(vec![(vv, ValueType::Boolean)]);
let read = conn.begin_read(&mut c).expect("read");
// N.B., you might choose to algebrize _without_ validating that the
// types are known. In this query we know that `?v` must be a boolean,
// and so we can kinda generate our own required input types!
let prepared = read.q_prepare(r#"[:find [?x ...]
:in ?v
:where [?x :foo/boolean ?v]]"#,
types).expect("prepare succeeded");
let v_yes = QueryInputs::with_value_sequence(vec![(vv.clone(), true.into())]);
let v_no = QueryInputs::with_value_sequence(vec![(vv.clone(), false.into())]);
let yeses = prepared.run(v_yes).expect("result");
let nos = prepared.run(v_no).expect("result");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment