@@ -10,7 +10,7 @@ use postgres::types::{RawFromSql, ToSql, RawToSql, Type, Oid};
10
10
use { Array , ArrayBase , DimensionInfo } ;
11
11
12
12
macro_rules! check_types {
13
- ( $( $expected: pat) |+ , $actual : ident ) => (
13
+ ( $actual : ident , $ ( $expected: pat) ,+ ) => (
14
14
match $actual {
15
15
$( & $expected) |+ => { }
16
16
actual => return Err ( :: postgres:: Error :: WrongType ( actual. clone( ) ) )
@@ -19,10 +19,10 @@ macro_rules! check_types {
19
19
}
20
20
21
21
macro_rules! from_sql_impl {
22
- ( $( $oid: pat) |+ , $t : ty ) => {
22
+ ( $t : ty , $ ( $oid: pat) ,+ ) => {
23
23
impl :: postgres:: FromSql for Option <:: ArrayBase <Option <$t>>> {
24
24
fn from_sql( ty: & :: postgres:: Type , raw: Option <& [ u8 ] >) -> :: postgres:: Result <Self > {
25
- check_types!( $( $oid) |+ , ty ) ;
25
+ check_types!( ty , $( $oid) ,+ ) ;
26
26
27
27
match raw {
28
28
Some ( mut raw) => :: postgres:: types:: RawFromSql :: raw_from_sql( & mut raw) . map( Some ) ,
@@ -45,17 +45,17 @@ macro_rules! from_sql_impl {
45
45
}
46
46
47
47
macro_rules! to_sql_impl {
48
- ( $( $oid: pat) |+ , $t : ty ) => {
48
+ ( $t : ty , $ ( $oid: pat) ,+ ) => {
49
49
impl :: postgres:: ToSql for :: ArrayBase <Option <$t>> {
50
50
fn to_sql( & self , ty: & :: postgres:: Type ) -> :: postgres:: Result <Option <Vec <u8 >>> {
51
- check_types!( $( $oid) |+ , ty ) ;
51
+ check_types!( ty , $( $oid) ,+ ) ;
52
52
Ok ( Some ( :: impls:: raw_to_array( self , ty) ) )
53
53
}
54
54
}
55
55
56
56
impl :: postgres:: ToSql for Option <:: ArrayBase <Option <$t>>> {
57
57
fn to_sql( & self , ty: & :: postgres:: Type ) -> :: postgres:: Result <Option <Vec <u8 >>> {
58
- check_types!( $( $oid) |+ , ty ) ;
58
+ check_types!( ty , $( $oid) ,+ ) ;
59
59
match * self {
60
60
Some ( ref arr) => arr. to_sql( ty) ,
61
61
None => Ok ( None )
@@ -71,21 +71,21 @@ mod uuid;
71
71
72
72
impl < T > RawFromSql for ArrayBase < Option < T > > where T : RawFromSql {
73
73
fn raw_from_sql < R : Reader > ( raw : & mut R ) -> postgres:: Result < ArrayBase < Option < T > > > {
74
- let ndim = try!( raw. read_be_u32 ( ) ) as uint ;
74
+ let ndim = try!( raw. read_be_u32 ( ) ) as usize ;
75
75
let _has_null = try!( raw. read_be_i32 ( ) ) == 1 ;
76
76
let _element_type: Oid = try!( raw. read_be_u32 ( ) ) ;
77
77
78
78
let mut dim_info = Vec :: with_capacity ( ndim) ;
79
79
for _ in range ( 0 , ndim) {
80
80
dim_info. push ( DimensionInfo {
81
- len : try!( raw. read_be_u32 ( ) ) as uint ,
82
- lower_bound : try!( raw. read_be_i32 ( ) ) as int ,
81
+ len : try!( raw. read_be_u32 ( ) ) as usize ,
82
+ lower_bound : try!( raw. read_be_i32 ( ) ) as isize ,
83
83
} ) ;
84
84
}
85
85
let nele = if dim_info. len ( ) == 0 {
86
86
0
87
87
} else {
88
- dim_info. iter ( ) . map ( |info| info. len as uint ) . product ( )
88
+ dim_info. iter ( ) . map ( |info| info. len as usize ) . product ( )
89
89
} ;
90
90
91
91
let mut elements = Vec :: with_capacity ( nele) ;
@@ -94,7 +94,7 @@ impl<T> RawFromSql for ArrayBase<Option<T>> where T: RawFromSql {
94
94
if len < 0 {
95
95
elements. push ( None ) ;
96
96
} else {
97
- let mut limit = LimitReader :: new ( raw. by_ref ( ) , len as uint ) ;
97
+ let mut limit = LimitReader :: new ( raw. by_ref ( ) , len as usize ) ;
98
98
elements. push ( Some ( try!( RawFromSql :: raw_from_sql ( & mut limit) ) ) ) ;
99
99
if limit. limit ( ) != 0 {
100
100
return Err ( Error :: BadData ) ;
@@ -106,17 +106,17 @@ impl<T> RawFromSql for ArrayBase<Option<T>> where T: RawFromSql {
106
106
}
107
107
}
108
108
109
- from_sql_impl ! ( Type :: BoolArray , bool ) ;
110
- from_sql_impl ! ( Type :: ByteAArray , Vec <u8 >) ;
111
- from_sql_impl ! ( Type :: CharArray , i8 ) ;
112
- from_sql_impl ! ( Type :: Int2Array , i16 ) ;
113
- from_sql_impl ! ( Type :: Int4Array , i32 ) ;
114
- from_sql_impl ! ( Type :: TextArray | Type :: CharNArray | Type :: VarcharArray | Type :: NameArray , String ) ;
115
- from_sql_impl ! ( Type :: Int8Array , i64 ) ;
116
- from_sql_impl ! ( Type :: JsonArray , Json ) ;
117
- from_sql_impl ! ( Type :: Float4Array , f32 ) ;
118
- from_sql_impl ! ( Type :: Float8Array , f64 ) ;
119
- from_sql_impl ! ( Type :: TimestampArray | Type :: TimestampTZArray , Timespec ) ;
109
+ from_sql_impl ! ( bool , Type :: BoolArray ) ;
110
+ from_sql_impl ! ( Vec <u8 >, Type :: ByteAArray ) ;
111
+ from_sql_impl ! ( i8 , Type :: CharArray ) ;
112
+ from_sql_impl ! ( i16 , Type :: Int2Array ) ;
113
+ from_sql_impl ! ( i32 , Type :: Int4Array ) ;
114
+ from_sql_impl ! ( String , Type :: TextArray , Type :: CharNArray , Type :: VarcharArray , Type :: NameArray ) ;
115
+ from_sql_impl ! ( i64 , Type :: Int8Array ) ;
116
+ from_sql_impl ! ( Json , Type :: JsonArray ) ;
117
+ from_sql_impl ! ( f32 , Type :: Float4Array ) ;
118
+ from_sql_impl ! ( f64 , Type :: Float8Array ) ;
119
+ from_sql_impl ! ( Timespec , Type :: TimestampArray , Type :: TimestampTZArray ) ;
120
120
121
121
fn raw_to_array < T > ( array : & ArrayBase < Option < T > > , ty : & Type ) -> Vec < u8 > where T : RawToSql {
122
122
let mut buf = vec ! [ ] ;
@@ -147,17 +147,17 @@ fn raw_to_array<T>(array: &ArrayBase<Option<T>>, ty: &Type) -> Vec<u8> where T:
147
147
buf
148
148
}
149
149
150
- to_sql_impl ! ( Type :: BoolArray , bool ) ;
151
- to_sql_impl ! ( Type :: ByteAArray , Vec <u8 >) ;
152
- to_sql_impl ! ( Type :: CharArray , i8 ) ;
153
- to_sql_impl ! ( Type :: Int2Array , i16 ) ;
154
- to_sql_impl ! ( Type :: Int4Array , i32 ) ;
155
- to_sql_impl ! ( Type :: Int8Array , i64 ) ;
156
- to_sql_impl ! ( Type :: TextArray | Type :: CharNArray | Type :: VarcharArray | Type :: NameArray , String ) ;
157
- to_sql_impl ! ( Type :: Float4Array , f32 ) ;
158
- to_sql_impl ! ( Type :: Float8Array , f64 ) ;
159
- to_sql_impl ! ( Type :: JsonArray , Json ) ;
160
- to_sql_impl ! ( Type :: TimestampArray | Type :: TimestampTZArray , Timespec ) ;
150
+ to_sql_impl ! ( bool , Type :: BoolArray ) ;
151
+ to_sql_impl ! ( Vec <u8 >, Type :: ByteAArray ) ;
152
+ to_sql_impl ! ( i8 , Type :: CharArray ) ;
153
+ to_sql_impl ! ( i16 , Type :: Int2Array ) ;
154
+ to_sql_impl ! ( i32 , Type :: Int4Array ) ;
155
+ to_sql_impl ! ( i64 , Type :: Int8Array ) ;
156
+ to_sql_impl ! ( String , Type :: TextArray , Type :: CharNArray , Type :: VarcharArray , Type :: NameArray ) ;
157
+ to_sql_impl ! ( f32 , Type :: Float4Array ) ;
158
+ to_sql_impl ! ( f64 , Type :: Float8Array ) ;
159
+ to_sql_impl ! ( Json , Type :: JsonArray ) ;
160
+ to_sql_impl ! ( Timespec , Type :: TimestampArray , Type :: TimestampTZArray ) ;
161
161
162
162
#[ cfg( test) ]
163
163
mod test {
@@ -166,15 +166,15 @@ mod test {
166
166
use postgres:: { Connection , SslMode , FromSql , ToSql } ;
167
167
use ArrayBase ;
168
168
169
- fn test_type < T : PartialEq +FromSql +ToSql , S : fmt:: Show > ( sql_type : & str , checks : & [ ( T , S ) ] ) {
169
+ fn test_type < T : PartialEq +FromSql +ToSql , S : fmt:: String > ( sql_type : & str , checks : & [ ( T , S ) ] ) {
170
170
let conn = Connection :: connect ( "postgres://postgres@localhost" , & SslMode :: None ) . unwrap ( ) ;
171
171
for & ( ref val, ref repr) in checks. iter ( ) {
172
- let stmt = conn. prepare ( format ! ( "SELECT {}::{}" , * repr, sql_type) [ ] ) . unwrap ( ) ;
173
- let result = stmt. query ( & [ ] ) . unwrap ( ) . next ( ) . unwrap ( ) . get ( 0 u ) ;
172
+ let stmt = conn. prepare ( & format ! ( "SELECT {}::{}" , * repr, sql_type) [ ] ) . unwrap ( ) ;
173
+ let result = stmt. query ( & [ ] ) . unwrap ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
174
174
assert ! ( val == & result) ;
175
175
176
- let stmt = conn. prepare ( format ! ( "SELECT $1::{}" , sql_type) [ ] ) . unwrap ( ) ;
177
- let result = stmt. query ( & [ val] ) . unwrap ( ) . next ( ) . unwrap ( ) . get ( 0 u ) ;
176
+ let stmt = conn. prepare ( & format ! ( "SELECT $1::{}" , sql_type) [ ] ) . unwrap ( ) ;
177
+ let result = stmt. query ( & [ val] ) . unwrap ( ) . next ( ) . unwrap ( ) . get ( 0 ) ;
178
178
assert ! ( val == & result) ;
179
179
}
180
180
}
@@ -183,15 +183,15 @@ mod test {
183
183
( $name: expr, $v1: expr, $s1: expr, $v2: expr, $s2: expr, $v3: expr, $s3: expr) => ( {
184
184
185
185
let tests = & [ ( Some ( ArrayBase :: from_vec( vec!( Some ( $v1) , Some ( $v2) , None ) , 1 ) ) ,
186
- format!( "'{{{},{},NULL}}'" , $s1, $s2) . into_string ( ) ) ,
186
+ format!( "'{{{},{},NULL}}'" , $s1, $s2) ) ,
187
187
( None , "NULL" . to_string( ) ) ] ;
188
- test_type( format!( "{}[]" , $name) [ ] , tests) ;
188
+ test_type( & format!( "{}[]" , $name) [ ] , tests) ;
189
189
let mut a = ArrayBase :: from_vec( vec!( Some ( $v1) , Some ( $v2) ) , 0 ) ;
190
190
a. wrap( -1 ) ;
191
191
a. push_move( ArrayBase :: from_vec( vec!( None , Some ( $v3) ) , 0 ) ) ;
192
192
let tests = & [ ( Some ( a) , format!( "'[-1:0][0:1]={{{{{},{}}},{{NULL,{}}}}}'" ,
193
- $s1, $s2, $s3) . into_string ( ) ) ] ;
194
- test_type( format!( "{}[][]" , $name) [ ] , tests) ;
193
+ $s1, $s2, $s3) ) ] ;
194
+ test_type( & format!( "{}[][]" , $name) [ ] , tests) ;
195
195
} )
196
196
}
197
197
0 commit comments