mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-14 08:20:25 +00:00
Reduce some duplication in type parsing
This commit is contained in:
@@ -110,16 +110,16 @@ impl<'c> Row<'c> {
|
||||
if is_null {
|
||||
values.push(None);
|
||||
} else {
|
||||
let size = match columns[column_idx] {
|
||||
TypeId::TINY_INT => 1,
|
||||
TypeId::SMALL_INT => 2,
|
||||
TypeId::INT | TypeId::FLOAT => 4,
|
||||
TypeId::BIG_INT | TypeId::DOUBLE => 8,
|
||||
let (offset, size) = match columns[column_idx] {
|
||||
TypeId::TINY_INT => (0, 1),
|
||||
TypeId::SMALL_INT => (0, 2),
|
||||
TypeId::INT | TypeId::FLOAT => (0, 4),
|
||||
TypeId::BIG_INT | TypeId::DOUBLE => (0, 8),
|
||||
|
||||
TypeId::DATE => 5,
|
||||
TypeId::TIME => 1 + buffer[index] as usize,
|
||||
TypeId::DATE => (0, 5),
|
||||
TypeId::TIME => (0, 1 + buffer[index] as usize),
|
||||
|
||||
TypeId::TIMESTAMP | TypeId::DATETIME => 1 + buffer[index] as usize,
|
||||
TypeId::TIMESTAMP | TypeId::DATETIME => (0, 1 + buffer[index] as usize),
|
||||
|
||||
TypeId::TINY_BLOB
|
||||
| TypeId::MEDIUM_BLOB
|
||||
@@ -129,7 +129,7 @@ impl<'c> Row<'c> {
|
||||
| TypeId::VAR_CHAR => {
|
||||
let (len_size, len) = get_lenenc(&buffer[index..]);
|
||||
|
||||
len_size + len.unwrap_or_default()
|
||||
(len_size, len.unwrap_or_default())
|
||||
}
|
||||
|
||||
id => {
|
||||
@@ -137,8 +137,8 @@ impl<'c> Row<'c> {
|
||||
}
|
||||
};
|
||||
|
||||
values.push(Some(index..(index + size)));
|
||||
index += size;
|
||||
values.push(Some((index + offset)..(index + offset + size)));
|
||||
index += size + offset;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ use byteorder::LittleEndian;
|
||||
|
||||
use crate::decode::Decode;
|
||||
use crate::encode::Encode;
|
||||
use crate::mysql::io::{BufExt, BufMutExt};
|
||||
use crate::mysql::io::{BufMutExt};
|
||||
use crate::mysql::protocol::TypeId;
|
||||
use crate::mysql::types::MySqlTypeInfo;
|
||||
use crate::mysql::{MySql, MySqlValue};
|
||||
@@ -41,16 +41,7 @@ impl Encode<MySql> for Vec<u8> {
|
||||
impl<'de> Decode<'de, MySql> for Vec<u8> {
|
||||
fn decode(value: Option<MySqlValue<'de>>) -> crate::Result<Self> {
|
||||
match value.try_into()? {
|
||||
MySqlValue::Binary(mut buf) => {
|
||||
let len = buf
|
||||
.get_uint_lenenc::<LittleEndian>()
|
||||
.map_err(crate::Error::decode)?
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok((&buf[..(len as usize)]).to_vec())
|
||||
}
|
||||
|
||||
MySqlValue::Text(s) => Ok(s.to_vec()),
|
||||
MySqlValue::Binary(buf) | MySqlValue::Text(buf) => Ok(buf.to_vec()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,16 +49,7 @@ impl<'de> Decode<'de, MySql> for Vec<u8> {
|
||||
impl<'de> Decode<'de, MySql> for &'de [u8] {
|
||||
fn decode(value: Option<MySqlValue<'de>>) -> crate::Result<Self> {
|
||||
match value.try_into()? {
|
||||
MySqlValue::Binary(mut buf) => {
|
||||
let len = buf
|
||||
.get_uint_lenenc::<LittleEndian>()
|
||||
.map_err(crate::Error::decode)?
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok(&buf[..(len as usize)])
|
||||
}
|
||||
|
||||
MySqlValue::Text(s) => Ok(s),
|
||||
MySqlValue::Binary(buf) | MySqlValue::Text(buf) => Ok(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use byteorder::LittleEndian;
|
||||
|
||||
use crate::decode::Decode;
|
||||
use crate::encode::Encode;
|
||||
use crate::mysql::io::{BufExt, BufMutExt};
|
||||
use crate::mysql::io::{BufMutExt};
|
||||
use crate::mysql::protocol::TypeId;
|
||||
use crate::mysql::types::MySqlTypeInfo;
|
||||
use crate::mysql::{MySql, MySqlValue};
|
||||
@@ -44,16 +44,9 @@ impl Encode<MySql> for String {
|
||||
impl<'de> Decode<'de, MySql> for &'de str {
|
||||
fn decode(value: Option<MySqlValue<'de>>) -> crate::Result<Self> {
|
||||
match value.try_into()? {
|
||||
MySqlValue::Binary(mut buf) => {
|
||||
let len = buf
|
||||
.get_uint_lenenc::<LittleEndian>()
|
||||
.map_err(crate::Error::decode)?
|
||||
.unwrap_or_default();
|
||||
|
||||
from_utf8(&buf[..(len as usize)]).map_err(crate::Error::decode)
|
||||
MySqlValue::Binary(buf) | MySqlValue::Text(buf) => {
|
||||
from_utf8(buf).map_err(crate::Error::decode)
|
||||
}
|
||||
|
||||
MySqlValue::Text(s) => from_utf8(s).map_err(crate::Error::decode),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user