@@ -4,62 +4,56 @@ use crate::statement::StatementHandle;
44use crate :: Sqlite ;
55use atoi:: atoi;
66use libsqlite3_sys:: SQLITE_OK ;
7- use std:: borrow :: Cow ;
7+ use std:: sync :: Arc ;
88
99pub ( crate ) use sqlx_core:: arguments:: * ;
1010use sqlx_core:: error:: BoxDynError ;
1111
1212#[ derive( Debug , Clone ) ]
13- pub enum SqliteArgumentValue < ' q > {
13+ pub enum SqliteArgumentValue {
1414 Null ,
15- Text ( Cow < ' q , str > ) ,
16- Blob ( Cow < ' q , [ u8 ] > ) ,
15+ Text ( Arc < String > ) ,
16+ TextSlice ( Arc < str > ) ,
17+ Blob ( Arc < Vec < u8 > > ) ,
1718 Double ( f64 ) ,
1819 Int ( i32 ) ,
1920 Int64 ( i64 ) ,
2021}
2122
2223#[ 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 ,
2526}
2627
27- impl < ' q > SqliteArguments < ' q > {
28+ #[ derive( Default , Debug , Clone ) ]
29+ pub struct SqliteArgumentsBuffer ( Vec < SqliteArgumentValue > ) ;
30+
31+ impl < ' q > SqliteArguments {
2832 pub ( crate ) fn add < T > ( & mut self , value : T ) -> Result < ( ) , BoxDynError >
2933 where
3034 T : Encode < ' q , Sqlite > ,
3135 {
32- let value_length_before_encoding = self . values . len ( ) ;
36+ let value_length_before_encoding = self . values . 0 . len ( ) ;
3337
3438 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 ) ,
3640 Ok ( IsNull :: No ) => { }
3741 Err ( error) => {
3842 // 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) ;
4044 return Err ( error) ;
4145 }
4246 } ;
4347
4448 Ok ( ( ) )
4549 }
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- }
5650}
5751
58- impl < ' q > Arguments < ' q > for SqliteArguments < ' q > {
52+ impl < ' q > Arguments < ' q > for SqliteArguments {
5953 type Database = Sqlite ;
6054
6155 fn reserve ( & mut self , len : usize , _size_hint : usize ) {
62- self . values . reserve ( len) ;
56+ self . values . 0 . reserve ( len) ;
6357 }
6458
6559 fn add < T > ( & mut self , value : T ) -> Result < ( ) , BoxDynError >
@@ -70,11 +64,11 @@ impl<'q> Arguments<'q> for SqliteArguments<'q> {
7064 }
7165
7266 fn len ( & self ) -> usize {
73- self . values . len ( )
67+ self . values . 0 . len ( )
7468 }
7569}
7670
77- impl SqliteArguments < ' _ > {
71+ impl SqliteArguments {
7872 pub ( super ) fn bind ( & self , handle : & mut StatementHandle , offset : usize ) -> Result < usize , Error > {
7973 let mut arg_i = offset;
8074 // for handle in &statement.handles {
@@ -103,7 +97,7 @@ impl SqliteArguments<'_> {
10397 arg_i
10498 } ;
10599
106- if n > self . values . len ( ) {
100+ if n > self . values . 0 . len ( ) {
107101 // SQLite treats unbound variables as NULL
108102 // we reproduce this here
109103 // If you are reading this and think this should be an error, open an issue and we can
@@ -113,32 +107,31 @@ impl SqliteArguments<'_> {
113107 break ;
114108 }
115109
116- self . values [ n - 1 ] . bind ( handle, param_i) ?;
110+ self . values . 0 [ n - 1 ] . bind ( handle, param_i) ?;
117111 }
118112
119113 Ok ( arg_i - offset)
120114 }
121115}
122116
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+ }
126122
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) ;
135125 }
126+ }
136127
128+ impl SqliteArgumentValue {
137129 fn bind ( & self , handle : & mut StatementHandle , i : usize ) -> Result < ( ) , Error > {
138130 use SqliteArgumentValue :: * ;
139131
140132 let status = match self {
141133 Text ( v) => handle. bind_text ( i, v) ,
134+ TextSlice ( v) => handle. bind_text ( i, v) ,
142135 Blob ( v) => handle. bind_blob ( i, v) ,
143136 Int ( v) => handle. bind_int ( i, * v) ,
144137 Int64 ( v) => handle. bind_int64 ( i, * v) ,
0 commit comments