Skip to content

Commit afb1322

Browse files
committed
Remove the HasSqlType bound everywhere it's not required
Back in 0.4, this was a trait called `NativeSqlType`. I stuck it as a bound on virtually every place a SQL type was expected, since it served as nice documentation for "the set of types you can use here". It lived on things like `Query` and `Expression` as well. In 0.5 we added support for SQLite, and things got considerably more complex. This trait could no longer appear in places that the backend doesn't, and the "implementors" section is much more noisy and no longer useful documentation of "what are the SQL types". Ultimately this trait has a single concrete use, and that is to get the metadata associated with bind parameters. So `BindCollector` needs it, `AstPass` needs it on the methods that interact there, and `Bound` will need it in its `QueryFragment` impl. Virtually every other place that has that bound doesn't need it, and requiring it usually just means "oh I have to stick this additional useless bound here that I don't know or understand". Really the annoyance that made me want this is that you usually wouldn't even import the trait otherwise. One point to note here is that I was unable to remove this bound from the `Connection` trait. This is because the MySQL connection is actually using this bound to tell the C API what type we expect the results to be. If I change the code to do the same thing we do for `sql_query`, where we simply ask the database for the result types, we learn that MySQL does a lot of things differently than we expected: - 32 bit addition/subtraction returns a 64 bit integer - 32 bit multiplication/division returns numeric - 32 bit sum returns numeric - 32 bit bind parameters are converted to 64 bit So it turns out that we're relying on the client library to do implicit coercion in more places than we should be. Ultimately we're going to need to start doing those conversions ourselves if we ever want to dump the C API. Once we start doing that, we can remove `HasSqlType` from `Connection` (and thus also from `LoadQuery`), and then we can deprecate the `row_metadata` method from that trait.
1 parent 40c4e36 commit afb1322

21 files changed

Lines changed: 42 additions & 83 deletions

File tree

diesel/src/expression/functions/aggregate_folding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use backend::Backend;
22
use expression::Expression;
33
use query_builder::*;
44
use result::QueryResult;
5-
use types::{Foldable, HasSqlType};
5+
use types::Foldable;
66

