@@ -4,62 +4,56 @@ use crate::statement::StatementHandle;
4
4
use crate :: Sqlite ;
5
5
use atoi:: atoi;
6
6
use libsqlite3_sys:: SQLITE_OK ;
7
- use std:: borrow :: Cow ;
7
+ use std:: sync :: Arc ;
8
8
9
9
pub ( crate ) use sqlx_core:: arguments:: * ;
10
10
use sqlx_core:: error:: BoxDynError ;
11
11
12
12
#[ derive( Debug , Clone ) ]
13
- pub enum SqliteArgumentValue < ' q > {
13
+ pub enum SqliteArgumentValue {
14
14
Null ,
15
- Text ( Cow < ' q , str > ) ,
16
- Blob ( Cow < ' q , [ u8 ] > ) ,
15
+ Text ( Arc < String > ) ,
16
+ TextSlice ( Arc < str > ) ,
17
+ Blob ( Arc < Vec < u8 > > ) ,
17
18
Double ( f64 ) ,
18
19
Int ( i32 ) ,
19
20
Int64 ( i64 ) ,
20
21
}
21
22
22
23
#[ derive( Default , Debug , Clone ) ]
23
- pub struct SqliteArguments < ' q > {
24
- pub ( crate ) values : Vec < SqliteArgumentValue < ' q > > ,
24
+ pub struct SqliteArguments {
25
+ pub ( crate ) values : SqliteArgumentsBuffer ,
25
26
}
26
27
27
- impl < ' q > SqliteArguments < ' q > {
28
+ #[ derive( Default , Debug , Clone ) ]
29
+ pub struct SqliteArgumentsBuffer ( Vec < SqliteArgumentValue > ) ;
30
+
31
+ impl < ' q > SqliteArguments {
28
32
pub ( crate ) fn add < T > ( & mut self , value : T ) -> Result < ( ) , BoxDynError >
29
33
where
30
34
T : Encode < ' q , Sqlite > ,
31
35
{
32
- let value_length_before_encoding = self . values . len ( ) ;
36
+ let value_length_before_encoding = self . values . 0 . len ( ) ;
33
37
34
38
match value. encode ( & mut self . values ) {
35
- Ok ( IsNull :: Yes ) => self . values . push ( SqliteArgumentValue :: Null ) ,
39
+ Ok ( IsNull :: Yes ) => self . values . 0 . push ( SqliteArgumentValue :: Null ) ,
36
40
Ok ( IsNull :: No ) => { }
37
41
Err ( error) => {
38
42
// reset the value buffer to its previous value if encoding failed so we don't leave a half-encoded value behind
39
- self . values . truncate ( value_length_before_encoding) ;
43
+ self . values . 0 . truncate ( value_length_before_encoding) ;
40
44
return Err ( error) ;
41
45
}
42
46
} ;
43
47
44
48
Ok ( ( ) )
45
49
}
46
-
47
- pub ( crate ) fn into_static ( self ) -> SqliteArguments < ' static > {
48
- SqliteArguments {
49
- values : self
50
- . values
51
- . into_iter ( )
52
- . map ( SqliteArgumentValue :: into_static)
53
- . collect ( ) ,
54
- }
55
- }
56
50
}
57
51
58
- impl < ' q > Arguments < ' q > for SqliteArguments < ' q > {
52
+ impl < ' q > Arguments < ' q > for SqliteArguments {
59
53
type Database = Sqlite ;
60
54
61
55
fn reserve ( & mut self , len : usize , _size_hint : usize ) {
62
- self . values . reserve ( len) ;
56
+ self . values . 0 . reserve ( len) ;
63
57
}
64
58
65
59
fn add < T > ( & mut self , value : T ) -> Result < ( ) , BoxDynError >
@@ -70,11 +64,11 @@ impl<'q> Arguments<'q> for SqliteArguments<'q> {
70
64
}
71
65
72
66
fn len ( & self ) -> usize {
73
- self . values . len ( )
67
+ self . values . 0 . len ( )
74
68
}
75
69
}
76
70
77
- impl SqliteArguments < ' _ > {
71
+ impl SqliteArguments {
78
72
pub ( super ) fn bind ( & self , handle : & mut StatementHandle , offset : usize ) -> Result < usize , Error > {
79
73
let mut arg_i = offset;
80
74
// for handle in &statement.handles {
@@ -103,7 +97,7 @@ impl SqliteArguments<'_> {
103
97
arg_i
104
98
} ;
105
99
106
- if n > self . values . len ( ) {
100
+ if n > self . values . 0 . len ( ) {
107
101
// SQLite treats unbound variables as NULL
108
102
// we reproduce this here
109
103
// If you are reading this and think this should be an error, open an issue and we can
@@ -113,32 +107,31 @@ impl SqliteArguments<'_> {
113
107
break ;
114
108
}
115
109
116
- self . values [ n - 1 ] . bind ( handle, param_i) ?;
110
+ self . values . 0 [ n - 1 ] . bind ( handle, param_i) ?;
117
111
}
118
112
119
113
Ok ( arg_i - offset)
120
114
}
121
115
}
122
116
123
- impl SqliteArgumentValue < ' _ > {
124
- fn into_static ( self ) -> SqliteArgumentValue < ' static > {
125
- use SqliteArgumentValue :: * ;
117
+ impl SqliteArgumentsBuffer {
118
+ #[ allow( dead_code) ] // clippy incorrectly reports this as unused
119
+ pub ( crate ) fn new ( values : Vec < SqliteArgumentValue > ) -> SqliteArgumentsBuffer {
120
+ Self ( values)
121
+ }
126
122
127
- match self {
128
- Null => Null ,
129
- Text ( text) => Text ( text. into_owned ( ) . into ( ) ) ,
130
- Blob ( blob) => Blob ( blob. into_owned ( ) . into ( ) ) ,
131
- Int ( v) => Int ( v) ,
132
- Int64 ( v) => Int64 ( v) ,
133
- Double ( v) => Double ( v) ,
134
- }
123
+ pub ( crate ) fn push ( & mut self , value : SqliteArgumentValue ) {
124
+ self . 0 . push ( value) ;
135
125
}
126
+ }
136
127
128
+ impl SqliteArgumentValue {
137
129
fn bind ( & self , handle : & mut StatementHandle , i : usize ) -> Result < ( ) , Error > {
138
130
use SqliteArgumentValue :: * ;
139
131
140
132
let status = match self {
141
133
Text ( v) => handle. bind_text ( i, v) ,
134
+ TextSlice ( v) => handle. bind_text ( i, v) ,
142
135
Blob ( v) => handle. bind_blob ( i, v) ,
143
136
Int ( v) => handle. bind_int ( i, * v) ,
144
137
Int64 ( v) => handle. bind_int64 ( i, * v) ,
0 commit comments