@@ -4,6 +4,7 @@ use std::cell::{Cell, RefMut};
4
4
use std:: collections:: VecDeque ;
5
5
use std:: fmt;
6
6
use std:: io:: { self , Read , Write } ;
7
+ use std:: sync:: Arc ;
7
8
8
9
use error:: { Error , DbError } ;
9
10
use types:: { SessionInfo , Type , ToSql , IsNull } ;
@@ -13,24 +14,22 @@ use message::WriteMessage;
13
14
use util;
14
15
use rows:: { Rows , LazyRows } ;
15
16
use { read_rows, bad_response, Connection , Transaction , StatementInternals , Result , RowsNew } ;
16
- use { InnerConnection , SessionInfoNew , LazyRowsNew , DbErrorNew , ColumnNew } ;
17
+ use { InnerConnection , SessionInfoNew , LazyRowsNew , DbErrorNew , ColumnNew , StatementInfo } ;
17
18
18
19
/// A prepared statement.
19
20
pub struct Statement < ' conn > {
20
21
conn : & ' conn Connection ,
21
- name : String ,
22
- param_types : Vec < Type > ,
23
- columns : Vec < Column > ,
22
+ info : Arc < StatementInfo > ,
24
23
next_portal_id : Cell < u32 > ,
25
24
finished : bool ,
26
25
}
27
26
28
27
impl < ' a > fmt:: Debug for Statement < ' a > {
29
28
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
30
29
fmt. debug_struct ( "Statement" )
31
- . field ( "name" , & self . name )
32
- . field ( "parameter_types" , & self . param_types )
33
- . field ( "columns" , & self . columns )
30
+ . field ( "name" , & self . info . name )
31
+ . field ( "parameter_types" , & self . info . param_types )
32
+ . field ( "columns" , & self . info . columns )
34
33
. finish ( )
35
34
}
36
35
}
@@ -43,17 +42,13 @@ impl<'conn> Drop for Statement<'conn> {
43
42
44
43
impl < ' conn > StatementInternals < ' conn > for Statement < ' conn > {
45
44
fn new ( conn : & ' conn Connection ,
46
- name : String ,
47
- param_types : Vec < Type > ,
48
- columns : Vec < Column > ,
45
+ info : Arc < StatementInfo > ,
49
46
next_portal_id : Cell < u32 > ,
50
47
finished : bool )
51
48
-> Statement < ' conn > {
52
49
Statement {
53
50
conn : conn,
54
- name : name,
55
- param_types : param_types,
56
- columns : columns,
51
+ info : info,
57
52
next_portal_id : next_portal_id,
58
53
finished : finished,
59
54
}
@@ -76,7 +71,7 @@ impl<'conn> Statement<'conn> {
76
71
self . finished = true ;
77
72
let mut conn = self . conn . conn . borrow_mut ( ) ;
78
73
check_desync ! ( conn) ;
79
- conn. close_statement ( & self . name , b'S' )
74
+ conn. close_statement ( & self . info . name , b'S' )
80
75
} else {
81
76
Ok ( ( ) )
82
77
}
@@ -86,13 +81,13 @@ impl<'conn> Statement<'conn> {
86
81
let mut conn = self . conn . conn . borrow_mut ( ) ;
87
82
assert ! ( self . param_types( ) . len( ) == params. len( ) ,
88
83
"expected {} parameters but got {}" ,
89
- self . param_types. len( ) ,
84
+ self . param_types( ) . len( ) ,
90
85
params. len( ) ) ;
91
86
debug ! ( "executing statement {} with parameters: {:?}" ,
92
- self . name,
87
+ self . info . name,
93
88
params) ;
94
89
let mut values = vec ! [ ] ;
95
- for ( param, ty) in params. iter ( ) . zip ( self . param_types . iter ( ) ) {
90
+ for ( param, ty) in params. iter ( ) . zip ( self . param_types ( ) ) {
96
91
let mut buf = vec ! [ ] ;
97
92
match try!( param. to_sql_checked ( ty, & mut buf, & SessionInfo :: new ( & * conn) ) ) {
98
93
IsNull :: Yes => values. push ( None ) ,
@@ -102,7 +97,7 @@ impl<'conn> Statement<'conn> {
102
97
103
98
try!( conn. write_messages ( & [ Bind {
104
99
portal : portal_name,
105
- statement : & self . name ,
100
+ statement : & self . info . name ,
106
101
formats : & [ 1 ] ,
107
102
values : & values,
108
103
result_formats : & [ 1 ] ,
@@ -140,12 +135,12 @@ impl<'conn> Statement<'conn> {
140
135
141
136
/// Returns a slice containing the expected parameter types.
142
137
pub fn param_types ( & self ) -> & [ Type ] {
143
- & self . param_types
138
+ & self . info . param_types
144
139
}
145
140
146
141
/// Returns a slice describing the columns of the result of the query.
147
142
pub fn columns ( & self ) -> & [ Column ] {
148
- & self . columns
143
+ & self . info . columns
149
144
}
150
145
151
146
/// Executes the prepared statement, returning the number of rows modified.
@@ -279,7 +274,7 @@ impl<'conn> Statement<'conn> {
279
274
280
275
let id = self . next_portal_id . get ( ) ;
281
276
self . next_portal_id . set ( id + 1 ) ;
282
- let portal_name = format ! ( "{}p{}" , self . name, id) ;
277
+ let portal_name = format ! ( "{}p{}" , self . info . name, id) ;
283
278
284
279
self . inner_query ( & portal_name, row_limit, params) . map ( move |( data, more_rows) | {
285
280
LazyRows :: new ( self , data, portal_name, row_limit, more_rows, false , trans)
0 commit comments