Skip to content

Commit 0e22fe3

Browse files
authored
Merge pull request diesel-rs#1001 from Fiedzia/mysql_types
Try to add Tinyint
2 parents ed3b2cb + 0bb67c1 commit 0e22fe3

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

diesel/src/mysql/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod backend;
22
mod connection;
3-
mod types;
43

54
pub mod query_builder;
5+
pub mod types;
66

77
pub use self::backend::{Mysql, MysqlType};
88
pub use self::connection::MysqlConnection;

diesel/src/mysql/types/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,29 @@
11
#[cfg(feature = "chrono")]
22
mod date_and_time;
33

4+
use byteorder::WriteBytesExt;
45
use mysql::{Mysql, MysqlType};
56
use std::error::Error as StdError;
67
use std::io::Write;
78
use types::{ToSql, IsNull, FromSql, HasSqlType};
89

10+
primitive_impls!(Tinyint -> (i8, mysql: (Tiny)));
11+
12+
impl ToSql<::types::Tinyint, Mysql> for i8 {
13+
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<StdError+Send+Sync>> {
14+
out.write_i8(*self)
15+
.map(|_| IsNull::No)
16+
.map_err(|e| Box::new(e) as Box<StdError+Send+Sync>)
17+
}
18+
}
19+
20+
impl FromSql<::types::Tinyint, Mysql> for i8 {
21+
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<StdError+Send+Sync>> {
22+
let bytes = not_none!(bytes);
23+
Ok(bytes[0] as i8)
24+
}
25+
}
26+
927
impl ToSql<::types::Bool, Mysql> for bool {
1028
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<StdError+Send+Sync>> {
1129
let int_value = if *self {

diesel/src/types/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ use std::io::Write;
5858
/// [bool]: https://doc.rust-lang.org/nightly/std/primitive.bool.html
5959
#[derive(Debug, Clone, Copy, Default)] pub struct Bool;
6060

61+
/// The tinyint SQL type. This is only available on MySQL.
62+
///
63+
/// ### [`ToSql`](/diesel/types/trait.ToSql.html) impls
64+
///
65+
/// - [`i8`][i8]
66+
///
67+
/// ### [`FromSql`](/diesel/types/trait.FromSql.html) impls
68+
///
69+
/// - [`i8`][i8]
70+
///
71+
/// [i8]: https://doc.rust-lang.org/nightly/std/primitive.i8.html
72+
#[derive(Debug, Clone, Copy, Default)] pub struct Tinyint;
73+
6174
/// The small integer SQL type.
6275
///
6376
/// ### [`ToSql`](/diesel/types/trait.ToSql.html) impls
@@ -276,6 +289,9 @@ pub type VarChar = Text;
276289
#[cfg(feature = "postgres")]
277290
pub use pg::types::sql_types::*;
278291

292+
#[cfg(feature = "mysql")]
293+
pub use mysql::types::*;
294+
279295
pub trait HasSqlType<ST>: TypeMetadata {
280296
fn metadata() -> Self::TypeMetadata;
281297

diesel_tests/tests/types_roundtrip.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ mod pg_types {
149149
mod mysql_types {
150150
use super::*;
151151

152+
test_round_trip!(i8_roundtrips, Tinyint, i8);
152153
test_round_trip!(naive_datetime_roundtrips, Timestamp, (i64, u32), mk_naive_datetime);
153154
test_round_trip!(naive_time_roundtrips, Time, (u32, u32), mk_naive_time);
154155
test_round_trip!(naive_date_roundtrips, Date, u32, mk_naive_date);

0 commit comments

Comments
 (0)