diff --git a/README.md b/README.md index c471d38..eed0db6 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ async fn main() -> SurrealismRes<()> { // dbg!(info_res); // create a table (you should define user table first!) let create_stmt = crate_user_table().build(); - let create_res = service.commit_sql(&create_stmt).await?; + let _ = service.commit_sql(&create_stmt).await?; // dbg!(create_res); // select user::surrealism table let select = SQLBuilderFactory::select().table("user").id("surrealism".into()).column("*", None).build(); @@ -483,41 +483,41 @@ full = ["row", "builder", "surreal"] | ------------ | ------------------------------------------------------------ | ------ | | && or AND | Checks whether both of two values are truthy | ✅ | | \|\| or OR | Checks whether either of two values is truthy | ✅ | -| ?? | Check whether either of two values are truthy and not `NULL` | ⛔ | -| ?: | Check whether either of two values are truthy | ⛔ | +| ?? | Check whether either of two values are truthy and not `NULL` | ✅ | +| ?: | Check whether either of two values are truthy | ✅ | | = or IS | Check whether two values are equal | ✅ | | != or IS NOT | Check whether two values are not equal | ✅ | | == | Check whether two values are exactly equal | ✅ | -| ?= | Check whether any value in a set is equal to a value | ⛔ | -| *= | Check whether all values in a set are equal to a value | ⛔ | -| ~ | Compare two values for equality using fuzzy matching | ⛔ | -| !~ | Compare two values for inequality using fuzzy matching | ⛔ | -| ?~ | Check whether any value in a set is equal to a value using fuzzy matching | ⛔ | -| *~ | Check whether all values in a set are equal to a value using fuzzy matching | ⛔ | +| ?= | Check whether any value in a set is equal to a value | ✅ | +| *= | Check whether all values in a set are equal to a value | ✅ | +| ~ | Compare two values for equality using fuzzy matching | ✅ | +| !~ | Compare two values for inequality using fuzzy matching | ✅ | +| ?~ | Check whether any value in a set is equal to a value using fuzzy matching | ✅ | +| *~ | Check whether all values in a set are equal to a value using fuzzy matching | ✅ | | < | Check whether a value is less than another value | ✅ | | <= | Check whether a value is less than or equal to another value | ✅ | | > | Check whether a value is greater than another value | ✅ | | >= | Check whether a value is greater than or equal to another value | ✅ | | + | Add two values together | ✅ | | - | Subtract a value from another value | ✅ | -| * or × | Multiply two values together | ⛔ | -| / or ÷ | Divide a value by another value | ⛔ | -| ** | Raises a base value by another value | ⛔ | -| IN | Checks whether a value is contained within another value | ⛔ | -| NOT IN | Checks whether a value is not contained within another value | ⛔ | +| * or × | Multiply two values together | ✅ | +| / or ÷ | Divide a value by another value | ✅ | +| ** | Raises a base value by another value | ✅ | +| IN | Checks whether a value is contained within another value | ✅ | +| NOT IN | Checks whether a value is not contained within another value | ✅ | | CONTAINS | Checks whether a value contains another value | ✅ | -| CONTAINSNOT | Checks whether a value does not contain another value | ⛔ | -| CONTAINSALL | Checks whether a value contains all other values | ⛔ | -| CONTAINSANY | Checks whether a value contains any other value | ⛔ | -| CONTAINSNONE | Checks whether a value contains none of the following values | ⛔ | -| INSIDE | Checks whether a value is contained within another value | ⛔ | -| NOTINSIDE | Checks whether a value is not contained within another value | ⛔ | -| ALLINSIDE | Checks whether all values are contained within other values | ⛔ | -| ANYINSIDE | Checks whether any value is contained within other values | ⛔ | -| NONEINSIDE | Checks whether no value is contained within other values | ⛔ | -| OUTSIDE | Checks whether a geometry type is outside of another geometry type | ⛔ | -| INTERSECTS | Checks whether a geometry type intersects another geometry type | ⛔ | -| @@ | Checks whether the terms are found in a full-text indexed field | ⛔ | +| CONTAINSNOT | Checks whether a value does not contain another value | ✅ | +| CONTAINSALL | Checks whether a value contains all other values | ✅ | +| CONTAINSANY | Checks whether a value contains any other value | ✅ | +| CONTAINSNONE | Checks whether a value contains none of the following values | ✅ | +| INSIDE | Checks whether a value is contained within another value | ✅ | +| NOTINSIDE | Checks whether a value is not contained within another value | ✅ | +| ALLINSIDE | Checks whether all values are contained within other values | ✅ | +| ANYINSIDE | Checks whether any value is contained within other values | ✅ | +| NONEINSIDE | Checks whether no value is contained within other values | ✅ | +| OUTSIDE | Checks whether a geometry type is outside of another geometry type | ✅ | +| INTERSECTS | Checks whether a geometry type intersects another geometry type | ✅ | +| @@ | Checks whether the terms are found in a full-text indexed field | ✅ | diff --git a/surrealism/src/core/builder/define/db.rs b/surrealism/src/core/builder/define/db.rs index 018bc5a..04136bf 100644 --- a/surrealism/src/core/builder/define/db.rs +++ b/surrealism/src/core/builder/define/db.rs @@ -7,7 +7,7 @@ //! ``` use std::fmt::{Display, Formatter}; -use crate::db::constants::{DEFINE_NS, STMT_END}; +use crate::db::constants::{DEFINE_DB, STMT_END}; #[derive(Debug, Clone)] pub struct DefineDB<'a> { @@ -39,6 +39,6 @@ impl<'a> DefineDB<'a> { impl<'a> Display for DefineDB<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{} {}{}", DEFINE_NS, self.name, STMT_END) + write!(f, "{} {}{}", DEFINE_DB, self.name, STMT_END) } } \ No newline at end of file diff --git a/surrealism/src/core/db/condition.rs b/surrealism/src/core/db/condition.rs index 7c3a693..9f19518 100644 --- a/surrealism/src/core/db/condition.rs +++ b/surrealism/src/core/db/condition.rs @@ -8,7 +8,7 @@ use std::fmt::{Display, Formatter}; use serde::{Serialize, Deserialize}; use crate::db::{SurrealValue, ParamCombine}; -use super::constants::{EQ, LT, GT, GTE, LTE, LINK, NEQ, WHERE, AND, OR, CONTAINS}; +use crate::db::constants::{ADD, ADD_OP, ALLEQ, BOTH, DIVIDE, EITHER, GT_EQ, HAS, INLIKE, IS, ISNOT, LIKE, LT_EQ, MATCHES, MINUS, NOTLIKE, PLUS, POW, EQ, LT, GT, GTE, LTE, LINK, NEQ, WHERE, AND, OR, CONTAINS, CONTAINSALL, CONTAINSNOT, CONTAINSANY, CONTAINSNONE, INSIDE, NOTINSIDE, ALLINSIDE, ANYINSIDE, NONEINSIDE, OUTSIDE, INTERSECTS, SELECT_ALL, STMT_END}; /// where condition for statment /// ## example @@ -226,27 +226,37 @@ impl Criteria { self } pub fn build(&self) -> String { + self.to_string() + } + /// consume self to SurrealValue + pub fn to_value(self) -> SurrealValue { + SurrealValue::from(self.build()) + } + /// build easy : select * from ... ; + pub fn build_easy(&self) -> String { + format!("{} {}{}", SELECT_ALL, &self.to_string(), STMT_END) + } +} + +impl Display for Criteria { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self.sign { CriteriaSign::Link => { match self.right { - SurrealValue::String(ref s) => format!("{} {} {} {}", self.sign.to_str(), &self.left, LINK, s), + SurrealValue::String(ref s) => write!(f, "{} {} {} {}", self.sign.to_str(), &self.left, LINK, s), _ => panic!("{}", "Link Multiple Tables need use SurrealValue::Str for right!") } } // for cheat CriteriaSign::Cheat(ref value) => { match self.right { - SurrealValue::String(ref s) => format!("{} {} {}", &self.left, value, s), + SurrealValue::String(ref s) => write!(f, "{} {} {}", &self.left, value, s), _ => panic!("{}", "This Panic may not exist , if you see this panic , please connect to author or commit issue on github!") } } - _ => format!("{} {} {}", &self.left, self.sign.to_str(), &self.right.to_string()) + _ => write!(f, "{} {} {}", &self.left, self.sign.to_str(), &self.right.to_string()) } } - /// consume self to SurrealValue - pub fn to_value(self) -> SurrealValue { - SurrealValue::from(self.build()) - } } impl ParamCombine for Criteria { @@ -257,24 +267,76 @@ impl ParamCombine for Criteria { /// # criteria sign -/// 作用于Criteria -/// - Eq:等于 -/// - Lt:小于 -/// - Gt:大于 -/// - Neq:不等于 -/// - Lte:小于等于 -/// - Gte:大于等于 -/// - Link:连接`->` +/// 作用于Criteria是SurrealDB的操作符 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum CriteriaSign { - Eq, + /// && + And, + /// || + Or, + /// ?? + Either, + /// ?: + Both, + /// = 会进行隐式转换 + Is, + /// != + IsNot, + /// ?= 检查数组中的任何值是否等于另一个值 + Has, + /// *= 检查数组中的所有值是否都等于另一个值 + AllEq, + /// ~ 使用模糊匹配比较两个值是否相等 + Like, + /// !~ 使用模糊匹配比较两个值的不等式 + NotLike, + /// ?~ 检查集合中的任何值是否等于使用模糊匹配的值 + InLike, + /// < Lt, + /// <= + Leq, + /// > Gt, - Neq, - Lte, - Gte, - Link, + /// >= + Geq, + Add, + Minus, + /// * 或 × + Plus, + /// / 或÷ + Divide, + /// == 检查两个值是否精确。该运算符还检查每个值是否具有相同的类型 + Eq, + /// 幂运算 ** + Pow, + /// CONTAINS 或 ∋ 检查一个值是否包含其他值 Contains, + /// CONTAINSNOT 或 ∌ 检查一个值是否不包含其他值 + ContainsNot, + /// CONTAINSALL 或 ⊇ 检查一个值是否包含所有多个值 + ContainsAll, + /// CONTAINSANY 或 ⊃ 检查一个值是否包含多个值中的任何一个 + ContainsAny, + /// CONTAINSNONE 或 ⊅ 检查一个值是否不包含任何多个值。 + ContainsNone, + /// INSIDE 或 ∈ 或 IN 检查一个值是否包含在另一个值中 + Inside, + /// NOTINSIDE 或 ∉ 或NOT IN 检查一个值是否不包含在另一个值中。 + NotInside, + /// ALLINSIDE 或 ⊆ 检查所有多个值是否包含在另一个值中 + AllInside, + /// ANYINSIDE 或 ⊂ 检查多个值中的任何一个是否包含在另一个值中 + AnyInside, + /// NONEINSIDE 或 ⊄ 检查多个值中是否没有一个包含在另一个值中 + NoneInside, + /// OUTSIDE 检查一个几何体值是否在另一个几何体值之外 + Outside, + /// INTERSECTS 检查一个几何值是否与另一个几何值相交 + Intersects, + /// MATCHES 检查是否在全文索引字段中找到术语 + Matches, + Link, Cheat(String), } @@ -297,17 +359,70 @@ impl Display for CriteriaSign { } impl CriteriaSign { + /// # quick build and select + /// + /// ## example + /// ```rust + /// use surrealism::builder::SQLBuilderFactory; + /// use surrealism::db::{Criteria, CriteriaSign}; + /// use surrealism::DefaultRes; + /// + /// // [tests\src\main.rs:13] operator1 = "SELECT * FROM 10 AND 20;" + /// // [tests\src\main.rs:14] operator2 = "SELECT * FROM 0 OR false;" + /// // [tests\src\main.rs:15] operator3 = "SELECT * FROM NULL ?? 0;" + /// // [tests\src\main.rs:16] operator4 = "SELECT * FROM true ?: 1;" + /// // [tests\src\main.rs:17] operator5 = "SELECT * FROM 'test text' ~ 'Test';" + /// #[tokio::main] + /// async fn main() -> DefaultRes<()> { + /// let operator1 = CriteriaSign::And.build(10.into(), 20.into()); + /// let operator2 = CriteriaSign::Or.build(0.into(), false.into()); + /// let operator3 = CriteriaSign::Either.build("NULL".into(), 0.into()); + /// let operator4 = CriteriaSign::Both.build(true.into(), 1.into()); + /// let operator5 = CriteriaSign::Like.build("test text".into(), "Test".into()); + /// Ok(()) + /// } + /// ``` + pub fn build(self, left: SurrealValue, right: SurrealValue) -> String { + format!("{} {} {} {}{}", SELECT_ALL, &left.to_string(), self.to_str(), &right.to_string(),STMT_END) + } pub fn to_str(&self) -> &str { match self { CriteriaSign::Eq => EQ, CriteriaSign::Lt => LT, CriteriaSign::Gt => GT, - CriteriaSign::Neq => NEQ, - CriteriaSign::Lte => LTE, - CriteriaSign::Gte => GTE, CriteriaSign::Link => LINK, CriteriaSign::Cheat(value) => value.as_str(), - CriteriaSign::Contains => CONTAINS + CriteriaSign::And => AND, + CriteriaSign::Or => OR, + CriteriaSign::Either => EITHER, + CriteriaSign::Both => BOTH, + CriteriaSign::Is => IS, + CriteriaSign::IsNot => ISNOT, + CriteriaSign::Has => HAS, + CriteriaSign::AllEq => ALLEQ, + CriteriaSign::Like => LIKE, + CriteriaSign::NotLike => NOTLIKE, + CriteriaSign::InLike => INLIKE, + CriteriaSign::Leq => LT_EQ, + CriteriaSign::Geq => GT_EQ, + CriteriaSign::Add => ADD, + CriteriaSign::Minus => MINUS, + CriteriaSign::Plus => PLUS, + CriteriaSign::Divide => DIVIDE, + CriteriaSign::Pow => POW, + CriteriaSign::Contains => CONTAINS, + CriteriaSign::ContainsNot => CONTAINSNOT, + CriteriaSign::ContainsAll => CONTAINSALL, + CriteriaSign::ContainsAny => CONTAINSANY, + CriteriaSign::ContainsNone => CONTAINSNONE, + CriteriaSign::Inside => INSIDE, + CriteriaSign::NotInside => NOTINSIDE, + CriteriaSign::AllInside => ALLINSIDE, + CriteriaSign::AnyInside => ANYINSIDE, + CriteriaSign::NoneInside => NONEINSIDE, + CriteriaSign::Outside => OUTSIDE, + CriteriaSign::Intersects => INTERSECTS, + CriteriaSign::Matches => MATCHES } } } @@ -315,14 +430,40 @@ impl CriteriaSign { impl From<&str> for CriteriaSign { fn from(value: &str) -> Self { match value { - EQ => CriteriaSign::Eq, + MATCHES => CriteriaSign::Matches, + INTERSECTS => CriteriaSign::Intersects, + OUTSIDE => CriteriaSign::Outside, + NONEINSIDE => CriteriaSign::NoneInside, + ANYINSIDE => CriteriaSign::AnyInside, + ALLINSIDE => CriteriaSign::AllInside, + NOTINSIDE => CriteriaSign::NotInside, + INSIDE => CriteriaSign::Inside, + CONTAINSNONE => CriteriaSign::ContainsNone, + CONTAINSANY => CriteriaSign::ContainsAny, + CONTAINSALL => CriteriaSign::ContainsAll, + CONTAINSNOT => CriteriaSign::ContainsNot, + CONTAINS => CriteriaSign::Contains, + POW => CriteriaSign::Pow, + DIVIDE => CriteriaSign::Divide, + PLUS => CriteriaSign::Plus, + MINUS => CriteriaSign::Minus, + ADD => CriteriaSign::Add, + GT_EQ => CriteriaSign::Geq, + LT_EQ => CriteriaSign::Leq, + INLIKE => CriteriaSign::InLike, + NOTLIKE => CriteriaSign::NotLike, + LIKE => CriteriaSign::Like, + ALLEQ => CriteriaSign::AllEq, + HAS => CriteriaSign::Has, + ISNOT => CriteriaSign::IsNot, + IS => CriteriaSign::Is, + BOTH => CriteriaSign::Both, + EITHER => CriteriaSign::Either, + OR => CriteriaSign::Or, + AND => CriteriaSign::And, LT => CriteriaSign::Lt, GT => CriteriaSign::Gt, - LTE => CriteriaSign::Lte, - GTE => CriteriaSign::Gte, - NEQ => CriteriaSign::Neq, LINK => CriteriaSign::Link, - CONTAINS => CriteriaSign::Contains, _ => CriteriaSign::Cheat(String::from(value)) } } diff --git a/surrealism/src/core/db/constants.rs b/surrealism/src/core/db/constants.rs index 8d3468c..f720faf 100644 --- a/surrealism/src/core/db/constants.rs +++ b/surrealism/src/core/db/constants.rs @@ -48,7 +48,10 @@ sql_const!( (REMOVE_USER,"REMOVE USER") (SHOW,"SHOW CHANGES FOR") (SINCE,"SINCE") (OWNER,"OWNER") (EDITOR,"EDITOR") (VIEWER,"VIEWER") (DEFINE_USER,"DEFINE USER") (ROLES,"ROLES") (SET_DOWN,"set") (WITH, "WITH") (WITH_NOINDEX,"WITH NOINDEX") (WITH_INDEX,"WITH INDEX") (EXPLAIN,"EXPLAIN") (YEAR,"YEAR") (MICROSECOND,"MICROSECOND") (NANOSECOND,"NANOSECOND") (DEFINE_ANALYZER,"DEFINE ANALYZER") (TOKENIZERS,"TOKENIZERS") (FILTERS,"FILTERS") - (HIGHLIGHTS,"HIGHLIGHTS") + (HIGHLIGHTS,"HIGHLIGHTS") (EITHER,"??") (BOTH,"?:") (IS ,"=") (ISNOT,"!=") (HAS,"?=") (ALLEQ,"*=") (LIKE,"~") (NOTLIKE,"!~") (INLIKE,"?~") + (POW,"**") (CONTAINSNOT,"CONTAINSNOT") (CONTAINSALL,"CONTAINSALL") (CONTAINSANY,"CONTAINSANY") (CONTAINSNONE,"CONTAINSNONE") + (INSIDE,"INSIDE") (NOTINSIDE,"NOTINSIDE") (ALLINSIDE,"ALLINSIDE") (ANYINSIDE,"ANYINSIDE") (NONEINSIDE,"NONEINSIDE") (OUTSIDE,"OUTSIDE") + (INTERSECTS,"INTERSECTS") (MATCHES,"@@") (ADD,"+") (MINUS,"-") (PLUS,"*") (DIVIDE,"/") (SELECT_ALL,"SELECT * FROM") ); pub const FALSE: bool = false; diff --git a/surrealism/src/core/db/sql.rs b/surrealism/src/core/db/sql.rs index 330b58a..984ef75 100644 --- a/surrealism/src/core/db/sql.rs +++ b/surrealism/src/core/db/sql.rs @@ -339,6 +339,7 @@ pub enum Operator { Plus, Divide, Eq, + } impl Operator { diff --git a/surrealism/src/core/surreal/connector.rs b/surrealism/src/core/surreal/connector.rs index 400148b..cdd69ce 100644 --- a/surrealism/src/core/surreal/connector.rs +++ b/surrealism/src/core/surreal/connector.rs @@ -21,6 +21,8 @@ use crate::surreal::SurrealismConfig; #[async_trait] pub trait UseNSDB { async fn use_commit(&self, ns: &str, db: &str) -> Result<(), Error>; + async fn use_ns(&self,ns:&str)->Result<(),Error>; + async fn use_db(&self,db:&str)->Result<(),Error>; } #[async_trait] @@ -38,6 +40,12 @@ impl UseNSDB for SurrealismConnector { async fn use_commit(&self, ns: &str, db: &str) -> Result<(), Error> { self.client.use_ns(ns).use_db(db).await } + async fn use_ns(&self,ns:&str)->Result<(),Error>{ + self.client.use_ns(ns).await + } + async fn use_db(&self,db:&str)->Result<(),Error>{ + self.client.use_db(db).await + } } #[async_trait] diff --git a/tests/Surrealism.toml b/tests/Surrealism.toml index 04f38ec..421272c 100644 --- a/tests/Surrealism.toml +++ b/tests/Surrealism.toml @@ -1,6 +1,6 @@ [default] username = "root" -password = "syf20020816" +password = "root" bind = "127.0.0.1:10086" mode = "Memory" log = "Info" diff --git a/tests/src/examples/easy_commit.rs b/tests/src/examples/overdue/easy_commit.rs similarity index 100% rename from tests/src/examples/easy_commit.rs rename to tests/src/examples/overdue/easy_commit.rs diff --git a/tests/src/examples/init.rs b/tests/src/examples/overdue/init.rs similarity index 100% rename from tests/src/examples/init.rs rename to tests/src/examples/overdue/init.rs diff --git a/tests/src/examples/quickstart.rs b/tests/src/examples/overdue/quickstart.rs similarity index 100% rename from tests/src/examples/quickstart.rs rename to tests/src/examples/overdue/quickstart.rs diff --git a/tests/src/examples/use_ns_db.rs b/tests/src/examples/overdue/use_ns_db.rs similarity index 100% rename from tests/src/examples/use_ns_db.rs rename to tests/src/examples/overdue/use_ns_db.rs diff --git a/tests/src/examples/v0.3+/other/criteria_sign_operator.rs b/tests/src/examples/v0.3+/other/criteria_sign_operator.rs new file mode 100644 index 0000000..57c4e21 --- /dev/null +++ b/tests/src/examples/v0.3+/other/criteria_sign_operator.rs @@ -0,0 +1,24 @@ +use surrealism::builder::SQLBuilderFactory; +use surrealism::db::{Criteria, CriteriaSign}; +use surrealism::DefaultRes; + +// [tests\src\main.rs:13] operator1 = "SELECT * FROM 10 AND 20;" +// [tests\src\main.rs:14] operator2 = "SELECT * FROM 0 OR false;" +// [tests\src\main.rs:15] operator3 = "SELECT * FROM NULL ?? 0;" +// [tests\src\main.rs:16] operator4 = "SELECT * FROM true ?: 1;" +// [tests\src\main.rs:17] operator5 = "SELECT * FROM 'test text' ~ 'Test';" +#[tokio::main] +async fn main() -> DefaultRes<()> { + let operator1 = CriteriaSign::And.build(10.into(), 20.into()); + let operator2 = CriteriaSign::Or.build(0.into(), false.into()); + let operator3 = CriteriaSign::Either.build("NULL".into(), 0.into()); + let operator4 = CriteriaSign::Both.build(true.into(), 1.into()); + let operator5 = CriteriaSign::Like.build("test text".into(), "Test".into()); + + dbg!(operator1); + dbg!(operator2); + dbg!(operator3); + dbg!(operator4); + dbg!(operator5); + Ok(()) +} \ No newline at end of file diff --git a/tests/src/examples/v0.3+/surreal/quickstart.rs b/tests/src/examples/v0.3+/surreal/quickstart.rs new file mode 100644 index 0000000..cf25cf8 --- /dev/null +++ b/tests/src/examples/v0.3+/surreal/quickstart.rs @@ -0,0 +1,65 @@ +use surrealism::db::{SurrealID, Table}; +use surrealism::builder::{BaseWrapperImpl, SQLBuilderFactory, TableImpl}; +use surrealism::builder::create::{CreateWrapper, CreateWrapperImpl}; +use serde::{Serialize, Deserialize}; +use surrealism::builder::select::SelectWrapperImpl; +use surrealism::surreal::{parse_response, SurrealismRes,DefaultInitService,UseNSDB,InitService,SurrealismCommit}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct User { + username: String, + pwd: String, + male: bool, + age: u8, +} + +/// create a new user table +/// table_name:user +/// table_id:surrealism +pub fn crate_user_table() -> CreateWrapper { + // create a user data + let user = User { + username: "Tobie".to_string(), + pwd: "Tobie001".to_string(), + male: true, + age: 23, + }; + // create table with content + let user_table = SQLBuilderFactory::create() + .table("user") + .id("surrealism".into()) + .content(&user) + .deref_mut(); + user_table +} + +//strict! +#[tokio::main] +async fn main() -> SurrealismRes<()> { + // init service + let mut service = DefaultInitService::new().init(); + // you have already define test namespace and test database! + // use ns:test and db:test + let _ = service.use_commit("test", "test").await?; + // get info from surrealdb + // let info = SQLBuilderFactory::info().db().build(); + // let info_res = service.commit_sql(&info).await?; + // dbg!(info_res); + // create a table (you should define user table first!) + let create_stmt = crate_user_table().build(); + let _ = service.commit_sql(&create_stmt).await?; + // dbg!(create_res); + // select user::surrealism table + let select = SQLBuilderFactory::select().table("user").id("surrealism".into()).column("*", None).build(); + let select_res = service.commit_sql(&select).await?; + //parse response to any type you want + let res: User = parse_response(select_res); + // [tests\src\main.rs:55] res = User { + // username: "Tobie", + // pwd: "Tobie001", + // male: true, + // age: 23, + // } + dbg!(&res); + Ok(()) +} \ No newline at end of file diff --git a/tests/src/examples/v0.3+/surreal/quickstart_strict.rs b/tests/src/examples/v0.3+/surreal/quickstart_strict.rs new file mode 100644 index 0000000..a57e6e3 --- /dev/null +++ b/tests/src/examples/v0.3+/surreal/quickstart_strict.rs @@ -0,0 +1,74 @@ +use surrealism::db::{SurrealID, Table}; +use surrealism::builder::{BaseWrapperImpl, SQLBuilderFactory, TableImpl}; +use surrealism::builder::create::{CreateWrapper, CreateWrapperImpl}; +use serde::{Serialize, Deserialize}; +use surrealism::builder::select::SelectWrapperImpl; +use surrealism::surreal::{parse_response, SurrealismRes,DefaultInitService,UseNSDB,InitService,SurrealismCommit}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct User { + username: String, + pwd: String, + male: bool, + age: u8, +} + +/// create a new user table +/// table_name:user +/// table_id:surrealism +pub fn crate_user_table() -> CreateWrapper { + // create a user data + let user = User { + username: "Tobie".to_string(), + pwd: "Tobie001".to_string(), + male: true, + age: 23, + }; + // create table with content + let user_table = SQLBuilderFactory::create() + .table("user") + .id("surrealism".into()) + .content(&user) + .deref_mut(); + user_table +} + +//strict! +#[tokio::main] +async fn main() -> SurrealismRes<()> { + // init service + let mut service = DefaultInitService::new().init(); + // you can use this way , and you can define and use ns | db | table with define in sql + let define_ns = SQLBuilderFactory::define().ns().name("test").build(); + let define_db = SQLBuilderFactory::define().db().name("test").build(); + service.commit_sql(&define_ns).await?; + service.use_ns("test").await?; + service.commit_sql(&define_db).await?; + // you have already define test namespace and test database! + // use ns:test and db:test + // let _ = service.use_commit("test", "test").await?; + service.use_db("test").await?; + let define_table = SQLBuilderFactory::define().table().name("user").build(); + service.commit_sql(&define_table).await?; + // get info from surrealdb + // let info = SQLBuilderFactory::info().db().build(); + // let info_res = service.commit_sql(&info).await?; + // dbg!(info_res); + // create a table (you should define user table first!) + let create_stmt = crate_user_table().build(); + let _ = service.commit_sql(&create_stmt).await?; + // dbg!(create_res); + // select user::surrealism table + let select = SQLBuilderFactory::select().table("user").id("surrealism".into()).column("*", None).build(); + let select_res = service.commit_sql(&select).await?; + //parse response to any type you want + let res: User = parse_response(select_res); + // [tests\src\main.rs:55] res = User { + // username: "Tobie", + // pwd: "Tobie001", + // male: true, + // age: 23, + // } + dbg!(&res); + Ok(()) +} \ No newline at end of file diff --git a/tests/src/main.rs b/tests/src/main.rs index 8ffdd1d..cf25cf8 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -1,9 +1,65 @@ -use surrealism::builder::SQLBuilderFactory; -use surrealism::DefaultRes; +use surrealism::db::{SurrealID, Table}; +use surrealism::builder::{BaseWrapperImpl, SQLBuilderFactory, TableImpl}; +use surrealism::builder::create::{CreateWrapper, CreateWrapperImpl}; +use serde::{Serialize, Deserialize}; +use surrealism::builder::select::SelectWrapperImpl; +use surrealism::surreal::{parse_response, SurrealismRes,DefaultInitService,UseNSDB,InitService,SurrealismCommit}; +#[derive(Debug, Clone, Serialize, Deserialize)] +struct User { + username: String, + pwd: String, + male: bool, + age: u8, +} + +/// create a new user table +/// table_name:user +/// table_id:surrealism +pub fn crate_user_table() -> CreateWrapper { + // create a user data + let user = User { + username: "Tobie".to_string(), + pwd: "Tobie001".to_string(), + male: true, + age: 23, + }; + // create table with content + let user_table = SQLBuilderFactory::create() + .table("user") + .id("surrealism".into()) + .content(&user) + .deref_mut(); + user_table +} + +//strict! #[tokio::main] -async fn main() -> DefaultRes<()> { - let kill = SQLBuilderFactory::kill("734732kjsjd37"); - dbg!(kill); +async fn main() -> SurrealismRes<()> { + // init service + let mut service = DefaultInitService::new().init(); + // you have already define test namespace and test database! + // use ns:test and db:test + let _ = service.use_commit("test", "test").await?; + // get info from surrealdb + // let info = SQLBuilderFactory::info().db().build(); + // let info_res = service.commit_sql(&info).await?; + // dbg!(info_res); + // create a table (you should define user table first!) + let create_stmt = crate_user_table().build(); + let _ = service.commit_sql(&create_stmt).await?; + // dbg!(create_res); + // select user::surrealism table + let select = SQLBuilderFactory::select().table("user").id("surrealism".into()).column("*", None).build(); + let select_res = service.commit_sql(&select).await?; + //parse response to any type you want + let res: User = parse_response(select_res); + // [tests\src\main.rs:55] res = User { + // username: "Tobie", + // pwd: "Tobie001", + // male: true, + // age: 23, + // } + dbg!(&res); Ok(()) } \ No newline at end of file