spin-sdk 7.0.0

The Spin Rust SDK makes it easy to build Spin components in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! MySQL relational database storage.
//!
//! You can use the [`Decode`] trait to convert a [`DbValue`] to a
//! suitable Rust type. The following table shows available conversions.
//!
//! # Types
//!
//! | Rust type | WIT (db-value)      | MySQL type(s)           |
//! |-----------|---------------------|-------------------------|
//! | `bool`    | int8(s8)            | TINYINT(1), BOOLEAN     |
//! | `i8`      | int8(s8)            | TINYINT                 |
//! | `i16`     | int16(s16)          | SMALLINT                |
//! | `i32`     | int32(s32)          | MEDIUM, INT             |
//! | `i64`     | int64(s64)          | BIGINT                  |
//! | `u8`      | uint8(u8)           | TINYINT UNSIGNED        |
//! | `u16`     | uint16(u16)         | SMALLINT UNSIGNED       |
//! | `u32`     | uint32(u32)         | INT UNSIGNED            |
//! | `u64`     | uint64(u64)         | BIGINT UNSIGNED         |
//! | `f32`     | floating32(float32) | FLOAT                   |
//! | `f64`     | floating64(float64) | DOUBLE                  |
//! | `String`  | str(string)         | VARCHAR, CHAR, TEXT     |
//! | `Vec<u8>` | binary(list\<u8\>)  | VARBINARY, BINARY, BLOB |

use crate::wit_bindgen;
use std::sync::Arc;

#[doc(hidden)]
/// Module containing wit bindgen generated code.
///
/// This is only meant for internal consumption.
pub mod wit {
    #![allow(missing_docs)]
    use crate::wit_bindgen;

    wit_bindgen::generate!({
        runtime_path: "crate::wit_bindgen::rt",
        world: "spin-sdk-mysql-v3",
        path: "wit",
        generate_all,
    });

    pub use spin::mysql::mysql;
}

/// An open connection to a MySQL database.
///
/// # Examples
///
/// Load a set of rows from a local MySQL database, and iterate over them.
///
/// ```no_run
/// use spin_sdk::mysql::{Connection, Decode, ParameterValue};
///
/// # async fn run() -> anyhow::Result<()> {
/// # let min_age = 0;
/// let db = Connection::open("mysql://root:my_password@localhost/mydb").await?;
///
/// let mut query_result = db.query(
///     "SELECT * FROM users WHERE age >= ?",
///     &[min_age.into()]
/// ).await?;
///
/// while let Some(row) = query_result.next().await {
///     let name = row.get::<String>("name").unwrap();
///     println!("Found user {name}");
/// }
///
/// query_result.result().await?;
/// # Ok(())
/// # }
/// ```
///
/// Perform an aggregate (scalar) operation over a table. The result set
/// contains a single column, with a single row.
///
/// ```no_run
/// use spin_sdk::mysql::{Connection, Decode};
///
/// # async fn run() -> anyhow::Result<()> {
/// let db = Connection::open("mysql://root:my_password@localhost/mydb").await?;
///
/// let mut query_result = db.query("SELECT COUNT(*) FROM users", &[]).await?;
///
/// assert_eq!(1, query_result.columns().len());
/// assert_eq!("COUNT(*)", query_result.columns()[0].name);
///
/// let rows = query_result.collect().await?;
///
/// assert_eq!(1, rows.len());
///
/// let count = &rows[0][0];
/// # Ok(())
/// # }
/// ```
///
/// Delete rows from a MySQL table. This uses [Connection::execute()]
/// instead of the `query` method.
///
/// ```no_run
/// use spin_sdk::mysql::{Connection, ParameterValue};
///
/// # async fn run() -> anyhow::Result<()> {
/// let db = Connection::open("mysql://root:my_password@localhost/mydb").await?;
///
/// db.execute(
///     "DELETE FROM users WHERE name = ?",
///     &["Baldrick".to_owned().into()]
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub struct Connection(wit::mysql::Connection);

