Skip to content

Commit

Permalink
Feature/detached (#18)
Browse files Browse the repository at this point in the history
* edgedb-query-derive:
 - add link_property argument to query result #[field] attribute

* edgedb-query-derive:
 - add link_property argument to query result #[field] attribute

* edgedb-query-derive:
 - add link_property argument to query result #[field] attribute

* edgedb-query:
 - add table_name() to ToEdgeQl trait

* edgedb-query:
 - change return type of ToEdgeQl trait's to_edgdeql() function  from String to edgedb-query::EdgeQl

* edgedb-query:
 - add edgedb_query::queries::set::Sets trait

* edgedb-query-derive:
 - add detached statement

* example:
  • Loading branch information
imagineDevit committed Apr 8, 2023
1 parent f1c2606 commit ece7af9
Show file tree
Hide file tree
Showing 38 changed files with 617 additions and 235 deletions.
2 changes: 1 addition & 1 deletion edgedb-query-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "edgedb-query-derive"
version = "0.2.1"
version = "0.2.2"
description = "Crate that provide a bunch of attribute macros that help to build EdgeDB query using edgedb-tokio crate"
authors = ["hsedjame <sedhjodev@gmail.com>"]
repository = "https://github.com/imagineDevit/edgedb/tree/main/edgedb-query-derive"
Expand Down
37 changes: 26 additions & 11 deletions edgedb-query-derive/src/builders/impl_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

use crate::queries::QueryField;
use syn::Ident;
use quote::quote;
use proc_macro2::TokenStream;
use edgedb_query::QueryType;
use crate::utils::derive_utils::{conflict_element_shape, conflict_element_value, nested_element_shape, nested_element_value};

#[derive(Clone, PartialEq)]
Expand All @@ -20,8 +22,11 @@ pub struct ImplBuilderField {

pub struct QueryImplBuilder {
pub struct_name: Ident,
pub query_type: QueryType,
pub table_name: Option<String>,
pub fields: Vec<ImplBuilderField>,
pub init_edgeql: String,
pub has_result: bool,
//pub init_edgeql: String,
pub static_const_check_statements: Vec<TokenStream>,
pub edgeql_statements: Vec<TokenStream>,
}
Expand All @@ -45,23 +50,34 @@ impl QueryImplBuilder {
pub fn build_to_edgeql_impl(&self) -> TokenStream {
let struct_name = self.struct_name.clone();

let query_str = self.init_edgeql.clone();
let query_str = String::default();

let table_name = self.table_name.clone().unwrap_or(String::new());

let q_ty = format!("{}", self.query_type);

let h_r = self.has_result;

let stmts: Vec<TokenStream> = self.edgeql_statements.clone();

quote! {
impl edgedb_query::ToEdgeQl for #struct_name {
fn to_edgeql(&self) -> String {
use edgedb_query::ToEdgeScalar;
use edgedb_query::queries::filter::Filter;
use edgedb_query::EdgeResult;
fn to_edgeql(&self) -> edgedb_query::EdgeQl {
use edgedb_query::ToEdgeScalar;
use edgedb_query::queries::filter::Filter;
use edgedb_query::EdgeResult;

let mut query = #query_str.to_owned();
let mut query = #query_str.to_owned();

#(#stmts)*
#(#stmts)*

query
edgedb_query::EdgeQl {
query_type: edgedb_query::QueryType::from(#q_ty.to_string()),
table_name: #table_name.to_string(),
content: query,
has_result: #h_r
}
}
}
}
}
Expand Down Expand Up @@ -160,8 +176,7 @@ impl QueryImplBuilder {
impl ToString for #struct_name {
fn to_string(&self) -> String {
use edgedb_query::ToEdgeQl;

self.to_edgeql()
self.to_edgeql().to_string()
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions edgedb-query-derive/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub const SCALAR_TYPE: &str = "$scalar_type$";
pub const EDGEQL: &str = "$edgeql$";
pub const INF_SIGN: &str = "<";
pub const SUP_SIGN: &str = ">";
pub const AT: &str = "@";
pub const ID: &str= "id";
// endregion other

Expand All @@ -14,10 +15,8 @@ pub const VEC: &str = "Vec";
// endregion

// region query types
pub const INSERT: &str = "insert";
pub const SELECT: &str = "select";
pub const DELETE: &str = "delete";
pub const UPDATE: &str = "update";

// endregion query types

// region metadata
Expand All @@ -26,6 +25,7 @@ pub const MODULE: &str = "module";
pub const TABLE: &str = "table";
pub const RESULT: &str = "result";
pub const SRC: &str = "src";
pub const ON: &str = "on";
// endregion metadata

// region tags
Expand All @@ -51,6 +51,7 @@ pub const WRAPPER_FN: &str = "wrapper_fn";
pub const COLUMN_NAME: &str = "column_name";
pub const DEFAULT_VALUE: &str = "default_value";
pub const SET_OPTION: &str = "option";
pub const LINK_PROPERTY: &str = "link_property";

pub const NESTED_QUERY: &str = "nested_query";

Expand Down Expand Up @@ -94,6 +95,8 @@ pub const CONCAT: &str = "concat";
pub const CONCAT_SIGN: &str = "++";
pub const PUSH: &str = "push";
pub const PUSH_SIGN: &str = "+=";
pub const REMOVE: &str = "remove";
pub const REMOVE_SIGN: &str = "-=";
// endregion setOption

// region patterns
Expand Down Expand Up @@ -121,7 +124,12 @@ pub const INVALID_INSERT_TAG: &str = r#"
"#;
pub const INVALID_FIELD_TAG: &str = r#"
Invalid field tag option.
Expected "column_name" , "param" or "scalar"
Expected "column_name" , "param", "scalar" or "link_property"
"#;

pub const INVALID_UNLESS_CONFLICT_TAG: &str = r#"
Invalid unless conflict tag option.
Expected "on"
"#;

pub const INVALID_BACKLINK_TAG: &str = r#"
Expand Down Expand Up @@ -156,7 +164,7 @@ pub const INVALID_SET_TAG_OPTION: &str = r#"

pub const INVALID_RESULT_FIELD_TAG: &str = r#"
Invalid result's field tag.
Expected "field", "back_link" or nothing
Expected "field", "back_link" or "link_property"
"#;

pub const INVALID_ENUM_VARIANT_TAG: &str = r#"
Expand All @@ -181,10 +189,12 @@ pub const INVALID_SETS_TAG: &str = r#"
"#;

pub const PUSH_OPTION_ONLY_FOR_VEC: &str = "Push option only accepts a Vec type";
pub const REMOVE_OPTION_ONLY_FOR_VEC: &str = "Remove option only accepts a Vec type";

pub const EXPECT_LIT_OR_NAMED_LIT: &str = "Expected a literal or a named string literal";
pub const EXPECT_NAMED_LIT: &str = "Expected a named string literal";
pub const EXPECT_LIT_STR: &str = "Expected a string literal";
pub const EXPECT_LIT_BOOL: &str = "Expected a boolean literal";

pub const INVALID_TYPE_TUPLE_FOR_OPERATOR: &str = "Invalid type () for operator";

Expand Down
5 changes: 4 additions & 1 deletion edgedb-query-derive/src/delete_query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use syn::{Ident, ItemStruct};
use syn::parse::{Parse, ParseStream};
use edgedb_query::QueryType;

use crate::constants::*;
use crate::{queries::Query, meta_data::{TableInfo, try_get_meta}};
Expand Down Expand Up @@ -46,10 +47,12 @@ impl Query for DeleteQuery {

Ok(QueryImplBuilder {
struct_name: self.ident.clone(),
table_name: Some(table_name.clone()),
fields,
init_edgeql: format!("{DELETE} {table_name}"),
query_type: QueryType::Delete,
static_const_check_statements: vec![],
edgeql_statements,
has_result: false
})
}
}
Expand Down
15 changes: 11 additions & 4 deletions edgedb-query-derive/src/edgedb_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,16 @@ impl EdgedbEnum {
}

impl edgedb_query::ToEdgeQl for #enum_name {
fn to_edgeql(&self) -> String {
match self {
fn to_edgeql(&self) -> edgedb_query::EdgeQl {
let s = match self {
#(#v_idents,)*
};

edgedb_query::EdgeQl {
query_type: edgedb_query::QueryType::None,
table_name: String::new(),
content: s,
has_result: false
}
}
}
Expand All @@ -64,14 +71,14 @@ impl EdgedbEnum {
impl edgedb_query::ToEdgeValue for #enum_name {
fn to_edge_value(&self) -> edgedb_protocol::value::Value {
use edgedb_query::ToEdgeQl;
edgedb_protocol::value::Value::Enum(edgedb_protocol::codec::EnumValue::from(self.to_edgeql().as_str()))
edgedb_protocol::value::Value::Enum(edgedb_protocol::codec::EnumValue::from(self.to_edgeql().to_string().as_str()))
}
}

impl ToString for #enum_name {
fn to_string(&self) -> String {
use edgedb_query::ToEdgeQl;
self.to_edgeql()
self.to_edgeql().to_string()
}
}

Expand Down
54 changes: 42 additions & 12 deletions edgedb-query-derive/src/edgedb_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quote::quote;
use syn::ItemStruct;
use syn::parse::{Parse, ParseStream};
use crate::constants::INVALID_SETS_TAG;
use crate::statements::set::{sets_from_fields, UpdateSetStatement};
use crate::statements::set::{sets_from_fields, SetStatement, UpdateSetStatement};

pub struct EdgedbSets {
pub ident: Ident,
Expand All @@ -13,42 +13,74 @@ pub struct EdgedbSets {

impl EdgedbSets {
fn new(ident: Ident) -> Self {
Self{
ident,
set_statement: UpdateSetStatement::None
Self {
ident,
set_statement: UpdateSetStatement::None,
}
}


pub fn to_token_stream(&self) -> syn::Result<TokenStream> {

let struct_name = self.ident.clone();

let fields_quote = self.set_statement.struct_field_quote();

let add_set = self.set_statement.add_set_statement_quote();
let add_set = self.set_statement.add_set_statement_quote(None);

let shapes = self.set_statement.shape_quote();

let values = self.set_statement.value_quote();

let tokens = quote!{
let s = if let UpdateSetStatement::ManySet(sets) = self.set_statement.clone() {
let ss =
sets
.iter()
.map(|s| {
if let SetStatement::NestedQuery(f) = s {
let f_name = f.field.ident.clone();
quote! {
let t = self.#f_name.to_edgeql();
tbs.push(t);
}
} else {
quote!()
}
});

quote! {
#(#ss)*
}
} else {
quote!()
};

let tokens = quote! {

#[derive(Debug, Clone)]
pub struct #struct_name {
#fields_quote
}

impl edgedb_query::ToEdgeQl for #struct_name {
impl edgedb_query::queries::set::Sets for #struct_name {

fn to_edgeql(&self) -> String {
use edgedb_query::{ToEdgeQl, EdgeQl};
#add_set

set_stmt
}
}

impl edgedb_query::ToEdgeValue for #struct_name {
fn nested_edgeqls(&self) -> Vec<edgedb_query::EdgeQl> {
use edgedb_query::{ToEdgeQl, EdgeQl};

let mut tbs = vec![];
#s
tbs
}

fn to_edge_value(&self) -> edgedb_protocol::value::Value {
use edgedb_query::{ToEdgeValue};

let mut fields: Vec<Option<edgedb_protocol::value::Value>> = vec![];
let mut shapes: Vec<edgedb_protocol::descriptors::ShapeElement> = vec![];
let mut element_names: Vec<String> = vec![];
Expand All @@ -62,8 +94,6 @@ impl EdgedbSets {
}
}
}


};

Ok(tokens.into())
Expand Down
14 changes: 12 additions & 2 deletions edgedb-query-derive/src/file_query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::convert::TryFrom;
use quote::quote;
use regex::Regex;
use syn::{Field, Ident, ItemStruct};
use syn::parse::{Parse, ParseStream};
use edgedb_query::QueryType;
use crate::constants::{PARAM, PARAM_PATTERN};
use crate::builders::impl_builder::{FieldCat, QueryImplBuilder, ImplBuilderField};
use crate::meta_data::{SrcFile, try_get_meta};
Expand Down Expand Up @@ -106,12 +108,20 @@ impl Query for FileQuery {
})
.collect::<Vec<ImplBuilderField>>();

let query = meta.get_content(&self.ident)?;

Ok(QueryImplBuilder {
struct_name: self.ident.clone(),
table_name: None,
fields,
init_edgeql: meta.get_content(&self.ident)?,
query_type: QueryType::None,
static_const_check_statements: vec![],
edgeql_statements: vec![],
edgeql_statements: vec![
quote! {
query.push_str(#query);
}
],
has_result: false
})
}
}
Expand Down
Loading

0 comments on commit ece7af9

Please sign in to comment.