@@ -9,8 +9,10 @@ use std::fmt;
9
9
use std:: io;
10
10
use std:: ops:: Deref ;
11
11
use std:: slice;
12
+ use std:: sync:: Arc ;
13
+ use std:: marker:: PhantomData ;
12
14
13
- use { Result , RowsNew , LazyRowsNew , StatementInternals } ;
15
+ use { Result , RowsNew , LazyRowsNew , StatementInternals , StatementInfo } ;
14
16
use transaction:: Transaction ;
15
17
use types:: { FromSql , WrongType } ;
16
18
use stmt:: { Statement , Column } ;
@@ -33,23 +35,18 @@ impl<'a, T> Deref for MaybeOwned<'a, T> {
33
35
}
34
36
35
37
/// The resulting rows of a query.
36
- pub struct Rows < ' stmt > {
37
- stmt : MaybeOwned < ' stmt , Statement < ' stmt > > ,
38
+ pub struct Rows < ' compat > {
39
+ stmt_info : Arc < StatementInfo > ,
38
40
data : Vec < RowData > ,
41
+ _marker : PhantomData < & ' compat u8 >
39
42
}
40
43
41
- impl < ' a > RowsNew < ' a > for Rows < ' a > {
42
- fn new ( stmt : & ' a Statement < ' a > , data : Vec < RowData > ) -> Rows < ' a > {
44
+ impl RowsNew for Rows < ' static > {
45
+ fn new ( stmt : & Statement , data : Vec < RowData > ) -> Rows < ' static > {
43
46
Rows {
44
- stmt : MaybeOwned :: Borrowed ( stmt) ,
45
- data : data,
46
- }
47
- }
48
-
49
- fn new_owned ( stmt : Statement < ' a > , data : Vec < RowData > ) -> Rows < ' a > {
50
- Rows {
51
- stmt : MaybeOwned :: Owned ( stmt) ,
47
+ stmt_info : stmt. info ( ) . clone ( ) ,
52
48
data : data,
49
+ _marker : PhantomData ,
53
50
}
54
51
}
55
52
}
@@ -63,10 +60,10 @@ impl<'a> fmt::Debug for Rows<'a> {
63
60
}
64
61
}
65
62
66
- impl < ' stmt > Rows < ' stmt > {
63
+ impl < ' rows > Rows < ' rows > {
67
64
/// Returns a slice describing the columns of the `Rows`.
68
65
pub fn columns ( & self ) -> & [ Column ] {
69
- self . stmt . columns ( )
66
+ & self . stmt_info . columns [ .. ]
70
67
}
71
68
72
69
/// Returns the number of rows present.
@@ -86,15 +83,15 @@ impl<'stmt> Rows<'stmt> {
86
83
/// Panics if `idx` is out of bounds.
87
84
pub fn get < ' a > ( & ' a self , idx : usize ) -> Row < ' a > {
88
85
Row {
89
- stmt : & * self . stmt ,
86
+ stmt_info : & self . stmt_info ,
90
87
data : MaybeOwned :: Borrowed ( & self . data [ idx] ) ,
91
88
}
92
89
}
93
90
94
91
/// Returns an iterator over the `Row`s.
95
92
pub fn iter < ' a > ( & ' a self ) -> Iter < ' a > {
96
93
Iter {
97
- stmt : & * self . stmt ,
94
+ stmt_info : & self . stmt_info ,
98
95
iter : self . data . iter ( ) ,
99
96
}
100
97
}
@@ -111,7 +108,7 @@ impl<'a> IntoIterator for &'a Rows<'a> {
111
108
112
109
/// An iterator over `Row`s.
113
110
pub struct Iter < ' a > {
114
- stmt : & ' a Statement < ' a > ,
111
+ stmt_info : & ' a StatementInfo ,
115
112
iter : slice:: Iter < ' a , RowData > ,
116
113
}
117
114
@@ -121,7 +118,7 @@ impl<'a> Iterator for Iter<'a> {
121
118
fn next ( & mut self ) -> Option < Row < ' a > > {
122
119
self . iter . next ( ) . map ( |row| {
123
120
Row {
124
- stmt : & * self . stmt ,
121
+ stmt_info : self . stmt_info ,
125
122
data : MaybeOwned :: Borrowed ( row) ,
126
123
}
127
124
} )
@@ -136,7 +133,7 @@ impl<'a> DoubleEndedIterator for Iter<'a> {
136
133
fn next_back ( & mut self ) -> Option < Row < ' a > > {
137
134
self . iter . next_back ( ) . map ( |row| {
138
135
Row {
139
- stmt : & * self . stmt ,
136
+ stmt_info : self . stmt_info ,
140
137
data : MaybeOwned :: Borrowed ( row) ,
141
138
}
142
139
} )
@@ -147,14 +144,14 @@ impl<'a> ExactSizeIterator for Iter<'a> {}
147
144
148
145
/// A single result row of a query.
149
146
pub struct Row < ' a > {
150
- stmt : & ' a Statement < ' a > ,
147
+ stmt_info : & ' a StatementInfo ,
151
148
data : MaybeOwned < ' a , RowData > ,
152
149
}
153
150
154
151
impl < ' a > fmt:: Debug for Row < ' a > {
155
152
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
156
153
fmt. debug_struct ( "Row" )
157
- . field ( "statement" , self . stmt )
154
+ . field ( "statement" , self . stmt_info )
158
155
. finish ( )
159
156
}
160
157
}
@@ -172,7 +169,7 @@ impl<'a> Row<'a> {
172
169
173
170
/// Returns a slice describing the columns of the `Row`.
174
171
pub fn columns ( & self ) -> & [ Column ] {
175
- self . stmt . columns ( )
172
+ & self . stmt_info . columns [ .. ]
176
173
}
177
174
178
175
/// Retrieves the contents of a field of the row.
@@ -227,12 +224,12 @@ impl<'a> Row<'a> {
227
224
where I : RowIndex ,
228
225
T : FromSql
229
226
{
230
- let idx = match idx. idx ( self . stmt ) {
227
+ let idx = match idx. idx ( & self . stmt_info . columns ) {
231
228
Some ( idx) => idx,
232
229
None => return None ,
233
230
} ;
234
231
235
- let ty = self . stmt . columns ( ) [ idx] . type_ ( ) ;
232
+ let ty = self . stmt_info . columns [ idx] . type_ ( ) ;
236
233
if !<T as FromSql >:: accepts ( ty) {
237
234
return Some ( Err ( Error :: Conversion ( Box :: new ( WrongType :: new ( ty. clone ( ) ) ) ) ) ) ;
238
235
}
@@ -248,7 +245,7 @@ impl<'a> Row<'a> {
248
245
pub fn get_bytes < I > ( & self , idx : I ) -> Option < & [ u8 ] >
249
246
where I : RowIndex + fmt:: Debug
250
247
{
251
- match idx. idx ( self . stmt ) {
248
+ match idx. idx ( & self . stmt_info . columns ) {
252
249
Some ( idx) => self . data . get ( idx) ,
253
250
None => panic ! ( "invalid index {:?}" , idx) ,
254
251
}
@@ -259,13 +256,13 @@ impl<'a> Row<'a> {
259
256
pub trait RowIndex {
260
257
/// Returns the index of the appropriate column, or `None` if no such
261
258
/// column exists.
262
- fn idx ( & self , stmt : & Statement ) -> Option < usize > ;
259
+ fn idx ( & self , _ : & [ Column ] ) -> Option < usize > ;
263
260
}
264
261
265
262
impl RowIndex for usize {
266
263
#[ inline]
267
- fn idx ( & self , stmt : & Statement ) -> Option < usize > {
268
- if * self >= stmt . columns ( ) . len ( ) {
264
+ fn idx ( & self , columns : & [ Column ] ) -> Option < usize > {
265
+ if * self >= columns. len ( ) {
269
266
None
270
267
} else {
271
268
Some ( * self )
@@ -275,15 +272,15 @@ impl RowIndex for usize {
275
272
276
273
impl < ' a > RowIndex for & ' a str {
277
274
#[ inline]
278
- fn idx ( & self , stmt : & Statement ) -> Option < usize > {
279
- if let Some ( idx) = stmt . columns ( ) . iter ( ) . position ( |d| d. name ( ) == * self ) {
275
+ fn idx ( & self , columns : & [ Column ] ) -> Option < usize > {
276
+ if let Some ( idx) = columns. iter ( ) . position ( |d| d. name ( ) == * self ) {
280
277
return Some ( idx) ;
281
278
} ;
282
279
283
280
// FIXME ASCII-only case insensitivity isn't really the right thing to
284
281
// do. Postgres itself uses a dubious wrapper around tolower and JDBC
285
282
// uses the US locale.
286
- stmt . columns ( ) . iter ( ) . position ( |d| d. name ( ) . eq_ignore_ascii_case ( * self ) )
283
+ columns. iter ( ) . position ( |d| d. name ( ) . eq_ignore_ascii_case ( * self ) )
287
284
}
288
285
}
289
286
@@ -381,7 +378,7 @@ impl<'trans, 'stmt> FallibleIterator for LazyRows<'trans, 'stmt> {
381
378
. pop_front ( )
382
379
. map ( |r| {
383
380
Row {
384
- stmt : self . stmt ,
381
+ stmt_info : & * * self . stmt . info ( ) ,
385
382
data : MaybeOwned :: Owned ( r) ,
386
383
}
387
384
} ) ;
0 commit comments