Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

added unsigned big integers support for mysql #335

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified db/test.db
Binary file not shown.
4 changes: 2 additions & 2 deletions src/ast/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ impl<'a> Select<'a> {
/// let (sql, params) = Sqlite::build(query)?;
///
/// assert_eq!("SELECT `users`.* FROM `users` LIMIT ?", sql);
/// assert_eq!(vec![Value::from(10)], params);
/// assert_eq!(vec![Value::from(10u64)], params);
/// # Ok(())
/// # }
pub fn limit(mut self, limit: usize) -> Self {
Expand All @@ -550,7 +550,7 @@ impl<'a> Select<'a> {
/// let (sql, params) = Sqlite::build(query)?;
///
/// assert_eq!("SELECT `users`.* FROM `users` LIMIT ? OFFSET ?", sql);
/// assert_eq!(vec![Value::from(-1), Value::from(10)], params);
/// assert_eq!(vec![Value::from(-1), Value::from(10u64)], params);
/// # Ok(())
/// # }
pub fn offset(mut self, offset: usize) -> Self {
Expand Down
28 changes: 26 additions & 2 deletions src/ast/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ where
/// compatibility.
#[derive(Debug, Clone, PartialEq)]
pub enum Value<'a> {
/// 64-bit unsigned integer.
UnsignedInteger(Option<u64>),
/// 64-bit signed integer.
Integer(Option<i64>),
/// 32-bit floating point.
Expand Down Expand Up @@ -107,6 +109,7 @@ impl<'a> fmt::Display for Params<'a> {
impl<'a> fmt::Display for Value<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let res = match self {
Value::UnsignedInteger(val) => val.map(|v| write!(f, "{}", v)),
Value::Integer(val) => val.map(|v| write!(f, "{}", v)),
Value::Float(val) => val.map(|v| write!(f, "{}", v)),
Value::Double(val) => val.map(|v| write!(f, "{}", v)),
Expand Down Expand Up @@ -155,6 +158,7 @@ impl<'a> fmt::Display for Value<'a> {
impl<'a> From<Value<'a>> for serde_json::Value {
fn from(pv: Value<'a>) -> Self {
let res = match pv {
Value::UnsignedInteger(i) => i.map(|i| serde_json::Value::Number(Number::from(i))),
Value::Integer(i) => i.map(|i| serde_json::Value::Number(Number::from(i))),
Value::Float(f) => f.map(|f| match Number::from_f64(f as f64) {
Some(number) => serde_json::Value::Number(number),
Expand Down Expand Up @@ -209,6 +213,14 @@ impl<'a> Value<'a> {
Value::Integer(Some(value.into()))
}

/// Creates a new unsigned integer value.
pub fn unsigned_integer<I>(value: I) -> Self
where
I: Into<u64>,
{
Value::UnsignedInteger(Some(value.into()))
}

/// Creates a new decimal value.
#[cfg(feature = "bigdecimal")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "bigdecimal")))]
Expand Down Expand Up @@ -321,6 +333,7 @@ impl<'a> Value<'a> {
/// `true` if the `Value` is null.
pub const fn is_null(&self) -> bool {
match self {
Value::UnsignedInteger(i) => i.is_none(),
Value::Integer(i) => i.is_none(),
Value::Float(i) => i.is_none(),
Value::Double(i) => i.is_none(),
Expand Down Expand Up @@ -412,7 +425,15 @@ impl<'a> Value<'a> {

/// `true` if the `Value` is an integer.
pub const fn is_integer(&self) -> bool {
matches!(self, Value::Integer(_))
matches!(self, Value::Integer(_) | Value::UnsignedInteger(_))
}

/// Returns an `u64` if the value is an integer, otherwise `None`.
pub const fn as_u64(&self) -> Option<u64> {
match self {
Value::UnsignedInteger(i) => *i,
_ => None,
}
}

/// Returns an `i64` if the value is an integer, otherwise `None`.
Expand Down Expand Up @@ -476,6 +497,7 @@ impl<'a> Value<'a> {
Value::Boolean(_) => true,
// For schemas which don't tag booleans
Value::Integer(Some(i)) if *i == 0 || *i == 1 => true,
Value::UnsignedInteger(Some(i)) if *i == 0 || *i == 1 => true,
_ => false,
}
}
Expand All @@ -486,6 +508,7 @@ impl<'a> Value<'a> {
Value::Boolean(b) => *b,
// For schemas which don't tag booleans
Value::Integer(Some(i)) if *i == 0 || *i == 1 => Some(*i == 1),
Value::UnsignedInteger(Some(i)) if *i == 0 || *i == 1 => Some(*i == 1),
_ => None,
}
}
Expand Down Expand Up @@ -610,10 +633,11 @@ impl<'a> Value<'a> {
}

value!(val: i64, Integer, val);
value!(val: u64, UnsignedInteger, val);
value!(val: bool, Boolean, val);
value!(val: &'a str, Text, val.into());
value!(val: String, Text, val.into());
value!(val: usize, Integer, i64::try_from(val).unwrap());
value!(val: usize, UnsignedInteger, u64::try_from(val).unwrap());
value!(val: i32, Integer, i64::try_from(val).unwrap());
value!(val: &'a [u8], Bytes, val.into());
value!(val: f64, Double, val);
Expand Down
1 change: 1 addition & 0 deletions src/connector/mssql/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn conv_params<'a>(params: &'a [Value<'a>]) -> crate::Result<Vec<&'a dyn ToS
impl<'a> ToSql for Value<'a> {
fn to_sql(&self) -> ColumnData<'_> {
match self {
Value::UnsignedInteger(val) => ColumnData::I64((*val).map(|v| v as i64)),
Value::Integer(val) => val.to_sql(),
Value::Float(val) => val.to_sql(),
Value::Double(val) => val.to_sql(),
Expand Down
38 changes: 22 additions & 16 deletions src/connector/mysql/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use mysql_async::{
self as my,
consts::{ColumnFlags, ColumnType},
};
use std::convert::TryFrom;

#[tracing::instrument(skip(params))]
pub fn conv_params<'a>(params: &[Value<'a>]) -> crate::Result<my::Params> {
Expand All @@ -22,6 +21,7 @@ pub fn conv_params<'a>(params: &[Value<'a>]) -> crate::Result<my::Params> {

for pv in params {
let res = match pv {
Value::UnsignedInteger(i) => i.map(my::Value::UInt),
Value::Integer(i) => i.map(my::Value::Int),
Value::Float(f) => f.map(my::Value::Float),
Value::Double(f) => f.map(my::Value::Double),
Expand Down Expand Up @@ -108,15 +108,25 @@ impl TypeIdentifier for my::Column {
fn is_integer(&self) -> bool {
use ColumnType::*;

matches!(
self.column_type(),
MYSQL_TYPE_TINY
| MYSQL_TYPE_SHORT
| MYSQL_TYPE_LONG
| MYSQL_TYPE_LONGLONG
| MYSQL_TYPE_YEAR
| MYSQL_TYPE_INT24
)
if self.flags().intersects(ColumnFlags::UNSIGNED_FLAG) && self.column_type() == MYSQL_TYPE_LONGLONG {
false
} else {
matches!(
self.column_type(),
MYSQL_TYPE_TINY
| MYSQL_TYPE_SHORT
| MYSQL_TYPE_LONG
| MYSQL_TYPE_LONGLONG
| MYSQL_TYPE_YEAR
| MYSQL_TYPE_INT24
)
}
}

fn is_unsigned_integer(&self) -> bool {
use ColumnType::*;

self.flags().intersects(ColumnFlags::UNSIGNED_FLAG) && self.column_type() == MYSQL_TYPE_LONGLONG
}

fn is_datetime(&self) -> bool {
Expand Down Expand Up @@ -245,12 +255,7 @@ impl TakeRow for my::Row {
my::Value::Bytes(b) if column.character_set() == 63 => Value::bytes(b),
my::Value::Bytes(s) => Value::text(String::from_utf8(s)?),
my::Value::Int(i) => Value::integer(i),
my::Value::UInt(i) => Value::integer(i64::try_from(i).map_err(|_| {
let msg = "Unsigned integers larger than 9_223_372_036_854_775_807 are currently not handled.";
let kind = ErrorKind::value_out_of_range(msg);

Error::builder(kind).build()
})?),
my::Value::UInt(i) => Value::unsigned_integer(i),
my::Value::Float(f) => Value::from(f),
my::Value::Double(f) => Value::from(f),
#[cfg(feature = "chrono")]
Expand Down Expand Up @@ -291,6 +296,7 @@ impl TakeRow for my::Row {
t if t.is_enum() => Value::Enum(None),
t if t.is_null() => Value::Integer(None),
t if t.is_integer() => Value::Integer(None),
t if t.is_unsigned_integer() => Value::UnsignedInteger(None),
t if t.is_float() => Value::Float(None),
t if t.is_double() => Value::Double(None),
t if t.is_text() => Value::Text(None),
Expand Down
7 changes: 7 additions & 0 deletions src/connector/postgres/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,15 @@ impl<'a> ToSql for Value<'a> {
(Value::Integer(integer), &PostgresType::TEXT) => {
integer.map(|integer| format!("{}", integer).to_sql(ty, out))
}
(Value::UnsignedInteger(integer), &PostgresType::TEXT) => {
integer.map(|integer| format!("{}", integer).to_sql(ty, out))
}
(Value::Integer(integer), &PostgresType::OID) => integer.map(|integer| (integer as u32).to_sql(ty, out)),
(Value::UnsignedInteger(integer), &PostgresType::OID) => {
integer.map(|integer| (integer as u32).to_sql(ty, out))
}
(Value::Integer(integer), _) => integer.map(|integer| (integer as i64).to_sql(ty, out)),
(Value::UnsignedInteger(integer), _) => integer.map(|integer| (integer as i64).to_sql(ty, out)),
(Value::Float(float), &PostgresType::FLOAT8) => float.map(|float| (float as f64).to_sql(ty, out)),
#[cfg(feature = "bigdecimal")]
(Value::Float(float), &PostgresType::NUMERIC) => float
Expand Down
5 changes: 5 additions & 0 deletions src/connector/sqlite/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl TypeIdentifier for Column<'_> {
)
}

fn is_unsigned_integer(&self) -> bool {
false
}

fn is_datetime(&self) -> bool {
matches!(
self.decl_type(),
Expand Down Expand Up @@ -222,6 +226,7 @@ impl<'a> ToColumnNames for SqliteRows<'a> {
impl<'a> ToSql for Value<'a> {
fn to_sql(&self) -> Result<ToSqlOutput, RusqlError> {
let value = match self {
Value::UnsignedInteger(integer) => integer.map(|integer| integer as i64).map(ToSqlOutput::from),
Value::Integer(integer) => integer.map(ToSqlOutput::from),
Value::Float(float) => float.map(|f| f as f64).map(ToSqlOutput::from),
Value::Double(double) => double.map(ToSqlOutput::from),
Expand Down
1 change: 1 addition & 0 deletions src/connector/type_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(crate) trait TypeIdentifier {
fn is_float(&self) -> bool;
fn is_double(&self) -> bool;
fn is_integer(&self) -> bool;
fn is_unsigned_integer(&self) -> bool;
fn is_datetime(&self) -> bool;
fn is_time(&self) -> bool;
fn is_date(&self) -> bool;
Expand Down
9 changes: 7 additions & 2 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
Value::Enum(None) => visitor.visit_none(),
Value::Integer(Some(i)) => visitor.visit_i64(i),
Value::Integer(None) => visitor.visit_none(),
Value::UnsignedInteger(Some(i)) => visitor.visit_u64(i),
Value::UnsignedInteger(None) => visitor.visit_none(),
Value::Boolean(Some(b)) => visitor.visit_bool(b),
Value::Boolean(None) => visitor.visit_none(),
Value::Char(Some(c)) => visitor.visit_char(c),
Expand Down Expand Up @@ -244,7 +246,10 @@ mod tests {

#[test]
fn deserialize_user() {
let row = make_row(vec![("id", Value::integer(12)), ("name", "Georgina".into())]);
let row = make_row(vec![
("id", Value::unsigned_integer(12u64)),
("name", "Georgina".into()),
]);
let user: User = from_row(row).unwrap();

assert_eq!(
Expand All @@ -260,7 +265,7 @@ mod tests {
#[test]
fn from_rows_works() {
let first_row = make_row(vec![
("id", Value::integer(12)),
("id", Value::unsigned_integer(12u64)),
("name", "Georgina".into()),
("bio", Value::Text(None)),
]);
Expand Down
24 changes: 20 additions & 4 deletions src/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,16 +1295,16 @@ async fn unsigned_integers_are_handled(api: &mut dyn TestApi) -> crate::Result<(

let insert = Insert::multi_into(&table, &["big"])
.values((2,))
.values((std::i64::MAX,));
.values((std::u64::MAX - 1,));
api.conn().insert(insert.into()).await?;

let select = Select::from_table(&table).column("big").order_by("id");
let roundtripped = api.conn().select(select).await?;

let expected = &[2, std::i64::MAX];
let actual: Vec<i64> = roundtripped
let expected = &[2, std::u64::MAX - 1];
let actual: Vec<u64> = roundtripped
.into_iter()
.map(|row| row.at(0).unwrap().as_i64().unwrap())
.map(|row| row.at(0).unwrap().as_u64().unwrap())
.collect();

assert_eq!(actual, expected);
Expand Down Expand Up @@ -2871,3 +2871,19 @@ async fn delete_comment(api: &mut dyn TestApi) -> crate::Result<()> {

Ok(())
}

#[test_each_connector(tags("mysql"))]
async fn unsigned_integer(api: &mut dyn TestApi) -> crate::Result<()> {
let table = api.create_table("id BIGINT UNSIGNED primary key").await?;

let insert = Insert::multi_into(&table, &["id"]).values((std::u64::MAX - 1,));

api.conn().query(insert.into()).await?;

let select = Select::from_table(&table).so_that("id".equals(std::u64::MAX - 1));
let result = api.conn().query(select.into()).await?;

assert!(!result.is_empty());

Ok(())
}
17 changes: 0 additions & 17 deletions src/tests/query/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,6 @@ async fn int_unsigned_negative_value_out_of_range(api: &mut dyn TestApi) -> crat
Ok(())
}

#[test_each_connector(tags("mysql"))]
async fn bigint_unsigned_positive_value_out_of_range(api: &mut dyn TestApi) -> crate::Result<()> {
let table = api
.create_table("id int4 auto_increment primary key, big bigint unsigned")
.await?;

let insert = format!(r#"INSERT INTO `{}` (`big`) VALUES (18446744073709551615)"#, table);
api.conn().execute_raw(&insert, &[]).await.unwrap();
let result = api.conn().select(Select::from_table(&table)).await;

assert!(
matches!(result.unwrap_err().kind(), ErrorKind::ValueOutOfRange { message } if message == "Unsigned integers larger than 9_223_372_036_854_775_807 are currently not handled.")
);

Ok(())
}

#[test_each_connector(tags("mysql", "mssql", "postgresql"))]
async fn length_mismatch(api: &mut dyn TestApi) -> crate::Result<()> {
let table = api.create_table("value varchar(3)").await?;
Expand Down
8 changes: 8 additions & 0 deletions src/tests/types/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ test_type!(bigint(
Value::integer(i64::MAX)
));

test_type!(bigint_unsigned(
mysql,
"bigint unsigned",
Value::UnsignedInteger(None),
Value::unsigned_integer(u64::MIN),
Value::unsigned_integer(u64::MAX)
));

#[cfg(feature = "bigdecimal")]
test_type!(decimal(
mysql,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/types/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod bigdecimal;

use crate::tests::test_api::*;
#[cfg(feature = "bigdecimal")]
#[allow(unused)]
use std::str::FromStr;

test_type!(boolean(
Expand Down
2 changes: 1 addition & 1 deletion src/tests/types/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::tests::test_api::sqlite_test_api;
use crate::tests::test_api::TestApi;
#[cfg(feature = "chrono")]
use crate::{ast::*, connector::Queryable};
#[cfg(feature = "bigdecimal")]
#[allow(unused)]
use std::str::FromStr;

test_type!(integer(
Expand Down
Loading