Skip to content

Commit 96b748c

Browse files
authored
Merge pull request diesel-rs#657 from diesel-rs/sg-cleanup-type-lookup
Clean up type lookup for MySQL and PG
2 parents 80bfff1 + 5b31948 commit 96b748c

5 files changed

Lines changed: 83 additions & 27 deletions

File tree

diesel_infer_schema/src/inference.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ pub fn determine_column_type(
105105
#[cfg(feature = "sqlite")]
106106
InferConnection::Sqlite(_) => ::sqlite::determine_column_type(attr),
107107
#[cfg(feature = "postgres")]
108-
InferConnection::Pg(_) => ::information_schema::determine_column_type(attr),
108+
InferConnection::Pg(_) => ::pg::determine_column_type(attr),
109109
#[cfg(feature = "mysql")]
110-
InferConnection::Mysql(_) => ::information_schema::determine_column_type(attr),
110+
InferConnection::Mysql(_) => ::mysql::determine_column_type(attr),
111111
}
112112
}
113113

diesel_infer_schema/src/information_schema.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,6 @@ mod information_schema {
9696
}
9797
}
9898

99-
pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box<Error>> {
100-
let is_array = attr.type_name.starts_with('_');
101-
let tpe = if is_array {
102-
&attr.type_name[1..]
103-
} else {
104-
&attr.type_name
105-
};
106-
107-
let tpe = if let Some(idx) = tpe.find('(') {
108-
&tpe[..idx]
109-
} else {
110-
tpe
111-
};
112-
113-
Ok(ColumnType {
114-
path: vec!["diesel".into(), "types".into(), capitalize(tpe)],
115-
is_array: is_array,
116-
is_nullable: attr.nullable,
117-
})
118-
}
119-
120-
fn capitalize(name: &str) -> String {
121-
name[..1].to_uppercase() + &name[1..]
122-
}
123-
12499
pub fn get_table_data<Conn>(conn: &Conn, table: &TableData)
125100
-> QueryResult<Vec<ColumnInformation>> where
126101
Conn: Connection,

diesel_infer_schema/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ mod table_data;
1414

1515
#[cfg(feature="uses_information_schema")]
1616
mod information_schema;
17+
#[cfg(feature="mysql")]
18+
mod mysql;
19+
#[cfg(feature="postgres")]
20+
mod pg;
1721
#[cfg(feature="sqlite")]
1822
mod sqlite;
1923

diesel_infer_schema/src/mysql.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::error::Error;
2+
3+
use data_structures::*;
4+
5+
pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box<Error>> {
6+
let tpe = determine_type_name(&attr.type_name);
7+
8+
Ok(ColumnType {
9+
path: vec!["diesel".into(), "types".into(), capitalize(tpe)],
10+
is_array: false,
11+
is_nullable: attr.nullable,
12+
})
13+
}
14+
15+
fn determine_type_name(sql_type_name: &str) -> &str {
16+
if sql_type_name == "tinyint(1)" {
17+
"bool"
18+
} else if sql_type_name.starts_with("int") {
19+
"integer"
20+
} else if let Some(idx) = sql_type_name.find('(') {
21+
&sql_type_name[..idx]
22+
} else {
23+
&sql_type_name
24+
}
25+
}
26+
27+
fn capitalize(name: &str) -> String {
28+
name[..1].to_uppercase() + &name[1..]
29+
}
30+
31+
#[test]
32+
fn values_which_already_map_to_type_are_returned_unchanged() {
33+
assert_eq!("text", determine_type_name("text"));
34+
assert_eq!("integer", determine_type_name("integer"));
35+
assert_eq!("biginteger", determine_type_name("biginteger"));
36+
}
37+
38+
#[test]
39+
fn trailing_parenthesis_are_stripped() {
40+
assert_eq!("varchar", determine_type_name("varchar(255)"));
41+
assert_eq!("decimal", determine_type_name("decimal(10, 2)"));
42+
assert_eq!("float", determine_type_name("float(1)"));
43+
}
44+
45+
#[test]
46+
fn tinyint_is_bool_if_limit_1() {
47+
assert_eq!("bool", determine_type_name("tinyint(1)"));
48+
assert_eq!("tinyint", determine_type_name("tinyint(2)"));
49+
}
50+
51+
#[test]
52+
fn int_is_treated_as_integer() {
53+
assert_eq!("integer", determine_type_name("int"));
54+
assert_eq!("integer", determine_type_name("int(11)"));
55+
}

diesel_infer_schema/src/pg.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::error::Error;
2+
3+
use data_structures::*;
4+
5+
pub fn determine_column_type(attr: &ColumnInformation) -> Result<ColumnType, Box<Error>> {
6+
let is_array = attr.type_name.starts_with('_');
7+
let tpe = if is_array {
8+
&attr.type_name[1..]
9+
} else {
10+
&attr.type_name
11+
};
12+
13+
Ok(ColumnType {
14+
path: vec!["diesel".into(), "types".into(), capitalize(tpe)],
15+
is_array: is_array,
16+
is_nullable: attr.nullable,
17+
})
18+
}
19+
20+
fn capitalize(name: &str) -> String {
21+
name[..1].to_uppercase() + &name[1..]
22+
}

0 commit comments

Comments
 (0)