Skip to content

Commit

Permalink
began moving query to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-ant committed Sep 29, 2024
1 parent 24be7f2 commit db09962
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rust/psibase/src/services/r_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
mod service {
use crate::HttpRequest;

#[action]
fn sqlQuery(query: String) -> String {
unimplemented!()
}

#[action]
fn serveSys(request: HttpRequest) -> Option<crate::http::HttpReply> {
unimplemented!()
Expand Down
51 changes: 51 additions & 0 deletions services/user/Chainmail/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@
mod bindings;

use bindings::exports::chainmail::plugin::api::{Error, Guest as API};
use bindings::exports::chainmail::plugin::queries::{Guest as QUERY, Message};
use bindings::transact::plugin::intf as Transact;
use psibase::fracpack::Pack;
use psibase::services::r_events::Wrapper as REventsSvc;
use psibase::AccountNumber;

struct ChainmailPlugin;

fn build_query(
archived_requested: bool,
sender: Option<String>,
receiver: Option<String>,
) -> String {
let where_clause_sender_receiver = String::from("");

let select_clause = format!("DISTINCT sent.rowid as msg_id, archive.event_id, sent.*");
let from_clause = format!("\"history.chainmail.sent\" AS sent LEFT JOIN \"history.chainmail.archive\" AS archive ON CONCAT(sent.receiver, sent.rowid) = archive.event_id" );
let where_clause_archived_or_not = format!(
"archive.event_id IS {} NULL",
if archived_requested { "NOT" } else { "" }
);
let order_by_clause = "sent.ROWID";

let sql_query_str = format!(
"SELECT {} FROM {} WHERE {} {} {} ORDER BY {}",
select_clause,
from_clause,
where_clause_archived_or_not,
if sender.is_some() || receiver.is_some() {
"AND"
} else {
""
},
where_clause_sender_receiver,
order_by_clause
);
let resp = REventsSvc::call().sqlQuery(sql_query_str);
println!("response: {}", resp);
resp
}

impl API for ChainmailPlugin {
fn send(receiver: String, subject: String, body: String) -> Result<(), Error> {
Transact::add_action_to_transaction(
Expand All @@ -31,4 +66,20 @@ impl API for ChainmailPlugin {
}
}

impl QUERY for ChainmailPlugin {
fn get_msgs(sender: Option<String>, receiver: Option<String>) -> Result<Vec<Message>, Error> {
let archived_requested = false;
let _resp = build_query(archived_requested, sender, receiver);

Ok(vec![])
}

fn get_archived_msgs(
_sender: Option<String>,
_receiver: Option<String>,
) -> Result<Vec<Message>, Error> {
Ok(vec![])
}
}

bindings::export!(ChainmailPlugin with_types_in bindings);
1 change: 1 addition & 0 deletions services/user/Chainmail/plugin/wit/impl.wit
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ world impl {
include host:common/imports;
include transact:plugin/imports;
export api;
export queries;
}
19 changes: 19 additions & 0 deletions services/user/Chainmail/plugin/wit/world.wit
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package chainmail:plugin;

interface types {
record message {
msg-id: u64,
receiver: string,
sender: string,
subject: string,
body: string
}
}

interface queries {
use host:common/types.{error};
use types.{message};

get-msgs: func(sender: option<string>, receiver: option<string>) -> result<list<message>, error>;
get-archived-msgs: func(sender: option<string>, receiver: option<string>) -> result<list<message>, error>;
}

interface api {
use host:common/types.{error};

Expand All @@ -12,4 +30,5 @@ interface api {

world imports {
import api;
import queries;
}

0 comments on commit db09962

Please sign in to comment.