New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating a dsl ordering var from String parameters #1322

Closed
greenpdx opened this Issue Nov 30, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@greenpdx

greenpdx commented Nov 30, 2017

I want to dynamically create an ordering var that I would be able to submit as a query. I am using Postgres and have created a view for a join of two tables. So far so good. I am using jsonrpc-http-server with r2d2 to a Vue frontend. My goal is to create an virtual datatable in vue with sorting.
I can sort locally in the browser, but I want new data from the server to be sorted

Hard coding the ordering works fine. I want to create a dynamic ordering. This will also help me work with other parameters passing. The parts that may be of use are commented out now.

I would like to also help, but my rust is not that good

use jsonrpc_core::*;
use meta::Meta;
use schema;
use diesel::prelude::*;
use diesel::Expression;

#[derive(Deserialize, Debug )]
struct Order {
    pub field: String,
    pub expr: String,
}

#[derive(Deserialize, Debug)]
struct Bye {
    pub start: i32,
    pub limit: i32,
    pub sort: Vec<Order>
}

pub fn methd_bye(params: Params, meta: Meta) -> Result<Value> {
    use self::schema::vidfull::dsl::*;
    use models::VidFull;
    let pool = meta.dbpool;
    let conn = pool.unwrap().pool.get().unwrap();
    println!("{:?}", &params);
    let parm: &Bye = &params.parse().unwrap();
    println!("BYE {:?}", parm);
    let sort = &parm.sort[0];
    let fld = &sort.field;  // String value of field name
    // I will need to loop to handle a multiple order
/*    let ordering: Box<BoxableExpression<fullvid, DB, SqlType=()>> =
        match sort.expr.as_ref() {
            "desc" => fld.desc(),
            "asc" => fld.asc(),
        }; */
    let rslt = vidfull.filter(id.ne(0))
//        .order(())
        .offset(parm.start as i64)
        .limit(parm.limit as i64)
        .load::<VidFull>(&*conn)
        .expect("Error");
    let r = json!(&rslt);

    Ok(Value::String(format!("{}", &r)))
}

Setup

Versions

  • **Rust:1.22
  • **Diesel: 0.16.0
  • **Database:postgres
  • **Operating System linux

Feature Flags

  • diesel:
  • diesel_codegen:

Problem Description

What are you trying to accomplish?

What is the expected output?

What is the actual output?

Are you seeing any additional errors?

Steps to reproduce

Checklist

  • I have already looked over the issue tracker for similar issues.
@sgrif

This comment has been minimized.

Member

sgrif commented Nov 30, 2017

See #1311 for someone's example of how they are doing dynamic ordering. You can also use the sql function for this.

@sgrif sgrif closed this Nov 30, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment