@@ -87,6 +87,7 @@ class SqlStorage final: public jsg::Object, private SqliteDatabase::Regulator {
8787 SqliteDatabase::Statement statement;
8888 kj::Maybe<jsg::JsRef<jsg::JsArray>> cachedColumnNames; // initialized on first exec
8989 kj::ListLink<CachedStatement> lruLink;
90+ uint useCount = 0 ;
9091
9192 CachedStatement (jsg::Lock& js,
9293 SqlStorage& sqlStorage,
@@ -190,7 +191,7 @@ class SqlStorage::Cursor final: public jsg::Object {
190191 double getRowsWritten ();
191192
192193 jsg::JsArray getColumnNames (jsg::Lock& js);
193- JSG_RESOURCE_TYPE (Cursor) {
194+ JSG_RESOURCE_TYPE (Cursor, CompatibilityFlags::Reader flags ) {
194195 JSG_METHOD (next);
195196 JSG_METHOD (toArray);
196197 JSG_METHOD (one);
@@ -210,6 +211,10 @@ class SqlStorage::Cursor final: public jsg::Object {
210211 one (): T;
211212 columnNames: string[];
212213 });
214+
215+ if (flags.getWorkerdExperimental ()) {
216+ JSG_READONLY_PROTOTYPE_PROPERTY (reusedCachedQueryForTest, getReusedCachedQueryForTest);
217+ }
213218 }
214219
215220 JSG_ITERATOR (RowIterator, rows, jsg::JsObject, jsg::Ref<Cursor>, rowIteratorNext);
@@ -226,6 +231,10 @@ class SqlStorage::Cursor final: public jsg::Object {
226231 tracker.trackField (" columnNames" , columnNames);
227232 }
228233
234+ bool getReusedCachedQueryForTest () {
235+ return reusedCachedQuery;
236+ }
237+
229238private:
230239 struct State {
231240 kj::Maybe<kj::Rc<CachedStatement>> cachedStatement;
@@ -251,6 +260,9 @@ class SqlStorage::Cursor final: public jsg::Object {
251260 // flag an error if the application tries to reuse the cursor.
252261 bool canceled = false ;
253262
263+ // Did we reuse a query from the query cache? Tracked for testing purposes.
264+ bool reusedCachedQuery = false ;
265+
254266 // Reference to a weak reference that might point back to this object. If so, null it out at
255267 // destruction. Used by Statement to invalidate past cursors when the statement is
256268 // executed again.
@@ -295,7 +307,9 @@ class SqlStorage::Statement final: public jsg::Object {
295307public:
296308 Statement (jsg::Lock& js, jsg::Ref<SqlStorage> sqlStorage, jsg::JsString query)
297309 : sqlStorage(kj::mv(sqlStorage)),
298- query (js.v8Isolate, query) {}
310+ // Internalize the string before constructing the statement so that it doesn't have to
311+ // re-lookup the internalized string for every invocation.
312+ query (js.v8Isolate, query.internalize(js)) {}
299313
300314 jsg::Ref<Cursor> run (jsg::Lock& js, jsg::Arguments<BindingValue> bindings);
301315
0 commit comments