77
macro_rules! fold_function {
88
($fn_name:ident, $type_name:ident, $operator:expr, $docs:expr) => {
@@ -31,7 +31,7 @@ macro_rules! fold_function {
3131

3232
impl<T, DB> QueryFragment<DB> for $type_name<T> where
3333
T: Expression + QueryFragment<DB>,
34-
DB: Backend + HasSqlType<T::SqlType>,
34+
DB: Backend,
3535
{
3636
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
3737
out.push_sql(concat!($operator, "("));

diesel/src/expression/functions/aggregate_ordering.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use backend::Backend;
22
use expression::Expression;
33
use query_builder::*;
44
use result::QueryResult;
5-
use types::{HasSqlType, IntoNullable, SqlOrd};
5+
use types::{IntoNullable, SqlOrd};
66

77
macro_rules! ord_function {
88
($fn_name:ident, $type_name:ident, $operator:expr, $docs:expr) => {
@@ -30,7 +30,7 @@ macro_rules! ord_function {
3030

3131
impl<T, DB> QueryFragment<DB> for $type_name<T> where
3232
T: Expression + QueryFragment<DB>,
33-
DB: Backend + HasSqlType<T::SqlType>,
33+
DB: Backend,
3434
{
3535
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
3636
out.push_sql(concat!($operator, "("));

diesel/src/expression/sql_literal.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use expression::*;
55
use query_builder::*;
66
use query_dsl::RunQueryDsl;
77
use result::QueryResult;
8-
use types::HasSqlType;
98

109
#[derive(Debug, Clone)]
1110
/// Returned by the [`sql()`] function.
@@ -32,7 +31,7 @@ impl<ST> Expression for SqlLiteral<ST> {
3231

3332
impl<ST, DB> QueryFragment<DB> for SqlLiteral<ST>
3433
where
35-
DB: Backend + HasSqlType<ST>,
34+
DB: Backend,
3635
{
3736
fn walk_ast(&self, mut out: AstPass<DB>) -> QueryResult<()> {
3837
out.unsafe_to_cache_prepared();

diesel/src/mysql/types/numeric.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ pub mod bigdecimal {
55
use std::error::Error;
66
use std::io::prelude::*;
77

8-
use mysql::{Mysql, MysqlType};
8+
use mysql::Mysql;
99

1010
use self::bigdecimal::BigDecimal;
1111

12-
use types::{self, FromSql, HasSqlType, IsNull, ToSql, ToSqlOutput};
12+
use types::{self, FromSql, IsNull, ToSql, ToSqlOutput};
1313

1414
impl ToSql<types::Numeric, Mysql> for BigDecimal {
1515
fn to_sql<W: Write>(
@@ -29,10 +29,4 @@ pub mod bigdecimal {
2929
.ok_or_else(|| Box::from(format!("{:?} is not valid decimal number ", bytes)))
3030
}
3131
}
32-
33-
impl HasSqlType<BigDecimal> for Mysql {
34-
fn metadata(_: &()) -> MysqlType {
35-
MysqlType::String
36-
}
37-
}
3832
}

diesel/src/pg/connection/cursor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use result::Error::DeserializationError;
44
use result::QueryResult;
55
use super::result::PgResult;
66
use super::row::PgNamedRow;
7-
use types::{FromSqlRow, HasSqlType};
7+
use types::FromSqlRow;
88

99
use std::marker::PhantomData;
1010

@@ -29,7 +29,6 @@ impl<ST, T> Cursor<ST, T> {
2929

3030
impl<ST, T> Iterator for Cursor<ST, T>
3131
where
32-
Pg: HasSqlType<ST>,
3332
T: Queryable<ST, Pg>,
3433
{
3534
type Item = QueryResult<T>;

diesel/src/pg/types/array.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ impl<T> SingleValue for Array<T> {}
2525
impl<T, ST> FromSql<Array<ST>, Pg> for Vec<T>
2626
where
2727
T: FromSql<ST, Pg>,
28-
Pg: HasSqlType<ST>,
2928
{
3029
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
3130
let mut bytes = not_none!(bytes);
@@ -67,9 +66,7 @@ use expression::bound::Bound;
6766

6867
macro_rules! array_as_expression {
6968
($ty:ty, $sql_type:ty) => {
70-
impl<'a, 'b, ST, T> AsExpression<$sql_type> for $ty where
71-
Pg: HasSqlType<ST>,
72-
{
69+
impl<'a, 'b, ST, T> AsExpression<$sql_type> for $ty {
7370
type Expression = Bound<$sql_type, Self>;
7471

7572
fn as_expression(self) -> Self::Expression {
@@ -128,7 +125,6 @@ where
128125

129126
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for [T]
130127
where
131-
Pg: HasSqlType<ST>,
132128
[T]: ToSql<Array<ST>, Pg>,
133129
{
134130
fn to_sql<W: Write>(
@@ -141,7 +137,6 @@ where
141137

142138
impl<ST, T> ToSql<Array<ST>, Pg> for Vec<T>
143139
where
144-
Pg: HasSqlType<ST>,
145140
[T]: ToSql<Array<ST>, Pg>,
146141
T: fmt::Debug,
147142
{
@@ -155,7 +150,6 @@ where
155150

156151
impl<ST, T> ToSql<Nullable<Array<ST>>, Pg> for Vec<T>
157152
where
158-
Pg: HasSqlType<ST>,
159153
Vec<T>: ToSql<Array<ST>, Pg>,
160154
{
161155
fn to_sql<W: Write>(

diesel/src/pg/types/ranges.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,38 @@ bitflags! {
2626
impl<T, ST> Queryable<Range<ST>, Pg> for (Bound<T>, Bound<T>)
2727
where
2828
T: FromSql<ST, Pg> + Queryable<ST, Pg>,
29-
Pg: HasSqlType<ST> + HasSqlType<Range<ST>>,
3029
{
3130
type Row = Self;
3231
fn build(row: Self) -> Self {
3332
row
3433
}
3534
}
3635

37-
impl<ST, T> AsExpression<Range<ST>> for (Bound<T>, Bound<T>)
38-
where
39-
Pg: HasSqlType<ST> + HasSqlType<Range<ST>>,
40-
{
36+
impl<ST, T> AsExpression<Range<ST>> for (Bound<T>, Bound<T>) {
4137
type Expression = SqlBound<Range<ST>, Self>;
4238

4339
fn as_expression(self) -> Self::Expression {
4440
SqlBound::new(self)
4541
}
4642
}
4743

48-
impl<'a, ST, T> AsExpression<Range<ST>> for &'a (Bound<T>, Bound<T>)
49-
where
50-
Pg: HasSqlType<Range<ST>>,
51-
{
44+
impl<'a, ST, T> AsExpression<Range<ST>> for &'a (Bound<T>, Bound<T>) {
5245
type Expression = SqlBound<Range<ST>, Self>;
5346

5447
fn as_expression(self) -> Self::Expression {
5548
SqlBound::new(self)
5649
}
5750
}
5851

59-
impl<ST, T> AsExpression<Nullable<Range<ST>>> for (Bound<T>, Bound<T>)
60-
where
61-
Pg: HasSqlType<Range<ST>>,
62-
{
52+
impl<ST, T> AsExpression<Nullable<Range<ST>>> for (Bound<T>, Bound<T>) {
6353
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
6454

6555
fn as_expression(self) -> Self::Expression {
6656
SqlBound::new(self)
6757
}
6858
}
6959

70-
impl<'a, ST, T> AsExpression<Nullable<Range<ST>>> for &'a (Bound<T>, Bound<T>)
71-
where
72-
Pg: HasSqlType<Range<ST>>,
73-
{
60+
impl<'a, ST, T> AsExpression<Nullable<Range<ST>>> for &'a (Bound<T>, Bound<T>) {
7461
type Expression = SqlBound<Nullable<Range<ST>>, Self>;
7562

7663
fn as_expression(self) -> Self::Expression {
@@ -81,7 +68,6 @@ where
8168
impl<T, ST> FromSqlRow<Range<ST>, Pg> for (Bound<T>, Bound<T>)
8269
where
8370
(Bound<T>, Bound<T>): FromSql<Range<ST>, Pg>,
84-
Pg: HasSqlType<ST> + HasSqlType<Range<ST>>,
8571
{
8672
fn build_from_row<R: ::row::Row<Pg>>(row: &mut R) -> Result<Self, Box<Error + Send + Sync>> {
8773
FromSql::<Range<ST>, Pg>::from_sql(row.take())
@@ -91,7 +77,6 @@ where
9177
impl<T, ST> FromSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
9278
where
9379
T: FromSql<ST, Pg>,
94-
Pg: HasSqlType<ST> + HasSqlType<Range<ST>>,
9580
{
9681
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
9782
let mut bytes = not_none!(bytes);
@@ -129,7 +114,6 @@ where
129114

130115
impl<ST, T> ToSql<Range<ST>, Pg> for (Bound<T>, Bound<T>)
131116
where
132-
Pg: HasSqlType<ST> + HasSqlType<Range<ST>>,
133117
T: ToSql<ST, Pg>,
134118
{
135119
fn to_sql<W: Write>(
@@ -178,7 +162,6 @@ where
178162

179163
impl<ST, T> ToSql<Nullable<Range<ST>>, Pg> for (Bound<T>, Bound<T>)
180164
where
181-
Pg: HasSqlType<Range<ST>>,
182165
(Bound<T>, Bound<T>): ToSql<Range<ST>, Pg>,
183166
{
184167
fn to_sql<W: Write>(

diesel/src/query_builder/select_statement/boxed.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use query_dsl::methods::*;
1414
use query_source::QuerySource;
1515
use query_source::joins::*;
1616
use result::QueryResult;
17-
use types::{BigInt, Bool, HasSqlType};
17+
use types::{BigInt, Bool};
1818

1919
#[allow(missing_debug_implementations)]
2020
pub struct BoxedSelectStatement<'a, ST, QS, DB> {
@@ -58,7 +58,6 @@ impl<'a, ST, QS, DB> BoxedSelectStatement<'a, ST, QS, DB> {
5858
impl<'a, ST, QS, DB> Query for BoxedSelectStatement<'a, ST, QS, DB>
5959
where
6060
DB: Backend,
61-
DB: HasSqlType<ST>,
6261
{
6362
type SqlType = ST;
6463
}
@@ -166,7 +165,7 @@ where
166165

167166
impl<'a, ST, QS, DB, Selection> SelectDsl<Selection> for BoxedSelectStatement<'a, ST, QS, DB>
168167
where
169-
DB: Backend + HasSqlType<Selection::SqlType>,
168+
DB: Backend,
170169
Selection: SelectableExpression<QS> + QueryFragment<DB> + 'a,
171170
{
172171
type Output = BoxedSelectStatement<'a, Selection::SqlType, QS, DB>;
@@ -187,7 +186,7 @@ where
187186

188187
impl<'a, ST, QS, DB, Predicate> FilterDsl<Predicate> for BoxedSelectStatement<'a, ST, QS, DB>
189188
where
190-
DB: Backend + HasSqlType<ST> + 'a,
189+
DB: Backend + 'a,
191190
Predicate: AppearsOnTable<QS, SqlType = Bool> + NonAggregate,
192191
Predicate: QueryFragment<DB> + 'a,
193192
{

diesel/src/query_source/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use backend::Backend;
1313
use expression::{Expression, NonAggregate, SelectableExpression};
1414
use query_builder::*;
1515
use row::NamedRow;
16-
use types::{FromSqlRow, HasSqlType};
16+
use types::FromSqlRow;
1717

1818
pub use self::joins::JoinTo;
1919
pub use self::peano_numbers::*;
@@ -108,7 +108,7 @@ pub use self::peano_numbers::*;
108108
/// # }
109109
pub trait Queryable<ST, DB>
110110
where
111-
DB: Backend + HasSqlType<ST>,
111+
DB: Backend,
112112
{
113113
/// The Rust type you'd like to map from.
114114
///
@@ -123,7 +123,7 @@ where
123123
//
124124
// impl<T, ST, DB> Queryable<ST, DB> for T
125125
// where
126-
// DB: Backend + HasSqlType<ST>,
126+
// DB: Backend,
127127
// T: FromSqlRow<ST, DB>,
128128
// {
129129
// type Row = Self;

diesel/src/row.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::error::Error;
44

55
use backend::Backend;
6-
use types::{FromSql, HasSqlType};
6+
use types::FromSql;
77

88
/// Represents a single database row.
99
/// Apps should not need to concern themselves with this trait.
@@ -48,7 +48,6 @@ pub trait NamedRow<DB: Backend> {
4848
/// this function is undefined.
4949
fn get<ST, T>(&self, column_name: &str) -> Result<T, Box<Error + Send + Sync>>
5050
where
51-
DB: HasSqlType<ST>,
5251
T: FromSql<ST, DB>,
5352
{
5453
let idx = self.index_of(column_name)

0 commit comments

Comments
 (0)