11namespace PowerSync . Common . MDSQLite ;
22
33using System . Data ;
4+ using System . Text ;
45using System . Threading . Tasks ;
56
67using Dapper ;
@@ -73,7 +74,8 @@ public void FlushUpdates()
7374 Emit ( new DBAdapterEvent { TablesUpdated = batchedUpdate } ) ;
7475 }
7576
76- private static List < string > PrepareQueryString ( ref string query , int parameterCount )
77+ // TODO only public for benchmarking
78+ public static List < string > PrepareQueryString ( ref string query , int parameterCount )
7779 {
7880 var parameterList = new List < string > ( ) ;
7981 if ( parameterCount == 0 )
@@ -88,21 +90,30 @@ private static List<string> PrepareQueryString(ref string query, int parameterCo
8890 }
8991
9092 // Replace `?` sequentially with named parameters
93+ var sb = new StringBuilder ( ) ;
94+ int lastPos = 0 ;
95+ int currentPos ;
9196 for ( int i = 0 ; i < parameterCount ; i ++ )
9297 {
98+ currentPos = query . IndexOf ( '?' , lastPos ) ;
99+
93100 string paramName = $ "@param{ i } ";
94101 parameterList . Add ( paramName ) ;
95102
96- int index = query . IndexOf ( '?' ) ;
97- if ( index == - 1 )
98- {
99- throw new ArgumentException ( "Mismatch between placeholders and parameters." ) ;
100- }
103+ sb . Append ( query , lastPos , currentPos - lastPos ) ;
104+ sb . Append ( paramName ) ;
101105
102- // TODO inefficient, but maybe not noticeably so
103- query = string . Concat ( query . Substring ( 0 , index ) , paramName , query . Substring ( index + 1 ) ) ;
106+ lastPos = currentPos + 1 ;
104107 }
105108
109+ // Append any remaining chars
110+ if ( lastPos < query . Length )
111+ {
112+ sb . Append ( query , lastPos , query . Length - lastPos ) ;
113+ }
114+
115+ query = sb . ToString ( ) ;
116+
106117 return parameterList ;
107118 }
108119
0 commit comments