Skip to content

Commit

Permalink
Merge pull request jonhoo#14 from datafuse-extras/bump-deps
Browse files Browse the repository at this point in the history
Bump deps
  • Loading branch information
sundy-li authored Jan 15, 2022
2 parents 077fbca + 9ffce67 commit 7547dbc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 152 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ codecov = { repository = "jonhoo/msql-srv", branch = "master", service = "github
maintenance = { status = "experimental" }

[dependencies]
nom = "7.0.0-alpha2"
mysql_common = "0.27"
nom = "7.1.0"
mysql_common = { version = "0.28.0", features = ["chrono"] }
byteorder = "1.4"
chrono = "0.4"

[dev-dependencies]
mysql = "21"
mysql = "22"
postgres = "0.19.1"
mysql_async = "0.20.0"
slab = "0.4.3"
Expand Down
3 changes: 2 additions & 1 deletion src/value/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ mod tests {
use crate::value::utils::tests::WriteMysqlExt;
use crate::{Column, ColumnFlags, ColumnType};
use chrono::{self, TimeZone};
use myc::proto::MySerialize;
use std::time;

macro_rules! rt {
Expand All @@ -307,7 +308,7 @@ mod tests {
}

let v: $t = $v;
data.write_bin_value(&myc::value::Value::from(v)).unwrap();
myc::value::Value::from(v).serialize(&mut data);
assert_eq!(
Into::<$t>::into(Value::parse_from(&mut &data[..], $ct, !$sig).unwrap()),
v
Expand Down
37 changes: 35 additions & 2 deletions src/value/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ mod tests {
use super::ToMysqlValue;
use crate::myc::value;
use crate::myc::value::convert::from_value;
use crate::myc::value::Value;
use crate::value::utils::tests::*;
use crate::{Column, ColumnFlags, ColumnType};
use chrono::{self, TimeZone};
Expand All @@ -671,15 +672,26 @@ mod tests {
mod roundtrip_text {
use super::*;

use myc::{
io::ParseBuf,
proto::MyDeserialize,
value::{convert::FromValue, TextValue, ValueDeserializer},
};

macro_rules! rt {
($name:ident, $t:ty, $v:expr) => {
#[test]
fn $name() {
let mut data = Vec::new();
let v: $t = $v;
v.to_mysql_text(&mut data).unwrap();
let mut pb = ParseBuf(&mut data[..]);
assert_eq!(
from_value::<$t>(read_text_value(&mut &data[..]).unwrap()),
<$t>::from_value(
ValueDeserializer::<TextValue>::deserialize((), &mut pb)
.unwrap()
.0,
),
v
);
}
Expand Down Expand Up @@ -729,6 +741,12 @@ mod tests {
mod roundtrip_bin {
use super::*;

use myc::{
io::ParseBuf,
proto::MyDeserialize,
value::{convert::FromValue, BinValue, ValueDeserializer},
};

macro_rules! rt {
($name:ident, $t:ty, $v:expr, $ct:expr) => {
rt!($name, $t, $v, $ct, false);
Expand All @@ -750,8 +768,23 @@ mod tests {

let v: $t = $v;
v.to_mysql_bin(&mut data, &col).unwrap();
let mut pb = ParseBuf(&mut data[..]);
assert_eq!(
from_value::<$t>(read_bin_value(&mut &data[..], $ct, !$sig).unwrap()),
<$t>::from_value(
ValueDeserializer::<BinValue>::deserialize(
(
$ct,
if $sig {
ColumnFlags::empty()
} else {
ColumnFlags::UNSIGNED_FLAG
}
),
&mut pb
)
.unwrap()
.0,
),
v
);
}
Expand Down
143 changes: 0 additions & 143 deletions src/value/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#[cfg(test)]
#[allow(unused_imports)]
pub mod tests {
/// Non panicking Slice::split_at
macro_rules! split_at_or_err {
($reader:expr, $at:expr, $msg:expr) => {
if $reader.len() >= $at {
Ok($reader.split_at($at))
} else {
Err(io::Error::new(io::ErrorKind::UnexpectedEof, $msg))
}
};
}

/// Reads MySql's length-encoded string
#[macro_export]
macro_rules! read_lenenc_str {
Expand Down Expand Up @@ -119,136 +108,4 @@ pub mod tests {
}

impl<T> WriteMysqlExt for T where T: WriteBytesExt {}

/// Reads value in binary format.
pub fn read_bin_value(
input: &mut &[u8],
column_type: ColumnType,
unsigned: bool,
) -> io::Result<Value> {
read_bin(input, column_type, unsigned)
}

/// Reads value in text format.
pub fn read_text_value(input: &mut &[u8]) -> io::Result<Value> {
read_text(input)
}

fn read_text(input: &mut &[u8]) -> io::Result<Value> {
if input.is_empty() {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"Unexpected EOF while reading Value",
))
} else if input[0] == 0xfb {
let _ = input.read_u8();
Ok(Value::NULL)
} else {
Ok(Value::Bytes(read_lenenc_str!(input)?.into()))
}
}

fn read_bin(input: &mut &[u8], column_type: ColumnType, unsigned: bool) -> io::Result<Value> {
match column_type {
ColumnType::MYSQL_TYPE_STRING
| ColumnType::MYSQL_TYPE_VAR_STRING
| ColumnType::MYSQL_TYPE_BLOB
| ColumnType::MYSQL_TYPE_TINY_BLOB
| ColumnType::MYSQL_TYPE_MEDIUM_BLOB
| ColumnType::MYSQL_TYPE_LONG_BLOB
| ColumnType::MYSQL_TYPE_SET
| ColumnType::MYSQL_TYPE_ENUM
| ColumnType::MYSQL_TYPE_DECIMAL
| ColumnType::MYSQL_TYPE_VARCHAR
| ColumnType::MYSQL_TYPE_BIT
| ColumnType::MYSQL_TYPE_NEWDECIMAL
| ColumnType::MYSQL_TYPE_GEOMETRY
| ColumnType::MYSQL_TYPE_JSON => Ok(Bytes(read_lenenc_str!(input)?.into())),
ColumnType::MYSQL_TYPE_TINY => {
if unsigned {
Ok(Int(input.read_u8()?.into()))
} else {
Ok(Int(input.read_i8()?.into()))
}
}
ColumnType::MYSQL_TYPE_SHORT | ColumnType::MYSQL_TYPE_YEAR => {
if unsigned {
Ok(Int(input.read_u16::<LE>()?.into()))
} else {
Ok(Int(input.read_i16::<LE>()?.into()))
}
}
ColumnType::MYSQL_TYPE_LONG | ColumnType::MYSQL_TYPE_INT24 => {
if unsigned {
Ok(Int(input.read_u32::<LE>()?.into()))
} else {
Ok(Int(input.read_i32::<LE>()?.into()))
}
}
ColumnType::MYSQL_TYPE_LONGLONG => {
if unsigned {
Ok(UInt(input.read_u64::<LE>()?))
} else {
Ok(Int(input.read_i64::<LE>()?))
}
}
ColumnType::MYSQL_TYPE_FLOAT => Ok(Float(input.read_f32::<LE>()?)),
ColumnType::MYSQL_TYPE_DOUBLE => Ok(Double(input.read_f64::<LE>()?)),
ColumnType::MYSQL_TYPE_TIMESTAMP
| ColumnType::MYSQL_TYPE_DATE
| ColumnType::MYSQL_TYPE_DATETIME => {
let len = input.read_u8()?;
let mut year = 0u16;
let mut month = 0u8;
let mut day = 0u8;
let mut hour = 0u8;
let mut minute = 0u8;
let mut second = 0u8;
let mut micro_second = 0u32;
if len >= 4u8 {
year = input.read_u16::<LE>()?;
month = input.read_u8()?;
day = input.read_u8()?;
}
if len >= 7u8 {
hour = input.read_u8()?;
minute = input.read_u8()?;
second = input.read_u8()?;
}
if len == 11u8 {
micro_second = input.read_u32::<LE>()?;
}
Ok(Date(year, month, day, hour, minute, second, micro_second))
}
ColumnType::MYSQL_TYPE_TIME => {
let len = input.read_u8()?;
let mut is_negative = false;
let mut days = 0u32;
let mut hours = 0u8;
let mut minutes = 0u8;
let mut seconds = 0u8;
let mut micro_seconds = 0u32;
if len >= 8u8 {
is_negative = input.read_u8()? == 1u8;
days = input.read_u32::<LE>()?;
hours = input.read_u8()?;
minutes = input.read_u8()?;
seconds = input.read_u8()?;
}
if len == 12u8 {
micro_seconds = input.read_u32::<LE>()?;
}
Ok(Time(
is_negative,
days,
hours,
minutes,
seconds,
micro_seconds,
))
}
ColumnType::MYSQL_TYPE_NULL => Ok(NULL),
x => unimplemented!("Unsupported column type {:?}", x),
}
}
}
6 changes: 3 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,21 +410,21 @@ fn multi_result() {
let mut result = db
.query_iter("SELECT a FROM foo; SELECT a FROM foo")
.unwrap();
let mut set = result.next_set().unwrap().unwrap();
let mut set = result.iter().unwrap();
let row1: Vec<_> = set
.by_ref()
.filter_map(|row| row.unwrap().get::<i16, _>(0))
.collect();
assert_eq!(row1, vec![1024]);
drop(set);
let mut set = result.next_set().unwrap().unwrap();
let mut set = result.iter().unwrap();
let row2: Vec<_> = set
.by_ref()
.filter_map(|row| row.unwrap().get::<i16, _>(0))
.collect();
assert_eq!(row2, vec![1025]);
drop(set);
assert!(result.next_set().is_none());
assert!(result.iter().is_none());
})
}

Expand Down

0 comments on commit 7547dbc

Please sign in to comment.