Expand description
Generic row <-> value conversion - the one place this crate reads a database row without knowing its shape ahead of time, and the one place it binds a value back without knowing its type ahead of time. Every other part of the SQL admin dashboard goes through these two functions rather than reimplementing either.
Why this needs a direct sqlx-core dependency. The sqlx facade
crate re-exports AnyRow/AnyTypeInfoKind but not AnyColumn or
AnyValueKind - the two types a generic decoder actually needs to
name (confirmed by grepping the whole sqlx-0.8.6 source tree: zero
hits for either). sqlx-core is added as a direct dependency,
version-pinned to the exact same range as sqlx itself, so it
resolves to the identical already-locked instance - not a second copy
of the type. Both AnyColumn/AnyValueKind are #[doc(hidden)] in
sqlx-core’s own re-export (semver-exempt, not part of sqlx’s stable
public API) - a future sqlx point release could restructure this
without a semver bump; worth knowing if a version bump ever breaks
this file specifically.
Functions§
- bind_
any - Binds one already-converted value onto a query.
AnyValueKinditself doesn’t implementEncode/Type(confirmed absent fromsqlx-core’s own source - the only impl found wasValue for AnyValue, the read-side trait, not the write-side one) - so this can’t be one generic.bind()call. Each arm unwraps to a concrete primitive and is its own monomorphized bind, mirroringsqlx-core’s own internalAnyArguments::convert_to, which does the identical thing. - json_
to_ any_ value - Converts a form-submitted value (already parsed as loose JSON, the
same “try JSON, fall back to a plain string” idea
larust_db::parse_cli_valueuses for the KV store) into the shapebind_anyneeds - driven by the column’s declared type, not the JSON value’s own shape. This matters concretely: this schema (like most SQLite-first apps) stores booleans and timestamps as plainINTEGER, never a nativeBOOLEAN/TIMESTAMPcolumn, so a checkbox postingtruefor such a column must becomeAnyValueKind::BigInt(1), notAnyValueKind::Bool(true)- binding the wrong kind against anINTEGERcolumn is exactly the mistake this function exists to avoid. - row_
to_ json - Decodes an entire row into a
{column: value}JSON object.sqlx::Anyhas already normalized every backend’s native types into one of 9 kinds by the time a row reaches here, so this needs no per-backend branching at all.