Skip to content

Commit 3183413

Browse files
authored
Merge pull request #50 from launchbadge/rl-refactor-types
Refactor HasSqlType to provide an array of compatible types
2 parents 306ed97 + 6b22fb7 commit 3183413

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+523
-386
lines changed

sqlx-core/src/database.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use std::fmt::Display;
2+
13
use crate::arguments::Arguments;
24
use crate::connection::Connection;
35
use crate::row::Row;
4-
use crate::types::HasTypeMetadata;
6+
use crate::types::TypeInfo;
57

68
/// A database driver.
79
///
810
/// This trait encapsulates a complete driver implementation to a specific
911
/// database (e.g., MySQL, Postgres).
10-
pub trait Database: HasTypeMetadata + 'static {
12+
pub trait Database: 'static {
1113
/// The concrete `Connection` implementation for this database.
1214
type Connection: Connection<Database = Self>;
1315

@@ -16,4 +18,10 @@ pub trait Database: HasTypeMetadata + 'static {
1618

1719
/// The concrete `Row` implementation for this database.
1820
type Row: Row<Database = Self>;
21+
22+
/// The concrete `TypeInfo` implementation for this database.
23+
type TypeInfo: TypeInfo;
24+
25+
/// The Rust type of table identifiers for this database.
26+
type TableId: Display + Clone;
1927
}

sqlx-core/src/describe.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Types for returning SQL type information about queries.
22
3-
use crate::types::HasTypeMetadata;
43
use crate::Database;
54
use std::fmt::{self, Debug};
65

