Gitter

Crates.io Docs.rs Build Status

mysql_common

This crate is an implementation of basic MySql protocol primitives.

This crate: * defines basic MySql constants; * implements necessary functionality for MySql cached_sha2_password, mysql_native_password and legacy authentication plugins; * implements helper traits for MySql protocol IO; * implements support of named parameters for prepared statements; * implements parsers for a subset of MySql protocol packets (including binlog packets); * defines rust representation of MySql protocol values and rows; * implements conversion between MySql values and rust types, between MySql rows and tuples of rust types. * implements FromRow and FromValue derive macros

Supported rust types

Crate offers conversion from/to MySql values for following types (please see MySql documentation on supported ranges for numeric types). Following table refers to MySql protocol types (see Value struct) and not to MySql column types. Please see MySql documentation for column and protocol type correspondence:

| Type | Notes | | ------------------------------------ | ------------------------------------------------------- | | {i,u}8..{i,u}128, {i,u}size | MySql int/uint will be converted, bytes will be parsed.
⚠️ Note that range of {i,u}128 is greater than supported by MySql integer types but it'll be serialized anyway (as decimal bytes string). | | f32 | MySql float will be converted to f32, bytes will be parsed as f32.
⚠️ MySql double won't be converted to f32 to avoid precision loss (see #17) | | f64 | MySql float and double will be converted to f64, bytes will be parsed as f64. | | bool | MySql int {0, 1} or bytes {"0x30", "0x31"} | | Vec<u8> | MySql bytes | | String | MySql bytes parsed as utf8 | | Duration (std and time) | MySql time or bytes parsed as MySql time string | | [time::PrimitiveDateTime] (v0.2.x) | MySql date time or bytes parsed as MySql date time string (⚠️ lossy! microseconds are ignored) | | [time::Date] (v0.2.x) | MySql date or bytes parsed as MySql date string (⚠️ lossy! microseconds are ignored) | | [time::Time] (v0.2.x) | MySql time or bytes parsed as MySql time string (⚠️ lossy! microseconds are ignored) | | [time::Duration] (v0.2.x) | MySql time or bytes parsed as MySql time string | | [time::PrimitiveDateTime] (v0.3.x) | MySql date time or bytes parsed as MySql date time string (⚠️ lossy! microseconds are ignored) | | [time::Date] (v0.3.x) | MySql date or bytes parsed as MySql date string (⚠️ lossy! microseconds are ignored) | | [time::Time] (v0.3.x) | MySql time or bytes parsed as MySql time string (⚠️ lossy! microseconds are ignored) | | [time::Duration] (v0.3.x) | MySql time or bytes parsed as MySql time string | | [chrono::NaiveTime] | MySql date or bytes parsed as MySql date string | | [chrono::NaiveDate] | MySql date or bytes parsed as MySql date string | | [chrono::NaiveDateTime] | MySql date or bytes parsed as MySql date string | | [uuid::Uuid] | MySql bytes parsed using Uuid::from_slice | | [serde_json::Value] | MySql bytes parsed using serde_json::from_str | | mysql_common::Deserialized<T : DeserializeOwned> | MySql bytes parsed using serde_json::from_str | | Option<T: FromValue> | Must be used for nullable columns to avoid errors | | [decimal::Decimal] | MySql int, uint or bytes parsed using Decimal::from_str.
⚠️ Note that this type doesn't support full range of MySql DECIMAL type. | | [bigdecimal::BigDecimal] (v0.2.x) | MySql int, uint, floats or bytes parsed using BigDecimal::parse_bytes.
⚠️ Note that range of this type is greater than supported by MySql DECIMAL type but it'll be serialized anyway. | | [bigdecimal::BigDecimal] (v0.3.x) | MySql int, uint, floats or bytes parsed using BigDecimal::parse_bytes.
⚠️ Note that range of this type is greater than supported by MySql DECIMAL type but it'll be serialized anyway. | | num_bigint::{BigInt, BigUint} | MySql int, uint or bytes parsed using _::parse_bytes.
⚠️ Note that range of this type is greater than supported by MySql integer types but it'll be serialized anyway (as decimal bytes string). |

Also crate provides from-row convertion for the following list of types (see FromRow trait):

| Type | Notes | | ----------------------------------------------- | ------------------------------------------------- | | Row | Trivial conversion for Row itself. | | T: FromValue | For rows with a single column. | | (T1: FromValue [, ..., T12: FromValue]) | Row to a tuple of arity 1-12. | | [frunk::Hlist!] types | Usefull to overcome tuple arity limitation |

