#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn rows(c: &Connection, sql: &str) -> Vec<Vec<Value>> {
c.query(sql).unwrap().rows
}
#[test]
fn constraint_level_on_conflict_action() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a UNIQUE ON CONFLICT REPLACE, b)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,'x')").unwrap();
c.execute("INSERT INTO t VALUES(1,'y')").unwrap(); assert_eq!(
rows(&c, "SELECT a,b FROM t"),
[vec![Value::Integer(1), Value::Text("y".into())]]
);
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a UNIQUE ON CONFLICT IGNORE, b)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,'x')").unwrap();
c.execute("INSERT INTO t VALUES(1,'y')").unwrap(); assert_eq!(rows(&c, "SELECT b FROM t"), [vec![Value::Text("x".into())]]);
c.execute("INSERT OR REPLACE INTO t VALUES(1,'z')").unwrap();
assert_eq!(rows(&c, "SELECT b FROM t"), [vec![Value::Text("z".into())]]);
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a, b, UNIQUE(a) ON CONFLICT REPLACE)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,'x'),(1,'y')").unwrap();
assert_eq!(
rows(&c, "SELECT a,b FROM t"),
[vec![Value::Integer(1), Value::Text("y".into())]]
);
assert_eq!(
rows(&c, "SELECT sql FROM sqlite_master WHERE type='table'"),
[vec![Value::Text(
"CREATE TABLE t(a, b, UNIQUE(a) ON CONFLICT REPLACE)".into()
)]]
);
}
#[test]
fn not_null_on_conflict_action() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a NOT NULL ON CONFLICT IGNORE, b)")
.unwrap();
c.execute("INSERT INTO t VALUES(NULL,'x'),(1,'y')").unwrap(); assert_eq!(
rows(&c, "SELECT a,b FROM t"),
[vec![Value::Integer(1), Value::Text("y".into())]]
);
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a NOT NULL ON CONFLICT REPLACE DEFAULT 9, b)")
.unwrap();
c.execute("INSERT INTO t VALUES(NULL,'x')").unwrap(); assert_eq!(rows(&c, "SELECT a FROM t"), [vec![Value::Integer(9)]]);
c.execute("UPDATE t SET a=NULL").unwrap();
assert_eq!(rows(&c, "SELECT a FROM t"), [vec![Value::Integer(9)]]);
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a NOT NULL ON CONFLICT REPLACE, b)")
.unwrap();
assert!(c.execute("INSERT INTO t VALUES(NULL,'x')").is_err());
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a NOT NULL ON CONFLICT IGNORE)")
.unwrap();
assert!(c.execute("INSERT OR ABORT INTO t VALUES(NULL)").is_err());
}