1
1
//! AST types specific to CREATE/ALTER variants of [Statement]
2
2
//! (commonly referred to as Data Definition Language, or DDL)
3
- use super :: { DataType , Expr , Ident , ObjectName } ;
3
+ use super :: { display_comma_separated, DataType , Expr , Ident , ObjectName } ;
4
+ use std:: fmt;
4
5
5
6
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
6
7
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
@@ -11,11 +12,11 @@ pub enum AlterTableOperation {
11
12
DropConstraint { name : Ident } ,
12
13
}
13
14
14
- impl ToString for AlterTableOperation {
15
- fn to_string ( & self ) -> String {
15
+ impl fmt :: Display for AlterTableOperation {
16
+ fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
16
17
match self {
17
- AlterTableOperation :: AddConstraint ( c) => format ! ( "ADD {}" , c. to_string ( ) ) ,
18
- AlterTableOperation :: DropConstraint { name } => format ! ( "DROP CONSTRAINT {}" , name) ,
18
+ AlterTableOperation :: AddConstraint ( c) => write ! ( f , "ADD {}" , c) ,
19
+ AlterTableOperation :: DropConstraint { name } => write ! ( f , "DROP CONSTRAINT {}" , name) ,
19
20
}
20
21
}
21
22
}
@@ -46,36 +47,36 @@ pub enum TableConstraint {
46
47
} ,
47
48
}
48
49
49
- impl ToString for TableConstraint {
50
- fn to_string ( & self ) -> String {
50
+ impl fmt :: Display for TableConstraint {
51
+ fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
51
52
match self {
52
53
TableConstraint :: Unique {
53
54
name,
54
55
columns,
55
56
is_primary,
56
- } => format ! (
57
+ } => write ! (
58
+ f,
57
59
"{}{} ({})" ,
58
- format_constraint_name ( name) ,
60
+ display_constraint_name ( name) ,
59
61
if * is_primary { "PRIMARY KEY" } else { "UNIQUE" } ,
60
- columns . join ( ", " )
62
+ display_comma_separated ( columns )
61
63
) ,
62
64
TableConstraint :: ForeignKey {
63
65
name,
64
66
columns,
65
67
foreign_table,
66
68
referred_columns,
67
- } => format ! (
69
+ } => write ! (
70
+ f,
68
71
"{}FOREIGN KEY ({}) REFERENCES {}({})" ,
69
- format_constraint_name( name) ,
70
- columns. join( ", " ) ,
71
- foreign_table. to_string( ) ,
72
- referred_columns. join( ", " )
73
- ) ,
74
- TableConstraint :: Check { name, expr } => format ! (
75
- "{}CHECK ({})" ,
76
- format_constraint_name( name) ,
77
- expr. to_string( )
72
+ display_constraint_name( name) ,
73
+ display_comma_separated( columns) ,
74
+ foreign_table,
75
+ display_comma_separated( referred_columns)
78
76
) ,
77
+ TableConstraint :: Check { name, expr } => {
78
+ write ! ( f, "{}CHECK ({})" , display_constraint_name( name) , expr)
79
+ }
79
80
}
80
81
}
81
82
}
@@ -89,18 +90,13 @@ pub struct ColumnDef {
89
90
pub options : Vec < ColumnOptionDef > ,
90
91
}
91
92
92
- impl ToString for ColumnDef {
93
- fn to_string ( & self ) -> String {
94
- format ! (
95
- "{} {}{}" ,
96
- self . name,
97
- self . data_type. to_string( ) ,
98
- self . options
99
- . iter( )
100
- . map( |c| format!( " {}" , c. to_string( ) ) )
101
- . collect:: <Vec <_>>( )
102
- . join( "" )
103
- )
93
+ impl fmt:: Display for ColumnDef {
94
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
95
+ write ! ( f, "{} {}" , self . name, self . data_type) ?;
96
+ for option in & self . options {
97
+ write ! ( f, " {}" , option) ?;
98
+ }
99
+ Ok ( ( ) )
104
100
}
105
101
}
106
102
@@ -126,13 +122,9 @@ pub struct ColumnOptionDef {
126
122
pub option : ColumnOption ,
127
123
}
128
124
129
- impl ToString for ColumnOptionDef {
130
- fn to_string ( & self ) -> String {
131
- format ! (
132
- "{}{}" ,
133
- format_constraint_name( & self . name) ,
134
- self . option. to_string( )
135
- )
125
+ impl fmt:: Display for ColumnOptionDef {
126
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
127
+ write ! ( f, "{}{}" , display_constraint_name( & self . name) , self . option)
136
128
}
137
129
}
138
130
@@ -160,35 +152,39 @@ pub enum ColumnOption {
160
152
Check ( Expr ) ,
161
153
}
162
154
163
- impl ToString for ColumnOption {
164
- fn to_string ( & self ) -> String {
155
+ impl fmt :: Display for ColumnOption {
156
+ fn fmt ( & self , f : & mut fmt :: Formatter ) -> fmt :: Result {
165
157
use ColumnOption :: * ;
166
158
match self {
167
- Null => "NULL" . to_string ( ) ,
168
- NotNull => "NOT NULL" . to_string ( ) ,
169
- Default ( expr) => format ! ( "DEFAULT {}" , expr. to_string ( ) ) ,
159
+ Null => write ! ( f , "NULL" ) ,
160
+ NotNull => write ! ( f , "NOT NULL" ) ,
161
+ Default ( expr) => write ! ( f , "DEFAULT {}" , expr) ,
170
162
Unique { is_primary } => {
171
- if * is_primary {
172
- "PRIMARY KEY" . to_string ( )
173
- } else {
174
- "UNIQUE" . to_string ( )
175
- }
163
+ write ! ( f, "{}" , if * is_primary { "PRIMARY KEY" } else { "UNIQUE" } )
176
164
}
177
165
ForeignKey {
178
166
foreign_table,
179
167
referred_columns,
180
- } => format ! (
168
+ } => write ! (
169
+ f,
181
170
"REFERENCES {} ({})" ,
182
- foreign_table. to_string ( ) ,
183
- referred_columns . join ( ", " )
171
+ foreign_table,
172
+ display_comma_separated ( referred_columns )
184
173
) ,
185
- Check ( expr) => format ! ( "CHECK ({})" , expr. to_string ( ) , ) ,
174
+ Check ( expr) => write ! ( f , "CHECK ({})" , expr) ,
186
175
}
187
176
}
188
177
}
189
178
190
- fn format_constraint_name ( name : & Option < Ident > ) -> String {
191
- name. as_ref ( )
192
- . map ( |name| format ! ( "CONSTRAINT {} " , name) )
193
- . unwrap_or_default ( )
179
+ fn display_constraint_name < ' a > ( name : & ' a Option < Ident > ) -> impl fmt:: Display + ' a {
180
+ struct ConstraintName < ' a > ( & ' a Option < Ident > ) ;
181
+ impl < ' a > fmt:: Display for ConstraintName < ' a > {
182
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
183
+ if let Some ( name) = self . 0 {
184
+ write ! ( f, "CONSTRAINT {} " , name) ?;
185
+ }
186
+ Ok ( ( ) )
187
+ }
188
+ }
189
+ ConstraintName ( name)
194
190
}
0 commit comments