#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
#[test]
fn non_integer_limit_offset_errors() {
let c = Connection::open_memory().unwrap();
for sql in [
"SELECT 1 LIMIT 1.9",
"SELECT 1 LIMIT '2abc'",
"SELECT 1 LIMIT 'abc'",
"SELECT 1 LIMIT NULL",
"SELECT 1 LIMIT 5 OFFSET NULL",
"SELECT 1 LIMIT 5 OFFSET 1.5",
"SELECT 1 LIMIT x'32'",
"SELECT 1 LIMIT 3.0/2",
] {
assert!(c.query(sql).is_err(), "{sql} should be a datatype mismatch");
}
}
#[test]
fn non_integer_limit_offset_errors_over_a_table_scan() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a)").unwrap();
c.execute("INSERT INTO t VALUES(1),(2),(3)").unwrap();
for sql in [
"SELECT a FROM t LIMIT 1.9",
"SELECT a FROM t LIMIT '2abc'",
"SELECT a FROM t LIMIT 'abc'",
"SELECT a FROM t LIMIT NULL",
"SELECT a FROM t LIMIT x'32'",
"SELECT a FROM t LIMIT 2 OFFSET 1.5",
"SELECT a FROM t LIMIT 2 OFFSET NULL",
"SELECT a FROM t LIMIT 2 OFFSET 'x'",
] {
assert!(c.query(sql).is_err(), "{sql} should be a datatype mismatch");
}
}
#[test]
fn integer_valued_limits_work() {
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();
let g = |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()
};
assert_eq!(g("SELECT a FROM t ORDER BY a LIMIT 2"), vec![1, 2]);
assert_eq!(g("SELECT a FROM t ORDER BY a LIMIT 2.0"), vec![1, 2]);
assert_eq!(g("SELECT a FROM t ORDER BY a LIMIT '3'"), vec![1, 2, 3]);
assert_eq!(g("SELECT a FROM t ORDER BY a LIMIT 2+1"), vec![1, 2, 3]);
assert_eq!(g("SELECT a FROM t ORDER BY a LIMIT 1.0"), vec![1]);
assert_eq!(
g("SELECT a FROM t ORDER BY a LIMIT -1 OFFSET 3"),
vec![4, 5]
);
assert_eq!(
g("SELECT a FROM t ORDER BY a LIMIT 2 OFFSET '1'"),
vec![2, 3]
);
}