impl Connection {
    /// Open a connection to a MySQL database.
    ///
    /// The address may be in connection string form (`"host=... dbname=..."`)
    /// or in URL form (`"mysql://<host>/<dbname>?..."`).
    pub async fn open(address: impl Into<String>) -> Result<Self, Error> {
        let inner = wit::mysql::Connection::open(address.into()).await?;
        Ok(Self(inner))
    }

    /// Query the database.
    ///
    /// Use this function for queries that return rows (typically `SELECT` queries).
    /// For side-effectful queries, see [`Connection::execute`].
    pub async fn query(
        &self,
        statement: impl Into<String>,
        params: impl Into<Vec<ParameterValue>>,
    ) -> Result<QueryResult, Error> {
        let (columns, rows, result) = self.0.query(statement.into(), params.into()).await?;
        Ok(QueryResult {
            columns: Arc::new(columns),
            rows,
            result,
        })
    }

    /// Execute a command against the database.
    ///
    /// Use this function for side-effectful queries (such as `INSERT` or `DELETE` queries).
    /// For queries that return row data, see [`Connection::query`].
    pub async fn execute(
        &self,
        statement: impl Into<String>,
        params: impl Into<Vec<ParameterValue>>,
    ) -> Result<(), Error> {
        self.0
            .execute(statement.into(), params.into())
            .await
            .map_err(Error::MysqlError)
    }
}

#[doc(inline)]
pub use wit::mysql::Error as MysqlError;

#[doc(inline)]
pub use wit::mysql::{Column, DbDataType, DbValue, ParameterValue};

/// The result of a [`Connection::query`] operation.
pub struct QueryResult {
    columns: Arc<Vec<Column>>,
    rows: wit_bindgen::StreamReader<Vec<DbValue>>,
    result: wit_bindgen::FutureReader<Result<(), MysqlError>>,
}

impl QueryResult {
    /// The columns in the query result.
    pub fn columns(&self) -> &[Column] {
        &self.columns
    }

    /// Gets the next row in the result set.
    ///
    /// If this is `None`, there are no more rows available. You _must_
    /// await [`QueryResult::result()`] to determine if all rows
    /// were read successfully.
    pub async fn next(&mut self) -> Option<Row> {
        self.rows.next().await.map(|r| Row {
            columns: self.columns.clone(),
            result: r,
        })
    }

    /// Whether the query completed successfully or with an error.
    pub async fn result(self) -> Result<(), Error> {
        self.result.await.map_err(Error::MysqlError)
    }

    /// Collect all rows in the result set.
    ///
    /// This is provided for when the result set is small enough to fit in
    /// memory and you do not require streaming behaviour.
    pub async fn collect(mut self) -> Result<Vec<Row>, Error> {
        let mut rows = vec![];
        while let Some(row) = self.next().await {
            rows.push(row);
        }
        self.result.await.map_err(Error::MysqlError)?;
        Ok(rows)
    }

    /// An asynchronous reader for the rows of the query result. Call
    /// `.next().await` to iterate over the rows. When this returns `None`,
    /// you have read all available rows. At this point you _must_ check
    /// [`QueryResult::result()`] to determine if the read completed
    /// successfully.
    ///
    /// This provides each row as a plain vector of database values.
    /// [`QueryResult::next()`] provides a more ergonomic wrapper.
    ///
    /// To collect all rows into a vector, see [`QueryResult::collect`].
    pub fn rows(&mut self) -> &mut wit_bindgen::StreamReader<Vec<DbValue>> {
        &mut self.rows
    }

    /// Extracts the underlying Wasm Component Model results of the query.
    #[allow(
        clippy::type_complexity,
        reason = "sorry clippy that's just what the inner bits are"
    )]
    pub fn into_inner(
        self,
    ) -> (
        Vec<Column>,
        wit_bindgen::StreamReader<Vec<DbValue>>,
        wit_bindgen::FutureReader<Result<(), MysqlError>>,
    ) {
        ((*self.columns).clone(), self.rows, self.result)
    }
}

