Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upProvide a better "raw sql" escape hatch for full queries that we don't support in the query builder #650
Comments
killercup
added
discussion desired
help wanted
labels
Feb 11, 2017
sgrif
referenced this issue
Feb 22, 2017
Closed
Remove the usage of tuples in argument positions from our API #747
sgrif
referenced this issue
Mar 15, 2017
Merged
Allow `SqlLiteral` to be used as an on conflict target #801
This comment has been minimized.
andy128k
commented
May 3, 2017
|
I'm new to Rust and Diesel. but what I love is type safety and all great things LoadDsl does. That's why I'd prefer "better I'm playing with Diesel and see a lot of limitations (like inability to make multiple joins or multiple belongs_to) and that's ok. Even with raw SQL Diesel is still great. I created couple workarounds to solve issues with raw sql queries. |
sgrif commentedFeb 9, 2017
Today we have some form of an escape hatch in the form of the
sqlfunction. However, it's meant for use with small fragments, not full queries. It's limited because it requires specifying the full SQL type of the output, and cannot handle bind parameters. I'd like to introduce a new function that is a more ergonomic API for raw SQL as an escape hatch for queries we don't support in the query builder yet.Ultimately I've resisted introducing this, as we want to support all possible safe queries in the query builder eventually, but I think we need a proper escape hatch for us to go 1.0.
I'd like this API to be fully untyped, and named to make it clear that no verification of the query is occurring. For deserialization, it should not go through
Queryable, as I do not think enforcing column order is ergonomic when writing raw SQL (andQueryableneeds a SQL type to work). Instead I'd like to introduce anUntypedQueryable, which takes a row directly, and is able to fetch fields by name. I'm fine with the API requiring specifying the SQL type on this call if type inference can't pick it up. e.g.let updated_at: NaiveDateTime = row.get::<Timestamp>("updated_at")?. I suspect for most types which only implementFromSqlfor a single DB type type inference will handle it, but we should explore our options here and see what feels best.I'm less clear what we should do with bind parameters. I do know that I don't want to go the route that the postgres crate went where we take
&[&UntypedToSql], as constructing that value is painful, it forces dynamic dispatch (which then usually also forces boxing), and requires&[]for queries with no bind parameters. We could potentially still pass tuples here as long as type inference does what we want. I also thinkconn.raw_query("SELECT * FROM users WHERE id = $1").bind(id)could work (as an aside, I think we will likely need to pass the connection in before the bind parameters for this to work, which will lead to a semi-weird api ofconn.raw_query().bind().execute()). I'm fine with also sometimes requiring giving a SQL type for the bind here as long as type inference is able to handle the most common cases.