Skip to content

Commit 2e39331

Browse files
authored
Refactor SQLPage functions into convention-driven modules (#1328)
* refactor: split SQLPage functions into one module each Each built-in `sqlpage.*` function is now a plain `async fn` in its own file under `sqlpage_functions/functions/`, with an ordinary Rust signature and no marker comments or macros inside it. Registration is automatic: `build.rs` lists the files in `functions/` into a `sqlpage_functions!` call (the only generated code, one line per function), and that macro declares the modules and builds the `SqlPageFunctionName` enum the SQL engine dispatches on. There is no marker-comment parsing and no per-argument codegen. Argument extraction, dispatch and return-value conversion are ordinary generic code in function_traits.rs (`Extract`, `Handler`, `IntoCowResult`), using the same `Fn`-arity trick axum uses for handlers, so a function's argument and return types are read straight from its signature. Also folds in main's FileAccess centralization (#1327) for the file-reading functions. Verified: `cargo test` passes (148 lib + 70 integration tests, including run_all_sql_test_files, the hmac webhook tests, the upload tests and test_file_upload_through_runsql which exercises the &mut DbConn path). `cargo clippy` and `cargo fmt --check` are clean. * less magic
1 parent 9ca655f commit 2e39331

42 files changed

Lines changed: 1506 additions & 1313 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Built-in SQL functions
2+
3+
Each built-in `sqlpage.*` function is a plain `async fn` in its own file in
4+
[`functions/`](functions). The file stem is the SQL function name and must match the Rust function it
5+
exports:
6+
7+
```rust
8+
use std::borrow::Cow;
9+
10+
use crate::webserver::http_request_info::RequestInfo;
11+
12+
pub(super) async fn example(request: &RequestInfo, value: Option<Cow<'_, str>>) -> Option<String> {
13+
// ...
14+
}
15+
```
16+
17+
To add `sqlpage.example`, create `functions/example.rs` and add it to the
18+
[`sqlpage_functions!`](function_traits.rs) call in [`functions.rs`](functions.rs):
19+
20+
```rust
21+
sqlpage_functions! {
22+
// ...
23+
example,
24+
}
25+
```
26+
27+
The [`sqlpage_functions!`](function_traits.rs) macro declares the modules and generates the
28+
`SqlPageFunctionName` enum the SQL engine dispatches on. Per-function argument extraction, dispatch,
29+
and return-value conversion are handled generically in [`function_traits.rs`](function_traits.rs) by
30+
the `Extract`, `Handler`, and `IntoCowResult` traits. A function's argument and return types are read
31+
straight from its signature, so supported argument types are the types that implement `Extract` there.
32+
Functions can take up to five arguments.
33+
34+
Keep helpers and unit tests that are specific to a function in that function's file. Shared helpers can
35+
be made `pub(super)` and imported by name from sibling function modules.

src/webserver/database/sqlpage_functions/function_definition_macro.rs

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)