Skip to main content

sqlx_json/
traits.rs

1//! Row-to-JSON conversion and query-result traits.
2
3use serde_json::Value;
4
5/// Converts a single database row into a JSON object.
6pub trait RowExt {
7    /// Converts this row's columns to a JSON object.
8    ///
9    /// Each column becomes a key in the returned object, with values
10    /// converted to the most appropriate JSON type. `NULL` columns
11    /// produce [`Value::Null`].
12    ///
13    /// # Returns
14    ///
15    /// A [`Value::Object`] where keys are column names and values are
16    /// type-appropriate JSON values.
17    fn to_json(&self) -> Value;
18}
19
20/// Extracts the affected row count from a backend query result.
21///
22/// sqlx's `Database::QueryResult` associated type exposes
23/// `rows_affected()` as an inherent method on each concrete backend
24/// type.  This trait provides a single generic bound so the blanket
25/// `Connection` impl can call it without knowing the concrete type.
26pub trait QueryResult {
27    /// Returns the number of rows affected by the executed statement.
28    fn rows_affected(&self) -> u64;
29}
30
31impl QueryResult for sqlx::mysql::MySqlQueryResult {
32    fn rows_affected(&self) -> u64 {
33        self.rows_affected()
34    }
35}
36
37impl QueryResult for sqlx::postgres::PgQueryResult {
38    fn rows_affected(&self) -> u64 {
39        self.rows_affected()
40    }
41}
42
43impl QueryResult for sqlx::sqlite::SqliteQueryResult {
44    fn rows_affected(&self) -> u64 {
45        self.rows_affected()
46    }
47}