/// A database row result.
///
/// There are two representations of a MySQL row in the SDK.  This type is useful for
/// addressing elements by column name, and is obtained from the [QueryResult::next()] function.
/// The [DbValue] vector representation is obtained from the [QueryResult::rows()] function, and provides
/// index-based lookup or low-level access to row values via a vector.
pub struct Row {
    columns: Arc<Vec<wit::mysql::Column>>,
    result: Vec<DbValue>,
}

impl Row {
    /// Get a value by its column name. The value is converted to the target type as per the
    /// conversion table shown in the module documentation.
    ///
    /// This function returns None for both no such column _and_ failed conversion. You should use
    /// it only if you do not need to address errors (that is, if you know that conversion should
    /// never fail). If your code does not know the type in advance, use the raw [QueryResult::rows()] function
    /// instead of the [`QueryResult::next()`] or [`QueryResult::collect()`] wrappers to access
    /// the underlying [DbValue] enum: this will allow you to
    /// determine the type and process it accordingly.
    ///
    /// Additionally, this function performs a name lookup each time it is called. If you are iterating
    /// over a large number of rows, it's more efficient to use column indexes, either calculated or
    /// statically known from the column order in the SQL.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use spin_sdk::mysql::{Connection, DbValue};
    ///
    /// # async fn run() -> anyhow::Result<()> {
    /// # let user_id = 0;
    /// let db = Connection::open("mysql://root:my_password@localhost/mydb").await?;
    /// let mut query_result = db.query(
    ///     "SELECT * FROM users WHERE id = ?",
    ///     &[user_id.into()]
    /// ).await?;
    /// let user_row = query_result.next().await.unwrap();
    ///
    /// let name = user_row.get::<String>("name").unwrap();
    /// let age = user_row.get::<i16>("age").unwrap();
    /// # Ok(())
    /// # }
    /// ```
    pub fn get<T: Decode>(&self, column: &str) -> Option<T> {
        let i = self.columns.iter().position(|c| c.name == column)?;
        let db_value = self.result.get(i)?;
        Decode::decode(db_value).ok()
    }
}

impl std::ops::Index<usize> for Row {
    type Output = DbValue;

    fn index(&self, index: usize) -> &Self::Output {
        &self.result[index]
    }
}

/// A MySQL error
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Failed to deserialize [`DbValue`]
    #[error("error value decoding: {0}")]
    Decode(String),
    /// MySQL query failed with an error
    #[error(transparent)]
    MysqlError(#[from] MysqlError),
}

/// A type that can be decoded from the database.
pub trait Decode: Sized {
    /// Decode a new value of this type using a [`DbValue`].
    fn decode(value: &DbValue) -> Result<Self, Error>;
}

impl<T> Decode for Option<T>
where
    T: Decode,
{
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::DbNull => Ok(None),
            v => Ok(Some(T::decode(v)?)),
        }
    }
}

impl Decode for bool {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Int8(0) => Ok(false),
            DbValue::Int8(1) => Ok(true),
            _ => Err(Error::Decode(format_decode_err(
                "TINYINT(1), BOOLEAN",
                value,
            ))),
        }
    }
}

impl Decode for i8 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Int8(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("TINYINT", value))),
        }
    }
}

impl Decode for i16 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Int16(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("SMALLINT", value))),
        }
    }
}

impl Decode for i32 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Int32(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("INT", value))),
        }
    }
}

impl Decode for i64 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Int64(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("BIGINT", value))),
        }
    }
}

impl Decode for u8 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Uint8(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("UNSIGNED TINYINT", value))),
        }
    }
}

impl Decode for u16 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Uint16(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("UNSIGNED SMALLINT", value))),
        }
    }
}

impl Decode for u32 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Uint32(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err(
                "UNISIGNED MEDIUMINT, UNSIGNED INT",
                value,
            ))),
        }
    }
}

impl Decode for u64 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Uint64(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("UNSIGNED BIGINT", value))),
        }
    }
}