Crate features

| Feature | Description | Default | | -------------- | ---------------------------------------------------- | ------- | | bigdecimal02 | Enables bigdecimal v0.2.x types support | 🔴 | | bigdecimal | Enables bigdecimal v0.3.x types support | 🟢 | | chrono | Enables chrono types support | 🔴 | | rust_decimal | Enables rust_decimal types support | 🟢 | | time02 | Enables time v0.2.x types support | 🔴 | | time | Enables time v0.3.x types support | 🟢 | | frunk | Enables FromRow for frunk::Hlist! types | 🟢 | | derive | Enables FromValue and FromRow derive macros | 🟢 |

Derive Macros

FromValue Derive

Supported derivations:

Enums

Container attributes:
Example

Given ENUM('x-small', 'small', 'medium', 'large', 'x-large') on MySql side:

```rust

fn main() {

/// Note: the crate_name attribute should not be necessary.

[derive(FromValue)]

[mysql(renameall = "kebab-case", cratename = "mysql_common")]

[repr(u8)]

enum Size { XSmall = 1, Small, Medium, Large, XLarge, }

fn assertfromrowworks(x: Row) -> Size { fromrow(x) }

} ```

Newtypes

It is expected, that wrapper value satisfies FromValue or deserialize_with is given. Also note, that to support FromRow the wrapped value must satisfy Into<Value> or serialize_with must be given.

Container attributes:
Example

```rust

/// Trivial example

[derive(FromValue)]

struct Inch(i32);

/// Example of a {serialize|deserialize}_with.

[derive(FromValue)]

[mysql(deserializewith = "negde", serializewith = "negser")]

struct Neg(i64);

/// Wrapped generic. Bounds are inferred.

[derive(FromValue)]

struct Foo(Option);

/// Example of additional bounds.

[derive(FromValue)]

[mysql(bound = "'b: 'a, T: 'a, U: From, V: From")]

struct Bar<'a, 'b, const N: usize, T, U, V>(ComplexTypeToWrap<'a, 'b, N, T, U, V>);

fn assertfromrowworks<'a, 'b, const N: usize, T, U, V>(x: Row) -> (Inch, Neg, Foo, Bar<'a, 'b, N, T, U, V>) where 'b: 'a, T: 'a, U: From, V: From, { fromrow(x) }

// test boilerplate..

/// Dummy complex type with additional bounds on FromValue impl. struct ComplexTypeToWrap<'a, 'b, const N: usize, T, U, V>([(&'a T, &'b U, V); N]);

struct FakeIr;

impl TryFrom for FakeIr { // ... }

impl<'a, 'b: 'a, const N: usize, T: 'a, U: From, V: From> From for ComplexTypeToWrap<'a, 'b, N, T, U, V> { // ... }

impl From for Value { // ... }

impl<'a, 'b: 'a, const N: usize, T: 'a, U: From, V: From> FromValue for ComplexTypeToWrap<'a, 'b, N, T, U, V> { type Intermediate = FakeIr; }

fn neg_de(v: Value) -> Result { match v { Value::Int(x) => Ok(-x), Value::UInt(x) => Ok(-(x as i64)), x => Err(FromValueError(x)), } }

fn neg_ser(x: i64) -> Value { Value::Int(-x) }

```

FromRow Derive

Also defines some constants on the struct:

Supported derivations:

Container attributes:

Field attributes:

Example

```rust

/// Note: the crate_name attribute should not be necessary.

[derive(Debug, PartialEq, Eq, FromRow)]

[mysql(tablename = "Foos", cratename = "mysql_common")]

struct Foo { id: u64, #[mysql(json, rename = "def")] definition: Bar, child: Option, }

[derive(Debug, serde::Deserialize, PartialEq, Eq)]

enum Bar { Left, Right, }

/// Returns the following row: /// /// /// +----+-----------+-------+ /// | id | def | child | /// +----+-----------+-------+ /// | 42 | '"Right"' | NULL | /// +----+-----------+-------+ /// fn get_row() -> Row { // ... }

asserteq!(Foo::TABLENAME, "Foos"); asserteq!(Foo::IDFIELD, "id"); asserteq!(Foo::DEFINITIONFIELD, "def"); asserteq!(Foo::CHILDFIELD, "child");

let foo = fromrow::(getrow()); assert_eq!(foo, Foo { id: 42, definition: Bar::Right, child: None }); ```

License

Licensed under either of * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.