Skip to main content

Module codec

Module codec 

Source
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. AnyValueKind itself doesn’t implement Encode/Type (confirmed absent from sqlx-core’s own source - the only impl found was Value 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, mirroring sqlx-core’s own internal AnyArguments::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_value uses for the KV store) into the shape bind_any needs - 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 plain INTEGER, never a native BOOLEAN/TIMESTAMP column, so a checkbox posting true for such a column must become AnyValueKind::BigInt(1), not AnyValueKind::Bool(true) - binding the wrong kind against an INTEGER column is exactly the mistake this function exists to avoid.
row_to_json
Decodes an entire row into a {column: value} JSON object. sqlx::Any has 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.