2023-02-19 15:45:50 +00:00
|
|
|
mod parse;
|
|
|
|
use parse::Parse;
|
2023-08-30 18:01:30 +00:00
|
|
|
mod helpers;
|
|
|
|
use helpers::new_ds;
|
2023-02-19 15:45:50 +00:00
|
|
|
use surrealdb::dbs::Session;
|
|
|
|
use surrealdb::err::Error;
|
|
|
|
use surrealdb::sql::Value;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn subquery_select() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
-- Create a record
|
|
|
|
CREATE person:test SET name = 'Tobie', age = 21;
|
|
|
|
-- Select all records, returning an array
|
|
|
|
SELECT age >= 18 as adult FROM person;
|
|
|
|
-- Select a specific record, still returning an array
|
|
|
|
SELECT age >= 18 as adult FROM person:test;
|
|
|
|
-- Select all records in a subquery, returning an array
|
|
|
|
RETURN (SELECT age >= 18 AS adult FROM person);
|
|
|
|
-- Select a specific record in a subquery, returning an object
|
|
|
|
RETURN (SELECT age >= 18 AS adult FROM person:test);
|
|
|
|
-- Using an outer SELECT, select all records in a subquery, returning an array
|
|
|
|
SELECT * FROM (SELECT age >= 18 AS adult FROM person) WHERE adult = true;
|
|
|
|
-- Using an outer SELECT, select a specific record in a subquery, returning an array
|
|
|
|
SELECT * FROM (SELECT age >= 18 AS adult FROM person:test) WHERE adult = true;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-02-19 15:45:50 +00:00
|
|
|
assert_eq!(res.len(), 7);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
age: 21,
|
|
|
|
id: person:test,
|
|
|
|
name: 'Tobie'
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
2023-02-19 15:45:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
|
|
|
"[
|
|
|
|
{
|
|
|
|
adult: true
|
|
|
|
}
|
|
|
|
]",
|
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-03-25 23:17:33 +00:00
|
|
|
async fn subquery_ifelse_set() -> Result<(), Error> {
|
|
|
|
let sql = "
|
|
|
|
-- Check if the record exists
|
|
|
|
LET $record = (SELECT *, count() AS count FROM person:test);
|
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
(UPSERT person:test SET sport +?= 'football' RETURN sport)
|
2023-03-25 23:17:33 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
(UPSERT person:test SET sport = ['basketball'] RETURN sport)
|
2023-03-25 23:17:33 +00:00
|
|
|
END;
|
|
|
|
-- Check if the record exists
|
|
|
|
LET $record = SELECT *, count() AS count FROM person:test;
|
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport +?= 'football' RETURN sport
|
2023-03-25 23:17:33 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport = ['basketball'] RETURN sport
|
2023-03-25 23:17:33 +00:00
|
|
|
END;
|
|
|
|
-- Check if the record exists
|
|
|
|
LET $record = SELECT *, count() AS count FROM person:test;
|
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport +?= 'football' RETURN sport;
|
2023-03-25 23:17:33 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport = ['basketball'] RETURN sport;
|
2023-03-25 23:17:33 +00:00
|
|
|
END;
|
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-03-25 23:17:33 +00:00
|
|
|
assert_eq!(res.len(), 9);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
2023-09-05 15:22:13 +00:00
|
|
|
let val = Value::parse("[]");
|
2023-03-25 23:17:33 +00:00
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-03-25 23:17:33 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
count: 1,
|
|
|
|
id: person:test,
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-03-25 23:17:33 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-03-25 23:17:33 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
count: 1,
|
|
|
|
id: person:test,
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-03-25 23:17:33 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-03-25 23:17:33 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn subquery_ifelse_array() -> Result<(), Error> {
|
2023-02-19 15:45:50 +00:00
|
|
|
let sql = "
|
|
|
|
-- Check if the record exists
|
|
|
|
LET $record = (SELECT *, count() AS count FROM person:test);
|
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
(UPSERT person:test SET sport += 'football' RETURN sport)
|
2023-02-19 15:45:50 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
(UPSERT person:test SET sport = ['basketball'] RETURN sport)
|
2023-02-19 15:45:50 +00:00
|
|
|
END;
|
|
|
|
-- Check if the record exists
|
2023-02-20 14:24:37 +00:00
|
|
|
LET $record = SELECT *, count() AS count FROM person:test;
|
2023-02-19 15:45:50 +00:00
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport += 'football' RETURN sport
|
2023-02-19 15:45:50 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport = ['basketball'] RETURN sport
|
2023-02-19 15:45:50 +00:00
|
|
|
END;
|
2023-02-20 20:40:05 +00:00
|
|
|
-- Check if the record exists
|
|
|
|
LET $record = SELECT *, count() AS count FROM person:test;
|
|
|
|
-- Return the specified record
|
|
|
|
RETURN $record;
|
|
|
|
-- Update the record field if it exists
|
|
|
|
IF $record.count THEN
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport += 'football' RETURN sport;
|
2023-02-20 20:40:05 +00:00
|
|
|
ELSE
|
2024-06-12 09:15:09 +00:00
|
|
|
UPSERT person:test SET sport = ['basketball'] RETURN sport;
|
2023-02-20 20:40:05 +00:00
|
|
|
END;
|
2023-02-19 15:45:50 +00:00
|
|
|
";
|
2023-08-30 18:01:30 +00:00
|
|
|
let dbs = new_ds().await?;
|
2023-07-29 18:47:25 +00:00
|
|
|
let ses = Session::owner().with_ns("test").with_db("test");
|
2023-07-05 21:26:13 +00:00
|
|
|
let res = &mut dbs.execute(sql, &ses, None).await?;
|
2023-02-20 20:40:05 +00:00
|
|
|
assert_eq!(res.len(), 9);
|
2023-02-19 15:45:50 +00:00
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
2023-09-05 15:22:13 +00:00
|
|
|
let val = Value::parse("[]");
|
2023-02-19 15:45:50 +00:00
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-02-19 15:45:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
count: 1,
|
|
|
|
id: person:test,
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-02-19 15:45:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-02-19 15:45:50 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2023-02-20 20:40:05 +00:00
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::None;
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
count: 1,
|
|
|
|
id: person:test,
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-02-20 20:40:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
|
|
|
let tmp = res.remove(0).result?;
|
|
|
|
let val = Value::parse(
|
2023-09-05 15:22:13 +00:00
|
|
|
"[
|
|
|
|
{
|
|
|
|
sport: [
|
|
|
|
'basketball',
|
|
|
|
'football',
|
|
|
|
'football',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]",
|
2023-02-20 20:40:05 +00:00
|
|
|
);
|
|
|
|
assert_eq!(tmp, val);
|
|
|
|
//
|
2023-02-19 15:45:50 +00:00
|
|
|
Ok(())
|
|
|
|
}
|