#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn ints(c: &Connection, sql: &str) -> Vec<i64> {
c.query(sql)
.unwrap()
.rows
.into_iter()
.map(|mut r| match r.remove(0) {
Value::Integer(i) => i,
other => panic!("{other:?}"),
})
.collect()
}
#[test]
fn limit_offset_accept_subqueries() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a)").unwrap();
c.execute("INSERT INTO t VALUES(1),(2),(3),(4),(5)")
.unwrap();
assert_eq!(
ints(&c, "SELECT a FROM t ORDER BY a LIMIT (SELECT 3)"),
vec![1, 2, 3]
);
assert_eq!(
ints(
&c,
"SELECT a FROM t ORDER BY a LIMIT (SELECT 2) OFFSET (SELECT 1)"
),
vec![2, 3]
);
assert_eq!(
ints(
&c,
"SELECT a FROM t ORDER BY a LIMIT (SELECT max(a) FROM t) - 3"
),
vec![1, 2]
);
assert_eq!(
ints(
&c,
"WITH RECURSIVE r(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM r WHERE x<10) \
SELECT x FROM r LIMIT (SELECT 4)"
),
vec![1, 2, 3, 4]
);
}