How to set Custom fields on query


#1

Im working with rust nightly and diesel 1.3.0 and mysql feature

Hello, I have something like this.
let results = places::table
.inner_join(categories_places::table)
.select(places::table::all_columns())
.filter(categories_places::category_id.eq(categorie_id_url))
.limit(LIMIT_SQL)
.load(connection).unwrap();

What I pretend is to change the line
.select(places::table::all_columns()) to something like
.select(“MY CUSTOM FIELDS”) taking the fields from a
Option<CustomFieldsStruct>

THanks in advence


#2

That is not possible in a general way, so that’s nothing that could be fixed by diesel 2.0
Let’s try to elaborate the problem on a example.

table! {
    example(id) {
        id -> Integer,
        some_text -> Text,
        some_bool -> Bool,
    }
}

enum ColumnToSelect {
    Id,
    SomeText,
    SomeBool,
}

fn select_from_example(column: ColumnToSelect, conn: &PgConnection, id: i32) -> Result<MyQueryResult, MyError> {
    let query = example::table.filter(example::id.eq(id));
    let query = match column {
        ColumnToSelect::Id => query.select(example::id),
        ColumnToSelect::SomeText => query.select(example::some_text),
        ColumnToSelect::SomeBool => query.select(example::some_bool),
    };
    Ok(query.first(conn)?)
}

So the question to answer is how should MyQueryResult look like? We really need to know which types are returned by some query to deserialize the result correctly.

Beside of that it is already possible do do such things today if and only if all queries return the same type using boxed queries or diesel-dynamic-schema


#3

I am also looking to find a solution for this.

Is it possible to add an unsafe first function that returns a pointer? This is already being done in the database connection modules. Can this be sent to the caller as a raw pointer or would it not be feasible to free the memory after the user uses it?

Alternately they can get wrapped inside an enum.