@@ -19,14 +19,14 @@ pub enum SQLType {
19
19
Blob ( usize ) ,
20
20
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
21
21
Decimal ( Option < usize > , Option < usize > ) ,
22
+ /// Floating point with optional precision e.g. FLOAT(8)
23
+ Float ( Option < usize > ) ,
22
24
/// Small integer
23
25
SmallInt ,
24
26
/// Integer
25
27
Int ,
26
28
/// Big integer
27
29
BigInt ,
28
- /// Floating point with optional precision e.g. FLOAT(8)
29
- Float ( Option < usize > ) ,
30
30
/// Floating point e.g. REAL
31
31
Real ,
32
32
/// Double e.g. DOUBLE PRECISION
@@ -54,20 +54,8 @@ pub enum SQLType {
54
54
impl ToString for SQLType {
55
55
fn to_string ( & self ) -> String {
56
56
match self {
57
- SQLType :: Char ( size) => {
58
- if let Some ( size) = size {
59
- format ! ( "char({})" , size)
60
- } else {
61
- "char" . to_string ( )
62
- }
63
- }
64
- SQLType :: Varchar ( size) => {
65
- if let Some ( size) = size {
66
- format ! ( "character varying({})" , size)
67
- } else {
68
- "character varying" . to_string ( )
69
- }
70
- }
57
+ SQLType :: Char ( size) => format_type_with_optional_length ( "char" , size) ,
58
+ SQLType :: Varchar ( size) => format_type_with_optional_length ( "character varying" , size) ,
71
59
SQLType :: Uuid => "uuid" . to_string ( ) ,
72
60
SQLType :: Clob ( size) => format ! ( "clob({})" , size) ,
73
61
SQLType :: Binary ( size) => format ! ( "binary({})" , size) ,
@@ -76,22 +64,14 @@ impl ToString for SQLType {
76
64
SQLType :: Decimal ( precision, scale) => {
77
65
if let Some ( scale) = scale {
78
66
format ! ( "numeric({},{})" , precision. unwrap( ) , scale)
79
- } else if let Some ( precision) = precision {
80
- format ! ( "numeric({})" , precision)
81
67
} else {
82
- format ! ( "numeric" )
68
+ format_type_with_optional_length ( "numeric" , precision )
83
69
}
84
70
}
71
+ SQLType :: Float ( size) => format_type_with_optional_length ( "float" , size) ,
85
72
SQLType :: SmallInt => "smallint" . to_string ( ) ,
86
73
SQLType :: Int => "int" . to_string ( ) ,
87
74
SQLType :: BigInt => "bigint" . to_string ( ) ,
88
- SQLType :: Float ( size) => {
89
- if let Some ( size) = size {
90
- format ! ( "float({})" , size)
91
- } else {
92
- "float" . to_string ( )
93
- }
94
- }
95
75
SQLType :: Real => "real" . to_string ( ) ,
96
76
SQLType :: Double => "double" . to_string ( ) ,
97
77
SQLType :: Boolean => "boolean" . to_string ( ) ,
@@ -106,3 +86,11 @@ impl ToString for SQLType {
106
86
}
107
87
}
108
88
}
89
+
90
+ fn format_type_with_optional_length ( sql_type : & str , len : & Option < usize > ) -> String {
91
+ let mut s = sql_type. to_string ( ) ;
92
+ if let Some ( len) = len {
93
+ s += & format ! ( "({})" , len) ;
94
+ }
95
+ s
96
+ }
0 commit comments