@@ -14,6 +14,7 @@ static INT2OID: Oid = 21;
1414static INT4OID : Oid = 23 ;
1515static FLOAT4OID : Oid = 700 ;
1616static FLOAT8OID : Oid = 701 ;
17+ static VARCHAROID : Oid = 1043 ;
1718
1819pub enum Format {
1920 Text = 0 ,
@@ -137,23 +138,24 @@ pub trait ToSql {
137138 fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) ;
138139}
139140
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) ;
146146 }
147147 )
148148)
149149
150150macro_rules! to_option_impl(
151- ( $t: ty) => (
151+ ( $oid : ident , $ t: ty) => (
152152 impl ToSql for Option <$t> {
153153 fn to_sql( & self , ty: Oid ) -> ( Format , Option <~[ u8 ] >) {
154+ check_oid!( $oid, ty)
155+
154156 match * self {
155157 None => ( Text , None ) ,
156- Some ( val) => val. to_sql( ty)
158+ Some ( ref val) => val. to_sql( ty)
157159 }
158160 }
159161 }
@@ -164,74 +166,47 @@ macro_rules! to_conversions_impl(
164166 ( $oid: ident, $t: ty, $f: ident) => (
165167 impl ToSql for $t {
166168 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( ) ) )
174174 }
175175 }
176176 )
177177)
178178
179179impl ToSql for bool {
180180 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 ] ) )
186183 }
187184}
188- to_option_impl ! ( bool )
185+ to_option_impl ! ( BOOLOID , bool )
189186
190187to_conversions_impl ! ( INT2OID , i16 , write_be_i16_)
191- to_option_impl ! ( i16 )
188+ to_option_impl ! ( INT2OID , i16 )
192189to_conversions_impl ! ( INT4OID , i32 , write_be_i32_)
193- to_option_impl ! ( i32 )
190+ to_option_impl ! ( INT4OID , i32 )
194191to_conversions_impl ! ( INT8OID , i64 , write_be_i64_)
195- to_option_impl ! ( i64 )
192+ to_option_impl ! ( INT8OID , i64 )
196193to_conversions_impl ! ( FLOAT4OID , f32 , write_be_f32_)
197- to_option_impl ! ( f32 )
194+ to_option_impl ! ( FLOAT4OID , f32 )
198195to_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 )
217197
218198impl < ' 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)
220201 ( Text , Some ( self . as_bytes ( ) . to_owned ( ) ) )
221202 }
222203}
223204
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 )
232206
233207impl < ' self > ToSql for Option < & ' self str > {
234208 fn to_sql ( & self , ty : Oid ) -> ( Format , Option < ~[ u8 ] > ) {
209+ check_oid ! ( VARCHAROID , ty)
235210 match * self {
236211 None => ( Text , None ) ,
237212 Some ( val) => val. to_sql ( ty)
0 commit comments