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

Add ability to select without a from clause #97

Closed
sgrif opened this Issue Jan 15, 2016 · 0 comments

Comments

Projects
None yet
1 participant
@sgrif
Member

sgrif commented Jan 15, 2016

I'm trying to more thoroughly test some cases, and I've got a lot of code that looks like:

connection.query_sql_params::<Bool, bool, Timestamp, SystemTime>(
    "SELECT $1::timestamp = '1970-01-01'", &UNIX_EPOCH)
.unwrap().nth(0).unwrap()

I'd like to instead be able to write

select(sql("'1970-01-01'").eq(UNIX_EPOCH))
    .first(&connection).unwrap()

This will require some changes to SelectStatement which make the from clause optional. I'm unsure if I want to allow select(foo).from(bar)

@sgrif sgrif added the enhancement label Jan 15, 2016

@sgrif sgrif closed this in c26d299 Jan 16, 2016

mcasper added a commit to mcasper/diesel that referenced this issue Jan 17, 2016

Add a bare `select` function for select statements with no from clause
In addition to this function, I have also added a `sql` function, which
is shorthand for `SqlLiteral` (but only takes `&str`, not `String`), and
`in_time_zone` which is not part of the public API.

`sql` takes `&str`, instead of `T: Into<String>`, because the first type
parameter is always going to need to be passed, and I find
`sql::<Timestamp, _>` ugly. There's always `SqlLiteral::new` if you need
to pass a `String`, or you can just deref it to `str` and accept the
copying.

I had hoped to eliminate `query_sql_params`, but there's one test we
have that's using it which requires a where clause. I can't just hack it
and do `select(sql("1 WHERE ..."))` because it uses bind params. I'll
implement `FilterDsl` for `BareSelectStatement` at some point. I'd also
like to remove `query_sql`, as I think that `select(sql` covers it
suffciently.

I've opted to leave `in_time_zone` out of the public API, as it should
actually have the return type of `Timestamptz`, and I'm really unsure
how various infix predicates with the lhs being `Timestamp` and the rhs
being `Timestamptz` are going to interact. We might be able to get away
with just not allowing it and have it be a non-issue, similarly to
`VarChar` and `Text`, but it needs further exploration.

Fixes diesel-rs#97.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment