feat(orm): add an escape hatch for raw SELECT queries#609
Open
m4tx wants to merge 2 commits into
Open
Conversation
|
| Project | cot |
| Branch | db-fetch-raw |
| Testbed | github-ubuntu-latest |
Click to view all benchmark results
| Benchmark | Latency | Benchmark Result microseconds (µs) (Result Δ%) | Upper Boundary microseconds (µs) (Limit %) |
|---|---|---|---|
| empty_router/empty_router | 📈 view plot 🚷 view threshold | 11,577.00 µs(+59.65%)Baseline: 7,251.66 µs | 12,548.18 µs (92.26%) |
| json_api/json_api | 📈 view plot 🚷 view threshold | 906.36 µs(-16.10%)Baseline: 1,080.30 µs | 1,366.69 µs (66.32%) |
| nested_routers/nested_routers | 📈 view plot 🚷 view threshold | 854.01 µs(-15.13%)Baseline: 1,006.29 µs | 1,243.37 µs (68.69%) |
| single_root_route/single_root_route | 📈 view plot 🚷 view threshold | 815.87 µs(-15.85%)Baseline: 969.60 µs | 1,206.40 µs (67.63%) |
| single_root_route_burst/single_root_route_burst | 📈 view plot 🚷 view threshold | 16,974.00 µs(-2.29%)Baseline: 17,371.55 µs | 21,686.46 µs (78.27%) |
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a raw-SQL “escape hatch” to Cot’s ORM so callers can execute raw SELECT statements and map result rows directly into Model types, complementing the existing raw statement execution APIs.
Changes:
- Introduces
Database::raw_as/Database::raw_as_withfor mapping rawSELECTresults into models. - Adds documentation for raw SQL query APIs, including parameterized variants and SQL injection warnings.
- Adds database tests covering raw statement execution and raw row-to-model mapping.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| docs/databases/queries.md | Documents raw SQL APIs and shows usage examples for raw statements and raw SELECT mapping. |
| cot/tests/db.rs | Adds tests validating raw, raw_as, and (SQLite-only) parameterized raw methods. |
| cot/src/db.rs | Implements raw_as / raw_as_with by routing raw SQL through the existing fetch_all path via a RawStatement binder. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| .collect::<Vec<_>>(); | ||
| let values = SqlxValues(sea_query::Values(values)); | ||
|
|
||
| let rows = self.fetch_all(&RawStatement { sql: query, values }).await?; |
| } | ||
| ``` | ||
|
|
||
| If your query takes parameters, use [`raw_with`](struct@cot::db::Database#method.raw_with) instead of interpolating the values into the query string yourself. The parameters are passed separately and bound safely by the underlying database driver, so this is the preferred way of running raw statements that depend on runtime values. |
Comment on lines
+1978
to
+1982
| /// This method executes the raw SQL string without any sanitization. | ||
| /// Callers are responsible for ensuring the query is safe. The bound | ||
| /// `values`, however, are passed to the database driver separately from | ||
| /// the query text and are safe from SQL injection. | ||
| /// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue or discussion
Description
Type of change
Checklist
just test-all)just clippy)cargo fmt)