@@ -14,6 +14,7 @@ static INT2OID: Oid = 21;
14
14
static INT4OID : Oid = 23 ;
15
15
static FLOAT4OID : Oid = 700 ;
16
16
static FLOAT8OID : Oid = 701 ;
17
+ static VARCHAROID : Oid = 1043 ;
17
18
18
19
pub enum Format {
19
20
Text = 0 ,
@@ -137,23 +138,24 @@ pub trait ToSql {
137
138
fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) ;
138
139
}
139
140
140
- macro_rules! to_str_impl(
141
- ( $t: ty) => (
142
- impl ToSql for $t {
143
- fn to_sql( & self , _ty: Oid ) -> ( Format , Option <~[ u8 ] >) {
144
- ( Text , Some ( self . to_str( ) . into_bytes( ) ) )
145
- }
141
+ macro_rules! check_oid(
142
+ ( $expected: ident, $actual: ident) => (
143
+ if $expected != $actual {
144
+ fail!( "Attempted to bind an invalid type. Expected Oid %? but got \
145
+ Oid %?", $expected, $actual) ;
146
146
}
147
147
)
148
148
)
149
149
150
150
macro_rules! to_option_impl(
151
- ( $t: ty) => (
151
+ ( $oid : ident , $ t: ty) => (
152
152
impl ToSql for Option <$t> {
153
153
fn to_sql( & self , ty: Oid ) -> ( Format , Option <~[ u8 ] >) {
154
+ check_oid!( $oid, ty)
155
+
154
156
match * self {
155
157
None => ( Text , None ) ,
156
- Some ( val) => val. to_sql( ty)
158
+ Some ( ref val) => val. to_sql( ty)
157
159
}
158
160
}
159
161
}
@@ -164,74 +166,47 @@ macro_rules! to_conversions_impl(
164
166
( $oid: ident, $t: ty, $f: ident) => (
165
167
impl ToSql for $t {
166
168
fn to_sql( & self , ty: Oid ) -> ( Format , Option <~[ u8 ] >) {
167
- if ty == $oid {
168
- let mut writer = MemWriter :: new( ) ;
169
- writer. $f( * self ) ;
170
- ( Binary , Some ( writer. inner( ) ) )
171
- } else {
172
- ( Text , Some ( self . to_str( ) . into_bytes( ) ) )
173
- }
169
+ check_oid!( $oid, ty)
170
+
171
+ let mut writer = MemWriter :: new( ) ;
172
+ writer. $f( * self ) ;
173
+ ( Binary , Some ( writer. inner( ) ) )
174
174
}
175
175
}
176
176
)
177
177
)
178
178
179
179
impl ToSql for bool {
180
180
fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
181
- if ty == BOOLOID {
182
- ( Binary , Some ( ~[ * self as u8 ] ) )
183
- } else {
184
- ( Text , Some ( self . to_str ( ) . into_bytes ( ) ) )
185
- }
181
+ check_oid ! ( BOOLOID , ty)
182
+ ( Binary , Some ( ~[ * self as u8 ] ) )
186
183
}
187
184
}
188
- to_option_impl ! ( bool )
185
+ to_option_impl ! ( BOOLOID , bool )
189
186
190
187
to_conversions_impl ! ( INT2OID , i16 , write_be_i16_)
191
- to_option_impl ! ( i16 )
188
+ to_option_impl ! ( INT2OID , i16 )
192
189
to_conversions_impl ! ( INT4OID , i32 , write_be_i32_)
193
- to_option_impl ! ( i32 )
190
+ to_option_impl ! ( INT4OID , i32 )
194
191
to_conversions_impl ! ( INT8OID , i64 , write_be_i64_)
195
- to_option_impl ! ( i64 )
192
+ to_option_impl ! ( INT8OID , i64 )
196
193
to_conversions_impl ! ( FLOAT4OID , f32 , write_be_f32_)
197
- to_option_impl ! ( f32 )
194
+ to_option_impl ! ( FLOAT4OID , f32 )
198
195
to_conversions_impl ! ( FLOAT8OID , f64 , write_be_f64_)
199
- to_option_impl ! ( f64 )
200
-
201
- to_str_impl ! ( int)
202
- to_option_impl ! ( int)
203
- to_str_impl ! ( i8 )
204
- to_option_impl ! ( i8 )
205
- to_str_impl ! ( uint)
206
- to_option_impl ! ( uint)
207
- to_str_impl ! ( u8 )
208
- to_option_impl ! ( u8 )
209
- to_str_impl ! ( u16 )
210
- to_option_impl ! ( u16 )
211
- to_str_impl ! ( u32 )
212
- to_option_impl ! ( u32 )
213
- to_str_impl ! ( u64 )
214
- to_option_impl ! ( u64 )
215
- to_str_impl ! ( float)
216
- to_option_impl ! ( float)
196
+ to_option_impl ! ( FLOAT8OID , f64 )
217
197
218
198
impl < ' self > ToSql for & ' self str {
219
- fn to_sql ( & self , _ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
199
+ fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
200
+ check_oid ! ( VARCHAROID , ty)
220
201
( Text , Some ( self . as_bytes ( ) . to_owned ( ) ) )
221
202
}
222
203
}
223
204
224
- impl ToSql for Option < ~str > {
225
- fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
226
- match * self {
227
- None => ( Text , None ) ,
228
- Some ( ref val) => val. to_sql ( ty)
229
- }
230
- }
231
- }
205
+ to_option_impl ! ( VARCHAROID , ~str )
232
206
233
207
impl < ' self > ToSql for Option < & ' self str > {
234
208
fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
209
+ check_oid ! ( VARCHAROID , ty)
235
210
match * self {
236
211
None => ( Text , None ) ,
237
212
Some ( val) => val. to_sql ( ty)
0 commit comments