impl Decode for f32 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Floating32(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("FLOAT", value))),
        }
    }
}

impl Decode for f64 {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Floating64(n) => Ok(*n),
            _ => Err(Error::Decode(format_decode_err("DOUBLE", value))),
        }
    }
}

impl Decode for Vec<u8> {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Binary(n) => Ok(n.to_owned()),
            _ => Err(Error::Decode(format_decode_err("BINARY, VARBINARY", value))),
        }
    }
}

impl Decode for String {
    fn decode(value: &DbValue) -> Result<Self, Error> {
        match value {
            DbValue::Str(s) => Ok(s.to_owned()),
            _ => Err(Error::Decode(format_decode_err(
                "CHAR, VARCHAR, TEXT",
                value,
            ))),
        }
    }
}

macro_rules! impl_parameter_value_conversions {
    ($($ty:ty => $id:ident),*) => {
        $(
            impl From<$ty> for ParameterValue {
                fn from(v: $ty) -> ParameterValue {
                    ParameterValue::$id(v)
                }
            }
        )*
    };
}

impl_parameter_value_conversions! {
    i8 => Int8,
    i16 => Int16,
    i32 => Int32,
    i64 => Int64,
    f32 => Floating32,
    f64 => Floating64,
    bool => Boolean,
    String => Str,
    Vec<u8> => Binary
}

fn format_decode_err(types: &str, value: &DbValue) -> String {
    format!("Expected {} from the DB but got {:?}", types, value)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn boolean() {
        assert!(bool::decode(&DbValue::Int8(1)).unwrap());
        assert!(bool::decode(&DbValue::Int8(3)).is_err());
        assert!(bool::decode(&DbValue::Int32(0)).is_err());
        assert!(Option::<bool>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn int8() {
        assert_eq!(i8::decode(&DbValue::Int8(0)).unwrap(), 0);
        assert!(i8::decode(&DbValue::Int32(0)).is_err());
        assert!(Option::<i8>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn int16() {
        assert_eq!(i16::decode(&DbValue::Int16(0)).unwrap(), 0);
        assert!(i16::decode(&DbValue::Int32(0)).is_err());
        assert!(Option::<i16>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn int32() {
        assert_eq!(i32::decode(&DbValue::Int32(0)).unwrap(), 0);
        assert!(i32::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<i32>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn int64() {
        assert_eq!(i64::decode(&DbValue::Int64(0)).unwrap(), 0);
        assert!(i64::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<i64>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn uint8() {
        assert_eq!(u8::decode(&DbValue::Uint8(0)).unwrap(), 0);
        assert!(u8::decode(&DbValue::Uint32(0)).is_err());
        assert!(Option::<u16>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn uint16() {
        assert_eq!(u16::decode(&DbValue::Uint16(0)).unwrap(), 0);
        assert!(u16::decode(&DbValue::Uint32(0)).is_err());
        assert!(Option::<u16>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn uint32() {
        assert_eq!(u32::decode(&DbValue::Uint32(0)).unwrap(), 0);
        assert!(u32::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<u32>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn uint64() {
        assert_eq!(u64::decode(&DbValue::Uint64(0)).unwrap(), 0);
        assert!(u64::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<u64>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn floating32() {
        assert!(f32::decode(&DbValue::Floating32(0.0)).is_ok());
        assert!(f32::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<f32>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn floating64() {
        assert!(f64::decode(&DbValue::Floating64(0.0)).is_ok());
        assert!(f64::decode(&DbValue::Boolean(false)).is_err());
        assert!(Option::<f64>::decode(&DbValue::DbNull).unwrap().is_none());
    }

    #[test]
    fn str() {
        assert_eq!(
            String::decode(&DbValue::Str(String::from("foo"))).unwrap(),
            String::from("foo")
        );

        assert!(String::decode(&DbValue::Int32(0)).is_err());
        assert!(
            Option::<String>::decode(&DbValue::DbNull)
                .unwrap()
                .is_none()
        );
    }
}