@@ -11,7 +10,7 @@ where
1110
DB: Database + ?Sized,
1211
{
1312
/// The expected types for the parameters of the query.
14-
pub param_types: Box<[<DB as HasTypeMetadata>::TypeId]>,
13+
pub param_types: Box<[DB::TypeInfo]>,
1514

1615
/// The type and table information, if any for the results of the query.
1716
pub result_columns: Box<[Column<DB>]>,
@@ -20,7 +19,7 @@ where
2019
impl<DB> Debug for Describe<DB>
2120
where
2221
DB: Database,
23-
<DB as HasTypeMetadata>::TypeId: Debug,
22+
DB::TypeInfo: Debug,
2423
Column<DB>: Debug,
2524
{
2625
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -38,21 +37,21 @@ where
3837
DB: Database + ?Sized,
3938
{
4039
pub name: Option<Box<str>>,
41-
pub table_id: Option<<DB as HasTypeMetadata>::TableId>,
42-
pub type_id: <DB as HasTypeMetadata>::TypeId,
40+
pub table_id: Option<DB::TableId>,
41+
pub type_info: DB::TypeInfo,
4342
}
4443

4544
impl<DB> Debug for Column<DB>
4645
where
4746
DB: Database + ?Sized,
48-
<DB as HasTypeMetadata>::TableId: Debug,
49-
<DB as HasTypeMetadata>::TypeId: Debug,
47+
DB::TableId: Debug,
48+
DB::TypeInfo: Debug,
5049
{
5150
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5251
f.debug_struct("Column")
5352
.field("name", &self.name)
5453
.field("table_id", &self.table_id)
55-
.field("type_id", &self.type_id)
54+
.field("type_id", &self.type_info)
5655
.finish()
5756
}
5857
}

sqlx-core/src/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ where
7474

7575
fn size_hint(&self) -> usize {
7676
if self.is_some() {
77-
mem::size_of::<T>()
77+
(*self).size_hint()
7878
} else {
7979
0
8080
}

sqlx-core/src/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub trait Executor {
6464
Box::pin(async move { s.try_next().await?.ok_or(crate::Error::NotFound) })
6565
}
6666

67-
/// Analyze the SQL query and report the inferred bind parameter types and returned columns.
67+
#[doc(hidden)]
6868
fn describe<'e, 'q: 'e>(
6969
&'e mut self,
7070
query: &'q str,

sqlx-core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ mod url;
2020

2121
#[macro_use]
2222
pub mod arguments;
23+
24+
#[doc(hidden)]
2325
pub mod decode;
26+
2427
pub mod describe;
2528
pub mod encode;
2629
pub mod pool;

sqlx-core/src/mysql/arguments.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::arguments::Arguments;
22
use crate::encode::{Encode, IsNull};
3-
use crate::mysql::types::MySqlTypeMetadata;
3+
use crate::mysql::types::MySqlTypeInfo;
44
use crate::mysql::MySql;
55
use crate::types::HasSqlType;
66

77
#[derive(Default)]
88
pub struct MySqlArguments {
9-
pub(crate) param_types: Vec<MySqlTypeMetadata>,
9+
pub(crate) param_types: Vec<MySqlTypeInfo>,
1010
pub(crate) params: Vec<u8>,
1111
pub(crate) null_bitmap: Vec<u8>,
1212
}
@@ -38,10 +38,10 @@ impl Arguments for MySqlArguments {
3838
Self::Database: HasSqlType<T>,
3939
T: Encode<Self::Database>,
4040
{
41-
let metadata = <MySql as HasSqlType<T>>::metadata();
41+
let type_id = <MySql as HasSqlType<T>>::type_info();
4242
let index = self.param_types.len();
4343

44-
self.param_types.push(metadata);
44+
self.param_types.push(type_id);
4545
self.null_bitmap.resize((index / 8) + 1, 0);
4646

4747
if let IsNull::Yes = value.encode_nullable(&mut self.params) {

sqlx-core/src/mysql/database.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ impl Database for MySql {
99
type Arguments = super::MySqlArguments;
1010

1111
type Row = super::MySqlRow;
12+
13+
type TypeInfo = super::MySqlTypeInfo;
14+
15+
type TableId = Box<str>;
1216
}
1317

1418
impl_into_arguments_for_database!(MySql);

sqlx-core/src/mysql/executor.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use crate::describe::{Column, Describe};
88
use crate::executor::Executor;
99
use crate::mysql::protocol::{
1010
Capabilities, ColumnCount, ColumnDefinition, ComQuery, ComStmtExecute, ComStmtPrepare,
11-
ComStmtPrepareOk, Cursor, Decode, EofPacket, OkPacket, Row, Type,
11+
ComStmtPrepareOk, Cursor, Decode, EofPacket, OkPacket, Row, TypeId,
1212
};
13-
use crate::mysql::{MySql, MySqlArguments, MySqlConnection, MySqlRow};
13+
use crate::mysql::{MySql, MySqlArguments, MySqlConnection, MySqlRow, MySqlTypeInfo};
1414

1515
enum Step {
1616
Command(u64),
@@ -48,14 +48,14 @@ impl MySqlConnection {
4848
}
4949
}
5050

51-
async fn receive_column_types(&mut self, count: usize) -> crate::Result<Box<[Type]>> {
52-
let mut columns: Vec<Type> = Vec::with_capacity(count);
51+
async fn receive_column_types(&mut self, count: usize) -> crate::Result<Box<[TypeId]>> {
52+
let mut columns: Vec<TypeId> = Vec::with_capacity(count);
5353

5454
for _ in 0..count {
5555
let column: ColumnDefinition =
5656
ColumnDefinition::decode(self.receive().await?.packet())?;
5757

58-
columns.push(column.r#type);
58+
columns.push(column.type_id);
5959
}
6060

6161
if count > 0 {
@@ -145,7 +145,7 @@ impl MySqlConnection {
145145
.await
146146
}
147147

148-
async fn step(&mut self, columns: &[Type], binary: bool) -> crate::Result<Option<Step>> {
148+
async fn step(&mut self, columns: &[TypeId], binary: bool) -> crate::Result<Option<Step>> {
149149
let capabilities = self.capabilities;
150150
ret_if_none!(self.try_receive().await?);
151151

@@ -244,7 +244,7 @@ impl MySqlConnection {
244244

245245
for _ in 0..prepare_ok.params {
246246
let param = ColumnDefinition::decode(self.receive().await?.packet())?;
247-
param_types.push(param.r#type.0);
247+
param_types.push(MySqlTypeInfo::from_column_def(&param));
248248
}
249249

250250
if prepare_ok.params > 0 {
@@ -254,11 +254,9 @@ impl MySqlConnection {
254254
for _ in 0..prepare_ok.columns {
255255
let column = ColumnDefinition::decode(self.receive().await?.packet())?;
256256
result_columns.push(Column::<MySql> {
257+
type_info: MySqlTypeInfo::from_column_def(&column),
257258
name: column.column_alias.or(column.column),
258-
259259
table_id: column.table_alias.or(column.table),
260-
261-
type_id: column.r#type.0,
262260
});
263261
}
264262

sqlx-core/src/mysql/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub use connection::MySqlConnection;
2020

2121
pub use error::MySqlError;
2222

23+
pub use types::MySqlTypeInfo;
24+
2325
pub use row::MySqlRow;
2426

2527
/// An alias for [`Pool`], specialized for **MySQL**.

sqlx-core/src/mysql/protocol/column_def.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use byteorder::LittleEndian;
22

33
use crate::io::Buf;
44
use crate::mysql::io::BufExt;
5-
use crate::mysql::protocol::{Decode, FieldFlags, Type};
5+
use crate::mysql::protocol::{Decode, FieldFlags, TypeId};
66

77
// https://dev.mysql.com/doc/dev/mysql-server/8.0.12/page_protocol_com_query_response_text_resultset_column_definition.html
88
// https://mariadb.com/kb/en/resultset/#column-definition-packet
@@ -20,7 +20,7 @@ pub struct ColumnDefinition {
2020

2121
pub max_size: u32,
2222

23-
pub r#type: Type,
23+
pub type_id: TypeId,
2424

2525
pub flags: FieldFlags,
2626

@@ -57,7 +57,7 @@ impl Decode for ColumnDefinition {
5757
let char_set = buf.get_u16::<LittleEndian>()?;
5858
let max_size = buf.get_u32::<LittleEndian>()?;
5959

60-
let r#type = buf.get_u8()?;
60+
let type_id = buf.get_u8()?;
6161
let flags = buf.get_u16::<LittleEndian>()?;
6262
let decimals = buf.get_u8()?;
6363

@@ -69,7 +69,7 @@ impl Decode for ColumnDefinition {
6969
column_alias,
7070
char_set,
7171
max_size,
72-
r#type: Type(r#type),
72+
type_id: TypeId(type_id),
7373
flags: FieldFlags::from_bits_truncate(flags),
7474
decimals,
7575
})

0 commit comments

Comments
 (0)