@@ -36,33 +36,7 @@ internal class InternalConnectionPool(
3636 readOnly = false ,
3737 )
3838
39- connection.execSQL(" pragma journal_mode = WAL" )
40- connection.execSQL(" pragma journal_size_limit = ${6 * 1024 * 1024 } " )
41- connection.execSQL(" pragma busy_timeout = 30000" )
42- connection.execSQL(" pragma cache_size = ${50 * 1024 } " )
43-
44- if (readOnly) {
45- connection.execSQL(" pragma query_only = TRUE" )
46- }
47-
48- // Older versions of the SDK used to set up an empty schema and raise the user version to 1.
49- // Keep doing that for consistency.
50- if (! readOnly) {
51- val version =
52- connection.prepare(" pragma user_version" ).use {
53- require(it.step())
54- if (it.isNull(0 )) 0L else it.getLong(0 )
55- }
56- if (version < 1L ) {
57- connection.execSQL(" pragma user_version = 1" )
58- }
59-
60- // Also install a commit, rollback and update hooks in the core extension to implement
61- // the updates flow here (not all our driver implementations support hooks, so this is
62- // a more reliable fallback).
63- connection.execSQL(" select powersync_update_hooks('install');" )
64- }
65-
39+ connection.setupDefaultPragmas(readOnly)
6640 return connection
6741 }
6842
@@ -75,13 +49,10 @@ internal class InternalConnectionPool(
7549 } finally {
7650 // When we've leased a write connection, we may have to update table update flows
7751 // after users ran their custom statements.
78- writeConnection.prepare(" SELECT powersync_update_hooks('get')" ).use {
79- check(it.step())
80- val updatedTables = JsonUtil .json.decodeFromString<Set <String >>(it.getText(0 ))
81- if (updatedTables.isNotEmpty()) {
82- scope.launch {
83- tableUpdatesFlow.emit(updatedTables)
84- }
52+ val updatedTables = writeConnection.readPendingUpdates()
53+ if (updatedTables.isNotEmpty()) {
54+ scope.launch {
55+ tableUpdatesFlow.emit(updatedTables)
8556 }
8657 }
8758 }
@@ -106,3 +77,39 @@ internal class InternalConnectionPool(
10677 readPool.close()
10778 }
10879}
80+
81+ internal fun SQLiteConnection.setupDefaultPragmas (readOnly : Boolean ) {
82+ execSQL(" pragma journal_mode = WAL" )
83+ execSQL(" pragma journal_size_limit = ${6 * 1024 * 1024 } " )
84+ execSQL(" pragma busy_timeout = 30000" )
85+ execSQL(" pragma cache_size = ${50 * 1024 } " )
86+
87+ if (readOnly) {
88+ execSQL(" pragma query_only = TRUE" )
89+ }
90+
91+ // Older versions of the SDK used to set up an empty schema and raise the user version to 1.
92+ // Keep doing that for consistency.
93+ if (! readOnly) {
94+ val version =
95+ prepare(" pragma user_version" ).use {
96+ require(it.step())
97+ if (it.isNull(0 )) 0L else it.getLong(0 )
98+ }
99+ if (version < 1L ) {
100+ execSQL(" pragma user_version = 1" )
101+ }
102+
103+ // Also install a commit, rollback and update hooks in the core extension to implement
104+ // the updates flow here (not all our driver implementations support hooks, so this is
105+ // a more reliable fallback).
106+ execSQL(" select powersync_update_hooks('install');" )
107+ }
108+ }
109+
110+ internal fun SQLiteConnection.readPendingUpdates (): Set <String > =
111+ prepare(" SELECT powersync_update_hooks('get')" ).use {
112+ check(it.step())
113+ val updatedTables = JsonUtil .json.decodeFromString<Set <String >>(it.getText(0 ))
114+ updatedTables
115+ }
0 commit comments